interface: let drivers control polling when down
[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 <vlib/unix/unix.h>
23
24 VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = {
25   .class_name = "interface",
26   .subclass_name = "runtime",
27 };
28
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__)
31
32 static char *node_state_str[] = {
33   [VLIB_NODE_STATE_DISABLED] = "disabled",
34   [VLIB_NODE_STATE_POLLING] = "polling",
35   [VLIB_NODE_STATE_INTERRUPT] = "interrupt",
36 };
37
38 static int
39 poll_data_sort (void *a1, void *a2)
40 {
41   vnet_hw_if_rxq_poll_vector_t *pv1 = a1;
42   vnet_hw_if_rxq_poll_vector_t *pv2 = a2;
43
44   if (pv1->dev_instance > pv2->dev_instance)
45     return 1;
46   else if (pv1->dev_instance < pv2->dev_instance)
47     return -1;
48   else if (pv1->queue_id > pv2->queue_id)
49     return 1;
50   else if (pv1->queue_id < pv2->queue_id)
51     return -1;
52   else
53     return 0;
54 }
55
56 void
57 vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index)
58 {
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 = vec_len (vlib_mains);
67   int something_changed = 0;
68   clib_bitmap_t *pending_int = 0;
69   int last_int = -1;
70
71   log_debug ("update node '%U' triggered by interface %v",
72              format_vlib_node_name, vm, node_index, hi->name);
73
74   vec_validate (d, n_threads - 1);
75   vec_validate_init_empty (per_thread_node_state, n_threads - 1,
76                            VLIB_NODE_STATE_DISABLED);
77
78   /* find out desired node state on each thread */
79   pool_foreach (rxq, im->hw_if_rx_queues)
80     {
81       u32 ti = rxq->thread_index;
82
83       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
84       ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
85
86       hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
87
88       if (hi->input_node_index != node_index)
89         continue;
90
91       if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
92         per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
93
94       if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
95         continue;
96
97       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
98           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
99         per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT;
100     }
101
102   /* construct per-thread polling vectors */
103   pool_foreach (rxq, im->hw_if_rx_queues)
104     {
105       u32 ti = rxq->thread_index;
106
107       hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
108
109       if (hi->input_node_index != node_index)
110         continue;
111
112       if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT ||
113           rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)
114         last_int = clib_max (last_int, rxq - im->hw_if_rx_queues);
115
116       if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING)
117         continue;
118
119       vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES);
120       pv->dev_instance = rxq->dev_instance;
121       pv->queue_id = rxq->queue_id;
122     }
123
124   /* sort poll vectors and compare them with active ones to avoid
125    * unnecesary barrier */
126   for (int i = 0; i < n_threads; i++)
127     {
128       vlib_node_state_t old_state;
129       vec_sort_with_function (d[i], poll_data_sort);
130
131       old_state = vlib_node_get_state (vlib_mains[i], node_index);
132       if (per_thread_node_state[i] != old_state)
133         {
134           something_changed = 1;
135           log_debug ("state changed for node %U on thread %u from %s to %s",
136                      format_vlib_node_name, vm, node_index, i,
137                      node_state_str[old_state],
138                      node_state_str[per_thread_node_state[i]]);
139         }
140
141       /* check if something changed */
142       if (something_changed == 0)
143         {
144           vnet_hw_if_rx_node_runtime_t *rt;
145           rt = vlib_node_get_runtime_data (vlib_mains[i], node_index);
146           if (vec_len (rt->rxq_poll_vector) != vec_len (d[i]))
147             something_changed = 1;
148           else if (memcmp (d[i], rt->rxq_poll_vector,
149                            vec_len (d[i]) * sizeof (*d)))
150             something_changed = 1;
151           if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
152             something_changed = 1;
153         }
154     }
155
156   if (something_changed)
157     {
158       int with_barrier;
159
160       if (vlib_worker_thread_barrier_held ())
161         {
162           with_barrier = 0;
163           log_debug ("%s", "already running under the barrier");
164         }
165       else
166         with_barrier = 1;
167
168       if (with_barrier)
169         vlib_worker_thread_barrier_sync (vm);
170
171       for (int i = 0; i < n_threads; i++)
172         {
173           vlib_main_t *vm = vlib_mains[i];
174           vnet_hw_if_rx_node_runtime_t *rt;
175           rt = vlib_node_get_runtime_data (vm, node_index);
176           pv = rt->rxq_poll_vector;
177           rt->rxq_poll_vector = d[i];
178           d[i] = pv;
179
180           if (rt->rxq_interrupts)
181             {
182               void *in = rt->rxq_interrupts;
183               int int_num = -1;
184               while ((int_num = clib_interrupt_get_next (in, int_num)) != -1)
185                 {
186                   clib_interrupt_clear (in, int_num);
187                   pending_int = clib_bitmap_set (pending_int, int_num, 1);
188                   last_int = clib_max (last_int, int_num);
189                 }
190             }
191
192           vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
193
194           if (last_int >= 0)
195             clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
196           else
197             clib_interrupt_free (&rt->rxq_interrupts);
198         }
199
200       if (with_barrier)
201         vlib_worker_thread_barrier_release (vm);
202     }
203   else
204     log_debug ("skipping update of node '%U', no changes detected",
205                format_vlib_node_name, vm, node_index);
206
207   if (pending_int)
208     {
209       int i;
210       clib_bitmap_foreach (i, pending_int)
211         {
212           vnet_hw_if_rx_queue_set_int_pending (vnm, i);
213         }
214       clib_bitmap_free (pending_int);
215     }
216
217   for (int i = 0; i < n_threads; i++)
218     vec_free (d[i]);
219
220   vec_free (d);
221   vec_free (per_thread_node_state);
222 }