virtio: Native virtio driver
[vpp.git] / src / vnet / devices / virtio / 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 #include <vnet/ip/ip4_packet.h>
27 #include <vnet/ip/ip6_packet.h>
28 #include <vnet/devices/virtio/virtio.h>
29 #include <vnet/devices/virtio/pci.h>
30
31 static clib_error_t *
32 virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
33                               vlib_cli_command_t * cmd)
34 {
35   unformat_input_t _line_input, *line_input = &_line_input;
36   virtio_pci_create_if_args_t args;
37   u32 tmp;
38   u64 feature_mask = (u64) ~ (0ULL);
39
40   /* Get a line of input. */
41   if (!unformat_user (input, unformat_line_input, line_input))
42     return 0;
43
44   memset (&args, 0, sizeof (args));
45   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46     {
47       if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
48         ;
49       else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
50         args.features = feature_mask;
51       else if (unformat (line_input, "rx-queue-size %u", &tmp))
52         args.rxq_size = tmp;
53       else if (unformat (line_input, "tx-queue-size %u", &tmp))
54         args.txq_size = tmp;
55       else
56         return clib_error_return (0, "unknown input `%U'",
57                                   format_unformat_error, input);
58     }
59   unformat_free (line_input);
60
61   virtio_pci_create_if (vm, &args);
62
63   return args.error;
64 }
65
66 /* *INDENT-OFF* */
67 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
68   .path = "create interface virtio",
69   .short_help = "create interface virtio <pci-address>"
70                 "[feature-mask <hex-mask>] [rx-queue-size <size>] [tx-queue-size <size>]",
71   .function = virtio_pci_create_command_fn,
72 };
73 /* *INDENT-ON* */
74
75 static clib_error_t *
76 virtio_pci_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   virtio_main_t *vmxm = &virtio_main;
83   virtio_if_t *vif;
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 || virtio_device_class.index != hw->dev_class_index)
109     return clib_error_return (0, "not a virtio interface");
110
111   vif = pool_elt_at_index (vmxm->interfaces, hw->dev_instance);
112
113   if (virtio_pci_delete_if (vm, vif) < 0)
114     return clib_error_return (0, "not a virtio pci interface");
115
116   return 0;
117 }
118
119 /* *INDENT-OFF* */
120 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
121   .path = "delete interface virtio",
122   .short_help = "delete interface virtio"
123     "{<interface> | sw_if_index <sw_idx>}",
124   .function = virtio_pci_delete_command_fn,
125 };
126 /* *INDENT-ON* */
127
128 static clib_error_t *
129 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
130                     vlib_cli_command_t * cmd)
131 {
132   virtio_main_t *vmxm = &virtio_main;
133   vnet_main_t *vnm = &vnet_main;
134   virtio_if_t *vif;
135   clib_error_t *error = 0;
136   u32 hw_if_index, *hw_if_indices = 0;
137   vnet_hw_interface_t *hi;
138   u8 show_descr = 0, show_device_config = 0;
139
140   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
141     {
142       if (unformat
143           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
144         {
145           hi = vnet_get_hw_interface (vnm, hw_if_index);
146           if (virtio_device_class.index != hi->dev_class_index)
147             {
148               error = clib_error_return (0, "unknown input `%U'",
149                                          format_unformat_error, input);
150               goto done;
151             }
152           vec_add1 (hw_if_indices, hw_if_index);
153         }
154       else if (unformat (input, "descriptors") || unformat (input, "desc"))
155         show_descr = 1;
156       else if (unformat (input, "debug-device"))
157         show_device_config = 1;
158       else
159         {
160           error = clib_error_return (0, "unknown input `%U'",
161                                      format_unformat_error, input);
162           goto done;
163         }
164     }
165
166   if (vec_len (hw_if_indices) == 0)
167     {
168       pool_foreach (vif, vmxm->interfaces,
169                     vec_add1 (hw_if_indices, vif->hw_if_index);
170         );
171     }
172   else if (show_device_config)
173     {
174       vif = pool_elt_at_index (vmxm->interfaces, hi->dev_instance);
175       if (vif->type == VIRTIO_IF_TYPE_PCI)
176         debug_device_config_space (vm, vif);
177     }
178
179   virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
180
181 done:
182   vec_free (hw_if_indices);
183   return error;
184 }
185
186 /* *INDENT-OFF* */
187 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
188   .path = "show virtio pci",
189   .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
190   .function = show_virtio_pci_fn,
191 };
192 /* *INDENT-ON* */
193
194 clib_error_t *
195 virtio_pci_cli_init (vlib_main_t * vm)
196 {
197   return 0;
198 }
199
200 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
201
202 /*
203  * fd.io coding-style-patch-verification: ON
204  *
205  * Local Variables:
206  * eval: (c-set-style "gnu")
207  * End:
208  */