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 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
520 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
521 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
525 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
526 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
528 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
530 vhost_user_update_iface_state (vui);
533 case VHOST_USER_SET_MEM_TABLE:
534 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
535 vui->hw_if_index, msg.memory.nregions);
537 if ((msg.memory.nregions < 1) ||
538 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
540 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
541 VHOST_MEMORY_MAX_NREGIONS);
545 if (msg.memory.nregions != number_of_fds)
547 vu_log_debug (vui, "each memory region must have FD");
551 /* Do the mmap without barrier sync */
552 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
553 for (i = 0; i < msg.memory.nregions; i++)
555 long page_sz = get_huge_page_size (fds[i]);
557 /* align size to page */
558 ssize_t map_sz = (msg.memory.regions[i].memory_size +
559 msg.memory.regions[i].mmap_offset +
560 page_sz - 1) & ~(page_sz - 1);
562 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
563 MAP_SHARED, fds[i], 0);
564 if (region_mmap_addr[i] == MAP_FAILED)
566 vu_log_err (vui, "failed to map memory. errno is %d", errno);
567 for (j = 0; j < i; j++)
568 munmap (region_mmap_addr[j], map_sz);
571 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
572 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
573 region_mmap_addr[i], page_sz);
576 vlib_worker_thread_barrier_sync (vm);
577 unmap_all_mem_regions (vui);
578 for (i = 0; i < msg.memory.nregions; i++)
580 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
581 sizeof (vhost_user_memory_region_t));
583 vui->region_mmap_addr[i] = region_mmap_addr[i];
584 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
585 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
586 vui->regions[i].memory_size;
588 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
589 vui->region_mmap_fd[i] = fds[i];
595 * Re-compute desc, used, and avail descriptor table if vring address
598 for (q = 0; q < vui->num_qid; q++)
600 if (vui->vrings[q].desc_user_addr &&
601 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
603 vui->vrings[q].desc =
604 map_user_mem (vui, vui->vrings[q].desc_user_addr);
605 vui->vrings[q].used =
606 map_user_mem (vui, vui->vrings[q].used_user_addr);
607 vui->vrings[q].avail =
608 map_user_mem (vui, vui->vrings[q].avail_user_addr);
611 vlib_worker_thread_barrier_release (vm);
614 case VHOST_USER_SET_VRING_NUM:
615 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
616 vui->hw_if_index, msg.state.index, msg.state.num);
618 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
619 (msg.state.num == 0) || /* it cannot be zero */
620 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
621 (msg.state.index >= vui->num_qid))
623 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
624 " %d, msg.state.index %d, curruent max q %d",
625 msg.state.num, msg.state.index, vui->num_qid);
628 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
631 case VHOST_USER_SET_VRING_ADDR:
632 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
633 vui->hw_if_index, msg.state.index);
635 if (msg.state.index >= vui->num_qid)
637 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
638 " %u >= %u", msg.state.index, vui->num_qid);
642 if (msg.size < sizeof (msg.addr))
644 vu_log_debug (vui, "vhost message is too short (%d < %d)",
645 msg.size, sizeof (msg.addr));
649 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
650 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
651 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
653 if ((desc == NULL) || (used == NULL) || (avail == NULL))
655 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
660 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
661 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
662 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
664 vlib_worker_thread_barrier_sync (vm);
665 vui->vrings[msg.state.index].desc = desc;
666 vui->vrings[msg.state.index].used = used;
667 vui->vrings[msg.state.index].avail = avail;
669 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
670 vui->vrings[msg.state.index].log_used =
671 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
673 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
674 the ring is initialized in an enabled state. */
675 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
676 vui->vrings[msg.state.index].enabled = 1;
678 vui->vrings[msg.state.index].last_used_idx =
679 vui->vrings[msg.state.index].last_avail_idx =
680 vui->vrings[msg.state.index].used->idx;
681 vui->vrings[msg.state.index].last_kick =
682 vui->vrings[msg.state.index].last_used_idx;
684 /* tell driver that we don't want interrupts */
685 if (vhost_user_is_packed_ring_supported (vui))
686 vui->vrings[msg.state.index].used_event->flags =
687 VRING_EVENT_F_DISABLE;
689 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
690 vlib_worker_thread_barrier_release (vm);
691 vhost_user_update_iface_state (vui);
694 case VHOST_USER_SET_OWNER:
695 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
698 case VHOST_USER_RESET_OWNER:
699 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
703 case VHOST_USER_SET_VRING_CALL:
704 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
705 vui->hw_if_index, msg.u64);
707 q = (u8) (msg.u64 & 0xFF);
708 if (vui->num_qid > q)
710 /* if there is old fd, delete and close it */
711 if (vui->vrings[q].callfd_idx != ~0)
713 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
714 vui->vrings[q].callfd_idx);
715 clib_file_del (&file_main, uf);
716 vui->vrings[q].callfd_idx = ~0;
719 else if (vec_len (vui->vrings) > q)
721 /* grow vrings by pair (RX + TX) */
722 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
726 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
729 * Double the array size if it is less than 64 entries.
730 * Slow down thereafter.
732 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
733 new_max_q = vec_len (vui->vrings) << 1;
735 new_max_q = vec_len (vui->vrings) +
736 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
737 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
738 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
740 /* sync with the worker threads, vrings may move due to realloc */
741 vlib_worker_thread_barrier_sync (vm);
742 vec_validate_aligned (vui->vrings, new_max_q - 1,
743 CLIB_CACHE_LINE_BYTES);
744 vlib_worker_thread_barrier_release (vm);
746 for (i = old_max_q; i < vec_len (vui->vrings); i++)
747 vhost_user_vring_init (vui, i);
749 /* grow vrings by pair (RX + TX) */
750 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
753 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
755 if (number_of_fds != 1)
757 vu_log_debug (vui, "More than one fd received !");
761 template.read_function = vhost_user_callfd_read_ready;
762 template.file_descriptor = fds[0];
763 template.private_data =
764 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
765 template.description = format (0, "vhost user");
766 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
769 vui->vrings[q].callfd_idx = ~0;
772 case VHOST_USER_SET_VRING_KICK:
773 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
774 vui->hw_if_index, msg.u64);
776 q = (u8) (msg.u64 & 0xFF);
777 if (q >= vui->num_qid)
779 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
780 " %u >= %u", q, vui->num_qid);
784 if (vui->vrings[q].kickfd_idx != ~0)
786 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
787 vui->vrings[q].kickfd_idx);
788 clib_file_del (&file_main, uf);
789 vui->vrings[q].kickfd_idx = ~0;
792 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
794 if (number_of_fds != 1)
796 vu_log_debug (vui, "More than one fd received !");
800 template.read_function = vhost_user_kickfd_read_ready;
801 template.file_descriptor = fds[0];
802 template.private_data =
803 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
805 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
809 //When no kickfd is set, the queue is initialized as started
810 vui->vrings[q].kickfd_idx = ~0;
811 vui->vrings[q].started = 1;
812 vhost_user_thread_placement (vui, q);
814 vhost_user_update_iface_state (vui);
817 case VHOST_USER_SET_VRING_ERR:
818 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
819 vui->hw_if_index, msg.u64);
821 q = (u8) (msg.u64 & 0xFF);
822 if (q >= vui->num_qid)
824 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
825 " %u >= %u", q, vui->num_qid);
829 if (vui->vrings[q].errfd != -1)
830 close (vui->vrings[q].errfd);
832 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
834 if (number_of_fds != 1)
837 vui->vrings[q].errfd = fds[0];
840 vui->vrings[q].errfd = -1;
843 case VHOST_USER_SET_VRING_BASE:
845 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
846 vui->hw_if_index, msg.state.index, msg.state.num);
847 if (msg.state.index >= vui->num_qid)
849 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
850 " %u >= %u", msg.state.index, vui->num_qid);
853 vlib_worker_thread_barrier_sync (vm);
854 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
855 if (vhost_user_is_packed_ring_supported (vui))
859 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * | last avail idx | | last used idx | |
862 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
865 * avail wrap counter used wrap counter
867 /* last avail idx at bit 0-14. */
868 vui->vrings[msg.state.index].last_avail_idx =
869 msg.state.num & 0x7fff;
870 /* avail wrap counter at bit 15 */
871 vui->vrings[msg.state.index].avail_wrap_counter =
872 ! !(msg.state.num & (1 << 15));
875 * Although last_used_idx is passed in the upper 16 bits in qemu
876 * implementation, in practice, last_avail_idx and last_used_idx are
877 * usually the same. As a result, DPDK does not bother to pass us
878 * last_used_idx. The spec is not clear on thex coding. I figured it
879 * out by reading the qemu code. So let's just read last_avail_idx
880 * and set last_used_idx equals to last_avail_idx.
882 vui->vrings[msg.state.index].last_used_idx =
883 vui->vrings[msg.state.index].last_avail_idx;
884 vui->vrings[msg.state.index].last_kick =
885 vui->vrings[msg.state.index].last_used_idx;
886 vui->vrings[msg.state.index].used_wrap_counter =
887 vui->vrings[msg.state.index].avail_wrap_counter;
889 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
890 vui->vrings[msg.state.index].avail_wrap_counter =
893 vlib_worker_thread_barrier_release (vm);
896 case VHOST_USER_GET_VRING_BASE:
897 if (msg.state.index >= vui->num_qid)
899 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
900 " %u >= %u", msg.state.index, vui->num_qid);
904 /* protection is needed to prevent rx/tx from changing last_avail_idx */
905 vlib_worker_thread_barrier_sync (vm);
907 * Copy last_avail_idx from the vring before closing it because
908 * closing the vring also initializes the vring last_avail_idx
910 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
911 if (vhost_user_is_packed_ring_supported (vui))
914 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
915 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
917 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
918 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
921 msg.size = sizeof (msg.state);
924 * Spec says: Client must [...] stop ring upon receiving
925 * VHOST_USER_GET_VRING_BASE
927 vhost_user_vring_close (vui, msg.state.index);
928 vlib_worker_thread_barrier_release (vm);
930 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
931 vui->hw_if_index, msg.state.index, msg.state.num);
933 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
934 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
936 vu_log_debug (vui, "could not send message response");
939 vhost_user_update_iface_state (vui);
942 case VHOST_USER_NONE:
943 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
946 case VHOST_USER_SET_LOG_BASE:
947 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
950 if (msg.size != sizeof (msg.log))
952 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
953 " %d instead of %d", msg.size, sizeof (msg.log));
957 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
959 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
960 "VHOST_USER_SET_LOG_BASE received");
965 /* align size to page */
966 long page_sz = get_huge_page_size (fd);
968 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
970 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
973 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
974 "mapped 0x%lx", map_sz, msg.log.offset, fd,
977 if (log_base_addr == MAP_FAILED)
979 vu_log_err (vui, "failed to map memory. errno is %d", errno);
983 vlib_worker_thread_barrier_sync (vm);
984 vui->log_base_addr = log_base_addr;
985 vui->log_base_addr += msg.log.offset;
986 vui->log_size = msg.log.size;
987 vlib_worker_thread_barrier_release (vm);
990 msg.size = sizeof (msg.u64);
992 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
993 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
995 vu_log_debug (vui, "could not send message response");
1000 case VHOST_USER_SET_LOG_FD:
1001 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1004 case VHOST_USER_GET_PROTOCOL_FEATURES:
1006 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1007 (1 << VHOST_USER_PROTOCOL_F_MQ);
1008 msg.size = sizeof (msg.u64);
1009 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1010 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1012 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1013 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1015 vu_log_debug (vui, "could not send message response");
1020 case VHOST_USER_SET_PROTOCOL_FEATURES:
1021 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1022 "features 0x%016llx", vui->hw_if_index, msg.u64);
1023 vui->protocol_features = msg.u64;
1026 case VHOST_USER_GET_QUEUE_NUM:
1028 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1029 msg.size = sizeof (msg.u64);
1030 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1031 vui->hw_if_index, msg.u64);
1033 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1034 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1036 vu_log_debug (vui, "could not send message response");
1041 case VHOST_USER_SET_VRING_ENABLE:
1042 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1043 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1045 if (msg.state.index >= vui->num_qid)
1047 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1048 " %u >= %u", msg.state.index, vui->num_qid);
1052 vui->vrings[msg.state.index].enabled = msg.state.num;
1053 vhost_user_thread_placement (vui, msg.state.index);
1054 vhost_user_update_iface_state (vui);
1058 vu_log_debug (vui, "unknown vhost-user message %d received. "
1059 "closing socket", msg.request);
1066 vlib_worker_thread_barrier_sync (vm);
1067 vhost_user_if_disconnect (vui);
1068 vlib_worker_thread_barrier_release (vm);
1069 vhost_user_update_iface_state (vui);
1073 static clib_error_t *
1074 vhost_user_socket_error (clib_file_t * uf)
1076 vlib_main_t *vm = vlib_get_main ();
1077 vhost_user_main_t *vum = &vhost_user_main;
1078 vhost_user_intf_t *vui =
1079 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1081 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1082 vlib_worker_thread_barrier_sync (vm);
1083 vhost_user_if_disconnect (vui);
1084 vlib_worker_thread_barrier_release (vm);
1088 static clib_error_t *
1089 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1091 int client_fd, client_len;
1092 struct sockaddr_un client;
1093 clib_file_t template = { 0 };
1094 vhost_user_main_t *vum = &vhost_user_main;
1095 vhost_user_intf_t *vui;
1097 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1099 client_len = sizeof (client);
1100 client_fd = accept (uf->file_descriptor,
1101 (struct sockaddr *) &client,
1102 (socklen_t *) & client_len);
1105 return clib_error_return_unix (0, "accept");
1107 if (vui->clib_file_index != ~0)
1109 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1110 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1111 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1114 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1115 vui->sw_if_index, client_fd);
1116 template.read_function = vhost_user_socket_read;
1117 template.error_function = vhost_user_socket_error;
1118 template.file_descriptor = client_fd;
1119 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1120 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1121 vui->clib_file_index = clib_file_add (&file_main, &template);
1126 static clib_error_t *
1127 vhost_user_init (vlib_main_t * vm)
1129 vhost_user_main_t *vum = &vhost_user_main;
1130 vlib_thread_main_t *tm = vlib_get_thread_main ();
1132 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1134 vum->coalesce_frames = 32;
1135 vum->coalesce_time = 1e-3;
1137 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1140 vec_foreach (cpu, vum->cpus)
1142 /* This is actually not necessary as validate already zeroes it
1143 * Just keeping the loop here for later because I am lazy. */
1144 cpu->rx_buffers_len = 0;
1147 vum->random = random_default_seed ();
1149 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1155 VLIB_INIT_FUNCTION (vhost_user_init) =
1157 .runs_after = VLIB_INITS("ip4_init"),
1162 vhost_user_send_interrupt_process (vlib_main_t * vm,
1163 vlib_node_runtime_t * rt, vlib_frame_t * f)
1165 vhost_user_intf_t *vui;
1166 f64 timeout = 3153600000.0 /* 100 years */ ;
1167 uword event_type, *event_data = 0;
1168 vhost_user_main_t *vum = &vhost_user_main;
1170 f64 now, poll_time_remaining;
1176 poll_time_remaining =
1177 vlib_process_wait_for_event_or_clock (vm, timeout);
1178 event_type = vlib_process_get_events (vm, &event_data);
1179 vec_reset_length (event_data);
1182 * Use the remaining timeout if it is less than coalesce time to avoid
1183 * resetting the existing timer in the middle of expiration
1185 timeout = poll_time_remaining;
1186 if (vlib_process_suspend_time_is_zero (timeout) ||
1187 (timeout > vum->coalesce_time))
1188 timeout = vum->coalesce_time;
1190 now = vlib_time_now (vm);
1193 case VHOST_USER_EVENT_STOP_TIMER:
1197 case VHOST_USER_EVENT_START_TIMER:
1199 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1205 pool_foreach (vui, vum->vhost_user_interfaces) {
1206 next_timeout = timeout;
1207 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1209 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1210 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1212 if (txvq->qid == -1)
1214 if (txvq->n_since_last_int)
1216 if (now >= txvq->int_deadline)
1217 vhost_user_send_call (vm, vui, txvq);
1219 next_timeout = txvq->int_deadline - now;
1222 if (rxvq->n_since_last_int)
1224 if (now >= rxvq->int_deadline)
1225 vhost_user_send_call (vm, vui, rxvq);
1227 next_timeout = rxvq->int_deadline - now;
1230 if ((next_timeout < timeout) && (next_timeout > 0.0))
1231 timeout = next_timeout;
1238 clib_warning ("BUG: unhandled event type %d", event_type);
1241 /* No less than 1 millisecond */
1245 timeout = 3153600000.0;
1251 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1252 .function = vhost_user_send_interrupt_process,
1253 .type = VLIB_NODE_TYPE_PROCESS,
1254 .name = "vhost-user-send-interrupt-process",
1259 vhost_user_process (vlib_main_t * vm,
1260 vlib_node_runtime_t * rt, vlib_frame_t * f)
1262 vhost_user_main_t *vum = &vhost_user_main;
1263 vhost_user_intf_t *vui;
1264 struct sockaddr_un sun;
1266 clib_file_t template = { 0 };
1267 f64 timeout = 3153600000.0 /* 100 years */ ;
1268 uword *event_data = 0;
1271 sun.sun_family = AF_UNIX;
1272 template.read_function = vhost_user_socket_read;
1273 template.error_function = vhost_user_socket_error;
1277 vlib_process_wait_for_event_or_clock (vm, timeout);
1278 vlib_process_get_events (vm, &event_data);
1279 vec_reset_length (event_data);
1284 pool_foreach (vui, vum->vhost_user_interfaces) {
1286 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1287 if (vui->clib_file_index == ~0)
1290 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1293 * 1st time error or new error for this interface,
1294 * spit out the message and record the error
1296 if (!vui->sock_errno || (vui->sock_errno != errno))
1299 ("Error: Could not open unix socket for %s",
1300 vui->sock_filename);
1301 vui->sock_errno = errno;
1306 /* try to connect */
1307 strncpy (sun.sun_path, (char *) vui->sock_filename,
1308 sizeof (sun.sun_path) - 1);
1309 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1311 /* Avoid hanging VPP if the other end does not accept */
1312 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1313 clib_unix_warning ("fcntl");
1315 if (connect (sockfd, (struct sockaddr *) &sun,
1316 sizeof (struct sockaddr_un)) == 0)
1318 /* Set the socket to blocking as it was before */
1319 if (fcntl(sockfd, F_SETFL, 0) < 0)
1320 clib_unix_warning ("fcntl2");
1322 vui->sock_errno = 0;
1323 template.file_descriptor = sockfd;
1324 template.private_data =
1325 vui - vhost_user_main.vhost_user_interfaces;
1326 template.description = format (0, "vhost user process");
1327 vui->clib_file_index = clib_file_add (&file_main, &template);
1330 /* This sockfd is considered consumed */
1335 vui->sock_errno = errno;
1340 /* check if socket is alive */
1342 socklen_t len = sizeof (error);
1343 int fd = UNIX_GET_FD(vui->clib_file_index);
1345 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1349 vu_log_debug (vui, "getsockopt returned %d", retval);
1350 vhost_user_if_disconnect (vui);
1361 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1362 .function = vhost_user_process,
1363 .type = VLIB_NODE_TYPE_PROCESS,
1364 .name = "vhost-user-process",
1369 * Disables and reset interface structure.
1370 * It can then be either init again, or removed from used interfaces.
1373 vhost_user_term_if (vhost_user_intf_t * vui)
1376 vhost_user_main_t *vum = &vhost_user_main;
1378 // disconnect interface sockets
1379 vhost_user_if_disconnect (vui);
1380 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1381 vhost_user_update_iface_state (vui);
1383 for (q = 0; q < vui->num_qid; q++)
1385 clib_spinlock_free (&vui->vrings[q].vring_lock);
1388 if (vui->unix_server_index != ~0)
1390 //Close server socket
1391 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1392 vui->unix_server_index);
1393 clib_file_del (&file_main, uf);
1394 vui->unix_server_index = ~0;
1395 unlink (vui->sock_filename);
1398 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1403 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1405 vhost_user_main_t *vum = &vhost_user_main;
1406 vhost_user_intf_t *vui;
1408 vnet_hw_interface_t *hwif;
1413 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1414 || hwif->dev_class_index != vhost_user_device_class.index)
1415 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1417 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1419 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1420 hwif->name, hwif->dev_instance);
1422 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1424 vhost_user_vring_t *txvq = &vui->vrings[qid];
1426 if (txvq->qid == -1)
1428 if ((vum->ifq_count > 0) &&
1429 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1430 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1433 // Stop the timer if there is no more interrupt interface/queue
1434 if ((vum->ifq_count == 0) &&
1435 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1437 vlib_process_signal_event (vm,
1438 vhost_user_send_interrupt_node.index,
1439 VHOST_USER_EVENT_STOP_TIMER, 0);
1445 // Disable and reset interface
1446 vhost_user_term_if (vui);
1448 // Reset renumbered iface
1449 if (hwif->dev_instance <
1450 vec_len (vum->show_dev_instance_by_real_dev_instance))
1451 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1453 // Delete ethernet interface
1454 ethernet_delete_interface (vnm, vui->hw_if_index);
1457 vec_free (vui->vrings);
1460 pool_put (vum->vhost_user_interfaces, vui);
1465 static clib_error_t *
1466 vhost_user_exit (vlib_main_t * vm)
1468 vnet_main_t *vnm = vnet_get_main ();
1469 vhost_user_main_t *vum = &vhost_user_main;
1470 vhost_user_intf_t *vui;
1472 vlib_worker_thread_barrier_sync (vlib_get_main ());
1474 pool_foreach (vui, vum->vhost_user_interfaces) {
1475 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1478 vlib_worker_thread_barrier_release (vlib_get_main ());
1482 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1485 * Open server unix socket on specified sock_filename.
1488 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1491 struct sockaddr_un un = { };
1493 /* create listening socket */
1494 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1495 return VNET_API_ERROR_SYSCALL_ERROR_1;
1497 un.sun_family = AF_UNIX;
1498 strncpy ((char *) un.sun_path, (char *) sock_filename,
1499 sizeof (un.sun_path) - 1);
1501 /* remove if exists */
1502 unlink ((char *) sock_filename);
1504 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1506 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1510 if (listen (fd, 1) == -1)
1512 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1525 * Create ethernet interface for vhost user interface.
1528 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1529 vhost_user_intf_t *vui,
1530 vhost_user_create_if_args_t *args)
1532 vhost_user_main_t *vum = &vhost_user_main;
1534 clib_error_t *error;
1536 /* create hw and sw interface */
1537 if (args->use_custom_mac)
1539 clib_memcpy (hwaddr, args->hwaddr, 6);
1543 random_u32 (&vum->random);
1544 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1549 error = ethernet_register_interface
1551 vhost_user_device_class.index,
1552 vui - vum->vhost_user_interfaces /* device instance */ ,
1553 hwaddr /* ethernet address */ ,
1554 &vui->hw_if_index, 0 /* flag change */ );
1557 clib_error_report (error);
1561 * Initialize vui with specified attributes
1564 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1565 int server_sock_fd, vhost_user_create_if_args_t * args,
1568 vnet_sw_interface_t *sw;
1570 vhost_user_main_t *vum = &vhost_user_main;
1571 vnet_hw_interface_t *hw;
1573 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1574 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1575 if (server_sock_fd != -1)
1577 clib_file_t template = { 0 };
1578 template.read_function = vhost_user_socksvr_accept_ready;
1579 template.file_descriptor = server_sock_fd;
1580 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1581 template.description = format (0, "vhost user %d", sw);
1582 vui->unix_server_index = clib_file_add (&file_main, &template);
1586 vui->unix_server_index = ~0;
1589 vui->sw_if_index = sw->sw_if_index;
1590 strncpy (vui->sock_filename, args->sock_filename,
1591 ARRAY_LEN (vui->sock_filename) - 1);
1592 vui->sock_errno = 0;
1594 vui->feature_mask = args->feature_mask;
1595 vui->clib_file_index = ~0;
1596 vui->log_base_addr = 0;
1597 vui->if_index = vui - vum->vhost_user_interfaces;
1598 vui->enable_gso = args->enable_gso;
1599 vui->enable_event_idx = args->enable_event_idx;
1600 vui->enable_packed = args->enable_packed;
1602 * enable_gso takes precedence over configurable feature mask if there
1604 * if feature mask disables gso, but enable_gso is configured,
1605 * then gso is enable
1606 * if feature mask enables gso, but enable_gso is not configured,
1607 * then gso is enable
1609 * if gso is enable via feature mask, it must enable both host and guest
1610 * gso feature mask, we don't support one sided GSO or partial GSO.
1612 if ((vui->enable_gso == 0) &&
1613 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1614 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1615 vui->enable_gso = 1;
1616 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1617 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1620 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1621 CLIB_CACHE_LINE_BYTES);
1623 for (q = 0; q < vec_len (vui->vrings); q++)
1624 vhost_user_vring_init (vui, q);
1626 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
1627 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1630 *sw_if_index = vui->sw_if_index;
1632 vec_validate (vui->per_cpu_tx_qid,
1633 vlib_get_thread_main ()->n_vlib_mains - 1);
1634 vhost_user_tx_thread_placement (vui);
1638 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1639 vhost_user_create_if_args_t * args)
1641 vhost_user_intf_t *vui = NULL;
1644 int server_sock_fd = -1;
1645 vhost_user_main_t *vum = &vhost_user_main;
1648 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1650 return VNET_API_ERROR_INVALID_ARGUMENT;
1653 if_index = mhash_get (&vum->if_index_by_sock_name,
1654 (void *) args->sock_filename);
1657 vui = &vum->vhost_user_interfaces[*if_index];
1658 args->sw_if_index = vui->sw_if_index;
1659 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1662 if (args->is_server)
1665 vhost_user_init_server_sock (args->sock_filename,
1666 &server_sock_fd)) != 0)
1672 /* Protect the uninitialized vui from being dispatched by rx/tx */
1673 vlib_worker_thread_barrier_sync (vm);
1674 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1675 vhost_user_create_ethernet (vnm, vm, vui, args);
1676 vlib_worker_thread_barrier_release (vm);
1678 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1679 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1680 vhost_user_rx_thread_placement (vui, 1);
1683 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1685 args->sw_if_index = sw_if_idx;
1687 // Process node must connect
1688 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1694 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1695 vhost_user_create_if_args_t * args)
1697 vhost_user_main_t *vum = &vhost_user_main;
1698 vhost_user_intf_t *vui = NULL;
1700 int server_sock_fd = -1;
1702 vnet_hw_interface_t *hwif;
1705 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1707 || hwif->dev_class_index != vhost_user_device_class.index)
1708 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1710 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1711 return VNET_API_ERROR_INVALID_ARGUMENT;
1713 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1716 * Disallow changing the interface to have the same path name
1717 * as other interface
1719 if_index = mhash_get (&vum->if_index_by_sock_name,
1720 (void *) args->sock_filename);
1721 if (if_index && (*if_index != vui->if_index))
1722 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1724 // First try to open server socket
1725 if (args->is_server)
1726 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1727 &server_sock_fd)) != 0)
1730 vhost_user_term_if (vui);
1731 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1734 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1736 // Process node must connect
1737 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1743 vhost_user_connect_command_fn (vlib_main_t * vm,
1744 unformat_input_t * input,
1745 vlib_cli_command_t * cmd)
1747 vnet_main_t *vnm = vnet_get_main ();
1748 unformat_input_t _line_input, *line_input = &_line_input;
1749 clib_error_t *error = NULL;
1750 vhost_user_create_if_args_t args = { 0 };
1753 /* Get a line of input. */
1754 if (!unformat_user (input, unformat_line_input, line_input))
1757 args.feature_mask = (u64) ~ (0ULL);
1758 args.custom_dev_instance = ~0;
1759 /* GSO feature is disable by default */
1760 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1761 /* packed-ring feature is disable by default */
1762 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1763 /* event_idx feature is disable by default */
1764 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1766 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1768 if (unformat (line_input, "socket %s", &args.sock_filename))
1770 else if (unformat (line_input, "server"))
1772 else if (unformat (line_input, "gso"))
1773 args.enable_gso = 1;
1774 else if (unformat (line_input, "packed"))
1775 args.enable_packed = 1;
1776 else if (unformat (line_input, "event-idx"))
1777 args.enable_event_idx = 1;
1778 else if (unformat (line_input, "feature-mask 0x%llx",
1779 &args.feature_mask))
1781 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1783 args.use_custom_mac = 1;
1784 else if (unformat (line_input, "renumber %d",
1785 &args.custom_dev_instance))
1789 error = clib_error_return (0, "unknown input `%U'",
1790 format_unformat_error, line_input);
1795 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1797 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1801 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1805 vec_free (args.sock_filename);
1806 unformat_free (line_input);
1812 vhost_user_delete_command_fn (vlib_main_t * vm,
1813 unformat_input_t * input,
1814 vlib_cli_command_t * cmd)
1816 unformat_input_t _line_input, *line_input = &_line_input;
1817 u32 sw_if_index = ~0;
1818 vnet_main_t *vnm = vnet_get_main ();
1819 clib_error_t *error = NULL;
1821 /* Get a line of input. */
1822 if (!unformat_user (input, unformat_line_input, line_input))
1825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1827 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1830 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1833 vnet_hw_interface_t *hwif =
1834 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1836 vhost_user_device_class.index != hwif->dev_class_index)
1838 error = clib_error_return (0, "Not a vhost interface");
1844 error = clib_error_return (0, "unknown input `%U'",
1845 format_unformat_error, line_input);
1850 vhost_user_delete_if (vnm, vm, sw_if_index);
1853 unformat_free (line_input);
1859 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1860 vhost_user_intf_details_t ** out_vuids)
1863 vhost_user_main_t *vum = &vhost_user_main;
1864 vhost_user_intf_t *vui;
1865 vhost_user_intf_details_t *r_vuids = NULL;
1866 vhost_user_intf_details_t *vuid = NULL;
1867 u32 *hw_if_indices = 0;
1868 vnet_hw_interface_t *hi;
1874 pool_foreach (vui, vum->vhost_user_interfaces)
1875 vec_add1 (hw_if_indices, vui->hw_if_index);
1877 for (i = 0; i < vec_len (hw_if_indices); i++)
1879 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1880 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1882 vec_add2 (r_vuids, vuid, 1);
1883 vuid->sw_if_index = vui->sw_if_index;
1884 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1885 vuid->features = vui->features;
1886 vuid->num_regions = vui->nregions;
1887 vuid->is_server = vui->unix_server_index != ~0;
1888 vuid->sock_errno = vui->sock_errno;
1889 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1890 "%s", vui->sock_filename);
1891 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1892 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1893 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1896 vec_free (hw_if_indices);
1898 *out_vuids = r_vuids;
1904 format_vhost_user_desc (u8 * s, va_list * args)
1906 char *fmt = va_arg (*args, char *);
1907 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1908 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1909 int idx = va_arg (*args, int);
1910 u32 *mem_hint = va_arg (*args, u32 *);
1912 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1913 desc_table[idx].flags, desc_table[idx].next,
1914 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1920 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1922 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1923 int callfd = UNIX_GET_FD (vq->callfd_idx);
1925 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1930 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1931 int show_descr, int show_verbose)
1937 vring_desc_t *desc_table;
1938 vhost_user_vring_t *vq = &vui->vrings[q];
1940 if (vq->avail && vq->used)
1941 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1942 "used event idx %u used.idx %d\n", vq->avail->flags,
1943 vhost_user_avail_event_idx (vq), vq->avail->idx,
1944 vhost_user_used_event_idx (vq), vq->used->idx);
1946 vhost_user_show_fds (vm, vq);
1950 vlib_cli_output (vm, "\n descriptor table:\n");
1951 vlib_cli_output (vm,
1952 " slot addr len flags next "
1954 vlib_cli_output (vm,
1955 " ===== ================== ===== ====== ===== "
1956 "==================\n");
1957 for (j = 0; j < vq->qsz_mask + 1; j++)
1959 desc_table = vq->desc;
1960 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1961 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1962 desc_table, j, &mem_hint);
1963 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1965 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1966 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1969 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1972 (vm, "%U", format_vhost_user_desc,
1973 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1974 desc_table, idx, &mem_hint);
1976 if (n_entries >= 20)
1977 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1986 format_vhost_user_packed_desc (u8 * s, va_list * args)
1988 char *fmt = va_arg (*args, char *);
1989 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1990 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1991 int idx = va_arg (*args, int);
1992 u32 *mem_hint = va_arg (*args, u32 *);
1994 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1995 desc_table[idx].flags, desc_table[idx].id,
1996 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2002 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
2004 u32 flags = va_arg (*args, u32);
2010 static event_idx_flags event_idx_array[] = {
2011 #define _(s,v) { .str = #s, .value = v, },
2012 foreach_virtio_event_idx_flags
2015 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2017 if (flags < num_entries)
2018 s = format (s, "%s", event_idx_array[flags].str);
2020 s = format (s, "%u", flags);
2025 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2026 int show_descr, int show_verbose)
2032 vring_packed_desc_t *desc_table;
2033 vhost_user_vring_t *vq = &vui->vrings[q];
2034 u16 off_wrap, event_idx;
2036 off_wrap = vq->avail_event->off_wrap;
2037 event_idx = off_wrap & 0x7fff;
2038 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2039 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2040 (u32) vq->avail_event->flags, off_wrap, event_idx);
2042 off_wrap = vq->used_event->off_wrap;
2043 event_idx = off_wrap & 0x7fff;
2044 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2045 "used event idx %u\n", format_vhost_user_event_idx_flags,
2046 (u32) vq->used_event->flags, off_wrap, event_idx);
2048 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2049 vq->avail_wrap_counter, vq->used_wrap_counter);
2051 vhost_user_show_fds (vm, vq);
2055 vlib_cli_output (vm, "\n descriptor table:\n");
2056 vlib_cli_output (vm,
2057 " slot addr len flags id "
2059 vlib_cli_output (vm,
2060 " ===== ================== ===== ====== ===== "
2061 "==================\n");
2062 for (j = 0; j < vq->qsz_mask + 1; j++)
2064 desc_table = vq->packed_desc;
2065 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2066 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2067 desc_table, j, &mem_hint);
2068 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2070 n_entries = desc_table[j].len >> 4;
2071 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2074 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2077 (vm, "%U", format_vhost_user_packed_desc,
2078 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2079 desc_table, idx, &mem_hint);
2081 if (n_entries >= 20)
2082 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2091 show_vhost_user_command_fn (vlib_main_t * vm,
2092 unformat_input_t * input,
2093 vlib_cli_command_t * cmd)
2095 clib_error_t *error = 0;
2096 vnet_main_t *vnm = vnet_get_main ();
2097 vhost_user_main_t *vum = &vhost_user_main;
2098 vhost_user_intf_t *vui;
2099 u32 hw_if_index, *hw_if_indices = 0;
2100 vnet_hw_interface_t *hi;
2105 int show_verbose = 0;
2111 struct feat_struct *feat_entry;
2113 static struct feat_struct feat_array[] = {
2114 #define _(s,b) { .str = #s, .bit = b, },
2115 foreach_virtio_net_features
2120 #define foreach_protocol_feature \
2121 _(VHOST_USER_PROTOCOL_F_MQ) \
2122 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2124 static struct feat_struct proto_feat_array[] = {
2125 #define _(s) { .str = #s, .bit = s},
2126 foreach_protocol_feature
2131 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2134 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2136 hi = vnet_get_hw_interface (vnm, hw_if_index);
2137 if (vhost_user_device_class.index != hi->dev_class_index)
2139 error = clib_error_return (0, "unknown input `%U'",
2140 format_unformat_error, input);
2143 vec_add1 (hw_if_indices, hw_if_index);
2145 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2147 else if (unformat (input, "verbose"))
2151 error = clib_error_return (0, "unknown input `%U'",
2152 format_unformat_error, input);
2156 if (vec_len (hw_if_indices) == 0)
2158 pool_foreach (vui, vum->vhost_user_interfaces)
2159 vec_add1 (hw_if_indices, vui->hw_if_index);
2161 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2162 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2163 vum->coalesce_frames, vum->coalesce_time);
2164 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2166 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2168 for (i = 0; i < vec_len (hw_if_indices); i++)
2170 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2171 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2172 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2173 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2175 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2176 if (vui->enable_gso)
2177 vlib_cli_output (vm, " GSO enable");
2178 if (vui->enable_packed)
2179 vlib_cli_output (vm, " Packed ring enable");
2180 if (vui->enable_event_idx)
2181 vlib_cli_output (vm, " Event index enable");
2183 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2184 " features mask (0x%llx): \n"
2185 " features (0x%llx): \n",
2186 vui->virtio_net_hdr_sz, vui->feature_mask,
2189 feat_entry = (struct feat_struct *) &feat_array;
2190 while (feat_entry->str)
2192 if (vui->features & (1ULL << feat_entry->bit))
2193 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2198 vlib_cli_output (vm, " protocol features (0x%llx)",
2199 vui->protocol_features);
2200 feat_entry = (struct feat_struct *) &proto_feat_array;
2201 while (feat_entry->str)
2203 if (vui->protocol_features & (1ULL << feat_entry->bit))
2204 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2209 vlib_cli_output (vm, "\n");
2211 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2213 (vui->unix_server_index != ~0) ? "server" : "client",
2214 strerror (vui->sock_errno));
2216 vlib_cli_output (vm, " rx placement: ");
2218 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2220 vnet_main_t *vnm = vnet_get_main ();
2222 vhost_user_vring_t *txvq = &vui->vrings[qid];
2224 if (txvq->qid == -1)
2227 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
2228 vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2229 qid, format_vnet_hw_if_rx_mode, txvq->mode);
2232 vlib_cli_output (vm, " tx placement: %s\n",
2233 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2235 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2237 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2238 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2241 vlib_cli_output (vm, "\n");
2243 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2247 vlib_cli_output (vm,
2248 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2249 vlib_cli_output (vm,
2250 " ====== ===== ================== ================== ================== ================== ==================\n");
2252 for (j = 0; j < vui->nregions; j++)
2254 vlib_cli_output (vm,
2255 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2256 j, vui->region_mmap_fd[j],
2257 vui->regions[j].guest_phys_addr,
2258 vui->regions[j].memory_size,
2259 vui->regions[j].userspace_addr,
2260 vui->regions[j].mmap_offset,
2261 pointer_to_uword (vui->region_mmap_addr[j]));
2263 for (q = 0; q < vui->num_qid; q++)
2265 if (!vui->vrings[q].started)
2268 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2269 (q & 1) ? "RX" : "TX",
2270 vui->vrings[q].enabled ? "" : " disabled");
2272 vlib_cli_output (vm,
2273 " qsz %d last_avail_idx %d last_used_idx %d"
2275 vui->vrings[q].qsz_mask + 1,
2276 vui->vrings[q].last_avail_idx,
2277 vui->vrings[q].last_used_idx,
2278 vui->vrings[q].last_kick);
2280 if (vhost_user_is_packed_ring_supported (vui))
2281 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2284 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2286 vlib_cli_output (vm, "\n");
2289 vec_free (hw_if_indices);
2298 * Create a vHost User interface. Once created, a new virtual interface
2299 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2300 * is the next free index.
2302 * There are several parameters associated with a vHost interface:
2304 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2305 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2306 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2307 * hypervisor will create the socket if it does not already exist. The VPP code
2308 * is indifferent to the file location. However, if SELinux is enabled, then the
2309 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2311 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2312 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2313 * mode, the VM can be reset without tearing down the vHost Interface. In
2314 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2315 * tearing down the vHost Interface.
2317 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2318 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2319 * parameter not be used except by experienced users. By default, all supported
2320 * features will be advertised. Otherwise, provide the set of features desired.
2321 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2322 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2323 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2324 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2325 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2326 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2327 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2328 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2329 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2331 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2332 * X:X:X:X:X:X unix or X.X.X cisco format.
2334 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2335 * in the name to be specified. If instance already exists, name will be used
2336 * anyway and multiple instances will have the same name. Use with caution.
2339 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2340 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2341 * VirtualEthernet0/0/0
2343 * Example of how to create a vhost interface with VPP as the server and with just
2344 * multiple queues enabled:
2345 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2346 * VirtualEthernet0/0/1
2348 * Once the vHost interface is created, enable the interface using:
2349 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2352 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2353 .path = "create vhost-user",
2354 .short_help = "create vhost-user socket <socket-filename> [server] "
2355 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2356 "[packed] [event-idx]",
2357 .function = vhost_user_connect_command_fn,
2363 * Delete a vHost User interface using the interface name or the
2364 * software interface index. Use the '<em>show interface</em>'
2365 * command to determine the software interface index. On deletion,
2366 * the linux socket will not be deleted.
2369 * Example of how to delete a vhost interface by name:
2370 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2371 * Example of how to delete a vhost interface by software interface index:
2372 * @cliexcmd{delete vhost-user sw_if_index 1}
2375 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2376 .path = "delete vhost-user",
2377 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2378 .function = vhost_user_delete_command_fn,
2382 * Display the attributes of a single vHost User interface (provide interface
2383 * name), multiple vHost User interfaces (provide a list of interface names seperated
2384 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2385 * vHost interfaces).
2389 * Example of how to display a vhost interface:
2390 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2391 * Virtio vhost-user interfaces
2393 * coalesce frames 32 time 1e-3
2394 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2395 * virtio_net_hdr_sz 12
2396 * features mask (0xffffffffffffffff):
2397 * features (0x50408000):
2398 * VIRTIO_NET_F_MRG_RXBUF (15)
2399 * VIRTIO_NET_F_MQ (22)
2400 * VIRTIO_F_INDIRECT_DESC (28)
2401 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2402 * protocol features (0x3)
2403 * VHOST_USER_PROTOCOL_F_MQ (0)
2404 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2406 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2409 * thread 1 on vring 1
2410 * thread 1 on vring 5
2411 * thread 2 on vring 3
2412 * thread 2 on vring 7
2413 * tx placement: spin-lock
2414 * thread 0 on vring 0
2415 * thread 1 on vring 2
2416 * thread 2 on vring 0
2418 * Memory regions (total 2)
2419 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2420 * ====== ===== ================== ================== ================== ================== ==================
2421 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2422 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2425 * qsz 256 last_avail_idx 0 last_used_idx 0
2426 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2427 * kickfd 62 callfd 64 errfd -1
2430 * qsz 256 last_avail_idx 0 last_used_idx 0
2431 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2432 * kickfd 65 callfd 66 errfd -1
2435 * qsz 256 last_avail_idx 0 last_used_idx 0
2436 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2437 * kickfd 63 callfd 70 errfd -1
2440 * qsz 256 last_avail_idx 0 last_used_idx 0
2441 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2442 * kickfd 72 callfd 74 errfd -1
2444 * Virtqueue 4 (TX disabled)
2445 * qsz 256 last_avail_idx 0 last_used_idx 0
2446 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2447 * kickfd 76 callfd 78 errfd -1
2449 * Virtqueue 5 (RX disabled)
2450 * qsz 256 last_avail_idx 0 last_used_idx 0
2451 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2452 * kickfd 80 callfd 82 errfd -1
2454 * Virtqueue 6 (TX disabled)
2455 * qsz 256 last_avail_idx 0 last_used_idx 0
2456 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2457 * kickfd 84 callfd 86 errfd -1
2459 * Virtqueue 7 (RX disabled)
2460 * qsz 256 last_avail_idx 0 last_used_idx 0
2461 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2462 * kickfd 88 callfd 90 errfd -1
2466 * The optional '<em>descriptors</em>' parameter will display the same output as
2467 * the previous example but will include the descriptor table for each queue.
2468 * The output is truncated below:
2469 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2470 * Virtio vhost-user interfaces
2472 * coalesce frames 32 time 1e-3
2473 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2474 * virtio_net_hdr_sz 12
2475 * features mask (0xffffffffffffffff):
2476 * features (0x50408000):
2477 * VIRTIO_NET_F_MRG_RXBUF (15)
2478 * VIRTIO_NET_F_MQ (22)
2481 * qsz 256 last_avail_idx 0 last_used_idx 0
2482 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2483 * kickfd 62 callfd 64 errfd -1
2486 * id addr len flags next user_addr
2487 * ===== ================== ===== ====== ===== ==================
2488 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2489 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2490 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2491 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2492 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2493 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2494 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2495 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2496 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2497 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2498 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2500 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2501 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2502 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2503 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2504 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2505 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2506 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2509 * qsz 256 last_avail_idx 0 last_used_idx 0
2515 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2516 .path = "show vhost-user",
2517 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2518 "[[descriptors] [verbose]]",
2519 .function = show_vhost_user_command_fn,
2524 static clib_error_t *
2525 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2527 vhost_user_main_t *vum = &vhost_user_main;
2529 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2531 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2533 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2535 else if (unformat (input, "dont-dump-memory"))
2536 vum->dont_dump_vhost_user_memory = 1;
2538 return clib_error_return (0, "unknown input `%U'",
2539 format_unformat_error, input);
2545 /* vhost-user { ... } configuration. */
2546 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2549 vhost_user_unmap_all (void)
2551 vhost_user_main_t *vum = &vhost_user_main;
2552 vhost_user_intf_t *vui;
2554 if (vum->dont_dump_vhost_user_memory)
2556 pool_foreach (vui, vum->vhost_user_interfaces)
2557 unmap_all_mem_regions (vui);
2562 * fd.io coding-style-patch-verification: ON
2565 * eval: (c-set-style "gnu")