blob: 03caf5c6dd7e856f15da3b30106f08465531665f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
John Lobcebbb92016-04-05 15:47:43 -040041#include <vppinfra/bitmap.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
Dave Barachba868bb2016-08-08 09:51:21 -040043u8 *
44format_vnet_sw_interface_flags (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
46 u32 flags = va_arg (*args, u32);
47
Dave Barachba868bb2016-08-08 09:51:21 -040048 if (flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
John Lobcebbb92016-04-05 15:47:43 -040049 s = format (s, "bond-slave");
Dave Barachba868bb2016-08-08 09:51:21 -040050 else
John Lobcebbb92016-04-05 15:47:43 -040051 {
Dave Barachba868bb2016-08-08 09:51:21 -040052 s = format (s, "%s",
John Lobcebbb92016-04-05 15:47:43 -040053 (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "down");
Dave Barachba868bb2016-08-08 09:51:21 -040054 if (flags & VNET_SW_INTERFACE_FLAG_PUNT)
John Lobcebbb92016-04-05 15:47:43 -040055 s = format (s, "/punt");
56 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 return s;
59}
60
Dave Barachba868bb2016-08-08 09:51:21 -040061u8 *
Damjan Marion44036902017-04-28 12:29:15 +020062format_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
78u8 *
Dave Barachba868bb2016-08-08 09:51:21 -040079format_vnet_hw_interface (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barachba868bb2016-08-08 09:51:21 -040081 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 int verbose = va_arg (*args, int);
86 uword indent;
87
Dave Barachba868bb2016-08-08 09:51:21 -040088 if (!hi)
89 return format (s, "%=32s%=6s%=8s%s", "Name", "Idx", "Link", "Hardware");
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91 indent = format_get_indent (s);
92
John Lobcebbb92016-04-05 15:47:43 -040093 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
Dave Barachba868bb2016-08-08 09:51:21 -040098 s = format (s, "%=8s",
John Lobcebbb92016-04-05 15:47:43 -040099 hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP ? "up" : "down");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
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
Dave Barachba868bb2016-08-08 09:51:21 -0400104 if (hi->bond_info && (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
John Lobcebbb92016-04-05 15:47:43 -0400105 {
106 int hw_idx;
107 s = format (s, "Slave-Idx:");
Dave Barachba868bb2016-08-08 09:51:21 -0400108 clib_bitmap_foreach (hw_idx, hi->bond_info, s =
109 format (s, " %d", hw_idx));
John Lobcebbb92016-04-05 15:47:43 -0400110 }
Dave Barachba868bb2016-08-08 09:51:21 -0400111 else if (dev_class->format_device_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 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",
Dave Barachba868bb2016-08-08 09:51:21 -0400125 format_white_space, indent + 2, hw_class->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 if (hw_class->format_address && vec_len (hi->hw_address) > 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400127 s =
128 format (s, " address %U", hw_class->format_address,
129 hi->hw_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }
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
Dave Barachba868bb2016-08-08 09:51:21 -0400141u8 *
142format_vnet_sw_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barachba868bb2016-08-08 09:51:21 -0400144 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
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
Dave Barachba868bb2016-08-08 09:51:21 -0400161u8 *
162format_vnet_sw_if_index_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
Dave Barachba868bb2016-08-08 09:51:21 -0400164 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 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
Dave Barachba868bb2016-08-08 09:51:21 -0400171u8 *
172format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
173 vnet_sw_interface_t * si)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 uword indent, n_printed;
176 int i, j, n_counters;
Dave Barachba868bb2016-08-08 09:51:21 -0400177 static vnet_main_t **my_vnet_mains;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 vec_reset_length (my_vnet_mains);
180
181 indent = format_get_indent (s);
182 n_printed = 0;
183
184 {
Dave Barachba868bb2016-08-08 09:51:21 -0400185 vlib_combined_counter_main_t *cm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 vlib_counter_t v, vtotal;
Dave Barachba868bb2016-08-08 09:51:21 -0400187 u8 *n = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
189 for (i = 0; i < vec_len (vnet_mains); i++)
190 {
Dave Barachba868bb2016-08-08 09:51:21 -0400191 if (vnet_mains[i])
192 vec_add1 (my_vnet_mains, vnet_mains[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 }
194
Dave Barachba868bb2016-08-08 09:51:21 -0400195 if (vec_len (my_vnet_mains) == 0)
196 vec_add1 (my_vnet_mains, &vnet_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
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 {
Dave Barachba868bb2016-08-08 09:51:21 -0400204 vtotal.packets = 0;
205 vtotal.bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Dave Barachba868bb2016-08-08 09:51:21 -0400207 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
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",
Dave Barachba868bb2016-08-08 09:51:21 -0400232 format_white_space, indent, n, vtotal.bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 }
234 vec_free (n);
235 }
236
237 {
Dave Barachba868bb2016-08-08 09:51:21 -0400238 vlib_simple_counter_main_t *cm;
239 u64 v, vtotal;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
241 n_counters = vec_len (im->sw_if_counters);
242
243 for (j = 0; j < n_counters; j++)
244 {
Dave Barachba868bb2016-08-08 09:51:21 -0400245 vtotal = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Dave Barachba868bb2016-08-08 09:51:21 -0400247 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Dave Barachba868bb2016-08-08 09:51:21 -0400252 v = vlib_get_simple_counter (cm, si->sw_if_index);
253 vtotal += v;
254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
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
Dave Barachba868bb2016-08-08 09:51:21 -0400271u8 *
272format_vnet_sw_interface (u8 * s, va_list * args)
Sean Hope679ea792016-02-22 15:12:01 -0500273{
Dave Barachba868bb2016-08-08 09:51:21 -0400274 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;
Sean Hope679ea792016-02-22 15:12:01 -0500277
Dave Barachba868bb2016-08-08 09:51:21 -0400278 if (!si)
Sean Hope679ea792016-02-22 15:12:01 -0500279 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
Dave Barachba868bb2016-08-08 09:51:21 -0400286 s = format_vnet_sw_interface_cntrs (s, im, si);
Sean Hope679ea792016-02-22 15:12:01 -0500287
288 return s;
289}
290
Dave Barachba868bb2016-08-08 09:51:21 -0400291u8 *
292format_vnet_sw_interface_name_override (u8 * s, va_list * args)
Sean Hope679ea792016-02-22 15:12:01 -0500293{
Dave Barachba868bb2016-08-08 09:51:21 -0400294 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
295 vnet_sw_interface_t *si = va_arg (*args, vnet_sw_interface_t *);
Sean Hope679ea792016-02-22 15:12:01 -0500296 /* caller supplied display name for this interface */
Dave Barachba868bb2016-08-08 09:51:21 -0400297 u8 *name = va_arg (*args, u8 *);
298 vnet_interface_main_t *im = &vnm->interface_main;
Sean Hope679ea792016-02-22 15:12:01 -0500299
300
Dave Barachba868bb2016-08-08 09:51:21 -0400301 if (!si)
Sean Hope679ea792016-02-22 15:12:01 -0500302 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
Dave Barachba868bb2016-08-08 09:51:21 -0400309 s = format_vnet_sw_interface_cntrs (s, im, si);
Sean Hope679ea792016-02-22 15:12:01 -0500310
311 return s;
312}
313
Dave Barachba868bb2016-08-08 09:51:21 -0400314uword
315unformat_vnet_hw_interface (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316{
Dave Barachba868bb2016-08-08 09:51:21 -0400317 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
322 /* Try per device class functions first. */
323 vec_foreach (c, im->device_classes)
Dave Barachba868bb2016-08-08 09:51:21 -0400324 {
325 if (c->unformat_device_name
326 && unformat_user (input, c->unformat_device_name, hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 return 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
330 return unformat_user (input, unformat_hash_vec_string,
331 im->hw_interface_by_name, hw_if_index);
332}
333
Dave Barachba868bb2016-08-08 09:51:21 -0400334uword
335unformat_vnet_sw_interface (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336{
Dave Barachba868bb2016-08-08 09:51:21 -0400337 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
338 u32 *result = va_arg (*args, u32 *);
339 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 u32 hw_if_index, id, id_specified;
Eyal Bari3212c572017-03-06 11:47:50 +0200341 u32 sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400342 u8 *if_name = 0;
343 uword *p, error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
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 }
Dave Barachba868bb2016-08-08 09:51:21 -0400352 else
353 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 id_specified = 0;
355 else
356 goto done;
357
358 hi = vnet_get_hw_interface (vnm, hw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400359 if (!id_specified)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 {
Eyal Bari3212c572017-03-06 11:47:50 +0200361 sw_if_index = hi->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 }
363 else
364 {
Dave Barachba868bb2016-08-08 09:51:21 -0400365 if (!(p = hash_get (hi->sub_interface_sw_if_index_by_id, id)))
Eyal Bari3212c572017-03-06 11:47:50 +0200366 goto done;
367 sw_if_index = p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 }
Eyal Bari3212c572017-03-06 11:47:50 +0200369 if (!vnet_sw_interface_is_api_visible (vnm, sw_if_index))
370 goto done;
371 *result = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 error = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400373done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 vec_free (if_name);
375 return error;
376}
377
Dave Barachba868bb2016-08-08 09:51:21 -0400378uword
379unformat_vnet_sw_interface_flags (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380{
Dave Barachba868bb2016-08-08 09:51:21 -0400381 u32 *result = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 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
Dave Barachba868bb2016-08-08 09:51:21 -0400399uword
400unformat_vnet_hw_interface_flags (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401{
Dave Barachba868bb2016-08-08 09:51:21 -0400402 u32 *result = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 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
Dave Barachba868bb2016-08-08 09:51:21 -0400416/*
417 * fd.io coding-style-patch-verification: ON
418 *
419 * Local Variables:
420 * eval: (c-set-style "gnu")
421 * End:
422 */