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>
42 u8 * format_vnet_sw_interface_flags (u8 * s, va_list * args)
44 u32 flags = va_arg (*args, u32);
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");
53 u8 * format_vnet_hw_interface (u8 * s, va_list * args)
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);
63 return format (s, "%=32s%=6s%=8s%s",
64 "Name", "Idx", "Link", "Hardware");
66 indent = format_get_indent (s);
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");
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);
75 if (dev_class->format_device_name)
76 s = format (s, "%U", dev_class->format_device_name, hi->dev_instance);
78 s = format (s, "%s%d", dev_class->name, hi->dev_instance);
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);
88 s = format (s, "\n%U%s",
89 format_white_space, indent + 2,
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);
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);
104 u8 * format_vnet_sw_interface_name (u8 * s, va_list * args)
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;
111 ASSERT (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
112 hi_sup = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
114 s = format (s, "%v", hi_sup->name);
116 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
117 s = format (s, ".%d", si->sub.id);
122 u8 * format_vnet_sw_if_index_name (u8 * s, va_list * args)
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));
131 u8 * format_vnet_sw_interface (u8 * s, va_list * args)
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;
141 return format (s, "%=32s%=5s%=16s%=16s%=16s",
142 "Name", "Idx", "State", "Counter", "Count");
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);
148 vec_reset_length (my_vnet_mains);
150 indent = format_get_indent (s);
154 vlib_combined_counter_main_t * cm;
155 vlib_counter_t v, vtotal;
158 for (i = 0; i < vec_len (vnet_mains); i++)
161 vec_add1 (my_vnet_mains, vnet_mains[i]);
164 if (vec_len(my_vnet_mains) == 0)
165 vec_add1 (my_vnet_mains, &vnet_main);
167 /* Each vnet_main_t has its own copy of the interface counters */
168 n_counters = vec_len (im->combined_sw_if_counters);
170 /* rx, tx counters... */
171 for (j = 0; j < n_counters; j++)
176 for (i = 0; i < vec_len(my_vnet_mains); i++)
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;
185 /* Only display non-zero counters. */
186 if (vtotal.packets == 0)
190 s = format (s, "\n%U", format_white_space, indent);
195 n = format (n, "%s packets", cm->name);
196 s = format (s, "%-16v%16Ld", n, vtotal.packets);
199 n = format (n, "%s bytes", cm->name);
200 s = format (s, "\n%U%-16v%16Ld",
201 format_white_space, indent,
208 vlib_simple_counter_main_t * cm;
211 n_counters = vec_len (im->sw_if_counters);
213 for (j = 0; j < n_counters; j++)
217 for (i = 0; i < vec_len(my_vnet_mains); i++)
219 im = &my_vnet_mains[i]->interface_main;
220 cm = im->sw_if_counters + j;
222 v = vlib_get_simple_counter (cm, si->sw_if_index);
226 /* Only display non-zero counters. */
231 s = format (s, "\n%U", format_white_space, indent);
234 s = format (s, "%-16s%16Ld", cm->name, vtotal);
241 uword unformat_vnet_hw_interface (unformat_input_t * input, va_list * args)
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;
248 /* Try per device class functions first. */
249 vec_foreach (c, im->device_classes)
251 if (c->unformat_device_name
252 && unformat_user (input, c->unformat_device_name, hw_if_index))
256 return unformat_user (input, unformat_hash_vec_string,
257 im->hw_interface_by_name, hw_if_index);
260 uword unformat_vnet_sw_interface (unformat_input_t * input, va_list * args)
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;
267 uword * p, error = 0;
270 if (unformat (input, "%_%v.%d%_", &if_name, &id)
271 && ((p = hash_get (vnm->interface_main.hw_interface_by_name, if_name))))
276 else if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
281 hi = vnet_get_hw_interface (vnm, hw_if_index);
284 *result = hi->sw_if_index;
288 if (! (p = hash_get (hi->sub_interface_sw_if_index_by_id, id)))
298 uword unformat_vnet_sw_interface_flags (unformat_input_t * input, va_list * args)
300 u32 * result = va_arg (*args, u32 *);
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;
318 uword unformat_vnet_hw_interface_flags (unformat_input_t * input, va_list * args)
320 u32 * result = va_arg (*args, u32 *);
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;