2 * Copyright (c) 2015 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>
22 vnet_device_main_t vnet_device_main;
25 device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
32 VLIB_REGISTER_NODE (device_input_node) = {
33 .function = device_input_fn,
34 .name = "device-input",
35 .runtime_data_bytes = sizeof (vnet_device_input_runtime_t),
36 .type = VLIB_NODE_TYPE_INPUT,
37 .state = VLIB_NODE_STATE_DISABLED,
38 .n_next_nodes = VNET_DEVICE_INPUT_N_NEXT_NODES,
39 .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES,
42 /* Table defines how much we need to advance current data pointer
43 in the buffer if we shortcut to l3 nodes */
45 const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES)))
46 device_input_next_node_advance[((VNET_DEVICE_INPUT_N_NEXT_NODES /
47 CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] =
49 [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = sizeof (ethernet_header_t),
50 [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = sizeof (ethernet_header_t),
51 [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = sizeof (ethernet_header_t),
52 [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = sizeof (ethernet_header_t),
55 VNET_FEATURE_ARC_INIT (device_input, static) =
57 .arc_name = "device-input",
58 .start_nodes = VNET_FEATURES ("device-input"),
59 .arc_index_ptr = &feature_main.device_input_feature_arc_index,
62 VNET_FEATURE_INIT (l2_patch, static) = {
63 .arc_name = "device-input",
64 .node_name = "l2-patch",
65 .runs_before = VNET_FEATURES ("ethernet-input"),
68 VNET_FEATURE_INIT (worker_handoff, static) = {
69 .arc_name = "device-input",
70 .node_name = "worker-handoff",
71 .runs_before = VNET_FEATURES ("ethernet-input"),
74 VNET_FEATURE_INIT (span_input, static) = {
75 .arc_name = "device-input",
76 .node_name = "span-input",
77 .runs_before = VNET_FEATURES ("ethernet-input"),
80 VNET_FEATURE_INIT (p2p_ethernet_node, static) = {
81 .arc_name = "device-input",
82 .node_name = "p2p-ethernet-input",
83 .runs_before = VNET_FEATURES ("ethernet-input"),
86 VNET_FEATURE_INIT (ethernet_input, static) = {
87 .arc_name = "device-input",
88 .node_name = "ethernet-input",
89 .runs_before = 0, /* not before any other features */
94 vnet_device_queue_sort (void *a1, void *a2)
96 vnet_device_and_queue_t *dq1 = a1;
97 vnet_device_and_queue_t *dq2 = a2;
99 if (dq1->dev_instance > dq2->dev_instance)
101 else if (dq1->dev_instance < dq2->dev_instance)
103 else if (dq1->queue_id > dq2->queue_id)
105 else if (dq1->queue_id < dq2->queue_id)
112 vnet_device_queue_update (vnet_main_t * vnm, vnet_device_input_runtime_t * rt)
114 vnet_device_and_queue_t *dq;
115 vnet_hw_interface_t *hw;
117 vec_sort_with_function (rt->devices_and_queues, vnet_device_queue_sort);
119 vec_foreach (dq, rt->devices_and_queues)
121 hw = vnet_get_hw_interface (vnm, dq->hw_if_index);
122 vec_validate (hw->dq_runtime_index_by_queue, dq->queue_id);
123 hw->dq_runtime_index_by_queue[dq->queue_id] = dq - rt->devices_and_queues;
128 vnet_hw_interface_assign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
129 u16 queue_id, uword thread_index)
131 vnet_device_main_t *vdm = &vnet_device_main;
132 vlib_main_t *vm, *vm0;
133 vnet_device_input_runtime_t *rt;
134 vnet_device_and_queue_t *dq;
135 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
137 ASSERT (hw->input_node_index > 0);
139 if (vdm->first_worker_thread_index == 0)
142 if (thread_index != 0 &&
143 (thread_index < vdm->first_worker_thread_index ||
144 thread_index > vdm->last_worker_thread_index))
146 thread_index = vdm->next_worker_thread_index++;
147 if (vdm->next_worker_thread_index > vdm->last_worker_thread_index)
148 vdm->next_worker_thread_index = vdm->first_worker_thread_index;
151 vm = vlib_mains[thread_index];
152 vm0 = vlib_get_main ();
154 vlib_worker_thread_barrier_sync (vm0);
156 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
158 vec_add2 (rt->devices_and_queues, dq, 1);
159 dq->hw_if_index = hw_if_index;
160 dq->dev_instance = hw->dev_instance;
161 dq->queue_id = queue_id;
162 dq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
163 rt->enabled_node_state = VLIB_NODE_STATE_POLLING;
165 vnet_device_queue_update (vnm, rt);
166 vec_validate (hw->input_node_thread_index_by_queue, queue_id);
167 vec_validate (hw->rx_mode_by_queue, queue_id);
168 hw->input_node_thread_index_by_queue[queue_id] = thread_index;
169 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_POLLING;
171 vlib_worker_thread_barrier_release (vm0);
173 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
177 vnet_hw_interface_unassign_rx_thread (vnet_main_t * vnm, u32 hw_if_index,
180 vlib_main_t *vm, *vm0;
181 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
182 vnet_device_input_runtime_t *rt;
183 vnet_device_and_queue_t *dq;
184 uword old_thread_index;
185 vnet_hw_interface_rx_mode mode;
187 if (hw->input_node_thread_index_by_queue == 0)
188 return VNET_API_ERROR_INVALID_INTERFACE;
190 if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
191 return VNET_API_ERROR_INVALID_INTERFACE;
193 old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
195 vm = vlib_mains[old_thread_index];
197 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
199 vec_foreach (dq, rt->devices_and_queues)
200 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
206 return VNET_API_ERROR_INVALID_INTERFACE;
210 vm0 = vlib_get_main ();
211 vlib_worker_thread_barrier_sync (vm0);
212 vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
213 vnet_device_queue_update (vnm, rt);
214 hw->rx_mode_by_queue[queue_id] = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
215 vlib_worker_thread_barrier_release (vm0);
217 if (vec_len (rt->devices_and_queues) == 0)
218 vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
219 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
222 * if the deleted interface is polling, we may need to set the node state
223 * to interrupt if there is no more polling interface for this device's
224 * corresponding thread. This is because mixed interfaces
225 * (polling and interrupt), assigned to the same thread, set the
226 * thread to polling prior to the deletion.
228 vec_foreach (dq, rt->devices_and_queues)
230 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
233 rt->enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
234 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
242 vnet_hw_interface_set_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
243 u16 queue_id, vnet_hw_interface_rx_mode mode)
247 vnet_device_and_queue_t *dq;
248 vlib_node_state_t enabled_node_state;
249 ASSERT (mode < VNET_HW_INTERFACE_NUM_RX_MODES);
250 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
251 vnet_device_input_runtime_t *rt;
254 if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
255 mode = hw->default_rx_mode;
257 if (hw->input_node_thread_index_by_queue == 0 || hw->rx_mode_by_queue == 0)
258 return VNET_API_ERROR_INVALID_INTERFACE;
260 if (hw->rx_mode_by_queue[queue_id] == mode)
263 if (mode != VNET_HW_INTERFACE_RX_MODE_POLLING &&
264 (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE) == 0)
265 return VNET_API_ERROR_UNSUPPORTED;
267 if ((vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1) ||
268 (vec_len (hw->rx_mode_by_queue) < queue_id + 1))
269 return VNET_API_ERROR_INVALID_QUEUE;
271 hw->rx_mode_by_queue[queue_id] = mode;
272 thread_index = hw->input_node_thread_index_by_queue[queue_id];
273 vm = vlib_mains[thread_index];
275 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
277 vec_foreach (dq, rt->devices_and_queues)
279 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
281 if (dq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
286 enabled_node_state = VLIB_NODE_STATE_POLLING;
288 enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
290 if (rt->enabled_node_state != enabled_node_state)
292 rt->enabled_node_state = enabled_node_state;
293 if (vlib_node_get_state (vm, hw->input_node_index) !=
294 VLIB_NODE_STATE_DISABLED)
295 vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
302 vnet_hw_interface_get_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
303 u16 queue_id, vnet_hw_interface_rx_mode * mode)
307 vnet_device_and_queue_t *dq;
308 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
309 vnet_device_input_runtime_t *rt;
311 if (hw->input_node_thread_index_by_queue == 0)
312 return VNET_API_ERROR_INVALID_INTERFACE;
314 if ((vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1) ||
315 (vec_len (hw->rx_mode_by_queue) < queue_id + 1))
316 return VNET_API_ERROR_INVALID_QUEUE;
318 thread_index = hw->input_node_thread_index_by_queue[queue_id];
319 vm = vlib_mains[thread_index];
321 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
323 vec_foreach (dq, rt->devices_and_queues)
324 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
330 return VNET_API_ERROR_INVALID_INTERFACE;
335 static clib_error_t *
336 vnet_device_init (vlib_main_t * vm)
338 vnet_device_main_t *vdm = &vnet_device_main;
339 vlib_thread_main_t *tm = vlib_get_thread_main ();
340 vlib_thread_registration_t *tr;
343 vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
344 CLIB_CACHE_LINE_BYTES);
346 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
347 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
348 if (tr && tr->count > 0)
350 vdm->first_worker_thread_index = tr->first_index;
351 vdm->next_worker_thread_index = tr->first_index;
352 vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
357 VLIB_INIT_FUNCTION (vnet_device_init);
360 * fd.io coding-style-patch-verification: ON
363 * eval: (c-set-style "gnu")