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