2 *------------------------------------------------------------------
5 * Copyright (c) 2014 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>
40 #include <vnet/devices/virtio/vhost-user.h>
42 #define VHOST_USER_DEBUG_SOCKET 0
43 #define VHOST_USER_DEBUG_VQ 0
45 /* Set to get virtio_net_hdr in buffer pre-data
46 details will be shown in packet trace */
47 #define VHOST_USER_COPY_TX_HDR 0
49 #if VHOST_USER_DEBUG_SOCKET == 1
50 #define DBG_SOCK(args...) clib_warning(args);
52 #define DBG_SOCK(args...)
55 #if VHOST_USER_DEBUG_VQ == 1
56 #define DBG_VQ(args...) clib_warning(args);
58 #define DBG_VQ(args...)
61 vlib_node_registration_t vhost_user_input_node;
63 #define foreach_vhost_user_tx_func_error \
65 _(NOT_READY, "vhost user state error") \
66 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
67 _(MMAP_FAIL, "mmap failure")
71 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
72 foreach_vhost_user_tx_func_error
74 VHOST_USER_TX_FUNC_N_ERROR,
75 } vhost_user_tx_func_error_t;
77 static char *vhost_user_tx_func_error_strings[] = {
79 foreach_vhost_user_tx_func_error
83 #define foreach_vhost_user_input_func_error \
84 _(NO_ERROR, "no error") \
85 _(NO_BUFFER, "no available buffer") \
86 _(MMAP_FAIL, "mmap failure") \
87 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)")
91 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
92 foreach_vhost_user_input_func_error
94 VHOST_USER_INPUT_FUNC_N_ERROR,
95 } vhost_user_input_func_error_t;
97 static char *vhost_user_input_func_error_strings[] = {
99 foreach_vhost_user_input_func_error
104 static vhost_user_main_t vhost_user_main = {
108 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
109 .name = "vhost-user",
114 format_vhost_user_interface_name (u8 * s, va_list * args)
116 u32 i = va_arg (*args, u32);
117 u32 show_dev_instance = ~0;
118 vhost_user_main_t *vum = &vhost_user_main;
120 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
121 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
123 if (show_dev_instance != ~0)
124 i = show_dev_instance;
126 s = format (s, "VirtualEthernet0/0/%d", i);
131 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
133 vhost_user_main_t *vum = &vhost_user_main;
135 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
136 hi->dev_instance, ~0);
138 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
141 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
142 hi->dev_instance, new_dev_instance);
149 map_guest_mem (vhost_user_intf_t * vui, uword addr)
152 for (i = 0; i < vui->nregions; i++)
154 if ((vui->regions[i].guest_phys_addr <= addr) &&
155 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
158 return (void *) (vui->region_mmap_addr[i] + addr -
159 vui->regions[i].guest_phys_addr);
162 DBG_VQ ("failed to map guest mem addr %llx", addr);
167 map_user_mem (vhost_user_intf_t * vui, uword addr)
170 for (i = 0; i < vui->nregions; i++)
172 if ((vui->regions[i].userspace_addr <= addr) &&
173 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
176 return (void *) (vui->region_mmap_addr[i] + addr -
177 vui->regions[i].userspace_addr);
184 get_huge_page_size (int fd)
192 unmap_all_mem_regions (vhost_user_intf_t * vui)
195 for (i = 0; i < vui->nregions; i++)
197 if (vui->region_mmap_addr[i] != (void *) -1)
200 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
202 ssize_t map_sz = (vui->regions[i].memory_size +
203 vui->regions[i].mmap_offset +
204 page_sz) & ~(page_sz - 1);
207 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
211 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
212 vui->region_mmap_addr[i], map_sz, page_sz);
214 vui->region_mmap_addr[i] = (void *) -1;
218 clib_warning ("failed to unmap memory region (errno %d)",
221 close (vui->region_mmap_fd[i]);
228 static clib_error_t *
229 vhost_user_callfd_read_ready (unix_file_t * uf)
231 __attribute__ ((unused)) int n;
233 n = read (uf->file_descriptor, ((char *) &buff), 8);
238 vhost_user_if_disconnect (vhost_user_intf_t * vui)
240 vhost_user_main_t *vum = &vhost_user_main;
241 vnet_main_t *vnm = vnet_get_main ();
244 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
246 if (vui->unix_file_index != ~0)
248 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
249 vui->unix_file_index = ~0;
252 hash_unset (vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
253 hash_unset (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
254 close (vui->unix_fd);
257 for (q = 0; q < vui->num_vrings; q++)
259 vui->vrings[q].desc = NULL;
260 vui->vrings[q].avail = NULL;
261 vui->vrings[q].used = NULL;
262 vui->vrings[q].log_guest_addr = 0;
263 vui->vrings[q].log_used = 0;
266 unmap_all_mem_regions (vui);
267 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
270 #define VHOST_LOG_PAGE 0x1000
272 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
274 if (PREDICT_TRUE (vui->log_base_addr == 0
275 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
279 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
281 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
285 CLIB_MEMORY_BARRIER ();
286 u64 page = addr / VHOST_LOG_PAGE;
287 while (page * VHOST_LOG_PAGE < addr + len)
289 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
294 #define vhost_user_log_dirty_ring(vui, vq, member) \
295 if (PREDICT_FALSE(vq->log_used)) { \
296 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
297 sizeof(vq->used->member)); \
300 static clib_error_t *
301 vhost_user_socket_read (unix_file_t * uf)
304 int fd, number_of_fds = 0;
305 int fds[VHOST_MEMORY_MAX_NREGIONS];
306 vhost_user_msg_t msg;
309 vhost_user_main_t *vum = &vhost_user_main;
310 vhost_user_intf_t *vui;
311 struct cmsghdr *cmsg;
314 unix_file_t template = { 0 };
315 vnet_main_t *vnm = vnet_get_main ();
317 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
318 uf->file_descriptor);
321 DBG_SOCK ("FD %d doesn't belong to any interface", uf->file_descriptor);
325 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
327 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
329 memset (&mh, 0, sizeof (mh));
330 memset (control, 0, sizeof (control));
332 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
335 /* set the payload */
336 iov[0].iov_base = (void *) &msg;
337 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
341 mh.msg_control = control;
342 mh.msg_controllen = sizeof (control);
344 n = recvmsg (uf->file_descriptor, &mh, 0);
346 if (n != VHOST_USER_MSG_HDR_SZ)
349 if (mh.msg_flags & MSG_CTRUNC)
354 cmsg = CMSG_FIRSTHDR (&mh);
356 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
357 (cmsg->cmsg_type == SCM_RIGHTS) &&
358 (cmsg->cmsg_len - CMSG_LEN (0) <=
359 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
361 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
362 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
365 /* version 1, no reply bit set */
366 if ((msg.flags & 7) != 1)
368 DBG_SOCK ("malformed message received. closing socket");
373 int rv __attribute__ ((unused));
374 /* $$$$ pay attention to rv */
375 rv = read (uf->file_descriptor, ((char *) &msg) + n, msg.size);
380 case VHOST_USER_GET_FEATURES:
381 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES", vui->hw_if_index);
384 msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
385 (1 << FEAT_VIRTIO_F_ANY_LAYOUT) |
386 (1 << FEAT_VHOST_F_LOG_ALL) |
387 (1 << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
388 (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES);
389 msg.u64 &= vui->feature_mask;
391 msg.size = sizeof (msg.u64);
394 case VHOST_USER_SET_FEATURES:
395 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
396 vui->hw_if_index, msg.u64);
398 vui->features = msg.u64;
400 if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
401 vui->virtio_net_hdr_sz = 12;
403 vui->virtio_net_hdr_sz = 10;
406 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
408 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
409 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
412 for (q = 0; q < 2; q++)
414 vui->vrings[q].desc = 0;
415 vui->vrings[q].avail = 0;
416 vui->vrings[q].used = 0;
417 vui->vrings[q].log_guest_addr = 0;
418 vui->vrings[q].log_used = 0;
421 DBG_SOCK ("interface %d disconnected", vui->sw_if_index);
425 case VHOST_USER_SET_MEM_TABLE:
426 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
427 vui->hw_if_index, msg.memory.nregions);
429 if ((msg.memory.nregions < 1) ||
430 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
433 DBG_SOCK ("number of mem regions must be between 1 and %i",
434 VHOST_MEMORY_MAX_NREGIONS);
439 if (msg.memory.nregions != number_of_fds)
441 DBG_SOCK ("each memory region must have FD");
444 unmap_all_mem_regions (vui);
445 for (i = 0; i < msg.memory.nregions; i++)
447 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
448 sizeof (vhost_user_memory_region_t));
450 long page_sz = get_huge_page_size (fds[i]);
452 /* align size to 2M page */
453 ssize_t map_sz = (vui->regions[i].memory_size +
454 vui->regions[i].mmap_offset +
455 page_sz) & ~(page_sz - 1);
457 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
458 MAP_SHARED, fds[i], 0);
461 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
462 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
465 if (vui->region_mmap_addr[i] == MAP_FAILED)
467 clib_warning ("failed to map memory. errno is %d", errno);
470 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
471 vui->region_mmap_fd[i] = fds[i];
473 vui->nregions = msg.memory.nregions;
476 case VHOST_USER_SET_VRING_NUM:
477 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
478 vui->hw_if_index, msg.state.index, msg.state.num);
480 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
481 (msg.state.num == 0) || /* it cannot be zero */
482 (msg.state.num % 2)) /* must be power of 2 */
484 vui->vrings[msg.state.index].qsz = msg.state.num;
487 case VHOST_USER_SET_VRING_ADDR:
488 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
489 vui->hw_if_index, msg.state.index);
491 vui->vrings[msg.state.index].desc = (vring_desc_t *)
492 map_user_mem (vui, msg.addr.desc_user_addr);
493 vui->vrings[msg.state.index].used = (vring_used_t *)
494 map_user_mem (vui, msg.addr.used_user_addr);
495 vui->vrings[msg.state.index].avail = (vring_avail_t *)
496 map_user_mem (vui, msg.addr.avail_user_addr);
498 if ((vui->vrings[msg.state.index].desc == NULL) ||
499 (vui->vrings[msg.state.index].used == NULL) ||
500 (vui->vrings[msg.state.index].avail == NULL))
502 DBG_SOCK ("failed to map user memory for hw_if_index %d",
507 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
508 vui->vrings[msg.state.index].log_used =
509 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
511 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
512 the ring is initialized in an enabled state. */
514 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
516 vui->vrings[msg.state.index].enabled = 1;
519 vui->vrings[msg.state.index].last_used_idx =
520 vui->vrings[msg.state.index].used->idx;
522 /* tell driver that we don't want interrupts */
523 vui->vrings[msg.state.index].used->flags |= 1;
526 case VHOST_USER_SET_OWNER:
527 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
530 case VHOST_USER_RESET_OWNER:
531 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
534 case VHOST_USER_SET_VRING_CALL:
535 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
536 vui->hw_if_index, msg.u64);
538 q = (u8) (msg.u64 & 0xFF);
540 if (!(msg.u64 & 0x100))
542 if (number_of_fds != 1)
545 /* if there is old fd, delete it */
546 if (vui->vrings[q].callfd)
548 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
549 vui->vrings[q].callfd_idx);
550 unix_file_del (&unix_main, uf);
552 vui->vrings[q].callfd = fds[0];
553 template.read_function = vhost_user_callfd_read_ready;
554 template.file_descriptor = fds[0];
555 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
558 vui->vrings[q].callfd = -1;
561 case VHOST_USER_SET_VRING_KICK:
562 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
563 vui->hw_if_index, msg.u64);
565 q = (u8) (msg.u64 & 0xFF);
567 if (!(msg.u64 & 0x100))
569 if (number_of_fds != 1)
572 vui->vrings[q].kickfd = fds[0];
575 vui->vrings[q].kickfd = -1;
578 case VHOST_USER_SET_VRING_ERR:
579 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
580 vui->hw_if_index, msg.u64);
582 q = (u8) (msg.u64 & 0xFF);
584 if (!(msg.u64 & 0x100))
586 if (number_of_fds != 1)
594 vui->vrings[q].errfd = fd;
597 case VHOST_USER_SET_VRING_BASE:
598 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
599 vui->hw_if_index, msg.state.index, msg.state.num);
601 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
604 case VHOST_USER_GET_VRING_BASE:
605 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
606 vui->hw_if_index, msg.state.index, msg.state.num);
608 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
609 vui->vrings[msg.state.index].enabled = 0;
611 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
613 msg.size = sizeof (msg.state);
616 case VHOST_USER_NONE:
617 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
621 case VHOST_USER_SET_LOG_BASE:
623 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
625 if (msg.size != sizeof (msg.log))
628 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
629 msg.size, sizeof (msg.log));
634 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
637 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
642 /* align size to 2M page */
643 long page_sz = get_huge_page_size (fd);
645 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
647 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
651 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
652 map_sz, msg.log.offset, fd, vui->log_base_addr);
654 if (vui->log_base_addr == MAP_FAILED)
656 clib_warning ("failed to map memory. errno is %d", errno);
660 vui->log_base_addr += msg.log.offset;
661 vui->log_size = msg.log.size;
664 msg.size = sizeof (msg.u64);
669 case VHOST_USER_SET_LOG_FD:
670 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
674 case VHOST_USER_GET_PROTOCOL_FEATURES:
675 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
679 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD);
680 msg.size = sizeof (msg.u64);
683 case VHOST_USER_SET_PROTOCOL_FEATURES:
684 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
685 vui->hw_if_index, msg.u64);
687 vui->protocol_features = msg.u64;
691 case VHOST_USER_SET_VRING_ENABLE:
692 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE, enable: %d",
693 vui->hw_if_index, msg.state.num);
694 vui->vrings[msg.state.index].enabled = msg.state.num;
698 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
703 /* if we have pointers to descriptor table, go up */
705 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
706 vui->vrings[VHOST_NET_VRING_IDX_RX].desc)
709 DBG_SOCK ("interface %d connected", vui->sw_if_index);
711 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
712 VNET_HW_INTERFACE_FLAG_LINK_UP);
717 /* if we need to reply */
721 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
722 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
729 vhost_user_if_disconnect (vui);
733 static clib_error_t *
734 vhost_user_socket_error (unix_file_t * uf)
736 vhost_user_main_t *vum = &vhost_user_main;
737 vhost_user_intf_t *vui;
740 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
741 uf->file_descriptor);
744 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
748 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
750 vhost_user_if_disconnect (vui);
754 static clib_error_t *
755 vhost_user_socksvr_accept_ready (unix_file_t * uf)
757 int client_fd, client_len;
758 struct sockaddr_un client;
759 unix_file_t template = { 0 };
760 vhost_user_main_t *vum = &vhost_user_main;
761 vhost_user_intf_t *vui;
764 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
765 uf->file_descriptor);
768 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
772 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
774 client_len = sizeof (client);
775 client_fd = accept (uf->file_descriptor,
776 (struct sockaddr *) &client,
777 (socklen_t *) & client_len);
780 return clib_error_return_unix (0, "accept");
782 template.read_function = vhost_user_socket_read;
783 template.error_function = vhost_user_socket_error;
784 template.file_descriptor = client_fd;
785 vui->unix_file_index = unix_file_add (&unix_main, &template);
787 vui->client_fd = client_fd;
788 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
789 vui - vum->vhost_user_interfaces);
794 static clib_error_t *
795 vhost_user_init (vlib_main_t * vm)
798 vhost_user_main_t *vum = &vhost_user_main;
799 vlib_thread_main_t *tm = vlib_get_thread_main ();
801 error = vlib_call_init_function (vm, ip4_init);
805 vum->vhost_user_interface_index_by_listener_fd =
806 hash_create (0, sizeof (uword));
807 vum->vhost_user_interface_index_by_sock_fd =
808 hash_create (0, sizeof (uword));
809 vum->vhost_user_interface_index_by_sw_if_index =
810 hash_create (0, sizeof (uword));
811 vum->coalesce_frames = 32;
812 vum->coalesce_time = 1e-3;
814 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
815 CLIB_CACHE_LINE_BYTES);
820 VLIB_INIT_FUNCTION (vhost_user_init);
822 static clib_error_t *
823 vhost_user_exit (vlib_main_t * vm)
829 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
833 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
834 VHOST_USER_RX_NEXT_DROP,
835 VHOST_USER_RX_N_NEXT,
843 #if VHOST_USER_COPY_TX_HDR == 1
844 virtio_net_hdr_t hdr;
846 } vhost_user_input_trace_t;
849 format_vhost_user_input_trace (u8 * s, va_list * va)
851 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
852 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
853 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
854 vhost_user_main_t *vum = &vhost_user_main;
855 vhost_user_input_trace_t *t = va_arg (*va, vhost_user_input_trace_t *);
856 vhost_user_intf_t *vui = vec_elt_at_index (vum->vhost_user_interfaces,
859 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
861 #if VHOST_USER_COPY_TX_HDR == 1
862 uword indent = format_get_indent (s);
865 s = format (s, "%U virtqueue %d",
866 format_vnet_sw_interface_name, vnm, sw, t->virtqueue);
868 #if VHOST_USER_COPY_TX_HDR == 1
869 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
870 format_white_space, indent,
871 t->hdr.flags, t->hdr.gso_type, t->hdr.hdr_len);
878 vhost_user_rx_trace (vlib_main_t * vm,
879 vlib_node_runtime_t * node,
880 vhost_user_intf_t * vui, i16 virtqueue)
883 vhost_user_main_t *vum = &vhost_user_main;
885 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
887 n_left = vec_len (vui->d_trace_buffers);
888 b = vui->d_trace_buffers;
894 vhost_user_input_trace_t *t0;
899 b0 = vlib_get_buffer (vm, bi0);
900 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
901 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
902 t0->virtqueue = virtqueue;
903 t0->device_index = vui - vum->vhost_user_interfaces;
904 #if VHOST_USER_COPY_TX_HDR == 1
905 clib_memcpy (&t0->hdr, b0->pre_data, sizeof (virtio_net_hdr_t));
913 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
915 vhost_user_main_t *vum = &vhost_user_main;
917 int rv __attribute__ ((unused));
918 /* $$$$ pay attention to rv */
919 rv = write (vq->callfd, &x, sizeof (x));
920 vq->n_since_last_int = 0;
921 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
926 vhost_user_if_input (vlib_main_t * vm,
927 vhost_user_main_t * vum,
928 vhost_user_intf_t * vui, vlib_node_runtime_t * node)
930 vhost_user_vring_t *txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
931 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
932 uword n_rx_packets = 0, n_rx_bytes = 0;
934 u32 n_left_to_next, *to_next;
937 uword n_trace = vlib_get_trace_count (vm, node);
939 u32 cpu_index, rx_len, drops, flush;
940 f64 now = vlib_time_now (vm);
942 vec_reset_length (vui->d_trace_buffers);
944 /* no descriptor ptr - bail out */
945 if (PREDICT_FALSE (!txvq->desc || !txvq->avail || !txvq->enabled))
948 /* do we have pending intterupts ? */
949 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
950 vhost_user_send_call (vm, txvq);
952 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
953 vhost_user_send_call (vm, rxvq);
955 /* only bit 0 of avail.flags is used so we don't want to deal with this
956 interface if any other bit is set */
957 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
961 if (txvq->avail->idx == txvq->last_avail_idx)
964 if (PREDICT_TRUE (txvq->avail->idx > txvq->last_avail_idx))
965 n_left = txvq->avail->idx - txvq->last_avail_idx;
967 n_left = (u16) - 1 - txvq->last_avail_idx + txvq->avail->idx;
969 if (PREDICT_FALSE (!vui->admin_up))
971 /* if intf is admin down, just drop all packets waiting in the ring */
972 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
973 CLIB_MEMORY_BARRIER ();
974 txvq->used->idx = txvq->last_used_idx;
975 vhost_user_log_dirty_ring (vui, txvq, idx);
976 vhost_user_send_call (vm, txvq);
980 if (PREDICT_FALSE (n_left > txvq->qsz))
983 qsz_mask = txvq->qsz - 1;
984 cpu_index = os_get_cpu_number ();
988 if (n_left > VLIB_FRAME_SIZE)
989 n_left = VLIB_FRAME_SIZE;
991 /* Allocate some buffers.
992 * Note that buffers that are chained for jumbo
993 * frames are allocated separately using a slower path.
994 * The idea is to be certain to have enough buffers at least
995 * to cycle through the descriptors without having to check for errors.
996 * For jumbo frames, the bottleneck is memory copy anyway.
998 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1000 vec_alloc (vum->rx_buffers[cpu_index], VLIB_FRAME_SIZE);
1002 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1003 flush = n_left; //Drop all input
1006 if (PREDICT_FALSE (_vec_len (vum->rx_buffers[cpu_index]) < n_left))
1008 _vec_len (vum->rx_buffers[cpu_index]) +=
1009 vlib_buffer_alloc_from_free_list (vm,
1010 vum->rx_buffers[cpu_index] +
1011 _vec_len (vum->rx_buffers
1014 _vec_len (vum->rx_buffers
1016 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1018 if (PREDICT_FALSE (n_left > _vec_len (vum->rx_buffers[cpu_index])))
1019 flush = n_left - _vec_len (vum->rx_buffers[cpu_index]);
1022 if (PREDICT_FALSE (flush))
1024 //Remove some input buffers
1027 vlib_error_count (vm, vhost_user_input_node.index,
1028 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1031 u16 desc_chain_head =
1032 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1033 txvq->last_avail_idx++;
1034 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1036 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1037 vhost_user_log_dirty_ring (vui, txvq,
1038 ring[txvq->last_used_idx & qsz_mask]);
1039 txvq->last_used_idx++;
1044 rx_len = vec_len (vum->rx_buffers[cpu_index]); //vector might be null
1047 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1049 while (n_left > 0 && n_left_to_next > 0)
1051 vlib_buffer_t *b_head, *b_current;
1052 u32 bi_head, bi_current;
1053 u16 desc_chain_head, desc_current;
1054 u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
1056 desc_chain_head = desc_current =
1057 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1058 bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
1059 b_head = b_current = vlib_get_buffer (vm, bi_head);
1060 vlib_buffer_chain_init (b_head);
1063 if (PREDICT_TRUE (vui->is_any_layout) ||
1064 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))
1066 /* ANYLAYOUT or single buffer */
1067 offset = vui->virtio_net_hdr_sz;
1071 /* CSR case without ANYLAYOUT, skip 1st buffer */
1072 offset = txvq->desc[desc_current].len;
1078 map_guest_mem (vui, txvq->desc[desc_current].addr);
1079 if (PREDICT_FALSE (buffer_addr == 0))
1081 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
1085 #if VHOST_USER_COPY_TX_HDR == 1
1086 if (PREDICT_TRUE (offset))
1087 clib_memcpy (b->pre_data, buffer_addr, sizeof (virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
1090 if (txvq->desc[desc_current].len > offset)
1092 u16 len = txvq->desc[desc_current].len - offset;
1093 u16 copied = vlib_buffer_chain_append_data_with_alloc (vm,
1094 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
1104 error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
1110 /* if next flag is set, take next desc in the chain */
1111 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1112 desc_current = txvq->desc[desc_current].next;
1117 /* consume the descriptor and return it as used */
1118 txvq->last_avail_idx++;
1119 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1121 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1122 vhost_user_log_dirty_ring (vui, txvq,
1123 ring[txvq->last_used_idx & qsz_mask]);
1124 txvq->last_used_idx++;
1126 if (PREDICT_FALSE (b_head->current_length < 14 &&
1127 error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1129 error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
1132 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1134 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1135 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1136 b_head->error = node->errors[error];
1138 if (PREDICT_FALSE (n_trace > n_rx_packets))
1139 vec_add1 (vui->d_trace_buffers, bi_head);
1141 if (PREDICT_FALSE (error))
1144 next0 = VHOST_USER_RX_NEXT_DROP;
1149 b_head->current_length +
1150 b_head->total_length_not_including_first_buffer;
1152 next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
1155 to_next[0] = bi_head;
1158 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1159 to_next, n_left_to_next,
1164 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1167 if (PREDICT_TRUE (vum->rx_buffers[cpu_index] != 0))
1168 _vec_len (vum->rx_buffers[cpu_index]) = rx_len;
1170 /* give buffers back to driver */
1171 CLIB_MEMORY_BARRIER ();
1172 txvq->used->idx = txvq->last_used_idx;
1173 vhost_user_log_dirty_ring (vui, txvq, idx);
1175 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
1177 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
1178 vlib_set_trace_count (vm, node,
1179 n_trace - vec_len (vui->d_trace_buffers));
1182 /* interrupt (call) handling */
1183 if ((txvq->callfd > 0) && !(txvq->avail->flags & 1))
1185 txvq->n_since_last_int += n_rx_packets;
1187 if (txvq->n_since_last_int > vum->coalesce_frames)
1188 vhost_user_send_call (vm, txvq);
1191 if (PREDICT_FALSE (drops))
1193 vlib_increment_simple_counter
1194 (vnet_main.interface_main.sw_if_counters
1195 + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number (),
1196 vui->sw_if_index, drops);
1199 /* increase rx counters */
1200 vlib_increment_combined_counter
1201 (vnet_main.interface_main.combined_sw_if_counters
1202 + VNET_INTERFACE_COUNTER_RX,
1203 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1205 return n_rx_packets;
1209 vhost_user_input (vlib_main_t * vm,
1210 vlib_node_runtime_t * node, vlib_frame_t * f)
1212 vhost_user_main_t *vum = &vhost_user_main;
1214 dpdk_main_t *dm = &dpdk_main;
1215 u32 cpu_index = os_get_cpu_number ();
1217 vhost_user_intf_t *vui;
1218 uword n_rx_packets = 0;
1221 for (i = 0; i < vec_len (vum->vhost_user_interfaces); i++)
1223 vui = vec_elt_at_index (vum->vhost_user_interfaces, i);
1227 if ((i % dm->input_cpu_count) ==
1228 (cpu_index - dm->input_cpu_first_index))
1230 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1233 return n_rx_packets;
1237 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1238 .function = vhost_user_input,
1239 .type = VLIB_NODE_TYPE_INPUT,
1240 .name = "vhost-user-input",
1242 /* Will be enabled if/when hardware is detected. */
1243 .state = VLIB_NODE_STATE_DISABLED,
1245 .format_buffer = format_ethernet_header_with_length,
1246 .format_trace = format_vhost_user_input_trace,
1248 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1249 .error_strings = vhost_user_input_func_error_strings,
1251 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1253 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1254 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1258 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1262 vhost_user_intfc_tx (vlib_main_t * vm,
1263 vlib_node_runtime_t * node, vlib_frame_t * frame)
1265 u32 *buffers = vlib_frame_args (frame);
1268 vhost_user_main_t *vum = &vhost_user_main;
1269 uword n_packets = 0;
1270 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1271 vhost_user_intf_t *vui =
1272 vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1273 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1275 u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1277 if (PREDICT_FALSE (!vui->is_up))
1281 (!rxvq->desc || !rxvq->avail || vui->sock_errno != 0 || !rxvq->enabled))
1283 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1287 if (PREDICT_FALSE (vui->lockp != 0))
1289 while (__sync_lock_test_and_set (vui->lockp, 1))
1293 /* only bit 0 of avail.flags is used so we don't want to deal with this
1294 interface if any other bit is set */
1295 if (PREDICT_FALSE (rxvq->avail->flags & 0xFFFE))
1297 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1301 if (PREDICT_FALSE ((rxvq->avail->idx == rxvq->last_avail_idx)))
1303 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1307 n_left = n_packets = frame->n_vectors;
1308 used_index = rxvq->used->idx;
1309 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1313 vlib_buffer_t *b0, *current_b0;
1314 u16 desc_chain_head, desc_current, desc_len;
1319 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
1321 b0 = vlib_get_buffer (vm, buffers[0]);
1325 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
1327 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1331 desc_current = desc_chain_head =
1332 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1333 offset = vui->virtio_net_hdr_sz;
1337 map_guest_mem (vui, rxvq->desc[desc_current].addr))))
1339 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1342 CLIB_PREFETCH (buffer_addr, clib_min (rxvq->desc[desc_current].len,
1343 4 * CLIB_CACHE_LINE_BYTES),
1346 virtio_net_hdr_mrg_rxbuf_t *hdr =
1347 (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1349 hdr->hdr.gso_type = 0;
1351 vhost_user_log_dirty_pages (vui, rxvq->desc[desc_current].addr,
1352 vui->virtio_net_hdr_sz);
1354 if (vui->virtio_net_hdr_sz == 12)
1355 hdr->num_buffers = 1;
1357 u16 bytes_left = b0->current_length;
1358 buffer_addr += offset;
1361 //FIXME: This was in the code but I don't think it is valid
1362 /*if (PREDICT_FALSE(!vui->is_any_layout && (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))) {
1363 rxvq->desc[desc_current].len = vui->virtio_net_hdr_sz;
1370 if (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1372 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
1373 bytes_left = current_b0->current_length;
1382 if (rxvq->desc[desc_current].len <= offset)
1384 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1387 desc_current = rxvq->desc[desc_current].next;
1390 map_guest_mem (vui, rxvq->desc[desc_current].addr))))
1392 used_index -= hdr->num_buffers - 1;
1393 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1394 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1398 else if (vui->virtio_net_hdr_sz == 12)
1399 { //MRG is available
1401 //Move from available to used buffer
1402 rxvq->used->ring[used_index & qsz_mask].id =
1404 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1405 vhost_user_log_dirty_ring (vui, rxvq,
1406 ring[used_index & qsz_mask]);
1407 rxvq->last_avail_idx++;
1412 (rxvq->last_avail_idx == rxvq->avail->idx))
1414 //Dequeue queued descriptors for this packet
1415 used_index -= hdr->num_buffers - 1;
1416 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1417 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1423 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1424 desc_current = desc_chain_head;
1429 map_guest_mem (vui, rxvq->desc[desc_current].addr))))
1431 //Dequeue queued descriptors for this packet
1432 used_index -= hdr->num_buffers - 1;
1433 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1434 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1440 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1447 (rxvq->desc[desc_current].len -
1448 offset) ? (rxvq->desc[desc_current].len - offset) : bytes_left;
1449 clib_memcpy (buffer_addr,
1450 vlib_buffer_get_current (current_b0) +
1451 current_b0->current_length - bytes_left,
1454 vhost_user_log_dirty_pages (vui,
1455 rxvq->desc[desc_current].addr + offset,
1457 bytes_left -= bytes_to_copy;
1458 offset += bytes_to_copy;
1459 buffer_addr += bytes_to_copy;
1460 desc_len += bytes_to_copy;
1463 //Move from available to used ring
1464 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1465 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1466 vhost_user_log_dirty_ring (vui, rxvq, ring[used_index & qsz_mask]);
1468 rxvq->last_avail_idx++;
1473 CLIB_MEMORY_BARRIER ();
1474 rxvq->used->idx = used_index;
1475 vhost_user_log_dirty_ring (vui, rxvq, idx);
1477 /* interrupt (call) handling */
1478 if ((rxvq->callfd > 0) && !(rxvq->avail->flags & 1))
1480 rxvq->n_since_last_int += n_packets - n_left;
1482 if (rxvq->n_since_last_int > vum->coalesce_frames)
1483 vhost_user_send_call (vm, rxvq);
1488 if (PREDICT_FALSE (vui->lockp != 0))
1491 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
1493 vlib_error_count (vm, node->node_index, error, n_left);
1494 vlib_increment_simple_counter
1495 (vnet_main.interface_main.sw_if_counters
1496 + VNET_INTERFACE_COUNTER_DROP,
1497 os_get_cpu_number (), vui->sw_if_index, n_left);
1500 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1501 return frame->n_vectors;
1504 static clib_error_t *
1505 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
1508 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1509 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1510 vhost_user_main_t *vum = &vhost_user_main;
1511 vhost_user_intf_t *vui =
1512 vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1514 vui->admin_up = is_up;
1517 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1518 VNET_HW_INTERFACE_FLAG_LINK_UP);
1520 return /* no error */ 0;
1524 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1525 .name = "vhost-user",
1526 .tx_function = vhost_user_intfc_tx,
1527 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1528 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1529 .format_device_name = format_vhost_user_interface_name,
1530 .name_renumber = vhost_user_name_renumber,
1531 .admin_up_down_function = vhost_user_interface_admin_up_down,
1532 .no_flatten_output_chains = 1,
1535 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
1536 vhost_user_intfc_tx)
1540 vhost_user_process (vlib_main_t * vm,
1541 vlib_node_runtime_t * rt, vlib_frame_t * f)
1543 vhost_user_main_t *vum = &vhost_user_main;
1544 vhost_user_intf_t *vui;
1545 struct sockaddr_un sun;
1547 unix_file_t template = { 0 };
1548 f64 timeout = 3153600000.0 /* 100 years */ ;
1549 uword *event_data = 0;
1551 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1552 sun.sun_family = AF_UNIX;
1553 template.read_function = vhost_user_socket_read;
1554 template.error_function = vhost_user_socket_error;
1562 vlib_process_wait_for_event_or_clock (vm, timeout);
1563 vlib_process_get_events (vm, &event_data);
1564 vec_reset_length (event_data);
1568 vec_foreach (vui, vum->vhost_user_interfaces)
1571 if (vui->sock_is_server || !vui->active)
1574 if (vui->unix_fd == -1)
1576 /* try to connect */
1578 strncpy (sun.sun_path, (char *) vui->sock_filename,
1579 sizeof (sun.sun_path) - 1);
1582 (sockfd, (struct sockaddr *) &sun,
1583 sizeof (struct sockaddr_un)) == 0)
1585 vui->sock_errno = 0;
1586 vui->unix_fd = sockfd;
1587 template.file_descriptor = sockfd;
1588 vui->unix_file_index = unix_file_add (&unix_main, &template);
1589 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd,
1590 vui - vum->vhost_user_interfaces);
1592 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1598 vui->sock_errno = errno;
1603 /* check if socket is alive */
1605 socklen_t len = sizeof (error);
1607 getsockopt (vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1610 vhost_user_if_disconnect (vui);
1618 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1619 .function = vhost_user_process,
1620 .type = VLIB_NODE_TYPE_PROCESS,
1621 .name = "vhost-user-process",
1626 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1628 vhost_user_main_t *vum = &vhost_user_main;
1629 vhost_user_intf_t *vui;
1633 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
1636 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1640 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1643 // interface is inactive
1645 // disconnect interface sockets
1646 vhost_user_if_disconnect (vui);
1647 // add to inactive interface list
1648 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1650 // reset renumbered iface
1651 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1652 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1654 ethernet_delete_interface (vnm, vui->hw_if_index);
1655 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1660 // init server socket on specified sock_filename
1662 vhost_user_init_server_sock (const char *sock_filename, int *sockfd)
1665 struct sockaddr_un un = { };
1667 /* create listening socket */
1668 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1672 return VNET_API_ERROR_SYSCALL_ERROR_1;
1675 un.sun_family = AF_UNIX;
1676 strncpy ((char *) un.sun_path, (char *) sock_filename,
1677 sizeof (un.sun_path) - 1);
1679 /* remove if exists */
1680 unlink ((char *) sock_filename);
1682 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1684 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1688 if (listen (fd, 1) == -1)
1690 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1694 unix_file_t template = { 0 };
1695 template.read_function = vhost_user_socksvr_accept_ready;
1696 template.file_descriptor = fd;
1697 unix_file_add (&unix_main, &template);
1706 // get new vhost_user_intf_t from inactive interfaces or create new one
1707 static vhost_user_intf_t *
1708 vhost_user_vui_new ()
1710 vhost_user_main_t *vum = &vhost_user_main;
1711 vhost_user_intf_t *vui = NULL;
1712 int inactive_cnt = vec_len (vum->vhost_user_inactive_interfaces_index);
1713 // if there are any inactive ifaces
1714 if (inactive_cnt > 0)
1718 vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1719 if (vec_len (vum->vhost_user_interfaces) > vui_idx)
1721 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1722 DBG_SOCK ("reusing inactive vhost-user interface index %d",
1725 // "remove" from inactive list
1726 _vec_len (vum->vhost_user_inactive_interfaces_index) -= 1;
1729 // vui was not retrieved from inactive ifaces - create new
1731 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1735 // create ethernet interface for vhost user intf
1737 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1738 vhost_user_intf_t * vui, u8 * hwaddress)
1740 vhost_user_main_t *vum = &vhost_user_main;
1742 clib_error_t *error;
1744 /* create hw and sw interface */
1747 clib_memcpy (hwaddr, hwaddress, 6);
1751 f64 now = vlib_time_now (vm);
1753 rnd = (u32) (now * 1e6);
1754 rnd = random_u32 (&rnd);
1756 clib_memcpy (hwaddr + 2, &rnd, sizeof (rnd));
1761 error = ethernet_register_interface
1763 vhost_user_dev_class.index,
1764 vui - vum->vhost_user_interfaces /* device instance */ ,
1765 hwaddr /* ethernet address */ ,
1766 &vui->hw_if_index, 0 /* flag change */ );
1768 clib_error_report (error);
1770 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1771 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1774 // initialize vui with specified attributes
1776 vhost_user_vui_init (vnet_main_t * vnm,
1777 vhost_user_intf_t * vui, int sockfd,
1778 const char *sock_filename,
1779 u8 is_server, u64 feature_mask, u32 * sw_if_index)
1781 vnet_sw_interface_t *sw;
1782 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1783 vlib_thread_main_t *tm = vlib_get_thread_main ();
1786 vui->unix_fd = sockfd;
1787 vui->sw_if_index = sw->sw_if_index;
1788 vui->num_vrings = 2;
1789 vui->sock_is_server = is_server;
1790 strncpy (vui->sock_filename, sock_filename,
1791 ARRAY_LEN (vui->sock_filename) - 1);
1792 vui->sock_errno = 0;
1794 vui->feature_mask = feature_mask;
1796 vui->unix_file_index = ~0;
1797 vui->log_base_addr = 0;
1799 for (q = 0; q < 2; q++)
1801 vui->vrings[q].enabled = 0;
1804 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1807 *sw_if_index = vui->sw_if_index;
1809 if (tm->n_vlib_mains > 1)
1811 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1812 CLIB_CACHE_LINE_BYTES);
1813 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
1817 // register vui and start polling on it
1819 vhost_user_vui_register (vlib_main_t * vm, vhost_user_intf_t * vui)
1821 vhost_user_main_t *vum = &vhost_user_main;
1823 dpdk_main_t *dm = &dpdk_main;
1825 vlib_thread_main_t *tm = vlib_get_thread_main ();
1828 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
1829 vui - vum->vhost_user_interfaces);
1830 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
1831 vui - vum->vhost_user_interfaces);
1835 cpu_index = dm->input_cpu_first_index +
1836 (vui - vum->vhost_user_interfaces) % dm->input_cpu_count;
1838 if (tm->n_vlib_mains == 1)
1840 vlib_node_set_state (vm, vhost_user_input_node.index,
1841 VLIB_NODE_STATE_POLLING);
1844 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
1845 VLIB_NODE_STATE_POLLING);
1848 /* tell process to start polling for sockets */
1849 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
1853 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1854 const char *sock_filename,
1858 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1860 vhost_user_intf_t *vui = NULL;
1867 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
1873 vui = vhost_user_vui_new ();
1874 ASSERT (vui != NULL);
1876 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1877 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1878 feature_mask, &sw_if_idx);
1882 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1885 vhost_user_vui_register (vm, vui);
1888 *sw_if_index = sw_if_idx;
1894 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1895 const char *sock_filename,
1898 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1900 vhost_user_main_t *vum = &vhost_user_main;
1901 vhost_user_intf_t *vui = NULL;
1907 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
1910 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1914 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1917 // interface is inactive
1919 // disconnect interface sockets
1920 vhost_user_if_disconnect (vui);
1924 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
1930 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1931 feature_mask, &sw_if_idx);
1935 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1938 vhost_user_vui_register (vm, vui);
1944 vhost_user_connect_command_fn (vlib_main_t * vm,
1945 unformat_input_t * input,
1946 vlib_cli_command_t * cmd)
1948 unformat_input_t _line_input, *line_input = &_line_input;
1949 u8 *sock_filename = NULL;
1952 u64 feature_mask = (u64) ~ 0;
1954 u32 custom_dev_instance = ~0;
1958 /* Get a line of input. */
1959 if (!unformat_user (input, unformat_line_input, line_input))
1962 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1964 if (unformat (line_input, "socket %s", &sock_filename))
1966 else if (unformat (line_input, "server"))
1968 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1972 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1974 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1979 return clib_error_return (0, "unknown input `%U'",
1980 format_unformat_error, input);
1982 unformat_free (line_input);
1984 vnet_main_t *vnm = vnet_get_main ();
1987 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1988 is_server, &sw_if_index, feature_mask,
1989 renumber, custom_dev_instance, hw)))
1991 vec_free (sock_filename);
1992 return clib_error_return (0, "vhost_user_create_if returned %d", rv);
1995 vec_free (sock_filename);
1996 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2002 vhost_user_delete_command_fn (vlib_main_t * vm,
2003 unformat_input_t * input,
2004 vlib_cli_command_t * cmd)
2006 unformat_input_t _line_input, *line_input = &_line_input;
2007 u32 sw_if_index = ~0;
2009 /* Get a line of input. */
2010 if (!unformat_user (input, unformat_line_input, line_input))
2013 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2015 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
2018 return clib_error_return (0, "unknown input `%U'",
2019 format_unformat_error, input);
2021 unformat_free (line_input);
2023 vnet_main_t *vnm = vnet_get_main ();
2025 vhost_user_delete_if (vnm, vm, sw_if_index);
2031 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
2032 vhost_user_intf_details_t ** out_vuids)
2035 vhost_user_main_t *vum = &vhost_user_main;
2036 vhost_user_intf_t *vui;
2037 vhost_user_intf_details_t *r_vuids = NULL;
2038 vhost_user_intf_details_t *vuid = NULL;
2039 u32 *hw_if_indices = 0;
2040 vnet_hw_interface_t *hi;
2047 vec_foreach (vui, vum->vhost_user_interfaces)
2050 vec_add1 (hw_if_indices, vui->hw_if_index);
2053 for (i = 0; i < vec_len (hw_if_indices); i++)
2055 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2056 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2058 vec_add2 (r_vuids, vuid, 1);
2059 vuid->sw_if_index = vui->sw_if_index;
2060 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
2061 vuid->features = vui->features;
2062 vuid->is_server = vui->sock_is_server;
2063 vuid->num_regions = vui->nregions;
2064 vuid->sock_errno = vui->sock_errno;
2065 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
2066 ARRAY_LEN (vuid->sock_filename) - 1);
2068 s = format (s, "%v%c", hi->name, 0);
2070 strncpy ((char *) vuid->if_name, (char *) s,
2071 ARRAY_LEN (vuid->if_name) - 1);
2076 vec_free (hw_if_indices);
2078 *out_vuids = r_vuids;
2084 show_vhost_user_command_fn (vlib_main_t * vm,
2085 unformat_input_t * input,
2086 vlib_cli_command_t * cmd)
2088 clib_error_t *error = 0;
2089 vnet_main_t *vnm = vnet_get_main ();
2090 vhost_user_main_t *vum = &vhost_user_main;
2091 vhost_user_intf_t *vui;
2092 u32 hw_if_index, *hw_if_indices = 0;
2093 vnet_hw_interface_t *hi;
2101 struct feat_struct *feat_entry;
2103 static struct feat_struct feat_array[] = {
2104 #define _(s,b) { .str = #s, .bit = b, },
2105 foreach_virtio_net_feature
2110 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2113 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2115 vec_add1 (hw_if_indices, hw_if_index);
2116 vlib_cli_output (vm, "add %d", hw_if_index);
2118 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2122 error = clib_error_return (0, "unknown input `%U'",
2123 format_unformat_error, input);
2127 if (vec_len (hw_if_indices) == 0)
2129 vec_foreach (vui, vum->vhost_user_interfaces)
2132 vec_add1 (hw_if_indices, vui->hw_if_index);
2135 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2136 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
2137 vum->coalesce_frames, vum->coalesce_time);
2139 for (i = 0; i < vec_len (hw_if_indices); i++)
2141 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2142 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2143 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
2144 hi->name, hw_if_indices[i]);
2146 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
2147 vui->virtio_net_hdr_sz, vui->features);
2149 feat_entry = (struct feat_struct *) &feat_array;
2150 while (feat_entry->str)
2152 if (vui->features & (1 << feat_entry->bit))
2153 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2158 vlib_cli_output (vm, "\n");
2161 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2163 vui->sock_is_server ? "server" : "client",
2164 strerror (vui->sock_errno));
2166 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2170 vlib_cli_output (vm,
2171 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2172 vlib_cli_output (vm,
2173 " ====== ===== ================== ================== ================== ================== ==================\n");
2175 for (j = 0; j < vui->nregions; j++)
2177 vlib_cli_output (vm,
2178 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2179 j, vui->region_mmap_fd[j],
2180 vui->regions[j].guest_phys_addr,
2181 vui->regions[j].memory_size,
2182 vui->regions[j].userspace_addr,
2183 vui->regions[j].mmap_offset,
2184 pointer_to_uword (vui->region_mmap_addr[j]));
2186 for (q = 0; q < vui->num_vrings; q++)
2188 vlib_cli_output (vm, "\n Virtqueue %d\n", q);
2190 vlib_cli_output (vm,
2191 " qsz %d last_avail_idx %d last_used_idx %d\n",
2192 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
2193 vui->vrings[q].last_used_idx);
2195 if (vui->vrings[q].avail && vui->vrings[q].used)
2196 vlib_cli_output (vm,
2197 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
2198 vui->vrings[q].avail->flags,
2199 vui->vrings[q].avail->idx,
2200 vui->vrings[q].used->flags,
2201 vui->vrings[q].used->idx);
2203 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
2204 vui->vrings[q].kickfd,
2205 vui->vrings[q].callfd, vui->vrings[q].errfd);
2209 vlib_cli_output (vm, "\n descriptor table:\n");
2210 vlib_cli_output (vm,
2211 " id addr len flags next user_addr\n");
2212 vlib_cli_output (vm,
2213 " ===== ================== ===== ====== ===== ==================\n");
2214 for (j = 0; j < vui->vrings[q].qsz; j++)
2216 vlib_cli_output (vm,
2217 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
2218 j, vui->vrings[q].desc[j].addr,
2219 vui->vrings[q].desc[j].len,
2220 vui->vrings[q].desc[j].flags,
2221 vui->vrings[q].desc[j].next,
2222 pointer_to_uword (map_guest_mem
2229 vlib_cli_output (vm, "\n");
2232 vec_free (hw_if_indices);
2242 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2243 .path = "create vhost-user",
2244 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
2245 .function = vhost_user_connect_command_fn,
2248 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2249 .path = "delete vhost-user",
2250 .short_help = "delete vhost-user sw_if_index <nn>",
2251 .function = vhost_user_delete_command_fn,
2254 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2255 .path = "show vhost-user",
2256 .short_help = "show vhost-user interface",
2257 .function = show_vhost_user_command_fn,
2262 static clib_error_t *
2263 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2265 vhost_user_main_t *vum = &vhost_user_main;
2267 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2269 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2271 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2273 else if (unformat (input, "dont-dump-memory"))
2274 vum->dont_dump_vhost_user_memory = 1;
2276 return clib_error_return (0, "unknown input `%U'",
2277 format_unformat_error, input);
2283 /* vhost-user { ... } configuration. */
2284 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2287 vhost_user_unmap_all (void)
2289 vhost_user_main_t *vum = &vhost_user_main;
2290 vhost_user_intf_t *vui;
2292 if (vum->dont_dump_vhost_user_memory)
2294 vec_foreach (vui, vum->vhost_user_interfaces)
2296 unmap_all_mem_regions (vui);
2302 * fd.io coding-style-patch-verification: ON
2305 * eval: (c-set-style "gnu")