1b6712773e90eb8184807edf775af6ede32a86d3
[vpp.git] / vnet / vnet / ethernet / format.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 /*
16  * ethernet_format.c: ethernet formatting/parsing.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vnet/ethernet/ethernet.h>
42
43 u8 *
44 format_ethernet_address (u8 * s, va_list * args)
45 {
46   ethernet_main_t *em = &ethernet_main;
47   u8 *a = va_arg (*args, u8 *);
48
49   if (em->format_ethernet_address_16bit)
50     return format (s, "%02x%02x.%02x%02x.%02x%02x",
51                    a[0], a[1], a[2], a[3], a[4], a[5]);
52   else
53     return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
54                    a[0], a[1], a[2], a[3], a[4], a[5]);
55 }
56
57 u8 *
58 format_ethernet_type (u8 * s, va_list * args)
59 {
60   ethernet_type_t type = va_arg (*args, u32);
61   ethernet_main_t *em = &ethernet_main;
62   ethernet_type_info_t *t = ethernet_get_type_info (em, type);
63
64   if (t)
65     s = format (s, "%s", t->name);
66   else
67     s = format (s, "0x%04x", type);
68
69   return s;
70 }
71
72 u8 *
73 format_ethernet_vlan_tci (u8 * s, va_list * va)
74 {
75   u32 vlan_tci = va_arg (*va, u32);
76
77   u32 vid = (vlan_tci & 0xfff);
78   u32 cfi = (vlan_tci >> 12) & 1;
79   u32 pri = (vlan_tci >> 13);
80
81   s = format (s, "%d", vid);
82   if (pri != 0)
83     s = format (s, " priority %d", pri);
84   if (cfi != 0)
85     s = format (s, " cfi");
86
87   return s;
88 }
89
90 u8 *
91 format_ethernet_header_with_length (u8 * s, va_list * args)
92 {
93   ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
94   u32 max_header_bytes = va_arg (*args, u32);
95   ethernet_main_t *em = &ethernet_main;
96   ethernet_header_t *e = &m->ethernet;
97   ethernet_vlan_header_t *v;
98   ethernet_type_t type = clib_net_to_host_u16 (e->type);
99   ethernet_type_t vlan_type[ARRAY_LEN (m->vlan)];
100   u32 n_vlan = 0, i, header_bytes;
101   uword indent;
102
103   while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD)
104          && n_vlan < ARRAY_LEN (m->vlan))
105     {
106       vlan_type[n_vlan] = type;
107       v = m->vlan + n_vlan;
108       type = clib_net_to_host_u16 (v->type);
109       n_vlan++;
110     }
111
112   header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]);
113   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
114     return format (s, "ethernet header truncated");
115
116   indent = format_get_indent (s);
117
118   s = format (s, "%U: %U -> %U",
119               format_ethernet_type, type,
120               format_ethernet_address, e->src_address,
121               format_ethernet_address, e->dst_address);
122
123   for (i = 0; i < n_vlan; i++)
124     {
125       u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id);
126       if (*vlan_type == ETHERNET_TYPE_VLAN)
127         s = format (s, " 802.1q vlan %U", format_ethernet_vlan_tci, v);
128       else
129         s = format (s, " 802.1ad vlan %U", format_ethernet_vlan_tci, v);
130     }
131
132   if (max_header_bytes != 0 && header_bytes < max_header_bytes)
133     {
134       ethernet_type_info_t *ti;
135       vlib_node_t *node = 0;
136
137       ti = ethernet_get_type_info (em, type);
138       if (ti && ti->node_index != ~0)
139         node = vlib_get_node (em->vlib_main, ti->node_index);
140       if (node && node->format_buffer)
141         s = format (s, "\n%U%U",
142                     format_white_space, indent,
143                     node->format_buffer, (void *) m + header_bytes,
144                     max_header_bytes - header_bytes);
145     }
146
147   return s;
148 }
149
150 u8 *
151 format_ethernet_header (u8 * s, va_list * args)
152 {
153   ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
154   return format (s, "%U", format_ethernet_header_with_length, m, 0);
155 }
156
157 /* Parse X:X:X:X:X:X unix style ethernet address. */
158 static uword
159 unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
160 {
161   u8 *result = va_arg (*args, u8 *);
162   u32 i, a[6];
163
164   if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
165                  &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
166     return 0;
167
168   /* Check range. */
169   for (i = 0; i < ARRAY_LEN (a); i++)
170     if (a[i] >= (1 << 8))
171       return 0;
172
173   for (i = 0; i < ARRAY_LEN (a); i++)
174     result[i] = a[i];
175
176   return 1;
177 }
178
179 /* Parse X.X.X cisco style ethernet address. */
180 static uword
181 unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
182 {
183   u8 *result = va_arg (*args, u8 *);
184   u32 i, a[3];
185
186   if (!unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
187     return 0;
188
189   /* Check range. */
190   for (i = 0; i < ARRAY_LEN (a); i++)
191     if (a[i] >= (1 << 16))
192       return 0;
193
194   result[0] = (a[0] >> 8) & 0xff;
195   result[1] = (a[0] >> 0) & 0xff;
196   result[2] = (a[1] >> 8) & 0xff;
197   result[3] = (a[1] >> 0) & 0xff;
198   result[4] = (a[2] >> 8) & 0xff;
199   result[5] = (a[2] >> 0) & 0xff;
200
201   return 1;
202 }
203
204 /* Parse ethernet address; accept either unix or style addresses. */
205 uword
206 unformat_ethernet_address (unformat_input_t * input, va_list * args)
207 {
208   u8 *result = va_arg (*args, u8 *);
209   return (unformat_user (input, unformat_ethernet_address_unix, result)
210           || unformat_user (input, unformat_ethernet_address_cisco, result));
211 }
212
213 /* Returns ethernet type as an int in host byte order. */
214 uword
215 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
216                                         va_list * args)
217 {
218   u16 *result = va_arg (*args, u16 *);
219   ethernet_main_t *em = &ethernet_main;
220   int type, i;
221
222   /* Numeric type. */
223   if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
224     {
225       if (type >= (1 << 16))
226         return 0;
227       *result = type;
228       return 1;
229     }
230
231   /* Named type. */
232   if (unformat_user (input, unformat_vlib_number_by_name,
233                      em->type_info_by_name, &i))
234     {
235       ethernet_type_info_t *ti = vec_elt_at_index (em->type_infos, i);
236       *result = ti->type;
237       return 1;
238     }
239
240   return 0;
241 }
242
243 uword
244 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
245                                        va_list * args)
246 {
247   u16 *result = va_arg (*args, u16 *);
248   if (!unformat_user (input, unformat_ethernet_type_host_byte_order, result))
249     return 0;
250
251   *result = clib_host_to_net_u16 ((u16) * result);
252   return 1;
253 }
254
255 uword
256 unformat_ethernet_header (unformat_input_t * input, va_list * args)
257 {
258   u8 **result = va_arg (*args, u8 **);
259   ethernet_max_header_t _m, *m = &_m;
260   ethernet_header_t *e = &m->ethernet;
261   u16 type;
262   u32 n_vlan;
263
264   if (!unformat (input, "%U: %U -> %U",
265                  unformat_ethernet_type_host_byte_order, &type,
266                  unformat_ethernet_address, &e->src_address,
267                  unformat_ethernet_address, &e->dst_address))
268     return 0;
269
270   n_vlan = 0;
271   while (unformat (input, "vlan"))
272     {
273       u32 id, priority;
274
275       if (!unformat_user (input, unformat_vlib_number, &id)
276           || id >= ETHERNET_N_VLAN)
277         return 0;
278
279       if (unformat (input, "priority %d", &priority))
280         {
281           if (priority >= 8)
282             return 0;
283           id |= priority << 13;
284         }
285
286       if (unformat (input, "cfi"))
287         id |= 1 << 12;
288
289       /* Too many vlans given. */
290       if (n_vlan >= ARRAY_LEN (m->vlan))
291         return 0;
292
293       m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
294       n_vlan++;
295     }
296
297   if (n_vlan == 0)
298     e->type = clib_host_to_net_u16 (type);
299   else
300     {
301       int i;
302
303       e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
304       for (i = 0; i < n_vlan - 1; i++)
305         m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
306       m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type);
307     }
308
309   /* Add header to result. */
310   {
311     void *p;
312     u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
313
314     vec_add2 (*result, p, n_bytes);
315     clib_memcpy (p, m, n_bytes);
316   }
317
318   return 1;
319 }
320
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */