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>
40 #include <vnet/devices/virtio/vhost_user.h>
41 #include <vnet/devices/virtio/vhost_user_inline.h>
45 * @brief vHost User Device Driver.
47 * This file contains the source code for vHost User interface.
51 vlib_node_registration_t vhost_user_send_interrupt_node;
54 vhost_user_main_t vhost_user_main = {
58 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
64 get_huge_page_size (int fd)
72 unmap_all_mem_regions (vhost_user_intf_t * vui)
75 vhost_user_vring_t *vq;
77 for (i = 0; i < vui->nregions; i++)
79 if (vui->region_mmap_addr[i] != MAP_FAILED)
82 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
84 ssize_t map_sz = (vui->regions[i].memory_size +
85 vui->regions[i].mmap_offset +
86 page_sz - 1) & ~(page_sz - 1);
89 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
92 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
93 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
96 vui->region_mmap_addr[i] = MAP_FAILED;
100 vu_log_err (vui, "failed to unmap memory region (errno %d)",
103 close (vui->region_mmap_fd[i]);
108 for (q = 0; q < vui->num_qid; q++)
110 vq = &vui->vrings[q];
117 static_always_inline void
118 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
120 //Let's try to assign one queue to each thread
122 u32 thread_index = 0;
124 vui->use_tx_spinlock = 0;
127 for (qid = 0; qid < vui->num_qid / 2; qid++)
129 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
130 if (!rxvq->started || !rxvq->enabled)
133 vui->per_cpu_tx_qid[thread_index] = qid;
135 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
138 //We need to loop, meaning the spinlock has to be used
139 vui->use_tx_spinlock = 1;
140 if (thread_index == 0)
142 //Could not find a single valid one
143 for (thread_index = 0;
144 thread_index < vlib_get_thread_main ()->n_vlib_mains;
147 vui->per_cpu_tx_qid[thread_index] = 0;
155 * @brief Unassign existing interface/queue to thread mappings and re-assign
156 * new interface/queue to thread mappings
158 static_always_inline void
159 vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
161 vhost_user_vring_t *txvq = &vui->vrings[qid];
162 vnet_main_t *vnm = vnet_get_main ();
166 ASSERT ((qid & 1) == 1); // should be odd
167 // Assign new queue mappings for the interface
168 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
169 vhost_user_input_node.index);
170 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, q, ~0);
171 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
172 /* Set polling as the default */
173 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
175 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
177 vu_log_warn (vui, "unable to set rx mode for interface %d, "
178 "queue %d: rc=%d", vui->hw_if_index, q, rv);
181 /** @brief Returns whether at least one TX and one RX vring are enabled */
182 static_always_inline int
183 vhost_user_intf_ready (vhost_user_intf_t * vui)
185 int i, found[2] = { }; //RX + TX
187 for (i = 0; i < vui->num_qid; i++)
188 if (vui->vrings[i].started && vui->vrings[i].enabled)
191 return found[0] && found[1];
194 static_always_inline void
195 vhost_user_update_iface_state (vhost_user_intf_t * vui)
197 /* if we have pointers to descriptor table, go up */
198 int is_ready = vhost_user_intf_ready (vui);
199 if (is_ready != vui->is_ready)
201 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
202 is_ready ? "ready" : "down");
204 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
205 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
207 vui->is_ready = is_ready;
212 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
215 vnet_main_t *vnm = vnet_get_main ();
219 /* Only care about the odd number, or TX, virtqueue */
222 if (vhost_user_intf_ready (vui))
223 // qid >> 1 is to convert virtqueue number to vring queue index
224 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
227 static clib_error_t *
228 vhost_user_callfd_read_ready (clib_file_t * uf)
230 __attribute__ ((unused)) int n;
233 n = read (uf->file_descriptor, ((char *) &buff), 8);
238 static_always_inline void
239 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
241 if (qid & 1) // RX is odd, TX is even
243 if (vui->vrings[qid].qid == -1)
244 vhost_user_rx_thread_placement (vui, qid);
247 vhost_user_tx_thread_placement (vui);
250 static clib_error_t *
251 vhost_user_kickfd_read_ready (clib_file_t * uf)
253 __attribute__ ((unused)) int n;
255 vhost_user_intf_t *vui =
256 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
257 uf->private_data >> 8);
258 u32 qid = uf->private_data & 0xff;
260 n = read (uf->file_descriptor, ((char *) &buff), 8);
261 vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
262 if (!vui->vrings[qid].started ||
263 (vhost_user_intf_ready (vui) != vui->is_ready))
265 if (vui->vrings[qid].started == 0)
267 vui->vrings[qid].started = 1;
268 vhost_user_thread_placement (vui, qid);
269 vhost_user_update_iface_state (vui);
273 vhost_user_set_interrupt_pending (vui, uf->private_data);
277 static_always_inline void
278 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
280 vhost_user_vring_t *vring = &vui->vrings[qid];
282 clib_memset (vring, 0, sizeof (*vring));
283 vring->kickfd_idx = ~0;
284 vring->callfd_idx = ~0;
288 clib_spinlock_init (&vring->vring_lock);
291 * We have a bug with some qemu 2.5, and this may be a fix.
292 * Feel like interpretation holy text, but this is from vhost-user.txt.
294 * One queue pair is enabled initially. More queues are enabled
295 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
297 * Don't know who's right, but this is what DPDK does.
299 if (qid == 0 || qid == 1)
303 static_always_inline void
304 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
306 vhost_user_vring_t *vring = &vui->vrings[qid];
308 if (vring->kickfd_idx != ~0)
310 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
312 clib_file_del (&file_main, uf);
313 vring->kickfd_idx = ~0;
315 if (vring->callfd_idx != ~0)
317 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
319 clib_file_del (&file_main, uf);
320 vring->callfd_idx = ~0;
322 if (vring->errfd != -1)
324 close (vring->errfd);
328 clib_spinlock_free (&vring->vring_lock);
330 // save the qid so that we don't need to unassign and assign_rx_thread
331 // when the interface comes back up. They are expensive calls.
332 u16 q = vui->vrings[qid].qid;
333 vhost_user_vring_init (vui, qid);
334 vui->vrings[qid].qid = q;
337 static_always_inline void
338 vhost_user_if_disconnect (vhost_user_intf_t * vui)
340 vnet_main_t *vnm = vnet_get_main ();
343 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
345 if (vui->clib_file_index != ~0)
347 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
348 vui->clib_file_index = ~0;
353 for (q = 0; q < vui->num_qid; q++)
354 vhost_user_vring_close (vui, q);
356 unmap_all_mem_regions (vui);
357 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
360 static clib_error_t *
361 vhost_user_socket_read (clib_file_t * uf)
364 int fd, number_of_fds = 0;
365 int fds[VHOST_MEMORY_MAX_NREGIONS];
366 vhost_user_msg_t msg;
369 vhost_user_main_t *vum = &vhost_user_main;
370 vhost_user_intf_t *vui;
371 struct cmsghdr *cmsg;
373 clib_file_t template = { 0 };
374 vnet_main_t *vnm = vnet_get_main ();
375 vlib_main_t *vm = vlib_get_main ();
377 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
379 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
381 clib_memset (&mh, 0, sizeof (mh));
382 clib_memset (control, 0, sizeof (control));
384 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
387 /* set the payload */
388 iov[0].iov_base = (void *) &msg;
389 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
393 mh.msg_control = control;
394 mh.msg_controllen = sizeof (control);
396 n = recvmsg (uf->file_descriptor, &mh, 0);
398 if (n != VHOST_USER_MSG_HDR_SZ)
402 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
407 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
408 n, VHOST_USER_MSG_HDR_SZ);
413 if (mh.msg_flags & MSG_CTRUNC)
415 vu_log_debug (vui, "MSG_CTRUNC is set");
419 cmsg = CMSG_FIRSTHDR (&mh);
421 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
422 (cmsg->cmsg_type == SCM_RIGHTS) &&
423 (cmsg->cmsg_len - CMSG_LEN (0) <=
424 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
426 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
427 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
430 /* version 1, no reply bit set */
431 if ((msg.flags & 7) != 1)
433 vu_log_debug (vui, "malformed message received. closing socket");
440 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
444 vu_log_debug (vui, "read failed %s", strerror (errno));
447 else if (rv != msg.size)
449 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
457 case VHOST_USER_GET_FEATURES:
459 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
460 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
461 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
462 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
463 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
464 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
465 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
466 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
467 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
468 msg.u64 &= vui->feature_mask;
470 if (vui->enable_event_idx)
471 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
473 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
474 if (vui->enable_packed)
475 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
477 msg.size = sizeof (msg.u64);
478 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
479 "0x%016llx", vui->hw_if_index, msg.u64);
481 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
482 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
484 vu_log_debug (vui, "could not send message response");
489 case VHOST_USER_SET_FEATURES:
490 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
491 "0x%016llx", vui->hw_if_index, msg.u64);
493 vui->features = msg.u64;
496 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
497 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
498 vui->virtio_net_hdr_sz = 12;
500 vui->virtio_net_hdr_sz = 10;
503 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
505 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
506 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
507 if (vui->enable_gso &&
508 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
509 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
511 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
512 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
514 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
515 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
516 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
518 vhost_user_update_iface_state (vui);
521 case VHOST_USER_SET_MEM_TABLE:
522 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
523 vui->hw_if_index, msg.memory.nregions);
525 if ((msg.memory.nregions < 1) ||
526 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
528 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
529 VHOST_MEMORY_MAX_NREGIONS);
533 if (msg.memory.nregions != number_of_fds)
535 vu_log_debug (vui, "each memory region must have FD");
539 /* Do the mmap without barrier sync */
540 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
541 for (i = 0; i < msg.memory.nregions; i++)
543 long page_sz = get_huge_page_size (fds[i]);
545 /* align size to page */
546 ssize_t map_sz = (msg.memory.regions[i].memory_size +
547 msg.memory.regions[i].mmap_offset +
548 page_sz - 1) & ~(page_sz - 1);
550 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
551 MAP_SHARED, fds[i], 0);
552 if (region_mmap_addr[i] == MAP_FAILED)
554 vu_log_err (vui, "failed to map memory. errno is %d", errno);
555 for (j = 0; j < i; j++)
556 munmap (region_mmap_addr[j], map_sz);
559 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
560 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
561 region_mmap_addr[i], page_sz);
564 vlib_worker_thread_barrier_sync (vm);
565 unmap_all_mem_regions (vui);
566 for (i = 0; i < msg.memory.nregions; i++)
568 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
569 sizeof (vhost_user_memory_region_t));
571 vui->region_mmap_addr[i] = region_mmap_addr[i];
572 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
573 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
574 vui->regions[i].memory_size;
576 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
577 vui->region_mmap_fd[i] = fds[i];
583 * Re-compute desc, used, and avail descriptor table if vring address
586 for (q = 0; q < vui->num_qid; q++)
588 if (vui->vrings[q].desc_user_addr &&
589 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
591 vui->vrings[q].desc =
592 map_user_mem (vui, vui->vrings[q].desc_user_addr);
593 vui->vrings[q].used =
594 map_user_mem (vui, vui->vrings[q].used_user_addr);
595 vui->vrings[q].avail =
596 map_user_mem (vui, vui->vrings[q].avail_user_addr);
599 vlib_worker_thread_barrier_release (vm);
602 case VHOST_USER_SET_VRING_NUM:
603 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
604 vui->hw_if_index, msg.state.index, msg.state.num);
606 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
607 (msg.state.num == 0) || /* it cannot be zero */
608 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
609 (msg.state.index >= vui->num_qid))
611 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
612 " %d, msg.state.index %d, curruent max q %d",
613 msg.state.num, msg.state.index, vui->num_qid);
616 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
619 case VHOST_USER_SET_VRING_ADDR:
620 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
621 vui->hw_if_index, msg.state.index);
623 if (msg.state.index >= vui->num_qid)
625 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
626 " %u >= %u", msg.state.index, vui->num_qid);
630 if (msg.size < sizeof (msg.addr))
632 vu_log_debug (vui, "vhost message is too short (%d < %d)",
633 msg.size, sizeof (msg.addr));
637 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
638 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
639 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
641 if ((desc == NULL) || (used == NULL) || (avail == NULL))
643 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
648 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
649 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
650 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
652 vlib_worker_thread_barrier_sync (vm);
653 vui->vrings[msg.state.index].desc = desc;
654 vui->vrings[msg.state.index].used = used;
655 vui->vrings[msg.state.index].avail = avail;
657 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
658 vui->vrings[msg.state.index].log_used =
659 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
661 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
662 the ring is initialized in an enabled state. */
663 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
664 vui->vrings[msg.state.index].enabled = 1;
666 vui->vrings[msg.state.index].last_used_idx =
667 vui->vrings[msg.state.index].last_avail_idx =
668 vui->vrings[msg.state.index].used->idx;
669 vui->vrings[msg.state.index].last_kick =
670 vui->vrings[msg.state.index].last_used_idx;
672 /* tell driver that we don't want interrupts */
673 if (vhost_user_is_packed_ring_supported (vui))
674 vui->vrings[msg.state.index].used_event->flags =
675 VRING_EVENT_F_DISABLE;
677 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
678 vlib_worker_thread_barrier_release (vm);
679 vhost_user_update_iface_state (vui);
682 case VHOST_USER_SET_OWNER:
683 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
686 case VHOST_USER_RESET_OWNER:
687 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
691 case VHOST_USER_SET_VRING_CALL:
692 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
693 vui->hw_if_index, msg.u64);
695 q = (u8) (msg.u64 & 0xFF);
696 if (vui->num_qid > q)
698 /* if there is old fd, delete and close it */
699 if (vui->vrings[q].callfd_idx != ~0)
701 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
702 vui->vrings[q].callfd_idx);
703 clib_file_del (&file_main, uf);
704 vui->vrings[q].callfd_idx = ~0;
707 else if (vec_len (vui->vrings) > q)
709 /* grow vrings by pair (RX + TX) */
710 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
714 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
717 * Double the array size if it is less than 64 entries.
718 * Slow down thereafter.
720 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
721 new_max_q = vec_len (vui->vrings) << 1;
723 new_max_q = vec_len (vui->vrings) +
724 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
725 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
726 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
728 /* sync with the worker threads, vrings may move due to realloc */
729 vlib_worker_thread_barrier_sync (vm);
730 vec_validate_aligned (vui->vrings, new_max_q - 1,
731 CLIB_CACHE_LINE_BYTES);
732 vlib_worker_thread_barrier_release (vm);
734 for (i = old_max_q; i < vec_len (vui->vrings); i++)
735 vhost_user_vring_init (vui, i);
737 /* grow vrings by pair (RX + TX) */
738 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
741 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
743 if (number_of_fds != 1)
745 vu_log_debug (vui, "More than one fd received !");
749 template.read_function = vhost_user_callfd_read_ready;
750 template.file_descriptor = fds[0];
751 template.private_data =
752 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
753 template.description = format (0, "vhost user");
754 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
757 vui->vrings[q].callfd_idx = ~0;
760 case VHOST_USER_SET_VRING_KICK:
761 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
762 vui->hw_if_index, msg.u64);
764 q = (u8) (msg.u64 & 0xFF);
765 if (q >= vui->num_qid)
767 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
768 " %u >= %u", q, vui->num_qid);
772 if (vui->vrings[q].kickfd_idx != ~0)
774 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
775 vui->vrings[q].kickfd_idx);
776 clib_file_del (&file_main, uf);
777 vui->vrings[q].kickfd_idx = ~0;
780 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
782 if (number_of_fds != 1)
784 vu_log_debug (vui, "More than one fd received !");
788 template.read_function = vhost_user_kickfd_read_ready;
789 template.file_descriptor = fds[0];
790 template.private_data =
791 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
793 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
797 //When no kickfd is set, the queue is initialized as started
798 vui->vrings[q].kickfd_idx = ~0;
799 vui->vrings[q].started = 1;
800 vhost_user_thread_placement (vui, q);
802 vhost_user_update_iface_state (vui);
805 case VHOST_USER_SET_VRING_ERR:
806 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
807 vui->hw_if_index, msg.u64);
809 q = (u8) (msg.u64 & 0xFF);
810 if (q >= vui->num_qid)
812 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
813 " %u >= %u", q, vui->num_qid);
817 if (vui->vrings[q].errfd != -1)
818 close (vui->vrings[q].errfd);
820 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
822 if (number_of_fds != 1)
825 vui->vrings[q].errfd = fds[0];
828 vui->vrings[q].errfd = -1;
831 case VHOST_USER_SET_VRING_BASE:
833 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
834 vui->hw_if_index, msg.state.index, msg.state.num);
835 if (msg.state.index >= vui->num_qid)
837 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
838 " %u >= %u", msg.state.index, vui->num_qid);
841 vlib_worker_thread_barrier_sync (vm);
842 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
843 if (vhost_user_is_packed_ring_supported (vui))
847 * 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
848 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
849 * | last avail idx | | last used idx | |
850 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
853 * avail wrap counter used wrap counter
855 /* last avail idx at bit 0-14. */
856 vui->vrings[msg.state.index].last_avail_idx =
857 msg.state.num & 0x7fff;
858 /* avail wrap counter at bit 15 */
859 vui->vrings[msg.state.index].avail_wrap_counter =
860 ! !(msg.state.num & (1 << 15));
863 * Although last_used_idx is passed in the upper 16 bits in qemu
864 * implementation, in practice, last_avail_idx and last_used_idx are
865 * usually the same. As a result, DPDK does not bother to pass us
866 * last_used_idx. The spec is not clear on thex coding. I figured it
867 * out by reading the qemu code. So let's just read last_avail_idx
868 * and set last_used_idx equals to last_avail_idx.
870 vui->vrings[msg.state.index].last_used_idx =
871 vui->vrings[msg.state.index].last_avail_idx;
872 vui->vrings[msg.state.index].last_kick =
873 vui->vrings[msg.state.index].last_used_idx;
874 vui->vrings[msg.state.index].used_wrap_counter =
875 vui->vrings[msg.state.index].avail_wrap_counter;
877 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
878 vui->vrings[msg.state.index].avail_wrap_counter =
881 vlib_worker_thread_barrier_release (vm);
884 case VHOST_USER_GET_VRING_BASE:
885 if (msg.state.index >= vui->num_qid)
887 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
888 " %u >= %u", msg.state.index, vui->num_qid);
892 /* protection is needed to prevent rx/tx from changing last_avail_idx */
893 vlib_worker_thread_barrier_sync (vm);
895 * Copy last_avail_idx from the vring before closing it because
896 * closing the vring also initializes the vring last_avail_idx
898 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
899 if (vhost_user_is_packed_ring_supported (vui))
902 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
903 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
905 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
906 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
909 msg.size = sizeof (msg.state);
912 * Spec says: Client must [...] stop ring upon receiving
913 * VHOST_USER_GET_VRING_BASE
915 vhost_user_vring_close (vui, msg.state.index);
916 vlib_worker_thread_barrier_release (vm);
918 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
919 vui->hw_if_index, msg.state.index, msg.state.num);
921 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
922 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
924 vu_log_debug (vui, "could not send message response");
927 vhost_user_update_iface_state (vui);
930 case VHOST_USER_NONE:
931 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
934 case VHOST_USER_SET_LOG_BASE:
935 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
938 if (msg.size != sizeof (msg.log))
940 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
941 " %d instead of %d", msg.size, sizeof (msg.log));
945 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
947 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
948 "VHOST_USER_SET_LOG_BASE received");
953 /* align size to page */
954 long page_sz = get_huge_page_size (fd);
956 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
958 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
961 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
962 "mapped 0x%lx", map_sz, msg.log.offset, fd,
965 if (log_base_addr == MAP_FAILED)
967 vu_log_err (vui, "failed to map memory. errno is %d", errno);
971 vlib_worker_thread_barrier_sync (vm);
972 vui->log_base_addr = log_base_addr;
973 vui->log_base_addr += msg.log.offset;
974 vui->log_size = msg.log.size;
975 vlib_worker_thread_barrier_release (vm);
978 msg.size = sizeof (msg.u64);
980 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
981 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
983 vu_log_debug (vui, "could not send message response");
988 case VHOST_USER_SET_LOG_FD:
989 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
992 case VHOST_USER_GET_PROTOCOL_FEATURES:
994 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
995 (1 << VHOST_USER_PROTOCOL_F_MQ);
996 msg.size = sizeof (msg.u64);
997 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
998 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1000 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1001 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1003 vu_log_debug (vui, "could not send message response");
1008 case VHOST_USER_SET_PROTOCOL_FEATURES:
1009 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1010 "features 0x%016llx", vui->hw_if_index, msg.u64);
1011 vui->protocol_features = msg.u64;
1014 case VHOST_USER_GET_QUEUE_NUM:
1016 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1017 msg.size = sizeof (msg.u64);
1018 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1019 vui->hw_if_index, msg.u64);
1021 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1022 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1024 vu_log_debug (vui, "could not send message response");
1029 case VHOST_USER_SET_VRING_ENABLE:
1030 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1031 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1033 if (msg.state.index >= vui->num_qid)
1035 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1036 " %u >= %u", msg.state.index, vui->num_qid);
1040 vui->vrings[msg.state.index].enabled = msg.state.num;
1041 vhost_user_thread_placement (vui, msg.state.index);
1042 vhost_user_update_iface_state (vui);
1046 vu_log_debug (vui, "unknown vhost-user message %d received. "
1047 "closing socket", msg.request);
1054 vlib_worker_thread_barrier_sync (vm);
1055 vhost_user_if_disconnect (vui);
1056 vlib_worker_thread_barrier_release (vm);
1057 vhost_user_update_iface_state (vui);
1061 static clib_error_t *
1062 vhost_user_socket_error (clib_file_t * uf)
1064 vlib_main_t *vm = vlib_get_main ();
1065 vhost_user_main_t *vum = &vhost_user_main;
1066 vhost_user_intf_t *vui =
1067 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1069 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1070 vlib_worker_thread_barrier_sync (vm);
1071 vhost_user_if_disconnect (vui);
1072 vlib_worker_thread_barrier_release (vm);
1076 static clib_error_t *
1077 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1079 int client_fd, client_len;
1080 struct sockaddr_un client;
1081 clib_file_t template = { 0 };
1082 vhost_user_main_t *vum = &vhost_user_main;
1083 vhost_user_intf_t *vui;
1085 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1087 client_len = sizeof (client);
1088 client_fd = accept (uf->file_descriptor,
1089 (struct sockaddr *) &client,
1090 (socklen_t *) & client_len);
1093 return clib_error_return_unix (0, "accept");
1095 if (vui->clib_file_index != ~0)
1097 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1098 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1099 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1102 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1103 vui->sw_if_index, client_fd);
1104 template.read_function = vhost_user_socket_read;
1105 template.error_function = vhost_user_socket_error;
1106 template.file_descriptor = client_fd;
1107 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1108 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1109 vui->clib_file_index = clib_file_add (&file_main, &template);
1114 static clib_error_t *
1115 vhost_user_init (vlib_main_t * vm)
1117 vhost_user_main_t *vum = &vhost_user_main;
1118 vlib_thread_main_t *tm = vlib_get_thread_main ();
1120 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1122 vum->coalesce_frames = 32;
1123 vum->coalesce_time = 1e-3;
1125 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1128 vec_foreach (cpu, vum->cpus)
1130 /* This is actually not necessary as validate already zeroes it
1131 * Just keeping the loop here for later because I am lazy. */
1132 cpu->rx_buffers_len = 0;
1135 vum->random = random_default_seed ();
1137 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1143 VLIB_INIT_FUNCTION (vhost_user_init) =
1145 .runs_after = VLIB_INITS("ip4_init"),
1150 vhost_user_send_interrupt_process (vlib_main_t * vm,
1151 vlib_node_runtime_t * rt, vlib_frame_t * f)
1153 vhost_user_intf_t *vui;
1154 f64 timeout = 3153600000.0 /* 100 years */ ;
1155 uword event_type, *event_data = 0;
1156 vhost_user_main_t *vum = &vhost_user_main;
1158 f64 now, poll_time_remaining;
1164 poll_time_remaining =
1165 vlib_process_wait_for_event_or_clock (vm, timeout);
1166 event_type = vlib_process_get_events (vm, &event_data);
1167 vec_reset_length (event_data);
1170 * Use the remaining timeout if it is less than coalesce time to avoid
1171 * resetting the existing timer in the middle of expiration
1173 timeout = poll_time_remaining;
1174 if (vlib_process_suspend_time_is_zero (timeout) ||
1175 (timeout > vum->coalesce_time))
1176 timeout = vum->coalesce_time;
1178 now = vlib_time_now (vm);
1181 case VHOST_USER_EVENT_STOP_TIMER:
1185 case VHOST_USER_EVENT_START_TIMER:
1187 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1193 pool_foreach (vui, vum->vhost_user_interfaces) {
1194 next_timeout = timeout;
1195 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1197 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1198 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1200 if (txvq->qid == -1)
1202 if (txvq->n_since_last_int)
1204 if (now >= txvq->int_deadline)
1205 vhost_user_send_call (vm, vui, txvq);
1207 next_timeout = txvq->int_deadline - now;
1210 if (rxvq->n_since_last_int)
1212 if (now >= rxvq->int_deadline)
1213 vhost_user_send_call (vm, vui, rxvq);
1215 next_timeout = rxvq->int_deadline - now;
1218 if ((next_timeout < timeout) && (next_timeout > 0.0))
1219 timeout = next_timeout;
1226 clib_warning ("BUG: unhandled event type %d", event_type);
1229 /* No less than 1 millisecond */
1233 timeout = 3153600000.0;
1239 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1240 .function = vhost_user_send_interrupt_process,
1241 .type = VLIB_NODE_TYPE_PROCESS,
1242 .name = "vhost-user-send-interrupt-process",
1247 vhost_user_process (vlib_main_t * vm,
1248 vlib_node_runtime_t * rt, vlib_frame_t * f)
1250 vhost_user_main_t *vum = &vhost_user_main;
1251 vhost_user_intf_t *vui;
1252 struct sockaddr_un sun;
1254 clib_file_t template = { 0 };
1255 f64 timeout = 3153600000.0 /* 100 years */ ;
1256 uword *event_data = 0;
1259 sun.sun_family = AF_UNIX;
1260 template.read_function = vhost_user_socket_read;
1261 template.error_function = vhost_user_socket_error;
1262 template.description = format (0, "vhost user process");
1266 vlib_process_wait_for_event_or_clock (vm, timeout);
1267 vlib_process_get_events (vm, &event_data);
1268 vec_reset_length (event_data);
1273 pool_foreach (vui, vum->vhost_user_interfaces) {
1275 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1276 if (vui->clib_file_index == ~0)
1279 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1282 * 1st time error or new error for this interface,
1283 * spit out the message and record the error
1285 if (!vui->sock_errno || (vui->sock_errno != errno))
1288 ("Error: Could not open unix socket for %s",
1289 vui->sock_filename);
1290 vui->sock_errno = errno;
1295 /* try to connect */
1296 strncpy (sun.sun_path, (char *) vui->sock_filename,
1297 sizeof (sun.sun_path) - 1);
1298 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1300 /* Avoid hanging VPP if the other end does not accept */
1301 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1302 clib_unix_warning ("fcntl");
1304 if (connect (sockfd, (struct sockaddr *) &sun,
1305 sizeof (struct sockaddr_un)) == 0)
1307 /* Set the socket to blocking as it was before */
1308 if (fcntl(sockfd, F_SETFL, 0) < 0)
1309 clib_unix_warning ("fcntl2");
1311 vui->sock_errno = 0;
1312 template.file_descriptor = sockfd;
1313 template.private_data =
1314 vui - vhost_user_main.vhost_user_interfaces;
1315 vui->clib_file_index = clib_file_add (&file_main, &template);
1318 /* This sockfd is considered consumed */
1323 vui->sock_errno = errno;
1328 /* check if socket is alive */
1330 socklen_t len = sizeof (error);
1331 int fd = UNIX_GET_FD(vui->clib_file_index);
1333 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1337 vu_log_debug (vui, "getsockopt returned %d", retval);
1338 vhost_user_if_disconnect (vui);
1349 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1350 .function = vhost_user_process,
1351 .type = VLIB_NODE_TYPE_PROCESS,
1352 .name = "vhost-user-process",
1357 * Disables and reset interface structure.
1358 * It can then be either init again, or removed from used interfaces.
1361 vhost_user_term_if (vhost_user_intf_t * vui)
1364 vhost_user_main_t *vum = &vhost_user_main;
1366 // disconnect interface sockets
1367 vhost_user_if_disconnect (vui);
1368 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1369 vhost_user_update_iface_state (vui);
1371 for (q = 0; q < vui->num_qid; q++)
1373 // Remove existing queue mapping for the interface
1377 vnet_main_t *vnm = vnet_get_main ();
1378 vhost_user_vring_t *txvq = &vui->vrings[q];
1380 if (txvq->qid != -1)
1382 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1386 vu_log_warn (vui, "unable to unassign interface %d, "
1387 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1391 clib_spinlock_free (&vui->vrings[q].vring_lock);
1394 if (vui->unix_server_index != ~0)
1396 //Close server socket
1397 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1398 vui->unix_server_index);
1399 clib_file_del (&file_main, uf);
1400 vui->unix_server_index = ~0;
1401 unlink (vui->sock_filename);
1404 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1409 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1411 vhost_user_main_t *vum = &vhost_user_main;
1412 vhost_user_intf_t *vui;
1414 vnet_hw_interface_t *hwif;
1419 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1420 || hwif->dev_class_index != vhost_user_device_class.index)
1421 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1423 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1425 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1426 hwif->name, hwif->dev_instance);
1428 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1430 vhost_user_vring_t *txvq = &vui->vrings[qid];
1432 if (txvq->qid == -1)
1434 if ((vum->ifq_count > 0) &&
1435 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1436 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1439 // Stop the timer if there is no more interrupt interface/queue
1440 if ((vum->ifq_count == 0) &&
1441 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1443 vlib_process_signal_event (vm,
1444 vhost_user_send_interrupt_node.index,
1445 VHOST_USER_EVENT_STOP_TIMER, 0);
1451 // Disable and reset interface
1452 vhost_user_term_if (vui);
1454 // Reset renumbered iface
1455 if (hwif->dev_instance <
1456 vec_len (vum->show_dev_instance_by_real_dev_instance))
1457 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1459 // Delete ethernet interface
1460 ethernet_delete_interface (vnm, vui->hw_if_index);
1463 vec_free (vui->vrings);
1466 pool_put (vum->vhost_user_interfaces, vui);
1471 static clib_error_t *
1472 vhost_user_exit (vlib_main_t * vm)
1474 vnet_main_t *vnm = vnet_get_main ();
1475 vhost_user_main_t *vum = &vhost_user_main;
1476 vhost_user_intf_t *vui;
1478 vlib_worker_thread_barrier_sync (vlib_get_main ());
1480 pool_foreach (vui, vum->vhost_user_interfaces) {
1481 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1484 vlib_worker_thread_barrier_release (vlib_get_main ());
1488 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1491 * Open server unix socket on specified sock_filename.
1494 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1497 struct sockaddr_un un = { };
1499 /* create listening socket */
1500 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1501 return VNET_API_ERROR_SYSCALL_ERROR_1;
1503 un.sun_family = AF_UNIX;
1504 strncpy ((char *) un.sun_path, (char *) sock_filename,
1505 sizeof (un.sun_path) - 1);
1507 /* remove if exists */
1508 unlink ((char *) sock_filename);
1510 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1512 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1516 if (listen (fd, 1) == -1)
1518 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1531 * Create ethernet interface for vhost user interface.
1534 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1535 vhost_user_intf_t * vui, u8 * hwaddress)
1537 vhost_user_main_t *vum = &vhost_user_main;
1539 clib_error_t *error;
1541 /* create hw and sw interface */
1544 clib_memcpy (hwaddr, hwaddress, 6);
1548 random_u32 (&vum->random);
1549 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1554 error = ethernet_register_interface
1556 vhost_user_device_class.index,
1557 vui - vum->vhost_user_interfaces /* device instance */ ,
1558 hwaddr /* ethernet address */ ,
1559 &vui->hw_if_index, 0 /* flag change */ );
1562 clib_error_report (error);
1566 * Initialize vui with specified attributes
1569 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1570 int server_sock_fd, vhost_user_create_if_args_t * args,
1573 vnet_sw_interface_t *sw;
1575 vhost_user_main_t *vum = &vhost_user_main;
1576 vnet_hw_interface_t *hw;
1578 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1579 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1580 if (server_sock_fd != -1)
1582 clib_file_t template = { 0 };
1583 template.read_function = vhost_user_socksvr_accept_ready;
1584 template.file_descriptor = server_sock_fd;
1585 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1586 template.description = format (0, "vhost user %d", sw);
1587 vui->unix_server_index = clib_file_add (&file_main, &template);
1591 vui->unix_server_index = ~0;
1594 vui->sw_if_index = sw->sw_if_index;
1595 strncpy (vui->sock_filename, args->sock_filename,
1596 ARRAY_LEN (vui->sock_filename) - 1);
1597 vui->sock_errno = 0;
1599 vui->feature_mask = args->feature_mask;
1600 vui->clib_file_index = ~0;
1601 vui->log_base_addr = 0;
1602 vui->if_index = vui - vum->vhost_user_interfaces;
1603 vui->enable_gso = args->enable_gso;
1604 vui->enable_event_idx = args->enable_event_idx;
1605 vui->enable_packed = args->enable_packed;
1607 * enable_gso takes precedence over configurable feature mask if there
1609 * if feature mask disables gso, but enable_gso is configured,
1610 * then gso is enable
1611 * if feature mask enables gso, but enable_gso is not configured,
1612 * then gso is enable
1614 * if gso is enable via feature mask, it must enable both host and guest
1615 * gso feature mask, we don't support one sided GSO or partial GSO.
1617 if ((vui->enable_gso == 0) &&
1618 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1619 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1620 vui->enable_gso = 1;
1621 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1622 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1625 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1626 CLIB_CACHE_LINE_BYTES);
1628 for (q = 0; q < vec_len (vui->vrings); q++)
1629 vhost_user_vring_init (vui, q);
1631 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1632 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1635 *sw_if_index = vui->sw_if_index;
1637 vec_validate (vui->per_cpu_tx_qid,
1638 vlib_get_thread_main ()->n_vlib_mains - 1);
1639 vhost_user_tx_thread_placement (vui);
1643 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1644 vhost_user_create_if_args_t * args)
1646 vhost_user_intf_t *vui = NULL;
1649 int server_sock_fd = -1;
1650 vhost_user_main_t *vum = &vhost_user_main;
1653 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1655 return VNET_API_ERROR_INVALID_ARGUMENT;
1658 if_index = mhash_get (&vum->if_index_by_sock_name,
1659 (void *) args->sock_filename);
1662 vui = &vum->vhost_user_interfaces[*if_index];
1663 args->sw_if_index = vui->sw_if_index;
1664 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1667 if (args->is_server)
1670 vhost_user_init_server_sock (args->sock_filename,
1671 &server_sock_fd)) != 0)
1677 /* Protect the uninitialized vui from being dispatched by rx/tx */
1678 vlib_worker_thread_barrier_sync (vm);
1679 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1680 vhost_user_create_ethernet (vnm, vm, vui, args->hwaddr);
1681 vlib_worker_thread_barrier_release (vm);
1683 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1684 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1685 vhost_user_rx_thread_placement (vui, 1);
1688 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1690 args->sw_if_index = sw_if_idx;
1692 // Process node must connect
1693 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1699 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1700 vhost_user_create_if_args_t * args)
1702 vhost_user_main_t *vum = &vhost_user_main;
1703 vhost_user_intf_t *vui = NULL;
1705 int server_sock_fd = -1;
1707 vnet_hw_interface_t *hwif;
1710 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1712 || hwif->dev_class_index != vhost_user_device_class.index)
1713 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1715 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1716 return VNET_API_ERROR_INVALID_ARGUMENT;
1718 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1721 * Disallow changing the interface to have the same path name
1722 * as other interface
1724 if_index = mhash_get (&vum->if_index_by_sock_name,
1725 (void *) args->sock_filename);
1726 if (if_index && (*if_index != vui->if_index))
1727 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1729 // First try to open server socket
1730 if (args->is_server)
1731 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1732 &server_sock_fd)) != 0)
1735 vhost_user_term_if (vui);
1736 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1739 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1741 // Process node must connect
1742 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1748 vhost_user_connect_command_fn (vlib_main_t * vm,
1749 unformat_input_t * input,
1750 vlib_cli_command_t * cmd)
1752 vnet_main_t *vnm = vnet_get_main ();
1753 unformat_input_t _line_input, *line_input = &_line_input;
1754 clib_error_t *error = NULL;
1755 vhost_user_create_if_args_t args = { 0 };
1758 /* Get a line of input. */
1759 if (!unformat_user (input, unformat_line_input, line_input))
1762 args.feature_mask = (u64) ~ (0ULL);
1763 args.custom_dev_instance = ~0;
1764 /* GSO feature is disable by default */
1765 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1766 /* packed-ring feature is disable by default */
1767 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1768 /* event_idx feature is disable by default */
1769 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1771 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1773 if (unformat (line_input, "socket %s", &args.sock_filename))
1775 else if (unformat (line_input, "server"))
1777 else if (unformat (line_input, "gso"))
1778 args.enable_gso = 1;
1779 else if (unformat (line_input, "packed"))
1780 args.enable_packed = 1;
1781 else if (unformat (line_input, "event-idx"))
1782 args.enable_event_idx = 1;
1783 else if (unformat (line_input, "feature-mask 0x%llx",
1784 &args.feature_mask))
1786 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1789 else if (unformat (line_input, "renumber %d",
1790 &args.custom_dev_instance))
1794 error = clib_error_return (0, "unknown input `%U'",
1795 format_unformat_error, line_input);
1800 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1802 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1806 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1810 vec_free (args.sock_filename);
1811 unformat_free (line_input);
1817 vhost_user_delete_command_fn (vlib_main_t * vm,
1818 unformat_input_t * input,
1819 vlib_cli_command_t * cmd)
1821 unformat_input_t _line_input, *line_input = &_line_input;
1822 u32 sw_if_index = ~0;
1823 vnet_main_t *vnm = vnet_get_main ();
1824 clib_error_t *error = NULL;
1826 /* Get a line of input. */
1827 if (!unformat_user (input, unformat_line_input, line_input))
1830 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1832 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1835 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1838 vnet_hw_interface_t *hwif =
1839 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1841 vhost_user_device_class.index != hwif->dev_class_index)
1843 error = clib_error_return (0, "Not a vhost interface");
1849 error = clib_error_return (0, "unknown input `%U'",
1850 format_unformat_error, line_input);
1855 vhost_user_delete_if (vnm, vm, sw_if_index);
1858 unformat_free (line_input);
1864 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1865 vhost_user_intf_details_t ** out_vuids)
1868 vhost_user_main_t *vum = &vhost_user_main;
1869 vhost_user_intf_t *vui;
1870 vhost_user_intf_details_t *r_vuids = NULL;
1871 vhost_user_intf_details_t *vuid = NULL;
1872 u32 *hw_if_indices = 0;
1873 vnet_hw_interface_t *hi;
1879 pool_foreach (vui, vum->vhost_user_interfaces)
1880 vec_add1 (hw_if_indices, vui->hw_if_index);
1882 for (i = 0; i < vec_len (hw_if_indices); i++)
1884 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1885 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1887 vec_add2 (r_vuids, vuid, 1);
1888 vuid->sw_if_index = vui->sw_if_index;
1889 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1890 vuid->features = vui->features;
1891 vuid->num_regions = vui->nregions;
1892 vuid->is_server = vui->unix_server_index != ~0;
1893 vuid->sock_errno = vui->sock_errno;
1894 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1895 "%s", vui->sock_filename);
1896 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1897 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1898 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1901 vec_free (hw_if_indices);
1903 *out_vuids = r_vuids;
1909 format_vhost_user_desc (u8 * s, va_list * args)
1911 char *fmt = va_arg (*args, char *);
1912 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1913 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1914 int idx = va_arg (*args, int);
1915 u32 *mem_hint = va_arg (*args, u32 *);
1917 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1918 desc_table[idx].flags, desc_table[idx].next,
1919 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1925 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1927 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1928 int callfd = UNIX_GET_FD (vq->callfd_idx);
1930 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1935 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1936 int show_descr, int show_verbose)
1942 vring_desc_t *desc_table;
1943 vhost_user_vring_t *vq = &vui->vrings[q];
1945 if (vq->avail && vq->used)
1946 vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1947 "used event idx %u used.idx %d\n", vq->avail->flags,
1948 vhost_user_avail_event_idx (vq), vq->avail->idx,
1949 vhost_user_used_event_idx (vq), vq->used->idx);
1951 vhost_user_show_fds (vm, vq);
1955 vlib_cli_output (vm, "\n descriptor table:\n");
1956 vlib_cli_output (vm,
1957 " slot addr len flags next "
1959 vlib_cli_output (vm,
1960 " ===== ================== ===== ====== ===== "
1961 "==================\n");
1962 for (j = 0; j < vq->qsz_mask + 1; j++)
1964 desc_table = vq->desc;
1965 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1966 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1967 desc_table, j, &mem_hint);
1968 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1970 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1971 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1974 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1977 (vm, "%U", format_vhost_user_desc,
1978 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1979 desc_table, idx, &mem_hint);
1981 if (n_entries >= 20)
1982 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1991 format_vhost_user_packed_desc (u8 * s, va_list * args)
1993 char *fmt = va_arg (*args, char *);
1994 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1995 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1996 int idx = va_arg (*args, int);
1997 u32 *mem_hint = va_arg (*args, u32 *);
1999 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2000 desc_table[idx].flags, desc_table[idx].id,
2001 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2007 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
2009 u32 flags = va_arg (*args, u32);
2015 static event_idx_flags event_idx_array[] = {
2016 #define _(s,v) { .str = #s, .value = v, },
2017 foreach_virtio_event_idx_flags
2020 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2022 if (flags < num_entries)
2023 s = format (s, "%s", event_idx_array[flags].str);
2025 s = format (s, "%u", flags);
2030 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2031 int show_descr, int show_verbose)
2037 vring_packed_desc_t *desc_table;
2038 vhost_user_vring_t *vq = &vui->vrings[q];
2039 u16 off_wrap, event_idx;
2041 off_wrap = vq->avail_event->off_wrap;
2042 event_idx = off_wrap & 0x7fff;
2043 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2044 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2045 (u32) vq->avail_event->flags, off_wrap, event_idx);
2047 off_wrap = vq->used_event->off_wrap;
2048 event_idx = off_wrap & 0x7fff;
2049 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2050 "used event idx %u\n", format_vhost_user_event_idx_flags,
2051 (u32) vq->used_event->flags, off_wrap, event_idx);
2053 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2054 vq->avail_wrap_counter, vq->used_wrap_counter);
2056 vhost_user_show_fds (vm, vq);
2060 vlib_cli_output (vm, "\n descriptor table:\n");
2061 vlib_cli_output (vm,
2062 " slot addr len flags id "
2064 vlib_cli_output (vm,
2065 " ===== ================== ===== ====== ===== "
2066 "==================\n");
2067 for (j = 0; j < vq->qsz_mask + 1; j++)
2069 desc_table = vq->packed_desc;
2070 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2071 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2072 desc_table, j, &mem_hint);
2073 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2075 n_entries = desc_table[j].len >> 4;
2076 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2079 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2082 (vm, "%U", format_vhost_user_packed_desc,
2083 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2084 desc_table, idx, &mem_hint);
2086 if (n_entries >= 20)
2087 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2096 show_vhost_user_command_fn (vlib_main_t * vm,
2097 unformat_input_t * input,
2098 vlib_cli_command_t * cmd)
2100 clib_error_t *error = 0;
2101 vnet_main_t *vnm = vnet_get_main ();
2102 vhost_user_main_t *vum = &vhost_user_main;
2103 vhost_user_intf_t *vui;
2104 u32 hw_if_index, *hw_if_indices = 0;
2105 vnet_hw_interface_t *hi;
2110 int show_verbose = 0;
2116 struct feat_struct *feat_entry;
2118 static struct feat_struct feat_array[] = {
2119 #define _(s,b) { .str = #s, .bit = b, },
2120 foreach_virtio_net_features
2125 #define foreach_protocol_feature \
2126 _(VHOST_USER_PROTOCOL_F_MQ) \
2127 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2129 static struct feat_struct proto_feat_array[] = {
2130 #define _(s) { .str = #s, .bit = s},
2131 foreach_protocol_feature
2136 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2139 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2141 hi = vnet_get_hw_interface (vnm, hw_if_index);
2142 if (vhost_user_device_class.index != hi->dev_class_index)
2144 error = clib_error_return (0, "unknown input `%U'",
2145 format_unformat_error, input);
2148 vec_add1 (hw_if_indices, hw_if_index);
2150 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2152 else if (unformat (input, "verbose"))
2156 error = clib_error_return (0, "unknown input `%U'",
2157 format_unformat_error, input);
2161 if (vec_len (hw_if_indices) == 0)
2163 pool_foreach (vui, vum->vhost_user_interfaces)
2164 vec_add1 (hw_if_indices, vui->hw_if_index);
2166 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2167 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2168 vum->coalesce_frames, vum->coalesce_time);
2169 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2171 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2173 for (i = 0; i < vec_len (hw_if_indices); i++)
2175 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2176 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2177 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2178 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2180 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2181 if (vui->enable_gso)
2182 vlib_cli_output (vm, " GSO enable");
2183 if (vui->enable_packed)
2184 vlib_cli_output (vm, " Packed ring enable");
2185 if (vui->enable_event_idx)
2186 vlib_cli_output (vm, " Event index enable");
2188 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2189 " features mask (0x%llx): \n"
2190 " features (0x%llx): \n",
2191 vui->virtio_net_hdr_sz, vui->feature_mask,
2194 feat_entry = (struct feat_struct *) &feat_array;
2195 while (feat_entry->str)
2197 if (vui->features & (1ULL << feat_entry->bit))
2198 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2203 vlib_cli_output (vm, " protocol features (0x%llx)",
2204 vui->protocol_features);
2205 feat_entry = (struct feat_struct *) &proto_feat_array;
2206 while (feat_entry->str)
2208 if (vui->protocol_features & (1ULL << feat_entry->bit))
2209 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2214 vlib_cli_output (vm, "\n");
2216 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2218 (vui->unix_server_index != ~0) ? "server" : "client",
2219 strerror (vui->sock_errno));
2221 vlib_cli_output (vm, " rx placement: ");
2223 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2225 vnet_main_t *vnm = vnet_get_main ();
2227 vnet_hw_if_rx_mode mode;
2228 vhost_user_vring_t *txvq = &vui->vrings[qid];
2230 if (txvq->qid == -1)
2233 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
2235 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
2237 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2239 format_vnet_hw_if_rx_mode, mode);
2242 vlib_cli_output (vm, " tx placement: %s\n",
2243 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2245 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2247 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2248 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2251 vlib_cli_output (vm, "\n");
2253 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2257 vlib_cli_output (vm,
2258 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2259 vlib_cli_output (vm,
2260 " ====== ===== ================== ================== ================== ================== ==================\n");
2262 for (j = 0; j < vui->nregions; j++)
2264 vlib_cli_output (vm,
2265 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2266 j, vui->region_mmap_fd[j],
2267 vui->regions[j].guest_phys_addr,
2268 vui->regions[j].memory_size,
2269 vui->regions[j].userspace_addr,
2270 vui->regions[j].mmap_offset,
2271 pointer_to_uword (vui->region_mmap_addr[j]));
2273 for (q = 0; q < vui->num_qid; q++)
2275 if (!vui->vrings[q].started)
2278 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2279 (q & 1) ? "RX" : "TX",
2280 vui->vrings[q].enabled ? "" : " disabled");
2282 vlib_cli_output (vm,
2283 " qsz %d last_avail_idx %d last_used_idx %d"
2285 vui->vrings[q].qsz_mask + 1,
2286 vui->vrings[q].last_avail_idx,
2287 vui->vrings[q].last_used_idx,
2288 vui->vrings[q].last_kick);
2290 if (vhost_user_is_packed_ring_supported (vui))
2291 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2294 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2296 vlib_cli_output (vm, "\n");
2299 vec_free (hw_if_indices);
2308 * Create a vHost User interface. Once created, a new virtual interface
2309 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2310 * is the next free index.
2312 * There are several parameters associated with a vHost interface:
2314 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2315 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2316 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2317 * hypervisor will create the socket if it does not already exist. The VPP code
2318 * is indifferent to the file location. However, if SELinux is enabled, then the
2319 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2321 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2322 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2323 * mode, the VM can be reset without tearing down the vHost Interface. In
2324 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2325 * tearing down the vHost Interface.
2327 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2328 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2329 * parameter not be used except by experienced users. By default, all supported
2330 * features will be advertised. Otherwise, provide the set of features desired.
2331 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2332 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2333 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2334 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2335 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2336 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2337 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2338 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2339 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2341 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2342 * X:X:X:X:X:X unix or X.X.X cisco format.
2344 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2345 * in the name to be specified. If instance already exists, name will be used
2346 * anyway and multiple instances will have the same name. Use with caution.
2349 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2350 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2351 * VirtualEthernet0/0/0
2353 * Example of how to create a vhost interface with VPP as the server and with just
2354 * multiple queues enabled:
2355 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2356 * VirtualEthernet0/0/1
2358 * Once the vHost interface is created, enable the interface using:
2359 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2362 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2363 .path = "create vhost-user",
2364 .short_help = "create vhost-user socket <socket-filename> [server] "
2365 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2366 "[packed] [event-idx]",
2367 .function = vhost_user_connect_command_fn,
2373 * Delete a vHost User interface using the interface name or the
2374 * software interface index. Use the '<em>show interface</em>'
2375 * command to determine the software interface index. On deletion,
2376 * the linux socket will not be deleted.
2379 * Example of how to delete a vhost interface by name:
2380 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2381 * Example of how to delete a vhost interface by software interface index:
2382 * @cliexcmd{delete vhost-user sw_if_index 1}
2385 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2386 .path = "delete vhost-user",
2387 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2388 .function = vhost_user_delete_command_fn,
2392 * Display the attributes of a single vHost User interface (provide interface
2393 * name), multiple vHost User interfaces (provide a list of interface names seperated
2394 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2395 * vHost interfaces).
2399 * Example of how to display a vhost interface:
2400 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2401 * Virtio vhost-user interfaces
2403 * coalesce frames 32 time 1e-3
2404 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2405 * virtio_net_hdr_sz 12
2406 * features mask (0xffffffffffffffff):
2407 * features (0x50408000):
2408 * VIRTIO_NET_F_MRG_RXBUF (15)
2409 * VIRTIO_NET_F_MQ (22)
2410 * VIRTIO_F_INDIRECT_DESC (28)
2411 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2412 * protocol features (0x3)
2413 * VHOST_USER_PROTOCOL_F_MQ (0)
2414 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2416 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2419 * thread 1 on vring 1
2420 * thread 1 on vring 5
2421 * thread 2 on vring 3
2422 * thread 2 on vring 7
2423 * tx placement: spin-lock
2424 * thread 0 on vring 0
2425 * thread 1 on vring 2
2426 * thread 2 on vring 0
2428 * Memory regions (total 2)
2429 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2430 * ====== ===== ================== ================== ================== ================== ==================
2431 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2432 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
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 62 callfd 64 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 65 callfd 66 errfd -1
2445 * qsz 256 last_avail_idx 0 last_used_idx 0
2446 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2447 * kickfd 63 callfd 70 errfd -1
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 72 callfd 74 errfd -1
2454 * Virtqueue 4 (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 76 callfd 78 errfd -1
2459 * Virtqueue 5 (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 80 callfd 82 errfd -1
2464 * Virtqueue 6 (TX disabled)
2465 * qsz 256 last_avail_idx 0 last_used_idx 0
2466 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2467 * kickfd 84 callfd 86 errfd -1
2469 * Virtqueue 7 (RX disabled)
2470 * qsz 256 last_avail_idx 0 last_used_idx 0
2471 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2472 * kickfd 88 callfd 90 errfd -1
2476 * The optional '<em>descriptors</em>' parameter will display the same output as
2477 * the previous example but will include the descriptor table for each queue.
2478 * The output is truncated below:
2479 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2480 * Virtio vhost-user interfaces
2482 * coalesce frames 32 time 1e-3
2483 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2484 * virtio_net_hdr_sz 12
2485 * features mask (0xffffffffffffffff):
2486 * features (0x50408000):
2487 * VIRTIO_NET_F_MRG_RXBUF (15)
2488 * VIRTIO_NET_F_MQ (22)
2491 * qsz 256 last_avail_idx 0 last_used_idx 0
2492 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2493 * kickfd 62 callfd 64 errfd -1
2496 * id addr len flags next user_addr
2497 * ===== ================== ===== ====== ===== ==================
2498 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2499 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2500 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2501 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2502 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2503 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2504 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2505 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2506 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2507 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2508 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2510 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2511 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2512 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2513 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2514 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2515 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2516 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2519 * qsz 256 last_avail_idx 0 last_used_idx 0
2525 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2526 .path = "show vhost-user",
2527 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2528 "[[descriptors] [verbose]]",
2529 .function = show_vhost_user_command_fn,
2534 static clib_error_t *
2535 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2537 vhost_user_main_t *vum = &vhost_user_main;
2539 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2541 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2543 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2545 else if (unformat (input, "dont-dump-memory"))
2546 vum->dont_dump_vhost_user_memory = 1;
2548 return clib_error_return (0, "unknown input `%U'",
2549 format_unformat_error, input);
2555 /* vhost-user { ... } configuration. */
2556 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2559 vhost_user_unmap_all (void)
2561 vhost_user_main_t *vum = &vhost_user_main;
2562 vhost_user_intf_t *vui;
2564 if (vum->dont_dump_vhost_user_memory)
2566 pool_foreach (vui, vum->vhost_user_interfaces)
2567 unmap_all_mem_regions (vui);
2572 * fd.io coding-style-patch-verification: ON
2575 * eval: (c-set-style "gnu")