vmxnet3 device driver
[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 = { 0 };
35   u32 tmp;
36
37   /* Get a line of input. */
38   if (!unformat_user (input, unformat_line_input, line_input))
39     return 0;
40
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", &tmp))
48         args.rxq_size = tmp;
49       else if (unformat (line_input, "tx-queue-size %u", &tmp))
50         args.txq_size = tmp;
51       else
52         return clib_error_return (0, "unknown input `%U'",
53                                   format_unformat_error, input);
54     }
55   unformat_free (line_input);
56
57
58   vmxnet3_create_if (vm, &args);
59
60   return args.error;
61 }
62
63 /* *INDENT-OFF* */
64 VLIB_CLI_COMMAND (vmxnet3_create_command, static) = {
65   .path = "create interface vmxnet3",
66   .short_help = "create interface vmxnet3 <pci-address>"
67                 "[rx-queue-size <size>] [tx-queue-size <size>]",
68   .function = vmxnet3_create_command_fn,
69 };
70 /* *INDENT-ON* */
71
72 static clib_error_t *
73 vmxnet3_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
74                            vlib_cli_command_t * cmd)
75 {
76   unformat_input_t _line_input, *line_input = &_line_input;
77   u32 sw_if_index = ~0;
78   vnet_hw_interface_t *hw;
79   vmxnet3_main_t *vmxm = &vmxnet3_main;
80   vmxnet3_device_t *vd;
81   vnet_main_t *vnm = vnet_get_main ();
82
83   /* Get a line of input. */
84   if (!unformat_user (input, unformat_line_input, line_input))
85     return 0;
86
87   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
88     {
89       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
90         ;
91       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
92                          vnm, &sw_if_index))
93         ;
94       else
95         return clib_error_return (0, "unknown input `%U'",
96                                   format_unformat_error, input);
97     }
98   unformat_free (line_input);
99
100   if (sw_if_index == ~0)
101     return clib_error_return (0,
102                               "please specify interface name or sw_if_index");
103
104   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
105   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
106     return clib_error_return (0, "not a vmxnet3 interface");
107
108   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
109
110   vmxnet3_delete_if (vm, vd);
111
112   return 0;
113 }
114
115 /* *INDENT-OFF* */
116 VLIB_CLI_COMMAND (vmxnet3_delete_command, static) = {
117   .path = "delete interface vmxnet3",
118   .short_help = "delete interface vmxnet3 "
119     "{<interface> | sw_if_index <sw_idx>}",
120   .function = vmxnet3_delete_command_fn,
121 };
122 /* *INDENT-ON* */
123
124 static clib_error_t *
125 vmxnet3_test_command_fn (vlib_main_t * vm, unformat_input_t * input,
126                          vlib_cli_command_t * cmd)
127 {
128   unformat_input_t _line_input, *line_input = &_line_input;
129   u32 sw_if_index = ~0;
130   vnet_hw_interface_t *hw;
131   vmxnet3_main_t *vmxm = &vmxnet3_main;
132   vmxnet3_device_t *vd;
133   vnet_main_t *vnm = vnet_get_main ();
134   int enable_elog = 0, disable_elog = 0;
135
136   /* Get a line of input. */
137   if (!unformat_user (input, unformat_line_input, line_input))
138     return 0;
139
140   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141     {
142       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
143         ;
144       else if (unformat (line_input, "elog-on"))
145         enable_elog = 1;
146       else if (unformat (line_input, "elog-off"))
147         disable_elog = 1;
148       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
149                          vnm, &sw_if_index))
150         ;
151       else
152         return clib_error_return (0, "unknown input `%U'",
153                                   format_unformat_error, input);
154     }
155   unformat_free (line_input);
156
157   if (sw_if_index == ~0)
158     return clib_error_return (0,
159                               "please specify interface name or sw_if_index");
160
161   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
162   if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
163     return clib_error_return (0, "not a vmxnet3 interface");
164
165   vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
166
167   if (enable_elog)
168     vd->flags |= VMXNET3_DEVICE_F_ELOG;
169
170   if (disable_elog)
171     vd->flags &= ~VMXNET3_DEVICE_F_ELOG;
172
173   return 0;
174 }
175
176 /* *INDENT-OFF* */
177 VLIB_CLI_COMMAND (vmxnet3_test_command, static) = {
178   .path = "test vmxnet3",
179   .short_help = "test vmxnet3 <interface> | sw_if_index <sw_idx> [irq] "
180     "[elog-on] [elog-off]",
181   .function = vmxnet3_test_command_fn,
182 };
183 /* *INDENT-ON* */
184
185 static void
186 show_vmxnet3 (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr)
187 {
188   u32 i, desc_idx;
189   vmxnet3_device_t *vd;
190   vnet_main_t *vnm = &vnet_main;
191   vmxnet3_main_t *vmxm = &vmxnet3_main;
192   vnet_hw_interface_t *hi;
193   vmxnet3_rxq_t *rxq;
194   vmxnet3_rx_desc *rxd;
195   vmxnet3_rx_comp *rx_comp;
196   vmxnet3_txq_t *txq;
197   vmxnet3_tx_desc *txd;
198   vmxnet3_tx_comp *tx_comp;
199   u16 qid;
200
201   if (!hw_if_indices)
202     return;
203
204   for (i = 0; i < vec_len (hw_if_indices); i++)
205     {
206       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
207       vd = vec_elt_at_index (vmxm->devices, hi->dev_instance);
208       vlib_cli_output (vm, "Interface: %s (ifindex %d)",
209                        hi->name, hw_if_indices[i]);
210       vlib_cli_output (vm, "  Version: %u", vd->version);
211       vlib_cli_output (vm, "  PCI Address: %U", format_vlib_pci_addr,
212                        &vd->pci_addr);
213       vlib_cli_output (vm, "  Mac Address: %U", format_ethernet_address,
214                        vd->mac_addr);
215       vlib_cli_output (vm, "  hw if index: %u", vd->hw_if_index);
216       vlib_cli_output (vm, "  Device instance: %u", vd->dev_instance);
217       vlib_cli_output (vm, "  Number of interrupts: %u", vd->num_intrs);
218
219       vec_foreach_index (qid, vd->rxqs)
220       {
221         rxq = vec_elt_at_index (vd->rxqs, qid);
222         u16 rid;
223
224         vlib_cli_output (vm, "  Queue %u (RX)", qid);
225         vlib_cli_output (vm, "    RX completion next index %u",
226                          rxq->rx_comp_ring.next);
227         for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
228           {
229             vmxnet3_rx_ring *ring = &rxq->rx_ring[rid];
230
231             vlib_cli_output (vm,
232                              "    ring %u size %u fill %u "
233                              "consume %u produce %u", rid,
234                              rxq->size, ring->fill, ring->consume,
235                              ring->produce);
236             if (show_descr)
237               {
238                 vlib_cli_output (vm, "RX descriptors table");
239                 vlib_cli_output (vm, "  %5s  %18s  %10s",
240                                  "slot", "address", "flags");
241                 for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
242                   {
243                     rxd = &rxq->rx_desc[rid][desc_idx];
244                     vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x",
245                                      desc_idx, rxd->address, rxd->flags);
246                   }
247                 vlib_cli_output (vm, "RX completion descriptors table");
248                 vlib_cli_output (vm, "  %5s  %10s  %10s  %10s  %10s",
249                                  "slot", "index", "rss", "len", "flags");
250                 for (desc_idx = 0; desc_idx < rxq->size; desc_idx++)
251                   {
252                     rx_comp = &rxq->rx_comp[desc_idx];
253                     vlib_cli_output (vm, "  %5u  0x%08x  %10u  %10u  0x%08x",
254                                      desc_idx, rx_comp->index, rx_comp->rss,
255                                      rx_comp->len, rx_comp->flags);
256                   }
257               }
258           }
259       }
260
261       vec_foreach_index (qid, vd->rxqs)
262       {
263         txq = vec_elt_at_index (vd->txqs, 0);
264         vlib_cli_output (vm, "  Queue %u (TX)", qid);
265         vlib_cli_output (vm, "    TX completion next index %u",
266                          txq->tx_comp_ring.next);
267         vlib_cli_output (vm, "    size %u consume %u produce %u",
268                          txq->size, txq->tx_ring.consume,
269                          txq->tx_ring.produce);
270         if (show_descr)
271           {
272             vlib_cli_output (vm, "TX descriptors table");
273             vlib_cli_output (vm, "  %5s  %18s  %10s  %10s",
274                              "slot", "address", "flags0", "flags1");
275             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
276               {
277                 txd = &txq->tx_desc[desc_idx];
278                 vlib_cli_output (vm, "  %5u  0x%016llx  0x%08x  0x%08x",
279                                  desc_idx, txd->address, txd->flags[0],
280                                  txd->flags[1]);
281               }
282             vlib_cli_output (vm, "TX completion descriptors table");
283             vlib_cli_output (vm, "  %5s  %10s  %10s",
284                              "slot", "index", "flags");
285             for (desc_idx = 0; desc_idx < txq->size; desc_idx++)
286               {
287                 tx_comp = &txq->tx_comp[desc_idx];
288                 vlib_cli_output (vm, "  %5u  0x%08x  0x%08x",
289                                  desc_idx, tx_comp->index, tx_comp->flags);
290               }
291           }
292       }
293     }
294 }
295
296 static clib_error_t *
297 show_vmxnet3_fn (vlib_main_t * vm, unformat_input_t * input,
298                  vlib_cli_command_t * cmd)
299 {
300   vmxnet3_main_t *vmxm = &vmxnet3_main;
301   vnet_main_t *vnm = &vnet_main;
302   vmxnet3_device_t *vd;
303   clib_error_t *error = 0;
304   u32 hw_if_index, *hw_if_indices = 0;
305   vnet_hw_interface_t *hi;
306   u8 show_descr = 0;
307
308   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309     {
310       if (unformat
311           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
312         {
313           hi = vnet_get_hw_interface (vnm, hw_if_index);
314           if (vmxnet3_device_class.index != hi->dev_class_index)
315             {
316               error = clib_error_return (0, "unknown input `%U'",
317                                          format_unformat_error, input);
318               goto done;
319             }
320           vec_add1 (hw_if_indices, hw_if_index);
321         }
322       else if (unformat (input, "descriptors") || unformat (input, "desc"))
323         show_descr = 1;
324       else
325         {
326           error = clib_error_return (0, "unknown input `%U'",
327                                      format_unformat_error, input);
328           goto done;
329         }
330     }
331
332   if (vec_len (hw_if_indices) == 0)
333     {
334       pool_foreach (vd, vmxm->devices,
335                     vec_add1 (hw_if_indices, vd->hw_if_index);
336         );
337     }
338
339   show_vmxnet3 (vm, hw_if_indices, show_descr);
340
341 done:
342   vec_free (hw_if_indices);
343   return error;
344 }
345
346 /* *INDENT-OFF* */
347 VLIB_CLI_COMMAND (show_vmxnet3_command, static) = {
348   .path = "show vmxnet3",
349   .short_help = "show vmxnet3 [<interface>]",
350   .function = show_vmxnet3_fn,
351 };
352 /* *INDENT-ON* */
353
354 clib_error_t *
355 vmxnet3_cli_init (vlib_main_t * vm)
356 {
357   /* initialize binary API */
358   vmxnet3_plugin_api_hookup (vm);
359
360   return 0;
361 }
362
363 VLIB_INIT_FUNCTION (vmxnet3_cli_init);
364
365 /*
366  * fd.io coding-style-patch-verification: ON
367  *
368  * Local Variables:
369  * eval: (c-set-style "gnu")
370  * End:
371  */