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/ip/ip.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.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 (q = 0; q < VHOST_VRING_MAX_N; q++)
112 vq = &vui->vrings[q];
119 static_always_inline void
120 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
122 //Let's try to assign one queue to each thread
124 u32 thread_index = 0;
126 vui->use_tx_spinlock = 0;
129 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
131 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
132 if (!rxvq->started || !rxvq->enabled)
135 vui->per_cpu_tx_qid[thread_index] = qid;
137 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
140 //We need to loop, meaning the spinlock has to be used
141 vui->use_tx_spinlock = 1;
142 if (thread_index == 0)
144 //Could not find a single valid one
145 for (thread_index = 0;
146 thread_index < vlib_get_thread_main ()->n_vlib_mains;
149 vui->per_cpu_tx_qid[thread_index] = 0;
157 * @brief Unassign existing interface/queue to thread mappings and re-assign
158 * new interface/queue to thread mappings
160 static_always_inline void
161 vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
163 vhost_user_vring_t *txvq = &vui->vrings[qid];
164 vnet_main_t *vnm = vnet_get_main ();
168 ASSERT ((qid & 1) == 1); // should be odd
169 // Assign new queue mappings for the interface
170 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
171 vhost_user_input_node.index);
172 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, q, ~0);
173 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
174 /* Set polling as the default */
175 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
177 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
179 vu_log_warn (vui, "unable to set rx mode for interface %d, "
180 "queue %d: rc=%d", vui->hw_if_index, q, rv);
183 /** @brief Returns whether at least one TX and one RX vring are enabled */
184 static_always_inline int
185 vhost_user_intf_ready (vhost_user_intf_t * vui)
187 int i, found[2] = { }; //RX + TX
189 for (i = 0; i < VHOST_VRING_MAX_N; i++)
190 if (vui->vrings[i].started && vui->vrings[i].enabled)
193 return found[0] && found[1];
196 static_always_inline void
197 vhost_user_update_iface_state (vhost_user_intf_t * vui)
199 /* if we have pointers to descriptor table, go up */
200 int is_ready = vhost_user_intf_ready (vui);
201 if (is_ready != vui->is_ready)
203 vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
204 is_ready ? "ready" : "down");
206 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
207 is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP
209 vui->is_ready = is_ready;
214 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
217 vnet_main_t *vnm = vnet_get_main ();
221 /* Only care about the odd number, or TX, virtqueue */
224 if (vhost_user_intf_ready (vui))
225 // qid >> 1 is to convert virtqueue number to vring queue index
226 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
229 static clib_error_t *
230 vhost_user_callfd_read_ready (clib_file_t * uf)
232 __attribute__ ((unused)) int n;
235 n = read (uf->file_descriptor, ((char *) &buff), 8);
240 static_always_inline void
241 vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid)
243 if (qid & 1) // RX is odd, TX is even
245 if (vui->vrings[qid].qid == -1)
246 vhost_user_rx_thread_placement (vui, qid);
249 vhost_user_tx_thread_placement (vui);
252 static clib_error_t *
253 vhost_user_kickfd_read_ready (clib_file_t * uf)
255 __attribute__ ((unused)) int n;
257 vhost_user_intf_t *vui =
258 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
259 uf->private_data >> 8);
260 u32 qid = uf->private_data & 0xff;
262 n = read (uf->file_descriptor, ((char *) &buff), 8);
263 vu_log_debug (vui, "if %d KICK queue %d", uf->private_data >> 8, qid);
264 if (!vui->vrings[qid].started ||
265 (vhost_user_intf_ready (vui) != vui->is_ready))
267 if (vui->vrings[qid].started == 0)
269 vui->vrings[qid].started = 1;
270 vhost_user_thread_placement (vui, qid);
271 vhost_user_update_iface_state (vui);
275 vhost_user_set_interrupt_pending (vui, uf->private_data);
279 static_always_inline void
280 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
282 vhost_user_vring_t *vring = &vui->vrings[qid];
283 clib_memset (vring, 0, sizeof (*vring));
284 vring->kickfd_idx = ~0;
285 vring->callfd_idx = ~0;
290 * We have a bug with some qemu 2.5, and this may be a fix.
291 * Feel like interpretation holy text, but this is from vhost-user.txt.
293 * One queue pair is enabled initially. More queues are enabled
294 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
296 * Don't know who's right, but this is what DPDK does.
298 if (qid == 0 || qid == 1)
302 static_always_inline void
303 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
305 vhost_user_vring_t *vring = &vui->vrings[qid];
307 if (vring->kickfd_idx != ~0)
309 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
311 clib_file_del (&file_main, uf);
312 vring->kickfd_idx = ~0;
314 if (vring->callfd_idx != ~0)
316 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
318 clib_file_del (&file_main, uf);
319 vring->callfd_idx = ~0;
321 if (vring->errfd != -1)
323 close (vring->errfd);
327 // save the qid so that we don't need to unassign and assign_rx_thread
328 // when the interface comes back up. They are expensive calls.
329 u16 q = vui->vrings[qid].qid;
330 vhost_user_vring_init (vui, qid);
331 vui->vrings[qid].qid = q;
334 static_always_inline void
335 vhost_user_if_disconnect (vhost_user_intf_t * vui)
337 vnet_main_t *vnm = vnet_get_main ();
340 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
342 if (vui->clib_file_index != ~0)
344 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
345 vui->clib_file_index = ~0;
350 for (q = 0; q < VHOST_VRING_MAX_N; q++)
351 vhost_user_vring_close (vui, q);
353 unmap_all_mem_regions (vui);
354 vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
357 static clib_error_t *
358 vhost_user_socket_read (clib_file_t * uf)
361 int fd, number_of_fds = 0;
362 int fds[VHOST_MEMORY_MAX_NREGIONS];
363 vhost_user_msg_t msg;
366 vhost_user_main_t *vum = &vhost_user_main;
367 vhost_user_intf_t *vui;
368 struct cmsghdr *cmsg;
370 clib_file_t template = { 0 };
371 vnet_main_t *vnm = vnet_get_main ();
372 vlib_main_t *vm = vlib_get_main ();
374 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
376 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
378 clib_memset (&mh, 0, sizeof (mh));
379 clib_memset (control, 0, sizeof (control));
381 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
384 /* set the payload */
385 iov[0].iov_base = (void *) &msg;
386 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
390 mh.msg_control = control;
391 mh.msg_controllen = sizeof (control);
393 n = recvmsg (uf->file_descriptor, &mh, 0);
395 if (n != VHOST_USER_MSG_HDR_SZ)
399 vu_log_debug (vui, "recvmsg returned error %d %s", errno,
404 vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
405 n, VHOST_USER_MSG_HDR_SZ);
410 if (mh.msg_flags & MSG_CTRUNC)
412 vu_log_debug (vui, "MSG_CTRUNC is set");
416 cmsg = CMSG_FIRSTHDR (&mh);
418 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
419 (cmsg->cmsg_type == SCM_RIGHTS) &&
420 (cmsg->cmsg_len - CMSG_LEN (0) <=
421 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
423 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
424 clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
427 /* version 1, no reply bit set */
428 if ((msg.flags & 7) != 1)
430 vu_log_debug (vui, "malformed message received. closing socket");
437 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
441 vu_log_debug (vui, "read failed %s", strerror (errno));
444 else if (rv != msg.size)
446 vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
454 case VHOST_USER_GET_FEATURES:
456 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
457 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
458 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
459 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
460 (1ULL << FEAT_VHOST_F_LOG_ALL) |
461 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
462 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
463 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
464 (1ULL << FEAT_VIRTIO_F_VERSION_1);
465 msg.u64 &= vui->feature_mask;
468 msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
469 if (vui->enable_packed)
470 msg.u64 |= (1ULL << FEAT_VIRTIO_F_RING_PACKED);
472 msg.size = sizeof (msg.u64);
473 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
474 "0x%016llx", vui->hw_if_index, msg.u64);
476 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
477 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
479 vu_log_debug (vui, "could not send message response");
484 case VHOST_USER_SET_FEATURES:
485 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
486 "0x%016llx", vui->hw_if_index, msg.u64);
488 vui->features = msg.u64;
491 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
492 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
493 vui->virtio_net_hdr_sz = 12;
495 vui->virtio_net_hdr_sz = 10;
498 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
500 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
501 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
502 if (vui->enable_gso &&
503 ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)
504 == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))
506 (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
507 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
509 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
510 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
511 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
513 vhost_user_update_iface_state (vui);
516 case VHOST_USER_SET_MEM_TABLE:
517 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
518 vui->hw_if_index, msg.memory.nregions);
520 if ((msg.memory.nregions < 1) ||
521 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
523 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
524 VHOST_MEMORY_MAX_NREGIONS);
528 if (msg.memory.nregions != number_of_fds)
530 vu_log_debug (vui, "each memory region must have FD");
534 /* Do the mmap without barrier sync */
535 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
536 for (i = 0; i < msg.memory.nregions; i++)
538 long page_sz = get_huge_page_size (fds[i]);
540 /* align size to page */
541 ssize_t map_sz = (msg.memory.regions[i].memory_size +
542 msg.memory.regions[i].mmap_offset +
543 page_sz - 1) & ~(page_sz - 1);
545 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
546 MAP_SHARED, fds[i], 0);
547 if (region_mmap_addr[i] == MAP_FAILED)
549 vu_log_err (vui, "failed to map memory. errno is %d", errno);
550 for (j = 0; j < i; j++)
551 munmap (region_mmap_addr[j], map_sz);
554 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
555 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
556 region_mmap_addr[i], page_sz);
559 vlib_worker_thread_barrier_sync (vm);
560 unmap_all_mem_regions (vui);
561 for (i = 0; i < msg.memory.nregions; i++)
563 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
564 sizeof (vhost_user_memory_region_t));
566 vui->region_mmap_addr[i] = region_mmap_addr[i];
567 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
568 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
569 vui->regions[i].memory_size;
571 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
572 vui->region_mmap_fd[i] = fds[i];
578 * Re-compute desc, used, and avail descriptor table if vring address
581 for (q = 0; q < VHOST_VRING_MAX_N; q++)
583 if (vui->vrings[q].desc_user_addr &&
584 vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
586 vui->vrings[q].desc =
587 map_user_mem (vui, vui->vrings[q].desc_user_addr);
588 vui->vrings[q].used =
589 map_user_mem (vui, vui->vrings[q].used_user_addr);
590 vui->vrings[q].avail =
591 map_user_mem (vui, vui->vrings[q].avail_user_addr);
594 vlib_worker_thread_barrier_release (vm);
597 case VHOST_USER_SET_VRING_NUM:
598 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
599 vui->hw_if_index, msg.state.index, msg.state.num);
601 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
602 (msg.state.num == 0) || /* it cannot be zero */
603 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
605 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
608 case VHOST_USER_SET_VRING_ADDR:
609 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
610 vui->hw_if_index, msg.state.index);
612 if (msg.state.index >= VHOST_VRING_MAX_N)
614 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
615 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
619 if (msg.size < sizeof (msg.addr))
621 vu_log_debug (vui, "vhost message is too short (%d < %d)",
622 msg.size, sizeof (msg.addr));
626 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
627 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
628 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
630 if ((desc == NULL) || (used == NULL) || (avail == NULL))
632 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
637 vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
638 vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
639 vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
641 vlib_worker_thread_barrier_sync (vm);
642 vui->vrings[msg.state.index].desc = desc;
643 vui->vrings[msg.state.index].used = used;
644 vui->vrings[msg.state.index].avail = avail;
646 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
647 vui->vrings[msg.state.index].log_used =
648 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
650 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
651 the ring is initialized in an enabled state. */
652 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
653 vui->vrings[msg.state.index].enabled = 1;
655 vui->vrings[msg.state.index].last_used_idx =
656 vui->vrings[msg.state.index].last_avail_idx =
657 vui->vrings[msg.state.index].used->idx;
659 /* tell driver that we don't want interrupts */
660 if (vhost_user_is_packed_ring_supported (vui))
661 vui->vrings[msg.state.index].used_event->flags =
662 VRING_EVENT_F_DISABLE;
664 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
665 vlib_worker_thread_barrier_release (vm);
666 vhost_user_update_iface_state (vui);
669 case VHOST_USER_SET_OWNER:
670 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
673 case VHOST_USER_RESET_OWNER:
674 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
678 case VHOST_USER_SET_VRING_CALL:
679 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
680 vui->hw_if_index, msg.u64);
682 q = (u8) (msg.u64 & 0xFF);
684 /* if there is old fd, delete and close it */
685 if (vui->vrings[q].callfd_idx != ~0)
687 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
688 vui->vrings[q].callfd_idx);
689 clib_file_del (&file_main, uf);
690 vui->vrings[q].callfd_idx = ~0;
693 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
695 if (number_of_fds != 1)
697 vu_log_debug (vui, "More than one fd received !");
701 template.read_function = vhost_user_callfd_read_ready;
702 template.file_descriptor = fds[0];
703 template.private_data =
704 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
705 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
708 vui->vrings[q].callfd_idx = ~0;
711 case VHOST_USER_SET_VRING_KICK:
712 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
713 vui->hw_if_index, msg.u64);
715 q = (u8) (msg.u64 & 0xFF);
717 if (vui->vrings[q].kickfd_idx != ~0)
719 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
720 vui->vrings[q].kickfd_idx);
721 clib_file_del (&file_main, uf);
722 vui->vrings[q].kickfd_idx = ~0;
725 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
727 if (number_of_fds != 1)
729 vu_log_debug (vui, "More than one fd received !");
733 template.read_function = vhost_user_kickfd_read_ready;
734 template.file_descriptor = fds[0];
735 template.private_data =
736 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
738 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
742 //When no kickfd is set, the queue is initialized as started
743 vui->vrings[q].kickfd_idx = ~0;
744 vui->vrings[q].started = 1;
745 vhost_user_thread_placement (vui, q);
747 vhost_user_update_iface_state (vui);
750 case VHOST_USER_SET_VRING_ERR:
751 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
752 vui->hw_if_index, msg.u64);
754 q = (u8) (msg.u64 & 0xFF);
756 if (vui->vrings[q].errfd != -1)
757 close (vui->vrings[q].errfd);
759 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
761 if (number_of_fds != 1)
764 vui->vrings[q].errfd = fds[0];
767 vui->vrings[q].errfd = -1;
770 case VHOST_USER_SET_VRING_BASE:
772 "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
773 vui->hw_if_index, msg.state.index, msg.state.num);
774 vlib_worker_thread_barrier_sync (vm);
775 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
776 if (vhost_user_is_packed_ring_supported (vui))
780 * 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
781 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
782 * | last avail idx | | last used idx | |
783 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
786 * avail wrap counter used wrap counter
788 /* last avail idx at bit 0-14. */
789 vui->vrings[msg.state.index].last_avail_idx =
790 msg.state.num & 0x7fff;
791 /* avail wrap counter at bit 15 */
792 vui->vrings[msg.state.index].avail_wrap_counter =
793 ! !(msg.state.num & (1 << 15));
796 * Although last_used_idx is passed in the upper 16 bits in qemu
797 * implementation, in practice, last_avail_idx and last_used_idx are
798 * usually the same. As a result, DPDK does not bother to pass us
799 * last_used_idx. The spec is not clear on thex coding. I figured it
800 * out by reading the qemu code. So let's just read last_avail_idx
801 * and set last_used_idx equals to last_avail_idx.
803 vui->vrings[msg.state.index].last_used_idx =
804 vui->vrings[msg.state.index].last_avail_idx;
805 vui->vrings[msg.state.index].used_wrap_counter =
806 vui->vrings[msg.state.index].avail_wrap_counter;
808 if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
809 vui->vrings[msg.state.index].avail_wrap_counter =
812 vlib_worker_thread_barrier_release (vm);
815 case VHOST_USER_GET_VRING_BASE:
816 if (msg.state.index >= VHOST_VRING_MAX_N)
818 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
819 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
823 /* protection is needed to prevent rx/tx from changing last_avail_idx */
824 vlib_worker_thread_barrier_sync (vm);
826 * Copy last_avail_idx from the vring before closing it because
827 * closing the vring also initializes the vring last_avail_idx
829 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
830 if (vhost_user_is_packed_ring_supported (vui))
833 (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
834 (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
836 ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
837 (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
840 msg.size = sizeof (msg.state);
843 * Spec says: Client must [...] stop ring upon receiving
844 * VHOST_USER_GET_VRING_BASE
846 vhost_user_vring_close (vui, msg.state.index);
847 vlib_worker_thread_barrier_release (vm);
849 "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
850 vui->hw_if_index, msg.state.index, msg.state.num);
852 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
853 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
855 vu_log_debug (vui, "could not send message response");
858 vhost_user_update_iface_state (vui);
861 case VHOST_USER_NONE:
862 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
865 case VHOST_USER_SET_LOG_BASE:
866 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
869 if (msg.size != sizeof (msg.log))
871 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
872 " %d instead of %d", msg.size, sizeof (msg.log));
876 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
878 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
879 "VHOST_USER_SET_LOG_BASE received");
884 /* align size to page */
885 long page_sz = get_huge_page_size (fd);
887 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
889 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
892 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
893 "mapped 0x%lx", map_sz, msg.log.offset, fd,
896 if (log_base_addr == MAP_FAILED)
898 vu_log_err (vui, "failed to map memory. errno is %d", errno);
902 vlib_worker_thread_barrier_sync (vm);
903 vui->log_base_addr = log_base_addr;
904 vui->log_base_addr += msg.log.offset;
905 vui->log_size = msg.log.size;
906 vlib_worker_thread_barrier_release (vm);
909 msg.size = sizeof (msg.u64);
911 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
912 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
914 vu_log_debug (vui, "could not send message response");
919 case VHOST_USER_SET_LOG_FD:
920 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
923 case VHOST_USER_GET_PROTOCOL_FEATURES:
925 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
926 (1 << VHOST_USER_PROTOCOL_F_MQ);
927 msg.size = sizeof (msg.u64);
928 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
929 "reply 0x%016llx", vui->hw_if_index, msg.u64);
931 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
932 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
934 vu_log_debug (vui, "could not send message response");
939 case VHOST_USER_SET_PROTOCOL_FEATURES:
940 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
941 "features 0x%016llx", vui->hw_if_index, msg.u64);
942 vui->protocol_features = msg.u64;
945 case VHOST_USER_GET_QUEUE_NUM:
947 msg.u64 = VHOST_VRING_MAX_N;
948 msg.size = sizeof (msg.u64);
949 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
950 vui->hw_if_index, msg.u64);
952 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
953 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
955 vu_log_debug (vui, "could not send message response");
960 case VHOST_USER_SET_VRING_ENABLE:
961 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
962 vui->hw_if_index, msg.state.num ? "enable" : "disable",
964 if (msg.state.index >= VHOST_VRING_MAX_N)
966 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
967 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
971 vui->vrings[msg.state.index].enabled = msg.state.num;
972 vhost_user_thread_placement (vui, msg.state.index);
973 vhost_user_update_iface_state (vui);
977 vu_log_debug (vui, "unknown vhost-user message %d received. "
978 "closing socket", msg.request);
985 vlib_worker_thread_barrier_sync (vm);
986 vhost_user_if_disconnect (vui);
987 vlib_worker_thread_barrier_release (vm);
988 vhost_user_update_iface_state (vui);
992 static clib_error_t *
993 vhost_user_socket_error (clib_file_t * uf)
995 vlib_main_t *vm = vlib_get_main ();
996 vhost_user_main_t *vum = &vhost_user_main;
997 vhost_user_intf_t *vui =
998 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1000 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1001 vlib_worker_thread_barrier_sync (vm);
1002 vhost_user_if_disconnect (vui);
1003 vlib_worker_thread_barrier_release (vm);
1007 static clib_error_t *
1008 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1010 int client_fd, client_len;
1011 struct sockaddr_un client;
1012 clib_file_t template = { 0 };
1013 vhost_user_main_t *vum = &vhost_user_main;
1014 vhost_user_intf_t *vui;
1016 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1018 client_len = sizeof (client);
1019 client_fd = accept (uf->file_descriptor,
1020 (struct sockaddr *) &client,
1021 (socklen_t *) & client_len);
1024 return clib_error_return_unix (0, "accept");
1026 if (vui->clib_file_index != ~0)
1028 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1029 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1030 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
1033 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1034 vui->sw_if_index, client_fd);
1035 template.read_function = vhost_user_socket_read;
1036 template.error_function = vhost_user_socket_error;
1037 template.file_descriptor = client_fd;
1038 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1039 vui->clib_file_index = clib_file_add (&file_main, &template);
1043 static clib_error_t *
1044 vhost_user_init (vlib_main_t * vm)
1046 vhost_user_main_t *vum = &vhost_user_main;
1047 vlib_thread_main_t *tm = vlib_get_thread_main ();
1049 vum->log_default = vlib_log_register_class ("vhost-user", 0);
1051 vum->coalesce_frames = 32;
1052 vum->coalesce_time = 1e-3;
1054 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1057 vec_foreach (cpu, vum->cpus)
1059 /* This is actually not necessary as validate already zeroes it
1060 * Just keeping the loop here for later because I am lazy. */
1061 cpu->rx_buffers_len = 0;
1064 vum->random = random_default_seed ();
1066 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1072 VLIB_INIT_FUNCTION (vhost_user_init) =
1074 .runs_after = VLIB_INITS("ip4_init"),
1079 vhost_user_send_interrupt_process (vlib_main_t * vm,
1080 vlib_node_runtime_t * rt, vlib_frame_t * f)
1082 vhost_user_intf_t *vui;
1083 f64 timeout = 3153600000.0 /* 100 years */ ;
1084 uword event_type, *event_data = 0;
1085 vhost_user_main_t *vum = &vhost_user_main;
1087 f64 now, poll_time_remaining;
1093 poll_time_remaining =
1094 vlib_process_wait_for_event_or_clock (vm, timeout);
1095 event_type = vlib_process_get_events (vm, &event_data);
1096 vec_reset_length (event_data);
1099 * Use the remaining timeout if it is less than coalesce time to avoid
1100 * resetting the existing timer in the middle of expiration
1102 timeout = poll_time_remaining;
1103 if (vlib_process_suspend_time_is_zero (timeout) ||
1104 (timeout > vum->coalesce_time))
1105 timeout = vum->coalesce_time;
1107 now = vlib_time_now (vm);
1110 case VHOST_USER_EVENT_STOP_TIMER:
1114 case VHOST_USER_EVENT_START_TIMER:
1116 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1122 pool_foreach (vui, vum->vhost_user_interfaces, {
1123 next_timeout = timeout;
1124 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1126 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1127 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1129 if (txvq->qid == -1)
1131 if (txvq->n_since_last_int)
1133 if (now >= txvq->int_deadline)
1134 vhost_user_send_call (vm, txvq);
1136 next_timeout = txvq->int_deadline - now;
1139 if (rxvq->n_since_last_int)
1141 if (now >= rxvq->int_deadline)
1142 vhost_user_send_call (vm, rxvq);
1144 next_timeout = rxvq->int_deadline - now;
1147 if ((next_timeout < timeout) && (next_timeout > 0.0))
1148 timeout = next_timeout;
1155 clib_warning ("BUG: unhandled event type %d", event_type);
1158 /* No less than 1 millisecond */
1162 timeout = 3153600000.0;
1168 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1169 .function = vhost_user_send_interrupt_process,
1170 .type = VLIB_NODE_TYPE_PROCESS,
1171 .name = "vhost-user-send-interrupt-process",
1176 vhost_user_process (vlib_main_t * vm,
1177 vlib_node_runtime_t * rt, vlib_frame_t * f)
1179 vhost_user_main_t *vum = &vhost_user_main;
1180 vhost_user_intf_t *vui;
1181 struct sockaddr_un sun;
1183 clib_file_t template = { 0 };
1184 f64 timeout = 3153600000.0 /* 100 years */ ;
1185 uword *event_data = 0;
1188 sun.sun_family = AF_UNIX;
1189 template.read_function = vhost_user_socket_read;
1190 template.error_function = vhost_user_socket_error;
1194 vlib_process_wait_for_event_or_clock (vm, timeout);
1195 vlib_process_get_events (vm, &event_data);
1196 vec_reset_length (event_data);
1201 pool_foreach (vui, vum->vhost_user_interfaces, {
1203 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1204 if (vui->clib_file_index == ~0)
1207 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1210 * 1st time error or new error for this interface,
1211 * spit out the message and record the error
1213 if (!vui->sock_errno || (vui->sock_errno != errno))
1216 ("Error: Could not open unix socket for %s",
1217 vui->sock_filename);
1218 vui->sock_errno = errno;
1223 /* try to connect */
1224 strncpy (sun.sun_path, (char *) vui->sock_filename,
1225 sizeof (sun.sun_path) - 1);
1227 /* Avoid hanging VPP if the other end does not accept */
1228 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1229 clib_unix_warning ("fcntl");
1231 if (connect (sockfd, (struct sockaddr *) &sun,
1232 sizeof (struct sockaddr_un)) == 0)
1234 /* Set the socket to blocking as it was before */
1235 if (fcntl(sockfd, F_SETFL, 0) < 0)
1236 clib_unix_warning ("fcntl2");
1238 vui->sock_errno = 0;
1239 template.file_descriptor = sockfd;
1240 template.private_data =
1241 vui - vhost_user_main.vhost_user_interfaces;
1242 vui->clib_file_index = clib_file_add (&file_main, &template);
1244 /* This sockfd is considered consumed */
1249 vui->sock_errno = errno;
1254 /* check if socket is alive */
1256 socklen_t len = sizeof (error);
1257 int fd = UNIX_GET_FD(vui->clib_file_index);
1259 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1263 vu_log_debug (vui, "getsockopt returned %d", retval);
1264 vhost_user_if_disconnect (vui);
1275 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1276 .function = vhost_user_process,
1277 .type = VLIB_NODE_TYPE_PROCESS,
1278 .name = "vhost-user-process",
1283 * Disables and reset interface structure.
1284 * It can then be either init again, or removed from used interfaces.
1287 vhost_user_term_if (vhost_user_intf_t * vui)
1290 vhost_user_main_t *vum = &vhost_user_main;
1292 // disconnect interface sockets
1293 vhost_user_if_disconnect (vui);
1294 vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1295 vhost_user_update_iface_state (vui);
1297 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1299 // Remove existing queue mapping for the interface
1303 vnet_main_t *vnm = vnet_get_main ();
1304 vhost_user_vring_t *txvq = &vui->vrings[q];
1306 if (txvq->qid != -1)
1308 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1312 vu_log_warn (vui, "unable to unassign interface %d, "
1313 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1317 clib_mem_free ((void *) vui->vring_locks[q]);
1320 if (vui->unix_server_index != ~0)
1322 //Close server socket
1323 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1324 vui->unix_server_index);
1325 clib_file_del (&file_main, uf);
1326 vui->unix_server_index = ~0;
1327 unlink (vui->sock_filename);
1330 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1335 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1337 vhost_user_main_t *vum = &vhost_user_main;
1338 vhost_user_intf_t *vui;
1340 vnet_hw_interface_t *hwif;
1345 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1346 || hwif->dev_class_index != vhost_user_device_class.index)
1347 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1349 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1351 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1352 hwif->name, hwif->dev_instance);
1354 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1356 vhost_user_vring_t *txvq = &vui->vrings[qid];
1358 if (txvq->qid == -1)
1360 if ((vum->ifq_count > 0) &&
1361 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1362 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1365 // Stop the timer if there is no more interrupt interface/queue
1366 if ((vum->ifq_count == 0) &&
1367 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1369 vlib_process_signal_event (vm,
1370 vhost_user_send_interrupt_node.index,
1371 VHOST_USER_EVENT_STOP_TIMER, 0);
1377 // Disable and reset interface
1378 vhost_user_term_if (vui);
1380 // Reset renumbered iface
1381 if (hwif->dev_instance <
1382 vec_len (vum->show_dev_instance_by_real_dev_instance))
1383 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1385 // Delete ethernet interface
1386 ethernet_delete_interface (vnm, vui->hw_if_index);
1389 pool_put (vum->vhost_user_interfaces, vui);
1394 static clib_error_t *
1395 vhost_user_exit (vlib_main_t * vm)
1397 vnet_main_t *vnm = vnet_get_main ();
1398 vhost_user_main_t *vum = &vhost_user_main;
1399 vhost_user_intf_t *vui;
1401 vlib_worker_thread_barrier_sync (vlib_get_main ());
1403 pool_foreach (vui, vum->vhost_user_interfaces, {
1404 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1407 vlib_worker_thread_barrier_release (vlib_get_main ());
1411 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1414 * Open server unix socket on specified sock_filename.
1417 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1420 struct sockaddr_un un = { };
1422 /* create listening socket */
1423 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1424 return VNET_API_ERROR_SYSCALL_ERROR_1;
1426 un.sun_family = AF_UNIX;
1427 strncpy ((char *) un.sun_path, (char *) sock_filename,
1428 sizeof (un.sun_path) - 1);
1430 /* remove if exists */
1431 unlink ((char *) sock_filename);
1433 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1435 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1439 if (listen (fd, 1) == -1)
1441 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1454 * Create ethernet interface for vhost user interface.
1457 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1458 vhost_user_intf_t * vui, u8 * hwaddress)
1460 vhost_user_main_t *vum = &vhost_user_main;
1462 clib_error_t *error;
1464 /* create hw and sw interface */
1467 clib_memcpy (hwaddr, hwaddress, 6);
1471 random_u32 (&vum->random);
1472 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1477 error = ethernet_register_interface
1479 vhost_user_device_class.index,
1480 vui - vum->vhost_user_interfaces /* device instance */ ,
1481 hwaddr /* ethernet address */ ,
1482 &vui->hw_if_index, 0 /* flag change */ );
1485 clib_error_report (error);
1489 * Initialize vui with specified attributes
1492 vhost_user_vui_init (vnet_main_t * vnm,
1493 vhost_user_intf_t * vui,
1495 const char *sock_filename,
1496 u64 feature_mask, u32 * sw_if_index, u8 enable_gso,
1499 vnet_sw_interface_t *sw;
1501 vhost_user_main_t *vum = &vhost_user_main;
1502 vnet_hw_interface_t *hw;
1504 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1505 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1506 if (server_sock_fd != -1)
1508 clib_file_t template = { 0 };
1509 template.read_function = vhost_user_socksvr_accept_ready;
1510 template.file_descriptor = server_sock_fd;
1511 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1512 vui->unix_server_index = clib_file_add (&file_main, &template);
1516 vui->unix_server_index = ~0;
1519 vui->sw_if_index = sw->sw_if_index;
1520 strncpy (vui->sock_filename, sock_filename,
1521 ARRAY_LEN (vui->sock_filename) - 1);
1522 vui->sock_errno = 0;
1524 vui->feature_mask = feature_mask;
1525 vui->clib_file_index = ~0;
1526 vui->log_base_addr = 0;
1527 vui->if_index = vui - vum->vhost_user_interfaces;
1528 vui->enable_gso = enable_gso;
1529 vui->enable_packed = enable_packed;
1531 * enable_gso takes precedence over configurable feature mask if there
1533 * if feature mask disables gso, but enable_gso is configured,
1534 * then gso is enable
1535 * if feature mask enables gso, but enable_gso is not configured,
1536 * then gso is enable
1538 * if gso is enable via feature mask, it must enable both host and guest
1539 * gso feature mask, we don't support one sided GSO or partial GSO.
1541 if ((vui->enable_gso == 0) &&
1542 ((feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) ==
1543 (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1544 vui->enable_gso = 1;
1545 vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1546 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1549 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1550 vhost_user_vring_init (vui, q);
1552 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1553 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1556 *sw_if_index = vui->sw_if_index;
1558 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1560 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1561 CLIB_CACHE_LINE_BYTES);
1562 clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1565 vec_validate (vui->per_cpu_tx_qid,
1566 vlib_get_thread_main ()->n_vlib_mains - 1);
1567 vhost_user_tx_thread_placement (vui);
1571 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1572 const char *sock_filename,
1576 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
1577 u8 enable_gso, u8 enable_packed)
1579 vhost_user_intf_t *vui = NULL;
1582 int server_sock_fd = -1;
1583 vhost_user_main_t *vum = &vhost_user_main;
1586 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1588 return VNET_API_ERROR_INVALID_ARGUMENT;
1591 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1596 vui = &vum->vhost_user_interfaces[*if_index];
1597 *sw_if_index = vui->sw_if_index;
1599 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1605 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1611 /* Protect the uninitialized vui from being dispatched by rx/tx */
1612 vlib_worker_thread_barrier_sync (vm);
1613 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1614 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1615 vlib_worker_thread_barrier_release (vm);
1617 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1618 feature_mask, &sw_if_idx, enable_gso, enable_packed);
1619 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1620 vhost_user_rx_thread_placement (vui, 1);
1623 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1626 *sw_if_index = sw_if_idx;
1628 // Process node must connect
1629 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1635 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1636 const char *sock_filename,
1639 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
1640 u8 enable_gso, u8 enable_packed)
1642 vhost_user_main_t *vum = &vhost_user_main;
1643 vhost_user_intf_t *vui = NULL;
1645 int server_sock_fd = -1;
1647 vnet_hw_interface_t *hwif;
1652 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index))
1653 || hwif->dev_class_index != vhost_user_device_class.index)
1654 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1656 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1657 return VNET_API_ERROR_INVALID_ARGUMENT;
1659 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1662 * Disallow changing the interface to have the same path name
1663 * as other interface
1665 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1666 if (if_index && (*if_index != vui->if_index))
1667 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1669 // First try to open server socket
1671 if ((rv = vhost_user_init_server_sock (sock_filename,
1672 &server_sock_fd)) != 0)
1675 vhost_user_term_if (vui);
1676 vhost_user_vui_init (vnm, vui, server_sock_fd,
1677 sock_filename, feature_mask, &sw_if_idx, enable_gso,
1681 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1683 // Process node must connect
1684 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1690 vhost_user_connect_command_fn (vlib_main_t * vm,
1691 unformat_input_t * input,
1692 vlib_cli_command_t * cmd)
1694 unformat_input_t _line_input, *line_input = &_line_input;
1695 u8 *sock_filename = NULL;
1698 u64 feature_mask = (u64) ~ (0ULL);
1700 u32 custom_dev_instance = ~0;
1703 clib_error_t *error = NULL;
1704 u8 enable_gso = 0, enable_packed = 0;
1706 /* Get a line of input. */
1707 if (!unformat_user (input, unformat_line_input, line_input))
1710 /* GSO feature is disable by default */
1711 feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
1712 /* packed-ring feature is disable by default */
1713 feature_mask &= ~(1ULL << FEAT_VIRTIO_F_RING_PACKED);
1714 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1716 if (unformat (line_input, "socket %s", &sock_filename))
1718 else if (unformat (line_input, "server"))
1720 else if (unformat (line_input, "gso"))
1722 else if (unformat (line_input, "packed"))
1724 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1728 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1730 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1736 error = clib_error_return (0, "unknown input `%U'",
1737 format_unformat_error, line_input);
1742 vnet_main_t *vnm = vnet_get_main ();
1745 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1746 is_server, &sw_if_index, feature_mask,
1747 renumber, custom_dev_instance, hw,
1748 enable_gso, enable_packed)))
1750 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1754 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1758 vec_free (sock_filename);
1759 unformat_free (line_input);
1765 vhost_user_delete_command_fn (vlib_main_t * vm,
1766 unformat_input_t * input,
1767 vlib_cli_command_t * cmd)
1769 unformat_input_t _line_input, *line_input = &_line_input;
1770 u32 sw_if_index = ~0;
1771 vnet_main_t *vnm = vnet_get_main ();
1772 clib_error_t *error = NULL;
1774 /* Get a line of input. */
1775 if (!unformat_user (input, unformat_line_input, line_input))
1778 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1780 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1783 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1786 vnet_hw_interface_t *hwif =
1787 vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
1789 vhost_user_device_class.index != hwif->dev_class_index)
1791 error = clib_error_return (0, "Not a vhost interface");
1797 error = clib_error_return (0, "unknown input `%U'",
1798 format_unformat_error, line_input);
1803 vhost_user_delete_if (vnm, vm, sw_if_index);
1806 unformat_free (line_input);
1812 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1813 vhost_user_intf_details_t ** out_vuids)
1816 vhost_user_main_t *vum = &vhost_user_main;
1817 vhost_user_intf_t *vui;
1818 vhost_user_intf_details_t *r_vuids = NULL;
1819 vhost_user_intf_details_t *vuid = NULL;
1820 u32 *hw_if_indices = 0;
1821 vnet_hw_interface_t *hi;
1827 pool_foreach (vui, vum->vhost_user_interfaces,
1828 vec_add1 (hw_if_indices, vui->hw_if_index);
1831 for (i = 0; i < vec_len (hw_if_indices); i++)
1833 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1834 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1836 vec_add2 (r_vuids, vuid, 1);
1837 vuid->sw_if_index = vui->sw_if_index;
1838 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1839 vuid->features = vui->features;
1840 vuid->num_regions = vui->nregions;
1841 vuid->is_server = vui->unix_server_index != ~0;
1842 vuid->sock_errno = vui->sock_errno;
1843 snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1844 "%s", vui->sock_filename);
1845 memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1846 clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1847 vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1850 vec_free (hw_if_indices);
1852 *out_vuids = r_vuids;
1858 format_vhost_user_desc (u8 * s, va_list * args)
1860 char *fmt = va_arg (*args, char *);
1861 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1862 vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1863 int idx = va_arg (*args, int);
1864 u32 *mem_hint = va_arg (*args, u32 *);
1866 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1867 desc_table[idx].flags, desc_table[idx].next,
1868 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1874 format_vhost_user_vring (u8 * s, va_list * args)
1876 char *fmt = va_arg (*args, char *);
1877 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1878 int q = va_arg (*args, int);
1880 s = format (s, fmt, vui->vrings[q].avail->flags, vui->vrings[q].avail->idx,
1881 vui->vrings[q].used->flags, vui->vrings[q].used->idx);
1886 vhost_user_show_fds (vlib_main_t * vm, vhost_user_intf_t * vui, int q)
1888 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1889 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1891 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1892 vui->vrings[q].errfd);
1896 vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1897 int show_descr, int show_verbose)
1903 vring_desc_t *desc_table;
1905 if (vui->vrings[q].avail && vui->vrings[q].used)
1906 vlib_cli_output (vm, "%U", format_vhost_user_vring,
1907 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1910 vhost_user_show_fds (vm, vui, q);
1914 vlib_cli_output (vm, "\n descriptor table:\n");
1915 vlib_cli_output (vm,
1916 " slot addr len flags next "
1918 vlib_cli_output (vm,
1919 " ===== ================== ===== ====== ===== "
1920 "==================\n");
1921 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1923 desc_table = vui->vrings[q].desc;
1924 vlib_cli_output (vm, "%U", format_vhost_user_desc,
1925 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1926 desc_table, j, &mem_hint);
1927 if (show_verbose && (desc_table[j].flags & VIRTQ_DESC_F_INDIRECT))
1929 n_entries = desc_table[j].len / sizeof (vring_desc_t);
1930 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1933 for (idx = 0; idx < clib_min (20, n_entries); idx++)
1936 (vm, "%U", format_vhost_user_desc,
1937 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1938 desc_table, idx, &mem_hint);
1940 if (n_entries >= 20)
1941 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1950 format_vhost_user_packed_desc (u8 * s, va_list * args)
1952 char *fmt = va_arg (*args, char *);
1953 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1954 vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1955 int idx = va_arg (*args, int);
1956 u32 *mem_hint = va_arg (*args, u32 *);
1958 s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1959 desc_table[idx].flags, desc_table[idx].id,
1960 pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1966 format_vhost_user_vring_packed (u8 * s, va_list * args)
1968 char *fmt = va_arg (*args, char *);
1969 vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1970 int q = va_arg (*args, int);
1972 s = format (s, fmt, vui->vrings[q].avail_event->flags,
1973 vui->vrings[q].avail_event->off_wrap,
1974 vui->vrings[q].used_event->flags,
1975 vui->vrings[q].used_event->off_wrap,
1976 vui->vrings[q].avail_wrap_counter,
1977 vui->vrings[q].used_wrap_counter);
1982 vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
1983 int show_descr, int show_verbose)
1989 vring_packed_desc_t *desc_table;
1991 if (vui->vrings[q].avail_event && vui->vrings[q].used_event)
1992 vlib_cli_output (vm, "%U", format_vhost_user_vring_packed,
1993 " avail_event.flags %x avail_event.off_wrap %u "
1994 "used_event.flags %x used_event.off_wrap %u\n"
1995 " avail wrap counter %u, used wrap counter %u\n",
1998 vhost_user_show_fds (vm, vui, q);
2002 vlib_cli_output (vm, "\n descriptor table:\n");
2003 vlib_cli_output (vm,
2004 " slot addr len flags id "
2006 vlib_cli_output (vm,
2007 " ===== ================== ===== ====== ===== "
2008 "==================\n");
2009 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
2011 desc_table = vui->vrings[q].packed_desc;
2012 vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
2013 " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2014 desc_table, j, &mem_hint);
2015 if (show_verbose && (desc_table[j].flags & VIRTQ_DESC_F_INDIRECT))
2017 n_entries = desc_table[j].len >> 4;
2018 desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2021 for (idx = 0; idx < clib_min (20, n_entries); idx++)
2024 (vm, "%U", format_vhost_user_packed_desc,
2025 "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2026 desc_table, idx, &mem_hint);
2028 if (n_entries >= 20)
2029 vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2038 show_vhost_user_command_fn (vlib_main_t * vm,
2039 unformat_input_t * input,
2040 vlib_cli_command_t * cmd)
2042 clib_error_t *error = 0;
2043 vnet_main_t *vnm = vnet_get_main ();
2044 vhost_user_main_t *vum = &vhost_user_main;
2045 vhost_user_intf_t *vui;
2046 u32 hw_if_index, *hw_if_indices = 0;
2047 vnet_hw_interface_t *hi;
2052 int show_verbose = 0;
2058 struct feat_struct *feat_entry;
2060 static struct feat_struct feat_array[] = {
2061 #define _(s,b) { .str = #s, .bit = b, },
2062 foreach_virtio_net_feature
2067 #define foreach_protocol_feature \
2068 _(VHOST_USER_PROTOCOL_F_MQ) \
2069 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2071 static struct feat_struct proto_feat_array[] = {
2072 #define _(s) { .str = #s, .bit = s},
2073 foreach_protocol_feature
2078 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2081 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2083 hi = vnet_get_hw_interface (vnm, hw_if_index);
2084 if (vhost_user_device_class.index != hi->dev_class_index)
2086 error = clib_error_return (0, "unknown input `%U'",
2087 format_unformat_error, input);
2090 vec_add1 (hw_if_indices, hw_if_index);
2092 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2094 else if (unformat (input, "verbose"))
2098 error = clib_error_return (0, "unknown input `%U'",
2099 format_unformat_error, input);
2103 if (vec_len (hw_if_indices) == 0)
2105 pool_foreach (vui, vum->vhost_user_interfaces,
2106 vec_add1 (hw_if_indices, vui->hw_if_index);
2109 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2110 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2111 vum->coalesce_frames, vum->coalesce_time);
2112 vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2114 vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2116 for (i = 0; i < vec_len (hw_if_indices); i++)
2118 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2119 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2120 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2121 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2123 if (vui->enable_gso)
2124 vlib_cli_output (vm, " GSO enable");
2125 if (vui->enable_packed)
2126 vlib_cli_output (vm, " Packed ring enable");
2128 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2129 " features mask (0x%llx): \n"
2130 " features (0x%llx): \n",
2131 vui->virtio_net_hdr_sz, vui->feature_mask,
2134 feat_entry = (struct feat_struct *) &feat_array;
2135 while (feat_entry->str)
2137 if (vui->features & (1ULL << feat_entry->bit))
2138 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2143 vlib_cli_output (vm, " protocol features (0x%llx)",
2144 vui->protocol_features);
2145 feat_entry = (struct feat_struct *) &proto_feat_array;
2146 while (feat_entry->str)
2148 if (vui->protocol_features & (1ULL << feat_entry->bit))
2149 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2154 vlib_cli_output (vm, "\n");
2156 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2158 (vui->unix_server_index != ~0) ? "server" : "client",
2159 strerror (vui->sock_errno));
2161 vlib_cli_output (vm, " rx placement: ");
2163 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
2165 vnet_main_t *vnm = vnet_get_main ();
2167 vnet_hw_interface_rx_mode mode;
2168 vhost_user_vring_t *txvq = &vui->vrings[qid];
2170 if (txvq->qid == -1)
2173 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
2175 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
2177 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2179 format_vnet_hw_interface_rx_mode, mode);
2182 vlib_cli_output (vm, " tx placement: %s\n",
2183 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2185 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2187 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2188 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2191 vlib_cli_output (vm, "\n");
2193 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2197 vlib_cli_output (vm,
2198 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2199 vlib_cli_output (vm,
2200 " ====== ===== ================== ================== ================== ================== ==================\n");
2202 for (j = 0; j < vui->nregions; j++)
2204 vlib_cli_output (vm,
2205 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2206 j, vui->region_mmap_fd[j],
2207 vui->regions[j].guest_phys_addr,
2208 vui->regions[j].memory_size,
2209 vui->regions[j].userspace_addr,
2210 vui->regions[j].mmap_offset,
2211 pointer_to_uword (vui->region_mmap_addr[j]));
2213 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2215 if (!vui->vrings[q].started)
2218 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2219 (q & 1) ? "RX" : "TX",
2220 vui->vrings[q].enabled ? "" : " disabled");
2222 vlib_cli_output (vm,
2223 " qsz %d last_avail_idx %d last_used_idx %d\n",
2224 vui->vrings[q].qsz_mask + 1,
2225 vui->vrings[q].last_avail_idx,
2226 vui->vrings[q].last_used_idx);
2228 if (vhost_user_is_packed_ring_supported (vui))
2229 vhost_user_show_desc_packed (vm, vui, q, show_descr,
2232 vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2234 vlib_cli_output (vm, "\n");
2237 vec_free (hw_if_indices);
2246 * Create a vHost User interface. Once created, a new virtual interface
2247 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2248 * is the next free index.
2250 * There are several parameters associated with a vHost interface:
2252 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2253 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2254 * create the socket if it does not already exist. If in '<em>client</em>' mode,
2255 * hypervisor will create the socket if it does not already exist. The VPP code
2256 * is indifferent to the file location. However, if SELinux is enabled, then the
2257 * socket needs to be created in '<em>/var/run/vpp/</em>'.
2259 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2260 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2261 * mode, the VM can be reset without tearing down the vHost Interface. In
2262 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2263 * tearing down the vHost Interface.
2265 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2266 * startup. <b>This is intended for degugging only.</b> It is recommended that this
2267 * parameter not be used except by experienced users. By default, all supported
2268 * features will be advertised. Otherwise, provide the set of features desired.
2269 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2270 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2271 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2272 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2273 * - 0x004000000 (26) - VHOST_F_LOG_ALL
2274 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2275 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2276 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2277 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2279 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2280 * X:X:X:X:X:X unix or X.X.X cisco format.
2282 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2283 * in the name to be specified. If instance already exists, name will be used
2284 * anyway and multiple instances will have the same name. Use with caution.
2287 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2288 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2289 * VirtualEthernet0/0/0
2291 * Example of how to create a vhost interface with VPP as the server and with just
2292 * multiple queues enabled:
2293 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2294 * VirtualEthernet0/0/1
2296 * Once the vHost interface is created, enable the interface using:
2297 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2300 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2301 .path = "create vhost-user",
2302 .short_help = "create vhost-user socket <socket-filename> [server] "
2303 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2305 .function = vhost_user_connect_command_fn,
2311 * Delete a vHost User interface using the interface name or the
2312 * software interface index. Use the '<em>show interface</em>'
2313 * command to determine the software interface index. On deletion,
2314 * the linux socket will not be deleted.
2317 * Example of how to delete a vhost interface by name:
2318 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2319 * Example of how to delete a vhost interface by software interface index:
2320 * @cliexcmd{delete vhost-user sw_if_index 1}
2323 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2324 .path = "delete vhost-user",
2325 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2326 .function = vhost_user_delete_command_fn,
2330 * Display the attributes of a single vHost User interface (provide interface
2331 * name), multiple vHost User interfaces (provide a list of interface names seperated
2332 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2333 * vHost interfaces).
2337 * Example of how to display a vhost interface:
2338 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2339 * Virtio vhost-user interfaces
2341 * coalesce frames 32 time 1e-3
2342 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2343 * virtio_net_hdr_sz 12
2344 * features mask (0xffffffffffffffff):
2345 * features (0x50408000):
2346 * VIRTIO_NET_F_MRG_RXBUF (15)
2347 * VIRTIO_NET_F_MQ (22)
2348 * VIRTIO_F_INDIRECT_DESC (28)
2349 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2350 * protocol features (0x3)
2351 * VHOST_USER_PROTOCOL_F_MQ (0)
2352 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2354 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2357 * thread 1 on vring 1
2358 * thread 1 on vring 5
2359 * thread 2 on vring 3
2360 * thread 2 on vring 7
2361 * tx placement: spin-lock
2362 * thread 0 on vring 0
2363 * thread 1 on vring 2
2364 * thread 2 on vring 0
2366 * Memory regions (total 2)
2367 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2368 * ====== ===== ================== ================== ================== ================== ==================
2369 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2370 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2373 * qsz 256 last_avail_idx 0 last_used_idx 0
2374 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2375 * kickfd 62 callfd 64 errfd -1
2378 * qsz 256 last_avail_idx 0 last_used_idx 0
2379 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2380 * kickfd 65 callfd 66 errfd -1
2383 * qsz 256 last_avail_idx 0 last_used_idx 0
2384 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2385 * kickfd 63 callfd 70 errfd -1
2388 * qsz 256 last_avail_idx 0 last_used_idx 0
2389 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2390 * kickfd 72 callfd 74 errfd -1
2392 * Virtqueue 4 (TX disabled)
2393 * qsz 256 last_avail_idx 0 last_used_idx 0
2394 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2395 * kickfd 76 callfd 78 errfd -1
2397 * Virtqueue 5 (RX disabled)
2398 * qsz 256 last_avail_idx 0 last_used_idx 0
2399 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2400 * kickfd 80 callfd 82 errfd -1
2402 * Virtqueue 6 (TX disabled)
2403 * qsz 256 last_avail_idx 0 last_used_idx 0
2404 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2405 * kickfd 84 callfd 86 errfd -1
2407 * Virtqueue 7 (RX disabled)
2408 * qsz 256 last_avail_idx 0 last_used_idx 0
2409 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2410 * kickfd 88 callfd 90 errfd -1
2414 * The optional '<em>descriptors</em>' parameter will display the same output as
2415 * the previous example but will include the descriptor table for each queue.
2416 * The output is truncated below:
2417 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2418 * Virtio vhost-user interfaces
2420 * coalesce frames 32 time 1e-3
2421 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2422 * virtio_net_hdr_sz 12
2423 * features mask (0xffffffffffffffff):
2424 * features (0x50408000):
2425 * VIRTIO_NET_F_MRG_RXBUF (15)
2426 * VIRTIO_NET_F_MQ (22)
2429 * qsz 256 last_avail_idx 0 last_used_idx 0
2430 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2431 * kickfd 62 callfd 64 errfd -1
2434 * id addr len flags next user_addr
2435 * ===== ================== ===== ====== ===== ==================
2436 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2437 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2438 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2439 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2440 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2441 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2442 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2443 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2444 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2445 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2446 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2448 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2449 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2450 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2451 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2452 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2453 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2454 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2457 * qsz 256 last_avail_idx 0 last_used_idx 0
2463 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2464 .path = "show vhost-user",
2465 .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2466 "[[descriptors] [verbose]]",
2467 .function = show_vhost_user_command_fn,
2472 static clib_error_t *
2473 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2475 vhost_user_main_t *vum = &vhost_user_main;
2477 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2479 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2481 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2483 else if (unformat (input, "dont-dump-memory"))
2484 vum->dont_dump_vhost_user_memory = 1;
2486 return clib_error_return (0, "unknown input `%U'",
2487 format_unformat_error, input);
2493 /* vhost-user { ... } configuration. */
2494 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2497 vhost_user_unmap_all (void)
2499 vhost_user_main_t *vum = &vhost_user_main;
2500 vhost_user_intf_t *vui;
2502 if (vum->dont_dump_vhost_user_memory)
2504 pool_foreach (vui, vum->vhost_user_interfaces,
2505 unmap_all_mem_regions (vui);
2511 * fd.io coding-style-patch-verification: ON
2514 * eval: (c-set-style "gnu")