Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1 | /* |
| 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 Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 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> |
| 25 | |
| 26 | static clib_error_t * |
| 27 | virtio_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 Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 32 | u64 feature_mask = (u64) ~ (0ULL); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 33 | u32 buffering_size = 0; |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 34 | u32 txq_size = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 35 | |
| 36 | /* Get a line of input. */ |
| 37 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 38 | return 0; |
| 39 | |
| 40 | memset (&args, 0, sizeof (args)); |
| 41 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 42 | { |
| 43 | if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr)) |
| 44 | ; |
| 45 | else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask)) |
| 46 | args.features = feature_mask; |
Vratko Polak | 23b6a9e | 2023-11-14 19:41:11 +0100 | [diff] [blame] | 47 | else if (unformat (line_input, "tx-queue-size %u", &txq_size)) |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 48 | args.tx_queue_size = txq_size; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 49 | else if (unformat (line_input, "gso-enabled")) |
| 50 | args.gso_enabled = 1; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 51 | else if (unformat (line_input, "csum-enabled")) |
| 52 | args.checksum_offload_enabled = 1; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 53 | else if (unformat (line_input, "buffering")) |
| 54 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 55 | args.virtio_flags |= VIRTIO_FLAG_BUFFERING; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 56 | if (unformat (line_input, "size %u", &buffering_size)) |
| 57 | args.buffering_size = buffering_size; |
| 58 | } |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 59 | else if (unformat (line_input, "packed")) |
| 60 | args.virtio_flags |= VIRTIO_FLAG_PACKED; |
Benoît Ganne | c04d8c4 | 2022-10-13 14:01:03 +0200 | [diff] [blame] | 61 | else if (unformat (line_input, "bind force")) |
| 62 | args.bind = VIRTIO_BIND_FORCE; |
| 63 | else if (unformat (line_input, "bind")) |
| 64 | args.bind = VIRTIO_BIND_DEFAULT; |
Steven Luong | e9bc332 | 2023-12-14 08:54:55 -0800 | [diff] [blame] | 65 | else if (unformat (line_input, "rss-enabled")) |
| 66 | args.rss_enabled = 1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 67 | else |
| 68 | return clib_error_return (0, "unknown input `%U'", |
| 69 | format_unformat_error, input); |
| 70 | } |
| 71 | unformat_free (line_input); |
| 72 | |
| 73 | virtio_pci_create_if (vm, &args); |
| 74 | |
| 75 | return args.error; |
| 76 | } |
| 77 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 78 | VLIB_CLI_COMMAND (virtio_pci_create_command, static) = { |
| 79 | .path = "create interface virtio", |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 80 | .short_help = "create interface virtio <pci-address> " |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 81 | "[feature-mask <hex-mask>] [tx-queue-size <size>] " |
Steven Luong | e9bc332 | 2023-12-14 08:54:55 -0800 | [diff] [blame] | 82 | "[gso-enabled] [csum-enabled] [rss-enabled] " |
Benoît Ganne | c04d8c4 | 2022-10-13 14:01:03 +0200 | [diff] [blame] | 83 | "[buffering [size <buffering-szie>]] [packed] [bind [force]]", |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 84 | .function = virtio_pci_create_command_fn, |
| 85 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 86 | |
| 87 | static clib_error_t * |
| 88 | virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 89 | vlib_cli_command_t * cmd) |
| 90 | { |
| 91 | unformat_input_t _line_input, *line_input = &_line_input; |
| 92 | u32 sw_if_index = ~0; |
| 93 | vnet_hw_interface_t *hw; |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 94 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 95 | virtio_if_t *vif; |
| 96 | vnet_main_t *vnm = vnet_get_main (); |
| 97 | |
| 98 | /* Get a line of input. */ |
| 99 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 100 | return 0; |
| 101 | |
| 102 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 103 | { |
| 104 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 105 | ; |
| 106 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 107 | vnm, &sw_if_index)) |
| 108 | ; |
| 109 | else |
| 110 | return clib_error_return (0, "unknown input `%U'", |
| 111 | format_unformat_error, input); |
| 112 | } |
| 113 | unformat_free (line_input); |
| 114 | |
| 115 | if (sw_if_index == ~0) |
| 116 | return clib_error_return (0, |
| 117 | "please specify interface name or sw_if_index"); |
| 118 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 119 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 120 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 121 | return clib_error_return (0, "not a virtio interface"); |
| 122 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 123 | vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 124 | |
| 125 | if (virtio_pci_delete_if (vm, vif) < 0) |
| 126 | return clib_error_return (0, "not a virtio pci interface"); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 131 | VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = { |
| 132 | .path = "delete interface virtio", |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 133 | .short_help = "delete interface virtio " |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 134 | "{<interface> | sw_if_index <sw_idx>}", |
| 135 | .function = virtio_pci_delete_command_fn, |
| 136 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 137 | |
| 138 | static clib_error_t * |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 139 | virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 140 | vlib_cli_command_t * cmd) |
| 141 | { |
| 142 | unformat_input_t _line_input, *line_input = &_line_input; |
| 143 | u32 sw_if_index = ~0; |
| 144 | vnet_hw_interface_t *hw; |
| 145 | virtio_main_t *vim = &virtio_main; |
| 146 | virtio_if_t *vif; |
| 147 | vnet_main_t *vnm = vnet_get_main (); |
| 148 | int gso_enabled = 0, checksum_offload_enabled = 0; |
| 149 | int offloads_disabled = 0; |
| 150 | |
| 151 | /* Get a line of input. */ |
| 152 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 153 | return 0; |
| 154 | |
| 155 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 156 | { |
| 157 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 158 | ; |
| 159 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 160 | vnm, &sw_if_index)) |
| 161 | ; |
| 162 | else if (unformat (line_input, "gso-enabled")) |
| 163 | gso_enabled = 1; |
| 164 | else if (unformat (line_input, "csum-offload-enabled")) |
| 165 | checksum_offload_enabled = 1; |
| 166 | else if (unformat (line_input, "offloads-disabled")) |
| 167 | offloads_disabled = 1; |
| 168 | else |
| 169 | return clib_error_return (0, "unknown input `%U'", |
| 170 | format_unformat_error, input); |
| 171 | } |
| 172 | unformat_free (line_input); |
| 173 | |
| 174 | if (sw_if_index == ~0) |
| 175 | return clib_error_return (0, |
| 176 | "please specify interface name or sw_if_index"); |
| 177 | |
| 178 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
| 179 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 180 | return clib_error_return (0, "not a virtio interface"); |
| 181 | |
| 182 | vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); |
| 183 | |
| 184 | if (virtio_pci_enable_disable_offloads |
| 185 | (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0) |
| 186 | return clib_error_return (0, "not able to enable/disable offloads"); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 191 | VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = { |
| 192 | .path = "set virtio pci", |
| 193 | .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}" |
| 194 | " [gso-enabled | csum-offload-enabled | offloads-disabled]", |
| 195 | .function = virtio_pci_enable_command_fn, |
| 196 | }; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 197 | |
| 198 | static clib_error_t * |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 199 | show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input, |
| 200 | vlib_cli_command_t * cmd) |
| 201 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 202 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 203 | vnet_main_t *vnm = &vnet_main; |
| 204 | virtio_if_t *vif; |
| 205 | clib_error_t *error = 0; |
| 206 | u32 hw_if_index, *hw_if_indices = 0; |
| 207 | vnet_hw_interface_t *hi; |
| 208 | u8 show_descr = 0, show_device_config = 0; |
| 209 | |
| 210 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 211 | { |
| 212 | if (unformat |
| 213 | (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) |
| 214 | { |
| 215 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 216 | if (virtio_device_class.index != hi->dev_class_index) |
| 217 | { |
| 218 | error = clib_error_return (0, "unknown input `%U'", |
| 219 | format_unformat_error, input); |
| 220 | goto done; |
| 221 | } |
| 222 | vec_add1 (hw_if_indices, hw_if_index); |
| 223 | } |
| 224 | else if (unformat (input, "descriptors") || unformat (input, "desc")) |
| 225 | show_descr = 1; |
| 226 | else if (unformat (input, "debug-device")) |
| 227 | show_device_config = 1; |
| 228 | else |
| 229 | { |
| 230 | error = clib_error_return (0, "unknown input `%U'", |
| 231 | format_unformat_error, input); |
| 232 | goto done; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (vec_len (hw_if_indices) == 0) |
| 237 | { |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 238 | pool_foreach (vif, vim->interfaces) |
| 239 | vec_add1 (hw_if_indices, vif->hw_if_index); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 240 | } |
| 241 | else if (show_device_config) |
| 242 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 243 | vif = pool_elt_at_index (vim->interfaces, hi->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 244 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 245 | vif->virtio_pci_func->device_debug_config_space (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI); |
| 249 | |
| 250 | done: |
| 251 | vec_free (hw_if_indices); |
| 252 | return error; |
| 253 | } |
| 254 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 255 | VLIB_CLI_COMMAND (show_virtio_pci_command, static) = { |
| 256 | .path = "show virtio pci", |
| 257 | .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]", |
| 258 | .function = show_virtio_pci_fn, |
| 259 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 260 | |
| 261 | clib_error_t * |
| 262 | virtio_pci_cli_init (vlib_main_t * vm) |
| 263 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | VLIB_INIT_FUNCTION (virtio_pci_cli_init); |
| 268 | |
| 269 | /* |
| 270 | * fd.io coding-style-patch-verification: ON |
| 271 | * |
| 272 | * Local Variables: |
| 273 | * eval: (c-set-style "gnu") |
| 274 | * End: |
| 275 | */ |