FD.io VPP  v21.01.1
Vector Packet Processing
vhost_user.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * vhost.c - vhost-user
4  *
5  * Copyright (c) 2014-2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19 
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
28 #include <sys/vfs.h>
29 
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
32 
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
39 
42 
43 /**
44  * @file
45  * @brief vHost User Device Driver.
46  *
47  * This file contains the source code for vHost User interface.
48  */
49 
50 
52 
53 /* *INDENT-OFF* */
54 vhost_user_main_t vhost_user_main = {
55  .mtu_bytes = 1518,
56 };
57 
58 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
59  .name = "vhost-user",
60 };
61 /* *INDENT-ON* */
62 
63 static long
65 {
66  struct statfs s;
67  fstatfs (fd, &s);
68  return s.f_bsize;
69 }
70 
71 static void
73 {
74  int i, r, q;
76 
77  for (i = 0; i < vui->nregions; i++)
78  {
79  if (vui->region_mmap_addr[i] != MAP_FAILED)
80  {
81 
82  long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
83 
84  ssize_t map_sz = (vui->regions[i].memory_size +
85  vui->regions[i].mmap_offset +
86  page_sz - 1) & ~(page_sz - 1);
87 
88  r =
89  munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
90  map_sz);
91 
92  vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
93  "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
94  page_sz);
95 
96  vui->region_mmap_addr[i] = MAP_FAILED;
97 
98  if (r == -1)
99  {
100  vu_log_err (vui, "failed to unmap memory region (errno %d)",
101  errno);
102  }
103  close (vui->region_mmap_fd[i]);
104  }
105  }
106  vui->nregions = 0;
107 
108  for (q = 0; q < VHOST_VRING_MAX_N; q++)
109  {
110  vq = &vui->vrings[q];
111  vq->avail = 0;
112  vq->used = 0;
113  vq->desc = 0;
114  }
115 }
116 
119 {
120  //Let's try to assign one queue to each thread
121  u32 qid;
122  u32 thread_index = 0;
123 
124  vui->use_tx_spinlock = 0;
125  while (1)
126  {
127  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
128  {
129  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
130  if (!rxvq->started || !rxvq->enabled)
131  continue;
132 
133  vui->per_cpu_tx_qid[thread_index] = qid;
134  thread_index++;
135  if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
136  return;
137  }
138  //We need to loop, meaning the spinlock has to be used
139  vui->use_tx_spinlock = 1;
140  if (thread_index == 0)
141  {
142  //Could not find a single valid one
143  for (thread_index = 0;
144  thread_index < vlib_get_thread_main ()->n_vlib_mains;
145  thread_index++)
146  {
147  vui->per_cpu_tx_qid[thread_index] = 0;
148  }
149  return;
150  }
151  }
152 }
153 
154 /**
155  * @brief Unassign existing interface/queue to thread mappings and re-assign
156  * new interface/queue to thread mappings
157  */
160 {
161  vhost_user_vring_t *txvq = &vui->vrings[qid];
162  vnet_main_t *vnm = vnet_get_main ();
163  int rv;
164  u32 q = qid >> 1;
165 
166  ASSERT ((qid & 1) == 1); // should be odd
167  // Assign new queue mappings for the interface
169  vhost_user_input_node.index);
171  if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
172  /* Set polling as the default */
174  txvq->qid = q;
175  rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
176  if (rv)
177  vu_log_warn (vui, "unable to set rx mode for interface %d, "
178  "queue %d: rc=%d", vui->hw_if_index, q, rv);
179 }
180 
181 /** @brief Returns whether at least one TX and one RX vring are enabled */
184 {
185  int i, found[2] = { }; //RX + TX
186 
187  for (i = 0; i < VHOST_VRING_MAX_N; i++)
188  if (vui->vrings[i].started && vui->vrings[i].enabled)
189  found[i & 1] = 1;
190 
191  return found[0] && found[1];
192 }
193 
196 {
197  /* if we have pointers to descriptor table, go up */
198  int is_ready = vhost_user_intf_ready (vui);
199  if (is_ready != vui->is_ready)
200  {
201  vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
202  is_ready ? "ready" : "down");
203  if (vui->admin_up)
206  : 0);
207  vui->is_ready = is_ready;
208  }
209 }
210 
211 static void
213 {
214  u32 qid;
215  vnet_main_t *vnm = vnet_get_main ();
216 
217  qid = ifq & 0xff;
218  if ((qid & 1) == 0)
219  /* Only care about the odd number, or TX, virtqueue */
220  return;
221 
222  if (vhost_user_intf_ready (vui))
223  // qid >> 1 is to convert virtqueue number to vring queue index
225 }
226 
227 static clib_error_t *
229 {
230  __attribute__ ((unused)) int n;
231  u8 buff[8];
232 
233  n = read (uf->file_descriptor, ((char *) &buff), 8);
234 
235  return 0;
236 }
237 
240 {
241  if (qid & 1) // RX is odd, TX is even
242  {
243  if (vui->vrings[qid].qid == -1)
245  }
246  else
248 }
249 
250 static clib_error_t *
252 {
253  __attribute__ ((unused)) int n;
254  u8 buff[8];
255  vhost_user_intf_t *vui =
256  pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
257  uf->private_data >> 8);
258  u32 qid = uf->private_data & 0xff;
259 
260  n = read (uf->file_descriptor, ((char *) &buff), 8);
261  vu_log_debug (vui, "if %d KICK queue %d", uf->private_data >> 8, qid);
262  if (!vui->vrings[qid].started ||
263  (vhost_user_intf_ready (vui) != vui->is_ready))
264  {
265  if (vui->vrings[qid].started == 0)
266  {
267  vui->vrings[qid].started = 1;
268  vhost_user_thread_placement (vui, qid);
270  }
271  }
272 
274  return 0;
275 }
276 
279 {
280  vhost_user_vring_t *vring = &vui->vrings[qid];
281  clib_memset (vring, 0, sizeof (*vring));
282  vring->kickfd_idx = ~0;
283  vring->callfd_idx = ~0;
284  vring->errfd = -1;
285  vring->qid = -1;
286 
287  /*
288  * We have a bug with some qemu 2.5, and this may be a fix.
289  * Feel like interpretation holy text, but this is from vhost-user.txt.
290  * "
291  * One queue pair is enabled initially. More queues are enabled
292  * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
293  * "
294  * Don't know who's right, but this is what DPDK does.
295  */
296  if (qid == 0 || qid == 1)
297  vring->enabled = 1;
298 }
299 
302 {
303  vhost_user_vring_t *vring = &vui->vrings[qid];
304 
305  if (vring->kickfd_idx != ~0)
306  {
308  vring->kickfd_idx);
309  clib_file_del (&file_main, uf);
310  vring->kickfd_idx = ~0;
311  }
312  if (vring->callfd_idx != ~0)
313  {
315  vring->callfd_idx);
316  clib_file_del (&file_main, uf);
317  vring->callfd_idx = ~0;
318  }
319  if (vring->errfd != -1)
320  {
321  close (vring->errfd);
322  vring->errfd = -1;
323  }
324 
325  // save the qid so that we don't need to unassign and assign_rx_thread
326  // when the interface comes back up. They are expensive calls.
327  u16 q = vui->vrings[qid].qid;
328  vhost_user_vring_init (vui, qid);
329  vui->vrings[qid].qid = q;
330 }
331 
334 {
335  vnet_main_t *vnm = vnet_get_main ();
336  int q;
337 
339 
340  if (vui->clib_file_index != ~0)
341  {
343  vui->clib_file_index = ~0;
344  }
345 
346  vui->is_ready = 0;
347 
348  for (q = 0; q < VHOST_VRING_MAX_N; q++)
349  vhost_user_vring_close (vui, q);
350 
351  unmap_all_mem_regions (vui);
352  vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
353 }
354 
355 static clib_error_t *
357 {
358  int n, i, j;
359  int fd, number_of_fds = 0;
360  int fds[VHOST_MEMORY_MAX_NREGIONS];
361  vhost_user_msg_t msg;
362  struct msghdr mh;
363  struct iovec iov[1];
365  vhost_user_intf_t *vui;
366  struct cmsghdr *cmsg;
367  u8 q;
368  clib_file_t template = { 0 };
369  vnet_main_t *vnm = vnet_get_main ();
371 
372  vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
373 
374  char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
375 
376  clib_memset (&mh, 0, sizeof (mh));
377  clib_memset (control, 0, sizeof (control));
378 
379  for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
380  fds[i] = -1;
381 
382  /* set the payload */
383  iov[0].iov_base = (void *) &msg;
384  iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
385 
386  mh.msg_iov = iov;
387  mh.msg_iovlen = 1;
388  mh.msg_control = control;
389  mh.msg_controllen = sizeof (control);
390 
391  n = recvmsg (uf->file_descriptor, &mh, 0);
392 
393  if (n != VHOST_USER_MSG_HDR_SZ)
394  {
395  if (n == -1)
396  {
397  vu_log_debug (vui, "recvmsg returned error %d %s", errno,
398  strerror (errno));
399  }
400  else
401  {
402  vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
404  }
405  goto close_socket;
406  }
407 
408  if (mh.msg_flags & MSG_CTRUNC)
409  {
410  vu_log_debug (vui, "MSG_CTRUNC is set");
411  goto close_socket;
412  }
413 
414  cmsg = CMSG_FIRSTHDR (&mh);
415 
416  if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
417  (cmsg->cmsg_type == SCM_RIGHTS) &&
418  (cmsg->cmsg_len - CMSG_LEN (0) <=
419  VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
420  {
421  number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
422  clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
423  }
424 
425  /* version 1, no reply bit set */
426  if ((msg.flags & 7) != 1)
427  {
428  vu_log_debug (vui, "malformed message received. closing socket");
429  goto close_socket;
430  }
431 
432  {
433  int rv;
434  rv =
435  read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
436  msg.size);
437  if (rv < 0)
438  {
439  vu_log_debug (vui, "read failed %s", strerror (errno));
440  goto close_socket;
441  }
442  else if (rv != msg.size)
443  {
444  vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
445  msg.size);
446  goto close_socket;
447  }
448  }
449 
450  switch (msg.request)
451  {
453  msg.flags |= 4;
454  msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
455  VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
456  VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
457  VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
458  VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
459  VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
460  VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
461  VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
462  VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
463  msg.u64 &= vui->feature_mask;
464 
465  if (vui->enable_gso)
467  if (vui->enable_packed)
468  msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
469 
470  msg.size = sizeof (msg.u64);
471  vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
472  "0x%016llx", vui->hw_if_index, msg.u64);
473  n =
474  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
475  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
476  {
477  vu_log_debug (vui, "could not send message response");
478  goto close_socket;
479  }
480  break;
481 
483  vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
484  "0x%016llx", vui->hw_if_index, msg.u64);
485 
486  vui->features = msg.u64;
487 
488  if (vui->features &
489  (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
490  VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
491  vui->virtio_net_hdr_sz = 12;
492  else
493  vui->virtio_net_hdr_sz = 10;
494 
495  vui->is_any_layout =
496  (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
497 
500  if (vui->enable_gso &&
503  hw->flags |=
506  else
510  vui->is_ready = 0;
512  break;
513 
515  vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
516  vui->hw_if_index, msg.memory.nregions);
517 
518  if ((msg.memory.nregions < 1) ||
519  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
520  {
521  vu_log_debug (vui, "number of mem regions must be between 1 and %i",
522  VHOST_MEMORY_MAX_NREGIONS);
523  goto close_socket;
524  }
525 
526  if (msg.memory.nregions != number_of_fds)
527  {
528  vu_log_debug (vui, "each memory region must have FD");
529  goto close_socket;
530  }
531 
532  /* Do the mmap without barrier sync */
533  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
534  for (i = 0; i < msg.memory.nregions; i++)
535  {
536  long page_sz = get_huge_page_size (fds[i]);
537 
538  /* align size to page */
539  ssize_t map_sz = (msg.memory.regions[i].memory_size +
540  msg.memory.regions[i].mmap_offset +
541  page_sz - 1) & ~(page_sz - 1);
542 
543  region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
544  MAP_SHARED, fds[i], 0);
545  if (region_mmap_addr[i] == MAP_FAILED)
546  {
547  vu_log_err (vui, "failed to map memory. errno is %d", errno);
548  for (j = 0; j < i; j++)
549  munmap (region_mmap_addr[j], map_sz);
550  goto close_socket;
551  }
552  vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
553  "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
554  region_mmap_addr[i], page_sz);
555  }
556 
558  unmap_all_mem_regions (vui);
559  for (i = 0; i < msg.memory.nregions; i++)
560  {
561  clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
562  sizeof (vhost_user_memory_region_t));
563 
564  vui->region_mmap_addr[i] = region_mmap_addr[i];
565  vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
566  vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
567  vui->regions[i].memory_size;
568 
569  vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
570  vui->region_mmap_fd[i] = fds[i];
571 
572  vui->nregions++;
573  }
574 
575  /*
576  * Re-compute desc, used, and avail descriptor table if vring address
577  * is set.
578  */
579  for (q = 0; q < VHOST_VRING_MAX_N; q++)
580  {
581  if (vui->vrings[q].desc_user_addr &&
582  vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
583  {
584  vui->vrings[q].desc =
585  map_user_mem (vui, vui->vrings[q].desc_user_addr);
586  vui->vrings[q].used =
587  map_user_mem (vui, vui->vrings[q].used_user_addr);
588  vui->vrings[q].avail =
589  map_user_mem (vui, vui->vrings[q].avail_user_addr);
590  }
591  }
593  break;
594 
596  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
597  vui->hw_if_index, msg.state.index, msg.state.num);
598 
599  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
600  (msg.state.num == 0) || /* it cannot be zero */
601  ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
602  (msg.state.index >= VHOST_VRING_MAX_N))
603  goto close_socket;
604  vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
605  break;
606 
608  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
609  vui->hw_if_index, msg.state.index);
610 
611  if (msg.state.index >= VHOST_VRING_MAX_N)
612  {
613  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
614  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
615  goto close_socket;
616  }
617 
618  if (msg.size < sizeof (msg.addr))
619  {
620  vu_log_debug (vui, "vhost message is too short (%d < %d)",
621  msg.size, sizeof (msg.addr));
622  goto close_socket;
623  }
624 
625  vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
626  vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
627  vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
628 
629  if ((desc == NULL) || (used == NULL) || (avail == NULL))
630  {
631  vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
632  vui->hw_if_index);
633  goto close_socket;
634  }
635 
636  vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
637  vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
638  vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
639 
641  vui->vrings[msg.state.index].desc = desc;
642  vui->vrings[msg.state.index].used = used;
643  vui->vrings[msg.state.index].avail = avail;
644 
645  vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
646  vui->vrings[msg.state.index].log_used =
647  (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
648 
649  /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
650  the ring is initialized in an enabled state. */
651  if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
652  vui->vrings[msg.state.index].enabled = 1;
653 
654  vui->vrings[msg.state.index].last_used_idx =
655  vui->vrings[msg.state.index].last_avail_idx =
656  vui->vrings[msg.state.index].used->idx;
657 
658  /* tell driver that we don't want interrupts */
660  vui->vrings[msg.state.index].used_event->flags =
662  else
663  vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
666  break;
667 
669  vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
670  break;
671 
673  vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
674  vui->hw_if_index);
675  break;
676 
678  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
679  vui->hw_if_index, msg.u64);
680 
681  q = (u8) (msg.u64 & 0xFF);
682  if (q >= VHOST_VRING_MAX_N)
683  goto close_socket;
684 
685  /* if there is old fd, delete and close it */
686  if (vui->vrings[q].callfd_idx != ~0)
687  {
689  vui->vrings[q].callfd_idx);
690  clib_file_del (&file_main, uf);
691  vui->vrings[q].callfd_idx = ~0;
692  }
693 
694  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
695  {
696  if (number_of_fds != 1)
697  {
698  vu_log_debug (vui, "More than one fd received !");
699  goto close_socket;
700  }
701 
702  template.read_function = vhost_user_callfd_read_ready;
703  template.file_descriptor = fds[0];
704  template.private_data =
705  ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
706  vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
707  }
708  else
709  vui->vrings[q].callfd_idx = ~0;
710  break;
711 
713  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
714  vui->hw_if_index, msg.u64);
715 
716  q = (u8) (msg.u64 & 0xFF);
717  if (q >= VHOST_VRING_MAX_N)
718  goto close_socket;
719 
720  if (vui->vrings[q].kickfd_idx != ~0)
721  {
723  vui->vrings[q].kickfd_idx);
724  clib_file_del (&file_main, uf);
725  vui->vrings[q].kickfd_idx = ~0;
726  }
727 
728  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
729  {
730  if (number_of_fds != 1)
731  {
732  vu_log_debug (vui, "More than one fd received !");
733  goto close_socket;
734  }
735 
736  template.read_function = vhost_user_kickfd_read_ready;
737  template.file_descriptor = fds[0];
738  template.private_data =
739  (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
740  q;
741  vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
742  }
743  else
744  {
745  //When no kickfd is set, the queue is initialized as started
746  vui->vrings[q].kickfd_idx = ~0;
747  vui->vrings[q].started = 1;
749  }
751  break;
752 
754  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
755  vui->hw_if_index, msg.u64);
756 
757  q = (u8) (msg.u64 & 0xFF);
758  if (q >= VHOST_VRING_MAX_N)
759  goto close_socket;
760 
761  if (vui->vrings[q].errfd != -1)
762  close (vui->vrings[q].errfd);
763 
764  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
765  {
766  if (number_of_fds != 1)
767  goto close_socket;
768 
769  vui->vrings[q].errfd = fds[0];
770  }
771  else
772  vui->vrings[q].errfd = -1;
773  break;
774 
776  vu_log_debug (vui,
777  "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
778  vui->hw_if_index, msg.state.index, msg.state.num);
779  if (msg.state.index >= VHOST_VRING_MAX_N)
780  goto close_socket;
782  vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
784  {
785  /*
786  * 0 1 2 3
787  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
788  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
789  * | last avail idx | | last used idx | |
790  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
791  * ^ ^
792  * | |
793  * avail wrap counter used wrap counter
794  */
795  /* last avail idx at bit 0-14. */
796  vui->vrings[msg.state.index].last_avail_idx =
797  msg.state.num & 0x7fff;
798  /* avail wrap counter at bit 15 */
799  vui->vrings[msg.state.index].avail_wrap_counter =
800  ! !(msg.state.num & (1 << 15));
801 
802  /*
803  * Although last_used_idx is passed in the upper 16 bits in qemu
804  * implementation, in practice, last_avail_idx and last_used_idx are
805  * usually the same. As a result, DPDK does not bother to pass us
806  * last_used_idx. The spec is not clear on thex coding. I figured it
807  * out by reading the qemu code. So let's just read last_avail_idx
808  * and set last_used_idx equals to last_avail_idx.
809  */
810  vui->vrings[msg.state.index].last_used_idx =
811  vui->vrings[msg.state.index].last_avail_idx;
812  vui->vrings[msg.state.index].used_wrap_counter =
813  vui->vrings[msg.state.index].avail_wrap_counter;
814 
815  if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
816  vui->vrings[msg.state.index].avail_wrap_counter =
818  }
820  break;
821 
823  if (msg.state.index >= VHOST_VRING_MAX_N)
824  {
825  vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
826  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
827  goto close_socket;
828  }
829 
830  /* protection is needed to prevent rx/tx from changing last_avail_idx */
832  /*
833  * Copy last_avail_idx from the vring before closing it because
834  * closing the vring also initializes the vring last_avail_idx
835  */
836  msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
838  {
839  msg.state.num =
840  (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
841  (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
842  msg.state.num |=
843  ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
844  (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
845  }
846  msg.flags |= 4;
847  msg.size = sizeof (msg.state);
848 
849  /*
850  * Spec says: Client must [...] stop ring upon receiving
851  * VHOST_USER_GET_VRING_BASE
852  */
853  vhost_user_vring_close (vui, msg.state.index);
855  vu_log_debug (vui,
856  "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
857  vui->hw_if_index, msg.state.index, msg.state.num);
858  n =
859  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
860  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
861  {
862  vu_log_debug (vui, "could not send message response");
863  goto close_socket;
864  }
866  break;
867 
868  case VHOST_USER_NONE:
869  vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
870  break;
871 
873  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
874  vui->hw_if_index);
875 
876  if (msg.size != sizeof (msg.log))
877  {
878  vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
879  " %d instead of %d", msg.size, sizeof (msg.log));
880  goto close_socket;
881  }
882 
884  {
885  vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
886  "VHOST_USER_SET_LOG_BASE received");
887  goto close_socket;
888  }
889 
890  fd = fds[0];
891  /* align size to page */
892  long page_sz = get_huge_page_size (fd);
893  ssize_t map_sz =
894  (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
895 
896  void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
897  MAP_SHARED, fd, 0);
898 
899  vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
900  "mapped 0x%lx", map_sz, msg.log.offset, fd,
901  log_base_addr);
902 
903  if (log_base_addr == MAP_FAILED)
904  {
905  vu_log_err (vui, "failed to map memory. errno is %d", errno);
906  goto close_socket;
907  }
908 
910  vui->log_base_addr = log_base_addr;
911  vui->log_base_addr += msg.log.offset;
912  vui->log_size = msg.log.size;
914 
915  msg.flags |= 4;
916  msg.size = sizeof (msg.u64);
917  n =
918  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
919  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
920  {
921  vu_log_debug (vui, "could not send message response");
922  goto close_socket;
923  }
924  break;
925 
927  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
928  break;
929 
931  msg.flags |= 4;
932  msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
934  msg.size = sizeof (msg.u64);
935  vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
936  "reply 0x%016llx", vui->hw_if_index, msg.u64);
937  n =
938  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
939  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
940  {
941  vu_log_debug (vui, "could not send message response");
942  goto close_socket;
943  }
944  break;
945 
947  vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
948  "features 0x%016llx", vui->hw_if_index, msg.u64);
949  vui->protocol_features = msg.u64;
950  break;
951 
953  msg.flags |= 4;
954  msg.u64 = VHOST_VRING_MAX_N;
955  msg.size = sizeof (msg.u64);
956  vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
957  vui->hw_if_index, msg.u64);
958  n =
959  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
960  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
961  {
962  vu_log_debug (vui, "could not send message response");
963  goto close_socket;
964  }
965  break;
966 
968  vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
969  vui->hw_if_index, msg.state.num ? "enable" : "disable",
970  msg.state.index);
971  if (msg.state.index >= VHOST_VRING_MAX_N)
972  {
973  vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
974  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
975  goto close_socket;
976  }
977 
978  vui->vrings[msg.state.index].enabled = msg.state.num;
979  vhost_user_thread_placement (vui, msg.state.index);
981  break;
982 
983  default:
984  vu_log_debug (vui, "unknown vhost-user message %d received. "
985  "closing socket", msg.request);
986  goto close_socket;
987  }
988 
989  return 0;
990 
991 close_socket:
996  return 0;
997 }
998 
999 static clib_error_t *
1001 {
1004  vhost_user_intf_t *vui =
1006 
1007  vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1011  return 0;
1012 }
1013 
1014 static clib_error_t *
1016 {
1017  int client_fd, client_len;
1018  struct sockaddr_un client;
1019  clib_file_t template = { 0 };
1021  vhost_user_intf_t *vui;
1022 
1024 
1025  client_len = sizeof (client);
1026  client_fd = accept (uf->file_descriptor,
1027  (struct sockaddr *) &client,
1028  (socklen_t *) & client_len);
1029 
1030  if (client_fd < 0)
1031  return clib_error_return_unix (0, "accept");
1032 
1033  if (vui->clib_file_index != ~0)
1034  {
1035  vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1036  vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1038  }
1039 
1040  vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1041  vui->sw_if_index, client_fd);
1042  template.read_function = vhost_user_socket_read;
1043  template.error_function = vhost_user_socket_error;
1044  template.file_descriptor = client_fd;
1045  template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1046  vui->clib_file_index = clib_file_add (&file_main, &template);
1047  return 0;
1048 }
1049 
1050 static clib_error_t *
1052 {
1055 
1056  vum->log_default = vlib_log_register_class ("vhost-user", 0);
1057 
1058  vum->coalesce_frames = 32;
1059  vum->coalesce_time = 1e-3;
1060 
1061  vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1062 
1063  vhost_cpu_t *cpu;
1064  vec_foreach (cpu, vum->cpus)
1065  {
1066  /* This is actually not necessary as validate already zeroes it
1067  * Just keeping the loop here for later because I am lazy. */
1068  cpu->rx_buffers_len = 0;
1069  }
1070 
1071  vum->random = random_default_seed ();
1072 
1074 
1075  return 0;
1076 }
1077 
1078 /* *INDENT-OFF* */
1080 {
1081  .runs_after = VLIB_INITS("ip4_init"),
1082 };
1083 /* *INDENT-ON* */
1084 
1085 static uword
1088 {
1089  vhost_user_intf_t *vui;
1090  f64 timeout = 3153600000.0 /* 100 years */ ;
1091  uword event_type, *event_data = 0;
1093  u16 qid;
1094  f64 now, poll_time_remaining;
1095  f64 next_timeout;
1096  u8 stop_timer = 0;
1097 
1098  while (1)
1099  {
1100  poll_time_remaining =
1102  event_type = vlib_process_get_events (vm, &event_data);
1103  vec_reset_length (event_data);
1104 
1105  /*
1106  * Use the remaining timeout if it is less than coalesce time to avoid
1107  * resetting the existing timer in the middle of expiration
1108  */
1109  timeout = poll_time_remaining;
1110  if (vlib_process_suspend_time_is_zero (timeout) ||
1111  (timeout > vum->coalesce_time))
1112  timeout = vum->coalesce_time;
1113 
1114  now = vlib_time_now (vm);
1115  switch (event_type)
1116  {
1118  stop_timer = 1;
1119  break;
1120 
1122  stop_timer = 0;
1123  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1124  break;
1125  /* fall through */
1126 
1127  case ~0:
1128  /* *INDENT-OFF* */
1129  pool_foreach (vui, vum->vhost_user_interfaces) {
1130  next_timeout = timeout;
1131  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1132  {
1133  vhost_user_vring_t *rxvq = &vui->vrings[qid];
1134  vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1135 
1136  if (txvq->qid == -1)
1137  continue;
1138  if (txvq->n_since_last_int)
1139  {
1140  if (now >= txvq->int_deadline)
1141  vhost_user_send_call (vm, txvq);
1142  else
1143  next_timeout = txvq->int_deadline - now;
1144  }
1145 
1146  if (rxvq->n_since_last_int)
1147  {
1148  if (now >= rxvq->int_deadline)
1149  vhost_user_send_call (vm, rxvq);
1150  else
1151  next_timeout = rxvq->int_deadline - now;
1152  }
1153 
1154  if ((next_timeout < timeout) && (next_timeout > 0.0))
1155  timeout = next_timeout;
1156  }
1157  }
1158  /* *INDENT-ON* */
1159  break;
1160 
1161  default:
1162  clib_warning ("BUG: unhandled event type %d", event_type);
1163  break;
1164  }
1165  /* No less than 1 millisecond */
1166  if (timeout < 1e-3)
1167  timeout = 1e-3;
1168  if (stop_timer)
1169  timeout = 3153600000.0;
1170  }
1171  return 0;
1172 }
1173 
1174 /* *INDENT-OFF* */
1177  .type = VLIB_NODE_TYPE_PROCESS,
1178  .name = "vhost-user-send-interrupt-process",
1179 };
1180 /* *INDENT-ON* */
1181 
1182 static uword
1185 {
1187  vhost_user_intf_t *vui;
1188  struct sockaddr_un sun;
1189  int sockfd;
1190  clib_file_t template = { 0 };
1191  f64 timeout = 3153600000.0 /* 100 years */ ;
1192  uword *event_data = 0;
1193 
1194  sockfd = -1;
1195  sun.sun_family = AF_UNIX;
1197  template.error_function = vhost_user_socket_error;
1198 
1199  while (1)
1200  {
1202  vlib_process_get_events (vm, &event_data);
1203  vec_reset_length (event_data);
1204 
1205  timeout = 3.0;
1206 
1207  /* *INDENT-OFF* */
1208  pool_foreach (vui, vum->vhost_user_interfaces) {
1209 
1210  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1211  if (vui->clib_file_index == ~0)
1212  {
1213  if ((sockfd < 0) &&
1214  ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1215  {
1216  /*
1217  * 1st time error or new error for this interface,
1218  * spit out the message and record the error
1219  */
1220  if (!vui->sock_errno || (vui->sock_errno != errno))
1221  {
1223  ("Error: Could not open unix socket for %s",
1224  vui->sock_filename);
1225  vui->sock_errno = errno;
1226  }
1227  continue;
1228  }
1229 
1230  /* try to connect */
1231  strncpy (sun.sun_path, (char *) vui->sock_filename,
1232  sizeof (sun.sun_path) - 1);
1233  sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1234 
1235  /* Avoid hanging VPP if the other end does not accept */
1236  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1237  clib_unix_warning ("fcntl");
1238 
1239  if (connect (sockfd, (struct sockaddr *) &sun,
1240  sizeof (struct sockaddr_un)) == 0)
1241  {
1242  /* Set the socket to blocking as it was before */
1243  if (fcntl(sockfd, F_SETFL, 0) < 0)
1244  clib_unix_warning ("fcntl2");
1245 
1246  vui->sock_errno = 0;
1247  template.file_descriptor = sockfd;
1248  template.private_data =
1249  vui - vhost_user_main.vhost_user_interfaces;
1250  vui->clib_file_index = clib_file_add (&file_main, &template);
1251 
1252  /* This sockfd is considered consumed */
1253  sockfd = -1;
1254  }
1255  else
1256  {
1257  vui->sock_errno = errno;
1258  }
1259  }
1260  else
1261  {
1262  /* check if socket is alive */
1263  int error = 0;
1264  socklen_t len = sizeof (error);
1265  int fd = UNIX_GET_FD(vui->clib_file_index);
1266  int retval =
1267  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1268 
1269  if (retval)
1270  {
1271  vu_log_debug (vui, "getsockopt returned %d", retval);
1273  }
1274  }
1275  }
1276  }
1277  /* *INDENT-ON* */
1278  }
1279  return 0;
1280 }
1281 
1282 /* *INDENT-OFF* */
1284  .function = vhost_user_process,
1285  .type = VLIB_NODE_TYPE_PROCESS,
1286  .name = "vhost-user-process",
1287 };
1288 /* *INDENT-ON* */
1289 
1290 /**
1291  * Disables and reset interface structure.
1292  * It can then be either init again, or removed from used interfaces.
1293  */
1294 static void
1296 {
1297  int q;
1299 
1300  // disconnect interface sockets
1302  vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1304 
1305  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1306  {
1307  // Remove existing queue mapping for the interface
1308  if (q & 1)
1309  {
1310  int rv;
1311  vnet_main_t *vnm = vnet_get_main ();
1312  vhost_user_vring_t *txvq = &vui->vrings[q];
1313 
1314  if (txvq->qid != -1)
1315  {
1317  vui->hw_if_index,
1318  q >> 1);
1319  if (rv)
1320  vu_log_warn (vui, "unable to unassign interface %d, "
1321  "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1322  }
1323  }
1324 
1325  clib_mem_free ((void *) vui->vring_locks[q]);
1326  }
1327 
1328  if (vui->unix_server_index != ~0)
1329  {
1330  //Close server socket
1332  vui->unix_server_index);
1333  clib_file_del (&file_main, uf);
1334  vui->unix_server_index = ~0;
1335  unlink (vui->sock_filename);
1336  }
1337 
1339  &vui->if_index);
1340 }
1341 
1342 int
1344 {
1346  vhost_user_intf_t *vui;
1347  int rv = 0;
1348  vnet_hw_interface_t *hwif;
1349  u16 qid;
1350 
1351  if (!
1352  (hwif =
1354  || hwif->dev_class_index != vhost_user_device_class.index)
1355  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1356 
1358 
1359  vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1360  hwif->name, hwif->dev_instance);
1361 
1362  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1363  {
1364  vhost_user_vring_t *txvq = &vui->vrings[qid];
1365 
1366  if (txvq->qid == -1)
1367  continue;
1368  if ((vum->ifq_count > 0) &&
1369  ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1370  (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1371  {
1372  vum->ifq_count--;
1373  // Stop the timer if there is no more interrupt interface/queue
1374  if ((vum->ifq_count == 0) &&
1375  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1376  {
1380  break;
1381  }
1382  }
1383  }
1384 
1385  // Disable and reset interface
1386  vhost_user_term_if (vui);
1387 
1388  // Reset renumbered iface
1389  if (hwif->dev_instance <
1392 
1393  // Delete ethernet interface
1395 
1396  // Back to pool
1397  pool_put (vum->vhost_user_interfaces, vui);
1398 
1399  return rv;
1400 }
1401 
1402 static clib_error_t *
1404 {
1405  vnet_main_t *vnm = vnet_get_main ();
1407  vhost_user_intf_t *vui;
1408 
1410  /* *INDENT-OFF* */
1411  pool_foreach (vui, vum->vhost_user_interfaces) {
1412  vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1413  }
1414  /* *INDENT-ON* */
1416  return 0;
1417 }
1418 
1420 
1421 /**
1422  * Open server unix socket on specified sock_filename.
1423  */
1424 static int
1425 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1426 {
1427  int rv = 0;
1428  struct sockaddr_un un = { };
1429  int fd;
1430  /* create listening socket */
1431  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1432  return VNET_API_ERROR_SYSCALL_ERROR_1;
1433 
1434  un.sun_family = AF_UNIX;
1435  strncpy ((char *) un.sun_path, (char *) sock_filename,
1436  sizeof (un.sun_path) - 1);
1437 
1438  /* remove if exists */
1439  unlink ((char *) sock_filename);
1440 
1441  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1442  {
1443  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1444  goto error;
1445  }
1446 
1447  if (listen (fd, 1) == -1)
1448  {
1449  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1450  goto error;
1451  }
1452 
1453  *sock_fd = fd;
1454  return 0;
1455 
1456 error:
1457  close (fd);
1458  return rv;
1459 }
1460 
1461 /**
1462  * Create ethernet interface for vhost user interface.
1463  */
1464 static void
1466  vhost_user_intf_t * vui, u8 * hwaddress)
1467 {
1469  u8 hwaddr[6];
1471 
1472  /* create hw and sw interface */
1473  if (hwaddress)
1474  {
1475  clib_memcpy (hwaddr, hwaddress, 6);
1476  }
1477  else
1478  {
1479  random_u32 (&vum->random);
1480  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1481  hwaddr[0] = 2;
1482  hwaddr[1] = 0xfe;
1483  }
1484 
1486  (vnm,
1488  vui - vum->vhost_user_interfaces /* device instance */ ,
1489  hwaddr /* ethernet address */ ,
1490  &vui->hw_if_index, 0 /* flag change */ );
1491 
1492  if (error)
1493  clib_error_report (error);
1494 }
1495 
1496 /*
1497  * Initialize vui with specified attributes
1498  */
1499 static void
1501  vhost_user_intf_t * vui,
1502  int server_sock_fd,
1503  const char *sock_filename,
1504  u64 feature_mask, u32 * sw_if_index, u8 enable_gso,
1505  u8 enable_packed)
1506 {
1507  vnet_sw_interface_t *sw;
1508  int q;
1510  vnet_hw_interface_t *hw;
1511 
1512  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1513  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1514  if (server_sock_fd != -1)
1515  {
1516  clib_file_t template = { 0 };
1518  template.file_descriptor = server_sock_fd;
1519  template.private_data = vui - vum->vhost_user_interfaces; //hw index
1520  vui->unix_server_index = clib_file_add (&file_main, &template);
1521  }
1522  else
1523  {
1524  vui->unix_server_index = ~0;
1525  }
1526 
1527  vui->sw_if_index = sw->sw_if_index;
1528  strncpy (vui->sock_filename, sock_filename,
1529  ARRAY_LEN (vui->sock_filename) - 1);
1530  vui->sock_errno = 0;
1531  vui->is_ready = 0;
1532  vui->feature_mask = feature_mask;
1533  vui->clib_file_index = ~0;
1534  vui->log_base_addr = 0;
1535  vui->if_index = vui - vum->vhost_user_interfaces;
1536  vui->enable_gso = enable_gso;
1537  vui->enable_packed = enable_packed;
1538  /*
1539  * enable_gso takes precedence over configurable feature mask if there
1540  * is a clash.
1541  * if feature mask disables gso, but enable_gso is configured,
1542  * then gso is enable
1543  * if feature mask enables gso, but enable_gso is not configured,
1544  * then gso is enable
1545  *
1546  * if gso is enable via feature mask, it must enable both host and guest
1547  * gso feature mask, we don't support one sided GSO or partial GSO.
1548  */
1549  if ((vui->enable_gso == 0) &&
1551  (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1552  vui->enable_gso = 1;
1553  vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1555  &vui->if_index, 0);
1556 
1557  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1558  vhost_user_vring_init (vui, q);
1559 
1562 
1563  if (sw_if_index)
1564  *sw_if_index = vui->sw_if_index;
1565 
1566  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1567  {
1570  clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1571  }
1572 
1574  vlib_get_thread_main ()->n_vlib_mains - 1);
1576 }
1577 
1578 int
1580  const char *sock_filename,
1581  u8 is_server,
1582  u32 * sw_if_index,
1583  u64 feature_mask,
1584  u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
1585  u8 enable_gso, u8 enable_packed)
1586 {
1587  vhost_user_intf_t *vui = NULL;
1588  u32 sw_if_idx = ~0;
1589  int rv = 0;
1590  int server_sock_fd = -1;
1592  uword *if_index;
1593 
1594  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1595  {
1596  return VNET_API_ERROR_INVALID_ARGUMENT;
1597  }
1598 
1599  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1600  if (if_index)
1601  {
1602  if (sw_if_index)
1603  {
1604  vui = &vum->vhost_user_interfaces[*if_index];
1605  *sw_if_index = vui->sw_if_index;
1606  }
1607  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1608  }
1609 
1610  if (is_server)
1611  {
1612  if ((rv =
1613  vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1614  {
1615  return rv;
1616  }
1617  }
1618 
1619  /* Protect the uninitialized vui from being dispatched by rx/tx */
1621  pool_get (vhost_user_main.vhost_user_interfaces, vui);
1622  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1624 
1625  vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1626  feature_mask, &sw_if_idx, enable_gso, enable_packed);
1627  vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1629 
1630  if (renumber)
1631  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1632 
1633  if (sw_if_index)
1634  *sw_if_index = sw_if_idx;
1635 
1636  // Process node must connect
1638 
1639  return rv;
1640 }
1641 
1642 int
1644  const char *sock_filename,
1645  u8 is_server,
1646  u32 sw_if_index,
1647  u64 feature_mask, u8 renumber, u32 custom_dev_instance,
1648  u8 enable_gso, u8 enable_packed)
1649 {
1651  vhost_user_intf_t *vui = NULL;
1652  u32 sw_if_idx = ~0;
1653  int server_sock_fd = -1;
1654  int rv = 0;
1655  vnet_hw_interface_t *hwif;
1656  uword *if_index;
1657 
1658  if (!
1659  (hwif =
1661  || hwif->dev_class_index != vhost_user_device_class.index)
1662  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1663 
1664  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1665  return VNET_API_ERROR_INVALID_ARGUMENT;
1666 
1668 
1669  /*
1670  * Disallow changing the interface to have the same path name
1671  * as other interface
1672  */
1673  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1674  if (if_index && (*if_index != vui->if_index))
1675  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1676 
1677  // First try to open server socket
1678  if (is_server)
1679  if ((rv = vhost_user_init_server_sock (sock_filename,
1680  &server_sock_fd)) != 0)
1681  return rv;
1682 
1683  vhost_user_term_if (vui);
1684  vhost_user_vui_init (vnm, vui, server_sock_fd,
1685  sock_filename, feature_mask, &sw_if_idx, enable_gso,
1686  enable_packed);
1687 
1688  if (renumber)
1689  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1690 
1691  // Process node must connect
1693 
1694  return rv;
1695 }
1696 
1697 clib_error_t *
1699  unformat_input_t * input,
1700  vlib_cli_command_t * cmd)
1701 {
1702  unformat_input_t _line_input, *line_input = &_line_input;
1703  u8 *sock_filename = NULL;
1704  u32 sw_if_index;
1705  u8 is_server = 0;
1706  u64 feature_mask = (u64) ~ (0ULL);
1707  u8 renumber = 0;
1708  u32 custom_dev_instance = ~0;
1709  u8 hwaddr[6];
1710  u8 *hw = NULL;
1711  clib_error_t *error = NULL;
1712  u8 enable_gso = 0, enable_packed = 0;
1713 
1714  /* Get a line of input. */
1715  if (!unformat_user (input, unformat_line_input, line_input))
1716  return 0;
1717 
1718  /* GSO feature is disable by default */
1720  /* packed-ring feature is disable by default */
1721  feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1722  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1723  {
1724  if (unformat (line_input, "socket %s", &sock_filename))
1725  ;
1726  else if (unformat (line_input, "server"))
1727  is_server = 1;
1728  else if (unformat (line_input, "gso"))
1729  enable_gso = 1;
1730  else if (unformat (line_input, "packed"))
1731  enable_packed = 1;
1732  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1733  ;
1734  else
1735  if (unformat
1736  (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1737  hw = hwaddr;
1738  else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1739  {
1740  renumber = 1;
1741  }
1742  else
1743  {
1744  error = clib_error_return (0, "unknown input `%U'",
1745  format_unformat_error, line_input);
1746  goto done;
1747  }
1748  }
1749 
1750  vnet_main_t *vnm = vnet_get_main ();
1751 
1752  int rv;
1753  if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1754  is_server, &sw_if_index, feature_mask,
1755  renumber, custom_dev_instance, hw,
1756  enable_gso, enable_packed)))
1757  {
1758  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1759  goto done;
1760  }
1761 
1763  sw_if_index);
1764 
1765 done:
1766  vec_free (sock_filename);
1767  unformat_free (line_input);
1768 
1769  return error;
1770 }
1771 
1772 clib_error_t *
1774  unformat_input_t * input,
1775  vlib_cli_command_t * cmd)
1776 {
1777  unformat_input_t _line_input, *line_input = &_line_input;
1778  u32 sw_if_index = ~0;
1779  vnet_main_t *vnm = vnet_get_main ();
1780  clib_error_t *error = NULL;
1781 
1782  /* Get a line of input. */
1783  if (!unformat_user (input, unformat_line_input, line_input))
1784  return 0;
1785 
1786  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1787  {
1788  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1789  ;
1790  else if (unformat
1791  (line_input, "%U", unformat_vnet_sw_interface, vnm,
1792  &sw_if_index))
1793  {
1794  vnet_hw_interface_t *hwif =
1796  if (hwif == NULL ||
1798  {
1799  error = clib_error_return (0, "Not a vhost interface");
1800  goto done;
1801  }
1802  }
1803  else
1804  {
1805  error = clib_error_return (0, "unknown input `%U'",
1806  format_unformat_error, line_input);
1807  goto done;
1808  }
1809  }
1810 
1811  vhost_user_delete_if (vnm, vm, sw_if_index);
1812 
1813 done:
1814  unformat_free (line_input);
1815 
1816  return error;
1817 }
1818 
1819 int
1821  vhost_user_intf_details_t ** out_vuids)
1822 {
1823  int rv = 0;
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;
1830  int i;
1831 
1832  if (!out_vuids)
1833  return -1;
1834 
1836  vec_add1 (hw_if_indices, vui->hw_if_index);
1837 
1838  for (i = 0; i < vec_len (hw_if_indices); i++)
1839  {
1840  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1842 
1843  vec_add2 (r_vuids, vuid, 1);
1844  vuid->sw_if_index = vui->sw_if_index;
1845  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1846  vuid->features = vui->features;
1847  vuid->num_regions = vui->nregions;
1848  vuid->is_server = vui->unix_server_index != ~0;
1849  vuid->sock_errno = vui->sock_errno;
1850  snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1851  "%s", vui->sock_filename);
1852  memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1853  clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1854  vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1855  }
1856 
1857  vec_free (hw_if_indices);
1858 
1859  *out_vuids = r_vuids;
1860 
1861  return rv;
1862 }
1863 
1864 static u8 *
1865 format_vhost_user_desc (u8 * s, va_list * args)
1866 {
1867  char *fmt = va_arg (*args, char *);
1868  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1869  vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1870  int idx = va_arg (*args, int);
1871  u32 *mem_hint = va_arg (*args, u32 *);
1872 
1873  s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1874  desc_table[idx].flags, desc_table[idx].next,
1875  pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1876  mem_hint)));
1877  return s;
1878 }
1879 
1880 static u8 *
1881 format_vhost_user_vring (u8 * s, va_list * args)
1882 {
1883  char *fmt = va_arg (*args, char *);
1884  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1885  int q = va_arg (*args, int);
1886 
1887  s = format (s, fmt, vui->vrings[q].avail->flags, vui->vrings[q].avail->idx,
1888  vui->vrings[q].used->flags, vui->vrings[q].used->idx);
1889  return s;
1890 }
1891 
1892 static void
1894 {
1895  int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1896  int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1897 
1898  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1899  vui->vrings[q].errfd);
1900 }
1901 
1902 static void
1904  int show_descr, int show_verbose)
1905 {
1906  int j;
1907  u32 mem_hint = 0;
1908  u32 idx;
1909  u32 n_entries;
1910  vring_desc_t *desc_table;
1911 
1912  if (vui->vrings[q].avail && vui->vrings[q].used)
1914  " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1915  vui, q);
1916 
1917  vhost_user_show_fds (vm, vui, q);
1918 
1919  if (show_descr)
1920  {
1921  vlib_cli_output (vm, "\n descriptor table:\n");
1922  vlib_cli_output (vm,
1923  " slot addr len flags next "
1924  "user_addr\n");
1925  vlib_cli_output (vm,
1926  " ===== ================== ===== ====== ===== "
1927  "==================\n");
1928  for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1929  {
1930  desc_table = vui->vrings[q].desc;
1932  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1933  desc_table, j, &mem_hint);
1934  if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1935  {
1936  n_entries = desc_table[j].len / sizeof (vring_desc_t);
1937  desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1938  if (desc_table)
1939  {
1940  for (idx = 0; idx < clib_min (20, n_entries); idx++)
1941  {
1943  (vm, "%U", format_vhost_user_desc,
1944  "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1945  desc_table, idx, &mem_hint);
1946  }
1947  if (n_entries >= 20)
1948  vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1949  n_entries);
1950  }
1951  }
1952  }
1953  }
1954 }
1955 
1956 static u8 *
1957 format_vhost_user_packed_desc (u8 * s, va_list * args)
1958 {
1959  char *fmt = va_arg (*args, char *);
1960  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1961  vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1962  int idx = va_arg (*args, int);
1963  u32 *mem_hint = va_arg (*args, u32 *);
1964 
1965  s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1966  desc_table[idx].flags, desc_table[idx].id,
1967  pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1968  mem_hint)));
1969  return s;
1970 }
1971 
1972 static u8 *
1973 format_vhost_user_vring_packed (u8 * s, va_list * args)
1974 {
1975  char *fmt = va_arg (*args, char *);
1976  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1977  int q = va_arg (*args, int);
1978 
1979  s = format (s, fmt, vui->vrings[q].avail_event->flags,
1980  vui->vrings[q].avail_event->off_wrap,
1981  vui->vrings[q].used_event->flags,
1982  vui->vrings[q].used_event->off_wrap,
1983  vui->vrings[q].avail_wrap_counter,
1984  vui->vrings[q].used_wrap_counter);
1985  return s;
1986 }
1987 
1988 static void
1990  int show_descr, int show_verbose)
1991 {
1992  int j;
1993  u32 mem_hint = 0;
1994  u32 idx;
1995  u32 n_entries;
1996  vring_packed_desc_t *desc_table;
1997 
1998  if (vui->vrings[q].avail_event && vui->vrings[q].used_event)
2000  " avail_event.flags %x avail_event.off_wrap %u "
2001  "used_event.flags %x used_event.off_wrap %u\n"
2002  " avail wrap counter %u, used wrap counter %u\n",
2003  vui, q);
2004 
2005  vhost_user_show_fds (vm, vui, q);
2006 
2007  if (show_descr)
2008  {
2009  vlib_cli_output (vm, "\n descriptor table:\n");
2010  vlib_cli_output (vm,
2011  " slot addr len flags id "
2012  "user_addr\n");
2013  vlib_cli_output (vm,
2014  " ===== ================== ===== ====== ===== "
2015  "==================\n");
2016  for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
2017  {
2018  desc_table = vui->vrings[q].packed_desc;
2020  " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2021  desc_table, j, &mem_hint);
2022  if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2023  {
2024  n_entries = desc_table[j].len >> 4;
2025  desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2026  if (desc_table)
2027  {
2028  for (idx = 0; idx < clib_min (20, n_entries); idx++)
2029  {
2031  (vm, "%U", format_vhost_user_packed_desc,
2032  "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2033  desc_table, idx, &mem_hint);
2034  }
2035  if (n_entries >= 20)
2036  vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2037  n_entries);
2038  }
2039  }
2040  }
2041  }
2042 }
2043 
2044 clib_error_t *
2046  unformat_input_t * input,
2047  vlib_cli_command_t * cmd)
2048 {
2049  clib_error_t *error = 0;
2050  vnet_main_t *vnm = vnet_get_main ();
2052  vhost_user_intf_t *vui;
2053  u32 hw_if_index, *hw_if_indices = 0;
2055  u16 qid;
2056  u32 ci;
2057  int i, j, q;
2058  int show_descr = 0;
2059  int show_verbose = 0;
2060  struct feat_struct
2061  {
2062  u8 bit;
2063  char *str;
2064  };
2065  struct feat_struct *feat_entry;
2066 
2067  static struct feat_struct feat_array[] = {
2068 #define _(s,b) { .str = #s, .bit = b, },
2070 #undef _
2071  {.str = NULL}
2072  };
2073 
2074 #define foreach_protocol_feature \
2075  _(VHOST_USER_PROTOCOL_F_MQ) \
2076  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2077 
2078  static struct feat_struct proto_feat_array[] = {
2079 #define _(s) { .str = #s, .bit = s},
2081 #undef _
2082  {.str = NULL}
2083  };
2084 
2085  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2086  {
2087  if (unformat
2088  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2089  {
2090  hi = vnet_get_hw_interface (vnm, hw_if_index);
2091  if (vhost_user_device_class.index != hi->dev_class_index)
2092  {
2093  error = clib_error_return (0, "unknown input `%U'",
2094  format_unformat_error, input);
2095  goto done;
2096  }
2097  vec_add1 (hw_if_indices, hw_if_index);
2098  }
2099  else if (unformat (input, "descriptors") || unformat (input, "desc"))
2100  show_descr = 1;
2101  else if (unformat (input, "verbose"))
2102  show_verbose = 1;
2103  else
2104  {
2105  error = clib_error_return (0, "unknown input `%U'",
2106  format_unformat_error, input);
2107  goto done;
2108  }
2109  }
2110  if (vec_len (hw_if_indices) == 0)
2111  {
2113  vec_add1 (hw_if_indices, vui->hw_if_index);
2114  }
2115  vlib_cli_output (vm, "Virtio vhost-user interfaces");
2116  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2117  vum->coalesce_frames, vum->coalesce_time);
2118  vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2119  vum->ifq_count);
2120  vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2121 
2122  for (i = 0; i < vec_len (hw_if_indices); i++)
2123  {
2124  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2126  vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2127  format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2128  hw_if_indices[i]);
2129  if (vui->enable_gso)
2130  vlib_cli_output (vm, " GSO enable");
2131  if (vui->enable_packed)
2132  vlib_cli_output (vm, " Packed ring enable");
2133 
2134  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2135  " features mask (0x%llx): \n"
2136  " features (0x%llx): \n",
2137  vui->virtio_net_hdr_sz, vui->feature_mask,
2138  vui->features);
2139 
2140  feat_entry = (struct feat_struct *) &feat_array;
2141  while (feat_entry->str)
2142  {
2143  if (vui->features & (1ULL << feat_entry->bit))
2144  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2145  feat_entry->bit);
2146  feat_entry++;
2147  }
2148 
2149  vlib_cli_output (vm, " protocol features (0x%llx)",
2150  vui->protocol_features);
2151  feat_entry = (struct feat_struct *) &proto_feat_array;
2152  while (feat_entry->str)
2153  {
2154  if (vui->protocol_features & (1ULL << feat_entry->bit))
2155  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2156  feat_entry->bit);
2157  feat_entry++;
2158  }
2159 
2160  vlib_cli_output (vm, "\n");
2161 
2162  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2163  vui->sock_filename,
2164  (vui->unix_server_index != ~0) ? "server" : "client",
2165  strerror (vui->sock_errno));
2166 
2167  vlib_cli_output (vm, " rx placement: ");
2168 
2169  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
2170  {
2171  vnet_main_t *vnm = vnet_get_main ();
2172  uword thread_index;
2174  vhost_user_vring_t *txvq = &vui->vrings[qid];
2175 
2176  if (txvq->qid == -1)
2177  continue;
2178  thread_index =
2180  qid >> 1);
2181  vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
2182  &mode);
2183  vlib_cli_output (vm, " thread %d on vring %d, %U\n",
2184  thread_index, qid,
2186  }
2187 
2188  vlib_cli_output (vm, " tx placement: %s\n",
2189  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2190 
2192  {
2193  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2194  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2195  }
2196 
2197  vlib_cli_output (vm, "\n");
2198 
2199  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2200 
2201  if (vui->nregions)
2202  {
2203  vlib_cli_output (vm,
2204  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2205  vlib_cli_output (vm,
2206  " ====== ===== ================== ================== ================== ================== ==================\n");
2207  }
2208  for (j = 0; j < vui->nregions; j++)
2209  {
2210  vlib_cli_output (vm,
2211  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2212  j, vui->region_mmap_fd[j],
2213  vui->regions[j].guest_phys_addr,
2214  vui->regions[j].memory_size,
2215  vui->regions[j].userspace_addr,
2216  vui->regions[j].mmap_offset,
2218  }
2219  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2220  {
2221  if (!vui->vrings[q].started)
2222  continue;
2223 
2224  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2225  (q & 1) ? "RX" : "TX",
2226  vui->vrings[q].enabled ? "" : " disabled");
2227 
2228  vlib_cli_output (vm,
2229  " qsz %d last_avail_idx %d last_used_idx %d\n",
2230  vui->vrings[q].qsz_mask + 1,
2231  vui->vrings[q].last_avail_idx,
2232  vui->vrings[q].last_used_idx);
2233 
2235  vhost_user_show_desc_packed (vm, vui, q, show_descr,
2236  show_verbose);
2237  else
2238  vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2239  }
2240  vlib_cli_output (vm, "\n");
2241  }
2242 done:
2243  vec_free (hw_if_indices);
2244  return error;
2245 }
2246 
2247 /*
2248  * CLI functions
2249  */
2250 
2251 /*?
2252  * Create a vHost User interface. Once created, a new virtual interface
2253  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2254  * is the next free index.
2255  *
2256  * There are several parameters associated with a vHost interface:
2257  *
2258  * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2259  * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2260  * create the socket if it does not already exist. If in '<em>client</em>' mode,
2261  * hypervisor will create the socket if it does not already exist. The VPP code
2262  * is indifferent to the file location. However, if SELinux is enabled, then the
2263  * socket needs to be created in '<em>/var/run/vpp/</em>'.
2264  *
2265  * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2266  * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2267  * mode, the VM can be reset without tearing down the vHost Interface. In
2268  * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2269  * tearing down the vHost Interface.
2270  *
2271  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2272  * startup. <b>This is intended for degugging only.</b> It is recommended that this
2273  * parameter not be used except by experienced users. By default, all supported
2274  * features will be advertised. Otherwise, provide the set of features desired.
2275  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2276  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2277  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2278  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2279  * - 0x004000000 (26) - VHOST_F_LOG_ALL
2280  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2281  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2282  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2283  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2284  *
2285  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2286  * X:X:X:X:X:X unix or X.X.X cisco format.
2287  *
2288  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2289  * in the name to be specified. If instance already exists, name will be used
2290  * anyway and multiple instances will have the same name. Use with caution.
2291  *
2292  * @cliexpar
2293  * Example of how to create a vhost interface with VPP as the client and all features enabled:
2294  * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2295  * VirtualEthernet0/0/0
2296  * @cliexend
2297  * Example of how to create a vhost interface with VPP as the server and with just
2298  * multiple queues enabled:
2299  * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2300  * VirtualEthernet0/0/1
2301  * @cliexend
2302  * Once the vHost interface is created, enable the interface using:
2303  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2304 ?*/
2305 /* *INDENT-OFF* */
2306 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2307  .path = "create vhost-user",
2308  .short_help = "create vhost-user socket <socket-filename> [server] "
2309  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2310  "[packed]",
2311  .function = vhost_user_connect_command_fn,
2312  .is_mp_safe = 1,
2313 };
2314 /* *INDENT-ON* */
2315 
2316 /*?
2317  * Delete a vHost User interface using the interface name or the
2318  * software interface index. Use the '<em>show interface</em>'
2319  * command to determine the software interface index. On deletion,
2320  * the linux socket will not be deleted.
2321  *
2322  * @cliexpar
2323  * Example of how to delete a vhost interface by name:
2324  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2325  * Example of how to delete a vhost interface by software interface index:
2326  * @cliexcmd{delete vhost-user sw_if_index 1}
2327 ?*/
2328 /* *INDENT-OFF* */
2329 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2330  .path = "delete vhost-user",
2331  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2332  .function = vhost_user_delete_command_fn,
2333 };
2334 
2335 /*?
2336  * Display the attributes of a single vHost User interface (provide interface
2337  * name), multiple vHost User interfaces (provide a list of interface names seperated
2338  * by spaces) or all Vhost User interfaces (omit an interface name to display all
2339  * vHost interfaces).
2340  *
2341  * @cliexpar
2342  * @parblock
2343  * Example of how to display a vhost interface:
2344  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2345  * Virtio vhost-user interfaces
2346  * Global:
2347  * coalesce frames 32 time 1e-3
2348  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2349  * virtio_net_hdr_sz 12
2350  * features mask (0xffffffffffffffff):
2351  * features (0x50408000):
2352  * VIRTIO_NET_F_MRG_RXBUF (15)
2353  * VIRTIO_NET_F_MQ (22)
2354  * VIRTIO_F_INDIRECT_DESC (28)
2355  * VHOST_USER_F_PROTOCOL_FEATURES (30)
2356  * protocol features (0x3)
2357  * VHOST_USER_PROTOCOL_F_MQ (0)
2358  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2359  *
2360  * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2361  *
2362  * rx placement:
2363  * thread 1 on vring 1
2364  * thread 1 on vring 5
2365  * thread 2 on vring 3
2366  * thread 2 on vring 7
2367  * tx placement: spin-lock
2368  * thread 0 on vring 0
2369  * thread 1 on vring 2
2370  * thread 2 on vring 0
2371  *
2372  * Memory regions (total 2)
2373  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2374  * ====== ===== ================== ================== ================== ================== ==================
2375  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2376  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2377  *
2378  * Virtqueue 0 (TX)
2379  * qsz 256 last_avail_idx 0 last_used_idx 0
2380  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2381  * kickfd 62 callfd 64 errfd -1
2382  *
2383  * Virtqueue 1 (RX)
2384  * qsz 256 last_avail_idx 0 last_used_idx 0
2385  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2386  * kickfd 65 callfd 66 errfd -1
2387  *
2388  * Virtqueue 2 (TX)
2389  * qsz 256 last_avail_idx 0 last_used_idx 0
2390  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2391  * kickfd 63 callfd 70 errfd -1
2392  *
2393  * Virtqueue 3 (RX)
2394  * qsz 256 last_avail_idx 0 last_used_idx 0
2395  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2396  * kickfd 72 callfd 74 errfd -1
2397  *
2398  * Virtqueue 4 (TX disabled)
2399  * qsz 256 last_avail_idx 0 last_used_idx 0
2400  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2401  * kickfd 76 callfd 78 errfd -1
2402  *
2403  * Virtqueue 5 (RX disabled)
2404  * qsz 256 last_avail_idx 0 last_used_idx 0
2405  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2406  * kickfd 80 callfd 82 errfd -1
2407  *
2408  * Virtqueue 6 (TX disabled)
2409  * qsz 256 last_avail_idx 0 last_used_idx 0
2410  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2411  * kickfd 84 callfd 86 errfd -1
2412  *
2413  * Virtqueue 7 (RX disabled)
2414  * qsz 256 last_avail_idx 0 last_used_idx 0
2415  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2416  * kickfd 88 callfd 90 errfd -1
2417  *
2418  * @cliexend
2419  *
2420  * The optional '<em>descriptors</em>' parameter will display the same output as
2421  * the previous example but will include the descriptor table for each queue.
2422  * The output is truncated below:
2423  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2424  * Virtio vhost-user interfaces
2425  * Global:
2426  * coalesce frames 32 time 1e-3
2427  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2428  * virtio_net_hdr_sz 12
2429  * features mask (0xffffffffffffffff):
2430  * features (0x50408000):
2431  * VIRTIO_NET_F_MRG_RXBUF (15)
2432  * VIRTIO_NET_F_MQ (22)
2433  * :
2434  * Virtqueue 0 (TX)
2435  * qsz 256 last_avail_idx 0 last_used_idx 0
2436  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2437  * kickfd 62 callfd 64 errfd -1
2438  *
2439  * descriptor table:
2440  * id addr len flags next user_addr
2441  * ===== ================== ===== ====== ===== ==================
2442  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2443  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2444  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2445  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2446  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2447  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2448  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2449  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2450  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2451  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2452  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2453  * :
2454  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2455  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2456  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2457  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2458  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2459  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2460  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2461  *
2462  * Virtqueue 1 (RX)
2463  * qsz 256 last_avail_idx 0 last_used_idx 0
2464  * :
2465  * @cliexend
2466  * @endparblock
2467 ?*/
2468 /* *INDENT-OFF* */
2469 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2470  .path = "show vhost-user",
2471  .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2472  "[[descriptors] [verbose]]",
2473  .function = show_vhost_user_command_fn,
2474 };
2475 /* *INDENT-ON* */
2476 
2477 
2478 static clib_error_t *
2480 {
2482 
2483  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2484  {
2485  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2486  ;
2487  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2488  ;
2489  else if (unformat (input, "dont-dump-memory"))
2490  vum->dont_dump_vhost_user_memory = 1;
2491  else
2492  return clib_error_return (0, "unknown input `%U'",
2493  format_unformat_error, input);
2494  }
2495 
2496  return 0;
2497 }
2498 
2499 /* vhost-user { ... } configuration. */
2500 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2501 
2502 void
2504 {
2506  vhost_user_intf_t *vui;
2507 
2508  if (vum->dont_dump_vhost_user_memory)
2509  {
2511  unmap_all_mem_regions (vui);
2512  }
2513 }
2514 
2515 /*
2516  * fd.io coding-style-patch-verification: ON
2517  *
2518  * Local Variables:
2519  * eval: (c-set-style "gnu")
2520  * End:
2521  */
unformat_function_t unformat_vnet_hw_interface
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:338
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
#define VRING_EVENT_F_DISABLE
Definition: virtio_std.h:81
format_function_t format_vnet_hw_if_rx_mode
#define vec_foreach_index(var, v)
Iterate over vector indices.
static void clib_file_del(clib_file_main_t *um, clib_file_t *f)
Definition: file.h:109
format_function_t format_vnet_hw_if_index_name
#define vu_log_warn(dev, f,...)
Definition: vhost_user.h:45
#define clib_min(x, y)
Definition: clib.h:328
vring_desc_t * desc
Definition: vhost_user.h:170
static uword random_default_seed(void)
Default random seed (unix/linux user-mode)
Definition: random.h:91
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD
Definition: vhost_user.h:32
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
VNET_HW_INTERFACE_CLASS(vhost_interface_class, static)
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:307
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
vring_desc_event_t * used_event
Definition: vhost_user.h:181
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:393
static void stop_timer(wg_peer_t *peer, u32 timer_id)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:237
void vnet_sw_interface_set_mtu(vnet_main_t *vnm, u32 sw_if_index, u32 mtu)
Definition: interface.c:686
static clib_error_t * vhost_user_socksvr_accept_ready(clib_file_t *uf)
Definition: vhost_user.c:1015
unsigned long u64
Definition: types.h:89
#define VHOST_VRING_MAX_N
Definition: vhost_user.h:25
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
vring_packed_desc_t * packed_desc
Definition: vhost_user.h:171
static_always_inline void vhost_user_update_iface_state(vhost_user_intf_t *vui)
Definition: vhost_user.c:195
u64 private_data
Definition: file.h:64
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
vring_avail_t * avail
Definition: vhost_user.h:175
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 file_descriptor
Definition: file.h:54
vnet_device_class_t vhost_user_device_class
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
static clib_error_t * vhost_user_callfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:228
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
#define VHOST_USER_EVENT_START_TIMER
Definition: vhost_user.h:212
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
vlib_main_t * vm
Definition: in2out_ed.c:1580
static_always_inline void vhost_user_vring_init(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:278
unformat_function_t unformat_vnet_sw_interface
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:509
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
vring_used_t * used
Definition: vhost_user.h:180
vhost_vring_addr_t addr
Definition: vhost_user.h:111
format_function_t format_vnet_sw_if_index_name
#define VHOST_USER_VRING_NOFD_MASK
Definition: vhost_user.h:29
unsigned char u8
Definition: types.h:56
static_always_inline void vhost_user_rx_thread_placement(vhost_user_intf_t *vui, u32 qid)
Unassign existing interface/queue to thread mappings and re-assign new interface/queue to thread mapp...
Definition: vhost_user.c:159
static uword vlib_process_suspend_time_is_zero(f64 dt)
Returns TRUE if a process suspend time is less than 10us.
Definition: node_funcs.h:470
#define VIRTIO_FEATURE(X)
Definition: virtio_std.h:69
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define vu_log_debug(dev, f,...)
Definition: vhost_user.h:38
clib_file_t * file_pool
Definition: file.h:88
#define static_always_inline
Definition: clib.h:109
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
static clib_error_t * vhost_user_socket_read(clib_file_t *uf)
Definition: vhost_user.c:356
#define UNIX_GET_FD(unixfd_idx)
Definition: vhost_user.h:58
static void vhost_user_show_desc(vlib_main_t *vm, vhost_user_intf_t *vui, int q, int show_descr, int show_verbose)
Definition: vhost_user.c:1903
void * log_base_addr
Definition: vhost_user.h:247
static_always_inline void vhost_user_thread_placement(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:239
static_always_inline void * map_guest_mem(vhost_user_intf_t *vui, uword addr, u32 *hint)
static_always_inline void vnet_device_input_set_interrupt_pending(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:127
description fragment has unexpected format
Definition: map.api:433
vnet_hw_interface_flags_t flags
Definition: interface.h:538
static void vhost_user_show_desc_packed(vlib_main_t *vm, vhost_user_intf_t *vui, int q, int show_descr, int show_verbose)
Definition: vhost_user.c:1989
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
unsigned int u32
Definition: types.h:88
static vlib_node_registration_t vhost_user_process_node
(constructor) VLIB_REGISTER_NODE (vhost_user_process_node)
Definition: vhost_user.c:1283
static clib_error_t * vhost_user_kickfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:251
static clib_error_t * vhost_user_socket_error(clib_file_t *uf)
Definition: vhost_user.c:1000
void vhost_user_unmap_all(void)
Definition: vhost_user.c:2503
static u8 * format_vhost_user_packed_desc(u8 *s, va_list *args)
Definition: vhost_user.c:1957
unformat_function_t unformat_line_input
Definition: format.h:282
static_always_inline void vhost_user_tx_thread_placement(vhost_user_intf_t *vui)
Definition: vhost_user.c:118
#define VHOST_USER_PROTOCOL_F_MQ
Definition: vhost_user.h:31
#define VRING_USED_F_NO_NOTIFY
Definition: virtio_std.h:84
Definition: cJSON.c:84
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
int vhost_user_create_if(vnet_main_t *vnm, vlib_main_t *vm, const char *sock_filename, u8 is_server, u32 *sw_if_index, u64 feature_mask, u8 renumber, u32 custom_dev_instance, u8 *hwaddr, u8 enable_gso, u8 enable_packed)
Definition: vhost_user.c:1579
static_always_inline uword vnet_get_device_input_thread_index(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:118
static void vhost_user_term_if(vhost_user_intf_t *vui)
Disables and reset interface structure.
Definition: vhost_user.c:1295
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
static_always_inline int vhost_user_intf_ready(vhost_user_intf_t *vui)
Returns whether at least one TX and one RX vring are enabled.
Definition: vhost_user.c:183
u32 random
Pseudo random iterator.
Definition: vhost_user.h:310
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define clib_error_return_unix(e, args...)
Definition: error.h:102
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_if_rx_mode mode)
Definition: devices.c:253
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:301
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost_user.c:1343
static u8 * format_vhost_user_desc(u8 *s, va_list *args)
Definition: vhost_user.c:1865
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:182
char sock_filename[256]
Definition: vhost_user.h:222
vhost_user_main_t vhost_user_main
Definition: vhost_user.c:54
#define VHOST_USER_MSG_HDR_SZ
Definition: vhost_user.h:24
u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:238
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1820
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:234
vlib_log_class_t log_default
Definition: vhost_user.h:316
int cJSON_bool fmt
Definition: cJSON.h:160
vl_api_tunnel_mode_t mode
Definition: gre.api:48
u8 len
Definition: ip_types.api:103
static_always_inline void vhost_user_update_gso_interface_count(vhost_user_intf_t *vui, u8 add)
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost_user.h:26
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost_user.h:301
static_always_inline void vhost_user_vring_close(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:301
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:300
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
static void mhash_init_c_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:78
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static void unmap_all_mem_regions(vhost_user_intf_t *vui)
Definition: vhost_user.c:72
static clib_error_t * vhost_user_config(vlib_main_t *vm, unformat_input_t *input)
Definition: vhost_user.c:2479
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:178
static_always_inline u64 vhost_user_is_packed_ring_supported(vhost_user_intf_t *vui)
#define VHOST_USER_EVENT_STOP_TIMER
Definition: vhost_user.h:213
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define clib_warning(format, args...)
Definition: error.h:59
static uword vhost_user_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost_user.c:1183
#define ARRAY_LEN(x)
Definition: clib.h:67
clib_error_t * vhost_user_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1773
#define foreach_protocol_feature
int vhost_user_modify_if(vnet_main_t *vnm, vlib_main_t *vm, const char *sock_filename, u8 is_server, u32 sw_if_index, u64 feature_mask, u8 renumber, u32 custom_dev_instance, u8 enable_gso, u8 enable_packed)
Definition: vhost_user.c:1643
static int vhost_user_init_server_sock(const char *sock_filename, int *sock_fd)
Open server unix socket on specified sock_filename.
Definition: vhost_user.c:1425
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
clib_error_t * show_vhost_user_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:2045
#define ASSERT(truth)
#define VHOST_VRING_F_LOG
Definition: vhost_user.h:33
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
Definition: devices.c:139
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
#define vu_log_err(dev, f,...)
Definition: vhost_user.h:51
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
static_always_inline void vhost_user_if_disconnect(vhost_user_intf_t *vui)
Definition: vhost_user.c:333
__clib_export errno_t memcpy_s(void *__restrict__ dest, rsize_t dmax, const void *__restrict__ src, rsize_t n)
copy src to dest, at most n bytes, up to dmax
Definition: string.c:120
static_always_inline void vhost_user_send_call(vlib_main_t *vm, vhost_user_vring_t *vq)
static void clib_mem_free(void *p)
Definition: mem.h:311
volatile u32 * vring_locks[VHOST_VRING_MAX_N]
Definition: vhost_user.h:242
vring_desc_event_t * avail_event
Definition: vhost_user.h:176
mhash_t if_index_by_sock_name
Definition: vhost_user.h:298
static clib_error_t * vhost_user_exit(vlib_main_t *vm)
Definition: vhost_user.c:1403
#define clib_error_report(e)
Definition: error.h:113
vlib_node_registration_t vhost_user_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (vhost_user_send_interrupt_node)
Definition: vhost_user.c:51
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static void vhost_user_create_ethernet(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_t *vui, u8 *hwaddress)
Create ethernet interface for vhost user interface.
Definition: vhost_user.c:1465
static u8 * format_vhost_user_vring_packed(u8 *s, va_list *args)
Definition: vhost_user.c:1973
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_ip4_address_t hi
Definition: arp.api:37
struct _vlib_node_registration vlib_node_registration_t
__clib_export uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:346
void * region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:235
static void vhost_user_vui_init(vnet_main_t *vnm, vhost_user_intf_t *vui, int server_sock_fd, const char *sock_filename, u64 feature_mask, u32 *sw_if_index, u8 enable_gso, u8 enable_packed)
Definition: vhost_user.c:1500
static long get_huge_page_size(int fd)
Definition: vhost_user.c:64
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static u8 * format_vhost_user_vring(u8 *s, va_list *args)
Definition: vhost_user.c:1881
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:348
vhost_user_vring_t vrings[VHOST_VRING_MAX_N]
Definition: vhost_user.h:241
static void vhost_user_set_interrupt_pending(vhost_user_intf_t *vui, u32 ifq)
Definition: vhost_user.c:212
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost_user.h:23
clib_error_t * vhost_user_connect_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1698
u32 rx_buffers_len
Definition: vhost_user.h:282
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.c:188
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:261
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1561
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1422
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
vnet_hw_if_rx_mode
Definition: interface.h:53
u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:236
#define vec_foreach(var, vec)
Vector iterator.
Definition: file.h:51
static clib_error_t * vhost_user_init(vlib_main_t *vm)
Definition: vhost_user.c:1051
static_always_inline void * map_user_mem(vhost_user_intf_t *vui, uword addr)
static void vhost_user_show_fds(vlib_main_t *vm, vhost_user_intf_t *vui, int q)
Definition: vhost_user.c:1893
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static uword vhost_user_send_interrupt_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost_user.c:1086
#define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS
Definition: vhost_user.h:89
vl_api_address_union_t un
Definition: ip_types.api:98
#define VLIB_INITS(...)
Definition: init.h:357
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: devices.h:79
__clib_export uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:264
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
#define VRING_DESC_F_INDIRECT
Definition: virtio_std.h:75
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
int dont_dump_vhost_user_memory
Definition: vhost_user.h:304
#define VRING_DESC_F_AVAIL
Definition: virtio_std.h:77
int vnet_hw_interface_get_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_if_rx_mode *mode)
Definition: devices.c:313