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