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