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;
471 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
472 if (vui->enable_packed)
473 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
475 msg.size = sizeof (msg.u64);
476 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
477 "0x%016llx", vui->hw_if_index, msg.u64);
479 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
480 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
482 vu_log_debug (vui, "could not send message response");
487 case VHOST_USER_SET_FEATURES:
488 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
489 "0x%016llx", vui->hw_if_index, msg.u64);
491 vui->features = msg.u64;
494 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
495 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
496 vui->virtio_net_hdr_sz = 12;
498 vui->virtio_net_hdr_sz = 10;
501 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
503 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
504 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
505 if (vui->enable_gso &&
506 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
507 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
509 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
510 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
512 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
513 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
514 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
516 vhost_user_update_iface_state (vui);
519 case VHOST_USER_SET_MEM_TABLE:
520 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
521 vui->hw_if_index, msg.memory.nregions);
523 if ((msg.memory.nregions < 1) ||
524 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
526 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
527 VHOST_MEMORY_MAX_NREGIONS);
531 if (msg.memory.nregions != number_of_fds)
533 vu_log_debug (vui, "each memory region must have FD");
537 /* Do the mmap without barrier sync */
538 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
539 for (i = 0; i < msg.memory.nregions; i++)
541 long page_sz = get_huge_page_size (fds[i]);
543 /* align size to page */
544 ssize_t map_sz = (msg.memory.regions[i].memory_size +
545 msg.memory.regions[i].mmap_offset +
546 page_sz - 1) & ~(page_sz - 1);
548 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
549 MAP_SHARED, fds[i], 0);
550 if (region_mmap_addr[i] == MAP_FAILED)
552 vu_log_err (vui, "failed to map memory. errno is %d", errno);
553 for (j = 0; j < i; j++)
554 munmap (region_mmap_addr[j], map_sz);
557 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
558 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
559 region_mmap_addr[i], page_sz);
562 vlib_worker_thread_barrier_sync (vm);
563 unmap_all_mem_regions (vui);
564 for (i = 0; i < msg.memory.nregions; i++)
566 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
567 sizeof (vhost_user_memory_region_t));
569 vui->region_mmap_addr[i] = region_mmap_addr[i];
570 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
571 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
572 vui->regions[i].memory_size;
574 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
575 vui->region_mmap_fd[i] = fds[i];
581 * Re-compute desc, used, and avail descriptor table if vring address
584 for (q = 0; q < vui->num_qid; q++)
586 if (vui->vrings[q].desc_user_addr &&
587 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
589 vui->vrings[q].desc =
590 map_user_mem (vui, vui->vrings[q].desc_user_addr);
591 vui->vrings[q].used =
592 map_user_mem (vui, vui->vrings[q].used_user_addr);
593 vui->vrings[q].avail =
594 map_user_mem (vui, vui->vrings[q].avail_user_addr);
597 vlib_worker_thread_barrier_release (vm);
600 case VHOST_USER_SET_VRING_NUM:
601 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
602 vui->hw_if_index, msg.state.index, msg.state.num);
604 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
605 (msg.state.num == 0) || /* it cannot be zero */
606 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
607 (msg.state.index >= vui->num_qid))
609 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
610 " %d, msg.state.index %d, curruent max q %d",
611 msg.state.num, msg.state.index, vui->num_qid);
614 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
617 case VHOST_USER_SET_VRING_ADDR:
618 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
619 vui->hw_if_index, msg.state.index);
621 if (msg.state.index >= vui->num_qid)
623 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
624 " %u >= %u", msg.state.index, vui->num_qid);
628 if (msg.size < sizeof (msg.addr))
630 vu_log_debug (vui, "vhost message is too short (%d < %d)",
631 msg.size, sizeof (msg.addr));
635 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
636 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
637 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
639 if ((desc == NULL) || (used == NULL) || (avail == NULL))
641 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
646 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
647 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
648 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
650 vlib_worker_thread_barrier_sync (vm);
651 vui->vrings[msg.state.index].desc = desc;
652 vui->vrings[msg.state.index].used = used;
653 vui->vrings[msg.state.index].avail = avail;
655 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
656 vui->vrings[msg.state.index].log_used =
657 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
659 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
660 the ring is initialized in an enabled state. */
661 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
662 vui->vrings[msg.state.index].enabled = 1;
664 vui->vrings[msg.state.index].last_used_idx =
665 vui->vrings[msg.state.index].last_avail_idx =
666 vui->vrings[msg.state.index].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 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
752 vui->vrings[q].callfd_idx = ~0;
755 case VHOST_USER_SET_VRING_KICK:
756 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
757 vui->hw_if_index, msg.u64);
759 q = (u8) (msg.u64 & 0xFF);
760 if (q >= vui->num_qid)
762 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
763 " %u >= %u", q, vui->num_qid);
767 if (vui->vrings[q].kickfd_idx != ~0)
769 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
770 vui->vrings[q].kickfd_idx);
771 clib_file_del (&file_main, uf);
772 vui->vrings[q].kickfd_idx = ~0;
775 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
777 if (number_of_fds != 1)
779 vu_log_debug (vui, "More than one fd received !");
783 template.read_function = vhost_user_kickfd_read_ready;
784 template.file_descriptor = fds[0];
785 template.private_data =
786 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
788 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
792 //When no kickfd is set, the queue is initialized as started
793 vui->vrings[q].kickfd_idx = ~0;
794 vui->vrings[q].started = 1;
795 vhost_user_thread_placement (vui, q);
797 vhost_user_update_iface_state (vui);
800 case VHOST_USER_SET_VRING_ERR:
801 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
802 vui->hw_if_index, msg.u64);
804 q = (u8) (msg.u64 & 0xFF);
805 if (q >= vui->num_qid)
807 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
808 " %u >= %u", q, vui->num_qid);
812 if (vui->vrings[q].errfd != -1)
813 close (vui->vrings[q].errfd);
815 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
817 if (number_of_fds != 1)
820 vui->vrings[q].errfd = fds[0];
823 vui->vrings[q].errfd = -1;
826 case VHOST_USER_SET_VRING_BASE:
828 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
829 vui->hw_if_index, msg.state.index, msg.state.num);
830 if (msg.state.index >= vui->num_qid)
832 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
833 " %u >= %u", msg.state.index, vui->num_qid);
836 vlib_worker_thread_barrier_sync (vm);
837 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
838 if (vhost_user_is_packed_ring_supported (vui))
842 * 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
843 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
844 * | last avail idx | | last used idx | |
845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
848 * avail wrap counter used wrap counter
850 /* last avail idx at bit 0-14. */
851 vui->vrings[msg.state.index].last_avail_idx =
852 msg.state.num & 0x7fff;
853 /* avail wrap counter at bit 15 */
854 vui->vrings[msg.state.index].avail_wrap_counter =
855 ! !(msg.state.num & (1 << 15));
858 * Although last_used_idx is passed in the upper 16 bits in qemu
859 * implementation, in practice, last_avail_idx and last_used_idx are
860 * usually the same. As a result, DPDK does not bother to pass us
861 * last_used_idx. The spec is not clear on thex coding. I figured it
862 * out by reading the qemu code. So let's just read last_avail_idx
863 * and set last_used_idx equals to last_avail_idx.
865 vui->vrings[msg.state.index].last_used_idx =
866 vui->vrings[msg.state.index].last_avail_idx;
867 vui->vrings[msg.state.index].used_wrap_counter =
868 vui->vrings[msg.state.index].avail_wrap_counter;
870 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
871 vui->vrings[msg.state.index].avail_wrap_counter =
874 vlib_worker_thread_barrier_release (vm);
877 case VHOST_USER_GET_VRING_BASE:
878 if (msg.state.index >= vui->num_qid)
880 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
881 " %u >= %u", msg.state.index, vui->num_qid);
885 /* protection is needed to prevent rx/tx from changing last_avail_idx */
886 vlib_worker_thread_barrier_sync (vm);
888 * Copy last_avail_idx from the vring before closing it because
889 * closing the vring also initializes the vring last_avail_idx
891 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
892 if (vhost_user_is_packed_ring_supported (vui))
895 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
896 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
898 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
899 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
902 msg.size = sizeof (msg.state);
905 * Spec says: Client must [...] stop ring upon receiving
906 * VHOST_USER_GET_VRING_BASE
908 vhost_user_vring_close (vui, msg.state.index);
909 vlib_worker_thread_barrier_release (vm);
911 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
912 vui->hw_if_index, msg.state.index, msg.state.num);
914 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
915 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
917 vu_log_debug (vui, "could not send message response");
920 vhost_user_update_iface_state (vui);
923 case VHOST_USER_NONE:
924 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
927 case VHOST_USER_SET_LOG_BASE:
928 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
931 if (msg.size != sizeof (msg.log))
933 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
934 " %d instead of %d", msg.size, sizeof (msg.log));
938 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
940 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
941 "VHOST_USER_SET_LOG_BASE received");
946 /* align size to page */
947 long page_sz = get_huge_page_size (fd);
949 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
951 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
954 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
955 "mapped 0x%lx", map_sz, msg.log.offset, fd,
958 if (log_base_addr == MAP_FAILED)
960 vu_log_err (vui, "failed to map memory. errno is %d", errno);
964 vlib_worker_thread_barrier_sync (vm);
965 vui->log_base_addr = log_base_addr;
966 vui->log_base_addr += msg.log.offset;
967 vui->log_size = msg.log.size;
968 vlib_worker_thread_barrier_release (vm);
971 msg.size = sizeof (msg.u64);
973 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
974 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
976 vu_log_debug (vui, "could not send message response");
981 case VHOST_USER_SET_LOG_FD:
982 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
985 case VHOST_USER_GET_PROTOCOL_FEATURES:
987 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
988 (1 << VHOST_USER_PROTOCOL_F_MQ);
989 msg.size = sizeof (msg.u64);
990 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
991 "reply 0x%016llx", vui->hw_if_index, msg.u64);
993 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
994 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
996 vu_log_debug (vui, "could not send message response");
1001 case VHOST_USER_SET_PROTOCOL_FEATURES:
1002 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1003 "features 0x%016llx", vui->hw_if_index, msg.u64);
1004 vui->protocol_features = msg.u64;
1007 case VHOST_USER_GET_QUEUE_NUM:
1009 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1010 msg.size = sizeof (msg.u64);
1011 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1012 vui->hw_if_index, msg.u64);
1014 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1015 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1017 vu_log_debug (vui, "could not send message response");
1022 case VHOST_USER_SET_VRING_ENABLE:
1023 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1024 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1026 if (msg.state.index >= vui->num_qid)
1028 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1029 " %u >= %u", msg.state.index, vui->num_qid);
1033 vui->vrings[msg.state.index].enabled = msg.state.num;
1034 vhost_user_thread_placement (vui, msg.state.index);
1035 vhost_user_update_iface_state (vui);
1039 vu_log_debug (vui, "unknown vhost-user message %d received. "
1040 "closing socket", msg.request);
1047 vlib_worker_thread_barrier_sync (vm);
1048 vhost_user_if_disconnect (vui);
1049 vlib_worker_thread_barrier_release (vm);
1050 vhost_user_update_iface_state (vui);
1054 static clib_error_t *
1055 vhost_user_socket_error (clib_file_t * uf)
1057 vlib_main_t *vm = vlib_get_main ();
1058 vhost_user_main_t *vum = &vhost_user_main;
1059 vhost_user_intf_t *vui =
1060 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1062 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1063 vlib_worker_thread_barrier_sync (vm);
1064 vhost_user_if_disconnect (vui);
1065 vlib_worker_thread_barrier_release (vm);
1069 static clib_error_t *
1070 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1072 int client_fd, client_len;
1073 struct sockaddr_un client;
1074 clib_file_t template = { 0 };
1075 vhost_user_main_t *vum = &vhost_user_main;
1076 vhost_user_intf_t *vui;
1078 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1080 client_len = sizeof (client);
1081 client_fd = accept (uf->file_descriptor,
1082 (struct sockaddr *) &client,
1083 (socklen_t *) & client_len);
1086 return clib_error_return_unix (0, "accept");
1088 if (vui->clib_file_index != ~0)
1090 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1091 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1092 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1095 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1096 vui->sw_if_index, client_fd);
1097 template.read_function = vhost_user_socket_read;
1098 template.error_function = vhost_user_socket_error;
1099 template.file_descriptor = client_fd;
1100 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1101 vui->clib_file_index = clib_file_add (&file_main, &template);
1106 static clib_error_t *
1107 vhost_user_init (vlib_main_t * vm)
1109 vhost_user_main_t *vum = &vhost_user_main;
1110 vlib_thread_main_t *tm = vlib_get_thread_main ();
1112 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1114 vum->coalesce_frames = 32;
1115 vum->coalesce_time = 1e-3;
1117 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1120 vec_foreach (cpu, vum->cpus)
1122 /* This is actually not necessary as validate already zeroes it
1123 * Just keeping the loop here for later because I am lazy. */
1124 cpu->rx_buffers_len = 0;
1127 vum->random = random_default_seed ();
1129 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1135 VLIB_INIT_FUNCTION (vhost_user_init) =
1137 .runs_after = VLIB_INITS("ip4_init"),
1142 vhost_user_send_interrupt_process (vlib_main_t * vm,
1143 vlib_node_runtime_t * rt, vlib_frame_t * f)
1145 vhost_user_intf_t *vui;
1146 f64 timeout = 3153600000.0 /* 100 years */ ;
1147 uword event_type, *event_data = 0;
1148 vhost_user_main_t *vum = &vhost_user_main;
1150 f64 now, poll_time_remaining;
1156 poll_time_remaining =
1157 vlib_process_wait_for_event_or_clock (vm, timeout);
1158 event_type = vlib_process_get_events (vm, &event_data);
1159 vec_reset_length (event_data);
1162 * Use the remaining timeout if it is less than coalesce time to avoid
1163 * resetting the existing timer in the middle of expiration
1165 timeout = poll_time_remaining;
1166 if (vlib_process_suspend_time_is_zero (timeout) ||
1167 (timeout > vum->coalesce_time))
1168 timeout = vum->coalesce_time;
1170 now = vlib_time_now (vm);
1173 case VHOST_USER_EVENT_STOP_TIMER:
1177 case VHOST_USER_EVENT_START_TIMER:
1179 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1185 pool_foreach (vui, vum->vhost_user_interfaces) {
1186 next_timeout = timeout;
1187 for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1189 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1190 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1192 if (txvq->qid == -1)
1194 if (txvq->n_since_last_int)
1196 if (now >= txvq->int_deadline)
1197 vhost_user_send_call (vm, txvq);
1199 next_timeout = txvq->int_deadline - now;
1202 if (rxvq->n_since_last_int)
1204 if (now >= rxvq->int_deadline)
1205 vhost_user_send_call (vm, rxvq);
1207 next_timeout = rxvq->int_deadline - now;
1210 if ((next_timeout < timeout) && (next_timeout > 0.0))
1211 timeout = next_timeout;
1218 clib_warning ("BUG: unhandled event type %d", event_type);
1221 /* No less than 1 millisecond */
1225 timeout = 3153600000.0;
1231 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1232 .function = vhost_user_send_interrupt_process,
1233 .type = VLIB_NODE_TYPE_PROCESS,
1234 .name = "vhost-user-send-interrupt-process",
1239 vhost_user_process (vlib_main_t * vm,
1240 vlib_node_runtime_t * rt, vlib_frame_t * f)
1242 vhost_user_main_t *vum = &vhost_user_main;
1243 vhost_user_intf_t *vui;
1244 struct sockaddr_un sun;
1246 clib_file_t template = { 0 };
1247 f64 timeout = 3153600000.0 /* 100 years */ ;
1248 uword *event_data = 0;
1251 sun.sun_family = AF_UNIX;
1252 template.read_function = vhost_user_socket_read;
1253 template.error_function = vhost_user_socket_error;
1257 vlib_process_wait_for_event_or_clock (vm, timeout);
1258 vlib_process_get_events (vm, &event_data);
1259 vec_reset_length (event_data);
1264 pool_foreach (vui, vum->vhost_user_interfaces) {
1266 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1267 if (vui->clib_file_index == ~0)
1270 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1273 * 1st time error or new error for this interface,
1274 * spit out the message and record the error
1276 if (!vui->sock_errno || (vui->sock_errno != errno))
1279 ("Error: Could not open unix socket for %s",
1280 vui->sock_filename);
1281 vui->sock_errno = errno;
1286 /* try to connect */
1287 strncpy (sun.sun_path, (char *) vui->sock_filename,
1288 sizeof (sun.sun_path) - 1);
1289 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1291 /* Avoid hanging VPP if the other end does not accept */
1292 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1293 clib_unix_warning ("fcntl");
1295 if (connect (sockfd, (struct sockaddr *) &sun,
1296 sizeof (struct sockaddr_un)) == 0)
1298 /* Set the socket to blocking as it was before */
1299 if (fcntl(sockfd, F_SETFL, 0) < 0)
1300 clib_unix_warning ("fcntl2");
1302 vui->sock_errno = 0;
1303 template.file_descriptor = sockfd;
1304 template.private_data =
1305 vui - vhost_user_main.vhost_user_interfaces;
1306 vui->clib_file_index = clib_file_add (&file_main, &template);
1309 /* This sockfd is considered consumed */
1314 vui->sock_errno = errno;
1319 /* check if socket is alive */
1321 socklen_t len = sizeof (error);
1322 int fd = UNIX_GET_FD(vui->clib_file_index);
1324 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1328 vu_log_debug (vui, "getsockopt returned %d", retval);
1329 vhost_user_if_disconnect (vui);
1340 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1341 .function = vhost_user_process,
1342 .type = VLIB_NODE_TYPE_PROCESS,
1343 .name = "vhost-user-process",
1348 * Disables and reset interface structure.
1349 * It can then be either init again, or removed from used interfaces.
1352 vhost_user_term_if (vhost_user_intf_t * vui)
1355 vhost_user_main_t *vum = &vhost_user_main;
1357 // disconnect interface sockets
1358 vhost_user_if_disconnect (vui);
1359 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1360 vhost_user_update_iface_state (vui);
1362 for (q = 0; q < vui->num_qid; q++)
1364 // Remove existing queue mapping for the interface
1368 vnet_main_t *vnm = vnet_get_main ();
1369 vhost_user_vring_t *txvq = &vui->vrings[q];
1371 if (txvq->qid != -1)
1373 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1377 vu_log_warn (vui, "unable to unassign interface %d, "
1378 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1382 clib_spinlock_free (&vui->vrings[q].vring_lock);
1385 if (vui->unix_server_index != ~0)
1387 //Close server socket
1388 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1389 vui->unix_server_index);
1390 clib_file_del (&file_main, uf);
1391 vui->unix_server_index = ~0;
1392 unlink (vui->sock_filename);
1395 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1400 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1402 vhost_user_main_t *vum = &vhost_user_main;
1403 vhost_user_intf_t *vui;
1405 vnet_hw_interface_t *hwif;
1410 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1411 || hwif->dev_class_index != vhost_user_device_class.index)
1412 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1414 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1416 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1417 hwif->name, hwif->dev_instance);
1419 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1421 vhost_user_vring_t *txvq = &vui->vrings[qid];
1423 if (txvq->qid == -1)
1425 if ((vum->ifq_count > 0) &&
1426 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1427 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1430 // Stop the timer if there is no more interrupt interface/queue
1431 if ((vum->ifq_count == 0) &&
1432 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1434 vlib_process_signal_event (vm,
1435 vhost_user_send_interrupt_node.index,
1436 VHOST_USER_EVENT_STOP_TIMER, 0);
1442 // Disable and reset interface
1443 vhost_user_term_if (vui);
1445 // Reset renumbered iface
1446 if (hwif->dev_instance <
1447 vec_len (vum->show_dev_instance_by_real_dev_instance))
1448 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1450 // Delete ethernet interface
1451 ethernet_delete_interface (vnm, vui->hw_if_index);
1454 vec_free (vui->vrings);
1457 pool_put (vum->vhost_user_interfaces, vui);
1462 static clib_error_t *
1463 vhost_user_exit (vlib_main_t * vm)
1465 vnet_main_t *vnm = vnet_get_main ();
1466 vhost_user_main_t *vum = &vhost_user_main;
1467 vhost_user_intf_t *vui;
1469 vlib_worker_thread_barrier_sync (vlib_get_main ());
1471 pool_foreach (vui, vum->vhost_user_interfaces) {
1472 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1475 vlib_worker_thread_barrier_release (vlib_get_main ());
1479 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1482 * Open server unix socket on specified sock_filename.
1485 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1488 struct sockaddr_un un = { };
1490 /* create listening socket */
1491 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1492 return VNET_API_ERROR_SYSCALL_ERROR_1;
1494 un.sun_family = AF_UNIX;
1495 strncpy ((char *) un.sun_path, (char *) sock_filename,
1496 sizeof (un.sun_path) - 1);
1498 /* remove if exists */
1499 unlink ((char *) sock_filename);
1501 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1503 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1507 if (listen (fd, 1) == -1)
1509 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1522 * Create ethernet interface for vhost user interface.
1525 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1526 vhost_user_intf_t * vui, u8 * hwaddress)
1528 vhost_user_main_t *vum = &vhost_user_main;
1530 clib_error_t *error;
1532 /* create hw and sw interface */
1535 clib_memcpy (hwaddr, hwaddress, 6);
1539 random_u32 (&vum->random);
1540 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1545 error = ethernet_register_interface
1547 vhost_user_device_class.index,
1548 vui - vum->vhost_user_interfaces /* device instance */ ,
1549 hwaddr /* ethernet address */ ,
1550 &vui->hw_if_index, 0 /* flag change */ );
1553 clib_error_report (error);
1557 * Initialize vui with specified attributes
1560 vhost_user_vui_init (vnet_main_t * vnm,
1561 vhost_user_intf_t * vui,
1563 const char *sock_filename,
1564 u64 feature_mask, u32 * sw_if_index, u8 enable_gso,
1567 vnet_sw_interface_t *sw;
1569 vhost_user_main_t *vum = &vhost_user_main;
1570 vnet_hw_interface_t *hw;
1572 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1573 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1574 if (server_sock_fd != -1)
1576 clib_file_t template = { 0 };
1577 template.read_function = vhost_user_socksvr_accept_ready;
1578 template.file_descriptor = server_sock_fd;
1579 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1580 vui->unix_server_index = clib_file_add (&file_main, &template);
1584 vui->unix_server_index = ~0;
1587 vui->sw_if_index = sw->sw_if_index;
1588 strncpy (vui->sock_filename, sock_filename,
1589 ARRAY_LEN (vui->sock_filename) - 1);
1590 vui->sock_errno = 0;
1592 vui->feature_mask = feature_mask;
1593 vui->clib_file_index = ~0;
1594 vui->log_base_addr = 0;
1595 vui->if_index = vui - vum->vhost_user_interfaces;
1596 vui->enable_gso = enable_gso;
1597 vui->enable_packed = enable_packed;
1599 * enable_gso takes precedence over configurable feature mask if there
1601 * if feature mask disables gso, but enable_gso is configured,
1602 * then gso is enable
1603 * if feature mask enables gso, but enable_gso is not configured,
1604 * then gso is enable
1606 * if gso is enable via feature mask, it must enable both host and guest
1607 * gso feature mask, we don't support one sided GSO or partial GSO.
1609 if ((vui->enable_gso == 0) &&
1610 ((feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) ==
1611 (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1612 vui->enable_gso = 1;
1613 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1614 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1617 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1618 CLIB_CACHE_LINE_BYTES);
1620 for (q = 0; q < vec_len (vui->vrings); q++)
1621 vhost_user_vring_init (vui, q);
1623 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1624 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1627 *sw_if_index = vui->sw_if_index;
1629 vec_validate (vui->per_cpu_tx_qid,
1630 vlib_get_thread_main ()->n_vlib_mains - 1);
1631 vhost_user_tx_thread_placement (vui);
1635 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1636 const char *sock_filename,
1640 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
1641 u8 enable_gso, u8 enable_packed)
1643 vhost_user_intf_t *vui = NULL;
1646 int server_sock_fd = -1;
1647 vhost_user_main_t *vum = &vhost_user_main;
1650 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1652 return VNET_API_ERROR_INVALID_ARGUMENT;
1655 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1660 vui = &vum->vhost_user_interfaces[*if_index];
1661 *sw_if_index = vui->sw_if_index;
1663 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1669 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1675 /* Protect the uninitialized vui from being dispatched by rx/tx */
1676 vlib_worker_thread_barrier_sync (vm);
1677 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1678 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1679 vlib_worker_thread_barrier_release (vm);
1681 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1682 feature_mask, &sw_if_idx, enable_gso, enable_packed);
1683 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1684 vhost_user_rx_thread_placement (vui, 1);
1687 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1690 *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 const char *sock_filename,
1703 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
1704 u8 enable_gso, u8 enable_packed)
1706 vhost_user_main_t *vum = &vhost_user_main;
1707 vhost_user_intf_t *vui = NULL;
1709 int server_sock_fd = -1;
1711 vnet_hw_interface_t *hwif;
1716 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1717 || hwif->dev_class_index != vhost_user_device_class.index)
1718 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1720 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1721 return VNET_API_ERROR_INVALID_ARGUMENT;
1723 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1726 * Disallow changing the interface to have the same path name
1727 * as other interface
1729 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1730 if (if_index && (*if_index != vui->if_index))
1731 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1733 // First try to open server socket
1735 if ((rv = vhost_user_init_server_sock (sock_filename,
1736 &server_sock_fd)) != 0)
1739 vhost_user_term_if (vui);
1740 vhost_user_vui_init (vnm, vui, server_sock_fd,
1741 sock_filename, feature_mask, &sw_if_idx, enable_gso,
1745 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1747 // Process node must connect
1748 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1754 vhost_user_connect_command_fn (vlib_main_t * vm,
1755 unformat_input_t * input,
1756 vlib_cli_command_t * cmd)
1758 unformat_input_t _line_input, *line_input = &_line_input;
1759 u8 *sock_filename = NULL;
1762 u64 feature_mask = (u64) ~ (0ULL);
1764 u32 custom_dev_instance = ~0;
1767 clib_error_t *error = NULL;
1768 u8 enable_gso = 0, enable_packed = 0;
1770 /* Get a line of input. */
1771 if (!unformat_user (input, unformat_line_input, line_input))
1774 /* GSO feature is disable by default */
1775 feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1776 /* packed-ring feature is disable by default */
1777 feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1778 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1780 if (unformat (line_input, "socket %s", &sock_filename))
1782 else if (unformat (line_input, "server"))
1784 else if (unformat (line_input, "gso"))
1786 else if (unformat (line_input, "packed"))
1788 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1792 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1794 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1800 error = clib_error_return (0, "unknown input `%U'",
1801 format_unformat_error, line_input);
1806 vnet_main_t *vnm = vnet_get_main ();
1809 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1810 is_server, &sw_if_index, feature_mask,
1811 renumber, custom_dev_instance, hw,
1812 enable_gso, enable_packed)))
1814 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1818 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1822 vec_free (sock_filename);
1823 unformat_free (line_input);
1829 vhost_user_delete_command_fn (vlib_main_t * vm,
1830 unformat_input_t * input,
1831 vlib_cli_command_t * cmd)
1833 unformat_input_t _line_input, *line_input = &_line_input;
1834 u32 sw_if_index = ~0;
1835 vnet_main_t *vnm = vnet_get_main ();
1836 clib_error_t *error = NULL;
1838 /* Get a line of input. */
1839 if (!unformat_user (input, unformat_line_input, line_input))
1842 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1844 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1847 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1850 vnet_hw_interface_t *hwif =
1851 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1853 vhost_user_device_class.index != hwif->dev_class_index)
1855 error = clib_error_return (0, "Not a vhost interface");
1861 error = clib_error_return (0, "unknown input `%U'",
1862 format_unformat_error, line_input);
1867 vhost_user_delete_if (vnm, vm, sw_if_index);
1870 unformat_free (line_input);
1876 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1877 vhost_user_intf_details_t ** out_vuids)
1880 vhost_user_main_t *vum = &vhost_user_main;
1881 vhost_user_intf_t *vui;
1882 vhost_user_intf_details_t *r_vuids = NULL;
1883 vhost_user_intf_details_t *vuid = NULL;
1884 u32 *hw_if_indices = 0;
1885 vnet_hw_interface_t *hi;
1891 pool_foreach (vui, vum->vhost_user_interfaces)
1892 vec_add1 (hw_if_indices, vui->hw_if_index);
1894 for (i = 0; i < vec_len (hw_if_indices); i++)
1896 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1897 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1899 vec_add2 (r_vuids, vuid, 1);
1900 vuid->sw_if_index = vui->sw_if_index;
1901 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1902 vuid->features = vui->features;
1903 vuid->num_regions = vui->nregions;
1904 vuid->is_server = vui->unix_server_index != ~0;
1905 vuid->sock_errno = vui->sock_errno;
1906 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1907 "%s", vui->sock_filename);
1908 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1909 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1910 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1913 vec_free (hw_if_indices);
1915 *out_vuids = r_vuids;
1921 format_vhost_user_desc (u8 * s, va_list * args)
1923 char *fmt = va_arg (*args, char *);
1924 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1925 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1926 int idx = va_arg (*args, int);
1927 u32 *mem_hint = va_arg (*args, u32 *);
1929 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1930 desc_table[idx].flags, desc_table[idx].next,
1931 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1937 format_vhost_user_vring (u8 * s, va_list * args)
1939 char *fmt = va_arg (*args, char *);
1940 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1941 int q = va_arg (*args, int);
1943 s = format (s, fmt, vui->vrings[q].avail->flags, vui->vrings[q].avail->idx,
1944 vui->vrings[q].used->flags, vui->vrings[q].used->idx);
1949 vhost_user_show_fds (vlib_main_t * vm, vhost_user_intf_t * vui, int q)
1951 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1952 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1954 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1955 vui->vrings[q].errfd);
1959 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1960 int show_descr, int show_verbose)
1966 vring_desc_t *desc_table;
1968 if (vui->vrings[q].avail && vui->vrings[q].used)
1969 vlib_cli_output (vm, "%U", format_vhost_user_vring,
1970 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1973 vhost_user_show_fds (vm, vui, q);
1977 vlib_cli_output (vm, "\n descriptor table:\n");
1978 vlib_cli_output (vm,
1979 " slot addr len flags next "
1981 vlib_cli_output (vm,
1982 " ===== ================== ===== ====== ===== "
1983 "==================\n");
1984 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1986 desc_table = vui->vrings[q].desc;
1987 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1988 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1989 desc_table, j, &mem_hint);
1990 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1992 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1993 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1996 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1999 (vm, "%U", format_vhost_user_desc,
2000 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2001 desc_table, idx, &mem_hint);
2003 if (n_entries >= 20)
2004 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2013 format_vhost_user_packed_desc (u8 * s, va_list * args)
2015 char *fmt = va_arg (*args, char *);
2016 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2017 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
2018 int idx = va_arg (*args, int);
2019 u32 *mem_hint = va_arg (*args, u32 *);
2021 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2022 desc_table[idx].flags, desc_table[idx].id,
2023 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2029 format_vhost_user_vring_packed (u8 * s, va_list * args)
2031 char *fmt = va_arg (*args, char *);
2032 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2033 int q = va_arg (*args, int);
2035 s = format (s, fmt, vui->vrings[q].avail_event->flags,
2036 vui->vrings[q].avail_event->off_wrap,
2037 vui->vrings[q].used_event->flags,
2038 vui->vrings[q].used_event->off_wrap,
2039 vui->vrings[q].avail_wrap_counter,
2040 vui->vrings[q].used_wrap_counter);
2045 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2046 int show_descr, int show_verbose)
2052 vring_packed_desc_t *desc_table;
2054 if (vui->vrings[q].avail_event && vui->vrings[q].used_event)
2055 vlib_cli_output (vm, "%U", format_vhost_user_vring_packed,
2056 " avail_event.flags %x avail_event.off_wrap %u "
2057 "used_event.flags %x used_event.off_wrap %u\n"
2058 " avail wrap counter %u, used wrap counter %u\n",
2061 vhost_user_show_fds (vm, vui, q);
2065 vlib_cli_output (vm, "\n descriptor table:\n");
2066 vlib_cli_output (vm,
2067 " slot addr len flags id "
2069 vlib_cli_output (vm,
2070 " ===== ================== ===== ====== ===== "
2071 "==================\n");
2072 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
2074 desc_table = vui->vrings[q].packed_desc;
2075 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2076 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2077 desc_table, j, &mem_hint);
2078 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2080 n_entries = desc_table[j].len >> 4;
2081 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2084 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2087 (vm, "%U", format_vhost_user_packed_desc,
2088 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2089 desc_table, idx, &mem_hint);
2091 if (n_entries >= 20)
2092 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2101 show_vhost_user_command_fn (vlib_main_t * vm,
2102 unformat_input_t * input,
2103 vlib_cli_command_t * cmd)
2105 clib_error_t *error = 0;
2106 vnet_main_t *vnm = vnet_get_main ();
2107 vhost_user_main_t *vum = &vhost_user_main;
2108 vhost_user_intf_t *vui;
2109 u32 hw_if_index, *hw_if_indices = 0;
2110 vnet_hw_interface_t *hi;
2115 int show_verbose = 0;
2121 struct feat_struct *feat_entry;
2123 static struct feat_struct feat_array[] = {
2124 #define _(s,b) { .str = #s, .bit = b, },
2125 foreach_virtio_net_features
2130 #define foreach_protocol_feature \
2131 _(VHOST_USER_PROTOCOL_F_MQ) \
2132 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2134 static struct feat_struct proto_feat_array[] = {
2135 #define _(s) { .str = #s, .bit = s},
2136 foreach_protocol_feature
2141 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2144 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2146 hi = vnet_get_hw_interface (vnm, hw_if_index);
2147 if (vhost_user_device_class.index != hi->dev_class_index)
2149 error = clib_error_return (0, "unknown input `%U'",
2150 format_unformat_error, input);
2153 vec_add1 (hw_if_indices, hw_if_index);
2155 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2157 else if (unformat (input, "verbose"))
2161 error = clib_error_return (0, "unknown input `%U'",
2162 format_unformat_error, input);
2166 if (vec_len (hw_if_indices) == 0)
2168 pool_foreach (vui, vum->vhost_user_interfaces)
2169 vec_add1 (hw_if_indices, vui->hw_if_index);
2171 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2172 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2173 vum->coalesce_frames, vum->coalesce_time);
2174 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2176 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2178 for (i = 0; i < vec_len (hw_if_indices); i++)
2180 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2181 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2182 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2183 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2185 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2186 if (vui->enable_gso)
2187 vlib_cli_output (vm, " GSO enable");
2188 if (vui->enable_packed)
2189 vlib_cli_output (vm, " Packed ring enable");
2191 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2192 " features mask (0x%llx): \n"
2193 " features (0x%llx): \n",
2194 vui->virtio_net_hdr_sz, vui->feature_mask,
2197 feat_entry = (struct feat_struct *) &feat_array;
2198 while (feat_entry->str)
2200 if (vui->features & (1ULL << feat_entry->bit))
2201 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2206 vlib_cli_output (vm, " protocol features (0x%llx)",
2207 vui->protocol_features);
2208 feat_entry = (struct feat_struct *) &proto_feat_array;
2209 while (feat_entry->str)
2211 if (vui->protocol_features & (1ULL << feat_entry->bit))
2212 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2217 vlib_cli_output (vm, "\n");
2219 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2221 (vui->unix_server_index != ~0) ? "server" : "client",
2222 strerror (vui->sock_errno));
2224 vlib_cli_output (vm, " rx placement: ");
2226 for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2228 vnet_main_t *vnm = vnet_get_main ();
2230 vnet_hw_if_rx_mode mode;
2231 vhost_user_vring_t *txvq = &vui->vrings[qid];
2233 if (txvq->qid == -1)
2236 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
2238 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
2240 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2242 format_vnet_hw_if_rx_mode, mode);
2245 vlib_cli_output (vm, " tx placement: %s\n",
2246 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2248 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2250 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2251 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2254 vlib_cli_output (vm, "\n");
2256 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2260 vlib_cli_output (vm,
2261 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2262 vlib_cli_output (vm,
2263 " ====== ===== ================== ================== ================== ================== ==================\n");
2265 for (j = 0; j < vui->nregions; j++)
2267 vlib_cli_output (vm,
2268 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2269 j, vui->region_mmap_fd[j],
2270 vui->regions[j].guest_phys_addr,
2271 vui->regions[j].memory_size,
2272 vui->regions[j].userspace_addr,
2273 vui->regions[j].mmap_offset,
2274 pointer_to_uword (vui->region_mmap_addr[j]));
2276 for (q = 0; q < vui->num_qid; q++)
2278 if (!vui->vrings[q].started)
2281 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2282 (q & 1) ? "RX" : "TX",
2283 vui->vrings[q].enabled ? "" : " disabled");
2285 vlib_cli_output (vm,
2286 " qsz %d last_avail_idx %d last_used_idx %d\n",
2287 vui->vrings[q].qsz_mask + 1,
2288 vui->vrings[q].last_avail_idx,
2289 vui->vrings[q].last_used_idx);
2291 if (vhost_user_is_packed_ring_supported (vui))
2292 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2295 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2297 vlib_cli_output (vm, "\n");
2300 vec_free (hw_if_indices);
2309 * Create a vHost User interface. Once created, a new virtual interface
2310 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2311 * is the next free index.
2313 * There are several parameters associated with a vHost interface:
2315 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2316 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2317 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2318 * hypervisor will create the socket if it does not already exist. The VPP code
2319 * is indifferent to the file location. However, if SELinux is enabled, then the
2320 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2322 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2323 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2324 * mode, the VM can be reset without tearing down the vHost Interface. In
2325 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2326 * tearing down the vHost Interface.
2328 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2329 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2330 * parameter not be used except by experienced users. By default, all supported
2331 * features will be advertised. Otherwise, provide the set of features desired.
2332 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2333 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2334 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2335 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2336 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2337 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2338 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2339 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2340 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2342 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2343 * X:X:X:X:X:X unix or X.X.X cisco format.
2345 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2346 * in the name to be specified. If instance already exists, name will be used
2347 * anyway and multiple instances will have the same name. Use with caution.
2350 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2351 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2352 * VirtualEthernet0/0/0
2354 * Example of how to create a vhost interface with VPP as the server and with just
2355 * multiple queues enabled:
2356 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2357 * VirtualEthernet0/0/1
2359 * Once the vHost interface is created, enable the interface using:
2360 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2363 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2364 .path = "create vhost-user",
2365 .short_help = "create vhost-user socket <socket-filename> [server] "
2366 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2368 .function = vhost_user_connect_command_fn,
2374 * Delete a vHost User interface using the interface name or the
2375 * software interface index. Use the '<em>show interface</em>'
2376 * command to determine the software interface index. On deletion,
2377 * the linux socket will not be deleted.
2380 * Example of how to delete a vhost interface by name:
2381 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2382 * Example of how to delete a vhost interface by software interface index:
2383 * @cliexcmd{delete vhost-user sw_if_index 1}
2386 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2387 .path = "delete vhost-user",
2388 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2389 .function = vhost_user_delete_command_fn,
2393 * Display the attributes of a single vHost User interface (provide interface
2394 * name), multiple vHost User interfaces (provide a list of interface names seperated
2395 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2396 * vHost interfaces).
2400 * Example of how to display a vhost interface:
2401 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2402 * Virtio vhost-user interfaces
2404 * coalesce frames 32 time 1e-3
2405 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2406 * virtio_net_hdr_sz 12
2407 * features mask (0xffffffffffffffff):
2408 * features (0x50408000):
2409 * VIRTIO_NET_F_MRG_RXBUF (15)
2410 * VIRTIO_NET_F_MQ (22)
2411 * VIRTIO_F_INDIRECT_DESC (28)
2412 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2413 * protocol features (0x3)
2414 * VHOST_USER_PROTOCOL_F_MQ (0)
2415 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2417 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2420 * thread 1 on vring 1
2421 * thread 1 on vring 5
2422 * thread 2 on vring 3
2423 * thread 2 on vring 7
2424 * tx placement: spin-lock
2425 * thread 0 on vring 0
2426 * thread 1 on vring 2
2427 * thread 2 on vring 0
2429 * Memory regions (total 2)
2430 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2431 * ====== ===== ================== ================== ================== ================== ==================
2432 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2433 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2436 * qsz 256 last_avail_idx 0 last_used_idx 0
2437 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2438 * kickfd 62 callfd 64 errfd -1
2441 * qsz 256 last_avail_idx 0 last_used_idx 0
2442 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2443 * kickfd 65 callfd 66 errfd -1
2446 * qsz 256 last_avail_idx 0 last_used_idx 0
2447 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2448 * kickfd 63 callfd 70 errfd -1
2451 * qsz 256 last_avail_idx 0 last_used_idx 0
2452 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2453 * kickfd 72 callfd 74 errfd -1
2455 * Virtqueue 4 (TX disabled)
2456 * qsz 256 last_avail_idx 0 last_used_idx 0
2457 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2458 * kickfd 76 callfd 78 errfd -1
2460 * Virtqueue 5 (RX disabled)
2461 * qsz 256 last_avail_idx 0 last_used_idx 0
2462 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2463 * kickfd 80 callfd 82 errfd -1
2465 * Virtqueue 6 (TX disabled)
2466 * qsz 256 last_avail_idx 0 last_used_idx 0
2467 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2468 * kickfd 84 callfd 86 errfd -1
2470 * Virtqueue 7 (RX disabled)
2471 * qsz 256 last_avail_idx 0 last_used_idx 0
2472 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2473 * kickfd 88 callfd 90 errfd -1
2477 * The optional '<em>descriptors</em>' parameter will display the same output as
2478 * the previous example but will include the descriptor table for each queue.
2479 * The output is truncated below:
2480 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2481 * Virtio vhost-user interfaces
2483 * coalesce frames 32 time 1e-3
2484 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2485 * virtio_net_hdr_sz 12
2486 * features mask (0xffffffffffffffff):
2487 * features (0x50408000):
2488 * VIRTIO_NET_F_MRG_RXBUF (15)
2489 * VIRTIO_NET_F_MQ (22)
2492 * qsz 256 last_avail_idx 0 last_used_idx 0
2493 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2494 * kickfd 62 callfd 64 errfd -1
2497 * id addr len flags next user_addr
2498 * ===== ================== ===== ====== ===== ==================
2499 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2500 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2501 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2502 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2503 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2504 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2505 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2506 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2507 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2508 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2509 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2511 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2512 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2513 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2514 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2515 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2516 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2517 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2520 * qsz 256 last_avail_idx 0 last_used_idx 0
2526 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2527 .path = "show vhost-user",
2528 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2529 "[[descriptors] [verbose]]",
2530 .function = show_vhost_user_command_fn,
2535 static clib_error_t *
2536 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2538 vhost_user_main_t *vum = &vhost_user_main;
2540 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2542 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2544 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2546 else if (unformat (input, "dont-dump-memory"))
2547 vum->dont_dump_vhost_user_memory = 1;
2549 return clib_error_return (0, "unknown input `%U'",
2550 format_unformat_error, input);
2556 /* vhost-user { ... } configuration. */
2557 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2560 vhost_user_unmap_all (void)
2562 vhost_user_main_t *vum = &vhost_user_main;
2563 vhost_user_intf_t *vui;
2565 if (vum->dont_dump_vhost_user_memory)
2567 pool_foreach (vui, vum->vhost_user_interfaces)
2568 unmap_all_mem_regions (vui);
2573 * fd.io coding-style-patch-verification: ON
2576 * eval: (c-set-style "gnu")