2 *------------------------------------------------------------------
5 * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 #include <vnet/interface/rx_queue_funcs.h>
41 #include <vnet/devices/virtio/vhost_user.h>
42 #include <vnet/devices/virtio/vhost_user_inline.h>
46 * @brief vHost User Device Driver.
48 * This file contains the source code for vHost User interface.
52 vlib_node_registration_t vhost_user_send_interrupt_node;
55 vhost_user_main_t vhost_user_main = {
59 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
65 get_huge_page_size (int fd)
73 unmap_all_mem_regions (vhost_user_intf_t * vui)
76 vhost_user_vring_t *vq;
78 for (i = 0; i < vui->nregions; i++)
80 if (vui->region_mmap_addr[i] != MAP_FAILED)
83 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
85 ssize_t map_sz = (vui->regions[i].memory_size +
86 vui->regions[i].mmap_offset +
87 page_sz - 1) & ~(page_sz - 1);
90 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
93 vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
94 "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
97 vui->region_mmap_addr[i] = MAP_FAILED;
101 vu_log_err (vui, "failed to unmap memory region (errno %d)",
104 close (vui->region_mmap_fd[i]);
109 FOR_ALL_VHOST_RX_TXQ (q, vui)
111 vq = &vui->vrings[q];
118 static_always_inline void
119 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
121 //Let's try to assign one queue to each thread
123 u32 thread_index = 0;
125 vui->use_tx_spinlock = 0;
128 for (qid = 0; qid < vui->num_qid / 2; qid++)
130 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
131 if (!rxvq->started || !rxvq->enabled)
134 vui->per_cpu_tx_qid[thread_index] = qid;
136 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
139 //We need to loop, meaning the spinlock has to be used
140 vui->use_tx_spinlock = 1;
141 if (thread_index == 0)
143 //Could not find a single valid one
144 for (thread_index = 0;
145 thread_index < vlib_get_thread_main ()->n_vlib_mains;
148 vui->per_cpu_tx_qid[thread_index] = 0;
156 * @brief Unassign existing interface/queue to thread mappings and re-assign
157 * new interface/queue to thread mappings
159 static_always_inline void
160 vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
162 vhost_user_vring_t *txvq = &vui->vrings[qid];
163 vnet_main_t *vnm = vnet_get_main ();
166 vhost_user_main_t *vum = &vhost_user_main;
168 ASSERT ((qid & 1) == 1); // should be odd
169 // Assign new queue mappings for the interface
170 if (txvq->queue_index != ~0)
172 vnet_hw_if_set_input_node (vnm, vui->hw_if_index,
173 vhost_user_input_node.index);
174 txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q,
175 VNET_HW_IF_RXQ_THREAD_ANY);
177 vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index);
179 if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
180 /* Set polling as the default */
181 txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
182 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
184 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
185 /* Keep a polling queue count for each thread */
186 cpu->polling_q_count++;
189 rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode);
191 vu_log_warn (vui, "unable to set rx mode for interface %d, "
192 "queue %d: rc=%d", vui->hw_if_index, q, rv);
193 vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index);
196 /** @brief Returns whether at least one TX and one RX vring are enabled */
197 static_always_inline int
198 vhost_user_intf_ready (vhost_user_intf_t * vui)
200 int i, found[2] = { }; //RX + TX
202 for (i = 0; i < vui->num_qid; i++)
203 if (vui->vrings[i].started && vui->vrings[i].enabled)
206 return found[0] && found[1];
209 static_always_inline void
210 vhost_user_update_iface_state (vhost_user_intf_t * vui)
212 /* if we have pointers to descriptor table, go up */
213 int is_ready = vhost_user_intf_ready (vui);
214 if (is_ready != vui->is_ready)
216 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
217 is_ready ? "ready" : "down");
219 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
220 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
222 vui->is_ready = is_ready;
226 static clib_error_t *
227 vhost_user_callfd_read_ready (clib_file_t * uf)
229 __attribute__ ((unused)) int n;
232 n = read (uf->file_descriptor, ((char *) &buff), 8);
237 static_always_inline void
238 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
240 if (qid & 1) // RX is odd, TX is even
242 if (vui->vrings[qid].queue_index == ~0)
243 vhost_user_rx_thread_placement (vui, qid);
246 vhost_user_tx_thread_placement (vui);
249 static clib_error_t *
250 vhost_user_kickfd_read_ready (clib_file_t * uf)
252 __attribute__ ((unused)) ssize_t n;
254 vhost_user_main_t *vum = &vhost_user_main;
255 vhost_user_intf_t *vui =
256 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8);
257 u32 qid = uf->private_data & 0xff;
258 u32 is_txq = qid & 1;
259 vhost_user_vring_t *vq = &vui->vrings[qid];
260 vnet_main_t *vnm = vnet_get_main ();
262 n = read (uf->file_descriptor, buff, 8);
263 if (vq->started == 0)
266 vhost_user_thread_placement (vui, qid);
267 vhost_user_update_iface_state (vui);
269 vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index,
273 if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) &&
274 vhost_user_intf_ready (vui))
276 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index);
278 * If the thread has more than 1 queue and the other queue is in polling
279 * mode, there is no need to trigger an interrupt
281 if (cpu->polling_q_count == 0)
282 vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index);
288 static_always_inline void
289 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
291 vhost_user_vring_t *vring = &vui->vrings[qid];
293 clib_memset (vring, 0, sizeof (*vring));
294 vring->kickfd_idx = ~0;
295 vring->callfd_idx = ~0;
298 vring->queue_index = ~0;
299 vring->thread_index = ~0;
300 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
302 clib_spinlock_init (&vring->vring_lock);
305 * We have a bug with some qemu 2.5, and this may be a fix.
306 * Feel like interpretation holy text, but this is from vhost-user.txt.
308 * One queue pair is enabled initially. More queues are enabled
309 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
311 * Don't know who's right, but this is what DPDK does.
313 if (qid == 0 || qid == 1)
317 static_always_inline void
318 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
320 vhost_user_vring_t *vring = &vui->vrings[qid];
322 if (vring->kickfd_idx != ~0)
324 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
326 clib_file_del (&file_main, uf);
327 vring->kickfd_idx = ~0;
329 if (vring->callfd_idx != ~0)
331 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
333 clib_file_del (&file_main, uf);
334 vring->callfd_idx = ~0;
336 if (vring->errfd != -1)
338 close (vring->errfd);
342 clib_spinlock_free (&vring->vring_lock);
344 // save the needed information in vrings prior to being wiped out
345 u16 q = vui->vrings[qid].qid;
346 u32 queue_index = vui->vrings[qid].queue_index;
347 u32 mode = vui->vrings[qid].mode;
348 u32 thread_index = vui->vrings[qid].thread_index;
349 vhost_user_vring_init (vui, qid);
350 vui->vrings[qid].qid = q;
351 vui->vrings[qid].queue_index = queue_index;
352 vui->vrings[qid].mode = mode;
353 vui->vrings[qid].thread_index = thread_index;
356 static_always_inline void
357 vhost_user_if_disconnect (vhost_user_intf_t * vui)
359 vnet_main_t *vnm = vnet_get_main ();
362 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
364 if (vui->clib_file_index != ~0)
366 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
367 vui->clib_file_index = ~0;
372 FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); }
374 unmap_all_mem_regions (vui);
375 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
379 vhost_user_set_operation_mode (vhost_user_intf_t *vui,
380 vhost_user_vring_t *txvq)
382 if (vhost_user_is_packed_ring_supported (vui))
384 if (txvq->used_event)
386 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
387 txvq->used_event->flags = VRING_EVENT_F_DISABLE;
389 txvq->used_event->flags = 0;
396 if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING)
397 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
399 txvq->used->flags = 0;
404 static clib_error_t *
405 vhost_user_socket_read (clib_file_t * uf)
408 int fd, number_of_fds = 0;
409 int fds[VHOST_MEMORY_MAX_NREGIONS];
410 vhost_user_msg_t msg;
413 vhost_user_main_t *vum = &vhost_user_main;
414 vhost_user_intf_t *vui;
415 struct cmsghdr *cmsg;
417 clib_file_t template = { 0 };
418 vnet_main_t *vnm = vnet_get_main ();
419 vlib_main_t *vm = vlib_get_main ();
421 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
423 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
425 clib_memset (&mh, 0, sizeof (mh));
426 clib_memset (control, 0, sizeof (control));
428 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
431 /* set the payload */
432 iov[0].iov_base = (void *) &msg;
433 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
437 mh.msg_control = control;
438 mh.msg_controllen = sizeof (control);
440 n = recvmsg (uf->file_descriptor, &mh, 0);
442 if (n != VHOST_USER_MSG_HDR_SZ)
446 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
451 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
452 n, VHOST_USER_MSG_HDR_SZ);
457 if (mh.msg_flags & MSG_CTRUNC)
459 vu_log_debug (vui, "MSG_CTRUNC is set");
463 cmsg = CMSG_FIRSTHDR (&mh);
465 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
466 (cmsg->cmsg_type == SCM_RIGHTS) &&
467 (cmsg->cmsg_len - CMSG_LEN (0) <=
468 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
470 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
471 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
474 /* version 1, no reply bit set */
475 if ((msg.flags & 7) != 1)
477 vu_log_debug (vui, "malformed message received. closing socket");
484 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
488 vu_log_debug (vui, "read failed %s", strerror (errno));
491 else if (rv != msg.size)
493 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
501 case VHOST_USER_GET_FEATURES:
503 msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
504 VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
505 VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
506 VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
507 VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
508 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
509 VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
510 VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
511 VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
512 msg.u64 &= vui->feature_mask;
514 if (vui->enable_event_idx)
515 msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
517 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
518 if (vui->enable_packed)
519 msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
521 msg.size = sizeof (msg.u64);
522 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
523 "0x%016llx", vui->hw_if_index, msg.u64);
525 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
526 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
528 vu_log_debug (vui, "could not send message response");
533 case VHOST_USER_SET_FEATURES:
534 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
535 "0x%016llx", vui->hw_if_index, msg.u64);
537 vui->features = msg.u64;
540 (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
541 VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
542 vui->virtio_net_hdr_sz = 12;
544 vui->virtio_net_hdr_sz = 10;
547 (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
549 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
550 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
551 if (vui->enable_gso &&
552 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
553 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
555 hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
556 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
557 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
561 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
562 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
564 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
566 vhost_user_update_iface_state (vui);
569 case VHOST_USER_SET_MEM_TABLE:
570 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
571 vui->hw_if_index, msg.memory.nregions);
573 if ((msg.memory.nregions < 1) ||
574 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
576 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
577 VHOST_MEMORY_MAX_NREGIONS);
581 if (msg.memory.nregions != number_of_fds)
583 vu_log_debug (vui, "each memory region must have FD");
587 /* Do the mmap without barrier sync */
588 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
589 for (i = 0; i < msg.memory.nregions; i++)
591 long page_sz = get_huge_page_size (fds[i]);
593 /* align size to page */
594 ssize_t map_sz = (msg.memory.regions[i].memory_size +
595 msg.memory.regions[i].mmap_offset +
596 page_sz - 1) & ~(page_sz - 1);
598 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
599 MAP_SHARED, fds[i], 0);
600 if (region_mmap_addr[i] == MAP_FAILED)
602 vu_log_err (vui, "failed to map memory. errno is %d", errno);
603 for (j = 0; j < i; j++)
604 munmap (region_mmap_addr[j], map_sz);
607 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
608 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
609 region_mmap_addr[i], page_sz);
612 vlib_worker_thread_barrier_sync (vm);
613 unmap_all_mem_regions (vui);
614 for (i = 0; i < msg.memory.nregions; i++)
616 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
617 sizeof (vhost_user_memory_region_t));
619 vui->region_mmap_addr[i] = region_mmap_addr[i];
620 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
621 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
622 vui->regions[i].memory_size;
624 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
625 vui->region_mmap_fd[i] = fds[i];
631 * Re-compute desc, used, and avail descriptor table if vring address
634 FOR_ALL_VHOST_RX_TXQ (q, vui)
636 if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr &&
637 vui->vrings[q].avail_user_addr)
639 vui->vrings[q].desc =
640 map_user_mem (vui, vui->vrings[q].desc_user_addr);
641 vui->vrings[q].used =
642 map_user_mem (vui, vui->vrings[q].used_user_addr);
643 vui->vrings[q].avail =
644 map_user_mem (vui, vui->vrings[q].avail_user_addr);
647 vlib_worker_thread_barrier_release (vm);
650 case VHOST_USER_SET_VRING_NUM:
651 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
652 vui->hw_if_index, msg.state.index, msg.state.num);
654 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
655 (msg.state.num == 0) || /* it cannot be zero */
656 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
657 (msg.state.index >= vui->num_qid))
659 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
660 " %d, msg.state.index %d, curruent max q %d",
661 msg.state.num, msg.state.index, vui->num_qid);
664 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
667 case VHOST_USER_SET_VRING_ADDR:
668 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
669 vui->hw_if_index, msg.state.index);
671 if (msg.state.index >= vui->num_qid)
673 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
674 " %u >= %u", msg.state.index, vui->num_qid);
678 if (msg.size < sizeof (msg.addr))
680 vu_log_debug (vui, "vhost message is too short (%d < %d)",
681 msg.size, sizeof (msg.addr));
685 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
686 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
687 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
689 if ((desc == NULL) || (used == NULL) || (avail == NULL))
691 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
696 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
697 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
698 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
700 vlib_worker_thread_barrier_sync (vm);
701 vui->vrings[msg.state.index].desc = desc;
702 vui->vrings[msg.state.index].used = used;
703 vui->vrings[msg.state.index].avail = avail;
705 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
706 vui->vrings[msg.state.index].log_used =
707 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
709 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
710 the ring is initialized in an enabled state. */
711 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
712 vui->vrings[msg.state.index].enabled = 1;
714 vui->vrings[msg.state.index].last_used_idx =
715 vui->vrings[msg.state.index].last_avail_idx =
716 vui->vrings[msg.state.index].used->idx;
717 vui->vrings[msg.state.index].last_kick =
718 vui->vrings[msg.state.index].last_used_idx;
720 /* tell driver that we want interrupts or not */
721 vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]);
722 vlib_worker_thread_barrier_release (vm);
723 vhost_user_update_iface_state (vui);
726 case VHOST_USER_SET_OWNER:
727 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
730 case VHOST_USER_RESET_OWNER:
731 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
735 case VHOST_USER_SET_VRING_CALL:
736 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
737 vui->hw_if_index, msg.u64);
739 q = (u8) (msg.u64 & 0xFF);
740 if (vui->num_qid > q)
742 /* if there is old fd, delete and close it */
743 if (vui->vrings[q].callfd_idx != ~0)
745 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
746 vui->vrings[q].callfd_idx);
747 clib_file_del (&file_main, uf);
748 vui->vrings[q].callfd_idx = ~0;
751 else if (vec_len (vui->vrings) > q)
753 /* grow vrings by pair (RX + TX) */
754 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
758 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
761 * Double the array size if it is less than 64 entries.
762 * Slow down thereafter.
764 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
765 new_max_q = vec_len (vui->vrings) << 1;
767 new_max_q = vec_len (vui->vrings) +
768 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
769 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
770 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
772 /* sync with the worker threads, vrings may move due to realloc */
773 vlib_worker_thread_barrier_sync (vm);
774 vec_validate_aligned (vui->vrings, new_max_q - 1,
775 CLIB_CACHE_LINE_BYTES);
776 vlib_worker_thread_barrier_release (vm);
778 for (i = old_max_q; i < vec_len (vui->vrings); i++)
779 vhost_user_vring_init (vui, i);
781 /* grow vrings by pair (RX + TX) */
782 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
785 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
787 if (number_of_fds != 1)
789 vu_log_debug (vui, "More than one fd received !");
793 template.read_function = vhost_user_callfd_read_ready;
794 template.file_descriptor = fds[0];
795 template.private_data =
796 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
797 template.description = format (0, "vhost user");
798 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
801 vui->vrings[q].callfd_idx = ~0;
804 case VHOST_USER_SET_VRING_KICK:
805 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
806 vui->hw_if_index, msg.u64);
808 q = (u8) (msg.u64 & 0xFF);
809 if (q >= vui->num_qid)
811 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
812 " %u >= %u", q, vui->num_qid);
816 if (vui->vrings[q].kickfd_idx != ~0)
818 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
819 vui->vrings[q].kickfd_idx);
820 clib_file_del (&file_main, uf);
821 vui->vrings[q].kickfd_idx = ~0;
824 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
826 if (number_of_fds != 1)
828 vu_log_debug (vui, "More than one fd received !");
832 template.read_function = vhost_user_kickfd_read_ready;
833 template.file_descriptor = fds[0];
834 template.private_data =
835 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
837 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
841 //When no kickfd is set, the queue is initialized as started
842 vui->vrings[q].kickfd_idx = ~0;
843 vui->vrings[q].started = 1;
844 vhost_user_thread_placement (vui, q);
846 vhost_user_update_iface_state (vui);
849 case VHOST_USER_SET_VRING_ERR:
850 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
851 vui->hw_if_index, msg.u64);
853 q = (u8) (msg.u64 & 0xFF);
854 if (q >= vui->num_qid)
856 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
857 " %u >= %u", q, vui->num_qid);
861 if (vui->vrings[q].errfd != -1)
862 close (vui->vrings[q].errfd);
864 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
866 if (number_of_fds != 1)
869 vui->vrings[q].errfd = fds[0];
872 vui->vrings[q].errfd = -1;
875 case VHOST_USER_SET_VRING_BASE:
877 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
878 vui->hw_if_index, msg.state.index, msg.state.num);
879 if (msg.state.index >= vui->num_qid)
881 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
882 " %u >= %u", msg.state.index, vui->num_qid);
885 vlib_worker_thread_barrier_sync (vm);
886 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
887 if (vhost_user_is_packed_ring_supported (vui))
891 * 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
892 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
893 * | last avail idx | | last used idx | |
894 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897 * avail wrap counter used wrap counter
899 /* last avail idx at bit 0-14. */
900 vui->vrings[msg.state.index].last_avail_idx =
901 msg.state.num & 0x7fff;
902 /* avail wrap counter at bit 15 */
903 vui->vrings[msg.state.index].avail_wrap_counter =
904 ! !(msg.state.num & (1 << 15));
907 * Although last_used_idx is passed in the upper 16 bits in qemu
908 * implementation, in practice, last_avail_idx and last_used_idx are
909 * usually the same. As a result, DPDK does not bother to pass us
910 * last_used_idx. The spec is not clear on thex coding. I figured it
911 * out by reading the qemu code. So let's just read last_avail_idx
912 * and set last_used_idx equals to last_avail_idx.
914 vui->vrings[msg.state.index].last_used_idx =
915 vui->vrings[msg.state.index].last_avail_idx;
916 vui->vrings[msg.state.index].last_kick =
917 vui->vrings[msg.state.index].last_used_idx;
918 vui->vrings[msg.state.index].used_wrap_counter =
919 vui->vrings[msg.state.index].avail_wrap_counter;
921 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
922 vui->vrings[msg.state.index].avail_wrap_counter =
925 vlib_worker_thread_barrier_release (vm);
928 case VHOST_USER_GET_VRING_BASE:
929 if (msg.state.index >= vui->num_qid)
931 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
932 " %u >= %u", msg.state.index, vui->num_qid);
936 /* protection is needed to prevent rx/tx from changing last_avail_idx */
937 vlib_worker_thread_barrier_sync (vm);
939 * Copy last_avail_idx from the vring before closing it because
940 * closing the vring also initializes the vring last_avail_idx
942 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
943 if (vhost_user_is_packed_ring_supported (vui))
946 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
947 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
949 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
950 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
953 msg.size = sizeof (msg.state);
956 * Spec says: Client must [...] stop ring upon receiving
957 * VHOST_USER_GET_VRING_BASE
959 vhost_user_vring_close (vui, msg.state.index);
960 vlib_worker_thread_barrier_release (vm);
962 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
963 vui->hw_if_index, msg.state.index, msg.state.num);
965 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
966 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
968 vu_log_debug (vui, "could not send message response");
971 vhost_user_update_iface_state (vui);
974 case VHOST_USER_NONE:
975 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
978 case VHOST_USER_SET_LOG_BASE:
979 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
982 if (msg.size != sizeof (msg.log))
984 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
985 " %d instead of %d", msg.size, sizeof (msg.log));
989 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
991 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
992 "VHOST_USER_SET_LOG_BASE received");
997 /* align size to page */
998 long page_sz = get_huge_page_size (fd);
1000 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
1002 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1005 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
1006 "mapped 0x%lx", map_sz, msg.log.offset, fd,
1009 if (log_base_addr == MAP_FAILED)
1011 vu_log_err (vui, "failed to map memory. errno is %d", errno);
1015 vlib_worker_thread_barrier_sync (vm);
1016 vui->log_base_addr = log_base_addr;
1017 vui->log_base_addr += msg.log.offset;
1018 vui->log_size = msg.log.size;
1019 vlib_worker_thread_barrier_release (vm);
1022 msg.size = sizeof (msg.u64);
1024 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1025 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1027 vu_log_debug (vui, "could not send message response");
1032 case VHOST_USER_SET_LOG_FD:
1033 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1036 case VHOST_USER_GET_PROTOCOL_FEATURES:
1038 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1039 (1 << VHOST_USER_PROTOCOL_F_MQ);
1040 msg.size = sizeof (msg.u64);
1041 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1042 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1044 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1045 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1047 vu_log_debug (vui, "could not send message response");
1052 case VHOST_USER_SET_PROTOCOL_FEATURES:
1053 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1054 "features 0x%016llx", vui->hw_if_index, msg.u64);
1055 vui->protocol_features = msg.u64;
1058 case VHOST_USER_GET_QUEUE_NUM:
1060 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1061 msg.size = sizeof (msg.u64);
1062 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1063 vui->hw_if_index, msg.u64);
1065 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1066 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1068 vu_log_debug (vui, "could not send message response");
1073 case VHOST_USER_SET_VRING_ENABLE:
1074 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1075 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1077 if (msg.state.index >= vui->num_qid)
1079 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1080 " %u >= %u", msg.state.index, vui->num_qid);
1084 vui->vrings[msg.state.index].enabled = msg.state.num;
1085 vhost_user_thread_placement (vui, msg.state.index);
1086 vhost_user_update_iface_state (vui);
1090 vu_log_debug (vui, "unknown vhost-user message %d received. "
1091 "closing socket", msg.request);
1098 vlib_worker_thread_barrier_sync (vm);
1099 vhost_user_if_disconnect (vui);
1100 vlib_worker_thread_barrier_release (vm);
1101 vhost_user_update_iface_state (vui);
1105 static clib_error_t *
1106 vhost_user_socket_error (clib_file_t * uf)
1108 vlib_main_t *vm = vlib_get_main ();
1109 vhost_user_main_t *vum = &vhost_user_main;
1110 vhost_user_intf_t *vui =
1111 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1113 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1114 vlib_worker_thread_barrier_sync (vm);
1115 vhost_user_if_disconnect (vui);
1116 vlib_worker_thread_barrier_release (vm);
1120 static clib_error_t *
1121 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1123 int client_fd, client_len;
1124 struct sockaddr_un client;
1125 clib_file_t template = { 0 };
1126 vhost_user_main_t *vum = &vhost_user_main;
1127 vhost_user_intf_t *vui;
1129 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1131 client_len = sizeof (client);
1132 client_fd = accept (uf->file_descriptor,
1133 (struct sockaddr *) &client,
1134 (socklen_t *) & client_len);
1137 return clib_error_return_unix (0, "accept");
1139 if (vui->clib_file_index != ~0)
1141 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1142 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1143 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1146 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1147 vui->sw_if_index, client_fd);
1148 template.read_function = vhost_user_socket_read;
1149 template.error_function = vhost_user_socket_error;
1150 template.file_descriptor = client_fd;
1151 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1152 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1153 vui->clib_file_index = clib_file_add (&file_main, &template);
1158 static clib_error_t *
1159 vhost_user_init (vlib_main_t * vm)
1161 vhost_user_main_t *vum = &vhost_user_main;
1162 vlib_thread_main_t *tm = vlib_get_thread_main ();
1164 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1166 vum->coalesce_frames = 32;
1167 vum->coalesce_time = 1e-3;
1169 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1172 vec_foreach (cpu, vum->cpus)
1174 /* This is actually not necessary as validate already zeroes it
1175 * Just keeping the loop here for later because I am lazy. */
1176 cpu->rx_buffers_len = 0;
1179 vum->random = random_default_seed ();
1181 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1187 VLIB_INIT_FUNCTION (vhost_user_init) =
1189 .runs_after = VLIB_INITS("ip4_init"),
1194 vhost_user_send_interrupt_process (vlib_main_t * vm,
1195 vlib_node_runtime_t * rt, vlib_frame_t * f)
1197 vhost_user_intf_t *vui;
1198 f64 timeout = 3153600000.0 /* 100 years */ ;
1199 uword event_type, *event_data = 0;
1200 vhost_user_main_t *vum = &vhost_user_main;
1202 f64 now, poll_time_remaining;
1208 poll_time_remaining =
1209 vlib_process_wait_for_event_or_clock (vm, timeout);
1210 event_type = vlib_process_get_events (vm, &event_data);
1211 vec_reset_length (event_data);
1214 * Use the remaining timeout if it is less than coalesce time to avoid
1215 * resetting the existing timer in the middle of expiration
1217 timeout = poll_time_remaining;
1218 if (vlib_process_suspend_time_is_zero (timeout) ||
1219 (timeout > vum->coalesce_time))
1220 timeout = vum->coalesce_time;
1222 now = vlib_time_now (vm);
1225 case VHOST_USER_EVENT_STOP_TIMER:
1229 case VHOST_USER_EVENT_START_TIMER:
1232 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1238 pool_foreach (vui, vum->vhost_user_interfaces) {
1239 next_timeout = timeout;
1240 FOR_ALL_VHOST_RX_TXQ (qid, vui)
1242 vhost_user_vring_t *vq = &vui->vrings[qid];
1244 if (vq->started == 0)
1246 if (vq->n_since_last_int)
1248 if (now >= vq->int_deadline)
1249 vhost_user_send_call (vm, vui, vq);
1251 next_timeout = vq->int_deadline - now;
1254 if ((next_timeout < timeout) && (next_timeout > 0.0))
1255 timeout = next_timeout;
1262 clib_warning ("BUG: unhandled event type %d", event_type);
1265 /* No less than 1 millisecond */
1269 timeout = 3153600000.0;
1275 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1276 .function = vhost_user_send_interrupt_process,
1277 .type = VLIB_NODE_TYPE_PROCESS,
1278 .name = "vhost-user-send-interrupt-process",
1283 vhost_user_process (vlib_main_t * vm,
1284 vlib_node_runtime_t * rt, vlib_frame_t * f)
1286 vhost_user_main_t *vum = &vhost_user_main;
1287 vhost_user_intf_t *vui;
1288 struct sockaddr_un sun;
1290 clib_file_t template = { 0 };
1291 f64 timeout = 3153600000.0 /* 100 years */ ;
1292 uword *event_data = 0;
1295 sun.sun_family = AF_UNIX;
1296 template.read_function = vhost_user_socket_read;
1297 template.error_function = vhost_user_socket_error;
1301 vlib_process_wait_for_event_or_clock (vm, timeout);
1302 vlib_process_get_events (vm, &event_data);
1303 vec_reset_length (event_data);
1308 pool_foreach (vui, vum->vhost_user_interfaces) {
1310 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1311 if (vui->clib_file_index == ~0)
1314 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1317 * 1st time error or new error for this interface,
1318 * spit out the message and record the error
1320 if (!vui->sock_errno || (vui->sock_errno != errno))
1323 ("Error: Could not open unix socket for %s",
1324 vui->sock_filename);
1325 vui->sock_errno = errno;
1330 /* try to connect */
1331 strncpy (sun.sun_path, (char *) vui->sock_filename,
1332 sizeof (sun.sun_path) - 1);
1333 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1335 /* Avoid hanging VPP if the other end does not accept */
1336 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1337 clib_unix_warning ("fcntl");
1339 if (connect (sockfd, (struct sockaddr *) &sun,
1340 sizeof (struct sockaddr_un)) == 0)
1342 /* Set the socket to blocking as it was before */
1343 if (fcntl(sockfd, F_SETFL, 0) < 0)
1344 clib_unix_warning ("fcntl2");
1346 vui->sock_errno = 0;
1347 template.file_descriptor = sockfd;
1348 template.private_data =
1349 vui - vhost_user_main.vhost_user_interfaces;
1350 template.description = format (0, "vhost user process");
1351 vui->clib_file_index = clib_file_add (&file_main, &template);
1354 /* This sockfd is considered consumed */
1359 vui->sock_errno = errno;
1364 /* check if socket is alive */
1366 socklen_t len = sizeof (error);
1367 int fd = UNIX_GET_FD(vui->clib_file_index);
1369 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1373 vu_log_debug (vui, "getsockopt returned %d", retval);
1374 vhost_user_if_disconnect (vui);
1385 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1386 .function = vhost_user_process,
1387 .type = VLIB_NODE_TYPE_PROCESS,
1388 .name = "vhost-user-process",
1393 * Disables and reset interface structure.
1394 * It can then be either init again, or removed from used interfaces.
1397 vhost_user_term_if (vhost_user_intf_t * vui)
1400 vhost_user_main_t *vum = &vhost_user_main;
1402 // disconnect interface sockets
1403 vhost_user_if_disconnect (vui);
1404 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1405 vhost_user_update_iface_state (vui);
1407 FOR_ALL_VHOST_RX_TXQ (q, vui)
1409 clib_spinlock_free (&vui->vrings[q].vring_lock);
1412 if (vui->unix_server_index != ~0)
1414 //Close server socket
1415 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1416 vui->unix_server_index);
1417 clib_file_del (&file_main, uf);
1418 vui->unix_server_index = ~0;
1419 unlink (vui->sock_filename);
1422 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1427 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1429 vhost_user_main_t *vum = &vhost_user_main;
1430 vhost_user_intf_t *vui;
1432 vnet_hw_interface_t *hwif;
1437 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1438 || hwif->dev_class_index != vhost_user_device_class.index)
1439 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1441 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1443 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1444 hwif->name, hwif->dev_instance);
1446 FOR_ALL_VHOST_TXQ (qid, vui)
1448 vhost_user_vring_t *txvq = &vui->vrings[qid];
1450 if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) &&
1451 (txvq->thread_index != ~0))
1453 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
1454 ASSERT (cpu->polling_q_count != 0);
1455 cpu->polling_q_count--;
1458 if ((vum->ifq_count > 0) &&
1459 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1460 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1463 // Stop the timer if there is no more interrupt interface/queue
1464 if (vum->ifq_count == 0)
1466 vlib_process_signal_event (vm,
1467 vhost_user_send_interrupt_node.index,
1468 VHOST_USER_EVENT_STOP_TIMER, 0);
1474 // Disable and reset interface
1475 vhost_user_term_if (vui);
1477 // Reset renumbered iface
1478 if (hwif->dev_instance <
1479 vec_len (vum->show_dev_instance_by_real_dev_instance))
1480 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1482 // Delete ethernet interface
1483 ethernet_delete_interface (vnm, vui->hw_if_index);
1486 vec_free (vui->vrings);
1489 pool_put (vum->vhost_user_interfaces, vui);
1494 static clib_error_t *
1495 vhost_user_exit (vlib_main_t * vm)
1497 vnet_main_t *vnm = vnet_get_main ();
1498 vhost_user_main_t *vum = &vhost_user_main;
1499 vhost_user_intf_t *vui;
1501 vlib_worker_thread_barrier_sync (vlib_get_main ());
1503 pool_foreach (vui, vum->vhost_user_interfaces) {
1504 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1507 vlib_worker_thread_barrier_release (vlib_get_main ());
1511 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1514 * Open server unix socket on specified sock_filename.
1517 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1520 struct sockaddr_un un = { };
1522 /* create listening socket */
1523 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1524 return VNET_API_ERROR_SYSCALL_ERROR_1;
1526 un.sun_family = AF_UNIX;
1527 strncpy ((char *) un.sun_path, (char *) sock_filename,
1528 sizeof (un.sun_path) - 1);
1530 /* remove if exists */
1531 unlink ((char *) sock_filename);
1533 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1535 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1539 if (listen (fd, 1) == -1)
1541 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1554 * Create ethernet interface for vhost user interface.
1557 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1558 vhost_user_intf_t *vui,
1559 vhost_user_create_if_args_t *args)
1561 vhost_user_main_t *vum = &vhost_user_main;
1563 clib_error_t *error;
1565 /* create hw and sw interface */
1566 if (args->use_custom_mac)
1568 clib_memcpy (hwaddr, args->hwaddr, 6);
1572 random_u32 (&vum->random);
1573 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1578 error = ethernet_register_interface
1580 vhost_user_device_class.index,
1581 vui - vum->vhost_user_interfaces /* device instance */ ,
1582 hwaddr /* ethernet address */ ,
1583 &vui->hw_if_index, 0 /* flag change */ );
1586 clib_error_report (error);
1590 * Initialize vui with specified attributes
1593 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1594 int server_sock_fd, vhost_user_create_if_args_t * args,
1597 vnet_sw_interface_t *sw;
1599 vhost_user_main_t *vum = &vhost_user_main;
1600 vnet_hw_interface_t *hw;
1602 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1603 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1604 if (server_sock_fd != -1)
1606 clib_file_t template = { 0 };
1607 template.read_function = vhost_user_socksvr_accept_ready;
1608 template.file_descriptor = server_sock_fd;
1609 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1610 template.description = format (0, "vhost user %d", sw);
1611 vui->unix_server_index = clib_file_add (&file_main, &template);
1615 vui->unix_server_index = ~0;
1618 vui->sw_if_index = sw->sw_if_index;
1619 strncpy (vui->sock_filename, args->sock_filename,
1620 ARRAY_LEN (vui->sock_filename) - 1);
1621 vui->sock_errno = 0;
1623 vui->feature_mask = args->feature_mask;
1624 vui->clib_file_index = ~0;
1625 vui->log_base_addr = 0;
1626 vui->if_index = vui - vum->vhost_user_interfaces;
1627 vui->enable_gso = args->enable_gso;
1628 vui->enable_event_idx = args->enable_event_idx;
1629 vui->enable_packed = args->enable_packed;
1631 * enable_gso takes precedence over configurable feature mask if there
1633 * if feature mask disables gso, but enable_gso is configured,
1634 * then gso is enable
1635 * if feature mask enables gso, but enable_gso is not configured,
1636 * then gso is enable
1638 * if gso is enable via feature mask, it must enable both host and guest
1639 * gso feature mask, we don't support one sided GSO or partial GSO.
1641 if ((vui->enable_gso == 0) &&
1642 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1643 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1644 vui->enable_gso = 1;
1645 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1646 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1649 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1650 CLIB_CACHE_LINE_BYTES);
1652 for (q = 0; q < vec_len (vui->vrings); q++)
1653 vhost_user_vring_init (vui, q);
1655 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
1656 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1659 *sw_if_index = vui->sw_if_index;
1661 vec_validate (vui->per_cpu_tx_qid,
1662 vlib_get_thread_main ()->n_vlib_mains - 1);
1663 vhost_user_tx_thread_placement (vui);
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;
2136 int show_verbose = 0;
2142 struct feat_struct *feat_entry;
2144 static struct feat_struct feat_array[] = {
2145 #define _(s,b) { .str = #s, .bit = b, },
2146 foreach_virtio_net_features
2151 #define foreach_protocol_feature \
2152 _(VHOST_USER_PROTOCOL_F_MQ) \
2153 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2155 static struct feat_struct proto_feat_array[] = {
2156 #define _(s) { .str = #s, .bit = s},
2157 foreach_protocol_feature
2162 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2165 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2167 hi = vnet_get_hw_interface (vnm, hw_if_index);
2168 if (vhost_user_device_class.index != hi->dev_class_index)
2170 error = clib_error_return (0, "unknown input `%U'",
2171 format_unformat_error, input);
2174 vec_add1 (hw_if_indices, hw_if_index);
2176 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2178 else if (unformat (input, "verbose"))
2182 error = clib_error_return (0, "unknown input `%U'",
2183 format_unformat_error, input);
2187 if (vec_len (hw_if_indices) == 0)
2189 pool_foreach (vui, vum->vhost_user_interfaces)
2190 vec_add1 (hw_if_indices, vui->hw_if_index);
2192 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2193 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2194 vum->coalesce_frames, vum->coalesce_time);
2195 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2197 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2198 for (u32 tid = 0; tid <= vlib_num_workers (); tid++)
2200 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid);
2201 vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid,
2202 cpu->polling_q_count);
2205 for (i = 0; i < vec_len (hw_if_indices); i++)
2207 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2208 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2209 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2210 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2212 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2213 if (vui->enable_gso)
2214 vlib_cli_output (vm, " GSO enable");
2215 if (vui->enable_packed)
2216 vlib_cli_output (vm, " Packed ring enable");
2217 if (vui->enable_event_idx)
2218 vlib_cli_output (vm, " Event index enable");
2220 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2221 " features mask (0x%llx): \n"
2222 " features (0x%llx): \n",
2223 vui->virtio_net_hdr_sz, vui->feature_mask,
2226 feat_entry = (struct feat_struct *) &feat_array;
2227 while (feat_entry->str)
2229 if (vui->features & (1ULL << feat_entry->bit))
2230 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2235 vlib_cli_output (vm, " protocol features (0x%llx)",
2236 vui->protocol_features);
2237 feat_entry = (struct feat_struct *) &proto_feat_array;
2238 while (feat_entry->str)
2240 if (vui->protocol_features & (1ULL << feat_entry->bit))
2241 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2246 vlib_cli_output (vm, "\n");
2248 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2250 (vui->unix_server_index != ~0) ? "server" : "client",
2251 strerror (vui->sock_errno));
2253 vlib_cli_output (vm, " rx placement: ");
2255 FOR_ALL_VHOST_TXQ (qid, vui)
2257 vhost_user_vring_t *txvq = &vui->vrings[qid];
2259 if (txvq->qid == -1)
2261 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2262 txvq->thread_index, qid, format_vnet_hw_if_rx_mode,
2266 vlib_cli_output (vm, " tx placement: %s\n",
2267 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2269 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2271 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2272 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2275 vlib_cli_output (vm, "\n");
2277 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2281 vlib_cli_output (vm,
2282 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2283 vlib_cli_output (vm,
2284 " ====== ===== ================== ================== ================== ================== ==================\n");
2286 for (j = 0; j < vui->nregions; j++)
2288 vlib_cli_output (vm,
2289 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2290 j, vui->region_mmap_fd[j],
2291 vui->regions[j].guest_phys_addr,
2292 vui->regions[j].memory_size,
2293 vui->regions[j].userspace_addr,
2294 vui->regions[j].mmap_offset,
2295 pointer_to_uword (vui->region_mmap_addr[j]));
2297 FOR_ALL_VHOST_RX_TXQ (q, vui)
2299 if (!vui->vrings[q].started)
2302 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2303 (q & 1) ? "RX" : "TX",
2304 vui->vrings[q].enabled ? "" : " disabled");
2306 vlib_cli_output (vm, " global RX queue index %u\n",
2307 vui->vrings[q].queue_index);
2311 " qsz %d last_avail_idx %d last_used_idx %d"
2313 vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx,
2314 vui->vrings[q].last_used_idx, vui->vrings[q].last_kick);
2316 if (vhost_user_is_packed_ring_supported (vui))
2317 vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose);
2319 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2321 vlib_cli_output (vm, "\n");
2324 vec_free (hw_if_indices);
2333 * Create a vHost User interface. Once created, a new virtual interface
2334 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2335 * is the next free index.
2337 * There are several parameters associated with a vHost interface:
2339 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2340 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2341 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2342 * hypervisor will create the socket if it does not already exist. The VPP code
2343 * is indifferent to the file location. However, if SELinux is enabled, then the
2344 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2346 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2347 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2348 * mode, the VM can be reset without tearing down the vHost Interface. In
2349 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2350 * tearing down the vHost Interface.
2352 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2353 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2354 * parameter not be used except by experienced users. By default, all supported
2355 * features will be advertised. Otherwise, provide the set of features desired.
2356 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2357 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2358 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2359 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2360 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2361 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2362 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2363 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2364 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2366 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2367 * X:X:X:X:X:X unix or X.X.X cisco format.
2369 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2370 * in the name to be specified. If instance already exists, name will be used
2371 * anyway and multiple instances will have the same name. Use with caution.
2374 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2375 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2376 * VirtualEthernet0/0/0
2378 * Example of how to create a vhost interface with VPP as the server and with just
2379 * multiple queues enabled:
2380 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2381 * VirtualEthernet0/0/1
2383 * Once the vHost interface is created, enable the interface using:
2384 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2387 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2388 .path = "create vhost-user",
2389 .short_help = "create vhost-user socket <socket-filename> [server] "
2390 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2391 "[packed] [event-idx]",
2392 .function = vhost_user_connect_command_fn,
2398 * Delete a vHost User interface using the interface name or the
2399 * software interface index. Use the '<em>show interface</em>'
2400 * command to determine the software interface index. On deletion,
2401 * the linux socket will not be deleted.
2404 * Example of how to delete a vhost interface by name:
2405 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2406 * Example of how to delete a vhost interface by software interface index:
2407 * @cliexcmd{delete vhost-user sw_if_index 1}
2410 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2411 .path = "delete vhost-user",
2412 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2413 .function = vhost_user_delete_command_fn,
2417 * Display the attributes of a single vHost User interface (provide interface
2418 * name), multiple vHost User interfaces (provide a list of interface names seperated
2419 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2420 * vHost interfaces).
2424 * Example of how to display a vhost interface:
2425 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2426 * Virtio vhost-user interfaces
2428 * coalesce frames 32 time 1e-3
2429 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2430 * virtio_net_hdr_sz 12
2431 * features mask (0xffffffffffffffff):
2432 * features (0x50408000):
2433 * VIRTIO_NET_F_MRG_RXBUF (15)
2434 * VIRTIO_NET_F_MQ (22)
2435 * VIRTIO_F_INDIRECT_DESC (28)
2436 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2437 * protocol features (0x3)
2438 * VHOST_USER_PROTOCOL_F_MQ (0)
2439 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2441 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2444 * thread 1 on vring 1
2445 * thread 1 on vring 5
2446 * thread 2 on vring 3
2447 * thread 2 on vring 7
2448 * tx placement: spin-lock
2449 * thread 0 on vring 0
2450 * thread 1 on vring 2
2451 * thread 2 on vring 0
2453 * Memory regions (total 2)
2454 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2455 * ====== ===== ================== ================== ================== ================== ==================
2456 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2457 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2460 * qsz 256 last_avail_idx 0 last_used_idx 0
2461 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2462 * kickfd 62 callfd 64 errfd -1
2465 * qsz 256 last_avail_idx 0 last_used_idx 0
2466 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2467 * kickfd 65 callfd 66 errfd -1
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 63 callfd 70 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 72 callfd 74 errfd -1
2479 * Virtqueue 4 (TX disabled)
2480 * qsz 256 last_avail_idx 0 last_used_idx 0
2481 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2482 * kickfd 76 callfd 78 errfd -1
2484 * Virtqueue 5 (RX disabled)
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 80 callfd 82 errfd -1
2489 * Virtqueue 6 (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 84 callfd 86 errfd -1
2494 * Virtqueue 7 (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 88 callfd 90 errfd -1
2501 * The optional '<em>descriptors</em>' parameter will display the same output as
2502 * the previous example but will include the descriptor table for each queue.
2503 * The output is truncated below:
2504 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2505 * Virtio vhost-user interfaces
2507 * coalesce frames 32 time 1e-3
2508 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2509 * virtio_net_hdr_sz 12
2510 * features mask (0xffffffffffffffff):
2511 * features (0x50408000):
2512 * VIRTIO_NET_F_MRG_RXBUF (15)
2513 * VIRTIO_NET_F_MQ (22)
2516 * qsz 256 last_avail_idx 0 last_used_idx 0
2517 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2518 * kickfd 62 callfd 64 errfd -1
2521 * id addr len flags next user_addr
2522 * ===== ================== ===== ====== ===== ==================
2523 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2524 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2525 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2526 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2527 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2528 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2529 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2530 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2531 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2532 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2533 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2535 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2536 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2537 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2538 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2539 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2540 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2541 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2544 * qsz 256 last_avail_idx 0 last_used_idx 0
2550 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2551 .path = "show vhost-user",
2552 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2553 "[[descriptors] [verbose]]",
2554 .function = show_vhost_user_command_fn,
2559 static clib_error_t *
2560 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2562 vhost_user_main_t *vum = &vhost_user_main;
2564 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2566 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2568 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2570 else if (unformat (input, "dont-dump-memory"))
2571 vum->dont_dump_vhost_user_memory = 1;
2573 return clib_error_return (0, "unknown input `%U'",
2574 format_unformat_error, input);
2580 /* vhost-user { ... } configuration. */
2581 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2584 vhost_user_unmap_all (void)
2586 vhost_user_main_t *vum = &vhost_user_main;
2587 vhost_user_intf_t *vui;
2589 if (vum->dont_dump_vhost_user_memory)
2591 pool_foreach (vui, vum->vhost_user_interfaces)
2592 unmap_all_mem_regions (vui);
2597 * fd.io coding-style-patch-verification: ON
2600 * eval: (c-set-style "gnu")