blob: b3f40354c7a8509a638084edf4d04a41fe3d8666 [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
5#include <vnet/vnet.h>
6#include <vnet/ethernet/ethernet.h>
7#include <vnet/dev/dev.h>
8#include <vnet/dev/counters.h>
9#include <vnet/dev/log.h>
10
11VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
12 .class_name = "dev",
13 .subclass_name = "port",
14};
15
16static uword
17dummy_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
18 vlib_frame_t *frame)
19{
20 ASSERT (0);
21 return 0;
22}
23
24VLIB_REGISTER_NODE (port_rx_eth_node) = {
25 .function = dummy_input_fn,
26 .name = "port-rx-eth",
27 .runtime_data_bytes = sizeof (vnet_dev_rx_node_runtime_t),
28 .type = VLIB_NODE_TYPE_INPUT,
29 .state = VLIB_NODE_STATE_DISABLED,
30 .n_next_nodes = VNET_DEV_ETH_RX_PORT_N_NEXTS,
31 .next_nodes = {
32#define _(n, s) [VNET_DEV_ETH_RX_PORT_NEXT_##n] = s,
33 foreach_vnet_dev_port_rx_next
34#undef _
35 },
36};
37
38u16 vnet_dev_default_next_index_by_port_type[] = {
39 [VNET_DEV_PORT_TYPE_ETHERNET] = VNET_DEV_ETH_RX_PORT_NEXT_ETH_INPUT,
40};
41
42VNET_FEATURE_ARC_INIT (eth_port_rx, static) = {
43 .arc_name = "port-rx-eth",
44 .start_nodes = VNET_FEATURES ("port-rx-eth"),
45 .last_in_arc = "ethernet-input",
46 .arc_index_ptr = &vnet_dev_main.eth_port_rx_feature_arc_index,
47};
48
49VNET_FEATURE_INIT (l2_patch, static) = {
50 .arc_name = "port-rx-eth",
51 .node_name = "l2-patch",
52 .runs_before = VNET_FEATURES ("ethernet-input"),
53};
54
55VNET_FEATURE_INIT (worker_handoff, static) = {
56 .arc_name = "port-rx-eth",
57 .node_name = "worker-handoff",
58 .runs_before = VNET_FEATURES ("ethernet-input"),
59};
60
61VNET_FEATURE_INIT (span_input, static) = {
62 .arc_name = "port-rx-eth",
63 .node_name = "span-input",
64 .runs_before = VNET_FEATURES ("ethernet-input"),
65};
66
67VNET_FEATURE_INIT (p2p_ethernet_node, static) = {
68 .arc_name = "port-rx-eth",
69 .node_name = "p2p-ethernet-input",
70 .runs_before = VNET_FEATURES ("ethernet-input"),
71};
72
73VNET_FEATURE_INIT (ethernet_input, static) = {
74 .arc_name = "port-rx-eth",
75 .node_name = "ethernet-input",
76 .runs_before = 0, /* not before any other features */
77};
78
79void
80vnet_dev_port_free (vlib_main_t *vm, vnet_dev_port_t *port)
81{
82 vnet_dev_t *dev = port->dev;
83
84 vnet_dev_port_validate (vm, port);
85
86 ASSERT (port->started == 0);
87
88 log_debug (dev, "port %u", port->port_id);
89
90 if (port->port_ops.free)
91 port->port_ops.free (vm, port);
92
93 pool_free (port->secondary_hw_addr);
94 pool_free (port->rx_queues);
95 pool_free (port->tx_queues);
Damjan Marion69768d92023-11-13 17:33:32 +000096 vnet_dev_arg_free (&port->args);
Damjan Marion38c61912023-10-17 16:06:26 +000097 pool_put_index (dev->ports, port->index);
98 clib_mem_free (port);
99}
100
101void
102vnet_dev_port_update_tx_node_runtime (vlib_main_t *vm, vnet_dev_port_t *port)
103{
104 vnet_dev_port_validate (vm, port);
105
106 foreach_vnet_dev_port_tx_queue (q, port)
107 {
108 u32 ti;
109 clib_bitmap_foreach (ti, q->assigned_threads)
110 {
111 vlib_main_t *tvm = vlib_get_main_by_index (ti);
112 vlib_node_runtime_t *nr =
113 vlib_node_get_runtime (tvm, port->intf.tx_node_index);
114 vnet_dev_tx_node_runtime_t *tnr = vnet_dev_get_tx_node_runtime (nr);
115 tnr->hw_if_index = port->intf.hw_if_index;
116 tnr->tx_queue = q;
117 }
118 }
119}
120
121void
122vnet_dev_port_stop (vlib_main_t *vm, vnet_dev_port_t *port)
123{
124 vnet_dev_t *dev = port->dev;
125 vnet_dev_rt_op_t *ops = 0;
Damjan Marionb8dd9812023-11-03 13:47:05 +0000126 u16 n_threads = vlib_get_n_threads ();
Damjan Marion38c61912023-10-17 16:06:26 +0000127
128 log_debug (dev, "stopping port %u", port->port_id);
129
Damjan Marionb8dd9812023-11-03 13:47:05 +0000130 for (u16 i = 0; i < n_threads; i++)
131 {
132 vnet_dev_rt_op_t op = { .thread_index = i, .port = port };
133 vec_add1 (ops, op);
134 }
Damjan Marion38c61912023-10-17 16:06:26 +0000135
136 vnet_dev_rt_exec_ops (vm, dev, ops, vec_len (ops));
137 vec_free (ops);
138
139 port->port_ops.stop (vm, port);
140
141 foreach_vnet_dev_port_rx_queue (q, port)
142 {
143 q->started = 0;
144 log_debug (dev, "port %u rx queue %u stopped", port->port_id,
145 q->queue_id);
146 }
147
148 foreach_vnet_dev_port_tx_queue (q, port)
149 {
150 q->started = 0;
151 log_debug (dev, "port %u tx queue %u stopped", port->port_id,
152 q->queue_id);
153 }
154
155 log_debug (dev, "port %u stopped", port->port_id);
156 port->started = 0;
157}
158
159vnet_dev_rv_t
160vnet_dev_port_start_all_rx_queues (vlib_main_t *vm, vnet_dev_port_t *port)
161{
162 vnet_dev_rv_t rv = VNET_DEV_OK;
163
164 vnet_dev_port_validate (vm, port);
165
166 foreach_vnet_dev_port_rx_queue (q, port)
167 {
168 rv = vnet_dev_rx_queue_start (vm, q);
169 if (rv != VNET_DEV_OK)
170 return rv;
171 }
172 return rv;
173}
174
175vnet_dev_rv_t
176vnet_dev_port_start_all_tx_queues (vlib_main_t *vm, vnet_dev_port_t *port)
177{
178 vnet_dev_rv_t rv = VNET_DEV_OK;
179
180 vnet_dev_port_validate (vm, port);
181
182 foreach_vnet_dev_port_tx_queue (q, port)
183 {
184 rv = vnet_dev_tx_queue_start (vm, q);
185 if (rv != VNET_DEV_OK)
186 return rv;
187 }
188 return rv;
189}
190
191vnet_dev_rv_t
192vnet_dev_port_start (vlib_main_t *vm, vnet_dev_port_t *port)
193{
Damjan Marionb8dd9812023-11-03 13:47:05 +0000194 u16 n_threads = vlib_get_n_threads ();
Damjan Marion38c61912023-10-17 16:06:26 +0000195 vnet_dev_t *dev = port->dev;
196 vnet_dev_rt_op_t *ops = 0;
197 vnet_dev_rv_t rv;
198
199 vnet_dev_port_validate (vm, port);
200
201 log_debug (dev, "starting port %u", port->port_id);
202
203 vnet_dev_port_update_tx_node_runtime (vm, port);
204
205 if ((rv = port->port_ops.start (vm, port)) != VNET_DEV_OK)
206 {
207 vnet_dev_port_stop (vm, port);
208 return rv;
209 }
210
Damjan Marionb8dd9812023-11-03 13:47:05 +0000211 for (u16 i = 0; i < n_threads; i++)
212 {
213 vnet_dev_rt_op_t op = { .thread_index = i, .port = port };
214 vec_add1 (ops, op);
215 }
Damjan Marion38c61912023-10-17 16:06:26 +0000216
217 vnet_dev_rt_exec_ops (vm, dev, ops, vec_len (ops));
218 vec_free (ops);
219
220 foreach_vnet_dev_port_rx_queue (q, port)
221 if (q->enabled)
222 {
223 log_debug (dev, "port %u rx queue %u started", port->port_id,
224 q->queue_id);
225 q->started = 1;
226 }
227
228 foreach_vnet_dev_port_tx_queue (q, port)
229 if (q->enabled)
230 {
231 log_debug (dev, "port %u tx queue %u started", port->port_id,
232 q->queue_id);
233 q->started = 1;
234 }
235
236 port->started = 1;
237 log_debug (dev, "port %u started", port->port_id);
238
239 return VNET_DEV_OK;
240}
241
242vnet_dev_rv_t
243vnet_dev_port_add (vlib_main_t *vm, vnet_dev_t *dev, vnet_dev_port_id_t id,
244 vnet_dev_port_add_args_t *args)
245{
246 vnet_dev_port_t **pp, *port;
247 vnet_dev_rv_t rv = VNET_DEV_OK;
248
249 ASSERT (args->port.attr.type != VNET_DEV_PORT_TYPE_UNKNOWN);
Damjan Marione596ca12023-11-08 19:12:27 +0000250 ASSERT (args->port.attr.max_supported_rx_frame_size);
Damjan Marion38c61912023-10-17 16:06:26 +0000251
252 port =
253 vnet_dev_alloc_with_data (sizeof (vnet_dev_port_t), args->port.data_size);
254 pool_get (dev->ports, pp);
255 pp[0] = port;
256 clib_memcpy (vnet_dev_get_port_data (port), args->port.initial_data,
257 args->port.data_size);
258 port->port_id = id;
259 port->index = pp - dev->ports;
260 port->dev = dev;
261 port->attr = args->port.attr;
262 port->rx_queue_config = args->rx_queue.config;
263 port->tx_queue_config = args->tx_queue.config;
264 port->rx_queue_ops = args->rx_queue.ops;
265 port->tx_queue_ops = args->tx_queue.ops;
266 port->port_ops = args->port.ops;
267 port->rx_node = *args->rx_node;
268 port->tx_node = *args->tx_node;
269
Damjan Marion69768d92023-11-13 17:33:32 +0000270 for (vnet_dev_arg_t *a = args->port.args; a->type != VNET_DEV_ARG_END; a++)
271 vec_add1 (port->args, *a);
272
Damjan Marion38c61912023-10-17 16:06:26 +0000273 /* defaults out of port attributes */
Damjan Marione596ca12023-11-08 19:12:27 +0000274 port->max_rx_frame_size = args->port.attr.max_supported_rx_frame_size;
Damjan Marion38c61912023-10-17 16:06:26 +0000275 port->primary_hw_addr = args->port.attr.hw_addr;
276
Damjan Marione596ca12023-11-08 19:12:27 +0000277 if (port->attr.type == VNET_DEV_PORT_TYPE_ETHERNET)
278 {
279 if (port->max_rx_frame_size > 1514 &&
280 port->attr.caps.change_max_rx_frame_size)
281 port->max_rx_frame_size = 1514;
282 }
283
Damjan Marion38c61912023-10-17 16:06:26 +0000284 if (port->port_ops.alloc)
285 rv = port->port_ops.alloc (vm, port);
286
287 if (rv == VNET_DEV_OK)
288 port->initialized = 1;
289
290 return rv;
291}
292
293vnet_dev_rv_t
294vnet_dev_port_cfg_change_req_validate (vlib_main_t *vm, vnet_dev_port_t *port,
295 vnet_dev_port_cfg_change_req_t *req)
296{
297 vnet_dev_rv_t rv;
298 vnet_dev_hw_addr_t *addr;
299 int found;
300
301 if (req->validated)
302 return VNET_DEV_OK;
303
304 switch (req->type)
305 {
Damjan Marione596ca12023-11-08 19:12:27 +0000306 case VNET_DEV_PORT_CFG_MAX_RX_FRAME_SIZE:
307 if (req->max_rx_frame_size > port->attr.max_supported_rx_frame_size)
Damjan Marion38c61912023-10-17 16:06:26 +0000308 return VNET_DEV_ERR_INVALID_VALUE;
Damjan Marione596ca12023-11-08 19:12:27 +0000309 if (req->max_rx_frame_size == port->max_rx_frame_size)
Damjan Marion38c61912023-10-17 16:06:26 +0000310 return VNET_DEV_ERR_NO_CHANGE;
311 break;
312
313 case VNET_DEV_PORT_CFG_PROMISC_MODE:
314 if (req->promisc == port->promisc)
315 return VNET_DEV_ERR_NO_CHANGE;
316 break;
317
318 case VNET_DEV_PORT_CFG_CHANGE_PRIMARY_HW_ADDR:
319 if (clib_memcmp (&req->addr, &port->primary_hw_addr,
320 sizeof (vnet_dev_hw_addr_t)) == 0)
321 return VNET_DEV_ERR_NO_CHANGE;
322 break;
323
324 case VNET_DEV_PORT_CFG_ADD_SECONDARY_HW_ADDR:
325 pool_foreach (addr, port->secondary_hw_addr)
326 if (clib_memcmp (addr, &req->addr, sizeof (*addr)) == 0)
327 return VNET_DEV_ERR_ALREADY_EXISTS;
328 break;
329
330 case VNET_DEV_PORT_CFG_REMOVE_SECONDARY_HW_ADDR:
331 found = 0;
332 pool_foreach (addr, port->secondary_hw_addr)
333 if (clib_memcmp (addr, &req->addr, sizeof (*addr)) == 0)
334 found = 1;
335 if (!found)
336 return VNET_DEV_ERR_NO_SUCH_ENTRY;
337 break;
338
339 default:
340 break;
341 }
342
343 if (port->port_ops.config_change_validate)
344 {
345 rv = port->port_ops.config_change_validate (vm, port, req);
346 if (rv != VNET_DEV_OK)
347 return rv;
348 }
Damjan Marione596ca12023-11-08 19:12:27 +0000349 else
350 return VNET_DEV_ERR_NOT_SUPPORTED;
Damjan Marion38c61912023-10-17 16:06:26 +0000351
352 req->validated = 1;
353 return VNET_DEV_OK;
354}
355
356vnet_dev_rv_t
357vnet_dev_port_cfg_change (vlib_main_t *vm, vnet_dev_port_t *port,
358 vnet_dev_port_cfg_change_req_t *req)
359{
360 vnet_dev_rv_t rv = VNET_DEV_OK;
361 vnet_dev_hw_addr_t *a;
Damjan Marionb8dd9812023-11-03 13:47:05 +0000362 vnet_dev_rx_queue_t *rxq = 0;
363 u8 enable = 0;
Damjan Marion38c61912023-10-17 16:06:26 +0000364
365 vnet_dev_port_validate (vm, port);
366
Damjan Marionb8dd9812023-11-03 13:47:05 +0000367 if (req->type == VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE ||
368 req->type == VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE)
369 {
370 if (req->all_queues == 0)
371 {
372 rxq = vnet_dev_port_get_rx_queue_by_id (port, req->queue_id);
373 if (rxq == 0)
374 return VNET_DEV_ERR_BUG;
375 }
376 }
377
378 if ((rv = vnet_dev_port_cfg_change_req_validate (vm, port, req)))
379 return rv;
Damjan Marion38c61912023-10-17 16:06:26 +0000380
381 if (port->port_ops.config_change)
382 rv = port->port_ops.config_change (vm, port, req);
Damjan Marione596ca12023-11-08 19:12:27 +0000383 else
384 return VNET_DEV_ERR_NOT_SUPPORTED;
Damjan Marion38c61912023-10-17 16:06:26 +0000385
386 if (rv != VNET_DEV_OK)
387 return rv;
388
389 switch (req->type)
390 {
Damjan Marione596ca12023-11-08 19:12:27 +0000391 case VNET_DEV_PORT_CFG_MAX_RX_FRAME_SIZE:
392 port->max_rx_frame_size = req->max_rx_frame_size;
Damjan Marion38c61912023-10-17 16:06:26 +0000393 break;
394
395 case VNET_DEV_PORT_CFG_PROMISC_MODE:
396 port->promisc = req->promisc;
397 break;
398
Damjan Marionb8dd9812023-11-03 13:47:05 +0000399 case VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE:
400 enable = 1;
401 case VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE:
402 if (req->all_queues)
403 {
404 clib_bitmap_t *bmp = 0;
405 vnet_dev_rt_op_t *ops = 0;
406 u32 i;
407
408 foreach_vnet_dev_port_rx_queue (q, port)
409 {
410 q->interrupt_mode = enable;
411 bmp = clib_bitmap_set (bmp, q->rx_thread_index, 1);
412 }
413
414 clib_bitmap_foreach (i, bmp)
415 {
416 vnet_dev_rt_op_t op = { .port = port, .thread_index = i };
417 vec_add1 (ops, op);
418 }
419
420 vnet_dev_rt_exec_ops (vm, port->dev, ops, vec_len (ops));
421 clib_bitmap_free (bmp);
422 vec_free (ops);
423 }
424 else
425 {
426 rxq->interrupt_mode = enable;
427 vnet_dev_rt_exec_ops (vm, port->dev,
428 &(vnet_dev_rt_op_t){
429 .port = port,
430 .thread_index = rxq->rx_thread_index,
431 },
432 1);
433 }
434 break;
435
Damjan Marion38c61912023-10-17 16:06:26 +0000436 case VNET_DEV_PORT_CFG_CHANGE_PRIMARY_HW_ADDR:
437 clib_memcpy (&port->primary_hw_addr, &req->addr,
438 sizeof (vnet_dev_hw_addr_t));
439 break;
440
441 case VNET_DEV_PORT_CFG_ADD_SECONDARY_HW_ADDR:
442 pool_get (port->secondary_hw_addr, a);
443 clib_memcpy (a, &req->addr, sizeof (vnet_dev_hw_addr_t));
444 break;
445
446 case VNET_DEV_PORT_CFG_REMOVE_SECONDARY_HW_ADDR:
447 pool_foreach (a, port->secondary_hw_addr)
448 if (clib_memcmp (a, &req->addr, sizeof (vnet_dev_hw_addr_t)) == 0)
449 {
450 pool_put (port->secondary_hw_addr, a);
451 break;
452 }
453 break;
454
455 default:
456 break;
457 }
458
459 return VNET_DEV_OK;
460}
461
462void
463vnet_dev_port_state_change (vlib_main_t *vm, vnet_dev_port_t *port,
464 vnet_dev_port_state_changes_t changes)
465{
466 vnet_main_t *vnm = vnet_get_main ();
467
468 vnet_dev_port_validate (vm, port);
469
470 if (changes.change.link_speed)
471 {
472 port->speed = changes.link_speed;
473 if (port->interface_created)
474 vnet_hw_interface_set_link_speed (vnm, port->intf.hw_if_index,
475 changes.link_speed);
476 log_debug (port->dev, "port speed changed to %u", changes.link_speed);
477 }
478
479 if (changes.change.link_state)
480 {
481 port->link_up = changes.link_state;
482 if (port->interface_created)
483 vnet_hw_interface_set_flags (
484 vnm, port->intf.hw_if_index,
485 changes.link_state ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
486 log_debug (port->dev, "port link state changed to %s",
487 changes.link_state ? "up" : "down");
488 }
489}
490
491void
492vnet_dev_port_add_counters (vlib_main_t *vm, vnet_dev_port_t *port,
493 vnet_dev_counter_t *counters, u16 n_counters)
494{
495 vnet_dev_port_validate (vm, port);
496
497 port->counter_main =
498 vnet_dev_counters_alloc (vm, counters, n_counters, "%s port %u counters",
499 port->dev->device_id, port->port_id);
500}
501
502void
503vnet_dev_port_free_counters (vlib_main_t *vm, vnet_dev_port_t *port)
504{
505 vnet_dev_port_validate (vm, port);
506
507 if (port->counter_main)
508 vnet_dev_counters_free (vm, port->counter_main);
509}
510
511vnet_dev_rv_t
512vnet_dev_port_if_create (vlib_main_t *vm, vnet_dev_port_t *port)
513{
514 vnet_main_t *vnm = vnet_get_main ();
515 u16 n_threads = vlib_get_n_threads ();
516 vnet_dev_main_t *dm = &vnet_dev_main;
517 vnet_dev_t *dev = port->dev;
518 vnet_dev_port_t **pp;
519 vnet_dev_rv_t rv;
520 u16 ti = 0;
521
522 if (port->intf.name[0] == 0)
523 {
524 u8 *s;
525 s = format (0, "%s%u/%u",
526 dm->drivers[port->dev->driver_index].registration->name,
527 port->dev->index, port->index);
528 u32 n = vec_len (s);
529
530 if (n >= sizeof (port->intf.name))
531 {
532 vec_free (s);
533 return VNET_DEV_ERR_BUG;
534 }
535 clib_memcpy (port->intf.name, s, n);
536 port->intf.name[n] = 0;
537 vec_free (s);
538 }
539
540 log_debug (
541 dev, "allocating %u rx queues with size %u and %u tx queues with size %u",
542 port->intf.num_rx_queues, port->intf.rxq_sz, port->intf.num_tx_queues,
543 port->intf.txq_sz);
544
545 for (int i = 0; i < port->intf.num_rx_queues; i++)
546 if ((rv = vnet_dev_rx_queue_alloc (vm, port, port->intf.rxq_sz)) !=
547 VNET_DEV_OK)
548 goto error;
549
550 for (u32 i = 0; i < port->intf.num_tx_queues; i++)
551 if ((rv = vnet_dev_tx_queue_alloc (vm, port, port->intf.txq_sz)) !=
552 VNET_DEV_OK)
553 goto error;
554
555 foreach_vnet_dev_port_tx_queue (q, port)
556 {
557 q->assigned_threads = clib_bitmap_set (q->assigned_threads, ti, 1);
558 log_debug (dev, "port %u tx queue %u assigned to thread %u",
559 port->port_id, q->queue_id, ti);
560 if (++ti >= n_threads)
561 break;
562 }
563
564 /* pool of port pointers helps us to assign unique dev_instance */
565 pool_get (dm->ports_by_dev_instance, pp);
566 port->intf.dev_instance = pp - dm->ports_by_dev_instance;
567 pp[0] = port;
568
569 if (port->attr.type == VNET_DEV_PORT_TYPE_ETHERNET)
570 {
571 vnet_device_class_t *dev_class;
572 vnet_dev_driver_t *driver;
573 vnet_sw_interface_t *sw;
574 vnet_hw_interface_t *hw;
575 u32 rx_node_index;
576
577 driver = pool_elt_at_index (dm->drivers, dev->driver_index);
578
579 /* hack to provide per-port tx node function */
580 dev_class = vnet_get_device_class (vnm, driver->dev_class_index);
581 dev_class->tx_fn_registrations = port->tx_node.registrations;
582 dev_class->format_tx_trace = port->tx_node.format_trace;
583 dev_class->tx_function_error_counters = port->tx_node.error_counters;
584 dev_class->tx_function_n_errors = port->tx_node.n_error_counters;
585
586 /* create new interface including tx and output nodes */
587 port->intf.hw_if_index = vnet_eth_register_interface (
588 vnm, &(vnet_eth_interface_registration_t){
589 .address = port->primary_hw_addr.eth_mac,
Damjan Marione596ca12023-11-08 19:12:27 +0000590 .max_frame_size = port->max_rx_frame_size,
Damjan Marion38c61912023-10-17 16:06:26 +0000591 .dev_class_index = driver->dev_class_index,
592 .dev_instance = port->intf.dev_instance,
593 .cb.set_max_frame_size = vnet_dev_port_set_max_frame_size,
594 .cb.flag_change = vnet_dev_port_eth_flag_change,
595 });
596
597 sw = vnet_get_hw_sw_interface (vnm, port->intf.hw_if_index);
598 hw = vnet_get_hw_interface (vnm, port->intf.hw_if_index);
599 port->intf.sw_if_index = sw->sw_if_index;
600 vnet_hw_interface_set_flags (
601 vnm, port->intf.hw_if_index,
602 port->link_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
603 if (port->speed)
604 vnet_hw_interface_set_link_speed (vnm, port->intf.hw_if_index,
605 port->speed);
606
607 port->intf.tx_node_index = hw->tx_node_index;
608
609 /* create / reuse rx node */
610 if (vec_len (dm->free_rx_node_indices))
611 {
612 vlib_node_t *n;
613 rx_node_index = vec_pop (dm->free_rx_node_indices);
614 vlib_node_rename (vm, rx_node_index, "%s-rx", port->intf.name);
615 n = vlib_get_node (vm, rx_node_index);
616 n->function = vlib_node_get_preferred_node_fn_variant (
617 vm, port->rx_node.registrations);
618 n->format_trace = port->rx_node.format_trace;
619 vlib_register_errors (vm, rx_node_index,
620 port->rx_node.n_error_counters, 0,
621 port->rx_node.error_counters);
622 }
623 else
624 {
625 dev_class->format_tx_trace = port->tx_node.format_trace;
626 dev_class->tx_function_error_counters = port->tx_node.error_counters;
627 dev_class->tx_function_n_errors = port->tx_node.n_error_counters;
628 vlib_node_registration_t rx_node_reg = {
629 .sibling_of = "port-rx-eth",
630 .type = VLIB_NODE_TYPE_INPUT,
631 .state = VLIB_NODE_STATE_DISABLED,
632 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
633 .node_fn_registrations = port->rx_node.registrations,
634 .format_trace = port->rx_node.format_trace,
635 .error_counters = port->rx_node.error_counters,
636 .n_errors = port->rx_node.n_error_counters,
637 };
638 rx_node_index =
639 vlib_register_node (vm, &rx_node_reg, "%s-rx", port->intf.name);
640 }
641 port->rx_node_assigned = 1;
642 port->intf.rx_node_index = rx_node_index;
643 port->intf.rx_next_index =
644 vnet_dev_default_next_index_by_port_type[port->attr.type];
645
646 vlib_worker_thread_node_runtime_update ();
647 log_debug (dev,
648 "ethernet interface created, hw_if_index %u sw_if_index %u "
649 "rx_node_index %u tx_node_index %u",
650 port->intf.hw_if_index, port->intf.sw_if_index,
651 port->intf.rx_node_index, port->intf.tx_node_index);
652 }
653
654 port->interface_created = 1;
655 foreach_vnet_dev_port_rx_queue (q, port)
656 {
657 vnet_buffer (&q->buffer_template)->sw_if_index[VLIB_RX] =
658 port->intf.sw_if_index;
659 /* poison to catch node not calling runtime update function */
660 q->next_index = ~0;
Damjan Marionb8dd9812023-11-03 13:47:05 +0000661 q->interrupt_mode = port->intf.default_is_intr_mode;
Damjan Marion38c61912023-10-17 16:06:26 +0000662 vnet_dev_rx_queue_rt_request (
663 vm, q, (vnet_dev_rx_queue_rt_req_t){ .update_next_index = 1 });
664 }
665
666 vnet_dev_port_update_tx_node_runtime (vm, port);
667
668 if (port->port_ops.init)
669 rv = port->port_ops.init (vm, port);
670
671error:
672 if (rv != VNET_DEV_OK)
673 vnet_dev_port_if_remove (vm, port);
674 return rv;
675}
676
677vnet_dev_rv_t
678vnet_dev_port_if_remove (vlib_main_t *vm, vnet_dev_port_t *port)
679{
680 vnet_dev_main_t *dm = &vnet_dev_main;
681 vnet_main_t *vnm = vnet_get_main ();
682
683 vnet_dev_port_validate (vm, port);
684
685 if (port->started)
686 vnet_dev_port_stop (vm, port);
687
688 if (port->rx_node_assigned)
689 {
690 vlib_node_rename (vm, port->intf.rx_node_index, "deleted-%u",
691 port->intf.rx_node_index);
692 vec_add1 (dm->free_rx_node_indices, port->intf.rx_node_index);
693 port->rx_node_assigned = 0;
694 }
695
696 if (port->interface_created)
697 {
698 vlib_worker_thread_barrier_sync (vm);
699 vnet_delete_hw_interface (vnm, port->intf.hw_if_index);
700 vlib_worker_thread_barrier_release (vm);
701 pool_put_index (dm->ports_by_dev_instance, port->intf.dev_instance);
702 port->interface_created = 0;
703 }
704
705 port->intf = (typeof (port->intf)){};
706
707 if (port->port_ops.deinit)
708 port->port_ops.deinit (vm, port);
709
710 foreach_vnet_dev_port_tx_queue (q, port)
711 vnet_dev_tx_queue_free (vm, q);
712
713 foreach_vnet_dev_port_rx_queue (q, port)
714 vnet_dev_rx_queue_free (vm, q);
715
716 vnet_dev_port_free_counters (vm, port);
717
Damjan Marion69768d92023-11-13 17:33:32 +0000718 foreach_vnet_dev_port_args (v, port)
719 vnet_dev_arg_clear_value (v);
720
Damjan Marion38c61912023-10-17 16:06:26 +0000721 return VNET_DEV_OK;
722}
723void
724vnet_dev_port_clear_counters (vlib_main_t *vm, vnet_dev_port_t *port)
725{
726 if (port->counter_main)
727 vnet_dev_counters_clear (vm, port->counter_main);
728
729 foreach_vnet_dev_port_rx_queue (q, port)
730 if (q->counter_main)
731 vnet_dev_counters_clear (vm, q->counter_main);
732
733 foreach_vnet_dev_port_tx_queue (q, port)
734 if (q->counter_main)
735 vnet_dev_counters_clear (vm, q->counter_main);
736
737 log_notice (port->dev, "counters cleared on port %u", port->port_id);
738}