virtio: add support for tx-queue-size
[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 <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vlib/pci/pci.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/devices/virtio/virtio.h>
24 #include <vnet/devices/virtio/pci.h>
25
26 static clib_error_t *
27 virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
28                               vlib_cli_command_t * cmd)
29 {
30   unformat_input_t _line_input, *line_input = &_line_input;
31   virtio_pci_create_if_args_t args;
32   u64 feature_mask = (u64) ~ (0ULL);
33   u32 buffering_size = 0;
34   u32 txq_size = 0;
35
36   /* Get a line of input. */
37   if (!unformat_user (input, unformat_line_input, line_input))
38     return 0;
39
40   memset (&args, 0, sizeof (args));
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, "feature-mask 0x%llx", &feature_mask))
46         args.features = feature_mask;
47       if (unformat (line_input, "tx-queue-size %u", &txq_size))
48         args.tx_queue_size = txq_size;
49       else if (unformat (line_input, "gso-enabled"))
50         args.gso_enabled = 1;
51       else if (unformat (line_input, "csum-enabled"))
52         args.checksum_offload_enabled = 1;
53       else if (unformat (line_input, "buffering"))
54         {
55           args.virtio_flags |= VIRTIO_FLAG_BUFFERING;
56           if (unformat (line_input, "size %u", &buffering_size))
57             args.buffering_size = buffering_size;
58         }
59       else if (unformat (line_input, "packed"))
60         args.virtio_flags |= VIRTIO_FLAG_PACKED;
61       else if (unformat (line_input, "bind force"))
62         args.bind = VIRTIO_BIND_FORCE;
63       else if (unformat (line_input, "bind"))
64         args.bind = VIRTIO_BIND_DEFAULT;
65       else
66         return clib_error_return (0, "unknown input `%U'",
67                                   format_unformat_error, input);
68     }
69   unformat_free (line_input);
70
71   virtio_pci_create_if (vm, &args);
72
73   return args.error;
74 }
75
76 /* *INDENT-OFF* */
77 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
78   .path = "create interface virtio",
79   .short_help = "create interface virtio <pci-address> "
80                 "[feature-mask <hex-mask>] [tx-queue-size <size>] "
81                 "[gso-enabled] [csum-enabled] "
82                 "[buffering [size <buffering-szie>]] [packed] [bind [force]]",
83   .function = virtio_pci_create_command_fn,
84 };
85 /* *INDENT-ON* */
86
87 static clib_error_t *
88 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
89                               vlib_cli_command_t * cmd)
90 {
91   unformat_input_t _line_input, *line_input = &_line_input;
92   u32 sw_if_index = ~0;
93   vnet_hw_interface_t *hw;
94   virtio_main_t *vim = &virtio_main;
95   virtio_if_t *vif;
96   vnet_main_t *vnm = vnet_get_main ();
97
98   /* Get a line of input. */
99   if (!unformat_user (input, unformat_line_input, line_input))
100     return 0;
101
102   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
105         ;
106       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
107                          vnm, &sw_if_index))
108         ;
109       else
110         return clib_error_return (0, "unknown input `%U'",
111                                   format_unformat_error, input);
112     }
113   unformat_free (line_input);
114
115   if (sw_if_index == ~0)
116     return clib_error_return (0,
117                               "please specify interface name or sw_if_index");
118
119   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
120   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
121     return clib_error_return (0, "not a virtio interface");
122
123   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
124
125   if (virtio_pci_delete_if (vm, vif) < 0)
126     return clib_error_return (0, "not a virtio pci interface");
127
128   return 0;
129 }
130
131 /* *INDENT-OFF* */
132 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
133   .path = "delete interface virtio",
134   .short_help = "delete interface virtio "
135     "{<interface> | sw_if_index <sw_idx>}",
136   .function = virtio_pci_delete_command_fn,
137 };
138 /* *INDENT-ON* */
139
140 static clib_error_t *
141 virtio_pci_enable_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   virtio_main_t *vim = &virtio_main;
148   virtio_if_t *vif;
149   vnet_main_t *vnm = vnet_get_main ();
150   int gso_enabled = 0, checksum_offload_enabled = 0;
151   int offloads_disabled = 0;
152
153   /* Get a line of input. */
154   if (!unformat_user (input, unformat_line_input, line_input))
155     return 0;
156
157   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
158     {
159       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
160         ;
161       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
162                          vnm, &sw_if_index))
163         ;
164       else if (unformat (line_input, "gso-enabled"))
165         gso_enabled = 1;
166       else if (unformat (line_input, "csum-offload-enabled"))
167         checksum_offload_enabled = 1;
168       else if (unformat (line_input, "offloads-disabled"))
169         offloads_disabled = 1;
170       else
171         return clib_error_return (0, "unknown input `%U'",
172                                   format_unformat_error, input);
173     }
174   unformat_free (line_input);
175
176   if (sw_if_index == ~0)
177     return clib_error_return (0,
178                               "please specify interface name or sw_if_index");
179
180   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
181   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
182     return clib_error_return (0, "not a virtio interface");
183
184   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
185
186   if (virtio_pci_enable_disable_offloads
187       (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
188     return clib_error_return (0, "not able to enable/disable offloads");
189
190   return 0;
191 }
192
193 /* *INDENT-OFF* */
194 VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
195   .path = "set virtio pci",
196   .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
197                 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
198   .function = virtio_pci_enable_command_fn,
199 };
200 /* *INDENT-ON* */
201
202 static clib_error_t *
203 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
204                     vlib_cli_command_t * cmd)
205 {
206   virtio_main_t *vim = &virtio_main;
207   vnet_main_t *vnm = &vnet_main;
208   virtio_if_t *vif;
209   clib_error_t *error = 0;
210   u32 hw_if_index, *hw_if_indices = 0;
211   vnet_hw_interface_t *hi;
212   u8 show_descr = 0, show_device_config = 0;
213
214   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
215     {
216       if (unformat
217           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
218         {
219           hi = vnet_get_hw_interface (vnm, hw_if_index);
220           if (virtio_device_class.index != hi->dev_class_index)
221             {
222               error = clib_error_return (0, "unknown input `%U'",
223                                          format_unformat_error, input);
224               goto done;
225             }
226           vec_add1 (hw_if_indices, hw_if_index);
227         }
228       else if (unformat (input, "descriptors") || unformat (input, "desc"))
229         show_descr = 1;
230       else if (unformat (input, "debug-device"))
231         show_device_config = 1;
232       else
233         {
234           error = clib_error_return (0, "unknown input `%U'",
235                                      format_unformat_error, input);
236           goto done;
237         }
238     }
239
240   if (vec_len (hw_if_indices) == 0)
241     {
242       pool_foreach (vif, vim->interfaces)
243         vec_add1 (hw_if_indices, vif->hw_if_index);
244     }
245   else if (show_device_config)
246     {
247       vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
248       if (vif->type == VIRTIO_IF_TYPE_PCI)
249         vif->virtio_pci_func->device_debug_config_space (vm, vif);
250     }
251
252   virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
253
254 done:
255   vec_free (hw_if_indices);
256   return error;
257 }
258
259 /* *INDENT-OFF* */
260 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
261   .path = "show virtio pci",
262   .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
263   .function = show_virtio_pci_fn,
264 };
265 /* *INDENT-ON* */
266
267 clib_error_t *
268 virtio_pci_cli_init (vlib_main_t * vm)
269 {
270   return 0;
271 }
272
273 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
274
275 /*
276  * fd.io coding-style-patch-verification: ON
277  *
278  * Local Variables:
279  * eval: (c-set-style "gnu")
280  * End:
281  */