blob: e63f1ecc2fc233ff618780925007955769dddf4d [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
75 log_debug ("update node '%U' triggered by interface %v",
76 format_vlib_node_name, vm, node_index, hi->name);
77
78 vec_validate (d, n_threads - 1);
Maxime Peim5b223392021-05-06 12:17:25 +020079 vec_validate (a, n_threads - 1);
Damjan Marion94100532020-11-06 23:25:57 +010080 vec_validate_init_empty (per_thread_node_state, n_threads - 1,
81 VLIB_NODE_STATE_DISABLED);
Florin Coras982e44f2021-03-19 13:12:41 -070082 vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0);
Damjan Marion94100532020-11-06 23:25:57 +010083
84 /* find out desired node state on each thread */
85 pool_foreach (rxq, im->hw_if_rx_queues)
86 {
87 u32 ti = rxq->thread_index;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020088 vnet_hw_interface_t *rxq_hi;
Damjan Marion94100532020-11-06 23:25:57 +010089
90 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
91 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
92
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020093 rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
Damjan Marion94100532020-11-06 23:25:57 +010094
Damjan Marion1bd6cbb2021-04-15 13:12:51 +020095 if (rxq_hi->input_node_index != node_index)
Damjan Marion94100532020-11-06 23:25:57 +010096 continue;
97
98 if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
Florin Coras982e44f2021-03-19 13:12:41 -070099 {
100 per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
101 per_thread_node_adaptive[ti] = 0;
102 }
Damjan Marion94100532020-11-06 23:25:57 +0100103
104 if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
105 continue;
106
107 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
108 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
109 per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
Florin Coras982e44f2021-03-19 13:12:41 -0700110
111 if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
112 per_thread_node_adaptive[ti] = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100113 }
114
115 /* construct per-thread polling vectors */
116 pool_foreach (rxq, im->hw_if_rx_queues)
117 {
118 u32 ti = rxq->thread_index;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200119 vnet_hw_interface_t *rxq_hi;
Damjan Marion94100532020-11-06 23:25:57 +0100120
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200121 rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
Damjan Marion94100532020-11-06 23:25:57 +0100122
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200123 if (rxq_hi->input_node_index != node_index)
Damjan Marion94100532020-11-06 23:25:57 +0100124 continue;
125
Damjan Marion94100532020-11-06 23:25:57 +0100126 if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
127 rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
128 last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
129
Maxime Peim5b223392021-05-06 12:17:25 +0200130 if (per_thread_node_adaptive[ti])
131 {
132 vec_add2_aligned (a[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
133 pv->dev_instance = rxq->dev_instance;
134 pv->queue_id = rxq->queue_id;
135 }
136
Damjan Marion94100532020-11-06 23:25:57 +0100137 if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
138 continue;
139
140 vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
141 pv->dev_instance = rxq->dev_instance;
142 pv->queue_id = rxq->queue_id;
143 }
144
145 /* sort poll vectors and compare them with active ones to avoid
146 * unnecesary barrier */
147 for (int i = 0; i < n_threads; i++)
148 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100149 vlib_main_t *ovm = vlib_get_main_by_index (i);
Damjan Marion94100532020-11-06 23:25:57 +0100150 vlib_node_state_t old_state;
151 vec_sort_with_function (d[i], poll_data_sort);
152
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100153 old_state = vlib_node_get_state (ovm, node_index);
Damjan Marion94100532020-11-06 23:25:57 +0100154 if (per_thread_node_state[i] != old_state)
155 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200156 something_changed_on_rx = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100157 log_debug ("state changed for node %U on thread %u from %s to %s",
158 format_vlib_node_name, vm, node_index, i,
159 node_state_str[old_state],
160 node_state_str[per_thread_node_state[i]]);
161 }
162
163 /* check if something changed */
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200164 if (something_changed_on_rx == 0)
Damjan Marion94100532020-11-06 23:25:57 +0100165 {
166 vnet_hw_if_rx_node_runtime_t *rt;
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100167 rt = vlib_node_get_runtime_data (ovm, node_index);
Maxime Peim5b223392021-05-06 12:17:25 +0200168 if (vec_len (rt->rxq_vector_int) != vec_len (d[i]))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200169 something_changed_on_rx = 1;
Maxime Peim5b223392021-05-06 12:17:25 +0200170 else if (memcmp (d[i], rt->rxq_vector_int,
Mohammed Hawaric3d00122021-05-11 17:28:44 +0200171 vec_len (d[i]) * sizeof (**d)))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200172 something_changed_on_rx = 1;
Damjan Marion94100532020-11-06 23:25:57 +0100173 if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200174 something_changed_on_rx = 1;
Maxime Peim5b223392021-05-06 12:17:25 +0200175
176 if (something_changed_on_rx == 0 && per_thread_node_adaptive[i])
177 {
178 if (vec_len (rt->rxq_vector_poll) != vec_len (a[i]))
179 something_changed_on_rx = 1;
180 else if (memcmp (a[i], rt->rxq_vector_poll,
Mohammed Hawari2f8174f2021-09-08 11:30:21 +0200181 vec_len (a[i]) * sizeof (**a)))
Maxime Peim5b223392021-05-06 12:17:25 +0200182 something_changed_on_rx = 1;
183 }
Damjan Marion94100532020-11-06 23:25:57 +0100184 }
185 }
186
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200187 new_out_runtimes =
188 vec_dup_aligned (hi->output_node_thread_runtimes, CLIB_CACHE_LINE_BYTES);
Damjan Marionc389de22021-05-14 15:48:52 +0200189 vec_validate_aligned (new_out_runtimes, n_threads - 1,
190 CLIB_CACHE_LINE_BYTES);
191
192 if (vec_len (hi->output_node_thread_runtimes) != vec_len (new_out_runtimes))
193 something_changed_on_tx = 1;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200194
195 for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
196 {
197 u32 thread_index;
198 u32 queue_index = hi->tx_queue_indices[i];
199 vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
Damjan Marionfbb02c42021-05-19 11:18:20 +0200200 uword n_threads = clib_bitmap_count_set_bits (txq->threads);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200201
202 clib_bitmap_foreach (thread_index, txq->threads)
203 {
204 vnet_hw_if_output_node_runtime_t *rt;
205 rt = vec_elt_at_index (new_out_runtimes, thread_index);
206 if ((rt->frame.queue_id != txq->queue_id) ||
Damjan Marionfbb02c42021-05-19 11:18:20 +0200207 (rt->n_threads != n_threads))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200208 {
209 log_debug ("tx queue data changed for interface %v, thread %u "
Damjan Marionfbb02c42021-05-19 11:18:20 +0200210 "(queue_id %u -> %u, n_threads %u -> %u)",
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200211 hi->name, thread_index, rt->frame.queue_id,
Damjan Marionfbb02c42021-05-19 11:18:20 +0200212 txq->queue_id, rt->n_threads, n_threads);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200213 something_changed_on_tx = 1;
Damjan Marione4c8d692021-05-13 14:19:33 +0200214 rt->frame.queue_id = txq->queue_id;
215 rt->frame.shared_queue = txq->shared_queue;
Damjan Marionfbb02c42021-05-19 11:18:20 +0200216 rt->n_threads = n_threads;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200217 }
218 }
219 }
220
221 if (something_changed_on_rx || something_changed_on_tx)
Damjan Marion94100532020-11-06 23:25:57 +0100222 {
223 int with_barrier;
224
225 if (vlib_worker_thread_barrier_held ())
226 {
227 with_barrier = 0;
228 log_debug ("%s", "already running under the barrier");
229 }
230 else
231 with_barrier = 1;
232
233 if (with_barrier)
234 vlib_worker_thread_barrier_sync (vm);
235
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200236 if (something_changed_on_rx)
Damjan Marion94100532020-11-06 23:25:57 +0100237 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200238 for (int i = 0; i < n_threads; i++)
Damjan Marion94100532020-11-06 23:25:57 +0100239 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200240 vlib_main_t *vm = vlib_get_main_by_index (i);
241 vnet_hw_if_rx_node_runtime_t *rt;
242 rt = vlib_node_get_runtime_data (vm, node_index);
Maxime Peim5b223392021-05-06 12:17:25 +0200243 pv = rt->rxq_vector_int;
244 rt->rxq_vector_int = d[i];
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200245 d[i] = pv;
246
Maxime Peim5b223392021-05-06 12:17:25 +0200247 if (per_thread_node_adaptive[i])
248 {
249 pv = rt->rxq_vector_poll;
250 rt->rxq_vector_poll = a[i];
251 a[i] = pv;
252 }
253
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200254 if (rt->rxq_interrupts)
Damjan Marion94100532020-11-06 23:25:57 +0100255 {
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200256 void *in = rt->rxq_interrupts;
257 int int_num = -1;
258 while ((int_num = clib_interrupt_get_next (in, int_num)) !=
259 -1)
260 {
261 clib_interrupt_clear (in, int_num);
262 pending_int = clib_bitmap_set (pending_int, int_num, 1);
263 last_int = clib_max (last_int, int_num);
264 }
Damjan Marion94100532020-11-06 23:25:57 +0100265 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200266
267 vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
268 vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
269 per_thread_node_adaptive[i]);
270
271 if (last_int >= 0)
272 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
273 else
274 clib_interrupt_free (&rt->rxq_interrupts);
Damjan Marion94100532020-11-06 23:25:57 +0100275 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200276 }
277 if (something_changed_on_tx)
278 {
279 vnet_hw_if_output_node_runtime_t *t;
280 t = hi->output_node_thread_runtimes;
281 hi->output_node_thread_runtimes = new_out_runtimes;
282 new_out_runtimes = t;
Damjan Marion94100532020-11-06 23:25:57 +0100283 }
284
285 if (with_barrier)
286 vlib_worker_thread_barrier_release (vm);
287 }
288 else
289 log_debug ("skipping update of node '%U', no changes detected",
290 format_vlib_node_name, vm, node_index);
291
292 if (pending_int)
293 {
294 int i;
295 clib_bitmap_foreach (i, pending_int)
296 {
297 vnet_hw_if_rx_queue_set_int_pending (vnm, i);
298 }
299 clib_bitmap_free (pending_int);
300 }
301
302 for (int i = 0; i < n_threads; i++)
Maxime Peim5b223392021-05-06 12:17:25 +0200303 {
304 vec_free (d[i]);
305 vec_free (a[i]);
306 }
Damjan Marion94100532020-11-06 23:25:57 +0100307
308 vec_free (d);
Maxime Peim5b223392021-05-06 12:17:25 +0200309 vec_free (a);
Damjan Marion94100532020-11-06 23:25:57 +0100310 vec_free (per_thread_node_state);
Florin Coras982e44f2021-03-19 13:12:41 -0700311 vec_free (per_thread_node_adaptive);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200312 vec_free (new_out_runtimes);
Damjan Marion94100532020-11-06 23:25:57 +0100313}