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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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>
27 virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
28 vlib_cli_command_t * cmd)
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;
35 /* Get a line of input. */
36 if (!unformat_user (input, unformat_line_input, line_input))
39 memset (&args, 0, sizeof (args));
40 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
42 if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
44 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
45 args.features = feature_mask;
46 else if (unformat (line_input, "gso-enabled"))
48 else if (unformat (line_input, "csum-enabled"))
49 args.checksum_offload_enabled = 1;
50 else if (unformat (line_input, "buffering"))
52 args.virtio_flags |= VIRTIO_FLAG_BUFFERING;
53 if (unformat (line_input, "size %u", &buffering_size))
54 args.buffering_size = buffering_size;
56 else if (unformat (line_input, "packed"))
57 args.virtio_flags |= VIRTIO_FLAG_PACKED;
58 else if (unformat (line_input, "bind force"))
59 args.bind = VIRTIO_BIND_FORCE;
60 else if (unformat (line_input, "bind"))
61 args.bind = VIRTIO_BIND_DEFAULT;
63 return clib_error_return (0, "unknown input `%U'",
64 format_unformat_error, input);
66 unformat_free (line_input);
68 virtio_pci_create_if (vm, &args);
74 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
75 .path = "create interface virtio",
76 .short_help = "create interface virtio <pci-address> "
77 "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled] "
78 "[buffering [size <buffering-szie>]] [packed] [bind [force]]",
79 .function = virtio_pci_create_command_fn,
84 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
85 vlib_cli_command_t * cmd)
87 unformat_input_t _line_input, *line_input = &_line_input;
89 vnet_hw_interface_t *hw;
90 virtio_main_t *vim = &virtio_main;
92 vnet_main_t *vnm = vnet_get_main ();
94 /* Get a line of input. */
95 if (!unformat_user (input, unformat_line_input, line_input))
98 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
100 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
102 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
106 return clib_error_return (0, "unknown input `%U'",
107 format_unformat_error, input);
109 unformat_free (line_input);
111 if (sw_if_index == ~0)
112 return clib_error_return (0,
113 "please specify interface name or sw_if_index");
115 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
116 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
117 return clib_error_return (0, "not a virtio interface");
119 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
121 if (virtio_pci_delete_if (vm, vif) < 0)
122 return clib_error_return (0, "not a virtio pci interface");
128 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
129 .path = "delete interface virtio",
130 .short_help = "delete interface virtio "
131 "{<interface> | sw_if_index <sw_idx>}",
132 .function = virtio_pci_delete_command_fn,
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)
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;
145 vnet_main_t *vnm = vnet_get_main ();
146 int gso_enabled = 0, checksum_offload_enabled = 0;
147 int offloads_disabled = 0;
149 /* Get a line of input. */
150 if (!unformat_user (input, unformat_line_input, line_input))
153 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
155 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
157 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
160 else if (unformat (line_input, "gso-enabled"))
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;
167 return clib_error_return (0, "unknown input `%U'",
168 format_unformat_error, input);
170 unformat_free (line_input);
172 if (sw_if_index == ~0)
173 return clib_error_return (0,
174 "please specify interface name or sw_if_index");
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");
180 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
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");
190 VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
191 .path = "set virtio pci",
192 .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
193 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
194 .function = virtio_pci_enable_command_fn,
198 static clib_error_t *
199 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
200 vlib_cli_command_t * cmd)
202 virtio_main_t *vim = &virtio_main;
203 vnet_main_t *vnm = &vnet_main;
205 clib_error_t *error = 0;
206 u32 hw_if_index, *hw_if_indices = 0;
207 vnet_hw_interface_t *hi;
208 u8 show_descr = 0, show_device_config = 0;
210 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
213 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
215 hi = vnet_get_hw_interface (vnm, hw_if_index);
216 if (virtio_device_class.index != hi->dev_class_index)
218 error = clib_error_return (0, "unknown input `%U'",
219 format_unformat_error, input);
222 vec_add1 (hw_if_indices, hw_if_index);
224 else if (unformat (input, "descriptors") || unformat (input, "desc"))
226 else if (unformat (input, "debug-device"))
227 show_device_config = 1;
230 error = clib_error_return (0, "unknown input `%U'",
231 format_unformat_error, input);
236 if (vec_len (hw_if_indices) == 0)
238 pool_foreach (vif, vim->interfaces)
239 vec_add1 (hw_if_indices, vif->hw_if_index);
241 else if (show_device_config)
243 vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
244 if (vif->type == VIRTIO_IF_TYPE_PCI)
245 vif->virtio_pci_func->device_debug_config_space (vm, vif);
248 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
251 vec_free (hw_if_indices);
256 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
257 .path = "show virtio pci",
258 .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
259 .function = show_virtio_pci_fn,
264 virtio_pci_cli_init (vlib_main_t * vm)
269 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
272 * fd.io coding-style-patch-verification: ON
275 * eval: (c-set-style "gnu")