2 /* SPDX-License-Identifier: Apache-2.0
3 * Copyright (c) 2023 Cisco Systems, Inc.
6 #include "vppinfra/bitmap.h"
7 #include "vppinfra/lock.h"
9 #include <vnet/dev/dev.h>
10 #include <vnet/dev/log.h>
12 VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
14 .subclass_name = "runtime",
17 static vnet_dev_rt_op_t *rt_ops;
20 _vnet_dev_rt_exec_op (vlib_main_t *vm, vnet_dev_rt_op_t *op)
22 if (op->type == VNET_DEV_RT_OP_TYPE_RX_QUEUE)
24 vnet_dev_rx_node_runtime_t *rtd;
25 vnet_dev_rx_queue_t *rxq = op->rx_queue;
26 u32 i, node_index = rxq->port->intf.rx_node_index;
28 rtd = vlib_node_get_runtime_data (vm, node_index);
30 if (op->action == VNET_DEV_RT_OP_ACTION_START)
32 for (i = 0; i < rtd->n_rx_queues; i++)
33 ASSERT (rtd->rx_queues[i] != op->rx_queue);
34 rtd->rx_queues[rtd->n_rx_queues++] = op->rx_queue;
37 else if (op->action == VNET_DEV_RT_OP_ACTION_STOP)
39 for (i = 0; i < rtd->n_rx_queues; i++)
40 if (rtd->rx_queues[i] == op->rx_queue)
42 ASSERT (i < rtd->n_rx_queues);
44 for (; i < rtd->n_rx_queues; i++)
45 rtd->rx_queues[i] = rtd->rx_queues[i + 1];
48 if (rtd->n_rx_queues == 1)
49 vlib_node_set_state (vm, node_index, VLIB_NODE_STATE_POLLING);
50 else if (rtd->n_rx_queues == 0)
51 vlib_node_set_state (vm, node_index, VLIB_NODE_STATE_DISABLED);
53 __atomic_store_n (&op->completed, 1, __ATOMIC_RELEASE);
58 _vnet_dev_rt_op_not_occured_before (vnet_dev_rt_op_t *first,
59 vnet_dev_rt_op_t *current)
61 for (vnet_dev_rt_op_t *op = first; op < current; op++)
62 if (op->rx_queue == current->rx_queue && op->completed == 0)
68 vnet_dev_rt_mgmt_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
71 u16 thread_index = vm->thread_index;
72 vnet_dev_rt_op_t *ops = __atomic_load_n (&rt_ops, __ATOMIC_ACQUIRE);
78 if (op->thread_index == thread_index)
80 if (_vnet_dev_rt_op_not_occured_before (ops, op))
82 _vnet_dev_rt_exec_op (vm, op);
90 vlib_node_set_interrupt_pending (vm, node->node_index);
95 VLIB_REGISTER_NODE (vnet_dev_rt_mgmt_node, static) = {
96 .function = vnet_dev_rt_mgmt_node_fn,
97 .name = "dev-rt-mgmt",
98 .type = VLIB_NODE_TYPE_PRE_INPUT,
99 .state = VLIB_NODE_STATE_INTERRUPT,
103 format_vnet_dev_mgmt_op (u8 *s, va_list *args)
105 vnet_dev_rt_op_t *op = va_arg (*args, vnet_dev_rt_op_t *);
108 [VNET_DEV_RT_OP_TYPE_RX_QUEUE] = "rx queue",
111 [VNET_DEV_RT_OP_ACTION_START] = "start",
112 [VNET_DEV_RT_OP_ACTION_STOP] = "stop",
115 return format (s, "port %u %s %u %s on thread %u",
116 op->rx_queue->port->port_id, types[op->type],
117 op->rx_queue->queue_id, actions[op->action],
122 vnet_dev_rt_exec_ops (vlib_main_t *vm, vnet_dev_t *dev, vnet_dev_rt_op_t *ops,
125 vnet_dev_rt_op_t *op = ops;
126 vnet_dev_rt_op_t *remote_ops = 0;
127 clib_bitmap_t *remote_bmp = 0;
130 ASSERT (rt_ops == 0);
132 for (op = ops; op < (ops + n_ops); op++)
134 vlib_main_t *tvm = vlib_get_main_by_index (op->thread_index);
136 if ((vlib_worker_thread_barrier_held ()) ||
137 (op->thread_index == vm->thread_index &&
138 _vnet_dev_rt_op_not_occured_before (ops, op)))
140 _vnet_dev_rt_exec_op (tvm, op);
141 log_debug (dev, "%U executed locally", format_vnet_dev_mgmt_op, op);
145 vec_add1 (remote_ops, *op);
146 log_debug (dev, "%U enqueued for remote execution",
147 format_vnet_dev_mgmt_op, op);
148 remote_bmp = clib_bitmap_set (remote_bmp, op->thread_index, 1);
154 __atomic_store_n (&rt_ops, remote_ops, __ATOMIC_RELEASE);
156 clib_bitmap_foreach (i, remote_bmp)
158 vlib_node_set_interrupt_pending (vlib_get_main_by_index (i),
159 vnet_dev_rt_mgmt_node.index);
160 log_debug (dev, "interrupt sent to %s node on thread %u",
161 vnet_dev_rt_mgmt_node.name, i);
164 vec_foreach (op, remote_ops)
166 while (op->completed == 0)
170 __atomic_store_n (&rt_ops, 0, __ATOMIC_RELAXED);
171 vec_free (remote_ops);
172 clib_bitmap_free (remote_bmp);