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