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