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/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 #include <vnet/interface/rx_queue_funcs.h>
41 #include <vnet/devices/virtio/vhost_user.h>
42 #include <vnet/devices/virtio/vhost_user_inline.h>
46 * @brief vHost User Device Driver.
48 * This file contains the source code for vHost User interface.
52 vlib_node_registration_t vhost_user_send_interrupt_node;
55 vhost_user_main_t vhost_user_main = {
59 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
65 get_huge_page_size (int fd)
73 unmap_all_mem_regions (vhost_user_intf_t * vui)
76 vhost_user_vring_t *vq;
78 for (i = 0; i < vui->nregions; i++)
80 if (vui->region_mmap_addr[i] != MAP_FAILED)
83 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
85 ssize_t map_sz = (vui->regions[i].memory_size +
86 vui->regions[i].mmap_offset +
87 page_sz - 1) & ~(page_sz - 1);
90 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
93 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
94 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
97 vui->region_mmap_addr[i] = MAP_FAILED;
101 vu_log_err (vui, "failed to unmap memory region (errno %d)",
104 close (vui->region_mmap_fd[i]);
109 for (q = 0; q < vui->num_qid; q++)
111 vq = &vui->vrings[q];
118 static_always_inline void
119 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
121 //Let's try to assign one queue to each thread
123 u32 thread_index = 0;
125 vui->use_tx_spinlock = 0;
128 for (qid = 0; qid < vui->num_qid / 2; qid++)
130 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
131 if (!rxvq->started || !rxvq->enabled)
134 vui->per_cpu_tx_qid[thread_index] = qid;
136 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
139 //We need to loop, meaning the spinlock has to be used
140 vui->use_tx_spinlock = 1;
141 if (thread_index == 0)
143 //Could not find a single valid one
144 for (thread_index = 0;
145 thread_index < vlib_get_thread_main ()->n_vlib_mains;
148 vui->per_cpu_tx_qid[thread_index] = 0;
156 * @brief Unassign existing interface/queue to thread mappings and re-assign
157 * new interface/queue to thread mappings
159 static_always_inline void
160 vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
162 vhost_user_vring_t *txvq = &vui->vrings[qid];
163 vnet_main_t *vnm = vnet_get_main ();
167 ASSERT ((qid & 1) == 1); // should be odd
168 // Assign new queue mappings for the interface
169 vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
170 vhost_user_input_node.index);
171 txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
172 VNET_HW_IF_RXQ_THREAD_ANY);
173 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
174 /* Set polling as the default */
175 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
177 rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
179 vu_log_warn (vui, "unable to set rx mode for interface %d, "
180 "queue %d: rc=%d", vui->hw_if_index, q, rv);
181 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
184 /** @brief Returns whether at least one TX and one RX vring are enabled */
185 static_always_inline int
186 vhost_user_intf_ready (vhost_user_intf_t * vui)
188 int i, found[2] = { }; //RX + TX
190 for (i = 0; i < vui->num_qid; i++)
191 if (vui->vrings[i].started && vui->vrings[i].enabled)
194 return found[0] && found[1];
197 static_always_inline void
198 vhost_user_update_iface_state (vhost_user_intf_t * vui)
200 /* if we have pointers to descriptor table, go up */
201 int is_ready = vhost_user_intf_ready (vui);
202 if (is_ready != vui->is_ready)
204 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
205 is_ready ? "ready" : "down");
207 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
208 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
210 vui->is_ready = is_ready;
215 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
218 vnet_main_t *vnm = vnet_get_main ();
219 vhost_user_vring_t *txvq;
223 /* Only care about the odd number, or TX, virtqueue */
226 // qid >> 1 is to convert virtqueue number to vring queue index
228 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
229 if (vhost_user_intf_ready (vui) &&
230 ((txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
231 (txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT)))
232 vnet_hw_if_rx_queue_set_int_pending (vnm, txvq->queue_index);
235 static clib_error_t *
236 vhost_user_callfd_read_ready (clib_file_t * uf)
238 __attribute__ ((unused)) int n;
241 n = read (uf->file_descriptor, ((char *) &buff), 8);
246 static_always_inline void
247 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
249 if (qid & 1) // RX is odd, TX is even
251 if (vui->vrings[qid].qid == -1)
252 vhost_user_rx_thread_placement (vui, qid);
255 vhost_user_tx_thread_placement (vui);
258 static clib_error_t *
259 vhost_user_kickfd_read_ready (clib_file_t * uf)
261 __attribute__ ((unused)) int n;
263 vhost_user_intf_t *vui =
264 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
265 uf->private_data >> 8);
266 u32 qid = uf->private_data & 0xff;
268 n = read (uf->file_descriptor, ((char *) &buff), 8);
269 vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
270 if (!vui->vrings[qid].started ||
271 (vhost_user_intf_ready (vui) != vui->is_ready))
273 if (vui->vrings[qid].started == 0)
275 vui->vrings[qid].started = 1;
276 vhost_user_thread_placement (vui, qid);
277 vhost_user_update_iface_state (vui);
281 vhost_user_set_interrupt_pending (vui, uf->private_data);
285 static_always_inline void
286 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
288 vhost_user_vring_t *vring = &vui->vrings[qid];
290 clib_memset (vring, 0, sizeof (*vring));
291 vring->kickfd_idx = ~0;
292 vring->callfd_idx = ~0;
296 clib_spinlock_init (&vring->vring_lock);
299 * We have a bug with some qemu 2.5, and this may be a fix.
300 * Feel like interpretation holy text, but this is from vhost-user.txt.
302 * One queue pair is enabled initially. More queues are enabled
303 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
305 * Don't know who's right, but this is what DPDK does.
307 if (qid == 0 || qid == 1)
311 static_always_inline void
312 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
314 vhost_user_vring_t *vring = &vui->vrings[qid];
316 if (vring->kickfd_idx != ~0)
318 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
320 clib_file_del (&file_main, uf);
321 vring->kickfd_idx = ~0;
323 if (vring->callfd_idx != ~0)
325 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
327 clib_file_del (&file_main, uf);
328 vring->callfd_idx = ~0;
330 if (vring->errfd != -1)
332 close (vring->errfd);
336 clib_spinlock_free (&vring->vring_lock);
338 // save the qid so that we don't need to unassign and assign_rx_thread
339 // when the interface comes back up. They are expensive calls.
340 u16 q = vui->vrings[qid].qid;
341 vhost_user_vring_init (vui, qid);
342 vui->vrings[qid].qid = q;
345 static_always_inline void
346 vhost_user_if_disconnect (vhost_user_intf_t * vui)
348 vnet_main_t *vnm = vnet_get_main ();
351 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
353 if (vui->clib_file_index != ~0)
355 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
356 vui->clib_file_index = ~0;
361 for (q = 0; q < vui->num_qid; q++)
362 vhost_user_vring_close (vui, q);
364 unmap_all_mem_regions (vui);
365 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
368 static clib_error_t *
369 vhost_user_socket_read (clib_file_t * uf)
372 int fd, number_of_fds = 0;
373 int fds[VHOST_MEMORY_MAX_NREGIONS];
374 vhost_user_msg_t msg;
377 vhost_user_main_t *vum = &vhost_user_main;
378 vhost_user_intf_t *vui;
379 struct cmsghdr *cmsg;
381 clib_file_t template = { 0 };
382 vnet_main_t *vnm = vnet_get_main ();
383 vlib_main_t *vm = vlib_get_main ();
385 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
387 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
389 clib_memset (&mh, 0, sizeof (mh));
390 clib_memset (control, 0, sizeof (control));
392 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
395 /* set the payload */
396 iov[0].iov_base = (void *) &msg;
397 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
401 mh.msg_control = control;
402 mh.msg_controllen = sizeof (control);
404 n = recvmsg (uf->file_descriptor, &mh, 0);
406 if (n != VHOST_USER_MSG_HDR_SZ)
410 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
415 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
416 n, VHOST_USER_MSG_HDR_SZ);
421 if (mh.msg_flags & MSG_CTRUNC)
423 vu_log_debug (vui, "MSG_CTRUNC is set");
427 cmsg = CMSG_FIRSTHDR (&mh);
429 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
430 (cmsg->cmsg_type == SCM_RIGHTS) &&
431 (cmsg->cmsg_len - CMSG_LEN (0) <=
432 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
434 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
435 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
438 /* version 1, no reply bit set */
439 if ((msg.flags & 7) != 1)
441 vu_log_debug (vui, "malformed message received. closing socket");
448 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
452 vu_log_debug (vui, "read failed %s", strerror (errno));
455 else if (rv != msg.size)
457 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
465 case VHOST_USER_GET_FEATURES:
467 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
468 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
469 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
470 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
471 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
472 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
473 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
474 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
475 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
476 msg.u64 &= vui->feature_mask;
478 if (vui->enable_event_idx)
479 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
481 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
482 if (vui->enable_packed)
483 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
485 msg.size = sizeof (msg.u64);
486 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
487 "0x%016llx", vui->hw_if_index, msg.u64);
489 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
490 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
492 vu_log_debug (vui, "could not send message response");
497 case VHOST_USER_SET_FEATURES:
498 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
499 "0x%016llx", vui->hw_if_index, msg.u64);
501 vui->features = msg.u64;
504 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
505 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
506 vui->virtio_net_hdr_sz = 12;
508 vui->virtio_net_hdr_sz = 10;
511 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
513 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
514 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
515 if (vui->enable_gso &&
516 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
517 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
519 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
520 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
522 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
523 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
524 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
526 vhost_user_update_iface_state (vui);
529 case VHOST_USER_SET_MEM_TABLE:
530 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
531 vui->hw_if_index, msg.memory.nregions);
533 if ((msg.memory.nregions < 1) ||
534 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
536 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
537 VHOST_MEMORY_MAX_NREGIONS);
541 if (msg.memory.nregions != number_of_fds)
543 vu_log_debug (vui, "each memory region must have FD");
547 /* Do the mmap without barrier sync */
548 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
549 for (i = 0; i < msg.memory.nregions; i++)
551 long page_sz = get_huge_page_size (fds[i]);
553 /* align size to page */
554 ssize_t map_sz = (msg.memory.regions[i].memory_size +
555 msg.memory.regions[i].mmap_offset +
556 page_sz - 1) & ~(page_sz - 1);
558 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
559 MAP_SHARED, fds[i], 0);
560 if (region_mmap_addr[i] == MAP_FAILED)
562 vu_log_err (vui, "failed to map memory. errno is %d", errno);
563 for (j = 0; j < i; j++)
564 munmap (region_mmap_addr[j], map_sz);
567 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
568 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
569 region_mmap_addr[i], page_sz);
572 vlib_worker_thread_barrier_sync (vm);
573 unmap_all_mem_regions (vui);
574 for (i = 0; i < msg.memory.nregions; i++)
576 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
577 sizeof (vhost_user_memory_region_t));
579 vui->region_mmap_addr[i] = region_mmap_addr[i];
580 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
581 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
582 vui->regions[i].memory_size;
584 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
585 vui->region_mmap_fd[i] = fds[i];
591 * Re-compute desc, used, and avail descriptor table if vring address
594 for (q = 0; q < vui->num_qid; q++)
596 if (vui->vrings[q].desc_user_addr &&
597 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
599 vui->vrings[q].desc =
600 map_user_mem (vui, vui->vrings[q].desc_user_addr);
601 vui->vrings[q].used =
602 map_user_mem (vui, vui->vrings[q].used_user_addr);
603 vui->vrings[q].avail =
604 map_user_mem (vui, vui->vrings[q].avail_user_addr);
607 vlib_worker_thread_barrier_release (vm);
610 case VHOST_USER_SET_VRING_NUM:
611 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
612 vui->hw_if_index, msg.state.index, msg.state.num);
614 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
615 (msg.state.num == 0) || /* it cannot be zero */
616 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
617 (msg.state.index >= vui->num_qid))
619 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
620 " %d, msg.state.index %d, curruent max q %d",
621 msg.state.num, msg.state.index, vui->num_qid);
624 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
627 case VHOST_USER_SET_VRING_ADDR:
628 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
629 vui->hw_if_index, msg.state.index);
631 if (msg.state.index >= vui->num_qid)
633 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
634 " %u >= %u", msg.state.index, vui->num_qid);
638 if (msg.size < sizeof (msg.addr))
640 vu_log_debug (vui, "vhost message is too short (%d < %d)",
641 msg.size, sizeof (msg.addr));
645 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
646 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
647 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
649 if ((desc == NULL) || (used == NULL) || (avail == NULL))
651 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
656 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
657 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
658 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
660 vlib_worker_thread_barrier_sync (vm);
661 vui->vrings[msg.state.index].desc = desc;
662 vui->vrings[msg.state.index].used = used;
663 vui->vrings[msg.state.index].avail = avail;
665 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
666 vui->vrings[msg.state.index].log_used =
667 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
669 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
670 the ring is initialized in an enabled state. */
671 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
672 vui->vrings[msg.state.index].enabled = 1;
674 vui->vrings[msg.state.index].last_used_idx =
675 vui->vrings[msg.state.index].last_avail_idx =
676 vui->vrings[msg.state.index].used->idx;
677 vui->vrings[msg.state.index].last_kick =
678 vui->vrings[msg.state.index].last_used_idx;
680 /* tell driver that we don't want interrupts */
681 if (vhost_user_is_packed_ring_supported (vui))
682 vui->vrings[msg.state.index].used_event->flags =
683 VRING_EVENT_F_DISABLE;
685 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
686 vlib_worker_thread_barrier_release (vm);
687 vhost_user_update_iface_state (vui);
690 case VHOST_USER_SET_OWNER:
691 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
694 case VHOST_USER_RESET_OWNER:
695 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
699 case VHOST_USER_SET_VRING_CALL:
700 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
701 vui->hw_if_index, msg.u64);
703 q = (u8) (msg.u64 & 0xFF);
704 if (vui->num_qid > q)
706 /* if there is old fd, delete and close it */
707 if (vui->vrings[q].callfd_idx != ~0)
709 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
710 vui->vrings[q].callfd_idx);
711 clib_file_del (&file_main, uf);
712 vui->vrings[q].callfd_idx = ~0;
715 else if (vec_len (vui->vrings) > q)
717 /* grow vrings by pair (RX + TX) */
718 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
722 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
725 * Double the array size if it is less than 64 entries.
726 * Slow down thereafter.
728 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
729 new_max_q = vec_len (vui->vrings) << 1;
731 new_max_q = vec_len (vui->vrings) +
732 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
733 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
734 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
736 /* sync with the worker threads, vrings may move due to realloc */
737 vlib_worker_thread_barrier_sync (vm);
738 vec_validate_aligned (vui->vrings, new_max_q - 1,
739 CLIB_CACHE_LINE_BYTES);
740 vlib_worker_thread_barrier_release (vm);
742 for (i = old_max_q; i < vec_len (vui->vrings); i++)
743 vhost_user_vring_init (vui, i);
745 /* grow vrings by pair (RX + TX) */
746 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
749 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
751 if (number_of_fds != 1)
753 vu_log_debug (vui, "More than one fd received !");
757 template.read_function = vhost_user_callfd_read_ready;
758 template.file_descriptor = fds[0];
759 template.private_data =
760 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
761 template.description = format (0, "vhost user");
762 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
765 vui->vrings[q].callfd_idx = ~0;
768 case VHOST_USER_SET_VRING_KICK:
769 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
770 vui->hw_if_index, msg.u64);
772 q = (u8) (msg.u64 & 0xFF);
773 if (q >= vui->num_qid)
775 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
776 " %u >= %u", q, vui->num_qid);
780 if (vui->vrings[q].kickfd_idx != ~0)
782 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
783 vui->vrings[q].kickfd_idx);
784 clib_file_del (&file_main, uf);
785 vui->vrings[q].kickfd_idx = ~0;
788 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
790 if (number_of_fds != 1)
792 vu_log_debug (vui, "More than one fd received !");
796 template.read_function = vhost_user_kickfd_read_ready;
797 template.file_descriptor = fds[0];
798 template.private_data =
799 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
801 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
805 //When no kickfd is set, the queue is initialized as started
806 vui->vrings[q].kickfd_idx = ~0;
807 vui->vrings[q].started = 1;
808 vhost_user_thread_placement (vui, q);
810 vhost_user_update_iface_state (vui);
813 case VHOST_USER_SET_VRING_ERR:
814 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
815 vui->hw_if_index, msg.u64);
817 q = (u8) (msg.u64 & 0xFF);
818 if (q >= vui->num_qid)
820 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
821 " %u >= %u", q, vui->num_qid);
825 if (vui->vrings[q].errfd != -1)
826 close (vui->vrings[q].errfd);
828 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
830 if (number_of_fds != 1)
833 vui->vrings[q].errfd = fds[0];
836 vui->vrings[q].errfd = -1;
839 case VHOST_USER_SET_VRING_BASE:
841 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
842 vui->hw_if_index, msg.state.index, msg.state.num);
843 if (msg.state.index >= vui->num_qid)
845 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
846 " %u >= %u", msg.state.index, vui->num_qid);
849 vlib_worker_thread_barrier_sync (vm);
850 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
851 if (vhost_user_is_packed_ring_supported (vui))
855 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
856 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
857 * | last avail idx | | last used idx | |
858 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * avail wrap counter used wrap counter
863 /* last avail idx at bit 0-14. */
864 vui->vrings[msg.state.index].last_avail_idx =
865 msg.state.num & 0x7fff;
866 /* avail wrap counter at bit 15 */
867 vui->vrings[msg.state.index].avail_wrap_counter =
868 ! !(msg.state.num & (1 << 15));
871 * Although last_used_idx is passed in the upper 16 bits in qemu
872 * implementation, in practice, last_avail_idx and last_used_idx are
873 * usually the same. As a result, DPDK does not bother to pass us
874 * last_used_idx. The spec is not clear on thex coding. I figured it
875 * out by reading the qemu code. So let's just read last_avail_idx
876 * and set last_used_idx equals to last_avail_idx.
878 vui->vrings[msg.state.index].last_used_idx =
879 vui->vrings[msg.state.index].last_avail_idx;
880 vui->vrings[msg.state.index].last_kick =
881 vui->vrings[msg.state.index].last_used_idx;
882 vui->vrings[msg.state.index].used_wrap_counter =
883 vui->vrings[msg.state.index].avail_wrap_counter;
885 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
886 vui->vrings[msg.state.index].avail_wrap_counter =
889 vlib_worker_thread_barrier_release (vm);
892 case VHOST_USER_GET_VRING_BASE:
893 if (msg.state.index >= vui->num_qid)
895 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
896 " %u >= %u", msg.state.index, vui->num_qid);
900 /* protection is needed to prevent rx/tx from changing last_avail_idx */
901 vlib_worker_thread_barrier_sync (vm);
903 * Copy last_avail_idx from the vring before closing it because
904 * closing the vring also initializes the vring last_avail_idx
906 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
907 if (vhost_user_is_packed_ring_supported (vui))
910 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
911 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
913 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
914 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
917 msg.size = sizeof (msg.state);
920 * Spec says: Client must [...] stop ring upon receiving
921 * VHOST_USER_GET_VRING_BASE
923 vhost_user_vring_close (vui, msg.state.index);
924 vlib_worker_thread_barrier_release (vm);
926 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
927 vui->hw_if_index, msg.state.index, msg.state.num);
929 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
930 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
932 vu_log_debug (vui, "could not send message response");
935 vhost_user_update_iface_state (vui);
938 case VHOST_USER_NONE:
939 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
942 case VHOST_USER_SET_LOG_BASE:
943 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
946 if (msg.size != sizeof (msg.log))
948 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
949 " %d instead of %d", msg.size, sizeof (msg.log));
953 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
955 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
956 "VHOST_USER_SET_LOG_BASE received");
961 /* align size to page */
962 long page_sz = get_huge_page_size (fd);
964 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
966 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
969 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
970 "mapped 0x%lx", map_sz, msg.log.offset, fd,
973 if (log_base_addr == MAP_FAILED)
975 vu_log_err (vui, "failed to map memory. errno is %d", errno);
979 vlib_worker_thread_barrier_sync (vm);
980 vui->log_base_addr = log_base_addr;
981 vui->log_base_addr += msg.log.offset;
982 vui->log_size = msg.log.size;
983 vlib_worker_thread_barrier_release (vm);
986 msg.size = sizeof (msg.u64);
988 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
989 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
991 vu_log_debug (vui, "could not send message response");
996 case VHOST_USER_SET_LOG_FD:
997 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1000 case VHOST_USER_GET_PROTOCOL_FEATURES:
1002 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1003 (1 << VHOST_USER_PROTOCOL_F_MQ);
1004 msg.size = sizeof (msg.u64);
1005 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1006 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1008 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1009 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1011 vu_log_debug (vui, "could not send message response");
1016 case VHOST_USER_SET_PROTOCOL_FEATURES:
1017 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1018 "features 0x%016llx", vui->hw_if_index, msg.u64);
1019 vui->protocol_features = msg.u64;
1022 case VHOST_USER_GET_QUEUE_NUM:
1024 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1025 msg.size = sizeof (msg.u64);
1026 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1027 vui->hw_if_index, msg.u64);
1029 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1030 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1032 vu_log_debug (vui, "could not send message response");
1037 case VHOST_USER_SET_VRING_ENABLE:
1038 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1039 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1041 if (msg.state.index >= vui->num_qid)
1043 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1044 " %u >= %u", msg.state.index, vui->num_qid);
1048 vui->vrings[msg.state.index].enabled = msg.state.num;
1049 vhost_user_thread_placement (vui, msg.state.index);
1050 vhost_user_update_iface_state (vui);
1054 vu_log_debug (vui, "unknown vhost-user message %d received. "
1055 "closing socket", msg.request);
1062 vlib_worker_thread_barrier_sync (vm);
1063 vhost_user_if_disconnect (vui);
1064 vlib_worker_thread_barrier_release (vm);
1065 vhost_user_update_iface_state (vui);
1069 static clib_error_t *
1070 vhost_user_socket_error (clib_file_t * uf)
1072 vlib_main_t *vm = vlib_get_main ();
1073 vhost_user_main_t *vum = &vhost_user_main;
1074 vhost_user_intf_t *vui =
1075 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1077 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1078 vlib_worker_thread_barrier_sync (vm);
1079 vhost_user_if_disconnect (vui);
1080 vlib_worker_thread_barrier_release (vm);
1084 static clib_error_t *
1085 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1087 int client_fd, client_len;
1088 struct sockaddr_un client;
1089 clib_file_t template = { 0 };
1090 vhost_user_main_t *vum = &vhost_user_main;
1091 vhost_user_intf_t *vui;
1093 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1095 client_len = sizeof (client);
1096 client_fd = accept (uf->file_descriptor,
1097 (struct sockaddr *) &client,
1098 (socklen_t *) & client_len);
1101 return clib_error_return_unix (0, "accept");
1103 if (vui->clib_file_index != ~0)
1105 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1106 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1107 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1110 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1111 vui->sw_if_index, client_fd);
1112 template.read_function = vhost_user_socket_read;
1113 template.error_function = vhost_user_socket_error;
1114 template.file_descriptor = client_fd;
1115 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1116 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1117 vui->clib_file_index = clib_file_add (&file_main, &template);
1122 static clib_error_t *
1123 vhost_user_init (vlib_main_t * vm)
1125 vhost_user_main_t *vum = &vhost_user_main;
1126 vlib_thread_main_t *tm = vlib_get_thread_main ();
1128 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1130 vum->coalesce_frames = 32;
1131 vum->coalesce_time = 1e-3;
1133 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1136 vec_foreach (cpu, vum->cpus)
1138 /* This is actually not necessary as validate already zeroes it
1139 * Just keeping the loop here for later because I am lazy. */
1140 cpu->rx_buffers_len = 0;
1143 vum->random = random_default_seed ();
1145 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1151 VLIB_INIT_FUNCTION (vhost_user_init) =
1153 .runs_after = VLIB_INITS("ip4_init"),
1158 vhost_user_send_interrupt_process (vlib_main_t * vm,
1159 vlib_node_runtime_t * rt, vlib_frame_t * f)
1161 vhost_user_intf_t *vui;
1162 f64 timeout = 3153600000.0 /* 100 years */ ;
1163 uword event_type, *event_data = 0;
1164 vhost_user_main_t *vum = &vhost_user_main;
1166 f64 now, poll_time_remaining;
1172 poll_time_remaining =
1173 vlib_process_wait_for_event_or_clock (vm, timeout);
1174 event_type = vlib_process_get_events (vm, &event_data);
1175 vec_reset_length (event_data);
1178 * Use the remaining timeout if it is less than coalesce time to avoid
1179 * resetting the existing timer in the middle of expiration
1181 timeout = poll_time_remaining;
1182 if (vlib_process_suspend_time_is_zero (timeout) ||
1183 (timeout > vum->coalesce_time))
1184 timeout = vum->coalesce_time;
1186 now = vlib_time_now (vm);
1189 case VHOST_USER_EVENT_STOP_TIMER:
1193 case VHOST_USER_EVENT_START_TIMER:
1195 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1201 pool_foreach (vui, vum->vhost_user_interfaces) {
1202 next_timeout = timeout;
1203 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1205 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1206 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1208 if (txvq->qid == -1)
1210 if (txvq->n_since_last_int)
1212 if (now >= txvq->int_deadline)
1213 vhost_user_send_call (vm, vui, txvq);
1215 next_timeout = txvq->int_deadline - now;
1218 if (rxvq->n_since_last_int)
1220 if (now >= rxvq->int_deadline)
1221 vhost_user_send_call (vm, vui, rxvq);
1223 next_timeout = rxvq->int_deadline - now;
1226 if ((next_timeout < timeout) && (next_timeout > 0.0))
1227 timeout = next_timeout;
1234 clib_warning ("BUG: unhandled event type %d", event_type);
1237 /* No less than 1 millisecond */
1241 timeout = 3153600000.0;
1247 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1248 .function = vhost_user_send_interrupt_process,
1249 .type = VLIB_NODE_TYPE_PROCESS,
1250 .name = "vhost-user-send-interrupt-process",
1255 vhost_user_process (vlib_main_t * vm,
1256 vlib_node_runtime_t * rt, vlib_frame_t * f)
1258 vhost_user_main_t *vum = &vhost_user_main;
1259 vhost_user_intf_t *vui;
1260 struct sockaddr_un sun;
1262 clib_file_t template = { 0 };
1263 f64 timeout = 3153600000.0 /* 100 years */ ;
1264 uword *event_data = 0;
1267 sun.sun_family = AF_UNIX;
1268 template.read_function = vhost_user_socket_read;
1269 template.error_function = vhost_user_socket_error;
1270 template.description = format (0, "vhost user process");
1274 vlib_process_wait_for_event_or_clock (vm, timeout);
1275 vlib_process_get_events (vm, &event_data);
1276 vec_reset_length (event_data);
1281 pool_foreach (vui, vum->vhost_user_interfaces) {
1283 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1284 if (vui->clib_file_index == ~0)
1287 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1290 * 1st time error or new error for this interface,
1291 * spit out the message and record the error
1293 if (!vui->sock_errno || (vui->sock_errno != errno))
1296 ("Error: Could not open unix socket for %s",
1297 vui->sock_filename);
1298 vui->sock_errno = errno;
1303 /* try to connect */
1304 strncpy (sun.sun_path, (char *) vui->sock_filename,
1305 sizeof (sun.sun_path) - 1);
1306 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1308 /* Avoid hanging VPP if the other end does not accept */
1309 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1310 clib_unix_warning ("fcntl");
1312 if (connect (sockfd, (struct sockaddr *) &sun,
1313 sizeof (struct sockaddr_un)) == 0)
1315 /* Set the socket to blocking as it was before */
1316 if (fcntl(sockfd, F_SETFL, 0) < 0)
1317 clib_unix_warning ("fcntl2");
1319 vui->sock_errno = 0;
1320 template.file_descriptor = sockfd;
1321 template.private_data =
1322 vui - vhost_user_main.vhost_user_interfaces;
1323 vui->clib_file_index = clib_file_add (&file_main, &template);
1326 /* This sockfd is considered consumed */
1331 vui->sock_errno = errno;
1336 /* check if socket is alive */
1338 socklen_t len = sizeof (error);
1339 int fd = UNIX_GET_FD(vui->clib_file_index);
1341 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1345 vu_log_debug (vui, "getsockopt returned %d", retval);
1346 vhost_user_if_disconnect (vui);
1357 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1358 .function = vhost_user_process,
1359 .type = VLIB_NODE_TYPE_PROCESS,
1360 .name = "vhost-user-process",
1365 * Disables and reset interface structure.
1366 * It can then be either init again, or removed from used interfaces.
1369 vhost_user_term_if (vhost_user_intf_t * vui)
1372 vhost_user_main_t *vum = &vhost_user_main;
1374 // disconnect interface sockets
1375 vhost_user_if_disconnect (vui);
1376 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1377 vhost_user_update_iface_state (vui);
1379 for (q = 0; q < vui->num_qid; q++)
1381 clib_spinlock_free (&vui->vrings[q].vring_lock);
1384 if (vui->unix_server_index != ~0)
1386 //Close server socket
1387 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1388 vui->unix_server_index);
1389 clib_file_del (&file_main, uf);
1390 vui->unix_server_index = ~0;
1391 unlink (vui->sock_filename);
1394 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1399 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1401 vhost_user_main_t *vum = &vhost_user_main;
1402 vhost_user_intf_t *vui;
1404 vnet_hw_interface_t *hwif;
1409 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1410 || hwif->dev_class_index != vhost_user_device_class.index)
1411 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1413 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1415 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1416 hwif->name, hwif->dev_instance);
1418 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1420 vhost_user_vring_t *txvq = &vui->vrings[qid];
1422 if (txvq->qid == -1)
1424 if ((vum->ifq_count > 0) &&
1425 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1426 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1429 // Stop the timer if there is no more interrupt interface/queue
1430 if ((vum->ifq_count == 0) &&
1431 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1433 vlib_process_signal_event (vm,
1434 vhost_user_send_interrupt_node.index,
1435 VHOST_USER_EVENT_STOP_TIMER, 0);
1441 // Disable and reset interface
1442 vhost_user_term_if (vui);
1444 // Reset renumbered iface
1445 if (hwif->dev_instance <
1446 vec_len (vum->show_dev_instance_by_real_dev_instance))
1447 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1449 // Delete ethernet interface
1450 ethernet_delete_interface (vnm, vui->hw_if_index);
1453 vec_free (vui->vrings);
1456 pool_put (vum->vhost_user_interfaces, vui);
1461 static clib_error_t *
1462 vhost_user_exit (vlib_main_t * vm)
1464 vnet_main_t *vnm = vnet_get_main ();
1465 vhost_user_main_t *vum = &vhost_user_main;
1466 vhost_user_intf_t *vui;
1468 vlib_worker_thread_barrier_sync (vlib_get_main ());
1470 pool_foreach (vui, vum->vhost_user_interfaces) {
1471 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1474 vlib_worker_thread_barrier_release (vlib_get_main ());
1478 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1481 * Open server unix socket on specified sock_filename.
1484 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1487 struct sockaddr_un un = { };
1489 /* create listening socket */
1490 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1491 return VNET_API_ERROR_SYSCALL_ERROR_1;
1493 un.sun_family = AF_UNIX;
1494 strncpy ((char *) un.sun_path, (char *) sock_filename,
1495 sizeof (un.sun_path) - 1);
1497 /* remove if exists */
1498 unlink ((char *) sock_filename);
1500 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1502 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1506 if (listen (fd, 1) == -1)
1508 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1521 * Create ethernet interface for vhost user interface.
1524 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1525 vhost_user_intf_t *vui,
1526 vhost_user_create_if_args_t *args)
1528 vhost_user_main_t *vum = &vhost_user_main;
1530 clib_error_t *error;
1532 /* create hw and sw interface */
1533 if (args->use_custom_mac)
1535 clib_memcpy (hwaddr, args->hwaddr, 6);
1539 random_u32 (&vum->random);
1540 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1545 error = ethernet_register_interface
1547 vhost_user_device_class.index,
1548 vui - vum->vhost_user_interfaces /* device instance */ ,
1549 hwaddr /* ethernet address */ ,
1550 &vui->hw_if_index, 0 /* flag change */ );
1553 clib_error_report (error);
1557 * Initialize vui with specified attributes
1560 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1561 int server_sock_fd, vhost_user_create_if_args_t * args,
1564 vnet_sw_interface_t *sw;
1566 vhost_user_main_t *vum = &vhost_user_main;
1567 vnet_hw_interface_t *hw;
1569 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1570 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1571 if (server_sock_fd != -1)
1573 clib_file_t template = { 0 };
1574 template.read_function = vhost_user_socksvr_accept_ready;
1575 template.file_descriptor = server_sock_fd;
1576 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1577 template.description = format (0, "vhost user %d", sw);
1578 vui->unix_server_index = clib_file_add (&file_main, &template);
1582 vui->unix_server_index = ~0;
1585 vui->sw_if_index = sw->sw_if_index;
1586 strncpy (vui->sock_filename, args->sock_filename,
1587 ARRAY_LEN (vui->sock_filename) - 1);
1588 vui->sock_errno = 0;
1590 vui->feature_mask = args->feature_mask;
1591 vui->clib_file_index = ~0;
1592 vui->log_base_addr = 0;
1593 vui->if_index = vui - vum->vhost_user_interfaces;
1594 vui->enable_gso = args->enable_gso;
1595 vui->enable_event_idx = args->enable_event_idx;
1596 vui->enable_packed = args->enable_packed;
1598 * enable_gso takes precedence over configurable feature mask if there
1600 * if feature mask disables gso, but enable_gso is configured,
1601 * then gso is enable
1602 * if feature mask enables gso, but enable_gso is not configured,
1603 * then gso is enable
1605 * if gso is enable via feature mask, it must enable both host and guest
1606 * gso feature mask, we don't support one sided GSO or partial GSO.
1608 if ((vui->enable_gso == 0) &&
1609 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1610 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1611 vui->enable_gso = 1;
1612 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1613 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1616 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1617 CLIB_CACHE_LINE_BYTES);
1619 for (q = 0; q < vec_len (vui->vrings); q++)
1620 vhost_user_vring_init (vui, q);
1622 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1623 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1626 *sw_if_index = vui->sw_if_index;
1628 vec_validate (vui->per_cpu_tx_qid,
1629 vlib_get_thread_main ()->n_vlib_mains - 1);
1630 vhost_user_tx_thread_placement (vui);
1634 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1635 vhost_user_create_if_args_t * args)
1637 vhost_user_intf_t *vui = NULL;
1640 int server_sock_fd = -1;
1641 vhost_user_main_t *vum = &vhost_user_main;
1644 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1646 return VNET_API_ERROR_INVALID_ARGUMENT;
1649 if_index = mhash_get (&vum->if_index_by_sock_name,
1650 (void *) args->sock_filename);
1653 vui = &vum->vhost_user_interfaces[*if_index];
1654 args->sw_if_index = vui->sw_if_index;
1655 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1658 if (args->is_server)
1661 vhost_user_init_server_sock (args->sock_filename,
1662 &server_sock_fd)) != 0)
1668 /* Protect the uninitialized vui from being dispatched by rx/tx */
1669 vlib_worker_thread_barrier_sync (vm);
1670 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1671 vhost_user_create_ethernet (vnm, vm, vui, args);
1672 vlib_worker_thread_barrier_release (vm);
1674 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1675 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1676 vhost_user_rx_thread_placement (vui, 1);
1679 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1681 args->sw_if_index = sw_if_idx;
1683 // Process node must connect
1684 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1690 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1691 vhost_user_create_if_args_t * args)
1693 vhost_user_main_t *vum = &vhost_user_main;
1694 vhost_user_intf_t *vui = NULL;
1696 int server_sock_fd = -1;
1698 vnet_hw_interface_t *hwif;
1701 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1703 || hwif->dev_class_index != vhost_user_device_class.index)
1704 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1706 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1707 return VNET_API_ERROR_INVALID_ARGUMENT;
1709 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1712 * Disallow changing the interface to have the same path name
1713 * as other interface
1715 if_index = mhash_get (&vum->if_index_by_sock_name,
1716 (void *) args->sock_filename);
1717 if (if_index && (*if_index != vui->if_index))
1718 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1720 // First try to open server socket
1721 if (args->is_server)
1722 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1723 &server_sock_fd)) != 0)
1726 vhost_user_term_if (vui);
1727 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1730 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1732 // Process node must connect
1733 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1739 vhost_user_connect_command_fn (vlib_main_t * vm,
1740 unformat_input_t * input,
1741 vlib_cli_command_t * cmd)
1743 vnet_main_t *vnm = vnet_get_main ();
1744 unformat_input_t _line_input, *line_input = &_line_input;
1745 clib_error_t *error = NULL;
1746 vhost_user_create_if_args_t args = { 0 };
1749 /* Get a line of input. */
1750 if (!unformat_user (input, unformat_line_input, line_input))
1753 args.feature_mask = (u64) ~ (0ULL);
1754 args.custom_dev_instance = ~0;
1755 /* GSO feature is disable by default */
1756 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1757 /* packed-ring feature is disable by default */
1758 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1759 /* event_idx feature is disable by default */
1760 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1762 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1764 if (unformat (line_input, "socket %s", &args.sock_filename))
1766 else if (unformat (line_input, "server"))
1768 else if (unformat (line_input, "gso"))
1769 args.enable_gso = 1;
1770 else if (unformat (line_input, "packed"))
1771 args.enable_packed = 1;
1772 else if (unformat (line_input, "event-idx"))
1773 args.enable_event_idx = 1;
1774 else if (unformat (line_input, "feature-mask 0x%llx",
1775 &args.feature_mask))
1777 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1779 args.use_custom_mac = 1;
1780 else if (unformat (line_input, "renumber %d",
1781 &args.custom_dev_instance))
1785 error = clib_error_return (0, "unknown input `%U'",
1786 format_unformat_error, line_input);
1791 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1793 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1797 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1801 vec_free (args.sock_filename);
1802 unformat_free (line_input);
1808 vhost_user_delete_command_fn (vlib_main_t * vm,
1809 unformat_input_t * input,
1810 vlib_cli_command_t * cmd)
1812 unformat_input_t _line_input, *line_input = &_line_input;
1813 u32 sw_if_index = ~0;
1814 vnet_main_t *vnm = vnet_get_main ();
1815 clib_error_t *error = NULL;
1817 /* Get a line of input. */
1818 if (!unformat_user (input, unformat_line_input, line_input))
1821 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1823 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1826 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1829 vnet_hw_interface_t *hwif =
1830 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1832 vhost_user_device_class.index != hwif->dev_class_index)
1834 error = clib_error_return (0, "Not a vhost interface");
1840 error = clib_error_return (0, "unknown input `%U'",
1841 format_unformat_error, line_input);
1846 vhost_user_delete_if (vnm, vm, sw_if_index);
1849 unformat_free (line_input);
1855 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1856 vhost_user_intf_details_t ** out_vuids)
1859 vhost_user_main_t *vum = &vhost_user_main;
1860 vhost_user_intf_t *vui;
1861 vhost_user_intf_details_t *r_vuids = NULL;
1862 vhost_user_intf_details_t *vuid = NULL;
1863 u32 *hw_if_indices = 0;
1864 vnet_hw_interface_t *hi;
1870 pool_foreach (vui, vum->vhost_user_interfaces)
1871 vec_add1 (hw_if_indices, vui->hw_if_index);
1873 for (i = 0; i < vec_len (hw_if_indices); i++)
1875 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1876 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1878 vec_add2 (r_vuids, vuid, 1);
1879 vuid->sw_if_index = vui->sw_if_index;
1880 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1881 vuid->features = vui->features;
1882 vuid->num_regions = vui->nregions;
1883 vuid->is_server = vui->unix_server_index != ~0;
1884 vuid->sock_errno = vui->sock_errno;
1885 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1886 "%s", vui->sock_filename);
1887 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1888 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1889 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1892 vec_free (hw_if_indices);
1894 *out_vuids = r_vuids;
1900 format_vhost_user_desc (u8 * s, va_list * args)
1902 char *fmt = va_arg (*args, char *);
1903 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1904 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1905 int idx = va_arg (*args, int);
1906 u32 *mem_hint = va_arg (*args, u32 *);
1908 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1909 desc_table[idx].flags, desc_table[idx].next,
1910 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1916 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1918 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1919 int callfd = UNIX_GET_FD (vq->callfd_idx);
1921 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1926 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1927 int show_descr, int show_verbose)
1933 vring_desc_t *desc_table;
1934 vhost_user_vring_t *vq = &vui->vrings[q];
1936 if (vq->avail && vq->used)
1937 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1938 "used event idx %u used.idx %d\n", vq->avail->flags,
1939 vhost_user_avail_event_idx (vq), vq->avail->idx,
1940 vhost_user_used_event_idx (vq), vq->used->idx);
1942 vhost_user_show_fds (vm, vq);
1946 vlib_cli_output (vm, "\n descriptor table:\n");
1947 vlib_cli_output (vm,
1948 " slot addr len flags next "
1950 vlib_cli_output (vm,
1951 " ===== ================== ===== ====== ===== "
1952 "==================\n");
1953 for (j = 0; j < vq->qsz_mask + 1; j++)
1955 desc_table = vq->desc;
1956 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1957 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1958 desc_table, j, &mem_hint);
1959 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1961 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1962 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1965 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1968 (vm, "%U", format_vhost_user_desc,
1969 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1970 desc_table, idx, &mem_hint);
1972 if (n_entries >= 20)
1973 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1982 format_vhost_user_packed_desc (u8 * s, va_list * args)
1984 char *fmt = va_arg (*args, char *);
1985 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1986 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1987 int idx = va_arg (*args, int);
1988 u32 *mem_hint = va_arg (*args, u32 *);
1990 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1991 desc_table[idx].flags, desc_table[idx].id,
1992 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1998 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
2000 u32 flags = va_arg (*args, u32);
2006 static event_idx_flags event_idx_array[] = {
2007 #define _(s,v) { .str = #s, .value = v, },
2008 foreach_virtio_event_idx_flags
2011 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2013 if (flags < num_entries)
2014 s = format (s, "%s", event_idx_array[flags].str);
2016 s = format (s, "%u", flags);
2021 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2022 int show_descr, int show_verbose)
2028 vring_packed_desc_t *desc_table;
2029 vhost_user_vring_t *vq = &vui->vrings[q];
2030 u16 off_wrap, event_idx;
2032 off_wrap = vq->avail_event->off_wrap;
2033 event_idx = off_wrap & 0x7fff;
2034 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2035 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2036 (u32) vq->avail_event->flags, off_wrap, event_idx);
2038 off_wrap = vq->used_event->off_wrap;
2039 event_idx = off_wrap & 0x7fff;
2040 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2041 "used event idx %u\n", format_vhost_user_event_idx_flags,
2042 (u32) vq->used_event->flags, off_wrap, event_idx);
2044 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2045 vq->avail_wrap_counter, vq->used_wrap_counter);
2047 vhost_user_show_fds (vm, vq);
2051 vlib_cli_output (vm, "\n descriptor table:\n");
2052 vlib_cli_output (vm,
2053 " slot addr len flags id "
2055 vlib_cli_output (vm,
2056 " ===== ================== ===== ====== ===== "
2057 "==================\n");
2058 for (j = 0; j < vq->qsz_mask + 1; j++)
2060 desc_table = vq->packed_desc;
2061 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2062 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2063 desc_table, j, &mem_hint);
2064 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2066 n_entries = desc_table[j].len >> 4;
2067 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2070 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2073 (vm, "%U", format_vhost_user_packed_desc,
2074 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2075 desc_table, idx, &mem_hint);
2077 if (n_entries >= 20)
2078 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2087 show_vhost_user_command_fn (vlib_main_t * vm,
2088 unformat_input_t * input,
2089 vlib_cli_command_t * cmd)
2091 clib_error_t *error = 0;
2092 vnet_main_t *vnm = vnet_get_main ();
2093 vhost_user_main_t *vum = &vhost_user_main;
2094 vhost_user_intf_t *vui;
2095 u32 hw_if_index, *hw_if_indices = 0;
2096 vnet_hw_interface_t *hi;
2101 int show_verbose = 0;
2107 struct feat_struct *feat_entry;
2109 static struct feat_struct feat_array[] = {
2110 #define _(s,b) { .str = #s, .bit = b, },
2111 foreach_virtio_net_features
2116 #define foreach_protocol_feature \
2117 _(VHOST_USER_PROTOCOL_F_MQ) \
2118 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2120 static struct feat_struct proto_feat_array[] = {
2121 #define _(s) { .str = #s, .bit = s},
2122 foreach_protocol_feature
2127 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2130 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2132 hi = vnet_get_hw_interface (vnm, hw_if_index);
2133 if (vhost_user_device_class.index != hi->dev_class_index)
2135 error = clib_error_return (0, "unknown input `%U'",
2136 format_unformat_error, input);
2139 vec_add1 (hw_if_indices, hw_if_index);
2141 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2143 else if (unformat (input, "verbose"))
2147 error = clib_error_return (0, "unknown input `%U'",
2148 format_unformat_error, input);
2152 if (vec_len (hw_if_indices) == 0)
2154 pool_foreach (vui, vum->vhost_user_interfaces)
2155 vec_add1 (hw_if_indices, vui->hw_if_index);
2157 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2158 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2159 vum->coalesce_frames, vum->coalesce_time);
2160 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2162 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2164 for (i = 0; i < vec_len (hw_if_indices); i++)
2166 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2167 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2168 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2169 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2171 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2172 if (vui->enable_gso)
2173 vlib_cli_output (vm, " GSO enable");
2174 if (vui->enable_packed)
2175 vlib_cli_output (vm, " Packed ring enable");
2176 if (vui->enable_event_idx)
2177 vlib_cli_output (vm, " Event index enable");
2179 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2180 " features mask (0x%llx): \n"
2181 " features (0x%llx): \n",
2182 vui->virtio_net_hdr_sz, vui->feature_mask,
2185 feat_entry = (struct feat_struct *) &feat_array;
2186 while (feat_entry->str)
2188 if (vui->features & (1ULL << feat_entry->bit))
2189 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2194 vlib_cli_output (vm, " protocol features (0x%llx)",
2195 vui->protocol_features);
2196 feat_entry = (struct feat_struct *) &proto_feat_array;
2197 while (feat_entry->str)
2199 if (vui->protocol_features & (1ULL << feat_entry->bit))
2200 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2205 vlib_cli_output (vm, "\n");
2207 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2209 (vui->unix_server_index != ~0) ? "server" : "client",
2210 strerror (vui->sock_errno));
2212 vlib_cli_output (vm, " rx placement: ");
2214 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2216 vnet_main_t *vnm = vnet_get_main ();
2218 vhost_user_vring_t *txvq = &vui->vrings[qid];
2220 if (txvq->qid == -1)
2223 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
2224 vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2225 qid, format_vnet_hw_if_rx_mode, txvq->mode);
2228 vlib_cli_output (vm, " tx placement: %s\n",
2229 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2231 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2233 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2234 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2237 vlib_cli_output (vm, "\n");
2239 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2243 vlib_cli_output (vm,
2244 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2245 vlib_cli_output (vm,
2246 " ====== ===== ================== ================== ================== ================== ==================\n");
2248 for (j = 0; j < vui->nregions; j++)
2250 vlib_cli_output (vm,
2251 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2252 j, vui->region_mmap_fd[j],
2253 vui->regions[j].guest_phys_addr,
2254 vui->regions[j].memory_size,
2255 vui->regions[j].userspace_addr,
2256 vui->regions[j].mmap_offset,
2257 pointer_to_uword (vui->region_mmap_addr[j]));
2259 for (q = 0; q < vui->num_qid; q++)
2261 if (!vui->vrings[q].started)
2264 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2265 (q & 1) ? "RX" : "TX",
2266 vui->vrings[q].enabled ? "" : " disabled");
2268 vlib_cli_output (vm,
2269 " qsz %d last_avail_idx %d last_used_idx %d"
2271 vui->vrings[q].qsz_mask + 1,
2272 vui->vrings[q].last_avail_idx,
2273 vui->vrings[q].last_used_idx,
2274 vui->vrings[q].last_kick);
2276 if (vhost_user_is_packed_ring_supported (vui))
2277 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2280 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2282 vlib_cli_output (vm, "\n");
2285 vec_free (hw_if_indices);
2294 * Create a vHost User interface. Once created, a new virtual interface
2295 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2296 * is the next free index.
2298 * There are several parameters associated with a vHost interface:
2300 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2301 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2302 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2303 * hypervisor will create the socket if it does not already exist. The VPP code
2304 * is indifferent to the file location. However, if SELinux is enabled, then the
2305 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2307 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2308 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2309 * mode, the VM can be reset without tearing down the vHost Interface. In
2310 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2311 * tearing down the vHost Interface.
2313 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2314 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2315 * parameter not be used except by experienced users. By default, all supported
2316 * features will be advertised. Otherwise, provide the set of features desired.
2317 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2318 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2319 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2320 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2321 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2322 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2323 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2324 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2325 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2327 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2328 * X:X:X:X:X:X unix or X.X.X cisco format.
2330 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2331 * in the name to be specified. If instance already exists, name will be used
2332 * anyway and multiple instances will have the same name. Use with caution.
2335 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2336 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2337 * VirtualEthernet0/0/0
2339 * Example of how to create a vhost interface with VPP as the server and with just
2340 * multiple queues enabled:
2341 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2342 * VirtualEthernet0/0/1
2344 * Once the vHost interface is created, enable the interface using:
2345 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2348 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2349 .path = "create vhost-user",
2350 .short_help = "create vhost-user socket <socket-filename> [server] "
2351 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2352 "[packed] [event-idx]",
2353 .function = vhost_user_connect_command_fn,
2359 * Delete a vHost User interface using the interface name or the
2360 * software interface index. Use the '<em>show interface</em>'
2361 * command to determine the software interface index. On deletion,
2362 * the linux socket will not be deleted.
2365 * Example of how to delete a vhost interface by name:
2366 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2367 * Example of how to delete a vhost interface by software interface index:
2368 * @cliexcmd{delete vhost-user sw_if_index 1}
2371 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2372 .path = "delete vhost-user",
2373 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2374 .function = vhost_user_delete_command_fn,
2378 * Display the attributes of a single vHost User interface (provide interface
2379 * name), multiple vHost User interfaces (provide a list of interface names seperated
2380 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2381 * vHost interfaces).
2385 * Example of how to display a vhost interface:
2386 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2387 * Virtio vhost-user interfaces
2389 * coalesce frames 32 time 1e-3
2390 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2391 * virtio_net_hdr_sz 12
2392 * features mask (0xffffffffffffffff):
2393 * features (0x50408000):
2394 * VIRTIO_NET_F_MRG_RXBUF (15)
2395 * VIRTIO_NET_F_MQ (22)
2396 * VIRTIO_F_INDIRECT_DESC (28)
2397 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2398 * protocol features (0x3)
2399 * VHOST_USER_PROTOCOL_F_MQ (0)
2400 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2402 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2405 * thread 1 on vring 1
2406 * thread 1 on vring 5
2407 * thread 2 on vring 3
2408 * thread 2 on vring 7
2409 * tx placement: spin-lock
2410 * thread 0 on vring 0
2411 * thread 1 on vring 2
2412 * thread 2 on vring 0
2414 * Memory regions (total 2)
2415 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2416 * ====== ===== ================== ================== ================== ================== ==================
2417 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2418 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2421 * qsz 256 last_avail_idx 0 last_used_idx 0
2422 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2423 * kickfd 62 callfd 64 errfd -1
2426 * qsz 256 last_avail_idx 0 last_used_idx 0
2427 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2428 * kickfd 65 callfd 66 errfd -1
2431 * qsz 256 last_avail_idx 0 last_used_idx 0
2432 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2433 * kickfd 63 callfd 70 errfd -1
2436 * qsz 256 last_avail_idx 0 last_used_idx 0
2437 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2438 * kickfd 72 callfd 74 errfd -1
2440 * Virtqueue 4 (TX disabled)
2441 * qsz 256 last_avail_idx 0 last_used_idx 0
2442 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2443 * kickfd 76 callfd 78 errfd -1
2445 * Virtqueue 5 (RX disabled)
2446 * qsz 256 last_avail_idx 0 last_used_idx 0
2447 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2448 * kickfd 80 callfd 82 errfd -1
2450 * Virtqueue 6 (TX disabled)
2451 * qsz 256 last_avail_idx 0 last_used_idx 0
2452 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2453 * kickfd 84 callfd 86 errfd -1
2455 * Virtqueue 7 (RX disabled)
2456 * qsz 256 last_avail_idx 0 last_used_idx 0
2457 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2458 * kickfd 88 callfd 90 errfd -1
2462 * The optional '<em>descriptors</em>' parameter will display the same output as
2463 * the previous example but will include the descriptor table for each queue.
2464 * The output is truncated below:
2465 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2466 * Virtio vhost-user interfaces
2468 * coalesce frames 32 time 1e-3
2469 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2470 * virtio_net_hdr_sz 12
2471 * features mask (0xffffffffffffffff):
2472 * features (0x50408000):
2473 * VIRTIO_NET_F_MRG_RXBUF (15)
2474 * VIRTIO_NET_F_MQ (22)
2477 * qsz 256 last_avail_idx 0 last_used_idx 0
2478 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2479 * kickfd 62 callfd 64 errfd -1
2482 * id addr len flags next user_addr
2483 * ===== ================== ===== ====== ===== ==================
2484 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2485 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2486 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2487 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2488 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2489 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2490 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2491 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2492 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2493 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2494 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2496 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2497 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2498 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2499 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2500 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2501 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2502 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2505 * qsz 256 last_avail_idx 0 last_used_idx 0
2511 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2512 .path = "show vhost-user",
2513 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2514 "[[descriptors] [verbose]]",
2515 .function = show_vhost_user_command_fn,
2520 static clib_error_t *
2521 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2523 vhost_user_main_t *vum = &vhost_user_main;
2525 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2527 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2529 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2531 else if (unformat (input, "dont-dump-memory"))
2532 vum->dont_dump_vhost_user_memory = 1;
2534 return clib_error_return (0, "unknown input `%U'",
2535 format_unformat_error, input);
2541 /* vhost-user { ... } configuration. */
2542 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2545 vhost_user_unmap_all (void)
2547 vhost_user_main_t *vum = &vhost_user_main;
2548 vhost_user_intf_t *vui;
2550 if (vum->dont_dump_vhost_user_memory)
2552 pool_foreach (vui, vum->vhost_user_interfaces)
2553 unmap_all_mem_regions (vui);
2558 * fd.io coding-style-patch-verification: ON
2561 * eval: (c-set-style "gnu")