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_INTERFACE_CAP_SUPPORTS_TCP_GSO |
562 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
563 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM);
567 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
568 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
570 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
572 vhost_user_update_iface_state (vui);
575 case VHOST_USER_SET_MEM_TABLE:
576 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
577 vui->hw_if_index, msg.memory.nregions);
579 if ((msg.memory.nregions < 1) ||
580 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
582 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
583 VHOST_MEMORY_MAX_NREGIONS);
587 if (msg.memory.nregions != number_of_fds)
589 vu_log_debug (vui, "each memory region must have FD");
593 /* Do the mmap without barrier sync */
594 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
595 for (i = 0; i < msg.memory.nregions; i++)
597 long page_sz = get_huge_page_size (fds[i]);
599 /* align size to page */
600 ssize_t map_sz = (msg.memory.regions[i].memory_size +
601 msg.memory.regions[i].mmap_offset +
602 page_sz - 1) & ~(page_sz - 1);
604 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
605 MAP_SHARED, fds[i], 0);
606 if (region_mmap_addr[i] == MAP_FAILED)
608 vu_log_err (vui, "failed to map memory. errno is %d", errno);
609 for (j = 0; j < i; j++)
610 munmap (region_mmap_addr[j], map_sz);
613 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
614 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
615 region_mmap_addr[i], page_sz);
618 vlib_worker_thread_barrier_sync (vm);
619 unmap_all_mem_regions (vui);
620 for (i = 0; i < msg.memory.nregions; i++)
622 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
623 sizeof (vhost_user_memory_region_t));
625 vui->region_mmap_addr[i] = region_mmap_addr[i];
626 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
627 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
628 vui->regions[i].memory_size;
630 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
631 vui->region_mmap_fd[i] = fds[i];
637 * Re-compute desc, used, and avail descriptor table if vring address
640 FOR_ALL_VHOST_RX_TXQ (q, vui)
642 if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr &&
643 vui->vrings[q].avail_user_addr)
645 vui->vrings[q].desc =
646 map_user_mem (vui, vui->vrings[q].desc_user_addr);
647 vui->vrings[q].used =
648 map_user_mem (vui, vui->vrings[q].used_user_addr);
649 vui->vrings[q].avail =
650 map_user_mem (vui, vui->vrings[q].avail_user_addr);
653 vlib_worker_thread_barrier_release (vm);
656 case VHOST_USER_SET_VRING_NUM:
657 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
658 vui->hw_if_index, msg.state.index, msg.state.num);
660 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
661 (msg.state.num == 0) || /* it cannot be zero */
662 ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
663 (msg.state.index >= vui->num_qid))
665 vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
666 " %d, msg.state.index %d, curruent max q %d",
667 msg.state.num, msg.state.index, vui->num_qid);
670 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
673 case VHOST_USER_SET_VRING_ADDR:
674 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
675 vui->hw_if_index, msg.state.index);
677 if (msg.state.index >= vui->num_qid)
679 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
680 " %u >= %u", msg.state.index, vui->num_qid);
684 if (msg.size < sizeof (msg.addr))
686 vu_log_debug (vui, "vhost message is too short (%d < %d)",
687 msg.size, sizeof (msg.addr));
691 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
692 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
693 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
695 if ((desc == NULL) || (used == NULL) || (avail == NULL))
697 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
702 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
703 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
704 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
706 vlib_worker_thread_barrier_sync (vm);
707 vui->vrings[msg.state.index].desc = desc;
708 vui->vrings[msg.state.index].used = used;
709 vui->vrings[msg.state.index].avail = avail;
711 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
712 vui->vrings[msg.state.index].log_used =
713 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
715 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
716 the ring is initialized in an enabled state. */
717 if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
718 vui->vrings[msg.state.index].enabled = 1;
720 vui->vrings[msg.state.index].last_used_idx =
721 vui->vrings[msg.state.index].last_avail_idx =
722 vui->vrings[msg.state.index].used->idx;
723 vui->vrings[msg.state.index].last_kick =
724 vui->vrings[msg.state.index].last_used_idx;
726 /* tell driver that we want interrupts or not */
727 vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]);
728 vlib_worker_thread_barrier_release (vm);
729 vhost_user_update_iface_state (vui);
732 case VHOST_USER_SET_OWNER:
733 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
736 case VHOST_USER_RESET_OWNER:
737 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
741 case VHOST_USER_SET_VRING_CALL:
742 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
743 vui->hw_if_index, msg.u64);
745 q = (u8) (msg.u64 & 0xFF);
746 if (vui->num_qid > q)
748 /* if there is old fd, delete and close it */
749 if (vui->vrings[q].callfd_idx != ~0)
751 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
752 vui->vrings[q].callfd_idx);
753 clib_file_del (&file_main, uf);
754 vui->vrings[q].callfd_idx = ~0;
757 else if (vec_len (vui->vrings) > q)
759 /* grow vrings by pair (RX + TX) */
760 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
764 u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
767 * Double the array size if it is less than 64 entries.
768 * Slow down thereafter.
770 if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
771 new_max_q = vec_len (vui->vrings) << 1;
773 new_max_q = vec_len (vui->vrings) +
774 (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
775 if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
776 new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
778 /* sync with the worker threads, vrings may move due to realloc */
779 vlib_worker_thread_barrier_sync (vm);
780 vec_validate_aligned (vui->vrings, new_max_q - 1,
781 CLIB_CACHE_LINE_BYTES);
782 vlib_worker_thread_barrier_release (vm);
784 for (i = old_max_q; i < vec_len (vui->vrings); i++)
785 vhost_user_vring_init (vui, i);
787 /* grow vrings by pair (RX + TX) */
788 vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
791 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
793 if (number_of_fds != 1)
795 vu_log_debug (vui, "More than one fd received !");
799 template.read_function = vhost_user_callfd_read_ready;
800 template.file_descriptor = fds[0];
801 template.private_data =
802 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
803 template.description = format (0, "vhost user");
804 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
807 vui->vrings[q].callfd_idx = ~0;
810 case VHOST_USER_SET_VRING_KICK:
811 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
812 vui->hw_if_index, msg.u64);
814 q = (u8) (msg.u64 & 0xFF);
815 if (q >= vui->num_qid)
817 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
818 " %u >= %u", q, vui->num_qid);
822 if (vui->vrings[q].kickfd_idx != ~0)
824 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
825 vui->vrings[q].kickfd_idx);
826 clib_file_del (&file_main, uf);
827 vui->vrings[q].kickfd_idx = ~0;
830 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
832 if (number_of_fds != 1)
834 vu_log_debug (vui, "More than one fd received !");
838 template.read_function = vhost_user_kickfd_read_ready;
839 template.file_descriptor = fds[0];
840 template.private_data =
841 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
843 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
847 //When no kickfd is set, the queue is initialized as started
848 vui->vrings[q].kickfd_idx = ~0;
849 vui->vrings[q].started = 1;
850 vhost_user_thread_placement (vui, q);
852 vhost_user_update_iface_state (vui);
855 case VHOST_USER_SET_VRING_ERR:
856 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
857 vui->hw_if_index, msg.u64);
859 q = (u8) (msg.u64 & 0xFF);
860 if (q >= vui->num_qid)
862 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
863 " %u >= %u", q, vui->num_qid);
867 if (vui->vrings[q].errfd != -1)
868 close (vui->vrings[q].errfd);
870 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
872 if (number_of_fds != 1)
875 vui->vrings[q].errfd = fds[0];
878 vui->vrings[q].errfd = -1;
881 case VHOST_USER_SET_VRING_BASE:
883 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
884 vui->hw_if_index, msg.state.index, msg.state.num);
885 if (msg.state.index >= vui->num_qid)
887 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
888 " %u >= %u", msg.state.index, vui->num_qid);
891 vlib_worker_thread_barrier_sync (vm);
892 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
893 if (vhost_user_is_packed_ring_supported (vui))
897 * 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
898 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
899 * | last avail idx | | last used idx | |
900 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
903 * avail wrap counter used wrap counter
905 /* last avail idx at bit 0-14. */
906 vui->vrings[msg.state.index].last_avail_idx =
907 msg.state.num & 0x7fff;
908 /* avail wrap counter at bit 15 */
909 vui->vrings[msg.state.index].avail_wrap_counter =
910 ! !(msg.state.num & (1 << 15));
913 * Although last_used_idx is passed in the upper 16 bits in qemu
914 * implementation, in practice, last_avail_idx and last_used_idx are
915 * usually the same. As a result, DPDK does not bother to pass us
916 * last_used_idx. The spec is not clear on thex coding. I figured it
917 * out by reading the qemu code. So let's just read last_avail_idx
918 * and set last_used_idx equals to last_avail_idx.
920 vui->vrings[msg.state.index].last_used_idx =
921 vui->vrings[msg.state.index].last_avail_idx;
922 vui->vrings[msg.state.index].last_kick =
923 vui->vrings[msg.state.index].last_used_idx;
924 vui->vrings[msg.state.index].used_wrap_counter =
925 vui->vrings[msg.state.index].avail_wrap_counter;
927 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
928 vui->vrings[msg.state.index].avail_wrap_counter =
931 vlib_worker_thread_barrier_release (vm);
934 case VHOST_USER_GET_VRING_BASE:
935 if (msg.state.index >= vui->num_qid)
937 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
938 " %u >= %u", msg.state.index, vui->num_qid);
942 /* protection is needed to prevent rx/tx from changing last_avail_idx */
943 vlib_worker_thread_barrier_sync (vm);
945 * Copy last_avail_idx from the vring before closing it because
946 * closing the vring also initializes the vring last_avail_idx
948 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
949 if (vhost_user_is_packed_ring_supported (vui))
952 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
953 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
955 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
956 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
959 msg.size = sizeof (msg.state);
962 * Spec says: Client must [...] stop ring upon receiving
963 * VHOST_USER_GET_VRING_BASE
965 vhost_user_vring_close (vui, msg.state.index);
966 vlib_worker_thread_barrier_release (vm);
968 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
969 vui->hw_if_index, msg.state.index, msg.state.num);
971 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
972 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
974 vu_log_debug (vui, "could not send message response");
977 vhost_user_update_iface_state (vui);
980 case VHOST_USER_NONE:
981 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
984 case VHOST_USER_SET_LOG_BASE:
985 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
988 if (msg.size != sizeof (msg.log))
990 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
991 " %d instead of %d", msg.size, sizeof (msg.log));
995 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
997 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
998 "VHOST_USER_SET_LOG_BASE received");
1003 /* align size to page */
1004 long page_sz = get_huge_page_size (fd);
1006 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
1008 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1011 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
1012 "mapped 0x%lx", map_sz, msg.log.offset, fd,
1015 if (log_base_addr == MAP_FAILED)
1017 vu_log_err (vui, "failed to map memory. errno is %d", errno);
1021 vlib_worker_thread_barrier_sync (vm);
1022 vui->log_base_addr = log_base_addr;
1023 vui->log_base_addr += msg.log.offset;
1024 vui->log_size = msg.log.size;
1025 vlib_worker_thread_barrier_release (vm);
1028 msg.size = sizeof (msg.u64);
1030 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1031 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1033 vu_log_debug (vui, "could not send message response");
1038 case VHOST_USER_SET_LOG_FD:
1039 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1042 case VHOST_USER_GET_PROTOCOL_FEATURES:
1044 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1045 (1 << VHOST_USER_PROTOCOL_F_MQ);
1046 msg.size = sizeof (msg.u64);
1047 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
1048 "reply 0x%016llx", vui->hw_if_index, msg.u64);
1050 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1051 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1053 vu_log_debug (vui, "could not send message response");
1058 case VHOST_USER_SET_PROTOCOL_FEATURES:
1059 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1060 "features 0x%016llx", vui->hw_if_index, msg.u64);
1061 vui->protocol_features = msg.u64;
1064 case VHOST_USER_GET_QUEUE_NUM:
1066 msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1067 msg.size = sizeof (msg.u64);
1068 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1069 vui->hw_if_index, msg.u64);
1071 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1072 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1074 vu_log_debug (vui, "could not send message response");
1079 case VHOST_USER_SET_VRING_ENABLE:
1080 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1081 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1083 if (msg.state.index >= vui->num_qid)
1085 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1086 " %u >= %u", msg.state.index, vui->num_qid);
1090 vui->vrings[msg.state.index].enabled = msg.state.num;
1091 vhost_user_thread_placement (vui, msg.state.index);
1092 vhost_user_update_iface_state (vui);
1096 vu_log_debug (vui, "unknown vhost-user message %d received. "
1097 "closing socket", msg.request);
1104 vlib_worker_thread_barrier_sync (vm);
1105 vhost_user_if_disconnect (vui);
1106 vlib_worker_thread_barrier_release (vm);
1107 vhost_user_update_iface_state (vui);
1111 static clib_error_t *
1112 vhost_user_socket_error (clib_file_t * uf)
1114 vlib_main_t *vm = vlib_get_main ();
1115 vhost_user_main_t *vum = &vhost_user_main;
1116 vhost_user_intf_t *vui =
1117 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1119 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1120 vlib_worker_thread_barrier_sync (vm);
1121 vhost_user_if_disconnect (vui);
1122 vlib_worker_thread_barrier_release (vm);
1126 static clib_error_t *
1127 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1129 int client_fd, client_len;
1130 struct sockaddr_un client;
1131 clib_file_t template = { 0 };
1132 vhost_user_main_t *vum = &vhost_user_main;
1133 vhost_user_intf_t *vui;
1135 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1137 client_len = sizeof (client);
1138 client_fd = accept (uf->file_descriptor,
1139 (struct sockaddr *) &client,
1140 (socklen_t *) & client_len);
1143 return clib_error_return_unix (0, "accept");
1145 if (vui->clib_file_index != ~0)
1147 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1148 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1149 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1152 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1153 vui->sw_if_index, client_fd);
1154 template.read_function = vhost_user_socket_read;
1155 template.error_function = vhost_user_socket_error;
1156 template.file_descriptor = client_fd;
1157 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1158 template.description = format (0, "vhost interface %d", vui->sw_if_index);
1159 vui->clib_file_index = clib_file_add (&file_main, &template);
1164 static clib_error_t *
1165 vhost_user_init (vlib_main_t * vm)
1167 vhost_user_main_t *vum = &vhost_user_main;
1168 vlib_thread_main_t *tm = vlib_get_thread_main ();
1170 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1172 vum->coalesce_frames = 32;
1173 vum->coalesce_time = 1e-3;
1175 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1178 vec_foreach (cpu, vum->cpus)
1180 /* This is actually not necessary as validate already zeroes it
1181 * Just keeping the loop here for later because I am lazy. */
1182 cpu->rx_buffers_len = 0;
1185 vum->random = random_default_seed ();
1187 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1193 VLIB_INIT_FUNCTION (vhost_user_init) =
1195 .runs_after = VLIB_INITS("ip4_init"),
1200 vhost_user_send_interrupt_process (vlib_main_t * vm,
1201 vlib_node_runtime_t * rt, vlib_frame_t * f)
1203 vhost_user_intf_t *vui;
1204 f64 timeout = 3153600000.0 /* 100 years */ ;
1205 uword event_type, *event_data = 0;
1206 vhost_user_main_t *vum = &vhost_user_main;
1208 f64 now, poll_time_remaining;
1214 poll_time_remaining =
1215 vlib_process_wait_for_event_or_clock (vm, timeout);
1216 event_type = vlib_process_get_events (vm, &event_data);
1217 vec_reset_length (event_data);
1220 * Use the remaining timeout if it is less than coalesce time to avoid
1221 * resetting the existing timer in the middle of expiration
1223 timeout = poll_time_remaining;
1224 if (vlib_process_suspend_time_is_zero (timeout) ||
1225 (timeout > vum->coalesce_time))
1226 timeout = vum->coalesce_time;
1228 now = vlib_time_now (vm);
1231 case VHOST_USER_EVENT_STOP_TIMER:
1235 case VHOST_USER_EVENT_START_TIMER:
1238 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1244 pool_foreach (vui, vum->vhost_user_interfaces) {
1245 next_timeout = timeout;
1246 FOR_ALL_VHOST_RX_TXQ (qid, vui)
1248 vhost_user_vring_t *vq = &vui->vrings[qid];
1250 if (vq->started == 0)
1252 if (vq->n_since_last_int)
1254 if (now >= vq->int_deadline)
1255 vhost_user_send_call (vm, vui, vq);
1257 next_timeout = vq->int_deadline - now;
1260 if ((next_timeout < timeout) && (next_timeout > 0.0))
1261 timeout = next_timeout;
1268 clib_warning ("BUG: unhandled event type %d", event_type);
1271 /* No less than 1 millisecond */
1275 timeout = 3153600000.0;
1281 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1282 .function = vhost_user_send_interrupt_process,
1283 .type = VLIB_NODE_TYPE_PROCESS,
1284 .name = "vhost-user-send-interrupt-process",
1289 vhost_user_process (vlib_main_t * vm,
1290 vlib_node_runtime_t * rt, vlib_frame_t * f)
1292 vhost_user_main_t *vum = &vhost_user_main;
1293 vhost_user_intf_t *vui;
1294 struct sockaddr_un sun;
1296 clib_file_t template = { 0 };
1297 f64 timeout = 3153600000.0 /* 100 years */ ;
1298 uword *event_data = 0;
1301 sun.sun_family = AF_UNIX;
1302 template.read_function = vhost_user_socket_read;
1303 template.error_function = vhost_user_socket_error;
1307 vlib_process_wait_for_event_or_clock (vm, timeout);
1308 vlib_process_get_events (vm, &event_data);
1309 vec_reset_length (event_data);
1314 pool_foreach (vui, vum->vhost_user_interfaces) {
1316 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1317 if (vui->clib_file_index == ~0)
1320 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1323 * 1st time error or new error for this interface,
1324 * spit out the message and record the error
1326 if (!vui->sock_errno || (vui->sock_errno != errno))
1329 ("Error: Could not open unix socket for %s",
1330 vui->sock_filename);
1331 vui->sock_errno = errno;
1336 /* try to connect */
1337 strncpy (sun.sun_path, (char *) vui->sock_filename,
1338 sizeof (sun.sun_path) - 1);
1339 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1341 /* Avoid hanging VPP if the other end does not accept */
1342 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1343 clib_unix_warning ("fcntl");
1345 if (connect (sockfd, (struct sockaddr *) &sun,
1346 sizeof (struct sockaddr_un)) == 0)
1348 /* Set the socket to blocking as it was before */
1349 if (fcntl(sockfd, F_SETFL, 0) < 0)
1350 clib_unix_warning ("fcntl2");
1352 vui->sock_errno = 0;
1353 template.file_descriptor = sockfd;
1354 template.private_data =
1355 vui - vhost_user_main.vhost_user_interfaces;
1356 template.description = format (0, "vhost user process");
1357 vui->clib_file_index = clib_file_add (&file_main, &template);
1360 /* This sockfd is considered consumed */
1365 vui->sock_errno = errno;
1370 /* check if socket is alive */
1372 socklen_t len = sizeof (error);
1373 int fd = UNIX_GET_FD(vui->clib_file_index);
1375 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1379 vu_log_debug (vui, "getsockopt returned %d", retval);
1380 vhost_user_if_disconnect (vui);
1391 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1392 .function = vhost_user_process,
1393 .type = VLIB_NODE_TYPE_PROCESS,
1394 .name = "vhost-user-process",
1399 * Disables and reset interface structure.
1400 * It can then be either init again, or removed from used interfaces.
1403 vhost_user_term_if (vhost_user_intf_t * vui)
1406 vhost_user_main_t *vum = &vhost_user_main;
1408 // disconnect interface sockets
1409 vhost_user_if_disconnect (vui);
1410 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1411 vhost_user_update_iface_state (vui);
1413 FOR_ALL_VHOST_RX_TXQ (q, vui)
1415 clib_spinlock_free (&vui->vrings[q].vring_lock);
1418 if (vui->unix_server_index != ~0)
1420 //Close server socket
1421 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1422 vui->unix_server_index);
1423 clib_file_del (&file_main, uf);
1424 vui->unix_server_index = ~0;
1425 unlink (vui->sock_filename);
1428 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1433 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1435 vhost_user_main_t *vum = &vhost_user_main;
1436 vhost_user_intf_t *vui;
1438 vnet_hw_interface_t *hwif;
1443 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1444 || hwif->dev_class_index != vhost_user_device_class.index)
1445 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1447 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1449 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1450 hwif->name, hwif->dev_instance);
1452 FOR_ALL_VHOST_TXQ (qid, vui)
1454 vhost_user_vring_t *txvq = &vui->vrings[qid];
1456 if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) &&
1457 (txvq->thread_index != ~0))
1459 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index);
1460 ASSERT (cpu->polling_q_count != 0);
1461 cpu->polling_q_count--;
1464 if ((vum->ifq_count > 0) &&
1465 ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1466 (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1469 // Stop the timer if there is no more interrupt interface/queue
1470 if (vum->ifq_count == 0)
1472 vlib_process_signal_event (vm,
1473 vhost_user_send_interrupt_node.index,
1474 VHOST_USER_EVENT_STOP_TIMER, 0);
1480 // Disable and reset interface
1481 vhost_user_term_if (vui);
1483 // Reset renumbered iface
1484 if (hwif->dev_instance <
1485 vec_len (vum->show_dev_instance_by_real_dev_instance))
1486 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1488 // Delete ethernet interface
1489 ethernet_delete_interface (vnm, vui->hw_if_index);
1492 vec_free (vui->vrings);
1495 pool_put (vum->vhost_user_interfaces, vui);
1500 static clib_error_t *
1501 vhost_user_exit (vlib_main_t * vm)
1503 vnet_main_t *vnm = vnet_get_main ();
1504 vhost_user_main_t *vum = &vhost_user_main;
1505 vhost_user_intf_t *vui;
1507 vlib_worker_thread_barrier_sync (vlib_get_main ());
1509 pool_foreach (vui, vum->vhost_user_interfaces) {
1510 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1513 vlib_worker_thread_barrier_release (vlib_get_main ());
1517 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1520 * Open server unix socket on specified sock_filename.
1523 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1526 struct sockaddr_un un = { };
1528 /* create listening socket */
1529 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1530 return VNET_API_ERROR_SYSCALL_ERROR_1;
1532 un.sun_family = AF_UNIX;
1533 strncpy ((char *) un.sun_path, (char *) sock_filename,
1534 sizeof (un.sun_path) - 1);
1536 /* remove if exists */
1537 unlink ((char *) sock_filename);
1539 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1541 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1545 if (listen (fd, 1) == -1)
1547 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1560 * Create ethernet interface for vhost user interface.
1563 vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm,
1564 vhost_user_intf_t *vui,
1565 vhost_user_create_if_args_t *args)
1567 vhost_user_main_t *vum = &vhost_user_main;
1569 clib_error_t *error;
1571 /* create hw and sw interface */
1572 if (args->use_custom_mac)
1574 clib_memcpy (hwaddr, args->hwaddr, 6);
1578 random_u32 (&vum->random);
1579 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1584 error = ethernet_register_interface
1586 vhost_user_device_class.index,
1587 vui - vum->vhost_user_interfaces /* device instance */ ,
1588 hwaddr /* ethernet address */ ,
1589 &vui->hw_if_index, 0 /* flag change */ );
1592 clib_error_report (error);
1596 * Initialize vui with specified attributes
1599 vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui,
1600 int server_sock_fd, vhost_user_create_if_args_t * args,
1603 vnet_sw_interface_t *sw;
1605 vhost_user_main_t *vum = &vhost_user_main;
1606 vnet_hw_interface_t *hw;
1608 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1609 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1610 if (server_sock_fd != -1)
1612 clib_file_t template = { 0 };
1613 template.read_function = vhost_user_socksvr_accept_ready;
1614 template.file_descriptor = server_sock_fd;
1615 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1616 template.description = format (0, "vhost user %d", sw);
1617 vui->unix_server_index = clib_file_add (&file_main, &template);
1621 vui->unix_server_index = ~0;
1624 vui->sw_if_index = sw->sw_if_index;
1625 strncpy (vui->sock_filename, args->sock_filename,
1626 ARRAY_LEN (vui->sock_filename) - 1);
1627 vui->sock_errno = 0;
1629 vui->feature_mask = args->feature_mask;
1630 vui->clib_file_index = ~0;
1631 vui->log_base_addr = 0;
1632 vui->if_index = vui - vum->vhost_user_interfaces;
1633 vui->enable_gso = args->enable_gso;
1634 vui->enable_event_idx = args->enable_event_idx;
1635 vui->enable_packed = args->enable_packed;
1637 * enable_gso takes precedence over configurable feature mask if there
1639 * if feature mask disables gso, but enable_gso is configured,
1640 * then gso is enable
1641 * if feature mask enables gso, but enable_gso is not configured,
1642 * then gso is enable
1644 * if gso is enable via feature mask, it must enable both host and guest
1645 * gso feature mask, we don't support one sided GSO or partial GSO.
1647 if ((vui->enable_gso == 0) &&
1648 ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
1649 == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1650 vui->enable_gso = 1;
1651 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1652 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1655 vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
1656 CLIB_CACHE_LINE_BYTES);
1658 for (q = 0; q < vec_len (vui->vrings); q++)
1659 vhost_user_vring_init (vui, q);
1661 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
1662 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1665 *sw_if_index = vui->sw_if_index;
1669 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1670 vhost_user_create_if_args_t * args)
1672 vhost_user_intf_t *vui = NULL;
1675 int server_sock_fd = -1;
1676 vhost_user_main_t *vum = &vhost_user_main;
1679 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1681 return VNET_API_ERROR_INVALID_ARGUMENT;
1684 if_index = mhash_get (&vum->if_index_by_sock_name,
1685 (void *) args->sock_filename);
1688 vui = &vum->vhost_user_interfaces[*if_index];
1689 args->sw_if_index = vui->sw_if_index;
1690 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1693 if (args->is_server)
1696 vhost_user_init_server_sock (args->sock_filename,
1697 &server_sock_fd)) != 0)
1703 /* Protect the uninitialized vui from being dispatched by rx/tx */
1704 vlib_worker_thread_barrier_sync (vm);
1705 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1706 vhost_user_create_ethernet (vnm, vm, vui, args);
1707 vlib_worker_thread_barrier_release (vm);
1709 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1710 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1711 vhost_user_rx_thread_placement (vui, 1);
1714 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1716 args->sw_if_index = sw_if_idx;
1718 // Process node must connect
1719 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1725 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1726 vhost_user_create_if_args_t * args)
1728 vhost_user_main_t *vum = &vhost_user_main;
1729 vhost_user_intf_t *vui = NULL;
1731 int server_sock_fd = -1;
1733 vnet_hw_interface_t *hwif;
1736 if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm,
1738 || hwif->dev_class_index != vhost_user_device_class.index)
1739 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1741 if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1742 return VNET_API_ERROR_INVALID_ARGUMENT;
1744 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1747 * Disallow changing the interface to have the same path name
1748 * as other interface
1750 if_index = mhash_get (&vum->if_index_by_sock_name,
1751 (void *) args->sock_filename);
1752 if (if_index && (*if_index != vui->if_index))
1753 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1755 // First try to open server socket
1756 if (args->is_server)
1757 if ((rv = vhost_user_init_server_sock (args->sock_filename,
1758 &server_sock_fd)) != 0)
1761 vhost_user_term_if (vui);
1762 vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1765 vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance);
1767 // Process node must connect
1768 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1774 vhost_user_connect_command_fn (vlib_main_t * vm,
1775 unformat_input_t * input,
1776 vlib_cli_command_t * cmd)
1778 vnet_main_t *vnm = vnet_get_main ();
1779 unformat_input_t _line_input, *line_input = &_line_input;
1780 clib_error_t *error = NULL;
1781 vhost_user_create_if_args_t args = { 0 };
1784 /* Get a line of input. */
1785 if (!unformat_user (input, unformat_line_input, line_input))
1788 args.feature_mask = (u64) ~ (0ULL);
1789 args.custom_dev_instance = ~0;
1790 /* GSO feature is disable by default */
1791 args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1792 /* packed-ring feature is disable by default */
1793 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1794 /* event_idx feature is disable by default */
1795 args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1797 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1799 if (unformat (line_input, "socket %s", &args.sock_filename))
1801 else if (unformat (line_input, "server"))
1803 else if (unformat (line_input, "gso"))
1804 args.enable_gso = 1;
1805 else if (unformat (line_input, "packed"))
1806 args.enable_packed = 1;
1807 else if (unformat (line_input, "event-idx"))
1808 args.enable_event_idx = 1;
1809 else if (unformat (line_input, "feature-mask 0x%llx",
1810 &args.feature_mask))
1812 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1814 args.use_custom_mac = 1;
1815 else if (unformat (line_input, "renumber %d",
1816 &args.custom_dev_instance))
1820 error = clib_error_return (0, "unknown input `%U'",
1821 format_unformat_error, line_input);
1826 if ((rv = vhost_user_create_if (vnm, vm, &args)))
1828 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1832 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm,
1836 vec_free (args.sock_filename);
1837 unformat_free (line_input);
1843 vhost_user_delete_command_fn (vlib_main_t * vm,
1844 unformat_input_t * input,
1845 vlib_cli_command_t * cmd)
1847 unformat_input_t _line_input, *line_input = &_line_input;
1848 u32 sw_if_index = ~0;
1849 vnet_main_t *vnm = vnet_get_main ();
1850 clib_error_t *error = NULL;
1852 /* Get a line of input. */
1853 if (!unformat_user (input, unformat_line_input, line_input))
1856 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1858 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1861 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1864 vnet_hw_interface_t *hwif =
1865 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1867 vhost_user_device_class.index != hwif->dev_class_index)
1869 error = clib_error_return (0, "Not a vhost interface");
1875 error = clib_error_return (0, "unknown input `%U'",
1876 format_unformat_error, line_input);
1881 vhost_user_delete_if (vnm, vm, sw_if_index);
1884 unformat_free (line_input);
1890 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1891 vhost_user_intf_details_t ** out_vuids)
1894 vhost_user_main_t *vum = &vhost_user_main;
1895 vhost_user_intf_t *vui;
1896 vhost_user_intf_details_t *r_vuids = NULL;
1897 vhost_user_intf_details_t *vuid = NULL;
1898 u32 *hw_if_indices = 0;
1899 vnet_hw_interface_t *hi;
1905 pool_foreach (vui, vum->vhost_user_interfaces)
1906 vec_add1 (hw_if_indices, vui->hw_if_index);
1908 for (i = 0; i < vec_len (hw_if_indices); i++)
1910 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1911 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1913 vec_add2 (r_vuids, vuid, 1);
1914 vuid->sw_if_index = vui->sw_if_index;
1915 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1916 vuid->features = vui->features;
1917 vuid->num_regions = vui->nregions;
1918 vuid->is_server = vui->unix_server_index != ~0;
1919 vuid->sock_errno = vui->sock_errno;
1920 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1921 "%s", vui->sock_filename);
1922 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1923 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1924 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1927 vec_free (hw_if_indices);
1929 *out_vuids = r_vuids;
1935 format_vhost_user_desc (u8 * s, va_list * args)
1937 char *fmt = va_arg (*args, char *);
1938 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1939 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1940 int idx = va_arg (*args, int);
1941 u32 *mem_hint = va_arg (*args, u32 *);
1943 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1944 desc_table[idx].flags, desc_table[idx].next,
1945 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1951 vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq)
1953 int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1954 int callfd = UNIX_GET_FD (vq->callfd_idx);
1956 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1961 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1962 int show_descr, int show_verbose)
1968 vring_desc_t *desc_table;
1969 vhost_user_vring_t *vq = &vui->vrings[q];
1971 if (vq->avail && vq->used)
1972 vlib_cli_output (vm,
1973 " avail.flags %x avail event idx %u avail.idx %d "
1974 "used.flags %x used event idx %u used.idx %d\n",
1975 vq->avail->flags, vhost_user_avail_event_idx (vq),
1976 vq->avail->idx, vq->used->flags,
1977 vhost_user_used_event_idx (vq), vq->used->idx);
1979 vhost_user_show_fds (vm, vq);
1983 vlib_cli_output (vm, "\n descriptor table:\n");
1984 vlib_cli_output (vm,
1985 " slot addr len flags next "
1987 vlib_cli_output (vm,
1988 " ===== ================== ===== ====== ===== "
1989 "==================\n");
1990 for (j = 0; j < vq->qsz_mask + 1; j++)
1992 desc_table = vq->desc;
1993 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1994 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1995 desc_table, j, &mem_hint);
1996 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1998 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1999 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2002 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2005 (vm, "%U", format_vhost_user_desc,
2006 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2007 desc_table, idx, &mem_hint);
2009 if (n_entries >= 20)
2010 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2019 format_vhost_user_packed_desc (u8 * s, va_list * args)
2021 char *fmt = va_arg (*args, char *);
2022 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
2023 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
2024 int idx = va_arg (*args, int);
2025 u32 *mem_hint = va_arg (*args, u32 *);
2027 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
2028 desc_table[idx].flags, desc_table[idx].id,
2029 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
2035 format_vhost_user_event_idx_flags (u8 * s, va_list * args)
2037 u32 flags = va_arg (*args, u32);
2043 static event_idx_flags event_idx_array[] = {
2044 #define _(s,v) { .str = #s, .value = v, },
2045 foreach_virtio_event_idx_flags
2048 u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2050 if (flags < num_entries)
2051 s = format (s, "%s", event_idx_array[flags].str);
2053 s = format (s, "%u", flags);
2058 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
2059 int show_descr, int show_verbose)
2065 vring_packed_desc_t *desc_table;
2066 vhost_user_vring_t *vq = &vui->vrings[q];
2067 u16 off_wrap, event_idx;
2069 off_wrap = vq->avail_event->off_wrap;
2070 event_idx = off_wrap & 0x7fff;
2071 vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2072 "avail event idx %u\n", format_vhost_user_event_idx_flags,
2073 (u32) vq->avail_event->flags, off_wrap, event_idx);
2075 off_wrap = vq->used_event->off_wrap;
2076 event_idx = off_wrap & 0x7fff;
2077 vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2078 "used event idx %u\n", format_vhost_user_event_idx_flags,
2079 (u32) vq->used_event->flags, off_wrap, event_idx);
2081 vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2082 vq->avail_wrap_counter, vq->used_wrap_counter);
2084 vhost_user_show_fds (vm, vq);
2088 vlib_cli_output (vm, "\n descriptor table:\n");
2089 vlib_cli_output (vm,
2090 " slot addr len flags id "
2092 vlib_cli_output (vm,
2093 " ===== ================== ===== ====== ===== "
2094 "==================\n");
2095 for (j = 0; j < vq->qsz_mask + 1; j++)
2097 desc_table = vq->packed_desc;
2098 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2099 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2100 desc_table, j, &mem_hint);
2101 if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2103 n_entries = desc_table[j].len >> 4;
2104 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2107 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2110 (vm, "%U", format_vhost_user_packed_desc,
2111 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2112 desc_table, idx, &mem_hint);
2114 if (n_entries >= 20)
2115 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2124 show_vhost_user_command_fn (vlib_main_t * vm,
2125 unformat_input_t * input,
2126 vlib_cli_command_t * cmd)
2128 clib_error_t *error = 0;
2129 vnet_main_t *vnm = vnet_get_main ();
2130 vhost_user_main_t *vum = &vhost_user_main;
2131 vhost_user_intf_t *vui;
2132 u32 hw_if_index, *hw_if_indices = 0;
2133 vnet_hw_interface_t *hi;
2137 int show_verbose = 0;
2143 struct feat_struct *feat_entry;
2145 static struct feat_struct feat_array[] = {
2146 #define _(s,b) { .str = #s, .bit = b, },
2147 foreach_virtio_net_features
2152 #define foreach_protocol_feature \
2153 _(VHOST_USER_PROTOCOL_F_MQ) \
2154 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2156 static struct feat_struct proto_feat_array[] = {
2157 #define _(s) { .str = #s, .bit = s},
2158 foreach_protocol_feature
2163 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2166 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2168 hi = vnet_get_hw_interface (vnm, hw_if_index);
2169 if (vhost_user_device_class.index != hi->dev_class_index)
2171 error = clib_error_return (0, "unknown input `%U'",
2172 format_unformat_error, input);
2175 vec_add1 (hw_if_indices, hw_if_index);
2177 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2179 else if (unformat (input, "verbose"))
2183 error = clib_error_return (0, "unknown input `%U'",
2184 format_unformat_error, input);
2188 if (vec_len (hw_if_indices) == 0)
2190 pool_foreach (vui, vum->vhost_user_interfaces)
2191 vec_add1 (hw_if_indices, vui->hw_if_index);
2193 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2194 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2195 vum->coalesce_frames, vum->coalesce_time);
2196 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2198 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2199 for (u32 tid = 0; tid <= vlib_num_workers (); tid++)
2201 vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid);
2202 vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid,
2203 cpu->polling_q_count);
2206 for (i = 0; i < vec_len (hw_if_indices); i++)
2208 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2209 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2210 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2211 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2213 vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2214 if (vui->enable_gso)
2215 vlib_cli_output (vm, " GSO enable");
2216 if (vui->enable_packed)
2217 vlib_cli_output (vm, " Packed ring enable");
2218 if (vui->enable_event_idx)
2219 vlib_cli_output (vm, " Event index enable");
2221 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2222 " features mask (0x%llx): \n"
2223 " features (0x%llx): \n",
2224 vui->virtio_net_hdr_sz, vui->feature_mask,
2227 feat_entry = (struct feat_struct *) &feat_array;
2228 while (feat_entry->str)
2230 if (vui->features & (1ULL << feat_entry->bit))
2231 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2236 vlib_cli_output (vm, " protocol features (0x%llx)",
2237 vui->protocol_features);
2238 feat_entry = (struct feat_struct *) &proto_feat_array;
2239 while (feat_entry->str)
2241 if (vui->protocol_features & (1ULL << feat_entry->bit))
2242 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2247 vlib_cli_output (vm, "\n");
2249 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2251 (vui->unix_server_index != ~0) ? "server" : "client",
2252 strerror (vui->sock_errno));
2254 vlib_cli_output (vm, " rx placement: ");
2256 FOR_ALL_VHOST_TXQ (qid, vui)
2258 vhost_user_vring_t *txvq = &vui->vrings[qid];
2260 if (txvq->qid == -1)
2262 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2263 txvq->thread_index, qid, format_vnet_hw_if_rx_mode,
2267 vlib_cli_output (vm, " tx placement\n");
2269 FOR_ALL_VHOST_RXQ (qid, vui)
2271 vhost_user_vring_t *rxvq = &vui->vrings[qid];
2272 vnet_hw_if_tx_queue_t *txq;
2274 if (rxvq->queue_index == ~0)
2276 txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index);
2278 vlib_cli_output (vm, " threads %U on vring %u: %s\n",
2279 format_bitmap_list, txq->threads, qid,
2280 txq->shared_queue ? "spin-lock" : "lock-free");
2283 vlib_cli_output (vm, "\n");
2285 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2289 vlib_cli_output (vm,
2290 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2291 vlib_cli_output (vm,
2292 " ====== ===== ================== ================== ================== ================== ==================\n");
2294 for (j = 0; j < vui->nregions; j++)
2296 vlib_cli_output (vm,
2297 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2298 j, vui->region_mmap_fd[j],
2299 vui->regions[j].guest_phys_addr,
2300 vui->regions[j].memory_size,
2301 vui->regions[j].userspace_addr,
2302 vui->regions[j].mmap_offset,
2303 pointer_to_uword (vui->region_mmap_addr[j]));
2305 FOR_ALL_VHOST_RX_TXQ (q, vui)
2307 if (!vui->vrings[q].started)
2310 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2311 (q & 1) ? "RX" : "TX",
2312 vui->vrings[q].enabled ? "" : " disabled");
2313 vlib_cli_output (vm, " global %s queue index %u\n",
2314 (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index);
2318 " qsz %d last_avail_idx %d last_used_idx %d"
2320 vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx,
2321 vui->vrings[q].last_used_idx, vui->vrings[q].last_kick);
2323 if (vhost_user_is_packed_ring_supported (vui))
2324 vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose);
2326 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2328 vlib_cli_output (vm, "\n");
2331 vec_free (hw_if_indices);
2340 * Create a vHost User interface. Once created, a new virtual interface
2341 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2342 * is the next free index.
2344 * There are several parameters associated with a vHost interface:
2346 * - <b>socket <socket-filename></b> - Name of the linux socket used by
2347 * hypervisor and VPP to manage the vHost interface. If in <em>server</em>
2348 * mode, VPP will create the socket if it does not already exist. If in
2349 * <em>client</em> mode, hypervisor will create the socket if it does not
2350 * already exist. The VPP code is indifferent to the file location. However,
2351 * if SELinux is enabled, then the socket needs to be created in
2352 * <em>/var/run/vpp/</em>.
2354 * - <b>server</b> - Optional flag to indicate that VPP should be the server
2355 * for the linux socket. If not provided, VPP will be the client. In
2356 * <em>server</em> mode, the VM can be reset without tearing down the vHost
2357 * Interface. In <em>client</em> mode, VPP can be reset without bringing down
2358 * the VM and tearing down the vHost Interface.
2360 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated
2361 * at startup. <b>This is intended for degugging only.</b> It is recommended
2362 * that this parameter not be used except by experienced users. By default,
2363 * all supported features will be advertised. Otherwise, provide the set of
2365 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2366 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2367 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2368 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2369 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2370 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2371 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2372 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2373 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2375 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2376 * X:X:X:X:X:X unix or X.X.X cisco format.
2378 * - <b>renumber <dev_instance></b> - Optional parameter which allows the
2379 * instance in the name to be specified. If instance already exists, name
2380 * will be used anyway and multiple instances will have the same name. Use
2384 * Example of how to create a vhost interface with VPP as the client and all
2386 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2387 * VirtualEthernet0/0/0
2389 * Example of how to create a vhost interface with VPP as the server and with
2390 * just multiple queues enabled:
2391 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server
2392 * feature-mask 0x40400000}
2393 * VirtualEthernet0/0/1
2395 * Once the vHost interface is created, enable the interface using:
2396 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2399 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2400 .path = "create vhost-user",
2401 .short_help = "create vhost-user socket <socket-filename> [server] "
2402 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2403 "[packed] [event-idx]",
2404 .function = vhost_user_connect_command_fn,
2410 * Delete a vHost User interface using the interface name or the
2411 * software interface index. Use the '<em>show interface</em>'
2412 * command to determine the software interface index. On deletion,
2413 * the linux socket will not be deleted.
2416 * Example of how to delete a vhost interface by name:
2417 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2418 * Example of how to delete a vhost interface by software interface index:
2419 * @cliexcmd{delete vhost-user sw_if_index 1}
2422 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2423 .path = "delete vhost-user",
2424 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2425 .function = vhost_user_delete_command_fn,
2429 * Display the attributes of a single vHost User interface (provide interface
2430 * name), multiple vHost User interfaces (provide a list of interface names
2431 * separated by spaces) or all Vhost User interfaces (omit an interface name
2432 * to display all vHost interfaces).
2436 * Example of how to display a vhost interface:
2437 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2438 * Virtio vhost-user interfaces
2440 * coalesce frames 32 time 1e-3
2441 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2442 * virtio_net_hdr_sz 12
2443 * features mask (0xffffffffffffffff):
2444 * features (0x50408000):
2445 * VIRTIO_NET_F_MRG_RXBUF (15)
2446 * VIRTIO_NET_F_MQ (22)
2447 * VIRTIO_F_INDIRECT_DESC (28)
2448 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2449 * protocol features (0x3)
2450 * VHOST_USER_PROTOCOL_F_MQ (0)
2451 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2453 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2456 * thread 1 on vring 1
2457 * thread 1 on vring 5
2458 * thread 2 on vring 3
2459 * thread 2 on vring 7
2460 * tx placement: spin-lock
2461 * thread 0 on vring 0
2462 * thread 1 on vring 2
2463 * thread 2 on vring 0
2465 * Memory regions (total 2)
2466 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2467 * ====== == =============== =========== ============== =========== ==========
2468 * 0 60 0x00000000 0x000a0000 0xaac00000 0x00000000 0x2b400000
2469 * 1 61 0x000c0000 0x3ff40000 0xaacc0000 0x000c0000 0xabcc0000
2472 * qsz 256 last_avail_idx 0 last_used_idx 0
2473 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2474 * kickfd 62 callfd 64 errfd -1
2477 * qsz 256 last_avail_idx 0 last_used_idx 0
2478 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2479 * kickfd 65 callfd 66 errfd -1
2482 * qsz 256 last_avail_idx 0 last_used_idx 0
2483 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2484 * kickfd 63 callfd 70 errfd -1
2487 * qsz 256 last_avail_idx 0 last_used_idx 0
2488 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2489 * kickfd 72 callfd 74 errfd -1
2491 * Virtqueue 4 (TX disabled)
2492 * qsz 256 last_avail_idx 0 last_used_idx 0
2493 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2494 * kickfd 76 callfd 78 errfd -1
2496 * Virtqueue 5 (RX disabled)
2497 * qsz 256 last_avail_idx 0 last_used_idx 0
2498 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2499 * kickfd 80 callfd 82 errfd -1
2501 * Virtqueue 6 (TX disabled)
2502 * qsz 256 last_avail_idx 0 last_used_idx 0
2503 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2504 * kickfd 84 callfd 86 errfd -1
2506 * Virtqueue 7 (RX disabled)
2507 * qsz 256 last_avail_idx 0 last_used_idx 0
2508 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2509 * kickfd 88 callfd 90 errfd -1
2513 * The optional '<em>descriptors</em>' parameter will display the same output
2514 * as the previous example but will include the descriptor table for each
2516 * The output is truncated below:
2517 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2518 * Virtio vhost-user interfaces
2520 * coalesce frames 32 time 1e-3
2521 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2522 * virtio_net_hdr_sz 12
2523 * features mask (0xffffffffffffffff):
2524 * features (0x50408000):
2525 * VIRTIO_NET_F_MRG_RXBUF (15)
2526 * VIRTIO_NET_F_MQ (22)
2529 * qsz 256 last_avail_idx 0 last_used_idx 0
2530 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2531 * kickfd 62 callfd 64 errfd -1
2534 * id addr len flags next user_addr
2535 * ===== ================== ===== ====== ===== ==================
2536 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2537 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2538 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2539 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2540 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2541 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2542 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2543 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2544 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2545 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2546 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2548 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2549 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2550 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2551 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2552 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2553 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2554 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2557 * qsz 256 last_avail_idx 0 last_used_idx 0
2563 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2564 .path = "show vhost-user",
2565 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2566 "[[descriptors] [verbose]]",
2567 .function = show_vhost_user_command_fn,
2572 static clib_error_t *
2573 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2575 vhost_user_main_t *vum = &vhost_user_main;
2577 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2579 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2581 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2583 else if (unformat (input, "dont-dump-memory"))
2584 vum->dont_dump_vhost_user_memory = 1;
2586 return clib_error_return (0, "unknown input `%U'",
2587 format_unformat_error, input);
2593 /* vhost-user { ... } configuration. */
2594 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2597 vhost_user_unmap_all (void)
2599 vhost_user_main_t *vum = &vhost_user_main;
2600 vhost_user_intf_t *vui;
2602 if (vum->dont_dump_vhost_user_memory)
2604 pool_foreach (vui, vum->vhost_user_interfaces)
2605 unmap_all_mem_regions (vui);
2610 * fd.io coding-style-patch-verification: ON
2613 * eval: (c-set-style "gnu")