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);
34 /* Get a line of input. */
35 if (!unformat_user (input, unformat_line_input, line_input))
38 memset (&args, 0, sizeof (args));
39 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
41 if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
43 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
44 args.features = feature_mask;
45 else if (unformat (line_input, "gso-enabled"))
47 else if (unformat (line_input, "csum-enabled"))
48 args.checksum_offload_enabled = 1;
50 return clib_error_return (0, "unknown input `%U'",
51 format_unformat_error, input);
53 unformat_free (line_input);
55 virtio_pci_create_if (vm, &args);
61 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
62 .path = "create interface virtio",
63 .short_help = "create interface virtio <pci-address> "
64 "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled]",
65 .function = virtio_pci_create_command_fn,
70 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
71 vlib_cli_command_t * cmd)
73 unformat_input_t _line_input, *line_input = &_line_input;
75 vnet_hw_interface_t *hw;
76 virtio_main_t *vim = &virtio_main;
78 vnet_main_t *vnm = vnet_get_main ();
80 /* Get a line of input. */
81 if (!unformat_user (input, unformat_line_input, line_input))
84 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
86 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
88 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
92 return clib_error_return (0, "unknown input `%U'",
93 format_unformat_error, input);
95 unformat_free (line_input);
97 if (sw_if_index == ~0)
98 return clib_error_return (0,
99 "please specify interface name or sw_if_index");
101 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
102 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
103 return clib_error_return (0, "not a virtio interface");
105 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
107 if (virtio_pci_delete_if (vm, vif) < 0)
108 return clib_error_return (0, "not a virtio pci interface");
114 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
115 .path = "delete interface virtio",
116 .short_help = "delete interface virtio "
117 "{<interface> | sw_if_index <sw_idx>}",
118 .function = virtio_pci_delete_command_fn,
122 static clib_error_t *
123 virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input,
124 vlib_cli_command_t * cmd)
126 unformat_input_t _line_input, *line_input = &_line_input;
127 u32 sw_if_index = ~0;
128 vnet_hw_interface_t *hw;
129 virtio_main_t *vim = &virtio_main;
131 vnet_main_t *vnm = vnet_get_main ();
132 int gso_enabled = 0, checksum_offload_enabled = 0;
133 int offloads_disabled = 0;
135 /* Get a line of input. */
136 if (!unformat_user (input, unformat_line_input, line_input))
139 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
143 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
146 else if (unformat (line_input, "gso-enabled"))
148 else if (unformat (line_input, "csum-offload-enabled"))
149 checksum_offload_enabled = 1;
150 else if (unformat (line_input, "offloads-disabled"))
151 offloads_disabled = 1;
153 return clib_error_return (0, "unknown input `%U'",
154 format_unformat_error, input);
156 unformat_free (line_input);
158 if (sw_if_index == ~0)
159 return clib_error_return (0,
160 "please specify interface name or sw_if_index");
162 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
163 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
164 return clib_error_return (0, "not a virtio interface");
166 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
168 if (virtio_pci_enable_disable_offloads
169 (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
170 return clib_error_return (0, "not able to enable/disable offloads");
176 VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
177 .path = "set virtio pci",
178 .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
179 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
180 .function = virtio_pci_enable_command_fn,
184 static clib_error_t *
185 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
186 vlib_cli_command_t * cmd)
188 virtio_main_t *vim = &virtio_main;
189 vnet_main_t *vnm = &vnet_main;
191 clib_error_t *error = 0;
192 u32 hw_if_index, *hw_if_indices = 0;
193 vnet_hw_interface_t *hi;
194 u8 show_descr = 0, show_device_config = 0;
196 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
199 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
201 hi = vnet_get_hw_interface (vnm, hw_if_index);
202 if (virtio_device_class.index != hi->dev_class_index)
204 error = clib_error_return (0, "unknown input `%U'",
205 format_unformat_error, input);
208 vec_add1 (hw_if_indices, hw_if_index);
210 else if (unformat (input, "descriptors") || unformat (input, "desc"))
212 else if (unformat (input, "debug-device"))
213 show_device_config = 1;
216 error = clib_error_return (0, "unknown input `%U'",
217 format_unformat_error, input);
222 if (vec_len (hw_if_indices) == 0)
224 pool_foreach (vif, vim->interfaces,
225 vec_add1 (hw_if_indices, vif->hw_if_index);
228 else if (show_device_config)
230 vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
231 if (vif->type == VIRTIO_IF_TYPE_PCI)
232 debug_device_config_space (vm, vif);
235 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
238 vec_free (hw_if_indices);
243 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
244 .path = "show virtio pci",
245 .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
246 .function = show_virtio_pci_fn,
251 virtio_pci_cli_init (vlib_main_t * vm)
256 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
259 * fd.io coding-style-patch-verification: ON
262 * eval: (c-set-style "gnu")