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