blob: 5f04db093952cc5b566b886ea9363ab03b908464 [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
214static void
215vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
216{
217 u32 qid;
218 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion94100532020-11-06 23:25:57 +0100219 vhost_user_vring_t *txvq;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200220
221 qid = ifq & 0xff;
222 if ((qid & 1) == 0)
223 /* Only care about the odd number, or TX, virtqueue */
224 return;
225
Damjan Marion94100532020-11-06 23:25:57 +0100226 // qid >> 1 is to convert virtqueue number to vring queue index
227 qid >>= 1;
228 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
229 if (vhost_user_intf_ready (vui) &&
230 ((txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
231 (txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT)))
232 vnet_hw_if_rx_queue_set_int_pending (vnm, txvq->queue_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200233}
234
235static clib_error_t *
236vhost_user_callfd_read_ready (clib_file_t * uf)
237{
238 __attribute__ ((unused)) int n;
239 u8 buff[8];
240
241 n = read (uf->file_descriptor, ((char *) &buff), 8);
242
243 return 0;
244}
245
Steven Luong67f935e2019-02-01 10:23:56 -0800246static_always_inline void
247vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
248{
249 if (qid & 1) // RX is odd, TX is even
250 {
251 if (vui->vrings[qid].qid == -1)
252 vhost_user_rx_thread_placement (vui, qid);
253 }
254 else
255 vhost_user_tx_thread_placement (vui);
256}
257
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200258static clib_error_t *
259vhost_user_kickfd_read_ready (clib_file_t * uf)
260{
261 __attribute__ ((unused)) int n;
262 u8 buff[8];
263 vhost_user_intf_t *vui =
264 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
265 uf->private_data >> 8);
266 u32 qid = uf->private_data & 0xff;
267
268 n = read (uf->file_descriptor, ((char *) &buff), 8);
Steven Luong2c1084a2020-12-10 20:44:22 -0800269 vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200270 if (!vui->vrings[qid].started ||
Juraj Slobodab192feb2018-10-01 12:42:07 +0200271 (vhost_user_intf_ready (vui) != vui->is_ready))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200272 {
Steven Luong67f935e2019-02-01 10:23:56 -0800273 if (vui->vrings[qid].started == 0)
274 {
275 vui->vrings[qid].started = 1;
276 vhost_user_thread_placement (vui, qid);
277 vhost_user_update_iface_state (vui);
278 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200279 }
280
281 vhost_user_set_interrupt_pending (vui, uf->private_data);
282 return 0;
283}
284
285static_always_inline void
286vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
287{
288 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong2c1084a2020-12-10 20:44:22 -0800289
Dave Barachb7b92992018-10-17 10:38:51 -0400290 clib_memset (vring, 0, sizeof (*vring));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200291 vring->kickfd_idx = ~0;
292 vring->callfd_idx = ~0;
293 vring->errfd = -1;
Steven Luong67f935e2019-02-01 10:23:56 -0800294 vring->qid = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200295
Steven Luong2c1084a2020-12-10 20:44:22 -0800296 clib_spinlock_init (&vring->vring_lock);
297
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200298 /*
299 * We have a bug with some qemu 2.5, and this may be a fix.
300 * Feel like interpretation holy text, but this is from vhost-user.txt.
301 * "
302 * One queue pair is enabled initially. More queues are enabled
303 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
304 * "
305 * Don't know who's right, but this is what DPDK does.
306 */
307 if (qid == 0 || qid == 1)
308 vring->enabled = 1;
309}
310
311static_always_inline void
312vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
313{
314 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong67f935e2019-02-01 10:23:56 -0800315
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200316 if (vring->kickfd_idx != ~0)
317 {
318 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
319 vring->kickfd_idx);
320 clib_file_del (&file_main, uf);
321 vring->kickfd_idx = ~0;
322 }
323 if (vring->callfd_idx != ~0)
324 {
325 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
326 vring->callfd_idx);
327 clib_file_del (&file_main, uf);
328 vring->callfd_idx = ~0;
329 }
330 if (vring->errfd != -1)
331 {
332 close (vring->errfd);
333 vring->errfd = -1;
334 }
Steven Luong67f935e2019-02-01 10:23:56 -0800335
Steven Luong2c1084a2020-12-10 20:44:22 -0800336 clib_spinlock_free (&vring->vring_lock);
337
Steven Luong67f935e2019-02-01 10:23:56 -0800338 // save the qid so that we don't need to unassign and assign_rx_thread
339 // when the interface comes back up. They are expensive calls.
340 u16 q = vui->vrings[qid].qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200341 vhost_user_vring_init (vui, qid);
Steven Luong67f935e2019-02-01 10:23:56 -0800342 vui->vrings[qid].qid = q;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200343}
344
345static_always_inline void
346vhost_user_if_disconnect (vhost_user_intf_t * vui)
347{
348 vnet_main_t *vnm = vnet_get_main ();
349 int q;
350
351 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
352
353 if (vui->clib_file_index != ~0)
354 {
355 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
356 vui->clib_file_index = ~0;
357 }
358
Juraj Slobodab192feb2018-10-01 12:42:07 +0200359 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200360
Steven Luong2c1084a2020-12-10 20:44:22 -0800361 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200362 vhost_user_vring_close (vui, q);
363
364 unmap_all_mem_regions (vui);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200365 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200366}
367
368static clib_error_t *
369vhost_user_socket_read (clib_file_t * uf)
370{
Steven Luong67f935e2019-02-01 10:23:56 -0800371 int n, i, j;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200372 int fd, number_of_fds = 0;
373 int fds[VHOST_MEMORY_MAX_NREGIONS];
374 vhost_user_msg_t msg;
375 struct msghdr mh;
376 struct iovec iov[1];
377 vhost_user_main_t *vum = &vhost_user_main;
378 vhost_user_intf_t *vui;
379 struct cmsghdr *cmsg;
380 u8 q;
381 clib_file_t template = { 0 };
382 vnet_main_t *vnm = vnet_get_main ();
Steven Luong67f935e2019-02-01 10:23:56 -0800383 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200384
385 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
386
387 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
388
Dave Barachb7b92992018-10-17 10:38:51 -0400389 clib_memset (&mh, 0, sizeof (mh));
390 clib_memset (control, 0, sizeof (control));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200391
392 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
393 fds[i] = -1;
394
395 /* set the payload */
396 iov[0].iov_base = (void *) &msg;
397 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
398
399 mh.msg_iov = iov;
400 mh.msg_iovlen = 1;
401 mh.msg_control = control;
402 mh.msg_controllen = sizeof (control);
403
404 n = recvmsg (uf->file_descriptor, &mh, 0);
405
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200406 if (n != VHOST_USER_MSG_HDR_SZ)
407 {
408 if (n == -1)
409 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200410 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
411 strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200412 }
413 else
414 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200415 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
416 n, VHOST_USER_MSG_HDR_SZ);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200417 }
418 goto close_socket;
419 }
420
421 if (mh.msg_flags & MSG_CTRUNC)
422 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200423 vu_log_debug (vui, "MSG_CTRUNC is set");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200424 goto close_socket;
425 }
426
427 cmsg = CMSG_FIRSTHDR (&mh);
428
429 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
430 (cmsg->cmsg_type == SCM_RIGHTS) &&
431 (cmsg->cmsg_len - CMSG_LEN (0) <=
432 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
433 {
434 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
Dave Barach178cf492018-11-13 16:34:13 -0500435 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200436 }
437
438 /* version 1, no reply bit set */
439 if ((msg.flags & 7) != 1)
440 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200441 vu_log_debug (vui, "malformed message received. closing socket");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200442 goto close_socket;
443 }
444
445 {
446 int rv;
447 rv =
448 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
449 msg.size);
450 if (rv < 0)
451 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200452 vu_log_debug (vui, "read failed %s", strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200453 goto close_socket;
454 }
455 else if (rv != msg.size)
456 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200457 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
458 msg.size);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200459 goto close_socket;
460 }
461 }
462
463 switch (msg.request)
464 {
465 case VHOST_USER_GET_FEATURES:
466 msg.flags |= 4;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200467 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
468 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
469 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
470 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
471 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
472 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
473 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
474 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
475 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200476 msg.u64 &= vui->feature_mask;
Steven Luong4208a4c2019-05-06 08:51:56 -0700477
Steven Luong27ba5002020-11-17 13:30:44 -0800478 if (vui->enable_event_idx)
479 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
Steven Luong4208a4c2019-05-06 08:51:56 -0700480 if (vui->enable_gso)
481 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700482 if (vui->enable_packed)
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200483 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
Steven Luong4208a4c2019-05-06 08:51:56 -0700484
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200485 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200486 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
487 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800488 n =
489 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
490 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
491 {
492 vu_log_debug (vui, "could not send message response");
493 goto close_socket;
494 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200495 break;
496
497 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200498 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
499 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200500
501 vui->features = msg.u64;
502
503 if (vui->features &
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200504 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
505 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200506 vui->virtio_net_hdr_sz = 12;
507 else
508 vui->virtio_net_hdr_sz = 10;
509
510 vui->is_any_layout =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200511 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200512
513 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Steven Luong4208a4c2019-05-06 08:51:56 -0700514 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
515 if (vui->enable_gso &&
Steven Luonga75ad872019-08-06 21:51:34 -0700516 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
517 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100518 {
519 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
520 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
521 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
522 }
Steven Luong4208a4c2019-05-06 08:51:56 -0700523 else
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100524 {
525 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
526 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
527 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200528 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200529 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700530 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200531 break;
532
533 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200534 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
535 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200536
537 if ((msg.memory.nregions < 1) ||
538 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
539 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200540 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
541 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200542 goto close_socket;
543 }
544
545 if (msg.memory.nregions != number_of_fds)
546 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200547 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200548 goto close_socket;
549 }
Steven Luong67f935e2019-02-01 10:23:56 -0800550
551 /* Do the mmap without barrier sync */
552 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
553 for (i = 0; i < msg.memory.nregions; i++)
554 {
555 long page_sz = get_huge_page_size (fds[i]);
556
557 /* align size to page */
558 ssize_t map_sz = (msg.memory.regions[i].memory_size +
559 msg.memory.regions[i].mmap_offset +
560 page_sz - 1) & ~(page_sz - 1);
561
562 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
563 MAP_SHARED, fds[i], 0);
564 if (region_mmap_addr[i] == MAP_FAILED)
565 {
566 vu_log_err (vui, "failed to map memory. errno is %d", errno);
567 for (j = 0; j < i; j++)
568 munmap (region_mmap_addr[j], map_sz);
569 goto close_socket;
570 }
571 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
572 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
573 region_mmap_addr[i], page_sz);
574 }
575
576 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200577 unmap_all_mem_regions (vui);
578 for (i = 0; i < msg.memory.nregions; i++)
579 {
Dave Barach178cf492018-11-13 16:34:13 -0500580 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
581 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200582
Steven Luong67f935e2019-02-01 10:23:56 -0800583 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200584 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
585 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
586 vui->regions[i].memory_size;
587
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200588 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
589 vui->region_mmap_fd[i] = fds[i];
590
591 vui->nregions++;
592 }
Steven Luong4442f7c2019-10-02 07:33:48 -0700593
594 /*
595 * Re-compute desc, used, and avail descriptor table if vring address
596 * is set.
597 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800598 for (q = 0; q < vui->num_qid; q++)
Steven Luong4442f7c2019-10-02 07:33:48 -0700599 {
600 if (vui->vrings[q].desc_user_addr &&
601 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
602 {
603 vui->vrings[q].desc =
604 map_user_mem (vui, vui->vrings[q].desc_user_addr);
605 vui->vrings[q].used =
606 map_user_mem (vui, vui->vrings[q].used_user_addr);
607 vui->vrings[q].avail =
608 map_user_mem (vui, vui->vrings[q].avail_user_addr);
609 }
610 }
Steven Luong67f935e2019-02-01 10:23:56 -0800611 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200612 break;
613
614 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200615 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
616 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200617
618 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
619 (msg.state.num == 0) || /* it cannot be zero */
Benoît Ganne32526a42020-12-08 19:08:05 +0100620 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800621 (msg.state.index >= vui->num_qid))
622 {
623 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
624 " %d, msg.state.index %d, curruent max q %d",
625 msg.state.num, msg.state.index, vui->num_qid);
626 goto close_socket;
627 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200628 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
629 break;
630
631 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200632 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
633 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200634
Steven Luong2c1084a2020-12-10 20:44:22 -0800635 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200636 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200637 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800638 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200639 goto close_socket;
640 }
641
642 if (msg.size < sizeof (msg.addr))
643 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200644 vu_log_debug (vui, "vhost message is too short (%d < %d)",
645 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200646 goto close_socket;
647 }
648
Steven Luong67f935e2019-02-01 10:23:56 -0800649 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
650 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
651 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200652
Steven Luong67f935e2019-02-01 10:23:56 -0800653 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200654 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200655 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
656 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200657 goto close_socket;
658 }
659
Steven Luong4442f7c2019-10-02 07:33:48 -0700660 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
661 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
662 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
663
Steven Luong67f935e2019-02-01 10:23:56 -0800664 vlib_worker_thread_barrier_sync (vm);
665 vui->vrings[msg.state.index].desc = desc;
666 vui->vrings[msg.state.index].used = used;
667 vui->vrings[msg.state.index].avail = avail;
668
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200669 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
670 vui->vrings[msg.state.index].log_used =
671 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
672
673 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
674 the ring is initialized in an enabled state. */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200675 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800676 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200677
678 vui->vrings[msg.state.index].last_used_idx =
679 vui->vrings[msg.state.index].last_avail_idx =
680 vui->vrings[msg.state.index].used->idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800681 vui->vrings[msg.state.index].last_kick =
682 vui->vrings[msg.state.index].last_used_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200683
684 /* tell driver that we don't want interrupts */
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700685 if (vhost_user_is_packed_ring_supported (vui))
686 vui->vrings[msg.state.index].used_event->flags =
687 VRING_EVENT_F_DISABLE;
688 else
689 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Steven Luong67f935e2019-02-01 10:23:56 -0800690 vlib_worker_thread_barrier_release (vm);
691 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200692 break;
693
694 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200695 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200696 break;
697
698 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200699 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
700 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200701 break;
702
703 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200704 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
705 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200706
707 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800708 if (vui->num_qid > q)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200709 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800710 /* if there is old fd, delete and close it */
711 if (vui->vrings[q].callfd_idx != ~0)
712 {
713 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
714 vui->vrings[q].callfd_idx);
715 clib_file_del (&file_main, uf);
716 vui->vrings[q].callfd_idx = ~0;
717 }
718 }
719 else if (vec_len (vui->vrings) > q)
720 {
721 /* grow vrings by pair (RX + TX) */
722 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
723 }
724 else
725 {
726 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
727
728 /*
729 * Double the array size if it is less than 64 entries.
730 * Slow down thereafter.
731 */
732 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
733 new_max_q = vec_len (vui->vrings) << 1;
734 else
735 new_max_q = vec_len (vui->vrings) +
736 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
737 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
738 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
739
740 /* sync with the worker threads, vrings may move due to realloc */
741 vlib_worker_thread_barrier_sync (vm);
742 vec_validate_aligned (vui->vrings, new_max_q - 1,
743 CLIB_CACHE_LINE_BYTES);
744 vlib_worker_thread_barrier_release (vm);
745
746 for (i = old_max_q; i < vec_len (vui->vrings); i++)
747 vhost_user_vring_init (vui, i);
748
749 /* grow vrings by pair (RX + TX) */
750 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200751 }
752
753 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
754 {
755 if (number_of_fds != 1)
756 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200757 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200758 goto close_socket;
759 }
760
761 template.read_function = vhost_user_callfd_read_ready;
762 template.file_descriptor = fds[0];
763 template.private_data =
764 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500765 template.description = format (0, "vhost user");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200766 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
767 }
768 else
769 vui->vrings[q].callfd_idx = ~0;
770 break;
771
772 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200773 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
774 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200775
776 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800777 if (q >= vui->num_qid)
778 {
779 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
780 " %u >= %u", q, vui->num_qid);
781 goto close_socket;
782 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200783
784 if (vui->vrings[q].kickfd_idx != ~0)
785 {
786 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
787 vui->vrings[q].kickfd_idx);
788 clib_file_del (&file_main, uf);
789 vui->vrings[q].kickfd_idx = ~0;
790 }
791
792 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
793 {
794 if (number_of_fds != 1)
795 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200796 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200797 goto close_socket;
798 }
799
800 template.read_function = vhost_user_kickfd_read_ready;
801 template.file_descriptor = fds[0];
802 template.private_data =
803 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
804 q;
805 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
806 }
807 else
808 {
809 //When no kickfd is set, the queue is initialized as started
810 vui->vrings[q].kickfd_idx = ~0;
811 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800812 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200813 }
Steven Luong67f935e2019-02-01 10:23:56 -0800814 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200815 break;
816
817 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200818 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
819 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200820
821 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800822 if (q >= vui->num_qid)
823 {
824 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
825 " %u >= %u", q, vui->num_qid);
826 goto close_socket;
827 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200828
829 if (vui->vrings[q].errfd != -1)
830 close (vui->vrings[q].errfd);
831
832 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
833 {
834 if (number_of_fds != 1)
835 goto close_socket;
836
837 vui->vrings[q].errfd = fds[0];
838 }
839 else
840 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200841 break;
842
843 case VHOST_USER_SET_VRING_BASE:
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700844 vu_log_debug (vui,
845 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200846 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong2c1084a2020-12-10 20:44:22 -0800847 if (msg.state.index >= vui->num_qid)
848 {
849 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
850 " %u >= %u", msg.state.index, vui->num_qid);
851 goto close_socket;
852 }
Steven Luong67f935e2019-02-01 10:23:56 -0800853 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200854 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700855 if (vhost_user_is_packed_ring_supported (vui))
856 {
857 /*
858 * 0 1 2 3
859 * 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
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * | last avail idx | | last used idx | |
862 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
863 * ^ ^
864 * | |
865 * avail wrap counter used wrap counter
866 */
867 /* last avail idx at bit 0-14. */
868 vui->vrings[msg.state.index].last_avail_idx =
869 msg.state.num & 0x7fff;
870 /* avail wrap counter at bit 15 */
871 vui->vrings[msg.state.index].avail_wrap_counter =
872 ! !(msg.state.num & (1 << 15));
873
874 /*
875 * Although last_used_idx is passed in the upper 16 bits in qemu
876 * implementation, in practice, last_avail_idx and last_used_idx are
877 * usually the same. As a result, DPDK does not bother to pass us
878 * last_used_idx. The spec is not clear on thex coding. I figured it
879 * out by reading the qemu code. So let's just read last_avail_idx
880 * and set last_used_idx equals to last_avail_idx.
881 */
882 vui->vrings[msg.state.index].last_used_idx =
883 vui->vrings[msg.state.index].last_avail_idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800884 vui->vrings[msg.state.index].last_kick =
885 vui->vrings[msg.state.index].last_used_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700886 vui->vrings[msg.state.index].used_wrap_counter =
887 vui->vrings[msg.state.index].avail_wrap_counter;
888
889 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
890 vui->vrings[msg.state.index].avail_wrap_counter =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200891 VRING_DESC_F_AVAIL;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700892 }
Steven Luong67f935e2019-02-01 10:23:56 -0800893 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200894 break;
895
896 case VHOST_USER_GET_VRING_BASE:
Steven Luong2c1084a2020-12-10 20:44:22 -0800897 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200898 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200899 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800900 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200901 goto close_socket;
902 }
903
Steven Luong67f935e2019-02-01 10:23:56 -0800904 /* protection is needed to prevent rx/tx from changing last_avail_idx */
905 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200906 /*
907 * Copy last_avail_idx from the vring before closing it because
908 * closing the vring also initializes the vring last_avail_idx
909 */
910 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700911 if (vhost_user_is_packed_ring_supported (vui))
912 {
913 msg.state.num =
914 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
915 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
916 msg.state.num |=
917 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
918 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
919 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200920 msg.flags |= 4;
921 msg.size = sizeof (msg.state);
922
Steven Luong67f935e2019-02-01 10:23:56 -0800923 /*
924 * Spec says: Client must [...] stop ring upon receiving
925 * VHOST_USER_GET_VRING_BASE
926 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200927 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800928 vlib_worker_thread_barrier_release (vm);
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700929 vu_log_debug (vui,
930 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200931 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800932 n =
933 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
934 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
935 {
936 vu_log_debug (vui, "could not send message response");
937 goto close_socket;
938 }
939 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200940 break;
941
942 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200943 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200944 break;
945
946 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800947 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
948 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200949
Steven Luong67f935e2019-02-01 10:23:56 -0800950 if (msg.size != sizeof (msg.log))
951 {
952 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
953 " %d instead of %d", msg.size, sizeof (msg.log));
954 goto close_socket;
955 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200956
Steven Luong67f935e2019-02-01 10:23:56 -0800957 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
958 {
959 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
960 "VHOST_USER_SET_LOG_BASE received");
961 goto close_socket;
962 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200963
Steven Luong67f935e2019-02-01 10:23:56 -0800964 fd = fds[0];
965 /* align size to page */
966 long page_sz = get_huge_page_size (fd);
967 ssize_t map_sz =
968 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200969
Steven Luong67f935e2019-02-01 10:23:56 -0800970 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
971 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200972
Steven Luong67f935e2019-02-01 10:23:56 -0800973 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
974 "mapped 0x%lx", map_sz, msg.log.offset, fd,
975 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200976
Steven Luong67f935e2019-02-01 10:23:56 -0800977 if (log_base_addr == MAP_FAILED)
978 {
979 vu_log_err (vui, "failed to map memory. errno is %d", errno);
980 goto close_socket;
981 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200982
Steven Luong67f935e2019-02-01 10:23:56 -0800983 vlib_worker_thread_barrier_sync (vm);
984 vui->log_base_addr = log_base_addr;
985 vui->log_base_addr += msg.log.offset;
986 vui->log_size = msg.log.size;
987 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200988
Steven Luong67f935e2019-02-01 10:23:56 -0800989 msg.flags |= 4;
990 msg.size = sizeof (msg.u64);
991 n =
992 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
993 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
994 {
995 vu_log_debug (vui, "could not send message response");
996 goto close_socket;
997 }
998 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200999
1000 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001001 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001002 break;
1003
1004 case VHOST_USER_GET_PROTOCOL_FEATURES:
1005 msg.flags |= 4;
1006 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1007 (1 << VHOST_USER_PROTOCOL_F_MQ);
1008 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001009 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1010 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001011 n =
1012 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1013 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1014 {
1015 vu_log_debug (vui, "could not send message response");
1016 goto close_socket;
1017 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001018 break;
1019
1020 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001021 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1022 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001023 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001024 break;
1025
1026 case VHOST_USER_GET_QUEUE_NUM:
1027 msg.flags |= 4;
Steven Luong2c1084a2020-12-10 20:44:22 -08001028 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001029 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001030 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1031 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001032 n =
1033 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1034 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1035 {
1036 vu_log_debug (vui, "could not send message response");
1037 goto close_socket;
1038 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001039 break;
1040
1041 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001042 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1043 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1044 msg.state.index);
Steven Luong2c1084a2020-12-10 20:44:22 -08001045 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001046 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001047 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
Steven Luong2c1084a2020-12-10 20:44:22 -08001048 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001049 goto close_socket;
1050 }
1051
1052 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -08001053 vhost_user_thread_placement (vui, msg.state.index);
1054 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001055 break;
1056
1057 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001058 vu_log_debug (vui, "unknown vhost-user message %d received. "
1059 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001060 goto close_socket;
1061 }
1062
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001063 return 0;
1064
1065close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -08001066 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001067 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -08001068 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001069 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001070 return 0;
1071}
1072
1073static clib_error_t *
1074vhost_user_socket_error (clib_file_t * uf)
1075{
1076 vlib_main_t *vm = vlib_get_main ();
1077 vhost_user_main_t *vum = &vhost_user_main;
1078 vhost_user_intf_t *vui =
1079 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1080
Jerome Tollet2f54c272018-10-02 11:41:11 +02001081 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001082 vlib_worker_thread_barrier_sync (vm);
1083 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001084 vlib_worker_thread_barrier_release (vm);
1085 return 0;
1086}
1087
1088static clib_error_t *
1089vhost_user_socksvr_accept_ready (clib_file_t * uf)
1090{
1091 int client_fd, client_len;
1092 struct sockaddr_un client;
1093 clib_file_t template = { 0 };
1094 vhost_user_main_t *vum = &vhost_user_main;
1095 vhost_user_intf_t *vui;
1096
1097 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1098
1099 client_len = sizeof (client);
1100 client_fd = accept (uf->file_descriptor,
1101 (struct sockaddr *) &client,
1102 (socklen_t *) & client_len);
1103
1104 if (client_fd < 0)
1105 return clib_error_return_unix (0, "accept");
1106
1107 if (vui->clib_file_index != ~0)
1108 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001109 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1110 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001111 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1112 }
1113
Jerome Tollet2f54c272018-10-02 11:41:11 +02001114 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1115 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001116 template.read_function = vhost_user_socket_read;
1117 template.error_function = vhost_user_socket_error;
1118 template.file_descriptor = client_fd;
1119 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001120 template.description = format (0, "vhost interface %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001121 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001122 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001123 return 0;
1124}
1125
1126static clib_error_t *
1127vhost_user_init (vlib_main_t * vm)
1128{
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001129 vhost_user_main_t *vum = &vhost_user_main;
1130 vlib_thread_main_t *tm = vlib_get_thread_main ();
1131
Jerome Tollet2f54c272018-10-02 11:41:11 +02001132 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1133
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001134 vum->coalesce_frames = 32;
1135 vum->coalesce_time = 1e-3;
1136
1137 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1138
1139 vhost_cpu_t *cpu;
1140 vec_foreach (cpu, vum->cpus)
1141 {
1142 /* This is actually not necessary as validate already zeroes it
1143 * Just keeping the loop here for later because I am lazy. */
1144 cpu->rx_buffers_len = 0;
1145 }
1146
1147 vum->random = random_default_seed ();
1148
1149 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1150
1151 return 0;
1152}
1153
Dave Barachf8d50682019-05-14 18:01:44 -04001154/* *INDENT-OFF* */
1155VLIB_INIT_FUNCTION (vhost_user_init) =
1156{
1157 .runs_after = VLIB_INITS("ip4_init"),
1158};
1159/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001160
1161static uword
1162vhost_user_send_interrupt_process (vlib_main_t * vm,
1163 vlib_node_runtime_t * rt, vlib_frame_t * f)
1164{
1165 vhost_user_intf_t *vui;
1166 f64 timeout = 3153600000.0 /* 100 years */ ;
1167 uword event_type, *event_data = 0;
1168 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -08001169 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001170 f64 now, poll_time_remaining;
1171 f64 next_timeout;
1172 u8 stop_timer = 0;
1173
1174 while (1)
1175 {
1176 poll_time_remaining =
1177 vlib_process_wait_for_event_or_clock (vm, timeout);
1178 event_type = vlib_process_get_events (vm, &event_data);
1179 vec_reset_length (event_data);
1180
1181 /*
1182 * Use the remaining timeout if it is less than coalesce time to avoid
1183 * resetting the existing timer in the middle of expiration
1184 */
1185 timeout = poll_time_remaining;
1186 if (vlib_process_suspend_time_is_zero (timeout) ||
1187 (timeout > vum->coalesce_time))
1188 timeout = vum->coalesce_time;
1189
1190 now = vlib_time_now (vm);
1191 switch (event_type)
1192 {
1193 case VHOST_USER_EVENT_STOP_TIMER:
1194 stop_timer = 1;
1195 break;
1196
1197 case VHOST_USER_EVENT_START_TIMER:
1198 stop_timer = 0;
1199 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1200 break;
1201 /* fall through */
1202
1203 case ~0:
1204 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001205 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001206 next_timeout = timeout;
Steven Luong2c1084a2020-12-10 20:44:22 -08001207 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001208 {
Steven Luong67f935e2019-02-01 10:23:56 -08001209 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1210 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001211
Steven Luong67f935e2019-02-01 10:23:56 -08001212 if (txvq->qid == -1)
1213 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001214 if (txvq->n_since_last_int)
1215 {
1216 if (now >= txvq->int_deadline)
Steven Luong27ba5002020-11-17 13:30:44 -08001217 vhost_user_send_call (vm, vui, txvq);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001218 else
1219 next_timeout = txvq->int_deadline - now;
1220 }
1221
1222 if (rxvq->n_since_last_int)
1223 {
1224 if (now >= rxvq->int_deadline)
Steven Luong27ba5002020-11-17 13:30:44 -08001225 vhost_user_send_call (vm, vui, rxvq);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001226 else
1227 next_timeout = rxvq->int_deadline - now;
1228 }
1229
1230 if ((next_timeout < timeout) && (next_timeout > 0.0))
1231 timeout = next_timeout;
1232 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001233 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001234 /* *INDENT-ON* */
1235 break;
1236
1237 default:
1238 clib_warning ("BUG: unhandled event type %d", event_type);
1239 break;
1240 }
1241 /* No less than 1 millisecond */
1242 if (timeout < 1e-3)
1243 timeout = 1e-3;
1244 if (stop_timer)
1245 timeout = 3153600000.0;
1246 }
1247 return 0;
1248}
1249
1250/* *INDENT-OFF* */
1251VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1252 .function = vhost_user_send_interrupt_process,
1253 .type = VLIB_NODE_TYPE_PROCESS,
1254 .name = "vhost-user-send-interrupt-process",
1255};
1256/* *INDENT-ON* */
1257
1258static uword
1259vhost_user_process (vlib_main_t * vm,
1260 vlib_node_runtime_t * rt, vlib_frame_t * f)
1261{
1262 vhost_user_main_t *vum = &vhost_user_main;
1263 vhost_user_intf_t *vui;
1264 struct sockaddr_un sun;
1265 int sockfd;
1266 clib_file_t template = { 0 };
1267 f64 timeout = 3153600000.0 /* 100 years */ ;
1268 uword *event_data = 0;
1269
1270 sockfd = -1;
1271 sun.sun_family = AF_UNIX;
1272 template.read_function = vhost_user_socket_read;
1273 template.error_function = vhost_user_socket_error;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001274 template.description = format (0, "vhost user process");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001275
1276 while (1)
1277 {
1278 vlib_process_wait_for_event_or_clock (vm, timeout);
1279 vlib_process_get_events (vm, &event_data);
1280 vec_reset_length (event_data);
1281
1282 timeout = 3.0;
1283
1284 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001285 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001286
1287 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1288 if (vui->clib_file_index == ~0)
1289 {
1290 if ((sockfd < 0) &&
1291 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1292 {
1293 /*
1294 * 1st time error or new error for this interface,
1295 * spit out the message and record the error
1296 */
1297 if (!vui->sock_errno || (vui->sock_errno != errno))
1298 {
1299 clib_unix_warning
1300 ("Error: Could not open unix socket for %s",
1301 vui->sock_filename);
1302 vui->sock_errno = errno;
1303 }
1304 continue;
1305 }
1306
1307 /* try to connect */
1308 strncpy (sun.sun_path, (char *) vui->sock_filename,
1309 sizeof (sun.sun_path) - 1);
Damjan Marion62c25ab2020-12-12 23:32:12 +01001310 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001311
1312 /* Avoid hanging VPP if the other end does not accept */
1313 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1314 clib_unix_warning ("fcntl");
1315
1316 if (connect (sockfd, (struct sockaddr *) &sun,
1317 sizeof (struct sockaddr_un)) == 0)
1318 {
1319 /* Set the socket to blocking as it was before */
1320 if (fcntl(sockfd, F_SETFL, 0) < 0)
1321 clib_unix_warning ("fcntl2");
1322
1323 vui->sock_errno = 0;
1324 template.file_descriptor = sockfd;
1325 template.private_data =
1326 vui - vhost_user_main.vhost_user_interfaces;
1327 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001328 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001329
1330 /* This sockfd is considered consumed */
1331 sockfd = -1;
1332 }
1333 else
1334 {
1335 vui->sock_errno = errno;
1336 }
1337 }
1338 else
1339 {
1340 /* check if socket is alive */
1341 int error = 0;
1342 socklen_t len = sizeof (error);
1343 int fd = UNIX_GET_FD(vui->clib_file_index);
1344 int retval =
1345 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1346
1347 if (retval)
1348 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001349 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001350 vhost_user_if_disconnect (vui);
1351 }
1352 }
1353 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001354 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001355 /* *INDENT-ON* */
1356 }
1357 return 0;
1358}
1359
1360/* *INDENT-OFF* */
1361VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1362 .function = vhost_user_process,
1363 .type = VLIB_NODE_TYPE_PROCESS,
1364 .name = "vhost-user-process",
1365};
1366/* *INDENT-ON* */
1367
1368/**
1369 * Disables and reset interface structure.
1370 * It can then be either init again, or removed from used interfaces.
1371 */
1372static void
1373vhost_user_term_if (vhost_user_intf_t * vui)
1374{
1375 int q;
1376 vhost_user_main_t *vum = &vhost_user_main;
1377
1378 // disconnect interface sockets
1379 vhost_user_if_disconnect (vui);
Steven Luong4208a4c2019-05-06 08:51:56 -07001380 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001381 vhost_user_update_iface_state (vui);
1382
Steven Luong2c1084a2020-12-10 20:44:22 -08001383 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001384 {
Steven Luong2c1084a2020-12-10 20:44:22 -08001385 clib_spinlock_free (&vui->vrings[q].vring_lock);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001386 }
1387
1388 if (vui->unix_server_index != ~0)
1389 {
1390 //Close server socket
1391 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1392 vui->unix_server_index);
1393 clib_file_del (&file_main, uf);
1394 vui->unix_server_index = ~0;
1395 unlink (vui->sock_filename);
1396 }
1397
1398 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1399 &vui->if_index);
1400}
1401
1402int
1403vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1404{
1405 vhost_user_main_t *vum = &vhost_user_main;
1406 vhost_user_intf_t *vui;
1407 int rv = 0;
1408 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001409 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001410
Dave Barach3940de32019-07-23 16:28:36 -04001411 if (!
1412 (hwif =
1413 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1414 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001415 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1416
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001417 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1418
Jerome Tollet2f54c272018-10-02 11:41:11 +02001419 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1420 hwif->name, hwif->dev_instance);
1421
Steven Luong2c1084a2020-12-10 20:44:22 -08001422 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08001423 {
1424 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001425
Steven Luong67f935e2019-02-01 10:23:56 -08001426 if (txvq->qid == -1)
1427 continue;
1428 if ((vum->ifq_count > 0) &&
Damjan Marioneabd4242020-10-07 20:59:07 +02001429 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1430 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
Steven Luong67f935e2019-02-01 10:23:56 -08001431 {
1432 vum->ifq_count--;
1433 // Stop the timer if there is no more interrupt interface/queue
1434 if ((vum->ifq_count == 0) &&
1435 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1436 {
1437 vlib_process_signal_event (vm,
1438 vhost_user_send_interrupt_node.index,
1439 VHOST_USER_EVENT_STOP_TIMER, 0);
1440 break;
1441 }
1442 }
1443 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001444
1445 // Disable and reset interface
1446 vhost_user_term_if (vui);
1447
1448 // Reset renumbered iface
1449 if (hwif->dev_instance <
1450 vec_len (vum->show_dev_instance_by_real_dev_instance))
1451 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1452
1453 // Delete ethernet interface
1454 ethernet_delete_interface (vnm, vui->hw_if_index);
1455
Steven Luong2c1084a2020-12-10 20:44:22 -08001456 // free vrings
1457 vec_free (vui->vrings);
1458
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001459 // Back to pool
1460 pool_put (vum->vhost_user_interfaces, vui);
1461
1462 return rv;
1463}
1464
1465static clib_error_t *
1466vhost_user_exit (vlib_main_t * vm)
1467{
1468 vnet_main_t *vnm = vnet_get_main ();
1469 vhost_user_main_t *vum = &vhost_user_main;
1470 vhost_user_intf_t *vui;
1471
1472 vlib_worker_thread_barrier_sync (vlib_get_main ());
1473 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001474 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001475 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001476 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001477 /* *INDENT-ON* */
1478 vlib_worker_thread_barrier_release (vlib_get_main ());
1479 return 0;
1480}
1481
1482VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1483
1484/**
1485 * Open server unix socket on specified sock_filename.
1486 */
1487static int
1488vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1489{
1490 int rv = 0;
1491 struct sockaddr_un un = { };
1492 int fd;
1493 /* create listening socket */
1494 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1495 return VNET_API_ERROR_SYSCALL_ERROR_1;
1496
1497 un.sun_family = AF_UNIX;
1498 strncpy ((char *) un.sun_path, (char *) sock_filename,
1499 sizeof (un.sun_path) - 1);
1500
1501 /* remove if exists */
1502 unlink ((char *) sock_filename);
1503
1504 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1505 {
1506 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1507 goto error;
1508 }
1509
1510 if (listen (fd, 1) == -1)
1511 {
1512 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1513 goto error;
1514 }
1515
1516 *sock_fd = fd;
1517 return 0;
1518
1519error:
1520 close (fd);
1521 return rv;
1522}
1523
1524/**
1525 * Create ethernet interface for vhost user interface.
1526 */
1527static void
Steven Luongd6361c72021-01-26 23:44:19 -08001528vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1529 vhost_user_intf_t *vui,
1530 vhost_user_create_if_args_t *args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001531{
1532 vhost_user_main_t *vum = &vhost_user_main;
1533 u8 hwaddr[6];
1534 clib_error_t *error;
1535
1536 /* create hw and sw interface */
Steven Luongd6361c72021-01-26 23:44:19 -08001537 if (args->use_custom_mac)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001538 {
Steven Luongd6361c72021-01-26 23:44:19 -08001539 clib_memcpy (hwaddr, args->hwaddr, 6);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001540 }
1541 else
1542 {
1543 random_u32 (&vum->random);
1544 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1545 hwaddr[0] = 2;
1546 hwaddr[1] = 0xfe;
1547 }
1548
1549 error = ethernet_register_interface
1550 (vnm,
1551 vhost_user_device_class.index,
1552 vui - vum->vhost_user_interfaces /* device instance */ ,
1553 hwaddr /* ethernet address */ ,
1554 &vui->hw_if_index, 0 /* flag change */ );
1555
1556 if (error)
1557 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001558}
1559
1560/*
1561 * Initialize vui with specified attributes
1562 */
1563static void
Steven Luong27ba5002020-11-17 13:30:44 -08001564vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1565 int server_sock_fd, vhost_user_create_if_args_t * args,
1566 u32 * sw_if_index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001567{
1568 vnet_sw_interface_t *sw;
1569 int q;
1570 vhost_user_main_t *vum = &vhost_user_main;
1571 vnet_hw_interface_t *hw;
1572
1573 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1574 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1575 if (server_sock_fd != -1)
1576 {
1577 clib_file_t template = { 0 };
1578 template.read_function = vhost_user_socksvr_accept_ready;
1579 template.file_descriptor = server_sock_fd;
1580 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001581 template.description = format (0, "vhost user %d", sw);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001582 vui->unix_server_index = clib_file_add (&file_main, &template);
1583 }
1584 else
1585 {
1586 vui->unix_server_index = ~0;
1587 }
1588
1589 vui->sw_if_index = sw->sw_if_index;
Steven Luong27ba5002020-11-17 13:30:44 -08001590 strncpy (vui->sock_filename, args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001591 ARRAY_LEN (vui->sock_filename) - 1);
1592 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001593 vui->is_ready = 0;
Steven Luong27ba5002020-11-17 13:30:44 -08001594 vui->feature_mask = args->feature_mask;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001595 vui->clib_file_index = ~0;
1596 vui->log_base_addr = 0;
1597 vui->if_index = vui - vum->vhost_user_interfaces;
Steven Luong27ba5002020-11-17 13:30:44 -08001598 vui->enable_gso = args->enable_gso;
1599 vui->enable_event_idx = args->enable_event_idx;
1600 vui->enable_packed = args->enable_packed;
Steven Luong4208a4c2019-05-06 08:51:56 -07001601 /*
1602 * enable_gso takes precedence over configurable feature mask if there
1603 * is a clash.
1604 * if feature mask disables gso, but enable_gso is configured,
1605 * then gso is enable
1606 * if feature mask enables gso, but enable_gso is not configured,
1607 * then gso is enable
1608 *
1609 * if gso is enable via feature mask, it must enable both host and guest
1610 * gso feature mask, we don't support one sided GSO or partial GSO.
1611 */
1612 if ((vui->enable_gso == 0) &&
Steven Luong27ba5002020-11-17 13:30:44 -08001613 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1614 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
Steven Luong4208a4c2019-05-06 08:51:56 -07001615 vui->enable_gso = 1;
1616 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001617 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1618 &vui->if_index, 0);
1619
Steven Luong2c1084a2020-12-10 20:44:22 -08001620 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1621 CLIB_CACHE_LINE_BYTES);
1622 vui->num_qid = 2;
1623 for (q = 0; q < vec_len (vui->vrings); q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001624 vhost_user_vring_init (vui, q);
1625
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +01001626 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001627 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1628
1629 if (sw_if_index)
1630 *sw_if_index = vui->sw_if_index;
1631
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001632 vec_validate (vui->per_cpu_tx_qid,
1633 vlib_get_thread_main ()->n_vlib_mains - 1);
1634 vhost_user_tx_thread_placement (vui);
1635}
1636
1637int
1638vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001639 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001640{
1641 vhost_user_intf_t *vui = NULL;
1642 u32 sw_if_idx = ~0;
1643 int rv = 0;
1644 int server_sock_fd = -1;
1645 vhost_user_main_t *vum = &vhost_user_main;
1646 uword *if_index;
1647
Steven Luong27ba5002020-11-17 13:30:44 -08001648 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001649 {
1650 return VNET_API_ERROR_INVALID_ARGUMENT;
1651 }
1652
Steven Luong27ba5002020-11-17 13:30:44 -08001653 if_index = mhash_get (&vum->if_index_by_sock_name,
1654 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001655 if (if_index)
1656 {
Steven Luong27ba5002020-11-17 13:30:44 -08001657 vui = &vum->vhost_user_interfaces[*if_index];
1658 args->sw_if_index = vui->sw_if_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001659 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1660 }
1661
Steven Luong27ba5002020-11-17 13:30:44 -08001662 if (args->is_server)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001663 {
1664 if ((rv =
Steven Luong27ba5002020-11-17 13:30:44 -08001665 vhost_user_init_server_sock (args->sock_filename,
1666 &server_sock_fd)) != 0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001667 {
1668 return rv;
1669 }
1670 }
1671
Steven Luong67f935e2019-02-01 10:23:56 -08001672 /* Protect the uninitialized vui from being dispatched by rx/tx */
1673 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001674 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Steven Luongd6361c72021-01-26 23:44:19 -08001675 vhost_user_create_ethernet (vnm, vm, vui, args);
Steven Luong67f935e2019-02-01 10:23:56 -08001676 vlib_worker_thread_barrier_release (vm);
1677
Steven Luong27ba5002020-11-17 13:30:44 -08001678 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001679 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001680 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001681
Steven Luong27ba5002020-11-17 13:30:44 -08001682 if (args->renumber)
1683 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001684
Steven Luong27ba5002020-11-17 13:30:44 -08001685 args->sw_if_index = sw_if_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001686
1687 // Process node must connect
1688 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1689
1690 return rv;
1691}
1692
1693int
1694vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001695 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001696{
1697 vhost_user_main_t *vum = &vhost_user_main;
1698 vhost_user_intf_t *vui = NULL;
1699 u32 sw_if_idx = ~0;
1700 int server_sock_fd = -1;
1701 int rv = 0;
1702 vnet_hw_interface_t *hwif;
1703 uword *if_index;
1704
Steven Luong27ba5002020-11-17 13:30:44 -08001705 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1706 args->sw_if_index))
Dave Barach3940de32019-07-23 16:28:36 -04001707 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001708 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1709
Steven Luong27ba5002020-11-17 13:30:44 -08001710 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001711 return VNET_API_ERROR_INVALID_ARGUMENT;
1712
1713 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1714
1715 /*
1716 * Disallow changing the interface to have the same path name
1717 * as other interface
1718 */
Steven Luong27ba5002020-11-17 13:30:44 -08001719 if_index = mhash_get (&vum->if_index_by_sock_name,
1720 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001721 if (if_index && (*if_index != vui->if_index))
1722 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1723
1724 // First try to open server socket
Steven Luong27ba5002020-11-17 13:30:44 -08001725 if (args->is_server)
1726 if ((rv = vhost_user_init_server_sock (args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001727 &server_sock_fd)) != 0)
1728 return rv;
1729
1730 vhost_user_term_if (vui);
Steven Luong27ba5002020-11-17 13:30:44 -08001731 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001732
Steven Luong27ba5002020-11-17 13:30:44 -08001733 if (args->renumber)
1734 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001735
1736 // Process node must connect
1737 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1738
1739 return rv;
1740}
1741
1742clib_error_t *
1743vhost_user_connect_command_fn (vlib_main_t * vm,
1744 unformat_input_t * input,
1745 vlib_cli_command_t * cmd)
1746{
Steven Luong27ba5002020-11-17 13:30:44 -08001747 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001748 unformat_input_t _line_input, *line_input = &_line_input;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001749 clib_error_t *error = NULL;
Steven Luong27ba5002020-11-17 13:30:44 -08001750 vhost_user_create_if_args_t args = { 0 };
1751 int rv;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001752
1753 /* Get a line of input. */
1754 if (!unformat_user (input, unformat_line_input, line_input))
1755 return 0;
1756
Steven Luong27ba5002020-11-17 13:30:44 -08001757 args.feature_mask = (u64) ~ (0ULL);
1758 args.custom_dev_instance = ~0;
Steven Luong4208a4c2019-05-06 08:51:56 -07001759 /* GSO feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001760 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001761 /* packed-ring feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001762 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1763 /* event_idx feature is disable by default */
1764 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1765
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001766 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1767 {
Steven Luong27ba5002020-11-17 13:30:44 -08001768 if (unformat (line_input, "socket %s", &args.sock_filename))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001769 ;
1770 else if (unformat (line_input, "server"))
Steven Luong27ba5002020-11-17 13:30:44 -08001771 args.is_server = 1;
Steven Luong4208a4c2019-05-06 08:51:56 -07001772 else if (unformat (line_input, "gso"))
Steven Luong27ba5002020-11-17 13:30:44 -08001773 args.enable_gso = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001774 else if (unformat (line_input, "packed"))
Steven Luong27ba5002020-11-17 13:30:44 -08001775 args.enable_packed = 1;
1776 else if (unformat (line_input, "event-idx"))
1777 args.enable_event_idx = 1;
1778 else if (unformat (line_input, "feature-mask 0x%llx",
1779 &args.feature_mask))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001780 ;
Steven Luong27ba5002020-11-17 13:30:44 -08001781 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1782 args.hwaddr))
Steven Luongd6361c72021-01-26 23:44:19 -08001783 args.use_custom_mac = 1;
Steven Luong27ba5002020-11-17 13:30:44 -08001784 else if (unformat (line_input, "renumber %d",
1785 &args.custom_dev_instance))
1786 args.renumber = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001787 else
1788 {
1789 error = clib_error_return (0, "unknown input `%U'",
1790 format_unformat_error, line_input);
1791 goto done;
1792 }
1793 }
1794
Steven Luong27ba5002020-11-17 13:30:44 -08001795 if ((rv = vhost_user_create_if (vnm, vm, &args)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001796 {
1797 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1798 goto done;
1799 }
1800
Steven Luong27ba5002020-11-17 13:30:44 -08001801 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1802 args.sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001803
1804done:
Steven Luong27ba5002020-11-17 13:30:44 -08001805 vec_free (args.sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001806 unformat_free (line_input);
1807
1808 return error;
1809}
1810
1811clib_error_t *
1812vhost_user_delete_command_fn (vlib_main_t * vm,
1813 unformat_input_t * input,
1814 vlib_cli_command_t * cmd)
1815{
1816 unformat_input_t _line_input, *line_input = &_line_input;
1817 u32 sw_if_index = ~0;
1818 vnet_main_t *vnm = vnet_get_main ();
1819 clib_error_t *error = NULL;
1820
1821 /* Get a line of input. */
1822 if (!unformat_user (input, unformat_line_input, line_input))
1823 return 0;
1824
1825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1826 {
1827 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1828 ;
1829 else if (unformat
1830 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1831 &sw_if_index))
1832 {
1833 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001834 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001835 if (hwif == NULL ||
1836 vhost_user_device_class.index != hwif->dev_class_index)
1837 {
1838 error = clib_error_return (0, "Not a vhost interface");
1839 goto done;
1840 }
1841 }
1842 else
1843 {
1844 error = clib_error_return (0, "unknown input `%U'",
1845 format_unformat_error, line_input);
1846 goto done;
1847 }
1848 }
1849
1850 vhost_user_delete_if (vnm, vm, sw_if_index);
1851
1852done:
1853 unformat_free (line_input);
1854
1855 return error;
1856}
1857
1858int
1859vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1860 vhost_user_intf_details_t ** out_vuids)
1861{
1862 int rv = 0;
1863 vhost_user_main_t *vum = &vhost_user_main;
1864 vhost_user_intf_t *vui;
1865 vhost_user_intf_details_t *r_vuids = NULL;
1866 vhost_user_intf_details_t *vuid = NULL;
1867 u32 *hw_if_indices = 0;
1868 vnet_hw_interface_t *hi;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001869 int i;
1870
1871 if (!out_vuids)
1872 return -1;
1873
Damjan Marionb2c31b62020-12-13 21:47:40 +01001874 pool_foreach (vui, vum->vhost_user_interfaces)
1875 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001876
1877 for (i = 0; i < vec_len (hw_if_indices); i++)
1878 {
1879 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1880 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1881
1882 vec_add2 (r_vuids, vuid, 1);
1883 vuid->sw_if_index = vui->sw_if_index;
1884 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1885 vuid->features = vui->features;
1886 vuid->num_regions = vui->nregions;
1887 vuid->is_server = vui->unix_server_index != ~0;
1888 vuid->sock_errno = vui->sock_errno;
Benoît Ganne3a76dc32020-04-24 10:33:40 +02001889 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1890 "%s", vui->sock_filename);
1891 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1892 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1893 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001894 }
1895
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001896 vec_free (hw_if_indices);
1897
1898 *out_vuids = r_vuids;
1899
1900 return rv;
1901}
1902
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001903static u8 *
1904format_vhost_user_desc (u8 * s, va_list * args)
1905{
1906 char *fmt = va_arg (*args, char *);
1907 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1908 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1909 int idx = va_arg (*args, int);
1910 u32 *mem_hint = va_arg (*args, u32 *);
1911
1912 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1913 desc_table[idx].flags, desc_table[idx].next,
1914 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1915 mem_hint)));
1916 return s;
1917}
1918
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001919static void
Steven Luong27ba5002020-11-17 13:30:44 -08001920vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001921{
Steven Luong27ba5002020-11-17 13:30:44 -08001922 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1923 int callfd = UNIX_GET_FD (vq->callfd_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001924
1925 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
Steven Luong27ba5002020-11-17 13:30:44 -08001926 vq->errfd);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001927}
1928
1929static void
1930vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1931 int show_descr, int show_verbose)
1932{
1933 int j;
1934 u32 mem_hint = 0;
1935 u32 idx;
1936 u32 n_entries;
1937 vring_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08001938 vhost_user_vring_t *vq = &vui->vrings[q];
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001939
Steven Luong27ba5002020-11-17 13:30:44 -08001940 if (vq->avail && vq->used)
1941 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1942 "used event idx %u used.idx %d\n", vq->avail->flags,
1943 vhost_user_avail_event_idx (vq), vq->avail->idx,
1944 vhost_user_used_event_idx (vq), vq->used->idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001945
Steven Luong27ba5002020-11-17 13:30:44 -08001946 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001947
1948 if (show_descr)
1949 {
1950 vlib_cli_output (vm, "\n descriptor table:\n");
1951 vlib_cli_output (vm,
1952 " slot addr len flags next "
1953 "user_addr\n");
1954 vlib_cli_output (vm,
1955 " ===== ================== ===== ====== ===== "
1956 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08001957 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001958 {
Steven Luong27ba5002020-11-17 13:30:44 -08001959 desc_table = vq->desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001960 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1961 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1962 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001963 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001964 {
1965 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1966 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1967 if (desc_table)
1968 {
1969 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1970 {
1971 vlib_cli_output
1972 (vm, "%U", format_vhost_user_desc,
1973 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1974 desc_table, idx, &mem_hint);
1975 }
1976 if (n_entries >= 20)
1977 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1978 n_entries);
1979 }
1980 }
1981 }
1982 }
1983}
1984
1985static u8 *
1986format_vhost_user_packed_desc (u8 * s, va_list * args)
1987{
1988 char *fmt = va_arg (*args, char *);
1989 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1990 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1991 int idx = va_arg (*args, int);
1992 u32 *mem_hint = va_arg (*args, u32 *);
1993
1994 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1995 desc_table[idx].flags, desc_table[idx].id,
1996 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1997 mem_hint)));
1998 return s;
1999}
2000
2001static u8 *
Steven Luong27ba5002020-11-17 13:30:44 -08002002format_vhost_user_event_idx_flags (u8 * s, va_list * args)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002003{
Steven Luong27ba5002020-11-17 13:30:44 -08002004 u32 flags = va_arg (*args, u32);
2005 typedef struct
2006 {
2007 u8 value;
2008 char *str;
2009 } event_idx_flags;
2010 static event_idx_flags event_idx_array[] = {
2011#define _(s,v) { .str = #s, .value = v, },
2012 foreach_virtio_event_idx_flags
2013#undef _
2014 };
2015 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002016
Steven Luong27ba5002020-11-17 13:30:44 -08002017 if (flags < num_entries)
2018 s = format (s, "%s", event_idx_array[flags].str);
2019 else
2020 s = format (s, "%u", flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002021 return s;
2022}
2023
2024static void
2025vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2026 int show_descr, int show_verbose)
2027{
2028 int j;
2029 u32 mem_hint = 0;
2030 u32 idx;
2031 u32 n_entries;
2032 vring_packed_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08002033 vhost_user_vring_t *vq = &vui->vrings[q];
2034 u16 off_wrap, event_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002035
Steven Luong27ba5002020-11-17 13:30:44 -08002036 off_wrap = vq->avail_event->off_wrap;
2037 event_idx = off_wrap & 0x7fff;
2038 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2039 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2040 (u32) vq->avail_event->flags, off_wrap, event_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002041
Steven Luong27ba5002020-11-17 13:30:44 -08002042 off_wrap = vq->used_event->off_wrap;
2043 event_idx = off_wrap & 0x7fff;
2044 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2045 "used event idx %u\n", format_vhost_user_event_idx_flags,
2046 (u32) vq->used_event->flags, off_wrap, event_idx);
2047
2048 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2049 vq->avail_wrap_counter, vq->used_wrap_counter);
2050
2051 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002052
2053 if (show_descr)
2054 {
2055 vlib_cli_output (vm, "\n descriptor table:\n");
2056 vlib_cli_output (vm,
2057 " slot addr len flags id "
2058 "user_addr\n");
2059 vlib_cli_output (vm,
2060 " ===== ================== ===== ====== ===== "
2061 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08002062 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002063 {
Steven Luong27ba5002020-11-17 13:30:44 -08002064 desc_table = vq->packed_desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002065 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2066 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2067 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002068 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002069 {
2070 n_entries = desc_table[j].len >> 4;
2071 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2072 if (desc_table)
2073 {
2074 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2075 {
2076 vlib_cli_output
2077 (vm, "%U", format_vhost_user_packed_desc,
2078 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2079 desc_table, idx, &mem_hint);
2080 }
2081 if (n_entries >= 20)
2082 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2083 n_entries);
2084 }
2085 }
2086 }
2087 }
2088}
2089
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002090clib_error_t *
2091show_vhost_user_command_fn (vlib_main_t * vm,
2092 unformat_input_t * input,
2093 vlib_cli_command_t * cmd)
2094{
2095 clib_error_t *error = 0;
2096 vnet_main_t *vnm = vnet_get_main ();
2097 vhost_user_main_t *vum = &vhost_user_main;
2098 vhost_user_intf_t *vui;
2099 u32 hw_if_index, *hw_if_indices = 0;
2100 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08002101 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002102 u32 ci;
2103 int i, j, q;
2104 int show_descr = 0;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002105 int show_verbose = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002106 struct feat_struct
2107 {
2108 u8 bit;
2109 char *str;
2110 };
2111 struct feat_struct *feat_entry;
2112
2113 static struct feat_struct feat_array[] = {
2114#define _(s,b) { .str = #s, .bit = b, },
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002115 foreach_virtio_net_features
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002116#undef _
2117 {.str = NULL}
2118 };
2119
2120#define foreach_protocol_feature \
2121 _(VHOST_USER_PROTOCOL_F_MQ) \
2122 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2123
2124 static struct feat_struct proto_feat_array[] = {
2125#define _(s) { .str = #s, .bit = s},
2126 foreach_protocol_feature
2127#undef _
2128 {.str = NULL}
2129 };
2130
2131 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2132 {
2133 if (unformat
2134 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2135 {
2136 hi = vnet_get_hw_interface (vnm, hw_if_index);
2137 if (vhost_user_device_class.index != hi->dev_class_index)
2138 {
2139 error = clib_error_return (0, "unknown input `%U'",
2140 format_unformat_error, input);
2141 goto done;
2142 }
2143 vec_add1 (hw_if_indices, hw_if_index);
2144 }
2145 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2146 show_descr = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002147 else if (unformat (input, "verbose"))
2148 show_verbose = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002149 else
2150 {
2151 error = clib_error_return (0, "unknown input `%U'",
2152 format_unformat_error, input);
2153 goto done;
2154 }
2155 }
2156 if (vec_len (hw_if_indices) == 0)
2157 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002158 pool_foreach (vui, vum->vhost_user_interfaces)
2159 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002160 }
2161 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2162 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2163 vum->coalesce_frames, vum->coalesce_time);
Steven Luong4208a4c2019-05-06 08:51:56 -07002164 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002165 vum->ifq_count);
Steven Luong4208a4c2019-05-06 08:51:56 -07002166 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002167
2168 for (i = 0; i < vec_len (hw_if_indices); i++)
2169 {
2170 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2171 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07002172 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2173 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2174 hw_if_indices[i]);
Steven Luong2c1084a2020-12-10 20:44:22 -08002175 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
Steven Luong4208a4c2019-05-06 08:51:56 -07002176 if (vui->enable_gso)
2177 vlib_cli_output (vm, " GSO enable");
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002178 if (vui->enable_packed)
2179 vlib_cli_output (vm, " Packed ring enable");
Steven Luong27ba5002020-11-17 13:30:44 -08002180 if (vui->enable_event_idx)
2181 vlib_cli_output (vm, " Event index enable");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002182
2183 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2184 " features mask (0x%llx): \n"
2185 " features (0x%llx): \n",
2186 vui->virtio_net_hdr_sz, vui->feature_mask,
2187 vui->features);
2188
2189 feat_entry = (struct feat_struct *) &feat_array;
2190 while (feat_entry->str)
2191 {
2192 if (vui->features & (1ULL << feat_entry->bit))
2193 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2194 feat_entry->bit);
2195 feat_entry++;
2196 }
2197
2198 vlib_cli_output (vm, " protocol features (0x%llx)",
2199 vui->protocol_features);
2200 feat_entry = (struct feat_struct *) &proto_feat_array;
2201 while (feat_entry->str)
2202 {
2203 if (vui->protocol_features & (1ULL << feat_entry->bit))
2204 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2205 feat_entry->bit);
2206 feat_entry++;
2207 }
2208
2209 vlib_cli_output (vm, "\n");
2210
2211 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2212 vui->sock_filename,
2213 (vui->unix_server_index != ~0) ? "server" : "client",
2214 strerror (vui->sock_errno));
2215
2216 vlib_cli_output (vm, " rx placement: ");
2217
Steven Luong2c1084a2020-12-10 20:44:22 -08002218 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
Steven Luong67f935e2019-02-01 10:23:56 -08002219 {
2220 vnet_main_t *vnm = vnet_get_main ();
2221 uword thread_index;
Steven Luong67f935e2019-02-01 10:23:56 -08002222 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002223
Steven Luong67f935e2019-02-01 10:23:56 -08002224 if (txvq->qid == -1)
2225 continue;
2226 thread_index =
Damjan Marion94100532020-11-06 23:25:57 +01002227 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
2228 vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2229 qid, format_vnet_hw_if_rx_mode, txvq->mode);
Steven Luong67f935e2019-02-01 10:23:56 -08002230 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002231
2232 vlib_cli_output (vm, " tx placement: %s\n",
2233 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2234
2235 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2236 {
2237 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2238 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2239 }
2240
2241 vlib_cli_output (vm, "\n");
2242
2243 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2244
2245 if (vui->nregions)
2246 {
2247 vlib_cli_output (vm,
2248 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2249 vlib_cli_output (vm,
2250 " ====== ===== ================== ================== ================== ================== ==================\n");
2251 }
2252 for (j = 0; j < vui->nregions; j++)
2253 {
2254 vlib_cli_output (vm,
2255 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2256 j, vui->region_mmap_fd[j],
2257 vui->regions[j].guest_phys_addr,
2258 vui->regions[j].memory_size,
2259 vui->regions[j].userspace_addr,
2260 vui->regions[j].mmap_offset,
2261 pointer_to_uword (vui->region_mmap_addr[j]));
2262 }
Steven Luong2c1084a2020-12-10 20:44:22 -08002263 for (q = 0; q < vui->num_qid; q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002264 {
2265 if (!vui->vrings[q].started)
2266 continue;
2267
2268 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2269 (q & 1) ? "RX" : "TX",
2270 vui->vrings[q].enabled ? "" : " disabled");
2271
2272 vlib_cli_output (vm,
Steven Luong27ba5002020-11-17 13:30:44 -08002273 " qsz %d last_avail_idx %d last_used_idx %d"
2274 " last_kick %u\n",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002275 vui->vrings[q].qsz_mask + 1,
2276 vui->vrings[q].last_avail_idx,
Steven Luong27ba5002020-11-17 13:30:44 -08002277 vui->vrings[q].last_used_idx,
2278 vui->vrings[q].last_kick);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002279
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002280 if (vhost_user_is_packed_ring_supported (vui))
2281 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2282 show_verbose);
2283 else
2284 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002285 }
2286 vlib_cli_output (vm, "\n");
2287 }
2288done:
2289 vec_free (hw_if_indices);
2290 return error;
2291}
2292
2293/*
2294 * CLI functions
2295 */
2296
2297/*?
2298 * Create a vHost User interface. Once created, a new virtual interface
2299 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2300 * is the next free index.
2301 *
2302 * There are several parameters associated with a vHost interface:
2303 *
2304 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2305 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2306 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2307 * hypervisor will create the socket if it does not already exist. The VPP code
2308 * is indifferent to the file location. However, if SELinux is enabled, then the
2309 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2310 *
2311 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2312 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2313 * mode, the VM can be reset without tearing down the vHost Interface. In
2314 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2315 * tearing down the vHost Interface.
2316 *
2317 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2318 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2319 * parameter not be used except by experienced users. By default, all supported
2320 * features will be advertised. Otherwise, provide the set of features desired.
2321 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2322 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2323 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2324 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2325 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2326 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2327 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2328 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2329 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2330 *
2331 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2332 * X:X:X:X:X:X unix or X.X.X cisco format.
2333 *
2334 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2335 * in the name to be specified. If instance already exists, name will be used
2336 * anyway and multiple instances will have the same name. Use with caution.
2337 *
2338 * @cliexpar
2339 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2340 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2341 * VirtualEthernet0/0/0
2342 * @cliexend
2343 * Example of how to create a vhost interface with VPP as the server and with just
2344 * multiple queues enabled:
2345 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2346 * VirtualEthernet0/0/1
2347 * @cliexend
2348 * Once the vHost interface is created, enable the interface using:
2349 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2350?*/
2351/* *INDENT-OFF* */
2352VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2353 .path = "create vhost-user",
2354 .short_help = "create vhost-user socket <socket-filename> [server] "
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002355 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
Steven Luong27ba5002020-11-17 13:30:44 -08002356 "[packed] [event-idx]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002357 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002358 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002359};
2360/* *INDENT-ON* */
2361
2362/*?
2363 * Delete a vHost User interface using the interface name or the
2364 * software interface index. Use the '<em>show interface</em>'
2365 * command to determine the software interface index. On deletion,
2366 * the linux socket will not be deleted.
2367 *
2368 * @cliexpar
2369 * Example of how to delete a vhost interface by name:
2370 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2371 * Example of how to delete a vhost interface by software interface index:
2372 * @cliexcmd{delete vhost-user sw_if_index 1}
2373?*/
2374/* *INDENT-OFF* */
2375VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2376 .path = "delete vhost-user",
2377 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2378 .function = vhost_user_delete_command_fn,
2379};
2380
2381/*?
2382 * Display the attributes of a single vHost User interface (provide interface
2383 * name), multiple vHost User interfaces (provide a list of interface names seperated
2384 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2385 * vHost interfaces).
2386 *
2387 * @cliexpar
2388 * @parblock
2389 * Example of how to display a vhost interface:
2390 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2391 * Virtio vhost-user interfaces
2392 * Global:
2393 * coalesce frames 32 time 1e-3
2394 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2395 * virtio_net_hdr_sz 12
2396 * features mask (0xffffffffffffffff):
2397 * features (0x50408000):
2398 * VIRTIO_NET_F_MRG_RXBUF (15)
2399 * VIRTIO_NET_F_MQ (22)
2400 * VIRTIO_F_INDIRECT_DESC (28)
2401 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2402 * protocol features (0x3)
2403 * VHOST_USER_PROTOCOL_F_MQ (0)
2404 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2405 *
2406 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2407 *
2408 * rx placement:
2409 * thread 1 on vring 1
2410 * thread 1 on vring 5
2411 * thread 2 on vring 3
2412 * thread 2 on vring 7
2413 * tx placement: spin-lock
2414 * thread 0 on vring 0
2415 * thread 1 on vring 2
2416 * thread 2 on vring 0
2417 *
2418 * Memory regions (total 2)
2419 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2420 * ====== ===== ================== ================== ================== ================== ==================
2421 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2422 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2423 *
2424 * Virtqueue 0 (TX)
2425 * qsz 256 last_avail_idx 0 last_used_idx 0
2426 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2427 * kickfd 62 callfd 64 errfd -1
2428 *
2429 * Virtqueue 1 (RX)
2430 * qsz 256 last_avail_idx 0 last_used_idx 0
2431 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2432 * kickfd 65 callfd 66 errfd -1
2433 *
2434 * Virtqueue 2 (TX)
2435 * qsz 256 last_avail_idx 0 last_used_idx 0
2436 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2437 * kickfd 63 callfd 70 errfd -1
2438 *
2439 * Virtqueue 3 (RX)
2440 * qsz 256 last_avail_idx 0 last_used_idx 0
2441 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2442 * kickfd 72 callfd 74 errfd -1
2443 *
2444 * Virtqueue 4 (TX disabled)
2445 * qsz 256 last_avail_idx 0 last_used_idx 0
2446 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2447 * kickfd 76 callfd 78 errfd -1
2448 *
2449 * Virtqueue 5 (RX disabled)
2450 * qsz 256 last_avail_idx 0 last_used_idx 0
2451 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2452 * kickfd 80 callfd 82 errfd -1
2453 *
2454 * Virtqueue 6 (TX disabled)
2455 * qsz 256 last_avail_idx 0 last_used_idx 0
2456 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2457 * kickfd 84 callfd 86 errfd -1
2458 *
2459 * Virtqueue 7 (RX disabled)
2460 * qsz 256 last_avail_idx 0 last_used_idx 0
2461 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2462 * kickfd 88 callfd 90 errfd -1
2463 *
2464 * @cliexend
2465 *
2466 * The optional '<em>descriptors</em>' parameter will display the same output as
2467 * the previous example but will include the descriptor table for each queue.
2468 * The output is truncated below:
2469 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2470 * Virtio vhost-user interfaces
2471 * Global:
2472 * coalesce frames 32 time 1e-3
2473 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2474 * virtio_net_hdr_sz 12
2475 * features mask (0xffffffffffffffff):
2476 * features (0x50408000):
2477 * VIRTIO_NET_F_MRG_RXBUF (15)
2478 * VIRTIO_NET_F_MQ (22)
2479 * :
2480 * Virtqueue 0 (TX)
2481 * qsz 256 last_avail_idx 0 last_used_idx 0
2482 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2483 * kickfd 62 callfd 64 errfd -1
2484 *
2485 * descriptor table:
2486 * id addr len flags next user_addr
2487 * ===== ================== ===== ====== ===== ==================
2488 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2489 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2490 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2491 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2492 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2493 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2494 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2495 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2496 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2497 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2498 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2499 * :
2500 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2501 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2502 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2503 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2504 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2505 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2506 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2507 *
2508 * Virtqueue 1 (RX)
2509 * qsz 256 last_avail_idx 0 last_used_idx 0
2510 * :
2511 * @cliexend
2512 * @endparblock
2513?*/
2514/* *INDENT-OFF* */
2515VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2516 .path = "show vhost-user",
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002517 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2518 "[[descriptors] [verbose]]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002519 .function = show_vhost_user_command_fn,
2520};
2521/* *INDENT-ON* */
2522
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002523
2524static clib_error_t *
2525vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2526{
2527 vhost_user_main_t *vum = &vhost_user_main;
2528
2529 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2530 {
2531 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2532 ;
2533 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2534 ;
2535 else if (unformat (input, "dont-dump-memory"))
2536 vum->dont_dump_vhost_user_memory = 1;
2537 else
2538 return clib_error_return (0, "unknown input `%U'",
2539 format_unformat_error, input);
2540 }
2541
2542 return 0;
2543}
2544
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002545/* vhost-user { ... } configuration. */
2546VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2547
2548void
2549vhost_user_unmap_all (void)
2550{
2551 vhost_user_main_t *vum = &vhost_user_main;
2552 vhost_user_intf_t *vui;
2553
2554 if (vum->dont_dump_vhost_user_memory)
2555 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002556 pool_foreach (vui, vum->vhost_user_interfaces)
2557 unmap_all_mem_regions (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002558 }
2559}
2560
2561/*
2562 * fd.io coding-style-patch-verification: ON
2563 *
2564 * Local Variables:
2565 * eval: (c-set-style "gnu")
2566 * End:
2567 */