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