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 \
64 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
65 _(MMAP_FAIL, "mmap failure")
68 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
69 foreach_vhost_user_tx_func_error
71 VHOST_USER_TX_FUNC_N_ERROR,
72 } vhost_user_tx_func_error_t;
74 static char * vhost_user_tx_func_error_strings[] = {
76 foreach_vhost_user_tx_func_error
80 #define foreach_vhost_user_input_func_error \
81 _(NO_ERROR, "no error") \
82 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)")
85 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
86 foreach_vhost_user_input_func_error
88 VHOST_USER_INPUT_FUNC_N_ERROR,
89 } vhost_user_input_func_error_t;
91 static char * vhost_user_input_func_error_strings[] = {
93 foreach_vhost_user_input_func_error
97 static vhost_user_main_t vhost_user_main = {
101 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
102 .name = "vhost-user",
105 static u8 * format_vhost_user_interface_name (u8 * s, va_list * args)
107 u32 i = va_arg (*args, u32);
108 u32 show_dev_instance = ~0;
109 vhost_user_main_t * vum = &vhost_user_main;
111 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
112 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
114 if (show_dev_instance != ~0)
115 i = show_dev_instance;
117 s = format (s, "VirtualEthernet0/0/%d", i);
121 static int vhost_user_name_renumber (vnet_hw_interface_t * hi,
122 u32 new_dev_instance)
124 vhost_user_main_t * vum = &vhost_user_main;
126 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
127 hi->dev_instance, ~0);
129 vum->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
132 DBG_SOCK("renumbered vhost-user interface dev_instance %d to %d",
133 hi->dev_instance, new_dev_instance);
139 static inline void * map_guest_mem(vhost_user_intf_t * vui, u64 addr)
142 for (i=0; i<vui->nregions; i++) {
143 if ((vui->regions[i].guest_phys_addr <= addr) &&
144 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > addr)) {
145 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].guest_phys_addr);
148 DBG_VQ("failed to map guest mem addr %llx", addr);
152 static inline void * map_user_mem(vhost_user_intf_t * vui, u64 addr)
155 for (i=0; i<vui->nregions; i++) {
156 if ((vui->regions[i].userspace_addr <= addr) &&
157 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > addr)) {
158 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].userspace_addr);
164 static long get_huge_page_size(int fd)
171 static void unmap_all_mem_regions(vhost_user_intf_t * vui)
174 for (i=0; i<vui->nregions; i++) {
175 if (vui->region_mmap_addr[i] != (void *) -1) {
177 long page_sz = get_huge_page_size(vui->region_mmap_fd[i]);
179 ssize_t map_sz = (vui->regions[i].memory_size +
180 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
182 r = munmap(vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, map_sz);
184 DBG_SOCK("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
185 vui->region_mmap_addr[i], map_sz, page_sz);
187 vui->region_mmap_addr[i]= (void *) -1;
190 clib_warning("failed to unmap memory region (errno %d)", errno);
192 close(vui->region_mmap_fd[i]);
199 static clib_error_t * vhost_user_callfd_read_ready (unix_file_t * uf)
201 __attribute__((unused)) int n;
203 n = read(uf->file_descriptor, ((char*)&buff), 8);
207 static inline void vhost_user_if_disconnect(vhost_user_intf_t * vui)
209 vhost_user_main_t * vum = &vhost_user_main;
210 vnet_main_t * vnm = vnet_get_main();
213 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
215 if (vui->unix_file_index != ~0) {
216 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
217 vui->unix_file_index = ~0;
220 hash_unset(vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
221 hash_unset(vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
225 for (q = 0; q < vui->num_vrings; q++) {
226 vui->vrings[q].desc = NULL;
227 vui->vrings[q].avail = NULL;
228 vui->vrings[q].used = NULL;
231 unmap_all_mem_regions(vui);
232 DBG_SOCK("interface ifindex %d disconnected", vui->sw_if_index);
235 static clib_error_t * vhost_user_socket_read (unix_file_t * uf)
238 int fd, number_of_fds = 0;
239 int fds[VHOST_MEMORY_MAX_NREGIONS];
240 vhost_user_msg_t msg;
243 vhost_user_main_t * vum = &vhost_user_main;
244 vhost_user_intf_t * vui;
245 struct cmsghdr *cmsg;
248 unix_file_t template = {0};
249 vnet_main_t * vnm = vnet_get_main();
251 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
252 uf->file_descriptor);
254 DBG_SOCK ("FD %d doesn't belong to any interface",
255 uf->file_descriptor);
259 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
261 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
263 memset(&mh, 0, sizeof(mh));
264 memset(control, 0, sizeof(control));
266 /* set the payload */
267 iov[0].iov_base = (void *) &msg;
268 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
272 mh.msg_control = control;
273 mh.msg_controllen = sizeof(control);
275 n = recvmsg(uf->file_descriptor, &mh, 0);
277 if (n != VHOST_USER_MSG_HDR_SZ)
280 if (mh.msg_flags & MSG_CTRUNC) {
284 cmsg = CMSG_FIRSTHDR(&mh);
286 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
287 (cmsg->cmsg_type == SCM_RIGHTS) &&
288 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
289 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
290 memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
293 /* version 1, no reply bit set*/
294 if ((msg.flags & 7) != 1) {
295 DBG_SOCK("malformed message received. closing socket");
300 int rv __attribute__((unused));
301 /* $$$$ pay attention to rv */
302 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
305 switch (msg.request) {
306 case VHOST_USER_GET_FEATURES:
307 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
311 msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
312 (1 << FEAT_VIRTIO_F_ANY_LAYOUT);
313 msg.u64 &= vui->feature_mask;
315 msg.size = sizeof(msg.u64);
318 case VHOST_USER_SET_FEATURES:
319 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
320 vui->hw_if_index, msg.u64);
322 vui->features = msg.u64;
323 if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
324 vui->virtio_net_hdr_sz = 12;
326 vui->virtio_net_hdr_sz = 10;
328 vui->is_any_layout = (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
330 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
331 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
334 for (q = 0; q < 2; q++) {
335 vui->vrings[q].desc = 0;
336 vui->vrings[q].avail = 0;
337 vui->vrings[q].used = 0;
340 DBG_SOCK("interface %d disconnected", vui->sw_if_index);
344 case VHOST_USER_SET_MEM_TABLE:
345 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
346 vui->hw_if_index, msg.memory.nregions);
348 if ((msg.memory.nregions < 1) ||
349 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
351 DBG_SOCK("number of mem regions must be between 1 and %i",
352 VHOST_MEMORY_MAX_NREGIONS);
357 if (msg.memory.nregions != number_of_fds) {
358 DBG_SOCK("each memory region must have FD");
361 unmap_all_mem_regions(vui);
362 for(i=0; i < msg.memory.nregions; i++) {
363 memcpy(&(vui->regions[i]), &msg.memory.regions[i],
364 sizeof(vhost_user_memory_region_t));
366 long page_sz = get_huge_page_size(fds[i]);
368 /* align size to 2M page */
369 ssize_t map_sz = (vui->regions[i].memory_size +
370 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
372 vui->region_mmap_addr[i] = mmap(0, map_sz, PROT_READ | PROT_WRITE,
373 MAP_SHARED, fds[i], 0);
375 DBG_SOCK("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
376 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i], page_sz);
378 if (vui->region_mmap_addr[i] == MAP_FAILED) {
379 clib_warning("failed to map memory. errno is %d", errno);
382 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
383 vui->region_mmap_fd[i] = fds[i];
385 vui->nregions = msg.memory.nregions;
388 case VHOST_USER_SET_VRING_NUM:
389 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
390 vui->hw_if_index, msg.state.index, msg.state.num);
392 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
393 (msg.state.num == 0) || /* it cannot be zero */
394 (msg.state.num % 2)) /* must be power of 2 */
396 vui->vrings[msg.state.index].qsz = msg.state.num;
399 case VHOST_USER_SET_VRING_ADDR:
400 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
401 vui->hw_if_index, msg.state.index);
403 vui->vrings[msg.state.index].desc = (vring_desc_t *)
404 map_user_mem(vui, msg.addr.desc_user_addr);
405 vui->vrings[msg.state.index].used = (vring_used_t *)
406 map_user_mem(vui, msg.addr.used_user_addr);
407 vui->vrings[msg.state.index].avail = (vring_avail_t *)
408 map_user_mem(vui, msg.addr.avail_user_addr);
410 if ((vui->vrings[msg.state.index].desc == NULL) ||
411 (vui->vrings[msg.state.index].used == NULL) ||
412 (vui->vrings[msg.state.index].avail == NULL)) {
413 DBG_SOCK("failed to map user memory for hw_if_index %d",
418 vui->vrings[msg.state.index].last_used_idx =
419 vui->vrings[msg.state.index].used->idx;
421 /* tell driver that we don't want interrupts */
422 vui->vrings[msg.state.index].used->flags |= 1;
425 case VHOST_USER_SET_OWNER:
426 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
430 case VHOST_USER_RESET_OWNER:
431 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
435 case VHOST_USER_SET_VRING_CALL:
436 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
437 vui->hw_if_index, msg.u64);
439 q = (u8) (msg.u64 & 0xFF);
441 if (!(msg.u64 & 0x100))
443 if (number_of_fds != 1)
446 /* if there is old fd, delete it */
447 if (vui->vrings[q].callfd) {
448 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
449 vui->vrings[q].callfd_idx);
450 unix_file_del (&unix_main, uf);
452 vui->vrings[q].callfd = fds[0];
453 template.read_function = vhost_user_callfd_read_ready;
454 template.file_descriptor = fds[0];
455 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
458 vui->vrings[q].callfd = -1;
461 case VHOST_USER_SET_VRING_KICK:
462 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
463 vui->hw_if_index, msg.u64);
465 q = (u8) (msg.u64 & 0xFF);
467 if (!(msg.u64 & 0x100))
469 if (number_of_fds != 1)
472 vui->vrings[q].kickfd = fds[0];
475 vui->vrings[q].kickfd = -1;
478 case VHOST_USER_SET_VRING_ERR:
479 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
480 vui->hw_if_index, msg.u64);
482 q = (u8) (msg.u64 & 0xFF);
484 if (!(msg.u64 & 0x100))
486 if (number_of_fds != 1)
494 vui->vrings[q].errfd = fd;
497 case VHOST_USER_SET_VRING_BASE:
498 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
499 vui->hw_if_index, msg.state.index, msg.state.num);
501 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
504 case VHOST_USER_GET_VRING_BASE:
505 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
506 vui->hw_if_index, msg.state.index, msg.state.num);
508 msg.state.num = vui->vrings[msg.state.index].last_used_idx;
510 msg.size = sizeof(msg.state);
513 case VHOST_USER_NONE:
514 DBG_SOCK("if %d msg VHOST_USER_NONE",
519 case VHOST_USER_SET_LOG_BASE:
520 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
525 case VHOST_USER_SET_LOG_FD:
526 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
532 DBG_SOCK("unknown vhost-user message %d received. closing socket",
537 /* if we have pointers to descriptor table, go up*/
539 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
540 vui->vrings[VHOST_NET_VRING_IDX_RX].desc) {
542 DBG_SOCK("interface %d connected", vui->sw_if_index);
544 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
549 /* if we need to reply */
552 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
553 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
560 vhost_user_if_disconnect(vui);
564 static clib_error_t * vhost_user_socket_error (unix_file_t * uf)
566 vhost_user_main_t * vum = &vhost_user_main;
567 vhost_user_intf_t * vui;
570 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
571 uf->file_descriptor);
573 DBG_SOCK ("fd %d doesn't belong to any interface",
574 uf->file_descriptor);
578 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
580 vhost_user_if_disconnect(vui);
584 static clib_error_t * vhost_user_socksvr_accept_ready (unix_file_t * uf)
586 int client_fd, client_len;
587 struct sockaddr_un client;
588 unix_file_t template = {0};
589 vhost_user_main_t * vum = &vhost_user_main;
590 vhost_user_intf_t * vui;
593 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
594 uf->file_descriptor);
596 DBG_SOCK ("fd %d doesn't belong to any interface",
597 uf->file_descriptor);
601 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
603 client_len = sizeof(client);
604 client_fd = accept (uf->file_descriptor,
605 (struct sockaddr *)&client,
606 (socklen_t *)&client_len);
609 return clib_error_return_unix (0, "accept");
611 template.read_function = vhost_user_socket_read;
612 template.error_function = vhost_user_socket_error;
613 template.file_descriptor = client_fd;
614 vui->unix_file_index = unix_file_add (&unix_main, &template);
616 vui->client_fd = client_fd;
617 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
618 vui - vum->vhost_user_interfaces);
623 static clib_error_t *
624 vhost_user_init (vlib_main_t * vm)
626 clib_error_t * error;
627 vhost_user_main_t * vum = &vhost_user_main;
628 vlib_thread_main_t * tm = vlib_get_thread_main();
630 error = vlib_call_init_function (vm, ip4_init);
634 vum->vhost_user_interface_index_by_listener_fd = hash_create (0, sizeof (uword));
635 vum->vhost_user_interface_index_by_sock_fd = hash_create (0, sizeof (uword));
636 vum->vhost_user_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
637 vum->coalesce_frames = 32;
638 vum->coalesce_time = 1e-3;
640 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
641 CLIB_CACHE_LINE_BYTES);
646 VLIB_INIT_FUNCTION (vhost_user_init);
648 static clib_error_t *
649 vhost_user_exit (vlib_main_t * vm)
655 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
658 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
659 VHOST_USER_RX_NEXT_DROP,
660 VHOST_USER_RX_N_NEXT,
667 #if VHOST_USER_COPY_TX_HDR == 1
668 virtio_net_hdr_t hdr;
670 } vhost_user_input_trace_t;
672 static u8 * format_vhost_user_input_trace (u8 * s, va_list * va)
674 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
675 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
676 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main();
677 vhost_user_main_t * vum = &vhost_user_main;
678 vhost_user_input_trace_t * t = va_arg (*va, vhost_user_input_trace_t *);
679 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces,
682 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
684 #if VHOST_USER_COPY_TX_HDR == 1
685 uword indent = format_get_indent (s);
688 s = format (s, "%U virtqueue %d",
689 format_vnet_sw_interface_name, vnm, sw,
692 #if VHOST_USER_COPY_TX_HDR == 1
693 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
694 format_white_space, indent,
703 void vhost_user_rx_trace (vlib_main_t * vm,
704 vlib_node_runtime_t * node,
705 vhost_user_intf_t *vui,
709 vhost_user_main_t * vum = &vhost_user_main;
711 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
713 n_left = vec_len(vui->d_trace_buffers);
714 b = vui->d_trace_buffers;
720 vhost_user_input_trace_t * t0;
725 b0 = vlib_get_buffer (vm, bi0);
726 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
727 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
728 t0->virtqueue = virtqueue;
729 t0->device_index = vui - vum->vhost_user_interfaces;
730 #if VHOST_USER_COPY_TX_HDR == 1
731 rte_memcpy(&t0->hdr, b0->pre_data, sizeof(virtio_net_hdr_t));
738 static inline void vhost_user_send_call(vlib_main_t * vm, vhost_user_vring_t * vq)
740 vhost_user_main_t * vum = &vhost_user_main;
742 int rv __attribute__((unused));
743 /* $$$$ pay attention to rv */
744 rv = write(vq->callfd, &x, sizeof(x));
745 vq->n_since_last_int = 0;
746 vq->int_deadline = vlib_time_now(vm) + vum->coalesce_time;
749 static u32 vhost_user_if_input ( vlib_main_t * vm,
750 vhost_user_main_t * vum,
751 vhost_user_intf_t * vui,
752 vlib_node_runtime_t * node)
754 vhost_user_vring_t * txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
755 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
756 uword n_rx_packets = 0;
759 u32 n_left_to_next, * to_next;
760 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
761 uword n_rx_bytes = 0;
762 uword n_trace = vlib_get_trace_count (vm, node);
764 f64 now = vlib_time_now (vm);
767 vec_reset_length (vui->d_trace_buffers);
768 u32 free_list_index = VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX;
770 /* no descriptor ptr - bail out */
771 if (PREDICT_FALSE(!txvq->desc))
774 /* do we have pending intterupts ? */
775 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
776 vhost_user_send_call(vm, txvq);
778 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
779 vhost_user_send_call(vm, rxvq);
781 /* only bit 0 of avail.flags is used so we don't want to deal with this
782 interface if any other bit is set */
783 if (PREDICT_FALSE(txvq->avail->flags & 0xFFFE))
787 if (txvq->avail->idx == txvq->last_avail_idx)
790 cpu_index = os_get_cpu_number();
792 if (PREDICT_TRUE(txvq->avail->idx > txvq->last_avail_idx))
793 n_left = txvq->avail->idx - txvq->last_avail_idx;
795 n_left = (u16) -1 - txvq->last_avail_idx + txvq->avail->idx;
797 if (PREDICT_FALSE(!vui->admin_up)) {
798 /* if intf is admin down, just drop all packets waiting in the ring */
799 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
800 CLIB_MEMORY_BARRIER();
801 txvq->used->idx = txvq->last_used_idx;
802 vhost_user_send_call(vm, txvq);
807 if (PREDICT_FALSE(n_left > txvq->qsz)) {
811 if (PREDICT_FALSE(n_left > VLIB_FRAME_SIZE))
812 n_left = VLIB_FRAME_SIZE;
814 /* Make sure we have some RX buffers. */
816 uword l = vec_len (vum->rx_buffers[cpu_index]);
821 if (! vum->rx_buffers[cpu_index]) {
822 vec_alloc (vum->rx_buffers[cpu_index], 2 * VLIB_FRAME_SIZE );
825 n_alloc = vlib_buffer_alloc_from_free_list
826 (vm, vum->rx_buffers[cpu_index] + l, 2 * VLIB_FRAME_SIZE - l,
830 _vec_len (vum->rx_buffers[cpu_index]) = l + n_alloc;
834 qsz_mask = txvq->qsz - 1;
837 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
839 while (n_left > 0 && n_left_to_next > 0) {
841 u16 desc_chain_head = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
842 u16 desc_current = desc_chain_head;
843 uword i_rx = vec_len (vum->rx_buffers[cpu_index]) - 1;
845 bi = vum->rx_buffers[cpu_index][i_rx];
846 b = vlib_get_buffer (vm, bi);
848 vlib_prefetch_buffer_with_index (vm, vum->rx_buffers[cpu_index][i_rx-1], STORE);
851 if (PREDICT_TRUE(vui->is_any_layout))
852 offset = vui->virtio_net_hdr_sz;
853 else if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))
854 /* WSA case, no ANYLAYOUT but single buffer */
855 offset = vui->virtio_net_hdr_sz;
857 /* CSR case without ANYLAYOUT, skip 1st buffer */
858 offset = txvq->desc[desc_current].len;
863 void * buffer_addr = map_guest_mem(vui, txvq->desc[desc_current].addr);
864 CLIB_PREFETCH (&txvq->desc[txvq->desc[desc_current].next], sizeof (vring_desc_t), READ);
866 #if VHOST_USER_COPY_TX_HDR == 1
867 if (PREDICT_TRUE(offset)) {
868 rte_memcpy(b->pre_data, buffer_addr, sizeof(virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
872 if (txvq->desc[desc_current].len > offset) {
873 u16 len = txvq->desc[desc_current].len - offset;
875 if (PREDICT_FALSE(len > VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES))
876 len = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
878 rte_memcpy(vlib_buffer_get_current (b) + ptr,
879 buffer_addr + offset, len);
881 ptr += txvq->desc[desc_current].len - offset;
884 /* if next flag is set, take next desc in the chain */
885 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT )
886 desc_current = txvq->desc[desc_current].next;
891 txvq->last_avail_idx++;
893 /* returning buffer */
894 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
895 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = ptr + vui->virtio_net_hdr_sz;
897 txvq->last_used_idx++;
899 b->current_length = ptr;
901 if(PREDICT_FALSE(b->current_length < 14)) {
902 vlib_error_count(vm, vhost_user_input_node.index,
903 VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME, 1);
909 b->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
911 _vec_len (vum->rx_buffers[cpu_index]) = i_rx;
914 * Turn this on if you run into
915 * "bad monkey" contexts, and you want to know exactly
916 * which nodes they've visited... See .../vlib/vlib/buffer.h
918 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b);
920 vnet_buffer (b)->sw_if_index[VLIB_RX] = vui->sw_if_index;
921 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0;
922 b->error = node->errors[0];
927 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
928 to_next, n_left_to_next,
931 if (PREDICT_FALSE (n_trace > n_rx_packets))
932 vec_add1 (vui->d_trace_buffers, bi);
939 /* give buffers back to driver */
940 CLIB_MEMORY_BARRIER();
941 txvq->used->idx = txvq->last_used_idx;
943 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
946 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
948 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
949 vlib_set_trace_count (vm, node, n_trace - vec_len (vui->d_trace_buffers));
952 /* if no packets received we're done */
956 /* interrupt (call) handling */
957 if((txvq->callfd > 0) && !(txvq->avail->flags & 1)) {
958 txvq->n_since_last_int += n_rx_packets;
960 if(txvq->n_since_last_int > vum->coalesce_frames)
961 vhost_user_send_call(vm, txvq);
964 /* increase rx counters */
965 vlib_increment_combined_counter
966 (vnet_main.interface_main.combined_sw_if_counters
967 + VNET_INTERFACE_COUNTER_RX,
970 n_rx_packets, n_rx_bytes);
976 vhost_user_input (vlib_main_t * vm,
977 vlib_node_runtime_t * node,
980 vhost_user_main_t * vum = &vhost_user_main;
981 dpdk_main_t * dm = &dpdk_main;
982 vhost_user_intf_t * vui;
983 uword n_rx_packets = 0;
984 u32 cpu_index = os_get_cpu_number();
987 for(i = 0; i < vec_len(vum->vhost_user_interfaces); i++ )
989 vui = vec_elt_at_index(vum->vhost_user_interfaces, i);
991 (i % dm->input_cpu_count) == (cpu_index - dm->input_cpu_first_index))
992 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
997 VLIB_REGISTER_NODE (vhost_user_input_node) = {
998 .function = vhost_user_input,
999 .type = VLIB_NODE_TYPE_INPUT,
1000 .name = "vhost-user-input",
1002 /* Will be enabled if/when hardware is detected. */
1003 .state = VLIB_NODE_STATE_DISABLED,
1005 .format_buffer = format_ethernet_header_with_length,
1006 .format_trace = format_vhost_user_input_trace,
1008 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1009 .error_strings = vhost_user_input_func_error_strings,
1011 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1013 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1014 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1019 vhost_user_intfc_tx (vlib_main_t * vm,
1020 vlib_node_runtime_t * node,
1021 vlib_frame_t * frame)
1023 u32 * buffers = vlib_frame_args (frame);
1026 vhost_user_main_t * vum = &vhost_user_main;
1027 uword n_packets = 0;
1029 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
1030 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1031 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1034 if (PREDICT_FALSE(!vui->is_up))
1037 if (PREDICT_FALSE(!rxvq->desc))
1040 if (PREDICT_FALSE(vui->lockp != 0))
1042 while (__sync_lock_test_and_set (vui->lockp, 1))
1047 /* only bit 0 of avail.flags is used so we don't want to deal with this
1048 interface if any other bit is set */
1049 if (PREDICT_FALSE(rxvq->avail->flags & 0xFFFE))
1052 if (PREDICT_FALSE((rxvq->avail->idx == rxvq->last_avail_idx) ||
1053 vui->sock_errno != 0)) {
1054 vlib_simple_counter_main_t * cm;
1055 vnet_main_t * vnm = vnet_get_main();
1057 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1058 VNET_INTERFACE_COUNTER_TX_ERROR);
1059 vlib_increment_simple_counter (cm, os_get_cpu_number(),
1060 0, frame->n_vectors);
1062 vlib_error_count (vm, node->node_index,
1063 VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF,
1068 if (PREDICT_TRUE(rxvq->avail->idx > rxvq->last_avail_idx))
1069 n_avail_desc = rxvq->avail->idx - rxvq->last_avail_idx;
1071 n_avail_desc = (u16) -1 - rxvq->last_avail_idx + rxvq->avail->idx;
1073 DBG_VQ("rxvq->avail->idx %d rxvq->last_avail_idx %d n_avail_desc %d",
1074 rxvq->avail->idx, rxvq->last_avail_idx, n_avail_desc);
1076 n_left = n_packets = frame->n_vectors;
1077 if (PREDICT_FALSE(n_packets > n_avail_desc)) {
1078 vlib_simple_counter_main_t * cm;
1079 vnet_main_t * vnm = vnet_get_main();
1081 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1082 VNET_INTERFACE_COUNTER_TX_ERROR);
1083 vlib_increment_simple_counter (cm, os_get_cpu_number(),
1084 0, frame->n_vectors);
1086 vlib_error_count (vm, node->node_index,
1087 VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF,
1088 n_packets - n_avail_desc);
1089 n_left = n_packets = n_avail_desc;
1092 used_index = rxvq->used->idx;
1093 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1097 vlib_buffer_t * b0, * b1;
1098 u16 desc_chain_head0,desc_chain_head1;
1099 u16 desc_current0,desc_current1;
1100 uword offset0, offset1;
1101 u16 bytes_left0, bytes_left1;
1102 void *buffer_addr0, *buffer_addr1;
1104 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
1105 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
1107 b0 = vlib_get_buffer (vm, buffers[0]);
1108 b1 = vlib_get_buffer (vm, buffers[1]);
1112 desc_current0 = desc_chain_head0 = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1113 desc_current1 = desc_chain_head1 = rxvq->avail->ring[(rxvq->last_avail_idx+1) & qsz_mask];
1115 offset0 = vui->virtio_net_hdr_sz;
1117 offset1 = vui->virtio_net_hdr_sz;
1119 bytes_left0 = b0->current_length;
1120 bytes_left1 = b1->current_length;
1122 buffer_addr0 = map_guest_mem(vui, rxvq->desc[desc_current0].addr);
1123 buffer_addr1 = map_guest_mem(vui, rxvq->desc[desc_current1].addr);
1125 if (PREDICT_FALSE(!buffer_addr0)) {
1126 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1129 if (PREDICT_FALSE(!buffer_addr1)) {
1130 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1134 virtio_net_hdr_mrg_rxbuf_t * hdr0 = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr0;
1135 virtio_net_hdr_mrg_rxbuf_t * hdr1 = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr1;
1136 hdr0->hdr.flags = 0;
1137 hdr1->hdr.flags = 0;
1138 hdr0->hdr.gso_type = 0;
1139 hdr1->hdr.gso_type = 0;
1141 if (vui->virtio_net_hdr_sz == 12) {
1142 hdr0->num_buffers = 1;
1143 hdr1->num_buffers = 1;
1146 buffer_addr0 += offset0;
1147 buffer_addr1 += offset1;
1149 if (PREDICT_FALSE(!vui->is_any_layout && rxvq->desc[desc_current0].flags & VIRTQ_DESC_F_NEXT))
1150 rxvq->desc[desc_current0].len = vui->virtio_net_hdr_sz;
1152 if (PREDICT_FALSE(!vui->is_any_layout && rxvq->desc[desc_current1].flags & VIRTQ_DESC_F_NEXT))
1153 rxvq->desc[desc_current1].len = vui->virtio_net_hdr_sz;
1156 if (rxvq->desc[desc_current0].len - offset0 > 0 ) {
1157 u16 bytes_to_copy = bytes_left0 > (rxvq->desc[desc_current0].len - offset0) ? (rxvq->desc[desc_current0].len - offset0) : bytes_left0;
1158 rte_memcpy(buffer_addr0, vlib_buffer_get_current (b0) + b0->current_length - bytes_left0, bytes_to_copy);
1159 bytes_left0 -= bytes_to_copy;
1162 if (rxvq->desc[desc_current0].flags & VIRTQ_DESC_F_NEXT ) {
1164 desc_current0 = rxvq->desc[desc_current1].next;
1165 buffer_addr0 = map_guest_mem(vui, rxvq->desc[desc_current0].addr);
1166 if (PREDICT_FALSE(!buffer_addr0)) {
1167 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1176 if (rxvq->desc[desc_current1].len - offset1 > 0 ) {
1177 u16 bytes_to_copy = bytes_left1 > (rxvq->desc[desc_current1].len - offset1) ? (rxvq->desc[desc_current1].len - offset1) : bytes_left1;
1178 rte_memcpy(buffer_addr1, vlib_buffer_get_current (b1) + b1->current_length - bytes_left1, bytes_to_copy);
1179 bytes_left1 -= bytes_to_copy;
1182 if (rxvq->desc[desc_current1].flags & VIRTQ_DESC_F_NEXT ) {
1184 desc_current1 = rxvq->desc[desc_current1].next;
1185 buffer_addr1 = map_guest_mem(vui, rxvq->desc[desc_current1].addr);
1186 if (PREDICT_FALSE(!buffer_addr1)) {
1187 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1195 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head0;
1196 rxvq->used->ring[used_index & qsz_mask].len = b0->current_length + vui->virtio_net_hdr_sz;
1198 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head1;
1199 rxvq->used->ring[used_index & qsz_mask].len = b1->current_length + vui->virtio_net_hdr_sz;
1201 rxvq->last_avail_idx+=2;
1207 u16 desc_chain_head;
1211 b0 = vlib_get_buffer (vm, buffers[0]);
1215 desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1216 desc_current = desc_chain_head;
1218 uword offset = vui->virtio_net_hdr_sz;
1220 u16 bytes_left = b0->current_length;
1221 buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr);
1222 if (PREDICT_FALSE(!buffer_addr)) {
1223 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1227 virtio_net_hdr_mrg_rxbuf_t * hdr = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1229 hdr->hdr.gso_type = 0;
1231 if (vui->virtio_net_hdr_sz == 12) {
1232 hdr->num_buffers = 1;
1235 buffer_addr += offset;
1237 if (PREDICT_FALSE(!vui->is_any_layout && rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))
1238 rxvq->desc[desc_current].len = vui->virtio_net_hdr_sz;
1241 if (rxvq->desc[desc_current].len - offset > 0 ) {
1242 u16 bytes_to_copy = bytes_left > (rxvq->desc[desc_current].len - offset) ? (rxvq->desc[desc_current].len - offset) : bytes_left;
1243 rte_memcpy(buffer_addr, vlib_buffer_get_current (b0) + b0->current_length - bytes_left, bytes_to_copy);
1244 bytes_left -= bytes_to_copy;
1247 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT ) {
1249 desc_current = rxvq->desc[desc_current].next;
1250 buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr);
1251 if (PREDICT_FALSE(!buffer_addr)) {
1252 vlib_error_count (vm, node->node_index, VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
1260 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1261 rxvq->used->ring[used_index & qsz_mask].len = b0->current_length + vui->virtio_net_hdr_sz;
1264 rxvq->last_avail_idx++;
1268 CLIB_MEMORY_BARRIER();
1269 rxvq->used->idx = used_index;
1271 /* interrupt (call) handling */
1272 if((rxvq->callfd > 0) && !(rxvq->avail->flags & 1)) {
1273 rxvq->n_since_last_int += n_packets - n_left;
1275 if(rxvq->n_since_last_int > vum->coalesce_frames)
1276 vhost_user_send_call(vm, rxvq);
1281 if (PREDICT_FALSE(vui->lockp != 0))
1284 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1285 return frame->n_vectors;
1288 static clib_error_t *
1289 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1291 vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
1292 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1293 vhost_user_main_t * vum = &vhost_user_main;
1294 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1296 vui->admin_up = is_up;
1299 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1300 VNET_HW_INTERFACE_FLAG_LINK_UP);
1302 return /* no error */ 0;
1305 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1306 .name = "vhost-user",
1307 .tx_function = vhost_user_intfc_tx,
1308 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1309 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1310 .format_device_name = format_vhost_user_interface_name,
1311 .name_renumber = vhost_user_name_renumber,
1312 .admin_up_down_function = vhost_user_interface_admin_up_down,
1316 vhost_user_process (vlib_main_t * vm,
1317 vlib_node_runtime_t * rt,
1320 vhost_user_main_t * vum = &vhost_user_main;
1321 vhost_user_intf_t * vui;
1322 struct sockaddr_un sun;
1324 unix_file_t template = {0};
1325 f64 timeout = 3153600000.0 /* 100 years */;
1326 uword *event_data = 0;
1328 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1329 sun.sun_family = AF_UNIX;
1330 template.read_function = vhost_user_socket_read;
1331 template.error_function = vhost_user_socket_error;
1338 vlib_process_wait_for_event_or_clock (vm, timeout);
1339 vlib_process_get_events (vm, &event_data);
1340 vec_reset_length (event_data);
1344 vec_foreach (vui, vum->vhost_user_interfaces) {
1346 if (vui->sock_is_server || !vui->active)
1349 if (vui->unix_fd == -1) {
1350 /* try to connect */
1352 strncpy(sun.sun_path, (char *) vui->sock_filename, sizeof(sun.sun_path) - 1);
1354 if (connect(sockfd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un)) == 0) {
1355 vui->sock_errno = 0;
1356 vui->unix_fd = sockfd;
1357 template.file_descriptor = sockfd;
1358 vui->unix_file_index = unix_file_add (&unix_main, &template);
1359 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd, vui - vum->vhost_user_interfaces);
1361 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1366 vui->sock_errno = errno;
1369 /* check if socket is alive */
1371 socklen_t len = sizeof (error);
1372 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1375 vhost_user_if_disconnect(vui);
1382 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1383 .function = vhost_user_process,
1384 .type = VLIB_NODE_TYPE_PROCESS,
1385 .name = "vhost-user-process",
1388 int vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1391 vhost_user_main_t * vum = &vhost_user_main;
1392 vhost_user_intf_t * vui;
1396 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1399 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1401 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1404 // interface is inactive
1406 // disconnect interface sockets
1407 vhost_user_if_disconnect(vui);
1408 // add to inactive interface list
1409 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1411 // reset renumbered iface
1412 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1413 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1415 ethernet_delete_interface (vnm, vui->hw_if_index);
1416 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1421 // init server socket on specified sock_filename
1422 static int vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1425 struct sockaddr_un un;
1427 /* create listening socket */
1428 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1431 return VNET_API_ERROR_SYSCALL_ERROR_1;
1434 un.sun_family = AF_UNIX;
1435 strcpy((char *) un.sun_path, (char *) sock_filename);
1437 /* remove if exists */
1438 unlink( (char *) sock_filename);
1440 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1442 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1443 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1447 if (listen(fd, 1) == -1) {
1448 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1452 unix_file_t template = {0};
1453 template.read_function = vhost_user_socksvr_accept_ready;
1454 template.file_descriptor = fd;
1455 unix_file_add (&unix_main, &template);
1464 // get new vhost_user_intf_t from inactive interfaces or create new one
1465 static vhost_user_intf_t *vhost_user_vui_new()
1467 vhost_user_main_t * vum = &vhost_user_main;
1468 vhost_user_intf_t * vui = NULL;
1469 int inactive_cnt = vec_len(vum->vhost_user_inactive_interfaces_index);
1470 // if there are any inactive ifaces
1471 if (inactive_cnt > 0) {
1473 u32 vui_idx = vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1474 if (vec_len(vum->vhost_user_interfaces) > vui_idx) {
1475 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1476 DBG_SOCK("reusing inactive vhost-user interface index %d", vui_idx);
1478 // "remove" from inactive list
1479 _vec_len(vum->vhost_user_inactive_interfaces_index) -= 1;
1482 // vui was not retrieved from inactive ifaces - create new
1484 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1488 // create ethernet interface for vhost user intf
1489 static void vhost_user_create_ethernet(vnet_main_t * vnm, vlib_main_t * vm,
1490 vhost_user_intf_t *vui)
1492 vhost_user_main_t * vum = &vhost_user_main;
1494 clib_error_t * error;
1496 /* create hw and sw interface */
1498 f64 now = vlib_time_now(vm);
1500 rnd = (u32) (now * 1e6);
1501 rnd = random_u32 (&rnd);
1503 memcpy (hwaddr+2, &rnd, sizeof(rnd));
1508 error = ethernet_register_interface
1510 vhost_user_dev_class.index,
1511 vui - vum->vhost_user_interfaces /* device instance */,
1512 hwaddr /* ethernet address */,
1514 0 /* flag change */);
1516 clib_error_report (error);
1519 // initialize vui with specified attributes
1520 static void vhost_user_vui_init(vnet_main_t * vnm,
1521 vhost_user_intf_t *vui, int sockfd,
1522 const char * sock_filename,
1523 u8 is_server, u64 feature_mask,
1526 vnet_sw_interface_t * sw;
1527 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1528 vlib_thread_main_t * tm = vlib_get_thread_main();
1530 vui->unix_fd = sockfd;
1531 vui->sw_if_index = sw->sw_if_index;
1532 vui->num_vrings = 2;
1533 vui->sock_is_server = is_server;
1534 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
1535 vui->sock_errno = 0;
1537 vui->feature_mask = feature_mask;
1539 vui->unix_file_index = ~0;
1541 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1544 *sw_if_index = vui->sw_if_index;
1546 if (tm->n_vlib_mains > 1)
1548 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1549 CLIB_CACHE_LINE_BYTES);
1550 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
1554 // register vui and start polling on it
1555 static void vhost_user_vui_register(vlib_main_t * vm, vhost_user_intf_t *vui)
1557 vhost_user_main_t * vum = &vhost_user_main;
1558 dpdk_main_t * dm = &dpdk_main;
1560 vlib_thread_main_t * tm = vlib_get_thread_main();
1562 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
1563 vui - vum->vhost_user_interfaces);
1564 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
1565 vui - vum->vhost_user_interfaces);
1568 cpu_index = dm->input_cpu_first_index +
1569 (vui - vum->vhost_user_interfaces) % dm->input_cpu_count;
1571 if (tm->n_vlib_mains == 1)
1572 vlib_node_set_state (vm, vhost_user_input_node.index,
1573 VLIB_NODE_STATE_POLLING);
1574 else if (!dm->have_io_threads)
1575 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
1576 VLIB_NODE_STATE_POLLING);
1578 /* tell process to start polling for sockets */
1579 vlib_process_signal_event(vm, vhost_user_process_node.index, 0, 0);
1582 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1583 const char * sock_filename,
1587 u8 renumber, u32 custom_dev_instance)
1589 vhost_user_intf_t * vui = NULL;
1590 dpdk_main_t * dm = &dpdk_main;
1591 vlib_thread_main_t * tm = vlib_get_thread_main();
1596 if (tm->n_vlib_mains > 1 && dm->have_io_threads)
1598 clib_warning("vhost-user interfaces are not supported with multiple io threads");
1603 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1608 vui = vhost_user_vui_new ();
1609 ASSERT(vui != NULL);
1611 vhost_user_create_ethernet (vnm, vm, vui);
1612 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1613 feature_mask, &sw_if_idx);
1616 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1619 vhost_user_vui_register (vm, vui);
1622 *sw_if_index = sw_if_idx;
1627 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1628 const char * sock_filename,
1632 u8 renumber, u32 custom_dev_instance)
1634 vhost_user_main_t * vum = &vhost_user_main;
1635 vhost_user_intf_t * vui = NULL;
1641 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1644 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1646 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1649 // interface is inactive
1651 // disconnect interface sockets
1652 vhost_user_if_disconnect(vui);
1655 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1660 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1661 feature_mask, &sw_if_idx);
1664 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1667 vhost_user_vui_register (vm, vui);
1673 vhost_user_connect_command_fn (vlib_main_t * vm,
1674 unformat_input_t * input,
1675 vlib_cli_command_t * cmd)
1677 unformat_input_t _line_input, * line_input = &_line_input;
1678 u8 * sock_filename = NULL;
1681 u64 feature_mask = (u64)~0;
1683 u32 custom_dev_instance = ~0;
1685 /* Get a line of input. */
1686 if (! unformat_user (input, unformat_line_input, line_input))
1689 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1690 if (unformat (line_input, "socket %s", &sock_filename))
1692 else if (unformat (line_input, "server"))
1694 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1696 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1700 return clib_error_return (0, "unknown input `%U'",
1701 format_unformat_error, input);
1703 unformat_free (line_input);
1705 vnet_main_t *vnm = vnet_get_main();
1707 vhost_user_create_if(vnm, vm, (char *)sock_filename,
1708 is_server, &sw_if_index, feature_mask,
1709 renumber, custom_dev_instance);
1711 vec_free(sock_filename);
1717 vhost_user_delete_command_fn (vlib_main_t * vm,
1718 unformat_input_t * input,
1719 vlib_cli_command_t * cmd)
1721 unformat_input_t _line_input, * line_input = &_line_input;
1722 u32 sw_if_index = ~0;
1724 /* Get a line of input. */
1725 if (! unformat_user (input, unformat_line_input, line_input))
1728 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1729 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1732 return clib_error_return (0, "unknown input `%U'",
1733 format_unformat_error, input);
1735 unformat_free (line_input);
1737 vnet_main_t *vnm = vnet_get_main();
1739 vhost_user_delete_if(vnm, vm, sw_if_index);
1744 int vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1747 vhost_user_main_t * vum = &vhost_user_main;
1748 vhost_user_intf_t * vui;
1749 vhost_user_intf_details_t * r_vuids = NULL;
1750 vhost_user_intf_details_t * vuid = NULL;
1751 u32 * hw_if_indices = 0;
1752 vnet_hw_interface_t * hi;
1759 vec_foreach (vui, vum->vhost_user_interfaces) {
1761 vec_add1(hw_if_indices, vui->hw_if_index);
1764 for (i = 0; i < vec_len (hw_if_indices); i++) {
1765 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1766 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1768 vec_add2(r_vuids, vuid, 1);
1769 vuid->sw_if_index = vui->sw_if_index;
1770 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1771 vuid->features = vui->features;
1772 vuid->is_server = vui->sock_is_server;
1773 vuid->num_regions = vui->nregions;
1774 vuid->sock_errno = vui->sock_errno;
1775 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1776 ARRAY_LEN(vuid->sock_filename)-1);
1778 s = format (s, "%v%c", hi->name, 0);
1780 strncpy((char *)vuid->if_name, (char *)s,
1781 ARRAY_LEN(vuid->if_name)-1);
1786 vec_free (hw_if_indices);
1788 *out_vuids = r_vuids;
1794 show_vhost_user_command_fn (vlib_main_t * vm,
1795 unformat_input_t * input,
1796 vlib_cli_command_t * cmd)
1798 clib_error_t * error = 0;
1799 vnet_main_t * vnm = vnet_get_main();
1800 vhost_user_main_t * vum = &vhost_user_main;
1801 vhost_user_intf_t * vui;
1802 u32 hw_if_index, * hw_if_indices = 0;
1803 vnet_hw_interface_t * hi;
1806 struct feat_struct { u8 bit; char *str;};
1807 struct feat_struct *feat_entry;
1809 static struct feat_struct feat_array[] = {
1810 #define _(s,b) { .str = #s, .bit = b, },
1811 foreach_virtio_net_feature
1816 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1817 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1818 vec_add1 (hw_if_indices, hw_if_index);
1819 vlib_cli_output(vm, "add %d", hw_if_index);
1821 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1824 error = clib_error_return (0, "unknown input `%U'",
1825 format_unformat_error, input);
1829 if (vec_len (hw_if_indices) == 0) {
1830 vec_foreach (vui, vum->vhost_user_interfaces) {
1832 vec_add1(hw_if_indices, vui->hw_if_index);
1835 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1836 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1837 vum->coalesce_frames, vum->coalesce_time);
1839 for (i = 0; i < vec_len (hw_if_indices); i++) {
1840 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1841 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1842 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1843 hi->name, hw_if_indices[i]);
1845 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1846 vui->virtio_net_hdr_sz, vui->features);
1848 feat_entry = (struct feat_struct *) &feat_array;
1849 while(feat_entry->str) {
1850 if (vui->features & (1 << feat_entry->bit))
1851 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1855 vlib_cli_output (vm, "\n");
1858 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1859 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1860 strerror(vui->sock_errno));
1862 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1865 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1866 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1868 for (j = 0; j < vui->nregions; j++) {
1869 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1870 vui->region_mmap_fd[j],
1871 vui->regions[j].guest_phys_addr,
1872 vui->regions[j].memory_size,
1873 vui->regions[j].userspace_addr,
1874 vui->regions[j].mmap_offset,
1875 (u64) vui->region_mmap_addr[j]);
1877 for (q = 0; q < vui->num_vrings; q++) {
1878 vlib_cli_output(vm, "\n Virtqueue %d\n", q);
1880 vlib_cli_output(vm, " qsz %d last_avail_idx %d last_used_idx %d\n",
1882 vui->vrings[q].last_avail_idx,
1883 vui->vrings[q].last_used_idx);
1885 if (vui->vrings[q].avail && vui->vrings[q].used)
1886 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1887 vui->vrings[q].avail->flags,
1888 vui->vrings[q].avail->idx,
1889 vui->vrings[q].used->flags,
1890 vui->vrings[q].used->idx);
1892 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d\n",
1893 vui->vrings[q].kickfd,
1894 vui->vrings[q].callfd,
1895 vui->vrings[q].errfd);
1898 vlib_cli_output(vm, "\n descriptor table:\n");
1899 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1900 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1901 for(j = 0; j < vui->vrings[q].qsz; j++) {
1902 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1904 vui->vrings[q].desc[j].addr,
1905 vui->vrings[q].desc[j].len,
1906 vui->vrings[q].desc[j].flags,
1907 vui->vrings[q].desc[j].next,
1908 (u64) map_guest_mem(vui, vui->vrings[q].desc[j].addr));}
1911 vlib_cli_output (vm, "\n");
1914 vec_free (hw_if_indices);
1918 static clib_error_t *
1919 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
1921 vhost_user_main_t * vum = &vhost_user_main;
1923 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1925 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
1927 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
1929 else if (unformat (input, "dont-dump-memory"))
1930 vum->dont_dump_vhost_user_memory = 1;
1932 return clib_error_return (0, "unknown input `%U'",
1933 format_unformat_error, input);
1939 /* vhost-user { ... } configuration. */
1940 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
1943 vhost_user_unmap_all (void)
1945 vhost_user_main_t * vum = &vhost_user_main;
1946 vhost_user_intf_t * vui;
1948 if (vum->dont_dump_vhost_user_memory)
1950 vec_foreach (vui, vum->vhost_user_interfaces)
1952 unmap_all_mem_regions(vui);