vmxnet3: lro/tso and rx checksum
[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   vlib_cli_output (vm, "LRO/TSO configured: %u", vmxm->lro_configured);
209   for (i = 0; i < vec_len (hw_if_indices); i++)
210     {
211       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
212       vd = vec_elt_at_index (vmxm->devices, hi->dev_instance);
213       vlib_cli_output (vm, "Interface: %U (ifindex %d)",
214                        format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
215                        hw_if_indices[i]);
216       vlib_cli_output (vm, "  Version: %u", vd->version);
217       vlib_cli_output (vm, "  LRO/TSO enable: %u", vd->lro_enable);
218       vlib_cli_output (vm, "  PCI Address: %U", format_vlib_pci_addr,
219                        &vd->pci_addr);
220       vlib_cli_output (vm, "  Mac Address: %U", format_ethernet_address,
221                        vd->mac_addr);
222       vlib_cli_output (vm, "  hw if index: %u", vd->hw_if_index);
223       vlib_cli_output (vm, "  Device instance: %u", vd->dev_instance);
224       vlib_cli_output (vm, "  Number of interrupts: %u", vd->num_intrs);
225
226       vec_foreach_index (qid, vd->rxqs)
227       {
228         rxq = vec_elt_at_index (vd->rxqs, qid);
229         u16 rid;
230
231         vlib_cli_output (vm, "  Queue %u (RX)", qid);
232         vlib_cli_output (vm, "    RX completion next index %u",
233                          rxq->rx_comp_ring.next);
234         vlib_cli_output (vm, "    RX completion generation flag 0x%x",
235                          rxq->rx_comp_ring.gen);
236
237         /* RX descriptors tables */
238         for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
239           {
240             vmxnet3_rx_ring *ring = &rxq->rx_ring[rid];
241
242             vlib_cli_output (vm,
243                              "    ring %u size %u fill %u "
244                              "consume %u produce %u", rid,
245                              rxq->size, ring->fill, ring->consume,
246                              ring->produce);
247             if (show_descr)
248               {
249                 vlib_cli_output (vm, "RX descriptors table");
250                 vlib_cli_output (vm, "  %5s  %18s  %10s",
251                                  "slot", "address", "flags");
252                 for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
253                   {
254                     rxd = &rxq->rx_desc[rid][desc_idx];
255                     vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
256                                      desc_idx, rxd->address, rxd->flags);
257                   }
258               }
259             else if (show_one_table)
260               {
261                 if (((which == VMXNET3_SHOW_RX_DESC0) && (rid == 0)) ||
262                     ((which == VMXNET3_SHOW_RX_DESC1) && (rid == 1)))
263                   {
264                     vlib_cli_output (vm, "RX descriptors table");
265                     vlib_cli_output (vm, "  %5s  %18s  %10s",
266                                      "slot", "address", "flags");
267                     if (show_one_slot)
268                       {
269                         rxd = &rxq->rx_desc[rid][slot];
270                         vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
271                                          slot, rxd->address, rxd->flags);
272                       }
273                     else
274                       for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
275                         {
276                           rxd = &rxq->rx_desc[rid][desc_idx];
277                           vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
278                                            desc_idx, rxd->address,
279                                            rxd->flags);
280                         }
281                   }
282               }
283           }
284
285         /* RX completion table */
286         if (show_descr)
287           {
288             vlib_cli_output (vm, "RX completion descriptors table");
289             vlib_cli_output (vm, "  %5s  %10s  %10s  %10s  %10s",
290                              "slot", "index", "rss", "len", "flags");
291             for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
292               {
293                 rx_comp = &rxq->rx_comp[desc_idx];
294                 vlib_cli_output (vm, "  %5u  0x%08x  %10u  %10u  0x%08x",
295                                  desc_idx, rx_comp->index, rx_comp->rss,
296                                  rx_comp->len, rx_comp->flags);
297               }
298           }
299         else if (show_one_table)
300           {
301             if (which == VMXNET3_SHOW_RX_COMP)
302               {
303                 vlib_cli_output (vm, "RX completion descriptors table");
304                 vlib_cli_output (vm, "  %5s  %10s  %10s  %10s  %10s",
305                                  "slot", "index", "rss", "len", "flags");
306                 if (show_one_slot)
307                   {
308                     rx_comp = &rxq->rx_comp[slot];
309                     vlib_cli_output (vm, "  %5u  0x%08x  %10u  %10u  0x%08x",
310                                      slot, rx_comp->index, rx_comp->rss,
311                                      rx_comp->len, rx_comp->flags);
312                   }
313                 else
314                   for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
315                     {
316                       rx_comp = &rxq->rx_comp[desc_idx];
317                       vlib_cli_output (vm,
318                                        "  %5u  0x%08x  %10u  %10u  0x%08x",
319                                        desc_idx, rx_comp->index, rx_comp->rss,
320                                        rx_comp->len, rx_comp->flags);
321                     }
322               }
323           }
324       }
325
326       vec_foreach_index (qid, vd->txqs)
327       {
328         txq = vec_elt_at_index (vd->txqs, qid);
329         vlib_cli_output (vm, "  Queue %u (TX)", qid);
330         vlib_cli_output (vm, "    TX completion next index %u",
331                          txq->tx_comp_ring.next);
332         vlib_cli_output (vm, "    TX completion generation flag 0x%x",
333                          txq->tx_comp_ring.gen);
334         vlib_cli_output (vm, "    size %u consume %u produce %u",
335                          txq->size, txq->tx_ring.consume,
336                          txq->tx_ring.produce);
337         if (show_descr)
338           {
339             vlib_cli_output (vm, "TX descriptors table");
340             vlib_cli_output (vm, "  %5s  %18s  %10s  %10s",
341                              "slot", "address", "flags0", "flags1");
342             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
343               {
344                 txd = &txq->tx_desc[desc_idx];
345                 vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
346                                  desc_idx, txd->address, txd->flags[0],
347                                  txd->flags[1]);
348               }
349
350             vlib_cli_output (vm, "TX completion descriptors table");
351             vlib_cli_output (vm, "  %5s  %10s  %10s",
352                              "slot", "index", "flags");
353             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
354               {
355                 tx_comp = &txq->tx_comp[desc_idx];
356                 vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
357                                  desc_idx, tx_comp->index, tx_comp->flags);
358               }
359           }
360         else if (show_one_table)
361           {
362             if (which == VMXNET3_SHOW_TX_DESC)
363               {
364                 vlib_cli_output (vm, "TX descriptors table");
365                 vlib_cli_output (vm, "  %5s  %18s  %10s  %10s",
366                                  "slot", "address", "flags0", "flags1");
367                 if (show_one_slot)
368                   {
369                     txd = &txq->tx_desc[slot];
370                     vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
371                                      slot, txd->address, txd->flags[0],
372                                      txd->flags[1]);
373                   }
374                 else
375                   for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
376                     {
377                       txd = &txq->tx_desc[desc_idx];
378                       vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
379                                        desc_idx, txd->address, txd->flags[0],
380                                        txd->flags[1]);
381                     }
382               }
383             else if (which == VMXNET3_SHOW_TX_COMP)
384               {
385                 vlib_cli_output (vm, "TX completion descriptors table");
386                 vlib_cli_output (vm, "  %5s  %10s  %10s",
387                                  "slot", "index", "flags");
388                 if (show_one_slot)
389                   {
390                     tx_comp = &txq->tx_comp[slot];
391                     vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
392                                      slot, tx_comp->index, tx_comp->flags);
393                   }
394                 else
395                   for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
396                     {
397                       tx_comp = &txq->tx_comp[desc_idx];
398                       vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
399                                        desc_idx, tx_comp->index,
400                                        tx_comp->flags);
401                     }
402               }
403           }
404       }
405     }
406 }
407
408 static clib_error_t *
409 show_vmxnet3_fn (vlib_main_t * vm, unformat_input_t * input,
410                  vlib_cli_command_t * cmd)
411 {
412   vmxnet3_main_t *vmxm = &vmxnet3_main;
413   vnet_main_t *vnm = &vnet_main;
414   vmxnet3_device_t *vd;
415   clib_error_t *error = 0;
416   u32 hw_if_index, *hw_if_indices = 0;
417   vnet_hw_interface_t *hi = 0;
418   u8 show_descr = 0, show_one_table = 0, show_one_slot = 0;
419   u32 which = ~0, slot;
420
421   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
422     {
423       if (unformat
424           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
425         {
426           hi = vnet_get_hw_interface (vnm, hw_if_index);
427           if (vmxnet3_device_class.index != hi->dev_class_index)
428             {
429               error = clib_error_return (0, "unknown input `%U'",
430                                          format_unformat_error, input);
431               goto done;
432             }
433           vec_add1 (hw_if_indices, hw_if_index);
434         }
435       else if (unformat (input, "desc"))
436         show_descr = 1;
437       else if (hi)
438         {
439           vmxnet3_device_t *vd =
440             vec_elt_at_index (vmxm->devices, hi->dev_instance);
441
442           if (unformat (input, "rx-comp"))
443             {
444               show_one_table = 1;
445               which = VMXNET3_SHOW_RX_COMP;
446               if (unformat (input, "%u", &slot))
447                 {
448                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
449
450                   if (slot >= rxq->size)
451                     {
452                       error = clib_error_return (0,
453                                                  "slot size must be < rx queue "
454                                                  "size %u", rxq->size);
455                       goto done;
456                     }
457                   show_one_slot = 1;
458                 }
459             }
460           else if (unformat (input, "rx-desc-0"))
461             {
462               show_one_table = 1;
463               which = VMXNET3_SHOW_RX_DESC0;
464               if (unformat (input, "%u", &slot))
465                 {
466                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
467
468                   if (slot >= rxq->size)
469                     {
470                       error = clib_error_return (0,
471                                                  "slot size must be < rx queue "
472                                                  "size %u", rxq->size);
473                       goto done;
474                     }
475                   show_one_slot = 1;
476                 }
477             }
478           else if (unformat (input, "rx-desc-1"))
479             {
480               show_one_table = 1;
481               which = VMXNET3_SHOW_RX_DESC1;
482               if (unformat (input, "%u", &slot))
483                 {
484                   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, 0);
485
486                   if (slot >= rxq->size)
487                     {
488                       error = clib_error_return (0,
489                                                  "slot size must be < rx queue "
490                                                  "size %u", rxq->size);
491                       goto done;
492                     }
493                   show_one_slot = 1;
494                 }
495             }
496           else if (unformat (input, "tx-comp"))
497             {
498               show_one_table = 1;
499               which = VMXNET3_SHOW_TX_COMP;
500               if (unformat (input, "%u", &slot))
501                 {
502                   vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, 0);
503
504                   if (slot >= txq->size)
505                     {
506                       error = clib_error_return (0,
507                                                  "slot size must be < tx queue "
508                                                  "size %u", txq->size);
509                       goto done;
510                     }
511                   show_one_slot = 1;
512                 }
513             }
514           else if (unformat (input, "tx-desc"))
515             {
516               show_one_table = 1;
517               which = VMXNET3_SHOW_TX_DESC;
518               if (unformat (input, "%u", &slot))
519                 {
520                   vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, 0);
521
522                   if (slot >= txq->size)
523                     {
524                       error = clib_error_return (0,
525                                                  "slot size must be < tx queue "
526                                                  "size %u", txq->size);
527                       goto done;
528                     }
529                   show_one_slot = 1;
530                 }
531             }
532           else
533             {
534               error = clib_error_return (0, "unknown input `%U'",
535                                          format_unformat_error, input);
536               goto done;
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
547   if (vec_len (hw_if_indices) == 0)
548     {
549       pool_foreach (vd, vmxm->devices,
550                     vec_add1 (hw_if_indices, vd->hw_if_index);
551         );
552     }
553
554   show_vmxnet3 (vm, hw_if_indices, show_descr, show_one_table, which,
555                 show_one_slot, slot);
556
557 done:
558   vec_free (hw_if_indices);
559   return error;
560 }
561
562 /* *INDENT-OFF* */
563 VLIB_CLI_COMMAND (show_vmxnet3_command, static) = {
564   .path = "show vmxnet3",
565   .short_help = "show vmxnet3 [[<interface>] ([desc] | ([rx-comp] | "
566   "[rx-desc-0] | [rx-desc-1] | [tx-comp] | [tx-desc]) [<slot>])]",
567   .function = show_vmxnet3_fn,
568 };
569 /* *INDENT-ON* */
570
571 clib_error_t *
572 vmxnet3_cli_init (vlib_main_t * vm)
573 {
574   vmxnet3_main_t *vmxm = &vmxnet3_main;
575
576   /* initialize binary API */
577   vmxnet3_plugin_api_hookup (vm);
578
579   vmxm->log_default = vlib_log_register_class ("vmxnet3", 0);
580   return 0;
581 }
582
583 VLIB_INIT_FUNCTION (vmxnet3_cli_init);
584
585 static clib_error_t *
586 vmxnet3_config (vlib_main_t * vm, unformat_input_t * input)
587 {
588   vmxnet3_main_t *vmxm = &vmxnet3_main;
589
590   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
591     {
592       if (unformat (input, "lro"))
593         vmxm->lro_configured = 1;
594       else
595         return clib_error_return (0, "unknown input `%U'",
596                                   format_unformat_error, input);
597     }
598
599   return 0;
600 }
601
602 /* vmxnet3 { ... } configuration. */
603 VLIB_CONFIG_FUNCTION (vmxnet3_config, "vmxnet3");
604
605 /*
606  * fd.io coding-style-patch-verification: ON
607  *
608  * Local Variables:
609  * eval: (c-set-style "gnu")
610  * End:
611  */