FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
format.c
Go to the documentation of this file.
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 {
47  u8 *a = va_arg (*args, u8 *);
48 
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_mac_address (u8 * s, va_list * args)
59 {
60  return (format_ethernet_address (s, args));
61 }
62 
63 u8 *
64 format_ethernet_type (u8 * s, va_list * args)
65 {
66  ethernet_type_t type = va_arg (*args, u32);
69 
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 
78 u8 *
79 format_ethernet_vlan_tci (u8 * s, va_list * va)
80 {
81  u32 vlan_tci = va_arg (*va, u32);
82 
83  u32 vid = (vlan_tci & 0xfff);
84  u32 cfi = (vlan_tci >> 12) & 1;
85  u32 pri = (vlan_tci >> 13);
86 
87  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");
92 
93  return s;
94 }
95 
96 u8 *
98 {
99  ethernet_pbb_header_packed_t *ph =
100  va_arg (*args, ethernet_pbb_header_packed_t *);
102  u32 max_header_bytes = va_arg (*args, u32);
104  ethernet_header_t *e = &m->ethernet;
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;
109  u32 indent;
110 
111  while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD
112  || type == ETHERNET_TYPE_DOT1AH) && n_vlan < ARRAY_LEN (m->vlan))
113  {
114  vlan_type[n_vlan] = type;
115  if (type != ETHERNET_TYPE_DOT1AH)
116  {
117  v = m->vlan + n_vlan;
118  type = clib_net_to_host_u16 (v->type);
119  }
120  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,
133 
134  if (type != ETHERNET_TYPE_DOT1AH)
135  {
136  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  {
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  }
159  }
160  else
161  {
162  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));
172  }
173 
174  return s;
175 }
176 
177 u8 *
178 format_ethernet_header (u8 * s, va_list * args)
179 {
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);
182 }
183 
184 /* Parse X:X:X:X:X:X unix style ethernet address. */
185 static uword
187 {
188  u8 *result = va_arg (*args, u8 *);
189  u32 i, a[6];
190 
191  if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
192  &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
193  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. */
207 static uword
209 {
210  u8 *result = va_arg (*args, u8 *);
211  u32 i, a[3];
212 
213  if (!unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
214  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. */
232 uword
234 {
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));
238 }
239 
240 uword
241 unformat_mac_address (unformat_input_t * input, va_list * args)
242 {
243  return (unformat_ethernet_address (input, args));
244 }
245 
246 
247 /* Returns ethernet type as an int in host byte order. */
248 uword
250  va_list * args)
251 {
252  u16 *result = va_arg (*args, u16 *);
254  int type, i;
255 
256  /* Numeric type. */
257  if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
258  {
259  if (type >= (1 << 16))
260  return 0;
261  *result = type;
262  return 1;
263  }
264 
265  /* Named type. */
267  em->type_info_by_name, &i))
268  {
270  *result = ti->type;
271  return 1;
272  }
273 
274  return 0;
275 }
276 
277 uword
279  va_list * args)
280 {
281  u16 *result = va_arg (*args, u16 *);
283  return 0;
284 
285  *result = clib_host_to_net_u16 ((u16) * result);
286  return 1;
287 }
288 
289 uword
291 {
292  u8 **result = va_arg (*args, u8 **);
293  ethernet_max_header_t _m, *m = &_m;
294  ethernet_header_t *e = &m->ethernet;
295  u16 type;
296  u32 n_vlan;
297 
298  if (!unformat (input, "%U: %U -> %U",
302  return 0;
303 
304  n_vlan = 0;
305  while (unformat (input, "vlan"))
306  {
307  u32 id, priority;
308 
309  if (!unformat_user (input, unformat_vlib_number, &id)
310  || 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  {
345  void *p;
346  u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
347 
348  vec_add2 (*result, p, n_bytes);
349  clib_memcpy (p, m, n_bytes);
350  }
351 
352  return 1;
353 }
354 
355 /*
356  * fd.io coding-style-patch-verification: ON
357  *
358  * Local Variables:
359  * eval: (c-set-style "gnu")
360  * End:
361  */
u8 * format_mac_address(u8 *s, va_list *args)
Definition: format.c:58
#define ETHERNET_N_VLAN
Definition: packet.h:133
a
Definition: bitmap.h:538
vlib_main_t * vlib_main
Definition: ethernet.h:292
ethernet_type_t
Definition: packet.h:45
u8 src_address[6]
Definition: packet.h:56
uword unformat_ethernet_type_net_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:278
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:564
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
uword * type_info_by_name
Definition: ethernet.h:310
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:148
ethernet_main_t ethernet_main
Definition: init.c:45
u8 dst_address[6]
Definition: packet.h:55
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u8 * format_ethernet_header(u8 *s, va_list *args)
Definition: format.c:178
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
int format_ethernet_address_16bit
Definition: ethernet.h:323
unsigned int u32
Definition: types.h:88
static ethernet_type_info_t * ethernet_get_type_info(ethernet_main_t *em, ethernet_type_t type)
Definition: ethernet.h:338
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
format_function_t * format_buffer
Definition: node.h:381
static uword unformat_ethernet_address_unix(unformat_input_t *input, va_list *args)
Definition: format.c:186
ethernet_vlan_header_t vlan[2]
Definition: ethernet.h:157
#define ARRAY_LEN(x)
Definition: clib.h:62
ethernet_type_t type
Definition: ethernet.h:200
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
u64 uword
Definition: types.h:112
static uword unformat_ethernet_address_cisco(unformat_input_t *input, va_list *args)
Definition: format.c:208
ethernet_header_t ethernet
Definition: ethernet.h:154
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
u32 id
Definition: udp.api:45
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
u8 * format_ethernet_type(u8 *s, va_list *args)
Definition: format.c:64
uword unformat_ethernet_type_host_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:249
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
u8 * format_ethernet_vlan_tci(u8 *s, va_list *va)
Definition: format.c:79
ethernet_type_info_t * type_infos
Definition: ethernet.h:307
uword unformat_mac_address(unformat_input_t *input, va_list *args)
Definition: format.c:241