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); |
| 33 | |
| 34 | /* Get a line of input. */ |
| 35 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 36 | return 0; |
| 37 | |
| 38 | memset (&args, 0, sizeof (args)); |
| 39 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 40 | { |
| 41 | if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr)) |
| 42 | ; |
| 43 | else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask)) |
| 44 | args.features = feature_mask; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 45 | else if (unformat (line_input, "gso-enabled")) |
| 46 | args.gso_enabled = 1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 47 | else |
| 48 | return clib_error_return (0, "unknown input `%U'", |
| 49 | format_unformat_error, input); |
| 50 | } |
| 51 | unformat_free (line_input); |
| 52 | |
| 53 | virtio_pci_create_if (vm, &args); |
| 54 | |
| 55 | return args.error; |
| 56 | } |
| 57 | |
| 58 | /* *INDENT-OFF* */ |
| 59 | VLIB_CLI_COMMAND (virtio_pci_create_command, static) = { |
| 60 | .path = "create interface virtio", |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 61 | .short_help = "create interface virtio <pci-address> " |
Chenmin Sun | 7c615ae | 2019-07-18 23:19:28 +0800 | [diff] [blame] | 62 | "[feature-mask <hex-mask>] [gso-enabled]", |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 63 | .function = virtio_pci_create_command_fn, |
| 64 | }; |
| 65 | /* *INDENT-ON* */ |
| 66 | |
| 67 | static clib_error_t * |
| 68 | virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 69 | vlib_cli_command_t * cmd) |
| 70 | { |
| 71 | unformat_input_t _line_input, *line_input = &_line_input; |
| 72 | u32 sw_if_index = ~0; |
| 73 | vnet_hw_interface_t *hw; |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 74 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 75 | virtio_if_t *vif; |
| 76 | vnet_main_t *vnm = vnet_get_main (); |
| 77 | |
| 78 | /* Get a line of input. */ |
| 79 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 80 | return 0; |
| 81 | |
| 82 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 83 | { |
| 84 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 85 | ; |
| 86 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 87 | vnm, &sw_if_index)) |
| 88 | ; |
| 89 | else |
| 90 | return clib_error_return (0, "unknown input `%U'", |
| 91 | format_unformat_error, input); |
| 92 | } |
| 93 | unformat_free (line_input); |
| 94 | |
| 95 | if (sw_if_index == ~0) |
| 96 | return clib_error_return (0, |
| 97 | "please specify interface name or sw_if_index"); |
| 98 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 99 | 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] | 100 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 101 | return clib_error_return (0, "not a virtio interface"); |
| 102 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 103 | vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 104 | |
| 105 | if (virtio_pci_delete_if (vm, vif) < 0) |
| 106 | return clib_error_return (0, "not a virtio pci interface"); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* *INDENT-OFF* */ |
| 112 | VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = { |
| 113 | .path = "delete interface virtio", |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 114 | .short_help = "delete interface virtio " |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 115 | "{<interface> | sw_if_index <sw_idx>}", |
| 116 | .function = virtio_pci_delete_command_fn, |
| 117 | }; |
| 118 | /* *INDENT-ON* */ |
| 119 | |
| 120 | static clib_error_t * |
| 121 | show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input, |
| 122 | vlib_cli_command_t * cmd) |
| 123 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 124 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 125 | vnet_main_t *vnm = &vnet_main; |
| 126 | virtio_if_t *vif; |
| 127 | clib_error_t *error = 0; |
| 128 | u32 hw_if_index, *hw_if_indices = 0; |
| 129 | vnet_hw_interface_t *hi; |
| 130 | u8 show_descr = 0, show_device_config = 0; |
| 131 | |
| 132 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 133 | { |
| 134 | if (unformat |
| 135 | (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) |
| 136 | { |
| 137 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 138 | if (virtio_device_class.index != hi->dev_class_index) |
| 139 | { |
| 140 | error = clib_error_return (0, "unknown input `%U'", |
| 141 | format_unformat_error, input); |
| 142 | goto done; |
| 143 | } |
| 144 | vec_add1 (hw_if_indices, hw_if_index); |
| 145 | } |
| 146 | else if (unformat (input, "descriptors") || unformat (input, "desc")) |
| 147 | show_descr = 1; |
| 148 | else if (unformat (input, "debug-device")) |
| 149 | show_device_config = 1; |
| 150 | else |
| 151 | { |
| 152 | error = clib_error_return (0, "unknown input `%U'", |
| 153 | format_unformat_error, input); |
| 154 | goto done; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (vec_len (hw_if_indices) == 0) |
| 159 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 160 | pool_foreach (vif, vim->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 161 | vec_add1 (hw_if_indices, vif->hw_if_index); |
| 162 | ); |
| 163 | } |
| 164 | else if (show_device_config) |
| 165 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 166 | vif = pool_elt_at_index (vim->interfaces, hi->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 167 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
| 168 | debug_device_config_space (vm, vif); |
| 169 | } |
| 170 | |
| 171 | virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI); |
| 172 | |
| 173 | done: |
| 174 | vec_free (hw_if_indices); |
| 175 | return error; |
| 176 | } |
| 177 | |
| 178 | /* *INDENT-OFF* */ |
| 179 | VLIB_CLI_COMMAND (show_virtio_pci_command, static) = { |
| 180 | .path = "show virtio pci", |
| 181 | .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]", |
| 182 | .function = show_virtio_pci_fn, |
| 183 | }; |
| 184 | /* *INDENT-ON* */ |
| 185 | |
| 186 | clib_error_t * |
| 187 | virtio_pci_cli_init (vlib_main_t * vm) |
| 188 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 189 | virtio_main_t *vim = &virtio_main; |
| 190 | vim->log_default = vlib_log_register_class ("virtio-pci", 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | VLIB_INIT_FUNCTION (virtio_pci_cli_init); |
| 195 | |
| 196 | /* |
| 197 | * fd.io coding-style-patch-verification: ON |
| 198 | * |
| 199 | * Local Variables: |
| 200 | * eval: (c-set-style "gnu") |
| 201 | * End: |
| 202 | */ |