2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ip/ip.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
42 #include <vnet/devices/virtio/vhost_user.h>
43 #include <vnet/devices/virtio/vhost_user_inline.h>
47 * @brief vHost User Device Driver.
49 * This file contains the source code for vHost User interface.
53 vlib_node_registration_t vhost_user_send_interrupt_node;
56 vhost_user_main_t vhost_user_main = {
60 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
66 get_huge_page_size (int fd)
74 unmap_all_mem_regions (vhost_user_intf_t * vui)
77 for (i = 0; i < vui->nregions; i++)
79 if (vui->region_mmap_addr[i] != MAP_FAILED)
82 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
84 ssize_t map_sz = (vui->regions[i].memory_size +
85 vui->regions[i].mmap_offset +
86 page_sz - 1) & ~(page_sz - 1);
89 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
93 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
94 vui->region_mmap_addr[i], map_sz, page_sz);
96 vui->region_mmap_addr[i] = MAP_FAILED;
100 clib_warning ("failed to unmap memory region (errno %d)",
103 close (vui->region_mmap_fd[i]);
110 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
112 //Let's try to assign one queue to each thread
114 u32 thread_index = 0;
115 vui->use_tx_spinlock = 0;
118 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
120 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
121 if (!rxvq->started || !rxvq->enabled)
124 vui->per_cpu_tx_qid[thread_index] = qid;
126 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
129 //We need to loop, meaning the spinlock has to be used
130 vui->use_tx_spinlock = 1;
131 if (thread_index == 0)
133 //Could not find a single valid one
134 for (thread_index = 0;
135 thread_index < vlib_get_thread_main ()->n_vlib_mains;
138 vui->per_cpu_tx_qid[thread_index] = 0;
146 * @brief Unassign existing interface/queue to thread mappings and re-assign
147 * new interface/queue to thread mappings
150 vhost_user_rx_thread_placement ()
152 vhost_user_main_t *vum = &vhost_user_main;
153 vhost_user_intf_t *vui;
154 vhost_user_vring_t *txvq;
155 vnet_main_t *vnm = vnet_get_main ();
160 // Scrap all existing mappings for all interfaces/queues
162 pool_foreach (vui, vum->vhost_user_interfaces, {
163 vec_foreach (queue, vui->rx_queues)
165 rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
168 clib_warning ("Warning: unable to unassign interface %d, "
169 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
171 vec_reset_length (vui->rx_queues);
175 // Create the rx_queues for all interfaces
177 pool_foreach (vui, vum->vhost_user_interfaces, {
178 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
180 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
183 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
184 /* Set polling as the default */
185 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
186 vec_add1 (vui->rx_queues, qid);
192 // Assign new mappings for all interfaces/queues
194 pool_foreach (vui, vum->vhost_user_interfaces, {
195 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
196 vhost_user_input_node.index);
197 vec_foreach (queue, vui->rx_queues)
199 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
201 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
202 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
205 clib_warning ("Warning: unable to set rx mode for interface %d, "
206 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
212 /** @brief Returns whether at least one TX and one RX vring are enabled */
213 static_always_inline int
214 vhost_user_intf_ready (vhost_user_intf_t * vui)
216 int i, found[2] = { }; //RX + TX
218 for (i = 0; i < VHOST_VRING_MAX_N; i++)
219 if (vui->vrings[i].started && vui->vrings[i].enabled)
222 return found[0] && found[1];
226 vhost_user_update_iface_state (vhost_user_intf_t * vui)
228 /* if we have pointers to descriptor table, go up */
229 int is_up = vhost_user_intf_ready (vui);
230 if (is_up != vui->is_up)
232 DBG_SOCK ("interface %d %s", vui->sw_if_index,
233 is_up ? "ready" : "down");
234 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
235 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
239 vhost_user_rx_thread_placement ();
240 vhost_user_tx_thread_placement (vui);
244 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
247 vnet_main_t *vnm = vnet_get_main ();
251 /* Only care about the odd number, or TX, virtqueue */
254 if (vhost_user_intf_ready (vui))
255 // qid >> 1 is to convert virtqueue number to vring queue index
256 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
259 static clib_error_t *
260 vhost_user_callfd_read_ready (clib_file_t * uf)
262 __attribute__ ((unused)) int n;
265 n = read (uf->file_descriptor, ((char *) &buff), 8);
270 static clib_error_t *
271 vhost_user_kickfd_read_ready (clib_file_t * uf)
273 __attribute__ ((unused)) int n;
275 vhost_user_intf_t *vui =
276 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
277 uf->private_data >> 8);
278 u32 qid = uf->private_data & 0xff;
280 n = read (uf->file_descriptor, ((char *) &buff), 8);
281 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
282 if (!vui->vrings[qid].started ||
283 (vhost_user_intf_ready (vui) != vui->is_up))
285 vlib_worker_thread_barrier_sync (vlib_get_main ());
286 vui->vrings[qid].started = 1;
287 vhost_user_update_iface_state (vui);
288 vlib_worker_thread_barrier_release (vlib_get_main ());
291 vhost_user_set_interrupt_pending (vui, uf->private_data);
295 static_always_inline void
296 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
298 vhost_user_vring_t *vring = &vui->vrings[qid];
299 memset (vring, 0, sizeof (*vring));
300 vring->kickfd_idx = ~0;
301 vring->callfd_idx = ~0;
305 * We have a bug with some qemu 2.5, and this may be a fix.
306 * Feel like interpretation holy text, but this is from vhost-user.txt.
308 * One queue pair is enabled initially. More queues are enabled
309 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
311 * Don't know who's right, but this is what DPDK does.
313 if (qid == 0 || qid == 1)
317 static_always_inline void
318 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
320 vhost_user_vring_t *vring = &vui->vrings[qid];
321 if (vring->kickfd_idx != ~0)
323 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
325 clib_file_del (&file_main, uf);
326 vring->kickfd_idx = ~0;
328 if (vring->callfd_idx != ~0)
330 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
332 clib_file_del (&file_main, uf);
333 vring->callfd_idx = ~0;
335 if (vring->errfd != -1)
337 close (vring->errfd);
340 vhost_user_vring_init (vui, qid);
343 static_always_inline void
344 vhost_user_if_disconnect (vhost_user_intf_t * vui)
346 vnet_main_t *vnm = vnet_get_main ();
349 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
351 if (vui->clib_file_index != ~0)
353 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
354 vui->clib_file_index = ~0;
359 for (q = 0; q < VHOST_VRING_MAX_N; q++)
360 vhost_user_vring_close (vui, q);
362 unmap_all_mem_regions (vui);
363 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
366 static clib_error_t *
367 vhost_user_socket_read (clib_file_t * uf)
370 int fd, number_of_fds = 0;
371 int fds[VHOST_MEMORY_MAX_NREGIONS];
372 vhost_user_msg_t msg;
375 vhost_user_main_t *vum = &vhost_user_main;
376 vhost_user_intf_t *vui;
377 struct cmsghdr *cmsg;
379 clib_file_t template = { 0 };
380 vnet_main_t *vnm = vnet_get_main ();
382 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
384 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
386 memset (&mh, 0, sizeof (mh));
387 memset (control, 0, sizeof (control));
389 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
392 /* set the payload */
393 iov[0].iov_base = (void *) &msg;
394 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
398 mh.msg_control = control;
399 mh.msg_controllen = sizeof (control);
401 n = recvmsg (uf->file_descriptor, &mh, 0);
403 /* Stop workers to avoid end of the world */
404 vlib_worker_thread_barrier_sync (vlib_get_main ());
406 if (n != VHOST_USER_MSG_HDR_SZ)
410 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
414 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
415 n, VHOST_USER_MSG_HDR_SZ);
420 if (mh.msg_flags & MSG_CTRUNC)
422 DBG_SOCK ("MSG_CTRUNC is set");
426 cmsg = CMSG_FIRSTHDR (&mh);
428 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
429 (cmsg->cmsg_type == SCM_RIGHTS) &&
430 (cmsg->cmsg_len - CMSG_LEN (0) <=
431 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
433 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
434 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
437 /* version 1, no reply bit set */
438 if ((msg.flags & 7) != 1)
440 DBG_SOCK ("malformed message received. closing socket");
447 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
451 DBG_SOCK ("read failed %s", strerror (errno));
454 else if (rv != msg.size)
456 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
463 case VHOST_USER_GET_FEATURES:
465 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
466 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
467 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
468 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
469 (1ULL << FEAT_VHOST_F_LOG_ALL) |
470 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
471 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
472 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
473 (1ULL << FEAT_VIRTIO_F_VERSION_1);
474 msg.u64 &= vui->feature_mask;
475 msg.size = sizeof (msg.u64);
476 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
477 vui->hw_if_index, msg.u64);
480 case VHOST_USER_SET_FEATURES:
481 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
482 vui->hw_if_index, msg.u64);
484 vui->features = msg.u64;
487 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
488 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
489 vui->virtio_net_hdr_sz = 12;
491 vui->virtio_net_hdr_sz = 10;
494 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
496 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
497 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
500 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
501 vhost_user_vring_close(&vui->vrings[q]); */
505 case VHOST_USER_SET_MEM_TABLE:
506 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
507 vui->hw_if_index, msg.memory.nregions);
509 if ((msg.memory.nregions < 1) ||
510 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
513 DBG_SOCK ("number of mem regions must be between 1 and %i",
514 VHOST_MEMORY_MAX_NREGIONS);
519 if (msg.memory.nregions != number_of_fds)
521 DBG_SOCK ("each memory region must have FD");
524 unmap_all_mem_regions (vui);
525 for (i = 0; i < msg.memory.nregions; i++)
527 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
528 sizeof (vhost_user_memory_region_t));
530 long page_sz = get_huge_page_size (fds[i]);
532 /* align size to page */
533 ssize_t map_sz = (vui->regions[i].memory_size +
534 vui->regions[i].mmap_offset +
535 page_sz - 1) & ~(page_sz - 1);
537 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
538 MAP_SHARED, fds[i], 0);
539 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
540 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
541 vui->regions[i].memory_size;
544 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
545 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
548 if (vui->region_mmap_addr[i] == MAP_FAILED)
550 clib_warning ("failed to map memory. errno is %d", errno);
553 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
554 vui->region_mmap_fd[i] = fds[i];
560 case VHOST_USER_SET_VRING_NUM:
561 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
562 vui->hw_if_index, msg.state.index, msg.state.num);
564 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
565 (msg.state.num == 0) || /* it cannot be zero */
566 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
568 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
571 case VHOST_USER_SET_VRING_ADDR:
572 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
573 vui->hw_if_index, msg.state.index);
575 if (msg.state.index >= VHOST_VRING_MAX_N)
577 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
578 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
582 if (msg.size < sizeof (msg.addr))
584 DBG_SOCK ("vhost message is too short (%d < %d)",
585 msg.size, sizeof (msg.addr));
589 vui->vrings[msg.state.index].desc = (vring_desc_t *)
590 map_user_mem (vui, msg.addr.desc_user_addr);
591 vui->vrings[msg.state.index].used = (vring_used_t *)
592 map_user_mem (vui, msg.addr.used_user_addr);
593 vui->vrings[msg.state.index].avail = (vring_avail_t *)
594 map_user_mem (vui, msg.addr.avail_user_addr);
596 if ((vui->vrings[msg.state.index].desc == NULL) ||
597 (vui->vrings[msg.state.index].used == NULL) ||
598 (vui->vrings[msg.state.index].avail == NULL))
600 DBG_SOCK ("failed to map user memory for hw_if_index %d",
605 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
606 vui->vrings[msg.state.index].log_used =
607 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
609 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
610 the ring is initialized in an enabled state. */
611 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
613 vui->vrings[msg.state.index].enabled = 1;
616 vui->vrings[msg.state.index].last_used_idx =
617 vui->vrings[msg.state.index].last_avail_idx =
618 vui->vrings[msg.state.index].used->idx;
620 /* tell driver that we don't want interrupts */
621 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
624 case VHOST_USER_SET_OWNER:
625 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
628 case VHOST_USER_RESET_OWNER:
629 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
632 case VHOST_USER_SET_VRING_CALL:
633 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL %d",
634 vui->hw_if_index, msg.u64);
636 q = (u8) (msg.u64 & 0xFF);
638 /* if there is old fd, delete and close it */
639 if (vui->vrings[q].callfd_idx != ~0)
641 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
642 vui->vrings[q].callfd_idx);
643 clib_file_del (&file_main, uf);
644 vui->vrings[q].callfd_idx = ~0;
647 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
649 if (number_of_fds != 1)
651 DBG_SOCK ("More than one fd received !");
655 template.read_function = vhost_user_callfd_read_ready;
656 template.file_descriptor = fds[0];
657 template.private_data =
658 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
659 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
662 vui->vrings[q].callfd_idx = ~0;
665 case VHOST_USER_SET_VRING_KICK:
666 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK %d",
667 vui->hw_if_index, msg.u64);
669 q = (u8) (msg.u64 & 0xFF);
671 if (vui->vrings[q].kickfd_idx != ~0)
673 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
674 vui->vrings[q].kickfd_idx);
675 clib_file_del (&file_main, uf);
676 vui->vrings[q].kickfd_idx = ~0;
679 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
681 if (number_of_fds != 1)
683 DBG_SOCK ("More than one fd received !");
687 template.read_function = vhost_user_kickfd_read_ready;
688 template.file_descriptor = fds[0];
689 template.private_data =
690 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
692 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
696 //When no kickfd is set, the queue is initialized as started
697 vui->vrings[q].kickfd_idx = ~0;
698 vui->vrings[q].started = 1;
703 case VHOST_USER_SET_VRING_ERR:
704 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR %d",
705 vui->hw_if_index, msg.u64);
707 q = (u8) (msg.u64 & 0xFF);
709 if (vui->vrings[q].errfd != -1)
710 close (vui->vrings[q].errfd);
712 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
714 if (number_of_fds != 1)
717 vui->vrings[q].errfd = fds[0];
720 vui->vrings[q].errfd = -1;
724 case VHOST_USER_SET_VRING_BASE:
725 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
726 vui->hw_if_index, msg.state.index, msg.state.num);
728 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
731 case VHOST_USER_GET_VRING_BASE:
732 if (msg.state.index >= VHOST_VRING_MAX_N)
734 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
735 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
740 * Copy last_avail_idx from the vring before closing it because
741 * closing the vring also initializes the vring last_avail_idx
743 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
745 msg.size = sizeof (msg.state);
747 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
748 vhost_user_vring_close (vui, msg.state.index);
749 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
750 vui->hw_if_index, msg.state.index, msg.state.num);
753 case VHOST_USER_NONE:
754 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
758 case VHOST_USER_SET_LOG_BASE:
760 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
762 if (msg.size != sizeof (msg.log))
765 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
766 msg.size, sizeof (msg.log));
771 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
774 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
779 /* align size to page */
780 long page_sz = get_huge_page_size (fd);
782 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
784 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
788 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
789 map_sz, msg.log.offset, fd, vui->log_base_addr);
791 if (vui->log_base_addr == MAP_FAILED)
793 clib_warning ("failed to map memory. errno is %d", errno);
797 vui->log_base_addr += msg.log.offset;
798 vui->log_size = msg.log.size;
801 msg.size = sizeof (msg.u64);
806 case VHOST_USER_SET_LOG_FD:
807 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
811 case VHOST_USER_GET_PROTOCOL_FEATURES:
813 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
814 (1 << VHOST_USER_PROTOCOL_F_MQ);
815 msg.size = sizeof (msg.u64);
817 ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - reply 0x%016llx",
818 vui->hw_if_index, msg.u64);
821 case VHOST_USER_SET_PROTOCOL_FEATURES:
823 ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%016llx",
824 vui->hw_if_index, msg.u64);
826 vui->protocol_features = msg.u64;
830 case VHOST_USER_GET_QUEUE_NUM:
832 msg.u64 = VHOST_VRING_MAX_N;
833 msg.size = sizeof (msg.u64);
834 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
835 vui->hw_if_index, msg.u64);
838 case VHOST_USER_SET_VRING_ENABLE:
839 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
840 vui->hw_if_index, msg.state.num ? "enable" : "disable",
842 if (msg.state.index >= VHOST_VRING_MAX_N)
844 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
845 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
849 vui->vrings[msg.state.index].enabled = msg.state.num;
853 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
858 /* if we need to reply */
862 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
863 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
865 DBG_SOCK ("could not send message response");
870 vhost_user_update_iface_state (vui);
871 vlib_worker_thread_barrier_release (vlib_get_main ());
875 vhost_user_if_disconnect (vui);
876 vhost_user_update_iface_state (vui);
877 vlib_worker_thread_barrier_release (vlib_get_main ());
881 static clib_error_t *
882 vhost_user_socket_error (clib_file_t * uf)
884 vlib_main_t *vm = vlib_get_main ();
885 vhost_user_main_t *vum = &vhost_user_main;
886 vhost_user_intf_t *vui =
887 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
889 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
890 vlib_worker_thread_barrier_sync (vm);
891 vhost_user_if_disconnect (vui);
892 vhost_user_rx_thread_placement ();
893 vlib_worker_thread_barrier_release (vm);
897 static clib_error_t *
898 vhost_user_socksvr_accept_ready (clib_file_t * uf)
900 int client_fd, client_len;
901 struct sockaddr_un client;
902 clib_file_t template = { 0 };
903 vhost_user_main_t *vum = &vhost_user_main;
904 vhost_user_intf_t *vui;
906 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
908 client_len = sizeof (client);
909 client_fd = accept (uf->file_descriptor,
910 (struct sockaddr *) &client,
911 (socklen_t *) & client_len);
914 return clib_error_return_unix (0, "accept");
916 if (vui->clib_file_index != ~0)
918 DBG_SOCK ("Close client socket for vhost interface %d, fd %d",
919 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
920 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
923 DBG_SOCK ("New client socket for vhost interface %d, fd %d",
924 vui->sw_if_index, client_fd);
925 template.read_function = vhost_user_socket_read;
926 template.error_function = vhost_user_socket_error;
927 template.file_descriptor = client_fd;
928 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
929 vui->clib_file_index = clib_file_add (&file_main, &template);
933 static clib_error_t *
934 vhost_user_init (vlib_main_t * vm)
937 vhost_user_main_t *vum = &vhost_user_main;
938 vlib_thread_main_t *tm = vlib_get_thread_main ();
940 error = vlib_call_init_function (vm, ip4_init);
944 vum->coalesce_frames = 32;
945 vum->coalesce_time = 1e-3;
947 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
950 vec_foreach (cpu, vum->cpus)
952 /* This is actually not necessary as validate already zeroes it
953 * Just keeping the loop here for later because I am lazy. */
954 cpu->rx_buffers_len = 0;
957 vum->random = random_default_seed ();
959 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
964 VLIB_INIT_FUNCTION (vhost_user_init);
967 vhost_user_send_interrupt_process (vlib_main_t * vm,
968 vlib_node_runtime_t * rt, vlib_frame_t * f)
970 vhost_user_intf_t *vui;
971 f64 timeout = 3153600000.0 /* 100 years */ ;
972 uword event_type, *event_data = 0;
973 vhost_user_main_t *vum = &vhost_user_main;
975 f64 now, poll_time_remaining;
981 poll_time_remaining =
982 vlib_process_wait_for_event_or_clock (vm, timeout);
983 event_type = vlib_process_get_events (vm, &event_data);
984 vec_reset_length (event_data);
987 * Use the remaining timeout if it is less than coalesce time to avoid
988 * resetting the existing timer in the middle of expiration
990 timeout = poll_time_remaining;
991 if (vlib_process_suspend_time_is_zero (timeout) ||
992 (timeout > vum->coalesce_time))
993 timeout = vum->coalesce_time;
995 now = vlib_time_now (vm);
998 case VHOST_USER_EVENT_STOP_TIMER:
1002 case VHOST_USER_EVENT_START_TIMER:
1004 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1010 pool_foreach (vui, vum->vhost_user_interfaces, {
1011 next_timeout = timeout;
1012 vec_foreach (queue, vui->rx_queues)
1014 vhost_user_vring_t *rxvq =
1015 &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
1016 vhost_user_vring_t *txvq =
1017 &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
1019 if (txvq->n_since_last_int)
1021 if (now >= txvq->int_deadline)
1022 vhost_user_send_call (vm, txvq);
1024 next_timeout = txvq->int_deadline - now;
1027 if (rxvq->n_since_last_int)
1029 if (now >= rxvq->int_deadline)
1030 vhost_user_send_call (vm, rxvq);
1032 next_timeout = rxvq->int_deadline - now;
1035 if ((next_timeout < timeout) && (next_timeout > 0.0))
1036 timeout = next_timeout;
1043 clib_warning ("BUG: unhandled event type %d", event_type);
1046 /* No less than 1 millisecond */
1050 timeout = 3153600000.0;
1056 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1057 .function = vhost_user_send_interrupt_process,
1058 .type = VLIB_NODE_TYPE_PROCESS,
1059 .name = "vhost-user-send-interrupt-process",
1064 vhost_user_process (vlib_main_t * vm,
1065 vlib_node_runtime_t * rt, vlib_frame_t * f)
1067 vhost_user_main_t *vum = &vhost_user_main;
1068 vhost_user_intf_t *vui;
1069 struct sockaddr_un sun;
1071 clib_file_t template = { 0 };
1072 f64 timeout = 3153600000.0 /* 100 years */ ;
1073 uword *event_data = 0;
1076 sun.sun_family = AF_UNIX;
1077 template.read_function = vhost_user_socket_read;
1078 template.error_function = vhost_user_socket_error;
1082 vlib_process_wait_for_event_or_clock (vm, timeout);
1083 vlib_process_get_events (vm, &event_data);
1084 vec_reset_length (event_data);
1089 pool_foreach (vui, vum->vhost_user_interfaces, {
1091 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1092 if (vui->clib_file_index == ~0)
1095 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1098 * 1st time error or new error for this interface,
1099 * spit out the message and record the error
1101 if (!vui->sock_errno || (vui->sock_errno != errno))
1104 ("Error: Could not open unix socket for %s",
1105 vui->sock_filename);
1106 vui->sock_errno = errno;
1111 /* try to connect */
1112 strncpy (sun.sun_path, (char *) vui->sock_filename,
1113 sizeof (sun.sun_path) - 1);
1115 /* Avoid hanging VPP if the other end does not accept */
1116 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1117 clib_unix_warning ("fcntl");
1119 if (connect (sockfd, (struct sockaddr *) &sun,
1120 sizeof (struct sockaddr_un)) == 0)
1122 /* Set the socket to blocking as it was before */
1123 if (fcntl(sockfd, F_SETFL, 0) < 0)
1124 clib_unix_warning ("fcntl2");
1126 vui->sock_errno = 0;
1127 template.file_descriptor = sockfd;
1128 template.private_data =
1129 vui - vhost_user_main.vhost_user_interfaces;
1130 vui->clib_file_index = clib_file_add (&file_main, &template);
1132 /* This sockfd is considered consumed */
1137 vui->sock_errno = errno;
1142 /* check if socket is alive */
1144 socklen_t len = sizeof (error);
1145 int fd = UNIX_GET_FD(vui->clib_file_index);
1147 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1151 DBG_SOCK ("getsockopt returned %d", retval);
1152 vhost_user_if_disconnect (vui);
1163 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1164 .function = vhost_user_process,
1165 .type = VLIB_NODE_TYPE_PROCESS,
1166 .name = "vhost-user-process",
1171 * Disables and reset interface structure.
1172 * It can then be either init again, or removed from used interfaces.
1175 vhost_user_term_if (vhost_user_intf_t * vui)
1178 vhost_user_main_t *vum = &vhost_user_main;
1180 // disconnect interface sockets
1181 vhost_user_if_disconnect (vui);
1182 vhost_user_update_iface_state (vui);
1184 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1186 clib_mem_free ((void *) vui->vring_locks[q]);
1189 if (vui->unix_server_index != ~0)
1191 //Close server socket
1192 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1193 vui->unix_server_index);
1194 clib_file_del (&file_main, uf);
1195 vui->unix_server_index = ~0;
1196 unlink (vui->sock_filename);
1199 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1204 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1206 vhost_user_main_t *vum = &vhost_user_main;
1207 vhost_user_intf_t *vui;
1209 vnet_hw_interface_t *hwif;
1212 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1213 hwif->dev_class_index != vhost_user_device_class.index)
1214 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1216 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
1217 hwif->name, hwif->dev_instance);
1219 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1221 vec_foreach (queue, vui->rx_queues)
1223 vhost_user_vring_t *txvq;
1225 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
1226 if ((vum->ifq_count > 0) &&
1227 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1228 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1231 // Stop the timer if there is no more interrupt interface/queue
1232 if ((vum->ifq_count == 0) &&
1233 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1235 vlib_process_signal_event (vm,
1236 vhost_user_send_interrupt_node.index,
1237 VHOST_USER_EVENT_STOP_TIMER, 0);
1243 // Disable and reset interface
1244 vhost_user_term_if (vui);
1246 // Reset renumbered iface
1247 if (hwif->dev_instance <
1248 vec_len (vum->show_dev_instance_by_real_dev_instance))
1249 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1251 // Delete ethernet interface
1252 ethernet_delete_interface (vnm, vui->hw_if_index);
1255 pool_put (vum->vhost_user_interfaces, vui);
1260 static clib_error_t *
1261 vhost_user_exit (vlib_main_t * vm)
1263 vnet_main_t *vnm = vnet_get_main ();
1264 vhost_user_main_t *vum = &vhost_user_main;
1265 vhost_user_intf_t *vui;
1267 vlib_worker_thread_barrier_sync (vlib_get_main ());
1269 pool_foreach (vui, vum->vhost_user_interfaces, {
1270 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1273 vlib_worker_thread_barrier_release (vlib_get_main ());
1277 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1280 * Open server unix socket on specified sock_filename.
1283 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1286 struct sockaddr_un un = { };
1288 /* create listening socket */
1289 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1290 return VNET_API_ERROR_SYSCALL_ERROR_1;
1292 un.sun_family = AF_UNIX;
1293 strncpy ((char *) un.sun_path, (char *) sock_filename,
1294 sizeof (un.sun_path) - 1);
1296 /* remove if exists */
1297 unlink ((char *) sock_filename);
1299 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1301 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1305 if (listen (fd, 1) == -1)
1307 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1320 * Create ethernet interface for vhost user interface.
1323 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1324 vhost_user_intf_t * vui, u8 * hwaddress)
1326 vhost_user_main_t *vum = &vhost_user_main;
1328 clib_error_t *error;
1330 /* create hw and sw interface */
1333 clib_memcpy (hwaddr, hwaddress, 6);
1337 random_u32 (&vum->random);
1338 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1343 error = ethernet_register_interface
1345 vhost_user_device_class.index,
1346 vui - vum->vhost_user_interfaces /* device instance */ ,
1347 hwaddr /* ethernet address */ ,
1348 &vui->hw_if_index, 0 /* flag change */ );
1351 clib_error_report (error);
1353 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1357 * Initialize vui with specified attributes
1360 vhost_user_vui_init (vnet_main_t * vnm,
1361 vhost_user_intf_t * vui,
1363 const char *sock_filename,
1364 u64 feature_mask, u32 * sw_if_index)
1366 vnet_sw_interface_t *sw;
1368 vhost_user_main_t *vum = &vhost_user_main;
1369 vnet_hw_interface_t *hw;
1371 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1372 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1373 if (server_sock_fd != -1)
1375 clib_file_t template = { 0 };
1376 template.read_function = vhost_user_socksvr_accept_ready;
1377 template.file_descriptor = server_sock_fd;
1378 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1379 vui->unix_server_index = clib_file_add (&file_main, &template);
1383 vui->unix_server_index = ~0;
1386 vui->sw_if_index = sw->sw_if_index;
1387 strncpy (vui->sock_filename, sock_filename,
1388 ARRAY_LEN (vui->sock_filename) - 1);
1389 vui->sock_errno = 0;
1391 vui->feature_mask = feature_mask;
1392 vui->clib_file_index = ~0;
1393 vui->log_base_addr = 0;
1394 vui->if_index = vui - vum->vhost_user_interfaces;
1395 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1398 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1399 vhost_user_vring_init (vui, q);
1401 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1402 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1405 *sw_if_index = vui->sw_if_index;
1407 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1409 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1410 CLIB_CACHE_LINE_BYTES);
1411 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1414 vec_validate (vui->per_cpu_tx_qid,
1415 vlib_get_thread_main ()->n_vlib_mains - 1);
1416 vhost_user_tx_thread_placement (vui);
1420 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1421 const char *sock_filename,
1425 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1427 vhost_user_intf_t *vui = NULL;
1430 int server_sock_fd = -1;
1431 vhost_user_main_t *vum = &vhost_user_main;
1434 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1436 return VNET_API_ERROR_INVALID_ARGUMENT;
1439 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1444 vui = &vum->vhost_user_interfaces[*if_index];
1445 *sw_if_index = vui->sw_if_index;
1447 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1453 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1459 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1461 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1462 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1463 feature_mask, &sw_if_idx);
1466 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1469 *sw_if_index = sw_if_idx;
1471 // Process node must connect
1472 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1478 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1479 const char *sock_filename,
1482 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1484 vhost_user_main_t *vum = &vhost_user_main;
1485 vhost_user_intf_t *vui = NULL;
1487 int server_sock_fd = -1;
1489 vnet_hw_interface_t *hwif;
1492 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1493 hwif->dev_class_index != vhost_user_device_class.index)
1494 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1496 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1497 return VNET_API_ERROR_INVALID_ARGUMENT;
1499 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1502 * Disallow changing the interface to have the same path name
1503 * as other interface
1505 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1506 if (if_index && (*if_index != vui->if_index))
1507 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1509 // First try to open server socket
1511 if ((rv = vhost_user_init_server_sock (sock_filename,
1512 &server_sock_fd)) != 0)
1515 vhost_user_term_if (vui);
1516 vhost_user_vui_init (vnm, vui, server_sock_fd,
1517 sock_filename, feature_mask, &sw_if_idx);
1520 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1522 // Process node must connect
1523 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1529 vhost_user_connect_command_fn (vlib_main_t * vm,
1530 unformat_input_t * input,
1531 vlib_cli_command_t * cmd)
1533 unformat_input_t _line_input, *line_input = &_line_input;
1534 u8 *sock_filename = NULL;
1537 u64 feature_mask = (u64) ~ (0ULL);
1539 u32 custom_dev_instance = ~0;
1542 clib_error_t *error = NULL;
1544 /* Get a line of input. */
1545 if (!unformat_user (input, unformat_line_input, line_input))
1548 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1550 if (unformat (line_input, "socket %s", &sock_filename))
1552 else if (unformat (line_input, "server"))
1554 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1558 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1560 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1566 error = clib_error_return (0, "unknown input `%U'",
1567 format_unformat_error, line_input);
1572 vnet_main_t *vnm = vnet_get_main ();
1575 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1576 is_server, &sw_if_index, feature_mask,
1577 renumber, custom_dev_instance, hw)))
1579 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1583 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1587 vec_free (sock_filename);
1588 unformat_free (line_input);
1594 vhost_user_delete_command_fn (vlib_main_t * vm,
1595 unformat_input_t * input,
1596 vlib_cli_command_t * cmd)
1598 unformat_input_t _line_input, *line_input = &_line_input;
1599 u32 sw_if_index = ~0;
1600 vnet_main_t *vnm = vnet_get_main ();
1601 clib_error_t *error = NULL;
1603 /* Get a line of input. */
1604 if (!unformat_user (input, unformat_line_input, line_input))
1607 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1609 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1612 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1615 vnet_hw_interface_t *hwif =
1616 vnet_get_sup_hw_interface (vnm, sw_if_index);
1618 vhost_user_device_class.index != hwif->dev_class_index)
1620 error = clib_error_return (0, "Not a vhost interface");
1626 error = clib_error_return (0, "unknown input `%U'",
1627 format_unformat_error, line_input);
1632 vhost_user_delete_if (vnm, vm, sw_if_index);
1635 unformat_free (line_input);
1641 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1642 vhost_user_intf_details_t ** out_vuids)
1645 vhost_user_main_t *vum = &vhost_user_main;
1646 vhost_user_intf_t *vui;
1647 vhost_user_intf_details_t *r_vuids = NULL;
1648 vhost_user_intf_details_t *vuid = NULL;
1649 u32 *hw_if_indices = 0;
1650 vnet_hw_interface_t *hi;
1657 pool_foreach (vui, vum->vhost_user_interfaces,
1658 vec_add1 (hw_if_indices, vui->hw_if_index);
1661 for (i = 0; i < vec_len (hw_if_indices); i++)
1663 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1664 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1666 vec_add2 (r_vuids, vuid, 1);
1667 vuid->sw_if_index = vui->sw_if_index;
1668 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1669 vuid->features = vui->features;
1670 vuid->num_regions = vui->nregions;
1671 vuid->is_server = vui->unix_server_index != ~0;
1672 vuid->sock_errno = vui->sock_errno;
1673 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1674 sizeof (vuid->sock_filename));
1675 vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1676 s = format (s, "%v%c", hi->name, 0);
1678 strncpy ((char *) vuid->if_name, (char *) s,
1679 ARRAY_LEN (vuid->if_name) - 1);
1684 vec_free (hw_if_indices);
1686 *out_vuids = r_vuids;
1692 show_vhost_user_command_fn (vlib_main_t * vm,
1693 unformat_input_t * input,
1694 vlib_cli_command_t * cmd)
1696 clib_error_t *error = 0;
1697 vnet_main_t *vnm = vnet_get_main ();
1698 vhost_user_main_t *vum = &vhost_user_main;
1699 vhost_user_intf_t *vui;
1700 u32 hw_if_index, *hw_if_indices = 0;
1701 vnet_hw_interface_t *hi;
1711 struct feat_struct *feat_entry;
1713 static struct feat_struct feat_array[] = {
1714 #define _(s,b) { .str = #s, .bit = b, },
1715 foreach_virtio_net_feature
1720 #define foreach_protocol_feature \
1721 _(VHOST_USER_PROTOCOL_F_MQ) \
1722 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1724 static struct feat_struct proto_feat_array[] = {
1725 #define _(s) { .str = #s, .bit = s},
1726 foreach_protocol_feature
1731 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1734 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1736 hi = vnet_get_hw_interface (vnm, hw_if_index);
1737 if (vhost_user_device_class.index != hi->dev_class_index)
1739 error = clib_error_return (0, "unknown input `%U'",
1740 format_unformat_error, input);
1743 vec_add1 (hw_if_indices, hw_if_index);
1745 else if (unformat (input, "descriptors") || unformat (input, "desc"))
1749 error = clib_error_return (0, "unknown input `%U'",
1750 format_unformat_error, input);
1754 if (vec_len (hw_if_indices) == 0)
1756 pool_foreach (vui, vum->vhost_user_interfaces,
1757 vec_add1 (hw_if_indices, vui->hw_if_index);
1760 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1761 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1762 vum->coalesce_frames, vum->coalesce_time);
1763 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
1766 for (i = 0; i < vec_len (hw_if_indices); i++)
1768 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1769 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1770 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1771 hi->name, hw_if_indices[i]);
1773 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1774 " features mask (0x%llx): \n"
1775 " features (0x%llx): \n",
1776 vui->virtio_net_hdr_sz, vui->feature_mask,
1779 feat_entry = (struct feat_struct *) &feat_array;
1780 while (feat_entry->str)
1782 if (vui->features & (1ULL << feat_entry->bit))
1783 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1788 vlib_cli_output (vm, " protocol features (0x%llx)",
1789 vui->protocol_features);
1790 feat_entry = (struct feat_struct *) &proto_feat_array;
1791 while (feat_entry->str)
1793 if (vui->protocol_features & (1ULL << feat_entry->bit))
1794 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1799 vlib_cli_output (vm, "\n");
1801 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1803 (vui->unix_server_index != ~0) ? "server" : "client",
1804 strerror (vui->sock_errno));
1806 vlib_cli_output (vm, " rx placement: ");
1808 vec_foreach (queue, vui->rx_queues)
1810 vnet_main_t *vnm = vnet_get_main ();
1812 vnet_hw_interface_rx_mode mode;
1814 thread_index = vnet_get_device_input_thread_index (vnm,
1817 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
1818 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1819 thread_index, VHOST_VRING_IDX_TX (*queue),
1820 format_vnet_hw_interface_rx_mode, mode);
1823 vlib_cli_output (vm, " tx placement: %s\n",
1824 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1826 vec_foreach_index (ci, vui->per_cpu_tx_qid)
1828 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1829 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1832 vlib_cli_output (vm, "\n");
1834 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1838 vlib_cli_output (vm,
1839 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1840 vlib_cli_output (vm,
1841 " ====== ===== ================== ================== ================== ================== ==================\n");
1843 for (j = 0; j < vui->nregions; j++)
1845 vlib_cli_output (vm,
1846 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1847 j, vui->region_mmap_fd[j],
1848 vui->regions[j].guest_phys_addr,
1849 vui->regions[j].memory_size,
1850 vui->regions[j].userspace_addr,
1851 vui->regions[j].mmap_offset,
1852 pointer_to_uword (vui->region_mmap_addr[j]));
1854 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1856 if (!vui->vrings[q].started)
1859 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1860 (q & 1) ? "RX" : "TX",
1861 vui->vrings[q].enabled ? "" : " disabled");
1863 vlib_cli_output (vm,
1864 " qsz %d last_avail_idx %d last_used_idx %d\n",
1865 vui->vrings[q].qsz_mask + 1,
1866 vui->vrings[q].last_avail_idx,
1867 vui->vrings[q].last_used_idx);
1869 if (vui->vrings[q].avail && vui->vrings[q].used)
1870 vlib_cli_output (vm,
1871 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1872 vui->vrings[q].avail->flags,
1873 vui->vrings[q].avail->idx,
1874 vui->vrings[q].used->flags,
1875 vui->vrings[q].used->idx);
1877 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1878 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1879 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1880 kickfd, callfd, vui->vrings[q].errfd);
1884 vlib_cli_output (vm, "\n descriptor table:\n");
1885 vlib_cli_output (vm,
1886 " id addr len flags next user_addr\n");
1887 vlib_cli_output (vm,
1888 " ===== ================== ===== ====== ===== ==================\n");
1889 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1892 vlib_cli_output (vm,
1893 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1894 j, vui->vrings[q].desc[j].addr,
1895 vui->vrings[q].desc[j].len,
1896 vui->vrings[q].desc[j].flags,
1897 vui->vrings[q].desc[j].next,
1898 pointer_to_uword (map_guest_mem
1900 vui->vrings[q].desc[j].
1905 vlib_cli_output (vm, "\n");
1908 vec_free (hw_if_indices);
1917 * Create a vHost User interface. Once created, a new virtual interface
1918 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
1919 * is the next free index.
1921 * There are several parameters associated with a vHost interface:
1923 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
1924 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
1925 * create the socket if it does not already exist. If in '<em>client</em>' mode,
1926 * hypervisor will create the socket if it does not already exist. The VPP code
1927 * is indifferent to the file location. However, if SELinux is enabled, then the
1928 * socket needs to be created in '<em>/var/run/vpp/</em>'.
1930 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
1931 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
1932 * mode, the VM can be reset without tearing down the vHost Interface. In
1933 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
1934 * tearing down the vHost Interface.
1936 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
1937 * startup. <b>This is intended for degugging only.</b> It is recommended that this
1938 * parameter not be used except by experienced users. By default, all supported
1939 * features will be advertised. Otherwise, provide the set of features desired.
1940 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
1941 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
1942 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
1943 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
1944 * - 0x004000000 (26) - VHOST_F_LOG_ALL
1945 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
1946 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
1947 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
1948 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
1950 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
1951 * X:X:X:X:X:X unix or X.X.X cisco format.
1953 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
1954 * in the name to be specified. If instance already exists, name will be used
1955 * anyway and multiple instances will have the same name. Use with caution.
1958 * Example of how to create a vhost interface with VPP as the client and all features enabled:
1959 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
1960 * VirtualEthernet0/0/0
1962 * Example of how to create a vhost interface with VPP as the server and with just
1963 * multiple queues enabled:
1964 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
1965 * VirtualEthernet0/0/1
1967 * Once the vHost interface is created, enable the interface using:
1968 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
1971 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
1972 .path = "create vhost-user",
1973 .short_help = "create vhost-user socket <socket-filename> [server] "
1974 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
1975 .function = vhost_user_connect_command_fn,
1980 * Delete a vHost User interface using the interface name or the
1981 * software interface index. Use the '<em>show interface</em>'
1982 * command to determine the software interface index. On deletion,
1983 * the linux socket will not be deleted.
1986 * Example of how to delete a vhost interface by name:
1987 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
1988 * Example of how to delete a vhost interface by software interface index:
1989 * @cliexcmd{delete vhost-user sw_if_index 1}
1992 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
1993 .path = "delete vhost-user",
1994 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
1995 .function = vhost_user_delete_command_fn,
1999 * Display the attributes of a single vHost User interface (provide interface
2000 * name), multiple vHost User interfaces (provide a list of interface names seperated
2001 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2002 * vHost interfaces).
2006 * Example of how to display a vhost interface:
2007 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2008 * Virtio vhost-user interfaces
2010 * coalesce frames 32 time 1e-3
2011 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2012 * virtio_net_hdr_sz 12
2013 * features mask (0xffffffffffffffff):
2014 * features (0x50408000):
2015 * VIRTIO_NET_F_MRG_RXBUF (15)
2016 * VIRTIO_NET_F_MQ (22)
2017 * VIRTIO_F_INDIRECT_DESC (28)
2018 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2019 * protocol features (0x3)
2020 * VHOST_USER_PROTOCOL_F_MQ (0)
2021 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2023 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2026 * thread 1 on vring 1
2027 * thread 1 on vring 5
2028 * thread 2 on vring 3
2029 * thread 2 on vring 7
2030 * tx placement: spin-lock
2031 * thread 0 on vring 0
2032 * thread 1 on vring 2
2033 * thread 2 on vring 0
2035 * Memory regions (total 2)
2036 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2037 * ====== ===== ================== ================== ================== ================== ==================
2038 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2039 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2042 * qsz 256 last_avail_idx 0 last_used_idx 0
2043 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2044 * kickfd 62 callfd 64 errfd -1
2047 * qsz 256 last_avail_idx 0 last_used_idx 0
2048 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2049 * kickfd 65 callfd 66 errfd -1
2052 * qsz 256 last_avail_idx 0 last_used_idx 0
2053 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2054 * kickfd 63 callfd 70 errfd -1
2057 * qsz 256 last_avail_idx 0 last_used_idx 0
2058 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2059 * kickfd 72 callfd 74 errfd -1
2061 * Virtqueue 4 (TX disabled)
2062 * qsz 256 last_avail_idx 0 last_used_idx 0
2063 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2064 * kickfd 76 callfd 78 errfd -1
2066 * Virtqueue 5 (RX disabled)
2067 * qsz 256 last_avail_idx 0 last_used_idx 0
2068 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2069 * kickfd 80 callfd 82 errfd -1
2071 * Virtqueue 6 (TX disabled)
2072 * qsz 256 last_avail_idx 0 last_used_idx 0
2073 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2074 * kickfd 84 callfd 86 errfd -1
2076 * Virtqueue 7 (RX disabled)
2077 * qsz 256 last_avail_idx 0 last_used_idx 0
2078 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2079 * kickfd 88 callfd 90 errfd -1
2083 * The optional '<em>descriptors</em>' parameter will display the same output as
2084 * the previous example but will include the descriptor table for each queue.
2085 * The output is truncated below:
2086 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2087 * Virtio vhost-user interfaces
2089 * coalesce frames 32 time 1e-3
2090 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2091 * virtio_net_hdr_sz 12
2092 * features mask (0xffffffffffffffff):
2093 * features (0x50408000):
2094 * VIRTIO_NET_F_MRG_RXBUF (15)
2095 * VIRTIO_NET_F_MQ (22)
2098 * qsz 256 last_avail_idx 0 last_used_idx 0
2099 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2100 * kickfd 62 callfd 64 errfd -1
2103 * id addr len flags next user_addr
2104 * ===== ================== ===== ====== ===== ==================
2105 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2106 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2107 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2108 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2109 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2110 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2111 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2112 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2113 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2114 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2115 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2117 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2118 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2119 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2120 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2121 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2122 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2123 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2126 * qsz 256 last_avail_idx 0 last_used_idx 0
2132 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2133 .path = "show vhost-user",
2134 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2135 .function = show_vhost_user_command_fn,
2140 debug_vhost_user_command_fn (vlib_main_t * vm,
2141 unformat_input_t * input,
2142 vlib_cli_command_t * cmd)
2144 unformat_input_t _line_input, *line_input = &_line_input;
2145 clib_error_t *error = NULL;
2146 vhost_user_main_t *vum = &vhost_user_main;
2150 /* Get a line of input. */
2151 if (!unformat_user (input, unformat_line_input, line_input))
2152 return clib_error_return (0, "missing argument");
2154 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2158 error = clib_error_return (0, "unknown input `%U'",
2159 format_unformat_error, line_input);
2163 if (unformat (line_input, "on"))
2168 else if (unformat (line_input, "off"))
2175 error = clib_error_return (0, "unknown input `%U'",
2176 format_unformat_error, line_input);
2184 unformat_free (line_input);
2190 VLIB_CLI_COMMAND (debug_vhost_user_command, static) = {
2191 .path = "debug vhost-user",
2192 .short_help = "debug vhost-user <on | off>",
2193 .function = debug_vhost_user_command_fn,
2197 static clib_error_t *
2198 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2200 vhost_user_main_t *vum = &vhost_user_main;
2202 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2204 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2206 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2208 else if (unformat (input, "dont-dump-memory"))
2209 vum->dont_dump_vhost_user_memory = 1;
2211 return clib_error_return (0, "unknown input `%U'",
2212 format_unformat_error, input);
2219 /* vhost-user { ... } configuration. */
2220 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2223 vhost_user_unmap_all (void)
2225 vhost_user_main_t *vum = &vhost_user_main;
2226 vhost_user_intf_t *vui;
2228 if (vum->dont_dump_vhost_user_memory)
2230 pool_foreach (vui, vum->vhost_user_interfaces,
2231 unmap_all_mem_regions (vui);
2237 * fd.io coding-style-patch-verification: ON
2240 * eval: (c-set-style "gnu")