Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * virtio_api.c - vnet virtio pci device driver API support |
| 4 | * |
| 5 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vnet/devices/virtio/virtio.h> |
| 27 | #include <vnet/devices/virtio/pci.h> |
Jakub Grajciar | 53f06a0 | 2020-03-30 08:12:57 +0200 | [diff] [blame] | 28 | #include <vlib/pci/pci_types_api.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 29 | |
| 30 | #include <vnet/vnet_msg_enum.h> |
| 31 | |
| 32 | #define vl_typedefs /* define message structures */ |
| 33 | #include <vnet/vnet_all_api_h.h> |
| 34 | #undef vl_typedefs |
| 35 | |
| 36 | #define vl_endianfun /* define message structures */ |
| 37 | #include <vnet/vnet_all_api_h.h> |
| 38 | #undef vl_endianfun |
| 39 | |
| 40 | /* instantiate all the print functions we know about */ |
| 41 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 42 | #define vl_printfun |
| 43 | #include <vnet/vnet_all_api_h.h> |
| 44 | #undef vl_printfun |
| 45 | |
| 46 | #include <vlibapi/api_helper_macros.h> |
| 47 | |
| 48 | #define foreach_virtio_pci_api_msg \ |
| 49 | _(VIRTIO_PCI_CREATE, virtio_pci_create) \ |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 50 | _(VIRTIO_PCI_CREATE_V2, virtio_pci_create_v2) \ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 51 | _(VIRTIO_PCI_DELETE, virtio_pci_delete) \ |
| 52 | _(SW_INTERFACE_VIRTIO_PCI_DUMP, sw_interface_virtio_pci_dump) |
| 53 | |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 54 | /* It will be deprecated in 21.01 */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 55 | static void |
| 56 | vl_api_virtio_pci_create_t_handler (vl_api_virtio_pci_create_t * mp) |
| 57 | { |
| 58 | vlib_main_t *vm = vlib_get_main (); |
| 59 | vl_api_virtio_pci_create_reply_t *rmp; |
| 60 | vl_api_registration_t *reg; |
| 61 | virtio_pci_create_if_args_t _a, *ap = &_a; |
| 62 | |
| 63 | clib_memset (ap, 0, sizeof (*ap)); |
| 64 | |
Jakub Grajciar | 2c504f8 | 2019-09-26 10:34:41 +0200 | [diff] [blame] | 65 | pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 66 | if (!mp->use_random_mac) |
| 67 | { |
| 68 | clib_memcpy (ap->mac_addr, mp->mac_address, 6); |
| 69 | ap->mac_addr_set = 1; |
| 70 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 71 | ap->sw_if_index = (u32) ~ 0; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 72 | if (mp->gso_enabled) |
| 73 | ap->gso_enabled = 1; |
| 74 | else |
| 75 | ap->gso_enabled = 0; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 76 | if (mp->checksum_offload_enabled) |
| 77 | ap->checksum_offload_enabled = 1; |
| 78 | else |
| 79 | ap->checksum_offload_enabled = 0; |
| 80 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 81 | ap->features = clib_net_to_host_u64 (mp->features); |
| 82 | |
| 83 | virtio_pci_create_if (vm, ap); |
| 84 | |
| 85 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 86 | if (!reg) |
| 87 | return;; |
| 88 | |
| 89 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 90 | rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY); |
| 91 | rmp->context = mp->context; |
| 92 | rmp->retval = htonl (ap->rv); |
| 93 | rmp->sw_if_index = htonl (ap->sw_if_index); |
| 94 | |
| 95 | vl_api_send_msg (reg, (u8 *) rmp); |
| 96 | } |
| 97 | |
| 98 | static void |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 99 | vl_api_virtio_pci_create_v2_t_handler (vl_api_virtio_pci_create_v2_t * mp) |
| 100 | { |
| 101 | vlib_main_t *vm = vlib_get_main (); |
| 102 | vl_api_virtio_pci_create_v2_reply_t *rmp; |
| 103 | vl_api_registration_t *reg; |
| 104 | virtio_pci_create_if_args_t _a, *ap = &_a; |
| 105 | |
| 106 | clib_memset (ap, 0, sizeof (*ap)); |
| 107 | |
| 108 | pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr); |
| 109 | if (!mp->use_random_mac) |
| 110 | { |
| 111 | clib_memcpy (ap->mac_addr, mp->mac_address, 6); |
| 112 | ap->mac_addr_set = 1; |
| 113 | } |
| 114 | ap->sw_if_index = (u32) ~ 0; |
| 115 | |
| 116 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_GSO == (int) VIRTIO_FLAG_GSO), |
| 117 | "virtio gso api flag mismatch"); |
| 118 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_CSUM_OFFLOAD == |
| 119 | (int) VIRTIO_FLAG_CSUM_OFFLOAD), |
| 120 | "virtio checksum offload api flag mismatch"); |
| 121 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_GRO_COALESCE == |
| 122 | (int) VIRTIO_FLAG_GRO_COALESCE), |
| 123 | "virtio gro coalesce api flag mismatch"); |
| 124 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_PACKED == (int) VIRTIO_FLAG_PACKED), |
| 125 | "virtio packed api flag mismatch"); |
| 126 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_IN_ORDER == |
| 127 | (int) VIRTIO_FLAG_IN_ORDER), |
| 128 | "virtio in-order api flag mismatch"); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame^] | 129 | STATIC_ASSERT (((int) VIRTIO_API_FLAG_BUFFERING == |
| 130 | (int) VIRTIO_FLAG_BUFFERING), |
| 131 | "virtio buffering api flag mismatch"); |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 132 | |
| 133 | ap->virtio_flags = clib_net_to_host_u32 (mp->virtio_flags); |
| 134 | ap->features = clib_net_to_host_u64 (mp->features); |
| 135 | |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame^] | 136 | if (ap->virtio_flags & VIRTIO_API_FLAG_GSO) |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 137 | ap->gso_enabled = 1; |
| 138 | else |
| 139 | ap->gso_enabled = 0; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame^] | 140 | if (ap->virtio_flags & VIRTIO_API_FLAG_CSUM_OFFLOAD) |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 141 | ap->checksum_offload_enabled = 1; |
| 142 | else |
| 143 | ap->checksum_offload_enabled = 0; |
| 144 | |
| 145 | virtio_pci_create_if (vm, ap); |
| 146 | |
| 147 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 148 | if (!reg) |
| 149 | return;; |
| 150 | |
| 151 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 152 | rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_V2_REPLY); |
| 153 | rmp->context = mp->context; |
| 154 | rmp->retval = htonl (ap->rv); |
| 155 | rmp->sw_if_index = htonl (ap->sw_if_index); |
| 156 | |
| 157 | vl_api_send_msg (reg, (u8 *) rmp); |
| 158 | } |
| 159 | |
| 160 | static void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 161 | vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp) |
| 162 | { |
| 163 | vnet_main_t *vnm = vnet_get_main (); |
| 164 | vlib_main_t *vm = vlib_get_main (); |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 165 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 166 | int rv = 0; |
| 167 | vnet_hw_interface_t *hw; |
| 168 | virtio_if_t *vif; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 169 | vl_api_virtio_pci_delete_reply_t *rmp; |
| 170 | vl_api_registration_t *reg; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 171 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 172 | hw = |
| 173 | vnet_get_sup_hw_interface_api_visible_or_null (vnm, |
| 174 | htonl (mp->sw_if_index)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 175 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 176 | { |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 177 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 178 | goto reply; |
| 179 | } |
| 180 | |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 181 | vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 182 | |
| 183 | rv = virtio_pci_delete_if (vm, vif); |
| 184 | |
| 185 | reply: |
| 186 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 187 | if (!reg) |
| 188 | return; |
| 189 | |
| 190 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 191 | rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY); |
| 192 | rmp->context = mp->context; |
| 193 | rmp->retval = htonl (rv); |
| 194 | |
| 195 | vl_api_send_msg (reg, (u8 *) rmp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | virtio_pci_send_sw_interface_details (vpe_api_main_t * am, |
| 200 | vl_api_registration_t * reg, |
| 201 | virtio_if_t * vif, u32 context) |
| 202 | { |
| 203 | vl_api_sw_interface_virtio_pci_details_t *mp; |
| 204 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 205 | |
| 206 | clib_memset (mp, 0, sizeof (*mp)); |
| 207 | |
| 208 | mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS); |
Jakub Grajciar | 2c504f8 | 2019-09-26 10:34:41 +0200 | [diff] [blame] | 209 | pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32, |
| 210 | &mp->pci_addr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 211 | mp->sw_if_index = htonl (vif->sw_if_index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 212 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0); |
| 213 | mp->rx_ring_sz = htons (vring->size); |
| 214 | vring = vec_elt_at_index (vif->txq_vrings, 0); |
| 215 | mp->tx_ring_sz = htons (vring->size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 216 | clib_memcpy (mp->mac_addr, vif->mac_addr, 6); |
| 217 | mp->features = clib_host_to_net_u64 (vif->features); |
| 218 | |
| 219 | mp->context = context; |
| 220 | vl_api_send_msg (reg, (u8 *) mp); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | vl_api_sw_interface_virtio_pci_dump_t_handler |
| 225 | (vl_api_sw_interface_virtio_pci_dump_t * mp) |
| 226 | { |
| 227 | vpe_api_main_t *am = &vpe_api_main; |
| 228 | vl_api_registration_t *reg; |
| 229 | virtio_main_t *vmx = &virtio_main; |
| 230 | virtio_if_t *vif; |
| 231 | |
| 232 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 233 | if (!reg) |
| 234 | return; |
| 235 | |
| 236 | pool_foreach (vif, vmx->interfaces, ( |
| 237 | { |
| 238 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
| 239 | { |
| 240 | virtio_pci_send_sw_interface_details |
| 241 | (am, reg, vif, mp->context);} |
| 242 | } |
| 243 | )); |
| 244 | } |
| 245 | |
| 246 | #define vl_msg_name_crc_list |
| 247 | #include <vnet/vnet_all_api_h.h> |
| 248 | #undef vl_msg_name_crc_list |
| 249 | |
| 250 | static void |
| 251 | setup_message_id_table (api_main_t * am) |
| 252 | { |
| 253 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 254 | foreach_vl_msg_name_crc_virtio; |
| 255 | #undef _ |
| 256 | } |
| 257 | |
| 258 | static clib_error_t * |
| 259 | virtio_pci_api_hookup (vlib_main_t * vm) |
| 260 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 261 | api_main_t *am = vlibapi_get_main (); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 262 | |
| 263 | #define _(N,n) \ |
| 264 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 265 | vl_api_##n##_t_handler, \ |
| 266 | vl_noop_handler, \ |
| 267 | vl_api_##n##_t_endian, \ |
| 268 | vl_api_##n##_t_print, \ |
| 269 | sizeof(vl_api_##n##_t), 1); |
| 270 | foreach_virtio_pci_api_msg; |
| 271 | #undef _ |
| 272 | |
| 273 | /* |
| 274 | * Set up the (msg_name, crc, message-id) table |
| 275 | */ |
| 276 | setup_message_id_table (am); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup); |
| 282 | |
| 283 | /* |
| 284 | * fd.io coding-style-patch-verification: ON |
| 285 | * |
| 286 | * Local Variables: |
| 287 | * eval: (c-set-style "gnu") |
| 288 | * End: |
| 289 | */ |