vmxnet3: auto bind support
[vpp.git] / src / plugins / vmxnet3 / cli.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/pci/pci.h>
25 #include <vnet/ethernet/ethernet.h>
26
27 #include <vmxnet3/vmxnet3.h>
28
29 static clib_error_t *
30 vmxnet3_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
31                            vlib_cli_command_t * cmd)
32 {
33   unformat_input_t _line_input, *line_input = &_line_input;
34   vmxnet3_create_if_args_t args;
35
36   /* Get a line of input. */
37   if (!unformat_user (input, unformat_line_input, line_input))
38     return 0;
39
40   clib_memset (&args, 0, sizeof (args));
41   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
42     {
43       if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
44         ;
45       else if (unformat (line_input, "elog"))
46         args.enable_elog = 1;
47       else if (unformat (line_input, "bind"))
48         args.bind = 1;
49       else if (unformat (line_input, "rx-queue-size %u", &args.rxq_size))
50         ;
51       else if (unformat (line_input, "tx-queue-size %u", &args.txq_size))
52         ;
53       else if (unformat (line_input, "num-tx-queues %u", &args.txq_num))
54         ;
55       else if (unformat (line_input, "num-rx-queues %u", &args.rxq_num))
56         ;
57       else
58         return clib_error_return (0, "unknown input `%U'",
59                                   format_unformat_error, input);
60     }
61   unformat_free (line_input);
62
63
64   vmxnet3_create_if (vm, &args);
65
66   return args.error;
67 }
68
69 /* *INDENT-OFF* */
70 VLIB_CLI_COMMAND (vmxnet3_create_command, static) = {
71   .path = "create interface vmxnet3",
72   .short_help = "create interface vmxnet3 <pci-address>"
73                 "[rx-queue-size <size>] [tx-queue-size <size>]"
74                 "[num-tx-queues <number>] [num-rx-queues <number>] [bind]",
75   .function = vmxnet3_create_command_fn,
76 };
77 /* *INDENT-ON* */
78
79 static clib_error_t *
80 vmxnet3_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
81                            vlib_cli_command_t * cmd)
82 {
83   unformat_input_t _line_input, *line_input = &_line_input;
84   u32 sw_if_index = ~0;
85   vnet_hw_interface_t *hw;
86   vmxnet3_main_t *vmxm = &vmxnet3_main;
87   vmxnet3_device_t *vd;
88   vnet_main_t *vnm = vnet_get_main ();
89
90   /* Get a line of input. */
91   if (!unformat_user (input, unformat_line_input, line_input))
92     return 0;
93
94   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
95     {
96       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
97         ;
98       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
99                          vnm, &sw_if_index))
100         ;
101       else
102         return clib_error_return (0, "unknown input `%U'",
103                                   format_unformat_error, input);
104     }
105   unformat_free (line_input);
106
107   if (sw_if_index == ~0)
108     return clib_error_return (0,
109                               "please specify interface name or sw_if_index");
110
111   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
112   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
113     return clib_error_return (0, "not a vmxnet3 interface");
114
115   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
116
117   vmxnet3_delete_if (vm, vd);
118
119   return 0;
120 }
121
122 /* *INDENT-OFF* */
123 VLIB_CLI_COMMAND (vmxnet3_delete_command, static) = {
124   .path = "delete interface vmxnet3",
125   .short_help = "delete interface vmxnet3 "
126     "{<interface> | sw_if_index <sw_idx>}",
127   .function = vmxnet3_delete_command_fn,
128 };
129 /* *INDENT-ON* */
130
131 static clib_error_t *
132 vmxnet3_test_command_fn (vlib_main_t * vm, unformat_input_t * input,
133                          vlib_cli_command_t * cmd)
134 {
135   unformat_input_t _line_input, *line_input = &_line_input;
136   u32 sw_if_index = ~0;
137   vnet_hw_interface_t *hw;
138   vmxnet3_main_t *vmxm = &vmxnet3_main;
139   vmxnet3_device_t *vd;
140   vnet_main_t *vnm = vnet_get_main ();
141   int enable_elog = 0, disable_elog = 0;
142
143   /* Get a line of input. */
144   if (!unformat_user (input, unformat_line_input, line_input))
145     return 0;
146
147   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
148     {
149       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
150         ;
151       else if (unformat (line_input, "elog-on"))
152         enable_elog = 1;
153       else if (unformat (line_input, "elog-off"))
154         disable_elog = 1;
155       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
156                          vnm, &sw_if_index))
157         ;
158       else
159         return clib_error_return (0, "unknown input `%U'",
160                                   format_unformat_error, input);
161     }
162   unformat_free (line_input);
163
164   if (sw_if_index == ~0)
165     return clib_error_return (0,
166                               "please specify interface name or sw_if_index");
167
168   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
169   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
170     return clib_error_return (0, "not a vmxnet3 interface");
171
172   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
173
174   if (enable_elog)
175     vd->flags |= VMXNET3_DEVICE_F_ELOG;
176
177   if (disable_elog)
178     vd->flags &= ~VMXNET3_DEVICE_F_ELOG;
179
180   return 0;
181 }
182
183 /* *INDENT-OFF* */
184 VLIB_CLI_COMMAND (vmxnet3_test_command, static) = {
185   .path = "test vmxnet3",
186   .short_help = "test vmxnet3 <interface> | sw_if_index <sw_idx> [irq] "
187     "[elog-on] [elog-off]",
188   .function = vmxnet3_test_command_fn,
189 };
190 /* *INDENT-ON* */
191
192 static void
193 show_vmxnet3 (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
194               u8 show_one_table, u32 which, u8 show_one_slot, u32 slot)
195 {
196   u32 i, desc_idx;
197   vmxnet3_device_t *vd;
198   vnet_main_t *vnm = &vnet_main;
199   vmxnet3_main_t *vmxm = &vmxnet3_main;
200   vnet_hw_interface_t *hi;
201   vmxnet3_rxq_t *rxq;
202   vmxnet3_rx_desc *rxd;
203   vmxnet3_rx_comp *rx_comp;
204   vmxnet3_txq_t *txq;
205   vmxnet3_tx_desc *txd;
206   vmxnet3_tx_comp *tx_comp;
207   u16 qid;
208
209   if (!hw_if_indices)
210     return;
211
212   vlib_cli_output (vm, "LRO/TSO configured: %u", vmxm->lro_configured);
213   for (i = 0; i < vec_len (hw_if_indices); i++)
214     {
215       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
216       vd = vec_elt_at_index (vmxm->devices, hi->dev_instance);
217       vlib_cli_output (vm, "Interface: %U (ifindex %d)",
218                        format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
219                        hw_if_indices[i]);
220       vlib_cli_output (vm, "  Version: %u", vd->version);
221       vlib_cli_output (vm, "  LRO/TSO enable: %u", vd->lro_enable);
222       vlib_cli_output (vm, "  PCI Address: %U", format_vlib_pci_addr,
223                        &vd->pci_addr);
224       vlib_cli_output (vm, "  Mac Address: %U", format_ethernet_address,
225                        vd->mac_addr);
226       vlib_cli_output (vm, "  hw if index: %u", vd->hw_if_index);
227       vlib_cli_output (vm, "  Device instance: %u", vd->dev_instance);
228       vlib_cli_output (vm, "  Number of interrupts: %u", vd->num_intrs);
229
230       vec_foreach_index (qid, vd->rxqs)
231       {
232         rxq = vec_elt_at_index (vd->rxqs, qid);
233         u16 rid;
234
235         vlib_cli_output (vm, "  Queue %u (RX)", qid);
236         vlib_cli_output (vm, "    RX completion next index %u",
237                          rxq->rx_comp_ring.next);
238         vlib_cli_output (vm, "    RX completion generation flag 0x%x",
239                          rxq->rx_comp_ring.gen);
240
241         /* RX descriptors tables */
242         for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
243           {
244             vmxnet3_rx_ring *ring = &rxq->rx_ring[rid];
245
246             vlib_cli_output (vm,
247                              "    ring %u size %u fill %u "
248                              "consume %u produce %u", rid,
249                              rxq->size, ring->fill, ring->consume,
250                              ring->produce);
251             if (show_descr)
252               {
253                 vlib_cli_output (vm, "RX descriptors table");
254                 vlib_cli_output (vm, "  %5s  %18s  %10s",
255                                  "slot", "address", "flags");
256                 for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
257                   {
258                     rxd = &rxq->rx_desc[rid][desc_idx];
259                     vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
260                                      desc_idx, rxd->address, rxd->flags);
261                   }
262               }
263             else if (show_one_table)
264               {
265                 if (((which == VMXNET3_SHOW_RX_DESC0) && (rid == 0)) ||
266                     ((which == VMXNET3_SHOW_RX_DESC1) && (rid == 1)))
267                   {
268                     vlib_cli_output (vm, "RX descriptors table");
269                     vlib_cli_output (vm, "  %5s  %18s  %10s",
270                                      "slot", "address", "flags");
271                     if (show_one_slot)
272                       {
273                         rxd = &rxq->rx_desc[rid][slot];
274                         vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
275                                          slot, rxd->address, rxd->flags);
276                       }
277                     else
278                       for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
279                         {
280                           rxd = &rxq->rx_desc[rid][desc_idx];
281                           vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
282                                            desc_idx, rxd->address,
283                                            rxd->flags);
284                         }
285                   }
286               }
287           }
288
289         /* RX completion table */
290         if (show_descr)
291           {
292             vlib_cli_output (vm, "RX completion descriptors table");
293             vlib_cli_output (vm, "  %5s  %10s  %10s  %10s  %10s",
294                              "slot", "index", "rss", "len", "flags");
295             for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
296               {
297                 rx_comp = &rxq->rx_comp[desc_idx];
298                 vlib_cli_output (vm, "  %5u  0x%08x  %10u  %10u  0x%08x",
299                                  desc_idx, rx_comp->index, rx_comp->rss,
300                                  rx_comp->len, rx_comp->flags);
301               }
302           }
303         else if (show_one_table)
304           {
305             if (which == VMXNET3_SHOW_RX_COMP)
306               {
307                 vlib_cli_output (vm, "RX completion descriptors table");
308                 vlib_cli_output (vm, "  %5s  %10s  %10s  %10s  %10s",
309                                  "slot", "index", "rss", "len", "flags");
310                 if (show_one_slot)
311                   {
312                     rx_comp = &rxq->rx_comp[slot];
313                     vlib_cli_output (vm, "  %5u  0x%08x  %10u  %10u  0x%08x",
314                                      slot, rx_comp->index, rx_comp->rss,
315                                      rx_comp->len, rx_comp->flags);
316                   }
317                 else
318                   for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
319                     {
320                       rx_comp = &rxq->rx_comp[desc_idx];
321                       vlib_cli_output (vm,
322                                        "  %5u  0x%08x  %10u  %10u  0x%08x",
323                                        desc_idx, rx_comp->index, rx_comp->rss,
324                                        rx_comp->len, rx_comp->flags);
325                     }
326               }
327           }
328       }
329
330       vec_foreach_index (qid, vd->txqs)
331       {
332         txq = vec_elt_at_index (vd->txqs, qid);
333         vlib_cli_output (vm, "  Queue %u (TX)", qid);
334         vlib_cli_output (vm, "    TX completion next index %u",
335                          txq->tx_comp_ring.next);
336         vlib_cli_output (vm, "    TX completion generation flag 0x%x",
337                          txq->tx_comp_ring.gen);
338         vlib_cli_output (vm, "    size %u consume %u produce %u",
339                          txq->size, txq->tx_ring.consume,
340                          txq->tx_ring.produce);
341         if (show_descr)
342           {
343             vlib_cli_output (vm, "TX descriptors table");
344             vlib_cli_output (vm, "  %5s  %18s  %10s  %10s",
345                              "slot", "address", "flags0", "flags1");
346             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
347               {
348                 txd = &txq->tx_desc[desc_idx];
349                 vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
350                                  desc_idx, txd->address, txd->flags[0],
351                                  txd->flags[1]);
352               }
353
354             vlib_cli_output (vm, "TX completion descriptors table");
355             vlib_cli_output (vm, "  %5s  %10s  %10s",
356                              "slot", "index", "flags");
357             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
358               {
359                 tx_comp = &txq->tx_comp[desc_idx];
360                 vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
361                                  desc_idx, tx_comp->index, tx_comp->flags);
362               }
363           }
364         else if (show_one_table)
365           {
366             if (which == VMXNET3_SHOW_TX_DESC)
367               {
368                 vlib_cli_output (vm, "TX descriptors table");
369                 vlib_cli_output (vm, "  %5s  %18s  %10s  %10s",
370                                  "slot", "address", "flags0", "flags1");
371                 if (show_one_slot)
372                   {
373                     txd = &txq->tx_desc[slot];
374                     vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
375                                      slot, txd->address, txd->flags[0],
376                                      txd->flags[1]);
377                   }
378                 else
379                   for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
380                     {
381                       txd = &txq->tx_desc[desc_idx];
382                       vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
383                                        desc_idx, txd->address, txd->flags[0],
384                                        txd->flags[1]);
385                     }
386               }
387             else if (which == VMXNET3_SHOW_TX_COMP)
388               {
389                 vlib_cli_output (vm, "TX completion descriptors table");
390                 vlib_cli_output (vm, "  %5s  %10s  %10s",
391                                  "slot", "index", "flags");
392                 if (show_one_slot)
393                   {
394                     tx_comp = &txq->tx_comp[slot];
395                     vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
396                                      slot, tx_comp->index, tx_comp->flags);
397                   }
398                 else
399                   for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
400                     {
401                       tx_comp = &txq->tx_comp[desc_idx];
402                       vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
403                                        desc_idx, tx_comp->index,
404                                        tx_comp->flags);
405                     }
406               }
407           }
408       }
409     }
410 }
411
412 static clib_error_t *
413 show_vmxnet3_fn (vlib_main_t * vm, unformat_input_t * input,
414                  vlib_cli_command_t * cmd)
415 {
416   vmxnet3_main_t *vmxm = &vmxnet3_main;
417   vnet_main_t *vnm = &vnet_main;
418   vmxnet3_device_t *vd;
419   clib_error_t *error = 0;
420   u32 hw_if_index, *hw_if_indices = 0;
421   vnet_hw_interface_t *hi = 0;
422   u8 show_descr = 0, show_one_table = 0, show_one_slot = 0;
423   u32 which = ~0, slot;
424
425   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
426     {
427       if (unformat
428           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
429         {
430           hi = vnet_get_hw_interface (vnm, hw_if_index);
431           if (vmxnet3_device_class.index != hi->dev_class_index)
432             {
433               error = clib_error_return (0, "unknown input `%U'",
434                                          format_unformat_error, input);
435               goto done;
436             }
437           vec_add1 (hw_if_indices, hw_if_index);
438         }
439       else if (unformat (input, "desc"))
440         show_descr = 1;
441       else if (hi)
442         {
443           vmxnet3_device_t *vd =
444             vec_elt_at_index (vmxm->devices, hi->dev_instance);
445
446           if (unformat (input, "rx-comp"))
447             {
448               show_one_table = 1;
449               which = VMXNET3_SHOW_RX_COMP;
450               if (unformat (input, "%u", &slot))
451                 {
452                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
453
454                   if (slot >= rxq->size)
455                     {
456                       error = clib_error_return (0,
457                                                  "slot size must be < rx queue "
458                                                  "size %u", rxq->size);
459                       goto done;
460                     }
461                   show_one_slot = 1;
462                 }
463             }
464           else if (unformat (input, "rx-desc-0"))
465             {
466               show_one_table = 1;
467               which = VMXNET3_SHOW_RX_DESC0;
468               if (unformat (input, "%u", &slot))
469                 {
470                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
471
472                   if (slot >= rxq->size)
473                     {
474                       error = clib_error_return (0,
475                                                  "slot size must be < rx queue "
476                                                  "size %u", rxq->size);
477                       goto done;
478                     }
479                   show_one_slot = 1;
480                 }
481             }
482           else if (unformat (input, "rx-desc-1"))
483             {
484               show_one_table = 1;
485               which = VMXNET3_SHOW_RX_DESC1;
486               if (unformat (input, "%u", &slot))
487                 {
488                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
489
490                   if (slot >= rxq->size)
491                     {
492                       error = clib_error_return (0,
493                                                  "slot size must be < rx queue "
494                                                  "size %u", rxq->size);
495                       goto done;
496                     }
497                   show_one_slot = 1;
498                 }
499             }
500           else if (unformat (input, "tx-comp"))
501             {
502               show_one_table = 1;
503               which = VMXNET3_SHOW_TX_COMP;
504               if (unformat (input, "%u", &slot))
505                 {
506                   vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, 0);
507
508                   if (slot >= txq->size)
509                     {
510                       error = clib_error_return (0,
511                                                  "slot size must be < tx queue "
512                                                  "size %u", txq->size);
513                       goto done;
514                     }
515                   show_one_slot = 1;
516                 }
517             }
518           else if (unformat (input, "tx-desc"))
519             {
520               show_one_table = 1;
521               which = VMXNET3_SHOW_TX_DESC;
522               if (unformat (input, "%u", &slot))
523                 {
524                   vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, 0);
525
526                   if (slot >= txq->size)
527                     {
528                       error = clib_error_return (0,
529                                                  "slot size must be < tx queue "
530                                                  "size %u", txq->size);
531                       goto done;
532                     }
533                   show_one_slot = 1;
534                 }
535             }
536           else
537             {
538               error = clib_error_return (0, "unknown input `%U'",
539                                          format_unformat_error, input);
540               goto done;
541             }
542         }
543       else
544         {
545           error = clib_error_return (0, "unknown input `%U'",
546                                      format_unformat_error, input);
547           goto done;
548         }
549     }
550
551   if (vec_len (hw_if_indices) == 0)
552     {
553       pool_foreach (vd, vmxm->devices,
554                     vec_add1 (hw_if_indices, vd->hw_if_index);
555         );
556     }
557
558   show_vmxnet3 (vm, hw_if_indices, show_descr, show_one_table, which,
559                 show_one_slot, slot);
560
561 done:
562   vec_free (hw_if_indices);
563   return error;
564 }
565
566 /* *INDENT-OFF* */
567 VLIB_CLI_COMMAND (show_vmxnet3_command, static) = {
568   .path = "show vmxnet3",
569   .short_help = "show vmxnet3 [[<interface>] ([desc] | ([rx-comp] | "
570   "[rx-desc-0] | [rx-desc-1] | [tx-comp] | [tx-desc]) [<slot>])]",
571   .function = show_vmxnet3_fn,
572 };
573 /* *INDENT-ON* */
574
575 clib_error_t *
576 vmxnet3_cli_init (vlib_main_t * vm)
577 {
578   vmxnet3_main_t *vmxm = &vmxnet3_main;
579
580   /* initialize binary API */
581   vmxnet3_plugin_api_hookup (vm);
582
583   vmxm->log_default = vlib_log_register_class ("vmxnet3", 0);
584   return 0;
585 }
586
587 VLIB_INIT_FUNCTION (vmxnet3_cli_init);
588
589 static clib_error_t *
590 vmxnet3_config (vlib_main_t * vm, unformat_input_t * input)
591 {
592   vmxnet3_main_t *vmxm = &vmxnet3_main;
593
594   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
595     {
596       if (unformat (input, "lro"))
597         vmxm->lro_configured = 1;
598       else
599         return clib_error_return (0, "unknown input `%U'",
600                                   format_unformat_error, input);
601     }
602
603   return 0;
604 }
605
606 /* vmxnet3 { ... } configuration. */
607 VLIB_CONFIG_FUNCTION (vmxnet3_config, "vmxnet3");
608
609 /*
610  * fd.io coding-style-patch-verification: ON
611  *
612  * Local Variables:
613  * eval: (c-set-style "gnu")
614  * End:
615  */