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;
466 msg.size = sizeof (msg.u64);
467 vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
468 "0x%016llx", vui->hw_if_index, msg.u64);
470 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
471 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
473 vu_log_debug (vui, "could not send message response");
478 case VHOST_USER_SET_FEATURES:
479 vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
480 "0x%016llx", vui->hw_if_index, msg.u64);
482 vui->features = msg.u64;
485 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
486 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
487 vui->virtio_net_hdr_sz = 12;
489 vui->virtio_net_hdr_sz = 10;
492 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
494 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
495 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
497 vhost_user_update_iface_state (vui);
500 case VHOST_USER_SET_MEM_TABLE:
501 vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
502 vui->hw_if_index, msg.memory.nregions);
504 if ((msg.memory.nregions < 1) ||
505 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
507 vu_log_debug (vui, "number of mem regions must be between 1 and %i",
508 VHOST_MEMORY_MAX_NREGIONS);
512 if (msg.memory.nregions != number_of_fds)
514 vu_log_debug (vui, "each memory region must have FD");
518 /* Do the mmap without barrier sync */
519 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
520 for (i = 0; i < msg.memory.nregions; i++)
522 long page_sz = get_huge_page_size (fds[i]);
524 /* align size to page */
525 ssize_t map_sz = (msg.memory.regions[i].memory_size +
526 msg.memory.regions[i].mmap_offset +
527 page_sz - 1) & ~(page_sz - 1);
529 region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
530 MAP_SHARED, fds[i], 0);
531 if (region_mmap_addr[i] == MAP_FAILED)
533 vu_log_err (vui, "failed to map memory. errno is %d", errno);
534 for (j = 0; j < i; j++)
535 munmap (region_mmap_addr[j], map_sz);
538 vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
539 "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
540 region_mmap_addr[i], page_sz);
543 vlib_worker_thread_barrier_sync (vm);
544 unmap_all_mem_regions (vui);
545 for (i = 0; i < msg.memory.nregions; i++)
547 clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
548 sizeof (vhost_user_memory_region_t));
550 vui->region_mmap_addr[i] = region_mmap_addr[i];
551 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
552 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
553 vui->regions[i].memory_size;
555 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
556 vui->region_mmap_fd[i] = fds[i];
560 vlib_worker_thread_barrier_release (vm);
563 case VHOST_USER_SET_VRING_NUM:
564 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
565 vui->hw_if_index, msg.state.index, msg.state.num);
567 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
568 (msg.state.num == 0) || /* it cannot be zero */
569 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
571 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
574 case VHOST_USER_SET_VRING_ADDR:
575 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
576 vui->hw_if_index, msg.state.index);
578 if (msg.state.index >= VHOST_VRING_MAX_N)
580 vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
581 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
585 if (msg.size < sizeof (msg.addr))
587 vu_log_debug (vui, "vhost message is too short (%d < %d)",
588 msg.size, sizeof (msg.addr));
592 vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
593 vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
594 vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
596 if ((desc == NULL) || (used == NULL) || (avail == NULL))
598 vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
603 vlib_worker_thread_barrier_sync (vm);
604 vui->vrings[msg.state.index].desc = desc;
605 vui->vrings[msg.state.index].used = used;
606 vui->vrings[msg.state.index].avail = avail;
608 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
609 vui->vrings[msg.state.index].log_used =
610 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
612 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
613 the ring is initialized in an enabled state. */
614 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
615 vui->vrings[msg.state.index].enabled = 1;
617 vui->vrings[msg.state.index].last_used_idx =
618 vui->vrings[msg.state.index].last_avail_idx =
619 vui->vrings[msg.state.index].used->idx;
621 /* tell driver that we don't want interrupts */
622 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
623 vlib_worker_thread_barrier_release (vm);
624 vhost_user_update_iface_state (vui);
627 case VHOST_USER_SET_OWNER:
628 vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
631 case VHOST_USER_RESET_OWNER:
632 vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
636 case VHOST_USER_SET_VRING_CALL:
637 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
638 vui->hw_if_index, msg.u64);
640 q = (u8) (msg.u64 & 0xFF);
642 /* if there is old fd, delete and close it */
643 if (vui->vrings[q].callfd_idx != ~0)
645 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
646 vui->vrings[q].callfd_idx);
647 clib_file_del (&file_main, uf);
648 vui->vrings[q].callfd_idx = ~0;
651 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
653 if (number_of_fds != 1)
655 vu_log_debug (vui, "More than one fd received !");
659 template.read_function = vhost_user_callfd_read_ready;
660 template.file_descriptor = fds[0];
661 template.private_data =
662 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
663 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
666 vui->vrings[q].callfd_idx = ~0;
669 case VHOST_USER_SET_VRING_KICK:
670 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
671 vui->hw_if_index, msg.u64);
673 q = (u8) (msg.u64 & 0xFF);
675 if (vui->vrings[q].kickfd_idx != ~0)
677 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
678 vui->vrings[q].kickfd_idx);
679 clib_file_del (&file_main, uf);
680 vui->vrings[q].kickfd_idx = ~0;
683 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
685 if (number_of_fds != 1)
687 vu_log_debug (vui, "More than one fd received !");
691 template.read_function = vhost_user_kickfd_read_ready;
692 template.file_descriptor = fds[0];
693 template.private_data =
694 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
696 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
700 //When no kickfd is set, the queue is initialized as started
701 vui->vrings[q].kickfd_idx = ~0;
702 vui->vrings[q].started = 1;
703 vhost_user_thread_placement (vui, q);
705 vhost_user_update_iface_state (vui);
708 case VHOST_USER_SET_VRING_ERR:
709 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
710 vui->hw_if_index, msg.u64);
712 q = (u8) (msg.u64 & 0xFF);
714 if (vui->vrings[q].errfd != -1)
715 close (vui->vrings[q].errfd);
717 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
719 if (number_of_fds != 1)
722 vui->vrings[q].errfd = fds[0];
725 vui->vrings[q].errfd = -1;
728 case VHOST_USER_SET_VRING_BASE:
729 vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
730 vui->hw_if_index, msg.state.index, msg.state.num);
731 vlib_worker_thread_barrier_sync (vm);
732 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
733 vlib_worker_thread_barrier_release (vm);
736 case VHOST_USER_GET_VRING_BASE:
737 if (msg.state.index >= VHOST_VRING_MAX_N)
739 vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
740 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
744 /* protection is needed to prevent rx/tx from changing last_avail_idx */
745 vlib_worker_thread_barrier_sync (vm);
747 * Copy last_avail_idx from the vring before closing it because
748 * closing the vring also initializes the vring last_avail_idx
750 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
752 msg.size = sizeof (msg.state);
755 * Spec says: Client must [...] stop ring upon receiving
756 * VHOST_USER_GET_VRING_BASE
758 vhost_user_vring_close (vui, msg.state.index);
759 vlib_worker_thread_barrier_release (vm);
760 vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
761 vui->hw_if_index, msg.state.index, msg.state.num);
763 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
764 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
766 vu_log_debug (vui, "could not send message response");
769 vhost_user_update_iface_state (vui);
772 case VHOST_USER_NONE:
773 vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
776 case VHOST_USER_SET_LOG_BASE:
777 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
780 if (msg.size != sizeof (msg.log))
782 vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
783 " %d instead of %d", msg.size, sizeof (msg.log));
787 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
789 vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
790 "VHOST_USER_SET_LOG_BASE received");
795 /* align size to page */
796 long page_sz = get_huge_page_size (fd);
798 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
800 void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
803 vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
804 "mapped 0x%lx", map_sz, msg.log.offset, fd,
807 if (log_base_addr == MAP_FAILED)
809 vu_log_err (vui, "failed to map memory. errno is %d", errno);
813 vlib_worker_thread_barrier_sync (vm);
814 vui->log_base_addr = log_base_addr;
815 vui->log_base_addr += msg.log.offset;
816 vui->log_size = msg.log.size;
817 vlib_worker_thread_barrier_release (vm);
820 msg.size = sizeof (msg.u64);
822 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
823 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
825 vu_log_debug (vui, "could not send message response");
830 case VHOST_USER_SET_LOG_FD:
831 vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
834 case VHOST_USER_GET_PROTOCOL_FEATURES:
836 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
837 (1 << VHOST_USER_PROTOCOL_F_MQ);
838 msg.size = sizeof (msg.u64);
839 vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
840 "reply 0x%016llx", vui->hw_if_index, msg.u64);
842 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
843 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
845 vu_log_debug (vui, "could not send message response");
850 case VHOST_USER_SET_PROTOCOL_FEATURES:
851 vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
852 "features 0x%016llx", vui->hw_if_index, msg.u64);
853 vui->protocol_features = msg.u64;
856 case VHOST_USER_GET_QUEUE_NUM:
858 msg.u64 = VHOST_VRING_MAX_N;
859 msg.size = sizeof (msg.u64);
860 vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
861 vui->hw_if_index, msg.u64);
863 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
864 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
866 vu_log_debug (vui, "could not send message response");
871 case VHOST_USER_SET_VRING_ENABLE:
872 vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
873 vui->hw_if_index, msg.state.num ? "enable" : "disable",
875 if (msg.state.index >= VHOST_VRING_MAX_N)
877 vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
878 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
882 vui->vrings[msg.state.index].enabled = msg.state.num;
883 vhost_user_thread_placement (vui, msg.state.index);
884 vhost_user_update_iface_state (vui);
888 vu_log_debug (vui, "unknown vhost-user message %d received. "
889 "closing socket", msg.request);
896 vlib_worker_thread_barrier_sync (vm);
897 vhost_user_if_disconnect (vui);
898 vlib_worker_thread_barrier_release (vm);
899 vhost_user_update_iface_state (vui);
903 static clib_error_t *
904 vhost_user_socket_error (clib_file_t * uf)
906 vlib_main_t *vm = vlib_get_main ();
907 vhost_user_main_t *vum = &vhost_user_main;
908 vhost_user_intf_t *vui =
909 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
911 vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
912 vlib_worker_thread_barrier_sync (vm);
913 vhost_user_if_disconnect (vui);
914 vlib_worker_thread_barrier_release (vm);
918 static clib_error_t *
919 vhost_user_socksvr_accept_ready (clib_file_t * uf)
921 int client_fd, client_len;
922 struct sockaddr_un client;
923 clib_file_t template = { 0 };
924 vhost_user_main_t *vum = &vhost_user_main;
925 vhost_user_intf_t *vui;
927 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
929 client_len = sizeof (client);
930 client_fd = accept (uf->file_descriptor,
931 (struct sockaddr *) &client,
932 (socklen_t *) & client_len);
935 return clib_error_return_unix (0, "accept");
937 if (vui->clib_file_index != ~0)
939 vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
940 vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
941 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
944 vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
945 vui->sw_if_index, client_fd);
946 template.read_function = vhost_user_socket_read;
947 template.error_function = vhost_user_socket_error;
948 template.file_descriptor = client_fd;
949 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
950 vui->clib_file_index = clib_file_add (&file_main, &template);
954 static clib_error_t *
955 vhost_user_init (vlib_main_t * vm)
958 vhost_user_main_t *vum = &vhost_user_main;
959 vlib_thread_main_t *tm = vlib_get_thread_main ();
961 error = vlib_call_init_function (vm, ip4_init);
965 vum->log_default = vlib_log_register_class ("vhost-user", 0);
967 vum->coalesce_frames = 32;
968 vum->coalesce_time = 1e-3;
970 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
973 vec_foreach (cpu, vum->cpus)
975 /* This is actually not necessary as validate already zeroes it
976 * Just keeping the loop here for later because I am lazy. */
977 cpu->rx_buffers_len = 0;
980 vum->random = random_default_seed ();
982 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
987 VLIB_INIT_FUNCTION (vhost_user_init);
990 vhost_user_send_interrupt_process (vlib_main_t * vm,
991 vlib_node_runtime_t * rt, vlib_frame_t * f)
993 vhost_user_intf_t *vui;
994 f64 timeout = 3153600000.0 /* 100 years */ ;
995 uword event_type, *event_data = 0;
996 vhost_user_main_t *vum = &vhost_user_main;
998 f64 now, poll_time_remaining;
1004 poll_time_remaining =
1005 vlib_process_wait_for_event_or_clock (vm, timeout);
1006 event_type = vlib_process_get_events (vm, &event_data);
1007 vec_reset_length (event_data);
1010 * Use the remaining timeout if it is less than coalesce time to avoid
1011 * resetting the existing timer in the middle of expiration
1013 timeout = poll_time_remaining;
1014 if (vlib_process_suspend_time_is_zero (timeout) ||
1015 (timeout > vum->coalesce_time))
1016 timeout = vum->coalesce_time;
1018 now = vlib_time_now (vm);
1021 case VHOST_USER_EVENT_STOP_TIMER:
1025 case VHOST_USER_EVENT_START_TIMER:
1027 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1033 pool_foreach (vui, vum->vhost_user_interfaces, {
1034 next_timeout = timeout;
1035 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1037 vhost_user_vring_t *rxvq = &vui->vrings[qid];
1038 vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1040 if (txvq->qid == -1)
1042 if (txvq->n_since_last_int)
1044 if (now >= txvq->int_deadline)
1045 vhost_user_send_call (vm, txvq);
1047 next_timeout = txvq->int_deadline - now;
1050 if (rxvq->n_since_last_int)
1052 if (now >= rxvq->int_deadline)
1053 vhost_user_send_call (vm, rxvq);
1055 next_timeout = rxvq->int_deadline - now;
1058 if ((next_timeout < timeout) && (next_timeout > 0.0))
1059 timeout = next_timeout;
1066 clib_warning ("BUG: unhandled event type %d", event_type);
1069 /* No less than 1 millisecond */
1073 timeout = 3153600000.0;
1079 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
1080 .function = vhost_user_send_interrupt_process,
1081 .type = VLIB_NODE_TYPE_PROCESS,
1082 .name = "vhost-user-send-interrupt-process",
1087 vhost_user_process (vlib_main_t * vm,
1088 vlib_node_runtime_t * rt, vlib_frame_t * f)
1090 vhost_user_main_t *vum = &vhost_user_main;
1091 vhost_user_intf_t *vui;
1092 struct sockaddr_un sun;
1094 clib_file_t template = { 0 };
1095 f64 timeout = 3153600000.0 /* 100 years */ ;
1096 uword *event_data = 0;
1099 sun.sun_family = AF_UNIX;
1100 template.read_function = vhost_user_socket_read;
1101 template.error_function = vhost_user_socket_error;
1105 vlib_process_wait_for_event_or_clock (vm, timeout);
1106 vlib_process_get_events (vm, &event_data);
1107 vec_reset_length (event_data);
1112 pool_foreach (vui, vum->vhost_user_interfaces, {
1114 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1115 if (vui->clib_file_index == ~0)
1118 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1121 * 1st time error or new error for this interface,
1122 * spit out the message and record the error
1124 if (!vui->sock_errno || (vui->sock_errno != errno))
1127 ("Error: Could not open unix socket for %s",
1128 vui->sock_filename);
1129 vui->sock_errno = errno;
1134 /* try to connect */
1135 strncpy (sun.sun_path, (char *) vui->sock_filename,
1136 sizeof (sun.sun_path) - 1);
1138 /* Avoid hanging VPP if the other end does not accept */
1139 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1140 clib_unix_warning ("fcntl");
1142 if (connect (sockfd, (struct sockaddr *) &sun,
1143 sizeof (struct sockaddr_un)) == 0)
1145 /* Set the socket to blocking as it was before */
1146 if (fcntl(sockfd, F_SETFL, 0) < 0)
1147 clib_unix_warning ("fcntl2");
1149 vui->sock_errno = 0;
1150 template.file_descriptor = sockfd;
1151 template.private_data =
1152 vui - vhost_user_main.vhost_user_interfaces;
1153 vui->clib_file_index = clib_file_add (&file_main, &template);
1155 /* This sockfd is considered consumed */
1160 vui->sock_errno = errno;
1165 /* check if socket is alive */
1167 socklen_t len = sizeof (error);
1168 int fd = UNIX_GET_FD(vui->clib_file_index);
1170 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1174 vu_log_debug (vui, "getsockopt returned %d", retval);
1175 vhost_user_if_disconnect (vui);
1186 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1187 .function = vhost_user_process,
1188 .type = VLIB_NODE_TYPE_PROCESS,
1189 .name = "vhost-user-process",
1194 * Disables and reset interface structure.
1195 * It can then be either init again, or removed from used interfaces.
1198 vhost_user_term_if (vhost_user_intf_t * vui)
1201 vhost_user_main_t *vum = &vhost_user_main;
1203 // disconnect interface sockets
1204 vhost_user_if_disconnect (vui);
1205 vhost_user_update_iface_state (vui);
1207 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1209 // Remove existing queue mapping for the interface
1213 vnet_main_t *vnm = vnet_get_main ();
1214 vhost_user_vring_t *txvq = &vui->vrings[q];
1216 if (txvq->qid != -1)
1218 rv = vnet_hw_interface_unassign_rx_thread (vnm,
1222 vu_log_warn (vui, "unable to unassign interface %d, "
1223 "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1227 clib_mem_free ((void *) vui->vring_locks[q]);
1230 if (vui->unix_server_index != ~0)
1232 //Close server socket
1233 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
1234 vui->unix_server_index);
1235 clib_file_del (&file_main, uf);
1236 vui->unix_server_index = ~0;
1237 unlink (vui->sock_filename);
1240 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
1245 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1247 vhost_user_main_t *vum = &vhost_user_main;
1248 vhost_user_intf_t *vui;
1250 vnet_hw_interface_t *hwif;
1253 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1254 hwif->dev_class_index != vhost_user_device_class.index)
1255 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1257 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1259 vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1260 hwif->name, hwif->dev_instance);
1262 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1264 vhost_user_vring_t *txvq = &vui->vrings[qid];
1266 if (txvq->qid == -1)
1268 if ((vum->ifq_count > 0) &&
1269 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
1270 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
1273 // Stop the timer if there is no more interrupt interface/queue
1274 if ((vum->ifq_count == 0) &&
1275 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1277 vlib_process_signal_event (vm,
1278 vhost_user_send_interrupt_node.index,
1279 VHOST_USER_EVENT_STOP_TIMER, 0);
1285 // Disable and reset interface
1286 vhost_user_term_if (vui);
1288 // Reset renumbered iface
1289 if (hwif->dev_instance <
1290 vec_len (vum->show_dev_instance_by_real_dev_instance))
1291 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
1293 // Delete ethernet interface
1294 ethernet_delete_interface (vnm, vui->hw_if_index);
1297 pool_put (vum->vhost_user_interfaces, vui);
1302 static clib_error_t *
1303 vhost_user_exit (vlib_main_t * vm)
1305 vnet_main_t *vnm = vnet_get_main ();
1306 vhost_user_main_t *vum = &vhost_user_main;
1307 vhost_user_intf_t *vui;
1309 vlib_worker_thread_barrier_sync (vlib_get_main ());
1311 pool_foreach (vui, vum->vhost_user_interfaces, {
1312 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1315 vlib_worker_thread_barrier_release (vlib_get_main ());
1319 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1322 * Open server unix socket on specified sock_filename.
1325 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1328 struct sockaddr_un un = { };
1330 /* create listening socket */
1331 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1332 return VNET_API_ERROR_SYSCALL_ERROR_1;
1334 un.sun_family = AF_UNIX;
1335 strncpy ((char *) un.sun_path, (char *) sock_filename,
1336 sizeof (un.sun_path) - 1);
1338 /* remove if exists */
1339 unlink ((char *) sock_filename);
1341 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1343 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1347 if (listen (fd, 1) == -1)
1349 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1362 * Create ethernet interface for vhost user interface.
1365 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1366 vhost_user_intf_t * vui, u8 * hwaddress)
1368 vhost_user_main_t *vum = &vhost_user_main;
1370 clib_error_t *error;
1372 /* create hw and sw interface */
1375 clib_memcpy (hwaddr, hwaddress, 6);
1379 random_u32 (&vum->random);
1380 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1385 error = ethernet_register_interface
1387 vhost_user_device_class.index,
1388 vui - vum->vhost_user_interfaces /* device instance */ ,
1389 hwaddr /* ethernet address */ ,
1390 &vui->hw_if_index, 0 /* flag change */ );
1393 clib_error_report (error);
1397 * Initialize vui with specified attributes
1400 vhost_user_vui_init (vnet_main_t * vnm,
1401 vhost_user_intf_t * vui,
1403 const char *sock_filename,
1404 u64 feature_mask, u32 * sw_if_index)
1406 vnet_sw_interface_t *sw;
1408 vhost_user_main_t *vum = &vhost_user_main;
1409 vnet_hw_interface_t *hw;
1411 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1412 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1413 if (server_sock_fd != -1)
1415 clib_file_t template = { 0 };
1416 template.read_function = vhost_user_socksvr_accept_ready;
1417 template.file_descriptor = server_sock_fd;
1418 template.private_data = vui - vum->vhost_user_interfaces; //hw index
1419 vui->unix_server_index = clib_file_add (&file_main, &template);
1423 vui->unix_server_index = ~0;
1426 vui->sw_if_index = sw->sw_if_index;
1427 strncpy (vui->sock_filename, sock_filename,
1428 ARRAY_LEN (vui->sock_filename) - 1);
1429 vui->sock_errno = 0;
1431 vui->feature_mask = feature_mask;
1432 vui->clib_file_index = ~0;
1433 vui->log_base_addr = 0;
1434 vui->if_index = vui - vum->vhost_user_interfaces;
1435 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
1438 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1439 vhost_user_vring_init (vui, q);
1441 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1442 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1445 *sw_if_index = vui->sw_if_index;
1447 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1449 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1450 CLIB_CACHE_LINE_BYTES);
1451 clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1454 vec_validate (vui->per_cpu_tx_qid,
1455 vlib_get_thread_main ()->n_vlib_mains - 1);
1456 vhost_user_tx_thread_placement (vui);
1460 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1461 const char *sock_filename,
1465 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1467 vhost_user_intf_t *vui = NULL;
1470 int server_sock_fd = -1;
1471 vhost_user_main_t *vum = &vhost_user_main;
1474 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1476 return VNET_API_ERROR_INVALID_ARGUMENT;
1479 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1484 vui = &vum->vhost_user_interfaces[*if_index];
1485 *sw_if_index = vui->sw_if_index;
1487 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1493 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1499 /* Protect the uninitialized vui from being dispatched by rx/tx */
1500 vlib_worker_thread_barrier_sync (vm);
1501 pool_get (vhost_user_main.vhost_user_interfaces, vui);
1502 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1503 vlib_worker_thread_barrier_release (vm);
1505 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1506 feature_mask, &sw_if_idx);
1507 vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1508 vhost_user_rx_thread_placement (vui, 1);
1511 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1514 *sw_if_index = sw_if_idx;
1516 // Process node must connect
1517 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1523 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1524 const char *sock_filename,
1527 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1529 vhost_user_main_t *vum = &vhost_user_main;
1530 vhost_user_intf_t *vui = NULL;
1532 int server_sock_fd = -1;
1534 vnet_hw_interface_t *hwif;
1537 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
1538 hwif->dev_class_index != vhost_user_device_class.index)
1539 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1541 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1542 return VNET_API_ERROR_INVALID_ARGUMENT;
1544 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
1547 * Disallow changing the interface to have the same path name
1548 * as other interface
1550 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1551 if (if_index && (*if_index != vui->if_index))
1552 return VNET_API_ERROR_IF_ALREADY_EXISTS;
1554 // First try to open server socket
1556 if ((rv = vhost_user_init_server_sock (sock_filename,
1557 &server_sock_fd)) != 0)
1560 vhost_user_term_if (vui);
1561 vhost_user_vui_init (vnm, vui, server_sock_fd,
1562 sock_filename, feature_mask, &sw_if_idx);
1565 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1567 // Process node must connect
1568 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1574 vhost_user_connect_command_fn (vlib_main_t * vm,
1575 unformat_input_t * input,
1576 vlib_cli_command_t * cmd)
1578 unformat_input_t _line_input, *line_input = &_line_input;
1579 u8 *sock_filename = NULL;
1582 u64 feature_mask = (u64) ~ (0ULL);
1584 u32 custom_dev_instance = ~0;
1587 clib_error_t *error = NULL;
1589 /* Get a line of input. */
1590 if (!unformat_user (input, unformat_line_input, line_input))
1593 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1595 if (unformat (line_input, "socket %s", &sock_filename))
1597 else if (unformat (line_input, "server"))
1599 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1603 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1605 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1611 error = clib_error_return (0, "unknown input `%U'",
1612 format_unformat_error, line_input);
1617 vnet_main_t *vnm = vnet_get_main ();
1620 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1621 is_server, &sw_if_index, feature_mask,
1622 renumber, custom_dev_instance, hw)))
1624 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1628 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1632 vec_free (sock_filename);
1633 unformat_free (line_input);
1639 vhost_user_delete_command_fn (vlib_main_t * vm,
1640 unformat_input_t * input,
1641 vlib_cli_command_t * cmd)
1643 unformat_input_t _line_input, *line_input = &_line_input;
1644 u32 sw_if_index = ~0;
1645 vnet_main_t *vnm = vnet_get_main ();
1646 clib_error_t *error = NULL;
1648 /* Get a line of input. */
1649 if (!unformat_user (input, unformat_line_input, line_input))
1652 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1654 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1657 (line_input, "%U", unformat_vnet_sw_interface, vnm,
1660 vnet_hw_interface_t *hwif =
1661 vnet_get_sup_hw_interface (vnm, sw_if_index);
1663 vhost_user_device_class.index != hwif->dev_class_index)
1665 error = clib_error_return (0, "Not a vhost interface");
1671 error = clib_error_return (0, "unknown input `%U'",
1672 format_unformat_error, line_input);
1677 vhost_user_delete_if (vnm, vm, sw_if_index);
1680 unformat_free (line_input);
1686 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1687 vhost_user_intf_details_t ** out_vuids)
1690 vhost_user_main_t *vum = &vhost_user_main;
1691 vhost_user_intf_t *vui;
1692 vhost_user_intf_details_t *r_vuids = NULL;
1693 vhost_user_intf_details_t *vuid = NULL;
1694 u32 *hw_if_indices = 0;
1695 vnet_hw_interface_t *hi;
1702 pool_foreach (vui, vum->vhost_user_interfaces,
1703 vec_add1 (hw_if_indices, vui->hw_if_index);
1706 for (i = 0; i < vec_len (hw_if_indices); i++)
1708 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1709 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1711 vec_add2 (r_vuids, vuid, 1);
1712 vuid->sw_if_index = vui->sw_if_index;
1713 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1714 vuid->features = vui->features;
1715 vuid->num_regions = vui->nregions;
1716 vuid->is_server = vui->unix_server_index != ~0;
1717 vuid->sock_errno = vui->sock_errno;
1718 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1719 sizeof (vuid->sock_filename));
1720 vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1721 s = format (s, "%v%c", hi->name, 0);
1723 strncpy ((char *) vuid->if_name, (char *) s,
1724 ARRAY_LEN (vuid->if_name) - 1);
1729 vec_free (hw_if_indices);
1731 *out_vuids = r_vuids;
1737 show_vhost_user_command_fn (vlib_main_t * vm,
1738 unformat_input_t * input,
1739 vlib_cli_command_t * cmd)
1741 clib_error_t *error = 0;
1742 vnet_main_t *vnm = vnet_get_main ();
1743 vhost_user_main_t *vum = &vhost_user_main;
1744 vhost_user_intf_t *vui;
1745 u32 hw_if_index, *hw_if_indices = 0;
1746 vnet_hw_interface_t *hi;
1756 struct feat_struct *feat_entry;
1758 static struct feat_struct feat_array[] = {
1759 #define _(s,b) { .str = #s, .bit = b, },
1760 foreach_virtio_net_feature
1765 #define foreach_protocol_feature \
1766 _(VHOST_USER_PROTOCOL_F_MQ) \
1767 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1769 static struct feat_struct proto_feat_array[] = {
1770 #define _(s) { .str = #s, .bit = s},
1771 foreach_protocol_feature
1776 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1779 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1781 hi = vnet_get_hw_interface (vnm, hw_if_index);
1782 if (vhost_user_device_class.index != hi->dev_class_index)
1784 error = clib_error_return (0, "unknown input `%U'",
1785 format_unformat_error, input);
1788 vec_add1 (hw_if_indices, hw_if_index);
1790 else if (unformat (input, "descriptors") || unformat (input, "desc"))
1794 error = clib_error_return (0, "unknown input `%U'",
1795 format_unformat_error, input);
1799 if (vec_len (hw_if_indices) == 0)
1801 pool_foreach (vui, vum->vhost_user_interfaces,
1802 vec_add1 (hw_if_indices, vui->hw_if_index);
1805 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1806 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1807 vum->coalesce_frames, vum->coalesce_time);
1808 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
1811 for (i = 0; i < vec_len (hw_if_indices); i++)
1813 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1814 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1815 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1816 format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1819 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1820 " features mask (0x%llx): \n"
1821 " features (0x%llx): \n",
1822 vui->virtio_net_hdr_sz, vui->feature_mask,
1825 feat_entry = (struct feat_struct *) &feat_array;
1826 while (feat_entry->str)
1828 if (vui->features & (1ULL << feat_entry->bit))
1829 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1834 vlib_cli_output (vm, " protocol features (0x%llx)",
1835 vui->protocol_features);
1836 feat_entry = (struct feat_struct *) &proto_feat_array;
1837 while (feat_entry->str)
1839 if (vui->protocol_features & (1ULL << feat_entry->bit))
1840 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1845 vlib_cli_output (vm, "\n");
1847 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1849 (vui->unix_server_index != ~0) ? "server" : "client",
1850 strerror (vui->sock_errno));
1852 vlib_cli_output (vm, " rx placement: ");
1854 for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1856 vnet_main_t *vnm = vnet_get_main ();
1858 vnet_hw_interface_rx_mode mode;
1859 vhost_user_vring_t *txvq = &vui->vrings[qid];
1861 if (txvq->qid == -1)
1864 vnet_get_device_input_thread_index (vnm, vui->hw_if_index,
1866 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
1868 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1870 format_vnet_hw_interface_rx_mode, mode);
1873 vlib_cli_output (vm, " tx placement: %s\n",
1874 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1876 vec_foreach_index (ci, vui->per_cpu_tx_qid)
1878 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1879 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1882 vlib_cli_output (vm, "\n");
1884 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1888 vlib_cli_output (vm,
1889 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1890 vlib_cli_output (vm,
1891 " ====== ===== ================== ================== ================== ================== ==================\n");
1893 for (j = 0; j < vui->nregions; j++)
1895 vlib_cli_output (vm,
1896 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1897 j, vui->region_mmap_fd[j],
1898 vui->regions[j].guest_phys_addr,
1899 vui->regions[j].memory_size,
1900 vui->regions[j].userspace_addr,
1901 vui->regions[j].mmap_offset,
1902 pointer_to_uword (vui->region_mmap_addr[j]));
1904 for (q = 0; q < VHOST_VRING_MAX_N; q++)
1906 if (!vui->vrings[q].started)
1909 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1910 (q & 1) ? "RX" : "TX",
1911 vui->vrings[q].enabled ? "" : " disabled");
1913 vlib_cli_output (vm,
1914 " qsz %d last_avail_idx %d last_used_idx %d\n",
1915 vui->vrings[q].qsz_mask + 1,
1916 vui->vrings[q].last_avail_idx,
1917 vui->vrings[q].last_used_idx);
1919 if (vui->vrings[q].avail && vui->vrings[q].used)
1920 vlib_cli_output (vm,
1921 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1922 vui->vrings[q].avail->flags,
1923 vui->vrings[q].avail->idx,
1924 vui->vrings[q].used->flags,
1925 vui->vrings[q].used->idx);
1927 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1928 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1929 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1930 kickfd, callfd, vui->vrings[q].errfd);
1934 vlib_cli_output (vm, "\n descriptor table:\n");
1935 vlib_cli_output (vm,
1936 " id addr len flags next user_addr\n");
1937 vlib_cli_output (vm,
1938 " ===== ================== ===== ====== ===== ==================\n");
1939 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1942 vlib_cli_output (vm,
1943 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1944 j, vui->vrings[q].desc[j].addr,
1945 vui->vrings[q].desc[j].len,
1946 vui->vrings[q].desc[j].flags,
1947 vui->vrings[q].desc[j].next,
1948 pointer_to_uword (map_guest_mem
1950 vui->vrings[q].desc[j].
1955 vlib_cli_output (vm, "\n");
1958 vec_free (hw_if_indices);
1967 * Create a vHost User interface. Once created, a new virtual interface
1968 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
1969 * is the next free index.
1971 * There are several parameters associated with a vHost interface:
1973 * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
1974 * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
1975 * create the socket if it does not already exist. If in '<em>client</em>' mode,
1976 * hypervisor will create the socket if it does not already exist. The VPP code
1977 * is indifferent to the file location. However, if SELinux is enabled, then the
1978 * socket needs to be created in '<em>/var/run/vpp/</em>'.
1980 * - <b>server</b> - Optional flag to indicate that VPP should be the server for
1981 * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
1982 * mode, the VM can be reset without tearing down the vHost Interface. In
1983 * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
1984 * tearing down the vHost Interface.
1986 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
1987 * startup. <b>This is intended for degugging only.</b> It is recommended that this
1988 * parameter not be used except by experienced users. By default, all supported
1989 * features will be advertised. Otherwise, provide the set of features desired.
1990 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
1991 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
1992 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
1993 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
1994 * - 0x004000000 (26) - VHOST_F_LOG_ALL
1995 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
1996 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
1997 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
1998 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2000 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2001 * X:X:X:X:X:X unix or X.X.X cisco format.
2003 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2004 * in the name to be specified. If instance already exists, name will be used
2005 * anyway and multiple instances will have the same name. Use with caution.
2008 * Example of how to create a vhost interface with VPP as the client and all features enabled:
2009 * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2010 * VirtualEthernet0/0/0
2012 * Example of how to create a vhost interface with VPP as the server and with just
2013 * multiple queues enabled:
2014 * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2015 * VirtualEthernet0/0/1
2017 * Once the vHost interface is created, enable the interface using:
2018 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2021 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2022 .path = "create vhost-user",
2023 .short_help = "create vhost-user socket <socket-filename> [server] "
2024 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
2025 .function = vhost_user_connect_command_fn,
2031 * Delete a vHost User interface using the interface name or the
2032 * software interface index. Use the '<em>show interface</em>'
2033 * command to determine the software interface index. On deletion,
2034 * the linux socket will not be deleted.
2037 * Example of how to delete a vhost interface by name:
2038 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2039 * Example of how to delete a vhost interface by software interface index:
2040 * @cliexcmd{delete vhost-user sw_if_index 1}
2043 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2044 .path = "delete vhost-user",
2045 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2046 .function = vhost_user_delete_command_fn,
2050 * Display the attributes of a single vHost User interface (provide interface
2051 * name), multiple vHost User interfaces (provide a list of interface names seperated
2052 * by spaces) or all Vhost User interfaces (omit an interface name to display all
2053 * vHost interfaces).
2057 * Example of how to display a vhost interface:
2058 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2059 * Virtio vhost-user interfaces
2061 * coalesce frames 32 time 1e-3
2062 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2063 * virtio_net_hdr_sz 12
2064 * features mask (0xffffffffffffffff):
2065 * features (0x50408000):
2066 * VIRTIO_NET_F_MRG_RXBUF (15)
2067 * VIRTIO_NET_F_MQ (22)
2068 * VIRTIO_F_INDIRECT_DESC (28)
2069 * VHOST_USER_F_PROTOCOL_FEATURES (30)
2070 * protocol features (0x3)
2071 * VHOST_USER_PROTOCOL_F_MQ (0)
2072 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2074 * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2077 * thread 1 on vring 1
2078 * thread 1 on vring 5
2079 * thread 2 on vring 3
2080 * thread 2 on vring 7
2081 * tx placement: spin-lock
2082 * thread 0 on vring 0
2083 * thread 1 on vring 2
2084 * thread 2 on vring 0
2086 * Memory regions (total 2)
2087 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2088 * ====== ===== ================== ================== ================== ================== ==================
2089 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2090 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2093 * qsz 256 last_avail_idx 0 last_used_idx 0
2094 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2095 * kickfd 62 callfd 64 errfd -1
2098 * qsz 256 last_avail_idx 0 last_used_idx 0
2099 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2100 * kickfd 65 callfd 66 errfd -1
2103 * qsz 256 last_avail_idx 0 last_used_idx 0
2104 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2105 * kickfd 63 callfd 70 errfd -1
2108 * qsz 256 last_avail_idx 0 last_used_idx 0
2109 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2110 * kickfd 72 callfd 74 errfd -1
2112 * Virtqueue 4 (TX disabled)
2113 * qsz 256 last_avail_idx 0 last_used_idx 0
2114 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2115 * kickfd 76 callfd 78 errfd -1
2117 * Virtqueue 5 (RX disabled)
2118 * qsz 256 last_avail_idx 0 last_used_idx 0
2119 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2120 * kickfd 80 callfd 82 errfd -1
2122 * Virtqueue 6 (TX disabled)
2123 * qsz 256 last_avail_idx 0 last_used_idx 0
2124 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2125 * kickfd 84 callfd 86 errfd -1
2127 * Virtqueue 7 (RX disabled)
2128 * qsz 256 last_avail_idx 0 last_used_idx 0
2129 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2130 * kickfd 88 callfd 90 errfd -1
2134 * The optional '<em>descriptors</em>' parameter will display the same output as
2135 * the previous example but will include the descriptor table for each queue.
2136 * The output is truncated below:
2137 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2138 * Virtio vhost-user interfaces
2140 * coalesce frames 32 time 1e-3
2141 * Interface: VirtualEthernet0/0/0 (ifindex 1)
2142 * virtio_net_hdr_sz 12
2143 * features mask (0xffffffffffffffff):
2144 * features (0x50408000):
2145 * VIRTIO_NET_F_MRG_RXBUF (15)
2146 * VIRTIO_NET_F_MQ (22)
2149 * qsz 256 last_avail_idx 0 last_used_idx 0
2150 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2151 * kickfd 62 callfd 64 errfd -1
2154 * id addr len flags next user_addr
2155 * ===== ================== ===== ====== ===== ==================
2156 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2157 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2158 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2159 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2160 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2161 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2162 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2163 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2164 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2165 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2166 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2168 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2169 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2170 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2171 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2172 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2173 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2174 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2177 * qsz 256 last_avail_idx 0 last_used_idx 0
2183 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2184 .path = "show vhost-user",
2185 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2186 .function = show_vhost_user_command_fn,
2191 static clib_error_t *
2192 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2194 vhost_user_main_t *vum = &vhost_user_main;
2196 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2198 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2200 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2202 else if (unformat (input, "dont-dump-memory"))
2203 vum->dont_dump_vhost_user_memory = 1;
2205 return clib_error_return (0, "unknown input `%U'",
2206 format_unformat_error, input);
2212 /* vhost-user { ... } configuration. */
2213 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2216 vhost_user_unmap_all (void)
2218 vhost_user_main_t *vum = &vhost_user_main;
2219 vhost_user_intf_t *vui;
2221 if (vum->dont_dump_vhost_user_memory)
2223 pool_foreach (vui, vum->vhost_user_interfaces,
2224 unmap_all_mem_regions (vui);
2230 * fd.io coding-style-patch-verification: ON
2233 * eval: (c-set-style "gnu")