blob: c06f78ce1bb116e7388dda04075785c2b8a7ce92 [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;
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;
469
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200470 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200471 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
472 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800473 n =
474 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
475 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
476 {
477 vu_log_debug (vui, "could not send message response");
478 goto close_socket;
479 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200480 break;
481
482 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200483 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
484 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200485
486 vui->features = msg.u64;
487
488 if (vui->features &
489 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
490 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
491 vui->virtio_net_hdr_sz = 12;
492 else
493 vui->virtio_net_hdr_sz = 10;
494
495 vui->is_any_layout =
496 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
497
498 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Steven Luong4208a4c2019-05-06 08:51:56 -0700499 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
500 if (vui->enable_gso &&
Steven Luonga75ad872019-08-06 21:51:34 -0700501 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
502 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
503 hw->flags |=
504 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
505 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Steven Luong4208a4c2019-05-06 08:51:56 -0700506 else
Steven Luonga75ad872019-08-06 21:51:34 -0700507 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
508 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200509 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200510 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700511 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200512 break;
513
514 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200515 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
516 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200517
518 if ((msg.memory.nregions < 1) ||
519 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
520 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200521 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
522 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200523 goto close_socket;
524 }
525
526 if (msg.memory.nregions != number_of_fds)
527 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200528 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200529 goto close_socket;
530 }
Steven Luong67f935e2019-02-01 10:23:56 -0800531
532 /* Do the mmap without barrier sync */
533 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
534 for (i = 0; i < msg.memory.nregions; i++)
535 {
536 long page_sz = get_huge_page_size (fds[i]);
537
538 /* align size to page */
539 ssize_t map_sz = (msg.memory.regions[i].memory_size +
540 msg.memory.regions[i].mmap_offset +
541 page_sz - 1) & ~(page_sz - 1);
542
543 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
544 MAP_SHARED, fds[i], 0);
545 if (region_mmap_addr[i] == MAP_FAILED)
546 {
547 vu_log_err (vui, "failed to map memory. errno is %d", errno);
548 for (j = 0; j < i; j++)
549 munmap (region_mmap_addr[j], map_sz);
550 goto close_socket;
551 }
552 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
553 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
554 region_mmap_addr[i], page_sz);
555 }
556
557 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200558 unmap_all_mem_regions (vui);
559 for (i = 0; i < msg.memory.nregions; i++)
560 {
Dave Barach178cf492018-11-13 16:34:13 -0500561 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
562 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200563
Steven Luong67f935e2019-02-01 10:23:56 -0800564 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200565 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
566 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
567 vui->regions[i].memory_size;
568
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200569 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
570 vui->region_mmap_fd[i] = fds[i];
571
572 vui->nregions++;
573 }
Steven Luong67f935e2019-02-01 10:23:56 -0800574 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200575 break;
576
577 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200578 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
579 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200580
581 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
582 (msg.state.num == 0) || /* it cannot be zero */
583 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
584 goto close_socket;
585 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
586 break;
587
588 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200589 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
590 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200591
592 if (msg.state.index >= VHOST_VRING_MAX_N)
593 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200594 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
595 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200596 goto close_socket;
597 }
598
599 if (msg.size < sizeof (msg.addr))
600 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200601 vu_log_debug (vui, "vhost message is too short (%d < %d)",
602 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200603 goto close_socket;
604 }
605
Steven Luong67f935e2019-02-01 10:23:56 -0800606 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
607 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
608 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200609
Steven Luong67f935e2019-02-01 10:23:56 -0800610 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200611 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200612 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
613 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200614 goto close_socket;
615 }
616
Steven Luong67f935e2019-02-01 10:23:56 -0800617 vlib_worker_thread_barrier_sync (vm);
618 vui->vrings[msg.state.index].desc = desc;
619 vui->vrings[msg.state.index].used = used;
620 vui->vrings[msg.state.index].avail = avail;
621
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200622 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
623 vui->vrings[msg.state.index].log_used =
624 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
625
626 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
627 the ring is initialized in an enabled state. */
628 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800629 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200630
631 vui->vrings[msg.state.index].last_used_idx =
632 vui->vrings[msg.state.index].last_avail_idx =
633 vui->vrings[msg.state.index].used->idx;
634
635 /* tell driver that we don't want interrupts */
636 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Steven Luong67f935e2019-02-01 10:23:56 -0800637 vlib_worker_thread_barrier_release (vm);
638 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200639 break;
640
641 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200642 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200643 break;
644
645 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200646 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
647 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200648 break;
649
650 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200651 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
652 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200653
654 q = (u8) (msg.u64 & 0xFF);
655
656 /* if there is old fd, delete and close it */
657 if (vui->vrings[q].callfd_idx != ~0)
658 {
659 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
660 vui->vrings[q].callfd_idx);
661 clib_file_del (&file_main, uf);
662 vui->vrings[q].callfd_idx = ~0;
663 }
664
665 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
666 {
667 if (number_of_fds != 1)
668 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200669 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200670 goto close_socket;
671 }
672
673 template.read_function = vhost_user_callfd_read_ready;
674 template.file_descriptor = fds[0];
675 template.private_data =
676 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
677 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
678 }
679 else
680 vui->vrings[q].callfd_idx = ~0;
681 break;
682
683 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200684 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
685 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200686
687 q = (u8) (msg.u64 & 0xFF);
688
689 if (vui->vrings[q].kickfd_idx != ~0)
690 {
691 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
692 vui->vrings[q].kickfd_idx);
693 clib_file_del (&file_main, uf);
694 vui->vrings[q].kickfd_idx = ~0;
695 }
696
697 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
698 {
699 if (number_of_fds != 1)
700 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200701 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200702 goto close_socket;
703 }
704
705 template.read_function = vhost_user_kickfd_read_ready;
706 template.file_descriptor = fds[0];
707 template.private_data =
708 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
709 q;
710 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
711 }
712 else
713 {
714 //When no kickfd is set, the queue is initialized as started
715 vui->vrings[q].kickfd_idx = ~0;
716 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800717 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200718 }
Steven Luong67f935e2019-02-01 10:23:56 -0800719 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200720 break;
721
722 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200723 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
724 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200725
726 q = (u8) (msg.u64 & 0xFF);
727
728 if (vui->vrings[q].errfd != -1)
729 close (vui->vrings[q].errfd);
730
731 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
732 {
733 if (number_of_fds != 1)
734 goto close_socket;
735
736 vui->vrings[q].errfd = fds[0];
737 }
738 else
739 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200740 break;
741
742 case VHOST_USER_SET_VRING_BASE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200743 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
744 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800745 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200746 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -0800747 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200748 break;
749
750 case VHOST_USER_GET_VRING_BASE:
751 if (msg.state.index >= VHOST_VRING_MAX_N)
752 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200753 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
754 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200755 goto close_socket;
756 }
757
Steven Luong67f935e2019-02-01 10:23:56 -0800758 /* protection is needed to prevent rx/tx from changing last_avail_idx */
759 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200760 /*
761 * Copy last_avail_idx from the vring before closing it because
762 * closing the vring also initializes the vring last_avail_idx
763 */
764 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
765 msg.flags |= 4;
766 msg.size = sizeof (msg.state);
767
Steven Luong67f935e2019-02-01 10:23:56 -0800768 /*
769 * Spec says: Client must [...] stop ring upon receiving
770 * VHOST_USER_GET_VRING_BASE
771 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200772 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800773 vlib_worker_thread_barrier_release (vm);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200774 vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
775 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800776 n =
777 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
778 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
779 {
780 vu_log_debug (vui, "could not send message response");
781 goto close_socket;
782 }
783 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200784 break;
785
786 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200787 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200788 break;
789
790 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800791 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
792 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200793
Steven Luong67f935e2019-02-01 10:23:56 -0800794 if (msg.size != sizeof (msg.log))
795 {
796 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
797 " %d instead of %d", msg.size, sizeof (msg.log));
798 goto close_socket;
799 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200800
Steven Luong67f935e2019-02-01 10:23:56 -0800801 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
802 {
803 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
804 "VHOST_USER_SET_LOG_BASE received");
805 goto close_socket;
806 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200807
Steven Luong67f935e2019-02-01 10:23:56 -0800808 fd = fds[0];
809 /* align size to page */
810 long page_sz = get_huge_page_size (fd);
811 ssize_t map_sz =
812 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200813
Steven Luong67f935e2019-02-01 10:23:56 -0800814 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
815 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200816
Steven Luong67f935e2019-02-01 10:23:56 -0800817 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
818 "mapped 0x%lx", map_sz, msg.log.offset, fd,
819 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200820
Steven Luong67f935e2019-02-01 10:23:56 -0800821 if (log_base_addr == MAP_FAILED)
822 {
823 vu_log_err (vui, "failed to map memory. errno is %d", errno);
824 goto close_socket;
825 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200826
Steven Luong67f935e2019-02-01 10:23:56 -0800827 vlib_worker_thread_barrier_sync (vm);
828 vui->log_base_addr = log_base_addr;
829 vui->log_base_addr += msg.log.offset;
830 vui->log_size = msg.log.size;
831 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200832
Steven Luong67f935e2019-02-01 10:23:56 -0800833 msg.flags |= 4;
834 msg.size = sizeof (msg.u64);
835 n =
836 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
837 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
838 {
839 vu_log_debug (vui, "could not send message response");
840 goto close_socket;
841 }
842 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200843
844 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200845 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200846 break;
847
848 case VHOST_USER_GET_PROTOCOL_FEATURES:
849 msg.flags |= 4;
850 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
851 (1 << VHOST_USER_PROTOCOL_F_MQ);
852 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200853 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
854 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800855 n =
856 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
857 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
858 {
859 vu_log_debug (vui, "could not send message response");
860 goto close_socket;
861 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200862 break;
863
864 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200865 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
866 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200867 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200868 break;
869
870 case VHOST_USER_GET_QUEUE_NUM:
871 msg.flags |= 4;
872 msg.u64 = VHOST_VRING_MAX_N;
873 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200874 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
875 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800876 n =
877 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
878 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
879 {
880 vu_log_debug (vui, "could not send message response");
881 goto close_socket;
882 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200883 break;
884
885 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200886 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
887 vui->hw_if_index, msg.state.num ? "enable" : "disable",
888 msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200889 if (msg.state.index >= VHOST_VRING_MAX_N)
890 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200891 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
892 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200893 goto close_socket;
894 }
895
896 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -0800897 vhost_user_thread_placement (vui, msg.state.index);
898 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200899 break;
900
901 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200902 vu_log_debug (vui, "unknown vhost-user message %d received. "
903 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200904 goto close_socket;
905 }
906
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200907 return 0;
908
909close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -0800910 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200911 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -0800912 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200913 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200914 return 0;
915}
916
917static clib_error_t *
918vhost_user_socket_error (clib_file_t * uf)
919{
920 vlib_main_t *vm = vlib_get_main ();
921 vhost_user_main_t *vum = &vhost_user_main;
922 vhost_user_intf_t *vui =
923 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
924
Jerome Tollet2f54c272018-10-02 11:41:11 +0200925 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200926 vlib_worker_thread_barrier_sync (vm);
927 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200928 vlib_worker_thread_barrier_release (vm);
929 return 0;
930}
931
932static clib_error_t *
933vhost_user_socksvr_accept_ready (clib_file_t * uf)
934{
935 int client_fd, client_len;
936 struct sockaddr_un client;
937 clib_file_t template = { 0 };
938 vhost_user_main_t *vum = &vhost_user_main;
939 vhost_user_intf_t *vui;
940
941 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
942
943 client_len = sizeof (client);
944 client_fd = accept (uf->file_descriptor,
945 (struct sockaddr *) &client,
946 (socklen_t *) & client_len);
947
948 if (client_fd < 0)
949 return clib_error_return_unix (0, "accept");
950
951 if (vui->clib_file_index != ~0)
952 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200953 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
954 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200955 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
956 }
957
Jerome Tollet2f54c272018-10-02 11:41:11 +0200958 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
959 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200960 template.read_function = vhost_user_socket_read;
961 template.error_function = vhost_user_socket_error;
962 template.file_descriptor = client_fd;
963 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
964 vui->clib_file_index = clib_file_add (&file_main, &template);
965 return 0;
966}
967
968static clib_error_t *
969vhost_user_init (vlib_main_t * vm)
970{
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200971 vhost_user_main_t *vum = &vhost_user_main;
972 vlib_thread_main_t *tm = vlib_get_thread_main ();
973
Jerome Tollet2f54c272018-10-02 11:41:11 +0200974 vum->log_default = vlib_log_register_class ("vhost-user", 0);
975
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200976 vum->coalesce_frames = 32;
977 vum->coalesce_time = 1e-3;
978
979 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
980
981 vhost_cpu_t *cpu;
982 vec_foreach (cpu, vum->cpus)
983 {
984 /* This is actually not necessary as validate already zeroes it
985 * Just keeping the loop here for later because I am lazy. */
986 cpu->rx_buffers_len = 0;
987 }
988
989 vum->random = random_default_seed ();
990
991 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
992
993 return 0;
994}
995
Dave Barachf8d50682019-05-14 18:01:44 -0400996/* *INDENT-OFF* */
997VLIB_INIT_FUNCTION (vhost_user_init) =
998{
999 .runs_after = VLIB_INITS("ip4_init"),
1000};
1001/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001002
1003static uword
1004vhost_user_send_interrupt_process (vlib_main_t * vm,
1005 vlib_node_runtime_t * rt, vlib_frame_t * f)
1006{
1007 vhost_user_intf_t *vui;
1008 f64 timeout = 3153600000.0 /* 100 years */ ;
1009 uword event_type, *event_data = 0;
1010 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -08001011 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001012 f64 now, poll_time_remaining;
1013 f64 next_timeout;
1014 u8 stop_timer = 0;
1015
1016 while (1)
1017 {
1018 poll_time_remaining =
1019 vlib_process_wait_for_event_or_clock (vm, timeout);
1020 event_type = vlib_process_get_events (vm, &event_data);
1021 vec_reset_length (event_data);
1022
1023 /*
1024 * Use the remaining timeout if it is less than coalesce time to avoid
1025 * resetting the existing timer in the middle of expiration
1026 */
1027 timeout = poll_time_remaining;
1028 if (vlib_process_suspend_time_is_zero (timeout) ||
1029 (timeout > vum->coalesce_time))
1030 timeout = vum->coalesce_time;
1031
1032 now = vlib_time_now (vm);
1033 switch (event_type)
1034 {
1035 case VHOST_USER_EVENT_STOP_TIMER:
1036 stop_timer = 1;
1037 break;
1038
1039 case VHOST_USER_EVENT_START_TIMER:
1040 stop_timer = 0;
1041 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1042 break;
1043 /* fall through */
1044
1045 case ~0:
1046 /* *INDENT-OFF* */
1047 pool_foreach (vui, vum->vhost_user_interfaces, {
1048 next_timeout = timeout;
Steven Luong67f935e2019-02-01 10:23:56 -08001049 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001050 {
Steven Luong67f935e2019-02-01 10:23:56 -08001051 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1052 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001053
Steven Luong67f935e2019-02-01 10:23:56 -08001054 if (txvq->qid == -1)
1055 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001056 if (txvq->n_since_last_int)
1057 {
1058 if (now >= txvq->int_deadline)
1059 vhost_user_send_call (vm, txvq);
1060 else
1061 next_timeout = txvq->int_deadline - now;
1062 }
1063
1064 if (rxvq->n_since_last_int)
1065 {
1066 if (now >= rxvq->int_deadline)
1067 vhost_user_send_call (vm, rxvq);
1068 else
1069 next_timeout = rxvq->int_deadline - now;
1070 }
1071
1072 if ((next_timeout < timeout) && (next_timeout > 0.0))
1073 timeout = next_timeout;
1074 }
1075 });
1076 /* *INDENT-ON* */
1077 break;
1078
1079 default:
1080 clib_warning ("BUG: unhandled event type %d", event_type);
1081 break;
1082 }
1083 /* No less than 1 millisecond */
1084 if (timeout < 1e-3)
1085 timeout = 1e-3;
1086 if (stop_timer)
1087 timeout = 3153600000.0;
1088 }
1089 return 0;
1090}
1091
1092/* *INDENT-OFF* */
1093VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1094 .function = vhost_user_send_interrupt_process,
1095 .type = VLIB_NODE_TYPE_PROCESS,
1096 .name = "vhost-user-send-interrupt-process",
1097};
1098/* *INDENT-ON* */
1099
1100static uword
1101vhost_user_process (vlib_main_t * vm,
1102 vlib_node_runtime_t * rt, vlib_frame_t * f)
1103{
1104 vhost_user_main_t *vum = &vhost_user_main;
1105 vhost_user_intf_t *vui;
1106 struct sockaddr_un sun;
1107 int sockfd;
1108 clib_file_t template = { 0 };
1109 f64 timeout = 3153600000.0 /* 100 years */ ;
1110 uword *event_data = 0;
1111
1112 sockfd = -1;
1113 sun.sun_family = AF_UNIX;
1114 template.read_function = vhost_user_socket_read;
1115 template.error_function = vhost_user_socket_error;
1116
1117 while (1)
1118 {
1119 vlib_process_wait_for_event_or_clock (vm, timeout);
1120 vlib_process_get_events (vm, &event_data);
1121 vec_reset_length (event_data);
1122
1123 timeout = 3.0;
1124
1125 /* *INDENT-OFF* */
1126 pool_foreach (vui, vum->vhost_user_interfaces, {
1127
1128 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1129 if (vui->clib_file_index == ~0)
1130 {
1131 if ((sockfd < 0) &&
1132 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1133 {
1134 /*
1135 * 1st time error or new error for this interface,
1136 * spit out the message and record the error
1137 */
1138 if (!vui->sock_errno || (vui->sock_errno != errno))
1139 {
1140 clib_unix_warning
1141 ("Error: Could not open unix socket for %s",
1142 vui->sock_filename);
1143 vui->sock_errno = errno;
1144 }
1145 continue;
1146 }
1147
1148 /* try to connect */
1149 strncpy (sun.sun_path, (char *) vui->sock_filename,
1150 sizeof (sun.sun_path) - 1);
1151
1152 /* Avoid hanging VPP if the other end does not accept */
1153 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1154 clib_unix_warning ("fcntl");
1155
1156 if (connect (sockfd, (struct sockaddr *) &sun,
1157 sizeof (struct sockaddr_un)) == 0)
1158 {
1159 /* Set the socket to blocking as it was before */
1160 if (fcntl(sockfd, F_SETFL, 0) < 0)
1161 clib_unix_warning ("fcntl2");
1162
1163 vui->sock_errno = 0;
1164 template.file_descriptor = sockfd;
1165 template.private_data =
1166 vui - vhost_user_main.vhost_user_interfaces;
1167 vui->clib_file_index = clib_file_add (&file_main, &template);
1168
1169 /* This sockfd is considered consumed */
1170 sockfd = -1;
1171 }
1172 else
1173 {
1174 vui->sock_errno = errno;
1175 }
1176 }
1177 else
1178 {
1179 /* check if socket is alive */
1180 int error = 0;
1181 socklen_t len = sizeof (error);
1182 int fd = UNIX_GET_FD(vui->clib_file_index);
1183 int retval =
1184 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1185
1186 if (retval)
1187 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001188 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001189 vhost_user_if_disconnect (vui);
1190 }
1191 }
1192 }
1193 });
1194 /* *INDENT-ON* */
1195 }
1196 return 0;
1197}
1198
1199/* *INDENT-OFF* */
1200VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1201 .function = vhost_user_process,
1202 .type = VLIB_NODE_TYPE_PROCESS,
1203 .name = "vhost-user-process",
1204};
1205/* *INDENT-ON* */
1206
1207/**
1208 * Disables and reset interface structure.
1209 * It can then be either init again, or removed from used interfaces.
1210 */
1211static void
1212vhost_user_term_if (vhost_user_intf_t * vui)
1213{
1214 int q;
1215 vhost_user_main_t *vum = &vhost_user_main;
1216
1217 // disconnect interface sockets
1218 vhost_user_if_disconnect (vui);
Steven Luong4208a4c2019-05-06 08:51:56 -07001219 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001220 vhost_user_update_iface_state (vui);
1221
1222 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1223 {
Steven Luong67f935e2019-02-01 10:23:56 -08001224 // Remove existing queue mapping for the interface
1225 if (q & 1)
1226 {
1227 int rv;
1228 vnet_main_t *vnm = vnet_get_main ();
1229 vhost_user_vring_t *txvq = &vui->vrings[q];
1230
1231 if (txvq->qid != -1)
1232 {
1233 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1234 vui->hw_if_index,
1235 q >> 1);
1236 if (rv)
1237 vu_log_warn (vui, "unable to unassign interface %d, "
1238 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1239 }
1240 }
1241
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001242 clib_mem_free ((void *) vui->vring_locks[q]);
1243 }
1244
1245 if (vui->unix_server_index != ~0)
1246 {
1247 //Close server socket
1248 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1249 vui->unix_server_index);
1250 clib_file_del (&file_main, uf);
1251 vui->unix_server_index = ~0;
1252 unlink (vui->sock_filename);
1253 }
1254
1255 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1256 &vui->if_index);
1257}
1258
1259int
1260vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1261{
1262 vhost_user_main_t *vum = &vhost_user_main;
1263 vhost_user_intf_t *vui;
1264 int rv = 0;
1265 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001266 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001267
Dave Barach3940de32019-07-23 16:28:36 -04001268 if (!
1269 (hwif =
1270 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1271 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001272 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1273
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001274 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1275
Jerome Tollet2f54c272018-10-02 11:41:11 +02001276 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1277 hwif->name, hwif->dev_instance);
1278
Steven Luong67f935e2019-02-01 10:23:56 -08001279 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1280 {
1281 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001282
Steven Luong67f935e2019-02-01 10:23:56 -08001283 if (txvq->qid == -1)
1284 continue;
1285 if ((vum->ifq_count > 0) &&
1286 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1287 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1288 {
1289 vum->ifq_count--;
1290 // Stop the timer if there is no more interrupt interface/queue
1291 if ((vum->ifq_count == 0) &&
1292 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1293 {
1294 vlib_process_signal_event (vm,
1295 vhost_user_send_interrupt_node.index,
1296 VHOST_USER_EVENT_STOP_TIMER, 0);
1297 break;
1298 }
1299 }
1300 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001301
1302 // Disable and reset interface
1303 vhost_user_term_if (vui);
1304
1305 // Reset renumbered iface
1306 if (hwif->dev_instance <
1307 vec_len (vum->show_dev_instance_by_real_dev_instance))
1308 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1309
1310 // Delete ethernet interface
1311 ethernet_delete_interface (vnm, vui->hw_if_index);
1312
1313 // Back to pool
1314 pool_put (vum->vhost_user_interfaces, vui);
1315
1316 return rv;
1317}
1318
1319static clib_error_t *
1320vhost_user_exit (vlib_main_t * vm)
1321{
1322 vnet_main_t *vnm = vnet_get_main ();
1323 vhost_user_main_t *vum = &vhost_user_main;
1324 vhost_user_intf_t *vui;
1325
1326 vlib_worker_thread_barrier_sync (vlib_get_main ());
1327 /* *INDENT-OFF* */
1328 pool_foreach (vui, vum->vhost_user_interfaces, {
1329 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1330 });
1331 /* *INDENT-ON* */
1332 vlib_worker_thread_barrier_release (vlib_get_main ());
1333 return 0;
1334}
1335
1336VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1337
1338/**
1339 * Open server unix socket on specified sock_filename.
1340 */
1341static int
1342vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1343{
1344 int rv = 0;
1345 struct sockaddr_un un = { };
1346 int fd;
1347 /* create listening socket */
1348 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1349 return VNET_API_ERROR_SYSCALL_ERROR_1;
1350
1351 un.sun_family = AF_UNIX;
1352 strncpy ((char *) un.sun_path, (char *) sock_filename,
1353 sizeof (un.sun_path) - 1);
1354
1355 /* remove if exists */
1356 unlink ((char *) sock_filename);
1357
1358 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1359 {
1360 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1361 goto error;
1362 }
1363
1364 if (listen (fd, 1) == -1)
1365 {
1366 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1367 goto error;
1368 }
1369
1370 *sock_fd = fd;
1371 return 0;
1372
1373error:
1374 close (fd);
1375 return rv;
1376}
1377
1378/**
1379 * Create ethernet interface for vhost user interface.
1380 */
1381static void
1382vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1383 vhost_user_intf_t * vui, u8 * hwaddress)
1384{
1385 vhost_user_main_t *vum = &vhost_user_main;
1386 u8 hwaddr[6];
1387 clib_error_t *error;
1388
1389 /* create hw and sw interface */
1390 if (hwaddress)
1391 {
1392 clib_memcpy (hwaddr, hwaddress, 6);
1393 }
1394 else
1395 {
1396 random_u32 (&vum->random);
1397 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1398 hwaddr[0] = 2;
1399 hwaddr[1] = 0xfe;
1400 }
1401
1402 error = ethernet_register_interface
1403 (vnm,
1404 vhost_user_device_class.index,
1405 vui - vum->vhost_user_interfaces /* device instance */ ,
1406 hwaddr /* ethernet address */ ,
1407 &vui->hw_if_index, 0 /* flag change */ );
1408
1409 if (error)
1410 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001411}
1412
1413/*
1414 * Initialize vui with specified attributes
1415 */
1416static void
1417vhost_user_vui_init (vnet_main_t * vnm,
1418 vhost_user_intf_t * vui,
1419 int server_sock_fd,
1420 const char *sock_filename,
Steven Luong4208a4c2019-05-06 08:51:56 -07001421 u64 feature_mask, u32 * sw_if_index, u8 enable_gso)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001422{
1423 vnet_sw_interface_t *sw;
1424 int q;
1425 vhost_user_main_t *vum = &vhost_user_main;
1426 vnet_hw_interface_t *hw;
1427
1428 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1429 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1430 if (server_sock_fd != -1)
1431 {
1432 clib_file_t template = { 0 };
1433 template.read_function = vhost_user_socksvr_accept_ready;
1434 template.file_descriptor = server_sock_fd;
1435 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1436 vui->unix_server_index = clib_file_add (&file_main, &template);
1437 }
1438 else
1439 {
1440 vui->unix_server_index = ~0;
1441 }
1442
1443 vui->sw_if_index = sw->sw_if_index;
1444 strncpy (vui->sock_filename, sock_filename,
1445 ARRAY_LEN (vui->sock_filename) - 1);
1446 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001447 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001448 vui->feature_mask = feature_mask;
1449 vui->clib_file_index = ~0;
1450 vui->log_base_addr = 0;
1451 vui->if_index = vui - vum->vhost_user_interfaces;
Steven Luong4208a4c2019-05-06 08:51:56 -07001452 vui->enable_gso = enable_gso;
1453 /*
1454 * enable_gso takes precedence over configurable feature mask if there
1455 * is a clash.
1456 * if feature mask disables gso, but enable_gso is configured,
1457 * then gso is enable
1458 * if feature mask enables gso, but enable_gso is not configured,
1459 * then gso is enable
1460 *
1461 * if gso is enable via feature mask, it must enable both host and guest
1462 * gso feature mask, we don't support one sided GSO or partial GSO.
1463 */
1464 if ((vui->enable_gso == 0) &&
1465 ((feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) ==
1466 (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1467 vui->enable_gso = 1;
1468 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001469 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1470 &vui->if_index, 0);
1471
1472 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1473 vhost_user_vring_init (vui, q);
1474
1475 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1476 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1477
1478 if (sw_if_index)
1479 *sw_if_index = vui->sw_if_index;
1480
1481 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1482 {
1483 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1484 CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -04001485 clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001486 }
1487
1488 vec_validate (vui->per_cpu_tx_qid,
1489 vlib_get_thread_main ()->n_vlib_mains - 1);
1490 vhost_user_tx_thread_placement (vui);
1491}
1492
1493int
1494vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1495 const char *sock_filename,
1496 u8 is_server,
1497 u32 * sw_if_index,
1498 u64 feature_mask,
Steven Luong4208a4c2019-05-06 08:51:56 -07001499 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
1500 u8 enable_gso)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001501{
1502 vhost_user_intf_t *vui = NULL;
1503 u32 sw_if_idx = ~0;
1504 int rv = 0;
1505 int server_sock_fd = -1;
1506 vhost_user_main_t *vum = &vhost_user_main;
1507 uword *if_index;
1508
1509 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1510 {
1511 return VNET_API_ERROR_INVALID_ARGUMENT;
1512 }
1513
1514 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1515 if (if_index)
1516 {
1517 if (sw_if_index)
1518 {
1519 vui = &vum->vhost_user_interfaces[*if_index];
1520 *sw_if_index = vui->sw_if_index;
1521 }
1522 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1523 }
1524
1525 if (is_server)
1526 {
1527 if ((rv =
1528 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1529 {
1530 return rv;
1531 }
1532 }
1533
Steven Luong67f935e2019-02-01 10:23:56 -08001534 /* Protect the uninitialized vui from being dispatched by rx/tx */
1535 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001536 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001537 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Steven Luong67f935e2019-02-01 10:23:56 -08001538 vlib_worker_thread_barrier_release (vm);
1539
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001540 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
Steven Luong4208a4c2019-05-06 08:51:56 -07001541 feature_mask, &sw_if_idx, enable_gso);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001542 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001543 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001544
1545 if (renumber)
1546 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1547
1548 if (sw_if_index)
1549 *sw_if_index = sw_if_idx;
1550
1551 // Process node must connect
1552 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1553
1554 return rv;
1555}
1556
1557int
1558vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1559 const char *sock_filename,
1560 u8 is_server,
1561 u32 sw_if_index,
Steven Luong4208a4c2019-05-06 08:51:56 -07001562 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
1563 u8 enable_gso)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001564{
1565 vhost_user_main_t *vum = &vhost_user_main;
1566 vhost_user_intf_t *vui = NULL;
1567 u32 sw_if_idx = ~0;
1568 int server_sock_fd = -1;
1569 int rv = 0;
1570 vnet_hw_interface_t *hwif;
1571 uword *if_index;
1572
Dave Barach3940de32019-07-23 16:28:36 -04001573 if (!
1574 (hwif =
1575 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1576 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001577 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1578
1579 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1580 return VNET_API_ERROR_INVALID_ARGUMENT;
1581
1582 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1583
1584 /*
1585 * Disallow changing the interface to have the same path name
1586 * as other interface
1587 */
1588 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1589 if (if_index && (*if_index != vui->if_index))
1590 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1591
1592 // First try to open server socket
1593 if (is_server)
1594 if ((rv = vhost_user_init_server_sock (sock_filename,
1595 &server_sock_fd)) != 0)
1596 return rv;
1597
1598 vhost_user_term_if (vui);
1599 vhost_user_vui_init (vnm, vui, server_sock_fd,
Steven Luong4208a4c2019-05-06 08:51:56 -07001600 sock_filename, feature_mask, &sw_if_idx, enable_gso);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001601
1602 if (renumber)
1603 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1604
1605 // Process node must connect
1606 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1607
1608 return rv;
1609}
1610
1611clib_error_t *
1612vhost_user_connect_command_fn (vlib_main_t * vm,
1613 unformat_input_t * input,
1614 vlib_cli_command_t * cmd)
1615{
1616 unformat_input_t _line_input, *line_input = &_line_input;
1617 u8 *sock_filename = NULL;
1618 u32 sw_if_index;
1619 u8 is_server = 0;
1620 u64 feature_mask = (u64) ~ (0ULL);
1621 u8 renumber = 0;
1622 u32 custom_dev_instance = ~0;
1623 u8 hwaddr[6];
1624 u8 *hw = NULL;
1625 clib_error_t *error = NULL;
Steven Luong4208a4c2019-05-06 08:51:56 -07001626 u8 enable_gso = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001627
1628 /* Get a line of input. */
1629 if (!unformat_user (input, unformat_line_input, line_input))
1630 return 0;
1631
Steven Luong4208a4c2019-05-06 08:51:56 -07001632 /* GSO feature is disable by default */
1633 feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001634 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1635 {
1636 if (unformat (line_input, "socket %s", &sock_filename))
1637 ;
1638 else if (unformat (line_input, "server"))
1639 is_server = 1;
Steven Luong4208a4c2019-05-06 08:51:56 -07001640 else if (unformat (line_input, "gso"))
1641 enable_gso = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001642 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1643 ;
1644 else
1645 if (unformat
1646 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1647 hw = hwaddr;
1648 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1649 {
1650 renumber = 1;
1651 }
1652 else
1653 {
1654 error = clib_error_return (0, "unknown input `%U'",
1655 format_unformat_error, line_input);
1656 goto done;
1657 }
1658 }
1659
1660 vnet_main_t *vnm = vnet_get_main ();
1661
1662 int rv;
1663 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1664 is_server, &sw_if_index, feature_mask,
Steven Luong4208a4c2019-05-06 08:51:56 -07001665 renumber, custom_dev_instance, hw,
1666 enable_gso)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001667 {
1668 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1669 goto done;
1670 }
1671
1672 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1673 sw_if_index);
1674
1675done:
1676 vec_free (sock_filename);
1677 unformat_free (line_input);
1678
1679 return error;
1680}
1681
1682clib_error_t *
1683vhost_user_delete_command_fn (vlib_main_t * vm,
1684 unformat_input_t * input,
1685 vlib_cli_command_t * cmd)
1686{
1687 unformat_input_t _line_input, *line_input = &_line_input;
1688 u32 sw_if_index = ~0;
1689 vnet_main_t *vnm = vnet_get_main ();
1690 clib_error_t *error = NULL;
1691
1692 /* Get a line of input. */
1693 if (!unformat_user (input, unformat_line_input, line_input))
1694 return 0;
1695
1696 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1697 {
1698 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1699 ;
1700 else if (unformat
1701 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1702 &sw_if_index))
1703 {
1704 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001705 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001706 if (hwif == NULL ||
1707 vhost_user_device_class.index != hwif->dev_class_index)
1708 {
1709 error = clib_error_return (0, "Not a vhost interface");
1710 goto done;
1711 }
1712 }
1713 else
1714 {
1715 error = clib_error_return (0, "unknown input `%U'",
1716 format_unformat_error, line_input);
1717 goto done;
1718 }
1719 }
1720
1721 vhost_user_delete_if (vnm, vm, sw_if_index);
1722
1723done:
1724 unformat_free (line_input);
1725
1726 return error;
1727}
1728
1729int
1730vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1731 vhost_user_intf_details_t ** out_vuids)
1732{
1733 int rv = 0;
1734 vhost_user_main_t *vum = &vhost_user_main;
1735 vhost_user_intf_t *vui;
1736 vhost_user_intf_details_t *r_vuids = NULL;
1737 vhost_user_intf_details_t *vuid = NULL;
1738 u32 *hw_if_indices = 0;
1739 vnet_hw_interface_t *hi;
1740 u8 *s = NULL;
1741 int i;
1742
1743 if (!out_vuids)
1744 return -1;
1745
1746 pool_foreach (vui, vum->vhost_user_interfaces,
1747 vec_add1 (hw_if_indices, vui->hw_if_index);
1748 );
1749
1750 for (i = 0; i < vec_len (hw_if_indices); i++)
1751 {
1752 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1753 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1754
1755 vec_add2 (r_vuids, vuid, 1);
1756 vuid->sw_if_index = vui->sw_if_index;
1757 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1758 vuid->features = vui->features;
1759 vuid->num_regions = vui->nregions;
1760 vuid->is_server = vui->unix_server_index != ~0;
1761 vuid->sock_errno = vui->sock_errno;
1762 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1763 sizeof (vuid->sock_filename));
1764 vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1765 s = format (s, "%v%c", hi->name, 0);
1766
1767 strncpy ((char *) vuid->if_name, (char *) s,
1768 ARRAY_LEN (vuid->if_name) - 1);
1769 _vec_len (s) = 0;
1770 }
1771
1772 vec_free (s);
1773 vec_free (hw_if_indices);
1774
1775 *out_vuids = r_vuids;
1776
1777 return rv;
1778}
1779
1780clib_error_t *
1781show_vhost_user_command_fn (vlib_main_t * vm,
1782 unformat_input_t * input,
1783 vlib_cli_command_t * cmd)
1784{
1785 clib_error_t *error = 0;
1786 vnet_main_t *vnm = vnet_get_main ();
1787 vhost_user_main_t *vum = &vhost_user_main;
1788 vhost_user_intf_t *vui;
1789 u32 hw_if_index, *hw_if_indices = 0;
1790 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08001791 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001792 u32 ci;
1793 int i, j, q;
1794 int show_descr = 0;
1795 struct feat_struct
1796 {
1797 u8 bit;
1798 char *str;
1799 };
1800 struct feat_struct *feat_entry;
1801
1802 static struct feat_struct feat_array[] = {
1803#define _(s,b) { .str = #s, .bit = b, },
1804 foreach_virtio_net_feature
1805#undef _
1806 {.str = NULL}
1807 };
1808
1809#define foreach_protocol_feature \
1810 _(VHOST_USER_PROTOCOL_F_MQ) \
1811 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1812
1813 static struct feat_struct proto_feat_array[] = {
1814#define _(s) { .str = #s, .bit = s},
1815 foreach_protocol_feature
1816#undef _
1817 {.str = NULL}
1818 };
1819
1820 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1821 {
1822 if (unformat
1823 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1824 {
1825 hi = vnet_get_hw_interface (vnm, hw_if_index);
1826 if (vhost_user_device_class.index != hi->dev_class_index)
1827 {
1828 error = clib_error_return (0, "unknown input `%U'",
1829 format_unformat_error, input);
1830 goto done;
1831 }
1832 vec_add1 (hw_if_indices, hw_if_index);
1833 }
1834 else if (unformat (input, "descriptors") || unformat (input, "desc"))
1835 show_descr = 1;
1836 else
1837 {
1838 error = clib_error_return (0, "unknown input `%U'",
1839 format_unformat_error, input);
1840 goto done;
1841 }
1842 }
1843 if (vec_len (hw_if_indices) == 0)
1844 {
1845 pool_foreach (vui, vum->vhost_user_interfaces,
1846 vec_add1 (hw_if_indices, vui->hw_if_index);
1847 );
1848 }
1849 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1850 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1851 vum->coalesce_frames, vum->coalesce_time);
Steven Luong4208a4c2019-05-06 08:51:56 -07001852 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001853 vum->ifq_count);
Steven Luong4208a4c2019-05-06 08:51:56 -07001854 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001855
1856 for (i = 0; i < vec_len (hw_if_indices); i++)
1857 {
1858 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1859 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07001860 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1861 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1862 hw_if_indices[i]);
Steven Luong4208a4c2019-05-06 08:51:56 -07001863 if (vui->enable_gso)
1864 vlib_cli_output (vm, " GSO enable");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001865
1866 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1867 " features mask (0x%llx): \n"
1868 " features (0x%llx): \n",
1869 vui->virtio_net_hdr_sz, vui->feature_mask,
1870 vui->features);
1871
1872 feat_entry = (struct feat_struct *) &feat_array;
1873 while (feat_entry->str)
1874 {
1875 if (vui->features & (1ULL << feat_entry->bit))
1876 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1877 feat_entry->bit);
1878 feat_entry++;
1879 }
1880
1881 vlib_cli_output (vm, " protocol features (0x%llx)",
1882 vui->protocol_features);
1883 feat_entry = (struct feat_struct *) &proto_feat_array;
1884 while (feat_entry->str)
1885 {
1886 if (vui->protocol_features & (1ULL << feat_entry->bit))
1887 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1888 feat_entry->bit);
1889 feat_entry++;
1890 }
1891
1892 vlib_cli_output (vm, "\n");
1893
1894 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1895 vui->sock_filename,
1896 (vui->unix_server_index != ~0) ? "server" : "client",
1897 strerror (vui->sock_errno));
1898
1899 vlib_cli_output (vm, " rx placement: ");
1900
Steven Luong67f935e2019-02-01 10:23:56 -08001901 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1902 {
1903 vnet_main_t *vnm = vnet_get_main ();
1904 uword thread_index;
1905 vnet_hw_interface_rx_mode mode;
1906 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001907
Steven Luong67f935e2019-02-01 10:23:56 -08001908 if (txvq->qid == -1)
1909 continue;
1910 thread_index =
1911 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
1912 qid >> 1);
1913 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
1914 &mode);
1915 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1916 thread_index, qid,
1917 format_vnet_hw_interface_rx_mode, mode);
1918 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001919
1920 vlib_cli_output (vm, " tx placement: %s\n",
1921 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1922
1923 vec_foreach_index (ci, vui->per_cpu_tx_qid)
1924 {
1925 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1926 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1927 }
1928
1929 vlib_cli_output (vm, "\n");
1930
1931 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1932
1933 if (vui->nregions)
1934 {
1935 vlib_cli_output (vm,
1936 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1937 vlib_cli_output (vm,
1938 " ====== ===== ================== ================== ================== ================== ==================\n");
1939 }
1940 for (j = 0; j < vui->nregions; j++)
1941 {
1942 vlib_cli_output (vm,
1943 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1944 j, vui->region_mmap_fd[j],
1945 vui->regions[j].guest_phys_addr,
1946 vui->regions[j].memory_size,
1947 vui->regions[j].userspace_addr,
1948 vui->regions[j].mmap_offset,
1949 pointer_to_uword (vui->region_mmap_addr[j]));
1950 }
1951 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1952 {
1953 if (!vui->vrings[q].started)
1954 continue;
1955
1956 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1957 (q & 1) ? "RX" : "TX",
1958 vui->vrings[q].enabled ? "" : " disabled");
1959
1960 vlib_cli_output (vm,
1961 " qsz %d last_avail_idx %d last_used_idx %d\n",
1962 vui->vrings[q].qsz_mask + 1,
1963 vui->vrings[q].last_avail_idx,
1964 vui->vrings[q].last_used_idx);
1965
1966 if (vui->vrings[q].avail && vui->vrings[q].used)
1967 vlib_cli_output (vm,
1968 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1969 vui->vrings[q].avail->flags,
1970 vui->vrings[q].avail->idx,
1971 vui->vrings[q].used->flags,
1972 vui->vrings[q].used->idx);
1973
1974 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1975 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1976 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1977 kickfd, callfd, vui->vrings[q].errfd);
1978
1979 if (show_descr)
1980 {
1981 vlib_cli_output (vm, "\n descriptor table:\n");
1982 vlib_cli_output (vm,
1983 " id addr len flags next user_addr\n");
1984 vlib_cli_output (vm,
1985 " ===== ================== ===== ====== ===== ==================\n");
1986 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1987 {
1988 u32 mem_hint = 0;
1989 vlib_cli_output (vm,
1990 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1991 j, vui->vrings[q].desc[j].addr,
1992 vui->vrings[q].desc[j].len,
1993 vui->vrings[q].desc[j].flags,
1994 vui->vrings[q].desc[j].next,
1995 pointer_to_uword (map_guest_mem
1996 (vui,
1997 vui->vrings[q].desc[j].
1998 addr, &mem_hint)));
1999 }
2000 }
2001 }
2002 vlib_cli_output (vm, "\n");
2003 }
2004done:
2005 vec_free (hw_if_indices);
2006 return error;
2007}
2008
2009/*
2010 * CLI functions
2011 */
2012
2013/*?
2014 * Create a vHost User interface. Once created, a new virtual interface
2015 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2016 * is the next free index.
2017 *
2018 * There are several parameters associated with a vHost interface:
2019 *
2020 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2021 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2022 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2023 * hypervisor will create the socket if it does not already exist. The VPP code
2024 * is indifferent to the file location. However, if SELinux is enabled, then the
2025 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2026 *
2027 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2028 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2029 * mode, the VM can be reset without tearing down the vHost Interface. In
2030 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2031 * tearing down the vHost Interface.
2032 *
2033 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2034 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2035 * parameter not be used except by experienced users. By default, all supported
2036 * features will be advertised. Otherwise, provide the set of features desired.
2037 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2038 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2039 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2040 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2041 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2042 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2043 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2044 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2045 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2046 *
2047 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2048 * X:X:X:X:X:X unix or X.X.X cisco format.
2049 *
2050 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2051 * in the name to be specified. If instance already exists, name will be used
2052 * anyway and multiple instances will have the same name. Use with caution.
2053 *
2054 * @cliexpar
2055 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2056 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2057 * VirtualEthernet0/0/0
2058 * @cliexend
2059 * Example of how to create a vhost interface with VPP as the server and with just
2060 * multiple queues enabled:
2061 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2062 * VirtualEthernet0/0/1
2063 * @cliexend
2064 * Once the vHost interface is created, enable the interface using:
2065 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2066?*/
2067/* *INDENT-OFF* */
2068VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2069 .path = "create vhost-user",
2070 .short_help = "create vhost-user socket <socket-filename> [server] "
Steven Luong4208a4c2019-05-06 08:51:56 -07002071 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002072 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002073 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002074};
2075/* *INDENT-ON* */
2076
2077/*?
2078 * Delete a vHost User interface using the interface name or the
2079 * software interface index. Use the '<em>show interface</em>'
2080 * command to determine the software interface index. On deletion,
2081 * the linux socket will not be deleted.
2082 *
2083 * @cliexpar
2084 * Example of how to delete a vhost interface by name:
2085 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2086 * Example of how to delete a vhost interface by software interface index:
2087 * @cliexcmd{delete vhost-user sw_if_index 1}
2088?*/
2089/* *INDENT-OFF* */
2090VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2091 .path = "delete vhost-user",
2092 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2093 .function = vhost_user_delete_command_fn,
2094};
2095
2096/*?
2097 * Display the attributes of a single vHost User interface (provide interface
2098 * name), multiple vHost User interfaces (provide a list of interface names seperated
2099 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2100 * vHost interfaces).
2101 *
2102 * @cliexpar
2103 * @parblock
2104 * Example of how to display a vhost interface:
2105 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2106 * Virtio vhost-user interfaces
2107 * Global:
2108 * coalesce frames 32 time 1e-3
2109 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2110 * virtio_net_hdr_sz 12
2111 * features mask (0xffffffffffffffff):
2112 * features (0x50408000):
2113 * VIRTIO_NET_F_MRG_RXBUF (15)
2114 * VIRTIO_NET_F_MQ (22)
2115 * VIRTIO_F_INDIRECT_DESC (28)
2116 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2117 * protocol features (0x3)
2118 * VHOST_USER_PROTOCOL_F_MQ (0)
2119 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2120 *
2121 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2122 *
2123 * rx placement:
2124 * thread 1 on vring 1
2125 * thread 1 on vring 5
2126 * thread 2 on vring 3
2127 * thread 2 on vring 7
2128 * tx placement: spin-lock
2129 * thread 0 on vring 0
2130 * thread 1 on vring 2
2131 * thread 2 on vring 0
2132 *
2133 * Memory regions (total 2)
2134 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2135 * ====== ===== ================== ================== ================== ================== ==================
2136 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2137 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2138 *
2139 * Virtqueue 0 (TX)
2140 * qsz 256 last_avail_idx 0 last_used_idx 0
2141 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2142 * kickfd 62 callfd 64 errfd -1
2143 *
2144 * Virtqueue 1 (RX)
2145 * qsz 256 last_avail_idx 0 last_used_idx 0
2146 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2147 * kickfd 65 callfd 66 errfd -1
2148 *
2149 * Virtqueue 2 (TX)
2150 * qsz 256 last_avail_idx 0 last_used_idx 0
2151 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2152 * kickfd 63 callfd 70 errfd -1
2153 *
2154 * Virtqueue 3 (RX)
2155 * qsz 256 last_avail_idx 0 last_used_idx 0
2156 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2157 * kickfd 72 callfd 74 errfd -1
2158 *
2159 * Virtqueue 4 (TX disabled)
2160 * qsz 256 last_avail_idx 0 last_used_idx 0
2161 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2162 * kickfd 76 callfd 78 errfd -1
2163 *
2164 * Virtqueue 5 (RX disabled)
2165 * qsz 256 last_avail_idx 0 last_used_idx 0
2166 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2167 * kickfd 80 callfd 82 errfd -1
2168 *
2169 * Virtqueue 6 (TX disabled)
2170 * qsz 256 last_avail_idx 0 last_used_idx 0
2171 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2172 * kickfd 84 callfd 86 errfd -1
2173 *
2174 * Virtqueue 7 (RX disabled)
2175 * qsz 256 last_avail_idx 0 last_used_idx 0
2176 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2177 * kickfd 88 callfd 90 errfd -1
2178 *
2179 * @cliexend
2180 *
2181 * The optional '<em>descriptors</em>' parameter will display the same output as
2182 * the previous example but will include the descriptor table for each queue.
2183 * The output is truncated below:
2184 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2185 * Virtio vhost-user interfaces
2186 * Global:
2187 * coalesce frames 32 time 1e-3
2188 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2189 * virtio_net_hdr_sz 12
2190 * features mask (0xffffffffffffffff):
2191 * features (0x50408000):
2192 * VIRTIO_NET_F_MRG_RXBUF (15)
2193 * VIRTIO_NET_F_MQ (22)
2194 * :
2195 * Virtqueue 0 (TX)
2196 * qsz 256 last_avail_idx 0 last_used_idx 0
2197 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2198 * kickfd 62 callfd 64 errfd -1
2199 *
2200 * descriptor table:
2201 * id addr len flags next user_addr
2202 * ===== ================== ===== ====== ===== ==================
2203 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2204 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2205 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2206 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2207 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2208 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2209 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2210 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2211 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2212 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2213 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2214 * :
2215 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2216 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2217 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2218 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2219 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2220 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2221 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2222 *
2223 * Virtqueue 1 (RX)
2224 * qsz 256 last_avail_idx 0 last_used_idx 0
2225 * :
2226 * @cliexend
2227 * @endparblock
2228?*/
2229/* *INDENT-OFF* */
2230VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2231 .path = "show vhost-user",
2232 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2233 .function = show_vhost_user_command_fn,
2234};
2235/* *INDENT-ON* */
2236
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002237
2238static clib_error_t *
2239vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2240{
2241 vhost_user_main_t *vum = &vhost_user_main;
2242
2243 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2244 {
2245 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2246 ;
2247 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2248 ;
2249 else if (unformat (input, "dont-dump-memory"))
2250 vum->dont_dump_vhost_user_memory = 1;
2251 else
2252 return clib_error_return (0, "unknown input `%U'",
2253 format_unformat_error, input);
2254 }
2255
2256 return 0;
2257}
2258
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002259/* vhost-user { ... } configuration. */
2260VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2261
2262void
2263vhost_user_unmap_all (void)
2264{
2265 vhost_user_main_t *vum = &vhost_user_main;
2266 vhost_user_intf_t *vui;
2267
2268 if (vum->dont_dump_vhost_user_memory)
2269 {
2270 pool_foreach (vui, vum->vhost_user_interfaces,
2271 unmap_all_mem_regions (vui);
2272 );
2273 }
2274}
2275
2276/*
2277 * fd.io coding-style-patch-verification: ON
2278 *
2279 * Local Variables:
2280 * eval: (c-set-style "gnu")
2281 * End:
2282 */