blob: 5c5d23f2017438702766c106e12baa88f3dfe470 [file] [log] [blame]
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001/*
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 Kazmie7cde312018-06-26 17:20:11 +020036#include <vnet/ethernet/ethernet.h>
37#include <vnet/devices/devices.h>
38#include <vnet/feature/feature.h>
39
40#include <vnet/devices/virtio/vhost_user.h>
41#include <vnet/devices/virtio/vhost_user_inline.h>
42
43/**
44 * @file
45 * @brief vHost User Device Driver.
46 *
47 * This file contains the source code for vHost User interface.
48 */
49
50
51vlib_node_registration_t vhost_user_send_interrupt_node;
52
53/* *INDENT-OFF* */
54vhost_user_main_t vhost_user_main = {
55 .mtu_bytes = 1518,
56};
57
58VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
59 .name = "vhost-user",
60};
61/* *INDENT-ON* */
62
63static long
64get_huge_page_size (int fd)
65{
66 struct statfs s;
67 fstatfs (fd, &s);
68 return s.f_bsize;
69}
70
71static void
72unmap_all_mem_regions (vhost_user_intf_t * vui)
73{
Yichen Wang28812a02018-08-28 23:05:27 -070074 int i, r, q;
75 vhost_user_vring_t *vq;
76
Mohsin Kazmie7cde312018-06-26 17:20:11 +020077 for (i = 0; i < vui->nregions; i++)
78 {
79 if (vui->region_mmap_addr[i] != MAP_FAILED)
80 {
81
82 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
83
84 ssize_t map_sz = (vui->regions[i].memory_size +
85 vui->regions[i].mmap_offset +
86 page_sz - 1) & ~(page_sz - 1);
87
88 r =
89 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
90 map_sz);
91
Jerome Tollet2f54c272018-10-02 11:41:11 +020092 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
93 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
94 page_sz);
Mohsin Kazmie7cde312018-06-26 17:20:11 +020095
96 vui->region_mmap_addr[i] = MAP_FAILED;
97
98 if (r == -1)
99 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200100 vu_log_err (vui, "failed to unmap memory region (errno %d)",
101 errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200102 }
103 close (vui->region_mmap_fd[i]);
104 }
105 }
106 vui->nregions = 0;
Yichen Wang28812a02018-08-28 23:05:27 -0700107
Steven Luong2c1084a2020-12-10 20:44:22 -0800108 for (q = 0; q < vui->num_qid; q++)
Yichen Wang28812a02018-08-28 23:05:27 -0700109 {
110 vq = &vui->vrings[q];
111 vq->avail = 0;
112 vq->used = 0;
113 vq->desc = 0;
114 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200115}
116
Steven Luong67f935e2019-02-01 10:23:56 -0800117static_always_inline void
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200118vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
119{
120 //Let's try to assign one queue to each thread
Steven Luong67f935e2019-02-01 10:23:56 -0800121 u32 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200122 u32 thread_index = 0;
Steven Luong67f935e2019-02-01 10:23:56 -0800123
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200124 vui->use_tx_spinlock = 0;
125 while (1)
126 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800127 for (qid = 0; qid < vui->num_qid / 2; qid++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200128 {
129 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
130 if (!rxvq->started || !rxvq->enabled)
131 continue;
132
133 vui->per_cpu_tx_qid[thread_index] = qid;
134 thread_index++;
135 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
136 return;
137 }
138 //We need to loop, meaning the spinlock has to be used
139 vui->use_tx_spinlock = 1;
140 if (thread_index == 0)
141 {
142 //Could not find a single valid one
143 for (thread_index = 0;
144 thread_index < vlib_get_thread_main ()->n_vlib_mains;
145 thread_index++)
146 {
147 vui->per_cpu_tx_qid[thread_index] = 0;
148 }
149 return;
150 }
151 }
152}
153
154/**
155 * @brief Unassign existing interface/queue to thread mappings and re-assign
156 * new interface/queue to thread mappings
157 */
Steven Luong67f935e2019-02-01 10:23:56 -0800158static_always_inline void
159vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200160{
Steven Luong67f935e2019-02-01 10:23:56 -0800161 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200162 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200163 int rv;
Steven Luong67f935e2019-02-01 10:23:56 -0800164 u32 q = qid >> 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200165
Steven Luong67f935e2019-02-01 10:23:56 -0800166 ASSERT ((qid & 1) == 1); // should be odd
167 // Assign new queue mappings for the interface
168 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
169 vhost_user_input_node.index);
170 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, q, ~0);
Damjan Marioneabd4242020-10-07 20:59:07 +0200171 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Steven Luong67f935e2019-02-01 10:23:56 -0800172 /* Set polling as the default */
Damjan Marioneabd4242020-10-07 20:59:07 +0200173 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
Steven Luong67f935e2019-02-01 10:23:56 -0800174 txvq->qid = q;
175 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
176 if (rv)
177 vu_log_warn (vui, "unable to set rx mode for interface %d, "
178 "queue %d: rc=%d", vui->hw_if_index, q, rv);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200179}
180
181/** @brief Returns whether at least one TX and one RX vring are enabled */
182static_always_inline int
183vhost_user_intf_ready (vhost_user_intf_t * vui)
184{
185 int i, found[2] = { }; //RX + TX
186
Steven Luong2c1084a2020-12-10 20:44:22 -0800187 for (i = 0; i < vui->num_qid; i++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200188 if (vui->vrings[i].started && vui->vrings[i].enabled)
189 found[i & 1] = 1;
190
191 return found[0] && found[1];
192}
193
Steven Luong67f935e2019-02-01 10:23:56 -0800194static_always_inline void
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200195vhost_user_update_iface_state (vhost_user_intf_t * vui)
196{
197 /* if we have pointers to descriptor table, go up */
Juraj Slobodab192feb2018-10-01 12:42:07 +0200198 int is_ready = vhost_user_intf_ready (vui);
199 if (is_ready != vui->is_ready)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200200 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200201 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
202 is_ready ? "ready" : "down");
Juraj Slobodab192feb2018-10-01 12:42:07 +0200203 if (vui->admin_up)
204 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
205 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
206 : 0);
207 vui->is_ready = is_ready;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200208 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200209}
210
211static void
212vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
213{
214 u32 qid;
215 vnet_main_t *vnm = vnet_get_main ();
216
217 qid = ifq & 0xff;
218 if ((qid & 1) == 0)
219 /* Only care about the odd number, or TX, virtqueue */
220 return;
221
222 if (vhost_user_intf_ready (vui))
223 // qid >> 1 is to convert virtqueue number to vring queue index
224 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
225}
226
227static clib_error_t *
228vhost_user_callfd_read_ready (clib_file_t * uf)
229{
230 __attribute__ ((unused)) int n;
231 u8 buff[8];
232
233 n = read (uf->file_descriptor, ((char *) &buff), 8);
234
235 return 0;
236}
237
Steven Luong67f935e2019-02-01 10:23:56 -0800238static_always_inline void
239vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
240{
241 if (qid & 1) // RX is odd, TX is even
242 {
243 if (vui->vrings[qid].qid == -1)
244 vhost_user_rx_thread_placement (vui, qid);
245 }
246 else
247 vhost_user_tx_thread_placement (vui);
248}
249
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200250static clib_error_t *
251vhost_user_kickfd_read_ready (clib_file_t * uf)
252{
253 __attribute__ ((unused)) int n;
254 u8 buff[8];
255 vhost_user_intf_t *vui =
256 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
257 uf->private_data >> 8);
258 u32 qid = uf->private_data & 0xff;
259
260 n = read (uf->file_descriptor, ((char *) &buff), 8);
Steven Luong2c1084a2020-12-10 20:44:22 -0800261 vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200262 if (!vui->vrings[qid].started ||
Juraj Slobodab192feb2018-10-01 12:42:07 +0200263 (vhost_user_intf_ready (vui) != vui->is_ready))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200264 {
Steven Luong67f935e2019-02-01 10:23:56 -0800265 if (vui->vrings[qid].started == 0)
266 {
267 vui->vrings[qid].started = 1;
268 vhost_user_thread_placement (vui, qid);
269 vhost_user_update_iface_state (vui);
270 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200271 }
272
273 vhost_user_set_interrupt_pending (vui, uf->private_data);
274 return 0;
275}
276
277static_always_inline void
278vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
279{
280 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong2c1084a2020-12-10 20:44:22 -0800281
Dave Barachb7b92992018-10-17 10:38:51 -0400282 clib_memset (vring, 0, sizeof (*vring));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200283 vring->kickfd_idx = ~0;
284 vring->callfd_idx = ~0;
285 vring->errfd = -1;
Steven Luong67f935e2019-02-01 10:23:56 -0800286 vring->qid = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200287
Steven Luong2c1084a2020-12-10 20:44:22 -0800288 clib_spinlock_init (&vring->vring_lock);
289
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200290 /*
291 * We have a bug with some qemu 2.5, and this may be a fix.
292 * Feel like interpretation holy text, but this is from vhost-user.txt.
293 * "
294 * One queue pair is enabled initially. More queues are enabled
295 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
296 * "
297 * Don't know who's right, but this is what DPDK does.
298 */
299 if (qid == 0 || qid == 1)
300 vring->enabled = 1;
301}
302
303static_always_inline void
304vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
305{
306 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong67f935e2019-02-01 10:23:56 -0800307
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200308 if (vring->kickfd_idx != ~0)
309 {
310 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
311 vring->kickfd_idx);
312 clib_file_del (&file_main, uf);
313 vring->kickfd_idx = ~0;
314 }
315 if (vring->callfd_idx != ~0)
316 {
317 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
318 vring->callfd_idx);
319 clib_file_del (&file_main, uf);
320 vring->callfd_idx = ~0;
321 }
322 if (vring->errfd != -1)
323 {
324 close (vring->errfd);
325 vring->errfd = -1;
326 }
Steven Luong67f935e2019-02-01 10:23:56 -0800327
Steven Luong2c1084a2020-12-10 20:44:22 -0800328 clib_spinlock_free (&vring->vring_lock);
329
Steven Luong67f935e2019-02-01 10:23:56 -0800330 // save the qid so that we don't need to unassign and assign_rx_thread
331 // when the interface comes back up. They are expensive calls.
332 u16 q = vui->vrings[qid].qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200333 vhost_user_vring_init (vui, qid);
Steven Luong67f935e2019-02-01 10:23:56 -0800334 vui->vrings[qid].qid = q;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200335}
336
337static_always_inline void
338vhost_user_if_disconnect (vhost_user_intf_t * vui)
339{
340 vnet_main_t *vnm = vnet_get_main ();
341 int q;
342
343 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
344
345 if (vui->clib_file_index != ~0)
346 {
347 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
348 vui->clib_file_index = ~0;
349 }
350
Juraj Slobodab192feb2018-10-01 12:42:07 +0200351 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200352
Steven Luong2c1084a2020-12-10 20:44:22 -0800353 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200354 vhost_user_vring_close (vui, q);
355
356 unmap_all_mem_regions (vui);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200357 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200358}
359
360static clib_error_t *
361vhost_user_socket_read (clib_file_t * uf)
362{
Steven Luong67f935e2019-02-01 10:23:56 -0800363 int n, i, j;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200364 int fd, number_of_fds = 0;
365 int fds[VHOST_MEMORY_MAX_NREGIONS];
366 vhost_user_msg_t msg;
367 struct msghdr mh;
368 struct iovec iov[1];
369 vhost_user_main_t *vum = &vhost_user_main;
370 vhost_user_intf_t *vui;
371 struct cmsghdr *cmsg;
372 u8 q;
373 clib_file_t template = { 0 };
374 vnet_main_t *vnm = vnet_get_main ();
Steven Luong67f935e2019-02-01 10:23:56 -0800375 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200376
377 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
378
379 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
380
Dave Barachb7b92992018-10-17 10:38:51 -0400381 clib_memset (&mh, 0, sizeof (mh));
382 clib_memset (control, 0, sizeof (control));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200383
384 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
385 fds[i] = -1;
386
387 /* set the payload */
388 iov[0].iov_base = (void *) &msg;
389 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
390
391 mh.msg_iov = iov;
392 mh.msg_iovlen = 1;
393 mh.msg_control = control;
394 mh.msg_controllen = sizeof (control);
395
396 n = recvmsg (uf->file_descriptor, &mh, 0);
397
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200398 if (n != VHOST_USER_MSG_HDR_SZ)
399 {
400 if (n == -1)
401 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200402 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
403 strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200404 }
405 else
406 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200407 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
408 n, VHOST_USER_MSG_HDR_SZ);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200409 }
410 goto close_socket;
411 }
412
413 if (mh.msg_flags & MSG_CTRUNC)
414 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200415 vu_log_debug (vui, "MSG_CTRUNC is set");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200416 goto close_socket;
417 }
418
419 cmsg = CMSG_FIRSTHDR (&mh);
420
421 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
422 (cmsg->cmsg_type == SCM_RIGHTS) &&
423 (cmsg->cmsg_len - CMSG_LEN (0) <=
424 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
425 {
426 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
Dave Barach178cf492018-11-13 16:34:13 -0500427 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200428 }
429
430 /* version 1, no reply bit set */
431 if ((msg.flags & 7) != 1)
432 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200433 vu_log_debug (vui, "malformed message received. closing socket");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200434 goto close_socket;
435 }
436
437 {
438 int rv;
439 rv =
440 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
441 msg.size);
442 if (rv < 0)
443 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200444 vu_log_debug (vui, "read failed %s", strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200445 goto close_socket;
446 }
447 else if (rv != msg.size)
448 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200449 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
450 msg.size);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200451 goto close_socket;
452 }
453 }
454
455 switch (msg.request)
456 {
457 case VHOST_USER_GET_FEATURES:
458 msg.flags |= 4;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200459 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
460 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
461 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
462 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
463 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
464 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
465 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
466 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
467 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200468 msg.u64 &= vui->feature_mask;
Steven Luong4208a4c2019-05-06 08:51:56 -0700469
470 if (vui->enable_gso)
471 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700472 if (vui->enable_packed)
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200473 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
Steven Luong4208a4c2019-05-06 08:51:56 -0700474
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200475 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200476 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
477 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800478 n =
479 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
480 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
481 {
482 vu_log_debug (vui, "could not send message response");
483 goto close_socket;
484 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200485 break;
486
487 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200488 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
489 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200490
491 vui->features = msg.u64;
492
493 if (vui->features &
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200494 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
495 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200496 vui->virtio_net_hdr_sz = 12;
497 else
498 vui->virtio_net_hdr_sz = 10;
499
500 vui->is_any_layout =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200501 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200502
503 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Steven Luong4208a4c2019-05-06 08:51:56 -0700504 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
505 if (vui->enable_gso &&
Steven Luonga75ad872019-08-06 21:51:34 -0700506 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
507 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
508 hw->flags |=
509 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
510 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Steven Luong4208a4c2019-05-06 08:51:56 -0700511 else
Steven Luonga75ad872019-08-06 21:51:34 -0700512 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
513 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200514 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200515 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700516 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200517 break;
518
519 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200520 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
521 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200522
523 if ((msg.memory.nregions < 1) ||
524 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
525 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200526 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
527 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200528 goto close_socket;
529 }
530
531 if (msg.memory.nregions != number_of_fds)
532 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200533 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200534 goto close_socket;
535 }
Steven Luong67f935e2019-02-01 10:23:56 -0800536
537 /* Do the mmap without barrier sync */
538 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
539 for (i = 0; i < msg.memory.nregions; i++)
540 {
541 long page_sz = get_huge_page_size (fds[i]);
542
543 /* align size to page */
544 ssize_t map_sz = (msg.memory.regions[i].memory_size +
545 msg.memory.regions[i].mmap_offset +
546 page_sz - 1) & ~(page_sz - 1);
547
548 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
549 MAP_SHARED, fds[i], 0);
550 if (region_mmap_addr[i] == MAP_FAILED)
551 {
552 vu_log_err (vui, "failed to map memory. errno is %d", errno);
553 for (j = 0; j < i; j++)
554 munmap (region_mmap_addr[j], map_sz);
555 goto close_socket;
556 }
557 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
558 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
559 region_mmap_addr[i], page_sz);
560 }
561
562 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200563 unmap_all_mem_regions (vui);
564 for (i = 0; i < msg.memory.nregions; i++)
565 {
Dave Barach178cf492018-11-13 16:34:13 -0500566 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
567 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200568
Steven Luong67f935e2019-02-01 10:23:56 -0800569 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200570 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
571 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
572 vui->regions[i].memory_size;
573
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200574 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
575 vui->region_mmap_fd[i] = fds[i];
576
577 vui->nregions++;
578 }
Steven Luong4442f7c2019-10-02 07:33:48 -0700579
580 /*
581 * Re-compute desc, used, and avail descriptor table if vring address
582 * is set.
583 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800584 for (q = 0; q < vui->num_qid; q++)
Steven Luong4442f7c2019-10-02 07:33:48 -0700585 {
586 if (vui->vrings[q].desc_user_addr &&
587 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
588 {
589 vui->vrings[q].desc =
590 map_user_mem (vui, vui->vrings[q].desc_user_addr);
591 vui->vrings[q].used =
592 map_user_mem (vui, vui->vrings[q].used_user_addr);
593 vui->vrings[q].avail =
594 map_user_mem (vui, vui->vrings[q].avail_user_addr);
595 }
596 }
Steven Luong67f935e2019-02-01 10:23:56 -0800597 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200598 break;
599
600 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200601 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
602 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200603
604 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
605 (msg.state.num == 0) || /* it cannot be zero */
Benoît Ganne32526a42020-12-08 19:08:05 +0100606 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800607 (msg.state.index >= vui->num_qid))
608 {
609 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
610 " %d, msg.state.index %d, curruent max q %d",
611 msg.state.num, msg.state.index, vui->num_qid);
612 goto close_socket;
613 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200614 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
615 break;
616
617 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200618 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
619 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200620
Steven Luong2c1084a2020-12-10 20:44:22 -0800621 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200622 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200623 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800624 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200625 goto close_socket;
626 }
627
628 if (msg.size < sizeof (msg.addr))
629 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200630 vu_log_debug (vui, "vhost message is too short (%d < %d)",
631 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200632 goto close_socket;
633 }
634
Steven Luong67f935e2019-02-01 10:23:56 -0800635 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
636 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
637 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200638
Steven Luong67f935e2019-02-01 10:23:56 -0800639 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200640 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200641 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
642 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200643 goto close_socket;
644 }
645
Steven Luong4442f7c2019-10-02 07:33:48 -0700646 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
647 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
648 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
649
Steven Luong67f935e2019-02-01 10:23:56 -0800650 vlib_worker_thread_barrier_sync (vm);
651 vui->vrings[msg.state.index].desc = desc;
652 vui->vrings[msg.state.index].used = used;
653 vui->vrings[msg.state.index].avail = avail;
654
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200655 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
656 vui->vrings[msg.state.index].log_used =
657 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
658
659 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
660 the ring is initialized in an enabled state. */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200661 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800662 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200663
664 vui->vrings[msg.state.index].last_used_idx =
665 vui->vrings[msg.state.index].last_avail_idx =
666 vui->vrings[msg.state.index].used->idx;
667
668 /* tell driver that we don't want interrupts */
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700669 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 Luong67f935e2019-02-01 10:23:56 -0800674 vlib_worker_thread_barrier_release (vm);
675 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200676 break;
677
678 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200679 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200680 break;
681
682 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200683 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
684 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200685 break;
686
687 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200688 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
689 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200690
691 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800692 if (vui->num_qid > q)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200693 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800694 /* 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 Kazmie7cde312018-06-26 17:20:11 +0200735 }
736
737 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
738 {
739 if (number_of_fds != 1)
740 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200741 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200742 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;
749 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
750 }
751 else
752 vui->vrings[q].callfd_idx = ~0;
753 break;
754
755 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200756 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
757 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200758
759 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800760 if (q >= vui->num_qid)
761 {
762 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
763 " %u >= %u", q, vui->num_qid);
764 goto close_socket;
765 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200766
767 if (vui->vrings[q].kickfd_idx != ~0)
768 {
769 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
770 vui->vrings[q].kickfd_idx);
771 clib_file_del (&file_main, uf);
772 vui->vrings[q].kickfd_idx = ~0;
773 }
774
775 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
776 {
777 if (number_of_fds != 1)
778 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200779 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200780 goto close_socket;
781 }
782
783 template.read_function = vhost_user_kickfd_read_ready;
784 template.file_descriptor = fds[0];
785 template.private_data =
786 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
787 q;
788 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
789 }
790 else
791 {
792 //When no kickfd is set, the queue is initialized as started
793 vui->vrings[q].kickfd_idx = ~0;
794 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800795 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200796 }
Steven Luong67f935e2019-02-01 10:23:56 -0800797 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200798 break;
799
800 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200801 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
802 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200803
804 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800805 if (q >= vui->num_qid)
806 {
807 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
808 " %u >= %u", q, vui->num_qid);
809 goto close_socket;
810 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200811
812 if (vui->vrings[q].errfd != -1)
813 close (vui->vrings[q].errfd);
814
815 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
816 {
817 if (number_of_fds != 1)
818 goto close_socket;
819
820 vui->vrings[q].errfd = fds[0];
821 }
822 else
823 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200824 break;
825
826 case VHOST_USER_SET_VRING_BASE:
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700827 vu_log_debug (vui,
828 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200829 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong2c1084a2020-12-10 20:44:22 -0800830 if (msg.state.index >= vui->num_qid)
831 {
832 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
833 " %u >= %u", msg.state.index, vui->num_qid);
834 goto close_socket;
835 }
Steven Luong67f935e2019-02-01 10:23:56 -0800836 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200837 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700838 if (vhost_user_is_packed_ring_supported (vui))
839 {
840 /*
841 * 0 1 2 3
842 * 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
843 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
844 * | last avail idx | | last used idx | |
845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
846 * ^ ^
847 * | |
848 * avail wrap counter used wrap counter
849 */
850 /* last avail idx at bit 0-14. */
851 vui->vrings[msg.state.index].last_avail_idx =
852 msg.state.num & 0x7fff;
853 /* avail wrap counter at bit 15 */
854 vui->vrings[msg.state.index].avail_wrap_counter =
855 ! !(msg.state.num & (1 << 15));
856
857 /*
858 * Although last_used_idx is passed in the upper 16 bits in qemu
859 * implementation, in practice, last_avail_idx and last_used_idx are
860 * usually the same. As a result, DPDK does not bother to pass us
861 * last_used_idx. The spec is not clear on thex coding. I figured it
862 * out by reading the qemu code. So let's just read last_avail_idx
863 * and set last_used_idx equals to last_avail_idx.
864 */
865 vui->vrings[msg.state.index].last_used_idx =
866 vui->vrings[msg.state.index].last_avail_idx;
867 vui->vrings[msg.state.index].used_wrap_counter =
868 vui->vrings[msg.state.index].avail_wrap_counter;
869
870 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
871 vui->vrings[msg.state.index].avail_wrap_counter =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200872 VRING_DESC_F_AVAIL;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700873 }
Steven Luong67f935e2019-02-01 10:23:56 -0800874 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200875 break;
876
877 case VHOST_USER_GET_VRING_BASE:
Steven Luong2c1084a2020-12-10 20:44:22 -0800878 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200879 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200880 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800881 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200882 goto close_socket;
883 }
884
Steven Luong67f935e2019-02-01 10:23:56 -0800885 /* protection is needed to prevent rx/tx from changing last_avail_idx */
886 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200887 /*
888 * Copy last_avail_idx from the vring before closing it because
889 * closing the vring also initializes the vring last_avail_idx
890 */
891 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700892 if (vhost_user_is_packed_ring_supported (vui))
893 {
894 msg.state.num =
895 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
896 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
897 msg.state.num |=
898 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
899 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
900 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200901 msg.flags |= 4;
902 msg.size = sizeof (msg.state);
903
Steven Luong67f935e2019-02-01 10:23:56 -0800904 /*
905 * Spec says: Client must [...] stop ring upon receiving
906 * VHOST_USER_GET_VRING_BASE
907 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200908 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800909 vlib_worker_thread_barrier_release (vm);
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700910 vu_log_debug (vui,
911 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200912 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800913 n =
914 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
915 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
916 {
917 vu_log_debug (vui, "could not send message response");
918 goto close_socket;
919 }
920 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200921 break;
922
923 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200924 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200925 break;
926
927 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800928 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
929 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200930
Steven Luong67f935e2019-02-01 10:23:56 -0800931 if (msg.size != sizeof (msg.log))
932 {
933 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
934 " %d instead of %d", msg.size, sizeof (msg.log));
935 goto close_socket;
936 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200937
Steven Luong67f935e2019-02-01 10:23:56 -0800938 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
939 {
940 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
941 "VHOST_USER_SET_LOG_BASE received");
942 goto close_socket;
943 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200944
Steven Luong67f935e2019-02-01 10:23:56 -0800945 fd = fds[0];
946 /* align size to page */
947 long page_sz = get_huge_page_size (fd);
948 ssize_t map_sz =
949 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200950
Steven Luong67f935e2019-02-01 10:23:56 -0800951 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
952 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200953
Steven Luong67f935e2019-02-01 10:23:56 -0800954 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
955 "mapped 0x%lx", map_sz, msg.log.offset, fd,
956 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200957
Steven Luong67f935e2019-02-01 10:23:56 -0800958 if (log_base_addr == MAP_FAILED)
959 {
960 vu_log_err (vui, "failed to map memory. errno is %d", errno);
961 goto close_socket;
962 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200963
Steven Luong67f935e2019-02-01 10:23:56 -0800964 vlib_worker_thread_barrier_sync (vm);
965 vui->log_base_addr = log_base_addr;
966 vui->log_base_addr += msg.log.offset;
967 vui->log_size = msg.log.size;
968 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200969
Steven Luong67f935e2019-02-01 10:23:56 -0800970 msg.flags |= 4;
971 msg.size = sizeof (msg.u64);
972 n =
973 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
974 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
975 {
976 vu_log_debug (vui, "could not send message response");
977 goto close_socket;
978 }
979 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200980
981 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200982 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200983 break;
984
985 case VHOST_USER_GET_PROTOCOL_FEATURES:
986 msg.flags |= 4;
987 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
988 (1 << VHOST_USER_PROTOCOL_F_MQ);
989 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200990 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
991 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800992 n =
993 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
994 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
995 {
996 vu_log_debug (vui, "could not send message response");
997 goto close_socket;
998 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200999 break;
1000
1001 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001002 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1003 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001004 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001005 break;
1006
1007 case VHOST_USER_GET_QUEUE_NUM:
1008 msg.flags |= 4;
Steven Luong2c1084a2020-12-10 20:44:22 -08001009 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001010 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001011 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1012 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001013 n =
1014 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1015 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1016 {
1017 vu_log_debug (vui, "could not send message response");
1018 goto close_socket;
1019 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001020 break;
1021
1022 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001023 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1024 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1025 msg.state.index);
Steven Luong2c1084a2020-12-10 20:44:22 -08001026 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001027 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001028 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
Steven Luong2c1084a2020-12-10 20:44:22 -08001029 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001030 goto close_socket;
1031 }
1032
1033 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -08001034 vhost_user_thread_placement (vui, msg.state.index);
1035 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001036 break;
1037
1038 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001039 vu_log_debug (vui, "unknown vhost-user message %d received. "
1040 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001041 goto close_socket;
1042 }
1043
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001044 return 0;
1045
1046close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -08001047 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001048 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -08001049 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001050 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001051 return 0;
1052}
1053
1054static clib_error_t *
1055vhost_user_socket_error (clib_file_t * uf)
1056{
1057 vlib_main_t *vm = vlib_get_main ();
1058 vhost_user_main_t *vum = &vhost_user_main;
1059 vhost_user_intf_t *vui =
1060 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1061
Jerome Tollet2f54c272018-10-02 11:41:11 +02001062 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001063 vlib_worker_thread_barrier_sync (vm);
1064 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001065 vlib_worker_thread_barrier_release (vm);
1066 return 0;
1067}
1068
1069static clib_error_t *
1070vhost_user_socksvr_accept_ready (clib_file_t * uf)
1071{
1072 int client_fd, client_len;
1073 struct sockaddr_un client;
1074 clib_file_t template = { 0 };
1075 vhost_user_main_t *vum = &vhost_user_main;
1076 vhost_user_intf_t *vui;
1077
1078 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1079
1080 client_len = sizeof (client);
1081 client_fd = accept (uf->file_descriptor,
1082 (struct sockaddr *) &client,
1083 (socklen_t *) & client_len);
1084
1085 if (client_fd < 0)
1086 return clib_error_return_unix (0, "accept");
1087
1088 if (vui->clib_file_index != ~0)
1089 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001090 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1091 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001092 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1093 }
1094
Jerome Tollet2f54c272018-10-02 11:41:11 +02001095 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1096 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001097 template.read_function = vhost_user_socket_read;
1098 template.error_function = vhost_user_socket_error;
1099 template.file_descriptor = client_fd;
1100 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1101 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001102 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001103 return 0;
1104}
1105
1106static clib_error_t *
1107vhost_user_init (vlib_main_t * vm)
1108{
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001109 vhost_user_main_t *vum = &vhost_user_main;
1110 vlib_thread_main_t *tm = vlib_get_thread_main ();
1111
Jerome Tollet2f54c272018-10-02 11:41:11 +02001112 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1113
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001114 vum->coalesce_frames = 32;
1115 vum->coalesce_time = 1e-3;
1116
1117 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1118
1119 vhost_cpu_t *cpu;
1120 vec_foreach (cpu, vum->cpus)
1121 {
1122 /* This is actually not necessary as validate already zeroes it
1123 * Just keeping the loop here for later because I am lazy. */
1124 cpu->rx_buffers_len = 0;
1125 }
1126
1127 vum->random = random_default_seed ();
1128
1129 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1130
1131 return 0;
1132}
1133
Dave Barachf8d50682019-05-14 18:01:44 -04001134/* *INDENT-OFF* */
1135VLIB_INIT_FUNCTION (vhost_user_init) =
1136{
1137 .runs_after = VLIB_INITS("ip4_init"),
1138};
1139/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001140
1141static uword
1142vhost_user_send_interrupt_process (vlib_main_t * vm,
1143 vlib_node_runtime_t * rt, vlib_frame_t * f)
1144{
1145 vhost_user_intf_t *vui;
1146 f64 timeout = 3153600000.0 /* 100 years */ ;
1147 uword event_type, *event_data = 0;
1148 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -08001149 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001150 f64 now, poll_time_remaining;
1151 f64 next_timeout;
1152 u8 stop_timer = 0;
1153
1154 while (1)
1155 {
1156 poll_time_remaining =
1157 vlib_process_wait_for_event_or_clock (vm, timeout);
1158 event_type = vlib_process_get_events (vm, &event_data);
1159 vec_reset_length (event_data);
1160
1161 /*
1162 * Use the remaining timeout if it is less than coalesce time to avoid
1163 * resetting the existing timer in the middle of expiration
1164 */
1165 timeout = poll_time_remaining;
1166 if (vlib_process_suspend_time_is_zero (timeout) ||
1167 (timeout > vum->coalesce_time))
1168 timeout = vum->coalesce_time;
1169
1170 now = vlib_time_now (vm);
1171 switch (event_type)
1172 {
1173 case VHOST_USER_EVENT_STOP_TIMER:
1174 stop_timer = 1;
1175 break;
1176
1177 case VHOST_USER_EVENT_START_TIMER:
1178 stop_timer = 0;
1179 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1180 break;
1181 /* fall through */
1182
1183 case ~0:
1184 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001185 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001186 next_timeout = timeout;
Steven Luong2c1084a2020-12-10 20:44:22 -08001187 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001188 {
Steven Luong67f935e2019-02-01 10:23:56 -08001189 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1190 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001191
Steven Luong67f935e2019-02-01 10:23:56 -08001192 if (txvq->qid == -1)
1193 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001194 if (txvq->n_since_last_int)
1195 {
1196 if (now >= txvq->int_deadline)
1197 vhost_user_send_call (vm, txvq);
1198 else
1199 next_timeout = txvq->int_deadline - now;
1200 }
1201
1202 if (rxvq->n_since_last_int)
1203 {
1204 if (now >= rxvq->int_deadline)
1205 vhost_user_send_call (vm, rxvq);
1206 else
1207 next_timeout = rxvq->int_deadline - now;
1208 }
1209
1210 if ((next_timeout < timeout) && (next_timeout > 0.0))
1211 timeout = next_timeout;
1212 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001213 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001214 /* *INDENT-ON* */
1215 break;
1216
1217 default:
1218 clib_warning ("BUG: unhandled event type %d", event_type);
1219 break;
1220 }
1221 /* No less than 1 millisecond */
1222 if (timeout < 1e-3)
1223 timeout = 1e-3;
1224 if (stop_timer)
1225 timeout = 3153600000.0;
1226 }
1227 return 0;
1228}
1229
1230/* *INDENT-OFF* */
1231VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1232 .function = vhost_user_send_interrupt_process,
1233 .type = VLIB_NODE_TYPE_PROCESS,
1234 .name = "vhost-user-send-interrupt-process",
1235};
1236/* *INDENT-ON* */
1237
1238static uword
1239vhost_user_process (vlib_main_t * vm,
1240 vlib_node_runtime_t * rt, vlib_frame_t * f)
1241{
1242 vhost_user_main_t *vum = &vhost_user_main;
1243 vhost_user_intf_t *vui;
1244 struct sockaddr_un sun;
1245 int sockfd;
1246 clib_file_t template = { 0 };
1247 f64 timeout = 3153600000.0 /* 100 years */ ;
1248 uword *event_data = 0;
1249
1250 sockfd = -1;
1251 sun.sun_family = AF_UNIX;
1252 template.read_function = vhost_user_socket_read;
1253 template.error_function = vhost_user_socket_error;
1254
1255 while (1)
1256 {
1257 vlib_process_wait_for_event_or_clock (vm, timeout);
1258 vlib_process_get_events (vm, &event_data);
1259 vec_reset_length (event_data);
1260
1261 timeout = 3.0;
1262
1263 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001264 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001265
1266 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1267 if (vui->clib_file_index == ~0)
1268 {
1269 if ((sockfd < 0) &&
1270 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1271 {
1272 /*
1273 * 1st time error or new error for this interface,
1274 * spit out the message and record the error
1275 */
1276 if (!vui->sock_errno || (vui->sock_errno != errno))
1277 {
1278 clib_unix_warning
1279 ("Error: Could not open unix socket for %s",
1280 vui->sock_filename);
1281 vui->sock_errno = errno;
1282 }
1283 continue;
1284 }
1285
1286 /* try to connect */
1287 strncpy (sun.sun_path, (char *) vui->sock_filename,
1288 sizeof (sun.sun_path) - 1);
Damjan Marion62c25ab2020-12-12 23:32:12 +01001289 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001290
1291 /* Avoid hanging VPP if the other end does not accept */
1292 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1293 clib_unix_warning ("fcntl");
1294
1295 if (connect (sockfd, (struct sockaddr *) &sun,
1296 sizeof (struct sockaddr_un)) == 0)
1297 {
1298 /* Set the socket to blocking as it was before */
1299 if (fcntl(sockfd, F_SETFL, 0) < 0)
1300 clib_unix_warning ("fcntl2");
1301
1302 vui->sock_errno = 0;
1303 template.file_descriptor = sockfd;
1304 template.private_data =
1305 vui - vhost_user_main.vhost_user_interfaces;
1306 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001307 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001308
1309 /* This sockfd is considered consumed */
1310 sockfd = -1;
1311 }
1312 else
1313 {
1314 vui->sock_errno = errno;
1315 }
1316 }
1317 else
1318 {
1319 /* check if socket is alive */
1320 int error = 0;
1321 socklen_t len = sizeof (error);
1322 int fd = UNIX_GET_FD(vui->clib_file_index);
1323 int retval =
1324 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1325
1326 if (retval)
1327 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001328 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001329 vhost_user_if_disconnect (vui);
1330 }
1331 }
1332 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001333 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001334 /* *INDENT-ON* */
1335 }
1336 return 0;
1337}
1338
1339/* *INDENT-OFF* */
1340VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1341 .function = vhost_user_process,
1342 .type = VLIB_NODE_TYPE_PROCESS,
1343 .name = "vhost-user-process",
1344};
1345/* *INDENT-ON* */
1346
1347/**
1348 * Disables and reset interface structure.
1349 * It can then be either init again, or removed from used interfaces.
1350 */
1351static void
1352vhost_user_term_if (vhost_user_intf_t * vui)
1353{
1354 int q;
1355 vhost_user_main_t *vum = &vhost_user_main;
1356
1357 // disconnect interface sockets
1358 vhost_user_if_disconnect (vui);
Steven Luong4208a4c2019-05-06 08:51:56 -07001359 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001360 vhost_user_update_iface_state (vui);
1361
Steven Luong2c1084a2020-12-10 20:44:22 -08001362 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001363 {
Steven Luong67f935e2019-02-01 10:23:56 -08001364 // Remove existing queue mapping for the interface
1365 if (q & 1)
1366 {
1367 int rv;
1368 vnet_main_t *vnm = vnet_get_main ();
1369 vhost_user_vring_t *txvq = &vui->vrings[q];
1370
1371 if (txvq->qid != -1)
1372 {
1373 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1374 vui->hw_if_index,
1375 q >> 1);
1376 if (rv)
1377 vu_log_warn (vui, "unable to unassign interface %d, "
1378 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1379 }
1380 }
1381
Steven Luong2c1084a2020-12-10 20:44:22 -08001382 clib_spinlock_free (&vui->vrings[q].vring_lock);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001383 }
1384
1385 if (vui->unix_server_index != ~0)
1386 {
1387 //Close server socket
1388 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1389 vui->unix_server_index);
1390 clib_file_del (&file_main, uf);
1391 vui->unix_server_index = ~0;
1392 unlink (vui->sock_filename);
1393 }
1394
1395 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1396 &vui->if_index);
1397}
1398
1399int
1400vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1401{
1402 vhost_user_main_t *vum = &vhost_user_main;
1403 vhost_user_intf_t *vui;
1404 int rv = 0;
1405 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001406 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001407
Dave Barach3940de32019-07-23 16:28:36 -04001408 if (!
1409 (hwif =
1410 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1411 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001412 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1413
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001414 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1415
Jerome Tollet2f54c272018-10-02 11:41:11 +02001416 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1417 hwif->name, hwif->dev_instance);
1418
Steven Luong2c1084a2020-12-10 20:44:22 -08001419 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08001420 {
1421 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001422
Steven Luong67f935e2019-02-01 10:23:56 -08001423 if (txvq->qid == -1)
1424 continue;
1425 if ((vum->ifq_count > 0) &&
Damjan Marioneabd4242020-10-07 20:59:07 +02001426 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1427 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
Steven Luong67f935e2019-02-01 10:23:56 -08001428 {
1429 vum->ifq_count--;
1430 // Stop the timer if there is no more interrupt interface/queue
1431 if ((vum->ifq_count == 0) &&
1432 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1433 {
1434 vlib_process_signal_event (vm,
1435 vhost_user_send_interrupt_node.index,
1436 VHOST_USER_EVENT_STOP_TIMER, 0);
1437 break;
1438 }
1439 }
1440 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001441
1442 // Disable and reset interface
1443 vhost_user_term_if (vui);
1444
1445 // Reset renumbered iface
1446 if (hwif->dev_instance <
1447 vec_len (vum->show_dev_instance_by_real_dev_instance))
1448 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1449
1450 // Delete ethernet interface
1451 ethernet_delete_interface (vnm, vui->hw_if_index);
1452
Steven Luong2c1084a2020-12-10 20:44:22 -08001453 // free vrings
1454 vec_free (vui->vrings);
1455
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001456 // Back to pool
1457 pool_put (vum->vhost_user_interfaces, vui);
1458
1459 return rv;
1460}
1461
1462static clib_error_t *
1463vhost_user_exit (vlib_main_t * vm)
1464{
1465 vnet_main_t *vnm = vnet_get_main ();
1466 vhost_user_main_t *vum = &vhost_user_main;
1467 vhost_user_intf_t *vui;
1468
1469 vlib_worker_thread_barrier_sync (vlib_get_main ());
1470 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001471 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001472 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001473 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001474 /* *INDENT-ON* */
1475 vlib_worker_thread_barrier_release (vlib_get_main ());
1476 return 0;
1477}
1478
1479VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1480
1481/**
1482 * Open server unix socket on specified sock_filename.
1483 */
1484static int
1485vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1486{
1487 int rv = 0;
1488 struct sockaddr_un un = { };
1489 int fd;
1490 /* create listening socket */
1491 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1492 return VNET_API_ERROR_SYSCALL_ERROR_1;
1493
1494 un.sun_family = AF_UNIX;
1495 strncpy ((char *) un.sun_path, (char *) sock_filename,
1496 sizeof (un.sun_path) - 1);
1497
1498 /* remove if exists */
1499 unlink ((char *) sock_filename);
1500
1501 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1502 {
1503 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1504 goto error;
1505 }
1506
1507 if (listen (fd, 1) == -1)
1508 {
1509 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1510 goto error;
1511 }
1512
1513 *sock_fd = fd;
1514 return 0;
1515
1516error:
1517 close (fd);
1518 return rv;
1519}
1520
1521/**
1522 * Create ethernet interface for vhost user interface.
1523 */
1524static void
1525vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1526 vhost_user_intf_t * vui, u8 * hwaddress)
1527{
1528 vhost_user_main_t *vum = &vhost_user_main;
1529 u8 hwaddr[6];
1530 clib_error_t *error;
1531
1532 /* create hw and sw interface */
1533 if (hwaddress)
1534 {
1535 clib_memcpy (hwaddr, hwaddress, 6);
1536 }
1537 else
1538 {
1539 random_u32 (&vum->random);
1540 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1541 hwaddr[0] = 2;
1542 hwaddr[1] = 0xfe;
1543 }
1544
1545 error = ethernet_register_interface
1546 (vnm,
1547 vhost_user_device_class.index,
1548 vui - vum->vhost_user_interfaces /* device instance */ ,
1549 hwaddr /* ethernet address */ ,
1550 &vui->hw_if_index, 0 /* flag change */ );
1551
1552 if (error)
1553 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001554}
1555
1556/*
1557 * Initialize vui with specified attributes
1558 */
1559static void
1560vhost_user_vui_init (vnet_main_t * vnm,
1561 vhost_user_intf_t * vui,
1562 int server_sock_fd,
1563 const char *sock_filename,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001564 u64 feature_mask, u32 * sw_if_index, u8 enable_gso,
1565 u8 enable_packed)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001566{
1567 vnet_sw_interface_t *sw;
1568 int q;
1569 vhost_user_main_t *vum = &vhost_user_main;
1570 vnet_hw_interface_t *hw;
1571
1572 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1573 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1574 if (server_sock_fd != -1)
1575 {
1576 clib_file_t template = { 0 };
1577 template.read_function = vhost_user_socksvr_accept_ready;
1578 template.file_descriptor = server_sock_fd;
1579 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1580 vui->unix_server_index = clib_file_add (&file_main, &template);
1581 }
1582 else
1583 {
1584 vui->unix_server_index = ~0;
1585 }
1586
1587 vui->sw_if_index = sw->sw_if_index;
1588 strncpy (vui->sock_filename, sock_filename,
1589 ARRAY_LEN (vui->sock_filename) - 1);
1590 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001591 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001592 vui->feature_mask = feature_mask;
1593 vui->clib_file_index = ~0;
1594 vui->log_base_addr = 0;
1595 vui->if_index = vui - vum->vhost_user_interfaces;
Steven Luong4208a4c2019-05-06 08:51:56 -07001596 vui->enable_gso = enable_gso;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001597 vui->enable_packed = enable_packed;
Steven Luong4208a4c2019-05-06 08:51:56 -07001598 /*
1599 * enable_gso takes precedence over configurable feature mask if there
1600 * is a clash.
1601 * if feature mask disables gso, but enable_gso is configured,
1602 * then gso is enable
1603 * if feature mask enables gso, but enable_gso is not configured,
1604 * then gso is enable
1605 *
1606 * if gso is enable via feature mask, it must enable both host and guest
1607 * gso feature mask, we don't support one sided GSO or partial GSO.
1608 */
1609 if ((vui->enable_gso == 0) &&
1610 ((feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) ==
1611 (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1612 vui->enable_gso = 1;
1613 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001614 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1615 &vui->if_index, 0);
1616
Steven Luong2c1084a2020-12-10 20:44:22 -08001617 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1618 CLIB_CACHE_LINE_BYTES);
1619 vui->num_qid = 2;
1620 for (q = 0; q < vec_len (vui->vrings); q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001621 vhost_user_vring_init (vui, q);
1622
1623 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1624 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1625
1626 if (sw_if_index)
1627 *sw_if_index = vui->sw_if_index;
1628
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001629 vec_validate (vui->per_cpu_tx_qid,
1630 vlib_get_thread_main ()->n_vlib_mains - 1);
1631 vhost_user_tx_thread_placement (vui);
1632}
1633
1634int
1635vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1636 const char *sock_filename,
1637 u8 is_server,
1638 u32 * sw_if_index,
1639 u64 feature_mask,
Steven Luong4208a4c2019-05-06 08:51:56 -07001640 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001641 u8 enable_gso, u8 enable_packed)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001642{
1643 vhost_user_intf_t *vui = NULL;
1644 u32 sw_if_idx = ~0;
1645 int rv = 0;
1646 int server_sock_fd = -1;
1647 vhost_user_main_t *vum = &vhost_user_main;
1648 uword *if_index;
1649
1650 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1651 {
1652 return VNET_API_ERROR_INVALID_ARGUMENT;
1653 }
1654
1655 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1656 if (if_index)
1657 {
1658 if (sw_if_index)
1659 {
1660 vui = &vum->vhost_user_interfaces[*if_index];
1661 *sw_if_index = vui->sw_if_index;
1662 }
1663 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1664 }
1665
1666 if (is_server)
1667 {
1668 if ((rv =
1669 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1670 {
1671 return rv;
1672 }
1673 }
1674
Steven Luong67f935e2019-02-01 10:23:56 -08001675 /* Protect the uninitialized vui from being dispatched by rx/tx */
1676 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001677 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001678 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Steven Luong67f935e2019-02-01 10:23:56 -08001679 vlib_worker_thread_barrier_release (vm);
1680
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001681 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001682 feature_mask, &sw_if_idx, enable_gso, enable_packed);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001683 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001684 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001685
1686 if (renumber)
1687 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1688
1689 if (sw_if_index)
1690 *sw_if_index = sw_if_idx;
1691
1692 // Process node must connect
1693 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1694
1695 return rv;
1696}
1697
1698int
1699vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1700 const char *sock_filename,
1701 u8 is_server,
1702 u32 sw_if_index,
Steven Luong4208a4c2019-05-06 08:51:56 -07001703 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001704 u8 enable_gso, u8 enable_packed)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001705{
1706 vhost_user_main_t *vum = &vhost_user_main;
1707 vhost_user_intf_t *vui = NULL;
1708 u32 sw_if_idx = ~0;
1709 int server_sock_fd = -1;
1710 int rv = 0;
1711 vnet_hw_interface_t *hwif;
1712 uword *if_index;
1713
Dave Barach3940de32019-07-23 16:28:36 -04001714 if (!
1715 (hwif =
1716 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1717 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001718 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1719
1720 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1721 return VNET_API_ERROR_INVALID_ARGUMENT;
1722
1723 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1724
1725 /*
1726 * Disallow changing the interface to have the same path name
1727 * as other interface
1728 */
1729 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1730 if (if_index && (*if_index != vui->if_index))
1731 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1732
1733 // First try to open server socket
1734 if (is_server)
1735 if ((rv = vhost_user_init_server_sock (sock_filename,
1736 &server_sock_fd)) != 0)
1737 return rv;
1738
1739 vhost_user_term_if (vui);
1740 vhost_user_vui_init (vnm, vui, server_sock_fd,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001741 sock_filename, feature_mask, &sw_if_idx, enable_gso,
1742 enable_packed);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001743
1744 if (renumber)
1745 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1746
1747 // Process node must connect
1748 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1749
1750 return rv;
1751}
1752
1753clib_error_t *
1754vhost_user_connect_command_fn (vlib_main_t * vm,
1755 unformat_input_t * input,
1756 vlib_cli_command_t * cmd)
1757{
1758 unformat_input_t _line_input, *line_input = &_line_input;
1759 u8 *sock_filename = NULL;
1760 u32 sw_if_index;
1761 u8 is_server = 0;
1762 u64 feature_mask = (u64) ~ (0ULL);
1763 u8 renumber = 0;
1764 u32 custom_dev_instance = ~0;
1765 u8 hwaddr[6];
1766 u8 *hw = NULL;
1767 clib_error_t *error = NULL;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001768 u8 enable_gso = 0, enable_packed = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001769
1770 /* Get a line of input. */
1771 if (!unformat_user (input, unformat_line_input, line_input))
1772 return 0;
1773
Steven Luong4208a4c2019-05-06 08:51:56 -07001774 /* GSO feature is disable by default */
1775 feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001776 /* packed-ring feature is disable by default */
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001777 feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001778 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1779 {
1780 if (unformat (line_input, "socket %s", &sock_filename))
1781 ;
1782 else if (unformat (line_input, "server"))
1783 is_server = 1;
Steven Luong4208a4c2019-05-06 08:51:56 -07001784 else if (unformat (line_input, "gso"))
1785 enable_gso = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001786 else if (unformat (line_input, "packed"))
1787 enable_packed = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001788 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1789 ;
1790 else
1791 if (unformat
1792 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1793 hw = hwaddr;
1794 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1795 {
1796 renumber = 1;
1797 }
1798 else
1799 {
1800 error = clib_error_return (0, "unknown input `%U'",
1801 format_unformat_error, line_input);
1802 goto done;
1803 }
1804 }
1805
1806 vnet_main_t *vnm = vnet_get_main ();
1807
1808 int rv;
1809 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1810 is_server, &sw_if_index, feature_mask,
Steven Luong4208a4c2019-05-06 08:51:56 -07001811 renumber, custom_dev_instance, hw,
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001812 enable_gso, enable_packed)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001813 {
1814 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1815 goto done;
1816 }
1817
1818 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1819 sw_if_index);
1820
1821done:
1822 vec_free (sock_filename);
1823 unformat_free (line_input);
1824
1825 return error;
1826}
1827
1828clib_error_t *
1829vhost_user_delete_command_fn (vlib_main_t * vm,
1830 unformat_input_t * input,
1831 vlib_cli_command_t * cmd)
1832{
1833 unformat_input_t _line_input, *line_input = &_line_input;
1834 u32 sw_if_index = ~0;
1835 vnet_main_t *vnm = vnet_get_main ();
1836 clib_error_t *error = NULL;
1837
1838 /* Get a line of input. */
1839 if (!unformat_user (input, unformat_line_input, line_input))
1840 return 0;
1841
1842 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1843 {
1844 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1845 ;
1846 else if (unformat
1847 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1848 &sw_if_index))
1849 {
1850 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001851 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001852 if (hwif == NULL ||
1853 vhost_user_device_class.index != hwif->dev_class_index)
1854 {
1855 error = clib_error_return (0, "Not a vhost interface");
1856 goto done;
1857 }
1858 }
1859 else
1860 {
1861 error = clib_error_return (0, "unknown input `%U'",
1862 format_unformat_error, line_input);
1863 goto done;
1864 }
1865 }
1866
1867 vhost_user_delete_if (vnm, vm, sw_if_index);
1868
1869done:
1870 unformat_free (line_input);
1871
1872 return error;
1873}
1874
1875int
1876vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1877 vhost_user_intf_details_t ** out_vuids)
1878{
1879 int rv = 0;
1880 vhost_user_main_t *vum = &vhost_user_main;
1881 vhost_user_intf_t *vui;
1882 vhost_user_intf_details_t *r_vuids = NULL;
1883 vhost_user_intf_details_t *vuid = NULL;
1884 u32 *hw_if_indices = 0;
1885 vnet_hw_interface_t *hi;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001886 int i;
1887
1888 if (!out_vuids)
1889 return -1;
1890
Damjan Marionb2c31b62020-12-13 21:47:40 +01001891 pool_foreach (vui, vum->vhost_user_interfaces)
1892 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001893
1894 for (i = 0; i < vec_len (hw_if_indices); i++)
1895 {
1896 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1897 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1898
1899 vec_add2 (r_vuids, vuid, 1);
1900 vuid->sw_if_index = vui->sw_if_index;
1901 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1902 vuid->features = vui->features;
1903 vuid->num_regions = vui->nregions;
1904 vuid->is_server = vui->unix_server_index != ~0;
1905 vuid->sock_errno = vui->sock_errno;
Benoît Ganne3a76dc32020-04-24 10:33:40 +02001906 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1907 "%s", vui->sock_filename);
1908 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1909 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1910 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001911 }
1912
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001913 vec_free (hw_if_indices);
1914
1915 *out_vuids = r_vuids;
1916
1917 return rv;
1918}
1919
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001920static u8 *
1921format_vhost_user_desc (u8 * s, va_list * args)
1922{
1923 char *fmt = va_arg (*args, char *);
1924 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1925 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1926 int idx = va_arg (*args, int);
1927 u32 *mem_hint = va_arg (*args, u32 *);
1928
1929 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1930 desc_table[idx].flags, desc_table[idx].next,
1931 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1932 mem_hint)));
1933 return s;
1934}
1935
1936static u8 *
1937format_vhost_user_vring (u8 * s, va_list * args)
1938{
1939 char *fmt = va_arg (*args, char *);
1940 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1941 int q = va_arg (*args, int);
1942
1943 s = format (s, fmt, vui->vrings[q].avail->flags, vui->vrings[q].avail->idx,
1944 vui->vrings[q].used->flags, vui->vrings[q].used->idx);
1945 return s;
1946}
1947
1948static void
1949vhost_user_show_fds (vlib_main_t * vm, vhost_user_intf_t * vui, int q)
1950{
1951 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1952 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1953
1954 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1955 vui->vrings[q].errfd);
1956}
1957
1958static void
1959vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1960 int show_descr, int show_verbose)
1961{
1962 int j;
1963 u32 mem_hint = 0;
1964 u32 idx;
1965 u32 n_entries;
1966 vring_desc_t *desc_table;
1967
1968 if (vui->vrings[q].avail && vui->vrings[q].used)
1969 vlib_cli_output (vm, "%U", format_vhost_user_vring,
1970 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1971 vui, q);
1972
1973 vhost_user_show_fds (vm, vui, q);
1974
1975 if (show_descr)
1976 {
1977 vlib_cli_output (vm, "\n descriptor table:\n");
1978 vlib_cli_output (vm,
1979 " slot addr len flags next "
1980 "user_addr\n");
1981 vlib_cli_output (vm,
1982 " ===== ================== ===== ====== ===== "
1983 "==================\n");
1984 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1985 {
1986 desc_table = vui->vrings[q].desc;
1987 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1988 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1989 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001990 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001991 {
1992 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1993 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1994 if (desc_table)
1995 {
1996 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1997 {
1998 vlib_cli_output
1999 (vm, "%U", format_vhost_user_desc,
2000 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2001 desc_table, idx, &mem_hint);
2002 }
2003 if (n_entries >= 20)
2004 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2005 n_entries);
2006 }
2007 }
2008 }
2009 }
2010}
2011
2012static u8 *
2013format_vhost_user_packed_desc (u8 * s, va_list * args)
2014{
2015 char *fmt = va_arg (*args, char *);
2016 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2017 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
2018 int idx = va_arg (*args, int);
2019 u32 *mem_hint = va_arg (*args, u32 *);
2020
2021 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2022 desc_table[idx].flags, desc_table[idx].id,
2023 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2024 mem_hint)));
2025 return s;
2026}
2027
2028static u8 *
2029format_vhost_user_vring_packed (u8 * s, va_list * args)
2030{
2031 char *fmt = va_arg (*args, char *);
2032 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2033 int q = va_arg (*args, int);
2034
2035 s = format (s, fmt, vui->vrings[q].avail_event->flags,
2036 vui->vrings[q].avail_event->off_wrap,
2037 vui->vrings[q].used_event->flags,
2038 vui->vrings[q].used_event->off_wrap,
2039 vui->vrings[q].avail_wrap_counter,
2040 vui->vrings[q].used_wrap_counter);
2041 return s;
2042}
2043
2044static void
2045vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2046 int show_descr, int show_verbose)
2047{
2048 int j;
2049 u32 mem_hint = 0;
2050 u32 idx;
2051 u32 n_entries;
2052 vring_packed_desc_t *desc_table;
2053
2054 if (vui->vrings[q].avail_event && vui->vrings[q].used_event)
2055 vlib_cli_output (vm, "%U", format_vhost_user_vring_packed,
2056 " avail_event.flags %x avail_event.off_wrap %u "
2057 "used_event.flags %x used_event.off_wrap %u\n"
2058 " avail wrap counter %u, used wrap counter %u\n",
2059 vui, q);
2060
2061 vhost_user_show_fds (vm, vui, q);
2062
2063 if (show_descr)
2064 {
2065 vlib_cli_output (vm, "\n descriptor table:\n");
2066 vlib_cli_output (vm,
2067 " slot addr len flags id "
2068 "user_addr\n");
2069 vlib_cli_output (vm,
2070 " ===== ================== ===== ====== ===== "
2071 "==================\n");
2072 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
2073 {
2074 desc_table = vui->vrings[q].packed_desc;
2075 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2076 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2077 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002078 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002079 {
2080 n_entries = desc_table[j].len >> 4;
2081 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2082 if (desc_table)
2083 {
2084 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2085 {
2086 vlib_cli_output
2087 (vm, "%U", format_vhost_user_packed_desc,
2088 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2089 desc_table, idx, &mem_hint);
2090 }
2091 if (n_entries >= 20)
2092 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2093 n_entries);
2094 }
2095 }
2096 }
2097 }
2098}
2099
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002100clib_error_t *
2101show_vhost_user_command_fn (vlib_main_t * vm,
2102 unformat_input_t * input,
2103 vlib_cli_command_t * cmd)
2104{
2105 clib_error_t *error = 0;
2106 vnet_main_t *vnm = vnet_get_main ();
2107 vhost_user_main_t *vum = &vhost_user_main;
2108 vhost_user_intf_t *vui;
2109 u32 hw_if_index, *hw_if_indices = 0;
2110 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08002111 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002112 u32 ci;
2113 int i, j, q;
2114 int show_descr = 0;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002115 int show_verbose = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002116 struct feat_struct
2117 {
2118 u8 bit;
2119 char *str;
2120 };
2121 struct feat_struct *feat_entry;
2122
2123 static struct feat_struct feat_array[] = {
2124#define _(s,b) { .str = #s, .bit = b, },
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002125 foreach_virtio_net_features
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002126#undef _
2127 {.str = NULL}
2128 };
2129
2130#define foreach_protocol_feature \
2131 _(VHOST_USER_PROTOCOL_F_MQ) \
2132 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2133
2134 static struct feat_struct proto_feat_array[] = {
2135#define _(s) { .str = #s, .bit = s},
2136 foreach_protocol_feature
2137#undef _
2138 {.str = NULL}
2139 };
2140
2141 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2142 {
2143 if (unformat
2144 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2145 {
2146 hi = vnet_get_hw_interface (vnm, hw_if_index);
2147 if (vhost_user_device_class.index != hi->dev_class_index)
2148 {
2149 error = clib_error_return (0, "unknown input `%U'",
2150 format_unformat_error, input);
2151 goto done;
2152 }
2153 vec_add1 (hw_if_indices, hw_if_index);
2154 }
2155 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2156 show_descr = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002157 else if (unformat (input, "verbose"))
2158 show_verbose = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002159 else
2160 {
2161 error = clib_error_return (0, "unknown input `%U'",
2162 format_unformat_error, input);
2163 goto done;
2164 }
2165 }
2166 if (vec_len (hw_if_indices) == 0)
2167 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002168 pool_foreach (vui, vum->vhost_user_interfaces)
2169 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002170 }
2171 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2172 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2173 vum->coalesce_frames, vum->coalesce_time);
Steven Luong4208a4c2019-05-06 08:51:56 -07002174 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002175 vum->ifq_count);
Steven Luong4208a4c2019-05-06 08:51:56 -07002176 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002177
2178 for (i = 0; i < vec_len (hw_if_indices); i++)
2179 {
2180 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2181 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07002182 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2183 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2184 hw_if_indices[i]);
Steven Luong2c1084a2020-12-10 20:44:22 -08002185 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
Steven Luong4208a4c2019-05-06 08:51:56 -07002186 if (vui->enable_gso)
2187 vlib_cli_output (vm, " GSO enable");
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002188 if (vui->enable_packed)
2189 vlib_cli_output (vm, " Packed ring enable");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002190
2191 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2192 " features mask (0x%llx): \n"
2193 " features (0x%llx): \n",
2194 vui->virtio_net_hdr_sz, vui->feature_mask,
2195 vui->features);
2196
2197 feat_entry = (struct feat_struct *) &feat_array;
2198 while (feat_entry->str)
2199 {
2200 if (vui->features & (1ULL << feat_entry->bit))
2201 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2202 feat_entry->bit);
2203 feat_entry++;
2204 }
2205
2206 vlib_cli_output (vm, " protocol features (0x%llx)",
2207 vui->protocol_features);
2208 feat_entry = (struct feat_struct *) &proto_feat_array;
2209 while (feat_entry->str)
2210 {
2211 if (vui->protocol_features & (1ULL << feat_entry->bit))
2212 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2213 feat_entry->bit);
2214 feat_entry++;
2215 }
2216
2217 vlib_cli_output (vm, "\n");
2218
2219 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2220 vui->sock_filename,
2221 (vui->unix_server_index != ~0) ? "server" : "client",
2222 strerror (vui->sock_errno));
2223
2224 vlib_cli_output (vm, " rx placement: ");
2225
Steven Luong2c1084a2020-12-10 20:44:22 -08002226 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08002227 {
2228 vnet_main_t *vnm = vnet_get_main ();
2229 uword thread_index;
Damjan Marioneabd4242020-10-07 20:59:07 +02002230 vnet_hw_if_rx_mode mode;
Steven Luong67f935e2019-02-01 10:23:56 -08002231 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002232
Steven Luong67f935e2019-02-01 10:23:56 -08002233 if (txvq->qid == -1)
2234 continue;
2235 thread_index =
2236 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
2237 qid >> 1);
2238 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
2239 &mode);
2240 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2241 thread_index, qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02002242 format_vnet_hw_if_rx_mode, mode);
Steven Luong67f935e2019-02-01 10:23:56 -08002243 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002244
2245 vlib_cli_output (vm, " tx placement: %s\n",
2246 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2247
2248 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2249 {
2250 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2251 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2252 }
2253
2254 vlib_cli_output (vm, "\n");
2255
2256 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2257
2258 if (vui->nregions)
2259 {
2260 vlib_cli_output (vm,
2261 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2262 vlib_cli_output (vm,
2263 " ====== ===== ================== ================== ================== ================== ==================\n");
2264 }
2265 for (j = 0; j < vui->nregions; j++)
2266 {
2267 vlib_cli_output (vm,
2268 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2269 j, vui->region_mmap_fd[j],
2270 vui->regions[j].guest_phys_addr,
2271 vui->regions[j].memory_size,
2272 vui->regions[j].userspace_addr,
2273 vui->regions[j].mmap_offset,
2274 pointer_to_uword (vui->region_mmap_addr[j]));
2275 }
Steven Luong2c1084a2020-12-10 20:44:22 -08002276 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002277 {
2278 if (!vui->vrings[q].started)
2279 continue;
2280
2281 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2282 (q & 1) ? "RX" : "TX",
2283 vui->vrings[q].enabled ? "" : " disabled");
2284
2285 vlib_cli_output (vm,
2286 " qsz %d last_avail_idx %d last_used_idx %d\n",
2287 vui->vrings[q].qsz_mask + 1,
2288 vui->vrings[q].last_avail_idx,
2289 vui->vrings[q].last_used_idx);
2290
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002291 if (vhost_user_is_packed_ring_supported (vui))
2292 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2293 show_verbose);
2294 else
2295 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002296 }
2297 vlib_cli_output (vm, "\n");
2298 }
2299done:
2300 vec_free (hw_if_indices);
2301 return error;
2302}
2303
2304/*
2305 * CLI functions
2306 */
2307
2308/*?
2309 * Create a vHost User interface. Once created, a new virtual interface
2310 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2311 * is the next free index.
2312 *
2313 * There are several parameters associated with a vHost interface:
2314 *
2315 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2316 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2317 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2318 * hypervisor will create the socket if it does not already exist. The VPP code
2319 * is indifferent to the file location. However, if SELinux is enabled, then the
2320 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2321 *
2322 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2323 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2324 * mode, the VM can be reset without tearing down the vHost Interface. In
2325 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2326 * tearing down the vHost Interface.
2327 *
2328 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2329 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2330 * parameter not be used except by experienced users. By default, all supported
2331 * features will be advertised. Otherwise, provide the set of features desired.
2332 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2333 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2334 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2335 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2336 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2337 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2338 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2339 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2340 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2341 *
2342 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2343 * X:X:X:X:X:X unix or X.X.X cisco format.
2344 *
2345 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2346 * in the name to be specified. If instance already exists, name will be used
2347 * anyway and multiple instances will have the same name. Use with caution.
2348 *
2349 * @cliexpar
2350 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2351 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2352 * VirtualEthernet0/0/0
2353 * @cliexend
2354 * Example of how to create a vhost interface with VPP as the server and with just
2355 * multiple queues enabled:
2356 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2357 * VirtualEthernet0/0/1
2358 * @cliexend
2359 * Once the vHost interface is created, enable the interface using:
2360 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2361?*/
2362/* *INDENT-OFF* */
2363VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2364 .path = "create vhost-user",
2365 .short_help = "create vhost-user socket <socket-filename> [server] "
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002366 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2367 "[packed]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002368 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002369 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002370};
2371/* *INDENT-ON* */
2372
2373/*?
2374 * Delete a vHost User interface using the interface name or the
2375 * software interface index. Use the '<em>show interface</em>'
2376 * command to determine the software interface index. On deletion,
2377 * the linux socket will not be deleted.
2378 *
2379 * @cliexpar
2380 * Example of how to delete a vhost interface by name:
2381 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2382 * Example of how to delete a vhost interface by software interface index:
2383 * @cliexcmd{delete vhost-user sw_if_index 1}
2384?*/
2385/* *INDENT-OFF* */
2386VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2387 .path = "delete vhost-user",
2388 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2389 .function = vhost_user_delete_command_fn,
2390};
2391
2392/*?
2393 * Display the attributes of a single vHost User interface (provide interface
2394 * name), multiple vHost User interfaces (provide a list of interface names seperated
2395 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2396 * vHost interfaces).
2397 *
2398 * @cliexpar
2399 * @parblock
2400 * Example of how to display a vhost interface:
2401 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2402 * Virtio vhost-user interfaces
2403 * Global:
2404 * coalesce frames 32 time 1e-3
2405 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2406 * virtio_net_hdr_sz 12
2407 * features mask (0xffffffffffffffff):
2408 * features (0x50408000):
2409 * VIRTIO_NET_F_MRG_RXBUF (15)
2410 * VIRTIO_NET_F_MQ (22)
2411 * VIRTIO_F_INDIRECT_DESC (28)
2412 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2413 * protocol features (0x3)
2414 * VHOST_USER_PROTOCOL_F_MQ (0)
2415 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2416 *
2417 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2418 *
2419 * rx placement:
2420 * thread 1 on vring 1
2421 * thread 1 on vring 5
2422 * thread 2 on vring 3
2423 * thread 2 on vring 7
2424 * tx placement: spin-lock
2425 * thread 0 on vring 0
2426 * thread 1 on vring 2
2427 * thread 2 on vring 0
2428 *
2429 * Memory regions (total 2)
2430 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2431 * ====== ===== ================== ================== ================== ================== ==================
2432 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2433 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2434 *
2435 * Virtqueue 0 (TX)
2436 * qsz 256 last_avail_idx 0 last_used_idx 0
2437 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2438 * kickfd 62 callfd 64 errfd -1
2439 *
2440 * Virtqueue 1 (RX)
2441 * qsz 256 last_avail_idx 0 last_used_idx 0
2442 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2443 * kickfd 65 callfd 66 errfd -1
2444 *
2445 * Virtqueue 2 (TX)
2446 * qsz 256 last_avail_idx 0 last_used_idx 0
2447 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2448 * kickfd 63 callfd 70 errfd -1
2449 *
2450 * Virtqueue 3 (RX)
2451 * qsz 256 last_avail_idx 0 last_used_idx 0
2452 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2453 * kickfd 72 callfd 74 errfd -1
2454 *
2455 * Virtqueue 4 (TX disabled)
2456 * qsz 256 last_avail_idx 0 last_used_idx 0
2457 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2458 * kickfd 76 callfd 78 errfd -1
2459 *
2460 * Virtqueue 5 (RX disabled)
2461 * qsz 256 last_avail_idx 0 last_used_idx 0
2462 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2463 * kickfd 80 callfd 82 errfd -1
2464 *
2465 * Virtqueue 6 (TX disabled)
2466 * qsz 256 last_avail_idx 0 last_used_idx 0
2467 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2468 * kickfd 84 callfd 86 errfd -1
2469 *
2470 * Virtqueue 7 (RX disabled)
2471 * qsz 256 last_avail_idx 0 last_used_idx 0
2472 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2473 * kickfd 88 callfd 90 errfd -1
2474 *
2475 * @cliexend
2476 *
2477 * The optional '<em>descriptors</em>' parameter will display the same output as
2478 * the previous example but will include the descriptor table for each queue.
2479 * The output is truncated below:
2480 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2481 * Virtio vhost-user interfaces
2482 * Global:
2483 * coalesce frames 32 time 1e-3
2484 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2485 * virtio_net_hdr_sz 12
2486 * features mask (0xffffffffffffffff):
2487 * features (0x50408000):
2488 * VIRTIO_NET_F_MRG_RXBUF (15)
2489 * VIRTIO_NET_F_MQ (22)
2490 * :
2491 * Virtqueue 0 (TX)
2492 * qsz 256 last_avail_idx 0 last_used_idx 0
2493 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2494 * kickfd 62 callfd 64 errfd -1
2495 *
2496 * descriptor table:
2497 * id addr len flags next user_addr
2498 * ===== ================== ===== ====== ===== ==================
2499 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2500 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2501 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2502 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2503 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2504 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2505 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2506 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2507 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2508 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2509 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2510 * :
2511 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2512 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2513 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2514 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2515 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2516 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2517 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2518 *
2519 * Virtqueue 1 (RX)
2520 * qsz 256 last_avail_idx 0 last_used_idx 0
2521 * :
2522 * @cliexend
2523 * @endparblock
2524?*/
2525/* *INDENT-OFF* */
2526VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2527 .path = "show vhost-user",
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002528 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2529 "[[descriptors] [verbose]]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002530 .function = show_vhost_user_command_fn,
2531};
2532/* *INDENT-ON* */
2533
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002534
2535static clib_error_t *
2536vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2537{
2538 vhost_user_main_t *vum = &vhost_user_main;
2539
2540 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2541 {
2542 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2543 ;
2544 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2545 ;
2546 else if (unformat (input, "dont-dump-memory"))
2547 vum->dont_dump_vhost_user_memory = 1;
2548 else
2549 return clib_error_return (0, "unknown input `%U'",
2550 format_unformat_error, input);
2551 }
2552
2553 return 0;
2554}
2555
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002556/* vhost-user { ... } configuration. */
2557VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2558
2559void
2560vhost_user_unmap_all (void)
2561{
2562 vhost_user_main_t *vum = &vhost_user_main;
2563 vhost_user_intf_t *vui;
2564
2565 if (vum->dont_dump_vhost_user_memory)
2566 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002567 pool_foreach (vui, vum->vhost_user_interfaces)
2568 unmap_all_mem_regions (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002569 }
2570}
2571
2572/*
2573 * fd.io coding-style-patch-verification: ON
2574 *
2575 * Local Variables:
2576 * eval: (c-set-style "gnu")
2577 * End:
2578 */