Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * vhost.c - vhost-user |
| 4 | * |
| 5 | * Copyright (c) 2014-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 <fcntl.h> /* for open */ |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/uio.h> /* for iovec */ |
| 27 | #include <netinet/in.h> |
| 28 | #include <sys/vfs.h> |
| 29 | |
| 30 | #include <linux/if_arp.h> |
| 31 | #include <linux/if_tun.h> |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vlib/unix/unix.h> |
| 35 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 36 | #include <vnet/ethernet/ethernet.h> |
| 37 | #include <vnet/devices/devices.h> |
| 38 | #include <vnet/feature/feature.h> |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 39 | #include <vnet/interface/rx_queue_funcs.h> |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 40 | #include <vnet/interface/tx_queue_funcs.h> |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 41 | |
| 42 | #include <vnet/devices/virtio/vhost_user.h> |
| 43 | #include <vnet/devices/virtio/vhost_user_inline.h> |
| 44 | |
| 45 | /** |
| 46 | * @file |
| 47 | * @brief vHost User Device Driver. |
| 48 | * |
| 49 | * This file contains the source code for vHost User interface. |
| 50 | */ |
| 51 | |
| 52 | |
| 53 | vlib_node_registration_t vhost_user_send_interrupt_node; |
| 54 | |
| 55 | /* *INDENT-OFF* */ |
| 56 | vhost_user_main_t vhost_user_main = { |
| 57 | .mtu_bytes = 1518, |
| 58 | }; |
| 59 | |
| 60 | VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = { |
| 61 | .name = "vhost-user", |
| 62 | }; |
| 63 | /* *INDENT-ON* */ |
| 64 | |
| 65 | static long |
| 66 | get_huge_page_size (int fd) |
| 67 | { |
| 68 | struct statfs s; |
| 69 | fstatfs (fd, &s); |
| 70 | return s.f_bsize; |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | unmap_all_mem_regions (vhost_user_intf_t * vui) |
| 75 | { |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 76 | int i, r, q; |
| 77 | vhost_user_vring_t *vq; |
| 78 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 79 | for (i = 0; i < vui->nregions; i++) |
| 80 | { |
| 81 | if (vui->region_mmap_addr[i] != MAP_FAILED) |
| 82 | { |
| 83 | |
| 84 | long page_sz = get_huge_page_size (vui->region_mmap_fd[i]); |
| 85 | |
| 86 | ssize_t map_sz = (vui->regions[i].memory_size + |
| 87 | vui->regions[i].mmap_offset + |
| 88 | page_sz - 1) & ~(page_sz - 1); |
| 89 | |
| 90 | r = |
| 91 | munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, |
| 92 | map_sz); |
| 93 | |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 94 | vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx " |
| 95 | "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz, |
| 96 | page_sz); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 97 | |
| 98 | vui->region_mmap_addr[i] = MAP_FAILED; |
| 99 | |
| 100 | if (r == -1) |
| 101 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 102 | vu_log_err (vui, "failed to unmap memory region (errno %d)", |
| 103 | errno); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 104 | } |
| 105 | close (vui->region_mmap_fd[i]); |
| 106 | } |
| 107 | } |
| 108 | vui->nregions = 0; |
Yichen Wang | 28812a0 | 2018-08-28 23:05:27 -0700 | [diff] [blame] | 109 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 110 | FOR_ALL_VHOST_RX_TXQ (q, vui) |
| 111 | { |
| 112 | vq = &vui->vrings[q]; |
| 113 | vq->avail = 0; |
| 114 | vq->used = 0; |
| 115 | vq->desc = 0; |
| 116 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 119 | static_always_inline void |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 120 | vhost_user_tx_thread_placement (vhost_user_intf_t *vui, u32 qid) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 121 | { |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 122 | vnet_main_t *vnm = vnet_get_main (); |
| 123 | vhost_user_vring_t *rxvq = &vui->vrings[qid]; |
| 124 | u32 q = qid >> 1, rxvq_count; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 125 | |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 126 | ASSERT ((qid & 1) == 0); |
| 127 | if (!rxvq->started || !rxvq->enabled) |
| 128 | return; |
| 129 | |
| 130 | rxvq_count = (qid >> 1) + 1; |
| 131 | if (rxvq->queue_index == ~0) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 132 | { |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 133 | rxvq->queue_index = |
| 134 | vnet_hw_if_register_tx_queue (vnm, vui->hw_if_index, q); |
| 135 | rxvq->qid = q; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 136 | } |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 137 | |
| 138 | FOR_ALL_VHOST_RXQ (q, vui) |
| 139 | { |
| 140 | vhost_user_vring_t *rxvq = &vui->vrings[q]; |
| 141 | u32 qi = rxvq->queue_index; |
| 142 | |
| 143 | if (rxvq->queue_index == ~0) |
| 144 | break; |
| 145 | for (u32 i = 0; i < vlib_get_n_threads (); i++) |
| 146 | vnet_hw_if_tx_queue_unassign_thread (vnm, qi, i); |
| 147 | } |
| 148 | |
| 149 | for (u32 i = 0; i < vlib_get_n_threads (); i++) |
| 150 | { |
| 151 | vhost_user_vring_t *rxvq = |
| 152 | &vui->vrings[VHOST_VRING_IDX_RX (i % rxvq_count)]; |
| 153 | u32 qi = rxvq->queue_index; |
| 154 | |
| 155 | vnet_hw_if_tx_queue_assign_thread (vnm, qi, i); |
| 156 | } |
| 157 | |
| 158 | vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @brief Unassign existing interface/queue to thread mappings and re-assign |
| 163 | * new interface/queue to thread mappings |
| 164 | */ |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 165 | static_always_inline void |
| 166 | vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 167 | { |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 168 | vhost_user_vring_t *txvq = &vui->vrings[qid]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 169 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 170 | int rv; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 171 | u32 q = qid >> 1; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 172 | vhost_user_main_t *vum = &vhost_user_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 173 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 174 | ASSERT ((qid & 1) == 1); // should be odd |
| 175 | // Assign new queue mappings for the interface |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 176 | if (txvq->queue_index != ~0) |
| 177 | return; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 178 | vnet_hw_if_set_input_node (vnm, vui->hw_if_index, |
| 179 | vhost_user_input_node.index); |
| 180 | txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q, |
| 181 | VNET_HW_IF_RXQ_THREAD_ANY); |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 182 | txvq->thread_index = |
| 183 | vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index); |
| 184 | |
Damjan Marion | eabd424 | 2020-10-07 20:59:07 +0200 | [diff] [blame] | 185 | if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN) |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 186 | /* Set polling as the default */ |
Damjan Marion | eabd424 | 2020-10-07 20:59:07 +0200 | [diff] [blame] | 187 | txvq->mode = VNET_HW_IF_RX_MODE_POLLING; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 188 | if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) |
| 189 | { |
| 190 | vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index); |
| 191 | /* Keep a polling queue count for each thread */ |
| 192 | cpu->polling_q_count++; |
| 193 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 194 | txvq->qid = q; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 195 | rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 196 | if (rv) |
| 197 | vu_log_warn (vui, "unable to set rx mode for interface %d, " |
| 198 | "queue %d: rc=%d", vui->hw_if_index, q, rv); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 199 | vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** @brief Returns whether at least one TX and one RX vring are enabled */ |
| 203 | static_always_inline int |
| 204 | vhost_user_intf_ready (vhost_user_intf_t * vui) |
| 205 | { |
| 206 | int i, found[2] = { }; //RX + TX |
| 207 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 208 | for (i = 0; i < vui->num_qid; i++) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 209 | if (vui->vrings[i].started && vui->vrings[i].enabled) |
| 210 | found[i & 1] = 1; |
| 211 | |
| 212 | return found[0] && found[1]; |
| 213 | } |
| 214 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 215 | static_always_inline void |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 216 | vhost_user_update_iface_state (vhost_user_intf_t * vui) |
| 217 | { |
| 218 | /* if we have pointers to descriptor table, go up */ |
Juraj Sloboda | b192feb | 2018-10-01 12:42:07 +0200 | [diff] [blame] | 219 | int is_ready = vhost_user_intf_ready (vui); |
| 220 | if (is_ready != vui->is_ready) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 221 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 222 | vu_log_debug (vui, "interface %d %s", vui->sw_if_index, |
| 223 | is_ready ? "ready" : "down"); |
Juraj Sloboda | b192feb | 2018-10-01 12:42:07 +0200 | [diff] [blame] | 224 | if (vui->admin_up) |
| 225 | vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index, |
| 226 | is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP |
| 227 | : 0); |
| 228 | vui->is_ready = is_ready; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 229 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 232 | static clib_error_t * |
| 233 | vhost_user_callfd_read_ready (clib_file_t * uf) |
| 234 | { |
| 235 | __attribute__ ((unused)) int n; |
| 236 | u8 buff[8]; |
| 237 | |
| 238 | n = read (uf->file_descriptor, ((char *) &buff), 8); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 243 | static_always_inline void |
| 244 | vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid) |
| 245 | { |
| 246 | if (qid & 1) // RX is odd, TX is even |
| 247 | { |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 248 | if (vui->vrings[qid].queue_index == ~0) |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 249 | vhost_user_rx_thread_placement (vui, qid); |
| 250 | } |
| 251 | else |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 252 | vhost_user_tx_thread_placement (vui, qid); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 255 | static clib_error_t * |
| 256 | vhost_user_kickfd_read_ready (clib_file_t * uf) |
| 257 | { |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 258 | __attribute__ ((unused)) ssize_t n; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 259 | u8 buff[8]; |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 260 | vhost_user_main_t *vum = &vhost_user_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 261 | vhost_user_intf_t *vui = |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 262 | pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 263 | u32 qid = uf->private_data & 0xff; |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 264 | u32 is_txq = qid & 1; |
| 265 | vhost_user_vring_t *vq = &vui->vrings[qid]; |
| 266 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 267 | |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 268 | n = read (uf->file_descriptor, buff, 8); |
| 269 | if (vq->started == 0) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 270 | { |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 271 | vq->started = 1; |
| 272 | vhost_user_thread_placement (vui, qid); |
| 273 | vhost_user_update_iface_state (vui); |
| 274 | if (is_txq) |
| 275 | vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index, |
| 276 | vq->kickfd_idx); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 279 | if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) && |
| 280 | vhost_user_intf_ready (vui)) |
| 281 | { |
| 282 | vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index); |
| 283 | /* |
| 284 | * If the thread has more than 1 queue and the other queue is in polling |
| 285 | * mode, there is no need to trigger an interrupt |
| 286 | */ |
| 287 | if (cpu->polling_q_count == 0) |
| 288 | vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index); |
| 289 | } |
Steven Luong | 27e93a5 | 2021-05-06 10:00:30 -0700 | [diff] [blame] | 290 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static_always_inline void |
| 295 | vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid) |
| 296 | { |
| 297 | vhost_user_vring_t *vring = &vui->vrings[qid]; |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 298 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 299 | clib_memset (vring, 0, sizeof (*vring)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 300 | vring->kickfd_idx = ~0; |
| 301 | vring->callfd_idx = ~0; |
| 302 | vring->errfd = -1; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 303 | vring->qid = -1; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 304 | vring->queue_index = ~0; |
| 305 | vring->thread_index = ~0; |
| 306 | vring->mode = VNET_HW_IF_RX_MODE_POLLING; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 307 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 308 | clib_spinlock_init (&vring->vring_lock); |
| 309 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 310 | /* |
| 311 | * We have a bug with some qemu 2.5, and this may be a fix. |
| 312 | * Feel like interpretation holy text, but this is from vhost-user.txt. |
| 313 | * " |
| 314 | * One queue pair is enabled initially. More queues are enabled |
| 315 | * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE. |
| 316 | * " |
| 317 | * Don't know who's right, but this is what DPDK does. |
| 318 | */ |
| 319 | if (qid == 0 || qid == 1) |
| 320 | vring->enabled = 1; |
| 321 | } |
| 322 | |
| 323 | static_always_inline void |
| 324 | vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid) |
| 325 | { |
| 326 | vhost_user_vring_t *vring = &vui->vrings[qid]; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 327 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 328 | if (vring->kickfd_idx != ~0) |
| 329 | { |
| 330 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
| 331 | vring->kickfd_idx); |
| 332 | clib_file_del (&file_main, uf); |
| 333 | vring->kickfd_idx = ~0; |
| 334 | } |
| 335 | if (vring->callfd_idx != ~0) |
| 336 | { |
| 337 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
| 338 | vring->callfd_idx); |
| 339 | clib_file_del (&file_main, uf); |
| 340 | vring->callfd_idx = ~0; |
| 341 | } |
| 342 | if (vring->errfd != -1) |
| 343 | { |
| 344 | close (vring->errfd); |
| 345 | vring->errfd = -1; |
| 346 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 347 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 348 | clib_spinlock_free (&vring->vring_lock); |
| 349 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 350 | // save the needed information in vrings prior to being wiped out |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 351 | u16 q = vui->vrings[qid].qid; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 352 | u32 queue_index = vui->vrings[qid].queue_index; |
| 353 | u32 mode = vui->vrings[qid].mode; |
| 354 | u32 thread_index = vui->vrings[qid].thread_index; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 355 | vhost_user_vring_init (vui, qid); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 356 | vui->vrings[qid].qid = q; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 357 | vui->vrings[qid].queue_index = queue_index; |
| 358 | vui->vrings[qid].mode = mode; |
| 359 | vui->vrings[qid].thread_index = thread_index; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static_always_inline void |
| 363 | vhost_user_if_disconnect (vhost_user_intf_t * vui) |
| 364 | { |
| 365 | vnet_main_t *vnm = vnet_get_main (); |
| 366 | int q; |
| 367 | |
| 368 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
| 369 | |
| 370 | if (vui->clib_file_index != ~0) |
| 371 | { |
| 372 | clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); |
| 373 | vui->clib_file_index = ~0; |
| 374 | } |
| 375 | |
Juraj Sloboda | b192feb | 2018-10-01 12:42:07 +0200 | [diff] [blame] | 376 | vui->is_ready = 0; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 377 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 378 | FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 379 | |
| 380 | unmap_all_mem_regions (vui); |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 381 | vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 384 | void |
| 385 | vhost_user_set_operation_mode (vhost_user_intf_t *vui, |
| 386 | vhost_user_vring_t *txvq) |
| 387 | { |
| 388 | if (vhost_user_is_packed_ring_supported (vui)) |
| 389 | { |
| 390 | if (txvq->used_event) |
| 391 | { |
| 392 | if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) |
| 393 | txvq->used_event->flags = VRING_EVENT_F_DISABLE; |
| 394 | else |
| 395 | txvq->used_event->flags = 0; |
| 396 | } |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | if (txvq->used) |
| 401 | { |
| 402 | if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) |
| 403 | txvq->used->flags = VRING_USED_F_NO_NOTIFY; |
| 404 | else |
| 405 | txvq->used->flags = 0; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 410 | static clib_error_t * |
| 411 | vhost_user_socket_read (clib_file_t * uf) |
| 412 | { |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 413 | int n, i, j; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 414 | int fd, number_of_fds = 0; |
| 415 | int fds[VHOST_MEMORY_MAX_NREGIONS]; |
| 416 | vhost_user_msg_t msg; |
| 417 | struct msghdr mh; |
| 418 | struct iovec iov[1]; |
| 419 | vhost_user_main_t *vum = &vhost_user_main; |
| 420 | vhost_user_intf_t *vui; |
| 421 | struct cmsghdr *cmsg; |
| 422 | u8 q; |
| 423 | clib_file_t template = { 0 }; |
| 424 | vnet_main_t *vnm = vnet_get_main (); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 425 | vlib_main_t *vm = vlib_get_main (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 426 | |
| 427 | vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
| 428 | |
| 429 | char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))]; |
| 430 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 431 | clib_memset (&mh, 0, sizeof (mh)); |
| 432 | clib_memset (control, 0, sizeof (control)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 433 | |
| 434 | for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++) |
| 435 | fds[i] = -1; |
| 436 | |
| 437 | /* set the payload */ |
| 438 | iov[0].iov_base = (void *) &msg; |
| 439 | iov[0].iov_len = VHOST_USER_MSG_HDR_SZ; |
| 440 | |
| 441 | mh.msg_iov = iov; |
| 442 | mh.msg_iovlen = 1; |
| 443 | mh.msg_control = control; |
| 444 | mh.msg_controllen = sizeof (control); |
| 445 | |
| 446 | n = recvmsg (uf->file_descriptor, &mh, 0); |
| 447 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 448 | if (n != VHOST_USER_MSG_HDR_SZ) |
| 449 | { |
| 450 | if (n == -1) |
| 451 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 452 | vu_log_debug (vui, "recvmsg returned error %d %s", errno, |
| 453 | strerror (errno)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 454 | } |
| 455 | else |
| 456 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 457 | vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)", |
| 458 | n, VHOST_USER_MSG_HDR_SZ); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 459 | } |
| 460 | goto close_socket; |
| 461 | } |
| 462 | |
| 463 | if (mh.msg_flags & MSG_CTRUNC) |
| 464 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 465 | vu_log_debug (vui, "MSG_CTRUNC is set"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 466 | goto close_socket; |
| 467 | } |
| 468 | |
| 469 | cmsg = CMSG_FIRSTHDR (&mh); |
| 470 | |
| 471 | if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) && |
| 472 | (cmsg->cmsg_type == SCM_RIGHTS) && |
| 473 | (cmsg->cmsg_len - CMSG_LEN (0) <= |
| 474 | VHOST_MEMORY_MAX_NREGIONS * sizeof (int))) |
| 475 | { |
| 476 | number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 477 | clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* version 1, no reply bit set */ |
| 481 | if ((msg.flags & 7) != 1) |
| 482 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 483 | vu_log_debug (vui, "malformed message received. closing socket"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 484 | goto close_socket; |
| 485 | } |
| 486 | |
| 487 | { |
| 488 | int rv; |
| 489 | rv = |
| 490 | read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ, |
| 491 | msg.size); |
| 492 | if (rv < 0) |
| 493 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 494 | vu_log_debug (vui, "read failed %s", strerror (errno)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 495 | goto close_socket; |
| 496 | } |
| 497 | else if (rv != msg.size) |
| 498 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 499 | vu_log_debug (vui, "message too short (read %dB should be %dB)", rv, |
| 500 | msg.size); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 501 | goto close_socket; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | switch (msg.request) |
| 506 | { |
| 507 | case VHOST_USER_GET_FEATURES: |
| 508 | msg.flags |= 4; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 509 | msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) | |
| 510 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) | |
| 511 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) | |
| 512 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) | |
| 513 | VIRTIO_FEATURE (VHOST_F_LOG_ALL) | |
| 514 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) | |
| 515 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) | |
| 516 | VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) | |
| 517 | VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 518 | msg.u64 &= vui->feature_mask; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 519 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 520 | if (vui->enable_event_idx) |
| 521 | msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 522 | if (vui->enable_gso) |
| 523 | msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 524 | if (vui->enable_packed) |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 525 | msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 526 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 527 | msg.size = sizeof (msg.u64); |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 528 | vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply " |
| 529 | "0x%016llx", vui->hw_if_index, msg.u64); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 530 | n = |
| 531 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
| 532 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
| 533 | { |
| 534 | vu_log_debug (vui, "could not send message response"); |
| 535 | goto close_socket; |
| 536 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | case VHOST_USER_SET_FEATURES: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 540 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features " |
| 541 | "0x%016llx", vui->hw_if_index, msg.u64); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 542 | |
| 543 | vui->features = msg.u64; |
| 544 | |
| 545 | if (vui->features & |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 546 | (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) | |
| 547 | VIRTIO_FEATURE (VIRTIO_F_VERSION_1))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 548 | vui->virtio_net_hdr_sz = 12; |
| 549 | else |
| 550 | vui->virtio_net_hdr_sz = 10; |
| 551 | |
| 552 | vui->is_any_layout = |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 553 | (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 554 | |
| 555 | ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 556 | if (vui->enable_gso && |
Steven Luong | a75ad87 | 2019-08-06 21:51:34 -0700 | [diff] [blame] | 557 | ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) |
| 558 | == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)) |
Mohsin Kazmi | 5b3f523 | 2021-02-10 12:03:53 +0100 | [diff] [blame] | 559 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame^] | 560 | vnet_hw_if_set_caps (vnm, vui->hw_if_index, |
| 561 | VNET_HW_IF_CAP_TCP_GSO | |
| 562 | VNET_HW_IF_CAP_TX_TCP_CKSUM | |
| 563 | VNET_HW_IF_CAP_TX_UDP_CKSUM); |
Mohsin Kazmi | 5b3f523 | 2021-02-10 12:03:53 +0100 | [diff] [blame] | 564 | } |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 565 | else |
Mohsin Kazmi | 5b3f523 | 2021-02-10 12:03:53 +0100 | [diff] [blame] | 566 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame^] | 567 | vnet_hw_if_unset_caps (vnm, vui->hw_if_index, |
| 568 | VNET_HW_IF_CAP_TCP_GSO | |
| 569 | VNET_HW_IF_CAP_L4_TX_CKSUM); |
Mohsin Kazmi | 5b3f523 | 2021-02-10 12:03:53 +0100 | [diff] [blame] | 570 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 571 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
Juraj Sloboda | b192feb | 2018-10-01 12:42:07 +0200 | [diff] [blame] | 572 | vui->is_ready = 0; |
Steven Luong | 545866b | 2019-07-18 18:38:52 -0700 | [diff] [blame] | 573 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 574 | break; |
| 575 | |
| 576 | case VHOST_USER_SET_MEM_TABLE: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 577 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d", |
| 578 | vui->hw_if_index, msg.memory.nregions); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 579 | |
| 580 | if ((msg.memory.nregions < 1) || |
| 581 | (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) |
| 582 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 583 | vu_log_debug (vui, "number of mem regions must be between 1 and %i", |
| 584 | VHOST_MEMORY_MAX_NREGIONS); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 585 | goto close_socket; |
| 586 | } |
| 587 | |
| 588 | if (msg.memory.nregions != number_of_fds) |
| 589 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 590 | vu_log_debug (vui, "each memory region must have FD"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 591 | goto close_socket; |
| 592 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 593 | |
| 594 | /* Do the mmap without barrier sync */ |
| 595 | void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]; |
| 596 | for (i = 0; i < msg.memory.nregions; i++) |
| 597 | { |
| 598 | long page_sz = get_huge_page_size (fds[i]); |
| 599 | |
| 600 | /* align size to page */ |
| 601 | ssize_t map_sz = (msg.memory.regions[i].memory_size + |
| 602 | msg.memory.regions[i].mmap_offset + |
| 603 | page_sz - 1) & ~(page_sz - 1); |
| 604 | |
| 605 | region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE, |
| 606 | MAP_SHARED, fds[i], 0); |
| 607 | if (region_mmap_addr[i] == MAP_FAILED) |
| 608 | { |
| 609 | vu_log_err (vui, "failed to map memory. errno is %d", errno); |
| 610 | for (j = 0; j < i; j++) |
| 611 | munmap (region_mmap_addr[j], map_sz); |
| 612 | goto close_socket; |
| 613 | } |
| 614 | vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d " |
| 615 | "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i], |
| 616 | region_mmap_addr[i], page_sz); |
| 617 | } |
| 618 | |
| 619 | vlib_worker_thread_barrier_sync (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 620 | unmap_all_mem_regions (vui); |
| 621 | for (i = 0; i < msg.memory.nregions; i++) |
| 622 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 623 | clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i], |
| 624 | sizeof (vhost_user_memory_region_t)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 625 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 626 | vui->region_mmap_addr[i] = region_mmap_addr[i]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 627 | vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr; |
| 628 | vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr + |
| 629 | vui->regions[i].memory_size; |
| 630 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 631 | vui->region_mmap_addr[i] += vui->regions[i].mmap_offset; |
| 632 | vui->region_mmap_fd[i] = fds[i]; |
| 633 | |
| 634 | vui->nregions++; |
| 635 | } |
Steven Luong | 4442f7c | 2019-10-02 07:33:48 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * Re-compute desc, used, and avail descriptor table if vring address |
| 639 | * is set. |
| 640 | */ |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 641 | FOR_ALL_VHOST_RX_TXQ (q, vui) |
| 642 | { |
| 643 | if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr && |
| 644 | vui->vrings[q].avail_user_addr) |
| 645 | { |
| 646 | vui->vrings[q].desc = |
| 647 | map_user_mem (vui, vui->vrings[q].desc_user_addr); |
| 648 | vui->vrings[q].used = |
| 649 | map_user_mem (vui, vui->vrings[q].used_user_addr); |
| 650 | vui->vrings[q].avail = |
| 651 | map_user_mem (vui, vui->vrings[q].avail_user_addr); |
| 652 | } |
| 653 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 654 | vlib_worker_thread_barrier_release (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 655 | break; |
| 656 | |
| 657 | case VHOST_USER_SET_VRING_NUM: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 658 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d", |
| 659 | vui->hw_if_index, msg.state.index, msg.state.num); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 660 | |
| 661 | if ((msg.state.num > 32768) || /* maximum ring size is 32768 */ |
| 662 | (msg.state.num == 0) || /* it cannot be zero */ |
Benoît Ganne | 32526a4 | 2020-12-08 19:08:05 +0100 | [diff] [blame] | 663 | ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */ |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 664 | (msg.state.index >= vui->num_qid)) |
| 665 | { |
| 666 | vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num" |
| 667 | " %d, msg.state.index %d, curruent max q %d", |
| 668 | msg.state.num, msg.state.index, vui->num_qid); |
| 669 | goto close_socket; |
| 670 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 671 | vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1; |
| 672 | break; |
| 673 | |
| 674 | case VHOST_USER_SET_VRING_ADDR: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 675 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d", |
| 676 | vui->hw_if_index, msg.state.index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 677 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 678 | if (msg.state.index >= vui->num_qid) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 679 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 680 | vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:" |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 681 | " %u >= %u", msg.state.index, vui->num_qid); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 682 | goto close_socket; |
| 683 | } |
| 684 | |
| 685 | if (msg.size < sizeof (msg.addr)) |
| 686 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 687 | vu_log_debug (vui, "vhost message is too short (%d < %d)", |
| 688 | msg.size, sizeof (msg.addr)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 689 | goto close_socket; |
| 690 | } |
| 691 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 692 | vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr); |
| 693 | vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr); |
| 694 | vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 695 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 696 | if ((desc == NULL) || (used == NULL) || (avail == NULL)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 697 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 698 | vu_log_debug (vui, "failed to map user memory for hw_if_index %d", |
| 699 | vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 700 | goto close_socket; |
| 701 | } |
| 702 | |
Steven Luong | 4442f7c | 2019-10-02 07:33:48 -0700 | [diff] [blame] | 703 | vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr; |
| 704 | vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr; |
| 705 | vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr; |
| 706 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 707 | vlib_worker_thread_barrier_sync (vm); |
| 708 | vui->vrings[msg.state.index].desc = desc; |
| 709 | vui->vrings[msg.state.index].used = used; |
| 710 | vui->vrings[msg.state.index].avail = avail; |
| 711 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 712 | vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr; |
| 713 | vui->vrings[msg.state.index].log_used = |
| 714 | (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0; |
| 715 | |
| 716 | /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated, |
| 717 | the ring is initialized in an enabled state. */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 718 | if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES))) |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 719 | vui->vrings[msg.state.index].enabled = 1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 720 | |
| 721 | vui->vrings[msg.state.index].last_used_idx = |
| 722 | vui->vrings[msg.state.index].last_avail_idx = |
| 723 | vui->vrings[msg.state.index].used->idx; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 724 | vui->vrings[msg.state.index].last_kick = |
| 725 | vui->vrings[msg.state.index].last_used_idx; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 726 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 727 | /* tell driver that we want interrupts or not */ |
| 728 | vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 729 | vlib_worker_thread_barrier_release (vm); |
| 730 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 731 | break; |
| 732 | |
| 733 | case VHOST_USER_SET_OWNER: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 734 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 735 | break; |
| 736 | |
| 737 | case VHOST_USER_RESET_OWNER: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 738 | vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER", |
| 739 | vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 740 | break; |
| 741 | |
| 742 | case VHOST_USER_SET_VRING_CALL: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 743 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d", |
| 744 | vui->hw_if_index, msg.u64); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 745 | |
| 746 | q = (u8) (msg.u64 & 0xFF); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 747 | if (vui->num_qid > q) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 748 | { |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 749 | /* if there is old fd, delete and close it */ |
| 750 | if (vui->vrings[q].callfd_idx != ~0) |
| 751 | { |
| 752 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
| 753 | vui->vrings[q].callfd_idx); |
| 754 | clib_file_del (&file_main, uf); |
| 755 | vui->vrings[q].callfd_idx = ~0; |
| 756 | } |
| 757 | } |
| 758 | else if (vec_len (vui->vrings) > q) |
| 759 | { |
| 760 | /* grow vrings by pair (RX + TX) */ |
| 761 | vui->num_qid = (q & 1) ? (q + 1) : (q + 2); |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | u32 i, new_max_q, old_max_q = vec_len (vui->vrings); |
| 766 | |
| 767 | /* |
| 768 | * Double the array size if it is less than 64 entries. |
| 769 | * Slow down thereafter. |
| 770 | */ |
| 771 | if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3)) |
| 772 | new_max_q = vec_len (vui->vrings) << 1; |
| 773 | else |
| 774 | new_max_q = vec_len (vui->vrings) + |
| 775 | (VHOST_VRING_INIT_MQ_PAIR_SZ << 2); |
| 776 | if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1)) |
| 777 | new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1); |
| 778 | |
| 779 | /* sync with the worker threads, vrings may move due to realloc */ |
| 780 | vlib_worker_thread_barrier_sync (vm); |
| 781 | vec_validate_aligned (vui->vrings, new_max_q - 1, |
| 782 | CLIB_CACHE_LINE_BYTES); |
| 783 | vlib_worker_thread_barrier_release (vm); |
| 784 | |
| 785 | for (i = old_max_q; i < vec_len (vui->vrings); i++) |
| 786 | vhost_user_vring_init (vui, i); |
| 787 | |
| 788 | /* grow vrings by pair (RX + TX) */ |
| 789 | vui->num_qid = (q & 1) ? (q + 1) : (q + 2); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
| 793 | { |
| 794 | if (number_of_fds != 1) |
| 795 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 796 | vu_log_debug (vui, "More than one fd received !"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 797 | goto close_socket; |
| 798 | } |
| 799 | |
| 800 | template.read_function = vhost_user_callfd_read_ready; |
| 801 | template.file_descriptor = fds[0]; |
| 802 | template.private_data = |
| 803 | ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q; |
Paul Vinciguerra | 5481ad4 | 2020-01-28 14:47:17 -0500 | [diff] [blame] | 804 | template.description = format (0, "vhost user"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 805 | vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template); |
| 806 | } |
| 807 | else |
| 808 | vui->vrings[q].callfd_idx = ~0; |
| 809 | break; |
| 810 | |
| 811 | case VHOST_USER_SET_VRING_KICK: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 812 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d", |
| 813 | vui->hw_if_index, msg.u64); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 814 | |
| 815 | q = (u8) (msg.u64 & 0xFF); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 816 | if (q >= vui->num_qid) |
| 817 | { |
| 818 | vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:" |
| 819 | " %u >= %u", q, vui->num_qid); |
| 820 | goto close_socket; |
| 821 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 822 | |
| 823 | if (vui->vrings[q].kickfd_idx != ~0) |
| 824 | { |
| 825 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
| 826 | vui->vrings[q].kickfd_idx); |
| 827 | clib_file_del (&file_main, uf); |
| 828 | vui->vrings[q].kickfd_idx = ~0; |
| 829 | } |
| 830 | |
| 831 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
| 832 | { |
| 833 | if (number_of_fds != 1) |
| 834 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 835 | vu_log_debug (vui, "More than one fd received !"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 836 | goto close_socket; |
| 837 | } |
| 838 | |
| 839 | template.read_function = vhost_user_kickfd_read_ready; |
| 840 | template.file_descriptor = fds[0]; |
| 841 | template.private_data = |
| 842 | (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) + |
| 843 | q; |
| 844 | vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template); |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | //When no kickfd is set, the queue is initialized as started |
| 849 | vui->vrings[q].kickfd_idx = ~0; |
| 850 | vui->vrings[q].started = 1; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 851 | vhost_user_thread_placement (vui, q); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 852 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 853 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 854 | break; |
| 855 | |
| 856 | case VHOST_USER_SET_VRING_ERR: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 857 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d", |
| 858 | vui->hw_if_index, msg.u64); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 859 | |
| 860 | q = (u8) (msg.u64 & 0xFF); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 861 | if (q >= vui->num_qid) |
| 862 | { |
| 863 | vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:" |
| 864 | " %u >= %u", q, vui->num_qid); |
| 865 | goto close_socket; |
| 866 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 867 | |
| 868 | if (vui->vrings[q].errfd != -1) |
| 869 | close (vui->vrings[q].errfd); |
| 870 | |
| 871 | if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) |
| 872 | { |
| 873 | if (number_of_fds != 1) |
| 874 | goto close_socket; |
| 875 | |
| 876 | vui->vrings[q].errfd = fds[0]; |
| 877 | } |
| 878 | else |
| 879 | vui->vrings[q].errfd = -1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 880 | break; |
| 881 | |
| 882 | case VHOST_USER_SET_VRING_BASE: |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 883 | vu_log_debug (vui, |
| 884 | "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x", |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 885 | vui->hw_if_index, msg.state.index, msg.state.num); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 886 | if (msg.state.index >= vui->num_qid) |
| 887 | { |
| 888 | vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:" |
| 889 | " %u >= %u", msg.state.index, vui->num_qid); |
| 890 | goto close_socket; |
| 891 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 892 | vlib_worker_thread_barrier_sync (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 893 | vui->vrings[msg.state.index].last_avail_idx = msg.state.num; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 894 | if (vhost_user_is_packed_ring_supported (vui)) |
| 895 | { |
| 896 | /* |
| 897 | * 0 1 2 3 |
| 898 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 899 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 900 | * | last avail idx | | last used idx | | |
| 901 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 902 | * ^ ^ |
| 903 | * | | |
| 904 | * avail wrap counter used wrap counter |
| 905 | */ |
| 906 | /* last avail idx at bit 0-14. */ |
| 907 | vui->vrings[msg.state.index].last_avail_idx = |
| 908 | msg.state.num & 0x7fff; |
| 909 | /* avail wrap counter at bit 15 */ |
| 910 | vui->vrings[msg.state.index].avail_wrap_counter = |
| 911 | ! !(msg.state.num & (1 << 15)); |
| 912 | |
| 913 | /* |
| 914 | * Although last_used_idx is passed in the upper 16 bits in qemu |
| 915 | * implementation, in practice, last_avail_idx and last_used_idx are |
| 916 | * usually the same. As a result, DPDK does not bother to pass us |
| 917 | * last_used_idx. The spec is not clear on thex coding. I figured it |
| 918 | * out by reading the qemu code. So let's just read last_avail_idx |
| 919 | * and set last_used_idx equals to last_avail_idx. |
| 920 | */ |
| 921 | vui->vrings[msg.state.index].last_used_idx = |
| 922 | vui->vrings[msg.state.index].last_avail_idx; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 923 | vui->vrings[msg.state.index].last_kick = |
| 924 | vui->vrings[msg.state.index].last_used_idx; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 925 | vui->vrings[msg.state.index].used_wrap_counter = |
| 926 | vui->vrings[msg.state.index].avail_wrap_counter; |
| 927 | |
| 928 | if (vui->vrings[msg.state.index].avail_wrap_counter == 1) |
| 929 | vui->vrings[msg.state.index].avail_wrap_counter = |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 930 | VRING_DESC_F_AVAIL; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 931 | } |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 932 | vlib_worker_thread_barrier_release (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 933 | break; |
| 934 | |
| 935 | case VHOST_USER_GET_VRING_BASE: |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 936 | if (msg.state.index >= vui->num_qid) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 937 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 938 | vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:" |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 939 | " %u >= %u", msg.state.index, vui->num_qid); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 940 | goto close_socket; |
| 941 | } |
| 942 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 943 | /* protection is needed to prevent rx/tx from changing last_avail_idx */ |
| 944 | vlib_worker_thread_barrier_sync (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 945 | /* |
| 946 | * Copy last_avail_idx from the vring before closing it because |
| 947 | * closing the vring also initializes the vring last_avail_idx |
| 948 | */ |
| 949 | msg.state.num = vui->vrings[msg.state.index].last_avail_idx; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 950 | if (vhost_user_is_packed_ring_supported (vui)) |
| 951 | { |
| 952 | msg.state.num = |
| 953 | (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) | |
| 954 | (! !vui->vrings[msg.state.index].avail_wrap_counter << 15); |
| 955 | msg.state.num |= |
| 956 | ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) | |
| 957 | (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16; |
| 958 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 959 | msg.flags |= 4; |
| 960 | msg.size = sizeof (msg.state); |
| 961 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 962 | /* |
| 963 | * Spec says: Client must [...] stop ring upon receiving |
| 964 | * VHOST_USER_GET_VRING_BASE |
| 965 | */ |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 966 | vhost_user_vring_close (vui, msg.state.index); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 967 | vlib_worker_thread_barrier_release (vm); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 968 | vu_log_debug (vui, |
| 969 | "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x", |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 970 | vui->hw_if_index, msg.state.index, msg.state.num); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 971 | n = |
| 972 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
| 973 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
| 974 | { |
| 975 | vu_log_debug (vui, "could not send message response"); |
| 976 | goto close_socket; |
| 977 | } |
| 978 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 979 | break; |
| 980 | |
| 981 | case VHOST_USER_NONE: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 982 | vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 983 | break; |
| 984 | |
| 985 | case VHOST_USER_SET_LOG_BASE: |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 986 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE", |
| 987 | vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 988 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 989 | if (msg.size != sizeof (msg.log)) |
| 990 | { |
| 991 | vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:" |
| 992 | " %d instead of %d", msg.size, sizeof (msg.log)); |
| 993 | goto close_socket; |
| 994 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 995 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 996 | if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) |
| 997 | { |
| 998 | vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but " |
| 999 | "VHOST_USER_SET_LOG_BASE received"); |
| 1000 | goto close_socket; |
| 1001 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1002 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1003 | fd = fds[0]; |
| 1004 | /* align size to page */ |
| 1005 | long page_sz = get_huge_page_size (fd); |
| 1006 | ssize_t map_sz = |
| 1007 | (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1008 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1009 | void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE, |
| 1010 | MAP_SHARED, fd, 0); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1011 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1012 | vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d " |
| 1013 | "mapped 0x%lx", map_sz, msg.log.offset, fd, |
| 1014 | log_base_addr); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1015 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1016 | if (log_base_addr == MAP_FAILED) |
| 1017 | { |
| 1018 | vu_log_err (vui, "failed to map memory. errno is %d", errno); |
| 1019 | goto close_socket; |
| 1020 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1021 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1022 | vlib_worker_thread_barrier_sync (vm); |
| 1023 | vui->log_base_addr = log_base_addr; |
| 1024 | vui->log_base_addr += msg.log.offset; |
| 1025 | vui->log_size = msg.log.size; |
| 1026 | vlib_worker_thread_barrier_release (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1027 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1028 | msg.flags |= 4; |
| 1029 | msg.size = sizeof (msg.u64); |
| 1030 | n = |
| 1031 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
| 1032 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
| 1033 | { |
| 1034 | vu_log_debug (vui, "could not send message response"); |
| 1035 | goto close_socket; |
| 1036 | } |
| 1037 | break; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1038 | |
| 1039 | case VHOST_USER_SET_LOG_FD: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1040 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1041 | break; |
| 1042 | |
| 1043 | case VHOST_USER_GET_PROTOCOL_FEATURES: |
| 1044 | msg.flags |= 4; |
| 1045 | msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | |
| 1046 | (1 << VHOST_USER_PROTOCOL_F_MQ); |
| 1047 | msg.size = sizeof (msg.u64); |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1048 | vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - " |
| 1049 | "reply 0x%016llx", vui->hw_if_index, msg.u64); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1050 | n = |
| 1051 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
| 1052 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
| 1053 | { |
| 1054 | vu_log_debug (vui, "could not send message response"); |
| 1055 | goto close_socket; |
| 1056 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1057 | break; |
| 1058 | |
| 1059 | case VHOST_USER_SET_PROTOCOL_FEATURES: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1060 | vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES " |
| 1061 | "features 0x%016llx", vui->hw_if_index, msg.u64); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1062 | vui->protocol_features = msg.u64; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1063 | break; |
| 1064 | |
| 1065 | case VHOST_USER_GET_QUEUE_NUM: |
| 1066 | msg.flags |= 4; |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1067 | msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1068 | msg.size = sizeof (msg.u64); |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1069 | vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d", |
| 1070 | vui->hw_if_index, msg.u64); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1071 | n = |
| 1072 | send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); |
| 1073 | if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) |
| 1074 | { |
| 1075 | vu_log_debug (vui, "could not send message response"); |
| 1076 | goto close_socket; |
| 1077 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1078 | break; |
| 1079 | |
| 1080 | case VHOST_USER_SET_VRING_ENABLE: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1081 | vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d", |
| 1082 | vui->hw_if_index, msg.state.num ? "enable" : "disable", |
| 1083 | msg.state.index); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1084 | if (msg.state.index >= vui->num_qid) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1085 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1086 | vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:" |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1087 | " %u >= %u", msg.state.index, vui->num_qid); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1088 | goto close_socket; |
| 1089 | } |
| 1090 | |
| 1091 | vui->vrings[msg.state.index].enabled = msg.state.num; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1092 | vhost_user_thread_placement (vui, msg.state.index); |
| 1093 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1094 | break; |
| 1095 | |
| 1096 | default: |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1097 | vu_log_debug (vui, "unknown vhost-user message %d received. " |
| 1098 | "closing socket", msg.request); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1099 | goto close_socket; |
| 1100 | } |
| 1101 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1102 | return 0; |
| 1103 | |
| 1104 | close_socket: |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1105 | vlib_worker_thread_barrier_sync (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1106 | vhost_user_if_disconnect (vui); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1107 | vlib_worker_thread_barrier_release (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1108 | vhost_user_update_iface_state (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static clib_error_t * |
| 1113 | vhost_user_socket_error (clib_file_t * uf) |
| 1114 | { |
| 1115 | vlib_main_t *vm = vlib_get_main (); |
| 1116 | vhost_user_main_t *vum = &vhost_user_main; |
| 1117 | vhost_user_intf_t *vui = |
| 1118 | pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
| 1119 | |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1120 | vu_log_debug (vui, "socket error on if %d", vui->sw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1121 | vlib_worker_thread_barrier_sync (vm); |
| 1122 | vhost_user_if_disconnect (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1123 | vlib_worker_thread_barrier_release (vm); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static clib_error_t * |
| 1128 | vhost_user_socksvr_accept_ready (clib_file_t * uf) |
| 1129 | { |
| 1130 | int client_fd, client_len; |
| 1131 | struct sockaddr_un client; |
| 1132 | clib_file_t template = { 0 }; |
| 1133 | vhost_user_main_t *vum = &vhost_user_main; |
| 1134 | vhost_user_intf_t *vui; |
| 1135 | |
| 1136 | vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); |
| 1137 | |
| 1138 | client_len = sizeof (client); |
| 1139 | client_fd = accept (uf->file_descriptor, |
| 1140 | (struct sockaddr *) &client, |
| 1141 | (socklen_t *) & client_len); |
| 1142 | |
| 1143 | if (client_fd < 0) |
| 1144 | return clib_error_return_unix (0, "accept"); |
| 1145 | |
| 1146 | if (vui->clib_file_index != ~0) |
| 1147 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1148 | vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d", |
| 1149 | vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index)); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1150 | clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); |
| 1151 | } |
| 1152 | |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1153 | vu_log_debug (vui, "New client socket for vhost interface %d, fd %d", |
| 1154 | vui->sw_if_index, client_fd); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1155 | template.read_function = vhost_user_socket_read; |
| 1156 | template.error_function = vhost_user_socket_error; |
| 1157 | template.file_descriptor = client_fd; |
| 1158 | template.private_data = vui - vhost_user_main.vhost_user_interfaces; |
Paul Vinciguerra | 5481ad4 | 2020-01-28 14:47:17 -0500 | [diff] [blame] | 1159 | template.description = format (0, "vhost interface %d", vui->sw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1160 | vui->clib_file_index = clib_file_add (&file_main, &template); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1161 | vui->num_qid = 2; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static clib_error_t * |
| 1166 | vhost_user_init (vlib_main_t * vm) |
| 1167 | { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1168 | vhost_user_main_t *vum = &vhost_user_main; |
| 1169 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 1170 | |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1171 | vum->log_default = vlib_log_register_class ("vhost-user", 0); |
| 1172 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1173 | vum->coalesce_frames = 32; |
| 1174 | vum->coalesce_time = 1e-3; |
| 1175 | |
| 1176 | vec_validate (vum->cpus, tm->n_vlib_mains - 1); |
| 1177 | |
| 1178 | vhost_cpu_t *cpu; |
| 1179 | vec_foreach (cpu, vum->cpus) |
| 1180 | { |
| 1181 | /* This is actually not necessary as validate already zeroes it |
| 1182 | * Just keeping the loop here for later because I am lazy. */ |
| 1183 | cpu->rx_buffers_len = 0; |
| 1184 | } |
| 1185 | |
| 1186 | vum->random = random_default_seed (); |
| 1187 | |
| 1188 | mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword)); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 1193 | /* *INDENT-OFF* */ |
| 1194 | VLIB_INIT_FUNCTION (vhost_user_init) = |
| 1195 | { |
| 1196 | .runs_after = VLIB_INITS("ip4_init"), |
| 1197 | }; |
| 1198 | /* *INDENT-ON* */ |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1199 | |
| 1200 | static uword |
| 1201 | vhost_user_send_interrupt_process (vlib_main_t * vm, |
| 1202 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 1203 | { |
| 1204 | vhost_user_intf_t *vui; |
| 1205 | f64 timeout = 3153600000.0 /* 100 years */ ; |
| 1206 | uword event_type, *event_data = 0; |
| 1207 | vhost_user_main_t *vum = &vhost_user_main; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1208 | u16 qid; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1209 | f64 now, poll_time_remaining; |
| 1210 | f64 next_timeout; |
| 1211 | u8 stop_timer = 0; |
| 1212 | |
| 1213 | while (1) |
| 1214 | { |
| 1215 | poll_time_remaining = |
| 1216 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 1217 | event_type = vlib_process_get_events (vm, &event_data); |
| 1218 | vec_reset_length (event_data); |
| 1219 | |
| 1220 | /* |
| 1221 | * Use the remaining timeout if it is less than coalesce time to avoid |
| 1222 | * resetting the existing timer in the middle of expiration |
| 1223 | */ |
| 1224 | timeout = poll_time_remaining; |
| 1225 | if (vlib_process_suspend_time_is_zero (timeout) || |
| 1226 | (timeout > vum->coalesce_time)) |
| 1227 | timeout = vum->coalesce_time; |
| 1228 | |
| 1229 | now = vlib_time_now (vm); |
| 1230 | switch (event_type) |
| 1231 | { |
| 1232 | case VHOST_USER_EVENT_STOP_TIMER: |
| 1233 | stop_timer = 1; |
| 1234 | break; |
| 1235 | |
| 1236 | case VHOST_USER_EVENT_START_TIMER: |
| 1237 | stop_timer = 0; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1238 | timeout = 1e-3; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1239 | if (!vlib_process_suspend_time_is_zero (poll_time_remaining)) |
| 1240 | break; |
| 1241 | /* fall through */ |
| 1242 | |
| 1243 | case ~0: |
| 1244 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1245 | pool_foreach (vui, vum->vhost_user_interfaces) { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1246 | next_timeout = timeout; |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1247 | FOR_ALL_VHOST_RX_TXQ (qid, vui) |
| 1248 | { |
| 1249 | vhost_user_vring_t *vq = &vui->vrings[qid]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1250 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1251 | if (vq->started == 0) |
| 1252 | continue; |
| 1253 | if (vq->n_since_last_int) |
| 1254 | { |
| 1255 | if (now >= vq->int_deadline) |
| 1256 | vhost_user_send_call (vm, vui, vq); |
| 1257 | else |
| 1258 | next_timeout = vq->int_deadline - now; |
| 1259 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1260 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1261 | if ((next_timeout < timeout) && (next_timeout > 0.0)) |
| 1262 | timeout = next_timeout; |
| 1263 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1264 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1265 | /* *INDENT-ON* */ |
| 1266 | break; |
| 1267 | |
| 1268 | default: |
| 1269 | clib_warning ("BUG: unhandled event type %d", event_type); |
| 1270 | break; |
| 1271 | } |
| 1272 | /* No less than 1 millisecond */ |
| 1273 | if (timeout < 1e-3) |
| 1274 | timeout = 1e-3; |
| 1275 | if (stop_timer) |
| 1276 | timeout = 3153600000.0; |
| 1277 | } |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /* *INDENT-OFF* */ |
| 1282 | VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = { |
| 1283 | .function = vhost_user_send_interrupt_process, |
| 1284 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1285 | .name = "vhost-user-send-interrupt-process", |
| 1286 | }; |
| 1287 | /* *INDENT-ON* */ |
| 1288 | |
| 1289 | static uword |
| 1290 | vhost_user_process (vlib_main_t * vm, |
| 1291 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 1292 | { |
| 1293 | vhost_user_main_t *vum = &vhost_user_main; |
| 1294 | vhost_user_intf_t *vui; |
| 1295 | struct sockaddr_un sun; |
| 1296 | int sockfd; |
| 1297 | clib_file_t template = { 0 }; |
| 1298 | f64 timeout = 3153600000.0 /* 100 years */ ; |
| 1299 | uword *event_data = 0; |
| 1300 | |
| 1301 | sockfd = -1; |
| 1302 | sun.sun_family = AF_UNIX; |
| 1303 | template.read_function = vhost_user_socket_read; |
| 1304 | template.error_function = vhost_user_socket_error; |
| 1305 | |
| 1306 | while (1) |
| 1307 | { |
| 1308 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 1309 | vlib_process_get_events (vm, &event_data); |
| 1310 | vec_reset_length (event_data); |
| 1311 | |
| 1312 | timeout = 3.0; |
| 1313 | |
| 1314 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1315 | pool_foreach (vui, vum->vhost_user_interfaces) { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1316 | |
| 1317 | if (vui->unix_server_index == ~0) { //Nothing to do for server sockets |
| 1318 | if (vui->clib_file_index == ~0) |
| 1319 | { |
| 1320 | if ((sockfd < 0) && |
| 1321 | ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)) |
| 1322 | { |
| 1323 | /* |
| 1324 | * 1st time error or new error for this interface, |
| 1325 | * spit out the message and record the error |
| 1326 | */ |
| 1327 | if (!vui->sock_errno || (vui->sock_errno != errno)) |
| 1328 | { |
| 1329 | clib_unix_warning |
| 1330 | ("Error: Could not open unix socket for %s", |
| 1331 | vui->sock_filename); |
| 1332 | vui->sock_errno = errno; |
| 1333 | } |
| 1334 | continue; |
| 1335 | } |
| 1336 | |
| 1337 | /* try to connect */ |
| 1338 | strncpy (sun.sun_path, (char *) vui->sock_filename, |
| 1339 | sizeof (sun.sun_path) - 1); |
Damjan Marion | 62c25ab | 2020-12-12 23:32:12 +0100 | [diff] [blame] | 1340 | sun.sun_path[sizeof (sun.sun_path) - 1] = 0; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1341 | |
| 1342 | /* Avoid hanging VPP if the other end does not accept */ |
| 1343 | if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) |
| 1344 | clib_unix_warning ("fcntl"); |
| 1345 | |
| 1346 | if (connect (sockfd, (struct sockaddr *) &sun, |
| 1347 | sizeof (struct sockaddr_un)) == 0) |
| 1348 | { |
| 1349 | /* Set the socket to blocking as it was before */ |
| 1350 | if (fcntl(sockfd, F_SETFL, 0) < 0) |
| 1351 | clib_unix_warning ("fcntl2"); |
| 1352 | |
| 1353 | vui->sock_errno = 0; |
| 1354 | template.file_descriptor = sockfd; |
| 1355 | template.private_data = |
| 1356 | vui - vhost_user_main.vhost_user_interfaces; |
Steven Luong | e2daada | 2021-04-02 22:42:26 -0700 | [diff] [blame] | 1357 | template.description = format (0, "vhost user process"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1358 | vui->clib_file_index = clib_file_add (&file_main, &template); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1359 | vui->num_qid = 2; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1360 | |
| 1361 | /* This sockfd is considered consumed */ |
| 1362 | sockfd = -1; |
| 1363 | } |
| 1364 | else |
| 1365 | { |
| 1366 | vui->sock_errno = errno; |
| 1367 | } |
| 1368 | } |
| 1369 | else |
| 1370 | { |
| 1371 | /* check if socket is alive */ |
| 1372 | int error = 0; |
| 1373 | socklen_t len = sizeof (error); |
| 1374 | int fd = UNIX_GET_FD(vui->clib_file_index); |
| 1375 | int retval = |
| 1376 | getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len); |
| 1377 | |
| 1378 | if (retval) |
| 1379 | { |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1380 | vu_log_debug (vui, "getsockopt returned %d", retval); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1381 | vhost_user_if_disconnect (vui); |
| 1382 | } |
| 1383 | } |
| 1384 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1385 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1386 | /* *INDENT-ON* */ |
| 1387 | } |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | /* *INDENT-OFF* */ |
| 1392 | VLIB_REGISTER_NODE (vhost_user_process_node,static) = { |
| 1393 | .function = vhost_user_process, |
| 1394 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1395 | .name = "vhost-user-process", |
| 1396 | }; |
| 1397 | /* *INDENT-ON* */ |
| 1398 | |
| 1399 | /** |
| 1400 | * Disables and reset interface structure. |
| 1401 | * It can then be either init again, or removed from used interfaces. |
| 1402 | */ |
| 1403 | static void |
| 1404 | vhost_user_term_if (vhost_user_intf_t * vui) |
| 1405 | { |
| 1406 | int q; |
| 1407 | vhost_user_main_t *vum = &vhost_user_main; |
| 1408 | |
| 1409 | // disconnect interface sockets |
| 1410 | vhost_user_if_disconnect (vui); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 1411 | vhost_user_update_gso_interface_count (vui, 0 /* delete */ ); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1412 | vhost_user_update_iface_state (vui); |
| 1413 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1414 | FOR_ALL_VHOST_RX_TXQ (q, vui) |
| 1415 | { |
| 1416 | clib_spinlock_free (&vui->vrings[q].vring_lock); |
| 1417 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1418 | |
| 1419 | if (vui->unix_server_index != ~0) |
| 1420 | { |
| 1421 | //Close server socket |
| 1422 | clib_file_t *uf = pool_elt_at_index (file_main.file_pool, |
| 1423 | vui->unix_server_index); |
| 1424 | clib_file_del (&file_main, uf); |
| 1425 | vui->unix_server_index = ~0; |
| 1426 | unlink (vui->sock_filename); |
| 1427 | } |
| 1428 | |
| 1429 | mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename, |
| 1430 | &vui->if_index); |
| 1431 | } |
| 1432 | |
| 1433 | int |
| 1434 | vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index) |
| 1435 | { |
| 1436 | vhost_user_main_t *vum = &vhost_user_main; |
| 1437 | vhost_user_intf_t *vui; |
| 1438 | int rv = 0; |
| 1439 | vnet_hw_interface_t *hwif; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1440 | u16 qid; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1441 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 1442 | if (! |
| 1443 | (hwif = |
| 1444 | vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index)) |
| 1445 | || hwif->dev_class_index != vhost_user_device_class.index) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1446 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 1447 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1448 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); |
| 1449 | |
Jerome Tollet | 2f54c27 | 2018-10-02 11:41:11 +0200 | [diff] [blame] | 1450 | vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)", |
| 1451 | hwif->name, hwif->dev_instance); |
| 1452 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1453 | FOR_ALL_VHOST_TXQ (qid, vui) |
| 1454 | { |
| 1455 | vhost_user_vring_t *txvq = &vui->vrings[qid]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1456 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1457 | if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) && |
| 1458 | (txvq->thread_index != ~0)) |
| 1459 | { |
| 1460 | vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index); |
| 1461 | ASSERT (cpu->polling_q_count != 0); |
| 1462 | cpu->polling_q_count--; |
| 1463 | } |
| 1464 | |
| 1465 | if ((vum->ifq_count > 0) && |
| 1466 | ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) || |
| 1467 | (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))) |
| 1468 | { |
| 1469 | vum->ifq_count--; |
| 1470 | // Stop the timer if there is no more interrupt interface/queue |
| 1471 | if (vum->ifq_count == 0) |
| 1472 | { |
| 1473 | vlib_process_signal_event (vm, |
| 1474 | vhost_user_send_interrupt_node.index, |
| 1475 | VHOST_USER_EVENT_STOP_TIMER, 0); |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1480 | |
| 1481 | // Disable and reset interface |
| 1482 | vhost_user_term_if (vui); |
| 1483 | |
| 1484 | // Reset renumbered iface |
| 1485 | if (hwif->dev_instance < |
| 1486 | vec_len (vum->show_dev_instance_by_real_dev_instance)) |
| 1487 | vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0; |
| 1488 | |
| 1489 | // Delete ethernet interface |
| 1490 | ethernet_delete_interface (vnm, vui->hw_if_index); |
| 1491 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1492 | // free vrings |
| 1493 | vec_free (vui->vrings); |
| 1494 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1495 | // Back to pool |
| 1496 | pool_put (vum->vhost_user_interfaces, vui); |
| 1497 | |
| 1498 | return rv; |
| 1499 | } |
| 1500 | |
| 1501 | static clib_error_t * |
| 1502 | vhost_user_exit (vlib_main_t * vm) |
| 1503 | { |
| 1504 | vnet_main_t *vnm = vnet_get_main (); |
| 1505 | vhost_user_main_t *vum = &vhost_user_main; |
| 1506 | vhost_user_intf_t *vui; |
| 1507 | |
| 1508 | vlib_worker_thread_barrier_sync (vlib_get_main ()); |
| 1509 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1510 | pool_foreach (vui, vum->vhost_user_interfaces) { |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1511 | vhost_user_delete_if (vnm, vm, vui->sw_if_index); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1512 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1513 | /* *INDENT-ON* */ |
| 1514 | vlib_worker_thread_barrier_release (vlib_get_main ()); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit); |
| 1519 | |
| 1520 | /** |
| 1521 | * Open server unix socket on specified sock_filename. |
| 1522 | */ |
| 1523 | static int |
| 1524 | vhost_user_init_server_sock (const char *sock_filename, int *sock_fd) |
| 1525 | { |
| 1526 | int rv = 0; |
| 1527 | struct sockaddr_un un = { }; |
| 1528 | int fd; |
| 1529 | /* create listening socket */ |
| 1530 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 1531 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 1532 | |
| 1533 | un.sun_family = AF_UNIX; |
| 1534 | strncpy ((char *) un.sun_path, (char *) sock_filename, |
| 1535 | sizeof (un.sun_path) - 1); |
| 1536 | |
| 1537 | /* remove if exists */ |
| 1538 | unlink ((char *) sock_filename); |
| 1539 | |
| 1540 | if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1) |
| 1541 | { |
| 1542 | rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 1543 | goto error; |
| 1544 | } |
| 1545 | |
| 1546 | if (listen (fd, 1) == -1) |
| 1547 | { |
| 1548 | rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 1549 | goto error; |
| 1550 | } |
| 1551 | |
| 1552 | *sock_fd = fd; |
| 1553 | return 0; |
| 1554 | |
| 1555 | error: |
| 1556 | close (fd); |
| 1557 | return rv; |
| 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Create ethernet interface for vhost user interface. |
| 1562 | */ |
| 1563 | static void |
Steven Luong | d6361c7 | 2021-01-26 23:44:19 -0800 | [diff] [blame] | 1564 | vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm, |
| 1565 | vhost_user_intf_t *vui, |
| 1566 | vhost_user_create_if_args_t *args) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1567 | { |
| 1568 | vhost_user_main_t *vum = &vhost_user_main; |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 1569 | vnet_eth_interface_registration_t eir = {}; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1570 | u8 hwaddr[6]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1571 | |
| 1572 | /* create hw and sw interface */ |
Steven Luong | d6361c7 | 2021-01-26 23:44:19 -0800 | [diff] [blame] | 1573 | if (args->use_custom_mac) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1574 | { |
Steven Luong | d6361c7 | 2021-01-26 23:44:19 -0800 | [diff] [blame] | 1575 | clib_memcpy (hwaddr, args->hwaddr, 6); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1576 | } |
| 1577 | else |
| 1578 | { |
| 1579 | random_u32 (&vum->random); |
| 1580 | clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random)); |
| 1581 | hwaddr[0] = 2; |
| 1582 | hwaddr[1] = 0xfe; |
| 1583 | } |
| 1584 | |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 1585 | eir.dev_class_index = vhost_user_device_class.index; |
| 1586 | eir.dev_instance = vui - vum->vhost_user_interfaces /* device instance */, |
| 1587 | eir.address = hwaddr; |
| 1588 | vui->hw_if_index = vnet_eth_register_interface (vnm, &eir); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* |
| 1592 | * Initialize vui with specified attributes |
| 1593 | */ |
| 1594 | static void |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1595 | vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui, |
| 1596 | int server_sock_fd, vhost_user_create_if_args_t * args, |
| 1597 | u32 * sw_if_index) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1598 | { |
| 1599 | vnet_sw_interface_t *sw; |
| 1600 | int q; |
| 1601 | vhost_user_main_t *vum = &vhost_user_main; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1602 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1603 | sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index); |
| 1604 | if (server_sock_fd != -1) |
| 1605 | { |
| 1606 | clib_file_t template = { 0 }; |
| 1607 | template.read_function = vhost_user_socksvr_accept_ready; |
| 1608 | template.file_descriptor = server_sock_fd; |
| 1609 | template.private_data = vui - vum->vhost_user_interfaces; //hw index |
Paul Vinciguerra | 5481ad4 | 2020-01-28 14:47:17 -0500 | [diff] [blame] | 1610 | template.description = format (0, "vhost user %d", sw); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1611 | vui->unix_server_index = clib_file_add (&file_main, &template); |
| 1612 | } |
| 1613 | else |
| 1614 | { |
| 1615 | vui->unix_server_index = ~0; |
| 1616 | } |
| 1617 | |
| 1618 | vui->sw_if_index = sw->sw_if_index; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1619 | strncpy (vui->sock_filename, args->sock_filename, |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1620 | ARRAY_LEN (vui->sock_filename) - 1); |
| 1621 | vui->sock_errno = 0; |
Juraj Sloboda | b192feb | 2018-10-01 12:42:07 +0200 | [diff] [blame] | 1622 | vui->is_ready = 0; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1623 | vui->feature_mask = args->feature_mask; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1624 | vui->clib_file_index = ~0; |
| 1625 | vui->log_base_addr = 0; |
| 1626 | vui->if_index = vui - vum->vhost_user_interfaces; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1627 | vui->enable_gso = args->enable_gso; |
| 1628 | vui->enable_event_idx = args->enable_event_idx; |
| 1629 | vui->enable_packed = args->enable_packed; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 1630 | /* |
| 1631 | * enable_gso takes precedence over configurable feature mask if there |
| 1632 | * is a clash. |
| 1633 | * if feature mask disables gso, but enable_gso is configured, |
| 1634 | * then gso is enable |
| 1635 | * if feature mask enables gso, but enable_gso is not configured, |
| 1636 | * then gso is enable |
| 1637 | * |
| 1638 | * if gso is enable via feature mask, it must enable both host and guest |
| 1639 | * gso feature mask, we don't support one sided GSO or partial GSO. |
| 1640 | */ |
| 1641 | if ((vui->enable_gso == 0) && |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1642 | ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) |
| 1643 | == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))) |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 1644 | vui->enable_gso = 1; |
| 1645 | vhost_user_update_gso_interface_count (vui, 1 /* add */ ); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1646 | mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename, |
| 1647 | &vui->if_index, 0); |
| 1648 | |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 1649 | vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1, |
| 1650 | CLIB_CACHE_LINE_BYTES); |
| 1651 | vui->num_qid = 2; |
| 1652 | for (q = 0; q < vec_len (vui->vrings); q++) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1653 | vhost_user_vring_init (vui, q); |
| 1654 | |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame^] | 1655 | vnet_hw_if_set_caps (vnm, vui->hw_if_index, VNET_HW_IF_CAP_INT_MODE); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1656 | vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); |
| 1657 | |
| 1658 | if (sw_if_index) |
| 1659 | *sw_if_index = vui->sw_if_index; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | int |
| 1663 | vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm, |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1664 | vhost_user_create_if_args_t * args) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1665 | { |
| 1666 | vhost_user_intf_t *vui = NULL; |
| 1667 | u32 sw_if_idx = ~0; |
| 1668 | int rv = 0; |
| 1669 | int server_sock_fd = -1; |
| 1670 | vhost_user_main_t *vum = &vhost_user_main; |
| 1671 | uword *if_index; |
| 1672 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1673 | if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1674 | { |
| 1675 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1676 | } |
| 1677 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1678 | if_index = mhash_get (&vum->if_index_by_sock_name, |
| 1679 | (void *) args->sock_filename); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1680 | if (if_index) |
| 1681 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1682 | vui = &vum->vhost_user_interfaces[*if_index]; |
| 1683 | args->sw_if_index = vui->sw_if_index; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1684 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 1685 | } |
| 1686 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1687 | if (args->is_server) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1688 | { |
| 1689 | if ((rv = |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1690 | vhost_user_init_server_sock (args->sock_filename, |
| 1691 | &server_sock_fd)) != 0) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1692 | { |
| 1693 | return rv; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1697 | /* Protect the uninitialized vui from being dispatched by rx/tx */ |
| 1698 | vlib_worker_thread_barrier_sync (vm); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1699 | pool_get (vhost_user_main.vhost_user_interfaces, vui); |
Steven Luong | d6361c7 | 2021-01-26 23:44:19 -0800 | [diff] [blame] | 1700 | vhost_user_create_ethernet (vnm, vm, vui, args); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1701 | vlib_worker_thread_barrier_release (vm); |
| 1702 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1703 | vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx); |
Juraj Sloboda | 83c46a2 | 2018-09-28 12:04:26 +0200 | [diff] [blame] | 1704 | vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000); |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 1705 | vhost_user_rx_thread_placement (vui, 1); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1706 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1707 | if (args->renumber) |
| 1708 | vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1709 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1710 | args->sw_if_index = sw_if_idx; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1711 | |
| 1712 | // Process node must connect |
| 1713 | vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); |
| 1714 | |
| 1715 | return rv; |
| 1716 | } |
| 1717 | |
| 1718 | int |
| 1719 | vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm, |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1720 | vhost_user_create_if_args_t * args) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1721 | { |
| 1722 | vhost_user_main_t *vum = &vhost_user_main; |
| 1723 | vhost_user_intf_t *vui = NULL; |
| 1724 | u32 sw_if_idx = ~0; |
| 1725 | int server_sock_fd = -1; |
| 1726 | int rv = 0; |
| 1727 | vnet_hw_interface_t *hwif; |
| 1728 | uword *if_index; |
| 1729 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1730 | if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm, |
| 1731 | args->sw_if_index)) |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 1732 | || hwif->dev_class_index != vhost_user_device_class.index) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1733 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 1734 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1735 | if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1736 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1737 | |
| 1738 | vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); |
| 1739 | |
| 1740 | /* |
| 1741 | * Disallow changing the interface to have the same path name |
| 1742 | * as other interface |
| 1743 | */ |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1744 | if_index = mhash_get (&vum->if_index_by_sock_name, |
| 1745 | (void *) args->sock_filename); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1746 | if (if_index && (*if_index != vui->if_index)) |
| 1747 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 1748 | |
| 1749 | // First try to open server socket |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1750 | if (args->is_server) |
| 1751 | if ((rv = vhost_user_init_server_sock (args->sock_filename, |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1752 | &server_sock_fd)) != 0) |
| 1753 | return rv; |
| 1754 | |
| 1755 | vhost_user_term_if (vui); |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1756 | vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1757 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1758 | if (args->renumber) |
| 1759 | vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1760 | |
| 1761 | // Process node must connect |
| 1762 | vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); |
| 1763 | |
| 1764 | return rv; |
| 1765 | } |
| 1766 | |
| 1767 | clib_error_t * |
| 1768 | vhost_user_connect_command_fn (vlib_main_t * vm, |
| 1769 | unformat_input_t * input, |
| 1770 | vlib_cli_command_t * cmd) |
| 1771 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1772 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1773 | unformat_input_t _line_input, *line_input = &_line_input; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1774 | clib_error_t *error = NULL; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1775 | vhost_user_create_if_args_t args = { 0 }; |
| 1776 | int rv; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1777 | |
| 1778 | /* Get a line of input. */ |
| 1779 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1780 | return 0; |
| 1781 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1782 | args.feature_mask = (u64) ~ (0ULL); |
| 1783 | args.custom_dev_instance = ~0; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 1784 | /* GSO feature is disable by default */ |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1785 | args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1786 | /* packed-ring feature is disable by default */ |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1787 | args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); |
| 1788 | /* event_idx feature is disable by default */ |
| 1789 | args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); |
| 1790 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1791 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1792 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1793 | if (unformat (line_input, "socket %s", &args.sock_filename)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1794 | ; |
| 1795 | else if (unformat (line_input, "server")) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1796 | args.is_server = 1; |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 1797 | else if (unformat (line_input, "gso")) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1798 | args.enable_gso = 1; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1799 | else if (unformat (line_input, "packed")) |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1800 | args.enable_packed = 1; |
| 1801 | else if (unformat (line_input, "event-idx")) |
| 1802 | args.enable_event_idx = 1; |
| 1803 | else if (unformat (line_input, "feature-mask 0x%llx", |
| 1804 | &args.feature_mask)) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1805 | ; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1806 | else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, |
| 1807 | args.hwaddr)) |
Steven Luong | d6361c7 | 2021-01-26 23:44:19 -0800 | [diff] [blame] | 1808 | args.use_custom_mac = 1; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1809 | else if (unformat (line_input, "renumber %d", |
| 1810 | &args.custom_dev_instance)) |
| 1811 | args.renumber = 1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1812 | else |
| 1813 | { |
| 1814 | error = clib_error_return (0, "unknown input `%U'", |
| 1815 | format_unformat_error, line_input); |
| 1816 | goto done; |
| 1817 | } |
| 1818 | } |
| 1819 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1820 | if ((rv = vhost_user_create_if (vnm, vm, &args))) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1821 | { |
| 1822 | error = clib_error_return (0, "vhost_user_create_if returned %d", rv); |
| 1823 | goto done; |
| 1824 | } |
| 1825 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1826 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm, |
| 1827 | args.sw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1828 | |
| 1829 | done: |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1830 | vec_free (args.sock_filename); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1831 | unformat_free (line_input); |
| 1832 | |
| 1833 | return error; |
| 1834 | } |
| 1835 | |
| 1836 | clib_error_t * |
| 1837 | vhost_user_delete_command_fn (vlib_main_t * vm, |
| 1838 | unformat_input_t * input, |
| 1839 | vlib_cli_command_t * cmd) |
| 1840 | { |
| 1841 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1842 | u32 sw_if_index = ~0; |
| 1843 | vnet_main_t *vnm = vnet_get_main (); |
| 1844 | clib_error_t *error = NULL; |
| 1845 | |
| 1846 | /* Get a line of input. */ |
| 1847 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1848 | return 0; |
| 1849 | |
| 1850 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1851 | { |
| 1852 | if (unformat (line_input, "sw_if_index %d", &sw_if_index)) |
| 1853 | ; |
| 1854 | else if (unformat |
| 1855 | (line_input, "%U", unformat_vnet_sw_interface, vnm, |
| 1856 | &sw_if_index)) |
| 1857 | { |
| 1858 | vnet_hw_interface_t *hwif = |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 1859 | vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1860 | if (hwif == NULL || |
| 1861 | vhost_user_device_class.index != hwif->dev_class_index) |
| 1862 | { |
| 1863 | error = clib_error_return (0, "Not a vhost interface"); |
| 1864 | goto done; |
| 1865 | } |
| 1866 | } |
| 1867 | else |
| 1868 | { |
| 1869 | error = clib_error_return (0, "unknown input `%U'", |
| 1870 | format_unformat_error, line_input); |
| 1871 | goto done; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | vhost_user_delete_if (vnm, vm, sw_if_index); |
| 1876 | |
| 1877 | done: |
| 1878 | unformat_free (line_input); |
| 1879 | |
| 1880 | return error; |
| 1881 | } |
| 1882 | |
| 1883 | int |
| 1884 | vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm, |
| 1885 | vhost_user_intf_details_t ** out_vuids) |
| 1886 | { |
| 1887 | int rv = 0; |
| 1888 | vhost_user_main_t *vum = &vhost_user_main; |
| 1889 | vhost_user_intf_t *vui; |
| 1890 | vhost_user_intf_details_t *r_vuids = NULL; |
| 1891 | vhost_user_intf_details_t *vuid = NULL; |
| 1892 | u32 *hw_if_indices = 0; |
| 1893 | vnet_hw_interface_t *hi; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1894 | int i; |
| 1895 | |
| 1896 | if (!out_vuids) |
| 1897 | return -1; |
| 1898 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1899 | pool_foreach (vui, vum->vhost_user_interfaces) |
| 1900 | vec_add1 (hw_if_indices, vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1901 | |
| 1902 | for (i = 0; i < vec_len (hw_if_indices); i++) |
| 1903 | { |
| 1904 | hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); |
| 1905 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); |
| 1906 | |
| 1907 | vec_add2 (r_vuids, vuid, 1); |
| 1908 | vuid->sw_if_index = vui->sw_if_index; |
| 1909 | vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz; |
| 1910 | vuid->features = vui->features; |
| 1911 | vuid->num_regions = vui->nregions; |
| 1912 | vuid->is_server = vui->unix_server_index != ~0; |
| 1913 | vuid->sock_errno = vui->sock_errno; |
Benoît Ganne | 3a76dc3 | 2020-04-24 10:33:40 +0200 | [diff] [blame] | 1914 | snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename), |
| 1915 | "%s", vui->sock_filename); |
| 1916 | memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name, |
| 1917 | clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1)); |
| 1918 | vuid->if_name[sizeof (vuid->if_name) - 1] = 0; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1919 | } |
| 1920 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 1921 | vec_free (hw_if_indices); |
| 1922 | |
| 1923 | *out_vuids = r_vuids; |
| 1924 | |
| 1925 | return rv; |
| 1926 | } |
| 1927 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1928 | static u8 * |
| 1929 | format_vhost_user_desc (u8 * s, va_list * args) |
| 1930 | { |
| 1931 | char *fmt = va_arg (*args, char *); |
| 1932 | vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *); |
| 1933 | vring_desc_t *desc_table = va_arg (*args, vring_desc_t *); |
| 1934 | int idx = va_arg (*args, int); |
| 1935 | u32 *mem_hint = va_arg (*args, u32 *); |
| 1936 | |
| 1937 | s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len, |
| 1938 | desc_table[idx].flags, desc_table[idx].next, |
| 1939 | pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr, |
| 1940 | mem_hint))); |
| 1941 | return s; |
| 1942 | } |
| 1943 | |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1944 | static void |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1945 | vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1946 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1947 | int kickfd = UNIX_GET_FD (vq->kickfd_idx); |
| 1948 | int callfd = UNIX_GET_FD (vq->callfd_idx); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1949 | |
| 1950 | vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd, |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1951 | vq->errfd); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | static void |
| 1955 | vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q, |
| 1956 | int show_descr, int show_verbose) |
| 1957 | { |
| 1958 | int j; |
| 1959 | u32 mem_hint = 0; |
| 1960 | u32 idx; |
| 1961 | u32 n_entries; |
| 1962 | vring_desc_t *desc_table; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1963 | vhost_user_vring_t *vq = &vui->vrings[q]; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1964 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1965 | if (vq->avail && vq->used) |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 1966 | vlib_cli_output (vm, |
| 1967 | " avail.flags %x avail event idx %u avail.idx %d " |
| 1968 | "used.flags %x used event idx %u used.idx %d\n", |
| 1969 | vq->avail->flags, vhost_user_avail_event_idx (vq), |
| 1970 | vq->avail->idx, vq->used->flags, |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1971 | vhost_user_used_event_idx (vq), vq->used->idx); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1972 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1973 | vhost_user_show_fds (vm, vq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1974 | |
| 1975 | if (show_descr) |
| 1976 | { |
| 1977 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 1978 | vlib_cli_output (vm, |
| 1979 | " slot addr len flags next " |
| 1980 | "user_addr\n"); |
| 1981 | vlib_cli_output (vm, |
| 1982 | " ===== ================== ===== ====== ===== " |
| 1983 | "==================\n"); |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1984 | for (j = 0; j < vq->qsz_mask + 1; j++) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1985 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 1986 | desc_table = vq->desc; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1987 | vlib_cli_output (vm, "%U", format_vhost_user_desc, |
| 1988 | " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui, |
| 1989 | desc_table, j, &mem_hint); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1990 | if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT)) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 1991 | { |
| 1992 | n_entries = desc_table[j].len / sizeof (vring_desc_t); |
| 1993 | desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint); |
| 1994 | if (desc_table) |
| 1995 | { |
| 1996 | for (idx = 0; idx < clib_min (20, n_entries); idx++) |
| 1997 | { |
| 1998 | vlib_cli_output |
| 1999 | (vm, "%U", format_vhost_user_desc, |
| 2000 | "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, |
| 2001 | desc_table, idx, &mem_hint); |
| 2002 | } |
| 2003 | if (n_entries >= 20) |
| 2004 | vlib_cli_output (vm, "Skip displaying entries 20...%u\n", |
| 2005 | n_entries); |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | static u8 * |
| 2013 | format_vhost_user_packed_desc (u8 * s, va_list * args) |
| 2014 | { |
| 2015 | char *fmt = va_arg (*args, char *); |
| 2016 | vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *); |
| 2017 | vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *); |
| 2018 | int idx = va_arg (*args, int); |
| 2019 | u32 *mem_hint = va_arg (*args, u32 *); |
| 2020 | |
| 2021 | s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len, |
| 2022 | desc_table[idx].flags, desc_table[idx].id, |
| 2023 | pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr, |
| 2024 | mem_hint))); |
| 2025 | return s; |
| 2026 | } |
| 2027 | |
| 2028 | static u8 * |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2029 | format_vhost_user_event_idx_flags (u8 * s, va_list * args) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2030 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2031 | u32 flags = va_arg (*args, u32); |
| 2032 | typedef struct |
| 2033 | { |
| 2034 | u8 value; |
| 2035 | char *str; |
| 2036 | } event_idx_flags; |
| 2037 | static event_idx_flags event_idx_array[] = { |
| 2038 | #define _(s,v) { .str = #s, .value = v, }, |
| 2039 | foreach_virtio_event_idx_flags |
| 2040 | #undef _ |
| 2041 | }; |
| 2042 | u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2043 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2044 | if (flags < num_entries) |
| 2045 | s = format (s, "%s", event_idx_array[flags].str); |
| 2046 | else |
| 2047 | s = format (s, "%u", flags); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2048 | return s; |
| 2049 | } |
| 2050 | |
| 2051 | static void |
| 2052 | vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q, |
| 2053 | int show_descr, int show_verbose) |
| 2054 | { |
| 2055 | int j; |
| 2056 | u32 mem_hint = 0; |
| 2057 | u32 idx; |
| 2058 | u32 n_entries; |
| 2059 | vring_packed_desc_t *desc_table; |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2060 | vhost_user_vring_t *vq = &vui->vrings[q]; |
| 2061 | u16 off_wrap, event_idx; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2062 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2063 | off_wrap = vq->avail_event->off_wrap; |
| 2064 | event_idx = off_wrap & 0x7fff; |
| 2065 | vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u " |
| 2066 | "avail event idx %u\n", format_vhost_user_event_idx_flags, |
| 2067 | (u32) vq->avail_event->flags, off_wrap, event_idx); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2068 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2069 | off_wrap = vq->used_event->off_wrap; |
| 2070 | event_idx = off_wrap & 0x7fff; |
| 2071 | vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u " |
| 2072 | "used event idx %u\n", format_vhost_user_event_idx_flags, |
| 2073 | (u32) vq->used_event->flags, off_wrap, event_idx); |
| 2074 | |
| 2075 | vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n", |
| 2076 | vq->avail_wrap_counter, vq->used_wrap_counter); |
| 2077 | |
| 2078 | vhost_user_show_fds (vm, vq); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2079 | |
| 2080 | if (show_descr) |
| 2081 | { |
| 2082 | vlib_cli_output (vm, "\n descriptor table:\n"); |
| 2083 | vlib_cli_output (vm, |
| 2084 | " slot addr len flags id " |
| 2085 | "user_addr\n"); |
| 2086 | vlib_cli_output (vm, |
| 2087 | " ===== ================== ===== ====== ===== " |
| 2088 | "==================\n"); |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2089 | for (j = 0; j < vq->qsz_mask + 1; j++) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2090 | { |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2091 | desc_table = vq->packed_desc; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2092 | vlib_cli_output (vm, "%U", format_vhost_user_packed_desc, |
| 2093 | " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, |
| 2094 | desc_table, j, &mem_hint); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 2095 | if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT)) |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2096 | { |
| 2097 | n_entries = desc_table[j].len >> 4; |
| 2098 | desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint); |
| 2099 | if (desc_table) |
| 2100 | { |
| 2101 | for (idx = 0; idx < clib_min (20, n_entries); idx++) |
| 2102 | { |
| 2103 | vlib_cli_output |
| 2104 | (vm, "%U", format_vhost_user_packed_desc, |
| 2105 | "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, |
| 2106 | desc_table, idx, &mem_hint); |
| 2107 | } |
| 2108 | if (n_entries >= 20) |
| 2109 | vlib_cli_output (vm, "Skip displaying entries 20...%u\n", |
| 2110 | n_entries); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2117 | clib_error_t * |
| 2118 | show_vhost_user_command_fn (vlib_main_t * vm, |
| 2119 | unformat_input_t * input, |
| 2120 | vlib_cli_command_t * cmd) |
| 2121 | { |
| 2122 | clib_error_t *error = 0; |
| 2123 | vnet_main_t *vnm = vnet_get_main (); |
| 2124 | vhost_user_main_t *vum = &vhost_user_main; |
| 2125 | vhost_user_intf_t *vui; |
| 2126 | u32 hw_if_index, *hw_if_indices = 0; |
| 2127 | vnet_hw_interface_t *hi; |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 2128 | u16 qid; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2129 | int i, j, q; |
| 2130 | int show_descr = 0; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2131 | int show_verbose = 0; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2132 | struct feat_struct |
| 2133 | { |
| 2134 | u8 bit; |
| 2135 | char *str; |
| 2136 | }; |
| 2137 | struct feat_struct *feat_entry; |
| 2138 | |
| 2139 | static struct feat_struct feat_array[] = { |
| 2140 | #define _(s,b) { .str = #s, .bit = b, }, |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 2141 | foreach_virtio_net_features |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2142 | #undef _ |
| 2143 | {.str = NULL} |
| 2144 | }; |
| 2145 | |
| 2146 | #define foreach_protocol_feature \ |
| 2147 | _(VHOST_USER_PROTOCOL_F_MQ) \ |
| 2148 | _(VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
| 2149 | |
| 2150 | static struct feat_struct proto_feat_array[] = { |
| 2151 | #define _(s) { .str = #s, .bit = s}, |
| 2152 | foreach_protocol_feature |
| 2153 | #undef _ |
| 2154 | {.str = NULL} |
| 2155 | }; |
| 2156 | |
| 2157 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2158 | { |
| 2159 | if (unformat |
| 2160 | (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) |
| 2161 | { |
| 2162 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 2163 | if (vhost_user_device_class.index != hi->dev_class_index) |
| 2164 | { |
| 2165 | error = clib_error_return (0, "unknown input `%U'", |
| 2166 | format_unformat_error, input); |
| 2167 | goto done; |
| 2168 | } |
| 2169 | vec_add1 (hw_if_indices, hw_if_index); |
| 2170 | } |
| 2171 | else if (unformat (input, "descriptors") || unformat (input, "desc")) |
| 2172 | show_descr = 1; |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2173 | else if (unformat (input, "verbose")) |
| 2174 | show_verbose = 1; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2175 | else |
| 2176 | { |
| 2177 | error = clib_error_return (0, "unknown input `%U'", |
| 2178 | format_unformat_error, input); |
| 2179 | goto done; |
| 2180 | } |
| 2181 | } |
| 2182 | if (vec_len (hw_if_indices) == 0) |
| 2183 | { |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2184 | pool_foreach (vui, vum->vhost_user_interfaces) |
| 2185 | vec_add1 (hw_if_indices, vui->hw_if_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2186 | } |
| 2187 | vlib_cli_output (vm, "Virtio vhost-user interfaces"); |
| 2188 | vlib_cli_output (vm, "Global:\n coalesce frames %d time %e", |
| 2189 | vum->coalesce_frames, vum->coalesce_time); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 2190 | vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d", |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2191 | vum->ifq_count); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 2192 | vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count); |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2193 | for (u32 tid = 0; tid <= vlib_num_workers (); tid++) |
| 2194 | { |
| 2195 | vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid); |
| 2196 | vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid, |
| 2197 | cpu->polling_q_count); |
| 2198 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2199 | |
| 2200 | for (i = 0; i < vec_len (hw_if_indices); i++) |
| 2201 | { |
| 2202 | hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); |
| 2203 | vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); |
Steven | 877ad14 | 2018-09-20 11:50:35 -0700 | [diff] [blame] | 2204 | vlib_cli_output (vm, "Interface: %U (ifindex %d)", |
| 2205 | format_vnet_hw_if_index_name, vnm, hw_if_indices[i], |
| 2206 | hw_if_indices[i]); |
Steven Luong | 2c1084a | 2020-12-10 20:44:22 -0800 | [diff] [blame] | 2207 | vlib_cli_output (vm, " Number of qids %u", vui->num_qid); |
Steven Luong | 4208a4c | 2019-05-06 08:51:56 -0700 | [diff] [blame] | 2208 | if (vui->enable_gso) |
| 2209 | vlib_cli_output (vm, " GSO enable"); |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2210 | if (vui->enable_packed) |
| 2211 | vlib_cli_output (vm, " Packed ring enable"); |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2212 | if (vui->enable_event_idx) |
| 2213 | vlib_cli_output (vm, " Event index enable"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2214 | |
| 2215 | vlib_cli_output (vm, "virtio_net_hdr_sz %d\n" |
| 2216 | " features mask (0x%llx): \n" |
| 2217 | " features (0x%llx): \n", |
| 2218 | vui->virtio_net_hdr_sz, vui->feature_mask, |
| 2219 | vui->features); |
| 2220 | |
| 2221 | feat_entry = (struct feat_struct *) &feat_array; |
| 2222 | while (feat_entry->str) |
| 2223 | { |
| 2224 | if (vui->features & (1ULL << feat_entry->bit)) |
| 2225 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 2226 | feat_entry->bit); |
| 2227 | feat_entry++; |
| 2228 | } |
| 2229 | |
| 2230 | vlib_cli_output (vm, " protocol features (0x%llx)", |
| 2231 | vui->protocol_features); |
| 2232 | feat_entry = (struct feat_struct *) &proto_feat_array; |
| 2233 | while (feat_entry->str) |
| 2234 | { |
| 2235 | if (vui->protocol_features & (1ULL << feat_entry->bit)) |
| 2236 | vlib_cli_output (vm, " %s (%d)", feat_entry->str, |
| 2237 | feat_entry->bit); |
| 2238 | feat_entry++; |
| 2239 | } |
| 2240 | |
| 2241 | vlib_cli_output (vm, "\n"); |
| 2242 | |
| 2243 | vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n", |
| 2244 | vui->sock_filename, |
| 2245 | (vui->unix_server_index != ~0) ? "server" : "client", |
| 2246 | strerror (vui->sock_errno)); |
| 2247 | |
| 2248 | vlib_cli_output (vm, " rx placement: "); |
| 2249 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2250 | FOR_ALL_VHOST_TXQ (qid, vui) |
| 2251 | { |
| 2252 | vhost_user_vring_t *txvq = &vui->vrings[qid]; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2253 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2254 | if (txvq->qid == -1) |
| 2255 | continue; |
| 2256 | vlib_cli_output (vm, " thread %d on vring %d, %U\n", |
| 2257 | txvq->thread_index, qid, format_vnet_hw_if_rx_mode, |
| 2258 | txvq->mode); |
| 2259 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2260 | |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 2261 | vlib_cli_output (vm, " tx placement\n"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2262 | |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 2263 | FOR_ALL_VHOST_RXQ (qid, vui) |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2264 | { |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 2265 | vhost_user_vring_t *rxvq = &vui->vrings[qid]; |
| 2266 | vnet_hw_if_tx_queue_t *txq; |
| 2267 | |
| 2268 | if (rxvq->queue_index == ~0) |
| 2269 | continue; |
| 2270 | txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index); |
| 2271 | if (txq->threads) |
| 2272 | vlib_cli_output (vm, " threads %U on vring %u: %s\n", |
| 2273 | format_bitmap_list, txq->threads, qid, |
| 2274 | txq->shared_queue ? "spin-lock" : "lock-free"); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | vlib_cli_output (vm, "\n"); |
| 2278 | |
| 2279 | vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions); |
| 2280 | |
| 2281 | if (vui->nregions) |
| 2282 | { |
| 2283 | vlib_cli_output (vm, |
| 2284 | " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n"); |
| 2285 | vlib_cli_output (vm, |
| 2286 | " ====== ===== ================== ================== ================== ================== ==================\n"); |
| 2287 | } |
| 2288 | for (j = 0; j < vui->nregions; j++) |
| 2289 | { |
| 2290 | vlib_cli_output (vm, |
| 2291 | " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", |
| 2292 | j, vui->region_mmap_fd[j], |
| 2293 | vui->regions[j].guest_phys_addr, |
| 2294 | vui->regions[j].memory_size, |
| 2295 | vui->regions[j].userspace_addr, |
| 2296 | vui->regions[j].mmap_offset, |
| 2297 | pointer_to_uword (vui->region_mmap_addr[j])); |
| 2298 | } |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2299 | FOR_ALL_VHOST_RX_TXQ (q, vui) |
| 2300 | { |
| 2301 | if (!vui->vrings[q].started) |
| 2302 | continue; |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2303 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2304 | vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q, |
| 2305 | (q & 1) ? "RX" : "TX", |
| 2306 | vui->vrings[q].enabled ? "" : " disabled"); |
Steven Luong | ce50758 | 2021-08-23 14:31:16 -0700 | [diff] [blame] | 2307 | vlib_cli_output (vm, " global %s queue index %u\n", |
| 2308 | (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2309 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2310 | vlib_cli_output ( |
| 2311 | vm, |
| 2312 | " qsz %d last_avail_idx %d last_used_idx %d" |
| 2313 | " last_kick %u\n", |
| 2314 | vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx, |
| 2315 | vui->vrings[q].last_used_idx, vui->vrings[q].last_kick); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2316 | |
Steven Luong | 38071b1 | 2021-04-21 09:54:34 -0700 | [diff] [blame] | 2317 | if (vhost_user_is_packed_ring_supported (vui)) |
| 2318 | vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose); |
| 2319 | else |
| 2320 | vhost_user_show_desc (vm, vui, q, show_descr, show_verbose); |
| 2321 | } |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2322 | vlib_cli_output (vm, "\n"); |
| 2323 | } |
| 2324 | done: |
| 2325 | vec_free (hw_if_indices); |
| 2326 | return error; |
| 2327 | } |
| 2328 | |
| 2329 | /* |
| 2330 | * CLI functions |
| 2331 | */ |
| 2332 | |
| 2333 | /*? |
| 2334 | * Create a vHost User interface. Once created, a new virtual interface |
| 2335 | * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>' |
| 2336 | * is the next free index. |
| 2337 | * |
| 2338 | * There are several parameters associated with a vHost interface: |
| 2339 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2340 | * - <b>socket <socket-filename></b> - Name of the linux socket used by |
| 2341 | * hypervisor and VPP to manage the vHost interface. If in <em>server</em> |
| 2342 | * mode, VPP will create the socket if it does not already exist. If in |
| 2343 | * <em>client</em> mode, hypervisor will create the socket if it does not |
| 2344 | * already exist. The VPP code is indifferent to the file location. However, |
| 2345 | * if SELinux is enabled, then the socket needs to be created in |
| 2346 | * <em>/var/run/vpp/</em>. |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2347 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2348 | * - <b>server</b> - Optional flag to indicate that VPP should be the server |
| 2349 | * for the linux socket. If not provided, VPP will be the client. In |
| 2350 | * <em>server</em> mode, the VM can be reset without tearing down the vHost |
| 2351 | * Interface. In <em>client</em> mode, VPP can be reset without bringing down |
| 2352 | * the VM and tearing down the vHost Interface. |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2353 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2354 | * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated |
| 2355 | * at startup. <b>This is intended for degugging only.</b> It is recommended |
| 2356 | * that this parameter not be used except by experienced users. By default, |
| 2357 | * all supported features will be advertised. Otherwise, provide the set of |
| 2358 | * features desired. |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2359 | * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF |
| 2360 | * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ |
| 2361 | * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE |
| 2362 | * - 0x000400000 (22) - VIRTIO_NET_F_MQ |
| 2363 | * - 0x004000000 (26) - VHOST_F_LOG_ALL |
| 2364 | * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT |
| 2365 | * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC |
| 2366 | * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES |
| 2367 | * - 0x100000000 (32) - VIRTIO_F_VERSION_1 |
| 2368 | * |
| 2369 | * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either |
| 2370 | * X:X:X:X:X:X unix or X.X.X cisco format. |
| 2371 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2372 | * - <b>renumber <dev_instance></b> - Optional parameter which allows the |
| 2373 | * instance in the name to be specified. If instance already exists, name |
| 2374 | * will be used anyway and multiple instances will have the same name. Use |
| 2375 | * with caution. |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2376 | * |
| 2377 | * @cliexpar |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2378 | * Example of how to create a vhost interface with VPP as the client and all |
| 2379 | * features enabled: |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2380 | * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock} |
| 2381 | * VirtualEthernet0/0/0 |
| 2382 | * @cliexend |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2383 | * Example of how to create a vhost interface with VPP as the server and with |
| 2384 | * just multiple queues enabled: |
| 2385 | * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server |
| 2386 | * feature-mask 0x40400000} |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2387 | * VirtualEthernet0/0/1 |
| 2388 | * @cliexend |
| 2389 | * Once the vHost interface is created, enable the interface using: |
| 2390 | * @cliexcmd{set interface state VirtualEthernet0/0/0 up} |
| 2391 | ?*/ |
| 2392 | /* *INDENT-OFF* */ |
| 2393 | VLIB_CLI_COMMAND (vhost_user_connect_command, static) = { |
| 2394 | .path = "create vhost-user", |
| 2395 | .short_help = "create vhost-user socket <socket-filename> [server] " |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2396 | "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] " |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 2397 | "[packed] [event-idx]", |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2398 | .function = vhost_user_connect_command_fn, |
Steven Luong | 67f935e | 2019-02-01 10:23:56 -0800 | [diff] [blame] | 2399 | .is_mp_safe = 1, |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2400 | }; |
| 2401 | /* *INDENT-ON* */ |
| 2402 | |
| 2403 | /*? |
| 2404 | * Delete a vHost User interface using the interface name or the |
| 2405 | * software interface index. Use the '<em>show interface</em>' |
| 2406 | * command to determine the software interface index. On deletion, |
| 2407 | * the linux socket will not be deleted. |
| 2408 | * |
| 2409 | * @cliexpar |
| 2410 | * Example of how to delete a vhost interface by name: |
| 2411 | * @cliexcmd{delete vhost-user VirtualEthernet0/0/1} |
| 2412 | * Example of how to delete a vhost interface by software interface index: |
| 2413 | * @cliexcmd{delete vhost-user sw_if_index 1} |
| 2414 | ?*/ |
| 2415 | /* *INDENT-OFF* */ |
| 2416 | VLIB_CLI_COMMAND (vhost_user_delete_command, static) = { |
| 2417 | .path = "delete vhost-user", |
| 2418 | .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}", |
| 2419 | .function = vhost_user_delete_command_fn, |
| 2420 | }; |
| 2421 | |
| 2422 | /*? |
| 2423 | * Display the attributes of a single vHost User interface (provide interface |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2424 | * name), multiple vHost User interfaces (provide a list of interface names |
| 2425 | * separated by spaces) or all Vhost User interfaces (omit an interface name |
| 2426 | * to display all vHost interfaces). |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2427 | * |
| 2428 | * @cliexpar |
| 2429 | * @parblock |
| 2430 | * Example of how to display a vhost interface: |
| 2431 | * @cliexstart{show vhost-user VirtualEthernet0/0/0} |
| 2432 | * Virtio vhost-user interfaces |
| 2433 | * Global: |
| 2434 | * coalesce frames 32 time 1e-3 |
| 2435 | * Interface: VirtualEthernet0/0/0 (ifindex 1) |
| 2436 | * virtio_net_hdr_sz 12 |
| 2437 | * features mask (0xffffffffffffffff): |
| 2438 | * features (0x50408000): |
| 2439 | * VIRTIO_NET_F_MRG_RXBUF (15) |
| 2440 | * VIRTIO_NET_F_MQ (22) |
| 2441 | * VIRTIO_F_INDIRECT_DESC (28) |
| 2442 | * VHOST_USER_F_PROTOCOL_FEATURES (30) |
| 2443 | * protocol features (0x3) |
| 2444 | * VHOST_USER_PROTOCOL_F_MQ (0) |
| 2445 | * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1) |
| 2446 | * |
| 2447 | * socket filename /var/run/vpp/vhost1.sock type client errno "Success" |
| 2448 | * |
| 2449 | * rx placement: |
| 2450 | * thread 1 on vring 1 |
| 2451 | * thread 1 on vring 5 |
| 2452 | * thread 2 on vring 3 |
| 2453 | * thread 2 on vring 7 |
| 2454 | * tx placement: spin-lock |
| 2455 | * thread 0 on vring 0 |
| 2456 | * thread 1 on vring 2 |
| 2457 | * thread 2 on vring 0 |
| 2458 | * |
| 2459 | * Memory regions (total 2) |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2460 | * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr |
| 2461 | * ====== == =============== =========== ============== =========== ========== |
| 2462 | * 0 60 0x00000000 0x000a0000 0xaac00000 0x00000000 0x2b400000 |
| 2463 | * 1 61 0x000c0000 0x3ff40000 0xaacc0000 0x000c0000 0xabcc0000 |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2464 | * |
| 2465 | * Virtqueue 0 (TX) |
| 2466 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2467 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 2468 | * kickfd 62 callfd 64 errfd -1 |
| 2469 | * |
| 2470 | * Virtqueue 1 (RX) |
| 2471 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2472 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2473 | * kickfd 65 callfd 66 errfd -1 |
| 2474 | * |
| 2475 | * Virtqueue 2 (TX) |
| 2476 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2477 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 2478 | * kickfd 63 callfd 70 errfd -1 |
| 2479 | * |
| 2480 | * Virtqueue 3 (RX) |
| 2481 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2482 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2483 | * kickfd 72 callfd 74 errfd -1 |
| 2484 | * |
| 2485 | * Virtqueue 4 (TX disabled) |
| 2486 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2487 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2488 | * kickfd 76 callfd 78 errfd -1 |
| 2489 | * |
| 2490 | * Virtqueue 5 (RX disabled) |
| 2491 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2492 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2493 | * kickfd 80 callfd 82 errfd -1 |
| 2494 | * |
| 2495 | * Virtqueue 6 (TX disabled) |
| 2496 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2497 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2498 | * kickfd 84 callfd 86 errfd -1 |
| 2499 | * |
| 2500 | * Virtqueue 7 (RX disabled) |
| 2501 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2502 | * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 |
| 2503 | * kickfd 88 callfd 90 errfd -1 |
| 2504 | * |
| 2505 | * @cliexend |
| 2506 | * |
Nathan Skrzypczak | 2c77ae4 | 2021-09-29 15:36:51 +0200 | [diff] [blame] | 2507 | * The optional '<em>descriptors</em>' parameter will display the same output |
| 2508 | * as the previous example but will include the descriptor table for each |
| 2509 | * queue. |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2510 | * The output is truncated below: |
| 2511 | * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors} |
| 2512 | * Virtio vhost-user interfaces |
| 2513 | * Global: |
| 2514 | * coalesce frames 32 time 1e-3 |
| 2515 | * Interface: VirtualEthernet0/0/0 (ifindex 1) |
| 2516 | * virtio_net_hdr_sz 12 |
| 2517 | * features mask (0xffffffffffffffff): |
| 2518 | * features (0x50408000): |
| 2519 | * VIRTIO_NET_F_MRG_RXBUF (15) |
| 2520 | * VIRTIO_NET_F_MQ (22) |
| 2521 | * : |
| 2522 | * Virtqueue 0 (TX) |
| 2523 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2524 | * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 |
| 2525 | * kickfd 62 callfd 64 errfd -1 |
| 2526 | * |
| 2527 | * descriptor table: |
| 2528 | * id addr len flags next user_addr |
| 2529 | * ===== ================== ===== ====== ===== ================== |
| 2530 | * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974 |
| 2531 | * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034 |
| 2532 | * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4 |
| 2533 | * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4 |
| 2534 | * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474 |
| 2535 | * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34 |
| 2536 | * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4 |
| 2537 | * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4 |
| 2538 | * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74 |
| 2539 | * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634 |
| 2540 | * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4 |
| 2541 | * : |
| 2542 | * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000 |
| 2543 | * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000 |
| 2544 | * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000 |
| 2545 | * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000 |
| 2546 | * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000 |
| 2547 | * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000 |
| 2548 | * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000 |
| 2549 | * |
| 2550 | * Virtqueue 1 (RX) |
| 2551 | * qsz 256 last_avail_idx 0 last_used_idx 0 |
| 2552 | * : |
| 2553 | * @cliexend |
| 2554 | * @endparblock |
| 2555 | ?*/ |
| 2556 | /* *INDENT-OFF* */ |
| 2557 | VLIB_CLI_COMMAND (show_vhost_user_command, static) = { |
| 2558 | .path = "show vhost-user", |
Steven Luong | bc0d9ff | 2020-03-23 09:34:59 -0700 | [diff] [blame] | 2559 | .short_help = "show vhost-user [<interface> [<interface> [..]]] " |
| 2560 | "[[descriptors] [verbose]]", |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2561 | .function = show_vhost_user_command_fn, |
| 2562 | }; |
| 2563 | /* *INDENT-ON* */ |
| 2564 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2565 | |
| 2566 | static clib_error_t * |
| 2567 | vhost_user_config (vlib_main_t * vm, unformat_input_t * input) |
| 2568 | { |
| 2569 | vhost_user_main_t *vum = &vhost_user_main; |
| 2570 | |
| 2571 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2572 | { |
| 2573 | if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames)) |
| 2574 | ; |
| 2575 | else if (unformat (input, "coalesce-time %f", &vum->coalesce_time)) |
| 2576 | ; |
| 2577 | else if (unformat (input, "dont-dump-memory")) |
| 2578 | vum->dont_dump_vhost_user_memory = 1; |
| 2579 | else |
| 2580 | return clib_error_return (0, "unknown input `%U'", |
| 2581 | format_unformat_error, input); |
| 2582 | } |
| 2583 | |
| 2584 | return 0; |
| 2585 | } |
| 2586 | |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2587 | /* vhost-user { ... } configuration. */ |
| 2588 | VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user"); |
| 2589 | |
| 2590 | void |
| 2591 | vhost_user_unmap_all (void) |
| 2592 | { |
| 2593 | vhost_user_main_t *vum = &vhost_user_main; |
| 2594 | vhost_user_intf_t *vui; |
| 2595 | |
| 2596 | if (vum->dont_dump_vhost_user_memory) |
| 2597 | { |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2598 | pool_foreach (vui, vum->vhost_user_interfaces) |
| 2599 | unmap_all_mem_regions (vui); |
Mohsin Kazmi | e7cde31 | 2018-06-26 17:20:11 +0200 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * fd.io coding-style-patch-verification: ON |
| 2605 | * |
| 2606 | * Local Variables: |
| 2607 | * eval: (c-set-style "gnu") |
| 2608 | * End: |
| 2609 | */ |