Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 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 | */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <net/if.h> |
| 22 | #include <linux/if_tun.h> |
| 23 | #include <sys/ioctl.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 24 | #include <sys/eventfd.h> |
| 25 | |
| 26 | #include <vlib/vlib.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 27 | #include <vlib/pci/pci.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 28 | #include <vlib/unix/unix.h> |
| 29 | #include <vnet/ethernet/ethernet.h> |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 30 | #include <vnet/ip/ip4_packet.h> |
| 31 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 32 | #include <vnet/devices/virtio/virtio.h> |
Mohsin Kazmi | 6799f9b | 2021-04-28 18:55:45 +0200 | [diff] [blame] | 33 | #include <vnet/devices/virtio/virtio_inline.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 34 | #include <vnet/devices/virtio/pci.h> |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 35 | #include <vnet/interface/rx_queue_funcs.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 36 | |
| 37 | virtio_main_t virtio_main; |
| 38 | |
| 39 | #define _IOCTL(fd,a,...) \ |
| 40 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 41 | { \ |
| 42 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 43 | goto error; \ |
| 44 | } |
| 45 | |
| 46 | static clib_error_t * |
| 47 | call_read_ready (clib_file_t * uf) |
| 48 | { |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 49 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 50 | u64 b; |
| 51 | |
| 52 | CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b)); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 53 | vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | clib_error_t * |
| 60 | virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) |
| 61 | { |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 62 | virtio_vring_t *vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 63 | int i; |
| 64 | |
| 65 | if (!is_pow2 (sz)) |
| 66 | return clib_error_return (0, "ring size must be power of 2"); |
| 67 | |
| 68 | if (sz > 32768) |
| 69 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 70 | |
| 71 | if (sz == 0) |
| 72 | sz = 256; |
| 73 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 74 | if (idx % 2) |
| 75 | { |
Mohsin Kazmi | 3f34017 | 2019-05-27 15:53:25 +0200 | [diff] [blame] | 76 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 77 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx), |
| 78 | CLIB_CACHE_LINE_BYTES); |
| 79 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx)); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 80 | if (thm->n_vlib_mains > vif->num_txqs) |
Mohsin Kazmi | 3f34017 | 2019-05-27 15:53:25 +0200 | [diff] [blame] | 81 | clib_spinlock_init (&vring->lockp); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
| 85 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx), |
| 86 | CLIB_CACHE_LINE_BYTES); |
| 87 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx)); |
| 88 | } |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 89 | i = sizeof (vring_desc_t) * sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 90 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 91 | vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 92 | clib_memset (vring->desc, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 93 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 94 | i = sizeof (vring_avail_t) + sz * sizeof (vring->avail->ring[0]); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 95 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 96 | vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 97 | clib_memset (vring->avail, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 98 | // tell kernel that we don't need interrupt |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 99 | vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 100 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 101 | i = sizeof (vring_used_t) + sz * sizeof (vring_used_elem_t); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 102 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 103 | vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 104 | clib_memset (vring->used, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 105 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 106 | vring->queue_id = idx; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 107 | ASSERT (vring->buffers == 0); |
Damjan Marion | c58408c | 2018-01-18 14:54:04 +0100 | [diff] [blame] | 108 | vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 109 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 110 | if (idx & 1) |
| 111 | { |
| 112 | clib_memset_u32 (vring->buffers, ~0, sz); |
Mohsin Kazmi | 4b56340 | 2021-01-27 14:16:56 +0000 | [diff] [blame] | 113 | // tx path: suppress the interrupts from kernel |
| 114 | vring->call_fd = -1; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 115 | } |
Mohsin Kazmi | 4b56340 | 2021-01-27 14:16:56 +0000 | [diff] [blame] | 116 | else |
| 117 | vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 118 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 119 | vring->size = sz; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 120 | vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
| 121 | virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx, |
| 122 | vring->size, vring->call_fd, vring->kick_fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 123 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 124 | return 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 127 | inline void |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 128 | virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 129 | { |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 130 | u16 used = vring->desc_in_use; |
Steven | 074f698 | 2018-03-30 22:18:11 -0700 | [diff] [blame] | 131 | u16 last = vring->last_used_idx; |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 132 | u16 mask = vring->size - 1; |
| 133 | |
| 134 | while (used) |
| 135 | { |
Steven | 074f698 | 2018-03-30 22:18:11 -0700 | [diff] [blame] | 136 | vlib_buffer_free (vm, &vring->buffers[last & mask], 1); |
| 137 | last++; |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 138 | used--; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | clib_error_t * |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 143 | virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx) |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 144 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 145 | virtio_vring_t *vring = |
| 146 | vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx)); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 147 | |
| 148 | clib_file_del_by_index (&file_main, vring->call_file_index); |
| 149 | close (vring->kick_fd); |
| 150 | close (vring->call_fd); |
| 151 | if (vring->used) |
| 152 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 153 | virtio_free_buffers (vm, vring); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 154 | clib_mem_free (vring->used); |
| 155 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 156 | if (vring->desc) |
| 157 | clib_mem_free (vring->desc); |
| 158 | if (vring->avail) |
| 159 | clib_mem_free (vring->avail); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 160 | vec_free (vring->buffers); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | clib_error_t * |
| 165 | virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx) |
| 166 | { |
| 167 | virtio_vring_t *vring = |
| 168 | vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx)); |
| 169 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 170 | close (vring->kick_fd); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 171 | if (vring->used) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 172 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 173 | virtio_free_buffers (vm, vring); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 174 | clib_mem_free (vring->used); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 175 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 176 | if (vring->desc) |
| 177 | clib_mem_free (vring->desc); |
| 178 | if (vring->avail) |
| 179 | clib_mem_free (vring->avail); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 180 | vec_free (vring->buffers); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 181 | gro_flow_table_free (vring->flow_table); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 182 | virtio_vring_buffering_free (vm, vring->buffering); |
Mohsin Kazmi | 3f34017 | 2019-05-27 15:53:25 +0200 | [diff] [blame] | 183 | clib_spinlock_free (&vring->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 187 | void |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 188 | virtio_set_packet_coalesce (virtio_if_t * vif) |
| 189 | { |
| 190 | vnet_main_t *vnm = vnet_get_main (); |
| 191 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 192 | virtio_vring_t *vring; |
Mohsin Kazmi | 1017a1d | 2020-09-25 15:36:19 +0200 | [diff] [blame] | 193 | vif->packet_coalesce = 1; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 194 | vec_foreach (vring, vif->txq_vrings) |
| 195 | { |
| 196 | gro_flow_table_init (&vring->flow_table, |
| 197 | vif->type & (VIRTIO_IF_TYPE_TAP | |
| 198 | VIRTIO_IF_TYPE_PCI), hw->tx_node_index); |
| 199 | } |
| 200 | } |
| 201 | |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 202 | clib_error_t * |
| 203 | virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size) |
| 204 | { |
| 205 | vnet_main_t *vnm = vnet_get_main (); |
| 206 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 207 | virtio_vring_t *vring; |
| 208 | clib_error_t *error = 0; |
| 209 | vif->packet_buffering = 1; |
| 210 | |
| 211 | vec_foreach (vring, vif->txq_vrings) |
| 212 | { |
| 213 | if ((error = |
| 214 | virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index, |
| 215 | buffering_size))) |
| 216 | { |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return error; |
| 222 | } |
| 223 | |
Mohsin Kazmi | 6799f9b | 2021-04-28 18:55:45 +0200 | [diff] [blame] | 224 | static void |
| 225 | virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif, virtio_vring_t *vring) |
| 226 | { |
| 227 | if (vif->is_packed) |
| 228 | virtio_refill_vring_packed (vm, vif, vif->type, vring, |
| 229 | vif->virtio_net_hdr_sz, |
| 230 | virtio_input_node.index); |
| 231 | else |
| 232 | virtio_refill_vring_split (vm, vif, vif->type, vring, |
| 233 | vif->virtio_net_hdr_sz, |
| 234 | virtio_input_node.index); |
| 235 | } |
| 236 | |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 237 | void |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 238 | virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif) |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 239 | { |
| 240 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 241 | virtio_vring_t *vring; |
Mohsin Kazmi | 90ffe06 | 2021-02-23 12:46:14 +0100 | [diff] [blame] | 242 | u32 i = 0; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 243 | |
| 244 | vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index); |
| 245 | |
| 246 | vec_foreach (vring, vif->rxq_vrings) |
| 247 | { |
| 248 | vring->queue_index = vnet_hw_if_register_rx_queue ( |
| 249 | vnm, vif->hw_if_index, RX_QUEUE_ACCESS (vring->queue_id), |
| 250 | VNET_HW_IF_RXQ_THREAD_ANY); |
| 251 | vring->buffer_pool_index = vlib_buffer_pool_get_default_for_numa ( |
| 252 | vm, vnet_hw_if_get_rx_queue_numa_node (vnm, vring->queue_index)); |
| 253 | if (vif->type == VIRTIO_IF_TYPE_TAP || vif->type == VIRTIO_IF_TYPE_TUN) |
| 254 | { |
| 255 | |
| 256 | clib_file_t f = { |
| 257 | .read_function = call_read_ready, |
| 258 | .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED, |
| 259 | .file_descriptor = vring->call_fd, |
| 260 | .private_data = vring->queue_index, |
| 261 | .description = format (0, "%U vring %u", format_virtio_device_name, |
| 262 | vif->dev_instance, vring->queue_id), |
| 263 | }; |
| 264 | |
| 265 | vring->call_file_index = clib_file_add (&file_main, &f); |
| 266 | vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index, |
| 267 | vring->call_file_index); |
| 268 | } |
Mohsin Kazmi | 90ffe06 | 2021-02-23 12:46:14 +0100 | [diff] [blame] | 269 | else if ((vif->type == VIRTIO_IF_TYPE_PCI) && (vif->support_int_mode) && |
| 270 | (vif->msix_enabled == VIRTIO_MSIX_ENABLED)) |
| 271 | { |
| 272 | u32 file_index; |
| 273 | file_index = |
| 274 | vlib_pci_get_msix_file_index (vm, vif->pci_dev_handle, i + 1); |
| 275 | vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index, |
| 276 | file_index); |
| 277 | i++; |
| 278 | } |
Mohsin Kazmi | 6799f9b | 2021-04-28 18:55:45 +0200 | [diff] [blame] | 279 | vnet_hw_if_set_rx_queue_mode (vnm, vring->queue_index, |
| 280 | VNET_HW_IF_RX_MODE_POLLING); |
| 281 | vring->mode = VNET_HW_IF_RX_MODE_POLLING; |
| 282 | virtio_vring_fill (vm, vif, vring); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 283 | } |
| 284 | vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index); |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 287 | inline void |
| 288 | virtio_set_net_hdr_size (virtio_if_t * vif) |
| 289 | { |
| 290 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) || |
| 291 | vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 292 | vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_v1_t); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 293 | else |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 294 | vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_t); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | inline void |
Mohsin Kazmi | e4efbe7 | 2021-09-22 18:56:05 +0000 | [diff] [blame] | 298 | virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, |
| 299 | virtio_if_type_t type) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 300 | { |
| 301 | u32 i, j, hw_if_index; |
| 302 | virtio_if_t *vif; |
| 303 | vnet_main_t *vnm = &vnet_main; |
| 304 | virtio_main_t *mm = &virtio_main; |
| 305 | virtio_vring_t *vring; |
| 306 | struct feat_struct |
| 307 | { |
| 308 | u8 bit; |
| 309 | char *str; |
| 310 | }; |
| 311 | struct feat_struct *feat_entry; |
| 312 | |
| 313 | static struct feat_struct feat_array[] = { |
| 314 | #define _(s,b) { .str = #s, .bit = b, }, |
| 315 | foreach_virtio_net_features |
| 316 | #undef _ |
| 317 | {.str = NULL} |
| 318 | }; |
| 319 | |
| 320 | struct feat_struct *flag_entry; |
| 321 | static struct feat_struct flags_array[] = { |
| 322 | #define _(b,e,s) { .bit = b, .str = s, }, |
| 323 | foreach_virtio_if_flag |
| 324 | #undef _ |
| 325 | {.str = NULL} |
| 326 | }; |
| 327 | |
| 328 | if (!hw_if_indices) |
| 329 | return; |
| 330 | |
| 331 | for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++) |
| 332 | { |
| 333 | vnet_hw_interface_t *hi = |
| 334 | vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]); |
| 335 | vif = pool_elt_at_index (mm->interfaces, hi->dev_instance); |
| 336 | if (vif->type != type) |
| 337 | continue; |
| 338 | vlib_cli_output (vm, "Interface: %U (ifindex %d)", |
| 339 | format_vnet_hw_if_index_name, vnm, |
| 340 | hw_if_indices[hw_if_index], vif->hw_if_index); |
| 341 | if (type == VIRTIO_IF_TYPE_PCI) |
| 342 | { |
| 343 | vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr, |
| 344 | &vif->pci_addr); |
| 345 | } |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 346 | if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 347 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 348 | u8 *str = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 349 | if (vif->host_if_name) |
| 350 | vlib_cli_output (vm, " name \"%s\"", vif->host_if_name); |
| 351 | if (vif->net_ns) |
| 352 | vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns); |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 353 | if (vif->host_mtu_size) |
| 354 | vlib_cli_output (vm, " host-mtu-size \"%d\"", |
| 355 | vif->host_mtu_size); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 356 | if (type == VIRTIO_IF_TYPE_TAP) |
| 357 | vlib_cli_output (vm, " host-mac-addr: %U", |
| 358 | format_ethernet_address, vif->host_mac_addr); |
Matthew Smith | bd50ed1 | 2020-07-24 13:38:03 -0500 | [diff] [blame] | 359 | vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 360 | |
| 361 | vec_foreach_index (i, vif->vhost_fds) |
| 362 | str = format (str, " %d", vif->vhost_fds[i]); |
| 363 | vlib_cli_output (vm, " vhost-fds%v", str); |
| 364 | vec_free (str); |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 365 | vec_foreach_index (i, vif->tap_fds) |
| 366 | str = format (str, " %d", vif->tap_fds[i]); |
| 367 | vlib_cli_output (vm, " tap-fds%v", str); |
| 368 | vec_free (str); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 369 | } |
Chenmin Sun | 7c615ae | 2019-07-18 23:19:28 +0800 | [diff] [blame] | 370 | vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled); |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 371 | vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 372 | vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 373 | vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 374 | if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI)) |
| 375 | vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address, |
| 376 | vif->mac_addr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 377 | vlib_cli_output (vm, " Device instance: %u", vif->dev_instance); |
| 378 | vlib_cli_output (vm, " flags 0x%x", vif->flags); |
| 379 | flag_entry = (struct feat_struct *) &flags_array; |
| 380 | while (flag_entry->str) |
| 381 | { |
| 382 | if (vif->flags & (1ULL << flag_entry->bit)) |
| 383 | vlib_cli_output (vm, " %s (%d)", flag_entry->str, |
| 384 | flag_entry->bit); |
| 385 | flag_entry++; |
| 386 | } |
| 387 | if (type == VIRTIO_IF_TYPE_PCI) |
| 388 | { |
| 389 | device_status (vm, vif); |
| 390 | } |
| 391 | vlib_cli_output (vm, " features 0x%lx", vif->features); |
| 392 | feat_entry = (struct feat_struct *) &feat_array; |
| 393 | while (feat_entry->str) |
| 394 | { |
| 395 | if (vif->features & (1ULL << feat_entry->bit)) |
| 396 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 397 | feat_entry->bit); |
| 398 | feat_entry++; |
| 399 | } |
| 400 | vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features); |
| 401 | feat_entry = (struct feat_struct *) &feat_array; |
| 402 | while (feat_entry->str) |
| 403 | { |
| 404 | if (vif->remote_features & (1ULL << feat_entry->bit)) |
| 405 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 406 | feat_entry->bit); |
| 407 | feat_entry++; |
| 408 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 409 | vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs); |
| 410 | vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs); |
Mohsin Kazmi | e4efbe7 | 2021-09-22 18:56:05 +0000 | [diff] [blame] | 411 | if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL && |
| 412 | vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 413 | vlib_cli_output (vm, " Number of CTRL Virtqueue 1"); |
| 414 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 415 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 416 | vring = vec_elt_at_index (vif->rxq_vrings, i); |
| 417 | vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 418 | vlib_cli_output (vm, |
| 419 | " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", |
| 420 | vring->size, vring->last_used_idx, vring->desc_next, |
| 421 | vring->desc_in_use); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 422 | if (vif->is_packed) |
| 423 | { |
| 424 | vlib_cli_output (vm, |
| 425 | " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d", |
| 426 | vring->driver_event->flags, |
| 427 | vring->driver_event->off_wrap, |
| 428 | vring->device_event->flags, |
| 429 | vring->device_event->off_wrap); |
| 430 | vlib_cli_output (vm, |
| 431 | " avail wrap counter %d, used wrap counter %d", |
| 432 | vring->avail_wrap_counter, |
| 433 | vring->used_wrap_counter); |
| 434 | } |
| 435 | else |
| 436 | vlib_cli_output (vm, |
| 437 | " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d", |
| 438 | vring->avail->flags, vring->avail->idx, |
| 439 | vring->used->flags, vring->used->idx); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 440 | if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 441 | { |
| 442 | vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd, |
| 443 | vring->call_fd); |
| 444 | } |
| 445 | if (show_descr) |
| 446 | { |
| 447 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 448 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 449 | " id addr len flags next/id user_addr\n"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 450 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 451 | " ===== ================== ===== ====== ======= ==================\n"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 452 | for (j = 0; j < vring->size; j++) |
| 453 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 454 | if (vif->is_packed) |
| 455 | { |
| 456 | vring_packed_desc_t *desc = &vring->packed_desc[j]; |
| 457 | vlib_cli_output (vm, |
| 458 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 459 | j, desc->addr, |
| 460 | desc->len, |
| 461 | desc->flags, desc->id, desc->addr); |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | vring_desc_t *desc = &vring->desc[j]; |
| 466 | vlib_cli_output (vm, |
| 467 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 468 | j, desc->addr, |
| 469 | desc->len, |
| 470 | desc->flags, desc->next, desc->addr); |
| 471 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 475 | vec_foreach_index (i, vif->txq_vrings) |
| 476 | { |
| 477 | vring = vec_elt_at_index (vif->txq_vrings, i); |
| 478 | vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id); |
| 479 | vlib_cli_output (vm, |
| 480 | " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", |
| 481 | vring->size, vring->last_used_idx, vring->desc_next, |
| 482 | vring->desc_in_use); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 483 | if (vif->is_packed) |
| 484 | { |
| 485 | vlib_cli_output (vm, |
| 486 | " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d", |
| 487 | vring->driver_event->flags, |
| 488 | vring->driver_event->off_wrap, |
| 489 | vring->device_event->flags, |
| 490 | vring->device_event->off_wrap); |
| 491 | vlib_cli_output (vm, |
| 492 | " avail wrap counter %d, used wrap counter %d", |
| 493 | vring->avail_wrap_counter, |
| 494 | vring->used_wrap_counter); |
| 495 | } |
| 496 | else |
| 497 | vlib_cli_output (vm, |
| 498 | " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d", |
| 499 | vring->avail->flags, vring->avail->idx, |
| 500 | vring->used->flags, vring->used->idx); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 501 | if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN)) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 502 | { |
| 503 | vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd, |
| 504 | vring->call_fd); |
| 505 | } |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 506 | if (vring->flow_table) |
| 507 | { |
| 508 | vlib_cli_output (vm, " %U", gro_flow_table_format, |
| 509 | vring->flow_table); |
| 510 | } |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 511 | if (vif->packet_buffering) |
| 512 | { |
| 513 | vlib_cli_output (vm, " %U", virtio_vring_buffering_format, |
| 514 | vring->buffering); |
| 515 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 516 | if (show_descr) |
| 517 | { |
| 518 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 519 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 520 | " id addr len flags next/id user_addr\n"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 521 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 522 | " ===== ================== ===== ====== ======== ==================\n"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 523 | for (j = 0; j < vring->size; j++) |
| 524 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 525 | if (vif->is_packed) |
| 526 | { |
| 527 | vring_packed_desc_t *desc = &vring->packed_desc[j]; |
| 528 | vlib_cli_output (vm, |
| 529 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 530 | j, desc->addr, |
| 531 | desc->len, |
| 532 | desc->flags, desc->id, desc->addr); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | vring_desc_t *desc = &vring->desc[j]; |
| 537 | vlib_cli_output (vm, |
| 538 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 539 | j, desc->addr, |
| 540 | desc->len, |
| 541 | desc->flags, desc->next, desc->addr); |
| 542 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
Mohsin Kazmi | e4efbe7 | 2021-09-22 18:56:05 +0000 | [diff] [blame] | 546 | if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL && |
| 547 | vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 548 | { |
| 549 | vring = vif->cxq_vring; |
| 550 | vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id); |
| 551 | vlib_cli_output (vm, |
| 552 | " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", |
| 553 | vring->size, vring->last_used_idx, |
| 554 | vring->desc_next, vring->desc_in_use); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 555 | if (vif->is_packed) |
| 556 | { |
| 557 | vlib_cli_output (vm, |
| 558 | " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d", |
| 559 | vring->driver_event->flags, |
| 560 | vring->driver_event->off_wrap, |
| 561 | vring->device_event->flags, |
| 562 | vring->device_event->off_wrap); |
| 563 | vlib_cli_output (vm, |
| 564 | " avail wrap counter %d, used wrap counter %d", |
| 565 | vring->avail_wrap_counter, |
| 566 | vring->used_wrap_counter); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | vlib_cli_output (vm, |
| 571 | " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d", |
| 572 | vring->avail->flags, vring->avail->idx, |
| 573 | vring->used->flags, vring->used->idx); |
| 574 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 575 | if (show_descr) |
| 576 | { |
| 577 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 578 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 579 | " id addr len flags next/id user_addr\n"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 580 | vlib_cli_output (vm, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 581 | " ===== ================== ===== ====== ======== ==================\n"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 582 | for (j = 0; j < vring->size; j++) |
| 583 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 584 | if (vif->is_packed) |
| 585 | { |
| 586 | vring_packed_desc_t *desc = &vring->packed_desc[j]; |
| 587 | vlib_cli_output (vm, |
| 588 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 589 | j, desc->addr, |
| 590 | desc->len, |
| 591 | desc->flags, desc->id, desc->addr); |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | vring_desc_t *desc = &vring->desc[j]; |
| 596 | vlib_cli_output (vm, |
| 597 | " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", |
| 598 | j, desc->addr, |
| 599 | desc->len, |
| 600 | desc->flags, desc->next, desc->addr); |
| 601 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | } |
| 608 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 609 | static clib_error_t * |
| 610 | virtio_init (vlib_main_t * vm) |
| 611 | { |
| 612 | virtio_main_t *vim = &virtio_main; |
| 613 | clib_error_t *error = 0; |
| 614 | |
| 615 | vim->log_default = vlib_log_register_class ("virtio", 0); |
| 616 | vlib_log_debug (vim->log_default, "initialized"); |
| 617 | |
| 618 | return error; |
| 619 | } |
| 620 | |
| 621 | VLIB_INIT_FUNCTION (virtio_init); |
| 622 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 623 | /* |
| 624 | * fd.io coding-style-patch-verification: ON |
| 625 | * |
| 626 | * Local Variables: |
| 627 | * eval: (c-set-style "gnu") |
| 628 | * End: |
| 629 | */ |