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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * interface_format.c: interface formatting
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vppinfra/bitmap.h>
44 format_vnet_sw_interface_flags (u8 * s, va_list * args)
46 u32 flags = va_arg (*args, u32);
48 if (flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
49 s = format (s, "bond-slave");
53 (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "down");
54 if (flags & VNET_SW_INTERFACE_FLAG_PUNT)
55 s = format (s, "/punt");
62 format_vnet_hw_interface_rx_mode (u8 * s, va_list * args)
64 vnet_hw_interface_rx_mode mode = va_arg (*args, vnet_hw_interface_rx_mode);
66 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
67 return format (s, "polling");
69 if (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT)
70 return format (s, "interrupt");
72 if (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)
73 return format (s, "adaptive");
75 return format (s, "unknown");
79 format_vnet_hw_interface (u8 * s, va_list * args)
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);
89 return format (s, "%=32s%=6s%=8s%s", "Name", "Idx", "Link", "Hardware");
91 indent = format_get_indent (s);
93 s = format (s, "%-32v%=6d", hi->name, hi->hw_if_index);
95 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
96 s = format (s, "%=8s", "slave");
98 s = format (s, "%=8s",
99 hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP ? "up" : "down");
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);
104 if (hi->bond_info && (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
107 s = format (s, "Slave-Idx:");
108 clib_bitmap_foreach (hw_idx, hi->bond_info, s =
109 format (s, " %d", hw_idx));
111 else if (dev_class->format_device_name)
112 s = format (s, "%U", dev_class->format_device_name, hi->dev_instance);
114 s = format (s, "%s%d", dev_class->name, hi->dev_instance);
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);
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)
128 format (s, " address %U", hw_class->format_address,
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);
142 format_vnet_sw_interface_name (u8 * s, va_list * args)
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;
150 ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
151 hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
153 s = format (s, "%v", hi_sup->name);
155 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
156 s = format (s, ".%d", si->sub.id);
162 format_vnet_sw_if_index_name (u8 * s, va_list * args)
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));
172 format_vnet_sw_interface_cntrs (u8 * s, vnet_interface_main_t * im,
173 vnet_sw_interface_t * si)
175 uword indent, n_printed;
176 int i, j, n_counters;
177 static vnet_main_t **my_vnet_mains;
179 vec_reset_length (my_vnet_mains);
181 indent = format_get_indent (s);
185 vlib_combined_counter_main_t *cm;
186 vlib_counter_t v, vtotal;
189 for (i = 0; i < vec_len (vnet_mains); i++)
192 vec_add1 (my_vnet_mains, vnet_mains[i]);
195 if (vec_len (my_vnet_mains) == 0)
196 vec_add1 (my_vnet_mains, &vnet_main);
198 /* Each vnet_main_t has its own copy of the interface counters */
199 n_counters = vec_len (im->combined_sw_if_counters);
201 /* rx, tx counters... */
202 for (j = 0; j < n_counters; j++)
207 for (i = 0; i < vec_len (my_vnet_mains); i++)
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;
216 /* Only display non-zero counters. */
217 if (vtotal.packets == 0)
221 s = format (s, "\n%U", format_white_space, indent);
226 n = format (n, "%s packets", cm->name);
227 s = format (s, "%-16v%16Ld", n, vtotal.packets);
230 n = format (n, "%s bytes", cm->name);
231 s = format (s, "\n%U%-16v%16Ld",
232 format_white_space, indent, n, vtotal.bytes);
238 vlib_simple_counter_main_t *cm;
241 n_counters = vec_len (im->sw_if_counters);
243 for (j = 0; j < n_counters; j++)
247 for (i = 0; i < vec_len (my_vnet_mains); i++)
249 im = &my_vnet_mains[i]->interface_main;
250 cm = im->sw_if_counters + j;
252 v = vlib_get_simple_counter (cm, si->sw_if_index);
256 /* Only display non-zero counters. */
261 s = format (s, "\n%U", format_white_space, indent);
264 s = format (s, "%-16s%16Ld", cm->name, vtotal);
272 format_vnet_sw_interface (u8 * s, va_list * args)
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;
279 return format (s, "%=32s%=5s%=16s%=16s%=16s",
280 "Name", "Idx", "State", "Counter", "Count");
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);
286 s = format_vnet_sw_interface_cntrs (s, im, si);
292 format_vnet_sw_interface_name_override (u8 * s, va_list * args)
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;
302 return format (s, "%=32s%=5s%=16s%=16s%=16s",
303 "Name", "Idx", "State", "Counter", "Count");
305 s = format (s, "%-32v%=5d%=16U",
306 name, si->sw_if_index,
307 format_vnet_sw_interface_flags, si->flags);
309 s = format_vnet_sw_interface_cntrs (s, im, si);
315 unformat_vnet_hw_interface (unformat_input_t * input, va_list * args)
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;
322 /* Try per device class functions first. */
323 vec_foreach (c, im->device_classes)
325 if (c->unformat_device_name
326 && unformat_user (input, c->unformat_device_name, hw_if_index))
330 return unformat_user (input, unformat_hash_vec_string,
331 im->hw_interface_by_name, hw_if_index);
335 unformat_vnet_sw_interface (unformat_input_t * input, va_list * args)
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;
346 if (unformat (input, "%_%v.%d%_", &if_name, &id)
347 && ((p = hash_get (vnm->interface_main.hw_interface_by_name, if_name))))
353 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
358 hi = vnet_get_hw_interface (vnm, hw_if_index);
361 sw_if_index = hi->sw_if_index;
365 if (!(p = hash_get (hi->sub_interface_sw_if_index_by_id, id)))
369 if (!vnet_sw_interface_is_api_visible (vnm, sw_if_index))
371 *result = sw_if_index;
379 unformat_vnet_sw_interface_flags (unformat_input_t * input, va_list * args)
381 u32 *result = va_arg (*args, u32 *);
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;
400 unformat_vnet_hw_interface_flags (unformat_input_t * input, va_list * args)
402 u32 *result = va_arg (*args, u32 *);
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;
417 * fd.io coding-style-patch-verification: ON
420 * eval: (c-set-style "gnu")