blob: 848cd13a6c351bc1008ec26baac15acab268e692 [file] [log] [blame]
Damjan Marion38c61912023-10-17 16:06:26 +00001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
3 */
4
Damjan Marion38c61912023-10-17 16:06:26 +00005#include <vnet/vnet.h>
6#include <vnet/dev/dev.h>
7#include <vnet/dev/counters.h>
8#include <vnet/ethernet/ethernet.h>
9
10u8 *
11format_vnet_dev_rv (u8 *s, va_list *args)
12{
13 vnet_dev_rv_t rv = va_arg (*args, vnet_dev_rv_t);
14 u32 index = -rv;
15
16 char *strings[] = { [0] = "OK",
17#define _(n, d) [-VNET_DEV_ERR_##n] = d,
18 foreach_vnet_dev_rv_type
19#undef _
20 };
21
22 if (index >= ARRAY_LEN (strings))
23 return format (s, "unknown return value (%d)", rv);
24 return format (s, "%s", strings[index]);
25}
26
27u8 *
28format_vnet_dev_addr (u8 *s, va_list *args)
29{
30 vnet_dev_main_t *dm = &vnet_dev_main;
31 vnet_dev_t *dev = va_arg (*args, vnet_dev_t *);
32 vnet_dev_bus_t *bus;
33
34 if (dev == 0)
35 return 0;
36
37 bus = pool_elt_at_index (dm->buses, dev->bus_index);
38 s = format (s, "%U", bus->ops.format_device_addr, dev);
39
40 return s;
41}
42
43u8 *
44format_vnet_dev_interface_name (u8 *s, va_list *args)
45{
46 u32 i = va_arg (*args, u32);
47 vnet_dev_port_t *port = vnet_dev_get_port_from_dev_instance (i);
48
49 return format (s, "%s", port->intf.name);
50}
51
52u8 *
53format_vnet_dev_info (u8 *s, va_list *args)
54{
55 vnet_dev_format_args_t *a = va_arg (*args, vnet_dev_format_args_t *);
56 vlib_main_t *vm = vlib_get_main ();
57 vnet_dev_main_t *dm = &vnet_dev_main;
58 vnet_dev_t *dev = va_arg (*args, vnet_dev_t *);
59 vnet_dev_driver_t *dr = pool_elt_at_index (dm->drivers, dev->driver_index);
60 vnet_dev_bus_t *bus = pool_elt_at_index (dm->buses, dev->bus_index);
61
62 u32 indent = format_get_indent (s);
63 s = format (s, "Driver is '%s', bus is '%s'", dr->registration->name,
64 bus->registration->name);
65
66 if (dev->description)
67 s = format (s, ", description is '%v'", dev->description);
68
69 if (bus->ops.format_device_info)
70 s = format (s, "\n%U%U", format_white_space, indent,
71 bus->ops.format_device_info, a, dev);
72
73 s = format (s, "\n%UAssigned process node is '%U'", format_white_space,
74 indent, format_vlib_node_name, vm, dev->process_node_index);
Damjan Marion69768d92023-11-13 17:33:32 +000075 if (dev->args)
76 s = format (s, "\n%UDevice Specific Arguments:\n%U%U", format_white_space,
77 indent, format_white_space, indent + 2, format_vnet_dev_args,
78 dev->args);
Damjan Marion38c61912023-10-17 16:06:26 +000079 if (dev->ops.format_info)
Damjan Marion69768d92023-11-13 17:33:32 +000080 s =
81 format (s, "\n%UDevice Specific Info:\n%U%U", format_white_space, indent,
82 format_white_space, indent + 2, dev->ops.format_info, a, dev);
Damjan Marion38c61912023-10-17 16:06:26 +000083 return s;
84}
85
86u8 *
87format_vnet_dev_hw_addr (u8 *s, va_list *args)
88{
89 vnet_dev_hw_addr_t *addr = va_arg (*args, vnet_dev_hw_addr_t *);
90 return format (s, "%U", format_ethernet_address, addr->eth_mac);
91}
92
93u8 *
94format_vnet_dev_port_info (u8 *s, va_list *args)
95{
96 vnet_dev_format_args_t *a = va_arg (*args, vnet_dev_format_args_t *);
97 vlib_main_t *vm = vlib_get_main ();
98 vnet_main_t *vnm = vnet_get_main ();
99 vnet_dev_port_t *port = va_arg (*args, vnet_dev_port_t *);
100
101 u32 indent = format_get_indent (s);
102
103 s = format (s, "Hardware Address is %U", format_vnet_dev_hw_addr,
104 &port->attr.hw_addr);
105 s = format (s, ", %u RX queues (max %u), %u TX queues (max %u)",
106 pool_elts (port->rx_queues), port->attr.max_rx_queues,
107 pool_elts (port->tx_queues), port->attr.max_tx_queues);
108 if (pool_elts (port->secondary_hw_addr))
109 {
110 u32 i = 0;
111 vnet_dev_hw_addr_t *a;
112 s = format (s, "\n%USecondary Hardware Address%s:", format_white_space,
113 indent,
114 pool_elts (port->secondary_hw_addr) > 1 ? "es are" : " is");
115 pool_foreach (a, port->secondary_hw_addr)
116 {
117 if (i++ % 6 == 0)
118 s = format (s, "\n%U", format_white_space, indent + 1);
119 s = format (s, " %U", format_vnet_dev_hw_addr, a);
120 }
121 }
Damjan Marione596ca12023-11-08 19:12:27 +0000122 s = format (s, "\n%UMax RX frame size is %u (max supported %u)",
123 format_white_space, indent, port->max_rx_frame_size,
124 port->attr.max_supported_rx_frame_size);
Damjan Mariond1eb1b72023-12-07 16:40:02 +0100125 s = format (s, "\n%UCaps: %U", format_white_space, indent,
126 format_vnet_dev_port_caps, &port->attr.caps);
127 s = format (s, "\n%URX Offloads: %U", format_white_space, indent,
128 format_vnet_dev_port_rx_offloads, &port->attr.rx_offloads);
129 s = format (s, "\n%UTX Offloads: %U", format_white_space, indent,
130 format_vnet_dev_port_tx_offloads, &port->attr.tx_offloads);
Damjan Marion38c61912023-10-17 16:06:26 +0000131 if (port->port_ops.format_status)
Damjan Marion69768d92023-11-13 17:33:32 +0000132 s = format (s, "\n%UDevice Specific Port Status:\n%U%U",
133 format_white_space, indent, format_white_space, indent + 2,
Damjan Marion38c61912023-10-17 16:06:26 +0000134 port->port_ops.format_status, a, port);
Damjan Marion69768d92023-11-13 17:33:32 +0000135 if (port->args)
136 s = format (s, "\n%UDevice Specific Port Arguments:\n%U%U",
137 format_white_space, indent, format_white_space, indent + 2,
138 format_vnet_dev_args, port->args);
Damjan Marion38c61912023-10-17 16:06:26 +0000139
140 s = format (s, "\n%UInterface ", format_white_space, indent);
141 if (port->interface_created)
142 {
143 s = format (s, "assigned, interface name is '%U', RX node is '%U'",
144 format_vnet_sw_if_index_name, vnm, port->intf.sw_if_index,
145 format_vlib_node_name, vm, port->intf.rx_node_index);
146 }
147 else
148 s = format (s, "not assigned");
149 return s;
150}
151
152u8 *
153format_vnet_dev_rx_queue_info (u8 *s, va_list *args)
154{
155 vnet_dev_format_args_t __clib_unused *a =
156 va_arg (*args, vnet_dev_format_args_t *);
157 vnet_dev_rx_queue_t *rxq = va_arg (*args, vnet_dev_rx_queue_t *);
158 u32 indent = format_get_indent (s);
159
160 s = format (s, "Size is %u, buffer pool index is %u", rxq->size,
161 vnet_dev_get_rx_queue_buffer_pool_index (rxq));
Damjan Marionb8dd9812023-11-03 13:47:05 +0000162 s = format (s, "\n%UPolling thread is %u, %sabled, %sstarted, %s mode",
Damjan Marion38c61912023-10-17 16:06:26 +0000163 format_white_space, indent, rxq->rx_thread_index,
Damjan Marionb8dd9812023-11-03 13:47:05 +0000164 rxq->enabled ? "en" : "dis", rxq->started ? "" : "not-",
165 rxq->interrupt_mode ? "interrupt" : "polling");
Damjan Marion28b6dfa2023-12-21 15:54:14 +0100166 if (rxq->port->rx_queue_ops.format_info)
167 s = format (s, "\n%U%U", format_white_space, indent,
168 rxq->port->rx_queue_ops.format_info, a, rxq);
Damjan Marion38c61912023-10-17 16:06:26 +0000169
170 return s;
171}
172
173u8 *
174format_vnet_dev_tx_queue_info (u8 *s, va_list *args)
175{
176 vnet_dev_format_args_t __clib_unused *a =
177 va_arg (*args, vnet_dev_format_args_t *);
178 vnet_dev_tx_queue_t *txq = va_arg (*args, vnet_dev_tx_queue_t *);
179 u32 indent = format_get_indent (s);
180 u32 n;
181
182 s = format (s, "Size is %u", txq->size);
183 s = format (s, "\n%U", format_white_space, indent);
184 n = clib_bitmap_count_set_bits (txq->assigned_threads);
185 if (n == 0)
186 s = format (s, "Not used by any thread");
187 else
188 s = format (s, "Used by thread%s %U", n > 1 ? "s" : "", format_bitmap_list,
189 txq->assigned_threads);
Damjan Marion28b6dfa2023-12-21 15:54:14 +0100190 if (txq->port->tx_queue_ops.format_info)
191 s = format (s, "\n%U%U", format_white_space, indent,
192 txq->port->tx_queue_ops.format_info, a, txq);
Damjan Marion38c61912023-10-17 16:06:26 +0000193
194 return s;
195}
196
197u8 *
198format_vnet_dev_interface_info (u8 *s, va_list *args)
199{
200 u32 i = va_arg (*args, u32);
201 vnet_dev_format_args_t fa = {}, *a = &fa;
202 vnet_dev_port_t *port = vnet_dev_get_port_from_dev_instance (i);
203 vnet_dev_t *dev = port->dev;
204 u32 indent = format_get_indent (s);
205
206 s = format (s, "Device:");
207 s = format (s, "\n%U%U", format_white_space, indent + 2,
208 format_vnet_dev_info, a, dev);
209
210 s = format (s, "\n%UPort %u:", format_white_space, indent, port->port_id);
211 s = format (s, "\n%U%U", format_white_space, indent + 2,
212 format_vnet_dev_port_info, a, port);
213
214 foreach_vnet_dev_port_rx_queue (q, port)
215 {
216 s = format (s, "\n%URX queue %u:", format_white_space, indent + 2,
217 q->queue_id);
218 s = format (s, "\n%U%U", format_white_space, indent + 4,
219 format_vnet_dev_rx_queue_info, a, q);
220 }
221
222 foreach_vnet_dev_port_tx_queue (q, port)
223 {
224 s = format (s, "\n%UTX queue %u:", format_white_space, indent + 2,
225 q->queue_id);
226 s = format (s, "\n%U%U", format_white_space, indent + 4,
227 format_vnet_dev_tx_queue_info, a, q);
228 }
229 return s;
230}
231
232static u64
233unformat_flags (unformat_input_t *input, char *names[], u64 val[], u32 n_flags)
234{
235 u64 rv = 0;
236 uword c = 0;
237 u8 *s = 0;
238
239 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
240 {
241 switch (c)
242 {
243 case 'a' ... 'z':
244 c -= 'a' - 'A';
245 case '0' ... '9':
246 case 'A' ... 'Z':
247 vec_add1 (s, c);
248 break;
249 case '-':
250 vec_add1 (s, '_');
251 break;
252 case ',':
253 vec_add1 (s, 0);
254 break;
255 default:
256 goto end_of_string;
257 }
258 }
259end_of_string:
260
261 if (s == 0)
262 return 0;
263
264 vec_add1 (s, 0);
265
266 for (u8 *p = s, *end = vec_end (s); p < end; p += strlen ((char *) p) + 1)
267 {
268 for (c = 0; c < n_flags; c++)
269 if (strcmp (names[c], (char *) p) == 0)
270 {
271 rv |= val[c];
272 break;
273 }
274 if (c == n_flags)
275 goto done;
276 }
277
278done:
279 vec_free (s);
280 return rv;
281}
282
283uword
284unformat_vnet_dev_flags (unformat_input_t *input, va_list *args)
285{
286 vnet_dev_flags_t *fp = va_arg (*args, vnet_dev_flags_t *);
287 u64 val;
288
289 char *names[] = {
290#define _(b, n, d) #n,
291 foreach_vnet_dev_flag
292#undef _
293 };
294 u64 vals[] = {
295#define _(b, n, d) 1ull << (b)
296 foreach_vnet_dev_flag
297#undef _
298 };
299
300 val = unformat_flags (input, names, vals, ARRAY_LEN (names));
301
302 if (!val)
303 return 0;
304
305 fp->n = val;
306 return 1;
307}
308
309uword
310unformat_vnet_dev_port_flags (unformat_input_t *input, va_list *args)
311{
312 vnet_dev_port_flags_t *fp = va_arg (*args, vnet_dev_port_flags_t *);
313 u64 val;
314
315 char *flag_names[] = {
316#define _(b, n, d) #n,
317 foreach_vnet_dev_port_flag
318#undef _
319 };
320 u64 flag_values[] = {
321#define _(b, n, d) 1ull << (b)
322 foreach_vnet_dev_port_flag
323#undef _
324 };
325
326 val =
327 unformat_flags (input, flag_names, flag_values, ARRAY_LEN (flag_names));
328
329 if (!val)
330 return 0;
331
332 fp->n = val;
333 return 1;
334}
335
336static u8 *
337format_flags (u8 *s, u64 val, char *flag_names[], u64 flag_values[],
338 u32 n_flags)
339{
340 u32 n = 0;
341 for (int i = 0; i < n_flags; i++)
342 {
343 if ((val & flag_values[i]) == 0)
344 continue;
345
346 if (n++)
347 vec_add1 (s, ' ');
348
349 for (char *c = flag_names[i]; c[0] != 0; c++)
350 {
351 switch (c[0])
352 {
353 case 'A' ... 'Z':
354 vec_add1 (s, c[0] + 'a' - 'A');
355 break;
356 case '_':
357 vec_add1 (s, '-');
358 break;
359 default:
360 vec_add1 (s, c[0]);
361 }
362 }
363 }
364
365 return s;
366}
367
368u8 *
369format_vnet_dev_flags (u8 *s, va_list *args)
370{
371 vnet_dev_flags_t *fp = va_arg (*args, vnet_dev_flags_t *);
372 char *flag_names[] = {
373#define _(b, n, d) #n,
374 foreach_vnet_dev_flag
375#undef _
376 };
377 u64 flag_values[] = {
378#define _(b, n, d) 1ull << (b)
379 foreach_vnet_dev_flag
380#undef _
381 };
382
383 return format_flags (s, fp->n, flag_names, flag_values,
384 ARRAY_LEN (flag_names));
385}
386
387u8 *
388format_vnet_dev_port_flags (u8 *s, va_list *args)
389{
390 vnet_dev_port_flags_t *fp = va_arg (*args, vnet_dev_port_flags_t *);
391 char *flag_names[] = {
392#define _(b, n, d) #n,
393 foreach_vnet_dev_port_flag
394#undef _
395 };
396 u64 flag_values[] = {
397#define _(b, n, d) 1ull << (b)
398 foreach_vnet_dev_port_flag
399#undef _
400 };
401
402 return format_flags (s, fp->n, flag_names, flag_values,
403 ARRAY_LEN (flag_names));
404}
405
406u8 *
407format_vnet_dev_log (u8 *s, va_list *args)
408{
409 vnet_dev_t *dev = va_arg (*args, vnet_dev_t *);
410 char *func = va_arg (*args, char *);
411
412 if (dev)
413 s = format (s, "%U", format_vnet_dev_addr, dev);
414 if (dev && func)
415 vec_add1 (s, ' ');
416 if (func)
Damjan Marion6bd6c802023-11-02 18:40:32 +0000417 s = format (s, "%s", func);
Damjan Marion38c61912023-10-17 16:06:26 +0000418 vec_add1 (s, ':');
419 vec_add1 (s, ' ');
420 return s;
421}
Damjan Mariond1eb1b72023-12-07 16:40:02 +0100422
423u8 *
424format_vnet_dev_port_caps (u8 *s, va_list *args)
425{
426 vnet_dev_port_caps_t *c = va_arg (*args, vnet_dev_port_caps_t *);
427 u32 line = 0;
428
429 if (c->as_number == 0)
430 return s;
431
432#define _(n) \
433 if (c->n) \
434 { \
435 if (line++) \
436 vec_add1 (s, ' '); \
437 for (char *str = #n; *str; str++) \
438 vec_add1 (s, *str == '_' ? '-' : *str); \
439 }
440 foreach_vnet_dev_port_caps;
441#undef _
442
443 return s;
444}
445
446u8 *
447format_vnet_dev_port_rx_offloads (u8 *s, va_list *args)
448{
449 vnet_dev_port_rx_offloads_t *c =
450 va_arg (*args, vnet_dev_port_rx_offloads_t *);
451 u32 line = 0;
452
453 if (c->as_number == 0)
454 return s;
455
456#define _(n) \
457 if (c->n) \
458 { \
459 if (line++) \
460 vec_add1 (s, ' '); \
461 for (char *str = #n; *str; str++) \
462 vec_add1 (s, *str == '_' ? '-' : *str); \
463 }
464 foreach_vnet_dev_port_rx_offloads;
465#undef _
466
467 return s;
468}
469
470u8 *
471format_vnet_dev_port_tx_offloads (u8 *s, va_list *args)
472{
473 vnet_dev_port_tx_offloads_t *c =
474 va_arg (*args, vnet_dev_port_tx_offloads_t *);
475 u32 line = 0;
476
477 if (c->as_number == 0)
478 return s;
479
480#define _(n) \
481 if (c->n) \
482 { \
483 if (line++) \
484 vec_add1 (s, ' '); \
485 for (char *str = #n; *str; str++) \
486 vec_add1 (s, *str == '_' ? '-' : *str); \
487 }
488 foreach_vnet_dev_port_tx_offloads;
489#undef _
490
491 return s;
492}