blob: 05f9eab18cffd903840c2b59394b3d9bfe6fcff3 [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 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010052 args.virtio_flags |= VIRTIO_FLAG_BUFFERING;
Mohsin Kazmie347acb2020-09-28 10:26:33 +000053 if (unformat (line_input, "size %u", &buffering_size))
54 args.buffering_size = buffering_size;
55 }
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010056 else if (unformat (line_input, "packed"))
57 args.virtio_flags |= VIRTIO_FLAG_PACKED;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020058 else
59 return clib_error_return (0, "unknown input `%U'",
60 format_unformat_error, input);
61 }
62 unformat_free (line_input);
63
64 virtio_pci_create_if (vm, &args);
65
66 return args.error;
67}
68
69/* *INDENT-OFF* */
70VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
71 .path = "create interface virtio",
Mohsin Kazmiddd21832019-01-22 13:05:00 +000072 .short_help = "create interface virtio <pci-address> "
Mohsin Kazmie347acb2020-09-28 10:26:33 +000073 "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled] "
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010074 "[buffering [size <buffering-szie>]] [packed]",
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020075 .function = virtio_pci_create_command_fn,
76};
77/* *INDENT-ON* */
78
79static clib_error_t *
80virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
81 vlib_cli_command_t * cmd)
82{
83 unformat_input_t _line_input, *line_input = &_line_input;
84 u32 sw_if_index = ~0;
85 vnet_hw_interface_t *hw;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +000086 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020087 virtio_if_t *vif;
88 vnet_main_t *vnm = vnet_get_main ();
89
90 /* Get a line of input. */
91 if (!unformat_user (input, unformat_line_input, line_input))
92 return 0;
93
94 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
95 {
96 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
97 ;
98 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
99 vnm, &sw_if_index))
100 ;
101 else
102 return clib_error_return (0, "unknown input `%U'",
103 format_unformat_error, input);
104 }
105 unformat_free (line_input);
106
107 if (sw_if_index == ~0)
108 return clib_error_return (0,
109 "please specify interface name or sw_if_index");
110
Dave Barach3940de32019-07-23 16:28:36 -0400111 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200112 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
113 return clib_error_return (0, "not a virtio interface");
114
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000115 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200116
117 if (virtio_pci_delete_if (vm, vif) < 0)
118 return clib_error_return (0, "not a virtio pci interface");
119
120 return 0;
121}
122
123/* *INDENT-OFF* */
124VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
125 .path = "delete interface virtio",
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000126 .short_help = "delete interface virtio "
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200127 "{<interface> | sw_if_index <sw_idx>}",
128 .function = virtio_pci_delete_command_fn,
129};
130/* *INDENT-ON* */
131
132static clib_error_t *
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000133virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input,
134 vlib_cli_command_t * cmd)
135{
136 unformat_input_t _line_input, *line_input = &_line_input;
137 u32 sw_if_index = ~0;
138 vnet_hw_interface_t *hw;
139 virtio_main_t *vim = &virtio_main;
140 virtio_if_t *vif;
141 vnet_main_t *vnm = vnet_get_main ();
142 int gso_enabled = 0, checksum_offload_enabled = 0;
143 int offloads_disabled = 0;
144
145 /* Get a line of input. */
146 if (!unformat_user (input, unformat_line_input, line_input))
147 return 0;
148
149 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
150 {
151 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
152 ;
153 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
154 vnm, &sw_if_index))
155 ;
156 else if (unformat (line_input, "gso-enabled"))
157 gso_enabled = 1;
158 else if (unformat (line_input, "csum-offload-enabled"))
159 checksum_offload_enabled = 1;
160 else if (unformat (line_input, "offloads-disabled"))
161 offloads_disabled = 1;
162 else
163 return clib_error_return (0, "unknown input `%U'",
164 format_unformat_error, input);
165 }
166 unformat_free (line_input);
167
168 if (sw_if_index == ~0)
169 return clib_error_return (0,
170 "please specify interface name or sw_if_index");
171
172 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
173 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
174 return clib_error_return (0, "not a virtio interface");
175
176 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
177
178 if (virtio_pci_enable_disable_offloads
179 (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
180 return clib_error_return (0, "not able to enable/disable offloads");
181
182 return 0;
183}
184
185/* *INDENT-OFF* */
186VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
187 .path = "set virtio pci",
188 .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
189 " [gso-enabled | csum-offload-enabled | offloads-disabled]",
190 .function = virtio_pci_enable_command_fn,
191};
192/* *INDENT-ON* */
193
194static clib_error_t *
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200195show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
196 vlib_cli_command_t * cmd)
197{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000198 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200199 vnet_main_t *vnm = &vnet_main;
200 virtio_if_t *vif;
201 clib_error_t *error = 0;
202 u32 hw_if_index, *hw_if_indices = 0;
203 vnet_hw_interface_t *hi;
204 u8 show_descr = 0, show_device_config = 0;
205
206 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
207 {
208 if (unformat
209 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
210 {
211 hi = vnet_get_hw_interface (vnm, hw_if_index);
212 if (virtio_device_class.index != hi->dev_class_index)
213 {
214 error = clib_error_return (0, "unknown input `%U'",
215 format_unformat_error, input);
216 goto done;
217 }
218 vec_add1 (hw_if_indices, hw_if_index);
219 }
220 else if (unformat (input, "descriptors") || unformat (input, "desc"))
221 show_descr = 1;
222 else if (unformat (input, "debug-device"))
223 show_device_config = 1;
224 else
225 {
226 error = clib_error_return (0, "unknown input `%U'",
227 format_unformat_error, input);
228 goto done;
229 }
230 }
231
232 if (vec_len (hw_if_indices) == 0)
233 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000234 pool_foreach (vif, vim->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200235 vec_add1 (hw_if_indices, vif->hw_if_index);
236 );
237 }
238 else if (show_device_config)
239 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000240 vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200241 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000242 vif->virtio_pci_func->device_debug_config_space (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200243 }
244
245 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
246
247done:
248 vec_free (hw_if_indices);
249 return error;
250}
251
252/* *INDENT-OFF* */
253VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
254 .path = "show virtio pci",
255 .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
256 .function = show_virtio_pci_fn,
257};
258/* *INDENT-ON* */
259
260clib_error_t *
261virtio_pci_cli_init (vlib_main_t * vm)
262{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200263 return 0;
264}
265
266VLIB_INIT_FUNCTION (virtio_pci_cli_init);
267
268/*
269 * fd.io coding-style-patch-verification: ON
270 *
271 * Local Variables:
272 * eval: (c-set-style "gnu")
273 * End:
274 */