c8a95087ec619211d076e7fa5a7705e5b36e5360
[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   if (old_thread_index == thread_index)
178     return 0;
179
180   vm = vlib_mains[old_thread_index];
181
182   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
183
184   vec_foreach (dq, rt->devices_and_queues)
185     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
186     {
187       vec_del1 (rt->devices_and_queues, dq - rt->devices_and_queues);
188       goto deleted;
189     }
190
191   return VNET_API_ERROR_INVALID_INTERFACE;
192
193 deleted:
194
195   vnet_device_queue_update (vnm, rt);
196
197   if (vec_len (rt->devices_and_queues) == 0)
198     vlib_node_set_state (vm, hw->input_node_index, VLIB_NODE_STATE_DISABLED);
199
200   return 0;
201 }
202
203
204 int
205 vnet_device_input_set_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
206                             vnet_device_input_mode_t mode)
207 {
208   vlib_main_t *vm;
209   uword thread_index;
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;
215   int is_polling = 0;
216
217   if (hw->input_node_thread_index_by_queue == 0)
218     return VNET_API_ERROR_INVALID_INTERFACE;
219
220   thread_index = hw->input_node_thread_index_by_queue[queue_id];
221   vm = vlib_mains[thread_index];
222
223   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
224
225   vec_foreach (dq, rt->devices_and_queues)
226   {
227     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
228       dq->mode = mode;
229     if (dq->mode == VNET_DEVICE_INPUT_MODE_POLLING)
230       is_polling = 1;
231   }
232
233   if (is_polling)
234     enabled_node_state = VLIB_NODE_STATE_POLLING;
235   else
236     enabled_node_state = VLIB_NODE_STATE_INTERRUPT;
237
238   if (rt->enabled_node_state != enabled_node_state)
239     {
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);
244     }
245
246   return 0;
247 }
248
249 int
250 vnet_device_input_get_mode (vnet_main_t * vnm, u32 hw_if_index, u16 queue_id,
251                             vnet_device_input_mode_t * mode)
252 {
253   vlib_main_t *vm;
254   uword thread_index;
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;
258
259   if (hw->input_node_thread_index_by_queue == 0)
260     return VNET_API_ERROR_INVALID_INTERFACE;
261
262   thread_index = hw->input_node_thread_index_by_queue[queue_id];
263   vm = vlib_mains[thread_index];
264
265   rt = vlib_node_get_runtime_data (vm, hw->input_node_index);
266
267   vec_foreach (dq, rt->devices_and_queues)
268     if (dq->hw_if_index == hw_if_index && dq->queue_id == queue_id)
269     {
270       *mode = dq->mode;
271       return 0;
272     }
273
274   return VNET_API_ERROR_INVALID_INTERFACE;
275 }
276
277 static clib_error_t *
278 show_device_placement_fn (vlib_main_t * vm, unformat_input_t * input,
279                           vlib_cli_command_t * cmd)
280 {
281   u8 *s = 0;
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");
286   uword si;
287   int index = 0;
288
289   /* *INDENT-OFF* */
290   foreach_vlib_main (({
291     clib_bitmap_foreach (si, pn->sibling_bitmap,
292       ({
293         rt = vlib_node_get_runtime_data (this_vlib_main, si);
294
295         if (vec_len (rt->devices_and_queues))
296           s = format (s, "  node %U:\n", format_vlib_node_name, vm, si);
297
298         vec_foreach (dq, rt->devices_and_queues)
299           {
300             s = format (s, "    %U queue %u (%s)\n",
301                         format_vnet_sw_if_index_name, vnm, dq->hw_if_index,
302                         dq->queue_id,
303                         dq->mode == VNET_DEVICE_INPUT_MODE_POLLING ?
304                         "polling" : "interrupt");
305           }
306       }));
307     if (vec_len (s) > 0)
308       {
309         vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
310                         vlib_worker_threads[index].name, s);
311         vec_reset_length (s);
312       }
313     index++;
314   }));
315   /* *INDENT-ON* */
316
317   vec_free (s);
318   return 0;
319 }
320
321 /* *INDENT-OFF* */
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,
326 };
327 /* *INDENT-ON* */
328
329 static clib_error_t *
330 set_device_placement (vlib_main_t * vm, unformat_input_t * input,
331                       vlib_cli_command_t * cmd)
332 {
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;
341   int rv;
342
343   if (!unformat_user (input, unformat_line_input, line_input))
344     return 0;
345
346   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
347     {
348       if (unformat
349           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
350         ;
351       else if (unformat (line_input, "queue %d", &queue_id))
352         ;
353       else if (unformat (line_input, "main", &thread_index))
354         thread_index = 0;
355       else if (unformat (line_input, "worker %d", &thread_index))
356         thread_index += vdm->first_worker_thread_index;
357       else
358         {
359           error = clib_error_return (0, "parse error: '%U'",
360                                      format_unformat_error, line_input);
361           unformat_free (line_input);
362           return error;
363         }
364     }
365
366   unformat_free (line_input);
367
368   if (hw_if_index == (u32) ~ 0)
369     return clib_error_return (0, "please specify valid interface name");
370
371   if (thread_index > vdm->last_worker_thread_index)
372     return clib_error_return (0,
373                               "please specify valid worker thread or main");
374
375   rv = vnet_device_input_get_mode (vnm, hw_if_index, queue_id, &mode);
376
377   if (rv)
378     return clib_error_return (0, "not found");
379
380   rv = vnet_device_input_unassign_thread (vnm, hw_if_index, queue_id,
381                                           thread_index);
382
383   if (rv)
384     return clib_error_return (0, "not found");
385
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);
388
389   return 0;
390 }
391
392 /*?
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,
395  * it defaults to 0.
396  *
397  * @cliexpar
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
406  * @cliexend
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}
409 ?*/
410 /* *INDENT-OFF* */
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,
415 };
416 /* *INDENT-ON* */
417
418 static clib_error_t *
419 vnet_device_init (vlib_main_t * vm)
420 {
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;
424   uword *p;
425
426   vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
427                         CLIB_CACHE_LINE_BYTES);
428
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)
432     {
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;
436     }
437   return 0;
438 }
439
440 VLIB_INIT_FUNCTION (vnet_device_init);
441
442 /*
443  * fd.io coding-style-patch-verification: ON
444  *
445  * Local Variables:
446  * eval: (c-set-style "gnu")
447  * End:
448  */