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;
214 static clib_error_t *
215 vhost_user_callfd_read_ready (clib_file_t * uf)
217 __attribute__ ((unused)) int n;
220 n = read (uf->file_descriptor, ((char *) &buff), 8);
225 static_always_inline void
226 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
228 if (qid & 1) // RX is odd, TX is even
230 if (vui->vrings[qid].qid == -1)
231 vhost_user_rx_thread_placement (vui, qid);
234 vhost_user_tx_thread_placement (vui);
237 static clib_error_t *
238 vhost_user_kickfd_read_ready (clib_file_t * uf)
240 __attribute__ ((unused)) ssize_t n;
242 vhost_user_main_t *vum = &vhost_user_main;
243 vhost_user_intf_t *vui =
244 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
245 u32 qid = uf->private_data & 0xff;
246 u32 is_txq = qid & 1;
247 vhost_user_vring_t *vq = &vui->vrings[qid];
248 vnet_main_t *vnm = vnet_get_main ();
250 n = read (uf->file_descriptor, buff, 8);
251 if (vq->started == 0)
254 vhost_user_thread_placement (vui, qid);
255 vhost_user_update_iface_state (vui);
257 vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
261 if (is_txq && (vhost_user_intf_ready (vui) &&
262 ((vq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
263 (vq->mode == VNET_HW_IF_RX_MODE_INTERRUPT))))
264 vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
269 static_always_inline void
270 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
272 vhost_user_vring_t *vring = &vui->vrings[qid];
274 clib_memset (vring, 0, sizeof (*vring));
275 vring->kickfd_idx = ~0;
276 vring->callfd_idx = ~0;
280 clib_spinlock_init (&vring->vring_lock);
283 * We have a bug with some qemu 2.5, and this may be a fix.
284 * Feel like interpretation holy text, but this is from vhost-user.txt.
286 * One queue pair is enabled initially. More queues are enabled
287 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
289 * Don't know who's right, but this is what DPDK does.
291 if (qid == 0 || qid == 1)
295 static_always_inline void
296 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
298 vhost_user_vring_t *vring = &vui->vrings[qid];
300 if (vring->kickfd_idx != ~0)
302 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
304 clib_file_del (&file_main, uf);
305 vring->kickfd_idx = ~0;
307 if (vring->callfd_idx != ~0)
309 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
311 clib_file_del (&file_main, uf);
312 vring->callfd_idx = ~0;
314 if (vring->errfd != -1)
316 close (vring->errfd);
320 clib_spinlock_free (&vring->vring_lock);
322 // save the qid so that we don't need to unassign and assign_rx_thread
323 // when the interface comes back up. They are expensive calls.
324 u16 q = vui->vrings[qid].qid;
325 vhost_user_vring_init (vui, qid);
326 vui->vrings[qid].qid = q;
329 static_always_inline void
330 vhost_user_if_disconnect (vhost_user_intf_t * vui)
332 vnet_main_t *vnm = vnet_get_main ();
335 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
337 if (vui->clib_file_index != ~0)
339 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
340 vui->clib_file_index = ~0;
345 for (q = 0; q < vui->num_qid; q++)
346 vhost_user_vring_close (vui, q);
348 unmap_all_mem_regions (vui);
349 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
352 static clib_error_t *
353 vhost_user_socket_read (clib_file_t * uf)
356 int fd, number_of_fds = 0;
357 int fds[VHOST_MEMORY_MAX_NREGIONS];
358 vhost_user_msg_t msg;
361 vhost_user_main_t *vum = &vhost_user_main;
362 vhost_user_intf_t *vui;
363 struct cmsghdr *cmsg;
365 clib_file_t template = { 0 };
366 vnet_main_t *vnm = vnet_get_main ();
367 vlib_main_t *vm = vlib_get_main ();
369 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
371 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
373 clib_memset (&mh, 0, sizeof (mh));
374 clib_memset (control, 0, sizeof (control));
376 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
379 /* set the payload */
380 iov[0].iov_base = (void *) &msg;
381 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
385 mh.msg_control = control;
386 mh.msg_controllen = sizeof (control);
388 n = recvmsg (uf->file_descriptor, &mh, 0);
390 if (n != VHOST_USER_MSG_HDR_SZ)
394 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
399 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
400 n, VHOST_USER_MSG_HDR_SZ);
405 if (mh.msg_flags & MSG_CTRUNC)
407 vu_log_debug (vui, "MSG_CTRUNC is set");
411 cmsg = CMSG_FIRSTHDR (&mh);
413 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
414 (cmsg->cmsg_type == SCM_RIGHTS) &&
415 (cmsg->cmsg_len - CMSG_LEN (0) <=
416 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
418 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
419 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
422 /* version 1, no reply bit set */
423 if ((msg.flags & 7) != 1)
425 vu_log_debug (vui, "malformed message received. closing socket");
432 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
436 vu_log_debug (vui, "read failed %s", strerror (errno));
439 else if (rv != msg.size)
441 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
449 case VHOST_USER_GET_FEATURES:
451 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
452 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
453 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
454 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
455 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
456 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
457 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
458 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
459 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
460 msg.u64 &= vui->feature_mask;
462 if (vui->enable_event_idx)
463 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
465 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
466 if (vui->enable_packed)
467 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
469 msg.size = sizeof (msg.u64);
470 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
471 "0x%016llx", vui->hw_if_index, msg.u64);
473 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
474 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
476 vu_log_debug (vui, "could not send message response");
481 case VHOST_USER_SET_FEATURES:
482 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
483 "0x%016llx", vui->hw_if_index, msg.u64);
485 vui->features = msg.u64;
488 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
489 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
490 vui->virtio_net_hdr_sz = 12;
492 vui->virtio_net_hdr_sz = 10;
495 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
497 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
498 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
499 if (vui->enable_gso &&
500 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
501 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
503 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
504 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
505 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
509 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
510 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
512 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
514 vhost_user_update_iface_state (vui);
517 case VHOST_USER_SET_MEM_TABLE:
518 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
519 vui->hw_if_index, msg.memory.nregions);
521 if ((msg.memory.nregions < 1) ||
522 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
524 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
525 VHOST_MEMORY_MAX_NREGIONS);
529 if (msg.memory.nregions != number_of_fds)
531 vu_log_debug (vui, "each memory region must have FD");
535 /* Do the mmap without barrier sync */
536 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
537 for (i = 0; i < msg.memory.nregions; i++)
539 long page_sz = get_huge_page_size (fds[i]);
541 /* align size to page */
542 ssize_t map_sz = (msg.memory.regions[i].memory_size +
543 msg.memory.regions[i].mmap_offset +
544 page_sz - 1) & ~(page_sz - 1);
546 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
547 MAP_SHARED, fds[i], 0);
548 if (region_mmap_addr[i] == MAP_FAILED)
550 vu_log_err (vui, "failed to map memory. errno is %d", errno);
551 for (j = 0; j < i; j++)
552 munmap (region_mmap_addr[j], map_sz);
555 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
556 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
557 region_mmap_addr[i], page_sz);
560 vlib_worker_thread_barrier_sync (vm);
561 unmap_all_mem_regions (vui);
562 for (i = 0; i < msg.memory.nregions; i++)
564 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
565 sizeof (vhost_user_memory_region_t));
567 vui->region_mmap_addr[i] = region_mmap_addr[i];
568 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
569 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
570 vui->regions[i].memory_size;
572 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
573 vui->region_mmap_fd[i] = fds[i];
579 * Re-compute desc, used, and avail descriptor table if vring address
582 for (q = 0; q < vui->num_qid; q++)
584 if (vui->vrings[q].desc_user_addr &&
585 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
587 vui->vrings[q].desc =
588 map_user_mem (vui, vui->vrings[q].desc_user_addr);
589 vui->vrings[q].used =
590 map_user_mem (vui, vui->vrings[q].used_user_addr);
591 vui->vrings[q].avail =
592 map_user_mem (vui, vui->vrings[q].avail_user_addr);
595 vlib_worker_thread_barrier_release (vm);
598 case VHOST_USER_SET_VRING_NUM:
599 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
600 vui->hw_if_index, msg.state.index, msg.state.num);
602 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
603 (msg.state.num == 0) || /* it cannot be zero */
604 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
605 (msg.state.index >= vui->num_qid))
607 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
608 " %d, msg.state.index %d, curruent max q %d",
609 msg.state.num, msg.state.index, vui->num_qid);
612 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
615 case VHOST_USER_SET_VRING_ADDR:
616 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
617 vui->hw_if_index, msg.state.index);
619 if (msg.state.index >= vui->num_qid)
621 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
622 " %u >= %u", msg.state.index, vui->num_qid);
626 if (msg.size < sizeof (msg.addr))
628 vu_log_debug (vui, "vhost message is too short (%d < %d)",
629 msg.size, sizeof (msg.addr));
633 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
634 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
635 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
637 if ((desc == NULL) || (used == NULL) || (avail == NULL))
639 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
644 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
645 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
646 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
648 vlib_worker_thread_barrier_sync (vm);
649 vui->vrings[msg.state.index].desc = desc;
650 vui->vrings[msg.state.index].used = used;
651 vui->vrings[msg.state.index].avail = avail;
653 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
654 vui->vrings[msg.state.index].log_used =
655 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
657 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
658 the ring is initialized in an enabled state. */
659 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
660 vui->vrings[msg.state.index].enabled = 1;
662 vui->vrings[msg.state.index].last_used_idx =
663 vui->vrings[msg.state.index].last_avail_idx =
664 vui->vrings[msg.state.index].used->idx;
665 vui->vrings[msg.state.index].last_kick =
666 vui->vrings[msg.state.index].last_used_idx;
668 /* tell driver that we don't want interrupts */
669 if (vhost_user_is_packed_ring_supported (vui))
670 vui->vrings[msg.state.index].used_event->flags =
671 VRING_EVENT_F_DISABLE;
673 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
674 vlib_worker_thread_barrier_release (vm);
675 vhost_user_update_iface_state (vui);
678 case VHOST_USER_SET_OWNER:
679 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
682 case VHOST_USER_RESET_OWNER:
683 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
687 case VHOST_USER_SET_VRING_CALL:
688 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
689 vui->hw_if_index, msg.u64);
691 q = (u8) (msg.u64 & 0xFF);
692 if (vui->num_qid > q)
694 /* if there is old fd, delete and close it */
695 if (vui->vrings[q].callfd_idx != ~0)
697 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
698 vui->vrings[q].callfd_idx);
699 clib_file_del (&file_main, uf);
700 vui->vrings[q].callfd_idx = ~0;
703 else if (vec_len (vui->vrings) > q)
705 /* grow vrings by pair (RX + TX) */
706 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
710 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
713 * Double the array size if it is less than 64 entries.
714 * Slow down thereafter.
716 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
717 new_max_q = vec_len (vui->vrings) << 1;
719 new_max_q = vec_len (vui->vrings) +
720 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
721 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
722 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
724 /* sync with the worker threads, vrings may move due to realloc */
725 vlib_worker_thread_barrier_sync (vm);
726 vec_validate_aligned (vui->vrings, new_max_q - 1,
727 CLIB_CACHE_LINE_BYTES);
728 vlib_worker_thread_barrier_release (vm);
730 for (i = old_max_q; i < vec_len (vui->vrings); i++)
731 vhost_user_vring_init (vui, i);
733 /* grow vrings by pair (RX + TX) */
734 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
737 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
739 if (number_of_fds != 1)
741 vu_log_debug (vui, "More than one fd received !");
745 template.read_function = vhost_user_callfd_read_ready;
746 template.file_descriptor = fds[0];
747 template.private_data =
748 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
749 template.description = format (0, "vhost user");
750 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
753 vui->vrings[q].callfd_idx = ~0;
756 case VHOST_USER_SET_VRING_KICK:
757 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
758 vui->hw_if_index, msg.u64);
760 q = (u8) (msg.u64 & 0xFF);
761 if (q >= vui->num_qid)
763 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
764 " %u >= %u", q, vui->num_qid);
768 if (vui->vrings[q].kickfd_idx != ~0)
770 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
771 vui->vrings[q].kickfd_idx);
772 clib_file_del (&file_main, uf);
773 vui->vrings[q].kickfd_idx = ~0;
776 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
778 if (number_of_fds != 1)
780 vu_log_debug (vui, "More than one fd received !");
784 template.read_function = vhost_user_kickfd_read_ready;
785 template.file_descriptor = fds[0];
786 template.private_data =
787 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
789 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
793 //When no kickfd is set, the queue is initialized as started
794 vui->vrings[q].kickfd_idx = ~0;
795 vui->vrings[q].started = 1;
796 vhost_user_thread_placement (vui, q);
798 vhost_user_update_iface_state (vui);
801 case VHOST_USER_SET_VRING_ERR:
802 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
803 vui->hw_if_index, msg.u64);
805 q = (u8) (msg.u64 & 0xFF);
806 if (q >= vui->num_qid)
808 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
809 " %u >= %u", q, vui->num_qid);
813 if (vui->vrings[q].errfd != -1)
814 close (vui->vrings[q].errfd);
816 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
818 if (number_of_fds != 1)
821 vui->vrings[q].errfd = fds[0];
824 vui->vrings[q].errfd = -1;
827 case VHOST_USER_SET_VRING_BASE:
829 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
830 vui->hw_if_index, msg.state.index, msg.state.num);
831 if (msg.state.index >= vui->num_qid)
833 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
834 " %u >= %u", msg.state.index, vui->num_qid);
837 vlib_worker_thread_barrier_sync (vm);
838 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
839 if (vhost_user_is_packed_ring_supported (vui))
843 * 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
844 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
845 * | last avail idx | | last used idx | |
846 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
849 * avail wrap counter used wrap counter
851 /* last avail idx at bit 0-14. */
852 vui->vrings[msg.state.index].last_avail_idx =
853 msg.state.num & 0x7fff;
854 /* avail wrap counter at bit 15 */
855 vui->vrings[msg.state.index].avail_wrap_counter =
856 ! !(msg.state.num & (1 << 15));
859 * Although last_used_idx is passed in the upper 16 bits in qemu
860 * implementation, in practice, last_avail_idx and last_used_idx are
861 * usually the same. As a result, DPDK does not bother to pass us
862 * last_used_idx. The spec is not clear on thex coding. I figured it
863 * out by reading the qemu code. So let's just read last_avail_idx
864 * and set last_used_idx equals to last_avail_idx.
866 vui->vrings[msg.state.index].last_used_idx =
867 vui->vrings[msg.state.index].last_avail_idx;
868 vui->vrings[msg.state.index].last_kick =
869 vui->vrings[msg.state.index].last_used_idx;
870 vui->vrings[msg.state.index].used_wrap_counter =
871 vui->vrings[msg.state.index].avail_wrap_counter;
873 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
874 vui->vrings[msg.state.index].avail_wrap_counter =
877 vlib_worker_thread_barrier_release (vm);
880 case VHOST_USER_GET_VRING_BASE:
881 if (msg.state.index >= vui->num_qid)
883 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
884 " %u >= %u", msg.state.index, vui->num_qid);
888 /* protection is needed to prevent rx/tx from changing last_avail_idx */
889 vlib_worker_thread_barrier_sync (vm);
891 * Copy last_avail_idx from the vring before closing it because
892 * closing the vring also initializes the vring last_avail_idx
894 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
895 if (vhost_user_is_packed_ring_supported (vui))
898 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
899 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
901 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
902 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
905 msg.size = sizeof (msg.state);
908 * Spec says: Client must [...] stop ring upon receiving
909 * VHOST_USER_GET_VRING_BASE
911 vhost_user_vring_close (vui, msg.state.index);
912 vlib_worker_thread_barrier_release (vm);
914 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
915 vui->hw_if_index, msg.state.index, msg.state.num);
917 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
918 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
920 vu_log_debug (vui, "could not send message response");
923 vhost_user_update_iface_state (vui);
926 case VHOST_USER_NONE:
927 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
930 case VHOST_USER_SET_LOG_BASE:
931 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
934 if (msg.size != sizeof (msg.log))
936 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
937 " %d instead of %d", msg.size, sizeof (msg.log));
941 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
943 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
944 "VHOST_USER_SET_LOG_BASE received");
949 /* align size to page */
950 long page_sz = get_huge_page_size (fd);
952 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
954 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
957 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
958 "mapped 0x%lx", map_sz, msg.log.offset, fd,
961 if (log_base_addr == MAP_FAILED)
963 vu_log_err (vui, "failed to map memory. errno is %d", errno);
967 vlib_worker_thread_barrier_sync (vm);
968 vui->log_base_addr = log_base_addr;
969 vui->log_base_addr += msg.log.offset;
970 vui->log_size = msg.log.size;
971 vlib_worker_thread_barrier_release (vm);
974 msg.size = sizeof (msg.u64);
976 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
977 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
979 vu_log_debug (vui, "could not send message response");
984 case VHOST_USER_SET_LOG_FD:
985 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
988 case VHOST_USER_GET_PROTOCOL_FEATURES:
990 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
991 (1 << VHOST_USER_PROTOCOL_F_MQ);
992 msg.size = sizeof (msg.u64);
993 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
994 "reply 0x%016llx", vui->hw_if_index, msg.u64);
996 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
997 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
999 vu_log_debug (vui, "could not send message response");
1004 case VHOST_USER_SET_PROTOCOL_FEATURES:
1005 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1006 "features 0x%016llx", vui->hw_if_index, msg.u64);
1007 vui->protocol_features = msg.u64;
1010 case VHOST_USER_GET_QUEUE_NUM:
1012 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1013 msg.size = sizeof (msg.u64);
1014 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1015 vui->hw_if_index, msg.u64);
1017 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1018 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1020 vu_log_debug (vui, "could not send message response");
1025 case VHOST_USER_SET_VRING_ENABLE:
1026 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1027 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1029 if (msg.state.index >= vui->num_qid)
1031 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1032 " %u >= %u", msg.state.index, vui->num_qid);
1036 vui->vrings[msg.state.index].enabled = msg.state.num;
1037 vhost_user_thread_placement (vui, msg.state.index);
1038 vhost_user_update_iface_state (vui);
1042 vu_log_debug (vui, "unknown vhost-user message %d received. "
1043 "closing socket", msg.request);
1050 vlib_worker_thread_barrier_sync (vm);
1051 vhost_user_if_disconnect (vui);
1052 vlib_worker_thread_barrier_release (vm);
1053 vhost_user_update_iface_state (vui);
1057 static clib_error_t *
1058 vhost_user_socket_error (clib_file_t * uf)
1060 vlib_main_t *vm = vlib_get_main ();
1061 vhost_user_main_t *vum = &vhost_user_main;
1062 vhost_user_intf_t *vui =
1063 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1065 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1066 vlib_worker_thread_barrier_sync (vm);
1067 vhost_user_if_disconnect (vui);
1068 vlib_worker_thread_barrier_release (vm);
1072 static clib_error_t *
1073 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1075 int client_fd, client_len;
1076 struct sockaddr_un client;
1077 clib_file_t template = { 0 };
1078 vhost_user_main_t *vum = &vhost_user_main;
1079 vhost_user_intf_t *vui;
1081 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1083 client_len = sizeof (client);
1084 client_fd = accept (uf->file_descriptor,
1085 (struct sockaddr *) &client,
1086 (socklen_t *) & client_len);
1089 return clib_error_return_unix (0, "accept");
1091 if (vui->clib_file_index != ~0)
1093 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1094 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1095 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1098 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1099 vui->sw_if_index, client_fd);
1100 template.read_function = vhost_user_socket_read;
1101 template.error_function = vhost_user_socket_error;
1102 template.file_descriptor = client_fd;
1103 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1104 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1105 vui->clib_file_index = clib_file_add (&file_main, &template);
1110 static clib_error_t *
1111 vhost_user_init (vlib_main_t * vm)
1113 vhost_user_main_t *vum = &vhost_user_main;
1114 vlib_thread_main_t *tm = vlib_get_thread_main ();
1116 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1118 vum->coalesce_frames = 32;
1119 vum->coalesce_time = 1e-3;
1121 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1124 vec_foreach (cpu, vum->cpus)
1126 /* This is actually not necessary as validate already zeroes it
1127 * Just keeping the loop here for later because I am lazy. */
1128 cpu->rx_buffers_len = 0;
1131 vum->random = random_default_seed ();
1133 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1139 VLIB_INIT_FUNCTION (vhost_user_init) =
1141 .runs_after = VLIB_INITS("ip4_init"),
1146 vhost_user_send_interrupt_process (vlib_main_t * vm,
1147 vlib_node_runtime_t * rt, vlib_frame_t * f)
1149 vhost_user_intf_t *vui;
1150 f64 timeout = 3153600000.0 /* 100 years */ ;
1151 uword event_type, *event_data = 0;
1152 vhost_user_main_t *vum = &vhost_user_main;
1154 f64 now, poll_time_remaining;
1160 poll_time_remaining =
1161 vlib_process_wait_for_event_or_clock (vm, timeout);
1162 event_type = vlib_process_get_events (vm, &event_data);
1163 vec_reset_length (event_data);
1166 * Use the remaining timeout if it is less than coalesce time to avoid
1167 * resetting the existing timer in the middle of expiration
1169 timeout = poll_time_remaining;
1170 if (vlib_process_suspend_time_is_zero (timeout) ||
1171 (timeout > vum->coalesce_time))
1172 timeout = vum->coalesce_time;
1174 now = vlib_time_now (vm);
1177 case VHOST_USER_EVENT_STOP_TIMER:
1181 case VHOST_USER_EVENT_START_TIMER:
1183 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1189 pool_foreach (vui, vum->vhost_user_interfaces) {
1190 next_timeout = timeout;
1191 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1193 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1194 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1196 if (txvq->qid == -1)
1198 if (txvq->n_since_last_int)
1200 if (now >= txvq->int_deadline)
1201 vhost_user_send_call (vm, vui, txvq);
1203 next_timeout = txvq->int_deadline - now;
1206 if (rxvq->n_since_last_int)
1208 if (now >= rxvq->int_deadline)
1209 vhost_user_send_call (vm, vui, rxvq);
1211 next_timeout = rxvq->int_deadline - now;
1214 if ((next_timeout < timeout) && (next_timeout > 0.0))
1215 timeout = next_timeout;
1222 clib_warning ("BUG: unhandled event type %d", event_type);
1225 /* No less than 1 millisecond */
1229 timeout = 3153600000.0;
1235 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1236 .function = vhost_user_send_interrupt_process,
1237 .type = VLIB_NODE_TYPE_PROCESS,
1238 .name = "vhost-user-send-interrupt-process",
1243 vhost_user_process (vlib_main_t * vm,
1244 vlib_node_runtime_t * rt, vlib_frame_t * f)
1246 vhost_user_main_t *vum = &vhost_user_main;
1247 vhost_user_intf_t *vui;
1248 struct sockaddr_un sun;
1250 clib_file_t template = { 0 };
1251 f64 timeout = 3153600000.0 /* 100 years */ ;
1252 uword *event_data = 0;
1255 sun.sun_family = AF_UNIX;
1256 template.read_function = vhost_user_socket_read;
1257 template.error_function = vhost_user_socket_error;
1261 vlib_process_wait_for_event_or_clock (vm, timeout);
1262 vlib_process_get_events (vm, &event_data);
1263 vec_reset_length (event_data);
1268 pool_foreach (vui, vum->vhost_user_interfaces) {
1270 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1271 if (vui->clib_file_index == ~0)
1274 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1277 * 1st time error or new error for this interface,
1278 * spit out the message and record the error
1280 if (!vui->sock_errno || (vui->sock_errno != errno))
1283 ("Error: Could not open unix socket for %s",
1284 vui->sock_filename);
1285 vui->sock_errno = errno;
1290 /* try to connect */
1291 strncpy (sun.sun_path, (char *) vui->sock_filename,
1292 sizeof (sun.sun_path) - 1);
1293 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1295 /* Avoid hanging VPP if the other end does not accept */
1296 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1297 clib_unix_warning ("fcntl");
1299 if (connect (sockfd, (struct sockaddr *) &sun,
1300 sizeof (struct sockaddr_un)) == 0)
1302 /* Set the socket to blocking as it was before */
1303 if (fcntl(sockfd, F_SETFL, 0) < 0)
1304 clib_unix_warning ("fcntl2");
1306 vui->sock_errno = 0;
1307 template.file_descriptor = sockfd;
1308 template.private_data =
1309 vui - vhost_user_main.vhost_user_interfaces;
1310 template.description = format (0, "vhost user process");
1311 vui->clib_file_index = clib_file_add (&file_main, &template);
1314 /* This sockfd is considered consumed */
1319 vui->sock_errno = errno;
1324 /* check if socket is alive */
1326 socklen_t len = sizeof (error);
1327 int fd = UNIX_GET_FD(vui->clib_file_index);
1329 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1333 vu_log_debug (vui, "getsockopt returned %d", retval);
1334 vhost_user_if_disconnect (vui);
1345 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1346 .function = vhost_user_process,
1347 .type = VLIB_NODE_TYPE_PROCESS,
1348 .name = "vhost-user-process",
1353 * Disables and reset interface structure.
1354 * It can then be either init again, or removed from used interfaces.
1357 vhost_user_term_if (vhost_user_intf_t * vui)
1360 vhost_user_main_t *vum = &vhost_user_main;
1362 // disconnect interface sockets
1363 vhost_user_if_disconnect (vui);
1364 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1365 vhost_user_update_iface_state (vui);
1367 for (q = 0; q < vui->num_qid; q++)
1369 clib_spinlock_free (&vui->vrings[q].vring_lock);
1372 if (vui->unix_server_index != ~0)
1374 //Close server socket
1375 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1376 vui->unix_server_index);
1377 clib_file_del (&file_main, uf);
1378 vui->unix_server_index = ~0;
1379 unlink (vui->sock_filename);
1382 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1387 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1389 vhost_user_main_t *vum = &vhost_user_main;
1390 vhost_user_intf_t *vui;
1392 vnet_hw_interface_t *hwif;
1397 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1398 || hwif->dev_class_index != vhost_user_device_class.index)
1399 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1401 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1403 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1404 hwif->name, hwif->dev_instance);
1406 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1408 vhost_user_vring_t *txvq = &vui->vrings[qid];
1410 if (txvq->qid == -1)
1412 if ((vum->ifq_count > 0) &&
1413 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1414 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1417 // Stop the timer if there is no more interrupt interface/queue
1418 if ((vum->ifq_count == 0) &&
1419 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1421 vlib_process_signal_event (vm,
1422 vhost_user_send_interrupt_node.index,
1423 VHOST_USER_EVENT_STOP_TIMER, 0);
1429 // Disable and reset interface
1430 vhost_user_term_if (vui);
1432 // Reset renumbered iface
1433 if (hwif->dev_instance <
1434 vec_len (vum->show_dev_instance_by_real_dev_instance))
1435 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1437 // Delete ethernet interface
1438 ethernet_delete_interface (vnm, vui->hw_if_index);
1441 vec_free (vui->vrings);
1444 pool_put (vum->vhost_user_interfaces, vui);
1449 static clib_error_t *
1450 vhost_user_exit (vlib_main_t * vm)
1452 vnet_main_t *vnm = vnet_get_main ();
1453 vhost_user_main_t *vum = &vhost_user_main;
1454 vhost_user_intf_t *vui;
1456 vlib_worker_thread_barrier_sync (vlib_get_main ());
1458 pool_foreach (vui, vum->vhost_user_interfaces) {
1459 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1462 vlib_worker_thread_barrier_release (vlib_get_main ());
1466 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1469 * Open server unix socket on specified sock_filename.
1472 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1475 struct sockaddr_un un = { };
1477 /* create listening socket */
1478 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1479 return VNET_API_ERROR_SYSCALL_ERROR_1;
1481 un.sun_family = AF_UNIX;
1482 strncpy ((char *) un.sun_path, (char *) sock_filename,
1483 sizeof (un.sun_path) - 1);
1485 /* remove if exists */
1486 unlink ((char *) sock_filename);
1488 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1490 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1494 if (listen (fd, 1) == -1)
1496 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1509 * Create ethernet interface for vhost user interface.
1512 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1513 vhost_user_intf_t *vui,
1514 vhost_user_create_if_args_t *args)
1516 vhost_user_main_t *vum = &vhost_user_main;
1518 clib_error_t *error;
1520 /* create hw and sw interface */
1521 if (args->use_custom_mac)
1523 clib_memcpy (hwaddr, args->hwaddr, 6);
1527 random_u32 (&vum->random);
1528 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1533 error = ethernet_register_interface
1535 vhost_user_device_class.index,
1536 vui - vum->vhost_user_interfaces /* device instance */ ,
1537 hwaddr /* ethernet address */ ,
1538 &vui->hw_if_index, 0 /* flag change */ );
1541 clib_error_report (error);
1545 * Initialize vui with specified attributes
1548 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1549 int server_sock_fd, vhost_user_create_if_args_t * args,
1552 vnet_sw_interface_t *sw;
1554 vhost_user_main_t *vum = &vhost_user_main;
1555 vnet_hw_interface_t *hw;
1557 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1558 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1559 if (server_sock_fd != -1)
1561 clib_file_t template = { 0 };
1562 template.read_function = vhost_user_socksvr_accept_ready;
1563 template.file_descriptor = server_sock_fd;
1564 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1565 template.description = format (0, "vhost user %d", sw);
1566 vui->unix_server_index = clib_file_add (&file_main, &template);
1570 vui->unix_server_index = ~0;
1573 vui->sw_if_index = sw->sw_if_index;
1574 strncpy (vui->sock_filename, args->sock_filename,
1575 ARRAY_LEN (vui->sock_filename) - 1);
1576 vui->sock_errno = 0;
1578 vui->feature_mask = args->feature_mask;
1579 vui->clib_file_index = ~0;
1580 vui->log_base_addr = 0;
1581 vui->if_index = vui - vum->vhost_user_interfaces;
1582 vui->enable_gso = args->enable_gso;
1583 vui->enable_event_idx = args->enable_event_idx;
1584 vui->enable_packed = args->enable_packed;
1586 * enable_gso takes precedence over configurable feature mask if there
1588 * if feature mask disables gso, but enable_gso is configured,
1589 * then gso is enable
1590 * if feature mask enables gso, but enable_gso is not configured,
1591 * then gso is enable
1593 * if gso is enable via feature mask, it must enable both host and guest
1594 * gso feature mask, we don't support one sided GSO or partial GSO.
1596 if ((vui->enable_gso == 0) &&
1597 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1598 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1599 vui->enable_gso = 1;
1600 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1601 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1604 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1605 CLIB_CACHE_LINE_BYTES);
1607 for (q = 0; q < vec_len (vui->vrings); q++)
1608 vhost_user_vring_init (vui, q);
1610 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
1611 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1614 *sw_if_index = vui->sw_if_index;
1616 vec_validate (vui->per_cpu_tx_qid,
1617 vlib_get_thread_main ()->n_vlib_mains - 1);
1618 vhost_user_tx_thread_placement (vui);
1622 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1623 vhost_user_create_if_args_t * args)
1625 vhost_user_intf_t *vui = NULL;
1628 int server_sock_fd = -1;
1629 vhost_user_main_t *vum = &vhost_user_main;
1632 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1634 return VNET_API_ERROR_INVALID_ARGUMENT;
1637 if_index = mhash_get (&vum->if_index_by_sock_name,
1638 (void *) args->sock_filename);
1641 vui = &vum->vhost_user_interfaces[*if_index];
1642 args->sw_if_index = vui->sw_if_index;
1643 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1646 if (args->is_server)
1649 vhost_user_init_server_sock (args->sock_filename,
1650 &server_sock_fd)) != 0)
1656 /* Protect the uninitialized vui from being dispatched by rx/tx */
1657 vlib_worker_thread_barrier_sync (vm);
1658 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1659 vhost_user_create_ethernet (vnm, vm, vui, args);
1660 vlib_worker_thread_barrier_release (vm);
1662 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1663 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1664 vhost_user_rx_thread_placement (vui, 1);
1667 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1669 args->sw_if_index = sw_if_idx;
1671 // Process node must connect
1672 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1678 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1679 vhost_user_create_if_args_t * args)
1681 vhost_user_main_t *vum = &vhost_user_main;
1682 vhost_user_intf_t *vui = NULL;
1684 int server_sock_fd = -1;
1686 vnet_hw_interface_t *hwif;
1689 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1691 || hwif->dev_class_index != vhost_user_device_class.index)
1692 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1694 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1695 return VNET_API_ERROR_INVALID_ARGUMENT;
1697 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1700 * Disallow changing the interface to have the same path name
1701 * as other interface
1703 if_index = mhash_get (&vum->if_index_by_sock_name,
1704 (void *) args->sock_filename);
1705 if (if_index && (*if_index != vui->if_index))
1706 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1708 // First try to open server socket
1709 if (args->is_server)
1710 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1711 &server_sock_fd)) != 0)
1714 vhost_user_term_if (vui);
1715 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1718 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1720 // Process node must connect
1721 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1727 vhost_user_connect_command_fn (vlib_main_t * vm,
1728 unformat_input_t * input,
1729 vlib_cli_command_t * cmd)
1731 vnet_main_t *vnm = vnet_get_main ();
1732 unformat_input_t _line_input, *line_input = &_line_input;
1733 clib_error_t *error = NULL;
1734 vhost_user_create_if_args_t args = { 0 };
1737 /* Get a line of input. */
1738 if (!unformat_user (input, unformat_line_input, line_input))
1741 args.feature_mask = (u64) ~ (0ULL);
1742 args.custom_dev_instance = ~0;
1743 /* GSO feature is disable by default */
1744 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1745 /* packed-ring feature is disable by default */
1746 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1747 /* event_idx feature is disable by default */
1748 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1750 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1752 if (unformat (line_input, "socket %s", &args.sock_filename))
1754 else if (unformat (line_input, "server"))
1756 else if (unformat (line_input, "gso"))
1757 args.enable_gso = 1;
1758 else if (unformat (line_input, "packed"))
1759 args.enable_packed = 1;
1760 else if (unformat (line_input, "event-idx"))
1761 args.enable_event_idx = 1;
1762 else if (unformat (line_input, "feature-mask 0x%llx",
1763 &args.feature_mask))
1765 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1767 args.use_custom_mac = 1;
1768 else if (unformat (line_input, "renumber %d",
1769 &args.custom_dev_instance))
1773 error = clib_error_return (0, "unknown input `%U'",
1774 format_unformat_error, line_input);
1779 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1781 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1785 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1789 vec_free (args.sock_filename);
1790 unformat_free (line_input);
1796 vhost_user_delete_command_fn (vlib_main_t * vm,
1797 unformat_input_t * input,
1798 vlib_cli_command_t * cmd)
1800 unformat_input_t _line_input, *line_input = &_line_input;
1801 u32 sw_if_index = ~0;
1802 vnet_main_t *vnm = vnet_get_main ();
1803 clib_error_t *error = NULL;
1805 /* Get a line of input. */
1806 if (!unformat_user (input, unformat_line_input, line_input))
1809 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1811 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1814 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1817 vnet_hw_interface_t *hwif =
1818 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1820 vhost_user_device_class.index != hwif->dev_class_index)
1822 error = clib_error_return (0, "Not a vhost interface");
1828 error = clib_error_return (0, "unknown input `%U'",
1829 format_unformat_error, line_input);
1834 vhost_user_delete_if (vnm, vm, sw_if_index);
1837 unformat_free (line_input);
1843 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1844 vhost_user_intf_details_t ** out_vuids)
1847 vhost_user_main_t *vum = &vhost_user_main;
1848 vhost_user_intf_t *vui;
1849 vhost_user_intf_details_t *r_vuids = NULL;
1850 vhost_user_intf_details_t *vuid = NULL;
1851 u32 *hw_if_indices = 0;
1852 vnet_hw_interface_t *hi;
1858 pool_foreach (vui, vum->vhost_user_interfaces)
1859 vec_add1 (hw_if_indices, vui->hw_if_index);
1861 for (i = 0; i < vec_len (hw_if_indices); i++)
1863 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1864 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1866 vec_add2 (r_vuids, vuid, 1);
1867 vuid->sw_if_index = vui->sw_if_index;
1868 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1869 vuid->features = vui->features;
1870 vuid->num_regions = vui->nregions;
1871 vuid->is_server = vui->unix_server_index != ~0;
1872 vuid->sock_errno = vui->sock_errno;
1873 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1874 "%s", vui->sock_filename);
1875 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1876 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1877 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1880 vec_free (hw_if_indices);
1882 *out_vuids = r_vuids;
1888 format_vhost_user_desc (u8 * s, va_list * args)
1890 char *fmt = va_arg (*args, char *);
1891 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1892 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1893 int idx = va_arg (*args, int);
1894 u32 *mem_hint = va_arg (*args, u32 *);
1896 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1897 desc_table[idx].flags, desc_table[idx].next,
1898 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1904 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1906 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1907 int callfd = UNIX_GET_FD (vq->callfd_idx);
1909 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1914 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1915 int show_descr, int show_verbose)
1921 vring_desc_t *desc_table;
1922 vhost_user_vring_t *vq = &vui->vrings[q];
1924 if (vq->avail && vq->used)
1925 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1926 "used event idx %u used.idx %d\n", vq->avail->flags,
1927 vhost_user_avail_event_idx (vq), vq->avail->idx,
1928 vhost_user_used_event_idx (vq), vq->used->idx);
1930 vhost_user_show_fds (vm, vq);
1934 vlib_cli_output (vm, "\n descriptor table:\n");
1935 vlib_cli_output (vm,
1936 " slot addr len flags next "
1938 vlib_cli_output (vm,
1939 " ===== ================== ===== ====== ===== "
1940 "==================\n");
1941 for (j = 0; j < vq->qsz_mask + 1; j++)
1943 desc_table = vq->desc;
1944 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1945 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1946 desc_table, j, &mem_hint);
1947 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1949 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1950 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1953 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1956 (vm, "%U", format_vhost_user_desc,
1957 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1958 desc_table, idx, &mem_hint);
1960 if (n_entries >= 20)
1961 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1970 format_vhost_user_packed_desc (u8 * s, va_list * args)
1972 char *fmt = va_arg (*args, char *);
1973 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1974 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1975 int idx = va_arg (*args, int);
1976 u32 *mem_hint = va_arg (*args, u32 *);
1978 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1979 desc_table[idx].flags, desc_table[idx].id,
1980 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1986 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
1988 u32 flags = va_arg (*args, u32);
1994 static event_idx_flags event_idx_array[] = {
1995 #define _(s,v) { .str = #s, .value = v, },
1996 foreach_virtio_event_idx_flags
1999 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2001 if (flags < num_entries)
2002 s = format (s, "%s", event_idx_array[flags].str);
2004 s = format (s, "%u", flags);
2009 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2010 int show_descr, int show_verbose)
2016 vring_packed_desc_t *desc_table;
2017 vhost_user_vring_t *vq = &vui->vrings[q];
2018 u16 off_wrap, event_idx;
2020 off_wrap = vq->avail_event->off_wrap;
2021 event_idx = off_wrap & 0x7fff;
2022 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2023 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2024 (u32) vq->avail_event->flags, off_wrap, event_idx);
2026 off_wrap = vq->used_event->off_wrap;
2027 event_idx = off_wrap & 0x7fff;
2028 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2029 "used event idx %u\n", format_vhost_user_event_idx_flags,
2030 (u32) vq->used_event->flags, off_wrap, event_idx);
2032 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2033 vq->avail_wrap_counter, vq->used_wrap_counter);
2035 vhost_user_show_fds (vm, vq);
2039 vlib_cli_output (vm, "\n descriptor table:\n");
2040 vlib_cli_output (vm,
2041 " slot addr len flags id "
2043 vlib_cli_output (vm,
2044 " ===== ================== ===== ====== ===== "
2045 "==================\n");
2046 for (j = 0; j < vq->qsz_mask + 1; j++)
2048 desc_table = vq->packed_desc;
2049 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2050 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2051 desc_table, j, &mem_hint);
2052 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2054 n_entries = desc_table[j].len >> 4;
2055 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2058 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2061 (vm, "%U", format_vhost_user_packed_desc,
2062 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2063 desc_table, idx, &mem_hint);
2065 if (n_entries >= 20)
2066 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2075 show_vhost_user_command_fn (vlib_main_t * vm,
2076 unformat_input_t * input,
2077 vlib_cli_command_t * cmd)
2079 clib_error_t *error = 0;
2080 vnet_main_t *vnm = vnet_get_main ();
2081 vhost_user_main_t *vum = &vhost_user_main;
2082 vhost_user_intf_t *vui;
2083 u32 hw_if_index, *hw_if_indices = 0;
2084 vnet_hw_interface_t *hi;
2089 int show_verbose = 0;
2095 struct feat_struct *feat_entry;
2097 static struct feat_struct feat_array[] = {
2098 #define _(s,b) { .str = #s, .bit = b, },
2099 foreach_virtio_net_features
2104 #define foreach_protocol_feature \
2105 _(VHOST_USER_PROTOCOL_F_MQ) \
2106 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2108 static struct feat_struct proto_feat_array[] = {
2109 #define _(s) { .str = #s, .bit = s},
2110 foreach_protocol_feature
2115 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2118 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2120 hi = vnet_get_hw_interface (vnm, hw_if_index);
2121 if (vhost_user_device_class.index != hi->dev_class_index)
2123 error = clib_error_return (0, "unknown input `%U'",
2124 format_unformat_error, input);
2127 vec_add1 (hw_if_indices, hw_if_index);
2129 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2131 else if (unformat (input, "verbose"))
2135 error = clib_error_return (0, "unknown input `%U'",
2136 format_unformat_error, input);
2140 if (vec_len (hw_if_indices) == 0)
2142 pool_foreach (vui, vum->vhost_user_interfaces)
2143 vec_add1 (hw_if_indices, vui->hw_if_index);
2145 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2146 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2147 vum->coalesce_frames, vum->coalesce_time);
2148 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2150 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2152 for (i = 0; i < vec_len (hw_if_indices); i++)
2154 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2155 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2156 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2157 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2159 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2160 if (vui->enable_gso)
2161 vlib_cli_output (vm, " GSO enable");
2162 if (vui->enable_packed)
2163 vlib_cli_output (vm, " Packed ring enable");
2164 if (vui->enable_event_idx)
2165 vlib_cli_output (vm, " Event index enable");
2167 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2168 " features mask (0x%llx): \n"
2169 " features (0x%llx): \n",
2170 vui->virtio_net_hdr_sz, vui->feature_mask,
2173 feat_entry = (struct feat_struct *) &feat_array;
2174 while (feat_entry->str)
2176 if (vui->features & (1ULL << feat_entry->bit))
2177 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2182 vlib_cli_output (vm, " protocol features (0x%llx)",
2183 vui->protocol_features);
2184 feat_entry = (struct feat_struct *) &proto_feat_array;
2185 while (feat_entry->str)
2187 if (vui->protocol_features & (1ULL << feat_entry->bit))
2188 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2193 vlib_cli_output (vm, "\n");
2195 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2197 (vui->unix_server_index != ~0) ? "server" : "client",
2198 strerror (vui->sock_errno));
2200 vlib_cli_output (vm, " rx placement: ");
2202 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2204 vnet_main_t *vnm = vnet_get_main ();
2206 vhost_user_vring_t *txvq = &vui->vrings[qid];
2208 if (txvq->qid == -1)
2211 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
2212 vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2213 qid, format_vnet_hw_if_rx_mode, txvq->mode);
2216 vlib_cli_output (vm, " tx placement: %s\n",
2217 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2219 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2221 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2222 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2225 vlib_cli_output (vm, "\n");
2227 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2231 vlib_cli_output (vm,
2232 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2233 vlib_cli_output (vm,
2234 " ====== ===== ================== ================== ================== ================== ==================\n");
2236 for (j = 0; j < vui->nregions; j++)
2238 vlib_cli_output (vm,
2239 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2240 j, vui->region_mmap_fd[j],
2241 vui->regions[j].guest_phys_addr,
2242 vui->regions[j].memory_size,
2243 vui->regions[j].userspace_addr,
2244 vui->regions[j].mmap_offset,
2245 pointer_to_uword (vui->region_mmap_addr[j]));
2247 for (q = 0; q < vui->num_qid; q++)
2249 if (!vui->vrings[q].started)
2252 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2253 (q & 1) ? "RX" : "TX",
2254 vui->vrings[q].enabled ? "" : " disabled");
2256 vlib_cli_output (vm,
2257 " qsz %d last_avail_idx %d last_used_idx %d"
2259 vui->vrings[q].qsz_mask + 1,
2260 vui->vrings[q].last_avail_idx,
2261 vui->vrings[q].last_used_idx,
2262 vui->vrings[q].last_kick);
2264 if (vhost_user_is_packed_ring_supported (vui))
2265 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2268 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2270 vlib_cli_output (vm, "\n");
2273 vec_free (hw_if_indices);
2282 * Create a vHost User interface. Once created, a new virtual interface
2283 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2284 * is the next free index.
2286 * There are several parameters associated with a vHost interface:
2288 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2289 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2290 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2291 * hypervisor will create the socket if it does not already exist. The VPP code
2292 * is indifferent to the file location. However, if SELinux is enabled, then the
2293 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2295 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2296 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2297 * mode, the VM can be reset without tearing down the vHost Interface. In
2298 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2299 * tearing down the vHost Interface.
2301 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2302 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2303 * parameter not be used except by experienced users. By default, all supported
2304 * features will be advertised. Otherwise, provide the set of features desired.
2305 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2306 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2307 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2308 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2309 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2310 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2311 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2312 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2313 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2315 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2316 * X:X:X:X:X:X unix or X.X.X cisco format.
2318 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2319 * in the name to be specified. If instance already exists, name will be used
2320 * anyway and multiple instances will have the same name. Use with caution.
2323 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2324 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2325 * VirtualEthernet0/0/0
2327 * Example of how to create a vhost interface with VPP as the server and with just
2328 * multiple queues enabled:
2329 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2330 * VirtualEthernet0/0/1
2332 * Once the vHost interface is created, enable the interface using:
2333 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2336 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2337 .path = "create vhost-user",
2338 .short_help = "create vhost-user socket <socket-filename> [server] "
2339 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2340 "[packed] [event-idx]",
2341 .function = vhost_user_connect_command_fn,
2347 * Delete a vHost User interface using the interface name or the
2348 * software interface index. Use the '<em>show interface</em>'
2349 * command to determine the software interface index. On deletion,
2350 * the linux socket will not be deleted.
2353 * Example of how to delete a vhost interface by name:
2354 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2355 * Example of how to delete a vhost interface by software interface index:
2356 * @cliexcmd{delete vhost-user sw_if_index 1}
2359 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2360 .path = "delete vhost-user",
2361 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2362 .function = vhost_user_delete_command_fn,
2366 * Display the attributes of a single vHost User interface (provide interface
2367 * name), multiple vHost User interfaces (provide a list of interface names seperated
2368 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2369 * vHost interfaces).
2373 * Example of how to display a vhost interface:
2374 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2375 * Virtio vhost-user interfaces
2377 * coalesce frames 32 time 1e-3
2378 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2379 * virtio_net_hdr_sz 12
2380 * features mask (0xffffffffffffffff):
2381 * features (0x50408000):
2382 * VIRTIO_NET_F_MRG_RXBUF (15)
2383 * VIRTIO_NET_F_MQ (22)
2384 * VIRTIO_F_INDIRECT_DESC (28)
2385 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2386 * protocol features (0x3)
2387 * VHOST_USER_PROTOCOL_F_MQ (0)
2388 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2390 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2393 * thread 1 on vring 1
2394 * thread 1 on vring 5
2395 * thread 2 on vring 3
2396 * thread 2 on vring 7
2397 * tx placement: spin-lock
2398 * thread 0 on vring 0
2399 * thread 1 on vring 2
2400 * thread 2 on vring 0
2402 * Memory regions (total 2)
2403 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2404 * ====== ===== ================== ================== ================== ================== ==================
2405 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2406 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2409 * qsz 256 last_avail_idx 0 last_used_idx 0
2410 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2411 * kickfd 62 callfd 64 errfd -1
2414 * qsz 256 last_avail_idx 0 last_used_idx 0
2415 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2416 * kickfd 65 callfd 66 errfd -1
2419 * qsz 256 last_avail_idx 0 last_used_idx 0
2420 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2421 * kickfd 63 callfd 70 errfd -1
2424 * qsz 256 last_avail_idx 0 last_used_idx 0
2425 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2426 * kickfd 72 callfd 74 errfd -1
2428 * Virtqueue 4 (TX disabled)
2429 * qsz 256 last_avail_idx 0 last_used_idx 0
2430 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2431 * kickfd 76 callfd 78 errfd -1
2433 * Virtqueue 5 (RX disabled)
2434 * qsz 256 last_avail_idx 0 last_used_idx 0
2435 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2436 * kickfd 80 callfd 82 errfd -1
2438 * Virtqueue 6 (TX disabled)
2439 * qsz 256 last_avail_idx 0 last_used_idx 0
2440 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2441 * kickfd 84 callfd 86 errfd -1
2443 * Virtqueue 7 (RX disabled)
2444 * qsz 256 last_avail_idx 0 last_used_idx 0
2445 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2446 * kickfd 88 callfd 90 errfd -1
2450 * The optional '<em>descriptors</em>' parameter will display the same output as
2451 * the previous example but will include the descriptor table for each queue.
2452 * The output is truncated below:
2453 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2454 * Virtio vhost-user interfaces
2456 * coalesce frames 32 time 1e-3
2457 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2458 * virtio_net_hdr_sz 12
2459 * features mask (0xffffffffffffffff):
2460 * features (0x50408000):
2461 * VIRTIO_NET_F_MRG_RXBUF (15)
2462 * VIRTIO_NET_F_MQ (22)
2465 * qsz 256 last_avail_idx 0 last_used_idx 0
2466 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2467 * kickfd 62 callfd 64 errfd -1
2470 * id addr len flags next user_addr
2471 * ===== ================== ===== ====== ===== ==================
2472 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2473 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2474 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2475 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2476 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2477 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2478 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2479 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2480 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2481 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2482 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2484 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2485 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2486 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2487 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2488 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2489 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2490 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2493 * qsz 256 last_avail_idx 0 last_used_idx 0
2499 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2500 .path = "show vhost-user",
2501 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2502 "[[descriptors] [verbose]]",
2503 .function = show_vhost_user_command_fn,
2508 static clib_error_t *
2509 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2511 vhost_user_main_t *vum = &vhost_user_main;
2513 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2515 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2517 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2519 else if (unformat (input, "dont-dump-memory"))
2520 vum->dont_dump_vhost_user_memory = 1;
2522 return clib_error_return (0, "unknown input `%U'",
2523 format_unformat_error, input);
2529 /* vhost-user { ... } configuration. */
2530 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2533 vhost_user_unmap_all (void)
2535 vhost_user_main_t *vum = &vhost_user_main;
2536 vhost_user_intf_t *vui;
2538 if (vum->dont_dump_vhost_user_memory)
2540 pool_foreach (vui, vum->vhost_user_interfaces)
2541 unmap_all_mem_regions (vui);
2546 * fd.io coding-style-patch-verification: ON
2549 * eval: (c-set-style "gnu")