blob: d13ea3beab3b94164e733871b4eeb4b07a87905a [file] [log] [blame]
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001/*
2 *------------------------------------------------------------------
3 * vhost.c - vhost-user
4 *
5 * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <fcntl.h> /* for open */
21#include <sys/ioctl.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/uio.h> /* for iovec */
27#include <netinet/in.h>
28#include <sys/vfs.h>
29
30#include <linux/if_arp.h>
31#include <linux/if_tun.h>
32
33#include <vlib/vlib.h>
34#include <vlib/unix/unix.h>
35
36#include <vnet/ip/ip.h>
37
38#include <vnet/ethernet/ethernet.h>
39#include <vnet/devices/devices.h>
40#include <vnet/feature/feature.h>
41
42#include <vnet/devices/virtio/vhost_user.h>
43#include <vnet/devices/virtio/vhost_user_inline.h>
44
45/**
46 * @file
47 * @brief vHost User Device Driver.
48 *
49 * This file contains the source code for vHost User interface.
50 */
51
52
53vlib_node_registration_t vhost_user_send_interrupt_node;
54
55/* *INDENT-OFF* */
56vhost_user_main_t vhost_user_main = {
57 .mtu_bytes = 1518,
58};
59
60VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
61 .name = "vhost-user",
62};
63/* *INDENT-ON* */
64
65static long
66get_huge_page_size (int fd)
67{
68 struct statfs s;
69 fstatfs (fd, &s);
70 return s.f_bsize;
71}
72
73static void
74unmap_all_mem_regions (vhost_user_intf_t * vui)
75{
Yichen Wang28812a02018-08-28 23:05:27 -070076 int i, r, q;
77 vhost_user_vring_t *vq;
78
Mohsin Kazmie7cde312018-06-26 17:20:11 +020079 for (i = 0; i < vui->nregions; i++)
80 {
81 if (vui->region_mmap_addr[i] != MAP_FAILED)
82 {
83
84 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
85
86 ssize_t map_sz = (vui->regions[i].memory_size +
87 vui->regions[i].mmap_offset +
88 page_sz - 1) & ~(page_sz - 1);
89
90 r =
91 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
92 map_sz);
93
Jerome Tollet2f54c272018-10-02 11:41:11 +020094 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
95 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
96 page_sz);
Mohsin Kazmie7cde312018-06-26 17:20:11 +020097
98 vui->region_mmap_addr[i] = MAP_FAILED;
99
100 if (r == -1)
101 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200102 vu_log_err (vui, "failed to unmap memory region (errno %d)",
103 errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200104 }
105 close (vui->region_mmap_fd[i]);
106 }
107 }
108 vui->nregions = 0;
Yichen Wang28812a02018-08-28 23:05:27 -0700109
110 for (q = 0; q < VHOST_VRING_MAX_N; q++)
111 {
112 vq = &vui->vrings[q];
113 vq->avail = 0;
114 vq->used = 0;
115 vq->desc = 0;
116 }
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200117}
118
119static void
120vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
121{
122 //Let's try to assign one queue to each thread
123 u32 qid = 0;
124 u32 thread_index = 0;
125 vui->use_tx_spinlock = 0;
126 while (1)
127 {
128 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
129 {
130 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
131 if (!rxvq->started || !rxvq->enabled)
132 continue;
133
134 vui->per_cpu_tx_qid[thread_index] = qid;
135 thread_index++;
136 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
137 return;
138 }
139 //We need to loop, meaning the spinlock has to be used
140 vui->use_tx_spinlock = 1;
141 if (thread_index == 0)
142 {
143 //Could not find a single valid one
144 for (thread_index = 0;
145 thread_index < vlib_get_thread_main ()->n_vlib_mains;
146 thread_index++)
147 {
148 vui->per_cpu_tx_qid[thread_index] = 0;
149 }
150 return;
151 }
152 }
153}
154
155/**
156 * @brief Unassign existing interface/queue to thread mappings and re-assign
157 * new interface/queue to thread mappings
158 */
159static void
160vhost_user_rx_thread_placement ()
161{
162 vhost_user_main_t *vum = &vhost_user_main;
163 vhost_user_intf_t *vui;
164 vhost_user_vring_t *txvq;
165 vnet_main_t *vnm = vnet_get_main ();
166 u32 qid;
167 int rv;
168 u16 *queue;
169
170 // Scrap all existing mappings for all interfaces/queues
171 /* *INDENT-OFF* */
172 pool_foreach (vui, vum->vhost_user_interfaces, {
173 vec_foreach (queue, vui->rx_queues)
174 {
175 rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
176 *queue);
177 if (rv)
Jerome Tollet2f54c272018-10-02 11:41:11 +0200178 vu_log_warn (vui, "unable to unassign interface %d, "
179 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200180 }
181 vec_reset_length (vui->rx_queues);
182 });
183 /* *INDENT-ON* */
184
185 // Create the rx_queues for all interfaces
186 /* *INDENT-OFF* */
187 pool_foreach (vui, vum->vhost_user_interfaces, {
188 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
189 {
190 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
191 if (txvq->started)
192 {
193 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
194 /* Set polling as the default */
195 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
196 vec_add1 (vui->rx_queues, qid);
197 }
198 }
199 });
200 /* *INDENT-ON* */
201
202 // Assign new mappings for all interfaces/queues
203 /* *INDENT-OFF* */
204 pool_foreach (vui, vum->vhost_user_interfaces, {
205 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
206 vhost_user_input_node.index);
207 vec_foreach (queue, vui->rx_queues)
208 {
209 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
210 ~0);
211 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
212 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
213 txvq->mode);
214 if (rv)
Jerome Tollet2f54c272018-10-02 11:41:11 +0200215 vu_log_warn (vui, "unable to set rx mode for interface %d, "
216 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200217 }
218 });
219 /* *INDENT-ON* */
220}
221
222/** @brief Returns whether at least one TX and one RX vring are enabled */
223static_always_inline int
224vhost_user_intf_ready (vhost_user_intf_t * vui)
225{
226 int i, found[2] = { }; //RX + TX
227
228 for (i = 0; i < VHOST_VRING_MAX_N; i++)
229 if (vui->vrings[i].started && vui->vrings[i].enabled)
230 found[i & 1] = 1;
231
232 return found[0] && found[1];
233}
234
235static void
236vhost_user_update_iface_state (vhost_user_intf_t * vui)
237{
238 /* if we have pointers to descriptor table, go up */
Juraj Slobodab192feb2018-10-01 12:42:07 +0200239 int is_ready = vhost_user_intf_ready (vui);
240 if (is_ready != vui->is_ready)
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200241 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200242 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
243 is_ready ? "ready" : "down");
Juraj Slobodab192feb2018-10-01 12:42:07 +0200244 if (vui->admin_up)
245 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
246 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
247 : 0);
248 vui->is_ready = is_ready;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200249 }
250 vhost_user_rx_thread_placement ();
251 vhost_user_tx_thread_placement (vui);
252}
253
254static void
255vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
256{
257 u32 qid;
258 vnet_main_t *vnm = vnet_get_main ();
259
260 qid = ifq & 0xff;
261 if ((qid & 1) == 0)
262 /* Only care about the odd number, or TX, virtqueue */
263 return;
264
265 if (vhost_user_intf_ready (vui))
266 // qid >> 1 is to convert virtqueue number to vring queue index
267 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
268}
269
270static clib_error_t *
271vhost_user_callfd_read_ready (clib_file_t * uf)
272{
273 __attribute__ ((unused)) int n;
274 u8 buff[8];
275
276 n = read (uf->file_descriptor, ((char *) &buff), 8);
277
278 return 0;
279}
280
281static clib_error_t *
282vhost_user_kickfd_read_ready (clib_file_t * uf)
283{
284 __attribute__ ((unused)) int n;
285 u8 buff[8];
286 vhost_user_intf_t *vui =
287 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
288 uf->private_data >> 8);
289 u32 qid = uf->private_data & 0xff;
290
291 n = read (uf->file_descriptor, ((char *) &buff), 8);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200292 vu_log_debug (vui, "if %d KICK queue %d", uf->private_data >> 8, qid);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200293 if (!vui->vrings[qid].started ||
Juraj Slobodab192feb2018-10-01 12:42:07 +0200294 (vhost_user_intf_ready (vui) != vui->is_ready))
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200295 {
296 vlib_worker_thread_barrier_sync (vlib_get_main ());
297 vui->vrings[qid].started = 1;
298 vhost_user_update_iface_state (vui);
299 vlib_worker_thread_barrier_release (vlib_get_main ());
300 }
301
302 vhost_user_set_interrupt_pending (vui, uf->private_data);
303 return 0;
304}
305
306static_always_inline void
307vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
308{
309 vhost_user_vring_t *vring = &vui->vrings[qid];
Dave Barachb7b92992018-10-17 10:38:51 -0400310 clib_memset (vring, 0, sizeof (*vring));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200311 vring->kickfd_idx = ~0;
312 vring->callfd_idx = ~0;
313 vring->errfd = -1;
314
315 /*
316 * We have a bug with some qemu 2.5, and this may be a fix.
317 * Feel like interpretation holy text, but this is from vhost-user.txt.
318 * "
319 * One queue pair is enabled initially. More queues are enabled
320 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
321 * "
322 * Don't know who's right, but this is what DPDK does.
323 */
324 if (qid == 0 || qid == 1)
325 vring->enabled = 1;
326}
327
328static_always_inline void
329vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
330{
331 vhost_user_vring_t *vring = &vui->vrings[qid];
332 if (vring->kickfd_idx != ~0)
333 {
334 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
335 vring->kickfd_idx);
336 clib_file_del (&file_main, uf);
337 vring->kickfd_idx = ~0;
338 }
339 if (vring->callfd_idx != ~0)
340 {
341 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
342 vring->callfd_idx);
343 clib_file_del (&file_main, uf);
344 vring->callfd_idx = ~0;
345 }
346 if (vring->errfd != -1)
347 {
348 close (vring->errfd);
349 vring->errfd = -1;
350 }
351 vhost_user_vring_init (vui, qid);
352}
353
354static_always_inline void
355vhost_user_if_disconnect (vhost_user_intf_t * vui)
356{
357 vnet_main_t *vnm = vnet_get_main ();
358 int q;
359
360 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
361
362 if (vui->clib_file_index != ~0)
363 {
364 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
365 vui->clib_file_index = ~0;
366 }
367
Juraj Slobodab192feb2018-10-01 12:42:07 +0200368 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200369
370 for (q = 0; q < VHOST_VRING_MAX_N; q++)
371 vhost_user_vring_close (vui, q);
372
373 unmap_all_mem_regions (vui);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200374 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200375}
376
377static clib_error_t *
378vhost_user_socket_read (clib_file_t * uf)
379{
380 int n, i;
381 int fd, number_of_fds = 0;
382 int fds[VHOST_MEMORY_MAX_NREGIONS];
383 vhost_user_msg_t msg;
384 struct msghdr mh;
385 struct iovec iov[1];
386 vhost_user_main_t *vum = &vhost_user_main;
387 vhost_user_intf_t *vui;
388 struct cmsghdr *cmsg;
389 u8 q;
390 clib_file_t template = { 0 };
391 vnet_main_t *vnm = vnet_get_main ();
392
393 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
394
395 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
396
Dave Barachb7b92992018-10-17 10:38:51 -0400397 clib_memset (&mh, 0, sizeof (mh));
398 clib_memset (control, 0, sizeof (control));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200399
400 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
401 fds[i] = -1;
402
403 /* set the payload */
404 iov[0].iov_base = (void *) &msg;
405 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
406
407 mh.msg_iov = iov;
408 mh.msg_iovlen = 1;
409 mh.msg_control = control;
410 mh.msg_controllen = sizeof (control);
411
412 n = recvmsg (uf->file_descriptor, &mh, 0);
413
414 /* Stop workers to avoid end of the world */
415 vlib_worker_thread_barrier_sync (vlib_get_main ());
416
417 if (n != VHOST_USER_MSG_HDR_SZ)
418 {
419 if (n == -1)
420 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200421 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
422 strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200423 }
424 else
425 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200426 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
427 n, VHOST_USER_MSG_HDR_SZ);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200428 }
429 goto close_socket;
430 }
431
432 if (mh.msg_flags & MSG_CTRUNC)
433 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200434 vu_log_debug (vui, "MSG_CTRUNC is set");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200435 goto close_socket;
436 }
437
438 cmsg = CMSG_FIRSTHDR (&mh);
439
440 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
441 (cmsg->cmsg_type == SCM_RIGHTS) &&
442 (cmsg->cmsg_len - CMSG_LEN (0) <=
443 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
444 {
445 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
Dave Barach178cf492018-11-13 16:34:13 -0500446 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200447 }
448
449 /* version 1, no reply bit set */
450 if ((msg.flags & 7) != 1)
451 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200452 vu_log_debug (vui, "malformed message received. closing socket");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200453 goto close_socket;
454 }
455
456 {
457 int rv;
458 rv =
459 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
460 msg.size);
461 if (rv < 0)
462 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200463 vu_log_debug (vui, "read failed %s", strerror (errno));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200464 goto close_socket;
465 }
466 else if (rv != msg.size)
467 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200468 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
469 msg.size);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200470 goto close_socket;
471 }
472 }
473
474 switch (msg.request)
475 {
476 case VHOST_USER_GET_FEATURES:
477 msg.flags |= 4;
478 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
479 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
480 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
481 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
482 (1ULL << FEAT_VHOST_F_LOG_ALL) |
483 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
484 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
485 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
486 (1ULL << FEAT_VIRTIO_F_VERSION_1);
487 msg.u64 &= vui->feature_mask;
488 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200489 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
490 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200491 break;
492
493 case VHOST_USER_SET_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200494 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
495 "0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200496
497 vui->features = msg.u64;
498
499 if (vui->features &
500 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
501 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
502 vui->virtio_net_hdr_sz = 12;
503 else
504 vui->virtio_net_hdr_sz = 10;
505
506 vui->is_any_layout =
507 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
508
509 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
510 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Juraj Slobodab192feb2018-10-01 12:42:07 +0200511 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200512
513 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
514 vhost_user_vring_close(&vui->vrings[q]); */
515
516 break;
517
518 case VHOST_USER_SET_MEM_TABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200519 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
520 vui->hw_if_index, msg.memory.nregions);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200521
522 if ((msg.memory.nregions < 1) ||
523 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
524 {
525
Jerome Tollet2f54c272018-10-02 11:41:11 +0200526 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
527 VHOST_MEMORY_MAX_NREGIONS);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200528
529 goto close_socket;
530 }
531
532 if (msg.memory.nregions != number_of_fds)
533 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200534 vu_log_debug (vui, "each memory region must have FD");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200535 goto close_socket;
536 }
537 unmap_all_mem_regions (vui);
538 for (i = 0; i < msg.memory.nregions; i++)
539 {
Dave Barach178cf492018-11-13 16:34:13 -0500540 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
541 sizeof (vhost_user_memory_region_t));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200542
543 long page_sz = get_huge_page_size (fds[i]);
544
545 /* align size to page */
546 ssize_t map_sz = (vui->regions[i].memory_size +
547 vui->regions[i].mmap_offset +
548 page_sz - 1) & ~(page_sz - 1);
549
550 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
551 MAP_SHARED, fds[i], 0);
552 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
553 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
554 vui->regions[i].memory_size;
555
Jerome Tollet2f54c272018-10-02 11:41:11 +0200556 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
557 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
558 vui->region_mmap_addr[i], page_sz);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200559
560 if (vui->region_mmap_addr[i] == MAP_FAILED)
561 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200562 vu_log_err (vui, "failed to map memory. errno is %d", errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200563 goto close_socket;
564 }
565 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
566 vui->region_mmap_fd[i] = fds[i];
567
568 vui->nregions++;
569 }
570 break;
571
572 case VHOST_USER_SET_VRING_NUM:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200573 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
574 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200575
576 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
577 (msg.state.num == 0) || /* it cannot be zero */
578 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
579 goto close_socket;
580 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
581 break;
582
583 case VHOST_USER_SET_VRING_ADDR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200584 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
585 vui->hw_if_index, msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200586
587 if (msg.state.index >= VHOST_VRING_MAX_N)
588 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200589 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
590 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200591 goto close_socket;
592 }
593
594 if (msg.size < sizeof (msg.addr))
595 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200596 vu_log_debug (vui, "vhost message is too short (%d < %d)",
597 msg.size, sizeof (msg.addr));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200598 goto close_socket;
599 }
600
601 vui->vrings[msg.state.index].desc = (vring_desc_t *)
602 map_user_mem (vui, msg.addr.desc_user_addr);
603 vui->vrings[msg.state.index].used = (vring_used_t *)
604 map_user_mem (vui, msg.addr.used_user_addr);
605 vui->vrings[msg.state.index].avail = (vring_avail_t *)
606 map_user_mem (vui, msg.addr.avail_user_addr);
607
608 if ((vui->vrings[msg.state.index].desc == NULL) ||
609 (vui->vrings[msg.state.index].used == NULL) ||
610 (vui->vrings[msg.state.index].avail == NULL))
611 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200612 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
613 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200614 goto close_socket;
615 }
616
617 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
618 vui->vrings[msg.state.index].log_used =
619 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
620
621 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
622 the ring is initialized in an enabled state. */
623 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
624 {
625 vui->vrings[msg.state.index].enabled = 1;
626 }
627
628 vui->vrings[msg.state.index].last_used_idx =
629 vui->vrings[msg.state.index].last_avail_idx =
630 vui->vrings[msg.state.index].used->idx;
631
632 /* tell driver that we don't want interrupts */
633 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
634 break;
635
636 case VHOST_USER_SET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200637 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200638 break;
639
640 case VHOST_USER_RESET_OWNER:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200641 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
642 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200643 break;
644
645 case VHOST_USER_SET_VRING_CALL:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200646 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
647 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200648
649 q = (u8) (msg.u64 & 0xFF);
650
651 /* if there is old fd, delete and close it */
652 if (vui->vrings[q].callfd_idx != ~0)
653 {
654 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
655 vui->vrings[q].callfd_idx);
656 clib_file_del (&file_main, uf);
657 vui->vrings[q].callfd_idx = ~0;
658 }
659
660 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
661 {
662 if (number_of_fds != 1)
663 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200664 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200665 goto close_socket;
666 }
667
668 template.read_function = vhost_user_callfd_read_ready;
669 template.file_descriptor = fds[0];
670 template.private_data =
671 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
672 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
673 }
674 else
675 vui->vrings[q].callfd_idx = ~0;
676 break;
677
678 case VHOST_USER_SET_VRING_KICK:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200679 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
680 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200681
682 q = (u8) (msg.u64 & 0xFF);
683
684 if (vui->vrings[q].kickfd_idx != ~0)
685 {
686 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
687 vui->vrings[q].kickfd_idx);
688 clib_file_del (&file_main, uf);
689 vui->vrings[q].kickfd_idx = ~0;
690 }
691
692 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
693 {
694 if (number_of_fds != 1)
695 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200696 vu_log_debug (vui, "More than one fd received !");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200697 goto close_socket;
698 }
699
700 template.read_function = vhost_user_kickfd_read_ready;
701 template.file_descriptor = fds[0];
702 template.private_data =
703 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
704 q;
705 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
706 }
707 else
708 {
709 //When no kickfd is set, the queue is initialized as started
710 vui->vrings[q].kickfd_idx = ~0;
711 vui->vrings[q].started = 1;
712 }
713
714 break;
715
716 case VHOST_USER_SET_VRING_ERR:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200717 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
718 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200719
720 q = (u8) (msg.u64 & 0xFF);
721
722 if (vui->vrings[q].errfd != -1)
723 close (vui->vrings[q].errfd);
724
725 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
726 {
727 if (number_of_fds != 1)
728 goto close_socket;
729
730 vui->vrings[q].errfd = fds[0];
731 }
732 else
733 vui->vrings[q].errfd = -1;
734
735 break;
736
737 case VHOST_USER_SET_VRING_BASE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200738 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
739 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200740
741 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
742 break;
743
744 case VHOST_USER_GET_VRING_BASE:
745 if (msg.state.index >= VHOST_VRING_MAX_N)
746 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200747 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
748 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200749 goto close_socket;
750 }
751
752 /*
753 * Copy last_avail_idx from the vring before closing it because
754 * closing the vring also initializes the vring last_avail_idx
755 */
756 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
757 msg.flags |= 4;
758 msg.size = sizeof (msg.state);
759
760 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
761 vhost_user_vring_close (vui, msg.state.index);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200762 vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
763 vui->hw_if_index, msg.state.index, msg.state.num);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200764 break;
765
766 case VHOST_USER_NONE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200767 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200768
769 break;
770
771 case VHOST_USER_SET_LOG_BASE:
772 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200773 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
774 vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200775
776 if (msg.size != sizeof (msg.log))
777 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200778 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
779 " %d instead of %d", msg.size, sizeof (msg.log));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200780 goto close_socket;
781 }
782
783 if (!
784 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
785 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200786 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
787 "VHOST_USER_SET_LOG_BASE received");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200788 goto close_socket;
789 }
790
791 fd = fds[0];
792 /* align size to page */
793 long page_sz = get_huge_page_size (fd);
794 ssize_t map_sz =
795 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
796
797 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
798 MAP_SHARED, fd, 0);
799
Jerome Tollet2f54c272018-10-02 11:41:11 +0200800 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
801 "mapped 0x%lx", map_sz, msg.log.offset, fd,
802 vui->log_base_addr);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200803
804 if (vui->log_base_addr == MAP_FAILED)
805 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200806 vu_log_err (vui, "failed to map memory. errno is %d", errno);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200807 goto close_socket;
808 }
809
810 vui->log_base_addr += msg.log.offset;
811 vui->log_size = msg.log.size;
812
813 msg.flags |= 4;
814 msg.size = sizeof (msg.u64);
815
816 break;
817 }
818
819 case VHOST_USER_SET_LOG_FD:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200820 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200821
822 break;
823
824 case VHOST_USER_GET_PROTOCOL_FEATURES:
825 msg.flags |= 4;
826 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
827 (1 << VHOST_USER_PROTOCOL_F_MQ);
828 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200829 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
830 "reply 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200831 break;
832
833 case VHOST_USER_SET_PROTOCOL_FEATURES:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200834 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
835 "features 0x%016llx", vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200836
837 vui->protocol_features = msg.u64;
838
839 break;
840
841 case VHOST_USER_GET_QUEUE_NUM:
842 msg.flags |= 4;
843 msg.u64 = VHOST_VRING_MAX_N;
844 msg.size = sizeof (msg.u64);
Jerome Tollet2f54c272018-10-02 11:41:11 +0200845 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
846 vui->hw_if_index, msg.u64);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200847 break;
848
849 case VHOST_USER_SET_VRING_ENABLE:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200850 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
851 vui->hw_if_index, msg.state.num ? "enable" : "disable",
852 msg.state.index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200853 if (msg.state.index >= VHOST_VRING_MAX_N)
854 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200855 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
856 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200857 goto close_socket;
858 }
859
860 vui->vrings[msg.state.index].enabled = msg.state.num;
861 break;
862
863 default:
Jerome Tollet2f54c272018-10-02 11:41:11 +0200864 vu_log_debug (vui, "unknown vhost-user message %d received. "
865 "closing socket", msg.request);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200866 goto close_socket;
867 }
868
869 /* if we need to reply */
870 if (msg.flags & 4)
871 {
872 n =
873 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
874 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
875 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200876 vu_log_debug (vui, "could not send message response");
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200877 goto close_socket;
878 }
879 }
880
881 vhost_user_update_iface_state (vui);
882 vlib_worker_thread_barrier_release (vlib_get_main ());
883 return 0;
884
885close_socket:
886 vhost_user_if_disconnect (vui);
887 vhost_user_update_iface_state (vui);
888 vlib_worker_thread_barrier_release (vlib_get_main ());
889 return 0;
890}
891
892static clib_error_t *
893vhost_user_socket_error (clib_file_t * uf)
894{
895 vlib_main_t *vm = vlib_get_main ();
896 vhost_user_main_t *vum = &vhost_user_main;
897 vhost_user_intf_t *vui =
898 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
899
Jerome Tollet2f54c272018-10-02 11:41:11 +0200900 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200901 vlib_worker_thread_barrier_sync (vm);
902 vhost_user_if_disconnect (vui);
903 vhost_user_rx_thread_placement ();
904 vlib_worker_thread_barrier_release (vm);
905 return 0;
906}
907
908static clib_error_t *
909vhost_user_socksvr_accept_ready (clib_file_t * uf)
910{
911 int client_fd, client_len;
912 struct sockaddr_un client;
913 clib_file_t template = { 0 };
914 vhost_user_main_t *vum = &vhost_user_main;
915 vhost_user_intf_t *vui;
916
917 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
918
919 client_len = sizeof (client);
920 client_fd = accept (uf->file_descriptor,
921 (struct sockaddr *) &client,
922 (socklen_t *) & client_len);
923
924 if (client_fd < 0)
925 return clib_error_return_unix (0, "accept");
926
927 if (vui->clib_file_index != ~0)
928 {
Jerome Tollet2f54c272018-10-02 11:41:11 +0200929 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
930 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200931 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
932 }
933
Jerome Tollet2f54c272018-10-02 11:41:11 +0200934 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
935 vui->sw_if_index, client_fd);
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200936 template.read_function = vhost_user_socket_read;
937 template.error_function = vhost_user_socket_error;
938 template.file_descriptor = client_fd;
939 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
940 vui->clib_file_index = clib_file_add (&file_main, &template);
941 return 0;
942}
943
944static clib_error_t *
945vhost_user_init (vlib_main_t * vm)
946{
947 clib_error_t *error;
948 vhost_user_main_t *vum = &vhost_user_main;
949 vlib_thread_main_t *tm = vlib_get_thread_main ();
950
951 error = vlib_call_init_function (vm, ip4_init);
952 if (error)
953 return error;
954
Jerome Tollet2f54c272018-10-02 11:41:11 +0200955 vum->log_default = vlib_log_register_class ("vhost-user", 0);
956
Mohsin Kazmie7cde312018-06-26 17:20:11 +0200957 vum->coalesce_frames = 32;
958 vum->coalesce_time = 1e-3;
959
960 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
961
962 vhost_cpu_t *cpu;
963 vec_foreach (cpu, vum->cpus)
964 {
965 /* This is actually not necessary as validate already zeroes it
966 * Just keeping the loop here for later because I am lazy. */
967 cpu->rx_buffers_len = 0;
968 }
969
970 vum->random = random_default_seed ();
971
972 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
973
974 return 0;
975}
976
977VLIB_INIT_FUNCTION (vhost_user_init);
978
979static uword
980vhost_user_send_interrupt_process (vlib_main_t * vm,
981 vlib_node_runtime_t * rt, vlib_frame_t * f)
982{
983 vhost_user_intf_t *vui;
984 f64 timeout = 3153600000.0 /* 100 years */ ;
985 uword event_type, *event_data = 0;
986 vhost_user_main_t *vum = &vhost_user_main;
987 u16 *queue;
988 f64 now, poll_time_remaining;
989 f64 next_timeout;
990 u8 stop_timer = 0;
991
992 while (1)
993 {
994 poll_time_remaining =
995 vlib_process_wait_for_event_or_clock (vm, timeout);
996 event_type = vlib_process_get_events (vm, &event_data);
997 vec_reset_length (event_data);
998
999 /*
1000 * Use the remaining timeout if it is less than coalesce time to avoid
1001 * resetting the existing timer in the middle of expiration
1002 */
1003 timeout = poll_time_remaining;
1004 if (vlib_process_suspend_time_is_zero (timeout) ||
1005 (timeout > vum->coalesce_time))
1006 timeout = vum->coalesce_time;
1007
1008 now = vlib_time_now (vm);
1009 switch (event_type)
1010 {
1011 case VHOST_USER_EVENT_STOP_TIMER:
1012 stop_timer = 1;
1013 break;
1014
1015 case VHOST_USER_EVENT_START_TIMER:
1016 stop_timer = 0;
1017 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1018 break;
1019 /* fall through */
1020
1021 case ~0:
1022 /* *INDENT-OFF* */
1023 pool_foreach (vui, vum->vhost_user_interfaces, {
1024 next_timeout = timeout;
1025 vec_foreach (queue, vui->rx_queues)
1026 {
1027 vhost_user_vring_t *rxvq =
1028 &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
1029 vhost_user_vring_t *txvq =
1030 &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
1031
1032 if (txvq->n_since_last_int)
1033 {
1034 if (now >= txvq->int_deadline)
1035 vhost_user_send_call (vm, txvq);
1036 else
1037 next_timeout = txvq->int_deadline - now;
1038 }
1039
1040 if (rxvq->n_since_last_int)
1041 {
1042 if (now >= rxvq->int_deadline)
1043 vhost_user_send_call (vm, rxvq);
1044 else
1045 next_timeout = rxvq->int_deadline - now;
1046 }
1047
1048 if ((next_timeout < timeout) && (next_timeout > 0.0))
1049 timeout = next_timeout;
1050 }
1051 });
1052 /* *INDENT-ON* */
1053 break;
1054
1055 default:
1056 clib_warning ("BUG: unhandled event type %d", event_type);
1057 break;
1058 }
1059 /* No less than 1 millisecond */
1060 if (timeout < 1e-3)
1061 timeout = 1e-3;
1062 if (stop_timer)
1063 timeout = 3153600000.0;
1064 }
1065 return 0;
1066}
1067
1068/* *INDENT-OFF* */
1069VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1070 .function = vhost_user_send_interrupt_process,
1071 .type = VLIB_NODE_TYPE_PROCESS,
1072 .name = "vhost-user-send-interrupt-process",
1073};
1074/* *INDENT-ON* */
1075
1076static uword
1077vhost_user_process (vlib_main_t * vm,
1078 vlib_node_runtime_t * rt, vlib_frame_t * f)
1079{
1080 vhost_user_main_t *vum = &vhost_user_main;
1081 vhost_user_intf_t *vui;
1082 struct sockaddr_un sun;
1083 int sockfd;
1084 clib_file_t template = { 0 };
1085 f64 timeout = 3153600000.0 /* 100 years */ ;
1086 uword *event_data = 0;
1087
1088 sockfd = -1;
1089 sun.sun_family = AF_UNIX;
1090 template.read_function = vhost_user_socket_read;
1091 template.error_function = vhost_user_socket_error;
1092
1093 while (1)
1094 {
1095 vlib_process_wait_for_event_or_clock (vm, timeout);
1096 vlib_process_get_events (vm, &event_data);
1097 vec_reset_length (event_data);
1098
1099 timeout = 3.0;
1100
1101 /* *INDENT-OFF* */
1102 pool_foreach (vui, vum->vhost_user_interfaces, {
1103
1104 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1105 if (vui->clib_file_index == ~0)
1106 {
1107 if ((sockfd < 0) &&
1108 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1109 {
1110 /*
1111 * 1st time error or new error for this interface,
1112 * spit out the message and record the error
1113 */
1114 if (!vui->sock_errno || (vui->sock_errno != errno))
1115 {
1116 clib_unix_warning
1117 ("Error: Could not open unix socket for %s",
1118 vui->sock_filename);
1119 vui->sock_errno = errno;
1120 }
1121 continue;
1122 }
1123
1124 /* try to connect */
1125 strncpy (sun.sun_path, (char *) vui->sock_filename,
1126 sizeof (sun.sun_path) - 1);
1127
1128 /* Avoid hanging VPP if the other end does not accept */
1129 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1130 clib_unix_warning ("fcntl");
1131
1132 if (connect (sockfd, (struct sockaddr *) &sun,
1133 sizeof (struct sockaddr_un)) == 0)
1134 {
1135 /* Set the socket to blocking as it was before */
1136 if (fcntl(sockfd, F_SETFL, 0) < 0)
1137 clib_unix_warning ("fcntl2");
1138
1139 vui->sock_errno = 0;
1140 template.file_descriptor = sockfd;
1141 template.private_data =
1142 vui - vhost_user_main.vhost_user_interfaces;
1143 vui->clib_file_index = clib_file_add (&file_main, &template);
1144
1145 /* This sockfd is considered consumed */
1146 sockfd = -1;
1147 }
1148 else
1149 {
1150 vui->sock_errno = errno;
1151 }
1152 }
1153 else
1154 {
1155 /* check if socket is alive */
1156 int error = 0;
1157 socklen_t len = sizeof (error);
1158 int fd = UNIX_GET_FD(vui->clib_file_index);
1159 int retval =
1160 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1161
1162 if (retval)
1163 {
Jerome Tollet2f54c272018-10-02 11:41:11 +02001164 vu_log_debug (vui, "getsockopt returned %d", retval);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001165 vhost_user_if_disconnect (vui);
1166 }
1167 }
1168 }
1169 });
1170 /* *INDENT-ON* */
1171 }
1172 return 0;
1173}
1174
1175/* *INDENT-OFF* */
1176VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1177 .function = vhost_user_process,
1178 .type = VLIB_NODE_TYPE_PROCESS,
1179 .name = "vhost-user-process",
1180};
1181/* *INDENT-ON* */
1182
1183/**
1184 * Disables and reset interface structure.
1185 * It can then be either init again, or removed from used interfaces.
1186 */
1187static void
1188vhost_user_term_if (vhost_user_intf_t * vui)
1189{
1190 int q;
1191 vhost_user_main_t *vum = &vhost_user_main;
1192
1193 // disconnect interface sockets
1194 vhost_user_if_disconnect (vui);
1195 vhost_user_update_iface_state (vui);
1196
1197 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1198 {
1199 clib_mem_free ((void *) vui->vring_locks[q]);
1200 }
1201
1202 if (vui->unix_server_index != ~0)
1203 {
1204 //Close server socket
1205 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1206 vui->unix_server_index);
1207 clib_file_del (&file_main, uf);
1208 vui->unix_server_index = ~0;
1209 unlink (vui->sock_filename);
1210 }
1211
1212 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1213 &vui->if_index);
1214}
1215
1216int
1217vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1218{
1219 vhost_user_main_t *vum = &vhost_user_main;
1220 vhost_user_intf_t *vui;
1221 int rv = 0;
1222 vnet_hw_interface_t *hwif;
1223 u16 *queue;
1224
1225 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1226 hwif->dev_class_index != vhost_user_device_class.index)
1227 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1228
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001229 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1230
Jerome Tollet2f54c272018-10-02 11:41:11 +02001231 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1232 hwif->name, hwif->dev_instance);
1233
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001234 vec_foreach (queue, vui->rx_queues)
1235 {
1236 vhost_user_vring_t *txvq;
1237
1238 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
1239 if ((vum->ifq_count > 0) &&
1240 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1241 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1242 {
1243 vum->ifq_count--;
1244 // Stop the timer if there is no more interrupt interface/queue
1245 if ((vum->ifq_count == 0) &&
1246 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1247 {
1248 vlib_process_signal_event (vm,
1249 vhost_user_send_interrupt_node.index,
1250 VHOST_USER_EVENT_STOP_TIMER, 0);
1251 break;
1252 }
1253 }
1254 }
1255
1256 // Disable and reset interface
1257 vhost_user_term_if (vui);
1258
1259 // Reset renumbered iface
1260 if (hwif->dev_instance <
1261 vec_len (vum->show_dev_instance_by_real_dev_instance))
1262 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1263
1264 // Delete ethernet interface
1265 ethernet_delete_interface (vnm, vui->hw_if_index);
1266
1267 // Back to pool
1268 pool_put (vum->vhost_user_interfaces, vui);
1269
1270 return rv;
1271}
1272
1273static clib_error_t *
1274vhost_user_exit (vlib_main_t * vm)
1275{
1276 vnet_main_t *vnm = vnet_get_main ();
1277 vhost_user_main_t *vum = &vhost_user_main;
1278 vhost_user_intf_t *vui;
1279
1280 vlib_worker_thread_barrier_sync (vlib_get_main ());
1281 /* *INDENT-OFF* */
1282 pool_foreach (vui, vum->vhost_user_interfaces, {
1283 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1284 });
1285 /* *INDENT-ON* */
1286 vlib_worker_thread_barrier_release (vlib_get_main ());
1287 return 0;
1288}
1289
1290VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1291
1292/**
1293 * Open server unix socket on specified sock_filename.
1294 */
1295static int
1296vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1297{
1298 int rv = 0;
1299 struct sockaddr_un un = { };
1300 int fd;
1301 /* create listening socket */
1302 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1303 return VNET_API_ERROR_SYSCALL_ERROR_1;
1304
1305 un.sun_family = AF_UNIX;
1306 strncpy ((char *) un.sun_path, (char *) sock_filename,
1307 sizeof (un.sun_path) - 1);
1308
1309 /* remove if exists */
1310 unlink ((char *) sock_filename);
1311
1312 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1313 {
1314 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1315 goto error;
1316 }
1317
1318 if (listen (fd, 1) == -1)
1319 {
1320 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1321 goto error;
1322 }
1323
1324 *sock_fd = fd;
1325 return 0;
1326
1327error:
1328 close (fd);
1329 return rv;
1330}
1331
1332/**
1333 * Create ethernet interface for vhost user interface.
1334 */
1335static void
1336vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1337 vhost_user_intf_t * vui, u8 * hwaddress)
1338{
1339 vhost_user_main_t *vum = &vhost_user_main;
1340 u8 hwaddr[6];
1341 clib_error_t *error;
1342
1343 /* create hw and sw interface */
1344 if (hwaddress)
1345 {
1346 clib_memcpy (hwaddr, hwaddress, 6);
1347 }
1348 else
1349 {
1350 random_u32 (&vum->random);
1351 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1352 hwaddr[0] = 2;
1353 hwaddr[1] = 0xfe;
1354 }
1355
1356 error = ethernet_register_interface
1357 (vnm,
1358 vhost_user_device_class.index,
1359 vui - vum->vhost_user_interfaces /* device instance */ ,
1360 hwaddr /* ethernet address */ ,
1361 &vui->hw_if_index, 0 /* flag change */ );
1362
1363 if (error)
1364 clib_error_report (error);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001365}
1366
1367/*
1368 * Initialize vui with specified attributes
1369 */
1370static void
1371vhost_user_vui_init (vnet_main_t * vnm,
1372 vhost_user_intf_t * vui,
1373 int server_sock_fd,
1374 const char *sock_filename,
1375 u64 feature_mask, u32 * sw_if_index)
1376{
1377 vnet_sw_interface_t *sw;
1378 int q;
1379 vhost_user_main_t *vum = &vhost_user_main;
1380 vnet_hw_interface_t *hw;
1381
1382 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1383 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1384 if (server_sock_fd != -1)
1385 {
1386 clib_file_t template = { 0 };
1387 template.read_function = vhost_user_socksvr_accept_ready;
1388 template.file_descriptor = server_sock_fd;
1389 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1390 vui->unix_server_index = clib_file_add (&file_main, &template);
1391 }
1392 else
1393 {
1394 vui->unix_server_index = ~0;
1395 }
1396
1397 vui->sw_if_index = sw->sw_if_index;
1398 strncpy (vui->sock_filename, sock_filename,
1399 ARRAY_LEN (vui->sock_filename) - 1);
1400 vui->sock_errno = 0;
Juraj Slobodab192feb2018-10-01 12:42:07 +02001401 vui->is_ready = 0;
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001402 vui->feature_mask = feature_mask;
1403 vui->clib_file_index = ~0;
1404 vui->log_base_addr = 0;
1405 vui->if_index = vui - vum->vhost_user_interfaces;
1406 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1407 &vui->if_index, 0);
1408
1409 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1410 vhost_user_vring_init (vui, q);
1411
1412 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1413 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1414
1415 if (sw_if_index)
1416 *sw_if_index = vui->sw_if_index;
1417
1418 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1419 {
1420 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1421 CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -04001422 clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001423 }
1424
1425 vec_validate (vui->per_cpu_tx_qid,
1426 vlib_get_thread_main ()->n_vlib_mains - 1);
1427 vhost_user_tx_thread_placement (vui);
1428}
1429
1430int
1431vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1432 const char *sock_filename,
1433 u8 is_server,
1434 u32 * sw_if_index,
1435 u64 feature_mask,
1436 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1437{
1438 vhost_user_intf_t *vui = NULL;
1439 u32 sw_if_idx = ~0;
1440 int rv = 0;
1441 int server_sock_fd = -1;
1442 vhost_user_main_t *vum = &vhost_user_main;
1443 uword *if_index;
1444
1445 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1446 {
1447 return VNET_API_ERROR_INVALID_ARGUMENT;
1448 }
1449
1450 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1451 if (if_index)
1452 {
1453 if (sw_if_index)
1454 {
1455 vui = &vum->vhost_user_interfaces[*if_index];
1456 *sw_if_index = vui->sw_if_index;
1457 }
1458 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1459 }
1460
1461 if (is_server)
1462 {
1463 if ((rv =
1464 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1465 {
1466 return rv;
1467 }
1468 }
1469
1470 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1471
1472 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1473 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1474 feature_mask, &sw_if_idx);
Juraj Sloboda83c46a22018-09-28 12:04:26 +02001475 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001476
1477 if (renumber)
1478 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1479
1480 if (sw_if_index)
1481 *sw_if_index = sw_if_idx;
1482
1483 // Process node must connect
1484 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1485
1486 return rv;
1487}
1488
1489int
1490vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1491 const char *sock_filename,
1492 u8 is_server,
1493 u32 sw_if_index,
1494 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1495{
1496 vhost_user_main_t *vum = &vhost_user_main;
1497 vhost_user_intf_t *vui = NULL;
1498 u32 sw_if_idx = ~0;
1499 int server_sock_fd = -1;
1500 int rv = 0;
1501 vnet_hw_interface_t *hwif;
1502 uword *if_index;
1503
1504 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1505 hwif->dev_class_index != vhost_user_device_class.index)
1506 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1507
1508 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1509 return VNET_API_ERROR_INVALID_ARGUMENT;
1510
1511 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1512
1513 /*
1514 * Disallow changing the interface to have the same path name
1515 * as other interface
1516 */
1517 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1518 if (if_index && (*if_index != vui->if_index))
1519 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1520
1521 // First try to open server socket
1522 if (is_server)
1523 if ((rv = vhost_user_init_server_sock (sock_filename,
1524 &server_sock_fd)) != 0)
1525 return rv;
1526
1527 vhost_user_term_if (vui);
1528 vhost_user_vui_init (vnm, vui, server_sock_fd,
1529 sock_filename, feature_mask, &sw_if_idx);
1530
1531 if (renumber)
1532 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1533
1534 // Process node must connect
1535 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1536
1537 return rv;
1538}
1539
1540clib_error_t *
1541vhost_user_connect_command_fn (vlib_main_t * vm,
1542 unformat_input_t * input,
1543 vlib_cli_command_t * cmd)
1544{
1545 unformat_input_t _line_input, *line_input = &_line_input;
1546 u8 *sock_filename = NULL;
1547 u32 sw_if_index;
1548 u8 is_server = 0;
1549 u64 feature_mask = (u64) ~ (0ULL);
1550 u8 renumber = 0;
1551 u32 custom_dev_instance = ~0;
1552 u8 hwaddr[6];
1553 u8 *hw = NULL;
1554 clib_error_t *error = NULL;
1555
1556 /* Get a line of input. */
1557 if (!unformat_user (input, unformat_line_input, line_input))
1558 return 0;
1559
1560 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1561 {
1562 if (unformat (line_input, "socket %s", &sock_filename))
1563 ;
1564 else if (unformat (line_input, "server"))
1565 is_server = 1;
1566 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1567 ;
1568 else
1569 if (unformat
1570 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1571 hw = hwaddr;
1572 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1573 {
1574 renumber = 1;
1575 }
1576 else
1577 {
1578 error = clib_error_return (0, "unknown input `%U'",
1579 format_unformat_error, line_input);
1580 goto done;
1581 }
1582 }
1583
1584 vnet_main_t *vnm = vnet_get_main ();
1585
1586 int rv;
1587 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1588 is_server, &sw_if_index, feature_mask,
1589 renumber, custom_dev_instance, hw)))
1590 {
1591 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1592 goto done;
1593 }
1594
1595 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1596 sw_if_index);
1597
1598done:
1599 vec_free (sock_filename);
1600 unformat_free (line_input);
1601
1602 return error;
1603}
1604
1605clib_error_t *
1606vhost_user_delete_command_fn (vlib_main_t * vm,
1607 unformat_input_t * input,
1608 vlib_cli_command_t * cmd)
1609{
1610 unformat_input_t _line_input, *line_input = &_line_input;
1611 u32 sw_if_index = ~0;
1612 vnet_main_t *vnm = vnet_get_main ();
1613 clib_error_t *error = NULL;
1614
1615 /* Get a line of input. */
1616 if (!unformat_user (input, unformat_line_input, line_input))
1617 return 0;
1618
1619 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1620 {
1621 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1622 ;
1623 else if (unformat
1624 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1625 &sw_if_index))
1626 {
1627 vnet_hw_interface_t *hwif =
1628 vnet_get_sup_hw_interface (vnm, sw_if_index);
1629 if (hwif == NULL ||
1630 vhost_user_device_class.index != hwif->dev_class_index)
1631 {
1632 error = clib_error_return (0, "Not a vhost interface");
1633 goto done;
1634 }
1635 }
1636 else
1637 {
1638 error = clib_error_return (0, "unknown input `%U'",
1639 format_unformat_error, line_input);
1640 goto done;
1641 }
1642 }
1643
1644 vhost_user_delete_if (vnm, vm, sw_if_index);
1645
1646done:
1647 unformat_free (line_input);
1648
1649 return error;
1650}
1651
1652int
1653vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1654 vhost_user_intf_details_t ** out_vuids)
1655{
1656 int rv = 0;
1657 vhost_user_main_t *vum = &vhost_user_main;
1658 vhost_user_intf_t *vui;
1659 vhost_user_intf_details_t *r_vuids = NULL;
1660 vhost_user_intf_details_t *vuid = NULL;
1661 u32 *hw_if_indices = 0;
1662 vnet_hw_interface_t *hi;
1663 u8 *s = NULL;
1664 int i;
1665
1666 if (!out_vuids)
1667 return -1;
1668
1669 pool_foreach (vui, vum->vhost_user_interfaces,
1670 vec_add1 (hw_if_indices, vui->hw_if_index);
1671 );
1672
1673 for (i = 0; i < vec_len (hw_if_indices); i++)
1674 {
1675 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1676 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1677
1678 vec_add2 (r_vuids, vuid, 1);
1679 vuid->sw_if_index = vui->sw_if_index;
1680 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1681 vuid->features = vui->features;
1682 vuid->num_regions = vui->nregions;
1683 vuid->is_server = vui->unix_server_index != ~0;
1684 vuid->sock_errno = vui->sock_errno;
1685 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1686 sizeof (vuid->sock_filename));
1687 vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1688 s = format (s, "%v%c", hi->name, 0);
1689
1690 strncpy ((char *) vuid->if_name, (char *) s,
1691 ARRAY_LEN (vuid->if_name) - 1);
1692 _vec_len (s) = 0;
1693 }
1694
1695 vec_free (s);
1696 vec_free (hw_if_indices);
1697
1698 *out_vuids = r_vuids;
1699
1700 return rv;
1701}
1702
1703clib_error_t *
1704show_vhost_user_command_fn (vlib_main_t * vm,
1705 unformat_input_t * input,
1706 vlib_cli_command_t * cmd)
1707{
1708 clib_error_t *error = 0;
1709 vnet_main_t *vnm = vnet_get_main ();
1710 vhost_user_main_t *vum = &vhost_user_main;
1711 vhost_user_intf_t *vui;
1712 u32 hw_if_index, *hw_if_indices = 0;
1713 vnet_hw_interface_t *hi;
1714 u16 *queue;
1715 u32 ci;
1716 int i, j, q;
1717 int show_descr = 0;
1718 struct feat_struct
1719 {
1720 u8 bit;
1721 char *str;
1722 };
1723 struct feat_struct *feat_entry;
1724
1725 static struct feat_struct feat_array[] = {
1726#define _(s,b) { .str = #s, .bit = b, },
1727 foreach_virtio_net_feature
1728#undef _
1729 {.str = NULL}
1730 };
1731
1732#define foreach_protocol_feature \
1733 _(VHOST_USER_PROTOCOL_F_MQ) \
1734 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1735
1736 static struct feat_struct proto_feat_array[] = {
1737#define _(s) { .str = #s, .bit = s},
1738 foreach_protocol_feature
1739#undef _
1740 {.str = NULL}
1741 };
1742
1743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1744 {
1745 if (unformat
1746 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1747 {
1748 hi = vnet_get_hw_interface (vnm, hw_if_index);
1749 if (vhost_user_device_class.index != hi->dev_class_index)
1750 {
1751 error = clib_error_return (0, "unknown input `%U'",
1752 format_unformat_error, input);
1753 goto done;
1754 }
1755 vec_add1 (hw_if_indices, hw_if_index);
1756 }
1757 else if (unformat (input, "descriptors") || unformat (input, "desc"))
1758 show_descr = 1;
1759 else
1760 {
1761 error = clib_error_return (0, "unknown input `%U'",
1762 format_unformat_error, input);
1763 goto done;
1764 }
1765 }
1766 if (vec_len (hw_if_indices) == 0)
1767 {
1768 pool_foreach (vui, vum->vhost_user_interfaces,
1769 vec_add1 (hw_if_indices, vui->hw_if_index);
1770 );
1771 }
1772 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1773 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1774 vum->coalesce_frames, vum->coalesce_time);
1775 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
1776 vum->ifq_count);
1777
1778 for (i = 0; i < vec_len (hw_if_indices); i++)
1779 {
1780 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1781 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Steven877ad142018-09-20 11:50:35 -07001782 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1783 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1784 hw_if_indices[i]);
Mohsin Kazmie7cde312018-06-26 17:20:11 +02001785
1786 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1787 " features mask (0x%llx): \n"
1788 " features (0x%llx): \n",
1789 vui->virtio_net_hdr_sz, vui->feature_mask,
1790 vui->features);
1791
1792 feat_entry = (struct feat_struct *) &feat_array;
1793 while (feat_entry->str)
1794 {
1795 if (vui->features & (1ULL << feat_entry->bit))
1796 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1797 feat_entry->bit);
1798 feat_entry++;
1799 }
1800
1801 vlib_cli_output (vm, " protocol features (0x%llx)",
1802 vui->protocol_features);
1803 feat_entry = (struct feat_struct *) &proto_feat_array;
1804 while (feat_entry->str)
1805 {
1806 if (vui->protocol_features & (1ULL << feat_entry->bit))
1807 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1808 feat_entry->bit);
1809 feat_entry++;
1810 }
1811
1812 vlib_cli_output (vm, "\n");
1813
1814 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1815 vui->sock_filename,
1816 (vui->unix_server_index != ~0) ? "server" : "client",
1817 strerror (vui->sock_errno));
1818
1819 vlib_cli_output (vm, " rx placement: ");
1820
1821 vec_foreach (queue, vui->rx_queues)
1822 {
1823 vnet_main_t *vnm = vnet_get_main ();
1824 uword thread_index;
1825 vnet_hw_interface_rx_mode mode;
1826
1827 thread_index = vnet_get_device_input_thread_index (vnm,
1828 vui->hw_if_index,
1829 *queue);
1830 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
1831 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1832 thread_index, VHOST_VRING_IDX_TX (*queue),
1833 format_vnet_hw_interface_rx_mode, mode);
1834 }
1835
1836 vlib_cli_output (vm, " tx placement: %s\n",
1837 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1838
1839 vec_foreach_index (ci, vui->per_cpu_tx_qid)
1840 {
1841 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1842 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1843 }
1844
1845 vlib_cli_output (vm, "\n");
1846
1847 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1848
1849 if (vui->nregions)
1850 {
1851 vlib_cli_output (vm,
1852 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1853 vlib_cli_output (vm,
1854 " ====== ===== ================== ================== ================== ================== ==================\n");
1855 }
1856 for (j = 0; j < vui->nregions; j++)
1857 {
1858 vlib_cli_output (vm,
1859 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1860 j, vui->region_mmap_fd[j],
1861 vui->regions[j].guest_phys_addr,
1862 vui->regions[j].memory_size,
1863 vui->regions[j].userspace_addr,
1864 vui->regions[j].mmap_offset,
1865 pointer_to_uword (vui->region_mmap_addr[j]));
1866 }
1867 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1868 {
1869 if (!vui->vrings[q].started)
1870 continue;
1871
1872 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1873 (q & 1) ? "RX" : "TX",
1874 vui->vrings[q].enabled ? "" : " disabled");
1875
1876 vlib_cli_output (vm,
1877 " qsz %d last_avail_idx %d last_used_idx %d\n",
1878 vui->vrings[q].qsz_mask + 1,
1879 vui->vrings[q].last_avail_idx,
1880 vui->vrings[q].last_used_idx);
1881
1882 if (vui->vrings[q].avail && vui->vrings[q].used)
1883 vlib_cli_output (vm,
1884 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1885 vui->vrings[q].avail->flags,
1886 vui->vrings[q].avail->idx,
1887 vui->vrings[q].used->flags,
1888 vui->vrings[q].used->idx);
1889
1890 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1891 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1892 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1893 kickfd, callfd, vui->vrings[q].errfd);
1894
1895 if (show_descr)
1896 {
1897 vlib_cli_output (vm, "\n descriptor table:\n");
1898 vlib_cli_output (vm,
1899 " id addr len flags next user_addr\n");
1900 vlib_cli_output (vm,
1901 " ===== ================== ===== ====== ===== ==================\n");
1902 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1903 {
1904 u32 mem_hint = 0;
1905 vlib_cli_output (vm,
1906 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1907 j, vui->vrings[q].desc[j].addr,
1908 vui->vrings[q].desc[j].len,
1909 vui->vrings[q].desc[j].flags,
1910 vui->vrings[q].desc[j].next,
1911 pointer_to_uword (map_guest_mem
1912 (vui,
1913 vui->vrings[q].desc[j].
1914 addr, &mem_hint)));
1915 }
1916 }
1917 }
1918 vlib_cli_output (vm, "\n");
1919 }
1920done:
1921 vec_free (hw_if_indices);
1922 return error;
1923}
1924
1925/*
1926 * CLI functions
1927 */
1928
1929/*?
1930 * Create a vHost User interface. Once created, a new virtual interface
1931 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
1932 * is the next free index.
1933 *
1934 * There are several parameters associated with a vHost interface:
1935 *
1936 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
1937 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
1938 * create the socket if it does not already exist. If in '<em>client</em>' mode,
1939 * hypervisor will create the socket if it does not already exist. The VPP code
1940 * is indifferent to the file location. However, if SELinux is enabled, then the
1941 * socket needs to be created in '<em>/var/run/vpp/</em>'.
1942 *
1943 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
1944 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
1945 * mode, the VM can be reset without tearing down the vHost Interface. In
1946 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
1947 * tearing down the vHost Interface.
1948 *
1949 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
1950 * startup. <b>This is intended for degugging only.</b> It is recommended that this
1951 * parameter not be used except by experienced users. By default, all supported
1952 * features will be advertised. Otherwise, provide the set of features desired.
1953 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
1954 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
1955 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
1956 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
1957 * - 0x004000000 (26) - VHOST_F_LOG_ALL
1958 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
1959 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
1960 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
1961 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
1962 *
1963 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
1964 * X:X:X:X:X:X unix or X.X.X cisco format.
1965 *
1966 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
1967 * in the name to be specified. If instance already exists, name will be used
1968 * anyway and multiple instances will have the same name. Use with caution.
1969 *
1970 * @cliexpar
1971 * Example of how to create a vhost interface with VPP as the client and all features enabled:
1972 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
1973 * VirtualEthernet0/0/0
1974 * @cliexend
1975 * Example of how to create a vhost interface with VPP as the server and with just
1976 * multiple queues enabled:
1977 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
1978 * VirtualEthernet0/0/1
1979 * @cliexend
1980 * Once the vHost interface is created, enable the interface using:
1981 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
1982?*/
1983/* *INDENT-OFF* */
1984VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
1985 .path = "create vhost-user",
1986 .short_help = "create vhost-user socket <socket-filename> [server] "
1987 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
1988 .function = vhost_user_connect_command_fn,
1989};
1990/* *INDENT-ON* */
1991
1992/*?
1993 * Delete a vHost User interface using the interface name or the
1994 * software interface index. Use the '<em>show interface</em>'
1995 * command to determine the software interface index. On deletion,
1996 * the linux socket will not be deleted.
1997 *
1998 * @cliexpar
1999 * Example of how to delete a vhost interface by name:
2000 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2001 * Example of how to delete a vhost interface by software interface index:
2002 * @cliexcmd{delete vhost-user sw_if_index 1}
2003?*/
2004/* *INDENT-OFF* */
2005VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2006 .path = "delete vhost-user",
2007 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2008 .function = vhost_user_delete_command_fn,
2009};
2010
2011/*?
2012 * Display the attributes of a single vHost User interface (provide interface
2013 * name), multiple vHost User interfaces (provide a list of interface names seperated
2014 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2015 * vHost interfaces).
2016 *
2017 * @cliexpar
2018 * @parblock
2019 * Example of how to display a vhost interface:
2020 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2021 * Virtio vhost-user interfaces
2022 * Global:
2023 * coalesce frames 32 time 1e-3
2024 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2025 * virtio_net_hdr_sz 12
2026 * features mask (0xffffffffffffffff):
2027 * features (0x50408000):
2028 * VIRTIO_NET_F_MRG_RXBUF (15)
2029 * VIRTIO_NET_F_MQ (22)
2030 * VIRTIO_F_INDIRECT_DESC (28)
2031 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2032 * protocol features (0x3)
2033 * VHOST_USER_PROTOCOL_F_MQ (0)
2034 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2035 *
2036 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2037 *
2038 * rx placement:
2039 * thread 1 on vring 1
2040 * thread 1 on vring 5
2041 * thread 2 on vring 3
2042 * thread 2 on vring 7
2043 * tx placement: spin-lock
2044 * thread 0 on vring 0
2045 * thread 1 on vring 2
2046 * thread 2 on vring 0
2047 *
2048 * Memory regions (total 2)
2049 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2050 * ====== ===== ================== ================== ================== ================== ==================
2051 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2052 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2053 *
2054 * Virtqueue 0 (TX)
2055 * qsz 256 last_avail_idx 0 last_used_idx 0
2056 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2057 * kickfd 62 callfd 64 errfd -1
2058 *
2059 * Virtqueue 1 (RX)
2060 * qsz 256 last_avail_idx 0 last_used_idx 0
2061 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2062 * kickfd 65 callfd 66 errfd -1
2063 *
2064 * Virtqueue 2 (TX)
2065 * qsz 256 last_avail_idx 0 last_used_idx 0
2066 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2067 * kickfd 63 callfd 70 errfd -1
2068 *
2069 * Virtqueue 3 (RX)
2070 * qsz 256 last_avail_idx 0 last_used_idx 0
2071 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2072 * kickfd 72 callfd 74 errfd -1
2073 *
2074 * Virtqueue 4 (TX disabled)
2075 * qsz 256 last_avail_idx 0 last_used_idx 0
2076 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2077 * kickfd 76 callfd 78 errfd -1
2078 *
2079 * Virtqueue 5 (RX disabled)
2080 * qsz 256 last_avail_idx 0 last_used_idx 0
2081 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2082 * kickfd 80 callfd 82 errfd -1
2083 *
2084 * Virtqueue 6 (TX disabled)
2085 * qsz 256 last_avail_idx 0 last_used_idx 0
2086 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2087 * kickfd 84 callfd 86 errfd -1
2088 *
2089 * Virtqueue 7 (RX disabled)
2090 * qsz 256 last_avail_idx 0 last_used_idx 0
2091 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2092 * kickfd 88 callfd 90 errfd -1
2093 *
2094 * @cliexend
2095 *
2096 * The optional '<em>descriptors</em>' parameter will display the same output as
2097 * the previous example but will include the descriptor table for each queue.
2098 * The output is truncated below:
2099 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2100 * Virtio vhost-user interfaces
2101 * Global:
2102 * coalesce frames 32 time 1e-3
2103 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2104 * virtio_net_hdr_sz 12
2105 * features mask (0xffffffffffffffff):
2106 * features (0x50408000):
2107 * VIRTIO_NET_F_MRG_RXBUF (15)
2108 * VIRTIO_NET_F_MQ (22)
2109 * :
2110 * Virtqueue 0 (TX)
2111 * qsz 256 last_avail_idx 0 last_used_idx 0
2112 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2113 * kickfd 62 callfd 64 errfd -1
2114 *
2115 * descriptor table:
2116 * id addr len flags next user_addr
2117 * ===== ================== ===== ====== ===== ==================
2118 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2119 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2120 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2121 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2122 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2123 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2124 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2125 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2126 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2127 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2128 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2129 * :
2130 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2131 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2132 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2133 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2134 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2135 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2136 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2137 *
2138 * Virtqueue 1 (RX)
2139 * qsz 256 last_avail_idx 0 last_used_idx 0
2140 * :
2141 * @cliexend
2142 * @endparblock
2143?*/
2144/* *INDENT-OFF* */
2145VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2146 .path = "show vhost-user",
2147 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2148 .function = show_vhost_user_command_fn,
2149};
2150/* *INDENT-ON* */
2151
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002152
2153static clib_error_t *
2154vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2155{
2156 vhost_user_main_t *vum = &vhost_user_main;
2157
2158 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2159 {
2160 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2161 ;
2162 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2163 ;
2164 else if (unformat (input, "dont-dump-memory"))
2165 vum->dont_dump_vhost_user_memory = 1;
2166 else
2167 return clib_error_return (0, "unknown input `%U'",
2168 format_unformat_error, input);
2169 }
2170
2171 return 0;
2172}
2173
Mohsin Kazmie7cde312018-06-26 17:20:11 +02002174/* vhost-user { ... } configuration. */
2175VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2176
2177void
2178vhost_user_unmap_all (void)
2179{
2180 vhost_user_main_t *vum = &vhost_user_main;
2181 vhost_user_intf_t *vui;
2182
2183 if (vum->dont_dump_vhost_user_memory)
2184 {
2185 pool_foreach (vui, vum->vhost_user_interfaces,
2186 unmap_all_mem_regions (vui);
2187 );
2188 }
2189}
2190
2191/*
2192 * fd.io coding-style-patch-verification: ON
2193 *
2194 * Local Variables:
2195 * eval: (c-set-style "gnu")
2196 * End:
2197 */