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 (ethernet_input, static) = {
81 .arc_name = "device-input",
82 .node_name = "ethernet-input",
83 .runs_before = 0, /* not before any other features */
88 vnet_device_queue_sort (void *a1, void *a2)
90 vnet_device_and_queue_t *dq1 = a1;
91 vnet_device_and_queue_t *dq2 = a2;
93 if (dq1->dev_instance > dq2->dev_instance)
95 else if (dq1->dev_instance < dq2->dev_instance)
97 else if (dq1->queue_id > dq2->queue_id)
99 else if (dq1->queue_id < dq2->queue_id)
106 vnet_device_queue_update (vnet_main_t * vnm, vnet_device_input_runtime_t * rt)
108 vnet_device_and_queue_t *dq;
109 vnet_hw_interface_t *hw;
111 vec_sort_with_function (rt->devices_and_queues, vnet_device_queue_sort);
113 vec_foreach (dq, rt->devices_and_queues)
115 hw = vnet_get_hw_interface (vnm, dq->hw_if_index);
116 vec_validate (hw->dq_runtime_index_by_queue, dq->queue_id);
117 hw->dq_runtime_index_by_queue[dq->queue_id] = dq - rt->devices_and_queues;
122 vnet_device_input_assign_thread (vnet_main_t * vnm, u32 hw_if_index,
123 u16 queue_id, uword thread_index)
125 vnet_device_main_t *vdm = &vnet_device_main;
127 vnet_device_input_runtime_t *rt;
128 vnet_device_and_queue_t *dq;
129 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
131 ASSERT (hw->input_node_index > 0);
133 if (vdm->first_worker_thread_index == 0)
136 if (thread_index != 0 &&
137 (thread_index < vdm->first_worker_thread_index ||
138 thread_index > vdm->last_worker_thread_index))
140 thread_index = vdm->next_worker_thread_index++;
141 if (vdm->next_worker_thread_index > vdm->last_worker_thread_index)
142 vdm->next_worker_thread_index = vdm->first_worker_thread_index;
145 vm = vlib_mains[thread_index];
146 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
148 vec_add2 (rt->devices_and_queues, dq, 1);
149 dq->hw_if_index = hw_if_index;
150 dq->dev_instance = hw->dev_instance;
151 dq->queue_id = queue_id;
153 vnet_device_queue_update (vnm, rt);
154 vec_validate (hw->input_node_thread_index_by_queue, queue_id);
155 hw->input_node_thread_index_by_queue[queue_id] = thread_index;
156 vlib_node_set_state (vm, hw->input_node_index, rt->enabled_node_state);
160 vnet_device_input_unassign_thread (vnet_main_t * vnm, u32 hw_if_index,
161 u16 queue_id, uword thread_index)
164 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
165 vnet_device_input_runtime_t *rt;
166 vnet_device_and_queue_t *dq;
167 uword old_thread_index;
169 if (hw->input_node_thread_index_by_queue == 0)
170 return VNET_API_ERROR_INVALID_INTERFACE;
172 if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
173 return VNET_API_ERROR_INVALID_INTERFACE;
175 old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
177 if (old_thread_index == thread_index)
180 vm = vlib_mains[old_thread_index];
182 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
184 vec_foreach (dq, rt->devices_and_queues)
185 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
187 vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
191 return VNET_API_ERROR_INVALID_INTERFACE;
195 vnet_device_queue_update (vnm, rt);
197 if (vec_len (rt->devices_and_queues) == 0)
198 vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
205 vnet_device_input_set_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
206 vnet_device_input_mode_t mode)
210 vnet_device_and_queue_t *dq;
211 vlib_node_state_t enabled_node_state;
212 ASSERT (mode < VNET_DEVICE_INPUT_N_MODES);
213 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
214 vnet_device_input_runtime_t *rt;
217 if (hw->input_node_thread_index_by_queue == 0)
218 return VNET_API_ERROR_INVALID_INTERFACE;
220 thread_index = hw->input_node_thread_index_by_queue[queue_id];
221 vm = vlib_mains[thread_index];
223 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
225 vec_foreach (dq, rt->devices_and_queues)
227 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
229 if (dq->mode == VNET_DEVICE_INPUT_MODE_POLLING)
234 enabled_node_state = VLIB_NODE_STATE_POLLING;
236 enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
238 if (rt->enabled_node_state != enabled_node_state)
240 rt->enabled_node_state = enabled_node_state;
241 if (vlib_node_get_state (vm, hw->input_node_index) !=
242 VLIB_NODE_STATE_DISABLED)
243 vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
250 vnet_device_input_get_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
251 vnet_device_input_mode_t * mode)
255 vnet_device_and_queue_t *dq;
256 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
257 vnet_device_input_runtime_t *rt;
259 if (hw->input_node_thread_index_by_queue == 0)
260 return VNET_API_ERROR_INVALID_INTERFACE;
262 thread_index = hw->input_node_thread_index_by_queue[queue_id];
263 vm = vlib_mains[thread_index];
265 rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
267 vec_foreach (dq, rt->devices_and_queues)
268 if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
274 return VNET_API_ERROR_INVALID_INTERFACE;
277 static clib_error_t *
278 show_device_placement_fn (vlib_main_t * vm, unformat_input_t * input,
279 vlib_cli_command_t * cmd)
282 vnet_main_t *vnm = vnet_get_main ();
283 vnet_device_input_runtime_t *rt;
284 vnet_device_and_queue_t *dq;
285 vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
290 foreach_vlib_main (({
291 clib_bitmap_foreach (si, pn->sibling_bitmap,
293 rt = vlib_node_get_runtime_data (this_vlib_main, si);
295 if (vec_len (rt->devices_and_queues))
296 s = format (s, " node %U:\n", format_vlib_node_name, vm, si);
298 vec_foreach (dq, rt->devices_and_queues)
300 s = format (s, " %U queue %u (%s)\n",
301 format_vnet_sw_if_index_name, vnm, dq->hw_if_index,
303 dq->mode == VNET_DEVICE_INPUT_MODE_POLLING ?
304 "polling" : "interrupt");
309 vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
310 vlib_worker_threads[index].name, s);
311 vec_reset_length (s);
322 VLIB_CLI_COMMAND (memif_delete_command, static) = {
323 .path = "show interface placement",
324 .short_help = "show interface placement",
325 .function = show_device_placement_fn,
329 static clib_error_t *
330 set_device_placement (vlib_main_t * vm, unformat_input_t * input,
331 vlib_cli_command_t * cmd)
333 clib_error_t *error = 0;
334 unformat_input_t _line_input, *line_input = &_line_input;
335 vnet_main_t *vnm = vnet_get_main ();
336 vnet_device_main_t *vdm = &vnet_device_main;
337 vnet_device_input_mode_t mode;
338 u32 hw_if_index = (u32) ~ 0;
339 u32 queue_id = (u32) 0;
340 u32 thread_index = (u32) ~ 0;
343 if (!unformat_user (input, unformat_line_input, line_input))
346 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
349 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
351 else if (unformat (line_input, "queue %d", &queue_id))
353 else if (unformat (line_input, "main", &thread_index))
355 else if (unformat (line_input, "worker %d", &thread_index))
356 thread_index += vdm->first_worker_thread_index;
359 error = clib_error_return (0, "parse error: '%U'",
360 format_unformat_error, line_input);
361 unformat_free (line_input);
366 unformat_free (line_input);
368 if (hw_if_index == (u32) ~ 0)
369 return clib_error_return (0, "please specify valid interface name");
371 if (thread_index > vdm->last_worker_thread_index)
372 return clib_error_return (0,
373 "please specify valid worker thread or main");
375 rv = vnet_device_input_get_mode (vnm, hw_if_index, queue_id, &mode);
378 return clib_error_return (0, "not found");
380 rv = vnet_device_input_unassign_thread (vnm, hw_if_index, queue_id,
384 return clib_error_return (0, "not found");
386 vnet_device_input_assign_thread (vnm, hw_if_index, queue_id, thread_index);
387 vnet_device_input_set_mode (vnm, hw_if_index, queue_id, mode);
393 * This command is used to assign a given interface, and optionally a
394 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
398 * Example of how to display the interface placement:
399 * @cliexstart{show interface placement}
400 * Thread 1 (vpp_wk_0):
401 * GigabitEthernet0/8/0 queue 0
402 * GigabitEthernet0/9/0 queue 0
403 * Thread 2 (vpp_wk_1):
404 * GigabitEthernet0/8/0 queue 1
405 * GigabitEthernet0/9/0 queue 1
407 * Example of how to assign a interface and queue to a thread:
408 * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
411 VLIB_CLI_COMMAND (cmd_set_dpdk_if_placement,static) = {
412 .path = "set interface placement",
413 .short_help = "set interface placement <interface> [queue <n>] [thread <n> | main]",
414 .function = set_device_placement,
418 static clib_error_t *
419 vnet_device_init (vlib_main_t * vm)
421 vnet_device_main_t *vdm = &vnet_device_main;
422 vlib_thread_main_t *tm = vlib_get_thread_main ();
423 vlib_thread_registration_t *tr;
426 vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
427 CLIB_CACHE_LINE_BYTES);
429 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
430 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
431 if (tr && tr->count > 0)
433 vdm->first_worker_thread_index = tr->first_index;
434 vdm->next_worker_thread_index = tr->first_index;
435 vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
440 VLIB_INIT_FUNCTION (vnet_device_init);
443 * fd.io coding-style-patch-verification: ON
446 * eval: (c-set-style "gnu")