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