blob: 1ca1bce8f2a8b18bf7978f19c333a76d852c4575 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070043u8 *
44format_ethernet_address (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070046 ethernet_main_t *em = &ethernet_main;
47 u8 *a = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070057u8 *
Neale Ranns571ab202018-08-22 04:27:15 -070058format_mac_address (u8 * s, va_list * args)
59{
60 return (format_ethernet_address (s, args));
61}
62
63u8 *
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070064format_ethernet_type (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
66 ethernet_type_t type = va_arg (*args, u32);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070067 ethernet_main_t *em = &ethernet_main;
68 ethernet_type_info_t *t = ethernet_get_type_info (em, type);
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
70 if (t)
71 s = format (s, "%s", t->name);
72 else
73 s = format (s, "0x%04x", type);
74
75 return s;
76}
77
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070078u8 *
79format_ethernet_vlan_tci (u8 * s, va_list * va)
marek zavodskyad2c9c02016-07-07 03:16:10 +020080{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070081 u32 vlan_tci = va_arg (*va, u32);
marek zavodskyad2c9c02016-07-07 03:16:10 +020082
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070083 u32 vid = (vlan_tci & 0xfff);
84 u32 cfi = (vlan_tci >> 12) & 1;
85 u32 pri = (vlan_tci >> 13);
marek zavodskyad2c9c02016-07-07 03:16:10 +020086
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070087 s = format (s, "%d", vid);
88 if (pri != 0)
89 s = format (s, " priority %d", pri);
90 if (cfi != 0)
91 s = format (s, " cfi");
marek zavodskyad2c9c02016-07-07 03:16:10 +020092
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070093 return s;
marek zavodskyad2c9c02016-07-07 03:16:10 +020094}
95
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070096u8 *
97format_ethernet_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098{
Pavel Kotucek95300d12016-08-26 16:11:36 +020099 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 u32 max_header_bytes = va_arg (*args, u32);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700103 ethernet_main_t *em = &ethernet_main;
104 ethernet_header_t *e = &m->ethernet;
105 ethernet_vlan_header_t *v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 ethernet_type_t type = clib_net_to_host_u16 (e->type);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700107 ethernet_type_t vlan_type[ARRAY_LEN (m->vlan)];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 u32 n_vlan = 0, i, header_bytes;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200109 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Pavel Kotucek95300d12016-08-26 16:11:36 +0200111 while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD
112 || type == ETHERNET_TYPE_DOT1AH) && n_vlan < ARRAY_LEN (m->vlan))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 {
marek zavodskyad2c9c02016-07-07 03:16:10 +0200114 vlan_type[n_vlan] = type;
Pavel Kotucek95300d12016-08-26 16:11:36 +0200115 if (type != ETHERNET_TYPE_DOT1AH)
116 {
117 v = m->vlan + n_vlan;
118 type = clib_net_to_host_u16 (v->type);
119 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 n_vlan++;
121 }
122
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");
126
127 indent = format_get_indent (s);
128
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);
133
Pavel Kotucek95300d12016-08-26 16:11:36 +0200134 if (type != ETHERNET_TYPE_DOT1AH)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 {
Pavel Kotucek95300d12016-08-26 16:11:36 +0200136 for (i = 0; i < n_vlan; i++)
137 {
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);
141 else
142 s = format (s, " 802.1ad vlan %U", format_ethernet_vlan_tci, v);
143 }
144
145 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
146 {
147 ethernet_type_info_t *ti;
148 vlib_node_t *node = 0;
149
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);
158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 }
Pavel Kotucek95300d12016-08-26 16:11:36 +0200160 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 {
Pavel Kotucek65e84572017-01-16 17:01:56 +0100162 s =
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));
167 s =
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));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 }
173
174 return s;
175}
176
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700177u8 *
178format_ethernet_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700180 ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 return format (s, "%U", format_ethernet_header_with_length, m, 0);
182}
183
184/* Parse X:X:X:X:X:X unix style ethernet address. */
185static uword
186unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
187{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700188 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 u32 i, a[6];
190
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700191 if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
192 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 return 0;
194
195 /* Check range. */
196 for (i = 0; i < ARRAY_LEN (a); i++)
197 if (a[i] >= (1 << 8))
198 return 0;
199
200 for (i = 0; i < ARRAY_LEN (a); i++)
201 result[i] = a[i];
202
203 return 1;
204}
205
206/* Parse X.X.X cisco style ethernet address. */
207static uword
208unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
209{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700210 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 u32 i, a[3];
212
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700213 if (!unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 return 0;
215
216 /* Check range. */
217 for (i = 0; i < ARRAY_LEN (a); i++)
218 if (a[i] >= (1 << 16))
219 return 0;
220
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;
227
228 return 1;
229}
230
231/* Parse ethernet address; accept either unix or style addresses. */
232uword
233unformat_ethernet_address (unformat_input_t * input, va_list * args)
234{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700235 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 return (unformat_user (input, unformat_ethernet_address_unix, result)
237 || unformat_user (input, unformat_ethernet_address_cisco, result));
238}
239
Neale Ranns571ab202018-08-22 04:27:15 -0700240uword
241unformat_mac_address (unformat_input_t * input, va_list * args)
242{
243 return (unformat_ethernet_address (input, args));
244}
245
246
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247/* Returns ethernet type as an int in host byte order. */
248uword
249unformat_ethernet_type_host_byte_order (unformat_input_t * input,
250 va_list * args)
251{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700252 u16 *result = va_arg (*args, u16 *);
253 ethernet_main_t *em = &ethernet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 int type, i;
255
256 /* Numeric type. */
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700257 if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 {
259 if (type >= (1 << 16))
260 return 0;
261 *result = type;
262 return 1;
263 }
264
265 /* Named type. */
266 if (unformat_user (input, unformat_vlib_number_by_name,
267 em->type_info_by_name, &i))
268 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700269 ethernet_type_info_t *ti = vec_elt_at_index (em->type_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 *result = ti->type;
271 return 1;
272 }
273
274 return 0;
275}
276
277uword
278unformat_ethernet_type_net_byte_order (unformat_input_t * input,
279 va_list * args)
280{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700281 u16 *result = va_arg (*args, u16 *);
282 if (!unformat_user (input, unformat_ethernet_type_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 return 0;
284
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700285 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 return 1;
287}
288
289uword
290unformat_ethernet_header (unformat_input_t * input, va_list * args)
291{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700292 u8 **result = va_arg (*args, u8 **);
293 ethernet_max_header_t _m, *m = &_m;
294 ethernet_header_t *e = &m->ethernet;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 u16 type;
296 u32 n_vlan;
297
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700298 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))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 return 0;
303
304 n_vlan = 0;
305 while (unformat (input, "vlan"))
306 {
307 u32 id, priority;
308
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700309 if (!unformat_user (input, unformat_vlib_number, &id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 || id >= ETHERNET_N_VLAN)
311 return 0;
312
313 if (unformat (input, "priority %d", &priority))
314 {
315 if (priority >= 8)
316 return 0;
317 id |= priority << 13;
318 }
319
320 if (unformat (input, "cfi"))
321 id |= 1 << 12;
322
323 /* Too many vlans given. */
324 if (n_vlan >= ARRAY_LEN (m->vlan))
325 return 0;
326
327 m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
328 n_vlan++;
329 }
330
331 if (n_vlan == 0)
332 e->type = clib_host_to_net_u16 (type);
333 else
334 {
335 int i;
336
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);
341 }
342
343 /* Add header to result. */
344 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700345 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
347
348 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100349 clib_memcpy (p, m, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700351
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 return 1;
353}
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700354
355/*
356 * fd.io coding-style-patch-verification: ON
357 *
358 * Local Variables:
359 * eval: (c-set-style "gnu")
360 * End:
361 */