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