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