blob: e26cfdfd8c868af391b8e0cd63cc2bf3ebb9a6fa [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);
173 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
174 /* Set polling as the default */
175 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
176 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;
456 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
457 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
458 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
459 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
460 (1ULL << FEAT_VHOST_F_LOG_ALL) |
461 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
462 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
463 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
464 (1ULL << FEAT_VIRTIO_F_VERSION_1);
465 msg.u64 &= vui->feature_mask;
466 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200467 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
468 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800469 n =
470 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
471 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
472 {
473 vu_log_debug (vui, "could not send message response");
474 goto close_socket;
475 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200476 break;
477
478 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200479 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
480 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200481
482 vui->features = msg.u64;
483
484 if (vui->features &
485 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
486 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
487 vui->virtio_net_hdr_sz = 12;
488 else
489 vui->virtio_net_hdr_sz = 10;
490
491 vui->is_any_layout =
492 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
493
494 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
495 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200496 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700497 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200498 break;
499
500 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200501 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
502 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200503
504 if ((msg.memory.nregions < 1) ||
505 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
506 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200507 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
508 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200509 goto close_socket;
510 }
511
512 if (msg.memory.nregions != number_of_fds)
513 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200514 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200515 goto close_socket;
516 }
Steven Luong67f935e2019-02-01 10:23:56 -0800517
518 /* Do the mmap without barrier sync */
519 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
520 for (i = 0; i < msg.memory.nregions; i++)
521 {
522 long page_sz = get_huge_page_size (fds[i]);
523
524 /* align size to page */
525 ssize_t map_sz = (msg.memory.regions[i].memory_size +
526 msg.memory.regions[i].mmap_offset +
527 page_sz - 1) & ~(page_sz - 1);
528
529 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
530 MAP_SHARED, fds[i], 0);
531 if (region_mmap_addr[i] == MAP_FAILED)
532 {
533 vu_log_err (vui, "failed to map memory. errno is %d", errno);
534 for (j = 0; j < i; j++)
535 munmap (region_mmap_addr[j], map_sz);
536 goto close_socket;
537 }
538 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
539 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
540 region_mmap_addr[i], page_sz);
541 }
542
543 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200544 unmap_all_mem_regions (vui);
545 for (i = 0; i < msg.memory.nregions; i++)
546 {
Dave Barach178cf492018-11-13 16:34:13 -0500547 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
548 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200549
Steven Luong67f935e2019-02-01 10:23:56 -0800550 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200551 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
552 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
553 vui->regions[i].memory_size;
554
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200555 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
556 vui->region_mmap_fd[i] = fds[i];
557
558 vui->nregions++;
559 }
Steven Luong67f935e2019-02-01 10:23:56 -0800560 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200561 break;
562
563 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200564 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
565 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200566
567 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
568 (msg.state.num == 0) || /* it cannot be zero */
569 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
570 goto close_socket;
571 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
572 break;
573
574 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200575 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
576 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200577
578 if (msg.state.index >= VHOST_VRING_MAX_N)
579 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200580 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
581 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200582 goto close_socket;
583 }
584
585 if (msg.size < sizeof (msg.addr))
586 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200587 vu_log_debug (vui, "vhost message is too short (%d < %d)",
588 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200589 goto close_socket;
590 }
591
Steven Luong67f935e2019-02-01 10:23:56 -0800592 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
593 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
594 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200595
Steven Luong67f935e2019-02-01 10:23:56 -0800596 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200597 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200598 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
599 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200600 goto close_socket;
601 }
602
Steven Luong67f935e2019-02-01 10:23:56 -0800603 vlib_worker_thread_barrier_sync (vm);
604 vui->vrings[msg.state.index].desc = desc;
605 vui->vrings[msg.state.index].used = used;
606 vui->vrings[msg.state.index].avail = avail;
607
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200608 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
609 vui->vrings[msg.state.index].log_used =
610 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
611
612 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
613 the ring is initialized in an enabled state. */
614 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800615 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200616
617 vui->vrings[msg.state.index].last_used_idx =
618 vui->vrings[msg.state.index].last_avail_idx =
619 vui->vrings[msg.state.index].used->idx;
620
621 /* tell driver that we don't want interrupts */
622 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Steven Luong67f935e2019-02-01 10:23:56 -0800623 vlib_worker_thread_barrier_release (vm);
624 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200625 break;
626
627 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200628 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200629 break;
630
631 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200632 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
633 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200634 break;
635
636 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200637 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
638 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200639
640 q = (u8) (msg.u64 & 0xFF);
641
642 /* if there is old fd, delete and close it */
643 if (vui->vrings[q].callfd_idx != ~0)
644 {
645 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
646 vui->vrings[q].callfd_idx);
647 clib_file_del (&file_main, uf);
648 vui->vrings[q].callfd_idx = ~0;
649 }
650
651 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
652 {
653 if (number_of_fds != 1)
654 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200655 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200656 goto close_socket;
657 }
658
659 template.read_function = vhost_user_callfd_read_ready;
660 template.file_descriptor = fds[0];
661 template.private_data =
662 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
663 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
664 }
665 else
666 vui->vrings[q].callfd_idx = ~0;
667 break;
668
669 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200670 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
671 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200672
673 q = (u8) (msg.u64 & 0xFF);
674
675 if (vui->vrings[q].kickfd_idx != ~0)
676 {
677 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
678 vui->vrings[q].kickfd_idx);
679 clib_file_del (&file_main, uf);
680 vui->vrings[q].kickfd_idx = ~0;
681 }
682
683 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
684 {
685 if (number_of_fds != 1)
686 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200687 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200688 goto close_socket;
689 }
690
691 template.read_function = vhost_user_kickfd_read_ready;
692 template.file_descriptor = fds[0];
693 template.private_data =
694 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
695 q;
696 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
697 }
698 else
699 {
700 //When no kickfd is set, the queue is initialized as started
701 vui->vrings[q].kickfd_idx = ~0;
702 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800703 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200704 }
Steven Luong67f935e2019-02-01 10:23:56 -0800705 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200706 break;
707
708 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200709 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
710 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200711
712 q = (u8) (msg.u64 & 0xFF);
713
714 if (vui->vrings[q].errfd != -1)
715 close (vui->vrings[q].errfd);
716
717 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
718 {
719 if (number_of_fds != 1)
720 goto close_socket;
721
722 vui->vrings[q].errfd = fds[0];
723 }
724 else
725 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200726 break;
727
728 case VHOST_USER_SET_VRING_BASE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200729 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
730 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800731 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200732 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -0800733 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200734 break;
735
736 case VHOST_USER_GET_VRING_BASE:
737 if (msg.state.index >= VHOST_VRING_MAX_N)
738 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200739 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
740 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200741 goto close_socket;
742 }
743
Steven Luong67f935e2019-02-01 10:23:56 -0800744 /* protection is needed to prevent rx/tx from changing last_avail_idx */
745 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200746 /*
747 * Copy last_avail_idx from the vring before closing it because
748 * closing the vring also initializes the vring last_avail_idx
749 */
750 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
751 msg.flags |= 4;
752 msg.size = sizeof (msg.state);
753
Steven Luong67f935e2019-02-01 10:23:56 -0800754 /*
755 * Spec says: Client must [...] stop ring upon receiving
756 * VHOST_USER_GET_VRING_BASE
757 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200758 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800759 vlib_worker_thread_barrier_release (vm);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200760 vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
761 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800762 n =
763 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
764 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
765 {
766 vu_log_debug (vui, "could not send message response");
767 goto close_socket;
768 }
769 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200770 break;
771
772 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200773 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200774 break;
775
776 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800777 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
778 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200779
Steven Luong67f935e2019-02-01 10:23:56 -0800780 if (msg.size != sizeof (msg.log))
781 {
782 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
783 " %d instead of %d", msg.size, sizeof (msg.log));
784 goto close_socket;
785 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200786
Steven Luong67f935e2019-02-01 10:23:56 -0800787 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
788 {
789 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
790 "VHOST_USER_SET_LOG_BASE received");
791 goto close_socket;
792 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200793
Steven Luong67f935e2019-02-01 10:23:56 -0800794 fd = fds[0];
795 /* align size to page */
796 long page_sz = get_huge_page_size (fd);
797 ssize_t map_sz =
798 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200799
Steven Luong67f935e2019-02-01 10:23:56 -0800800 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
801 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200802
Steven Luong67f935e2019-02-01 10:23:56 -0800803 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
804 "mapped 0x%lx", map_sz, msg.log.offset, fd,
805 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200806
Steven Luong67f935e2019-02-01 10:23:56 -0800807 if (log_base_addr == MAP_FAILED)
808 {
809 vu_log_err (vui, "failed to map memory. errno is %d", errno);
810 goto close_socket;
811 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200812
Steven Luong67f935e2019-02-01 10:23:56 -0800813 vlib_worker_thread_barrier_sync (vm);
814 vui->log_base_addr = log_base_addr;
815 vui->log_base_addr += msg.log.offset;
816 vui->log_size = msg.log.size;
817 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200818
Steven Luong67f935e2019-02-01 10:23:56 -0800819 msg.flags |= 4;
820 msg.size = sizeof (msg.u64);
821 n =
822 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
823 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
824 {
825 vu_log_debug (vui, "could not send message response");
826 goto close_socket;
827 }
828 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200829
830 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200831 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200832 break;
833
834 case VHOST_USER_GET_PROTOCOL_FEATURES:
835 msg.flags |= 4;
836 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
837 (1 << VHOST_USER_PROTOCOL_F_MQ);
838 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200839 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
840 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800841 n =
842 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
843 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
844 {
845 vu_log_debug (vui, "could not send message response");
846 goto close_socket;
847 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200848 break;
849
850 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200851 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
852 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200853 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200854 break;
855
856 case VHOST_USER_GET_QUEUE_NUM:
857 msg.flags |= 4;
858 msg.u64 = VHOST_VRING_MAX_N;
859 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200860 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
861 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800862 n =
863 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
864 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
865 {
866 vu_log_debug (vui, "could not send message response");
867 goto close_socket;
868 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200869 break;
870
871 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200872 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
873 vui->hw_if_index, msg.state.num ? "enable" : "disable",
874 msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200875 if (msg.state.index >= VHOST_VRING_MAX_N)
876 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200877 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
878 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200879 goto close_socket;
880 }
881
882 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -0800883 vhost_user_thread_placement (vui, msg.state.index);
884 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200885 break;
886
887 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200888 vu_log_debug (vui, "unknown vhost-user message %d received. "
889 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200890 goto close_socket;
891 }
892
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200893 return 0;
894
895close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -0800896 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200897 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -0800898 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200899 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200900 return 0;
901}
902
903static clib_error_t *
904vhost_user_socket_error (clib_file_t * uf)
905{
906 vlib_main_t *vm = vlib_get_main ();
907 vhost_user_main_t *vum = &vhost_user_main;
908 vhost_user_intf_t *vui =
909 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
910
Jerome Tollet2f54c272018-10-02 11:41:11 +0200911 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200912 vlib_worker_thread_barrier_sync (vm);
913 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200914 vlib_worker_thread_barrier_release (vm);
915 return 0;
916}
917
918static clib_error_t *
919vhost_user_socksvr_accept_ready (clib_file_t * uf)
920{
921 int client_fd, client_len;
922 struct sockaddr_un client;
923 clib_file_t template = { 0 };
924 vhost_user_main_t *vum = &vhost_user_main;
925 vhost_user_intf_t *vui;
926
927 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
928
929 client_len = sizeof (client);
930 client_fd = accept (uf->file_descriptor,
931 (struct sockaddr *) &client,
932 (socklen_t *) & client_len);
933
934 if (client_fd < 0)
935 return clib_error_return_unix (0, "accept");
936
937 if (vui->clib_file_index != ~0)
938 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200939 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
940 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200941 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
942 }
943
Jerome Tollet2f54c272018-10-02 11:41:11 +0200944 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
945 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200946 template.read_function = vhost_user_socket_read;
947 template.error_function = vhost_user_socket_error;
948 template.file_descriptor = client_fd;
949 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
950 vui->clib_file_index = clib_file_add (&file_main, &template);
951 return 0;
952}
953
954static clib_error_t *
955vhost_user_init (vlib_main_t * vm)
956{
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200957 vhost_user_main_t *vum = &vhost_user_main;
958 vlib_thread_main_t *tm = vlib_get_thread_main ();
959
Jerome Tollet2f54c272018-10-02 11:41:11 +0200960 vum->log_default = vlib_log_register_class ("vhost-user", 0);
961
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200962 vum->coalesce_frames = 32;
963 vum->coalesce_time = 1e-3;
964
965 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
966
967 vhost_cpu_t *cpu;
968 vec_foreach (cpu, vum->cpus)
969 {
970 /* This is actually not necessary as validate already zeroes it
971 * Just keeping the loop here for later because I am lazy. */
972 cpu->rx_buffers_len = 0;
973 }
974
975 vum->random = random_default_seed ();
976
977 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
978
979 return 0;
980}
981
Dave Barachf8d50682019-05-14 18:01:44 -0400982/* *INDENT-OFF* */
983VLIB_INIT_FUNCTION (vhost_user_init) =
984{
985 .runs_after = VLIB_INITS("ip4_init"),
986};
987/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200988
989static uword
990vhost_user_send_interrupt_process (vlib_main_t * vm,
991 vlib_node_runtime_t * rt, vlib_frame_t * f)
992{
993 vhost_user_intf_t *vui;
994 f64 timeout = 3153600000.0 /* 100 years */ ;
995 uword event_type, *event_data = 0;
996 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -0800997 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200998 f64 now, poll_time_remaining;
999 f64 next_timeout;
1000 u8 stop_timer = 0;
1001
1002 while (1)
1003 {
1004 poll_time_remaining =
1005 vlib_process_wait_for_event_or_clock (vm, timeout);
1006 event_type = vlib_process_get_events (vm, &event_data);
1007 vec_reset_length (event_data);
1008
1009 /*
1010 * Use the remaining timeout if it is less than coalesce time to avoid
1011 * resetting the existing timer in the middle of expiration
1012 */
1013 timeout = poll_time_remaining;
1014 if (vlib_process_suspend_time_is_zero (timeout) ||
1015 (timeout > vum->coalesce_time))
1016 timeout = vum->coalesce_time;
1017
1018 now = vlib_time_now (vm);
1019 switch (event_type)
1020 {
1021 case VHOST_USER_EVENT_STOP_TIMER:
1022 stop_timer = 1;
1023 break;
1024
1025 case VHOST_USER_EVENT_START_TIMER:
1026 stop_timer = 0;
1027 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1028 break;
1029 /* fall through */
1030
1031 case ~0:
1032 /* *INDENT-OFF* */
1033 pool_foreach (vui, vum->vhost_user_interfaces, {
1034 next_timeout = timeout;
Steven Luong67f935e2019-02-01 10:23:56 -08001035 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001036 {
Steven Luong67f935e2019-02-01 10:23:56 -08001037 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1038 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001039
Steven Luong67f935e2019-02-01 10:23:56 -08001040 if (txvq->qid == -1)
1041 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001042 if (txvq->n_since_last_int)
1043 {
1044 if (now >= txvq->int_deadline)
1045 vhost_user_send_call (vm, txvq);
1046 else
1047 next_timeout = txvq->int_deadline - now;
1048 }
1049
1050 if (rxvq->n_since_last_int)
1051 {
1052 if (now >= rxvq->int_deadline)
1053 vhost_user_send_call (vm, rxvq);
1054 else
1055 next_timeout = rxvq->int_deadline - now;
1056 }
1057
1058 if ((next_timeout < timeout) && (next_timeout > 0.0))
1059 timeout = next_timeout;
1060 }
1061 });
1062 /* *INDENT-ON* */
1063 break;
1064
1065 default:
1066 clib_warning ("BUG: unhandled event type %d", event_type);
1067 break;
1068 }
1069 /* No less than 1 millisecond */
1070 if (timeout < 1e-3)
1071 timeout = 1e-3;
1072 if (stop_timer)
1073 timeout = 3153600000.0;
1074 }
1075 return 0;
1076}
1077
1078/* *INDENT-OFF* */
1079VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1080 .function = vhost_user_send_interrupt_process,
1081 .type = VLIB_NODE_TYPE_PROCESS,
1082 .name = "vhost-user-send-interrupt-process",
1083};
1084/* *INDENT-ON* */
1085
1086static uword
1087vhost_user_process (vlib_main_t * vm,
1088 vlib_node_runtime_t * rt, vlib_frame_t * f)
1089{
1090 vhost_user_main_t *vum = &vhost_user_main;
1091 vhost_user_intf_t *vui;
1092 struct sockaddr_un sun;
1093 int sockfd;
1094 clib_file_t template = { 0 };
1095 f64 timeout = 3153600000.0 /* 100 years */ ;
1096 uword *event_data = 0;
1097
1098 sockfd = -1;
1099 sun.sun_family = AF_UNIX;
1100 template.read_function = vhost_user_socket_read;
1101 template.error_function = vhost_user_socket_error;
1102
1103 while (1)
1104 {
1105 vlib_process_wait_for_event_or_clock (vm, timeout);
1106 vlib_process_get_events (vm, &event_data);
1107 vec_reset_length (event_data);
1108
1109 timeout = 3.0;
1110
1111 /* *INDENT-OFF* */
1112 pool_foreach (vui, vum->vhost_user_interfaces, {
1113
1114 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1115 if (vui->clib_file_index == ~0)
1116 {
1117 if ((sockfd < 0) &&
1118 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1119 {
1120 /*
1121 * 1st time error or new error for this interface,
1122 * spit out the message and record the error
1123 */
1124 if (!vui->sock_errno || (vui->sock_errno != errno))
1125 {
1126 clib_unix_warning
1127 ("Error: Could not open unix socket for %s",
1128 vui->sock_filename);
1129 vui->sock_errno = errno;
1130 }
1131 continue;
1132 }
1133
1134 /* try to connect */
1135 strncpy (sun.sun_path, (char *) vui->sock_filename,
1136 sizeof (sun.sun_path) - 1);
1137
1138 /* Avoid hanging VPP if the other end does not accept */
1139 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1140 clib_unix_warning ("fcntl");
1141
1142 if (connect (sockfd, (struct sockaddr *) &sun,
1143 sizeof (struct sockaddr_un)) == 0)
1144 {
1145 /* Set the socket to blocking as it was before */
1146 if (fcntl(sockfd, F_SETFL, 0) < 0)
1147 clib_unix_warning ("fcntl2");
1148
1149 vui->sock_errno = 0;
1150 template.file_descriptor = sockfd;
1151 template.private_data =
1152 vui - vhost_user_main.vhost_user_interfaces;
1153 vui->clib_file_index = clib_file_add (&file_main, &template);
1154
1155 /* This sockfd is considered consumed */
1156 sockfd = -1;
1157 }
1158 else
1159 {
1160 vui->sock_errno = errno;
1161 }
1162 }
1163 else
1164 {
1165 /* check if socket is alive */
1166 int error = 0;
1167 socklen_t len = sizeof (error);
1168 int fd = UNIX_GET_FD(vui->clib_file_index);
1169 int retval =
1170 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1171
1172 if (retval)
1173 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001174 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001175 vhost_user_if_disconnect (vui);
1176 }
1177 }
1178 }
1179 });
1180 /* *INDENT-ON* */
1181 }
1182 return 0;
1183}
1184
1185/* *INDENT-OFF* */
1186VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1187 .function = vhost_user_process,
1188 .type = VLIB_NODE_TYPE_PROCESS,
1189 .name = "vhost-user-process",
1190};
1191/* *INDENT-ON* */
1192
1193/**
1194 * Disables and reset interface structure.
1195 * It can then be either init again, or removed from used interfaces.
1196 */
1197static void
1198vhost_user_term_if (vhost_user_intf_t * vui)
1199{
1200 int q;
1201 vhost_user_main_t *vum = &vhost_user_main;
1202
1203 // disconnect interface sockets
1204 vhost_user_if_disconnect (vui);
1205 vhost_user_update_iface_state (vui);
1206
1207 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1208 {
Steven Luong67f935e2019-02-01 10:23:56 -08001209 // Remove existing queue mapping for the interface
1210 if (q & 1)
1211 {
1212 int rv;
1213 vnet_main_t *vnm = vnet_get_main ();
1214 vhost_user_vring_t *txvq = &vui->vrings[q];
1215
1216 if (txvq->qid != -1)
1217 {
1218 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1219 vui->hw_if_index,
1220 q >> 1);
1221 if (rv)
1222 vu_log_warn (vui, "unable to unassign interface %d, "
1223 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1224 }
1225 }
1226
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001227 clib_mem_free ((void *) vui->vring_locks[q]);
1228 }
1229
1230 if (vui->unix_server_index != ~0)
1231 {
1232 //Close server socket
1233 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1234 vui->unix_server_index);
1235 clib_file_del (&file_main, uf);
1236 vui->unix_server_index = ~0;
1237 unlink (vui->sock_filename);
1238 }
1239
1240 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1241 &vui->if_index);
1242}
1243
1244int
1245vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1246{
1247 vhost_user_main_t *vum = &vhost_user_main;
1248 vhost_user_intf_t *vui;
1249 int rv = 0;
1250 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001251 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001252
Dave Barach3940de32019-07-23 16:28:36 -04001253 if (!
1254 (hwif =
1255 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1256 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001257 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1258
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001259 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1260
Jerome Tollet2f54c272018-10-02 11:41:11 +02001261 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1262 hwif->name, hwif->dev_instance);
1263
Steven Luong67f935e2019-02-01 10:23:56 -08001264 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1265 {
1266 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001267
Steven Luong67f935e2019-02-01 10:23:56 -08001268 if (txvq->qid == -1)
1269 continue;
1270 if ((vum->ifq_count > 0) &&
1271 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1272 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1273 {
1274 vum->ifq_count--;
1275 // Stop the timer if there is no more interrupt interface/queue
1276 if ((vum->ifq_count == 0) &&
1277 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1278 {
1279 vlib_process_signal_event (vm,
1280 vhost_user_send_interrupt_node.index,
1281 VHOST_USER_EVENT_STOP_TIMER, 0);
1282 break;
1283 }
1284 }
1285 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001286
1287 // Disable and reset interface
1288 vhost_user_term_if (vui);
1289
1290 // Reset renumbered iface
1291 if (hwif->dev_instance <
1292 vec_len (vum->show_dev_instance_by_real_dev_instance))
1293 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1294
1295 // Delete ethernet interface
1296 ethernet_delete_interface (vnm, vui->hw_if_index);
1297
1298 // Back to pool
1299 pool_put (vum->vhost_user_interfaces, vui);
1300
1301 return rv;
1302}
1303
1304static clib_error_t *
1305vhost_user_exit (vlib_main_t * vm)
1306{
1307 vnet_main_t *vnm = vnet_get_main ();
1308 vhost_user_main_t *vum = &vhost_user_main;
1309 vhost_user_intf_t *vui;
1310
1311 vlib_worker_thread_barrier_sync (vlib_get_main ());
1312 /* *INDENT-OFF* */
1313 pool_foreach (vui, vum->vhost_user_interfaces, {
1314 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1315 });
1316 /* *INDENT-ON* */
1317 vlib_worker_thread_barrier_release (vlib_get_main ());
1318 return 0;
1319}
1320
1321VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1322
1323/**
1324 * Open server unix socket on specified sock_filename.
1325 */
1326static int
1327vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1328{
1329 int rv = 0;
1330 struct sockaddr_un un = { };
1331 int fd;
1332 /* create listening socket */
1333 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1334 return VNET_API_ERROR_SYSCALL_ERROR_1;
1335
1336 un.sun_family = AF_UNIX;
1337 strncpy ((char *) un.sun_path, (char *) sock_filename,
1338 sizeof (un.sun_path) - 1);
1339
1340 /* remove if exists */
1341 unlink ((char *) sock_filename);
1342
1343 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1344 {
1345 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1346 goto error;
1347 }
1348
1349 if (listen (fd, 1) == -1)
1350 {
1351 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1352 goto error;
1353 }
1354
1355 *sock_fd = fd;
1356 return 0;
1357
1358error:
1359 close (fd);
1360 return rv;
1361}
1362
1363/**
1364 * Create ethernet interface for vhost user interface.
1365 */
1366static void
1367vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1368 vhost_user_intf_t * vui, u8 * hwaddress)
1369{
1370 vhost_user_main_t *vum = &vhost_user_main;
1371 u8 hwaddr[6];
1372 clib_error_t *error;
1373
1374 /* create hw and sw interface */
1375 if (hwaddress)
1376 {
1377 clib_memcpy (hwaddr, hwaddress, 6);
1378 }
1379 else
1380 {
1381 random_u32 (&vum->random);
1382 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1383 hwaddr[0] = 2;
1384 hwaddr[1] = 0xfe;
1385 }
1386
1387 error = ethernet_register_interface
1388 (vnm,
1389 vhost_user_device_class.index,
1390 vui - vum->vhost_user_interfaces /* device instance */ ,
1391 hwaddr /* ethernet address */ ,
1392 &vui->hw_if_index, 0 /* flag change */ );
1393
1394 if (error)
1395 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001396}
1397
1398/*
1399 * Initialize vui with specified attributes
1400 */
1401static void
1402vhost_user_vui_init (vnet_main_t * vnm,
1403 vhost_user_intf_t * vui,
1404 int server_sock_fd,
1405 const char *sock_filename,
1406 u64 feature_mask, u32 * sw_if_index)
1407{
1408 vnet_sw_interface_t *sw;
1409 int q;
1410 vhost_user_main_t *vum = &vhost_user_main;
1411 vnet_hw_interface_t *hw;
1412
1413 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1414 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1415 if (server_sock_fd != -1)
1416 {
1417 clib_file_t template = { 0 };
1418 template.read_function = vhost_user_socksvr_accept_ready;
1419 template.file_descriptor = server_sock_fd;
1420 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1421 vui->unix_server_index = clib_file_add (&file_main, &template);
1422 }
1423 else
1424 {
1425 vui->unix_server_index = ~0;
1426 }
1427
1428 vui->sw_if_index = sw->sw_if_index;
1429 strncpy (vui->sock_filename, sock_filename,
1430 ARRAY_LEN (vui->sock_filename) - 1);
1431 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001432 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001433 vui->feature_mask = feature_mask;
1434 vui->clib_file_index = ~0;
1435 vui->log_base_addr = 0;
1436 vui->if_index = vui - vum->vhost_user_interfaces;
1437 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1438 &vui->if_index, 0);
1439
1440 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1441 vhost_user_vring_init (vui, q);
1442
1443 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1444 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1445
1446 if (sw_if_index)
1447 *sw_if_index = vui->sw_if_index;
1448
1449 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1450 {
1451 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1452 CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -04001453 clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001454 }
1455
1456 vec_validate (vui->per_cpu_tx_qid,
1457 vlib_get_thread_main ()->n_vlib_mains - 1);
1458 vhost_user_tx_thread_placement (vui);
1459}
1460
1461int
1462vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1463 const char *sock_filename,
1464 u8 is_server,
1465 u32 * sw_if_index,
1466 u64 feature_mask,
1467 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1468{
1469 vhost_user_intf_t *vui = NULL;
1470 u32 sw_if_idx = ~0;
1471 int rv = 0;
1472 int server_sock_fd = -1;
1473 vhost_user_main_t *vum = &vhost_user_main;
1474 uword *if_index;
1475
1476 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1477 {
1478 return VNET_API_ERROR_INVALID_ARGUMENT;
1479 }
1480
1481 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1482 if (if_index)
1483 {
1484 if (sw_if_index)
1485 {
1486 vui = &vum->vhost_user_interfaces[*if_index];
1487 *sw_if_index = vui->sw_if_index;
1488 }
1489 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1490 }
1491
1492 if (is_server)
1493 {
1494 if ((rv =
1495 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1496 {
1497 return rv;
1498 }
1499 }
1500
Steven Luong67f935e2019-02-01 10:23:56 -08001501 /* Protect the uninitialized vui from being dispatched by rx/tx */
1502 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001503 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001504 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Steven Luong67f935e2019-02-01 10:23:56 -08001505 vlib_worker_thread_barrier_release (vm);
1506
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001507 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1508 feature_mask, &sw_if_idx);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001509 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001510 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001511
1512 if (renumber)
1513 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1514
1515 if (sw_if_index)
1516 *sw_if_index = sw_if_idx;
1517
1518 // Process node must connect
1519 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1520
1521 return rv;
1522}
1523
1524int
1525vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1526 const char *sock_filename,
1527 u8 is_server,
1528 u32 sw_if_index,
1529 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1530{
1531 vhost_user_main_t *vum = &vhost_user_main;
1532 vhost_user_intf_t *vui = NULL;
1533 u32 sw_if_idx = ~0;
1534 int server_sock_fd = -1;
1535 int rv = 0;
1536 vnet_hw_interface_t *hwif;
1537 uword *if_index;
1538
Dave Barach3940de32019-07-23 16:28:36 -04001539 if (!
1540 (hwif =
1541 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1542 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001543 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1544
1545 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1546 return VNET_API_ERROR_INVALID_ARGUMENT;
1547
1548 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1549
1550 /*
1551 * Disallow changing the interface to have the same path name
1552 * as other interface
1553 */
1554 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1555 if (if_index && (*if_index != vui->if_index))
1556 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1557
1558 // First try to open server socket
1559 if (is_server)
1560 if ((rv = vhost_user_init_server_sock (sock_filename,
1561 &server_sock_fd)) != 0)
1562 return rv;
1563
1564 vhost_user_term_if (vui);
1565 vhost_user_vui_init (vnm, vui, server_sock_fd,
1566 sock_filename, feature_mask, &sw_if_idx);
1567
1568 if (renumber)
1569 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1570
1571 // Process node must connect
1572 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1573
1574 return rv;
1575}
1576
1577clib_error_t *
1578vhost_user_connect_command_fn (vlib_main_t * vm,
1579 unformat_input_t * input,
1580 vlib_cli_command_t * cmd)
1581{
1582 unformat_input_t _line_input, *line_input = &_line_input;
1583 u8 *sock_filename = NULL;
1584 u32 sw_if_index;
1585 u8 is_server = 0;
1586 u64 feature_mask = (u64) ~ (0ULL);
1587 u8 renumber = 0;
1588 u32 custom_dev_instance = ~0;
1589 u8 hwaddr[6];
1590 u8 *hw = NULL;
1591 clib_error_t *error = NULL;
1592
1593 /* Get a line of input. */
1594 if (!unformat_user (input, unformat_line_input, line_input))
1595 return 0;
1596
1597 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1598 {
1599 if (unformat (line_input, "socket %s", &sock_filename))
1600 ;
1601 else if (unformat (line_input, "server"))
1602 is_server = 1;
1603 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1604 ;
1605 else
1606 if (unformat
1607 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1608 hw = hwaddr;
1609 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1610 {
1611 renumber = 1;
1612 }
1613 else
1614 {
1615 error = clib_error_return (0, "unknown input `%U'",
1616 format_unformat_error, line_input);
1617 goto done;
1618 }
1619 }
1620
1621 vnet_main_t *vnm = vnet_get_main ();
1622
1623 int rv;
1624 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1625 is_server, &sw_if_index, feature_mask,
1626 renumber, custom_dev_instance, hw)))
1627 {
1628 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1629 goto done;
1630 }
1631
1632 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1633 sw_if_index);
1634
1635done:
1636 vec_free (sock_filename);
1637 unformat_free (line_input);
1638
1639 return error;
1640}
1641
1642clib_error_t *
1643vhost_user_delete_command_fn (vlib_main_t * vm,
1644 unformat_input_t * input,
1645 vlib_cli_command_t * cmd)
1646{
1647 unformat_input_t _line_input, *line_input = &_line_input;
1648 u32 sw_if_index = ~0;
1649 vnet_main_t *vnm = vnet_get_main ();
1650 clib_error_t *error = NULL;
1651
1652 /* Get a line of input. */
1653 if (!unformat_user (input, unformat_line_input, line_input))
1654 return 0;
1655
1656 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1657 {
1658 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1659 ;
1660 else if (unformat
1661 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1662 &sw_if_index))
1663 {
1664 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001665 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001666 if (hwif == NULL ||
1667 vhost_user_device_class.index != hwif->dev_class_index)
1668 {
1669 error = clib_error_return (0, "Not a vhost interface");
1670 goto done;
1671 }
1672 }
1673 else
1674 {
1675 error = clib_error_return (0, "unknown input `%U'",
1676 format_unformat_error, line_input);
1677 goto done;
1678 }
1679 }
1680
1681 vhost_user_delete_if (vnm, vm, sw_if_index);
1682
1683done:
1684 unformat_free (line_input);
1685
1686 return error;
1687}
1688
1689int
1690vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1691 vhost_user_intf_details_t ** out_vuids)
1692{
1693 int rv = 0;
1694 vhost_user_main_t *vum = &vhost_user_main;
1695 vhost_user_intf_t *vui;
1696 vhost_user_intf_details_t *r_vuids = NULL;
1697 vhost_user_intf_details_t *vuid = NULL;
1698 u32 *hw_if_indices = 0;
1699 vnet_hw_interface_t *hi;
1700 u8 *s = NULL;
1701 int i;
1702
1703 if (!out_vuids)
1704 return -1;
1705
1706 pool_foreach (vui, vum->vhost_user_interfaces,
1707 vec_add1 (hw_if_indices, vui->hw_if_index);
1708 );
1709
1710 for (i = 0; i < vec_len (hw_if_indices); i++)
1711 {
1712 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1713 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1714
1715 vec_add2 (r_vuids, vuid, 1);
1716 vuid->sw_if_index = vui->sw_if_index;
1717 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1718 vuid->features = vui->features;
1719 vuid->num_regions = vui->nregions;
1720 vuid->is_server = vui->unix_server_index != ~0;
1721 vuid->sock_errno = vui->sock_errno;
1722 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1723 sizeof (vuid->sock_filename));
1724 vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1725 s = format (s, "%v%c", hi->name, 0);
1726
1727 strncpy ((char *) vuid->if_name, (char *) s,
1728 ARRAY_LEN (vuid->if_name) - 1);
1729 _vec_len (s) = 0;
1730 }
1731
1732 vec_free (s);
1733 vec_free (hw_if_indices);
1734
1735 *out_vuids = r_vuids;
1736
1737 return rv;
1738}
1739
1740clib_error_t *
1741show_vhost_user_command_fn (vlib_main_t * vm,
1742 unformat_input_t * input,
1743 vlib_cli_command_t * cmd)
1744{
1745 clib_error_t *error = 0;
1746 vnet_main_t *vnm = vnet_get_main ();
1747 vhost_user_main_t *vum = &vhost_user_main;
1748 vhost_user_intf_t *vui;
1749 u32 hw_if_index, *hw_if_indices = 0;
1750 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08001751 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001752 u32 ci;
1753 int i, j, q;
1754 int show_descr = 0;
1755 struct feat_struct
1756 {
1757 u8 bit;
1758 char *str;
1759 };
1760 struct feat_struct *feat_entry;
1761
1762 static struct feat_struct feat_array[] = {
1763#define _(s,b) { .str = #s, .bit = b, },
1764 foreach_virtio_net_feature
1765#undef _
1766 {.str = NULL}
1767 };
1768
1769#define foreach_protocol_feature \
1770 _(VHOST_USER_PROTOCOL_F_MQ) \
1771 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1772
1773 static struct feat_struct proto_feat_array[] = {
1774#define _(s) { .str = #s, .bit = s},
1775 foreach_protocol_feature
1776#undef _
1777 {.str = NULL}
1778 };
1779
1780 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1781 {
1782 if (unformat
1783 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1784 {
1785 hi = vnet_get_hw_interface (vnm, hw_if_index);
1786 if (vhost_user_device_class.index != hi->dev_class_index)
1787 {
1788 error = clib_error_return (0, "unknown input `%U'",
1789 format_unformat_error, input);
1790 goto done;
1791 }
1792 vec_add1 (hw_if_indices, hw_if_index);
1793 }
1794 else if (unformat (input, "descriptors") || unformat (input, "desc"))
1795 show_descr = 1;
1796 else
1797 {
1798 error = clib_error_return (0, "unknown input `%U'",
1799 format_unformat_error, input);
1800 goto done;
1801 }
1802 }
1803 if (vec_len (hw_if_indices) == 0)
1804 {
1805 pool_foreach (vui, vum->vhost_user_interfaces,
1806 vec_add1 (hw_if_indices, vui->hw_if_index);
1807 );
1808 }
1809 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1810 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1811 vum->coalesce_frames, vum->coalesce_time);
1812 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
1813 vum->ifq_count);
1814
1815 for (i = 0; i < vec_len (hw_if_indices); i++)
1816 {
1817 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1818 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07001819 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1820 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1821 hw_if_indices[i]);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001822
1823 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1824 " features mask (0x%llx): \n"
1825 " features (0x%llx): \n",
1826 vui->virtio_net_hdr_sz, vui->feature_mask,
1827 vui->features);
1828
1829 feat_entry = (struct feat_struct *) &feat_array;
1830 while (feat_entry->str)
1831 {
1832 if (vui->features & (1ULL << feat_entry->bit))
1833 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1834 feat_entry->bit);
1835 feat_entry++;
1836 }
1837
1838 vlib_cli_output (vm, " protocol features (0x%llx)",
1839 vui->protocol_features);
1840 feat_entry = (struct feat_struct *) &proto_feat_array;
1841 while (feat_entry->str)
1842 {
1843 if (vui->protocol_features & (1ULL << feat_entry->bit))
1844 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1845 feat_entry->bit);
1846 feat_entry++;
1847 }
1848
1849 vlib_cli_output (vm, "\n");
1850
1851 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1852 vui->sock_filename,
1853 (vui->unix_server_index != ~0) ? "server" : "client",
1854 strerror (vui->sock_errno));
1855
1856 vlib_cli_output (vm, " rx placement: ");
1857
Steven Luong67f935e2019-02-01 10:23:56 -08001858 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1859 {
1860 vnet_main_t *vnm = vnet_get_main ();
1861 uword thread_index;
1862 vnet_hw_interface_rx_mode mode;
1863 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001864
Steven Luong67f935e2019-02-01 10:23:56 -08001865 if (txvq->qid == -1)
1866 continue;
1867 thread_index =
1868 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
1869 qid >> 1);
1870 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
1871 &mode);
1872 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1873 thread_index, qid,
1874 format_vnet_hw_interface_rx_mode, mode);
1875 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001876
1877 vlib_cli_output (vm, " tx placement: %s\n",
1878 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1879
1880 vec_foreach_index (ci, vui->per_cpu_tx_qid)
1881 {
1882 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1883 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1884 }
1885
1886 vlib_cli_output (vm, "\n");
1887
1888 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1889
1890 if (vui->nregions)
1891 {
1892 vlib_cli_output (vm,
1893 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1894 vlib_cli_output (vm,
1895 " ====== ===== ================== ================== ================== ================== ==================\n");
1896 }
1897 for (j = 0; j < vui->nregions; j++)
1898 {
1899 vlib_cli_output (vm,
1900 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1901 j, vui->region_mmap_fd[j],
1902 vui->regions[j].guest_phys_addr,
1903 vui->regions[j].memory_size,
1904 vui->regions[j].userspace_addr,
1905 vui->regions[j].mmap_offset,
1906 pointer_to_uword (vui->region_mmap_addr[j]));
1907 }
1908 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1909 {
1910 if (!vui->vrings[q].started)
1911 continue;
1912
1913 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1914 (q & 1) ? "RX" : "TX",
1915 vui->vrings[q].enabled ? "" : " disabled");
1916
1917 vlib_cli_output (vm,
1918 " qsz %d last_avail_idx %d last_used_idx %d\n",
1919 vui->vrings[q].qsz_mask + 1,
1920 vui->vrings[q].last_avail_idx,
1921 vui->vrings[q].last_used_idx);
1922
1923 if (vui->vrings[q].avail && vui->vrings[q].used)
1924 vlib_cli_output (vm,
1925 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1926 vui->vrings[q].avail->flags,
1927 vui->vrings[q].avail->idx,
1928 vui->vrings[q].used->flags,
1929 vui->vrings[q].used->idx);
1930
1931 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1932 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1933 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1934 kickfd, callfd, vui->vrings[q].errfd);
1935
1936 if (show_descr)
1937 {
1938 vlib_cli_output (vm, "\n descriptor table:\n");
1939 vlib_cli_output (vm,
1940 " id addr len flags next user_addr\n");
1941 vlib_cli_output (vm,
1942 " ===== ================== ===== ====== ===== ==================\n");
1943 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1944 {
1945 u32 mem_hint = 0;
1946 vlib_cli_output (vm,
1947 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1948 j, vui->vrings[q].desc[j].addr,
1949 vui->vrings[q].desc[j].len,
1950 vui->vrings[q].desc[j].flags,
1951 vui->vrings[q].desc[j].next,
1952 pointer_to_uword (map_guest_mem
1953 (vui,
1954 vui->vrings[q].desc[j].
1955 addr, &mem_hint)));
1956 }
1957 }
1958 }
1959 vlib_cli_output (vm, "\n");
1960 }
1961done:
1962 vec_free (hw_if_indices);
1963 return error;
1964}
1965
1966/*
1967 * CLI functions
1968 */
1969
1970/*?
1971 * Create a vHost User interface. Once created, a new virtual interface
1972 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
1973 * is the next free index.
1974 *
1975 * There are several parameters associated with a vHost interface:
1976 *
1977 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
1978 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
1979 * create the socket if it does not already exist. If in '<em>client</em>' mode,
1980 * hypervisor will create the socket if it does not already exist. The VPP code
1981 * is indifferent to the file location. However, if SELinux is enabled, then the
1982 * socket needs to be created in '<em>/var/run/vpp/</em>'.
1983 *
1984 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
1985 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
1986 * mode, the VM can be reset without tearing down the vHost Interface. In
1987 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
1988 * tearing down the vHost Interface.
1989 *
1990 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
1991 * startup. <b>This is intended for degugging only.</b> It is recommended that this
1992 * parameter not be used except by experienced users. By default, all supported
1993 * features will be advertised. Otherwise, provide the set of features desired.
1994 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
1995 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
1996 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
1997 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
1998 * - 0x004000000 (26) - VHOST_F_LOG_ALL
1999 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2000 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2001 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2002 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2003 *
2004 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2005 * X:X:X:X:X:X unix or X.X.X cisco format.
2006 *
2007 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2008 * in the name to be specified. If instance already exists, name will be used
2009 * anyway and multiple instances will have the same name. Use with caution.
2010 *
2011 * @cliexpar
2012 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2013 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2014 * VirtualEthernet0/0/0
2015 * @cliexend
2016 * Example of how to create a vhost interface with VPP as the server and with just
2017 * multiple queues enabled:
2018 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2019 * VirtualEthernet0/0/1
2020 * @cliexend
2021 * Once the vHost interface is created, enable the interface using:
2022 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2023?*/
2024/* *INDENT-OFF* */
2025VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2026 .path = "create vhost-user",
2027 .short_help = "create vhost-user socket <socket-filename> [server] "
2028 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
2029 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002030 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002031};
2032/* *INDENT-ON* */
2033
2034/*?
2035 * Delete a vHost User interface using the interface name or the
2036 * software interface index. Use the '<em>show interface</em>'
2037 * command to determine the software interface index. On deletion,
2038 * the linux socket will not be deleted.
2039 *
2040 * @cliexpar
2041 * Example of how to delete a vhost interface by name:
2042 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2043 * Example of how to delete a vhost interface by software interface index:
2044 * @cliexcmd{delete vhost-user sw_if_index 1}
2045?*/
2046/* *INDENT-OFF* */
2047VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2048 .path = "delete vhost-user",
2049 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2050 .function = vhost_user_delete_command_fn,
2051};
2052
2053/*?
2054 * Display the attributes of a single vHost User interface (provide interface
2055 * name), multiple vHost User interfaces (provide a list of interface names seperated
2056 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2057 * vHost interfaces).
2058 *
2059 * @cliexpar
2060 * @parblock
2061 * Example of how to display a vhost interface:
2062 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2063 * Virtio vhost-user interfaces
2064 * Global:
2065 * coalesce frames 32 time 1e-3
2066 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2067 * virtio_net_hdr_sz 12
2068 * features mask (0xffffffffffffffff):
2069 * features (0x50408000):
2070 * VIRTIO_NET_F_MRG_RXBUF (15)
2071 * VIRTIO_NET_F_MQ (22)
2072 * VIRTIO_F_INDIRECT_DESC (28)
2073 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2074 * protocol features (0x3)
2075 * VHOST_USER_PROTOCOL_F_MQ (0)
2076 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2077 *
2078 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2079 *
2080 * rx placement:
2081 * thread 1 on vring 1
2082 * thread 1 on vring 5
2083 * thread 2 on vring 3
2084 * thread 2 on vring 7
2085 * tx placement: spin-lock
2086 * thread 0 on vring 0
2087 * thread 1 on vring 2
2088 * thread 2 on vring 0
2089 *
2090 * Memory regions (total 2)
2091 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2092 * ====== ===== ================== ================== ================== ================== ==================
2093 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2094 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2095 *
2096 * Virtqueue 0 (TX)
2097 * qsz 256 last_avail_idx 0 last_used_idx 0
2098 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2099 * kickfd 62 callfd 64 errfd -1
2100 *
2101 * Virtqueue 1 (RX)
2102 * qsz 256 last_avail_idx 0 last_used_idx 0
2103 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2104 * kickfd 65 callfd 66 errfd -1
2105 *
2106 * Virtqueue 2 (TX)
2107 * qsz 256 last_avail_idx 0 last_used_idx 0
2108 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2109 * kickfd 63 callfd 70 errfd -1
2110 *
2111 * Virtqueue 3 (RX)
2112 * qsz 256 last_avail_idx 0 last_used_idx 0
2113 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2114 * kickfd 72 callfd 74 errfd -1
2115 *
2116 * Virtqueue 4 (TX disabled)
2117 * qsz 256 last_avail_idx 0 last_used_idx 0
2118 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2119 * kickfd 76 callfd 78 errfd -1
2120 *
2121 * Virtqueue 5 (RX disabled)
2122 * qsz 256 last_avail_idx 0 last_used_idx 0
2123 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2124 * kickfd 80 callfd 82 errfd -1
2125 *
2126 * Virtqueue 6 (TX disabled)
2127 * qsz 256 last_avail_idx 0 last_used_idx 0
2128 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2129 * kickfd 84 callfd 86 errfd -1
2130 *
2131 * Virtqueue 7 (RX disabled)
2132 * qsz 256 last_avail_idx 0 last_used_idx 0
2133 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2134 * kickfd 88 callfd 90 errfd -1
2135 *
2136 * @cliexend
2137 *
2138 * The optional '<em>descriptors</em>' parameter will display the same output as
2139 * the previous example but will include the descriptor table for each queue.
2140 * The output is truncated below:
2141 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2142 * Virtio vhost-user interfaces
2143 * Global:
2144 * coalesce frames 32 time 1e-3
2145 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2146 * virtio_net_hdr_sz 12
2147 * features mask (0xffffffffffffffff):
2148 * features (0x50408000):
2149 * VIRTIO_NET_F_MRG_RXBUF (15)
2150 * VIRTIO_NET_F_MQ (22)
2151 * :
2152 * Virtqueue 0 (TX)
2153 * qsz 256 last_avail_idx 0 last_used_idx 0
2154 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2155 * kickfd 62 callfd 64 errfd -1
2156 *
2157 * descriptor table:
2158 * id addr len flags next user_addr
2159 * ===== ================== ===== ====== ===== ==================
2160 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2161 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2162 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2163 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2164 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2165 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2166 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2167 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2168 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2169 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2170 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2171 * :
2172 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2173 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2174 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2175 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2176 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2177 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2178 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2179 *
2180 * Virtqueue 1 (RX)
2181 * qsz 256 last_avail_idx 0 last_used_idx 0
2182 * :
2183 * @cliexend
2184 * @endparblock
2185?*/
2186/* *INDENT-OFF* */
2187VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2188 .path = "show vhost-user",
2189 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2190 .function = show_vhost_user_command_fn,
2191};
2192/* *INDENT-ON* */
2193
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002194
2195static clib_error_t *
2196vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2197{
2198 vhost_user_main_t *vum = &vhost_user_main;
2199
2200 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2201 {
2202 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2203 ;
2204 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2205 ;
2206 else if (unformat (input, "dont-dump-memory"))
2207 vum->dont_dump_vhost_user_memory = 1;
2208 else
2209 return clib_error_return (0, "unknown input `%U'",
2210 format_unformat_error, input);
2211 }
2212
2213 return 0;
2214}
2215
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002216/* vhost-user { ... } configuration. */
2217VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2218
2219void
2220vhost_user_unmap_all (void)
2221{
2222 vhost_user_main_t *vum = &vhost_user_main;
2223 vhost_user_intf_t *vui;
2224
2225 if (vum->dont_dump_vhost_user_memory)
2226 {
2227 pool_foreach (vui, vum->vhost_user_interfaces,
2228 unmap_all_mem_regions (vui);
2229 );
2230 }
2231}
2232
2233/*
2234 * fd.io coding-style-patch-verification: ON
2235 *
2236 * Local Variables:
2237 * eval: (c-set-style "gnu")
2238 * End:
2239 */