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