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 = vec_len (vlib_mains);
67 int something_changed = 0;
68 clib_bitmap_t *pending_int = 0;
71 log_debug ("update node '%U' triggered by interface %v",
72 format_vlib_node_name, vm, node_index, hi->name);
74 vec_validate (d, n_threads - 1);
75 vec_validate_init_empty (per_thread_node_state, n_threads - 1,
76 VLIB_NODE_STATE_DISABLED);
78 /* find out desired node state on each thread */
79 pool_foreach (rxq, im->hw_if_rx_queues)
81 u32 ti = rxq->thread_index;
83 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN);
84 ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT);
86 hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
88 if (hi->input_node_index != node_index)
91 if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING)
92 per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING;
94 if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING)
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;
102 /* construct per-thread polling vectors */
103 pool_foreach (rxq, im->hw_if_rx_queues)
105 u32 ti = rxq->thread_index;
108 hi = vnet_get_hw_interface (vnm, rxq->hw_if_index);
110 if (hi->input_node_index != node_index)
113 flags = vnet_sw_interface_get_flags (vnm, hi->sw_if_index);
114 if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
116 log_debug ("skip interface %v (admin down)", hi->name);
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_node_state_t old_state;
137 vec_sort_with_function (d[i], poll_data_sort);
139 old_state = vlib_node_get_state (vlib_mains[i], node_index);
140 if (per_thread_node_state[i] != old_state)
142 something_changed = 1;
143 log_debug ("state changed for node %U on thread %u from %s to %s",
144 format_vlib_node_name, vm, node_index, i,
145 node_state_str[old_state],
146 node_state_str[per_thread_node_state[i]]);
149 /* check if something changed */
150 if (something_changed == 0)
152 vnet_hw_if_rx_node_runtime_t *rt;
153 rt = vlib_node_get_runtime_data (vlib_mains[i], node_index);
154 if (vec_len (rt->rxq_poll_vector) != vec_len (d[i]))
155 something_changed = 1;
156 else if (memcmp (d[i], rt->rxq_poll_vector,
157 vec_len (d[i]) * sizeof (*d)))
158 something_changed = 1;
159 if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1)
160 something_changed = 1;
164 if (something_changed)
168 if (vlib_worker_thread_barrier_held ())
171 log_debug ("%s", "already running under the barrier");
177 vlib_worker_thread_barrier_sync (vm);
179 for (int i = 0; i < n_threads; i++)
181 vlib_main_t *vm = vlib_mains[i];
182 vnet_hw_if_rx_node_runtime_t *rt;
183 rt = vlib_node_get_runtime_data (vm, node_index);
184 pv = rt->rxq_poll_vector;
185 rt->rxq_poll_vector = d[i];
188 if (rt->rxq_interrupts)
190 void *in = rt->rxq_interrupts;
192 while ((int_num = clib_interrupt_get_next (in, int_num)) != -1)
194 clib_interrupt_clear (in, int_num);
195 pending_int = clib_bitmap_set (pending_int, int_num, 1);
199 vlib_node_set_state (vm, node_index, per_thread_node_state[i]);
202 clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1);
204 clib_interrupt_free (&rt->rxq_interrupts);
208 vlib_worker_thread_barrier_release (vm);
211 log_debug ("skipping update of node '%U', no changes detected",
212 format_vlib_node_name, vm, node_index);
217 clib_bitmap_foreach (i, pending_int)
219 vnet_hw_if_rx_queue_set_int_pending (vnm, i);
221 clib_bitmap_free (pending_int);
224 for (int i = 0; i < n_threads; i++)
228 vec_free (per_thread_node_state);