dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / vat / plugin_api.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vat/vat.h>
16 #include <vnet/ip/ip.h>
17
18 uword
19 unformat_sw_if_index (unformat_input_t * input, va_list * args)
20 {
21   vat_main_t *vam = va_arg (*args, vat_main_t *);
22   u32 *result = va_arg (*args, u32 *);
23   u8 *if_name;
24   uword *p;
25
26   if (!unformat (input, "%s", &if_name))
27     return 0;
28
29   p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
30   if (p == 0)
31     return 0;
32   *result = p[0];
33   return 1;
34 }
35
36 /* Parse an IP4 address %d.%d.%d.%d. */
37 uword
38 unformat_ip4_address (unformat_input_t * input, va_list * args)
39 {
40   u8 *result = va_arg (*args, u8 *);
41   unsigned a[4];
42
43   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
44     return 0;
45
46   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
47     return 0;
48
49   result[0] = a[0];
50   result[1] = a[1];
51   result[2] = a[2];
52   result[3] = a[3];
53
54   return 1;
55 }
56
57 uword
58 unformat_ethernet_address (unformat_input_t * input, va_list * args)
59 {
60   u8 *result = va_arg (*args, u8 *);
61   u32 i, a[6];
62
63   if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
64                  &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
65     return 0;
66
67   /* Check range. */
68   for (i = 0; i < 6; i++)
69     if (a[i] >= (1 << 8))
70       return 0;
71
72   for (i = 0; i < 6; i++)
73     result[i] = a[i];
74
75   return 1;
76 }
77
78 /* Returns ethernet type as an int in host byte order. */
79 uword
80 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
81                                         va_list * args)
82 {
83   u16 *result = va_arg (*args, u16 *);
84   int type;
85
86   /* Numeric type. */
87   if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
88     {
89       if (type >= (1 << 16))
90         return 0;
91       *result = type;
92       return 1;
93     }
94   return 0;
95 }
96
97 /* Parse an IP6 address. */
98 uword
99 unformat_ip6_address (unformat_input_t * input, va_list * args)
100 {
101   ip6_address_t *result = va_arg (*args, ip6_address_t *);
102   u16 hex_quads[8];
103   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
104   uword c, n_colon, double_colon_index;
105
106   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
107   double_colon_index = ARRAY_LEN (hex_quads);
108   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
109     {
110       hex_digit = 16;
111       if (c >= '0' && c <= '9')
112         hex_digit = c - '0';
113       else if (c >= 'a' && c <= 'f')
114         hex_digit = c + 10 - 'a';
115       else if (c >= 'A' && c <= 'F')
116         hex_digit = c + 10 - 'A';
117       else if (c == ':' && n_colon < 2)
118         n_colon++;
119       else
120         {
121           unformat_put_input (input);
122           break;
123         }
124
125       /* Too many hex quads. */
126       if (n_hex_quads >= ARRAY_LEN (hex_quads))
127         return 0;
128
129       if (hex_digit < 16)
130         {
131           hex_quad = (hex_quad << 4) | hex_digit;
132
133           /* Hex quad must fit in 16 bits. */
134           if (n_hex_digits >= 4)
135             return 0;
136
137           n_colon = 0;
138           n_hex_digits++;
139         }
140
141       /* Save position of :: */
142       if (n_colon == 2)
143         {
144           /* More than one :: ? */
145           if (double_colon_index < ARRAY_LEN (hex_quads))
146             return 0;
147           double_colon_index = n_hex_quads;
148         }
149
150       if (n_colon > 0 && n_hex_digits > 0)
151         {
152           hex_quads[n_hex_quads++] = hex_quad;
153           hex_quad = 0;
154           n_hex_digits = 0;
155         }
156     }
157
158   if (n_hex_digits > 0)
159     hex_quads[n_hex_quads++] = hex_quad;
160
161   {
162     word i;
163
164     /* Expand :: to appropriate number of zero hex quads. */
165     if (double_colon_index < ARRAY_LEN (hex_quads))
166       {
167         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
168
169         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
170           hex_quads[n_zero + i] = hex_quads[i];
171
172         for (i = 0; i < n_zero; i++)
173           hex_quads[double_colon_index + i] = 0;
174
175         n_hex_quads = ARRAY_LEN (hex_quads);
176       }
177
178     /* Too few hex quads given. */
179     if (n_hex_quads < ARRAY_LEN (hex_quads))
180       return 0;
181
182     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
183       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
184
185     return 1;
186   }
187 }
188
189 u8 *
190 format_ip4_address (u8 * s, va_list * args)
191 {
192   u8 *a = va_arg (*args, u8 *);
193   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
194 }
195
196 u8 *
197 format_ethernet_address (u8 * s, va_list * args)
198 {
199   u8 *a = va_arg (*args, u8 *);
200
201   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
202                  a[0], a[1], a[2], a[3], a[4], a[5]);
203 }
204
205 void
206 vat_plugin_api_reference (void)
207 {
208 }
209
210 /*
211  * fd.io coding-style-patch-verification: ON
212  *
213  * Local Variables:
214  * eval: (c-set-style "gnu")
215  * End:
216  */