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