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")
70 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
71 foreach_vhost_user_tx_func_error
73 VHOST_USER_TX_FUNC_N_ERROR,
74 } vhost_user_tx_func_error_t;
76 static char * vhost_user_tx_func_error_strings[] = {
78 foreach_vhost_user_tx_func_error
82 #define foreach_vhost_user_input_func_error \
83 _(NO_ERROR, "no error") \
84 _(NO_BUFFER, "no available buffer") \
85 _(MMAP_FAIL, "mmap failure") \
86 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)")
89 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
90 foreach_vhost_user_input_func_error
92 VHOST_USER_INPUT_FUNC_N_ERROR,
93 } vhost_user_input_func_error_t;
95 static char * vhost_user_input_func_error_strings[] = {
97 foreach_vhost_user_input_func_error
101 static vhost_user_main_t vhost_user_main = {
105 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
106 .name = "vhost-user",
109 static u8 * format_vhost_user_interface_name (u8 * s, va_list * args)
111 u32 i = va_arg (*args, u32);
112 u32 show_dev_instance = ~0;
113 vhost_user_main_t * vum = &vhost_user_main;
115 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
116 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
118 if (show_dev_instance != ~0)
119 i = show_dev_instance;
121 s = format (s, "VirtualEthernet0/0/%d", i);
125 static int vhost_user_name_renumber (vnet_hw_interface_t * hi,
126 u32 new_dev_instance)
128 vhost_user_main_t * vum = &vhost_user_main;
130 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
131 hi->dev_instance, ~0);
133 vum->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
136 DBG_SOCK("renumbered vhost-user interface dev_instance %d to %d",
137 hi->dev_instance, new_dev_instance);
143 static inline void * map_guest_mem(vhost_user_intf_t * vui, uword addr)
146 for (i=0; i<vui->nregions; i++) {
147 if ((vui->regions[i].guest_phys_addr <= addr) &&
148 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > addr)) {
149 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].guest_phys_addr);
152 DBG_VQ("failed to map guest mem addr %llx", addr);
156 static inline void * map_user_mem(vhost_user_intf_t * vui, uword addr)
159 for (i=0; i<vui->nregions; i++) {
160 if ((vui->regions[i].userspace_addr <= addr) &&
161 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > addr)) {
162 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].userspace_addr);
168 static long get_huge_page_size(int fd)
175 static void unmap_all_mem_regions(vhost_user_intf_t * vui)
178 for (i=0; i<vui->nregions; i++) {
179 if (vui->region_mmap_addr[i] != (void *) -1) {
181 long page_sz = get_huge_page_size(vui->region_mmap_fd[i]);
183 ssize_t map_sz = (vui->regions[i].memory_size +
184 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
186 r = munmap(vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, map_sz);
188 DBG_SOCK("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
189 vui->region_mmap_addr[i], map_sz, page_sz);
191 vui->region_mmap_addr[i]= (void *) -1;
194 clib_warning("failed to unmap memory region (errno %d)", errno);
196 close(vui->region_mmap_fd[i]);
203 static clib_error_t * vhost_user_callfd_read_ready (unix_file_t * uf)
205 __attribute__((unused)) int n;
207 n = read(uf->file_descriptor, ((char*)&buff), 8);
211 static inline void vhost_user_if_disconnect(vhost_user_intf_t * vui)
213 vhost_user_main_t * vum = &vhost_user_main;
214 vnet_main_t * vnm = vnet_get_main();
217 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
219 if (vui->unix_file_index != ~0) {
220 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
221 vui->unix_file_index = ~0;
224 hash_unset(vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
225 hash_unset(vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
229 for (q = 0; q < vui->num_vrings; q++) {
230 vui->vrings[q].desc = NULL;
231 vui->vrings[q].avail = NULL;
232 vui->vrings[q].used = NULL;
233 vui->vrings[q].log_guest_addr = 0;
234 vui->vrings[q].log_used = 0;
237 unmap_all_mem_regions(vui);
238 DBG_SOCK("interface ifindex %d disconnected", vui->sw_if_index);
241 #define VHOST_LOG_PAGE 0x1000
242 always_inline void vhost_user_log_dirty_pages(vhost_user_intf_t * vui,
245 if (PREDICT_TRUE(vui->log_base_addr == 0
246 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL)))) {
249 if (PREDICT_FALSE((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size)) {
250 DBG_SOCK("vhost_user_log_dirty_pages(): out of range\n");
254 CLIB_MEMORY_BARRIER();
255 u64 page = addr / VHOST_LOG_PAGE;
256 while (page * VHOST_LOG_PAGE < addr + len) {
257 ((u8*)vui->log_base_addr)[page / 8] |= 1 << page % 8;
262 #define vhost_user_log_dirty_ring(vui, vq, member) \
263 if (PREDICT_FALSE(vq->log_used)) { \
264 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + offsetof(vring_used_t, member), \
265 sizeof(vq->used->member)); \
268 static clib_error_t * vhost_user_socket_read (unix_file_t * uf)
271 int fd, number_of_fds = 0;
272 int fds[VHOST_MEMORY_MAX_NREGIONS];
273 vhost_user_msg_t msg;
276 vhost_user_main_t * vum = &vhost_user_main;
277 vhost_user_intf_t * vui;
278 struct cmsghdr *cmsg;
281 unix_file_t template = {0};
282 vnet_main_t * vnm = vnet_get_main();
284 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
285 uf->file_descriptor);
287 DBG_SOCK ("FD %d doesn't belong to any interface",
288 uf->file_descriptor);
292 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
294 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
296 memset(&mh, 0, sizeof(mh));
297 memset(control, 0, sizeof(control));
299 /* set the payload */
300 iov[0].iov_base = (void *) &msg;
301 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
305 mh.msg_control = control;
306 mh.msg_controllen = sizeof(control);
308 n = recvmsg(uf->file_descriptor, &mh, 0);
310 if (n != VHOST_USER_MSG_HDR_SZ)
313 if (mh.msg_flags & MSG_CTRUNC) {
317 cmsg = CMSG_FIRSTHDR(&mh);
319 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
320 (cmsg->cmsg_type == SCM_RIGHTS) &&
321 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
322 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
323 clib_memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
326 /* version 1, no reply bit set*/
327 if ((msg.flags & 7) != 1) {
328 DBG_SOCK("malformed message received. closing socket");
333 int rv __attribute__((unused));
334 /* $$$$ pay attention to rv */
335 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
338 switch (msg.request) {
339 case VHOST_USER_GET_FEATURES:
340 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
344 msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
345 (1 << FEAT_VIRTIO_F_ANY_LAYOUT) |
346 (1 << FEAT_VHOST_F_LOG_ALL) |
347 (1 << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
348 (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES);
349 msg.u64 &= vui->feature_mask;
351 msg.size = sizeof(msg.u64);
354 case VHOST_USER_SET_FEATURES:
355 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
356 vui->hw_if_index, msg.u64);
358 vui->features = msg.u64;
360 if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
361 vui->virtio_net_hdr_sz = 12;
363 vui->virtio_net_hdr_sz = 10;
365 vui->is_any_layout = (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
367 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
368 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
371 for (q = 0; q < 2; q++) {
372 vui->vrings[q].desc = 0;
373 vui->vrings[q].avail = 0;
374 vui->vrings[q].used = 0;
375 vui->vrings[q].log_guest_addr = 0;
376 vui->vrings[q].log_used = 0;
379 DBG_SOCK("interface %d disconnected", vui->sw_if_index);
383 case VHOST_USER_SET_MEM_TABLE:
384 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
385 vui->hw_if_index, msg.memory.nregions);
387 if ((msg.memory.nregions < 1) ||
388 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
390 DBG_SOCK("number of mem regions must be between 1 and %i",
391 VHOST_MEMORY_MAX_NREGIONS);
396 if (msg.memory.nregions != number_of_fds) {
397 DBG_SOCK("each memory region must have FD");
400 unmap_all_mem_regions(vui);
401 for(i=0; i < msg.memory.nregions; i++) {
402 clib_memcpy(&(vui->regions[i]), &msg.memory.regions[i],
403 sizeof(vhost_user_memory_region_t));
405 long page_sz = get_huge_page_size(fds[i]);
407 /* align size to 2M page */
408 ssize_t map_sz = (vui->regions[i].memory_size +
409 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
411 vui->region_mmap_addr[i] = mmap(0, map_sz, PROT_READ | PROT_WRITE,
412 MAP_SHARED, fds[i], 0);
414 DBG_SOCK("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
415 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i], page_sz);
417 if (vui->region_mmap_addr[i] == MAP_FAILED) {
418 clib_warning("failed to map memory. errno is %d", errno);
421 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
422 vui->region_mmap_fd[i] = fds[i];
424 vui->nregions = msg.memory.nregions;
427 case VHOST_USER_SET_VRING_NUM:
428 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
429 vui->hw_if_index, msg.state.index, msg.state.num);
431 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
432 (msg.state.num == 0) || /* it cannot be zero */
433 (msg.state.num % 2)) /* must be power of 2 */
435 vui->vrings[msg.state.index].qsz = msg.state.num;
438 case VHOST_USER_SET_VRING_ADDR:
439 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
440 vui->hw_if_index, msg.state.index);
442 vui->vrings[msg.state.index].desc = (vring_desc_t *)
443 map_user_mem(vui, msg.addr.desc_user_addr);
444 vui->vrings[msg.state.index].used = (vring_used_t *)
445 map_user_mem(vui, msg.addr.used_user_addr);
446 vui->vrings[msg.state.index].avail = (vring_avail_t *)
447 map_user_mem(vui, msg.addr.avail_user_addr);
449 if ((vui->vrings[msg.state.index].desc == NULL) ||
450 (vui->vrings[msg.state.index].used == NULL) ||
451 (vui->vrings[msg.state.index].avail == NULL)) {
452 DBG_SOCK("failed to map user memory for hw_if_index %d",
457 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
458 vui->vrings[msg.state.index].log_used =
459 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
461 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
462 the ring is initialized in an enabled state. */
464 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES))) {
465 vui->vrings[msg.state.index].enabled = 1;
468 vui->vrings[msg.state.index].last_used_idx =
469 vui->vrings[msg.state.index].used->idx;
471 /* tell driver that we don't want interrupts */
472 vui->vrings[msg.state.index].used->flags |= 1;
475 case VHOST_USER_SET_OWNER:
476 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
480 case VHOST_USER_RESET_OWNER:
481 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
485 case VHOST_USER_SET_VRING_CALL:
486 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
487 vui->hw_if_index, msg.u64);
489 q = (u8) (msg.u64 & 0xFF);
491 if (!(msg.u64 & 0x100))
493 if (number_of_fds != 1)
496 /* if there is old fd, delete it */
497 if (vui->vrings[q].callfd) {
498 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
499 vui->vrings[q].callfd_idx);
500 unix_file_del (&unix_main, uf);
502 vui->vrings[q].callfd = fds[0];
503 template.read_function = vhost_user_callfd_read_ready;
504 template.file_descriptor = fds[0];
505 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
508 vui->vrings[q].callfd = -1;
511 case VHOST_USER_SET_VRING_KICK:
512 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
513 vui->hw_if_index, msg.u64);
515 q = (u8) (msg.u64 & 0xFF);
517 if (!(msg.u64 & 0x100))
519 if (number_of_fds != 1)
522 vui->vrings[q].kickfd = fds[0];
525 vui->vrings[q].kickfd = -1;
528 case VHOST_USER_SET_VRING_ERR:
529 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
530 vui->hw_if_index, msg.u64);
532 q = (u8) (msg.u64 & 0xFF);
534 if (!(msg.u64 & 0x100))
536 if (number_of_fds != 1)
544 vui->vrings[q].errfd = fd;
547 case VHOST_USER_SET_VRING_BASE:
548 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
549 vui->hw_if_index, msg.state.index, msg.state.num);
551 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
554 case VHOST_USER_GET_VRING_BASE:
555 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
556 vui->hw_if_index, msg.state.index, msg.state.num);
558 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
559 vui->vrings[msg.state.index].enabled = 0;
561 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
563 msg.size = sizeof(msg.state);
566 case VHOST_USER_NONE:
567 DBG_SOCK("if %d msg VHOST_USER_NONE",
572 case VHOST_USER_SET_LOG_BASE:
574 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
577 if (msg.size != sizeof(msg.log)) {
578 DBG_SOCK("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
579 msg.size, sizeof(msg.log));
583 if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) {
584 DBG_SOCK("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
589 /* align size to 2M page */
590 long page_sz = get_huge_page_size(fd);
591 ssize_t map_sz = (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
593 vui->log_base_addr = mmap(0, map_sz, PROT_READ | PROT_WRITE,
596 DBG_SOCK("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
597 map_sz, msg.log.offset, fd, vui->log_base_addr);
599 if (vui->log_base_addr == MAP_FAILED) {
600 clib_warning("failed to map memory. errno is %d", errno);
604 vui->log_base_addr += msg.log.offset;
605 vui->log_size = msg.log.size;
608 msg.size = sizeof(msg.u64);
613 case VHOST_USER_SET_LOG_FD:
614 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
619 case VHOST_USER_GET_PROTOCOL_FEATURES:
620 DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES", vui->hw_if_index);
623 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD);
624 msg.size = sizeof(msg.u64);
627 case VHOST_USER_SET_PROTOCOL_FEATURES:
628 DBG_SOCK("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
629 vui->hw_if_index, msg.u64);
631 vui->protocol_features = msg.u64;
635 case VHOST_USER_SET_VRING_ENABLE:
636 DBG_SOCK("if %d VHOST_USER_SET_VRING_ENABLE, enable: %d",
637 vui->hw_if_index, msg.state.num);
638 vui->vrings[msg.state.index].enabled = msg.state.num;
642 DBG_SOCK("unknown vhost-user message %d received. closing socket",
647 /* if we have pointers to descriptor table, go up*/
649 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
650 vui->vrings[VHOST_NET_VRING_IDX_RX].desc) {
652 DBG_SOCK("interface %d connected", vui->sw_if_index);
654 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
659 /* if we need to reply */
662 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
663 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
670 vhost_user_if_disconnect(vui);
674 static clib_error_t * vhost_user_socket_error (unix_file_t * uf)
676 vhost_user_main_t * vum = &vhost_user_main;
677 vhost_user_intf_t * vui;
680 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
681 uf->file_descriptor);
683 DBG_SOCK ("fd %d doesn't belong to any interface",
684 uf->file_descriptor);
688 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
690 vhost_user_if_disconnect(vui);
694 static clib_error_t * vhost_user_socksvr_accept_ready (unix_file_t * uf)
696 int client_fd, client_len;
697 struct sockaddr_un client;
698 unix_file_t template = {0};
699 vhost_user_main_t * vum = &vhost_user_main;
700 vhost_user_intf_t * vui;
703 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
704 uf->file_descriptor);
706 DBG_SOCK ("fd %d doesn't belong to any interface",
707 uf->file_descriptor);
711 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
713 client_len = sizeof(client);
714 client_fd = accept (uf->file_descriptor,
715 (struct sockaddr *)&client,
716 (socklen_t *)&client_len);
719 return clib_error_return_unix (0, "accept");
721 template.read_function = vhost_user_socket_read;
722 template.error_function = vhost_user_socket_error;
723 template.file_descriptor = client_fd;
724 vui->unix_file_index = unix_file_add (&unix_main, &template);
726 vui->client_fd = client_fd;
727 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
728 vui - vum->vhost_user_interfaces);
733 static clib_error_t *
734 vhost_user_init (vlib_main_t * vm)
736 clib_error_t * error;
737 vhost_user_main_t * vum = &vhost_user_main;
738 vlib_thread_main_t * tm = vlib_get_thread_main();
740 error = vlib_call_init_function (vm, ip4_init);
744 vum->vhost_user_interface_index_by_listener_fd = hash_create (0, sizeof (uword));
745 vum->vhost_user_interface_index_by_sock_fd = hash_create (0, sizeof (uword));
746 vum->vhost_user_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
747 vum->coalesce_frames = 32;
748 vum->coalesce_time = 1e-3;
750 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
751 CLIB_CACHE_LINE_BYTES);
756 VLIB_INIT_FUNCTION (vhost_user_init);
758 static clib_error_t *
759 vhost_user_exit (vlib_main_t * vm)
765 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
768 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
769 VHOST_USER_RX_NEXT_DROP,
770 VHOST_USER_RX_N_NEXT,
777 #if VHOST_USER_COPY_TX_HDR == 1
778 virtio_net_hdr_t hdr;
780 } vhost_user_input_trace_t;
782 static u8 * format_vhost_user_input_trace (u8 * s, va_list * va)
784 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
785 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
786 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main();
787 vhost_user_main_t * vum = &vhost_user_main;
788 vhost_user_input_trace_t * t = va_arg (*va, vhost_user_input_trace_t *);
789 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces,
792 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
794 #if VHOST_USER_COPY_TX_HDR == 1
795 uword indent = format_get_indent (s);
798 s = format (s, "%U virtqueue %d",
799 format_vnet_sw_interface_name, vnm, sw,
802 #if VHOST_USER_COPY_TX_HDR == 1
803 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
804 format_white_space, indent,
813 void vhost_user_rx_trace (vlib_main_t * vm,
814 vlib_node_runtime_t * node,
815 vhost_user_intf_t *vui,
819 vhost_user_main_t * vum = &vhost_user_main;
821 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
823 n_left = vec_len(vui->d_trace_buffers);
824 b = vui->d_trace_buffers;
830 vhost_user_input_trace_t * t0;
835 b0 = vlib_get_buffer (vm, bi0);
836 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
837 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
838 t0->virtqueue = virtqueue;
839 t0->device_index = vui - vum->vhost_user_interfaces;
840 #if VHOST_USER_COPY_TX_HDR == 1
841 clib_memcpy(&t0->hdr, b0->pre_data, sizeof(virtio_net_hdr_t));
848 static inline void vhost_user_send_call(vlib_main_t * vm, vhost_user_vring_t * vq)
850 vhost_user_main_t * vum = &vhost_user_main;
852 int rv __attribute__((unused));
853 /* $$$$ pay attention to rv */
854 rv = write(vq->callfd, &x, sizeof(x));
855 vq->n_since_last_int = 0;
856 vq->int_deadline = vlib_time_now(vm) + vum->coalesce_time;
860 static u32 vhost_user_if_input ( vlib_main_t * vm,
861 vhost_user_main_t * vum,
862 vhost_user_intf_t * vui,
863 vlib_node_runtime_t * node)
865 vhost_user_vring_t * txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
866 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
867 uword n_rx_packets = 0, n_rx_bytes = 0;
869 u32 n_left_to_next, * to_next;
872 uword n_trace = vlib_get_trace_count (vm, node);
874 u32 cpu_index, rx_len, drops, flush;
875 f64 now = vlib_time_now (vm);
877 vec_reset_length (vui->d_trace_buffers);
879 /* no descriptor ptr - bail out */
880 if (PREDICT_FALSE(!txvq->desc || !txvq->avail || !txvq->enabled))
883 /* do we have pending intterupts ? */
884 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
885 vhost_user_send_call(vm, txvq);
887 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
888 vhost_user_send_call(vm, rxvq);
890 /* only bit 0 of avail.flags is used so we don't want to deal with this
891 interface if any other bit is set */
892 if (PREDICT_FALSE(txvq->avail->flags & 0xFFFE))
896 if (txvq->avail->idx == txvq->last_avail_idx)
899 if (PREDICT_TRUE(txvq->avail->idx > txvq->last_avail_idx))
900 n_left = txvq->avail->idx - txvq->last_avail_idx;
902 n_left = (u16) -1 - txvq->last_avail_idx + txvq->avail->idx;
904 if (PREDICT_FALSE(!vui->admin_up)) {
905 /* if intf is admin down, just drop all packets waiting in the ring */
906 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
907 CLIB_MEMORY_BARRIER();
908 txvq->used->idx = txvq->last_used_idx;
909 vhost_user_log_dirty_ring(vui, txvq, idx);
910 vhost_user_send_call(vm, txvq);
914 if (PREDICT_FALSE(n_left > txvq->qsz))
917 qsz_mask = txvq->qsz - 1;
918 cpu_index = os_get_cpu_number();
922 if (n_left > VLIB_FRAME_SIZE)
923 n_left = VLIB_FRAME_SIZE;
925 /* Allocate some buffers.
926 * Note that buffers that are chained for jumbo
927 * frames are allocated separately using a slower path.
928 * The idea is to be certain to have enough buffers at least
929 * to cycle through the descriptors without having to check for errors.
930 * For jumbo frames, the bottleneck is memory copy anyway.
932 if (PREDICT_FALSE(!vum->rx_buffers[cpu_index])) {
933 vec_alloc (vum->rx_buffers[cpu_index], VLIB_FRAME_SIZE);
935 if (PREDICT_FALSE(!vum->rx_buffers[cpu_index]))
936 flush = n_left; //Drop all input
939 if (PREDICT_FALSE(_vec_len(vum->rx_buffers[cpu_index]) < n_left)) {
940 _vec_len(vum->rx_buffers[cpu_index]) +=
941 vlib_buffer_alloc_from_free_list(vm, vum->rx_buffers[cpu_index] + _vec_len(vum->rx_buffers[cpu_index]),
942 VLIB_FRAME_SIZE - _vec_len(vum->rx_buffers[cpu_index]),
943 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
945 if (PREDICT_FALSE(n_left > _vec_len(vum->rx_buffers[cpu_index])))
946 flush = n_left - _vec_len(vum->rx_buffers[cpu_index]);
949 if (PREDICT_FALSE(flush)) {
950 //Remove some input buffers
953 vlib_error_count(vm, vhost_user_input_node.index,
954 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
956 u16 desc_chain_head = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
957 txvq->last_avail_idx++;
958 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
959 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
960 vhost_user_log_dirty_ring(vui, txvq, ring[txvq->last_used_idx & qsz_mask]);
961 txvq->last_used_idx++;
966 rx_len = vec_len(vum->rx_buffers[cpu_index]); //vector might be null
968 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
970 while (n_left > 0 && n_left_to_next > 0) {
971 vlib_buffer_t *b_head, *b_current;
972 u32 bi_head, bi_current;
973 u16 desc_chain_head, desc_current;
974 u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
976 desc_chain_head = desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
977 bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
978 b_head = b_current = vlib_get_buffer (vm, bi_head);
979 vlib_buffer_chain_init(b_head);
982 if (PREDICT_TRUE(vui->is_any_layout) ||
983 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)) {
984 /* ANYLAYOUT or single buffer */
985 offset = vui->virtio_net_hdr_sz;
987 /* CSR case without ANYLAYOUT, skip 1st buffer */
988 offset = txvq->desc[desc_current].len;
992 void * buffer_addr = map_guest_mem(vui, txvq->desc[desc_current].addr);
993 if (PREDICT_FALSE(buffer_addr == 0)) {
994 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
998 #if VHOST_USER_COPY_TX_HDR == 1
999 if (PREDICT_TRUE(offset))
1000 clib_memcpy(b->pre_data, buffer_addr, sizeof(virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
1003 if (txvq->desc[desc_current].len > offset) {
1004 u16 len = txvq->desc[desc_current].len - offset;
1005 u16 copied = vlib_buffer_chain_append_data_with_alloc(vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
1006 b_head, &b_current, buffer_addr + offset, len);
1008 if (copied != len) {
1009 error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
1015 /* if next flag is set, take next desc in the chain */
1016 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT )
1017 desc_current = txvq->desc[desc_current].next;
1022 /* consume the descriptor and return it as used */
1023 txvq->last_avail_idx++;
1024 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1025 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1026 vhost_user_log_dirty_ring(vui, txvq, ring[txvq->last_used_idx & qsz_mask]);
1027 txvq->last_used_idx++;
1029 if(PREDICT_FALSE(b_head->current_length < 14 &&
1030 error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) {
1031 error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
1034 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_head);
1036 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1037 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32)~0;
1038 b_head->error = node->errors[error];
1040 if (PREDICT_FALSE (n_trace > n_rx_packets))
1041 vec_add1 (vui->d_trace_buffers, bi_head);
1043 if (PREDICT_FALSE(error)) {
1045 next0 = VHOST_USER_RX_NEXT_DROP;
1047 n_rx_bytes += b_head->current_length + b_head->total_length_not_including_first_buffer;
1049 next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
1052 to_next[0] = bi_head;
1055 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1056 to_next, n_left_to_next,
1061 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1064 if (PREDICT_TRUE(vum->rx_buffers[cpu_index] != 0))
1065 _vec_len(vum->rx_buffers[cpu_index]) = rx_len;
1067 /* give buffers back to driver */
1068 CLIB_MEMORY_BARRIER();
1069 txvq->used->idx = txvq->last_used_idx;
1070 vhost_user_log_dirty_ring(vui, txvq, idx);
1072 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
1074 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
1075 vlib_set_trace_count (vm, node, n_trace - vec_len (vui->d_trace_buffers));
1078 /* interrupt (call) handling */
1079 if((txvq->callfd > 0) && !(txvq->avail->flags & 1)) {
1080 txvq->n_since_last_int += n_rx_packets;
1082 if(txvq->n_since_last_int > vum->coalesce_frames)
1083 vhost_user_send_call(vm, txvq);
1086 if (PREDICT_FALSE(drops)) {
1087 vlib_increment_simple_counter
1088 (vnet_main.interface_main.sw_if_counters
1089 + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number(),
1090 vui->sw_if_index, drops);
1093 /* increase rx counters */
1094 vlib_increment_combined_counter
1095 (vnet_main.interface_main.combined_sw_if_counters
1096 + VNET_INTERFACE_COUNTER_RX,
1097 os_get_cpu_number(),
1099 n_rx_packets, n_rx_bytes);
1101 return n_rx_packets;
1105 vhost_user_input (vlib_main_t * vm,
1106 vlib_node_runtime_t * node,
1109 vhost_user_main_t * vum = &vhost_user_main;
1110 dpdk_main_t * dm = &dpdk_main;
1111 vhost_user_intf_t * vui;
1112 uword n_rx_packets = 0;
1113 u32 cpu_index = os_get_cpu_number();
1116 for(i = 0; i < vec_len(vum->vhost_user_interfaces); i++ )
1118 vui = vec_elt_at_index(vum->vhost_user_interfaces, i);
1120 (i % dm->input_cpu_count) == (cpu_index - dm->input_cpu_first_index))
1121 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1123 return n_rx_packets;
1126 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1127 .function = vhost_user_input,
1128 .type = VLIB_NODE_TYPE_INPUT,
1129 .name = "vhost-user-input",
1131 /* Will be enabled if/when hardware is detected. */
1132 .state = VLIB_NODE_STATE_DISABLED,
1134 .format_buffer = format_ethernet_header_with_length,
1135 .format_trace = format_vhost_user_input_trace,
1137 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1138 .error_strings = vhost_user_input_func_error_strings,
1140 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1142 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1143 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1148 vhost_user_intfc_tx (vlib_main_t * vm,
1149 vlib_node_runtime_t * node,
1150 vlib_frame_t * frame)
1152 u32 * buffers = vlib_frame_args (frame);
1155 vhost_user_main_t * vum = &vhost_user_main;
1156 uword n_packets = 0;
1157 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
1158 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1159 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1161 u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1163 if (PREDICT_FALSE(!vui->is_up))
1166 if (PREDICT_FALSE(!rxvq->desc || !rxvq->avail || vui->sock_errno != 0 || !rxvq->enabled)) {
1167 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1171 if (PREDICT_FALSE(vui->lockp != 0))
1173 while (__sync_lock_test_and_set (vui->lockp, 1))
1177 /* only bit 0 of avail.flags is used so we don't want to deal with this
1178 interface if any other bit is set */
1179 if (PREDICT_FALSE(rxvq->avail->flags & 0xFFFE)) {
1180 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1184 if (PREDICT_FALSE((rxvq->avail->idx == rxvq->last_avail_idx))) {
1185 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1189 n_left = n_packets = frame->n_vectors;
1190 used_index = rxvq->used->idx;
1191 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1195 vlib_buffer_t *b0, *current_b0;
1196 u16 desc_chain_head, desc_current, desc_len;
1201 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
1203 b0 = vlib_get_buffer (vm, buffers[0]);
1207 if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1208 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1212 desc_current = desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1213 offset = vui->virtio_net_hdr_sz;
1215 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1216 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1219 CLIB_PREFETCH(buffer_addr, clib_min(rxvq->desc[desc_current].len,
1220 4*CLIB_CACHE_LINE_BYTES), STORE);
1222 virtio_net_hdr_mrg_rxbuf_t * hdr = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1224 hdr->hdr.gso_type = 0;
1226 vhost_user_log_dirty_pages(vui, rxvq->desc[desc_current].addr, vui->virtio_net_hdr_sz);
1228 if (vui->virtio_net_hdr_sz == 12)
1229 hdr->num_buffers = 1;
1231 u16 bytes_left = b0->current_length;
1232 buffer_addr += offset;
1235 //FIXME: This was in the code but I don't think it is valid
1236 /*if (PREDICT_FALSE(!vui->is_any_layout && (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))) {
1237 rxvq->desc[desc_current].len = vui->virtio_net_hdr_sz;
1241 if (!bytes_left) { //Get new input
1242 if (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT) {
1243 current_b0 = vlib_get_buffer(vm, current_b0->next_buffer);
1244 bytes_left = current_b0->current_length;
1251 if (rxvq->desc[desc_current].len <= offset) { //Get new output
1252 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) {
1254 desc_current = rxvq->desc[desc_current].next;
1255 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1256 used_index -= hdr->num_buffers - 1;
1257 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1258 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1261 } else if (vui->virtio_net_hdr_sz == 12) { //MRG is available
1263 //Move from available to used buffer
1264 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1265 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1266 vhost_user_log_dirty_ring(vui, rxvq, ring[used_index & qsz_mask]);
1267 rxvq->last_avail_idx++;
1271 if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1272 //Dequeue queued descriptors for this packet
1273 used_index -= hdr->num_buffers - 1;
1274 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1275 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1280 desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1281 desc_current = desc_chain_head;
1284 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1285 //Dequeue queued descriptors for this packet
1286 used_index -= hdr->num_buffers - 1;
1287 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1288 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1292 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1297 u16 bytes_to_copy = bytes_left > (rxvq->desc[desc_current].len - offset) ? (rxvq->desc[desc_current].len - offset) : bytes_left;
1298 clib_memcpy(buffer_addr, vlib_buffer_get_current (current_b0) + current_b0->current_length - bytes_left, bytes_to_copy);
1300 vhost_user_log_dirty_pages(vui, rxvq->desc[desc_current].addr + offset, bytes_to_copy);
1301 bytes_left -= bytes_to_copy;
1302 offset += bytes_to_copy;
1303 buffer_addr += bytes_to_copy;
1304 desc_len += bytes_to_copy;
1307 //Move from available to used ring
1308 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1309 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1310 vhost_user_log_dirty_ring(vui, rxvq, ring[used_index & qsz_mask]);
1312 rxvq->last_avail_idx++;
1317 CLIB_MEMORY_BARRIER();
1318 rxvq->used->idx = used_index;
1319 vhost_user_log_dirty_ring(vui, rxvq, idx);
1321 /* interrupt (call) handling */
1322 if((rxvq->callfd > 0) && !(rxvq->avail->flags & 1)) {
1323 rxvq->n_since_last_int += n_packets - n_left;
1325 if(rxvq->n_since_last_int > vum->coalesce_frames)
1326 vhost_user_send_call(vm, rxvq);
1331 if (PREDICT_FALSE(vui->lockp != 0))
1334 if (PREDICT_FALSE(n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) {
1335 vlib_error_count(vm, node->node_index, error, n_left);
1336 vlib_increment_simple_counter
1337 (vnet_main.interface_main.sw_if_counters
1338 + VNET_INTERFACE_COUNTER_DROP,
1339 os_get_cpu_number(),
1344 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1345 return frame->n_vectors;
1348 static clib_error_t *
1349 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1351 vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
1352 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1353 vhost_user_main_t * vum = &vhost_user_main;
1354 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1356 vui->admin_up = is_up;
1359 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1360 VNET_HW_INTERFACE_FLAG_LINK_UP);
1362 return /* no error */ 0;
1365 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1366 .name = "vhost-user",
1367 .tx_function = vhost_user_intfc_tx,
1368 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1369 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1370 .format_device_name = format_vhost_user_interface_name,
1371 .name_renumber = vhost_user_name_renumber,
1372 .admin_up_down_function = vhost_user_interface_admin_up_down,
1373 .no_flatten_output_chains = 1,
1377 vhost_user_process (vlib_main_t * vm,
1378 vlib_node_runtime_t * rt,
1381 vhost_user_main_t * vum = &vhost_user_main;
1382 vhost_user_intf_t * vui;
1383 struct sockaddr_un sun;
1385 unix_file_t template = {0};
1386 f64 timeout = 3153600000.0 /* 100 years */;
1387 uword *event_data = 0;
1389 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1390 sun.sun_family = AF_UNIX;
1391 template.read_function = vhost_user_socket_read;
1392 template.error_function = vhost_user_socket_error;
1399 vlib_process_wait_for_event_or_clock (vm, timeout);
1400 vlib_process_get_events (vm, &event_data);
1401 vec_reset_length (event_data);
1405 vec_foreach (vui, vum->vhost_user_interfaces) {
1407 if (vui->sock_is_server || !vui->active)
1410 if (vui->unix_fd == -1) {
1411 /* try to connect */
1413 strncpy(sun.sun_path, (char *) vui->sock_filename, sizeof(sun.sun_path) - 1);
1415 if (connect(sockfd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un)) == 0) {
1416 vui->sock_errno = 0;
1417 vui->unix_fd = sockfd;
1418 template.file_descriptor = sockfd;
1419 vui->unix_file_index = unix_file_add (&unix_main, &template);
1420 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd, vui - vum->vhost_user_interfaces);
1422 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1427 vui->sock_errno = errno;
1430 /* check if socket is alive */
1432 socklen_t len = sizeof (error);
1433 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1436 vhost_user_if_disconnect(vui);
1443 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1444 .function = vhost_user_process,
1445 .type = VLIB_NODE_TYPE_PROCESS,
1446 .name = "vhost-user-process",
1449 int vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1452 vhost_user_main_t * vum = &vhost_user_main;
1453 vhost_user_intf_t * vui;
1457 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1460 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1462 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1465 // interface is inactive
1467 // disconnect interface sockets
1468 vhost_user_if_disconnect(vui);
1469 // add to inactive interface list
1470 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1472 // reset renumbered iface
1473 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1474 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1476 ethernet_delete_interface (vnm, vui->hw_if_index);
1477 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1482 // init server socket on specified sock_filename
1483 static int vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1486 struct sockaddr_un un;
1488 /* create listening socket */
1489 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1492 return VNET_API_ERROR_SYSCALL_ERROR_1;
1495 un.sun_family = AF_UNIX;
1496 strcpy((char *) un.sun_path, (char *) sock_filename);
1498 /* remove if exists */
1499 unlink( (char *) sock_filename);
1501 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1503 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1504 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1508 if (listen(fd, 1) == -1) {
1509 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1513 unix_file_t template = {0};
1514 template.read_function = vhost_user_socksvr_accept_ready;
1515 template.file_descriptor = fd;
1516 unix_file_add (&unix_main, &template);
1525 // get new vhost_user_intf_t from inactive interfaces or create new one
1526 static vhost_user_intf_t *vhost_user_vui_new()
1528 vhost_user_main_t * vum = &vhost_user_main;
1529 vhost_user_intf_t * vui = NULL;
1530 int inactive_cnt = vec_len(vum->vhost_user_inactive_interfaces_index);
1531 // if there are any inactive ifaces
1532 if (inactive_cnt > 0) {
1534 u32 vui_idx = vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1535 if (vec_len(vum->vhost_user_interfaces) > vui_idx) {
1536 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1537 DBG_SOCK("reusing inactive vhost-user interface index %d", vui_idx);
1539 // "remove" from inactive list
1540 _vec_len(vum->vhost_user_inactive_interfaces_index) -= 1;
1543 // vui was not retrieved from inactive ifaces - create new
1545 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1549 // create ethernet interface for vhost user intf
1550 static void vhost_user_create_ethernet(vnet_main_t * vnm, vlib_main_t * vm,
1551 vhost_user_intf_t *vui, u8 *hwaddress)
1553 vhost_user_main_t * vum = &vhost_user_main;
1555 clib_error_t * error;
1557 /* create hw and sw interface */
1559 clib_memcpy(hwaddr, hwaddress, 6);
1561 f64 now = vlib_time_now(vm);
1563 rnd = (u32) (now * 1e6);
1564 rnd = random_u32 (&rnd);
1566 clib_memcpy (hwaddr+2, &rnd, sizeof(rnd));
1571 error = ethernet_register_interface
1573 vhost_user_dev_class.index,
1574 vui - vum->vhost_user_interfaces /* device instance */,
1575 hwaddr /* ethernet address */,
1577 0 /* flag change */);
1579 clib_error_report (error);
1581 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1582 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1585 // initialize vui with specified attributes
1586 static void vhost_user_vui_init(vnet_main_t * vnm,
1587 vhost_user_intf_t *vui, int sockfd,
1588 const char * sock_filename,
1589 u8 is_server, u64 feature_mask,
1592 vnet_sw_interface_t * sw;
1593 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1594 vlib_thread_main_t * tm = vlib_get_thread_main();
1597 vui->unix_fd = sockfd;
1598 vui->sw_if_index = sw->sw_if_index;
1599 vui->num_vrings = 2;
1600 vui->sock_is_server = is_server;
1601 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
1602 vui->sock_errno = 0;
1604 vui->feature_mask = feature_mask;
1606 vui->unix_file_index = ~0;
1607 vui->log_base_addr = 0;
1609 for (q = 0; q < 2; q++) {
1610 vui->vrings[q].enabled = 0;
1613 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1616 *sw_if_index = vui->sw_if_index;
1618 if (tm->n_vlib_mains > 1)
1620 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1621 CLIB_CACHE_LINE_BYTES);
1622 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
1626 // register vui and start polling on it
1627 static void vhost_user_vui_register(vlib_main_t * vm, vhost_user_intf_t *vui)
1629 vhost_user_main_t * vum = &vhost_user_main;
1630 dpdk_main_t * dm = &dpdk_main;
1632 vlib_thread_main_t * tm = vlib_get_thread_main();
1634 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
1635 vui - vum->vhost_user_interfaces);
1636 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
1637 vui - vum->vhost_user_interfaces);
1640 cpu_index = dm->input_cpu_first_index +
1641 (vui - vum->vhost_user_interfaces) % dm->input_cpu_count;
1643 if (tm->n_vlib_mains == 1)
1644 vlib_node_set_state (vm, vhost_user_input_node.index,
1645 VLIB_NODE_STATE_POLLING);
1646 else if (!dm->have_io_threads)
1647 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
1648 VLIB_NODE_STATE_POLLING);
1650 /* tell process to start polling for sockets */
1651 vlib_process_signal_event(vm, vhost_user_process_node.index, 0, 0);
1654 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1655 const char * sock_filename,
1659 u8 renumber, u32 custom_dev_instance,
1662 vhost_user_intf_t * vui = NULL;
1663 dpdk_main_t * dm = &dpdk_main;
1664 vlib_thread_main_t * tm = vlib_get_thread_main();
1669 if (tm->n_vlib_mains > 1 && dm->have_io_threads)
1671 clib_warning("vhost-user interfaces are not supported with multiple io threads");
1676 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1681 vui = vhost_user_vui_new ();
1682 ASSERT(vui != NULL);
1684 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1685 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1686 feature_mask, &sw_if_idx);
1689 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1692 vhost_user_vui_register (vm, vui);
1695 *sw_if_index = sw_if_idx;
1700 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1701 const char * sock_filename,
1705 u8 renumber, u32 custom_dev_instance)
1707 vhost_user_main_t * vum = &vhost_user_main;
1708 vhost_user_intf_t * vui = NULL;
1714 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1717 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1719 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1722 // interface is inactive
1724 // disconnect interface sockets
1725 vhost_user_if_disconnect(vui);
1728 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1733 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1734 feature_mask, &sw_if_idx);
1737 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1740 vhost_user_vui_register (vm, vui);
1746 vhost_user_connect_command_fn (vlib_main_t * vm,
1747 unformat_input_t * input,
1748 vlib_cli_command_t * cmd)
1750 unformat_input_t _line_input, * line_input = &_line_input;
1751 u8 * sock_filename = NULL;
1754 u64 feature_mask = (u64)~0;
1756 u32 custom_dev_instance = ~0;
1760 /* Get a line of input. */
1761 if (! unformat_user (input, unformat_line_input, line_input))
1764 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1765 if (unformat (line_input, "socket %s", &sock_filename))
1767 else if (unformat (line_input, "server"))
1769 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1771 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1773 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1777 return clib_error_return (0, "unknown input `%U'",
1778 format_unformat_error, input);
1780 unformat_free (line_input);
1782 vnet_main_t *vnm = vnet_get_main();
1784 vhost_user_create_if(vnm, vm, (char *)sock_filename,
1785 is_server, &sw_if_index, feature_mask,
1786 renumber, custom_dev_instance, hw);
1788 vec_free(sock_filename);
1794 vhost_user_delete_command_fn (vlib_main_t * vm,
1795 unformat_input_t * input,
1796 vlib_cli_command_t * cmd)
1798 unformat_input_t _line_input, * line_input = &_line_input;
1799 u32 sw_if_index = ~0;
1801 /* Get a line of input. */
1802 if (! unformat_user (input, unformat_line_input, line_input))
1805 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1806 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1809 return clib_error_return (0, "unknown input `%U'",
1810 format_unformat_error, input);
1812 unformat_free (line_input);
1814 vnet_main_t *vnm = vnet_get_main();
1816 vhost_user_delete_if(vnm, vm, sw_if_index);
1821 int vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1824 vhost_user_main_t * vum = &vhost_user_main;
1825 vhost_user_intf_t * vui;
1826 vhost_user_intf_details_t * r_vuids = NULL;
1827 vhost_user_intf_details_t * vuid = NULL;
1828 u32 * hw_if_indices = 0;
1829 vnet_hw_interface_t * hi;
1836 vec_foreach (vui, vum->vhost_user_interfaces) {
1838 vec_add1(hw_if_indices, vui->hw_if_index);
1841 for (i = 0; i < vec_len (hw_if_indices); i++) {
1842 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1843 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1845 vec_add2(r_vuids, vuid, 1);
1846 vuid->sw_if_index = vui->sw_if_index;
1847 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1848 vuid->features = vui->features;
1849 vuid->is_server = vui->sock_is_server;
1850 vuid->num_regions = vui->nregions;
1851 vuid->sock_errno = vui->sock_errno;
1852 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1853 ARRAY_LEN(vuid->sock_filename)-1);
1855 s = format (s, "%v%c", hi->name, 0);
1857 strncpy((char *)vuid->if_name, (char *)s,
1858 ARRAY_LEN(vuid->if_name)-1);
1863 vec_free (hw_if_indices);
1865 *out_vuids = r_vuids;
1871 show_vhost_user_command_fn (vlib_main_t * vm,
1872 unformat_input_t * input,
1873 vlib_cli_command_t * cmd)
1875 clib_error_t * error = 0;
1876 vnet_main_t * vnm = vnet_get_main();
1877 vhost_user_main_t * vum = &vhost_user_main;
1878 vhost_user_intf_t * vui;
1879 u32 hw_if_index, * hw_if_indices = 0;
1880 vnet_hw_interface_t * hi;
1883 struct feat_struct { u8 bit; char *str;};
1884 struct feat_struct *feat_entry;
1886 static struct feat_struct feat_array[] = {
1887 #define _(s,b) { .str = #s, .bit = b, },
1888 foreach_virtio_net_feature
1893 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1894 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1895 vec_add1 (hw_if_indices, hw_if_index);
1896 vlib_cli_output(vm, "add %d", hw_if_index);
1898 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1901 error = clib_error_return (0, "unknown input `%U'",
1902 format_unformat_error, input);
1906 if (vec_len (hw_if_indices) == 0) {
1907 vec_foreach (vui, vum->vhost_user_interfaces) {
1909 vec_add1(hw_if_indices, vui->hw_if_index);
1912 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1913 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1914 vum->coalesce_frames, vum->coalesce_time);
1916 for (i = 0; i < vec_len (hw_if_indices); i++) {
1917 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1918 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1919 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1920 hi->name, hw_if_indices[i]);
1922 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1923 vui->virtio_net_hdr_sz, vui->features);
1925 feat_entry = (struct feat_struct *) &feat_array;
1926 while(feat_entry->str) {
1927 if (vui->features & (1 << feat_entry->bit))
1928 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1932 vlib_cli_output (vm, "\n");
1935 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1936 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1937 strerror(vui->sock_errno));
1939 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1942 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1943 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1945 for (j = 0; j < vui->nregions; j++) {
1946 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1947 vui->region_mmap_fd[j],
1948 vui->regions[j].guest_phys_addr,
1949 vui->regions[j].memory_size,
1950 vui->regions[j].userspace_addr,
1951 vui->regions[j].mmap_offset,
1952 pointer_to_uword( vui->region_mmap_addr[j]) );
1954 for (q = 0; q < vui->num_vrings; q++) {
1955 vlib_cli_output(vm, "\n Virtqueue %d\n", q);
1957 vlib_cli_output(vm, " qsz %d last_avail_idx %d last_used_idx %d\n",
1959 vui->vrings[q].last_avail_idx,
1960 vui->vrings[q].last_used_idx);
1962 if (vui->vrings[q].avail && vui->vrings[q].used)
1963 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1964 vui->vrings[q].avail->flags,
1965 vui->vrings[q].avail->idx,
1966 vui->vrings[q].used->flags,
1967 vui->vrings[q].used->idx);
1969 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d\n",
1970 vui->vrings[q].kickfd,
1971 vui->vrings[q].callfd,
1972 vui->vrings[q].errfd);
1975 vlib_cli_output(vm, "\n descriptor table:\n");
1976 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1977 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1978 for(j = 0; j < vui->vrings[q].qsz; j++) {
1979 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1981 vui->vrings[q].desc[j].addr,
1982 vui->vrings[q].desc[j].len,
1983 vui->vrings[q].desc[j].flags,
1984 vui->vrings[q].desc[j].next,
1985 pointer_to_uword(map_guest_mem(vui, vui->vrings[q].desc[j].addr)));}
1988 vlib_cli_output (vm, "\n");
1991 vec_free (hw_if_indices);
1995 static clib_error_t *
1996 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
1998 vhost_user_main_t * vum = &vhost_user_main;
2000 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2002 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2004 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2006 else if (unformat (input, "dont-dump-memory"))
2007 vum->dont_dump_vhost_user_memory = 1;
2009 return clib_error_return (0, "unknown input `%U'",
2010 format_unformat_error, input);
2016 /* vhost-user { ... } configuration. */
2017 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2020 vhost_user_unmap_all (void)
2022 vhost_user_main_t * vum = &vhost_user_main;
2023 vhost_user_intf_t * vui;
2025 if (vum->dont_dump_vhost_user_memory)
2027 vec_foreach (vui, vum->vhost_user_interfaces)
2029 unmap_all_mem_regions(vui);