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 | 2c504f8 | 2019-09-26 10:34:41 +0200 | [diff] [blame] | 28 | #include <vnet/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) \ |
| 50 | _(VIRTIO_PCI_DELETE, virtio_pci_delete) \ |
| 51 | _(SW_INTERFACE_VIRTIO_PCI_DUMP, sw_interface_virtio_pci_dump) |
| 52 | |
| 53 | static void |
| 54 | vl_api_virtio_pci_create_t_handler (vl_api_virtio_pci_create_t * mp) |
| 55 | { |
| 56 | vlib_main_t *vm = vlib_get_main (); |
| 57 | vl_api_virtio_pci_create_reply_t *rmp; |
| 58 | vl_api_registration_t *reg; |
| 59 | virtio_pci_create_if_args_t _a, *ap = &_a; |
| 60 | |
| 61 | clib_memset (ap, 0, sizeof (*ap)); |
| 62 | |
Jakub Grajciar | 2c504f8 | 2019-09-26 10:34:41 +0200 | [diff] [blame] | 63 | pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 64 | if (!mp->use_random_mac) |
| 65 | { |
| 66 | clib_memcpy (ap->mac_addr, mp->mac_address, 6); |
| 67 | ap->mac_addr_set = 1; |
| 68 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 69 | ap->sw_if_index = (u32) ~ 0; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 70 | if (mp->gso_enabled) |
| 71 | ap->gso_enabled = 1; |
| 72 | else |
| 73 | ap->gso_enabled = 0; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 74 | if (mp->checksum_offload_enabled) |
| 75 | ap->checksum_offload_enabled = 1; |
| 76 | else |
| 77 | ap->checksum_offload_enabled = 0; |
| 78 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 79 | ap->features = clib_net_to_host_u64 (mp->features); |
| 80 | |
| 81 | virtio_pci_create_if (vm, ap); |
| 82 | |
| 83 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 84 | if (!reg) |
| 85 | return;; |
| 86 | |
| 87 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 88 | rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY); |
| 89 | rmp->context = mp->context; |
| 90 | rmp->retval = htonl (ap->rv); |
| 91 | rmp->sw_if_index = htonl (ap->sw_if_index); |
| 92 | |
| 93 | vl_api_send_msg (reg, (u8 *) rmp); |
| 94 | } |
| 95 | |
| 96 | static void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 97 | vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp) |
| 98 | { |
| 99 | vnet_main_t *vnm = vnet_get_main (); |
| 100 | vlib_main_t *vm = vlib_get_main (); |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 101 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 102 | int rv = 0; |
| 103 | vnet_hw_interface_t *hw; |
| 104 | virtio_if_t *vif; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 105 | vl_api_virtio_pci_delete_reply_t *rmp; |
| 106 | vl_api_registration_t *reg; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 107 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 108 | hw = |
| 109 | vnet_get_sup_hw_interface_api_visible_or_null (vnm, |
| 110 | htonl (mp->sw_if_index)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 111 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 112 | { |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 113 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 114 | goto reply; |
| 115 | } |
| 116 | |
Mohsin Kazmi | ddd2183 | 2019-01-22 13:05:00 +0000 | [diff] [blame] | 117 | vif = pool_elt_at_index (vim->interfaces, hw->dev_instance); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 118 | |
| 119 | rv = virtio_pci_delete_if (vm, vif); |
| 120 | |
| 121 | reply: |
| 122 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 123 | if (!reg) |
| 124 | return; |
| 125 | |
| 126 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 127 | rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY); |
| 128 | rmp->context = mp->context; |
| 129 | rmp->retval = htonl (rv); |
| 130 | |
| 131 | vl_api_send_msg (reg, (u8 *) rmp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void |
| 135 | virtio_pci_send_sw_interface_details (vpe_api_main_t * am, |
| 136 | vl_api_registration_t * reg, |
| 137 | virtio_if_t * vif, u32 context) |
| 138 | { |
| 139 | vl_api_sw_interface_virtio_pci_details_t *mp; |
| 140 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 141 | |
| 142 | clib_memset (mp, 0, sizeof (*mp)); |
| 143 | |
| 144 | mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS); |
Jakub Grajciar | 2c504f8 | 2019-09-26 10:34:41 +0200 | [diff] [blame] | 145 | pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32, |
| 146 | &mp->pci_addr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 147 | mp->sw_if_index = htonl (vif->sw_if_index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 148 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0); |
| 149 | mp->rx_ring_sz = htons (vring->size); |
| 150 | vring = vec_elt_at_index (vif->txq_vrings, 0); |
| 151 | mp->tx_ring_sz = htons (vring->size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 152 | clib_memcpy (mp->mac_addr, vif->mac_addr, 6); |
| 153 | mp->features = clib_host_to_net_u64 (vif->features); |
| 154 | |
| 155 | mp->context = context; |
| 156 | vl_api_send_msg (reg, (u8 *) mp); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | vl_api_sw_interface_virtio_pci_dump_t_handler |
| 161 | (vl_api_sw_interface_virtio_pci_dump_t * mp) |
| 162 | { |
| 163 | vpe_api_main_t *am = &vpe_api_main; |
| 164 | vl_api_registration_t *reg; |
| 165 | virtio_main_t *vmx = &virtio_main; |
| 166 | virtio_if_t *vif; |
| 167 | |
| 168 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 169 | if (!reg) |
| 170 | return; |
| 171 | |
| 172 | pool_foreach (vif, vmx->interfaces, ( |
| 173 | { |
| 174 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
| 175 | { |
| 176 | virtio_pci_send_sw_interface_details |
| 177 | (am, reg, vif, mp->context);} |
| 178 | } |
| 179 | )); |
| 180 | } |
| 181 | |
| 182 | #define vl_msg_name_crc_list |
| 183 | #include <vnet/vnet_all_api_h.h> |
| 184 | #undef vl_msg_name_crc_list |
| 185 | |
| 186 | static void |
| 187 | setup_message_id_table (api_main_t * am) |
| 188 | { |
| 189 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 190 | foreach_vl_msg_name_crc_virtio; |
| 191 | #undef _ |
| 192 | } |
| 193 | |
| 194 | static clib_error_t * |
| 195 | virtio_pci_api_hookup (vlib_main_t * vm) |
| 196 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 197 | api_main_t *am = vlibapi_get_main (); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 198 | |
| 199 | #define _(N,n) \ |
| 200 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 201 | vl_api_##n##_t_handler, \ |
| 202 | vl_noop_handler, \ |
| 203 | vl_api_##n##_t_endian, \ |
| 204 | vl_api_##n##_t_print, \ |
| 205 | sizeof(vl_api_##n##_t), 1); |
| 206 | foreach_virtio_pci_api_msg; |
| 207 | #undef _ |
| 208 | |
| 209 | /* |
| 210 | * Set up the (msg_name, crc, message-id) table |
| 211 | */ |
| 212 | setup_message_id_table (am); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup); |
| 218 | |
| 219 | /* |
| 220 | * fd.io coding-style-patch-verification: ON |
| 221 | * |
| 222 | * Local Variables: |
| 223 | * eval: (c-set-style "gnu") |
| 224 | * End: |
| 225 | */ |