FD.io VPP  v16.09
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_ethernet_type (u8 * s, va_list * args)
59 {
60  ethernet_type_t type = va_arg (*args, u32);
63 
64  if (t)
65  s = format (s, "%s", t->name);
66  else
67  s = format (s, "0x%04x", type);
68 
69  return s;
70 }
71 
72 u8 *
73 format_ethernet_vlan_tci (u8 * s, va_list * va)
74 {
75  u32 vlan_tci = va_arg (*va, u32);
76 
77  u32 vid = (vlan_tci & 0xfff);
78  u32 cfi = (vlan_tci >> 12) & 1;
79  u32 pri = (vlan_tci >> 13);
80 
81  s = format (s, "%d", vid);
82  if (pri != 0)
83  s = format (s, " priority %d", pri);
84  if (cfi != 0)
85  s = format (s, " cfi");
86 
87  return s;
88 }
89 
90 u8 *
92 {
93  ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
94  u32 max_header_bytes = va_arg (*args, u32);
96  ethernet_header_t *e = &m->ethernet;
98  ethernet_type_t type = clib_net_to_host_u16 (e->type);
99  ethernet_type_t vlan_type[ARRAY_LEN (m->vlan)];
100  u32 n_vlan = 0, i, header_bytes;
101  uword indent;
102 
103  while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD)
104  && n_vlan < ARRAY_LEN (m->vlan))
105  {
106  vlan_type[n_vlan] = type;
107  v = m->vlan + n_vlan;
108  type = clib_net_to_host_u16 (v->type);
109  n_vlan++;
110  }
111 
112  header_bytes = sizeof (e[0]) + n_vlan * sizeof (v[0]);
113  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
114  return format (s, "ethernet header truncated");
115 
116  indent = format_get_indent (s);
117 
118  s = format (s, "%U: %U -> %U",
119  format_ethernet_type, type,
122 
123  for (i = 0; i < n_vlan; i++)
124  {
125  u32 v = clib_net_to_host_u16 (m->vlan[i].priority_cfi_and_id);
126  if (*vlan_type == ETHERNET_TYPE_VLAN)
127  s = format (s, " 802.1q vlan %U", format_ethernet_vlan_tci, v);
128  else
129  s = format (s, " 802.1ad vlan %U", format_ethernet_vlan_tci, v);
130  }
131 
132  if (max_header_bytes != 0 && header_bytes < max_header_bytes)
133  {
135  vlib_node_t *node = 0;
136 
137  ti = ethernet_get_type_info (em, type);
138  if (ti && ti->node_index != ~0)
139  node = vlib_get_node (em->vlib_main, ti->node_index);
140  if (node && node->format_buffer)
141  s = format (s, "\n%U%U",
142  format_white_space, indent,
143  node->format_buffer, (void *) m + header_bytes,
144  max_header_bytes - header_bytes);
145  }
146 
147  return s;
148 }
149 
150 u8 *
151 format_ethernet_header (u8 * s, va_list * args)
152 {
153  ethernet_max_header_t *m = va_arg (*args, ethernet_max_header_t *);
154  return format (s, "%U", format_ethernet_header_with_length, m, 0);
155 }
156 
157 /* Parse X:X:X:X:X:X unix style ethernet address. */
158 static uword
160 {
161  u8 *result = va_arg (*args, u8 *);
162  u32 i, a[6];
163 
164  if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
165  &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
166  return 0;
167 
168  /* Check range. */
169  for (i = 0; i < ARRAY_LEN (a); i++)
170  if (a[i] >= (1 << 8))
171  return 0;
172 
173  for (i = 0; i < ARRAY_LEN (a); i++)
174  result[i] = a[i];
175 
176  return 1;
177 }
178 
179 /* Parse X.X.X cisco style ethernet address. */
180 static uword
182 {
183  u8 *result = va_arg (*args, u8 *);
184  u32 i, a[3];
185 
186  if (!unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
187  return 0;
188 
189  /* Check range. */
190  for (i = 0; i < ARRAY_LEN (a); i++)
191  if (a[i] >= (1 << 16))
192  return 0;
193 
194  result[0] = (a[0] >> 8) & 0xff;
195  result[1] = (a[0] >> 0) & 0xff;
196  result[2] = (a[1] >> 8) & 0xff;
197  result[3] = (a[1] >> 0) & 0xff;
198  result[4] = (a[2] >> 8) & 0xff;
199  result[5] = (a[2] >> 0) & 0xff;
200 
201  return 1;
202 }
203 
204 /* Parse ethernet address; accept either unix or style addresses. */
205 uword
207 {
208  u8 *result = va_arg (*args, u8 *);
209  return (unformat_user (input, unformat_ethernet_address_unix, result)
210  || unformat_user (input, unformat_ethernet_address_cisco, result));
211 }
212 
213 /* Returns ethernet type as an int in host byte order. */
214 uword
216  va_list * args)
217 {
218  u16 *result = va_arg (*args, u16 *);
220  int type, i;
221 
222  /* Numeric type. */
223  if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
224  {
225  if (type >= (1 << 16))
226  return 0;
227  *result = type;
228  return 1;
229  }
230 
231  /* Named type. */
233  em->type_info_by_name, &i))
234  {
236  *result = ti->type;
237  return 1;
238  }
239 
240  return 0;
241 }
242 
243 uword
245  va_list * args)
246 {
247  u16 *result = va_arg (*args, u16 *);
249  return 0;
250 
251  *result = clib_host_to_net_u16 ((u16) * result);
252  return 1;
253 }
254 
255 uword
257 {
258  u8 **result = va_arg (*args, u8 **);
259  ethernet_max_header_t _m, *m = &_m;
260  ethernet_header_t *e = &m->ethernet;
261  u16 type;
262  u32 n_vlan;
263 
264  if (!unformat (input, "%U: %U -> %U",
268  return 0;
269 
270  n_vlan = 0;
271  while (unformat (input, "vlan"))
272  {
273  u32 id, priority;
274 
275  if (!unformat_user (input, unformat_vlib_number, &id)
276  || id >= ETHERNET_N_VLAN)
277  return 0;
278 
279  if (unformat (input, "priority %d", &priority))
280  {
281  if (priority >= 8)
282  return 0;
283  id |= priority << 13;
284  }
285 
286  if (unformat (input, "cfi"))
287  id |= 1 << 12;
288 
289  /* Too many vlans given. */
290  if (n_vlan >= ARRAY_LEN (m->vlan))
291  return 0;
292 
293  m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
294  n_vlan++;
295  }
296 
297  if (n_vlan == 0)
298  e->type = clib_host_to_net_u16 (type);
299  else
300  {
301  int i;
302 
303  e->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
304  for (i = 0; i < n_vlan - 1; i++)
305  m->vlan[i].type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
306  m->vlan[n_vlan - 1].type = clib_host_to_net_u16 (type);
307  }
308 
309  /* Add header to result. */
310  {
311  void *p;
312  u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
313 
314  vec_add2 (*result, p, n_bytes);
315  clib_memcpy (p, m, n_bytes);
316  }
317 
318  return 1;
319 }
320 
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */
uword unformat_vlib_number(unformat_input_t *input, va_list *args)
Definition: format.c:148
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:256
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
#define ETHERNET_N_VLAN
Definition: packet.h:88
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
a
Definition: bitmap.h:516
vlib_main_t * vlib_main
Definition: ethernet.h:203
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
ethernet_type_t
Definition: packet.h:43
u8 src_address[6]
Definition: packet.h:54
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
uword * type_info_by_name
Definition: ethernet.h:221
u8 * format_ethernet_vlan_tci(u8 *s, va_list *va)
Definition: format.c:73
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:91
ethernet_main_t ethernet_main
Definition: ethernet.h:241
u8 dst_address[6]
Definition: packet.h:53
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
int format_ethernet_address_16bit
Definition: ethernet.h:234
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
static uword format_get_indent(u8 *s)
Definition: format.h:72
uword unformat_ethernet_type_net_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:244
static ethernet_type_info_t * ethernet_get_type_info(ethernet_main_t *em, ethernet_type_t type)
Definition: ethernet.h:244
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:206
format_function_t * format_buffer
Definition: node.h:311
static uword unformat_ethernet_address_unix(unformat_input_t *input, va_list *args)
Definition: format.c:159
#define clib_memcpy(a, b, c)
Definition: string.h:63
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
ethernet_vlan_header_t vlan[2]
Definition: ethernet.h:69
#define ARRAY_LEN(x)
Definition: clib.h:59
u8 * format_ethernet_header(u8 *s, va_list *args)
Definition: format.c:151
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
ethernet_type_t type
Definition: ethernet.h:111
static uword unformat_ethernet_address_cisco(unformat_input_t *input, va_list *args)
Definition: format.c:181
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
ethernet_header_t ethernet
Definition: ethernet.h:66
uword unformat_ethernet_type_host_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:215
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
struct _unformat_input_t unformat_input_t
u8 * format_ethernet_type(u8 *s, va_list *args)
Definition: format.c:58
ethernet_type_info_t * type_infos
Definition: ethernet.h:218