2 *------------------------------------------------------------------
5 * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 #include <vnet/interface/rx_queue_funcs.h>
40 #include <vnet/interface/tx_queue_funcs.h>
42 #include <vnet/devices/virtio/vhost_user.h>
43 #include <vnet/devices/virtio/vhost_user_inline.h>
47 * @brief vHost User Device Driver.
49 * This file contains the source code for vHost User interface.
53 vlib_node_registration_t vhost_user_send_interrupt_node;
56 vhost_user_main_t vhost_user_main = {
60 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
66 get_huge_page_size (int fd)
74 unmap_all_mem_regions (vhost_user_intf_t * vui)
77 vhost_user_vring_t *vq;
79 for (i = 0; i < vui->nregions; i++)
81 if (vui->region_mmap_addr[i] != MAP_FAILED)
84 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
86 ssize_t map_sz = (vui->regions[i].memory_size +
87 vui->regions[i].mmap_offset +
88 page_sz - 1) & ~(page_sz - 1);
91 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
94 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
95 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
98 vui->region_mmap_addr[i] = MAP_FAILED;
102 vu_log_err (vui, "failed to unmap memory region (errno %d)",
105 close (vui->region_mmap_fd[i]);
110 FOR_ALL_VHOST_RX_TXQ (q, vui)
112 vq = &vui->vrings[q];
119 static_always_inline void
120 vhost_user_tx_thread_placement (vhost_user_intf_t *vui, u32 qid)
122 vnet_main_t *vnm = vnet_get_main ();
123 vhost_user_vring_t *rxvq = &vui->vrings[qid];
124 u32 q = qid >> 1, rxvq_count;
126 ASSERT ((qid & 1) == 0);
127 if (!rxvq->started || !rxvq->enabled)
130 rxvq_count = (qid >> 1) + 1;
131 if (rxvq->queue_index == ~0)
134 vnet_hw_if_register_tx_queue (vnm, vui->hw_if_index, q);
138 FOR_ALL_VHOST_RXQ (q, vui)
140 vhost_user_vring_t *rxvq = &vui->vrings[q];
141 u32 qi = rxvq->queue_index;
143 if (rxvq->queue_index == ~0)
145 for (u32 i = 0; i < vlib_get_n_threads (); i++)
146 vnet_hw_if_tx_queue_unassign_thread (vnm, qi, i);
149 for (u32 i = 0; i < vlib_get_n_threads (); i++)
151 vhost_user_vring_t *rxvq =
152 &vui->vrings[VHOST_VRING_IDX_RX (i % rxvq_count)];
153 u32 qi = rxvq->queue_index;
155 vnet_hw_if_tx_queue_assign_thread (vnm, qi, i);
158 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
162 * @brief Unassign existing interface/queue to thread mappings and re-assign
163 * new interface/queue to thread mappings
165 static_always_inline void
166 vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
168 vhost_user_vring_t *txvq = &vui->vrings[qid];
169 vnet_main_t *vnm = vnet_get_main ();
172 vhost_user_main_t *vum = &vhost_user_main;
174 ASSERT ((qid & 1) == 1); // should be odd
175 // Assign new queue mappings for the interface
176 if (txvq->queue_index != ~0)
178 vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
179 vhost_user_input_node.index);
180 txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
181 VNET_HW_IF_RXQ_THREAD_ANY);
183 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
185 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
186 /* Set polling as the default */
187 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
188 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
190 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
191 /* Keep a polling queue count for each thread */
192 cpu->polling_q_count++;
195 rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
197 vu_log_warn (vui, "unable to set rx mode for interface %d, "
198 "queue %d: rc=%d", vui->hw_if_index, q, rv);
199 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
202 /** @brief Returns whether at least one TX and one RX vring are enabled */
203 static_always_inline int
204 vhost_user_intf_ready (vhost_user_intf_t * vui)
206 int i, found[2] = { }; //RX + TX
208 for (i = 0; i < vui->num_qid; i++)
209 if (vui->vrings[i].started && vui->vrings[i].enabled)
212 return found[0] && found[1];
215 static_always_inline void
216 vhost_user_update_iface_state (vhost_user_intf_t * vui)
218 /* if we have pointers to descriptor table, go up */
219 int is_ready = vhost_user_intf_ready (vui);
220 if (is_ready != vui->is_ready)
222 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
223 is_ready ? "ready" : "down");
225 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
226 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
228 vui->is_ready = is_ready;
232 static clib_error_t *
233 vhost_user_callfd_read_ready (clib_file_t * uf)
235 __attribute__ ((unused)) int n;
238 n = read (uf->file_descriptor, ((char *) &buff), 8);
243 static_always_inline void
244 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
246 if (qid & 1) // RX is odd, TX is even
248 if (vui->vrings[qid].queue_index == ~0)
249 vhost_user_rx_thread_placement (vui, qid);
252 vhost_user_tx_thread_placement (vui, qid);
255 static clib_error_t *
256 vhost_user_kickfd_read_ready (clib_file_t * uf)
258 __attribute__ ((unused)) ssize_t n;
260 vhost_user_main_t *vum = &vhost_user_main;
261 vhost_user_intf_t *vui =
262 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
263 u32 qid = uf->private_data & 0xff;
264 u32 is_txq = qid & 1;
265 vhost_user_vring_t *vq = &vui->vrings[qid];
266 vnet_main_t *vnm = vnet_get_main ();
268 n = read (uf->file_descriptor, buff, 8);
269 if (vq->started == 0)
272 vhost_user_thread_placement (vui, qid);
273 vhost_user_update_iface_state (vui);
275 vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
279 if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) &&
280 vhost_user_intf_ready (vui))
282 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index);
284 * If the thread has more than 1 queue and the other queue is in polling
285 * mode, there is no need to trigger an interrupt
287 if (cpu->polling_q_count == 0)
288 vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
294 static_always_inline void
295 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
297 vhost_user_vring_t *vring = &vui->vrings[qid];
299 clib_memset (vring, 0, sizeof (*vring));
300 vring->kickfd_idx = ~0;
301 vring->callfd_idx = ~0;
304 vring->queue_index = ~0;
305 vring->thread_index = ~0;
306 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
308 clib_spinlock_init (&vring->vring_lock);
311 * We have a bug with some qemu 2.5, and this may be a fix.
312 * Feel like interpretation holy text, but this is from vhost-user.txt.
314 * One queue pair is enabled initially. More queues are enabled
315 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
317 * Don't know who's right, but this is what DPDK does.
319 if (qid == 0 || qid == 1)
323 static_always_inline void
324 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
326 vhost_user_vring_t *vring = &vui->vrings[qid];
328 if (vring->kickfd_idx != ~0)
330 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
332 clib_file_del (&file_main, uf);
333 vring->kickfd_idx = ~0;
335 if (vring->callfd_idx != ~0)
337 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
339 clib_file_del (&file_main, uf);
340 vring->callfd_idx = ~0;
342 if (vring->errfd != -1)
344 close (vring->errfd);
348 clib_spinlock_free (&vring->vring_lock);
350 // save the needed information in vrings prior to being wiped out
351 u16 q = vui->vrings[qid].qid;
352 u32 queue_index = vui->vrings[qid].queue_index;
353 u32 mode = vui->vrings[qid].mode;
354 u32 thread_index = vui->vrings[qid].thread_index;
355 vhost_user_vring_init (vui, qid);
356 vui->vrings[qid].qid = q;
357 vui->vrings[qid].queue_index = queue_index;
358 vui->vrings[qid].mode = mode;
359 vui->vrings[qid].thread_index = thread_index;
362 static_always_inline void
363 vhost_user_if_disconnect (vhost_user_intf_t * vui)
365 vnet_main_t *vnm = vnet_get_main ();
368 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
370 if (vui->clib_file_index != ~0)
372 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
373 vui->clib_file_index = ~0;
378 FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); }
380 unmap_all_mem_regions (vui);
381 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
385 vhost_user_set_operation_mode (vhost_user_intf_t *vui,
386 vhost_user_vring_t *txvq)
388 if (vhost_user_is_packed_ring_supported (vui))
390 if (txvq->used_event)
392 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
393 txvq->used_event->flags = VRING_EVENT_F_DISABLE;
395 txvq->used_event->flags = 0;
402 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
403 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
405 txvq->used->flags = 0;
410 static clib_error_t *
411 vhost_user_socket_read (clib_file_t * uf)
414 int fd, number_of_fds = 0;
415 int fds[VHOST_MEMORY_MAX_NREGIONS];
416 vhost_user_msg_t msg;
419 vhost_user_main_t *vum = &vhost_user_main;
420 vhost_user_intf_t *vui;
421 struct cmsghdr *cmsg;
423 clib_file_t template = { 0 };
424 vnet_main_t *vnm = vnet_get_main ();
425 vlib_main_t *vm = vlib_get_main ();
427 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
429 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
431 clib_memset (&mh, 0, sizeof (mh));
432 clib_memset (control, 0, sizeof (control));
434 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
437 /* set the payload */
438 iov[0].iov_base = (void *) &msg;
439 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
443 mh.msg_control = control;
444 mh.msg_controllen = sizeof (control);
446 n = recvmsg (uf->file_descriptor, &mh, 0);
448 if (n != VHOST_USER_MSG_HDR_SZ)
452 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
457 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
458 n, VHOST_USER_MSG_HDR_SZ);
463 if (mh.msg_flags & MSG_CTRUNC)
465 vu_log_debug (vui, "MSG_CTRUNC is set");
469 cmsg = CMSG_FIRSTHDR (&mh);
471 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
472 (cmsg->cmsg_type == SCM_RIGHTS) &&
473 (cmsg->cmsg_len - CMSG_LEN (0) <=
474 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
476 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
477 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
480 /* version 1, no reply bit set */
481 if ((msg.flags & 7) != 1)
483 vu_log_debug (vui, "malformed message received. closing socket");
490 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
494 vu_log_debug (vui, "read failed %s", strerror (errno));
497 else if (rv != msg.size)
499 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
507 case VHOST_USER_GET_FEATURES:
509 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
510 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
511 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
512 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
513 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
514 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
515 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
516 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
517 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
518 msg.u64 &= vui->feature_mask;
520 if (vui->enable_event_idx)
521 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
523 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
524 if (vui->enable_packed)
525 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
527 msg.size = sizeof (msg.u64);
528 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
529 "0x%016llx", vui->hw_if_index, msg.u64);
531 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
532 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
534 vu_log_debug (vui, "could not send message response");
539 case VHOST_USER_SET_FEATURES:
540 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
541 "0x%016llx", vui->hw_if_index, msg.u64);
543 vui->features = msg.u64;
546 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
547 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
548 vui->virtio_net_hdr_sz = 12;
550 vui->virtio_net_hdr_sz = 10;
553 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
555 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
556 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
557 if (vui->enable_gso &&
558 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
559 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
561 hw->caps |= (VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
562 VNET_HW_IF_CAP_TX_UDP_CKSUM);
566 hw->caps &= ~(VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM);
568 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
570 vhost_user_update_iface_state (vui);
573 case VHOST_USER_SET_MEM_TABLE:
574 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
575 vui->hw_if_index, msg.memory.nregions);
577 if ((msg.memory.nregions < 1) ||
578 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
580 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
581 VHOST_MEMORY_MAX_NREGIONS);
585 if (msg.memory.nregions != number_of_fds)
587 vu_log_debug (vui, "each memory region must have FD");
591 /* Do the mmap without barrier sync */
592 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
593 for (i = 0; i < msg.memory.nregions; i++)
595 long page_sz = get_huge_page_size (fds[i]);
597 /* align size to page */
598 ssize_t map_sz = (msg.memory.regions[i].memory_size +
599 msg.memory.regions[i].mmap_offset +
600 page_sz - 1) & ~(page_sz - 1);
602 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
603 MAP_SHARED, fds[i], 0);
604 if (region_mmap_addr[i] == MAP_FAILED)
606 vu_log_err (vui, "failed to map memory. errno is %d", errno);
607 for (j = 0; j < i; j++)
608 munmap (region_mmap_addr[j], map_sz);
611 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
612 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
613 region_mmap_addr[i], page_sz);
616 vlib_worker_thread_barrier_sync (vm);
617 unmap_all_mem_regions (vui);
618 for (i = 0; i < msg.memory.nregions; i++)
620 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
621 sizeof (vhost_user_memory_region_t));
623 vui->region_mmap_addr[i] = region_mmap_addr[i];
624 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
625 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
626 vui->regions[i].memory_size;
628 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
629 vui->region_mmap_fd[i] = fds[i];
635 * Re-compute desc, used, and avail descriptor table if vring address
638 FOR_ALL_VHOST_RX_TXQ (q, vui)
640 if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr &&
641 vui->vrings[q].avail_user_addr)
643 vui->vrings[q].desc =
644 map_user_mem (vui, vui->vrings[q].desc_user_addr);
645 vui->vrings[q].used =
646 map_user_mem (vui, vui->vrings[q].used_user_addr);
647 vui->vrings[q].avail =
648 map_user_mem (vui, vui->vrings[q].avail_user_addr);
651 vlib_worker_thread_barrier_release (vm);
654 case VHOST_USER_SET_VRING_NUM:
655 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
656 vui->hw_if_index, msg.state.index, msg.state.num);
658 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
659 (msg.state.num == 0) || /* it cannot be zero */
660 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
661 (msg.state.index >= vui->num_qid))
663 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
664 " %d, msg.state.index %d, curruent max q %d",
665 msg.state.num, msg.state.index, vui->num_qid);
668 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
671 case VHOST_USER_SET_VRING_ADDR:
672 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
673 vui->hw_if_index, msg.state.index);
675 if (msg.state.index >= vui->num_qid)
677 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
678 " %u >= %u", msg.state.index, vui->num_qid);
682 if (msg.size < sizeof (msg.addr))
684 vu_log_debug (vui, "vhost message is too short (%d < %d)",
685 msg.size, sizeof (msg.addr));
689 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
690 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
691 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
693 if ((desc == NULL) || (used == NULL) || (avail == NULL))
695 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
700 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
701 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
702 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
704 vlib_worker_thread_barrier_sync (vm);
705 vui->vrings[msg.state.index].desc = desc;
706 vui->vrings[msg.state.index].used = used;
707 vui->vrings[msg.state.index].avail = avail;
709 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
710 vui->vrings[msg.state.index].log_used =
711 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
713 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
714 the ring is initialized in an enabled state. */
715 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
716 vui->vrings[msg.state.index].enabled = 1;
718 vui->vrings[msg.state.index].last_used_idx =
719 vui->vrings[msg.state.index].last_avail_idx =
720 vui->vrings[msg.state.index].used->idx;
721 vui->vrings[msg.state.index].last_kick =
722 vui->vrings[msg.state.index].last_used_idx;
724 /* tell driver that we want interrupts or not */
725 vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]);
726 vlib_worker_thread_barrier_release (vm);
727 vhost_user_update_iface_state (vui);
730 case VHOST_USER_SET_OWNER:
731 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
734 case VHOST_USER_RESET_OWNER:
735 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
739 case VHOST_USER_SET_VRING_CALL:
740 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
741 vui->hw_if_index, msg.u64);
743 q = (u8) (msg.u64 & 0xFF);
744 if (vui->num_qid > q)
746 /* if there is old fd, delete and close it */
747 if (vui->vrings[q].callfd_idx != ~0)
749 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
750 vui->vrings[q].callfd_idx);
751 clib_file_del (&file_main, uf);
752 vui->vrings[q].callfd_idx = ~0;
755 else if (vec_len (vui->vrings) > q)
757 /* grow vrings by pair (RX + TX) */
758 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
762 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
765 * Double the array size if it is less than 64 entries.
766 * Slow down thereafter.
768 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
769 new_max_q = vec_len (vui->vrings) << 1;
771 new_max_q = vec_len (vui->vrings) +
772 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
773 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
774 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
776 /* sync with the worker threads, vrings may move due to realloc */
777 vlib_worker_thread_barrier_sync (vm);
778 vec_validate_aligned (vui->vrings, new_max_q - 1,
779 CLIB_CACHE_LINE_BYTES);
780 vlib_worker_thread_barrier_release (vm);
782 for (i = old_max_q; i < vec_len (vui->vrings); i++)
783 vhost_user_vring_init (vui, i);
785 /* grow vrings by pair (RX + TX) */
786 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
789 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
791 if (number_of_fds != 1)
793 vu_log_debug (vui, "More than one fd received !");
797 template.read_function = vhost_user_callfd_read_ready;
798 template.file_descriptor = fds[0];
799 template.private_data =
800 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
801 template.description = format (0, "vhost user");
802 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
805 vui->vrings[q].callfd_idx = ~0;
808 case VHOST_USER_SET_VRING_KICK:
809 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
810 vui->hw_if_index, msg.u64);
812 q = (u8) (msg.u64 & 0xFF);
813 if (q >= vui->num_qid)
815 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
816 " %u >= %u", q, vui->num_qid);
820 if (vui->vrings[q].kickfd_idx != ~0)
822 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
823 vui->vrings[q].kickfd_idx);
824 clib_file_del (&file_main, uf);
825 vui->vrings[q].kickfd_idx = ~0;
828 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
830 if (number_of_fds != 1)
832 vu_log_debug (vui, "More than one fd received !");
836 template.read_function = vhost_user_kickfd_read_ready;
837 template.file_descriptor = fds[0];
838 template.private_data =
839 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
841 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
845 //When no kickfd is set, the queue is initialized as started
846 vui->vrings[q].kickfd_idx = ~0;
847 vui->vrings[q].started = 1;
848 vhost_user_thread_placement (vui, q);
850 vhost_user_update_iface_state (vui);
853 case VHOST_USER_SET_VRING_ERR:
854 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
855 vui->hw_if_index, msg.u64);
857 q = (u8) (msg.u64 & 0xFF);
858 if (q >= vui->num_qid)
860 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
861 " %u >= %u", q, vui->num_qid);
865 if (vui->vrings[q].errfd != -1)
866 close (vui->vrings[q].errfd);
868 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
870 if (number_of_fds != 1)
873 vui->vrings[q].errfd = fds[0];
876 vui->vrings[q].errfd = -1;
879 case VHOST_USER_SET_VRING_BASE:
881 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
882 vui->hw_if_index, msg.state.index, msg.state.num);
883 if (msg.state.index >= vui->num_qid)
885 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
886 " %u >= %u", msg.state.index, vui->num_qid);
889 vlib_worker_thread_barrier_sync (vm);
890 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
891 if (vhost_user_is_packed_ring_supported (vui))
895 * 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
896 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897 * | last avail idx | | last used idx | |
898 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
901 * avail wrap counter used wrap counter
903 /* last avail idx at bit 0-14. */
904 vui->vrings[msg.state.index].last_avail_idx =
905 msg.state.num & 0x7fff;
906 /* avail wrap counter at bit 15 */
907 vui->vrings[msg.state.index].avail_wrap_counter =
908 ! !(msg.state.num & (1 << 15));
911 * Although last_used_idx is passed in the upper 16 bits in qemu
912 * implementation, in practice, last_avail_idx and last_used_idx are
913 * usually the same. As a result, DPDK does not bother to pass us
914 * last_used_idx. The spec is not clear on thex coding. I figured it
915 * out by reading the qemu code. So let's just read last_avail_idx
916 * and set last_used_idx equals to last_avail_idx.
918 vui->vrings[msg.state.index].last_used_idx =
919 vui->vrings[msg.state.index].last_avail_idx;
920 vui->vrings[msg.state.index].last_kick =
921 vui->vrings[msg.state.index].last_used_idx;
922 vui->vrings[msg.state.index].used_wrap_counter =
923 vui->vrings[msg.state.index].avail_wrap_counter;
925 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
926 vui->vrings[msg.state.index].avail_wrap_counter =
929 vlib_worker_thread_barrier_release (vm);
932 case VHOST_USER_GET_VRING_BASE:
933 if (msg.state.index >= vui->num_qid)
935 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
936 " %u >= %u", msg.state.index, vui->num_qid);
940 /* protection is needed to prevent rx/tx from changing last_avail_idx */
941 vlib_worker_thread_barrier_sync (vm);
943 * Copy last_avail_idx from the vring before closing it because
944 * closing the vring also initializes the vring last_avail_idx
946 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
947 if (vhost_user_is_packed_ring_supported (vui))
950 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
951 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
953 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
954 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
957 msg.size = sizeof (msg.state);
960 * Spec says: Client must [...] stop ring upon receiving
961 * VHOST_USER_GET_VRING_BASE
963 vhost_user_vring_close (vui, msg.state.index);
964 vlib_worker_thread_barrier_release (vm);
966 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
967 vui->hw_if_index, msg.state.index, msg.state.num);
969 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
970 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
972 vu_log_debug (vui, "could not send message response");
975 vhost_user_update_iface_state (vui);
978 case VHOST_USER_NONE:
979 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
982 case VHOST_USER_SET_LOG_BASE:
983 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
986 if (msg.size != sizeof (msg.log))
988 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
989 " %d instead of %d", msg.size, sizeof (msg.log));
993 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
995 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
996 "VHOST_USER_SET_LOG_BASE received");
1001 /* align size to page */
1002 long page_sz = get_huge_page_size (fd);
1004 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
1006 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1009 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
1010 "mapped 0x%lx", map_sz, msg.log.offset, fd,
1013 if (log_base_addr == MAP_FAILED)
1015 vu_log_err (vui, "failed to map memory. errno is %d", errno);
1019 vlib_worker_thread_barrier_sync (vm);
1020 vui->log_base_addr = log_base_addr;
1021 vui->log_base_addr += msg.log.offset;
1022 vui->log_size = msg.log.size;
1023 vlib_worker_thread_barrier_release (vm);
1026 msg.size = sizeof (msg.u64);
1028 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1029 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1031 vu_log_debug (vui, "could not send message response");
1036 case VHOST_USER_SET_LOG_FD:
1037 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1040 case VHOST_USER_GET_PROTOCOL_FEATURES:
1042 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1043 (1 << VHOST_USER_PROTOCOL_F_MQ);
1044 msg.size = sizeof (msg.u64);
1045 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1046 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1048 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1049 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1051 vu_log_debug (vui, "could not send message response");
1056 case VHOST_USER_SET_PROTOCOL_FEATURES:
1057 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1058 "features 0x%016llx", vui->hw_if_index, msg.u64);
1059 vui->protocol_features = msg.u64;
1062 case VHOST_USER_GET_QUEUE_NUM:
1064 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1065 msg.size = sizeof (msg.u64);
1066 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1067 vui->hw_if_index, msg.u64);
1069 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1070 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1072 vu_log_debug (vui, "could not send message response");
1077 case VHOST_USER_SET_VRING_ENABLE:
1078 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1079 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1081 if (msg.state.index >= vui->num_qid)
1083 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1084 " %u >= %u", msg.state.index, vui->num_qid);
1088 vui->vrings[msg.state.index].enabled = msg.state.num;
1089 vhost_user_thread_placement (vui, msg.state.index);
1090 vhost_user_update_iface_state (vui);
1094 vu_log_debug (vui, "unknown vhost-user message %d received. "
1095 "closing socket", msg.request);
1102 vlib_worker_thread_barrier_sync (vm);
1103 vhost_user_if_disconnect (vui);
1104 vlib_worker_thread_barrier_release (vm);
1105 vhost_user_update_iface_state (vui);
1109 static clib_error_t *
1110 vhost_user_socket_error (clib_file_t * uf)
1112 vlib_main_t *vm = vlib_get_main ();
1113 vhost_user_main_t *vum = &vhost_user_main;
1114 vhost_user_intf_t *vui =
1115 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1117 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1118 vlib_worker_thread_barrier_sync (vm);
1119 vhost_user_if_disconnect (vui);
1120 vlib_worker_thread_barrier_release (vm);
1124 static clib_error_t *
1125 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1127 int client_fd, client_len;
1128 struct sockaddr_un client;
1129 clib_file_t template = { 0 };
1130 vhost_user_main_t *vum = &vhost_user_main;
1131 vhost_user_intf_t *vui;
1133 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1135 client_len = sizeof (client);
1136 client_fd = accept (uf->file_descriptor,
1137 (struct sockaddr *) &client,
1138 (socklen_t *) & client_len);
1141 return clib_error_return_unix (0, "accept");
1143 if (vui->clib_file_index != ~0)
1145 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1146 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1147 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1150 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1151 vui->sw_if_index, client_fd);
1152 template.read_function = vhost_user_socket_read;
1153 template.error_function = vhost_user_socket_error;
1154 template.file_descriptor = client_fd;
1155 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1156 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1157 vui->clib_file_index = clib_file_add (&file_main, &template);
1162 static clib_error_t *
1163 vhost_user_init (vlib_main_t * vm)
1165 vhost_user_main_t *vum = &vhost_user_main;
1166 vlib_thread_main_t *tm = vlib_get_thread_main ();
1168 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1170 vum->coalesce_frames = 32;
1171 vum->coalesce_time = 1e-3;
1173 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1176 vec_foreach (cpu, vum->cpus)
1178 /* This is actually not necessary as validate already zeroes it
1179 * Just keeping the loop here for later because I am lazy. */
1180 cpu->rx_buffers_len = 0;
1183 vum->random = random_default_seed ();
1185 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1191 VLIB_INIT_FUNCTION (vhost_user_init) =
1193 .runs_after = VLIB_INITS("ip4_init"),
1198 vhost_user_send_interrupt_process (vlib_main_t * vm,
1199 vlib_node_runtime_t * rt, vlib_frame_t * f)
1201 vhost_user_intf_t *vui;
1202 f64 timeout = 3153600000.0 /* 100 years */ ;
1203 uword event_type, *event_data = 0;
1204 vhost_user_main_t *vum = &vhost_user_main;
1206 f64 now, poll_time_remaining;
1212 poll_time_remaining =
1213 vlib_process_wait_for_event_or_clock (vm, timeout);
1214 event_type = vlib_process_get_events (vm, &event_data);
1215 vec_reset_length (event_data);
1218 * Use the remaining timeout if it is less than coalesce time to avoid
1219 * resetting the existing timer in the middle of expiration
1221 timeout = poll_time_remaining;
1222 if (vlib_process_suspend_time_is_zero (timeout) ||
1223 (timeout > vum->coalesce_time))
1224 timeout = vum->coalesce_time;
1226 now = vlib_time_now (vm);
1229 case VHOST_USER_EVENT_STOP_TIMER:
1233 case VHOST_USER_EVENT_START_TIMER:
1236 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1242 pool_foreach (vui, vum->vhost_user_interfaces) {
1243 next_timeout = timeout;
1244 FOR_ALL_VHOST_RX_TXQ (qid, vui)
1246 vhost_user_vring_t *vq = &vui->vrings[qid];
1248 if (vq->started == 0)
1250 if (vq->n_since_last_int)
1252 if (now >= vq->int_deadline)
1253 vhost_user_send_call (vm, vui, vq);
1255 next_timeout = vq->int_deadline - now;
1258 if ((next_timeout < timeout) && (next_timeout > 0.0))
1259 timeout = next_timeout;
1266 clib_warning ("BUG: unhandled event type %d", event_type);
1269 /* No less than 1 millisecond */
1273 timeout = 3153600000.0;
1279 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1280 .function = vhost_user_send_interrupt_process,
1281 .type = VLIB_NODE_TYPE_PROCESS,
1282 .name = "vhost-user-send-interrupt-process",
1287 vhost_user_process (vlib_main_t * vm,
1288 vlib_node_runtime_t * rt, vlib_frame_t * f)
1290 vhost_user_main_t *vum = &vhost_user_main;
1291 vhost_user_intf_t *vui;
1292 struct sockaddr_un sun;
1294 clib_file_t template = { 0 };
1295 f64 timeout = 3153600000.0 /* 100 years */ ;
1296 uword *event_data = 0;
1299 sun.sun_family = AF_UNIX;
1300 template.read_function = vhost_user_socket_read;
1301 template.error_function = vhost_user_socket_error;
1305 vlib_process_wait_for_event_or_clock (vm, timeout);
1306 vlib_process_get_events (vm, &event_data);
1307 vec_reset_length (event_data);
1312 pool_foreach (vui, vum->vhost_user_interfaces) {
1314 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1315 if (vui->clib_file_index == ~0)
1318 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1321 * 1st time error or new error for this interface,
1322 * spit out the message and record the error
1324 if (!vui->sock_errno || (vui->sock_errno != errno))
1327 ("Error: Could not open unix socket for %s",
1328 vui->sock_filename);
1329 vui->sock_errno = errno;
1334 /* try to connect */
1335 strncpy (sun.sun_path, (char *) vui->sock_filename,
1336 sizeof (sun.sun_path) - 1);
1337 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1339 /* Avoid hanging VPP if the other end does not accept */
1340 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1341 clib_unix_warning ("fcntl");
1343 if (connect (sockfd, (struct sockaddr *) &sun,
1344 sizeof (struct sockaddr_un)) == 0)
1346 /* Set the socket to blocking as it was before */
1347 if (fcntl(sockfd, F_SETFL, 0) < 0)
1348 clib_unix_warning ("fcntl2");
1350 vui->sock_errno = 0;
1351 template.file_descriptor = sockfd;
1352 template.private_data =
1353 vui - vhost_user_main.vhost_user_interfaces;
1354 template.description = format (0, "vhost user process");
1355 vui->clib_file_index = clib_file_add (&file_main, &template);
1358 /* This sockfd is considered consumed */
1363 vui->sock_errno = errno;
1368 /* check if socket is alive */
1370 socklen_t len = sizeof (error);
1371 int fd = UNIX_GET_FD(vui->clib_file_index);
1373 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1377 vu_log_debug (vui, "getsockopt returned %d", retval);
1378 vhost_user_if_disconnect (vui);
1389 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1390 .function = vhost_user_process,
1391 .type = VLIB_NODE_TYPE_PROCESS,
1392 .name = "vhost-user-process",
1397 * Disables and reset interface structure.
1398 * It can then be either init again, or removed from used interfaces.
1401 vhost_user_term_if (vhost_user_intf_t * vui)
1404 vhost_user_main_t *vum = &vhost_user_main;
1406 // disconnect interface sockets
1407 vhost_user_if_disconnect (vui);
1408 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1409 vhost_user_update_iface_state (vui);
1411 FOR_ALL_VHOST_RX_TXQ (q, vui)
1413 clib_spinlock_free (&vui->vrings[q].vring_lock);
1416 if (vui->unix_server_index != ~0)
1418 //Close server socket
1419 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1420 vui->unix_server_index);
1421 clib_file_del (&file_main, uf);
1422 vui->unix_server_index = ~0;
1423 unlink (vui->sock_filename);
1426 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1431 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1433 vhost_user_main_t *vum = &vhost_user_main;
1434 vhost_user_intf_t *vui;
1436 vnet_hw_interface_t *hwif;
1441 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1442 || hwif->dev_class_index != vhost_user_device_class.index)
1443 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1445 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1447 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1448 hwif->name, hwif->dev_instance);
1450 FOR_ALL_VHOST_TXQ (qid, vui)
1452 vhost_user_vring_t *txvq = &vui->vrings[qid];
1454 if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) &&
1455 (txvq->thread_index != ~0))
1457 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
1458 ASSERT (cpu->polling_q_count != 0);
1459 cpu->polling_q_count--;
1462 if ((vum->ifq_count > 0) &&
1463 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1464 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1467 // Stop the timer if there is no more interrupt interface/queue
1468 if (vum->ifq_count == 0)
1470 vlib_process_signal_event (vm,
1471 vhost_user_send_interrupt_node.index,
1472 VHOST_USER_EVENT_STOP_TIMER, 0);
1478 // Disable and reset interface
1479 vhost_user_term_if (vui);
1481 // Reset renumbered iface
1482 if (hwif->dev_instance <
1483 vec_len (vum->show_dev_instance_by_real_dev_instance))
1484 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1486 // Delete ethernet interface
1487 ethernet_delete_interface (vnm, vui->hw_if_index);
1490 vec_free (vui->vrings);
1493 pool_put (vum->vhost_user_interfaces, vui);
1498 static clib_error_t *
1499 vhost_user_exit (vlib_main_t * vm)
1501 vnet_main_t *vnm = vnet_get_main ();
1502 vhost_user_main_t *vum = &vhost_user_main;
1503 vhost_user_intf_t *vui;
1505 vlib_worker_thread_barrier_sync (vlib_get_main ());
1507 pool_foreach (vui, vum->vhost_user_interfaces) {
1508 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1511 vlib_worker_thread_barrier_release (vlib_get_main ());
1515 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1518 * Open server unix socket on specified sock_filename.
1521 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1524 struct sockaddr_un un = { };
1526 /* create listening socket */
1527 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1528 return VNET_API_ERROR_SYSCALL_ERROR_1;
1530 un.sun_family = AF_UNIX;
1531 strncpy ((char *) un.sun_path, (char *) sock_filename,
1532 sizeof (un.sun_path) - 1);
1534 /* remove if exists */
1535 unlink ((char *) sock_filename);
1537 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1539 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1543 if (listen (fd, 1) == -1)
1545 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1558 * Create ethernet interface for vhost user interface.
1561 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1562 vhost_user_intf_t *vui,
1563 vhost_user_create_if_args_t *args)
1565 vhost_user_main_t *vum = &vhost_user_main;
1567 clib_error_t *error;
1569 /* create hw and sw interface */
1570 if (args->use_custom_mac)
1572 clib_memcpy (hwaddr, args->hwaddr, 6);
1576 random_u32 (&vum->random);
1577 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1582 error = ethernet_register_interface
1584 vhost_user_device_class.index,
1585 vui - vum->vhost_user_interfaces /* device instance */ ,
1586 hwaddr /* ethernet address */ ,
1587 &vui->hw_if_index, 0 /* flag change */ );
1590 clib_error_report (error);
1594 * Initialize vui with specified attributes
1597 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1598 int server_sock_fd, vhost_user_create_if_args_t * args,
1601 vnet_sw_interface_t *sw;
1603 vhost_user_main_t *vum = &vhost_user_main;
1604 vnet_hw_interface_t *hw;
1606 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1607 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1608 if (server_sock_fd != -1)
1610 clib_file_t template = { 0 };
1611 template.read_function = vhost_user_socksvr_accept_ready;
1612 template.file_descriptor = server_sock_fd;
1613 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1614 template.description = format (0, "vhost user %d", sw);
1615 vui->unix_server_index = clib_file_add (&file_main, &template);
1619 vui->unix_server_index = ~0;
1622 vui->sw_if_index = sw->sw_if_index;
1623 strncpy (vui->sock_filename, args->sock_filename,
1624 ARRAY_LEN (vui->sock_filename) - 1);
1625 vui->sock_errno = 0;
1627 vui->feature_mask = args->feature_mask;
1628 vui->clib_file_index = ~0;
1629 vui->log_base_addr = 0;
1630 vui->if_index = vui - vum->vhost_user_interfaces;
1631 vui->enable_gso = args->enable_gso;
1632 vui->enable_event_idx = args->enable_event_idx;
1633 vui->enable_packed = args->enable_packed;
1635 * enable_gso takes precedence over configurable feature mask if there
1637 * if feature mask disables gso, but enable_gso is configured,
1638 * then gso is enable
1639 * if feature mask enables gso, but enable_gso is not configured,
1640 * then gso is enable
1642 * if gso is enable via feature mask, it must enable both host and guest
1643 * gso feature mask, we don't support one sided GSO or partial GSO.
1645 if ((vui->enable_gso == 0) &&
1646 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1647 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1648 vui->enable_gso = 1;
1649 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1650 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1653 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1654 CLIB_CACHE_LINE_BYTES);
1656 for (q = 0; q < vec_len (vui->vrings); q++)
1657 vhost_user_vring_init (vui, q);
1659 hw->caps |= VNET_HW_IF_CAP_INT_MODE;
1660 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1663 *sw_if_index = vui->sw_if_index;
1667 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1668 vhost_user_create_if_args_t * args)
1670 vhost_user_intf_t *vui = NULL;
1673 int server_sock_fd = -1;
1674 vhost_user_main_t *vum = &vhost_user_main;
1677 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1679 return VNET_API_ERROR_INVALID_ARGUMENT;
1682 if_index = mhash_get (&vum->if_index_by_sock_name,
1683 (void *) args->sock_filename);
1686 vui = &vum->vhost_user_interfaces[*if_index];
1687 args->sw_if_index = vui->sw_if_index;
1688 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1691 if (args->is_server)
1694 vhost_user_init_server_sock (args->sock_filename,
1695 &server_sock_fd)) != 0)
1701 /* Protect the uninitialized vui from being dispatched by rx/tx */
1702 vlib_worker_thread_barrier_sync (vm);
1703 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1704 vhost_user_create_ethernet (vnm, vm, vui, args);
1705 vlib_worker_thread_barrier_release (vm);
1707 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1708 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1709 vhost_user_rx_thread_placement (vui, 1);
1712 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1714 args->sw_if_index = sw_if_idx;
1716 // Process node must connect
1717 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1723 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1724 vhost_user_create_if_args_t * args)
1726 vhost_user_main_t *vum = &vhost_user_main;
1727 vhost_user_intf_t *vui = NULL;
1729 int server_sock_fd = -1;
1731 vnet_hw_interface_t *hwif;
1734 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1736 || hwif->dev_class_index != vhost_user_device_class.index)
1737 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1739 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1740 return VNET_API_ERROR_INVALID_ARGUMENT;
1742 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1745 * Disallow changing the interface to have the same path name
1746 * as other interface
1748 if_index = mhash_get (&vum->if_index_by_sock_name,
1749 (void *) args->sock_filename);
1750 if (if_index && (*if_index != vui->if_index))
1751 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1753 // First try to open server socket
1754 if (args->is_server)
1755 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1756 &server_sock_fd)) != 0)
1759 vhost_user_term_if (vui);
1760 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1763 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1765 // Process node must connect
1766 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1772 vhost_user_connect_command_fn (vlib_main_t * vm,
1773 unformat_input_t * input,
1774 vlib_cli_command_t * cmd)
1776 vnet_main_t *vnm = vnet_get_main ();
1777 unformat_input_t _line_input, *line_input = &_line_input;
1778 clib_error_t *error = NULL;
1779 vhost_user_create_if_args_t args = { 0 };
1782 /* Get a line of input. */
1783 if (!unformat_user (input, unformat_line_input, line_input))
1786 args.feature_mask = (u64) ~ (0ULL);
1787 args.custom_dev_instance = ~0;
1788 /* GSO feature is disable by default */
1789 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1790 /* packed-ring feature is disable by default */
1791 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1792 /* event_idx feature is disable by default */
1793 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1795 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1797 if (unformat (line_input, "socket %s", &args.sock_filename))
1799 else if (unformat (line_input, "server"))
1801 else if (unformat (line_input, "gso"))
1802 args.enable_gso = 1;
1803 else if (unformat (line_input, "packed"))
1804 args.enable_packed = 1;
1805 else if (unformat (line_input, "event-idx"))
1806 args.enable_event_idx = 1;
1807 else if (unformat (line_input, "feature-mask 0x%llx",
1808 &args.feature_mask))
1810 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1812 args.use_custom_mac = 1;
1813 else if (unformat (line_input, "renumber %d",
1814 &args.custom_dev_instance))
1818 error = clib_error_return (0, "unknown input `%U'",
1819 format_unformat_error, line_input);
1824 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1826 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1830 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1834 vec_free (args.sock_filename);
1835 unformat_free (line_input);
1841 vhost_user_delete_command_fn (vlib_main_t * vm,
1842 unformat_input_t * input,
1843 vlib_cli_command_t * cmd)
1845 unformat_input_t _line_input, *line_input = &_line_input;
1846 u32 sw_if_index = ~0;
1847 vnet_main_t *vnm = vnet_get_main ();
1848 clib_error_t *error = NULL;
1850 /* Get a line of input. */
1851 if (!unformat_user (input, unformat_line_input, line_input))
1854 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1856 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1859 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1862 vnet_hw_interface_t *hwif =
1863 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1865 vhost_user_device_class.index != hwif->dev_class_index)
1867 error = clib_error_return (0, "Not a vhost interface");
1873 error = clib_error_return (0, "unknown input `%U'",
1874 format_unformat_error, line_input);
1879 vhost_user_delete_if (vnm, vm, sw_if_index);
1882 unformat_free (line_input);
1888 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1889 vhost_user_intf_details_t ** out_vuids)
1892 vhost_user_main_t *vum = &vhost_user_main;
1893 vhost_user_intf_t *vui;
1894 vhost_user_intf_details_t *r_vuids = NULL;
1895 vhost_user_intf_details_t *vuid = NULL;
1896 u32 *hw_if_indices = 0;
1897 vnet_hw_interface_t *hi;
1903 pool_foreach (vui, vum->vhost_user_interfaces)
1904 vec_add1 (hw_if_indices, vui->hw_if_index);
1906 for (i = 0; i < vec_len (hw_if_indices); i++)
1908 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1909 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1911 vec_add2 (r_vuids, vuid, 1);
1912 vuid->sw_if_index = vui->sw_if_index;
1913 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1914 vuid->features = vui->features;
1915 vuid->num_regions = vui->nregions;
1916 vuid->is_server = vui->unix_server_index != ~0;
1917 vuid->sock_errno = vui->sock_errno;
1918 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1919 "%s", vui->sock_filename);
1920 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1921 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1922 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1925 vec_free (hw_if_indices);
1927 *out_vuids = r_vuids;
1933 format_vhost_user_desc (u8 * s, va_list * args)
1935 char *fmt = va_arg (*args, char *);
1936 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1937 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1938 int idx = va_arg (*args, int);
1939 u32 *mem_hint = va_arg (*args, u32 *);
1941 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1942 desc_table[idx].flags, desc_table[idx].next,
1943 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1949 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1951 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1952 int callfd = UNIX_GET_FD (vq->callfd_idx);
1954 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
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;
1967 vhost_user_vring_t *vq = &vui->vrings[q];
1969 if (vq->avail && vq->used)
1970 vlib_cli_output (vm,
1971 " avail.flags %x avail event idx %u avail.idx %d "
1972 "used.flags %x used event idx %u used.idx %d\n",
1973 vq->avail->flags, vhost_user_avail_event_idx (vq),
1974 vq->avail->idx, vq->used->flags,
1975 vhost_user_used_event_idx (vq), vq->used->idx);
1977 vhost_user_show_fds (vm, vq);
1981 vlib_cli_output (vm, "\n descriptor table:\n");
1982 vlib_cli_output (vm,
1983 " slot addr len flags next "
1985 vlib_cli_output (vm,
1986 " ===== ================== ===== ====== ===== "
1987 "==================\n");
1988 for (j = 0; j < vq->qsz_mask + 1; j++)
1990 desc_table = vq->desc;
1991 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1992 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1993 desc_table, j, &mem_hint);
1994 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1996 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1997 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2000 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2003 (vm, "%U", format_vhost_user_desc,
2004 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2005 desc_table, idx, &mem_hint);
2007 if (n_entries >= 20)
2008 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2017 format_vhost_user_packed_desc (u8 * s, va_list * args)
2019 char *fmt = va_arg (*args, char *);
2020 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2021 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
2022 int idx = va_arg (*args, int);
2023 u32 *mem_hint = va_arg (*args, u32 *);
2025 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2026 desc_table[idx].flags, desc_table[idx].id,
2027 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2033 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
2035 u32 flags = va_arg (*args, u32);
2041 static event_idx_flags event_idx_array[] = {
2042 #define _(s,v) { .str = #s, .value = v, },
2043 foreach_virtio_event_idx_flags
2046 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2048 if (flags < num_entries)
2049 s = format (s, "%s", event_idx_array[flags].str);
2051 s = format (s, "%u", flags);
2056 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2057 int show_descr, int show_verbose)
2063 vring_packed_desc_t *desc_table;
2064 vhost_user_vring_t *vq = &vui->vrings[q];
2065 u16 off_wrap, event_idx;
2067 off_wrap = vq->avail_event->off_wrap;
2068 event_idx = off_wrap & 0x7fff;
2069 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2070 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2071 (u32) vq->avail_event->flags, off_wrap, event_idx);
2073 off_wrap = vq->used_event->off_wrap;
2074 event_idx = off_wrap & 0x7fff;
2075 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2076 "used event idx %u\n", format_vhost_user_event_idx_flags,
2077 (u32) vq->used_event->flags, off_wrap, event_idx);
2079 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2080 vq->avail_wrap_counter, vq->used_wrap_counter);
2082 vhost_user_show_fds (vm, vq);
2086 vlib_cli_output (vm, "\n descriptor table:\n");
2087 vlib_cli_output (vm,
2088 " slot addr len flags id "
2090 vlib_cli_output (vm,
2091 " ===== ================== ===== ====== ===== "
2092 "==================\n");
2093 for (j = 0; j < vq->qsz_mask + 1; j++)
2095 desc_table = vq->packed_desc;
2096 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2097 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2098 desc_table, j, &mem_hint);
2099 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2101 n_entries = desc_table[j].len >> 4;
2102 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2105 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2108 (vm, "%U", format_vhost_user_packed_desc,
2109 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2110 desc_table, idx, &mem_hint);
2112 if (n_entries >= 20)
2113 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2122 show_vhost_user_command_fn (vlib_main_t * vm,
2123 unformat_input_t * input,
2124 vlib_cli_command_t * cmd)
2126 clib_error_t *error = 0;
2127 vnet_main_t *vnm = vnet_get_main ();
2128 vhost_user_main_t *vum = &vhost_user_main;
2129 vhost_user_intf_t *vui;
2130 u32 hw_if_index, *hw_if_indices = 0;
2131 vnet_hw_interface_t *hi;
2135 int show_verbose = 0;
2141 struct feat_struct *feat_entry;
2143 static struct feat_struct feat_array[] = {
2144 #define _(s,b) { .str = #s, .bit = b, },
2145 foreach_virtio_net_features
2150 #define foreach_protocol_feature \
2151 _(VHOST_USER_PROTOCOL_F_MQ) \
2152 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2154 static struct feat_struct proto_feat_array[] = {
2155 #define _(s) { .str = #s, .bit = s},
2156 foreach_protocol_feature
2161 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2164 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2166 hi = vnet_get_hw_interface (vnm, hw_if_index);
2167 if (vhost_user_device_class.index != hi->dev_class_index)
2169 error = clib_error_return (0, "unknown input `%U'",
2170 format_unformat_error, input);
2173 vec_add1 (hw_if_indices, hw_if_index);
2175 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2177 else if (unformat (input, "verbose"))
2181 error = clib_error_return (0, "unknown input `%U'",
2182 format_unformat_error, input);
2186 if (vec_len (hw_if_indices) == 0)
2188 pool_foreach (vui, vum->vhost_user_interfaces)
2189 vec_add1 (hw_if_indices, vui->hw_if_index);
2191 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2192 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2193 vum->coalesce_frames, vum->coalesce_time);
2194 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2196 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2197 for (u32 tid = 0; tid <= vlib_num_workers (); tid++)
2199 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid);
2200 vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid,
2201 cpu->polling_q_count);
2204 for (i = 0; i < vec_len (hw_if_indices); i++)
2206 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2207 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2208 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2209 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2211 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2212 if (vui->enable_gso)
2213 vlib_cli_output (vm, " GSO enable");
2214 if (vui->enable_packed)
2215 vlib_cli_output (vm, " Packed ring enable");
2216 if (vui->enable_event_idx)
2217 vlib_cli_output (vm, " Event index enable");
2219 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2220 " features mask (0x%llx): \n"
2221 " features (0x%llx): \n",
2222 vui->virtio_net_hdr_sz, vui->feature_mask,
2225 feat_entry = (struct feat_struct *) &feat_array;
2226 while (feat_entry->str)
2228 if (vui->features & (1ULL << feat_entry->bit))
2229 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2234 vlib_cli_output (vm, " protocol features (0x%llx)",
2235 vui->protocol_features);
2236 feat_entry = (struct feat_struct *) &proto_feat_array;
2237 while (feat_entry->str)
2239 if (vui->protocol_features & (1ULL << feat_entry->bit))
2240 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2245 vlib_cli_output (vm, "\n");
2247 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2249 (vui->unix_server_index != ~0) ? "server" : "client",
2250 strerror (vui->sock_errno));
2252 vlib_cli_output (vm, " rx placement: ");
2254 FOR_ALL_VHOST_TXQ (qid, vui)
2256 vhost_user_vring_t *txvq = &vui->vrings[qid];
2258 if (txvq->qid == -1)
2260 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2261 txvq->thread_index, qid, format_vnet_hw_if_rx_mode,
2265 vlib_cli_output (vm, " tx placement\n");
2267 FOR_ALL_VHOST_RXQ (qid, vui)
2269 vhost_user_vring_t *rxvq = &vui->vrings[qid];
2270 vnet_hw_if_tx_queue_t *txq;
2272 if (rxvq->queue_index == ~0)
2274 txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index);
2276 vlib_cli_output (vm, " threads %U on vring %u: %s\n",
2277 format_bitmap_list, txq->threads, qid,
2278 txq->shared_queue ? "spin-lock" : "lock-free");
2281 vlib_cli_output (vm, "\n");
2283 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2287 vlib_cli_output (vm,
2288 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2289 vlib_cli_output (vm,
2290 " ====== ===== ================== ================== ================== ================== ==================\n");
2292 for (j = 0; j < vui->nregions; j++)
2294 vlib_cli_output (vm,
2295 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2296 j, vui->region_mmap_fd[j],
2297 vui->regions[j].guest_phys_addr,
2298 vui->regions[j].memory_size,
2299 vui->regions[j].userspace_addr,
2300 vui->regions[j].mmap_offset,
2301 pointer_to_uword (vui->region_mmap_addr[j]));
2303 FOR_ALL_VHOST_RX_TXQ (q, vui)
2305 if (!vui->vrings[q].started)
2308 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2309 (q & 1) ? "RX" : "TX",
2310 vui->vrings[q].enabled ? "" : " disabled");
2311 vlib_cli_output (vm, " global %s queue index %u\n",
2312 (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index);
2316 " qsz %d last_avail_idx %d last_used_idx %d"
2318 vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx,
2319 vui->vrings[q].last_used_idx, vui->vrings[q].last_kick);
2321 if (vhost_user_is_packed_ring_supported (vui))
2322 vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose);
2324 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2326 vlib_cli_output (vm, "\n");
2329 vec_free (hw_if_indices);
2338 * Create a vHost User interface. Once created, a new virtual interface
2339 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2340 * is the next free index.
2342 * There are several parameters associated with a vHost interface:
2344 * - <b>socket <socket-filename></b> - Name of the linux socket used by
2345 * hypervisor and VPP to manage the vHost interface. If in <em>server</em>
2346 * mode, VPP will create the socket if it does not already exist. If in
2347 * <em>client</em> mode, hypervisor will create the socket if it does not
2348 * already exist. The VPP code is indifferent to the file location. However,
2349 * if SELinux is enabled, then the socket needs to be created in
2350 * <em>/var/run/vpp/</em>.
2352 * - <b>server</b> - Optional flag to indicate that VPP should be the server
2353 * for the linux socket. If not provided, VPP will be the client. In
2354 * <em>server</em> mode, the VM can be reset without tearing down the vHost
2355 * Interface. In <em>client</em> mode, VPP can be reset without bringing down
2356 * the VM and tearing down the vHost Interface.
2358 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated
2359 * at startup. <b>This is intended for degugging only.</b> It is recommended
2360 * that this parameter not be used except by experienced users. By default,
2361 * all supported features will be advertised. Otherwise, provide the set of
2363 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2364 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2365 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2366 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2367 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2368 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2369 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2370 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2371 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2373 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2374 * X:X:X:X:X:X unix or X.X.X cisco format.
2376 * - <b>renumber <dev_instance></b> - Optional parameter which allows the
2377 * instance in the name to be specified. If instance already exists, name
2378 * will be used anyway and multiple instances will have the same name. Use
2382 * Example of how to create a vhost interface with VPP as the client and all
2384 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2385 * VirtualEthernet0/0/0
2387 * Example of how to create a vhost interface with VPP as the server and with
2388 * just multiple queues enabled:
2389 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server
2390 * feature-mask 0x40400000}
2391 * VirtualEthernet0/0/1
2393 * Once the vHost interface is created, enable the interface using:
2394 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2397 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2398 .path = "create vhost-user",
2399 .short_help = "create vhost-user socket <socket-filename> [server] "
2400 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2401 "[packed] [event-idx]",
2402 .function = vhost_user_connect_command_fn,
2408 * Delete a vHost User interface using the interface name or the
2409 * software interface index. Use the '<em>show interface</em>'
2410 * command to determine the software interface index. On deletion,
2411 * the linux socket will not be deleted.
2414 * Example of how to delete a vhost interface by name:
2415 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2416 * Example of how to delete a vhost interface by software interface index:
2417 * @cliexcmd{delete vhost-user sw_if_index 1}
2420 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2421 .path = "delete vhost-user",
2422 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2423 .function = vhost_user_delete_command_fn,
2427 * Display the attributes of a single vHost User interface (provide interface
2428 * name), multiple vHost User interfaces (provide a list of interface names
2429 * separated by spaces) or all Vhost User interfaces (omit an interface name
2430 * to display all vHost interfaces).
2434 * Example of how to display a vhost interface:
2435 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2436 * Virtio vhost-user interfaces
2438 * coalesce frames 32 time 1e-3
2439 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2440 * virtio_net_hdr_sz 12
2441 * features mask (0xffffffffffffffff):
2442 * features (0x50408000):
2443 * VIRTIO_NET_F_MRG_RXBUF (15)
2444 * VIRTIO_NET_F_MQ (22)
2445 * VIRTIO_F_INDIRECT_DESC (28)
2446 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2447 * protocol features (0x3)
2448 * VHOST_USER_PROTOCOL_F_MQ (0)
2449 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2451 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2454 * thread 1 on vring 1
2455 * thread 1 on vring 5
2456 * thread 2 on vring 3
2457 * thread 2 on vring 7
2458 * tx placement: spin-lock
2459 * thread 0 on vring 0
2460 * thread 1 on vring 2
2461 * thread 2 on vring 0
2463 * Memory regions (total 2)
2464 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2465 * ====== == =============== =========== ============== =========== ==========
2466 * 0 60 0x00000000 0x000a0000 0xaac00000 0x00000000 0x2b400000
2467 * 1 61 0x000c0000 0x3ff40000 0xaacc0000 0x000c0000 0xabcc0000
2470 * qsz 256 last_avail_idx 0 last_used_idx 0
2471 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2472 * kickfd 62 callfd 64 errfd -1
2475 * qsz 256 last_avail_idx 0 last_used_idx 0
2476 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2477 * kickfd 65 callfd 66 errfd -1
2480 * qsz 256 last_avail_idx 0 last_used_idx 0
2481 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2482 * kickfd 63 callfd 70 errfd -1
2485 * qsz 256 last_avail_idx 0 last_used_idx 0
2486 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2487 * kickfd 72 callfd 74 errfd -1
2489 * Virtqueue 4 (TX disabled)
2490 * qsz 256 last_avail_idx 0 last_used_idx 0
2491 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2492 * kickfd 76 callfd 78 errfd -1
2494 * Virtqueue 5 (RX disabled)
2495 * qsz 256 last_avail_idx 0 last_used_idx 0
2496 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2497 * kickfd 80 callfd 82 errfd -1
2499 * Virtqueue 6 (TX disabled)
2500 * qsz 256 last_avail_idx 0 last_used_idx 0
2501 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2502 * kickfd 84 callfd 86 errfd -1
2504 * Virtqueue 7 (RX disabled)
2505 * qsz 256 last_avail_idx 0 last_used_idx 0
2506 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2507 * kickfd 88 callfd 90 errfd -1
2511 * The optional '<em>descriptors</em>' parameter will display the same output
2512 * as the previous example but will include the descriptor table for each
2514 * The output is truncated below:
2515 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2516 * Virtio vhost-user interfaces
2518 * coalesce frames 32 time 1e-3
2519 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2520 * virtio_net_hdr_sz 12
2521 * features mask (0xffffffffffffffff):
2522 * features (0x50408000):
2523 * VIRTIO_NET_F_MRG_RXBUF (15)
2524 * VIRTIO_NET_F_MQ (22)
2527 * qsz 256 last_avail_idx 0 last_used_idx 0
2528 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2529 * kickfd 62 callfd 64 errfd -1
2532 * id addr len flags next user_addr
2533 * ===== ================== ===== ====== ===== ==================
2534 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2535 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2536 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2537 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2538 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2539 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2540 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2541 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2542 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2543 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2544 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2546 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2547 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2548 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2549 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2550 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2551 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2552 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2555 * qsz 256 last_avail_idx 0 last_used_idx 0
2561 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2562 .path = "show vhost-user",
2563 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2564 "[[descriptors] [verbose]]",
2565 .function = show_vhost_user_command_fn,
2570 static clib_error_t *
2571 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2573 vhost_user_main_t *vum = &vhost_user_main;
2575 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2577 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2579 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2581 else if (unformat (input, "dont-dump-memory"))
2582 vum->dont_dump_vhost_user_memory = 1;
2584 return clib_error_return (0, "unknown input `%U'",
2585 format_unformat_error, input);
2591 /* vhost-user { ... } configuration. */
2592 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2595 vhost_user_unmap_all (void)
2597 vhost_user_main_t *vum = &vhost_user_main;
2598 vhost_user_intf_t *vui;
2600 if (vum->dont_dump_vhost_user_memory)
2602 pool_foreach (vui, vum->vhost_user_interfaces)
2603 unmap_all_mem_regions (vui);
2608 * fd.io coding-style-patch-verification: ON
2611 * eval: (c-set-style "gnu")