2 * Copyright (c) 2020 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 #include <vnet/vnet.h>
17 #include <vnet/devices/devices.h>
18 #include <vnet/feature/feature.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/interface/rx_queue_funcs.h>
22 #include <vlib/unix/unix.h>
24 VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = {
25 .class_name = "interface",
26 .subclass_name = "runtime",
29 #define log_debug(fmt, ...) vlib_log_debug (if_rxq_log.class, fmt, __VA_ARGS__)
30 #define log_err(fmt, ...) vlib_log_err (if_rxq_log.class, fmt, __VA_ARGS__)
32 static char *node_state_str[] = {
33 [VLIB_NODE_STATE_DISABLED] = "disabled",
34 [VLIB_NODE_STATE_POLLING] = "polling",
35 [VLIB_NODE_STATE_INTERRUPT] = "interrupt",
39 poll_data_sort (void *a1, void *a2)
41 vnet_hw_if_rxq_poll_vector_t *pv1 = a1;
42 vnet_hw_if_rxq_poll_vector_t *pv2 = a2;
44 if (pv1->dev_instance > pv2->dev_instance)
46 else if (pv1->dev_instance < pv2->dev_instance)
48 else if (pv1->queue_id > pv2->queue_id)
50 else if (pv1->queue_id < pv2->queue_id)
57 vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index)
59 vlib_main_t *vm = vlib_get_main ();
60 vnet_interface_main_t *im = &vnm->interface_main;
61 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
62 u32 node_index = hi->input_node_index;
63 vnet_hw_if_rx_queue_t *rxq;
64 vnet_hw_if_rxq_poll_vector_t *pv, **d = 0;
65 vlib_node_state_t *per_thread_node_state = 0;
66 u32 n_threads = vlib_get_n_threads ();
67 u16 *per_thread_node_adaptive = 0;
68 int something_changed = 0;
69 clib_bitmap_t *pending_int = 0;
72 log_debug ("update node '%U' triggered by interface %v",
73 format_vlib_node_name, vm, node_index, hi->name);
75 vec_validate (d, n_threads - 1);
76 vec_validate_init_empty (per_thread_node_state, n_threads - 1,
77 VLIB_NODE_STATE_DISABLED);
78 vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0);
80 /* find out desired node state on each thread */
81 pool_foreach (rxq, im->hw_if_rx_queues)
83 u32 ti = rxq->thread_index;
85 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
86 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
88 hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
90 if (hi->input_node_index != node_index)
93 if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
95 per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
96 per_thread_node_adaptive[ti] = 0;
99 if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
102 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
103 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
104 per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
106 if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
107 per_thread_node_adaptive[ti] = 1;
110 /* construct per-thread polling vectors */
111 pool_foreach (rxq, im->hw_if_rx_queues)
113 u32 ti = rxq->thread_index;
115 hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
117 if (hi->input_node_index != node_index)
120 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
121 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
122 last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
124 if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
127 vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
128 pv->dev_instance = rxq->dev_instance;
129 pv->queue_id = rxq->queue_id;
132 /* sort poll vectors and compare them with active ones to avoid
133 * unnecesary barrier */
134 for (int i = 0; i < n_threads; i++)
136 vlib_main_t *ovm = vlib_get_main_by_index (i);
137 vlib_node_state_t old_state;
138 vec_sort_with_function (d[i], poll_data_sort);
140 old_state = vlib_node_get_state (ovm, node_index);
141 if (per_thread_node_state[i] != old_state)
143 something_changed = 1;
144 log_debug ("state changed for node %U on thread %u from %s to %s",
145 format_vlib_node_name, vm, node_index, i,
146 node_state_str[old_state],
147 node_state_str[per_thread_node_state[i]]);
150 /* check if something changed */
151 if (something_changed == 0)
153 vnet_hw_if_rx_node_runtime_t *rt;
154 rt = vlib_node_get_runtime_data (ovm, node_index);
155 if (vec_len (rt->rxq_poll_vector) != vec_len (d[i]))
156 something_changed = 1;
157 else if (memcmp (d[i], rt->rxq_poll_vector,
158 vec_len (d[i]) * sizeof (*d)))
159 something_changed = 1;
160 if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
161 something_changed = 1;
165 if (something_changed)
169 if (vlib_worker_thread_barrier_held ())
172 log_debug ("%s", "already running under the barrier");
178 vlib_worker_thread_barrier_sync (vm);
180 for (int i = 0; i < n_threads; i++)
182 vlib_main_t *vm = vlib_get_main_by_index (i);
183 vnet_hw_if_rx_node_runtime_t *rt;
184 rt = vlib_node_get_runtime_data (vm, node_index);
185 pv = rt->rxq_poll_vector;
186 rt->rxq_poll_vector = d[i];
189 if (rt->rxq_interrupts)
191 void *in = rt->rxq_interrupts;
193 while ((int_num = clib_interrupt_get_next (in, int_num)) != -1)
195 clib_interrupt_clear (in, int_num);
196 pending_int = clib_bitmap_set (pending_int, int_num, 1);
197 last_int = clib_max (last_int, int_num);
201 vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
202 vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
203 per_thread_node_adaptive[i]);
206 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
208 clib_interrupt_free (&rt->rxq_interrupts);
212 vlib_worker_thread_barrier_release (vm);
215 log_debug ("skipping update of node '%U', no changes detected",
216 format_vlib_node_name, vm, node_index);
221 clib_bitmap_foreach (i, pending_int)
223 vnet_hw_if_rx_queue_set_int_pending (vnm, i);
225 clib_bitmap_free (pending_int);
228 for (int i = 0; i < n_threads; i++)
232 vec_free (per_thread_node_state);
233 vec_free (per_thread_node_adaptive);