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_header_with_length (u8 * s, va_list * args)
72 ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *);
73 u32 max_header_bytes = va_arg (*args, u32);
74 ethernet_main_t * em = ðernet_main;
75 ethernet_header_t * e = &m->ethernet;
76 ethernet_vlan_header_t * v;
77 ethernet_type_t type = clib_net_to_host_u16 (e->type);
78 u32 n_vlan = 0, i, header_bytes;
81 while (type == ETHERNET_TYPE_VLAN
82 && n_vlan < ARRAY_LEN (m->vlan))
85 type = clib_net_to_host_u16 (v->type);
89 header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]);
90 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
91 return format (s, "ethernet header truncated");
93 indent = format_get_indent (s);
95 s = format (s, "%U: %U -> %U",
96 format_ethernet_type, type,
97 format_ethernet_address, e->src_address,
98 format_ethernet_address, e->dst_address);
100 for (i = 0; i < n_vlan; i++)
102 u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id);
103 u32 vid = (v & 0xfff);
104 u32 cfi = (v >> 12) & 1;
107 s = format (s, " vlan %d", vid);
109 s = format (s, " priority %d", pri);
111 s = format (s, " cfi");
114 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
116 ethernet_type_info_t * ti;
117 vlib_node_t * node = 0;
119 ti = ethernet_get_type_info (em, type);
120 if (ti && ti->node_index != ~0)
121 node = vlib_get_node (em->vlib_main, ti->node_index);
122 if (node && node->format_buffer)
123 s = format (s, "\n%U%U",
124 format_white_space, indent,
125 node->format_buffer, (void *) m + header_bytes,
126 max_header_bytes - header_bytes);
132 u8 * format_ethernet_header (u8 * s, va_list * args)
134 ethernet_max_header_t * m = va_arg (*args, ethernet_max_header_t *);
135 return format (s, "%U", format_ethernet_header_with_length, m, 0);
138 /* Parse X:X:X:X:X:X unix style ethernet address. */
140 unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
142 u8 * result = va_arg (*args, u8 *);
145 if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
146 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
150 for (i = 0; i < ARRAY_LEN (a); i++)
151 if (a[i] >= (1 << 8))
154 for (i = 0; i < ARRAY_LEN (a); i++)
160 /* Parse X.X.X cisco style ethernet address. */
162 unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
164 u8 * result = va_arg (*args, u8 *);
167 if (! unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
171 for (i = 0; i < ARRAY_LEN (a); i++)
172 if (a[i] >= (1 << 16))
175 result[0] = (a[0] >> 8) & 0xff;
176 result[1] = (a[0] >> 0) & 0xff;
177 result[2] = (a[1] >> 8) & 0xff;
178 result[3] = (a[1] >> 0) & 0xff;
179 result[4] = (a[2] >> 8) & 0xff;
180 result[5] = (a[2] >> 0) & 0xff;
185 /* Parse ethernet address; accept either unix or style addresses. */
187 unformat_ethernet_address (unformat_input_t * input, va_list * args)
189 u8 * result = va_arg (*args, u8 *);
190 return (unformat_user (input, unformat_ethernet_address_unix, result)
191 || unformat_user (input, unformat_ethernet_address_cisco, result));
194 /* Returns ethernet type as an int in host byte order. */
196 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
199 u16 * result = va_arg (*args, u16 *);
200 ethernet_main_t * em = ðernet_main;
204 if (unformat (input, "0x%x", &type)
205 || unformat (input, "%d", &type))
207 if (type >= (1 << 16))
214 if (unformat_user (input, unformat_vlib_number_by_name,
215 em->type_info_by_name, &i))
217 ethernet_type_info_t * ti = vec_elt_at_index (em->type_infos, i);
226 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
229 u16 * result = va_arg (*args, u16 *);
230 if (! unformat_user (input, unformat_ethernet_type_host_byte_order, result))
233 *result = clib_host_to_net_u16 ((u16) *result);
238 unformat_ethernet_header (unformat_input_t * input, va_list * args)
240 u8 ** result = va_arg (*args, u8 **);
241 ethernet_max_header_t _m, * m = &_m;
242 ethernet_header_t * e = &m->ethernet;
246 if (! unformat (input, "%U: %U -> %U",
247 unformat_ethernet_type_host_byte_order, &type,
248 unformat_ethernet_address, &e->src_address,
249 unformat_ethernet_address, &e->dst_address))
253 while (unformat (input, "vlan"))
257 if (! unformat_user (input, unformat_vlib_number, &id)
258 || id >= ETHERNET_N_VLAN)
261 if (unformat (input, "priority %d", &priority))
265 id |= priority << 13;
268 if (unformat (input, "cfi"))
271 /* Too many vlans given. */
272 if (n_vlan >= ARRAY_LEN (m->vlan))
275 m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
280 e->type = clib_host_to_net_u16 (type);
285 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
286 for (i = 0; i < n_vlan - 1; i++)
287 m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
288 m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type);
291 /* Add header to result. */
294 u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
296 vec_add2 (*result, p, n_bytes);
297 memcpy (p, m, n_bytes);