builtinurl: mark api as deprecated
[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       else 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 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
77   .path = "create interface virtio",
78   .short_help = "create interface virtio <pci-address> "
79                 "[feature-mask <hex-mask>] [tx-queue-size <size>] "
80                 "[gso-enabled] [csum-enabled] "
81                 "[buffering [size <buffering-szie>]] [packed] [bind [force]]",
82   .function = virtio_pci_create_command_fn,
83 };
84
85 static clib_error_t *
86 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
87                               vlib_cli_command_t * cmd)
88 {
89   unformat_input_t _line_input, *line_input = &_line_input;
90   u32 sw_if_index = ~0;
91   vnet_hw_interface_t *hw;
92   virtio_main_t *vim = &virtio_main;
93   virtio_if_t *vif;
94   vnet_main_t *vnm = vnet_get_main ();
95
96   /* Get a line of input. */
97   if (!unformat_user (input, unformat_line_input, line_input))
98     return 0;
99
100   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
101     {
102       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
103         ;
104       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
105                          vnm, &sw_if_index))
106         ;
107       else
108         return clib_error_return (0, "unknown input `%U'",
109                                   format_unformat_error, input);
110     }
111   unformat_free (line_input);
112
113   if (sw_if_index == ~0)
114     return clib_error_return (0,
115                               "please specify interface name or sw_if_index");
116
117   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
118   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
119     return clib_error_return (0, "not a virtio interface");
120
121   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
122
123   if (virtio_pci_delete_if (vm, vif) < 0)
124     return clib_error_return (0, "not a virtio pci interface");
125
126   return 0;
127 }
128
129 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
130   .path = "delete interface virtio",
131   .short_help = "delete interface virtio "
132     "{<interface> | sw_if_index <sw_idx>}",
133   .function = virtio_pci_delete_command_fn,
134 };
135
136 static clib_error_t *
137 virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input,
138                               vlib_cli_command_t * cmd)
139 {
140   unformat_input_t _line_input, *line_input = &_line_input;
141   u32 sw_if_index = ~0;
142   vnet_hw_interface_t *hw;
143   virtio_main_t *vim = &virtio_main;
144   virtio_if_t *vif;
145   vnet_main_t *vnm = vnet_get_main ();
146   int gso_enabled = 0, checksum_offload_enabled = 0;
147   int offloads_disabled = 0;
148
149   /* Get a line of input. */
150   if (!unformat_user (input, unformat_line_input, line_input))
151     return 0;
152
153   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
154     {
155       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
156         ;
157       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
158                          vnm, &sw_if_index))
159         ;
160       else if (unformat (line_input, "gso-enabled"))
161         gso_enabled = 1;
162       else if (unformat (line_input, "csum-offload-enabled"))
163         checksum_offload_enabled = 1;
164       else if (unformat (line_input, "offloads-disabled"))
165         offloads_disabled = 1;
166       else
167         return clib_error_return (0, "unknown input `%U'",
168                                   format_unformat_error, input);
169     }
170   unformat_free (line_input);
171
172   if (sw_if_index == ~0)
173     return clib_error_return (0,
174                               "please specify interface name or sw_if_index");
175
176   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
177   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
178     return clib_error_return (0, "not a virtio interface");
179
180   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
181
182   if (virtio_pci_enable_disable_offloads
183       (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
184     return clib_error_return (0, "not able to enable/disable offloads");
185
186   return 0;
187 }
188
189 VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
190   .path = "set virtio pci",
191   .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
192                 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
193   .function = virtio_pci_enable_command_fn,
194 };
195
196 static clib_error_t *
197 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
198                     vlib_cli_command_t * cmd)
199 {
200   virtio_main_t *vim = &virtio_main;
201   vnet_main_t *vnm = &vnet_main;
202   virtio_if_t *vif;
203   clib_error_t *error = 0;
204   u32 hw_if_index, *hw_if_indices = 0;
205   vnet_hw_interface_t *hi;
206   u8 show_descr = 0, show_device_config = 0;
207
208   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
209     {
210       if (unformat
211           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
212         {
213           hi = vnet_get_hw_interface (vnm, hw_if_index);
214           if (virtio_device_class.index != hi->dev_class_index)
215             {
216               error = clib_error_return (0, "unknown input `%U'",
217                                          format_unformat_error, input);
218               goto done;
219             }
220           vec_add1 (hw_if_indices, hw_if_index);
221         }
222       else if (unformat (input, "descriptors") || unformat (input, "desc"))
223         show_descr = 1;
224       else if (unformat (input, "debug-device"))
225         show_device_config = 1;
226       else
227         {
228           error = clib_error_return (0, "unknown input `%U'",
229                                      format_unformat_error, input);
230           goto done;
231         }
232     }
233
234   if (vec_len (hw_if_indices) == 0)
235     {
236       pool_foreach (vif, vim->interfaces)
237         vec_add1 (hw_if_indices, vif->hw_if_index);
238     }
239   else if (show_device_config)
240     {
241       vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
242       if (vif->type == VIRTIO_IF_TYPE_PCI)
243         vif->virtio_pci_func->device_debug_config_space (vm, vif);
244     }
245
246   virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
247
248 done:
249   vec_free (hw_if_indices);
250   return error;
251 }
252
253 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
254   .path = "show virtio pci",
255   .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
256   .function = show_virtio_pci_fn,
257 };
258
259 clib_error_t *
260 virtio_pci_cli_init (vlib_main_t * vm)
261 {
262   return 0;
263 }
264
265 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
266
267 /*
268  * fd.io coding-style-patch-verification: ON
269  *
270  * Local Variables:
271  * eval: (c-set-style "gnu")
272  * End:
273  */