Output format change from "vlan 802.1q" to "802.1q vlan", addition to VPP-111
[vpp.git] / vnet / vnet / ethernet / format.c
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 * format_ethernet_address (u8 * s, va_list * args)
44 {
45   ethernet_main_t * em = &ethernet_main;
46   u8 * a = va_arg (*args, u8 *);
47
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]);
51   else
52     return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
53                    a[0], a[1], a[2], a[3], a[4], a[5]);
54 }
55
56 u8 * format_ethernet_type (u8 * s, va_list * args)
57 {
58   ethernet_type_t type = va_arg (*args, u32);
59   ethernet_main_t * em = &ethernet_main;
60   ethernet_type_info_t * t = ethernet_get_type_info (em, type);
61
62   if (t)
63     s = format (s, "%s", t->name);
64   else
65     s = format (s, "0x%04x", type);
66
67   return s;
68 }
69
70 u8 * format_ethernet_vlan_tci (u8 * s, va_list * va)
71 {
72     u32 vlan_tci = va_arg (*va, u32);
73
74     u32 vid = (vlan_tci & 0xfff);
75     u32 cfi = (vlan_tci >> 12) & 1;
76     u32 pri = (vlan_tci >> 13);
77
78     s = format (s, "%d", vid);
79     if (pri != 0)
80         s = format (s, " priority %d", pri);
81     if (cfi != 0)
82         s = format (s, " cfi");
83
84     return s;
85 }
86
87 u8 * format_ethernet_header_with_length (u8 * s, va_list * args)
88 {
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 = &ethernet_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;
97   uword indent;
98
99   while ((type == ETHERNET_TYPE_VLAN || type == ETHERNET_TYPE_DOT1AD)
100          && n_vlan < ARRAY_LEN (m->vlan))
101     {
102       vlan_type[n_vlan] = type;
103       v = m->vlan + n_vlan;
104       type = clib_net_to_host_u16 (v->type);
105       n_vlan++;
106     }
107
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");
111
112   indent = format_get_indent (s);
113
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);
118
119   for (i = 0; i < n_vlan; i++)
120     {
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, " 802.1q vlan %U", format_ethernet_vlan_tci, v);
124       else
125           s = format (s, " 802.1ad vlan %U", format_ethernet_vlan_tci, v);
126     }
127
128   if (max_header_bytes != 0 && header_bytes < max_header_bytes)
129     {
130       ethernet_type_info_t * ti;
131       vlib_node_t * node = 0;
132
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);
141     }
142
143   return s;
144 }
145
146 u8 * format_ethernet_header (u8 * s, va_list * args)
147 {
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);
150 }
151
152 /* Parse X:X:X:X:X:X unix style ethernet address. */
153 static uword
154 unformat_ethernet_address_unix (unformat_input_t * input, va_list * args)
155 {
156   u8 * result = va_arg (*args, u8 *);
157   u32 i, a[6];
158
159   if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
160                   &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
161     return 0;
162
163   /* Check range. */
164   for (i = 0; i < ARRAY_LEN (a); i++)
165     if (a[i] >= (1 << 8))
166       return 0;
167
168   for (i = 0; i < ARRAY_LEN (a); i++)
169     result[i] = a[i];
170
171   return 1;
172 }
173
174 /* Parse X.X.X cisco style ethernet address. */
175 static uword
176 unformat_ethernet_address_cisco (unformat_input_t * input, va_list * args)
177 {
178   u8 * result = va_arg (*args, u8 *);
179   u32 i, a[3];
180
181   if (! unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
182     return 0;
183
184   /* Check range. */
185   for (i = 0; i < ARRAY_LEN (a); i++)
186     if (a[i] >= (1 << 16))
187       return 0;
188
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;
195
196   return 1;
197 }
198
199 /* Parse ethernet address; accept either unix or style addresses. */
200 uword
201 unformat_ethernet_address (unformat_input_t * input, va_list * args)
202 {
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));
206 }
207
208 /* Returns ethernet type as an int in host byte order. */
209 uword
210 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
211                                         va_list * args)
212 {
213   u16 * result = va_arg (*args, u16 *);
214   ethernet_main_t * em = &ethernet_main;
215   int type, i;
216
217   /* Numeric type. */
218   if (unformat (input, "0x%x", &type)
219       || unformat (input, "%d", &type))
220     {
221       if (type >= (1 << 16))
222         return 0;
223       *result = type;
224       return 1;
225     }
226
227   /* Named type. */
228   if (unformat_user (input, unformat_vlib_number_by_name,
229                      em->type_info_by_name, &i))
230     {
231       ethernet_type_info_t * ti = vec_elt_at_index (em->type_infos, i);
232       *result = ti->type;
233       return 1;
234     }
235
236   return 0;
237 }
238
239 uword
240 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
241                                        va_list * args)
242 {
243   u16 * result = va_arg (*args, u16 *);
244   if (! unformat_user (input, unformat_ethernet_type_host_byte_order, result))
245     return 0;
246
247   *result = clib_host_to_net_u16 ((u16) *result);
248   return 1;
249 }
250
251 uword
252 unformat_ethernet_header (unformat_input_t * input, va_list * args)
253 {
254   u8 ** result = va_arg (*args, u8 **);
255   ethernet_max_header_t _m, * m = &_m;
256   ethernet_header_t * e = &m->ethernet;
257   u16 type;
258   u32 n_vlan;
259
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))
264     return 0;
265
266   n_vlan = 0;
267   while (unformat (input, "vlan"))
268     {
269       u32 id, priority;
270
271       if (! unformat_user (input, unformat_vlib_number, &id)
272           || id >= ETHERNET_N_VLAN)
273         return 0;
274
275       if (unformat (input, "priority %d", &priority))
276         {
277           if (priority >= 8)
278             return 0;
279           id |= priority << 13;
280         }
281
282       if (unformat (input, "cfi"))
283         id |= 1 << 12;
284
285       /* Too many vlans given. */
286       if (n_vlan >= ARRAY_LEN (m->vlan))
287         return 0;
288
289       m->vlan[n_vlan].priority_cfi_and_id = clib_host_to_net_u16 (id);
290       n_vlan++;
291     }
292
293   if (n_vlan == 0)
294     e->type = clib_host_to_net_u16 (type);
295   else
296     {
297       int i;
298
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);
303     }
304
305   /* Add header to result. */
306   {
307     void * p;
308     u32 n_bytes = sizeof (e[0]) + n_vlan * sizeof (m->vlan[0]);
309
310     vec_add2 (*result, p, n_bytes);
311     clib_memcpy (p, m, n_bytes);
312   }
313   
314   return 1;
315 }