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;
46 return clib_error_return (0, "unknown input `%U'",
47 format_unformat_error, input);
49 unformat_free (line_input);
51 virtio_pci_create_if (vm, &args);
57 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
58 .path = "create interface virtio",
59 .short_help = "create interface virtio <pci-address> "
60 "[feature-mask <hex-mask>]",
61 .function = virtio_pci_create_command_fn,
66 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
67 vlib_cli_command_t * cmd)
69 unformat_input_t _line_input, *line_input = &_line_input;
71 vnet_hw_interface_t *hw;
72 virtio_main_t *vim = &virtio_main;
74 vnet_main_t *vnm = vnet_get_main ();
76 /* Get a line of input. */
77 if (!unformat_user (input, unformat_line_input, line_input))
80 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
82 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
84 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
88 return clib_error_return (0, "unknown input `%U'",
89 format_unformat_error, input);
91 unformat_free (line_input);
93 if (sw_if_index == ~0)
94 return clib_error_return (0,
95 "please specify interface name or sw_if_index");
97 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
98 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
99 return clib_error_return (0, "not a virtio interface");
101 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
103 if (virtio_pci_delete_if (vm, vif) < 0)
104 return clib_error_return (0, "not a virtio pci interface");
110 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
111 .path = "delete interface virtio",
112 .short_help = "delete interface virtio "
113 "{<interface> | sw_if_index <sw_idx>}",
114 .function = virtio_pci_delete_command_fn,
118 static clib_error_t *
119 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
120 vlib_cli_command_t * cmd)
122 virtio_main_t *vim = &virtio_main;
123 vnet_main_t *vnm = &vnet_main;
125 clib_error_t *error = 0;
126 u32 hw_if_index, *hw_if_indices = 0;
127 vnet_hw_interface_t *hi;
128 u8 show_descr = 0, show_device_config = 0;
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
133 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
135 hi = vnet_get_hw_interface (vnm, hw_if_index);
136 if (virtio_device_class.index != hi->dev_class_index)
138 error = clib_error_return (0, "unknown input `%U'",
139 format_unformat_error, input);
142 vec_add1 (hw_if_indices, hw_if_index);
144 else if (unformat (input, "descriptors") || unformat (input, "desc"))
146 else if (unformat (input, "debug-device"))
147 show_device_config = 1;
150 error = clib_error_return (0, "unknown input `%U'",
151 format_unformat_error, input);
156 if (vec_len (hw_if_indices) == 0)
158 pool_foreach (vif, vim->interfaces,
159 vec_add1 (hw_if_indices, vif->hw_if_index);
162 else if (show_device_config)
164 vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
165 if (vif->type == VIRTIO_IF_TYPE_PCI)
166 debug_device_config_space (vm, vif);
169 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
172 vec_free (hw_if_indices);
177 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
178 .path = "show virtio pci",
179 .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
180 .function = show_virtio_pci_fn,
185 virtio_pci_cli_init (vlib_main_t * vm)
187 virtio_main_t *vim = &virtio_main;
188 vim->log_default = vlib_log_register_class ("virtio-pci", 0);
192 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
195 * fd.io coding-style-patch-verification: ON
198 * eval: (c-set-style "gnu")