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>
43 u8 * format_ethernet_address (u8 * s, va_list * args)
45 ethernet_main_t * em = ðernet_main;
46 u8 * a = va_arg (*args, u8 *);
48 if (em->format_ethernet_address_16bit)
49 return format (s, "%02x%02x.%02x%02x.%02x%02x",
50 a[0], a[1], a[2], a[3], a[4], a[5]);
52 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
53 a[0], a[1], a[2], a[3], a[4], a[5]);
56 u8 * format_ethernet_type (u8 * s, va_list * args)
58 ethernet_type_t type = va_arg (*args, u32);
59 ethernet_main_t * em = ðernet_main;
60 ethernet_type_info_t * t = ethernet_get_type_info (em, type);
63 s = format (s, "%s", t->name);
65 s = format (s, "0x%04x", type);
70 u8 * format_ethernet_vlan_tci (u8 * s, va_list * va)
72 u32 vlan_tci = va_arg (*va, u32);
74 u32 vid = (vlan_tci & 0xfff);
75 u32 cfi = (vlan_tci >> 12) & 1;
76 u32 pri = (vlan_tci >> 13);
78 s = format (s, "id %d", vid);
80 s = format (s, " priority %d", pri);
82 s = format (s, " cfi");
87 u8 * format_ethernet_header_with_length (u8 * s, va_list * args)
89 ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *);
90 u32 max_header_bytes = va_arg (*args, u32);
91 ethernet_main_t * em = ðernet_main;
92 ethernet_header_t * e = &m->ethernet;
93 ethernet_vlan_header_t * v;
94 ethernet_type_t type = clib_net_to_host_u16 (e->type);
95 ethernet_type_t vlan_type[ARRAY_LEN(m->vlan)];
96 u32 n_vlan = 0, i, header_bytes;
99 while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD)
100 && n_vlan < ARRAY_LEN (m->vlan))
102 vlan_type[n_vlan] = type;
103 v = m->vlan + n_vlan;
104 type = clib_net_to_host_u16 (v->type);
108 header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]);
109 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
110 return format (s, "ethernet header truncated");
112 indent = format_get_indent (s);
114 s = format (s, "%U: %U -> %U",
115 format_ethernet_type, type,
116 format_ethernet_address, e->src_address,
117 format_ethernet_address, e->dst_address);
119 for (i = 0; i < n_vlan; i++)
121 u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id);
122 if (*vlan_type == ETHERNET_TYPE_VLAN)
123 s = format (s, " vlan 802.1q %U", format_ethernet_vlan_tci, v);
125 s = format (s, " vlan 802.1ad %U", format_ethernet_vlan_tci, v);
128 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
130 ethernet_type_info_t * ti;
131 vlib_node_t * node = 0;
133 ti = ethernet_get_type_info (em, type);
134 if (ti && ti->node_index != ~0)
135 node = vlib_get_node (em->vlib_main, ti->node_index);
136 if (node && node->format_buffer)
137 s = format (s, "\n%U%U",
138 format_white_space, indent,
139 node->format_buffer, (void *) m + header_bytes,
140 max_header_bytes - header_bytes);
146 u8 * format_ethernet_header (u8 * s, va_list * args)
148 ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *);
149 return format (s, "%U", format_ethernet_header_with_length, m, 0);
152 /* Parse X:X:X:X:X:X unix style ethernet address. */
154 unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
156 u8 * result = va_arg (*args, u8 *);
159 if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
160 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
164 for (i = 0; i < ARRAY_LEN (a); i++)
165 if (a[i] >= (1 << 8))
168 for (i = 0; i < ARRAY_LEN (a); i++)
174 /* Parse X.X.X cisco style ethernet address. */
176 unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
178 u8 * result = va_arg (*args, u8 *);
181 if (! unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
185 for (i = 0; i < ARRAY_LEN (a); i++)
186 if (a[i] >= (1 << 16))
189 result[0] = (a[0] >> 8) & 0xff;
190 result[1] = (a[0] >> 0) & 0xff;
191 result[2] = (a[1] >> 8) & 0xff;
192 result[3] = (a[1] >> 0) & 0xff;
193 result[4] = (a[2] >> 8) & 0xff;
194 result[5] = (a[2] >> 0) & 0xff;
199 /* Parse ethernet address; accept either unix or style addresses. */
201 unformat_ethernet_address (unformat_input_t * input, va_list * args)
203 u8 * result = va_arg (*args, u8 *);
204 return (unformat_user (input, unformat_ethernet_address_unix, result)
205 || unformat_user (input, unformat_ethernet_address_cisco, result));
208 /* Returns ethernet type as an int in host byte order. */
210 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
213 u16 * result = va_arg (*args, u16 *);
214 ethernet_main_t * em = ðernet_main;
218 if (unformat (input, "0x%x", &type)
219 || unformat (input, "%d", &type))
221 if (type >= (1 << 16))
228 if (unformat_user (input, unformat_vlib_number_by_name,
229 em->type_info_by_name, &i))
231 ethernet_type_info_t * ti = vec_elt_at_index (em->type_infos, i);
240 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
243 u16 * result = va_arg (*args, u16 *);
244 if (! unformat_user (input, unformat_ethernet_type_host_byte_order, result))
247 *result = clib_host_to_net_u16 ((u16) *result);
252 unformat_ethernet_header (unformat_input_t * input, va_list * args)
254 u8 ** result = va_arg (*args, u8 **);
255 ethernet_max_header_t _m, * m = &_m;
256 ethernet_header_t * e = &m->ethernet;
260 if (! unformat (input, "%U: %U -> %U",
261 unformat_ethernet_type_host_byte_order, &type,
262 unformat_ethernet_address, &e->src_address,
263 unformat_ethernet_address, &e->dst_address))
267 while (unformat (input, "vlan"))
271 if (! unformat_user (input, unformat_vlib_number, &id)
272 || id >= ETHERNET_N_VLAN)
275 if (unformat (input, "priority %d", &priority))
279 id |= priority << 13;
282 if (unformat (input, "cfi"))
285 /* Too many vlans given. */
286 if (n_vlan >= ARRAY_LEN (m->vlan))
289 m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
294 e->type = clib_host_to_net_u16 (type);
299 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
300 for (i = 0; i < n_vlan - 1; i++)
301 m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
302 m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type);
305 /* Add header to result. */
308 u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
310 vec_add2 (*result, p, n_bytes);
311 clib_memcpy (p, m, n_bytes);