blob: 8a9c97be8cb3de37d29993c1e22675bc32838867 [file] [log] [blame]
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001/*
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 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020017#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
26static clib_error_t *
27virtio_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;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020032 u64 feature_mask = (u64) ~ (0ULL);
Mohsin Kazmie347acb2020-09-28 10:26:33 +000033 u32 buffering_size = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020034
35 /* Get a line of input. */
36 if (!unformat_user (input, unformat_line_input, line_input))
37 return 0;
38
39 memset (&args, 0, sizeof (args));
40 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
41 {
42 if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
43 ;
44 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
45 args.features = feature_mask;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +020046 else if (unformat (line_input, "gso-enabled"))
47 args.gso_enabled = 1;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +000048 else if (unformat (line_input, "csum-enabled"))
49 args.checksum_offload_enabled = 1;
Mohsin Kazmie347acb2020-09-28 10:26:33 +000050 else if (unformat (line_input, "buffering"))
51 {
52 args.virtio_flags = VIRTIO_FLAG_BUFFERING;
53 if (unformat (line_input, "size %u", &buffering_size))
54 args.buffering_size = buffering_size;
55 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020056 else
57 return clib_error_return (0, "unknown input `%U'",
58 format_unformat_error, input);
59 }
60 unformat_free (line_input);
61
62 virtio_pci_create_if (vm, &args);
63
64 return args.error;
65}
66
67/* *INDENT-OFF* */
68VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
69 .path = "create interface virtio",
Mohsin Kazmiddd21832019-01-22 13:05:00 +000070 .short_help = "create interface virtio <pci-address> "
Mohsin Kazmie347acb2020-09-28 10:26:33 +000071 "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled] "
72 "[buffering [size <buffering-szie>]]",
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020073 .function = virtio_pci_create_command_fn,
74};
75/* *INDENT-ON* */
76
77static clib_error_t *
78virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
79 vlib_cli_command_t * cmd)
80{
81 unformat_input_t _line_input, *line_input = &_line_input;
82 u32 sw_if_index = ~0;
83 vnet_hw_interface_t *hw;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +000084 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020085 virtio_if_t *vif;
86 vnet_main_t *vnm = vnet_get_main ();
87
88 /* Get a line of input. */
89 if (!unformat_user (input, unformat_line_input, line_input))
90 return 0;
91
92 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
93 {
94 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
95 ;
96 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
97 vnm, &sw_if_index))
98 ;
99 else
100 return clib_error_return (0, "unknown input `%U'",
101 format_unformat_error, input);
102 }
103 unformat_free (line_input);
104
105 if (sw_if_index == ~0)
106 return clib_error_return (0,
107 "please specify interface name or sw_if_index");
108
Dave Barach3940de32019-07-23 16:28:36 -0400109 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200110 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
111 return clib_error_return (0, "not a virtio interface");
112
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000113 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200114
115 if (virtio_pci_delete_if (vm, vif) < 0)
116 return clib_error_return (0, "not a virtio pci interface");
117
118 return 0;
119}
120
121/* *INDENT-OFF* */
122VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
123 .path = "delete interface virtio",
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000124 .short_help = "delete interface virtio "
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200125 "{<interface> | sw_if_index <sw_idx>}",
126 .function = virtio_pci_delete_command_fn,
127};
128/* *INDENT-ON* */
129
130static clib_error_t *
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000131virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input,
132 vlib_cli_command_t * cmd)
133{
134 unformat_input_t _line_input, *line_input = &_line_input;
135 u32 sw_if_index = ~0;
136 vnet_hw_interface_t *hw;
137 virtio_main_t *vim = &virtio_main;
138 virtio_if_t *vif;
139 vnet_main_t *vnm = vnet_get_main ();
140 int gso_enabled = 0, checksum_offload_enabled = 0;
141 int offloads_disabled = 0;
142
143 /* Get a line of input. */
144 if (!unformat_user (input, unformat_line_input, line_input))
145 return 0;
146
147 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
148 {
149 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
150 ;
151 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
152 vnm, &sw_if_index))
153 ;
154 else if (unformat (line_input, "gso-enabled"))
155 gso_enabled = 1;
156 else if (unformat (line_input, "csum-offload-enabled"))
157 checksum_offload_enabled = 1;
158 else if (unformat (line_input, "offloads-disabled"))
159 offloads_disabled = 1;
160 else
161 return clib_error_return (0, "unknown input `%U'",
162 format_unformat_error, input);
163 }
164 unformat_free (line_input);
165
166 if (sw_if_index == ~0)
167 return clib_error_return (0,
168 "please specify interface name or sw_if_index");
169
170 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
171 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
172 return clib_error_return (0, "not a virtio interface");
173
174 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
175
176 if (virtio_pci_enable_disable_offloads
177 (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
178 return clib_error_return (0, "not able to enable/disable offloads");
179
180 return 0;
181}
182
183/* *INDENT-OFF* */
184VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
185 .path = "set virtio pci",
186 .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
187 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
188 .function = virtio_pci_enable_command_fn,
189};
190/* *INDENT-ON* */
191
192static clib_error_t *
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200193show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
194 vlib_cli_command_t * cmd)
195{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000196 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200197 vnet_main_t *vnm = &vnet_main;
198 virtio_if_t *vif;
199 clib_error_t *error = 0;
200 u32 hw_if_index, *hw_if_indices = 0;
201 vnet_hw_interface_t *hi;
202 u8 show_descr = 0, show_device_config = 0;
203
204 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
205 {
206 if (unformat
207 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
208 {
209 hi = vnet_get_hw_interface (vnm, hw_if_index);
210 if (virtio_device_class.index != hi->dev_class_index)
211 {
212 error = clib_error_return (0, "unknown input `%U'",
213 format_unformat_error, input);
214 goto done;
215 }
216 vec_add1 (hw_if_indices, hw_if_index);
217 }
218 else if (unformat (input, "descriptors") || unformat (input, "desc"))
219 show_descr = 1;
220 else if (unformat (input, "debug-device"))
221 show_device_config = 1;
222 else
223 {
224 error = clib_error_return (0, "unknown input `%U'",
225 format_unformat_error, input);
226 goto done;
227 }
228 }
229
230 if (vec_len (hw_if_indices) == 0)
231 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000232 pool_foreach (vif, vim->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200233 vec_add1 (hw_if_indices, vif->hw_if_index);
234 );
235 }
236 else if (show_device_config)
237 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000238 vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200239 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000240 vif->virtio_pci_func->device_debug_config_space (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200241 }
242
243 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
244
245done:
246 vec_free (hw_if_indices);
247 return error;
248}
249
250/* *INDENT-OFF* */
251VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
252 .path = "show virtio pci",
253 .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
254 .function = show_virtio_pci_fn,
255};
256/* *INDENT-ON* */
257
258clib_error_t *
259virtio_pci_cli_init (vlib_main_t * vm)
260{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200261 return 0;
262}
263
264VLIB_INIT_FUNCTION (virtio_pci_cli_init);
265
266/*
267 * fd.io coding-style-patch-verification: ON
268 *
269 * Local Variables:
270 * eval: (c-set-style "gnu")
271 * End:
272 */