462f7bbfba7cbe6ecb55fdeeb81bead9beeebbed
[vpp.git] / src / vnet / interface / runtime.c
1 /*
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>
22 #include <vnet/interface/tx_queue_funcs.h>
23 #include <vlib/unix/unix.h>
24
25 VLIB_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
33 static char *node_state_str[] = {
34   [VLIB_NODE_STATE_DISABLED] = "disabled",
35   [VLIB_NODE_STATE_POLLING] = "polling",
36   [VLIB_NODE_STATE_INTERRUPT] = "interrupt",
37 };
38
39 static int
40 poll_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
57 void
58 vnet_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;
65   vnet_hw_if_rxq_poll_vector_t *pv, **d = 0;
66   vnet_hw_if_output_node_runtime_t *new_out_runtimes = 0;
67   vlib_node_state_t *per_thread_node_state = 0;
68   u32 n_threads = vlib_get_n_threads ();
69   u16 *per_thread_node_adaptive = 0;
70   int something_changed_on_rx = 0;
71   int something_changed_on_tx = 0;
72   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);
79   vec_validate_init_empty (per_thread_node_state, n_threads - 1,
80                            VLIB_NODE_STATE_DISABLED);
81   vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0);
82
83   /* find out desired node state on each thread */
84   pool_foreach (rxq, im->hw_if_rx_queues)
85     {
86       u32 ti = rxq->thread_index;
87       vnet_hw_interface_t *rxq_hi;
88
89       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
90       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
91
92       rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
93
94       if (rxq_hi->input_node_index != node_index)
95         continue;
96
97       if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
98         {
99           per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
100           per_thread_node_adaptive[ti] = 0;
101         }
102
103       if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
104         continue;
105
106       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
107           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
108         per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
109
110       if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
111         per_thread_node_adaptive[ti] = 1;
112     }
113
114   /* construct per-thread polling vectors */
115   pool_foreach (rxq, im->hw_if_rx_queues)
116     {
117       u32 ti = rxq->thread_index;
118       vnet_hw_interface_t *rxq_hi;
119
120       rxq_hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
121
122       if (rxq_hi->input_node_index != node_index)
123         continue;
124
125       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
126           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
127         last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
128
129       if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
130         continue;
131
132       vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
133       pv->dev_instance = rxq->dev_instance;
134       pv->queue_id = rxq->queue_id;
135     }
136
137   /* sort poll vectors and compare them with active ones to avoid
138    * unnecesary barrier */
139   for (int i = 0; i < n_threads; i++)
140     {
141       vlib_main_t *ovm = vlib_get_main_by_index (i);
142       vlib_node_state_t old_state;
143       vec_sort_with_function (d[i], poll_data_sort);
144
145       old_state = vlib_node_get_state (ovm, node_index);
146       if (per_thread_node_state[i] != old_state)
147         {
148           something_changed_on_rx = 1;
149           log_debug ("state changed for node %U on thread %u from %s to %s",
150                      format_vlib_node_name, vm, node_index, i,
151                      node_state_str[old_state],
152                      node_state_str[per_thread_node_state[i]]);
153         }
154
155       /* check if something changed */
156       if (something_changed_on_rx == 0)
157         {
158           vnet_hw_if_rx_node_runtime_t *rt;
159           rt = vlib_node_get_runtime_data (ovm, node_index);
160           if (vec_len (rt->rxq_poll_vector) != vec_len (d[i]))
161             something_changed_on_rx = 1;
162           else if (memcmp (d[i], rt->rxq_poll_vector,
163                            vec_len (d[i]) * sizeof (**d)))
164             something_changed_on_rx = 1;
165           if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
166             something_changed_on_rx = 1;
167         }
168     }
169
170   new_out_runtimes =
171     vec_dup_aligned (hi->output_node_thread_runtimes, CLIB_CACHE_LINE_BYTES);
172   vec_validate_aligned (new_out_runtimes, n_threads - 1,
173                         CLIB_CACHE_LINE_BYTES);
174
175   if (vec_len (hi->output_node_thread_runtimes) != vec_len (new_out_runtimes))
176     something_changed_on_tx = 1;
177
178   for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
179     {
180       u32 thread_index;
181       u32 queue_index = hi->tx_queue_indices[i];
182       vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
183       uword n_threads = clib_bitmap_count_set_bits (txq->threads);
184
185       clib_bitmap_foreach (thread_index, txq->threads)
186         {
187           vnet_hw_if_output_node_runtime_t *rt;
188           rt = vec_elt_at_index (new_out_runtimes, thread_index);
189           if ((rt->frame.queue_id != txq->queue_id) ||
190               (rt->n_threads != n_threads))
191             {
192               log_debug ("tx queue data changed for interface %v, thread %u "
193                          "(queue_id %u -> %u, n_threads %u -> %u)",
194                          hi->name, thread_index, rt->frame.queue_id,
195                          txq->queue_id, rt->n_threads, n_threads);
196               something_changed_on_tx = 1;
197               rt->frame.queue_id = txq->queue_id;
198               rt->frame.shared_queue = txq->shared_queue;
199               rt->n_threads = n_threads;
200             }
201         }
202     }
203
204   if (something_changed_on_rx || something_changed_on_tx)
205     {
206       int with_barrier;
207
208       if (vlib_worker_thread_barrier_held ())
209         {
210           with_barrier = 0;
211           log_debug ("%s", "already running under the barrier");
212         }
213       else
214         with_barrier = 1;
215
216       if (with_barrier)
217         vlib_worker_thread_barrier_sync (vm);
218
219       if (something_changed_on_rx)
220         {
221           for (int i = 0; i < n_threads; i++)
222             {
223               vlib_main_t *vm = vlib_get_main_by_index (i);
224               vnet_hw_if_rx_node_runtime_t *rt;
225               rt = vlib_node_get_runtime_data (vm, node_index);
226               pv = rt->rxq_poll_vector;
227               rt->rxq_poll_vector = d[i];
228               d[i] = pv;
229
230               if (rt->rxq_interrupts)
231                 {
232                   void *in = rt->rxq_interrupts;
233                   int int_num = -1;
234                   while ((int_num = clib_interrupt_get_next (in, int_num)) !=
235                          -1)
236                     {
237                       clib_interrupt_clear (in, int_num);
238                       pending_int = clib_bitmap_set (pending_int, int_num, 1);
239                       last_int = clib_max (last_int, int_num);
240                     }
241                 }
242
243               vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
244               vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
245                                   per_thread_node_adaptive[i]);
246
247               if (last_int >= 0)
248                 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
249               else
250                 clib_interrupt_free (&rt->rxq_interrupts);
251             }
252         }
253       if (something_changed_on_tx)
254         {
255           vnet_hw_if_output_node_runtime_t *t;
256           t = hi->output_node_thread_runtimes;
257           hi->output_node_thread_runtimes = new_out_runtimes;
258           new_out_runtimes = t;
259         }
260
261       if (with_barrier)
262         vlib_worker_thread_barrier_release (vm);
263     }
264   else
265     log_debug ("skipping update of node '%U', no changes detected",
266                format_vlib_node_name, vm, node_index);
267
268   if (pending_int)
269     {
270       int i;
271       clib_bitmap_foreach (i, pending_int)
272         {
273           vnet_hw_if_rx_queue_set_int_pending (vnm, i);
274         }
275       clib_bitmap_free (pending_int);
276     }
277
278   for (int i = 0; i < n_threads; i++)
279     vec_free (d[i]);
280
281   vec_free (d);
282   vec_free (per_thread_node_state);
283   vec_free (per_thread_node_adaptive);
284   vec_free (new_out_runtimes);
285 }