Devices: set interface placement does not remove the old interface placement
[vpp.git] / src / vnet / devices / devices.c
1 /*
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:
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
22 vnet_device_main_t vnet_device_main;
23
24 static uword
25 device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
26                  vlib_frame_t * frame)
27 {
28   return 0;
29 }
30
31 /* *INDENT-OFF* */
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,
40 };
41
42 /* Table defines how much we need to advance current data pointer
43    in the buffer if we shortcut to l3 nodes */
44
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] =
48 {
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),
53 };
54
55 VNET_FEATURE_ARC_INIT (device_input, static) =
56 {
57   .arc_name  = "device-input",
58   .start_nodes = VNET_FEATURES ("device-input"),
59   .arc_index_ptr = &feature_main.device_input_feature_arc_index,
60 };
61
62 VNET_FEATURE_INIT (l2_patch, static) = {
63   .arc_name = "device-input",
64   .node_name = "l2-patch",
65   .runs_before = VNET_FEATURES ("ethernet-input"),
66 };
67
68 VNET_FEATURE_INIT (worker_handoff, static) = {
69   .arc_name = "device-input",
70   .node_name = "worker-handoff",
71   .runs_before = VNET_FEATURES ("ethernet-input"),
72 };
73
74 VNET_FEATURE_INIT (span_input, static) = {
75   .arc_name = "device-input",
76   .node_name = "span-input",
77   .runs_before = VNET_FEATURES ("ethernet-input"),
78 };
79
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 */
84 };
85 /* *INDENT-ON* */
86
87 static int
88 vnet_device_queue_sort (void *a1, void *a2)
89 {
90   vnet_device_and_queue_t *dq1 = a1;
91   vnet_device_and_queue_t *dq2 = a2;
92
93   if (dq1->dev_instance > dq2->dev_instance)
94     return 1;
95   else if (dq1->dev_instance < dq2->dev_instance)
96     return -1;
97   else if (dq1->queue_id > dq2->queue_id)
98     return 1;
99   else if (dq1->queue_id < dq2->queue_id)
100     return -1;
101   else
102     return 0;
103 }
104
105 static void
106 vnet_device_queue_update (vnet_main_t * vnm, vnet_device_input_runtime_t * rt)
107 {
108   vnet_device_and_queue_t *dq;
109   vnet_hw_interface_t *hw;
110
111   vec_sort_with_function (rt->devices_and_queues, vnet_device_queue_sort);
112
113   vec_foreach (dq, rt->devices_and_queues)
114   {
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;
118   }
119 }
120
121 void
122 vnet_device_input_assign_thread (vnet_main_t * vnm, u32 hw_if_index,
123                                  u16 queue_id, uword thread_index)
124 {
125   vnet_device_main_t *vdm = &vnet_device_main;
126   vlib_main_t *vm;
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);
130
131   ASSERT (hw->input_node_index > 0);
132
133   if (vdm->first_worker_thread_index == 0)
134     thread_index = 0;
135
136   if (thread_index != 0 &&
137       (thread_index < vdm->first_worker_thread_index ||
138        thread_index > vdm->last_worker_thread_index))
139     {
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;
143     }
144
145   vm = vlib_mains[thread_index];
146   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
147
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;
152
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);
157 }
158
159 int
160 vnet_device_input_unassign_thread (vnet_main_t * vnm, u32 hw_if_index,
161                                    u16 queue_id, uword thread_index)
162 {
163   vlib_main_t *vm;
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;
168
169   if (hw->input_node_thread_index_by_queue == 0)
170     return VNET_API_ERROR_INVALID_INTERFACE;
171
172   if (vec_len (hw->input_node_thread_index_by_queue) < queue_id + 1)
173     return VNET_API_ERROR_INVALID_INTERFACE;
174
175   old_thread_index = hw->input_node_thread_index_by_queue[queue_id];
176
177   vm = vlib_mains[old_thread_index];
178
179   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
180
181   vec_foreach (dq, rt->devices_and_queues)
182     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
183     {
184       vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
185       goto deleted;
186     }
187
188   return VNET_API_ERROR_INVALID_INTERFACE;
189
190 deleted:
191
192   vnet_device_queue_update (vnm, rt);
193
194   if (vec_len (rt->devices_and_queues) == 0)
195     vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
196
197   return 0;
198 }
199
200
201 int
202 vnet_device_input_set_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
203                             vnet_device_input_mode_t mode)
204 {
205   vlib_main_t *vm;
206   uword thread_index;
207   vnet_device_and_queue_t *dq;
208   vlib_node_state_t enabled_node_state;
209   ASSERT (mode < VNET_DEVICE_INPUT_N_MODES);
210   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
211   vnet_device_input_runtime_t *rt;
212   int is_polling = 0;
213
214   if (hw->input_node_thread_index_by_queue == 0)
215     return VNET_API_ERROR_INVALID_INTERFACE;
216
217   thread_index = hw->input_node_thread_index_by_queue[queue_id];
218   vm = vlib_mains[thread_index];
219
220   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
221
222   vec_foreach (dq, rt->devices_and_queues)
223   {
224     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
225       dq->mode = mode;
226     if (dq->mode == VNET_DEVICE_INPUT_MODE_POLLING)
227       is_polling = 1;
228   }
229
230   if (is_polling)
231     enabled_node_state = VLIB_NODE_STATE_POLLING;
232   else
233     enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
234
235   if (rt->enabled_node_state != enabled_node_state)
236     {
237       rt->enabled_node_state = enabled_node_state;
238       if (vlib_node_get_state (vm, hw->input_node_index) !=
239           VLIB_NODE_STATE_DISABLED)
240         vlib_node_set_state (vm, hw->input_node_index, enabled_node_state);
241     }
242
243   return 0;
244 }
245
246 int
247 vnet_device_input_get_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
248                             vnet_device_input_mode_t * mode)
249 {
250   vlib_main_t *vm;
251   uword thread_index;
252   vnet_device_and_queue_t *dq;
253   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
254   vnet_device_input_runtime_t *rt;
255
256   if (hw->input_node_thread_index_by_queue == 0)
257     return VNET_API_ERROR_INVALID_INTERFACE;
258
259   thread_index = hw->input_node_thread_index_by_queue[queue_id];
260   vm = vlib_mains[thread_index];
261
262   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
263
264   vec_foreach (dq, rt->devices_and_queues)
265     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
266     {
267       *mode = dq->mode;
268       return 0;
269     }
270
271   return VNET_API_ERROR_INVALID_INTERFACE;
272 }
273
274 static clib_error_t *
275 show_device_placement_fn (vlib_main_t * vm, unformat_input_t * input,
276                           vlib_cli_command_t * cmd)
277 {
278   u8 *s = 0;
279   vnet_main_t *vnm = vnet_get_main ();
280   vnet_device_input_runtime_t *rt;
281   vnet_device_and_queue_t *dq;
282   vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
283   uword si;
284   int index = 0;
285
286   /* *INDENT-OFF* */
287   foreach_vlib_main (({
288     clib_bitmap_foreach (si, pn->sibling_bitmap,
289       ({
290         rt = vlib_node_get_runtime_data (this_vlib_main, si);
291
292         if (vec_len (rt->devices_and_queues))
293           s = format (s, "  node %U:\n", format_vlib_node_name, vm, si);
294
295         vec_foreach (dq, rt->devices_and_queues)
296           {
297             s = format (s, "    %U queue %u (%s)\n",
298                         format_vnet_sw_if_index_name, vnm, dq->hw_if_index,
299                         dq->queue_id,
300                         dq->mode == VNET_DEVICE_INPUT_MODE_POLLING ?
301                         "polling" : "interrupt");
302           }
303       }));
304     if (vec_len (s) > 0)
305       {
306         vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
307                         vlib_worker_threads[index].name, s);
308         vec_reset_length (s);
309       }
310     index++;
311   }));
312   /* *INDENT-ON* */
313
314   vec_free (s);
315   return 0;
316 }
317
318 /* *INDENT-OFF* */
319 VLIB_CLI_COMMAND (memif_delete_command, static) = {
320   .path = "show interface placement",
321   .short_help = "show interface placement",
322   .function = show_device_placement_fn,
323 };
324 /* *INDENT-ON* */
325
326 static clib_error_t *
327 set_device_placement (vlib_main_t * vm, unformat_input_t * input,
328                       vlib_cli_command_t * cmd)
329 {
330   clib_error_t *error = 0;
331   unformat_input_t _line_input, *line_input = &_line_input;
332   vnet_main_t *vnm = vnet_get_main ();
333   vnet_device_main_t *vdm = &vnet_device_main;
334   vnet_device_input_mode_t mode;
335   u32 hw_if_index = (u32) ~ 0;
336   u32 queue_id = (u32) 0;
337   u32 thread_index = (u32) ~ 0;
338   int rv;
339
340   if (!unformat_user (input, unformat_line_input, line_input))
341     return 0;
342
343   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
344     {
345       if (unformat
346           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
347         ;
348       else if (unformat (line_input, "queue %d", &queue_id))
349         ;
350       else if (unformat (line_input, "main", &thread_index))
351         thread_index = 0;
352       else if (unformat (line_input, "worker %d", &thread_index))
353         thread_index += vdm->first_worker_thread_index;
354       else
355         {
356           error = clib_error_return (0, "parse error: '%U'",
357                                      format_unformat_error, line_input);
358           unformat_free (line_input);
359           return error;
360         }
361     }
362
363   unformat_free (line_input);
364
365   if (hw_if_index == (u32) ~ 0)
366     return clib_error_return (0, "please specify valid interface name");
367
368   if (thread_index > vdm->last_worker_thread_index)
369     return clib_error_return (0,
370                               "please specify valid worker thread or main");
371
372   rv = vnet_device_input_get_mode (vnm, hw_if_index, queue_id, &mode);
373
374   if (rv)
375     return clib_error_return (0, "not found");
376
377   rv = vnet_device_input_unassign_thread (vnm, hw_if_index, queue_id,
378                                           thread_index);
379
380   if (rv)
381     return clib_error_return (0, "not found");
382
383   vnet_device_input_assign_thread (vnm, hw_if_index, queue_id, thread_index);
384   vnet_device_input_set_mode (vnm, hw_if_index, queue_id, mode);
385
386   return 0;
387 }
388
389 /*?
390  * This command is used to assign a given interface, and optionally a
391  * given queue, to a different thread. If the '<em>queue</em>' is not provided,
392  * it defaults to 0.
393  *
394  * @cliexpar
395  * Example of how to display the interface placement:
396  * @cliexstart{show interface placement}
397  * Thread 1 (vpp_wk_0):
398  *   GigabitEthernet0/8/0 queue 0
399  *   GigabitEthernet0/9/0 queue 0
400  * Thread 2 (vpp_wk_1):
401  *   GigabitEthernet0/8/0 queue 1
402  *   GigabitEthernet0/9/0 queue 1
403  * @cliexend
404  * Example of how to assign a interface and queue to a thread:
405  * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
406 ?*/
407 /* *INDENT-OFF* */
408 VLIB_CLI_COMMAND (cmd_set_dpdk_if_placement,static) = {
409     .path = "set interface placement",
410     .short_help = "set interface placement <interface> [queue <n>] [thread <n> | main]",
411     .function = set_device_placement,
412 };
413 /* *INDENT-ON* */
414
415 static clib_error_t *
416 vnet_device_init (vlib_main_t * vm)
417 {
418   vnet_device_main_t *vdm = &vnet_device_main;
419   vlib_thread_main_t *tm = vlib_get_thread_main ();
420   vlib_thread_registration_t *tr;
421   uword *p;
422
423   vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
424                         CLIB_CACHE_LINE_BYTES);
425
426   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
427   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
428   if (tr && tr->count > 0)
429     {
430       vdm->first_worker_thread_index = tr->first_index;
431       vdm->next_worker_thread_index = tr->first_index;
432       vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
433     }
434   return 0;
435 }
436
437 VLIB_INIT_FUNCTION (vnet_device_init);
438
439 /*
440  * fd.io coding-style-patch-verification: ON
441  *
442  * Local Variables:
443  * eval: (c-set-style "gnu")
444  * End:
445  */