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