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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ethernet_format.c: ethernet formatting/parsing.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vnet/ethernet/ethernet.h>
44 format_ethernet_address (u8 * s, va_list * args)
46 ethernet_main_t *em = ðernet_main;
47 u8 *a = va_arg (*args, u8 *);
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]);
53 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
54 a[0], a[1], a[2], a[3], a[4], a[5]);
58 format_mac_address (u8 * s, va_list * args)
60 return (format_ethernet_address (s, args));
64 format_ethernet_type (u8 * s, va_list * args)
66 ethernet_type_t type = va_arg (*args, u32);
67 ethernet_main_t *em = ðernet_main;
68 ethernet_type_info_t *t = ethernet_get_type_info (em, type);
71 s = format (s, "%s", t->name);
73 s = format (s, "0x%04x", type);
79 format_ethernet_vlan_tci (u8 * s, va_list * va)
81 u32 vlan_tci = va_arg (*va, u32);
83 u32 vid = (vlan_tci & 0xfff);
84 u32 cfi = (vlan_tci >> 12) & 1;
85 u32 pri = (vlan_tci >> 13);
87 s = format (s, "%d", vid);
89 s = format (s, " priority %d", pri);
91 s = format (s, " cfi");
97 format_ethernet_header_with_length (u8 * s, va_list * args)
99 ethernet_pbb_header_packed_t *ph =
100 va_arg (*args, ethernet_pbb_header_packed_t *);
101 ethernet_max_header_t *m = (ethernet_max_header_t *) ph;
102 u32 max_header_bytes = va_arg (*args, u32);
103 ethernet_main_t *em = ðernet_main;
104 ethernet_header_t *e = &m->ethernet;
105 ethernet_vlan_header_t *v;
106 ethernet_type_t type = clib_net_to_host_u16 (e->type);
107 ethernet_type_t vlan_type[ARRAY_LEN (m->vlan)];
108 u32 n_vlan = 0, i, header_bytes;
111 while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD
112 || type == ETHERNET_TYPE_DOT1AH) && n_vlan < ARRAY_LEN (m->vlan))
114 vlan_type[n_vlan] = type;
115 if (type != ETHERNET_TYPE_DOT1AH)
117 v = m->vlan + n_vlan;
118 type = clib_net_to_host_u16 (v->type);
123 header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]);
124 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
125 return format (s, "ethernet header truncated");
127 indent = format_get_indent (s);
129 s = format (s, "%U: %U -> %U",
130 format_ethernet_type, type,
131 format_ethernet_address, e->src_address,
132 format_ethernet_address, e->dst_address);
134 if (type != ETHERNET_TYPE_DOT1AH)
136 for (i = 0; i < n_vlan; i++)
138 u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id);
139 if (*vlan_type == ETHERNET_TYPE_VLAN)
140 s = format (s, " 802.1q vlan %U", format_ethernet_vlan_tci, v);
142 s = format (s, " 802.1ad vlan %U", format_ethernet_vlan_tci, v);
145 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
147 ethernet_type_info_t *ti;
148 vlib_node_t *node = 0;
150 ti = ethernet_get_type_info (em, type);
151 if (ti && ti->node_index != ~0)
152 node = vlib_get_node (em->vlib_main, ti->node_index);
153 if (node && node->format_buffer)
154 s = format (s, "\n%U%U",
155 format_white_space, indent,
156 node->format_buffer, (void *) m + header_bytes,
157 max_header_bytes - header_bytes);
163 format (s, " %s b-tag %04X",
164 (clib_net_to_host_u16 (ph->b_type) ==
165 ETHERNET_TYPE_DOT1AD) ? "802.1ad" : "",
166 clib_net_to_host_u16 (ph->priority_dei_id));
168 format (s, " %s i-tag %08X",
169 (clib_net_to_host_u16 (ph->i_type) ==
170 ETHERNET_TYPE_DOT1AH) ? "802.1ah" : "",
171 clib_net_to_host_u32 (ph->priority_dei_uca_res_sid));
178 format_ethernet_header (u8 * s, va_list * args)
180 ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
181 return format (s, "%U", format_ethernet_header_with_length, m, 0);
184 /* Parse X:X:X:X:X:X unix style ethernet address. */
186 unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
188 u8 *result = va_arg (*args, u8 *);
191 if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
192 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
196 for (i = 0; i < ARRAY_LEN (a); i++)
197 if (a[i] >= (1 << 8))
200 for (i = 0; i < ARRAY_LEN (a); i++)
206 /* Parse X.X.X cisco style ethernet address. */
208 unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
210 u8 *result = va_arg (*args, u8 *);
213 if (!unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
217 for (i = 0; i < ARRAY_LEN (a); i++)
218 if (a[i] >= (1 << 16))
221 result[0] = (a[0] >> 8) & 0xff;
222 result[1] = (a[0] >> 0) & 0xff;
223 result[2] = (a[1] >> 8) & 0xff;
224 result[3] = (a[1] >> 0) & 0xff;
225 result[4] = (a[2] >> 8) & 0xff;
226 result[5] = (a[2] >> 0) & 0xff;
231 /* Parse ethernet address; accept either unix or style addresses. */
233 unformat_ethernet_address (unformat_input_t * input, va_list * args)
235 u8 *result = va_arg (*args, u8 *);
236 return (unformat_user (input, unformat_ethernet_address_unix, result)
237 || unformat_user (input, unformat_ethernet_address_cisco, result));
241 unformat_mac_address (unformat_input_t * input, va_list * args)
243 return (unformat_ethernet_address (input, args));
247 /* Returns ethernet type as an int in host byte order. */
249 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
252 u16 *result = va_arg (*args, u16 *);
253 ethernet_main_t *em = ðernet_main;
257 if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
259 if (type >= (1 << 16))
266 if (unformat_user (input, unformat_vlib_number_by_name,
267 em->type_info_by_name, &i))
269 ethernet_type_info_t *ti = vec_elt_at_index (em->type_infos, i);
278 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
281 u16 *result = va_arg (*args, u16 *);
282 if (!unformat_user (input, unformat_ethernet_type_host_byte_order, result))
285 *result = clib_host_to_net_u16 ((u16) * result);
290 unformat_ethernet_header (unformat_input_t * input, va_list * args)
292 u8 **result = va_arg (*args, u8 **);
293 ethernet_max_header_t _m, *m = &_m;
294 ethernet_header_t *e = &m->ethernet;
298 if (!unformat (input, "%U: %U -> %U",
299 unformat_ethernet_type_host_byte_order, &type,
300 unformat_ethernet_address, &e->src_address,
301 unformat_ethernet_address, &e->dst_address))
305 while (unformat (input, "vlan"))
309 if (!unformat_user (input, unformat_vlib_number, &id)
310 || id >= ETHERNET_N_VLAN)
313 if (unformat (input, "priority %d", &priority))
317 id |= priority << 13;
320 if (unformat (input, "cfi"))
323 /* Too many vlans given. */
324 if (n_vlan >= ARRAY_LEN (m->vlan))
327 m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
332 e->type = clib_host_to_net_u16 (type);
337 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
338 for (i = 0; i < n_vlan - 1; i++)
339 m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
340 m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type);
343 /* Add header to result. */
346 u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
348 vec_add2 (*result, p, n_bytes);
349 clib_memcpy (p, m, n_bytes);
356 * fd.io coding-style-patch-verification: ON
359 * eval: (c-set-style "gnu")