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