blob: 6428d5ab75bb2f9f9e2c9425ecdef1629ea3a54e [file] [log] [blame]
Damjan Marion94100532020-11-06 23:25:57 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
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>
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020022#include <vnet/interface/tx_queue_funcs.h>
Damjan Marion94100532020-11-06 23:25:57 +010023#include <vlib/unix/unix.h>
24
25VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = {
26 .class_name = "interface",
27 .subclass_name = "runtime",
28};
29
30#define log_debug(fmt, ...) vlib_log_debug (if_rxq_log.class, fmt, __VA_ARGS__)
31#define log_err(fmt, ...) vlib_log_err (if_rxq_log.class, fmt, __VA_ARGS__)
32
33static char *node_state_str[] = {
34 [VLIB_NODE_STATE_DISABLED] = "disabled",
35 [VLIB_NODE_STATE_POLLING] = "polling",
36 [VLIB_NODE_STATE_INTERRUPT] = "interrupt",
37};
38
39static int
40poll_data_sort (void *a1, void *a2)
41{
42 vnet_hw_if_rxq_poll_vector_t *pv1 = a1;
43 vnet_hw_if_rxq_poll_vector_t *pv2 = a2;
44
45 if (pv1->dev_instance > pv2->dev_instance)
46 return 1;
47 else if (pv1->dev_instance < pv2->dev_instance)
48 return -1;
49 else if (pv1->queue_id > pv2->queue_id)
50 return 1;
51 else if (pv1->queue_id < pv2->queue_id)
52 return -1;
53 else
54 return 0;
55}
56
57void
58vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index)
59{
60 vlib_main_t *vm = vlib_get_main ();
61 vnet_interface_main_t *im = &vnm->interface_main;
62 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
63 u32 node_index = hi->input_node_index;
64 vnet_hw_if_rx_queue_t *rxq;
Maxime Peim5b223392021-05-06 12:17:25 +020065 vnet_hw_if_rxq_poll_vector_t *pv, **d = 0, **a = 0;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020066 vnet_hw_if_output_node_runtime_t *new_out_runtimes = 0;
Damjan Marion94100532020-11-06 23:25:57 +010067 vlib_node_state_t *per_thread_node_state = 0;
Damjan Marion6ffb7c62021-03-26 13:06:13 +010068 u32 n_threads = vlib_get_n_threads ();
Florin Coras982e44f2021-03-19 13:12:41 -070069 u16 *per_thread_node_adaptive = 0;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020070 int something_changed_on_rx = 0;
71 int something_changed_on_tx = 0;
Damjan Marion94100532020-11-06 23:25:57 +010072 clib_bitmap_t *pending_int = 0;
73 int last_int = -1;
74
Damjan Marion82aa9a62024-08-28 13:02:09 +020075 if (node_index == 0)
76 return;
77
Damjan Marion94100532020-11-06 23:25:57 +010078 log_debug ("update node '%U' triggered by interface %v",
79 format_vlib_node_name, vm, node_index, hi->name);
80
81 vec_validate (d, n_threads - 1);
Maxime Peim5b223392021-05-06 12:17:25 +020082 vec_validate (a, n_threads - 1);
Damjan Marion94100532020-11-06 23:25:57 +010083 vec_validate_init_empty (per_thread_node_state, n_threads - 1,
84 VLIB_NODE_STATE_DISABLED);
Florin Coras982e44f2021-03-19 13:12:41 -070085 vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0);
Damjan Marion94100532020-11-06 23:25:57 +010086
87 /* find out desired node state on each thread */
88 pool_foreach (rxq, im->hw_if_rx_queues)
89 {
90 u32 ti = rxq->thread_index;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020091 vnet_hw_interface_t *rxq_hi;
Damjan Marion94100532020-11-06 23:25:57 +010092
93 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
94 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
95
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020096 rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
Damjan Marion94100532020-11-06 23:25:57 +010097
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020098 if (rxq_hi->input_node_index != node_index)
Damjan Marion94100532020-11-06 23:25:57 +010099 continue;
100
101 if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
Florin Coras982e44f2021-03-19 13:12:41 -0700102 {
103 per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
104 per_thread_node_adaptive[ti] = 0;
105 }
Damjan Marion94100532020-11-06 23:25:57 +0100106
107 if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
108 continue;
109
110 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
111 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
112 per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
Florin Coras982e44f2021-03-19 13:12:41 -0700113
114 if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
115 per_thread_node_adaptive[ti] = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100116 }
117
118 /* construct per-thread polling vectors */
119 pool_foreach (rxq, im->hw_if_rx_queues)
120 {
121 u32 ti = rxq->thread_index;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200122 vnet_hw_interface_t *rxq_hi;
Damjan Marion94100532020-11-06 23:25:57 +0100123
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200124 rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
Damjan Marion94100532020-11-06 23:25:57 +0100125
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200126 if (rxq_hi->input_node_index != node_index)
Damjan Marion94100532020-11-06 23:25:57 +0100127 continue;
128
Damjan Marion94100532020-11-06 23:25:57 +0100129 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
130 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
131 last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
132
Maxime Peim5b223392021-05-06 12:17:25 +0200133 if (per_thread_node_adaptive[ti])
134 {
135 vec_add2_aligned (a[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
136 pv->dev_instance = rxq->dev_instance;
137 pv->queue_id = rxq->queue_id;
138 }
139
Damjan Marion94100532020-11-06 23:25:57 +0100140 if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
141 continue;
142
143 vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
144 pv->dev_instance = rxq->dev_instance;
145 pv->queue_id = rxq->queue_id;
146 }
147
148 /* sort poll vectors and compare them with active ones to avoid
149 * unnecesary barrier */
150 for (int i = 0; i < n_threads; i++)
151 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100152 vlib_main_t *ovm = vlib_get_main_by_index (i);
Damjan Marion94100532020-11-06 23:25:57 +0100153 vlib_node_state_t old_state;
154 vec_sort_with_function (d[i], poll_data_sort);
155
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100156 old_state = vlib_node_get_state (ovm, node_index);
Damjan Marion94100532020-11-06 23:25:57 +0100157 if (per_thread_node_state[i] != old_state)
158 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200159 something_changed_on_rx = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100160 log_debug ("state changed for node %U on thread %u from %s to %s",
161 format_vlib_node_name, vm, node_index, i,
162 node_state_str[old_state],
163 node_state_str[per_thread_node_state[i]]);
164 }
165
166 /* check if something changed */
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200167 if (something_changed_on_rx == 0)
Damjan Marion94100532020-11-06 23:25:57 +0100168 {
169 vnet_hw_if_rx_node_runtime_t *rt;
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100170 rt = vlib_node_get_runtime_data (ovm, node_index);
Maxime Peim5b223392021-05-06 12:17:25 +0200171 if (vec_len (rt->rxq_vector_int) != vec_len (d[i]))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200172 something_changed_on_rx = 1;
Maxime Peim5b223392021-05-06 12:17:25 +0200173 else if (memcmp (d[i], rt->rxq_vector_int,
Mohammed Hawaric3d00122021-05-11 17:28:44 +0200174 vec_len (d[i]) * sizeof (**d)))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200175 something_changed_on_rx = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100176 if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200177 something_changed_on_rx = 1;
Maxime Peim5b223392021-05-06 12:17:25 +0200178
179 if (something_changed_on_rx == 0 && per_thread_node_adaptive[i])
180 {
181 if (vec_len (rt->rxq_vector_poll) != vec_len (a[i]))
182 something_changed_on_rx = 1;
183 else if (memcmp (a[i], rt->rxq_vector_poll,
Mohammed Hawari2f8174f2021-09-08 11:30:21 +0200184 vec_len (a[i]) * sizeof (**a)))
Maxime Peim5b223392021-05-06 12:17:25 +0200185 something_changed_on_rx = 1;
186 }
Damjan Marion94100532020-11-06 23:25:57 +0100187 }
188 }
189
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000190 if (vec_len (hi->tx_queue_indices) > 0)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200191 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000192 new_out_runtimes = vec_dup_aligned (hi->output_node_thread_runtimes,
193 CLIB_CACHE_LINE_BYTES);
194 vec_validate_aligned (new_out_runtimes, n_threads - 1,
195 CLIB_CACHE_LINE_BYTES);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200196
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000197 for (u32 i = 0; i < vec_len (new_out_runtimes); i++)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200198 {
199 vnet_hw_if_output_node_runtime_t *rt;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000200 rt = vec_elt_at_index (new_out_runtimes, i);
201 u32 n_queues = 0, total_queues = vec_len (hi->tx_queue_indices);
202 rt->frame = 0;
203 rt->lookup_table = 0;
204
205 for (u32 j = 0; j < total_queues; j++)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200206 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000207 u32 queue_index = hi->tx_queue_indices[j];
208 vnet_hw_if_tx_frame_t frame = { .shared_queue = 0,
209 .hints = 7,
210 .queue_id = ~0 };
211 vnet_hw_if_tx_queue_t *txq =
212 vnet_hw_if_get_tx_queue (vnm, queue_index);
213 if (!clib_bitmap_get (txq->threads, i))
214 continue;
215
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200216 log_debug ("tx queue data changed for interface %v, thread %u "
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000217 "(queue_id %u)",
218 hi->name, i, txq->queue_id);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200219 something_changed_on_tx = 1;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000220
221 frame.queue_id = txq->queue_id;
222 frame.shared_queue = txq->shared_queue;
223 vec_add1 (rt->frame, frame);
224 n_queues++;
225 }
226
227 // don't initialize rt->n_queues above
228 if (rt->n_queues != n_queues)
229 {
230 something_changed_on_tx = 1;
231 rt->n_queues = n_queues;
232 }
233 /*
234 * It is only used in case of multiple txq.
235 */
236 if (rt->n_queues > 0)
237 {
238 if (!is_pow2 (n_queues))
239 n_queues = max_pow2 (n_queues);
240
241 vec_validate_aligned (rt->lookup_table, n_queues - 1,
242 CLIB_CACHE_LINE_BYTES);
243
244 for (u32 k = 0; k < vec_len (rt->lookup_table); k++)
245 {
246 rt->lookup_table[k] = rt->frame[k % rt->n_queues].queue_id;
247 log_debug ("tx queue lookup table changed for interface %v, "
248 "(lookup table [%u]=%u)",
249 hi->name, k, rt->lookup_table[k]);
250 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200251 }
252 }
253 }
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000254 else
255 /* interface deleted */
256 something_changed_on_tx = 1;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200257
258 if (something_changed_on_rx || something_changed_on_tx)
Damjan Marion94100532020-11-06 23:25:57 +0100259 {
260 int with_barrier;
261
262 if (vlib_worker_thread_barrier_held ())
263 {
264 with_barrier = 0;
265 log_debug ("%s", "already running under the barrier");
266 }
267 else
268 with_barrier = 1;
269
270 if (with_barrier)
271 vlib_worker_thread_barrier_sync (vm);
272
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200273 if (something_changed_on_rx)
Damjan Marion94100532020-11-06 23:25:57 +0100274 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200275 for (int i = 0; i < n_threads; i++)
Damjan Marion94100532020-11-06 23:25:57 +0100276 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200277 vlib_main_t *vm = vlib_get_main_by_index (i);
278 vnet_hw_if_rx_node_runtime_t *rt;
279 rt = vlib_node_get_runtime_data (vm, node_index);
Maxime Peim5b223392021-05-06 12:17:25 +0200280 pv = rt->rxq_vector_int;
281 rt->rxq_vector_int = d[i];
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200282 d[i] = pv;
283
Maxime Peim5b223392021-05-06 12:17:25 +0200284 if (per_thread_node_adaptive[i])
285 {
286 pv = rt->rxq_vector_poll;
287 rt->rxq_vector_poll = a[i];
288 a[i] = pv;
289 }
290
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200291 if (rt->rxq_interrupts)
Damjan Marion94100532020-11-06 23:25:57 +0100292 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200293 void *in = rt->rxq_interrupts;
294 int int_num = -1;
Damjan Marion7f75e802023-11-03 21:57:42 +0000295 while ((int_num = clib_interrupt_get_next_and_clear (
296 in, int_num)) != -1)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200297 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200298 pending_int = clib_bitmap_set (pending_int, int_num, 1);
299 last_int = clib_max (last_int, int_num);
300 }
Damjan Marion94100532020-11-06 23:25:57 +0100301 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200302
303 vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
304 vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
305 per_thread_node_adaptive[i]);
306
307 if (last_int >= 0)
308 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
309 else
310 clib_interrupt_free (&rt->rxq_interrupts);
Damjan Marion94100532020-11-06 23:25:57 +0100311 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200312 }
313 if (something_changed_on_tx)
314 {
315 vnet_hw_if_output_node_runtime_t *t;
316 t = hi->output_node_thread_runtimes;
317 hi->output_node_thread_runtimes = new_out_runtimes;
318 new_out_runtimes = t;
Damjan Marion94100532020-11-06 23:25:57 +0100319 }
320
321 if (with_barrier)
322 vlib_worker_thread_barrier_release (vm);
323 }
324 else
325 log_debug ("skipping update of node '%U', no changes detected",
326 format_vlib_node_name, vm, node_index);
327
328 if (pending_int)
329 {
330 int i;
331 clib_bitmap_foreach (i, pending_int)
332 {
333 vnet_hw_if_rx_queue_set_int_pending (vnm, i);
334 }
335 clib_bitmap_free (pending_int);
336 }
337
338 for (int i = 0; i < n_threads; i++)
Maxime Peim5b223392021-05-06 12:17:25 +0200339 {
340 vec_free (d[i]);
341 vec_free (a[i]);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000342 if (new_out_runtimes)
343 {
344 vec_free (new_out_runtimes[i].frame);
345 vec_free (new_out_runtimes[i].lookup_table);
346 }
Maxime Peim5b223392021-05-06 12:17:25 +0200347 }
Damjan Marion94100532020-11-06 23:25:57 +0100348
349 vec_free (d);
Maxime Peim5b223392021-05-06 12:17:25 +0200350 vec_free (a);
Damjan Marion94100532020-11-06 23:25:57 +0100351 vec_free (per_thread_node_state);
Florin Coras982e44f2021-03-19 13:12:41 -0700352 vec_free (per_thread_node_adaptive);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200353 vec_free (new_out_runtimes);
Damjan Marion94100532020-11-06 23:25:57 +0100354}