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