Initial commit of vpp code.
[vpp.git] / vnet / vnet / interface_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  * interface_format.c: interface formatting
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 <vnet/vnet.h>
41
42 u8 * format_vnet_sw_interface_flags (u8 * s, va_list * args)
43 {
44   u32 flags = va_arg (*args, u32);
45
46   s = format (s, "%s", (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "down");
47   if (flags & VNET_SW_INTERFACE_FLAG_PUNT)
48     s = format (s, "/punt");
49
50   return s;
51 }
52
53 u8 * format_vnet_hw_interface (u8 * s, va_list * args)
54 {
55   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
56   vnet_hw_interface_t * hi = va_arg (*args, vnet_hw_interface_t *);
57   vnet_hw_interface_class_t * hw_class;
58   vnet_device_class_t * dev_class;
59   int verbose = va_arg (*args, int);
60   uword indent;
61
62   if (! hi)
63     return format (s, "%=32s%=6s%=8s%s",
64                    "Name", "Idx", "Link", "Hardware");
65
66   indent = format_get_indent (s);
67
68   s = format (s, "%-32v%=6d%=8s",
69               hi->name, hi->hw_if_index,
70               hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP ? "up" : "down");
71
72   hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
73   dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
74
75   if (dev_class->format_device_name)  
76     s = format (s, "%U", dev_class->format_device_name, hi->dev_instance);
77   else
78     s = format (s, "%s%d", dev_class->name, hi->dev_instance);
79
80   if (verbose)
81     {
82       if (hw_class->format_device)
83         s = format (s, "\n%U%U",
84                     format_white_space, indent + 2,
85                     hw_class->format_device, hi->hw_if_index, verbose);
86       else
87         {
88           s = format (s, "\n%U%s",
89                       format_white_space, indent + 2,
90                       hw_class->name);
91           if (hw_class->format_address && vec_len (hi->hw_address) > 0)
92             s = format (s, " address %U", hw_class->format_address, hi->hw_address);
93         }
94
95       if (dev_class->format_device)
96         s = format (s, "\n%U%U",
97                     format_white_space, indent + 2,
98                     dev_class->format_device, hi->dev_instance, verbose);
99     }
100
101   return s;
102 }
103
104 u8 * format_vnet_sw_interface_name (u8 * s, va_list * args)
105 {
106   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
107   vnet_sw_interface_t * si = va_arg (*args, vnet_sw_interface_t *);
108   vnet_sw_interface_t * si_sup = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
109   vnet_hw_interface_t * hi_sup;
110
111   ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
112   hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
113
114   s = format (s, "%v", hi_sup->name);
115
116   if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
117     s = format (s, ".%d", si->sub.id);
118
119   return s;
120 }
121
122 u8 * format_vnet_sw_if_index_name (u8 * s, va_list * args)
123 {
124   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
125   u32 sw_if_index = va_arg (*args, u32);
126   return format (s, "%U",
127                  format_vnet_sw_interface_name, vnm,
128                  vnet_get_sw_interface (vnm, sw_if_index));
129 }
130
131 u8 * format_vnet_sw_interface (u8 * s, va_list * args)
132 {
133   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
134   vnet_sw_interface_t * si = va_arg (*args, vnet_sw_interface_t *);
135   vnet_interface_main_t * im = &vnm->interface_main;
136   uword indent, n_printed;
137   int i, j, n_counters;
138   static vnet_main_t ** my_vnet_mains;
139
140   if (! si)
141     return format (s, "%=32s%=5s%=16s%=16s%=16s",
142                    "Name", "Idx", "State", "Counter", "Count");
143
144   s = format (s, "%-32U%=5d%=16U",
145               format_vnet_sw_interface_name, vnm, si, si->sw_if_index,
146               format_vnet_sw_interface_flags, si->flags);
147
148   vec_reset_length (my_vnet_mains);
149
150   indent = format_get_indent (s);
151   n_printed = 0;
152
153   {
154     vlib_combined_counter_main_t * cm;
155     vlib_counter_t v, vtotal;
156     u8 * n = 0;
157
158     for (i = 0; i < vec_len (vnet_mains); i++)
159       {
160         if (vnet_mains[i])
161           vec_add1 (my_vnet_mains, vnet_mains[i]);
162       }
163
164     if (vec_len(my_vnet_mains) == 0)
165         vec_add1 (my_vnet_mains, &vnet_main);
166
167     /* Each vnet_main_t has its own copy of the interface counters */
168     n_counters = vec_len (im->combined_sw_if_counters);
169
170     /* rx, tx counters... */
171     for (j = 0; j < n_counters; j++)
172       {
173         vtotal.packets = 0;
174         vtotal.bytes = 0;
175
176         for (i = 0; i < vec_len(my_vnet_mains); i++)
177           {
178             im = &my_vnet_mains[i]->interface_main;
179             cm = im->combined_sw_if_counters + j;
180             vlib_get_combined_counter (cm, si->sw_if_index, &v);
181             vtotal.packets += v.packets;
182             vtotal.bytes += v.bytes;
183           }
184
185         /* Only display non-zero counters. */
186         if (vtotal.packets == 0)
187           continue;
188
189         if (n_printed > 0)
190           s = format (s, "\n%U", format_white_space, indent);
191         n_printed += 2;
192
193         if (n)
194           _vec_len (n) = 0;
195         n = format (n, "%s packets", cm->name);
196         s = format (s, "%-16v%16Ld", n, vtotal.packets);
197
198         _vec_len (n) = 0;
199         n = format (n, "%s bytes", cm->name);
200         s = format (s, "\n%U%-16v%16Ld",
201                     format_white_space, indent,
202                     n, vtotal.bytes);
203       }
204     vec_free (n);
205   }
206
207   {
208     vlib_simple_counter_main_t * cm;
209     u64 v, vtotal ;
210
211     n_counters = vec_len (im->sw_if_counters);
212
213     for (j = 0; j < n_counters; j++)
214       {
215         vtotal = 0;
216
217         for (i = 0; i < vec_len(my_vnet_mains); i++)
218           {
219             im = &my_vnet_mains[i]->interface_main;
220             cm = im->sw_if_counters + j;
221
222             v = vlib_get_simple_counter (cm, si->sw_if_index);
223             vtotal += v;
224           }
225
226         /* Only display non-zero counters. */
227         if (vtotal == 0)
228           continue;
229
230         if (n_printed > 0)
231           s = format (s, "\n%U", format_white_space, indent);
232         n_printed += 1;
233
234         s = format (s, "%-16s%16Ld", cm->name, vtotal);
235       }
236   }
237
238   return s;
239 }
240
241 uword unformat_vnet_hw_interface (unformat_input_t * input, va_list * args)
242 {
243   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
244   u32 * hw_if_index = va_arg (*args, u32 *);
245   vnet_interface_main_t * im = &vnm->interface_main;
246   vnet_device_class_t * c;
247
248   /* Try per device class functions first. */
249   vec_foreach (c, im->device_classes)
250     {
251       if (c->unformat_device_name
252           && unformat_user (input, c->unformat_device_name, hw_if_index))
253       return 1;
254     }
255
256   return unformat_user (input, unformat_hash_vec_string,
257                         im->hw_interface_by_name, hw_if_index);
258 }
259
260 uword unformat_vnet_sw_interface (unformat_input_t * input, va_list * args)
261 {
262   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
263   u32 * result = va_arg (*args, u32 *);
264   vnet_hw_interface_t * hi;
265   u32 hw_if_index, id, id_specified;
266   u8 * if_name = 0;
267   uword * p, error = 0;
268
269   id = ~0;
270   if (unformat (input, "%_%v.%d%_", &if_name, &id)
271       && ((p = hash_get (vnm->interface_main.hw_interface_by_name, if_name))))
272     {
273       hw_if_index = p[0];
274       id_specified = 1;
275     }
276   else if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
277     id_specified = 0;
278   else
279     goto done;
280
281   hi = vnet_get_hw_interface (vnm, hw_if_index);
282   if (! id_specified)
283     {
284       *result = hi->sw_if_index;
285     }
286   else
287     {
288       if (! (p = hash_get (hi->sub_interface_sw_if_index_by_id, id)))
289         return 0;
290       *result = p[0];
291     }
292   error = 1;
293  done:
294   vec_free (if_name);
295   return error;
296 }
297
298 uword unformat_vnet_sw_interface_flags (unformat_input_t * input, va_list * args)
299 {
300   u32 * result = va_arg (*args, u32 *);
301   u32 flags = 0;
302
303   if (unformat (input, "up"))
304     flags |= VNET_SW_INTERFACE_FLAG_ADMIN_UP;
305   else if (unformat (input, "down"))
306     flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
307   else if (unformat (input, "punt"))
308     flags |= VNET_SW_INTERFACE_FLAG_PUNT;
309   else if (unformat (input, "enable"))
310     flags &= ~VNET_SW_INTERFACE_FLAG_PUNT;
311   else
312     return 0;
313
314   *result = flags;
315   return 1;
316 }
317
318 uword unformat_vnet_hw_interface_flags (unformat_input_t * input, va_list * args)
319 {
320   u32 * result = va_arg (*args, u32 *);
321   u32 flags = 0;
322
323   if (unformat (input, "up"))
324     flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
325   else if (unformat (input, "down"))
326     flags &= ~VNET_HW_INTERFACE_FLAG_LINK_UP;
327   else
328     return 0;
329
330   *result = flags;
331   return 1;
332 }
333