blob: 3217c72f95d16491858ad7ddc92c5f2777e9737f [file] [log] [blame]
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001/*
2 *------------------------------------------------------------------
3 * vhost.c - vhost-user
4 *
5 * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <fcntl.h> /* for open */
21#include <sys/ioctl.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/uio.h> /* for iovec */
27#include <netinet/in.h>
28#include <sys/vfs.h>
29
30#include <linux/if_arp.h>
31#include <linux/if_tun.h>
32
33#include <vlib/vlib.h>
34#include <vlib/unix/unix.h>
35
Mohsin Kazmie7cde312018-06-26 17:20:11 +020036#include <vnet/ethernet/ethernet.h>
37#include <vnet/devices/devices.h>
38#include <vnet/feature/feature.h>
Damjan Marion94100532020-11-06 23:25:57 +010039#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmie7cde312018-06-26 17:20:11 +020040
41#include <vnet/devices/virtio/vhost_user.h>
42#include <vnet/devices/virtio/vhost_user_inline.h>
43
44/**
45 * @file
46 * @brief vHost User Device Driver.
47 *
48 * This file contains the source code for vHost User interface.
49 */
50
51
52vlib_node_registration_t vhost_user_send_interrupt_node;
53
54/* *INDENT-OFF* */
55vhost_user_main_t vhost_user_main = {
56 .mtu_bytes = 1518,
57};
58
59VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
60 .name = "vhost-user",
61};
62/* *INDENT-ON* */
63
64static long
65get_huge_page_size (int fd)
66{
67 struct statfs s;
68 fstatfs (fd, &s);
69 return s.f_bsize;
70}
71
72static void
73unmap_all_mem_regions (vhost_user_intf_t * vui)
74{
Yichen Wang28812a02018-08-28 23:05:27 -070075 int i, r, q;
76 vhost_user_vring_t *vq;
77
Mohsin Kazmie7cde312018-06-26 17:20:11 +020078 for (i = 0; i < vui->nregions; i++)
79 {
80 if (vui->region_mmap_addr[i] != MAP_FAILED)
81 {
82
83 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
84
85 ssize_t map_sz = (vui->regions[i].memory_size +
86 vui->regions[i].mmap_offset +
87 page_sz - 1) & ~(page_sz - 1);
88
89 r =
90 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
91 map_sz);
92
Jerome Tollet2f54c272018-10-02 11:41:11 +020093 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
94 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
95 page_sz);
Mohsin Kazmie7cde312018-06-26 17:20:11 +020096
97 vui->region_mmap_addr[i] = MAP_FAILED;
98
99 if (r == -1)
100 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200101 vu_log_err (vui, "failed to unmap memory region (errno %d)",
102 errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200103 }
104 close (vui->region_mmap_fd[i]);
105 }
106 }
107 vui->nregions = 0;
Yichen Wang28812a02018-08-28 23:05:27 -0700108
Steven Luong2c1084a2020-12-10 20:44:22 -0800109 for (q = 0; q < vui->num_qid; q++)
Yichen Wang28812a02018-08-28 23:05:27 -0700110 {
111 vq = &vui->vrings[q];
112 vq->avail = 0;
113 vq->used = 0;
114 vq->desc = 0;
115 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200116}
117
Steven Luong67f935e2019-02-01 10:23:56 -0800118static_always_inline void
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200119vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
120{
121 //Let's try to assign one queue to each thread
Steven Luong67f935e2019-02-01 10:23:56 -0800122 u32 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200123 u32 thread_index = 0;
Steven Luong67f935e2019-02-01 10:23:56 -0800124
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200125 vui->use_tx_spinlock = 0;
126 while (1)
127 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800128 for (qid = 0; qid < vui->num_qid / 2; qid++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200129 {
130 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
131 if (!rxvq->started || !rxvq->enabled)
132 continue;
133
134 vui->per_cpu_tx_qid[thread_index] = qid;
135 thread_index++;
136 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
137 return;
138 }
139 //We need to loop, meaning the spinlock has to be used
140 vui->use_tx_spinlock = 1;
141 if (thread_index == 0)
142 {
143 //Could not find a single valid one
144 for (thread_index = 0;
145 thread_index < vlib_get_thread_main ()->n_vlib_mains;
146 thread_index++)
147 {
148 vui->per_cpu_tx_qid[thread_index] = 0;
149 }
150 return;
151 }
152 }
153}
154
155/**
156 * @brief Unassign existing interface/queue to thread mappings and re-assign
157 * new interface/queue to thread mappings
158 */
Steven Luong67f935e2019-02-01 10:23:56 -0800159static_always_inline void
160vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200161{
Steven Luong67f935e2019-02-01 10:23:56 -0800162 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200163 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200164 int rv;
Steven Luong67f935e2019-02-01 10:23:56 -0800165 u32 q = qid >> 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200166
Steven Luong67f935e2019-02-01 10:23:56 -0800167 ASSERT ((qid & 1) == 1); // should be odd
168 // Assign new queue mappings for the interface
Damjan Marion94100532020-11-06 23:25:57 +0100169 vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
170 vhost_user_input_node.index);
171 txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
172 VNET_HW_IF_RXQ_THREAD_ANY);
Damjan Marioneabd4242020-10-07 20:59:07 +0200173 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Steven Luong67f935e2019-02-01 10:23:56 -0800174 /* Set polling as the default */
Damjan Marioneabd4242020-10-07 20:59:07 +0200175 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
Steven Luong67f935e2019-02-01 10:23:56 -0800176 txvq->qid = q;
Damjan Marion94100532020-11-06 23:25:57 +0100177 rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
Steven Luong67f935e2019-02-01 10:23:56 -0800178 if (rv)
179 vu_log_warn (vui, "unable to set rx mode for interface %d, "
180 "queue %d: rc=%d", vui->hw_if_index, q, rv);
Damjan Marion94100532020-11-06 23:25:57 +0100181 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200182}
183
184/** @brief Returns whether at least one TX and one RX vring are enabled */
185static_always_inline int
186vhost_user_intf_ready (vhost_user_intf_t * vui)
187{
188 int i, found[2] = { }; //RX + TX
189
Steven Luong2c1084a2020-12-10 20:44:22 -0800190 for (i = 0; i < vui->num_qid; i++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200191 if (vui->vrings[i].started && vui->vrings[i].enabled)
192 found[i & 1] = 1;
193
194 return found[0] && found[1];
195}
196
Steven Luong67f935e2019-02-01 10:23:56 -0800197static_always_inline void
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200198vhost_user_update_iface_state (vhost_user_intf_t * vui)
199{
200 /* if we have pointers to descriptor table, go up */
Juraj Slobodab192feb2018-10-01 12:42:07 +0200201 int is_ready = vhost_user_intf_ready (vui);
202 if (is_ready != vui->is_ready)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200203 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200204 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
205 is_ready ? "ready" : "down");
Juraj Slobodab192feb2018-10-01 12:42:07 +0200206 if (vui->admin_up)
207 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
208 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
209 : 0);
210 vui->is_ready = is_ready;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200211 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200212}
213
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200214static clib_error_t *
215vhost_user_callfd_read_ready (clib_file_t * uf)
216{
217 __attribute__ ((unused)) int n;
218 u8 buff[8];
219
220 n = read (uf->file_descriptor, ((char *) &buff), 8);
221
222 return 0;
223}
224
Steven Luong67f935e2019-02-01 10:23:56 -0800225static_always_inline void
226vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
227{
228 if (qid & 1) // RX is odd, TX is even
229 {
230 if (vui->vrings[qid].qid == -1)
231 vhost_user_rx_thread_placement (vui, qid);
232 }
233 else
234 vhost_user_tx_thread_placement (vui);
235}
236
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200237static clib_error_t *
238vhost_user_kickfd_read_ready (clib_file_t * uf)
239{
Steven Luong27e93a52021-05-06 10:00:30 -0700240 __attribute__ ((unused)) ssize_t n;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200241 u8 buff[8];
Steven Luong27e93a52021-05-06 10:00:30 -0700242 vhost_user_main_t *vum = &vhost_user_main;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200243 vhost_user_intf_t *vui =
Steven Luong27e93a52021-05-06 10:00:30 -0700244 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200245 u32 qid = uf->private_data & 0xff;
Steven Luong27e93a52021-05-06 10:00:30 -0700246 u32 is_txq = qid & 1;
247 vhost_user_vring_t *vq = &vui->vrings[qid];
248 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200249
Steven Luong27e93a52021-05-06 10:00:30 -0700250 n = read (uf->file_descriptor, buff, 8);
251 if (vq->started == 0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200252 {
Steven Luong27e93a52021-05-06 10:00:30 -0700253 vq->started = 1;
254 vhost_user_thread_placement (vui, qid);
255 vhost_user_update_iface_state (vui);
256 if (is_txq)
257 vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
258 vq->kickfd_idx);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200259 }
260
Steven Luong27e93a52021-05-06 10:00:30 -0700261 if (is_txq && (vhost_user_intf_ready (vui) &&
262 ((vq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
263 (vq->mode == VNET_HW_IF_RX_MODE_INTERRUPT))))
264 vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
265
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200266 return 0;
267}
268
269static_always_inline void
270vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
271{
272 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong2c1084a2020-12-10 20:44:22 -0800273
Dave Barachb7b92992018-10-17 10:38:51 -0400274 clib_memset (vring, 0, sizeof (*vring));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200275 vring->kickfd_idx = ~0;
276 vring->callfd_idx = ~0;
277 vring->errfd = -1;
Steven Luong67f935e2019-02-01 10:23:56 -0800278 vring->qid = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200279
Steven Luong2c1084a2020-12-10 20:44:22 -0800280 clib_spinlock_init (&vring->vring_lock);
281
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200282 /*
283 * We have a bug with some qemu 2.5, and this may be a fix.
284 * Feel like interpretation holy text, but this is from vhost-user.txt.
285 * "
286 * One queue pair is enabled initially. More queues are enabled
287 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
288 * "
289 * Don't know who's right, but this is what DPDK does.
290 */
291 if (qid == 0 || qid == 1)
292 vring->enabled = 1;
293}
294
295static_always_inline void
296vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
297{
298 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong67f935e2019-02-01 10:23:56 -0800299
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200300 if (vring->kickfd_idx != ~0)
301 {
302 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
303 vring->kickfd_idx);
304 clib_file_del (&file_main, uf);
305 vring->kickfd_idx = ~0;
306 }
307 if (vring->callfd_idx != ~0)
308 {
309 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
310 vring->callfd_idx);
311 clib_file_del (&file_main, uf);
312 vring->callfd_idx = ~0;
313 }
314 if (vring->errfd != -1)
315 {
316 close (vring->errfd);
317 vring->errfd = -1;
318 }
Steven Luong67f935e2019-02-01 10:23:56 -0800319
Steven Luong2c1084a2020-12-10 20:44:22 -0800320 clib_spinlock_free (&vring->vring_lock);
321
Steven Luong67f935e2019-02-01 10:23:56 -0800322 // save the qid so that we don't need to unassign and assign_rx_thread
323 // when the interface comes back up. They are expensive calls.
324 u16 q = vui->vrings[qid].qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200325 vhost_user_vring_init (vui, qid);
Steven Luong67f935e2019-02-01 10:23:56 -0800326 vui->vrings[qid].qid = q;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200327}
328
329static_always_inline void
330vhost_user_if_disconnect (vhost_user_intf_t * vui)
331{
332 vnet_main_t *vnm = vnet_get_main ();
333 int q;
334
335 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
336
337 if (vui->clib_file_index != ~0)
338 {
339 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
340 vui->clib_file_index = ~0;
341 }
342
Juraj Slobodab192feb2018-10-01 12:42:07 +0200343 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200344
Steven Luong2c1084a2020-12-10 20:44:22 -0800345 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200346 vhost_user_vring_close (vui, q);
347
348 unmap_all_mem_regions (vui);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200349 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200350}
351
352static clib_error_t *
353vhost_user_socket_read (clib_file_t * uf)
354{
Steven Luong67f935e2019-02-01 10:23:56 -0800355 int n, i, j;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200356 int fd, number_of_fds = 0;
357 int fds[VHOST_MEMORY_MAX_NREGIONS];
358 vhost_user_msg_t msg;
359 struct msghdr mh;
360 struct iovec iov[1];
361 vhost_user_main_t *vum = &vhost_user_main;
362 vhost_user_intf_t *vui;
363 struct cmsghdr *cmsg;
364 u8 q;
365 clib_file_t template = { 0 };
366 vnet_main_t *vnm = vnet_get_main ();
Steven Luong67f935e2019-02-01 10:23:56 -0800367 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200368
369 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
370
371 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
372
Dave Barachb7b92992018-10-17 10:38:51 -0400373 clib_memset (&mh, 0, sizeof (mh));
374 clib_memset (control, 0, sizeof (control));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200375
376 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
377 fds[i] = -1;
378
379 /* set the payload */
380 iov[0].iov_base = (void *) &msg;
381 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
382
383 mh.msg_iov = iov;
384 mh.msg_iovlen = 1;
385 mh.msg_control = control;
386 mh.msg_controllen = sizeof (control);
387
388 n = recvmsg (uf->file_descriptor, &mh, 0);
389
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200390 if (n != VHOST_USER_MSG_HDR_SZ)
391 {
392 if (n == -1)
393 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200394 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
395 strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200396 }
397 else
398 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200399 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
400 n, VHOST_USER_MSG_HDR_SZ);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200401 }
402 goto close_socket;
403 }
404
405 if (mh.msg_flags & MSG_CTRUNC)
406 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200407 vu_log_debug (vui, "MSG_CTRUNC is set");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200408 goto close_socket;
409 }
410
411 cmsg = CMSG_FIRSTHDR (&mh);
412
413 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
414 (cmsg->cmsg_type == SCM_RIGHTS) &&
415 (cmsg->cmsg_len - CMSG_LEN (0) <=
416 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
417 {
418 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
Dave Barach178cf492018-11-13 16:34:13 -0500419 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200420 }
421
422 /* version 1, no reply bit set */
423 if ((msg.flags & 7) != 1)
424 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200425 vu_log_debug (vui, "malformed message received. closing socket");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200426 goto close_socket;
427 }
428
429 {
430 int rv;
431 rv =
432 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
433 msg.size);
434 if (rv < 0)
435 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200436 vu_log_debug (vui, "read failed %s", strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200437 goto close_socket;
438 }
439 else if (rv != msg.size)
440 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200441 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
442 msg.size);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200443 goto close_socket;
444 }
445 }
446
447 switch (msg.request)
448 {
449 case VHOST_USER_GET_FEATURES:
450 msg.flags |= 4;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200451 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
452 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
453 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
454 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
455 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
456 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
457 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
458 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
459 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200460 msg.u64 &= vui->feature_mask;
Steven Luong4208a4c2019-05-06 08:51:56 -0700461
Steven Luong27ba5002020-11-17 13:30:44 -0800462 if (vui->enable_event_idx)
463 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
Steven Luong4208a4c2019-05-06 08:51:56 -0700464 if (vui->enable_gso)
465 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700466 if (vui->enable_packed)
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200467 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
Steven Luong4208a4c2019-05-06 08:51:56 -0700468
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200469 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200470 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
471 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800472 n =
473 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
474 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
475 {
476 vu_log_debug (vui, "could not send message response");
477 goto close_socket;
478 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200479 break;
480
481 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200482 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
483 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200484
485 vui->features = msg.u64;
486
487 if (vui->features &
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200488 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
489 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200490 vui->virtio_net_hdr_sz = 12;
491 else
492 vui->virtio_net_hdr_sz = 10;
493
494 vui->is_any_layout =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200495 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200496
497 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Steven Luong4208a4c2019-05-06 08:51:56 -0700498 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
499 if (vui->enable_gso &&
Steven Luonga75ad872019-08-06 21:51:34 -0700500 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
501 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100502 {
503 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
504 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
505 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
506 }
Steven Luong4208a4c2019-05-06 08:51:56 -0700507 else
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100508 {
509 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
510 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
511 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200512 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200513 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700514 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200515 break;
516
517 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200518 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
519 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200520
521 if ((msg.memory.nregions < 1) ||
522 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
523 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200524 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
525 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200526 goto close_socket;
527 }
528
529 if (msg.memory.nregions != number_of_fds)
530 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200531 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200532 goto close_socket;
533 }
Steven Luong67f935e2019-02-01 10:23:56 -0800534
535 /* Do the mmap without barrier sync */
536 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
537 for (i = 0; i < msg.memory.nregions; i++)
538 {
539 long page_sz = get_huge_page_size (fds[i]);
540
541 /* align size to page */
542 ssize_t map_sz = (msg.memory.regions[i].memory_size +
543 msg.memory.regions[i].mmap_offset +
544 page_sz - 1) & ~(page_sz - 1);
545
546 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
547 MAP_SHARED, fds[i], 0);
548 if (region_mmap_addr[i] == MAP_FAILED)
549 {
550 vu_log_err (vui, "failed to map memory. errno is %d", errno);
551 for (j = 0; j < i; j++)
552 munmap (region_mmap_addr[j], map_sz);
553 goto close_socket;
554 }
555 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
556 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
557 region_mmap_addr[i], page_sz);
558 }
559
560 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200561 unmap_all_mem_regions (vui);
562 for (i = 0; i < msg.memory.nregions; i++)
563 {
Dave Barach178cf492018-11-13 16:34:13 -0500564 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
565 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200566
Steven Luong67f935e2019-02-01 10:23:56 -0800567 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200568 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
569 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
570 vui->regions[i].memory_size;
571
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200572 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
573 vui->region_mmap_fd[i] = fds[i];
574
575 vui->nregions++;
576 }
Steven Luong4442f7c2019-10-02 07:33:48 -0700577
578 /*
579 * Re-compute desc, used, and avail descriptor table if vring address
580 * is set.
581 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800582 for (q = 0; q < vui->num_qid; q++)
Steven Luong4442f7c2019-10-02 07:33:48 -0700583 {
584 if (vui->vrings[q].desc_user_addr &&
585 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
586 {
587 vui->vrings[q].desc =
588 map_user_mem (vui, vui->vrings[q].desc_user_addr);
589 vui->vrings[q].used =
590 map_user_mem (vui, vui->vrings[q].used_user_addr);
591 vui->vrings[q].avail =
592 map_user_mem (vui, vui->vrings[q].avail_user_addr);
593 }
594 }
Steven Luong67f935e2019-02-01 10:23:56 -0800595 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200596 break;
597
598 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200599 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
600 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200601
602 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
603 (msg.state.num == 0) || /* it cannot be zero */
Benoît Ganne32526a42020-12-08 19:08:05 +0100604 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800605 (msg.state.index >= vui->num_qid))
606 {
607 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
608 " %d, msg.state.index %d, curruent max q %d",
609 msg.state.num, msg.state.index, vui->num_qid);
610 goto close_socket;
611 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200612 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
613 break;
614
615 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200616 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
617 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200618
Steven Luong2c1084a2020-12-10 20:44:22 -0800619 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200620 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200621 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800622 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200623 goto close_socket;
624 }
625
626 if (msg.size < sizeof (msg.addr))
627 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200628 vu_log_debug (vui, "vhost message is too short (%d < %d)",
629 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200630 goto close_socket;
631 }
632
Steven Luong67f935e2019-02-01 10:23:56 -0800633 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
634 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
635 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200636
Steven Luong67f935e2019-02-01 10:23:56 -0800637 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200638 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200639 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
640 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200641 goto close_socket;
642 }
643
Steven Luong4442f7c2019-10-02 07:33:48 -0700644 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
645 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
646 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
647
Steven Luong67f935e2019-02-01 10:23:56 -0800648 vlib_worker_thread_barrier_sync (vm);
649 vui->vrings[msg.state.index].desc = desc;
650 vui->vrings[msg.state.index].used = used;
651 vui->vrings[msg.state.index].avail = avail;
652
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200653 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
654 vui->vrings[msg.state.index].log_used =
655 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
656
657 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
658 the ring is initialized in an enabled state. */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200659 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800660 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200661
662 vui->vrings[msg.state.index].last_used_idx =
663 vui->vrings[msg.state.index].last_avail_idx =
664 vui->vrings[msg.state.index].used->idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800665 vui->vrings[msg.state.index].last_kick =
666 vui->vrings[msg.state.index].last_used_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200667
668 /* tell driver that we don't want interrupts */
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700669 if (vhost_user_is_packed_ring_supported (vui))
670 vui->vrings[msg.state.index].used_event->flags =
671 VRING_EVENT_F_DISABLE;
672 else
673 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Steven Luong67f935e2019-02-01 10:23:56 -0800674 vlib_worker_thread_barrier_release (vm);
675 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200676 break;
677
678 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200679 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200680 break;
681
682 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200683 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
684 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200685 break;
686
687 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200688 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
689 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200690
691 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800692 if (vui->num_qid > q)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200693 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800694 /* if there is old fd, delete and close it */
695 if (vui->vrings[q].callfd_idx != ~0)
696 {
697 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
698 vui->vrings[q].callfd_idx);
699 clib_file_del (&file_main, uf);
700 vui->vrings[q].callfd_idx = ~0;
701 }
702 }
703 else if (vec_len (vui->vrings) > q)
704 {
705 /* grow vrings by pair (RX + TX) */
706 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
707 }
708 else
709 {
710 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
711
712 /*
713 * Double the array size if it is less than 64 entries.
714 * Slow down thereafter.
715 */
716 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
717 new_max_q = vec_len (vui->vrings) << 1;
718 else
719 new_max_q = vec_len (vui->vrings) +
720 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
721 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
722 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
723
724 /* sync with the worker threads, vrings may move due to realloc */
725 vlib_worker_thread_barrier_sync (vm);
726 vec_validate_aligned (vui->vrings, new_max_q - 1,
727 CLIB_CACHE_LINE_BYTES);
728 vlib_worker_thread_barrier_release (vm);
729
730 for (i = old_max_q; i < vec_len (vui->vrings); i++)
731 vhost_user_vring_init (vui, i);
732
733 /* grow vrings by pair (RX + TX) */
734 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200735 }
736
737 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
738 {
739 if (number_of_fds != 1)
740 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200741 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200742 goto close_socket;
743 }
744
745 template.read_function = vhost_user_callfd_read_ready;
746 template.file_descriptor = fds[0];
747 template.private_data =
748 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500749 template.description = format (0, "vhost user");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200750 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
751 }
752 else
753 vui->vrings[q].callfd_idx = ~0;
754 break;
755
756 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200757 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
758 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200759
760 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800761 if (q >= vui->num_qid)
762 {
763 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
764 " %u >= %u", q, vui->num_qid);
765 goto close_socket;
766 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200767
768 if (vui->vrings[q].kickfd_idx != ~0)
769 {
770 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
771 vui->vrings[q].kickfd_idx);
772 clib_file_del (&file_main, uf);
773 vui->vrings[q].kickfd_idx = ~0;
774 }
775
776 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
777 {
778 if (number_of_fds != 1)
779 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200780 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200781 goto close_socket;
782 }
783
784 template.read_function = vhost_user_kickfd_read_ready;
785 template.file_descriptor = fds[0];
786 template.private_data =
787 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
788 q;
789 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
790 }
791 else
792 {
793 //When no kickfd is set, the queue is initialized as started
794 vui->vrings[q].kickfd_idx = ~0;
795 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800796 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200797 }
Steven Luong67f935e2019-02-01 10:23:56 -0800798 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200799 break;
800
801 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200802 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
803 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200804
805 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800806 if (q >= vui->num_qid)
807 {
808 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
809 " %u >= %u", q, vui->num_qid);
810 goto close_socket;
811 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200812
813 if (vui->vrings[q].errfd != -1)
814 close (vui->vrings[q].errfd);
815
816 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
817 {
818 if (number_of_fds != 1)
819 goto close_socket;
820
821 vui->vrings[q].errfd = fds[0];
822 }
823 else
824 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200825 break;
826
827 case VHOST_USER_SET_VRING_BASE:
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700828 vu_log_debug (vui,
829 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200830 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong2c1084a2020-12-10 20:44:22 -0800831 if (msg.state.index >= vui->num_qid)
832 {
833 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
834 " %u >= %u", msg.state.index, vui->num_qid);
835 goto close_socket;
836 }
Steven Luong67f935e2019-02-01 10:23:56 -0800837 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200838 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700839 if (vhost_user_is_packed_ring_supported (vui))
840 {
841 /*
842 * 0 1 2 3
843 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
844 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
845 * | last avail idx | | last used idx | |
846 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
847 * ^ ^
848 * | |
849 * avail wrap counter used wrap counter
850 */
851 /* last avail idx at bit 0-14. */
852 vui->vrings[msg.state.index].last_avail_idx =
853 msg.state.num & 0x7fff;
854 /* avail wrap counter at bit 15 */
855 vui->vrings[msg.state.index].avail_wrap_counter =
856 ! !(msg.state.num & (1 << 15));
857
858 /*
859 * Although last_used_idx is passed in the upper 16 bits in qemu
860 * implementation, in practice, last_avail_idx and last_used_idx are
861 * usually the same. As a result, DPDK does not bother to pass us
862 * last_used_idx. The spec is not clear on thex coding. I figured it
863 * out by reading the qemu code. So let's just read last_avail_idx
864 * and set last_used_idx equals to last_avail_idx.
865 */
866 vui->vrings[msg.state.index].last_used_idx =
867 vui->vrings[msg.state.index].last_avail_idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800868 vui->vrings[msg.state.index].last_kick =
869 vui->vrings[msg.state.index].last_used_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700870 vui->vrings[msg.state.index].used_wrap_counter =
871 vui->vrings[msg.state.index].avail_wrap_counter;
872
873 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
874 vui->vrings[msg.state.index].avail_wrap_counter =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200875 VRING_DESC_F_AVAIL;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700876 }
Steven Luong67f935e2019-02-01 10:23:56 -0800877 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200878 break;
879
880 case VHOST_USER_GET_VRING_BASE:
Steven Luong2c1084a2020-12-10 20:44:22 -0800881 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200882 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200883 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800884 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200885 goto close_socket;
886 }
887
Steven Luong67f935e2019-02-01 10:23:56 -0800888 /* protection is needed to prevent rx/tx from changing last_avail_idx */
889 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200890 /*
891 * Copy last_avail_idx from the vring before closing it because
892 * closing the vring also initializes the vring last_avail_idx
893 */
894 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700895 if (vhost_user_is_packed_ring_supported (vui))
896 {
897 msg.state.num =
898 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
899 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
900 msg.state.num |=
901 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
902 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
903 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200904 msg.flags |= 4;
905 msg.size = sizeof (msg.state);
906
Steven Luong67f935e2019-02-01 10:23:56 -0800907 /*
908 * Spec says: Client must [...] stop ring upon receiving
909 * VHOST_USER_GET_VRING_BASE
910 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200911 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800912 vlib_worker_thread_barrier_release (vm);
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700913 vu_log_debug (vui,
914 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200915 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800916 n =
917 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
918 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
919 {
920 vu_log_debug (vui, "could not send message response");
921 goto close_socket;
922 }
923 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200924 break;
925
926 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200927 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200928 break;
929
930 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800931 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
932 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200933
Steven Luong67f935e2019-02-01 10:23:56 -0800934 if (msg.size != sizeof (msg.log))
935 {
936 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
937 " %d instead of %d", msg.size, sizeof (msg.log));
938 goto close_socket;
939 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200940
Steven Luong67f935e2019-02-01 10:23:56 -0800941 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
942 {
943 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
944 "VHOST_USER_SET_LOG_BASE received");
945 goto close_socket;
946 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200947
Steven Luong67f935e2019-02-01 10:23:56 -0800948 fd = fds[0];
949 /* align size to page */
950 long page_sz = get_huge_page_size (fd);
951 ssize_t map_sz =
952 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200953
Steven Luong67f935e2019-02-01 10:23:56 -0800954 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
955 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200956
Steven Luong67f935e2019-02-01 10:23:56 -0800957 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
958 "mapped 0x%lx", map_sz, msg.log.offset, fd,
959 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200960
Steven Luong67f935e2019-02-01 10:23:56 -0800961 if (log_base_addr == MAP_FAILED)
962 {
963 vu_log_err (vui, "failed to map memory. errno is %d", errno);
964 goto close_socket;
965 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200966
Steven Luong67f935e2019-02-01 10:23:56 -0800967 vlib_worker_thread_barrier_sync (vm);
968 vui->log_base_addr = log_base_addr;
969 vui->log_base_addr += msg.log.offset;
970 vui->log_size = msg.log.size;
971 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200972
Steven Luong67f935e2019-02-01 10:23:56 -0800973 msg.flags |= 4;
974 msg.size = sizeof (msg.u64);
975 n =
976 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
977 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
978 {
979 vu_log_debug (vui, "could not send message response");
980 goto close_socket;
981 }
982 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200983
984 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200985 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200986 break;
987
988 case VHOST_USER_GET_PROTOCOL_FEATURES:
989 msg.flags |= 4;
990 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
991 (1 << VHOST_USER_PROTOCOL_F_MQ);
992 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200993 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
994 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800995 n =
996 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
997 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
998 {
999 vu_log_debug (vui, "could not send message response");
1000 goto close_socket;
1001 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001002 break;
1003
1004 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001005 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1006 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001007 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001008 break;
1009
1010 case VHOST_USER_GET_QUEUE_NUM:
1011 msg.flags |= 4;
Steven Luong2c1084a2020-12-10 20:44:22 -08001012 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001013 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001014 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1015 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001016 n =
1017 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1018 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1019 {
1020 vu_log_debug (vui, "could not send message response");
1021 goto close_socket;
1022 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001023 break;
1024
1025 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001026 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1027 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1028 msg.state.index);
Steven Luong2c1084a2020-12-10 20:44:22 -08001029 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001030 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001031 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
Steven Luong2c1084a2020-12-10 20:44:22 -08001032 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001033 goto close_socket;
1034 }
1035
1036 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -08001037 vhost_user_thread_placement (vui, msg.state.index);
1038 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001039 break;
1040
1041 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001042 vu_log_debug (vui, "unknown vhost-user message %d received. "
1043 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001044 goto close_socket;
1045 }
1046
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001047 return 0;
1048
1049close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -08001050 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001051 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -08001052 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001053 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001054 return 0;
1055}
1056
1057static clib_error_t *
1058vhost_user_socket_error (clib_file_t * uf)
1059{
1060 vlib_main_t *vm = vlib_get_main ();
1061 vhost_user_main_t *vum = &vhost_user_main;
1062 vhost_user_intf_t *vui =
1063 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1064
Jerome Tollet2f54c272018-10-02 11:41:11 +02001065 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001066 vlib_worker_thread_barrier_sync (vm);
1067 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001068 vlib_worker_thread_barrier_release (vm);
1069 return 0;
1070}
1071
1072static clib_error_t *
1073vhost_user_socksvr_accept_ready (clib_file_t * uf)
1074{
1075 int client_fd, client_len;
1076 struct sockaddr_un client;
1077 clib_file_t template = { 0 };
1078 vhost_user_main_t *vum = &vhost_user_main;
1079 vhost_user_intf_t *vui;
1080
1081 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1082
1083 client_len = sizeof (client);
1084 client_fd = accept (uf->file_descriptor,
1085 (struct sockaddr *) &client,
1086 (socklen_t *) & client_len);
1087
1088 if (client_fd < 0)
1089 return clib_error_return_unix (0, "accept");
1090
1091 if (vui->clib_file_index != ~0)
1092 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001093 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1094 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001095 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1096 }
1097
Jerome Tollet2f54c272018-10-02 11:41:11 +02001098 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1099 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001100 template.read_function = vhost_user_socket_read;
1101 template.error_function = vhost_user_socket_error;
1102 template.file_descriptor = client_fd;
1103 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001104 template.description = format (0, "vhost interface %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001105 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001106 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001107 return 0;
1108}
1109
1110static clib_error_t *
1111vhost_user_init (vlib_main_t * vm)
1112{
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001113 vhost_user_main_t *vum = &vhost_user_main;
1114 vlib_thread_main_t *tm = vlib_get_thread_main ();
1115
Jerome Tollet2f54c272018-10-02 11:41:11 +02001116 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1117
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001118 vum->coalesce_frames = 32;
1119 vum->coalesce_time = 1e-3;
1120
1121 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1122
1123 vhost_cpu_t *cpu;
1124 vec_foreach (cpu, vum->cpus)
1125 {
1126 /* This is actually not necessary as validate already zeroes it
1127 * Just keeping the loop here for later because I am lazy. */
1128 cpu->rx_buffers_len = 0;
1129 }
1130
1131 vum->random = random_default_seed ();
1132
1133 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1134
1135 return 0;
1136}
1137
Dave Barachf8d50682019-05-14 18:01:44 -04001138/* *INDENT-OFF* */
1139VLIB_INIT_FUNCTION (vhost_user_init) =
1140{
1141 .runs_after = VLIB_INITS("ip4_init"),
1142};
1143/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001144
1145static uword
1146vhost_user_send_interrupt_process (vlib_main_t * vm,
1147 vlib_node_runtime_t * rt, vlib_frame_t * f)
1148{
1149 vhost_user_intf_t *vui;
1150 f64 timeout = 3153600000.0 /* 100 years */ ;
1151 uword event_type, *event_data = 0;
1152 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -08001153 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001154 f64 now, poll_time_remaining;
1155 f64 next_timeout;
1156 u8 stop_timer = 0;
1157
1158 while (1)
1159 {
1160 poll_time_remaining =
1161 vlib_process_wait_for_event_or_clock (vm, timeout);
1162 event_type = vlib_process_get_events (vm, &event_data);
1163 vec_reset_length (event_data);
1164
1165 /*
1166 * Use the remaining timeout if it is less than coalesce time to avoid
1167 * resetting the existing timer in the middle of expiration
1168 */
1169 timeout = poll_time_remaining;
1170 if (vlib_process_suspend_time_is_zero (timeout) ||
1171 (timeout > vum->coalesce_time))
1172 timeout = vum->coalesce_time;
1173
1174 now = vlib_time_now (vm);
1175 switch (event_type)
1176 {
1177 case VHOST_USER_EVENT_STOP_TIMER:
1178 stop_timer = 1;
1179 break;
1180
1181 case VHOST_USER_EVENT_START_TIMER:
1182 stop_timer = 0;
1183 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1184 break;
1185 /* fall through */
1186
1187 case ~0:
1188 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001189 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001190 next_timeout = timeout;
Steven Luong2c1084a2020-12-10 20:44:22 -08001191 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001192 {
Steven Luong67f935e2019-02-01 10:23:56 -08001193 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1194 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001195
Steven Luong67f935e2019-02-01 10:23:56 -08001196 if (txvq->qid == -1)
1197 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001198 if (txvq->n_since_last_int)
1199 {
1200 if (now >= txvq->int_deadline)
Steven Luong27ba5002020-11-17 13:30:44 -08001201 vhost_user_send_call (vm, vui, txvq);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001202 else
1203 next_timeout = txvq->int_deadline - now;
1204 }
1205
1206 if (rxvq->n_since_last_int)
1207 {
1208 if (now >= rxvq->int_deadline)
Steven Luong27ba5002020-11-17 13:30:44 -08001209 vhost_user_send_call (vm, vui, rxvq);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001210 else
1211 next_timeout = rxvq->int_deadline - now;
1212 }
1213
1214 if ((next_timeout < timeout) && (next_timeout > 0.0))
1215 timeout = next_timeout;
1216 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001217 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001218 /* *INDENT-ON* */
1219 break;
1220
1221 default:
1222 clib_warning ("BUG: unhandled event type %d", event_type);
1223 break;
1224 }
1225 /* No less than 1 millisecond */
1226 if (timeout < 1e-3)
1227 timeout = 1e-3;
1228 if (stop_timer)
1229 timeout = 3153600000.0;
1230 }
1231 return 0;
1232}
1233
1234/* *INDENT-OFF* */
1235VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1236 .function = vhost_user_send_interrupt_process,
1237 .type = VLIB_NODE_TYPE_PROCESS,
1238 .name = "vhost-user-send-interrupt-process",
1239};
1240/* *INDENT-ON* */
1241
1242static uword
1243vhost_user_process (vlib_main_t * vm,
1244 vlib_node_runtime_t * rt, vlib_frame_t * f)
1245{
1246 vhost_user_main_t *vum = &vhost_user_main;
1247 vhost_user_intf_t *vui;
1248 struct sockaddr_un sun;
1249 int sockfd;
1250 clib_file_t template = { 0 };
1251 f64 timeout = 3153600000.0 /* 100 years */ ;
1252 uword *event_data = 0;
1253
1254 sockfd = -1;
1255 sun.sun_family = AF_UNIX;
1256 template.read_function = vhost_user_socket_read;
1257 template.error_function = vhost_user_socket_error;
1258
1259 while (1)
1260 {
1261 vlib_process_wait_for_event_or_clock (vm, timeout);
1262 vlib_process_get_events (vm, &event_data);
1263 vec_reset_length (event_data);
1264
1265 timeout = 3.0;
1266
1267 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001268 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001269
1270 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1271 if (vui->clib_file_index == ~0)
1272 {
1273 if ((sockfd < 0) &&
1274 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1275 {
1276 /*
1277 * 1st time error or new error for this interface,
1278 * spit out the message and record the error
1279 */
1280 if (!vui->sock_errno || (vui->sock_errno != errno))
1281 {
1282 clib_unix_warning
1283 ("Error: Could not open unix socket for %s",
1284 vui->sock_filename);
1285 vui->sock_errno = errno;
1286 }
1287 continue;
1288 }
1289
1290 /* try to connect */
1291 strncpy (sun.sun_path, (char *) vui->sock_filename,
1292 sizeof (sun.sun_path) - 1);
Damjan Marion62c25ab2020-12-12 23:32:12 +01001293 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001294
1295 /* Avoid hanging VPP if the other end does not accept */
1296 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1297 clib_unix_warning ("fcntl");
1298
1299 if (connect (sockfd, (struct sockaddr *) &sun,
1300 sizeof (struct sockaddr_un)) == 0)
1301 {
1302 /* Set the socket to blocking as it was before */
1303 if (fcntl(sockfd, F_SETFL, 0) < 0)
1304 clib_unix_warning ("fcntl2");
1305
1306 vui->sock_errno = 0;
1307 template.file_descriptor = sockfd;
1308 template.private_data =
1309 vui - vhost_user_main.vhost_user_interfaces;
Steven Luonge2daada2021-04-02 22:42:26 -07001310 template.description = format (0, "vhost user process");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001311 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001312 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001313
1314 /* This sockfd is considered consumed */
1315 sockfd = -1;
1316 }
1317 else
1318 {
1319 vui->sock_errno = errno;
1320 }
1321 }
1322 else
1323 {
1324 /* check if socket is alive */
1325 int error = 0;
1326 socklen_t len = sizeof (error);
1327 int fd = UNIX_GET_FD(vui->clib_file_index);
1328 int retval =
1329 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1330
1331 if (retval)
1332 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001333 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001334 vhost_user_if_disconnect (vui);
1335 }
1336 }
1337 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001338 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001339 /* *INDENT-ON* */
1340 }
1341 return 0;
1342}
1343
1344/* *INDENT-OFF* */
1345VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1346 .function = vhost_user_process,
1347 .type = VLIB_NODE_TYPE_PROCESS,
1348 .name = "vhost-user-process",
1349};
1350/* *INDENT-ON* */
1351
1352/**
1353 * Disables and reset interface structure.
1354 * It can then be either init again, or removed from used interfaces.
1355 */
1356static void
1357vhost_user_term_if (vhost_user_intf_t * vui)
1358{
1359 int q;
1360 vhost_user_main_t *vum = &vhost_user_main;
1361
1362 // disconnect interface sockets
1363 vhost_user_if_disconnect (vui);
Steven Luong4208a4c2019-05-06 08:51:56 -07001364 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001365 vhost_user_update_iface_state (vui);
1366
Steven Luong2c1084a2020-12-10 20:44:22 -08001367 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001368 {
Steven Luong2c1084a2020-12-10 20:44:22 -08001369 clib_spinlock_free (&vui->vrings[q].vring_lock);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001370 }
1371
1372 if (vui->unix_server_index != ~0)
1373 {
1374 //Close server socket
1375 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1376 vui->unix_server_index);
1377 clib_file_del (&file_main, uf);
1378 vui->unix_server_index = ~0;
1379 unlink (vui->sock_filename);
1380 }
1381
1382 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1383 &vui->if_index);
1384}
1385
1386int
1387vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1388{
1389 vhost_user_main_t *vum = &vhost_user_main;
1390 vhost_user_intf_t *vui;
1391 int rv = 0;
1392 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001393 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001394
Dave Barach3940de32019-07-23 16:28:36 -04001395 if (!
1396 (hwif =
1397 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1398 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001399 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1400
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001401 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1402
Jerome Tollet2f54c272018-10-02 11:41:11 +02001403 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1404 hwif->name, hwif->dev_instance);
1405
Steven Luong2c1084a2020-12-10 20:44:22 -08001406 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08001407 {
1408 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001409
Steven Luong67f935e2019-02-01 10:23:56 -08001410 if (txvq->qid == -1)
1411 continue;
1412 if ((vum->ifq_count > 0) &&
Damjan Marioneabd4242020-10-07 20:59:07 +02001413 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1414 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
Steven Luong67f935e2019-02-01 10:23:56 -08001415 {
1416 vum->ifq_count--;
1417 // Stop the timer if there is no more interrupt interface/queue
1418 if ((vum->ifq_count == 0) &&
1419 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1420 {
1421 vlib_process_signal_event (vm,
1422 vhost_user_send_interrupt_node.index,
1423 VHOST_USER_EVENT_STOP_TIMER, 0);
1424 break;
1425 }
1426 }
1427 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001428
1429 // Disable and reset interface
1430 vhost_user_term_if (vui);
1431
1432 // Reset renumbered iface
1433 if (hwif->dev_instance <
1434 vec_len (vum->show_dev_instance_by_real_dev_instance))
1435 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1436
1437 // Delete ethernet interface
1438 ethernet_delete_interface (vnm, vui->hw_if_index);
1439
Steven Luong2c1084a2020-12-10 20:44:22 -08001440 // free vrings
1441 vec_free (vui->vrings);
1442
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001443 // Back to pool
1444 pool_put (vum->vhost_user_interfaces, vui);
1445
1446 return rv;
1447}
1448
1449static clib_error_t *
1450vhost_user_exit (vlib_main_t * vm)
1451{
1452 vnet_main_t *vnm = vnet_get_main ();
1453 vhost_user_main_t *vum = &vhost_user_main;
1454 vhost_user_intf_t *vui;
1455
1456 vlib_worker_thread_barrier_sync (vlib_get_main ());
1457 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001458 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001459 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001460 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001461 /* *INDENT-ON* */
1462 vlib_worker_thread_barrier_release (vlib_get_main ());
1463 return 0;
1464}
1465
1466VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1467
1468/**
1469 * Open server unix socket on specified sock_filename.
1470 */
1471static int
1472vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1473{
1474 int rv = 0;
1475 struct sockaddr_un un = { };
1476 int fd;
1477 /* create listening socket */
1478 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1479 return VNET_API_ERROR_SYSCALL_ERROR_1;
1480
1481 un.sun_family = AF_UNIX;
1482 strncpy ((char *) un.sun_path, (char *) sock_filename,
1483 sizeof (un.sun_path) - 1);
1484
1485 /* remove if exists */
1486 unlink ((char *) sock_filename);
1487
1488 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1489 {
1490 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1491 goto error;
1492 }
1493
1494 if (listen (fd, 1) == -1)
1495 {
1496 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1497 goto error;
1498 }
1499
1500 *sock_fd = fd;
1501 return 0;
1502
1503error:
1504 close (fd);
1505 return rv;
1506}
1507
1508/**
1509 * Create ethernet interface for vhost user interface.
1510 */
1511static void
Steven Luongd6361c72021-01-26 23:44:19 -08001512vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1513 vhost_user_intf_t *vui,
1514 vhost_user_create_if_args_t *args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001515{
1516 vhost_user_main_t *vum = &vhost_user_main;
1517 u8 hwaddr[6];
1518 clib_error_t *error;
1519
1520 /* create hw and sw interface */
Steven Luongd6361c72021-01-26 23:44:19 -08001521 if (args->use_custom_mac)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001522 {
Steven Luongd6361c72021-01-26 23:44:19 -08001523 clib_memcpy (hwaddr, args->hwaddr, 6);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001524 }
1525 else
1526 {
1527 random_u32 (&vum->random);
1528 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1529 hwaddr[0] = 2;
1530 hwaddr[1] = 0xfe;
1531 }
1532
1533 error = ethernet_register_interface
1534 (vnm,
1535 vhost_user_device_class.index,
1536 vui - vum->vhost_user_interfaces /* device instance */ ,
1537 hwaddr /* ethernet address */ ,
1538 &vui->hw_if_index, 0 /* flag change */ );
1539
1540 if (error)
1541 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001542}
1543
1544/*
1545 * Initialize vui with specified attributes
1546 */
1547static void
Steven Luong27ba5002020-11-17 13:30:44 -08001548vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1549 int server_sock_fd, vhost_user_create_if_args_t * args,
1550 u32 * sw_if_index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001551{
1552 vnet_sw_interface_t *sw;
1553 int q;
1554 vhost_user_main_t *vum = &vhost_user_main;
1555 vnet_hw_interface_t *hw;
1556
1557 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1558 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1559 if (server_sock_fd != -1)
1560 {
1561 clib_file_t template = { 0 };
1562 template.read_function = vhost_user_socksvr_accept_ready;
1563 template.file_descriptor = server_sock_fd;
1564 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001565 template.description = format (0, "vhost user %d", sw);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001566 vui->unix_server_index = clib_file_add (&file_main, &template);
1567 }
1568 else
1569 {
1570 vui->unix_server_index = ~0;
1571 }
1572
1573 vui->sw_if_index = sw->sw_if_index;
Steven Luong27ba5002020-11-17 13:30:44 -08001574 strncpy (vui->sock_filename, args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001575 ARRAY_LEN (vui->sock_filename) - 1);
1576 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001577 vui->is_ready = 0;
Steven Luong27ba5002020-11-17 13:30:44 -08001578 vui->feature_mask = args->feature_mask;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001579 vui->clib_file_index = ~0;
1580 vui->log_base_addr = 0;
1581 vui->if_index = vui - vum->vhost_user_interfaces;
Steven Luong27ba5002020-11-17 13:30:44 -08001582 vui->enable_gso = args->enable_gso;
1583 vui->enable_event_idx = args->enable_event_idx;
1584 vui->enable_packed = args->enable_packed;
Steven Luong4208a4c2019-05-06 08:51:56 -07001585 /*
1586 * enable_gso takes precedence over configurable feature mask if there
1587 * is a clash.
1588 * if feature mask disables gso, but enable_gso is configured,
1589 * then gso is enable
1590 * if feature mask enables gso, but enable_gso is not configured,
1591 * then gso is enable
1592 *
1593 * if gso is enable via feature mask, it must enable both host and guest
1594 * gso feature mask, we don't support one sided GSO or partial GSO.
1595 */
1596 if ((vui->enable_gso == 0) &&
Steven Luong27ba5002020-11-17 13:30:44 -08001597 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1598 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
Steven Luong4208a4c2019-05-06 08:51:56 -07001599 vui->enable_gso = 1;
1600 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001601 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1602 &vui->if_index, 0);
1603
Steven Luong2c1084a2020-12-10 20:44:22 -08001604 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1605 CLIB_CACHE_LINE_BYTES);
1606 vui->num_qid = 2;
1607 for (q = 0; q < vec_len (vui->vrings); q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001608 vhost_user_vring_init (vui, q);
1609
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +01001610 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001611 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1612
1613 if (sw_if_index)
1614 *sw_if_index = vui->sw_if_index;
1615
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001616 vec_validate (vui->per_cpu_tx_qid,
1617 vlib_get_thread_main ()->n_vlib_mains - 1);
1618 vhost_user_tx_thread_placement (vui);
1619}
1620
1621int
1622vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001623 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001624{
1625 vhost_user_intf_t *vui = NULL;
1626 u32 sw_if_idx = ~0;
1627 int rv = 0;
1628 int server_sock_fd = -1;
1629 vhost_user_main_t *vum = &vhost_user_main;
1630 uword *if_index;
1631
Steven Luong27ba5002020-11-17 13:30:44 -08001632 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001633 {
1634 return VNET_API_ERROR_INVALID_ARGUMENT;
1635 }
1636
Steven Luong27ba5002020-11-17 13:30:44 -08001637 if_index = mhash_get (&vum->if_index_by_sock_name,
1638 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001639 if (if_index)
1640 {
Steven Luong27ba5002020-11-17 13:30:44 -08001641 vui = &vum->vhost_user_interfaces[*if_index];
1642 args->sw_if_index = vui->sw_if_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001643 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1644 }
1645
Steven Luong27ba5002020-11-17 13:30:44 -08001646 if (args->is_server)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001647 {
1648 if ((rv =
Steven Luong27ba5002020-11-17 13:30:44 -08001649 vhost_user_init_server_sock (args->sock_filename,
1650 &server_sock_fd)) != 0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001651 {
1652 return rv;
1653 }
1654 }
1655
Steven Luong67f935e2019-02-01 10:23:56 -08001656 /* Protect the uninitialized vui from being dispatched by rx/tx */
1657 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001658 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Steven Luongd6361c72021-01-26 23:44:19 -08001659 vhost_user_create_ethernet (vnm, vm, vui, args);
Steven Luong67f935e2019-02-01 10:23:56 -08001660 vlib_worker_thread_barrier_release (vm);
1661
Steven Luong27ba5002020-11-17 13:30:44 -08001662 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001663 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001664 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001665
Steven Luong27ba5002020-11-17 13:30:44 -08001666 if (args->renumber)
1667 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001668
Steven Luong27ba5002020-11-17 13:30:44 -08001669 args->sw_if_index = sw_if_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001670
1671 // Process node must connect
1672 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1673
1674 return rv;
1675}
1676
1677int
1678vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001679 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001680{
1681 vhost_user_main_t *vum = &vhost_user_main;
1682 vhost_user_intf_t *vui = NULL;
1683 u32 sw_if_idx = ~0;
1684 int server_sock_fd = -1;
1685 int rv = 0;
1686 vnet_hw_interface_t *hwif;
1687 uword *if_index;
1688
Steven Luong27ba5002020-11-17 13:30:44 -08001689 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1690 args->sw_if_index))
Dave Barach3940de32019-07-23 16:28:36 -04001691 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001692 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1693
Steven Luong27ba5002020-11-17 13:30:44 -08001694 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001695 return VNET_API_ERROR_INVALID_ARGUMENT;
1696
1697 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1698
1699 /*
1700 * Disallow changing the interface to have the same path name
1701 * as other interface
1702 */
Steven Luong27ba5002020-11-17 13:30:44 -08001703 if_index = mhash_get (&vum->if_index_by_sock_name,
1704 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001705 if (if_index && (*if_index != vui->if_index))
1706 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1707
1708 // First try to open server socket
Steven Luong27ba5002020-11-17 13:30:44 -08001709 if (args->is_server)
1710 if ((rv = vhost_user_init_server_sock (args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001711 &server_sock_fd)) != 0)
1712 return rv;
1713
1714 vhost_user_term_if (vui);
Steven Luong27ba5002020-11-17 13:30:44 -08001715 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001716
Steven Luong27ba5002020-11-17 13:30:44 -08001717 if (args->renumber)
1718 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001719
1720 // Process node must connect
1721 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1722
1723 return rv;
1724}
1725
1726clib_error_t *
1727vhost_user_connect_command_fn (vlib_main_t * vm,
1728 unformat_input_t * input,
1729 vlib_cli_command_t * cmd)
1730{
Steven Luong27ba5002020-11-17 13:30:44 -08001731 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001732 unformat_input_t _line_input, *line_input = &_line_input;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001733 clib_error_t *error = NULL;
Steven Luong27ba5002020-11-17 13:30:44 -08001734 vhost_user_create_if_args_t args = { 0 };
1735 int rv;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001736
1737 /* Get a line of input. */
1738 if (!unformat_user (input, unformat_line_input, line_input))
1739 return 0;
1740
Steven Luong27ba5002020-11-17 13:30:44 -08001741 args.feature_mask = (u64) ~ (0ULL);
1742 args.custom_dev_instance = ~0;
Steven Luong4208a4c2019-05-06 08:51:56 -07001743 /* GSO feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001744 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001745 /* packed-ring feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001746 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1747 /* event_idx feature is disable by default */
1748 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1749
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001750 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1751 {
Steven Luong27ba5002020-11-17 13:30:44 -08001752 if (unformat (line_input, "socket %s", &args.sock_filename))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001753 ;
1754 else if (unformat (line_input, "server"))
Steven Luong27ba5002020-11-17 13:30:44 -08001755 args.is_server = 1;
Steven Luong4208a4c2019-05-06 08:51:56 -07001756 else if (unformat (line_input, "gso"))
Steven Luong27ba5002020-11-17 13:30:44 -08001757 args.enable_gso = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001758 else if (unformat (line_input, "packed"))
Steven Luong27ba5002020-11-17 13:30:44 -08001759 args.enable_packed = 1;
1760 else if (unformat (line_input, "event-idx"))
1761 args.enable_event_idx = 1;
1762 else if (unformat (line_input, "feature-mask 0x%llx",
1763 &args.feature_mask))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001764 ;
Steven Luong27ba5002020-11-17 13:30:44 -08001765 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1766 args.hwaddr))
Steven Luongd6361c72021-01-26 23:44:19 -08001767 args.use_custom_mac = 1;
Steven Luong27ba5002020-11-17 13:30:44 -08001768 else if (unformat (line_input, "renumber %d",
1769 &args.custom_dev_instance))
1770 args.renumber = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001771 else
1772 {
1773 error = clib_error_return (0, "unknown input `%U'",
1774 format_unformat_error, line_input);
1775 goto done;
1776 }
1777 }
1778
Steven Luong27ba5002020-11-17 13:30:44 -08001779 if ((rv = vhost_user_create_if (vnm, vm, &args)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001780 {
1781 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1782 goto done;
1783 }
1784
Steven Luong27ba5002020-11-17 13:30:44 -08001785 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1786 args.sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001787
1788done:
Steven Luong27ba5002020-11-17 13:30:44 -08001789 vec_free (args.sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001790 unformat_free (line_input);
1791
1792 return error;
1793}
1794
1795clib_error_t *
1796vhost_user_delete_command_fn (vlib_main_t * vm,
1797 unformat_input_t * input,
1798 vlib_cli_command_t * cmd)
1799{
1800 unformat_input_t _line_input, *line_input = &_line_input;
1801 u32 sw_if_index = ~0;
1802 vnet_main_t *vnm = vnet_get_main ();
1803 clib_error_t *error = NULL;
1804
1805 /* Get a line of input. */
1806 if (!unformat_user (input, unformat_line_input, line_input))
1807 return 0;
1808
1809 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1810 {
1811 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1812 ;
1813 else if (unformat
1814 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1815 &sw_if_index))
1816 {
1817 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001818 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001819 if (hwif == NULL ||
1820 vhost_user_device_class.index != hwif->dev_class_index)
1821 {
1822 error = clib_error_return (0, "Not a vhost interface");
1823 goto done;
1824 }
1825 }
1826 else
1827 {
1828 error = clib_error_return (0, "unknown input `%U'",
1829 format_unformat_error, line_input);
1830 goto done;
1831 }
1832 }
1833
1834 vhost_user_delete_if (vnm, vm, sw_if_index);
1835
1836done:
1837 unformat_free (line_input);
1838
1839 return error;
1840}
1841
1842int
1843vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1844 vhost_user_intf_details_t ** out_vuids)
1845{
1846 int rv = 0;
1847 vhost_user_main_t *vum = &vhost_user_main;
1848 vhost_user_intf_t *vui;
1849 vhost_user_intf_details_t *r_vuids = NULL;
1850 vhost_user_intf_details_t *vuid = NULL;
1851 u32 *hw_if_indices = 0;
1852 vnet_hw_interface_t *hi;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001853 int i;
1854
1855 if (!out_vuids)
1856 return -1;
1857
Damjan Marionb2c31b62020-12-13 21:47:40 +01001858 pool_foreach (vui, vum->vhost_user_interfaces)
1859 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001860
1861 for (i = 0; i < vec_len (hw_if_indices); i++)
1862 {
1863 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1864 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1865
1866 vec_add2 (r_vuids, vuid, 1);
1867 vuid->sw_if_index = vui->sw_if_index;
1868 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1869 vuid->features = vui->features;
1870 vuid->num_regions = vui->nregions;
1871 vuid->is_server = vui->unix_server_index != ~0;
1872 vuid->sock_errno = vui->sock_errno;
Benoît Ganne3a76dc32020-04-24 10:33:40 +02001873 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1874 "%s", vui->sock_filename);
1875 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1876 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1877 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001878 }
1879
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001880 vec_free (hw_if_indices);
1881
1882 *out_vuids = r_vuids;
1883
1884 return rv;
1885}
1886
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001887static u8 *
1888format_vhost_user_desc (u8 * s, va_list * args)
1889{
1890 char *fmt = va_arg (*args, char *);
1891 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1892 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1893 int idx = va_arg (*args, int);
1894 u32 *mem_hint = va_arg (*args, u32 *);
1895
1896 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1897 desc_table[idx].flags, desc_table[idx].next,
1898 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1899 mem_hint)));
1900 return s;
1901}
1902
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001903static void
Steven Luong27ba5002020-11-17 13:30:44 -08001904vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001905{
Steven Luong27ba5002020-11-17 13:30:44 -08001906 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1907 int callfd = UNIX_GET_FD (vq->callfd_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001908
1909 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
Steven Luong27ba5002020-11-17 13:30:44 -08001910 vq->errfd);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001911}
1912
1913static void
1914vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1915 int show_descr, int show_verbose)
1916{
1917 int j;
1918 u32 mem_hint = 0;
1919 u32 idx;
1920 u32 n_entries;
1921 vring_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08001922 vhost_user_vring_t *vq = &vui->vrings[q];
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001923
Steven Luong27ba5002020-11-17 13:30:44 -08001924 if (vq->avail && vq->used)
1925 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1926 "used event idx %u used.idx %d\n", vq->avail->flags,
1927 vhost_user_avail_event_idx (vq), vq->avail->idx,
1928 vhost_user_used_event_idx (vq), vq->used->idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001929
Steven Luong27ba5002020-11-17 13:30:44 -08001930 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001931
1932 if (show_descr)
1933 {
1934 vlib_cli_output (vm, "\n descriptor table:\n");
1935 vlib_cli_output (vm,
1936 " slot addr len flags next "
1937 "user_addr\n");
1938 vlib_cli_output (vm,
1939 " ===== ================== ===== ====== ===== "
1940 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08001941 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001942 {
Steven Luong27ba5002020-11-17 13:30:44 -08001943 desc_table = vq->desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001944 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1945 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1946 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001947 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001948 {
1949 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1950 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1951 if (desc_table)
1952 {
1953 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1954 {
1955 vlib_cli_output
1956 (vm, "%U", format_vhost_user_desc,
1957 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1958 desc_table, idx, &mem_hint);
1959 }
1960 if (n_entries >= 20)
1961 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1962 n_entries);
1963 }
1964 }
1965 }
1966 }
1967}
1968
1969static u8 *
1970format_vhost_user_packed_desc (u8 * s, va_list * args)
1971{
1972 char *fmt = va_arg (*args, char *);
1973 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1974 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1975 int idx = va_arg (*args, int);
1976 u32 *mem_hint = va_arg (*args, u32 *);
1977
1978 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1979 desc_table[idx].flags, desc_table[idx].id,
1980 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1981 mem_hint)));
1982 return s;
1983}
1984
1985static u8 *
Steven Luong27ba5002020-11-17 13:30:44 -08001986format_vhost_user_event_idx_flags (u8 * s, va_list * args)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001987{
Steven Luong27ba5002020-11-17 13:30:44 -08001988 u32 flags = va_arg (*args, u32);
1989 typedef struct
1990 {
1991 u8 value;
1992 char *str;
1993 } event_idx_flags;
1994 static event_idx_flags event_idx_array[] = {
1995#define _(s,v) { .str = #s, .value = v, },
1996 foreach_virtio_event_idx_flags
1997#undef _
1998 };
1999 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002000
Steven Luong27ba5002020-11-17 13:30:44 -08002001 if (flags < num_entries)
2002 s = format (s, "%s", event_idx_array[flags].str);
2003 else
2004 s = format (s, "%u", flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002005 return s;
2006}
2007
2008static void
2009vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2010 int show_descr, int show_verbose)
2011{
2012 int j;
2013 u32 mem_hint = 0;
2014 u32 idx;
2015 u32 n_entries;
2016 vring_packed_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08002017 vhost_user_vring_t *vq = &vui->vrings[q];
2018 u16 off_wrap, event_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002019
Steven Luong27ba5002020-11-17 13:30:44 -08002020 off_wrap = vq->avail_event->off_wrap;
2021 event_idx = off_wrap & 0x7fff;
2022 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2023 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2024 (u32) vq->avail_event->flags, off_wrap, event_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002025
Steven Luong27ba5002020-11-17 13:30:44 -08002026 off_wrap = vq->used_event->off_wrap;
2027 event_idx = off_wrap & 0x7fff;
2028 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2029 "used event idx %u\n", format_vhost_user_event_idx_flags,
2030 (u32) vq->used_event->flags, off_wrap, event_idx);
2031
2032 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2033 vq->avail_wrap_counter, vq->used_wrap_counter);
2034
2035 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002036
2037 if (show_descr)
2038 {
2039 vlib_cli_output (vm, "\n descriptor table:\n");
2040 vlib_cli_output (vm,
2041 " slot addr len flags id "
2042 "user_addr\n");
2043 vlib_cli_output (vm,
2044 " ===== ================== ===== ====== ===== "
2045 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08002046 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002047 {
Steven Luong27ba5002020-11-17 13:30:44 -08002048 desc_table = vq->packed_desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002049 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2050 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2051 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002052 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002053 {
2054 n_entries = desc_table[j].len >> 4;
2055 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2056 if (desc_table)
2057 {
2058 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2059 {
2060 vlib_cli_output
2061 (vm, "%U", format_vhost_user_packed_desc,
2062 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2063 desc_table, idx, &mem_hint);
2064 }
2065 if (n_entries >= 20)
2066 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2067 n_entries);
2068 }
2069 }
2070 }
2071 }
2072}
2073
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002074clib_error_t *
2075show_vhost_user_command_fn (vlib_main_t * vm,
2076 unformat_input_t * input,
2077 vlib_cli_command_t * cmd)
2078{
2079 clib_error_t *error = 0;
2080 vnet_main_t *vnm = vnet_get_main ();
2081 vhost_user_main_t *vum = &vhost_user_main;
2082 vhost_user_intf_t *vui;
2083 u32 hw_if_index, *hw_if_indices = 0;
2084 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08002085 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002086 u32 ci;
2087 int i, j, q;
2088 int show_descr = 0;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002089 int show_verbose = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002090 struct feat_struct
2091 {
2092 u8 bit;
2093 char *str;
2094 };
2095 struct feat_struct *feat_entry;
2096
2097 static struct feat_struct feat_array[] = {
2098#define _(s,b) { .str = #s, .bit = b, },
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002099 foreach_virtio_net_features
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002100#undef _
2101 {.str = NULL}
2102 };
2103
2104#define foreach_protocol_feature \
2105 _(VHOST_USER_PROTOCOL_F_MQ) \
2106 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2107
2108 static struct feat_struct proto_feat_array[] = {
2109#define _(s) { .str = #s, .bit = s},
2110 foreach_protocol_feature
2111#undef _
2112 {.str = NULL}
2113 };
2114
2115 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2116 {
2117 if (unformat
2118 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2119 {
2120 hi = vnet_get_hw_interface (vnm, hw_if_index);
2121 if (vhost_user_device_class.index != hi->dev_class_index)
2122 {
2123 error = clib_error_return (0, "unknown input `%U'",
2124 format_unformat_error, input);
2125 goto done;
2126 }
2127 vec_add1 (hw_if_indices, hw_if_index);
2128 }
2129 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2130 show_descr = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002131 else if (unformat (input, "verbose"))
2132 show_verbose = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002133 else
2134 {
2135 error = clib_error_return (0, "unknown input `%U'",
2136 format_unformat_error, input);
2137 goto done;
2138 }
2139 }
2140 if (vec_len (hw_if_indices) == 0)
2141 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002142 pool_foreach (vui, vum->vhost_user_interfaces)
2143 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002144 }
2145 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2146 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2147 vum->coalesce_frames, vum->coalesce_time);
Steven Luong4208a4c2019-05-06 08:51:56 -07002148 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002149 vum->ifq_count);
Steven Luong4208a4c2019-05-06 08:51:56 -07002150 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002151
2152 for (i = 0; i < vec_len (hw_if_indices); i++)
2153 {
2154 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2155 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07002156 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2157 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2158 hw_if_indices[i]);
Steven Luong2c1084a2020-12-10 20:44:22 -08002159 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
Steven Luong4208a4c2019-05-06 08:51:56 -07002160 if (vui->enable_gso)
2161 vlib_cli_output (vm, " GSO enable");
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002162 if (vui->enable_packed)
2163 vlib_cli_output (vm, " Packed ring enable");
Steven Luong27ba5002020-11-17 13:30:44 -08002164 if (vui->enable_event_idx)
2165 vlib_cli_output (vm, " Event index enable");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002166
2167 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2168 " features mask (0x%llx): \n"
2169 " features (0x%llx): \n",
2170 vui->virtio_net_hdr_sz, vui->feature_mask,
2171 vui->features);
2172
2173 feat_entry = (struct feat_struct *) &feat_array;
2174 while (feat_entry->str)
2175 {
2176 if (vui->features & (1ULL << feat_entry->bit))
2177 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2178 feat_entry->bit);
2179 feat_entry++;
2180 }
2181
2182 vlib_cli_output (vm, " protocol features (0x%llx)",
2183 vui->protocol_features);
2184 feat_entry = (struct feat_struct *) &proto_feat_array;
2185 while (feat_entry->str)
2186 {
2187 if (vui->protocol_features & (1ULL << feat_entry->bit))
2188 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2189 feat_entry->bit);
2190 feat_entry++;
2191 }
2192
2193 vlib_cli_output (vm, "\n");
2194
2195 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2196 vui->sock_filename,
2197 (vui->unix_server_index != ~0) ? "server" : "client",
2198 strerror (vui->sock_errno));
2199
2200 vlib_cli_output (vm, " rx placement: ");
2201
Steven Luong2c1084a2020-12-10 20:44:22 -08002202 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08002203 {
2204 vnet_main_t *vnm = vnet_get_main ();
2205 uword thread_index;
Steven Luong67f935e2019-02-01 10:23:56 -08002206 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002207
Steven Luong67f935e2019-02-01 10:23:56 -08002208 if (txvq->qid == -1)
2209 continue;
2210 thread_index =
Damjan Marion94100532020-11-06 23:25:57 +01002211 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
2212 vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2213 qid, format_vnet_hw_if_rx_mode, txvq->mode);
Steven Luong67f935e2019-02-01 10:23:56 -08002214 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002215
2216 vlib_cli_output (vm, " tx placement: %s\n",
2217 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2218
2219 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2220 {
2221 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2222 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2223 }
2224
2225 vlib_cli_output (vm, "\n");
2226
2227 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2228
2229 if (vui->nregions)
2230 {
2231 vlib_cli_output (vm,
2232 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2233 vlib_cli_output (vm,
2234 " ====== ===== ================== ================== ================== ================== ==================\n");
2235 }
2236 for (j = 0; j < vui->nregions; j++)
2237 {
2238 vlib_cli_output (vm,
2239 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2240 j, vui->region_mmap_fd[j],
2241 vui->regions[j].guest_phys_addr,
2242 vui->regions[j].memory_size,
2243 vui->regions[j].userspace_addr,
2244 vui->regions[j].mmap_offset,
2245 pointer_to_uword (vui->region_mmap_addr[j]));
2246 }
Steven Luong2c1084a2020-12-10 20:44:22 -08002247 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002248 {
2249 if (!vui->vrings[q].started)
2250 continue;
2251
2252 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2253 (q & 1) ? "RX" : "TX",
2254 vui->vrings[q].enabled ? "" : " disabled");
2255
2256 vlib_cli_output (vm,
Steven Luong27ba5002020-11-17 13:30:44 -08002257 " qsz %d last_avail_idx %d last_used_idx %d"
2258 " last_kick %u\n",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002259 vui->vrings[q].qsz_mask + 1,
2260 vui->vrings[q].last_avail_idx,
Steven Luong27ba5002020-11-17 13:30:44 -08002261 vui->vrings[q].last_used_idx,
2262 vui->vrings[q].last_kick);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002263
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002264 if (vhost_user_is_packed_ring_supported (vui))
2265 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2266 show_verbose);
2267 else
2268 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002269 }
2270 vlib_cli_output (vm, "\n");
2271 }
2272done:
2273 vec_free (hw_if_indices);
2274 return error;
2275}
2276
2277/*
2278 * CLI functions
2279 */
2280
2281/*?
2282 * Create a vHost User interface. Once created, a new virtual interface
2283 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2284 * is the next free index.
2285 *
2286 * There are several parameters associated with a vHost interface:
2287 *
2288 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2289 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2290 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2291 * hypervisor will create the socket if it does not already exist. The VPP code
2292 * is indifferent to the file location. However, if SELinux is enabled, then the
2293 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2294 *
2295 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2296 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2297 * mode, the VM can be reset without tearing down the vHost Interface. In
2298 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2299 * tearing down the vHost Interface.
2300 *
2301 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2302 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2303 * parameter not be used except by experienced users. By default, all supported
2304 * features will be advertised. Otherwise, provide the set of features desired.
2305 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2306 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2307 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2308 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2309 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2310 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2311 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2312 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2313 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2314 *
2315 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2316 * X:X:X:X:X:X unix or X.X.X cisco format.
2317 *
2318 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2319 * in the name to be specified. If instance already exists, name will be used
2320 * anyway and multiple instances will have the same name. Use with caution.
2321 *
2322 * @cliexpar
2323 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2324 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2325 * VirtualEthernet0/0/0
2326 * @cliexend
2327 * Example of how to create a vhost interface with VPP as the server and with just
2328 * multiple queues enabled:
2329 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2330 * VirtualEthernet0/0/1
2331 * @cliexend
2332 * Once the vHost interface is created, enable the interface using:
2333 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2334?*/
2335/* *INDENT-OFF* */
2336VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2337 .path = "create vhost-user",
2338 .short_help = "create vhost-user socket <socket-filename> [server] "
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002339 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
Steven Luong27ba5002020-11-17 13:30:44 -08002340 "[packed] [event-idx]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002341 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002342 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002343};
2344/* *INDENT-ON* */
2345
2346/*?
2347 * Delete a vHost User interface using the interface name or the
2348 * software interface index. Use the '<em>show interface</em>'
2349 * command to determine the software interface index. On deletion,
2350 * the linux socket will not be deleted.
2351 *
2352 * @cliexpar
2353 * Example of how to delete a vhost interface by name:
2354 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2355 * Example of how to delete a vhost interface by software interface index:
2356 * @cliexcmd{delete vhost-user sw_if_index 1}
2357?*/
2358/* *INDENT-OFF* */
2359VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2360 .path = "delete vhost-user",
2361 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2362 .function = vhost_user_delete_command_fn,
2363};
2364
2365/*?
2366 * Display the attributes of a single vHost User interface (provide interface
2367 * name), multiple vHost User interfaces (provide a list of interface names seperated
2368 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2369 * vHost interfaces).
2370 *
2371 * @cliexpar
2372 * @parblock
2373 * Example of how to display a vhost interface:
2374 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2375 * Virtio vhost-user interfaces
2376 * Global:
2377 * coalesce frames 32 time 1e-3
2378 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2379 * virtio_net_hdr_sz 12
2380 * features mask (0xffffffffffffffff):
2381 * features (0x50408000):
2382 * VIRTIO_NET_F_MRG_RXBUF (15)
2383 * VIRTIO_NET_F_MQ (22)
2384 * VIRTIO_F_INDIRECT_DESC (28)
2385 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2386 * protocol features (0x3)
2387 * VHOST_USER_PROTOCOL_F_MQ (0)
2388 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2389 *
2390 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2391 *
2392 * rx placement:
2393 * thread 1 on vring 1
2394 * thread 1 on vring 5
2395 * thread 2 on vring 3
2396 * thread 2 on vring 7
2397 * tx placement: spin-lock
2398 * thread 0 on vring 0
2399 * thread 1 on vring 2
2400 * thread 2 on vring 0
2401 *
2402 * Memory regions (total 2)
2403 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2404 * ====== ===== ================== ================== ================== ================== ==================
2405 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2406 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2407 *
2408 * Virtqueue 0 (TX)
2409 * qsz 256 last_avail_idx 0 last_used_idx 0
2410 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2411 * kickfd 62 callfd 64 errfd -1
2412 *
2413 * Virtqueue 1 (RX)
2414 * qsz 256 last_avail_idx 0 last_used_idx 0
2415 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2416 * kickfd 65 callfd 66 errfd -1
2417 *
2418 * Virtqueue 2 (TX)
2419 * qsz 256 last_avail_idx 0 last_used_idx 0
2420 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2421 * kickfd 63 callfd 70 errfd -1
2422 *
2423 * Virtqueue 3 (RX)
2424 * qsz 256 last_avail_idx 0 last_used_idx 0
2425 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2426 * kickfd 72 callfd 74 errfd -1
2427 *
2428 * Virtqueue 4 (TX disabled)
2429 * qsz 256 last_avail_idx 0 last_used_idx 0
2430 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2431 * kickfd 76 callfd 78 errfd -1
2432 *
2433 * Virtqueue 5 (RX disabled)
2434 * qsz 256 last_avail_idx 0 last_used_idx 0
2435 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2436 * kickfd 80 callfd 82 errfd -1
2437 *
2438 * Virtqueue 6 (TX disabled)
2439 * qsz 256 last_avail_idx 0 last_used_idx 0
2440 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2441 * kickfd 84 callfd 86 errfd -1
2442 *
2443 * Virtqueue 7 (RX disabled)
2444 * qsz 256 last_avail_idx 0 last_used_idx 0
2445 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2446 * kickfd 88 callfd 90 errfd -1
2447 *
2448 * @cliexend
2449 *
2450 * The optional '<em>descriptors</em>' parameter will display the same output as
2451 * the previous example but will include the descriptor table for each queue.
2452 * The output is truncated below:
2453 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2454 * Virtio vhost-user interfaces
2455 * Global:
2456 * coalesce frames 32 time 1e-3
2457 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2458 * virtio_net_hdr_sz 12
2459 * features mask (0xffffffffffffffff):
2460 * features (0x50408000):
2461 * VIRTIO_NET_F_MRG_RXBUF (15)
2462 * VIRTIO_NET_F_MQ (22)
2463 * :
2464 * Virtqueue 0 (TX)
2465 * qsz 256 last_avail_idx 0 last_used_idx 0
2466 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2467 * kickfd 62 callfd 64 errfd -1
2468 *
2469 * descriptor table:
2470 * id addr len flags next user_addr
2471 * ===== ================== ===== ====== ===== ==================
2472 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2473 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2474 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2475 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2476 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2477 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2478 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2479 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2480 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2481 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2482 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2483 * :
2484 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2485 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2486 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2487 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2488 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2489 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2490 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2491 *
2492 * Virtqueue 1 (RX)
2493 * qsz 256 last_avail_idx 0 last_used_idx 0
2494 * :
2495 * @cliexend
2496 * @endparblock
2497?*/
2498/* *INDENT-OFF* */
2499VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2500 .path = "show vhost-user",
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002501 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2502 "[[descriptors] [verbose]]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002503 .function = show_vhost_user_command_fn,
2504};
2505/* *INDENT-ON* */
2506
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002507
2508static clib_error_t *
2509vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2510{
2511 vhost_user_main_t *vum = &vhost_user_main;
2512
2513 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2514 {
2515 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2516 ;
2517 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2518 ;
2519 else if (unformat (input, "dont-dump-memory"))
2520 vum->dont_dump_vhost_user_memory = 1;
2521 else
2522 return clib_error_return (0, "unknown input `%U'",
2523 format_unformat_error, input);
2524 }
2525
2526 return 0;
2527}
2528
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002529/* vhost-user { ... } configuration. */
2530VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2531
2532void
2533vhost_user_unmap_all (void)
2534{
2535 vhost_user_main_t *vum = &vhost_user_main;
2536 vhost_user_intf_t *vui;
2537
2538 if (vum->dont_dump_vhost_user_memory)
2539 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002540 pool_foreach (vui, vum->vhost_user_interfaces)
2541 unmap_all_mem_regions (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002542 }
2543}
2544
2545/*
2546 * fd.io coding-style-patch-verification: ON
2547 *
2548 * Local Variables:
2549 * eval: (c-set-style "gnu")
2550 * End:
2551 */