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