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 *------------------------------------------------------------------
19 #include <sys/ioctl.h>
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>
32 virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
33 vlib_cli_command_t * cmd)
35 unformat_input_t _line_input, *line_input = &_line_input;
36 virtio_pci_create_if_args_t args;
38 u64 feature_mask = (u64) ~ (0ULL);
40 /* Get a line of input. */
41 if (!unformat_user (input, unformat_line_input, line_input))
44 memset (&args, 0, sizeof (args));
45 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47 if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
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))
53 else if (unformat (line_input, "tx-queue-size %u", &tmp))
56 return clib_error_return (0, "unknown input `%U'",
57 format_unformat_error, input);
59 unformat_free (line_input);
61 virtio_pci_create_if (vm, &args);
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,
76 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
77 vlib_cli_command_t * cmd)
79 unformat_input_t _line_input, *line_input = &_line_input;
81 vnet_hw_interface_t *hw;
82 virtio_main_t *vmxm = &virtio_main;
84 vnet_main_t *vnm = vnet_get_main ();
86 /* Get a line of input. */
87 if (!unformat_user (input, unformat_line_input, line_input))
90 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
92 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
94 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
98 return clib_error_return (0, "unknown input `%U'",
99 format_unformat_error, input);
101 unformat_free (line_input);
103 if (sw_if_index == ~0)
104 return clib_error_return (0,
105 "please specify interface name or sw_if_index");
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");
111 vif = pool_elt_at_index (vmxm->interfaces, hw->dev_instance);
113 if (virtio_pci_delete_if (vm, vif) < 0)
114 return clib_error_return (0, "not a virtio pci interface");
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,
128 static clib_error_t *
129 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
130 vlib_cli_command_t * cmd)
132 virtio_main_t *vmxm = &virtio_main;
133 vnet_main_t *vnm = &vnet_main;
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;
140 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
143 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
145 hi = vnet_get_hw_interface (vnm, hw_if_index);
146 if (virtio_device_class.index != hi->dev_class_index)
148 error = clib_error_return (0, "unknown input `%U'",
149 format_unformat_error, input);
152 vec_add1 (hw_if_indices, hw_if_index);
154 else if (unformat (input, "descriptors") || unformat (input, "desc"))
156 else if (unformat (input, "debug-device"))
157 show_device_config = 1;
160 error = clib_error_return (0, "unknown input `%U'",
161 format_unformat_error, input);
166 if (vec_len (hw_if_indices) == 0)
168 pool_foreach (vif, vmxm->interfaces,
169 vec_add1 (hw_if_indices, vif->hw_if_index);
172 else if (show_device_config)
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);
179 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
182 vec_free (hw_if_indices);
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,
195 virtio_pci_cli_init (vlib_main_t * vm)
200 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
203 * fd.io coding-style-patch-verification: ON
206 * eval: (c-set-style "gnu")