blob: 2d8cad8da4329668cec8b89cd2f52b48fe253971 [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>
Steven Luongce507582021-08-23 14:31:16 -070040#include <vnet/interface/tx_queue_funcs.h>
Mohsin Kazmie7cde312018-06-26 17:20:11 +020041
42#include <vnet/devices/virtio/vhost_user.h>
43#include <vnet/devices/virtio/vhost_user_inline.h>
44
45/**
46 * @file
47 * @brief vHost User Device Driver.
48 *
49 * This file contains the source code for vHost User interface.
50 */
51
52
53vlib_node_registration_t vhost_user_send_interrupt_node;
54
55/* *INDENT-OFF* */
56vhost_user_main_t vhost_user_main = {
57 .mtu_bytes = 1518,
58};
59
60VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
61 .name = "vhost-user",
62};
63/* *INDENT-ON* */
64
65static long
66get_huge_page_size (int fd)
67{
68 struct statfs s;
69 fstatfs (fd, &s);
70 return s.f_bsize;
71}
72
73static void
74unmap_all_mem_regions (vhost_user_intf_t * vui)
75{
Yichen Wang28812a02018-08-28 23:05:27 -070076 int i, r, q;
77 vhost_user_vring_t *vq;
78
Mohsin Kazmie7cde312018-06-26 17:20:11 +020079 for (i = 0; i < vui->nregions; i++)
80 {
81 if (vui->region_mmap_addr[i] != MAP_FAILED)
82 {
83
84 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
85
86 ssize_t map_sz = (vui->regions[i].memory_size +
87 vui->regions[i].mmap_offset +
88 page_sz - 1) & ~(page_sz - 1);
89
90 r =
91 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
92 map_sz);
93
Jerome Tollet2f54c272018-10-02 11:41:11 +020094 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
95 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
96 page_sz);
Mohsin Kazmie7cde312018-06-26 17:20:11 +020097
98 vui->region_mmap_addr[i] = MAP_FAILED;
99
100 if (r == -1)
101 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200102 vu_log_err (vui, "failed to unmap memory region (errno %d)",
103 errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200104 }
105 close (vui->region_mmap_fd[i]);
106 }
107 }
108 vui->nregions = 0;
Yichen Wang28812a02018-08-28 23:05:27 -0700109
Steven Luong38071b12021-04-21 09:54:34 -0700110 FOR_ALL_VHOST_RX_TXQ (q, vui)
111 {
112 vq = &vui->vrings[q];
113 vq->avail = 0;
114 vq->used = 0;
115 vq->desc = 0;
116 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200117}
118
Steven Luong67f935e2019-02-01 10:23:56 -0800119static_always_inline void
Steven Luongce507582021-08-23 14:31:16 -0700120vhost_user_tx_thread_placement (vhost_user_intf_t *vui, u32 qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200121{
Steven Luongce507582021-08-23 14:31:16 -0700122 vnet_main_t *vnm = vnet_get_main ();
123 vhost_user_vring_t *rxvq = &vui->vrings[qid];
124 u32 q = qid >> 1, rxvq_count;
Steven Luong67f935e2019-02-01 10:23:56 -0800125
Steven Luongce507582021-08-23 14:31:16 -0700126 ASSERT ((qid & 1) == 0);
127 if (!rxvq->started || !rxvq->enabled)
128 return;
129
130 rxvq_count = (qid >> 1) + 1;
131 if (rxvq->queue_index == ~0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200132 {
Steven Luongce507582021-08-23 14:31:16 -0700133 rxvq->queue_index =
134 vnet_hw_if_register_tx_queue (vnm, vui->hw_if_index, q);
135 rxvq->qid = q;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200136 }
Steven Luongce507582021-08-23 14:31:16 -0700137
138 FOR_ALL_VHOST_RXQ (q, vui)
139 {
140 vhost_user_vring_t *rxvq = &vui->vrings[q];
141 u32 qi = rxvq->queue_index;
142
143 if (rxvq->queue_index == ~0)
144 break;
145 for (u32 i = 0; i < vlib_get_n_threads (); i++)
146 vnet_hw_if_tx_queue_unassign_thread (vnm, qi, i);
147 }
148
149 for (u32 i = 0; i < vlib_get_n_threads (); i++)
150 {
151 vhost_user_vring_t *rxvq =
152 &vui->vrings[VHOST_VRING_IDX_RX (i % rxvq_count)];
153 u32 qi = rxvq->queue_index;
154
155 vnet_hw_if_tx_queue_assign_thread (vnm, qi, i);
156 }
157
158 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200159}
160
161/**
162 * @brief Unassign existing interface/queue to thread mappings and re-assign
163 * new interface/queue to thread mappings
164 */
Steven Luong67f935e2019-02-01 10:23:56 -0800165static_always_inline void
166vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200167{
Steven Luong67f935e2019-02-01 10:23:56 -0800168 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200169 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200170 int rv;
Steven Luong67f935e2019-02-01 10:23:56 -0800171 u32 q = qid >> 1;
Steven Luong38071b12021-04-21 09:54:34 -0700172 vhost_user_main_t *vum = &vhost_user_main;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200173
Steven Luong67f935e2019-02-01 10:23:56 -0800174 ASSERT ((qid & 1) == 1); // should be odd
175 // Assign new queue mappings for the interface
Steven Luong38071b12021-04-21 09:54:34 -0700176 if (txvq->queue_index != ~0)
177 return;
Damjan Marion94100532020-11-06 23:25:57 +0100178 vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
179 vhost_user_input_node.index);
180 txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
181 VNET_HW_IF_RXQ_THREAD_ANY);
Steven Luong38071b12021-04-21 09:54:34 -0700182 txvq->thread_index =
183 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
184
Damjan Marioneabd4242020-10-07 20:59:07 +0200185 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Steven Luong67f935e2019-02-01 10:23:56 -0800186 /* Set polling as the default */
Damjan Marioneabd4242020-10-07 20:59:07 +0200187 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
Steven Luong38071b12021-04-21 09:54:34 -0700188 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
189 {
190 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
191 /* Keep a polling queue count for each thread */
192 cpu->polling_q_count++;
193 }
Steven Luong67f935e2019-02-01 10:23:56 -0800194 txvq->qid = q;
Damjan Marion94100532020-11-06 23:25:57 +0100195 rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
Steven Luong67f935e2019-02-01 10:23:56 -0800196 if (rv)
197 vu_log_warn (vui, "unable to set rx mode for interface %d, "
198 "queue %d: rc=%d", vui->hw_if_index, q, rv);
Damjan Marion94100532020-11-06 23:25:57 +0100199 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200200}
201
202/** @brief Returns whether at least one TX and one RX vring are enabled */
203static_always_inline int
204vhost_user_intf_ready (vhost_user_intf_t * vui)
205{
206 int i, found[2] = { }; //RX + TX
207
Steven Luong2c1084a2020-12-10 20:44:22 -0800208 for (i = 0; i < vui->num_qid; i++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200209 if (vui->vrings[i].started && vui->vrings[i].enabled)
210 found[i & 1] = 1;
211
212 return found[0] && found[1];
213}
214
Steven Luong67f935e2019-02-01 10:23:56 -0800215static_always_inline void
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200216vhost_user_update_iface_state (vhost_user_intf_t * vui)
217{
218 /* if we have pointers to descriptor table, go up */
Juraj Slobodab192feb2018-10-01 12:42:07 +0200219 int is_ready = vhost_user_intf_ready (vui);
220 if (is_ready != vui->is_ready)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200221 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200222 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
223 is_ready ? "ready" : "down");
Juraj Slobodab192feb2018-10-01 12:42:07 +0200224 if (vui->admin_up)
225 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
226 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
227 : 0);
228 vui->is_ready = is_ready;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200229 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200230}
231
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200232static clib_error_t *
233vhost_user_callfd_read_ready (clib_file_t * uf)
234{
235 __attribute__ ((unused)) int n;
236 u8 buff[8];
237
238 n = read (uf->file_descriptor, ((char *) &buff), 8);
239
240 return 0;
241}
242
Steven Luong67f935e2019-02-01 10:23:56 -0800243static_always_inline void
244vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
245{
246 if (qid & 1) // RX is odd, TX is even
247 {
Steven Luong38071b12021-04-21 09:54:34 -0700248 if (vui->vrings[qid].queue_index == ~0)
Steven Luong67f935e2019-02-01 10:23:56 -0800249 vhost_user_rx_thread_placement (vui, qid);
250 }
251 else
Steven Luongce507582021-08-23 14:31:16 -0700252 vhost_user_tx_thread_placement (vui, qid);
Steven Luong67f935e2019-02-01 10:23:56 -0800253}
254
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200255static clib_error_t *
256vhost_user_kickfd_read_ready (clib_file_t * uf)
257{
Steven Luong27e93a52021-05-06 10:00:30 -0700258 __attribute__ ((unused)) ssize_t n;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200259 u8 buff[8];
Steven Luong27e93a52021-05-06 10:00:30 -0700260 vhost_user_main_t *vum = &vhost_user_main;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200261 vhost_user_intf_t *vui =
Steven Luong27e93a52021-05-06 10:00:30 -0700262 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200263 u32 qid = uf->private_data & 0xff;
Steven Luong27e93a52021-05-06 10:00:30 -0700264 u32 is_txq = qid & 1;
265 vhost_user_vring_t *vq = &vui->vrings[qid];
266 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200267
Steven Luong27e93a52021-05-06 10:00:30 -0700268 n = read (uf->file_descriptor, buff, 8);
269 if (vq->started == 0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200270 {
Steven Luong27e93a52021-05-06 10:00:30 -0700271 vq->started = 1;
272 vhost_user_thread_placement (vui, qid);
273 vhost_user_update_iface_state (vui);
274 if (is_txq)
275 vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
276 vq->kickfd_idx);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200277 }
278
Steven Luong38071b12021-04-21 09:54:34 -0700279 if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) &&
280 vhost_user_intf_ready (vui))
281 {
282 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index);
283 /*
284 * If the thread has more than 1 queue and the other queue is in polling
285 * mode, there is no need to trigger an interrupt
286 */
287 if (cpu->polling_q_count == 0)
288 vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
289 }
Steven Luong27e93a52021-05-06 10:00:30 -0700290
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200291 return 0;
292}
293
294static_always_inline void
295vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
296{
297 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong2c1084a2020-12-10 20:44:22 -0800298
Dave Barachb7b92992018-10-17 10:38:51 -0400299 clib_memset (vring, 0, sizeof (*vring));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200300 vring->kickfd_idx = ~0;
301 vring->callfd_idx = ~0;
302 vring->errfd = -1;
Steven Luong67f935e2019-02-01 10:23:56 -0800303 vring->qid = -1;
Steven Luong38071b12021-04-21 09:54:34 -0700304 vring->queue_index = ~0;
305 vring->thread_index = ~0;
306 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200307
Steven Luong2c1084a2020-12-10 20:44:22 -0800308 clib_spinlock_init (&vring->vring_lock);
309
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200310 /*
311 * We have a bug with some qemu 2.5, and this may be a fix.
312 * Feel like interpretation holy text, but this is from vhost-user.txt.
313 * "
314 * One queue pair is enabled initially. More queues are enabled
315 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
316 * "
317 * Don't know who's right, but this is what DPDK does.
318 */
319 if (qid == 0 || qid == 1)
320 vring->enabled = 1;
321}
322
323static_always_inline void
324vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
325{
326 vhost_user_vring_t *vring = &vui->vrings[qid];
Steven Luong67f935e2019-02-01 10:23:56 -0800327
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200328 if (vring->kickfd_idx != ~0)
329 {
330 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
331 vring->kickfd_idx);
332 clib_file_del (&file_main, uf);
333 vring->kickfd_idx = ~0;
334 }
335 if (vring->callfd_idx != ~0)
336 {
337 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
338 vring->callfd_idx);
339 clib_file_del (&file_main, uf);
340 vring->callfd_idx = ~0;
341 }
342 if (vring->errfd != -1)
343 {
344 close (vring->errfd);
345 vring->errfd = -1;
346 }
Steven Luong67f935e2019-02-01 10:23:56 -0800347
Steven Luong2c1084a2020-12-10 20:44:22 -0800348 clib_spinlock_free (&vring->vring_lock);
349
Steven Luong38071b12021-04-21 09:54:34 -0700350 // save the needed information in vrings prior to being wiped out
Steven Luong67f935e2019-02-01 10:23:56 -0800351 u16 q = vui->vrings[qid].qid;
Steven Luong38071b12021-04-21 09:54:34 -0700352 u32 queue_index = vui->vrings[qid].queue_index;
353 u32 mode = vui->vrings[qid].mode;
354 u32 thread_index = vui->vrings[qid].thread_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200355 vhost_user_vring_init (vui, qid);
Steven Luong67f935e2019-02-01 10:23:56 -0800356 vui->vrings[qid].qid = q;
Steven Luong38071b12021-04-21 09:54:34 -0700357 vui->vrings[qid].queue_index = queue_index;
358 vui->vrings[qid].mode = mode;
359 vui->vrings[qid].thread_index = thread_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200360}
361
362static_always_inline void
363vhost_user_if_disconnect (vhost_user_intf_t * vui)
364{
365 vnet_main_t *vnm = vnet_get_main ();
366 int q;
367
368 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
369
370 if (vui->clib_file_index != ~0)
371 {
372 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
373 vui->clib_file_index = ~0;
374 }
375
Juraj Slobodab192feb2018-10-01 12:42:07 +0200376 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200377
Steven Luong38071b12021-04-21 09:54:34 -0700378 FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200379
380 unmap_all_mem_regions (vui);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200381 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200382}
383
Steven Luong38071b12021-04-21 09:54:34 -0700384void
385vhost_user_set_operation_mode (vhost_user_intf_t *vui,
386 vhost_user_vring_t *txvq)
387{
388 if (vhost_user_is_packed_ring_supported (vui))
389 {
390 if (txvq->used_event)
391 {
392 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
393 txvq->used_event->flags = VRING_EVENT_F_DISABLE;
394 else
395 txvq->used_event->flags = 0;
396 }
397 }
398 else
399 {
400 if (txvq->used)
401 {
402 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
403 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
404 else
405 txvq->used->flags = 0;
406 }
407 }
408}
409
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200410static clib_error_t *
411vhost_user_socket_read (clib_file_t * uf)
412{
Steven Luong67f935e2019-02-01 10:23:56 -0800413 int n, i, j;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200414 int fd, number_of_fds = 0;
415 int fds[VHOST_MEMORY_MAX_NREGIONS];
416 vhost_user_msg_t msg;
417 struct msghdr mh;
418 struct iovec iov[1];
419 vhost_user_main_t *vum = &vhost_user_main;
420 vhost_user_intf_t *vui;
421 struct cmsghdr *cmsg;
422 u8 q;
423 clib_file_t template = { 0 };
424 vnet_main_t *vnm = vnet_get_main ();
Steven Luong67f935e2019-02-01 10:23:56 -0800425 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200426
427 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
428
429 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
430
Dave Barachb7b92992018-10-17 10:38:51 -0400431 clib_memset (&mh, 0, sizeof (mh));
432 clib_memset (control, 0, sizeof (control));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200433
434 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
435 fds[i] = -1;
436
437 /* set the payload */
438 iov[0].iov_base = (void *) &msg;
439 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
440
441 mh.msg_iov = iov;
442 mh.msg_iovlen = 1;
443 mh.msg_control = control;
444 mh.msg_controllen = sizeof (control);
445
446 n = recvmsg (uf->file_descriptor, &mh, 0);
447
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200448 if (n != VHOST_USER_MSG_HDR_SZ)
449 {
450 if (n == -1)
451 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200452 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
453 strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200454 }
455 else
456 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200457 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
458 n, VHOST_USER_MSG_HDR_SZ);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200459 }
460 goto close_socket;
461 }
462
463 if (mh.msg_flags & MSG_CTRUNC)
464 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200465 vu_log_debug (vui, "MSG_CTRUNC is set");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200466 goto close_socket;
467 }
468
469 cmsg = CMSG_FIRSTHDR (&mh);
470
471 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
472 (cmsg->cmsg_type == SCM_RIGHTS) &&
473 (cmsg->cmsg_len - CMSG_LEN (0) <=
474 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
475 {
476 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
Dave Barach178cf492018-11-13 16:34:13 -0500477 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200478 }
479
480 /* version 1, no reply bit set */
481 if ((msg.flags & 7) != 1)
482 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200483 vu_log_debug (vui, "malformed message received. closing socket");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200484 goto close_socket;
485 }
486
487 {
488 int rv;
489 rv =
490 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
491 msg.size);
492 if (rv < 0)
493 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200494 vu_log_debug (vui, "read failed %s", strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200495 goto close_socket;
496 }
497 else if (rv != msg.size)
498 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200499 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
500 msg.size);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200501 goto close_socket;
502 }
503 }
504
505 switch (msg.request)
506 {
507 case VHOST_USER_GET_FEATURES:
508 msg.flags |= 4;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200509 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
510 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
511 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
512 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
513 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
514 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
515 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
516 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
517 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200518 msg.u64 &= vui->feature_mask;
Steven Luong4208a4c2019-05-06 08:51:56 -0700519
Steven Luong27ba5002020-11-17 13:30:44 -0800520 if (vui->enable_event_idx)
521 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
Steven Luong4208a4c2019-05-06 08:51:56 -0700522 if (vui->enable_gso)
523 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700524 if (vui->enable_packed)
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200525 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
Steven Luong4208a4c2019-05-06 08:51:56 -0700526
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200527 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200528 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
529 "0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -0800530 n =
531 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
532 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
533 {
534 vu_log_debug (vui, "could not send message response");
535 goto close_socket;
536 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200537 break;
538
539 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200540 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
541 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200542
543 vui->features = msg.u64;
544
545 if (vui->features &
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200546 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
547 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200548 vui->virtio_net_hdr_sz = 12;
549 else
550 vui->virtio_net_hdr_sz = 10;
551
552 vui->is_any_layout =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200553 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200554
555 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Steven Luong4208a4c2019-05-06 08:51:56 -0700556 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
557 if (vui->enable_gso &&
Steven Luonga75ad872019-08-06 21:51:34 -0700558 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
559 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100560 {
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100561 hw->caps |= (VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
562 VNET_HW_IF_CAP_TX_UDP_CKSUM);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100563 }
Steven Luong4208a4c2019-05-06 08:51:56 -0700564 else
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100565 {
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100566 hw->caps &= ~(VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100567 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200568 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200569 vui->is_ready = 0;
Steven Luong545866b2019-07-18 18:38:52 -0700570 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200571 break;
572
573 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200574 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
575 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200576
577 if ((msg.memory.nregions < 1) ||
578 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
579 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200580 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
581 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200582 goto close_socket;
583 }
584
585 if (msg.memory.nregions != number_of_fds)
586 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200587 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200588 goto close_socket;
589 }
Steven Luong67f935e2019-02-01 10:23:56 -0800590
591 /* Do the mmap without barrier sync */
592 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
593 for (i = 0; i < msg.memory.nregions; i++)
594 {
595 long page_sz = get_huge_page_size (fds[i]);
596
597 /* align size to page */
598 ssize_t map_sz = (msg.memory.regions[i].memory_size +
599 msg.memory.regions[i].mmap_offset +
600 page_sz - 1) & ~(page_sz - 1);
601
602 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
603 MAP_SHARED, fds[i], 0);
604 if (region_mmap_addr[i] == MAP_FAILED)
605 {
606 vu_log_err (vui, "failed to map memory. errno is %d", errno);
607 for (j = 0; j < i; j++)
608 munmap (region_mmap_addr[j], map_sz);
609 goto close_socket;
610 }
611 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
612 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
613 region_mmap_addr[i], page_sz);
614 }
615
616 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200617 unmap_all_mem_regions (vui);
618 for (i = 0; i < msg.memory.nregions; i++)
619 {
Dave Barach178cf492018-11-13 16:34:13 -0500620 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
621 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200622
Steven Luong67f935e2019-02-01 10:23:56 -0800623 vui->region_mmap_addr[i] = region_mmap_addr[i];
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200624 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
625 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
626 vui->regions[i].memory_size;
627
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200628 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
629 vui->region_mmap_fd[i] = fds[i];
630
631 vui->nregions++;
632 }
Steven Luong4442f7c2019-10-02 07:33:48 -0700633
634 /*
635 * Re-compute desc, used, and avail descriptor table if vring address
636 * is set.
637 */
Steven Luong38071b12021-04-21 09:54:34 -0700638 FOR_ALL_VHOST_RX_TXQ (q, vui)
639 {
640 if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr &&
641 vui->vrings[q].avail_user_addr)
642 {
643 vui->vrings[q].desc =
644 map_user_mem (vui, vui->vrings[q].desc_user_addr);
645 vui->vrings[q].used =
646 map_user_mem (vui, vui->vrings[q].used_user_addr);
647 vui->vrings[q].avail =
648 map_user_mem (vui, vui->vrings[q].avail_user_addr);
649 }
650 }
Steven Luong67f935e2019-02-01 10:23:56 -0800651 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200652 break;
653
654 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200655 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
656 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200657
658 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
659 (msg.state.num == 0) || /* it cannot be zero */
Benoît Ganne32526a42020-12-08 19:08:05 +0100660 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
Steven Luong2c1084a2020-12-10 20:44:22 -0800661 (msg.state.index >= vui->num_qid))
662 {
663 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
664 " %d, msg.state.index %d, curruent max q %d",
665 msg.state.num, msg.state.index, vui->num_qid);
666 goto close_socket;
667 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200668 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
669 break;
670
671 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200672 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
673 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200674
Steven Luong2c1084a2020-12-10 20:44:22 -0800675 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200676 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200677 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800678 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200679 goto close_socket;
680 }
681
682 if (msg.size < sizeof (msg.addr))
683 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200684 vu_log_debug (vui, "vhost message is too short (%d < %d)",
685 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200686 goto close_socket;
687 }
688
Steven Luong67f935e2019-02-01 10:23:56 -0800689 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
690 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
691 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200692
Steven Luong67f935e2019-02-01 10:23:56 -0800693 if ((desc == NULL) || (used == NULL) || (avail == NULL))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200694 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200695 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
696 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200697 goto close_socket;
698 }
699
Steven Luong4442f7c2019-10-02 07:33:48 -0700700 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
701 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
702 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
703
Steven Luong67f935e2019-02-01 10:23:56 -0800704 vlib_worker_thread_barrier_sync (vm);
705 vui->vrings[msg.state.index].desc = desc;
706 vui->vrings[msg.state.index].used = used;
707 vui->vrings[msg.state.index].avail = avail;
708
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200709 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
710 vui->vrings[msg.state.index].log_used =
711 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
712
713 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
714 the ring is initialized in an enabled state. */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200715 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
Steven Luong67f935e2019-02-01 10:23:56 -0800716 vui->vrings[msg.state.index].enabled = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200717
718 vui->vrings[msg.state.index].last_used_idx =
719 vui->vrings[msg.state.index].last_avail_idx =
720 vui->vrings[msg.state.index].used->idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800721 vui->vrings[msg.state.index].last_kick =
722 vui->vrings[msg.state.index].last_used_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200723
Steven Luong38071b12021-04-21 09:54:34 -0700724 /* tell driver that we want interrupts or not */
725 vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]);
Steven Luong67f935e2019-02-01 10:23:56 -0800726 vlib_worker_thread_barrier_release (vm);
727 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200728 break;
729
730 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200731 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200732 break;
733
734 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200735 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
736 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200737 break;
738
739 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200740 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
741 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200742
743 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800744 if (vui->num_qid > q)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200745 {
Steven Luong2c1084a2020-12-10 20:44:22 -0800746 /* if there is old fd, delete and close it */
747 if (vui->vrings[q].callfd_idx != ~0)
748 {
749 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
750 vui->vrings[q].callfd_idx);
751 clib_file_del (&file_main, uf);
752 vui->vrings[q].callfd_idx = ~0;
753 }
754 }
755 else if (vec_len (vui->vrings) > q)
756 {
757 /* grow vrings by pair (RX + TX) */
758 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
759 }
760 else
761 {
762 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
763
764 /*
765 * Double the array size if it is less than 64 entries.
766 * Slow down thereafter.
767 */
768 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
769 new_max_q = vec_len (vui->vrings) << 1;
770 else
771 new_max_q = vec_len (vui->vrings) +
772 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
773 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
774 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
775
776 /* sync with the worker threads, vrings may move due to realloc */
777 vlib_worker_thread_barrier_sync (vm);
778 vec_validate_aligned (vui->vrings, new_max_q - 1,
779 CLIB_CACHE_LINE_BYTES);
780 vlib_worker_thread_barrier_release (vm);
781
782 for (i = old_max_q; i < vec_len (vui->vrings); i++)
783 vhost_user_vring_init (vui, i);
784
785 /* grow vrings by pair (RX + TX) */
786 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200787 }
788
789 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
790 {
791 if (number_of_fds != 1)
792 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200793 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200794 goto close_socket;
795 }
796
797 template.read_function = vhost_user_callfd_read_ready;
798 template.file_descriptor = fds[0];
799 template.private_data =
800 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500801 template.description = format (0, "vhost user");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200802 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
803 }
804 else
805 vui->vrings[q].callfd_idx = ~0;
806 break;
807
808 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200809 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
810 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200811
812 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800813 if (q >= vui->num_qid)
814 {
815 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
816 " %u >= %u", q, vui->num_qid);
817 goto close_socket;
818 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200819
820 if (vui->vrings[q].kickfd_idx != ~0)
821 {
822 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
823 vui->vrings[q].kickfd_idx);
824 clib_file_del (&file_main, uf);
825 vui->vrings[q].kickfd_idx = ~0;
826 }
827
828 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
829 {
830 if (number_of_fds != 1)
831 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200832 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200833 goto close_socket;
834 }
835
836 template.read_function = vhost_user_kickfd_read_ready;
837 template.file_descriptor = fds[0];
838 template.private_data =
839 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
840 q;
841 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
842 }
843 else
844 {
845 //When no kickfd is set, the queue is initialized as started
846 vui->vrings[q].kickfd_idx = ~0;
847 vui->vrings[q].started = 1;
Steven Luong67f935e2019-02-01 10:23:56 -0800848 vhost_user_thread_placement (vui, q);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200849 }
Steven Luong67f935e2019-02-01 10:23:56 -0800850 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200851 break;
852
853 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200854 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
855 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200856
857 q = (u8) (msg.u64 & 0xFF);
Steven Luong2c1084a2020-12-10 20:44:22 -0800858 if (q >= vui->num_qid)
859 {
860 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
861 " %u >= %u", q, vui->num_qid);
862 goto close_socket;
863 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200864
865 if (vui->vrings[q].errfd != -1)
866 close (vui->vrings[q].errfd);
867
868 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
869 {
870 if (number_of_fds != 1)
871 goto close_socket;
872
873 vui->vrings[q].errfd = fds[0];
874 }
875 else
876 vui->vrings[q].errfd = -1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200877 break;
878
879 case VHOST_USER_SET_VRING_BASE:
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700880 vu_log_debug (vui,
881 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200882 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong2c1084a2020-12-10 20:44:22 -0800883 if (msg.state.index >= vui->num_qid)
884 {
885 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
886 " %u >= %u", msg.state.index, vui->num_qid);
887 goto close_socket;
888 }
Steven Luong67f935e2019-02-01 10:23:56 -0800889 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200890 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700891 if (vhost_user_is_packed_ring_supported (vui))
892 {
893 /*
894 * 0 1 2 3
895 * 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
896 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897 * | last avail idx | | last used idx | |
898 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
899 * ^ ^
900 * | |
901 * avail wrap counter used wrap counter
902 */
903 /* last avail idx at bit 0-14. */
904 vui->vrings[msg.state.index].last_avail_idx =
905 msg.state.num & 0x7fff;
906 /* avail wrap counter at bit 15 */
907 vui->vrings[msg.state.index].avail_wrap_counter =
908 ! !(msg.state.num & (1 << 15));
909
910 /*
911 * Although last_used_idx is passed in the upper 16 bits in qemu
912 * implementation, in practice, last_avail_idx and last_used_idx are
913 * usually the same. As a result, DPDK does not bother to pass us
914 * last_used_idx. The spec is not clear on thex coding. I figured it
915 * out by reading the qemu code. So let's just read last_avail_idx
916 * and set last_used_idx equals to last_avail_idx.
917 */
918 vui->vrings[msg.state.index].last_used_idx =
919 vui->vrings[msg.state.index].last_avail_idx;
Steven Luong27ba5002020-11-17 13:30:44 -0800920 vui->vrings[msg.state.index].last_kick =
921 vui->vrings[msg.state.index].last_used_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700922 vui->vrings[msg.state.index].used_wrap_counter =
923 vui->vrings[msg.state.index].avail_wrap_counter;
924
925 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
926 vui->vrings[msg.state.index].avail_wrap_counter =
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200927 VRING_DESC_F_AVAIL;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700928 }
Steven Luong67f935e2019-02-01 10:23:56 -0800929 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200930 break;
931
932 case VHOST_USER_GET_VRING_BASE:
Steven Luong2c1084a2020-12-10 20:44:22 -0800933 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200934 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200935 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
Steven Luong2c1084a2020-12-10 20:44:22 -0800936 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200937 goto close_socket;
938 }
939
Steven Luong67f935e2019-02-01 10:23:56 -0800940 /* protection is needed to prevent rx/tx from changing last_avail_idx */
941 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200942 /*
943 * Copy last_avail_idx from the vring before closing it because
944 * closing the vring also initializes the vring last_avail_idx
945 */
946 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700947 if (vhost_user_is_packed_ring_supported (vui))
948 {
949 msg.state.num =
950 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
951 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
952 msg.state.num |=
953 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
954 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
955 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200956 msg.flags |= 4;
957 msg.size = sizeof (msg.state);
958
Steven Luong67f935e2019-02-01 10:23:56 -0800959 /*
960 * Spec says: Client must [...] stop ring upon receiving
961 * VHOST_USER_GET_VRING_BASE
962 */
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200963 vhost_user_vring_close (vui, msg.state.index);
Steven Luong67f935e2019-02-01 10:23:56 -0800964 vlib_worker_thread_barrier_release (vm);
Steven Luongbc0d9ff2020-03-23 09:34:59 -0700965 vu_log_debug (vui,
966 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
Jerome Tollet2f54c272018-10-02 11:41:11 +0200967 vui->hw_if_index, msg.state.index, msg.state.num);
Steven Luong67f935e2019-02-01 10:23:56 -0800968 n =
969 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
970 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
971 {
972 vu_log_debug (vui, "could not send message response");
973 goto close_socket;
974 }
975 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200976 break;
977
978 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200979 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200980 break;
981
982 case VHOST_USER_SET_LOG_BASE:
Steven Luong67f935e2019-02-01 10:23:56 -0800983 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
984 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200985
Steven Luong67f935e2019-02-01 10:23:56 -0800986 if (msg.size != sizeof (msg.log))
987 {
988 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
989 " %d instead of %d", msg.size, sizeof (msg.log));
990 goto close_socket;
991 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200992
Steven Luong67f935e2019-02-01 10:23:56 -0800993 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
994 {
995 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
996 "VHOST_USER_SET_LOG_BASE received");
997 goto close_socket;
998 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200999
Steven Luong67f935e2019-02-01 10:23:56 -08001000 fd = fds[0];
1001 /* align size to page */
1002 long page_sz = get_huge_page_size (fd);
1003 ssize_t map_sz =
1004 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001005
Steven Luong67f935e2019-02-01 10:23:56 -08001006 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1007 MAP_SHARED, fd, 0);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001008
Steven Luong67f935e2019-02-01 10:23:56 -08001009 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
1010 "mapped 0x%lx", map_sz, msg.log.offset, fd,
1011 log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001012
Steven Luong67f935e2019-02-01 10:23:56 -08001013 if (log_base_addr == MAP_FAILED)
1014 {
1015 vu_log_err (vui, "failed to map memory. errno is %d", errno);
1016 goto close_socket;
1017 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001018
Steven Luong67f935e2019-02-01 10:23:56 -08001019 vlib_worker_thread_barrier_sync (vm);
1020 vui->log_base_addr = log_base_addr;
1021 vui->log_base_addr += msg.log.offset;
1022 vui->log_size = msg.log.size;
1023 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001024
Steven Luong67f935e2019-02-01 10:23:56 -08001025 msg.flags |= 4;
1026 msg.size = sizeof (msg.u64);
1027 n =
1028 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1029 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1030 {
1031 vu_log_debug (vui, "could not send message response");
1032 goto close_socket;
1033 }
1034 break;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001035
1036 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001037 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001038 break;
1039
1040 case VHOST_USER_GET_PROTOCOL_FEATURES:
1041 msg.flags |= 4;
1042 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1043 (1 << VHOST_USER_PROTOCOL_F_MQ);
1044 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001045 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1046 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001047 n =
1048 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1049 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1050 {
1051 vu_log_debug (vui, "could not send message response");
1052 goto close_socket;
1053 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001054 break;
1055
1056 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001057 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1058 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001059 vui->protocol_features = msg.u64;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001060 break;
1061
1062 case VHOST_USER_GET_QUEUE_NUM:
1063 msg.flags |= 4;
Steven Luong2c1084a2020-12-10 20:44:22 -08001064 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001065 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +02001066 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1067 vui->hw_if_index, msg.u64);
Steven Luong67f935e2019-02-01 10:23:56 -08001068 n =
1069 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1070 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1071 {
1072 vu_log_debug (vui, "could not send message response");
1073 goto close_socket;
1074 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001075 break;
1076
1077 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001078 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1079 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1080 msg.state.index);
Steven Luong2c1084a2020-12-10 20:44:22 -08001081 if (msg.state.index >= vui->num_qid)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001082 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001083 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
Steven Luong2c1084a2020-12-10 20:44:22 -08001084 " %u >= %u", msg.state.index, vui->num_qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001085 goto close_socket;
1086 }
1087
1088 vui->vrings[msg.state.index].enabled = msg.state.num;
Steven Luong67f935e2019-02-01 10:23:56 -08001089 vhost_user_thread_placement (vui, msg.state.index);
1090 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001091 break;
1092
1093 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +02001094 vu_log_debug (vui, "unknown vhost-user message %d received. "
1095 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001096 goto close_socket;
1097 }
1098
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001099 return 0;
1100
1101close_socket:
Steven Luong67f935e2019-02-01 10:23:56 -08001102 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001103 vhost_user_if_disconnect (vui);
Steven Luong67f935e2019-02-01 10:23:56 -08001104 vlib_worker_thread_barrier_release (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001105 vhost_user_update_iface_state (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001106 return 0;
1107}
1108
1109static clib_error_t *
1110vhost_user_socket_error (clib_file_t * uf)
1111{
1112 vlib_main_t *vm = vlib_get_main ();
1113 vhost_user_main_t *vum = &vhost_user_main;
1114 vhost_user_intf_t *vui =
1115 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1116
Jerome Tollet2f54c272018-10-02 11:41:11 +02001117 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001118 vlib_worker_thread_barrier_sync (vm);
1119 vhost_user_if_disconnect (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001120 vlib_worker_thread_barrier_release (vm);
1121 return 0;
1122}
1123
1124static clib_error_t *
1125vhost_user_socksvr_accept_ready (clib_file_t * uf)
1126{
1127 int client_fd, client_len;
1128 struct sockaddr_un client;
1129 clib_file_t template = { 0 };
1130 vhost_user_main_t *vum = &vhost_user_main;
1131 vhost_user_intf_t *vui;
1132
1133 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1134
1135 client_len = sizeof (client);
1136 client_fd = accept (uf->file_descriptor,
1137 (struct sockaddr *) &client,
1138 (socklen_t *) & client_len);
1139
1140 if (client_fd < 0)
1141 return clib_error_return_unix (0, "accept");
1142
1143 if (vui->clib_file_index != ~0)
1144 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001145 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1146 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001147 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1148 }
1149
Jerome Tollet2f54c272018-10-02 11:41:11 +02001150 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1151 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001152 template.read_function = vhost_user_socket_read;
1153 template.error_function = vhost_user_socket_error;
1154 template.file_descriptor = client_fd;
1155 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001156 template.description = format (0, "vhost interface %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001157 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001158 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001159 return 0;
1160}
1161
1162static clib_error_t *
1163vhost_user_init (vlib_main_t * vm)
1164{
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001165 vhost_user_main_t *vum = &vhost_user_main;
1166 vlib_thread_main_t *tm = vlib_get_thread_main ();
1167
Jerome Tollet2f54c272018-10-02 11:41:11 +02001168 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1169
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001170 vum->coalesce_frames = 32;
1171 vum->coalesce_time = 1e-3;
1172
1173 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1174
1175 vhost_cpu_t *cpu;
1176 vec_foreach (cpu, vum->cpus)
1177 {
1178 /* This is actually not necessary as validate already zeroes it
1179 * Just keeping the loop here for later because I am lazy. */
1180 cpu->rx_buffers_len = 0;
1181 }
1182
1183 vum->random = random_default_seed ();
1184
1185 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1186
1187 return 0;
1188}
1189
Dave Barachf8d50682019-05-14 18:01:44 -04001190/* *INDENT-OFF* */
1191VLIB_INIT_FUNCTION (vhost_user_init) =
1192{
1193 .runs_after = VLIB_INITS("ip4_init"),
1194};
1195/* *INDENT-ON* */
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001196
1197static uword
1198vhost_user_send_interrupt_process (vlib_main_t * vm,
1199 vlib_node_runtime_t * rt, vlib_frame_t * f)
1200{
1201 vhost_user_intf_t *vui;
1202 f64 timeout = 3153600000.0 /* 100 years */ ;
1203 uword event_type, *event_data = 0;
1204 vhost_user_main_t *vum = &vhost_user_main;
Steven Luong67f935e2019-02-01 10:23:56 -08001205 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001206 f64 now, poll_time_remaining;
1207 f64 next_timeout;
1208 u8 stop_timer = 0;
1209
1210 while (1)
1211 {
1212 poll_time_remaining =
1213 vlib_process_wait_for_event_or_clock (vm, timeout);
1214 event_type = vlib_process_get_events (vm, &event_data);
1215 vec_reset_length (event_data);
1216
1217 /*
1218 * Use the remaining timeout if it is less than coalesce time to avoid
1219 * resetting the existing timer in the middle of expiration
1220 */
1221 timeout = poll_time_remaining;
1222 if (vlib_process_suspend_time_is_zero (timeout) ||
1223 (timeout > vum->coalesce_time))
1224 timeout = vum->coalesce_time;
1225
1226 now = vlib_time_now (vm);
1227 switch (event_type)
1228 {
1229 case VHOST_USER_EVENT_STOP_TIMER:
1230 stop_timer = 1;
1231 break;
1232
1233 case VHOST_USER_EVENT_START_TIMER:
1234 stop_timer = 0;
Steven Luong38071b12021-04-21 09:54:34 -07001235 timeout = 1e-3;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001236 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1237 break;
1238 /* fall through */
1239
1240 case ~0:
1241 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001242 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001243 next_timeout = timeout;
Steven Luong38071b12021-04-21 09:54:34 -07001244 FOR_ALL_VHOST_RX_TXQ (qid, vui)
1245 {
1246 vhost_user_vring_t *vq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001247
Steven Luong38071b12021-04-21 09:54:34 -07001248 if (vq->started == 0)
1249 continue;
1250 if (vq->n_since_last_int)
1251 {
1252 if (now >= vq->int_deadline)
1253 vhost_user_send_call (vm, vui, vq);
1254 else
1255 next_timeout = vq->int_deadline - now;
1256 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001257
Steven Luong38071b12021-04-21 09:54:34 -07001258 if ((next_timeout < timeout) && (next_timeout > 0.0))
1259 timeout = next_timeout;
1260 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001261 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001262 /* *INDENT-ON* */
1263 break;
1264
1265 default:
1266 clib_warning ("BUG: unhandled event type %d", event_type);
1267 break;
1268 }
1269 /* No less than 1 millisecond */
1270 if (timeout < 1e-3)
1271 timeout = 1e-3;
1272 if (stop_timer)
1273 timeout = 3153600000.0;
1274 }
1275 return 0;
1276}
1277
1278/* *INDENT-OFF* */
1279VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1280 .function = vhost_user_send_interrupt_process,
1281 .type = VLIB_NODE_TYPE_PROCESS,
1282 .name = "vhost-user-send-interrupt-process",
1283};
1284/* *INDENT-ON* */
1285
1286static uword
1287vhost_user_process (vlib_main_t * vm,
1288 vlib_node_runtime_t * rt, vlib_frame_t * f)
1289{
1290 vhost_user_main_t *vum = &vhost_user_main;
1291 vhost_user_intf_t *vui;
1292 struct sockaddr_un sun;
1293 int sockfd;
1294 clib_file_t template = { 0 };
1295 f64 timeout = 3153600000.0 /* 100 years */ ;
1296 uword *event_data = 0;
1297
1298 sockfd = -1;
1299 sun.sun_family = AF_UNIX;
1300 template.read_function = vhost_user_socket_read;
1301 template.error_function = vhost_user_socket_error;
1302
1303 while (1)
1304 {
1305 vlib_process_wait_for_event_or_clock (vm, timeout);
1306 vlib_process_get_events (vm, &event_data);
1307 vec_reset_length (event_data);
1308
1309 timeout = 3.0;
1310
1311 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001312 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001313
1314 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1315 if (vui->clib_file_index == ~0)
1316 {
1317 if ((sockfd < 0) &&
1318 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1319 {
1320 /*
1321 * 1st time error or new error for this interface,
1322 * spit out the message and record the error
1323 */
1324 if (!vui->sock_errno || (vui->sock_errno != errno))
1325 {
1326 clib_unix_warning
1327 ("Error: Could not open unix socket for %s",
1328 vui->sock_filename);
1329 vui->sock_errno = errno;
1330 }
1331 continue;
1332 }
1333
1334 /* try to connect */
1335 strncpy (sun.sun_path, (char *) vui->sock_filename,
1336 sizeof (sun.sun_path) - 1);
Damjan Marion62c25ab2020-12-12 23:32:12 +01001337 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001338
1339 /* Avoid hanging VPP if the other end does not accept */
1340 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1341 clib_unix_warning ("fcntl");
1342
1343 if (connect (sockfd, (struct sockaddr *) &sun,
1344 sizeof (struct sockaddr_un)) == 0)
1345 {
1346 /* Set the socket to blocking as it was before */
1347 if (fcntl(sockfd, F_SETFL, 0) < 0)
1348 clib_unix_warning ("fcntl2");
1349
1350 vui->sock_errno = 0;
1351 template.file_descriptor = sockfd;
1352 template.private_data =
1353 vui - vhost_user_main.vhost_user_interfaces;
Steven Luonge2daada2021-04-02 22:42:26 -07001354 template.description = format (0, "vhost user process");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001355 vui->clib_file_index = clib_file_add (&file_main, &template);
Steven Luong2c1084a2020-12-10 20:44:22 -08001356 vui->num_qid = 2;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001357
1358 /* This sockfd is considered consumed */
1359 sockfd = -1;
1360 }
1361 else
1362 {
1363 vui->sock_errno = errno;
1364 }
1365 }
1366 else
1367 {
1368 /* check if socket is alive */
1369 int error = 0;
1370 socklen_t len = sizeof (error);
1371 int fd = UNIX_GET_FD(vui->clib_file_index);
1372 int retval =
1373 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1374
1375 if (retval)
1376 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001377 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001378 vhost_user_if_disconnect (vui);
1379 }
1380 }
1381 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001382 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001383 /* *INDENT-ON* */
1384 }
1385 return 0;
1386}
1387
1388/* *INDENT-OFF* */
1389VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1390 .function = vhost_user_process,
1391 .type = VLIB_NODE_TYPE_PROCESS,
1392 .name = "vhost-user-process",
1393};
1394/* *INDENT-ON* */
1395
1396/**
1397 * Disables and reset interface structure.
1398 * It can then be either init again, or removed from used interfaces.
1399 */
1400static void
1401vhost_user_term_if (vhost_user_intf_t * vui)
1402{
1403 int q;
1404 vhost_user_main_t *vum = &vhost_user_main;
1405
1406 // disconnect interface sockets
1407 vhost_user_if_disconnect (vui);
Steven Luong4208a4c2019-05-06 08:51:56 -07001408 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001409 vhost_user_update_iface_state (vui);
1410
Steven Luong38071b12021-04-21 09:54:34 -07001411 FOR_ALL_VHOST_RX_TXQ (q, vui)
1412 {
1413 clib_spinlock_free (&vui->vrings[q].vring_lock);
1414 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001415
1416 if (vui->unix_server_index != ~0)
1417 {
1418 //Close server socket
1419 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1420 vui->unix_server_index);
1421 clib_file_del (&file_main, uf);
1422 vui->unix_server_index = ~0;
1423 unlink (vui->sock_filename);
1424 }
1425
1426 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1427 &vui->if_index);
1428}
1429
1430int
1431vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1432{
1433 vhost_user_main_t *vum = &vhost_user_main;
1434 vhost_user_intf_t *vui;
1435 int rv = 0;
1436 vnet_hw_interface_t *hwif;
Steven Luong67f935e2019-02-01 10:23:56 -08001437 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001438
Dave Barach3940de32019-07-23 16:28:36 -04001439 if (!
1440 (hwif =
1441 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1442 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001443 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1444
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001445 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1446
Jerome Tollet2f54c272018-10-02 11:41:11 +02001447 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1448 hwif->name, hwif->dev_instance);
1449
Steven Luong38071b12021-04-21 09:54:34 -07001450 FOR_ALL_VHOST_TXQ (qid, vui)
1451 {
1452 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001453
Steven Luong38071b12021-04-21 09:54:34 -07001454 if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) &&
1455 (txvq->thread_index != ~0))
1456 {
1457 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
1458 ASSERT (cpu->polling_q_count != 0);
1459 cpu->polling_q_count--;
1460 }
1461
1462 if ((vum->ifq_count > 0) &&
1463 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1464 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1465 {
1466 vum->ifq_count--;
1467 // Stop the timer if there is no more interrupt interface/queue
1468 if (vum->ifq_count == 0)
1469 {
1470 vlib_process_signal_event (vm,
1471 vhost_user_send_interrupt_node.index,
1472 VHOST_USER_EVENT_STOP_TIMER, 0);
1473 break;
1474 }
1475 }
1476 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001477
1478 // Disable and reset interface
1479 vhost_user_term_if (vui);
1480
1481 // Reset renumbered iface
1482 if (hwif->dev_instance <
1483 vec_len (vum->show_dev_instance_by_real_dev_instance))
1484 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1485
1486 // Delete ethernet interface
1487 ethernet_delete_interface (vnm, vui->hw_if_index);
1488
Steven Luong2c1084a2020-12-10 20:44:22 -08001489 // free vrings
1490 vec_free (vui->vrings);
1491
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001492 // Back to pool
1493 pool_put (vum->vhost_user_interfaces, vui);
1494
1495 return rv;
1496}
1497
1498static clib_error_t *
1499vhost_user_exit (vlib_main_t * vm)
1500{
1501 vnet_main_t *vnm = vnet_get_main ();
1502 vhost_user_main_t *vum = &vhost_user_main;
1503 vhost_user_intf_t *vui;
1504
1505 vlib_worker_thread_barrier_sync (vlib_get_main ());
1506 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001507 pool_foreach (vui, vum->vhost_user_interfaces) {
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001508 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001509 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001510 /* *INDENT-ON* */
1511 vlib_worker_thread_barrier_release (vlib_get_main ());
1512 return 0;
1513}
1514
1515VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1516
1517/**
1518 * Open server unix socket on specified sock_filename.
1519 */
1520static int
1521vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1522{
1523 int rv = 0;
1524 struct sockaddr_un un = { };
1525 int fd;
1526 /* create listening socket */
1527 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1528 return VNET_API_ERROR_SYSCALL_ERROR_1;
1529
1530 un.sun_family = AF_UNIX;
1531 strncpy ((char *) un.sun_path, (char *) sock_filename,
1532 sizeof (un.sun_path) - 1);
1533
1534 /* remove if exists */
1535 unlink ((char *) sock_filename);
1536
1537 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1538 {
1539 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1540 goto error;
1541 }
1542
1543 if (listen (fd, 1) == -1)
1544 {
1545 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1546 goto error;
1547 }
1548
1549 *sock_fd = fd;
1550 return 0;
1551
1552error:
1553 close (fd);
1554 return rv;
1555}
1556
1557/**
1558 * Create ethernet interface for vhost user interface.
1559 */
1560static void
Steven Luongd6361c72021-01-26 23:44:19 -08001561vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1562 vhost_user_intf_t *vui,
1563 vhost_user_create_if_args_t *args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001564{
1565 vhost_user_main_t *vum = &vhost_user_main;
Damjan Marion5c954c42022-01-06 20:36:14 +01001566 vnet_eth_interface_registration_t eir = {};
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001567 u8 hwaddr[6];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001568
1569 /* create hw and sw interface */
Steven Luongd6361c72021-01-26 23:44:19 -08001570 if (args->use_custom_mac)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001571 {
Steven Luongd6361c72021-01-26 23:44:19 -08001572 clib_memcpy (hwaddr, args->hwaddr, 6);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001573 }
1574 else
1575 {
1576 random_u32 (&vum->random);
1577 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1578 hwaddr[0] = 2;
1579 hwaddr[1] = 0xfe;
1580 }
1581
Damjan Marion5c954c42022-01-06 20:36:14 +01001582 eir.dev_class_index = vhost_user_device_class.index;
1583 eir.dev_instance = vui - vum->vhost_user_interfaces /* device instance */,
1584 eir.address = hwaddr;
1585 vui->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001586}
1587
1588/*
1589 * Initialize vui with specified attributes
1590 */
1591static void
Steven Luong27ba5002020-11-17 13:30:44 -08001592vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1593 int server_sock_fd, vhost_user_create_if_args_t * args,
1594 u32 * sw_if_index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001595{
1596 vnet_sw_interface_t *sw;
1597 int q;
1598 vhost_user_main_t *vum = &vhost_user_main;
1599 vnet_hw_interface_t *hw;
1600
1601 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1602 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1603 if (server_sock_fd != -1)
1604 {
1605 clib_file_t template = { 0 };
1606 template.read_function = vhost_user_socksvr_accept_ready;
1607 template.file_descriptor = server_sock_fd;
1608 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Paul Vinciguerra5481ad42020-01-28 14:47:17 -05001609 template.description = format (0, "vhost user %d", sw);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001610 vui->unix_server_index = clib_file_add (&file_main, &template);
1611 }
1612 else
1613 {
1614 vui->unix_server_index = ~0;
1615 }
1616
1617 vui->sw_if_index = sw->sw_if_index;
Steven Luong27ba5002020-11-17 13:30:44 -08001618 strncpy (vui->sock_filename, args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001619 ARRAY_LEN (vui->sock_filename) - 1);
1620 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001621 vui->is_ready = 0;
Steven Luong27ba5002020-11-17 13:30:44 -08001622 vui->feature_mask = args->feature_mask;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001623 vui->clib_file_index = ~0;
1624 vui->log_base_addr = 0;
1625 vui->if_index = vui - vum->vhost_user_interfaces;
Steven Luong27ba5002020-11-17 13:30:44 -08001626 vui->enable_gso = args->enable_gso;
1627 vui->enable_event_idx = args->enable_event_idx;
1628 vui->enable_packed = args->enable_packed;
Steven Luong4208a4c2019-05-06 08:51:56 -07001629 /*
1630 * enable_gso takes precedence over configurable feature mask if there
1631 * is a clash.
1632 * if feature mask disables gso, but enable_gso is configured,
1633 * then gso is enable
1634 * if feature mask enables gso, but enable_gso is not configured,
1635 * then gso is enable
1636 *
1637 * if gso is enable via feature mask, it must enable both host and guest
1638 * gso feature mask, we don't support one sided GSO or partial GSO.
1639 */
1640 if ((vui->enable_gso == 0) &&
Steven Luong27ba5002020-11-17 13:30:44 -08001641 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1642 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
Steven Luong4208a4c2019-05-06 08:51:56 -07001643 vui->enable_gso = 1;
1644 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001645 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1646 &vui->if_index, 0);
1647
Steven Luong2c1084a2020-12-10 20:44:22 -08001648 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1649 CLIB_CACHE_LINE_BYTES);
1650 vui->num_qid = 2;
1651 for (q = 0; q < vec_len (vui->vrings); q++)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001652 vhost_user_vring_init (vui, q);
1653
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001654 hw->caps |= VNET_HW_IF_CAP_INT_MODE;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001655 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1656
1657 if (sw_if_index)
1658 *sw_if_index = vui->sw_if_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001659}
1660
1661int
1662vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001663 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001664{
1665 vhost_user_intf_t *vui = NULL;
1666 u32 sw_if_idx = ~0;
1667 int rv = 0;
1668 int server_sock_fd = -1;
1669 vhost_user_main_t *vum = &vhost_user_main;
1670 uword *if_index;
1671
Steven Luong27ba5002020-11-17 13:30:44 -08001672 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001673 {
1674 return VNET_API_ERROR_INVALID_ARGUMENT;
1675 }
1676
Steven Luong27ba5002020-11-17 13:30:44 -08001677 if_index = mhash_get (&vum->if_index_by_sock_name,
1678 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001679 if (if_index)
1680 {
Steven Luong27ba5002020-11-17 13:30:44 -08001681 vui = &vum->vhost_user_interfaces[*if_index];
1682 args->sw_if_index = vui->sw_if_index;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001683 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1684 }
1685
Steven Luong27ba5002020-11-17 13:30:44 -08001686 if (args->is_server)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001687 {
1688 if ((rv =
Steven Luong27ba5002020-11-17 13:30:44 -08001689 vhost_user_init_server_sock (args->sock_filename,
1690 &server_sock_fd)) != 0)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001691 {
1692 return rv;
1693 }
1694 }
1695
Steven Luong67f935e2019-02-01 10:23:56 -08001696 /* Protect the uninitialized vui from being dispatched by rx/tx */
1697 vlib_worker_thread_barrier_sync (vm);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001698 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Steven Luongd6361c72021-01-26 23:44:19 -08001699 vhost_user_create_ethernet (vnm, vm, vui, args);
Steven Luong67f935e2019-02-01 10:23:56 -08001700 vlib_worker_thread_barrier_release (vm);
1701
Steven Luong27ba5002020-11-17 13:30:44 -08001702 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001703 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Steven Luong67f935e2019-02-01 10:23:56 -08001704 vhost_user_rx_thread_placement (vui, 1);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001705
Steven Luong27ba5002020-11-17 13:30:44 -08001706 if (args->renumber)
1707 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001708
Steven Luong27ba5002020-11-17 13:30:44 -08001709 args->sw_if_index = sw_if_idx;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001710
1711 // Process node must connect
1712 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1713
1714 return rv;
1715}
1716
1717int
1718vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
Steven Luong27ba5002020-11-17 13:30:44 -08001719 vhost_user_create_if_args_t * args)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001720{
1721 vhost_user_main_t *vum = &vhost_user_main;
1722 vhost_user_intf_t *vui = NULL;
1723 u32 sw_if_idx = ~0;
1724 int server_sock_fd = -1;
1725 int rv = 0;
1726 vnet_hw_interface_t *hwif;
1727 uword *if_index;
1728
Steven Luong27ba5002020-11-17 13:30:44 -08001729 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1730 args->sw_if_index))
Dave Barach3940de32019-07-23 16:28:36 -04001731 || hwif->dev_class_index != vhost_user_device_class.index)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001732 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1733
Steven Luong27ba5002020-11-17 13:30:44 -08001734 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001735 return VNET_API_ERROR_INVALID_ARGUMENT;
1736
1737 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1738
1739 /*
1740 * Disallow changing the interface to have the same path name
1741 * as other interface
1742 */
Steven Luong27ba5002020-11-17 13:30:44 -08001743 if_index = mhash_get (&vum->if_index_by_sock_name,
1744 (void *) args->sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001745 if (if_index && (*if_index != vui->if_index))
1746 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1747
1748 // First try to open server socket
Steven Luong27ba5002020-11-17 13:30:44 -08001749 if (args->is_server)
1750 if ((rv = vhost_user_init_server_sock (args->sock_filename,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001751 &server_sock_fd)) != 0)
1752 return rv;
1753
1754 vhost_user_term_if (vui);
Steven Luong27ba5002020-11-17 13:30:44 -08001755 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001756
Steven Luong27ba5002020-11-17 13:30:44 -08001757 if (args->renumber)
1758 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001759
1760 // Process node must connect
1761 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1762
1763 return rv;
1764}
1765
1766clib_error_t *
1767vhost_user_connect_command_fn (vlib_main_t * vm,
1768 unformat_input_t * input,
1769 vlib_cli_command_t * cmd)
1770{
Steven Luong27ba5002020-11-17 13:30:44 -08001771 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001772 unformat_input_t _line_input, *line_input = &_line_input;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001773 clib_error_t *error = NULL;
Steven Luong27ba5002020-11-17 13:30:44 -08001774 vhost_user_create_if_args_t args = { 0 };
1775 int rv;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001776
1777 /* Get a line of input. */
1778 if (!unformat_user (input, unformat_line_input, line_input))
1779 return 0;
1780
Steven Luong27ba5002020-11-17 13:30:44 -08001781 args.feature_mask = (u64) ~ (0ULL);
1782 args.custom_dev_instance = ~0;
Steven Luong4208a4c2019-05-06 08:51:56 -07001783 /* GSO feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001784 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001785 /* packed-ring feature is disable by default */
Steven Luong27ba5002020-11-17 13:30:44 -08001786 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1787 /* event_idx feature is disable by default */
1788 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1789
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001790 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1791 {
Steven Luong27ba5002020-11-17 13:30:44 -08001792 if (unformat (line_input, "socket %s", &args.sock_filename))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001793 ;
1794 else if (unformat (line_input, "server"))
Steven Luong27ba5002020-11-17 13:30:44 -08001795 args.is_server = 1;
Steven Luong4208a4c2019-05-06 08:51:56 -07001796 else if (unformat (line_input, "gso"))
Steven Luong27ba5002020-11-17 13:30:44 -08001797 args.enable_gso = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001798 else if (unformat (line_input, "packed"))
Steven Luong27ba5002020-11-17 13:30:44 -08001799 args.enable_packed = 1;
1800 else if (unformat (line_input, "event-idx"))
1801 args.enable_event_idx = 1;
1802 else if (unformat (line_input, "feature-mask 0x%llx",
1803 &args.feature_mask))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001804 ;
Steven Luong27ba5002020-11-17 13:30:44 -08001805 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1806 args.hwaddr))
Steven Luongd6361c72021-01-26 23:44:19 -08001807 args.use_custom_mac = 1;
Steven Luong27ba5002020-11-17 13:30:44 -08001808 else if (unformat (line_input, "renumber %d",
1809 &args.custom_dev_instance))
1810 args.renumber = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001811 else
1812 {
1813 error = clib_error_return (0, "unknown input `%U'",
1814 format_unformat_error, line_input);
1815 goto done;
1816 }
1817 }
1818
Steven Luong27ba5002020-11-17 13:30:44 -08001819 if ((rv = vhost_user_create_if (vnm, vm, &args)))
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001820 {
1821 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1822 goto done;
1823 }
1824
Steven Luong27ba5002020-11-17 13:30:44 -08001825 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1826 args.sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001827
1828done:
Steven Luong27ba5002020-11-17 13:30:44 -08001829 vec_free (args.sock_filename);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001830 unformat_free (line_input);
1831
1832 return error;
1833}
1834
1835clib_error_t *
1836vhost_user_delete_command_fn (vlib_main_t * vm,
1837 unformat_input_t * input,
1838 vlib_cli_command_t * cmd)
1839{
1840 unformat_input_t _line_input, *line_input = &_line_input;
1841 u32 sw_if_index = ~0;
1842 vnet_main_t *vnm = vnet_get_main ();
1843 clib_error_t *error = NULL;
1844
1845 /* Get a line of input. */
1846 if (!unformat_user (input, unformat_line_input, line_input))
1847 return 0;
1848
1849 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1850 {
1851 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1852 ;
1853 else if (unformat
1854 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1855 &sw_if_index))
1856 {
1857 vnet_hw_interface_t *hwif =
Dave Barach3940de32019-07-23 16:28:36 -04001858 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001859 if (hwif == NULL ||
1860 vhost_user_device_class.index != hwif->dev_class_index)
1861 {
1862 error = clib_error_return (0, "Not a vhost interface");
1863 goto done;
1864 }
1865 }
1866 else
1867 {
1868 error = clib_error_return (0, "unknown input `%U'",
1869 format_unformat_error, line_input);
1870 goto done;
1871 }
1872 }
1873
1874 vhost_user_delete_if (vnm, vm, sw_if_index);
1875
1876done:
1877 unformat_free (line_input);
1878
1879 return error;
1880}
1881
1882int
1883vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1884 vhost_user_intf_details_t ** out_vuids)
1885{
1886 int rv = 0;
1887 vhost_user_main_t *vum = &vhost_user_main;
1888 vhost_user_intf_t *vui;
1889 vhost_user_intf_details_t *r_vuids = NULL;
1890 vhost_user_intf_details_t *vuid = NULL;
1891 u32 *hw_if_indices = 0;
1892 vnet_hw_interface_t *hi;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001893 int i;
1894
1895 if (!out_vuids)
1896 return -1;
1897
Damjan Marionb2c31b62020-12-13 21:47:40 +01001898 pool_foreach (vui, vum->vhost_user_interfaces)
1899 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001900
1901 for (i = 0; i < vec_len (hw_if_indices); i++)
1902 {
1903 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1904 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1905
1906 vec_add2 (r_vuids, vuid, 1);
1907 vuid->sw_if_index = vui->sw_if_index;
1908 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1909 vuid->features = vui->features;
1910 vuid->num_regions = vui->nregions;
1911 vuid->is_server = vui->unix_server_index != ~0;
1912 vuid->sock_errno = vui->sock_errno;
Benoît Ganne3a76dc32020-04-24 10:33:40 +02001913 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1914 "%s", vui->sock_filename);
1915 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1916 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1917 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001918 }
1919
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001920 vec_free (hw_if_indices);
1921
1922 *out_vuids = r_vuids;
1923
1924 return rv;
1925}
1926
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001927static u8 *
1928format_vhost_user_desc (u8 * s, va_list * args)
1929{
1930 char *fmt = va_arg (*args, char *);
1931 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1932 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1933 int idx = va_arg (*args, int);
1934 u32 *mem_hint = va_arg (*args, u32 *);
1935
1936 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1937 desc_table[idx].flags, desc_table[idx].next,
1938 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1939 mem_hint)));
1940 return s;
1941}
1942
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001943static void
Steven Luong27ba5002020-11-17 13:30:44 -08001944vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001945{
Steven Luong27ba5002020-11-17 13:30:44 -08001946 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1947 int callfd = UNIX_GET_FD (vq->callfd_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001948
1949 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
Steven Luong27ba5002020-11-17 13:30:44 -08001950 vq->errfd);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001951}
1952
1953static void
1954vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1955 int show_descr, int show_verbose)
1956{
1957 int j;
1958 u32 mem_hint = 0;
1959 u32 idx;
1960 u32 n_entries;
1961 vring_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08001962 vhost_user_vring_t *vq = &vui->vrings[q];
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001963
Steven Luong27ba5002020-11-17 13:30:44 -08001964 if (vq->avail && vq->used)
Steven Luong38071b12021-04-21 09:54:34 -07001965 vlib_cli_output (vm,
1966 " avail.flags %x avail event idx %u avail.idx %d "
1967 "used.flags %x used event idx %u used.idx %d\n",
1968 vq->avail->flags, vhost_user_avail_event_idx (vq),
1969 vq->avail->idx, vq->used->flags,
Steven Luong27ba5002020-11-17 13:30:44 -08001970 vhost_user_used_event_idx (vq), vq->used->idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001971
Steven Luong27ba5002020-11-17 13:30:44 -08001972 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001973
1974 if (show_descr)
1975 {
1976 vlib_cli_output (vm, "\n descriptor table:\n");
1977 vlib_cli_output (vm,
1978 " slot addr len flags next "
1979 "user_addr\n");
1980 vlib_cli_output (vm,
1981 " ===== ================== ===== ====== ===== "
1982 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08001983 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001984 {
Steven Luong27ba5002020-11-17 13:30:44 -08001985 desc_table = vq->desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001986 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1987 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1988 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001989 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07001990 {
1991 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1992 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1993 if (desc_table)
1994 {
1995 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1996 {
1997 vlib_cli_output
1998 (vm, "%U", format_vhost_user_desc,
1999 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2000 desc_table, idx, &mem_hint);
2001 }
2002 if (n_entries >= 20)
2003 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2004 n_entries);
2005 }
2006 }
2007 }
2008 }
2009}
2010
2011static u8 *
2012format_vhost_user_packed_desc (u8 * s, va_list * args)
2013{
2014 char *fmt = va_arg (*args, char *);
2015 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2016 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
2017 int idx = va_arg (*args, int);
2018 u32 *mem_hint = va_arg (*args, u32 *);
2019
2020 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2021 desc_table[idx].flags, desc_table[idx].id,
2022 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2023 mem_hint)));
2024 return s;
2025}
2026
2027static u8 *
Steven Luong27ba5002020-11-17 13:30:44 -08002028format_vhost_user_event_idx_flags (u8 * s, va_list * args)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002029{
Steven Luong27ba5002020-11-17 13:30:44 -08002030 u32 flags = va_arg (*args, u32);
2031 typedef struct
2032 {
2033 u8 value;
2034 char *str;
2035 } event_idx_flags;
2036 static event_idx_flags event_idx_array[] = {
2037#define _(s,v) { .str = #s, .value = v, },
2038 foreach_virtio_event_idx_flags
2039#undef _
2040 };
2041 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002042
Steven Luong27ba5002020-11-17 13:30:44 -08002043 if (flags < num_entries)
2044 s = format (s, "%s", event_idx_array[flags].str);
2045 else
2046 s = format (s, "%u", flags);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002047 return s;
2048}
2049
2050static void
2051vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2052 int show_descr, int show_verbose)
2053{
2054 int j;
2055 u32 mem_hint = 0;
2056 u32 idx;
2057 u32 n_entries;
2058 vring_packed_desc_t *desc_table;
Steven Luong27ba5002020-11-17 13:30:44 -08002059 vhost_user_vring_t *vq = &vui->vrings[q];
2060 u16 off_wrap, event_idx;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002061
Steven Luong27ba5002020-11-17 13:30:44 -08002062 off_wrap = vq->avail_event->off_wrap;
2063 event_idx = off_wrap & 0x7fff;
2064 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2065 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2066 (u32) vq->avail_event->flags, off_wrap, event_idx);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002067
Steven Luong27ba5002020-11-17 13:30:44 -08002068 off_wrap = vq->used_event->off_wrap;
2069 event_idx = off_wrap & 0x7fff;
2070 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2071 "used event idx %u\n", format_vhost_user_event_idx_flags,
2072 (u32) vq->used_event->flags, off_wrap, event_idx);
2073
2074 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2075 vq->avail_wrap_counter, vq->used_wrap_counter);
2076
2077 vhost_user_show_fds (vm, vq);
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002078
2079 if (show_descr)
2080 {
2081 vlib_cli_output (vm, "\n descriptor table:\n");
2082 vlib_cli_output (vm,
2083 " slot addr len flags id "
2084 "user_addr\n");
2085 vlib_cli_output (vm,
2086 " ===== ================== ===== ====== ===== "
2087 "==================\n");
Steven Luong27ba5002020-11-17 13:30:44 -08002088 for (j = 0; j < vq->qsz_mask + 1; j++)
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002089 {
Steven Luong27ba5002020-11-17 13:30:44 -08002090 desc_table = vq->packed_desc;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002091 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2092 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2093 desc_table, j, &mem_hint);
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002094 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002095 {
2096 n_entries = desc_table[j].len >> 4;
2097 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2098 if (desc_table)
2099 {
2100 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2101 {
2102 vlib_cli_output
2103 (vm, "%U", format_vhost_user_packed_desc,
2104 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2105 desc_table, idx, &mem_hint);
2106 }
2107 if (n_entries >= 20)
2108 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2109 n_entries);
2110 }
2111 }
2112 }
2113 }
2114}
2115
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002116clib_error_t *
2117show_vhost_user_command_fn (vlib_main_t * vm,
2118 unformat_input_t * input,
2119 vlib_cli_command_t * cmd)
2120{
2121 clib_error_t *error = 0;
2122 vnet_main_t *vnm = vnet_get_main ();
2123 vhost_user_main_t *vum = &vhost_user_main;
2124 vhost_user_intf_t *vui;
2125 u32 hw_if_index, *hw_if_indices = 0;
2126 vnet_hw_interface_t *hi;
Steven Luong67f935e2019-02-01 10:23:56 -08002127 u16 qid;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002128 int i, j, q;
2129 int show_descr = 0;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002130 int show_verbose = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002131 struct feat_struct
2132 {
2133 u8 bit;
2134 char *str;
2135 };
2136 struct feat_struct *feat_entry;
2137
2138 static struct feat_struct feat_array[] = {
2139#define _(s,b) { .str = #s, .bit = b, },
Mohsin Kazmia7a22812020-08-31 17:17:16 +02002140 foreach_virtio_net_features
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002141#undef _
2142 {.str = NULL}
2143 };
2144
2145#define foreach_protocol_feature \
2146 _(VHOST_USER_PROTOCOL_F_MQ) \
2147 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2148
2149 static struct feat_struct proto_feat_array[] = {
2150#define _(s) { .str = #s, .bit = s},
2151 foreach_protocol_feature
2152#undef _
2153 {.str = NULL}
2154 };
2155
2156 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2157 {
2158 if (unformat
2159 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2160 {
2161 hi = vnet_get_hw_interface (vnm, hw_if_index);
2162 if (vhost_user_device_class.index != hi->dev_class_index)
2163 {
2164 error = clib_error_return (0, "unknown input `%U'",
2165 format_unformat_error, input);
2166 goto done;
2167 }
2168 vec_add1 (hw_if_indices, hw_if_index);
2169 }
2170 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2171 show_descr = 1;
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002172 else if (unformat (input, "verbose"))
2173 show_verbose = 1;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002174 else
2175 {
2176 error = clib_error_return (0, "unknown input `%U'",
2177 format_unformat_error, input);
2178 goto done;
2179 }
2180 }
2181 if (vec_len (hw_if_indices) == 0)
2182 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002183 pool_foreach (vui, vum->vhost_user_interfaces)
2184 vec_add1 (hw_if_indices, vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002185 }
2186 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2187 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2188 vum->coalesce_frames, vum->coalesce_time);
Steven Luong4208a4c2019-05-06 08:51:56 -07002189 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002190 vum->ifq_count);
Steven Luong4208a4c2019-05-06 08:51:56 -07002191 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
Steven Luong38071b12021-04-21 09:54:34 -07002192 for (u32 tid = 0; tid <= vlib_num_workers (); tid++)
2193 {
2194 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid);
2195 vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid,
2196 cpu->polling_q_count);
2197 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002198
2199 for (i = 0; i < vec_len (hw_if_indices); i++)
2200 {
2201 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2202 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07002203 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2204 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2205 hw_if_indices[i]);
Steven Luong2c1084a2020-12-10 20:44:22 -08002206 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
Steven Luong4208a4c2019-05-06 08:51:56 -07002207 if (vui->enable_gso)
2208 vlib_cli_output (vm, " GSO enable");
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002209 if (vui->enable_packed)
2210 vlib_cli_output (vm, " Packed ring enable");
Steven Luong27ba5002020-11-17 13:30:44 -08002211 if (vui->enable_event_idx)
2212 vlib_cli_output (vm, " Event index enable");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002213
2214 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2215 " features mask (0x%llx): \n"
2216 " features (0x%llx): \n",
2217 vui->virtio_net_hdr_sz, vui->feature_mask,
2218 vui->features);
2219
2220 feat_entry = (struct feat_struct *) &feat_array;
2221 while (feat_entry->str)
2222 {
2223 if (vui->features & (1ULL << feat_entry->bit))
2224 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2225 feat_entry->bit);
2226 feat_entry++;
2227 }
2228
2229 vlib_cli_output (vm, " protocol features (0x%llx)",
2230 vui->protocol_features);
2231 feat_entry = (struct feat_struct *) &proto_feat_array;
2232 while (feat_entry->str)
2233 {
2234 if (vui->protocol_features & (1ULL << feat_entry->bit))
2235 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2236 feat_entry->bit);
2237 feat_entry++;
2238 }
2239
2240 vlib_cli_output (vm, "\n");
2241
2242 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2243 vui->sock_filename,
2244 (vui->unix_server_index != ~0) ? "server" : "client",
2245 strerror (vui->sock_errno));
2246
2247 vlib_cli_output (vm, " rx placement: ");
2248
Steven Luong38071b12021-04-21 09:54:34 -07002249 FOR_ALL_VHOST_TXQ (qid, vui)
2250 {
2251 vhost_user_vring_t *txvq = &vui->vrings[qid];
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002252
Steven Luong38071b12021-04-21 09:54:34 -07002253 if (txvq->qid == -1)
2254 continue;
2255 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2256 txvq->thread_index, qid, format_vnet_hw_if_rx_mode,
2257 txvq->mode);
2258 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002259
Steven Luongce507582021-08-23 14:31:16 -07002260 vlib_cli_output (vm, " tx placement\n");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002261
Steven Luongce507582021-08-23 14:31:16 -07002262 FOR_ALL_VHOST_RXQ (qid, vui)
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002263 {
Steven Luongce507582021-08-23 14:31:16 -07002264 vhost_user_vring_t *rxvq = &vui->vrings[qid];
2265 vnet_hw_if_tx_queue_t *txq;
2266
2267 if (rxvq->queue_index == ~0)
2268 continue;
2269 txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index);
2270 if (txq->threads)
2271 vlib_cli_output (vm, " threads %U on vring %u: %s\n",
2272 format_bitmap_list, txq->threads, qid,
2273 txq->shared_queue ? "spin-lock" : "lock-free");
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002274 }
2275
2276 vlib_cli_output (vm, "\n");
2277
2278 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2279
2280 if (vui->nregions)
2281 {
2282 vlib_cli_output (vm,
2283 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2284 vlib_cli_output (vm,
2285 " ====== ===== ================== ================== ================== ================== ==================\n");
2286 }
2287 for (j = 0; j < vui->nregions; j++)
2288 {
2289 vlib_cli_output (vm,
2290 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2291 j, vui->region_mmap_fd[j],
2292 vui->regions[j].guest_phys_addr,
2293 vui->regions[j].memory_size,
2294 vui->regions[j].userspace_addr,
2295 vui->regions[j].mmap_offset,
2296 pointer_to_uword (vui->region_mmap_addr[j]));
2297 }
Steven Luong38071b12021-04-21 09:54:34 -07002298 FOR_ALL_VHOST_RX_TXQ (q, vui)
2299 {
2300 if (!vui->vrings[q].started)
2301 continue;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002302
Steven Luong38071b12021-04-21 09:54:34 -07002303 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2304 (q & 1) ? "RX" : "TX",
2305 vui->vrings[q].enabled ? "" : " disabled");
Steven Luongce507582021-08-23 14:31:16 -07002306 vlib_cli_output (vm, " global %s queue index %u\n",
2307 (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002308
Steven Luong38071b12021-04-21 09:54:34 -07002309 vlib_cli_output (
2310 vm,
2311 " qsz %d last_avail_idx %d last_used_idx %d"
2312 " last_kick %u\n",
2313 vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx,
2314 vui->vrings[q].last_used_idx, vui->vrings[q].last_kick);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002315
Steven Luong38071b12021-04-21 09:54:34 -07002316 if (vhost_user_is_packed_ring_supported (vui))
2317 vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose);
2318 else
2319 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2320 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002321 vlib_cli_output (vm, "\n");
2322 }
2323done:
2324 vec_free (hw_if_indices);
2325 return error;
2326}
2327
2328/*
2329 * CLI functions
2330 */
2331
2332/*?
2333 * Create a vHost User interface. Once created, a new virtual interface
2334 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2335 * is the next free index.
2336 *
2337 * There are several parameters associated with a vHost interface:
2338 *
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002339 * - <b>socket <socket-filename></b> - Name of the linux socket used by
2340 * hypervisor and VPP to manage the vHost interface. If in <em>server</em>
2341 * mode, VPP will create the socket if it does not already exist. If in
2342 * <em>client</em> mode, hypervisor will create the socket if it does not
2343 * already exist. The VPP code is indifferent to the file location. However,
2344 * if SELinux is enabled, then the socket needs to be created in
2345 * <em>/var/run/vpp/</em>.
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002346 *
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002347 * - <b>server</b> - Optional flag to indicate that VPP should be the server
2348 * for the linux socket. If not provided, VPP will be the client. In
2349 * <em>server</em> mode, the VM can be reset without tearing down the vHost
2350 * Interface. In <em>client</em> mode, VPP can be reset without bringing down
2351 * the VM and tearing down the vHost Interface.
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002352 *
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002353 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated
2354 * at startup. <b>This is intended for degugging only.</b> It is recommended
2355 * that this parameter not be used except by experienced users. By default,
2356 * all supported features will be advertised. Otherwise, provide the set of
2357 * features desired.
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002358 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2359 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2360 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2361 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2362 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2363 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2364 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2365 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2366 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2367 *
2368 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2369 * X:X:X:X:X:X unix or X.X.X cisco format.
2370 *
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002371 * - <b>renumber <dev_instance></b> - Optional parameter which allows the
2372 * instance in the name to be specified. If instance already exists, name
2373 * will be used anyway and multiple instances will have the same name. Use
2374 * with caution.
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002375 *
2376 * @cliexpar
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002377 * Example of how to create a vhost interface with VPP as the client and all
2378 * features enabled:
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002379 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2380 * VirtualEthernet0/0/0
2381 * @cliexend
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002382 * Example of how to create a vhost interface with VPP as the server and with
2383 * just multiple queues enabled:
2384 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server
2385 * feature-mask 0x40400000}
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002386 * VirtualEthernet0/0/1
2387 * @cliexend
2388 * Once the vHost interface is created, enable the interface using:
2389 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2390?*/
2391/* *INDENT-OFF* */
2392VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2393 .path = "create vhost-user",
2394 .short_help = "create vhost-user socket <socket-filename> [server] "
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002395 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
Steven Luong27ba5002020-11-17 13:30:44 -08002396 "[packed] [event-idx]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002397 .function = vhost_user_connect_command_fn,
Steven Luong67f935e2019-02-01 10:23:56 -08002398 .is_mp_safe = 1,
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002399};
2400/* *INDENT-ON* */
2401
2402/*?
2403 * Delete a vHost User interface using the interface name or the
2404 * software interface index. Use the '<em>show interface</em>'
2405 * command to determine the software interface index. On deletion,
2406 * the linux socket will not be deleted.
2407 *
2408 * @cliexpar
2409 * Example of how to delete a vhost interface by name:
2410 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2411 * Example of how to delete a vhost interface by software interface index:
2412 * @cliexcmd{delete vhost-user sw_if_index 1}
2413?*/
2414/* *INDENT-OFF* */
2415VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2416 .path = "delete vhost-user",
2417 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2418 .function = vhost_user_delete_command_fn,
2419};
2420
2421/*?
2422 * Display the attributes of a single vHost User interface (provide interface
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002423 * name), multiple vHost User interfaces (provide a list of interface names
2424 * separated by spaces) or all Vhost User interfaces (omit an interface name
2425 * to display all vHost interfaces).
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002426 *
2427 * @cliexpar
2428 * @parblock
2429 * Example of how to display a vhost interface:
2430 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2431 * Virtio vhost-user interfaces
2432 * Global:
2433 * coalesce frames 32 time 1e-3
2434 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2435 * virtio_net_hdr_sz 12
2436 * features mask (0xffffffffffffffff):
2437 * features (0x50408000):
2438 * VIRTIO_NET_F_MRG_RXBUF (15)
2439 * VIRTIO_NET_F_MQ (22)
2440 * VIRTIO_F_INDIRECT_DESC (28)
2441 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2442 * protocol features (0x3)
2443 * VHOST_USER_PROTOCOL_F_MQ (0)
2444 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2445 *
2446 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2447 *
2448 * rx placement:
2449 * thread 1 on vring 1
2450 * thread 1 on vring 5
2451 * thread 2 on vring 3
2452 * thread 2 on vring 7
2453 * tx placement: spin-lock
2454 * thread 0 on vring 0
2455 * thread 1 on vring 2
2456 * thread 2 on vring 0
2457 *
2458 * Memory regions (total 2)
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002459 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2460 * ====== == =============== =========== ============== =========== ==========
2461 * 0 60 0x00000000 0x000a0000 0xaac00000 0x00000000 0x2b400000
2462 * 1 61 0x000c0000 0x3ff40000 0xaacc0000 0x000c0000 0xabcc0000
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002463 *
2464 * Virtqueue 0 (TX)
2465 * qsz 256 last_avail_idx 0 last_used_idx 0
2466 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2467 * kickfd 62 callfd 64 errfd -1
2468 *
2469 * Virtqueue 1 (RX)
2470 * qsz 256 last_avail_idx 0 last_used_idx 0
2471 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2472 * kickfd 65 callfd 66 errfd -1
2473 *
2474 * Virtqueue 2 (TX)
2475 * qsz 256 last_avail_idx 0 last_used_idx 0
2476 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2477 * kickfd 63 callfd 70 errfd -1
2478 *
2479 * Virtqueue 3 (RX)
2480 * qsz 256 last_avail_idx 0 last_used_idx 0
2481 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2482 * kickfd 72 callfd 74 errfd -1
2483 *
2484 * Virtqueue 4 (TX disabled)
2485 * qsz 256 last_avail_idx 0 last_used_idx 0
2486 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2487 * kickfd 76 callfd 78 errfd -1
2488 *
2489 * Virtqueue 5 (RX disabled)
2490 * qsz 256 last_avail_idx 0 last_used_idx 0
2491 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2492 * kickfd 80 callfd 82 errfd -1
2493 *
2494 * Virtqueue 6 (TX disabled)
2495 * qsz 256 last_avail_idx 0 last_used_idx 0
2496 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2497 * kickfd 84 callfd 86 errfd -1
2498 *
2499 * Virtqueue 7 (RX disabled)
2500 * qsz 256 last_avail_idx 0 last_used_idx 0
2501 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2502 * kickfd 88 callfd 90 errfd -1
2503 *
2504 * @cliexend
2505 *
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002506 * The optional '<em>descriptors</em>' parameter will display the same output
2507 * as the previous example but will include the descriptor table for each
2508 * queue.
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002509 * The output is truncated below:
2510 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2511 * Virtio vhost-user interfaces
2512 * Global:
2513 * coalesce frames 32 time 1e-3
2514 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2515 * virtio_net_hdr_sz 12
2516 * features mask (0xffffffffffffffff):
2517 * features (0x50408000):
2518 * VIRTIO_NET_F_MRG_RXBUF (15)
2519 * VIRTIO_NET_F_MQ (22)
2520 * :
2521 * Virtqueue 0 (TX)
2522 * qsz 256 last_avail_idx 0 last_used_idx 0
2523 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2524 * kickfd 62 callfd 64 errfd -1
2525 *
2526 * descriptor table:
2527 * id addr len flags next user_addr
2528 * ===== ================== ===== ====== ===== ==================
2529 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2530 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2531 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2532 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2533 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2534 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2535 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2536 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2537 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2538 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2539 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2540 * :
2541 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2542 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2543 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2544 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2545 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2546 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2547 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2548 *
2549 * Virtqueue 1 (RX)
2550 * qsz 256 last_avail_idx 0 last_used_idx 0
2551 * :
2552 * @cliexend
2553 * @endparblock
2554?*/
2555/* *INDENT-OFF* */
2556VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2557 .path = "show vhost-user",
Steven Luongbc0d9ff2020-03-23 09:34:59 -07002558 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2559 "[[descriptors] [verbose]]",
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002560 .function = show_vhost_user_command_fn,
2561};
2562/* *INDENT-ON* */
2563
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002564
2565static clib_error_t *
2566vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2567{
2568 vhost_user_main_t *vum = &vhost_user_main;
2569
2570 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2571 {
2572 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2573 ;
2574 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2575 ;
2576 else if (unformat (input, "dont-dump-memory"))
2577 vum->dont_dump_vhost_user_memory = 1;
2578 else
2579 return clib_error_return (0, "unknown input `%U'",
2580 format_unformat_error, input);
2581 }
2582
2583 return 0;
2584}
2585
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002586/* vhost-user { ... } configuration. */
2587VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2588
2589void
2590vhost_user_unmap_all (void)
2591{
2592 vhost_user_main_t *vum = &vhost_user_main;
2593 vhost_user_intf_t *vui;
2594
2595 if (vum->dont_dump_vhost_user_memory)
2596 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01002597 pool_foreach (vui, vum->vhost_user_interfaces)
2598 unmap_all_mem_regions (vui);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002599 }
2600}
2601
2602/*
2603 * fd.io coding-style-patch-verification: ON
2604 *
2605 * Local Variables:
2606 * eval: (c-set-style "gnu")
2607 * End:
2608 */