FD.io VPP  v17.07.01-10-g3be13f0
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 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/ip/ip.h>
37 
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
41 
43 
44 /**
45  * @file
46  * @brief vHost User Device Driver.
47  *
48  * This file contains the source code for vHost User interface.
49  */
50 
51 
52 #define VHOST_DEBUG_VQ 0
53 
54 #define DBG_SOCK(args...) \
55  { \
56  vhost_user_main_t *_vum = &vhost_user_main; \
57  if (_vum->debug) \
58  clib_warning(args); \
59  };
60 
61 #if VHOST_DEBUG_VQ == 1
62 #define DBG_VQ(args...) clib_warning(args);
63 #else
64 #define DBG_VQ(args...)
65 #endif
66 
67 /*
68  * When an RX queue is down but active, received packets
69  * must be discarded. This value controls up to how many
70  * packets will be discarded during each round.
71  */
72 #define VHOST_USER_DOWN_DISCARD_COUNT 256
73 
74 /*
75  * When the number of available buffers gets under this threshold,
76  * RX node will start discarding packets.
77  */
78 #define VHOST_USER_RX_BUFFER_STARVATION 32
79 
80 /*
81  * On the receive side, the host should free descriptors as soon
82  * as possible in order to avoid TX drop in the VM.
83  * This value controls the number of copy operations that are stacked
84  * before copy is done for all and descriptors are given back to
85  * the guest.
86  * The value 64 was obtained by testing (48 and 128 were not as good).
87  */
88 #define VHOST_USER_RX_COPY_THRESHOLD 64
89 /*
90  * On the transmit side, we keep processing the buffers from vlib in the while
91  * loop and prepare the copy order to be executed later. However, the static
92  * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
93  * entries. In order to not corrupt memory, we have to do the copy when the
94  * static array reaches the copy threshold. We subtract 40 in case the code
95  * goes into the inner loop for a maximum of 64k frames which may require
96  * more array entries.
97  */
98 #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 40)
99 
100 #define UNIX_GET_FD(unixfd_idx) \
101  (unixfd_idx != ~0) ? \
102  pool_elt_at_index (unix_main.file_pool, \
103  unixfd_idx)->file_descriptor : -1;
104 
105 #define foreach_virtio_trace_flags \
106  _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
107  _ (SINGLE_DESC, 1, "Single descriptor packet") \
108  _ (INDIRECT, 2, "Indirect descriptor") \
109  _ (MAP_ERROR, 4, "Memory mapping error")
110 
111 typedef enum
112 {
113 #define _(n,i,s) VIRTIO_TRACE_F_##n,
115 #undef _
117 
119 
120 #define foreach_vhost_user_tx_func_error \
121  _(NONE, "no error") \
122  _(NOT_READY, "vhost vring not ready") \
123  _(DOWN, "vhost interface is down") \
124  _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
125  _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
126  _(MMAP_FAIL, "mmap failure") \
127  _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
128 
129 typedef enum
130 {
131 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
133 #undef _
136 
138 #define _(n,s) s,
140 #undef _
141 };
142 
143 #define foreach_vhost_user_input_func_error \
144  _(NO_ERROR, "no error") \
145  _(NO_BUFFER, "no available buffer") \
146  _(MMAP_FAIL, "mmap failure") \
147  _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
148  _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
149  _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
150 
151 typedef enum
152 {
153 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
155 #undef _
158 
160 #define _(n,s) s,
162 #undef _
163 };
164 
165 /* *INDENT-OFF* */
166 static vhost_user_main_t vhost_user_main = {
167  .mtu_bytes = 1518,
168 };
169 
170 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
171  .name = "vhost-user",
172 };
173 /* *INDENT-ON* */
174 
175 static u8 *
177 {
178  u32 i = va_arg (*args, u32);
179  u32 show_dev_instance = ~0;
181 
183  show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
184 
185  if (show_dev_instance != ~0)
186  i = show_dev_instance;
187 
188  s = format (s, "VirtualEthernet0/0/%d", i);
189  return s;
190 }
191 
192 static int
194 {
195  // FIXME: check if the new dev instance is already used
198  hi->dev_instance, ~0);
199 
201  new_dev_instance;
202 
203  DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
204  hi->dev_instance, new_dev_instance);
205 
206  return 0;
207 }
208 
211 {
212  int i = *hint;
213  if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
214  ((vui->regions[i].guest_phys_addr +
215  vui->regions[i].memory_size) > addr)))
216  {
217  return (void *) (vui->region_mmap_addr[i] + addr -
218  vui->regions[i].guest_phys_addr);
219  }
220 #if __SSE4_2__
221  __m128i rl, rh, al, ah, r;
222  al = _mm_set1_epi64x (addr + 1);
223  ah = _mm_set1_epi64x (addr);
224 
225  rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
226  rl = _mm_cmpgt_epi64 (al, rl);
227  rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
228  rh = _mm_cmpgt_epi64 (rh, ah);
229  r = _mm_and_si128 (rl, rh);
230 
231  rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
232  rl = _mm_cmpgt_epi64 (al, rl);
233  rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
234  rh = _mm_cmpgt_epi64 (rh, ah);
235  r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
236 
237  rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
238  rl = _mm_cmpgt_epi64 (al, rl);
239  rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
240  rh = _mm_cmpgt_epi64 (rh, ah);
241  r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
242 
243  rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
244  rl = _mm_cmpgt_epi64 (al, rl);
245  rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
246  rh = _mm_cmpgt_epi64 (rh, ah);
247  r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
248 
249  r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
250  i = __builtin_ctzll (_mm_movemask_epi8 (r) |
252 
253  if (i < vui->nregions)
254  {
255  *hint = i;
256  return (void *) (vui->region_mmap_addr[i] + addr -
257  vui->regions[i].guest_phys_addr);
258  }
259 
260 #else
261  for (i = 0; i < vui->nregions; i++)
262  {
263  if ((vui->regions[i].guest_phys_addr <= addr) &&
264  ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
265  addr))
266  {
267  *hint = i;
268  return (void *) (vui->region_mmap_addr[i] + addr -
269  vui->regions[i].guest_phys_addr);
270  }
271  }
272 #endif
273  DBG_VQ ("failed to map guest mem addr %llx", addr);
274  *hint = 0;
275  return 0;
276 }
277 
278 static inline void *
280 {
281  int i;
282  for (i = 0; i < vui->nregions; i++)
283  {
284  if ((vui->regions[i].userspace_addr <= addr) &&
285  ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
286  addr))
287  {
288  return (void *) (vui->region_mmap_addr[i] + addr -
289  vui->regions[i].userspace_addr);
290  }
291  }
292  return 0;
293 }
294 
295 static long
297 {
298  struct statfs s;
299  fstatfs (fd, &s);
300  return s.f_bsize;
301 }
302 
303 static void
305 {
306  int i, r;
307  for (i = 0; i < vui->nregions; i++)
308  {
309  if (vui->region_mmap_addr[i] != (void *) -1)
310  {
311 
312  long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
313 
314  ssize_t map_sz = (vui->regions[i].memory_size +
315  vui->regions[i].mmap_offset +
316  page_sz - 1) & ~(page_sz - 1);
317 
318  r =
319  munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
320  map_sz);
321 
322  DBG_SOCK
323  ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
324  vui->region_mmap_addr[i], map_sz, page_sz);
325 
326  vui->region_mmap_addr[i] = (void *) -1;
327 
328  if (r == -1)
329  {
330  clib_warning ("failed to unmap memory region (errno %d)",
331  errno);
332  }
333  close (vui->region_mmap_fd[i]);
334  }
335  }
336  vui->nregions = 0;
337 }
338 
339 static void
341 {
342  //Let's try to assign one queue to each thread
343  u32 qid = 0;
344  u32 thread_index = 0;
345  vui->use_tx_spinlock = 0;
346  while (1)
347  {
348  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
349  {
350  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
351  if (!rxvq->started || !rxvq->enabled)
352  continue;
353 
354  vui->per_cpu_tx_qid[thread_index] = qid;
355  thread_index++;
356  if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
357  return;
358  }
359  //We need to loop, meaning the spinlock has to be used
360  vui->use_tx_spinlock = 1;
361  if (thread_index == 0)
362  {
363  //Could not find a single valid one
364  for (thread_index = 0;
365  thread_index < vlib_get_thread_main ()->n_vlib_mains;
366  thread_index++)
367  {
368  vui->per_cpu_tx_qid[thread_index] = 0;
369  }
370  return;
371  }
372  }
373 }
374 
375 /**
376  * @brief Unassign existing interface/queue to thread mappings and re-assign
377  * new interface/queue to thread mappings
378  */
379 static void
381 {
383  vhost_user_intf_t *vui;
384  vhost_user_vring_t *txvq;
385  vnet_main_t *vnm = vnet_get_main ();
386  u32 qid;
387  int rv;
388  u16 *queue;
389 
390  // Scrap all existing mappings for all interfaces/queues
391  /* *INDENT-OFF* */
392  pool_foreach (vui, vum->vhost_user_interfaces, {
393  vec_foreach (queue, vui->rx_queues)
394  {
395  rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
396  *queue);
397  if (rv)
398  clib_warning ("Warning: unable to unassign interface %d, "
399  "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
400  }
402  });
403  /* *INDENT-ON* */
404 
405  // Create the rx_queues for all interfaces
406  /* *INDENT-OFF* */
407  pool_foreach (vui, vum->vhost_user_interfaces, {
408  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
409  {
410  txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
411  if (txvq->started)
412  {
413  if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
414  /* Set polling as the default */
415  txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
416  vec_add1 (vui->rx_queues, qid);
417  }
418  }
419  });
420  /* *INDENT-ON* */
421 
422  // Assign new mappings for all interfaces/queues
423  /* *INDENT-OFF* */
424  pool_foreach (vui, vum->vhost_user_interfaces, {
425  vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
426  vhost_user_input_node.index);
427  vec_foreach (queue, vui->rx_queues)
428  {
429  vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
430  ~0);
431  txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
432  rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
433  txvq->mode);
434  if (rv)
435  clib_warning ("Warning: unable to set rx mode for interface %d, "
436  "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
437  }
438  });
439  /* *INDENT-ON* */
440 }
441 
442 /** @brief Returns whether at least one TX and one RX vring are enabled */
443 int
445 {
446  int i, found[2] = { }; //RX + TX
447 
448  for (i = 0; i < VHOST_VRING_MAX_N; i++)
449  if (vui->vrings[i].started && vui->vrings[i].enabled)
450  found[i & 1] = 1;
451 
452  return found[0] && found[1];
453 }
454 
455 static void
457 {
458  /* if we have pointers to descriptor table, go up */
459  int is_up = vhost_user_intf_ready (vui);
460  if (is_up != vui->is_up)
461  {
462  DBG_SOCK ("interface %d %s", vui->sw_if_index,
463  is_up ? "ready" : "down");
466  0);
467  vui->is_up = is_up;
468  }
471 }
472 
473 static void
475 {
476  u32 qid;
477  vnet_main_t *vnm = vnet_get_main ();
478 
479  qid = ifq & 0xff;
480  if ((qid & 1) == 0)
481  /* Only care about the odd number, or TX, virtqueue */
482  return;
483 
484  if (vhost_user_intf_ready (vui))
485  // qid >> 1 is to convert virtqueue number to vring queue index
487 }
488 
489 static clib_error_t *
491 {
492  __attribute__ ((unused)) int n;
493  u8 buff[8];
494 
495  n = read (uf->file_descriptor, ((char *) &buff), 8);
496 
497  return 0;
498 }
499 
500 static clib_error_t *
502 {
503  __attribute__ ((unused)) int n;
504  u8 buff[8];
505  vhost_user_intf_t *vui =
506  pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
507  uf->private_data >> 8);
508  u32 qid = uf->private_data & 0xff;
509 
510  n = read (uf->file_descriptor, ((char *) &buff), 8);
511  DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
512  if (!vui->vrings[qid].started ||
513  (vhost_user_intf_ready (vui) != vui->is_up))
514  {
516  vui->vrings[qid].started = 1;
519  }
520 
522  return 0;
523 }
524 
525 /**
526  * @brief Try once to lock the vring
527  * @return 0 on success, non-zero on failure.
528  */
529 static inline int
531 {
532  return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
533 }
534 
535 /**
536  * @brief Spin until the vring is successfully locked
537  */
538 static inline void
540 {
541  while (vhost_user_vring_try_lock (vui, qid))
542  ;
543 }
544 
545 /**
546  * @brief Unlock the vring lock
547  */
548 static inline void
550 {
551  *vui->vring_locks[qid] = 0;
552 }
553 
554 static inline void
556 {
557  vhost_user_vring_t *vring = &vui->vrings[qid];
558  memset (vring, 0, sizeof (*vring));
559  vring->kickfd_idx = ~0;
560  vring->callfd_idx = ~0;
561  vring->errfd = -1;
562 
563  /*
564  * We have a bug with some qemu 2.5, and this may be a fix.
565  * Feel like interpretation holy text, but this is from vhost-user.txt.
566  * "
567  * One queue pair is enabled initially. More queues are enabled
568  * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
569  * "
570  * Don't know who's right, but this is what DPDK does.
571  */
572  if (qid == 0 || qid == 1)
573  vring->enabled = 1;
574 }
575 
576 static inline void
578 {
579  vhost_user_vring_t *vring = &vui->vrings[qid];
580  if (vring->kickfd_idx != ~0)
581  {
583  vring->kickfd_idx);
584  unix_file_del (&unix_main, uf);
585  vring->kickfd_idx = ~0;
586  }
587  if (vring->callfd_idx != ~0)
588  {
590  vring->callfd_idx);
591  unix_file_del (&unix_main, uf);
592  vring->callfd_idx = ~0;
593  }
594  if (vring->errfd != -1)
595  {
596  close (vring->errfd);
597  vring->errfd = -1;
598  }
599  vhost_user_vring_init (vui, qid);
600 }
601 
602 static inline void
604 {
605  vnet_main_t *vnm = vnet_get_main ();
606  int q;
607 
609 
610  if (vui->unix_file_index != ~0)
611  {
613  vui->unix_file_index = ~0;
614  }
615 
616  vui->is_up = 0;
617 
618  for (q = 0; q < VHOST_VRING_MAX_N; q++)
619  vhost_user_vring_close (vui, q);
620 
621  unmap_all_mem_regions (vui);
622  DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
623 }
624 
625 #define VHOST_LOG_PAGE 0x1000
628  u64 addr, u64 len, u8 is_host_address)
629 {
630  if (PREDICT_TRUE (vui->log_base_addr == 0
631  || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
632  {
633  return;
634  }
635  if (is_host_address)
636  {
637  addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
638  }
639  if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
640  {
641  DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
642  return;
643  }
644 
646  u64 page = addr / VHOST_LOG_PAGE;
647  while (page * VHOST_LOG_PAGE < addr + len)
648  {
649  ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
650  page++;
651  }
652 }
653 
656 {
657  vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
658 }
659 
660 #define vhost_user_log_dirty_ring(vui, vq, member) \
661  if (PREDICT_FALSE(vq->log_used)) { \
662  vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
663  sizeof(vq->used->member)); \
664  }
665 
666 static clib_error_t *
668 {
669  int n, i;
670  int fd, number_of_fds = 0;
671  int fds[VHOST_MEMORY_MAX_NREGIONS];
672  vhost_user_msg_t msg;
673  struct msghdr mh;
674  struct iovec iov[1];
676  vhost_user_intf_t *vui;
677  struct cmsghdr *cmsg;
678  u8 q;
679  unix_file_t template = { 0 };
680  vnet_main_t *vnm = vnet_get_main ();
681 
682  vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
683 
684  char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
685 
686  memset (&mh, 0, sizeof (mh));
687  memset (control, 0, sizeof (control));
688 
689  for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
690  fds[i] = -1;
691 
692  /* set the payload */
693  iov[0].iov_base = (void *) &msg;
694  iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
695 
696  mh.msg_iov = iov;
697  mh.msg_iovlen = 1;
698  mh.msg_control = control;
699  mh.msg_controllen = sizeof (control);
700 
701  n = recvmsg (uf->file_descriptor, &mh, 0);
702 
703  /* Stop workers to avoid end of the world */
705 
706  if (n != VHOST_USER_MSG_HDR_SZ)
707  {
708  if (n == -1)
709  {
710  DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
711  }
712  else
713  {
714  DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
716  }
717  goto close_socket;
718  }
719 
720  if (mh.msg_flags & MSG_CTRUNC)
721  {
722  DBG_SOCK ("MSG_CTRUNC is set");
723  goto close_socket;
724  }
725 
726  cmsg = CMSG_FIRSTHDR (&mh);
727 
728  if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
729  (cmsg->cmsg_type == SCM_RIGHTS) &&
730  (cmsg->cmsg_len - CMSG_LEN (0) <=
731  VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
732  {
733  number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
734  clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
735  }
736 
737  /* version 1, no reply bit set */
738  if ((msg.flags & 7) != 1)
739  {
740  DBG_SOCK ("malformed message received. closing socket");
741  goto close_socket;
742  }
743 
744  {
745  int rv;
746  rv =
747  read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
748  msg.size);
749  if (rv < 0)
750  {
751  DBG_SOCK ("read failed %s", strerror (errno));
752  goto close_socket;
753  }
754  else if (rv != msg.size)
755  {
756  DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
757  goto close_socket;
758  }
759  }
760 
761  switch (msg.request)
762  {
764  msg.flags |= 4;
765  msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
766  (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
767  (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
768  (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
769  (1ULL << FEAT_VHOST_F_LOG_ALL) |
770  (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
771  (1ULL << FEAT_VIRTIO_NET_F_MQ) |
772  (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
773  (1ULL << FEAT_VIRTIO_F_VERSION_1);
774  msg.u64 &= vui->feature_mask;
775  msg.size = sizeof (msg.u64);
776  DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
777  vui->hw_if_index, msg.u64);
778  break;
779 
781  DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
782  vui->hw_if_index, msg.u64);
783 
784  vui->features = msg.u64;
785 
786  if (vui->features &
787  ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
788  (1ULL << FEAT_VIRTIO_F_VERSION_1)))
789  vui->virtio_net_hdr_sz = 12;
790  else
791  vui->virtio_net_hdr_sz = 10;
792 
793  vui->is_any_layout =
794  (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
795 
798  vui->is_up = 0;
799 
800  /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
801  vhost_user_vring_close(&vui->vrings[q]); */
802 
803  break;
804 
806  DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
807  vui->hw_if_index, msg.memory.nregions);
808 
809  if ((msg.memory.nregions < 1) ||
810  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
811  {
812 
813  DBG_SOCK ("number of mem regions must be between 1 and %i",
814  VHOST_MEMORY_MAX_NREGIONS);
815 
816  goto close_socket;
817  }
818 
819  if (msg.memory.nregions != number_of_fds)
820  {
821  DBG_SOCK ("each memory region must have FD");
822  goto close_socket;
823  }
824  unmap_all_mem_regions (vui);
825  for (i = 0; i < msg.memory.nregions; i++)
826  {
827  clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
828  sizeof (vhost_user_memory_region_t));
829 
830  long page_sz = get_huge_page_size (fds[i]);
831 
832  /* align size to 2M page */
833  ssize_t map_sz = (vui->regions[i].memory_size +
834  vui->regions[i].mmap_offset +
835  page_sz - 1) & ~(page_sz - 1);
836 
837  vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
838  MAP_SHARED, fds[i], 0);
839  vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
840  vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
841  vui->regions[i].memory_size;
842 
843  DBG_SOCK
844  ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
845  "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
846  page_sz);
847 
848  if (vui->region_mmap_addr[i] == MAP_FAILED)
849  {
850  clib_warning ("failed to map memory. errno is %d", errno);
851  goto close_socket;
852  }
853  vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
854  vui->region_mmap_fd[i] = fds[i];
855  }
856  vui->nregions = msg.memory.nregions;
857  break;
858 
860  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
861  vui->hw_if_index, msg.state.index, msg.state.num);
862 
863  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
864  (msg.state.num == 0) || /* it cannot be zero */
865  ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
866  goto close_socket;
867  vui->vrings[msg.state.index].qsz = msg.state.num;
868  break;
869 
871  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
872  vui->hw_if_index, msg.state.index);
873 
874  if (msg.state.index >= VHOST_VRING_MAX_N)
875  {
876  DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
877  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
878  goto close_socket;
879  }
880 
881  if (msg.size < sizeof (msg.addr))
882  {
883  DBG_SOCK ("vhost message is too short (%d < %d)",
884  msg.size, sizeof (msg.addr));
885  goto close_socket;
886  }
887 
888  vui->vrings[msg.state.index].desc = (vring_desc_t *)
889  map_user_mem (vui, msg.addr.desc_user_addr);
890  vui->vrings[msg.state.index].used = (vring_used_t *)
891  map_user_mem (vui, msg.addr.used_user_addr);
892  vui->vrings[msg.state.index].avail = (vring_avail_t *)
893  map_user_mem (vui, msg.addr.avail_user_addr);
894 
895  if ((vui->vrings[msg.state.index].desc == NULL) ||
896  (vui->vrings[msg.state.index].used == NULL) ||
897  (vui->vrings[msg.state.index].avail == NULL))
898  {
899  DBG_SOCK ("failed to map user memory for hw_if_index %d",
900  vui->hw_if_index);
901  goto close_socket;
902  }
903 
904  vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
905  vui->vrings[msg.state.index].log_used =
906  (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
907 
908  /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
909  the ring is initialized in an enabled state. */
910  if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
911  {
912  vui->vrings[msg.state.index].enabled = 1;
913  }
914 
915  vui->vrings[msg.state.index].last_used_idx =
916  vui->vrings[msg.state.index].last_avail_idx =
917  vui->vrings[msg.state.index].used->idx;
918 
919  /* tell driver that we don't want interrupts */
920  vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
921  break;
922 
924  DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
925  break;
926 
928  DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
929  break;
930 
932  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL %d",
933  vui->hw_if_index, msg.u64);
934 
935  q = (u8) (msg.u64 & 0xFF);
936 
937  /* if there is old fd, delete and close it */
938  if (vui->vrings[q].callfd_idx != ~0)
939  {
941  vui->vrings[q].callfd_idx);
942  unix_file_del (&unix_main, uf);
943  vui->vrings[q].callfd_idx = ~0;
944  }
945 
946  if (!(msg.u64 & 0x100))
947  {
948  if (number_of_fds != 1)
949  {
950  DBG_SOCK ("More than one fd received !");
951  goto close_socket;
952  }
953 
954  template.read_function = vhost_user_callfd_read_ready;
955  template.file_descriptor = fds[0];
956  template.private_data =
957  ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
958  vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
959  }
960  else
961  vui->vrings[q].callfd_idx = ~0;
962  break;
963 
965  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK %d",
966  vui->hw_if_index, msg.u64);
967 
968  q = (u8) (msg.u64 & 0xFF);
969 
970  if (vui->vrings[q].kickfd_idx != ~0)
971  {
973  vui->vrings[q].kickfd_idx);
974  unix_file_del (&unix_main, uf);
975  vui->vrings[q].kickfd_idx = ~0;
976  }
977 
978  if (!(msg.u64 & 0x100))
979  {
980  if (number_of_fds != 1)
981  {
982  DBG_SOCK ("More than one fd received !");
983  goto close_socket;
984  }
985 
986  template.read_function = vhost_user_kickfd_read_ready;
987  template.file_descriptor = fds[0];
988  template.private_data =
989  (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
990  q;
991  vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
992  }
993  else
994  {
995  //When no kickfd is set, the queue is initialized as started
996  vui->vrings[q].kickfd_idx = ~0;
997  vui->vrings[q].started = 1;
998  }
999 
1000  break;
1001 
1003  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR %d",
1004  vui->hw_if_index, msg.u64);
1005 
1006  q = (u8) (msg.u64 & 0xFF);
1007 
1008  if (vui->vrings[q].errfd != -1)
1009  close (vui->vrings[q].errfd);
1010 
1011  if (!(msg.u64 & 0x100))
1012  {
1013  if (number_of_fds != 1)
1014  goto close_socket;
1015 
1016  vui->vrings[q].errfd = fds[0];
1017  }
1018  else
1019  vui->vrings[q].errfd = -1;
1020 
1021  break;
1022 
1024  DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1025  vui->hw_if_index, msg.state.index, msg.state.num);
1026 
1027  vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1028  break;
1029 
1031  if (msg.state.index >= VHOST_VRING_MAX_N)
1032  {
1033  DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1034  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1035  goto close_socket;
1036  }
1037 
1038  /*
1039  * Copy last_avail_idx from the vring before closing it because
1040  * closing the vring also initializes the vring last_avail_idx
1041  */
1042  msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1043  msg.flags |= 4;
1044  msg.size = sizeof (msg.state);
1045 
1046  /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1047  vhost_user_vring_close (vui, msg.state.index);
1048  DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1049  vui->hw_if_index, msg.state.index, msg.state.num);
1050  break;
1051 
1052  case VHOST_USER_NONE:
1053  DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1054 
1055  break;
1056 
1058  {
1059  DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1060 
1061  if (msg.size != sizeof (msg.log))
1062  {
1063  DBG_SOCK
1064  ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1065  msg.size, sizeof (msg.log));
1066  goto close_socket;
1067  }
1068 
1069  if (!
1071  {
1072  DBG_SOCK
1073  ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1074  goto close_socket;
1075  }
1076 
1077  fd = fds[0];
1078  /* align size to 2M page */
1079  long page_sz = get_huge_page_size (fd);
1080  ssize_t map_sz =
1081  (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
1082 
1083  vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1084  MAP_SHARED, fd, 0);
1085 
1086  DBG_SOCK
1087  ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1088  map_sz, msg.log.offset, fd, vui->log_base_addr);
1089 
1090  if (vui->log_base_addr == MAP_FAILED)
1091  {
1092  clib_warning ("failed to map memory. errno is %d", errno);
1093  goto close_socket;
1094  }
1095 
1096  vui->log_base_addr += msg.log.offset;
1097  vui->log_size = msg.log.size;
1098 
1099  msg.flags |= 4;
1100  msg.size = sizeof (msg.u64);
1101 
1102  break;
1103  }
1104 
1105  case VHOST_USER_SET_LOG_FD:
1106  DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1107 
1108  break;
1109 
1111  msg.flags |= 4;
1112  msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1113  (1 << VHOST_USER_PROTOCOL_F_MQ);
1114  msg.size = sizeof (msg.u64);
1115  DBG_SOCK
1116  ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - reply 0x%016llx",
1117  vui->hw_if_index, msg.u64);
1118  break;
1119 
1121  DBG_SOCK
1122  ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%016llx",
1123  vui->hw_if_index, msg.u64);
1124 
1125  vui->protocol_features = msg.u64;
1126 
1127  break;
1128 
1130  msg.flags |= 4;
1131  msg.u64 = VHOST_VRING_MAX_N;
1132  msg.size = sizeof (msg.u64);
1133  DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1134  vui->hw_if_index, msg.u64);
1135  break;
1136 
1138  DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1139  vui->hw_if_index, msg.state.num ? "enable" : "disable",
1140  msg.state.index);
1141  if (msg.state.index >= VHOST_VRING_MAX_N)
1142  {
1143  DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1144  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1145  goto close_socket;
1146  }
1147 
1148  vui->vrings[msg.state.index].enabled = msg.state.num;
1149  break;
1150 
1151  default:
1152  DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1153  msg.request);
1154  goto close_socket;
1155  }
1156 
1157  /* if we need to reply */
1158  if (msg.flags & 4)
1159  {
1160  n =
1161  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1162  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1163  {
1164  DBG_SOCK ("could not send message response");
1165  goto close_socket;
1166  }
1167  }
1168 
1171  return 0;
1172 
1173 close_socket:
1177  return 0;
1178 }
1179 
1180 static clib_error_t *
1182 {
1183  vlib_main_t *vm = vlib_get_main ();
1185  vhost_user_intf_t *vui =
1187 
1188  DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1193  return 0;
1194 }
1195 
1196 static clib_error_t *
1198 {
1199  int client_fd, client_len;
1200  struct sockaddr_un client;
1201  unix_file_t template = { 0 };
1203  vhost_user_intf_t *vui;
1204 
1206 
1207  client_len = sizeof (client);
1208  client_fd = accept (uf->file_descriptor,
1209  (struct sockaddr *) &client,
1210  (socklen_t *) & client_len);
1211 
1212  if (client_fd < 0)
1213  return clib_error_return_unix (0, "accept");
1214 
1215  DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1216  template.read_function = vhost_user_socket_read;
1217  template.error_function = vhost_user_socket_error;
1218  template.file_descriptor = client_fd;
1219  template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1220  vui->unix_file_index = unix_file_add (&unix_main, &template);
1221  return 0;
1222 }
1223 
1224 static clib_error_t *
1226 {
1227  clib_error_t *error;
1230 
1231  error = vlib_call_init_function (vm, ip4_init);
1232  if (error)
1233  return error;
1234 
1235  vum->coalesce_frames = 32;
1236  vum->coalesce_time = 1e-3;
1237 
1238  vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1239 
1240  vhost_cpu_t *cpu;
1241  vec_foreach (cpu, vum->cpus)
1242  {
1243  /* This is actually not necessary as validate already zeroes it
1244  * Just keeping the loop here for later because I am lazy. */
1245  cpu->rx_buffers_len = 0;
1246  }
1247 
1248  vum->random = random_default_seed ();
1249 
1251 
1252  return 0;
1253 }
1254 
1256 
1257 static u8 *
1258 format_vhost_trace (u8 * s, va_list * va)
1259 {
1260  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1261  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1262  CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1264  vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1266  t->device_index);
1267 
1269 
1270  uword indent = format_get_indent (s);
1271 
1272  s = format (s, "%U %U queue %d\n", format_white_space, indent,
1273  format_vnet_sw_interface_name, vnm, sw, t->qid);
1274 
1275  s = format (s, "%U virtio flags:\n", format_white_space, indent);
1276 #define _(n,i,st) \
1277  if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1278  s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1280 #undef _
1281  s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1282  format_white_space, indent, t->first_desc_len);
1283 
1284  s = format (s, "%U flags 0x%02x gso_type %u\n",
1285  format_white_space, indent,
1286  t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1287 
1288  if (vui->virtio_net_hdr_sz == 12)
1289  s = format (s, "%U num_buff %u",
1290  format_white_space, indent, t->hdr.num_buffers);
1291 
1292  return s;
1293 }
1294 
1295 void
1297  vhost_user_intf_t * vui, u16 qid,
1298  vlib_buffer_t * b, vhost_user_vring_t * txvq)
1299 {
1301  u32 qsz_mask = txvq->qsz - 1;
1302  u32 last_avail_idx = txvq->last_avail_idx;
1303  u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1304  vring_desc_t *hdr_desc = 0;
1305  virtio_net_hdr_mrg_rxbuf_t *hdr;
1306  u32 hint = 0;
1307 
1308  memset (t, 0, sizeof (*t));
1309  t->device_index = vui - vum->vhost_user_interfaces;
1310  t->qid = qid;
1311 
1312  hdr_desc = &txvq->desc[desc_current];
1313  if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1314  {
1315  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1316  /* Header is the first here */
1317  hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1318  }
1319  if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1320  {
1321  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1322  }
1323  if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1324  !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1325  {
1326  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1327  }
1328 
1329  t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1330 
1331  if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1332  {
1333  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1334  }
1335  else
1336  {
1337  u32 len = vui->virtio_net_hdr_sz;
1338  memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1339  }
1340 }
1341 
1342 static inline void
1344 {
1346  u64 x = 1;
1347  int fd = UNIX_GET_FD (vq->callfd_idx);
1348  int rv;
1349 
1350  rv = write (fd, &x, sizeof (x));
1351  if (rv <= 0)
1352  {
1354  ("Error: Could not write to unix socket for callfd %d", fd);
1355  return;
1356  }
1357 
1358  vq->n_since_last_int = 0;
1359  vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1360 }
1361 
1364  u16 copy_len, u32 * map_hint)
1365 {
1366  void *src0, *src1, *src2, *src3;
1367  if (PREDICT_TRUE (copy_len >= 4))
1368  {
1369  if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1370  return 1;
1371  if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1372  return 1;
1373 
1374  while (PREDICT_TRUE (copy_len >= 4))
1375  {
1376  src0 = src2;
1377  src1 = src3;
1378 
1379  if (PREDICT_FALSE
1380  (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1381  return 1;
1382  if (PREDICT_FALSE
1383  (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1384  return 1;
1385 
1386  CLIB_PREFETCH (src2, 64, LOAD);
1387  CLIB_PREFETCH (src3, 64, LOAD);
1388 
1389  clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1390  clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1391  copy_len -= 2;
1392  cpy += 2;
1393  }
1394  }
1395  while (copy_len)
1396  {
1397  if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1398  return 1;
1399  clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1400  copy_len -= 1;
1401  cpy += 1;
1402  }
1403  return 0;
1404 }
1405 
1406 /**
1407  * Try to discard packets from the tx ring (VPP RX path).
1408  * Returns the number of discarded packets.
1409  */
1410 u32
1412  vhost_user_intf_t * vui,
1413  vhost_user_vring_t * txvq, u32 discard_max)
1414 {
1415  /*
1416  * On the RX side, each packet corresponds to one descriptor
1417  * (it is the same whether it is a shallow descriptor, chained, or indirect).
1418  * Therefore, discarding a packet is like discarding a descriptor.
1419  */
1420  u32 discarded_packets = 0;
1421  u32 avail_idx = txvq->avail->idx;
1422  u16 qsz_mask = txvq->qsz - 1;
1423  while (discarded_packets != discard_max)
1424  {
1425  if (avail_idx == txvq->last_avail_idx)
1426  goto out;
1427 
1428  u16 desc_chain_head =
1429  txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1430  txvq->last_avail_idx++;
1431  txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1432  txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1433  vhost_user_log_dirty_ring (vui, txvq,
1434  ring[txvq->last_used_idx & qsz_mask]);
1435  txvq->last_used_idx++;
1436  discarded_packets++;
1437  }
1438 
1439 out:
1441  txvq->used->idx = txvq->last_used_idx;
1442  vhost_user_log_dirty_ring (vui, txvq, idx);
1443  return discarded_packets;
1444 }
1445 
1446 /*
1447  * In case of overflow, we need to rewind the array of allocated buffers.
1448  */
1449 static void
1451  vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1452 {
1453  u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1454  vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1455  b_current->current_length = 0;
1456  b_current->flags = 0;
1457  while (b_current != b_head)
1458  {
1459  cpu->rx_buffers_len++;
1460  bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1461  b_current = vlib_get_buffer (vm, bi_current);
1462  b_current->current_length = 0;
1463  b_current->flags = 0;
1464  }
1465  cpu->rx_buffers_len++;
1466 }
1467 
1468 static u32
1470  vhost_user_main_t * vum,
1471  vhost_user_intf_t * vui,
1472  u16 qid, vlib_node_runtime_t * node,
1474 {
1475  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1476  u16 n_rx_packets = 0;
1477  u32 n_rx_bytes = 0;
1478  u16 n_left;
1479  u32 n_left_to_next, *to_next;
1481  u32 n_trace = vlib_get_trace_count (vm, node);
1482  u16 qsz_mask;
1483  u32 map_hint = 0;
1484  u16 thread_index = vlib_get_thread_index ();
1485  u16 copy_len = 0;
1486 
1487  {
1488  /* do we have pending interrupts ? */
1489  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1490  f64 now = vlib_time_now (vm);
1491 
1492  if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1493  vhost_user_send_call (vm, txvq);
1494 
1495  if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1496  vhost_user_send_call (vm, rxvq);
1497  }
1498 
1499  /*
1500  * For adaptive mode, it is optimized to reduce interrupts.
1501  * If the scheduler switches the input node to polling due
1502  * to burst of traffic, we tell the driver no interrupt.
1503  * When the traffic subsides, the scheduler switches the node back to
1504  * interrupt mode. We must tell the driver we want interrupt.
1505  */
1507  {
1508  if ((node->flags &
1510  !(node->flags &
1512  /* Tell driver we want notification */
1513  txvq->used->flags = 0;
1514  else
1515  /* Tell driver we don't want notification */
1516  txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1517  }
1518 
1519  if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1520  return 0;
1521 
1522  n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1523 
1524  /* nothing to do */
1525  if (PREDICT_FALSE (n_left == 0))
1526  return 0;
1527 
1528  if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1529  {
1530  /*
1531  * Discard input packet if interface is admin down or vring is not
1532  * enabled.
1533  * "For example, for a networking device, in the disabled state
1534  * client must not supply any new RX packets, but must process
1535  * and discard any TX packets."
1536  */
1537  vhost_user_rx_discard_packet (vm, vui, txvq,
1539  return 0;
1540  }
1541 
1542  if (PREDICT_FALSE (n_left == txvq->qsz))
1543  {
1544  /*
1545  * Informational error logging when VPP is not
1546  * receiving packets fast enough.
1547  */
1548  vlib_error_count (vm, node->node_index,
1549  VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1550  }
1551 
1552  qsz_mask = txvq->qsz - 1;
1553 
1554  if (n_left > VLIB_FRAME_SIZE)
1555  n_left = VLIB_FRAME_SIZE;
1556 
1557  /*
1558  * For small packets (<2kB), we will not need more than one vlib buffer
1559  * per packet. In case packets are bigger, we will just yeld at some point
1560  * in the loop and come back later. This is not an issue as for big packet,
1561  * processing cost really comes from the memory copy.
1562  * The assumption is that even big packets will fit in 40 buffers.
1563  */
1564  if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 ||
1565  vum->cpus[thread_index].rx_buffers_len < 40))
1566  {
1567  u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1568  vum->cpus[thread_index].rx_buffers_len +=
1570  vum->cpus[thread_index].rx_buffers +
1571  curr_len,
1572  VHOST_USER_RX_BUFFERS_N - curr_len,
1574 
1575  if (PREDICT_FALSE
1576  (vum->cpus[thread_index].rx_buffers_len <
1578  {
1579  /* In case of buffer starvation, discard some packets from the queue
1580  * and log the event.
1581  * We keep doing best effort for the remaining packets. */
1582  u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1583  n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
1584  flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1585 
1586  n_left -= flush;
1588  interface_main.sw_if_counters +
1591  vui->sw_if_index, flush);
1592 
1594  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1595  }
1596  }
1597 
1598  while (n_left > 0)
1599  {
1600  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1601 
1602  while (n_left > 0 && n_left_to_next > 0)
1603  {
1604  vlib_buffer_t *b_head, *b_current;
1605  u32 bi_current;
1606  u16 desc_current;
1607  u32 desc_data_offset;
1608  vring_desc_t *desc_table = txvq->desc;
1609 
1610  if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
1611  {
1612  /* Not enough rx_buffers
1613  * Note: We yeld on 1 so we don't need to do an additional
1614  * check for the next buffer prefetch.
1615  */
1616  n_left = 0;
1617  break;
1618  }
1619 
1620  desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1621  vum->cpus[thread_index].rx_buffers_len--;
1622  bi_current = (vum->cpus[thread_index].rx_buffers)
1623  [vum->cpus[thread_index].rx_buffers_len];
1624  b_head = b_current = vlib_get_buffer (vm, bi_current);
1625  to_next[0] = bi_current; //We do that now so we can forget about bi_current
1626  to_next++;
1627  n_left_to_next--;
1628 
1630  (vum->
1631  cpus[thread_index].rx_buffers)
1632  [vum->cpus[thread_index].
1633  rx_buffers_len - 1], LOAD);
1634 
1635  /* Just preset the used descriptor id and length for later */
1636  txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1637  txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1638  vhost_user_log_dirty_ring (vui, txvq,
1639  ring[txvq->last_used_idx & qsz_mask]);
1640 
1641  /* The buffer should already be initialized */
1644 
1645  if (PREDICT_FALSE (n_trace))
1646  {
1647  //TODO: next_index is not exactly known at that point
1648  vlib_trace_buffer (vm, node, next_index, b_head,
1649  /* follow_chain */ 0);
1650  vhost_trace_t *t0 =
1651  vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1652  vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1653  n_trace--;
1654  vlib_set_trace_count (vm, node, n_trace);
1655  }
1656 
1657  /* This depends on the setup but is very consistent
1658  * So I think the CPU branch predictor will make a pretty good job
1659  * at optimizing the decision. */
1660  if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1661  {
1662  desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1663  &map_hint);
1664  desc_current = 0;
1665  if (PREDICT_FALSE (desc_table == 0))
1666  {
1667  vlib_error_count (vm, node->node_index,
1668  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1669  goto out;
1670  }
1671  }
1672 
1673  if (PREDICT_TRUE (vui->is_any_layout) ||
1674  (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1675  {
1676  /* ANYLAYOUT or single buffer */
1677  desc_data_offset = vui->virtio_net_hdr_sz;
1678  }
1679  else
1680  {
1681  /* CSR case without ANYLAYOUT, skip 1st buffer */
1682  desc_data_offset = desc_table[desc_current].len;
1683  }
1684 
1685  while (1)
1686  {
1687  /* Get more input if necessary. Or end of packet. */
1688  if (desc_data_offset == desc_table[desc_current].len)
1689  {
1690  if (PREDICT_FALSE (desc_table[desc_current].flags &
1691  VIRTQ_DESC_F_NEXT))
1692  {
1693  desc_current = desc_table[desc_current].next;
1694  desc_data_offset = 0;
1695  }
1696  else
1697  {
1698  goto out;
1699  }
1700  }
1701 
1702  /* Get more output if necessary. Or end of packet. */
1703  if (PREDICT_FALSE
1704  (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1705  {
1706  if (PREDICT_FALSE
1707  (vum->cpus[thread_index].rx_buffers_len == 0))
1708  {
1709  /* Cancel speculation */
1710  to_next--;
1711  n_left_to_next++;
1712 
1713  /*
1714  * Checking if there are some left buffers.
1715  * If not, just rewind the used buffers and stop.
1716  * Note: Scheduled copies are not cancelled. This is
1717  * not an issue as they would still be valid. Useless,
1718  * but valid.
1719  */
1721  &vum->cpus
1722  [thread_index],
1723  b_head);
1724  n_left = 0;
1725  goto stop;
1726  }
1727 
1728  /* Get next output */
1729  vum->cpus[thread_index].rx_buffers_len--;
1730  u32 bi_next =
1731  (vum->cpus[thread_index].rx_buffers)[vum->cpus
1732  [thread_index].rx_buffers_len];
1733  b_current->next_buffer = bi_next;
1734  b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1735  bi_current = bi_next;
1736  b_current = vlib_get_buffer (vm, bi_current);
1737  }
1738 
1739  /* Prepare a copy order executed later for the data */
1740  vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
1741  copy_len++;
1742  u32 desc_data_l =
1743  desc_table[desc_current].len - desc_data_offset;
1744  cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1745  cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1746  cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
1747  b_current->current_length);
1748  cpy->src = desc_table[desc_current].addr + desc_data_offset;
1749 
1750  desc_data_offset += cpy->len;
1751 
1752  b_current->current_length += cpy->len;
1754  }
1755 
1756  out:
1757  CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1758 
1759  n_rx_bytes += b_head->total_length_not_including_first_buffer;
1760  n_rx_packets++;
1761 
1763  b_head->current_length;
1764 
1765  /* consume the descriptor and return it as used */
1766  txvq->last_avail_idx++;
1767  txvq->last_used_idx++;
1768 
1770 
1771  vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1772  vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1773  b_head->error = 0;
1774 
1775  {
1777 
1778  /* redirect if feature path enabled */
1780  b_head);
1781 
1782  u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1783  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1784  to_next, n_left_to_next,
1785  bi, next0);
1786  }
1787 
1788  n_left--;
1789 
1790  /*
1791  * Although separating memory copies from virtio ring parsing
1792  * is beneficial, we can offer to perform the copies from time
1793  * to time in order to free some space in the ring.
1794  */
1795  if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1796  {
1797  if (PREDICT_FALSE
1798  (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1799  copy_len, &map_hint)))
1800  {
1801  vlib_error_count (vm, node->node_index,
1802  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1803  }
1804  copy_len = 0;
1805 
1806  /* give buffers back to driver */
1808  txvq->used->idx = txvq->last_used_idx;
1809  vhost_user_log_dirty_ring (vui, txvq, idx);
1810  }
1811  }
1812  stop:
1813  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1814  }
1815 
1816  /* Do the memory copies */
1817  if (PREDICT_FALSE
1818  (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1819  copy_len, &map_hint)))
1820  {
1821  vlib_error_count (vm, node->node_index,
1822  VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1823  }
1824 
1825  /* give buffers back to driver */
1827  txvq->used->idx = txvq->last_used_idx;
1828  vhost_user_log_dirty_ring (vui, txvq, idx);
1829 
1830  /* interrupt (call) handling */
1831  if ((txvq->callfd_idx != ~0) &&
1832  !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1833  {
1834  txvq->n_since_last_int += n_rx_packets;
1835 
1836  if (txvq->n_since_last_int > vum->coalesce_frames)
1837  vhost_user_send_call (vm, txvq);
1838  }
1839 
1840  /* increase rx counters */
1844  vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1845 
1846  vnet_device_increment_rx_packets (thread_index, n_rx_packets);
1847 
1848  return n_rx_packets;
1849 }
1850 
1851 static uword
1853  vlib_node_runtime_t * node, vlib_frame_t * f)
1854 {
1856  uword n_rx_packets = 0;
1857  vhost_user_intf_t *vui;
1861 
1863  {
1864  if (clib_smp_swap (&dq->interrupt_pending, 0) ||
1865  (node->state == VLIB_NODE_STATE_POLLING))
1866  {
1867  vui =
1868  pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
1869  n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
1870  dq->mode);
1871  }
1872  }
1873 
1874  return n_rx_packets;
1875 }
1876 
1877 /* *INDENT-OFF* */
1879  .function = vhost_user_input,
1880  .type = VLIB_NODE_TYPE_INPUT,
1881  .name = "vhost-user-input",
1882  .sibling_of = "device-input",
1883 
1884  /* Will be enabled if/when hardware is detected. */
1885  .state = VLIB_NODE_STATE_DISABLED,
1886 
1887  .format_buffer = format_ethernet_header_with_length,
1888  .format_trace = format_vhost_trace,
1889 
1890  .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1891  .error_strings = vhost_user_input_func_error_strings,
1892 };
1893 
1895 /* *INDENT-ON* */
1896 
1897 
1898 void
1900  vhost_user_intf_t * vui, u16 qid,
1901  vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1902 {
1904  u32 qsz_mask = rxvq->qsz - 1;
1905  u32 last_avail_idx = rxvq->last_avail_idx;
1906  u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
1907  vring_desc_t *hdr_desc = 0;
1908  u32 hint = 0;
1909 
1910  memset (t, 0, sizeof (*t));
1911  t->device_index = vui - vum->vhost_user_interfaces;
1912  t->qid = qid;
1913 
1914  hdr_desc = &rxvq->desc[desc_current];
1915  if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1916  {
1917  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1918  /* Header is the first here */
1919  hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1920  }
1921  if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1922  {
1923  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1924  }
1925  if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1926  !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1927  {
1928  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1929  }
1930 
1931  t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1932 }
1933 
1936  u16 copy_len, u32 * map_hint)
1937 {
1938  void *dst0, *dst1, *dst2, *dst3;
1939  if (PREDICT_TRUE (copy_len >= 4))
1940  {
1941  if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1942  return 1;
1943  if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1944  return 1;
1945  while (PREDICT_TRUE (copy_len >= 4))
1946  {
1947  dst0 = dst2;
1948  dst1 = dst3;
1949 
1950  if (PREDICT_FALSE
1951  (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1952  return 1;
1953  if (PREDICT_FALSE
1954  (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1955  return 1;
1956 
1957  CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1958  CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1959 
1960  clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1961  clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1962 
1963  vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1964  vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1965  copy_len -= 2;
1966  cpy += 2;
1967  }
1968  }
1969  while (copy_len)
1970  {
1971  if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1972  return 1;
1973  clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1974  vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1975  copy_len -= 1;
1976  cpy += 1;
1977  }
1978  return 0;
1979 }
1980 
1981 
1982 static uword
1984  vlib_node_runtime_t * node, vlib_frame_t * frame)
1985 {
1986  u32 *buffers = vlib_frame_args (frame);
1987  u32 n_left = frame->n_vectors;
1989  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1990  vhost_user_intf_t *vui =
1992  u32 qid = ~0;
1993  vhost_user_vring_t *rxvq;
1994  u16 qsz_mask;
1995  u8 error;
1996  u32 thread_index = vlib_get_thread_index ();
1997  u32 map_hint = 0;
1998  u8 retry = 8;
1999  u16 copy_len;
2000  u16 tx_headers_len;
2001 
2002  if (PREDICT_FALSE (!vui->admin_up))
2003  {
2004  error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2005  goto done3;
2006  }
2007 
2008  if (PREDICT_FALSE (!vui->is_up))
2009  {
2010  error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
2011  goto done3;
2012  }
2013 
2014  qid =
2016  (vui->per_cpu_tx_qid, thread_index));
2017  rxvq = &vui->vrings[qid];
2018  if (PREDICT_FALSE (vui->use_tx_spinlock))
2019  vhost_user_vring_lock (vui, qid);
2020 
2021  qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
2022 
2023 retry:
2024  error = VHOST_USER_TX_FUNC_ERROR_NONE;
2025  tx_headers_len = 0;
2026  copy_len = 0;
2027  while (n_left > 0)
2028  {
2029  vlib_buffer_t *b0, *current_b0;
2030  u16 desc_head, desc_index, desc_len;
2031  vring_desc_t *desc_table;
2032  uword buffer_map_addr;
2033  u32 buffer_len;
2034  u16 bytes_left;
2035 
2036  if (PREDICT_TRUE (n_left > 1))
2037  vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2038 
2039  b0 = vlib_get_buffer (vm, buffers[0]);
2040 
2042  {
2043  vum->cpus[thread_index].current_trace =
2044  vlib_add_trace (vm, node, b0,
2045  sizeof (*vum->cpus[thread_index].current_trace));
2046  vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
2047  vui, qid / 2, b0, rxvq);
2048  }
2049 
2050  if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2051  {
2052  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2053  goto done;
2054  }
2055 
2056  desc_table = rxvq->desc;
2057  desc_head = desc_index =
2058  rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2059 
2060  /* Go deeper in case of indirect descriptor
2061  * I don't know of any driver providing indirect for RX. */
2062  if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2063  {
2064  if (PREDICT_FALSE
2065  (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2066  {
2067  error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2068  goto done;
2069  }
2070  if (PREDICT_FALSE
2071  (!(desc_table =
2072  map_guest_mem (vui, rxvq->desc[desc_index].addr,
2073  &map_hint))))
2074  {
2075  error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2076  goto done;
2077  }
2078  desc_index = 0;
2079  }
2080 
2081  desc_len = vui->virtio_net_hdr_sz;
2082  buffer_map_addr = desc_table[desc_index].addr;
2083  buffer_len = desc_table[desc_index].len;
2084 
2085  {
2086  // Get a header from the header array
2087  virtio_net_hdr_mrg_rxbuf_t *hdr =
2088  &vum->cpus[thread_index].tx_headers[tx_headers_len];
2089  tx_headers_len++;
2090  hdr->hdr.flags = 0;
2091  hdr->hdr.gso_type = 0;
2092  hdr->num_buffers = 1; //This is local, no need to check
2093 
2094  // Prepare a copy order executed later for the header
2095  vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2096  copy_len++;
2097  cpy->len = vui->virtio_net_hdr_sz;
2098  cpy->dst = buffer_map_addr;
2099  cpy->src = (uword) hdr;
2100  }
2101 
2102  buffer_map_addr += vui->virtio_net_hdr_sz;
2103  buffer_len -= vui->virtio_net_hdr_sz;
2104  bytes_left = b0->current_length;
2105  current_b0 = b0;
2106  while (1)
2107  {
2108  if (buffer_len == 0)
2109  { //Get new output
2110  if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2111  {
2112  //Next one is chained
2113  desc_index = desc_table[desc_index].next;
2114  buffer_map_addr = desc_table[desc_index].addr;
2115  buffer_len = desc_table[desc_index].len;
2116  }
2117  else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2118  {
2119  virtio_net_hdr_mrg_rxbuf_t *hdr =
2120  &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2121 
2122  //Move from available to used buffer
2123  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2124  desc_head;
2125  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2126  desc_len;
2127  vhost_user_log_dirty_ring (vui, rxvq,
2128  ring[rxvq->last_used_idx &
2129  qsz_mask]);
2130 
2131  rxvq->last_avail_idx++;
2132  rxvq->last_used_idx++;
2133  hdr->num_buffers++;
2134  desc_len = 0;
2135 
2136  if (PREDICT_FALSE
2137  (rxvq->last_avail_idx == rxvq->avail->idx))
2138  {
2139  //Dequeue queued descriptors for this packet
2140  rxvq->last_used_idx -= hdr->num_buffers - 1;
2141  rxvq->last_avail_idx -= hdr->num_buffers - 1;
2142  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2143  goto done;
2144  }
2145 
2146  desc_table = rxvq->desc;
2147  desc_head = desc_index =
2148  rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2149  if (PREDICT_FALSE
2150  (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2151  {
2152  //It is seriously unlikely that a driver will put indirect descriptor
2153  //after non-indirect descriptor.
2154  if (PREDICT_FALSE
2155  (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2156  {
2157  error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2158  goto done;
2159  }
2160  if (PREDICT_FALSE
2161  (!(desc_table =
2162  map_guest_mem (vui,
2163  rxvq->desc[desc_index].addr,
2164  &map_hint))))
2165  {
2166  error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2167  goto done;
2168  }
2169  desc_index = 0;
2170  }
2171  buffer_map_addr = desc_table[desc_index].addr;
2172  buffer_len = desc_table[desc_index].len;
2173  }
2174  else
2175  {
2176  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2177  goto done;
2178  }
2179  }
2180 
2181  {
2182  vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2183  copy_len++;
2184  cpy->len = bytes_left;
2185  cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2186  cpy->dst = buffer_map_addr;
2187  cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2188  current_b0->current_length - bytes_left;
2189 
2190  bytes_left -= cpy->len;
2191  buffer_len -= cpy->len;
2192  buffer_map_addr += cpy->len;
2193  desc_len += cpy->len;
2194 
2195  CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2196  }
2197 
2198  // Check if vlib buffer has more data. If not, get more or break.
2199  if (PREDICT_TRUE (!bytes_left))
2200  {
2201  if (PREDICT_FALSE
2202  (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2203  {
2204  current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2205  bytes_left = current_b0->current_length;
2206  }
2207  else
2208  {
2209  //End of packet
2210  break;
2211  }
2212  }
2213  }
2214 
2215  //Move from available to used ring
2216  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2217  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2218  vhost_user_log_dirty_ring (vui, rxvq,
2219  ring[rxvq->last_used_idx & qsz_mask]);
2220  rxvq->last_avail_idx++;
2221  rxvq->last_used_idx++;
2222 
2224  {
2225  vum->cpus[thread_index].current_trace->hdr =
2226  vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2227  }
2228 
2229  n_left--; //At the end for error counting when 'goto done' is invoked
2230 
2231  /*
2232  * Do the copy periodically to prevent
2233  * vum->cpus[thread_index].copy array overflow and corrupt memory
2234  */
2235  if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
2236  {
2237  if (PREDICT_FALSE
2238  (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
2239  copy_len, &map_hint)))
2240  {
2241  vlib_error_count (vm, node->node_index,
2242  VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
2243  }
2244  copy_len = 0;
2245 
2246  /* give buffers back to driver */
2248  rxvq->used->idx = rxvq->last_used_idx;
2249  vhost_user_log_dirty_ring (vui, rxvq, idx);
2250  }
2251  buffers++;
2252  }
2253 
2254 done:
2255  //Do the memory copies
2256  if (PREDICT_FALSE
2257  (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
2258  copy_len, &map_hint)))
2259  {
2260  vlib_error_count (vm, node->node_index,
2261  VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
2262  }
2263 
2265  rxvq->used->idx = rxvq->last_used_idx;
2266  vhost_user_log_dirty_ring (vui, rxvq, idx);
2267 
2268  /*
2269  * When n_left is set, error is always set to something too.
2270  * In case error is due to lack of remaining buffers, we go back up and
2271  * retry.
2272  * The idea is that it is better to waste some time on packets
2273  * that have been processed already than dropping them and get
2274  * more fresh packets with a good likelyhood that they will be dropped too.
2275  * This technique also gives more time to VM driver to pick-up packets.
2276  * In case the traffic flows from physical to virtual interfaces, this
2277  * technique will end-up leveraging the physical NIC buffer in order to
2278  * absorb the VM's CPU jitter.
2279  */
2280  if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2281  {
2282  retry--;
2283  goto retry;
2284  }
2285 
2286  /* interrupt (call) handling */
2287  if ((rxvq->callfd_idx != ~0) &&
2288  !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
2289  {
2290  rxvq->n_since_last_int += frame->n_vectors - n_left;
2291 
2292  if (rxvq->n_since_last_int > vum->coalesce_frames)
2293  vhost_user_send_call (vm, rxvq);
2294  }
2295 
2296  vhost_user_vring_unlock (vui, qid);
2297 
2298 done3:
2299  if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2300  {
2301  vlib_error_count (vm, node->node_index, error, n_left);
2305  thread_index, vui->sw_if_index, n_left);
2306  }
2307 
2308  vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2309  return frame->n_vectors;
2310 }
2311 
2312 static uword
2315 {
2316  vhost_user_intf_t *vui;
2317  f64 timeout = 3153600000.0 /* 100 years */ ;
2318  uword event_type, *event_data = 0;
2320  u16 *queue;
2321  f64 now, poll_time_remaining;
2322  f64 next_timeout;
2323  u8 stop_timer = 0;
2324 
2325  while (1)
2326  {
2327  poll_time_remaining =
2329  event_type = vlib_process_get_events (vm, &event_data);
2330  vec_reset_length (event_data);
2331 
2332  /*
2333  * Use the remaining timeout if it is less than coalesce time to avoid
2334  * resetting the existing timer in the middle of expiration
2335  */
2336  timeout = poll_time_remaining;
2337  if (vlib_process_suspend_time_is_zero (timeout) ||
2338  (timeout > vum->coalesce_time))
2339  timeout = vum->coalesce_time;
2340 
2341  now = vlib_time_now (vm);
2342  switch (event_type)
2343  {
2345  stop_timer = 1;
2346  break;
2347 
2349  stop_timer = 0;
2350  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2351  break;
2352  /* fall through */
2353 
2354  case ~0:
2355  /* *INDENT-OFF* */
2356  pool_foreach (vui, vum->vhost_user_interfaces, {
2357  next_timeout = timeout;
2358  vec_foreach (queue, vui->rx_queues)
2359  {
2360  vhost_user_vring_t *rxvq =
2361  &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
2362  vhost_user_vring_t *txvq =
2363  &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2364 
2365  if (txvq->n_since_last_int)
2366  {
2367  if (now >= txvq->int_deadline)
2368  vhost_user_send_call (vm, txvq);
2369  else
2370  next_timeout = txvq->int_deadline - now;
2371  }
2372 
2373  if (rxvq->n_since_last_int)
2374  {
2375  if (now >= rxvq->int_deadline)
2376  vhost_user_send_call (vm, rxvq);
2377  else
2378  next_timeout = rxvq->int_deadline - now;
2379  }
2380 
2381  if ((next_timeout < timeout) && (next_timeout > 0.0))
2382  timeout = next_timeout;
2383  }
2384  });
2385  /* *INDENT-ON* */
2386  break;
2387 
2388  default:
2389  clib_warning ("BUG: unhandled event type %d", event_type);
2390  break;
2391  }
2392  /* No less than 1 millisecond */
2393  if (timeout < 1e-3)
2394  timeout = 1e-3;
2395  if (stop_timer)
2396  timeout = 3153600000.0;
2397  }
2398  return 0;
2399 }
2400 
2401 /* *INDENT-OFF* */
2404  .type = VLIB_NODE_TYPE_PROCESS,
2405  .name = "vhost-user-send-interrupt-process",
2406 };
2407 /* *INDENT-ON* */
2408 
2409 static clib_error_t *
2411  u32 qid, vnet_hw_interface_rx_mode mode)
2412 {
2413  vlib_main_t *vm = vnm->vlib_main;
2414  vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2416  vhost_user_intf_t *vui =
2418  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
2419 
2420  if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2422  {
2424  {
2425  vum->ifq_count++;
2426  // Start the timer if this is the first encounter on interrupt
2427  // interface/queue
2428  if ((vum->ifq_count == 1) &&
2429  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2433  }
2434  }
2435  else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2436  {
2437  if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2439  vum->ifq_count)
2440  {
2441  vum->ifq_count--;
2442  // Stop the timer if there is no more interrupt interface/queue
2443  if ((vum->ifq_count == 0) &&
2444  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2448  }
2449  }
2450 
2451  txvq->mode = mode;
2453  txvq->used->flags = VRING_USED_F_NO_NOTIFY;
2454  else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
2456  txvq->used->flags = 0;
2457  else
2458  {
2459  clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
2460  hw_if_index, qid);
2461  return clib_error_return (0, "unsupported");
2462  }
2463 
2464  return 0;
2465 }
2466 
2467 static clib_error_t *
2469  u32 flags)
2470 {
2471  vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2472  uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2474  vhost_user_intf_t *vui =
2476 
2477  vui->admin_up = is_up;
2478 
2479  if (is_up)
2482 
2483  return /* no error */ 0;
2484 }
2485 
2486 /* *INDENT-OFF* */
2487 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2488  .name = "vhost-user",
2489  .tx_function = vhost_user_tx,
2490  .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2491  .tx_function_error_strings = vhost_user_tx_func_error_strings,
2492  .format_device_name = format_vhost_user_interface_name,
2493  .name_renumber = vhost_user_name_renumber,
2494  .admin_up_down_function = vhost_user_interface_admin_up_down,
2495  .rx_mode_change_function = vhost_user_interface_rx_mode_change,
2496  .format_tx_trace = format_vhost_trace,
2497 };
2498 
2500  vhost_user_tx)
2501 /* *INDENT-ON* */
2502 
2503 static uword
2504 vhost_user_process (vlib_main_t * vm,
2506 {
2508  vhost_user_intf_t *vui;
2509  struct sockaddr_un sun;
2510  int sockfd;
2511  unix_file_t template = { 0 };
2512  f64 timeout = 3153600000.0 /* 100 years */ ;
2513  uword *event_data = 0;
2514 
2515  sockfd = -1;
2516  sun.sun_family = AF_UNIX;
2518  template.error_function = vhost_user_socket_error;
2519 
2520  while (1)
2521  {
2523  vlib_process_get_events (vm, &event_data);
2524  vec_reset_length (event_data);
2525 
2526  timeout = 3.0;
2527 
2528  /* *INDENT-OFF* */
2529  pool_foreach (vui, vum->vhost_user_interfaces, {
2530 
2531  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2532  if (vui->unix_file_index == ~0)
2533  {
2534  if ((sockfd < 0) &&
2535  ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2536  {
2537  /*
2538  * 1st time error or new error for this interface,
2539  * spit out the message and record the error
2540  */
2541  if (!vui->sock_errno || (vui->sock_errno != errno))
2542  {
2543  clib_unix_warning
2544  ("Error: Could not open unix socket for %s",
2545  vui->sock_filename);
2546  vui->sock_errno = errno;
2547  }
2548  continue;
2549  }
2550 
2551  /* try to connect */
2552  strncpy (sun.sun_path, (char *) vui->sock_filename,
2553  sizeof (sun.sun_path) - 1);
2554 
2555  /* Avoid hanging VPP if the other end does not accept */
2556  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2557  clib_unix_warning ("fcntl");
2558 
2559  if (connect (sockfd, (struct sockaddr *) &sun,
2560  sizeof (struct sockaddr_un)) == 0)
2561  {
2562  /* Set the socket to blocking as it was before */
2563  if (fcntl(sockfd, F_SETFL, 0) < 0)
2564  clib_unix_warning ("fcntl2");
2565 
2566  vui->sock_errno = 0;
2567  template.file_descriptor = sockfd;
2568  template.private_data =
2569  vui - vhost_user_main.vhost_user_interfaces;
2570  vui->unix_file_index = unix_file_add (&unix_main, &template);
2571 
2572  /* This sockfd is considered consumed */
2573  sockfd = -1;
2574  }
2575  else
2576  {
2577  vui->sock_errno = errno;
2578  }
2579  }
2580  else
2581  {
2582  /* check if socket is alive */
2583  int error = 0;
2584  socklen_t len = sizeof (error);
2585  int fd = UNIX_GET_FD(vui->unix_file_index);
2586  int retval =
2587  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2588 
2589  if (retval)
2590  {
2591  DBG_SOCK ("getsockopt returned %d", retval);
2592  vhost_user_if_disconnect (vui);
2593  }
2594  }
2595  }
2596  });
2597  /* *INDENT-ON* */
2598  }
2599  return 0;
2600 }
2601 
2602 /* *INDENT-OFF* */
2604  .function = vhost_user_process,
2605  .type = VLIB_NODE_TYPE_PROCESS,
2606  .name = "vhost-user-process",
2607 };
2608 /* *INDENT-ON* */
2609 
2610 /**
2611  * Disables and reset interface structure.
2612  * It can then be either init again, or removed from used interfaces.
2613  */
2614 static void
2616 {
2617  int q;
2619 
2620  // disconnect interface sockets
2623 
2624  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2625  {
2626  clib_mem_free ((void *) vui->vring_locks[q]);
2627  }
2628 
2629  if (vui->unix_server_index != ~0)
2630  {
2631  //Close server socket
2633  vui->unix_server_index);
2634  unix_file_del (&unix_main, uf);
2635  vui->unix_server_index = ~0;
2636  unlink (vui->sock_filename);
2637  }
2638 
2640  &vui->if_index);
2641 }
2642 
2643 int
2645 {
2647  vhost_user_intf_t *vui;
2648  int rv = 0;
2649  vnet_hw_interface_t *hwif;
2650  u16 *queue;
2651 
2652  if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2653  hwif->dev_class_index != vhost_user_dev_class.index)
2654  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2655 
2656  DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2657  hwif->name, hwif->dev_instance);
2658 
2660 
2661  vec_foreach (queue, vui->rx_queues)
2662  {
2663  vhost_user_vring_t *txvq;
2664 
2665  txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2666  if ((vum->ifq_count > 0) &&
2669  {
2670  vum->ifq_count--;
2671  // Stop the timer if there is no more interrupt interface/queue
2672  if ((vum->ifq_count == 0) &&
2673  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2674  {
2678  break;
2679  }
2680  }
2681  }
2682 
2683  // Disable and reset interface
2684  vhost_user_term_if (vui);
2685 
2686  // Reset renumbered iface
2687  if (hwif->dev_instance <
2690 
2691  // Delete ethernet interface
2693 
2694  // Back to pool
2695  pool_put (vum->vhost_user_interfaces, vui);
2696 
2697  return rv;
2698 }
2699 
2700 static clib_error_t *
2702 {
2703  vnet_main_t *vnm = vnet_get_main ();
2705  vhost_user_intf_t *vui;
2706 
2708  /* *INDENT-OFF* */
2709  pool_foreach (vui, vum->vhost_user_interfaces, {
2710  vhost_user_delete_if (vnm, vm, vui->sw_if_index);
2711  });
2712  /* *INDENT-ON* */
2714  return 0;
2715 }
2716 
2718 
2719 /**
2720  * Open server unix socket on specified sock_filename.
2721  */
2722 static int
2723 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2724 {
2725  int rv = 0;
2726  struct sockaddr_un un = { };
2727  int fd;
2728  /* create listening socket */
2729  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2730  return VNET_API_ERROR_SYSCALL_ERROR_1;
2731 
2732  un.sun_family = AF_UNIX;
2733  strncpy ((char *) un.sun_path, (char *) sock_filename,
2734  sizeof (un.sun_path) - 1);
2735 
2736  /* remove if exists */
2737  unlink ((char *) sock_filename);
2738 
2739  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2740  {
2741  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2742  goto error;
2743  }
2744 
2745  if (listen (fd, 1) == -1)
2746  {
2747  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2748  goto error;
2749  }
2750 
2751  *sock_fd = fd;
2752  return 0;
2753 
2754 error:
2755  close (fd);
2756  return rv;
2757 }
2758 
2759 /**
2760  * Create ethernet interface for vhost user interface.
2761  */
2762 static void
2764  vhost_user_intf_t * vui, u8 * hwaddress)
2765 {
2767  u8 hwaddr[6];
2768  clib_error_t *error;
2769 
2770  /* create hw and sw interface */
2771  if (hwaddress)
2772  {
2773  clib_memcpy (hwaddr, hwaddress, 6);
2774  }
2775  else
2776  {
2777  random_u32 (&vum->random);
2778  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2779  hwaddr[0] = 2;
2780  hwaddr[1] = 0xfe;
2781  }
2782 
2784  (vnm,
2785  vhost_user_dev_class.index,
2786  vui - vum->vhost_user_interfaces /* device instance */ ,
2787  hwaddr /* ethernet address */ ,
2788  &vui->hw_if_index, 0 /* flag change */ );
2789 
2790  if (error)
2791  clib_error_report (error);
2792 
2795 }
2796 
2797 /*
2798  * Initialize vui with specified attributes
2799  */
2800 static void
2802  vhost_user_intf_t * vui,
2803  int server_sock_fd,
2804  const char *sock_filename,
2805  u64 feature_mask, u32 * sw_if_index)
2806 {
2807  vnet_sw_interface_t *sw;
2808  int q;
2810  vnet_hw_interface_t *hw;
2811 
2812  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
2813  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2814  if (server_sock_fd != -1)
2815  {
2816  unix_file_t template = { 0 };
2818  template.file_descriptor = server_sock_fd;
2819  template.private_data = vui - vum->vhost_user_interfaces; //hw index
2820  vui->unix_server_index = unix_file_add (&unix_main, &template);
2821  }
2822  else
2823  {
2824  vui->unix_server_index = ~0;
2825  }
2826 
2827  vui->sw_if_index = sw->sw_if_index;
2828  strncpy (vui->sock_filename, sock_filename,
2829  ARRAY_LEN (vui->sock_filename) - 1);
2830  vui->sock_errno = 0;
2831  vui->is_up = 0;
2832  vui->feature_mask = feature_mask;
2833  vui->unix_file_index = ~0;
2834  vui->log_base_addr = 0;
2835  vui->if_index = vui - vum->vhost_user_interfaces;
2837  &vui->if_index, 0);
2838 
2839  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2840  vhost_user_vring_init (vui, q);
2841 
2844 
2845  if (sw_if_index)
2846  *sw_if_index = vui->sw_if_index;
2847 
2848  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2849  {
2852  memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2853  }
2854 
2856  vlib_get_thread_main ()->n_vlib_mains - 1);
2858 }
2859 
2860 int
2862  const char *sock_filename,
2863  u8 is_server,
2864  u32 * sw_if_index,
2865  u64 feature_mask,
2866  u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
2867 {
2868  vhost_user_intf_t *vui = NULL;
2869  u32 sw_if_idx = ~0;
2870  int rv = 0;
2871  int server_sock_fd = -1;
2873  uword *if_index;
2874 
2875  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2876  {
2877  return VNET_API_ERROR_INVALID_ARGUMENT;
2878  }
2879 
2880  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2881  if (if_index)
2882  {
2883  if (sw_if_index)
2884  {
2885  vui = &vum->vhost_user_interfaces[*if_index];
2886  *sw_if_index = vui->sw_if_index;
2887  }
2888  return VNET_API_ERROR_IF_ALREADY_EXISTS;
2889  }
2890 
2891  if (is_server)
2892  {
2893  if ((rv =
2894  vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2895  {
2896  return rv;
2897  }
2898  }
2899 
2900  pool_get (vhost_user_main.vhost_user_interfaces, vui);
2901 
2902  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2903  vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2904  feature_mask, &sw_if_idx);
2905 
2906  if (renumber)
2907  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2908 
2909  if (sw_if_index)
2910  *sw_if_index = sw_if_idx;
2911 
2912  // Process node must connect
2914 
2915  return rv;
2916 }
2917 
2918 int
2920  const char *sock_filename,
2921  u8 is_server,
2922  u32 sw_if_index,
2923  u64 feature_mask, u8 renumber, u32 custom_dev_instance)
2924 {
2926  vhost_user_intf_t *vui = NULL;
2927  u32 sw_if_idx = ~0;
2928  int server_sock_fd = -1;
2929  int rv = 0;
2930  vnet_hw_interface_t *hwif;
2931  uword *if_index;
2932 
2933  if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2934  hwif->dev_class_index != vhost_user_dev_class.index)
2935  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2936 
2937  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2938  return VNET_API_ERROR_INVALID_ARGUMENT;
2939 
2941 
2942  /*
2943  * Disallow changing the interface to have the same path name
2944  * as other interface
2945  */
2946  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2947  if (if_index && (*if_index != vui->if_index))
2948  return VNET_API_ERROR_IF_ALREADY_EXISTS;
2949 
2950  // First try to open server socket
2951  if (is_server)
2952  if ((rv = vhost_user_init_server_sock (sock_filename,
2953  &server_sock_fd)) != 0)
2954  return rv;
2955 
2956  vhost_user_term_if (vui);
2957  vhost_user_vui_init (vnm, vui, server_sock_fd,
2958  sock_filename, feature_mask, &sw_if_idx);
2959 
2960  if (renumber)
2961  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2962 
2963  // Process node must connect
2965 
2966  return rv;
2967 }
2968 
2969 clib_error_t *
2971  unformat_input_t * input,
2972  vlib_cli_command_t * cmd)
2973 {
2974  unformat_input_t _line_input, *line_input = &_line_input;
2975  u8 *sock_filename = NULL;
2976  u32 sw_if_index;
2977  u8 is_server = 0;
2978  u64 feature_mask = (u64) ~ (0ULL);
2979  u8 renumber = 0;
2980  u32 custom_dev_instance = ~0;
2981  u8 hwaddr[6];
2982  u8 *hw = NULL;
2983  clib_error_t *error = NULL;
2984 
2985  /* Get a line of input. */
2986  if (!unformat_user (input, unformat_line_input, line_input))
2987  return 0;
2988 
2989  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2990  {
2991  if (unformat (line_input, "socket %s", &sock_filename))
2992  ;
2993  else if (unformat (line_input, "server"))
2994  is_server = 1;
2995  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2996  ;
2997  else
2998  if (unformat
2999  (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
3000  hw = hwaddr;
3001  else if (unformat (line_input, "renumber %d", &custom_dev_instance))
3002  {
3003  renumber = 1;
3004  }
3005  else
3006  {
3007  error = clib_error_return (0, "unknown input `%U'",
3008  format_unformat_error, line_input);
3009  goto done;
3010  }
3011  }
3012 
3013  vnet_main_t *vnm = vnet_get_main ();
3014 
3015  int rv;
3016  if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
3017  is_server, &sw_if_index, feature_mask,
3018  renumber, custom_dev_instance, hw)))
3019  {
3020  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
3021  goto done;
3022  }
3023 
3025  sw_if_index);
3026 
3027 done:
3028  vec_free (sock_filename);
3029  unformat_free (line_input);
3030 
3031  return error;
3032 }
3033 
3034 clib_error_t *
3036  unformat_input_t * input,
3037  vlib_cli_command_t * cmd)
3038 {
3039  unformat_input_t _line_input, *line_input = &_line_input;
3040  u32 sw_if_index = ~0;
3041  vnet_main_t *vnm = vnet_get_main ();
3042  clib_error_t *error = NULL;
3043 
3044  /* Get a line of input. */
3045  if (!unformat_user (input, unformat_line_input, line_input))
3046  return 0;
3047 
3048  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3049  {
3050  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3051  ;
3052  else if (unformat
3053  (line_input, "%U", unformat_vnet_sw_interface, vnm,
3054  &sw_if_index))
3055  {
3056  vnet_hw_interface_t *hwif =
3057  vnet_get_sup_hw_interface (vnm, sw_if_index);
3058  if (hwif == NULL ||
3059  vhost_user_dev_class.index != hwif->dev_class_index)
3060  {
3061  error = clib_error_return (0, "Not a vhost interface");
3062  goto done;
3063  }
3064  }
3065  else
3066  {
3067  error = clib_error_return (0, "unknown input `%U'",
3068  format_unformat_error, line_input);
3069  goto done;
3070  }
3071  }
3072 
3073  vhost_user_delete_if (vnm, vm, sw_if_index);
3074 
3075 done:
3076  unformat_free (line_input);
3077 
3078  return error;
3079 }
3080 
3081 int
3083  vhost_user_intf_details_t ** out_vuids)
3084 {
3085  int rv = 0;
3087  vhost_user_intf_t *vui;
3088  vhost_user_intf_details_t *r_vuids = NULL;
3090  u32 *hw_if_indices = 0;
3092  u8 *s = NULL;
3093  int i;
3094 
3095  if (!out_vuids)
3096  return -1;
3097 
3099  vec_add1 (hw_if_indices, vui->hw_if_index);
3100  );
3101 
3102  for (i = 0; i < vec_len (hw_if_indices); i++)
3103  {
3104  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3106 
3107  vec_add2 (r_vuids, vuid, 1);
3108  vuid->sw_if_index = vui->sw_if_index;
3109  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3110  vuid->features = vui->features;
3111  vuid->num_regions = vui->nregions;
3112  vuid->is_server = vui->unix_server_index != ~0;
3113  vuid->sock_errno = vui->sock_errno;
3114  strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3115  ARRAY_LEN (vuid->sock_filename) - 1);
3116 
3117  s = format (s, "%v%c", hi->name, 0);
3118 
3119  strncpy ((char *) vuid->if_name, (char *) s,
3120  ARRAY_LEN (vuid->if_name) - 1);
3121  _vec_len (s) = 0;
3122  }
3123 
3124  vec_free (s);
3125  vec_free (hw_if_indices);
3126 
3127  *out_vuids = r_vuids;
3128 
3129  return rv;
3130 }
3131 
3132 clib_error_t *
3134  unformat_input_t * input,
3135  vlib_cli_command_t * cmd)
3136 {
3137  clib_error_t *error = 0;
3138  vnet_main_t *vnm = vnet_get_main ();
3140  vhost_user_intf_t *vui;
3141  u32 hw_if_index, *hw_if_indices = 0;
3143  u16 *queue;
3144  u32 ci;
3145  int i, j, q;
3146  int show_descr = 0;
3147  struct feat_struct
3148  {
3149  u8 bit;
3150  char *str;
3151  };
3152  struct feat_struct *feat_entry;
3153 
3154  static struct feat_struct feat_array[] = {
3155 #define _(s,b) { .str = #s, .bit = b, },
3157 #undef _
3158  {.str = NULL}
3159  };
3160 
3161 #define foreach_protocol_feature \
3162  _(VHOST_USER_PROTOCOL_F_MQ) \
3163  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3164 
3165  static struct feat_struct proto_feat_array[] = {
3166 #define _(s) { .str = #s, .bit = s},
3168 #undef _
3169  {.str = NULL}
3170  };
3171 
3172  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3173  {
3174  if (unformat
3175  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3176  {
3177  vec_add1 (hw_if_indices, hw_if_index);
3178  }
3179  else if (unformat (input, "descriptors") || unformat (input, "desc"))
3180  show_descr = 1;
3181  else
3182  {
3183  error = clib_error_return (0, "unknown input `%U'",
3184  format_unformat_error, input);
3185  goto done;
3186  }
3187  }
3188  if (vec_len (hw_if_indices) == 0)
3189  {
3191  vec_add1 (hw_if_indices, vui->hw_if_index);
3192  );
3193  }
3194  vlib_cli_output (vm, "Virtio vhost-user interfaces");
3195  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
3196  vum->coalesce_frames, vum->coalesce_time);
3197  vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
3198  vum->ifq_count);
3199 
3200  for (i = 0; i < vec_len (hw_if_indices); i++)
3201  {
3202  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3204  vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3205  hi->name, hw_if_indices[i]);
3206 
3207  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3208  " features mask (0x%llx): \n"
3209  " features (0x%llx): \n",
3210  vui->virtio_net_hdr_sz, vui->feature_mask,
3211  vui->features);
3212 
3213  feat_entry = (struct feat_struct *) &feat_array;
3214  while (feat_entry->str)
3215  {
3216  if (vui->features & (1ULL << feat_entry->bit))
3217  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3218  feat_entry->bit);
3219  feat_entry++;
3220  }
3221 
3222  vlib_cli_output (vm, " protocol features (0x%llx)",
3223  vui->protocol_features);
3224  feat_entry = (struct feat_struct *) &proto_feat_array;
3225  while (feat_entry->str)
3226  {
3227  if (vui->protocol_features & (1ULL << feat_entry->bit))
3228  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3229  feat_entry->bit);
3230  feat_entry++;
3231  }
3232 
3233  vlib_cli_output (vm, "\n");
3234 
3235  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3236  vui->sock_filename,
3237  (vui->unix_server_index != ~0) ? "server" : "client",
3238  strerror (vui->sock_errno));
3239 
3240  vlib_cli_output (vm, " rx placement: ");
3241 
3242  vec_foreach (queue, vui->rx_queues)
3243  {
3244  vnet_main_t *vnm = vnet_get_main ();
3245  uword thread_index;
3247 
3248  thread_index = vnet_get_device_input_thread_index (vnm,
3249  vui->hw_if_index,
3250  *queue);
3251  vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
3252  vlib_cli_output (vm, " thread %d on vring %d, %U\n",
3253  thread_index, VHOST_VRING_IDX_TX (*queue),
3255  }
3256 
3257  vlib_cli_output (vm, " tx placement: %s\n",
3258  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3259 
3261  {
3262  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3263  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3264  }
3265 
3266  vlib_cli_output (vm, "\n");
3267 
3268  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3269 
3270  if (vui->nregions)
3271  {
3272  vlib_cli_output (vm,
3273  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3274  vlib_cli_output (vm,
3275  " ====== ===== ================== ================== ================== ================== ==================\n");
3276  }
3277  for (j = 0; j < vui->nregions; j++)
3278  {
3279  vlib_cli_output (vm,
3280  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3281  j, vui->region_mmap_fd[j],
3282  vui->regions[j].guest_phys_addr,
3283  vui->regions[j].memory_size,
3284  vui->regions[j].userspace_addr,
3285  vui->regions[j].mmap_offset,
3287  }
3288  for (q = 0; q < VHOST_VRING_MAX_N; q++)
3289  {
3290  if (!vui->vrings[q].started)
3291  continue;
3292 
3293  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3294  (q & 1) ? "RX" : "TX",
3295  vui->vrings[q].enabled ? "" : " disabled");
3296 
3297  vlib_cli_output (vm,
3298  " qsz %d last_avail_idx %d last_used_idx %d\n",
3299  vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3300  vui->vrings[q].last_used_idx);
3301 
3302  if (vui->vrings[q].avail && vui->vrings[q].used)
3303  vlib_cli_output (vm,
3304  " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3305  vui->vrings[q].avail->flags,
3306  vui->vrings[q].avail->idx,
3307  vui->vrings[q].used->flags,
3308  vui->vrings[q].used->idx);
3309 
3310  int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3311  int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3312  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3313  kickfd, callfd, vui->vrings[q].errfd);
3314 
3315  if (show_descr)
3316  {
3317  vlib_cli_output (vm, "\n descriptor table:\n");
3318  vlib_cli_output (vm,
3319  " id addr len flags next user_addr\n");
3320  vlib_cli_output (vm,
3321  " ===== ================== ===== ====== ===== ==================\n");
3322  for (j = 0; j < vui->vrings[q].qsz; j++)
3323  {
3324  u32 mem_hint = 0;
3325  vlib_cli_output (vm,
3326  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3327  j, vui->vrings[q].desc[j].addr,
3328  vui->vrings[q].desc[j].len,
3329  vui->vrings[q].desc[j].flags,
3330  vui->vrings[q].desc[j].next,
3332  (vui,
3333  vui->vrings[q].desc[j].
3334  addr, &mem_hint)));
3335  }
3336  }
3337  }
3338  vlib_cli_output (vm, "\n");
3339  }
3340 done:
3341  vec_free (hw_if_indices);
3342  return error;
3343 }
3344 
3345 /*
3346  * CLI functions
3347  */
3348 
3349 /*?
3350  * Create a vHost User interface. Once created, a new virtual interface
3351  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3352  * is the next free index.
3353  *
3354  * There are several parameters associated with a vHost interface:
3355  *
3356  * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3357  * VPP to manage the vHost interface. If socket does not already exist, VPP will
3358  * create the socket.
3359  *
3360  * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3361  * linux socket. If not provided, VPP will be the client.
3362  *
3363  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3364  * startup. By default, all supported features will be advertised. Otherwise,
3365  * provide the set of features desired.
3366  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3367  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3368  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3369  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3370  * - 0x004000000 (26) - VHOST_F_LOG_ALL
3371  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3372  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3373  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3374  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3375  *
3376  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3377  * X:X:X:X:X:X unix or X.X.X cisco format.
3378  *
3379  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3380  * in the name to be specified. If instance already exists, name will be used
3381  * anyway and multiple instances will have the same name. Use with caution.
3382  *
3383  * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3384  * the input thread polling policy.
3385  *
3386  * @cliexpar
3387  * Example of how to create a vhost interface with VPP as the client and all features enabled:
3388  * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3389  * VirtualEthernet0/0/0
3390  * @cliexend
3391  * Example of how to create a vhost interface with VPP as the server and with just
3392  * multiple queues enabled:
3393  * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3394  * VirtualEthernet0/0/1
3395  * @cliexend
3396  * Once the vHost interface is created, enable the interface using:
3397  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3398 ?*/
3399 /* *INDENT-OFF* */
3400 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3401  .path = "create vhost-user",
3402  .short_help = "create vhost-user socket <socket-filename> [server] "
3403  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
3404  .function = vhost_user_connect_command_fn,
3405 };
3406 /* *INDENT-ON* */
3407 
3408 /*?
3409  * Delete a vHost User interface using the interface name or the
3410  * software interface index. Use the '<em>show interface</em>'
3411  * command to determine the software interface index. On deletion,
3412  * the linux socket will not be deleted.
3413  *
3414  * @cliexpar
3415  * Example of how to delete a vhost interface by name:
3416  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3417  * Example of how to delete a vhost interface by software interface index:
3418  * @cliexcmd{delete vhost-user sw_if_index 1}
3419 ?*/
3420 /* *INDENT-OFF* */
3421 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3422  .path = "delete vhost-user",
3423  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3424  .function = vhost_user_delete_command_fn,
3425 };
3426 
3427 /*?
3428  * Display the attributes of a single vHost User interface (provide interface
3429  * name), multiple vHost User interfaces (provide a list of interface names seperated
3430  * by spaces) or all Vhost User interfaces (omit an interface name to display all
3431  * vHost interfaces).
3432  *
3433  * @cliexpar
3434  * @parblock
3435  * Example of how to display a vhost interface:
3436  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3437  * Virtio vhost-user interfaces
3438  * Global:
3439  * coalesce frames 32 time 1e-3
3440  * Interface: VirtualEthernet0/0/0 (ifindex 1)
3441  * virtio_net_hdr_sz 12
3442  * features mask (0xffffffffffffffff):
3443  * features (0x50408000):
3444  * VIRTIO_NET_F_MRG_RXBUF (15)
3445  * VIRTIO_NET_F_MQ (22)
3446  * VIRTIO_F_INDIRECT_DESC (28)
3447  * VHOST_USER_F_PROTOCOL_FEATURES (30)
3448  * protocol features (0x3)
3449  * VHOST_USER_PROTOCOL_F_MQ (0)
3450  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3451  *
3452  * socket filename /tmp/vhost1.sock type client errno "Success"
3453  *
3454  * rx placement:
3455  * thread 1 on vring 1
3456  * thread 1 on vring 5
3457  * thread 2 on vring 3
3458  * thread 2 on vring 7
3459  * tx placement: spin-lock
3460  * thread 0 on vring 0
3461  * thread 1 on vring 2
3462  * thread 2 on vring 0
3463  *
3464  * Memory regions (total 2)
3465  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3466  * ====== ===== ================== ================== ================== ================== ==================
3467  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3468  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3469  *
3470  * Virtqueue 0 (TX)
3471  * qsz 256 last_avail_idx 0 last_used_idx 0
3472  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3473  * kickfd 62 callfd 64 errfd -1
3474  *
3475  * Virtqueue 1 (RX)
3476  * qsz 256 last_avail_idx 0 last_used_idx 0
3477  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3478  * kickfd 65 callfd 66 errfd -1
3479  *
3480  * Virtqueue 2 (TX)
3481  * qsz 256 last_avail_idx 0 last_used_idx 0
3482  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3483  * kickfd 63 callfd 70 errfd -1
3484  *
3485  * Virtqueue 3 (RX)
3486  * qsz 256 last_avail_idx 0 last_used_idx 0
3487  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3488  * kickfd 72 callfd 74 errfd -1
3489  *
3490  * Virtqueue 4 (TX disabled)
3491  * qsz 256 last_avail_idx 0 last_used_idx 0
3492  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3493  * kickfd 76 callfd 78 errfd -1
3494  *
3495  * Virtqueue 5 (RX disabled)
3496  * qsz 256 last_avail_idx 0 last_used_idx 0
3497  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3498  * kickfd 80 callfd 82 errfd -1
3499  *
3500  * Virtqueue 6 (TX disabled)
3501  * qsz 256 last_avail_idx 0 last_used_idx 0
3502  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3503  * kickfd 84 callfd 86 errfd -1
3504  *
3505  * Virtqueue 7 (RX disabled)
3506  * qsz 256 last_avail_idx 0 last_used_idx 0
3507  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3508  * kickfd 88 callfd 90 errfd -1
3509  *
3510  * @cliexend
3511  *
3512  * The optional '<em>descriptors</em>' parameter will display the same output as
3513  * the previous example but will include the descriptor table for each queue.
3514  * The output is truncated below:
3515  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3516  * Virtio vhost-user interfaces
3517  * Global:
3518  * coalesce frames 32 time 1e-3
3519  * Interface: VirtualEthernet0/0/0 (ifindex 1)
3520  * virtio_net_hdr_sz 12
3521  * features mask (0xffffffffffffffff):
3522  * features (0x50408000):
3523  * VIRTIO_NET_F_MRG_RXBUF (15)
3524  * VIRTIO_NET_F_MQ (22)
3525  * :
3526  * Virtqueue 0 (TX)
3527  * qsz 256 last_avail_idx 0 last_used_idx 0
3528  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3529  * kickfd 62 callfd 64 errfd -1
3530  *
3531  * descriptor table:
3532  * id addr len flags next user_addr
3533  * ===== ================== ===== ====== ===== ==================
3534  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3535  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3536  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3537  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3538  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3539  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3540  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3541  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3542  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3543  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3544  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3545  * :
3546  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3547  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3548  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3549  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3550  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3551  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3552  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3553  *
3554  * Virtqueue 1 (RX)
3555  * qsz 256 last_avail_idx 0 last_used_idx 0
3556  * :
3557  * @cliexend
3558  * @endparblock
3559 ?*/
3560 /* *INDENT-OFF* */
3561 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3562  .path = "show vhost-user",
3563  .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3564  .function = show_vhost_user_command_fn,
3565 };
3566 /* *INDENT-ON* */
3567 
3568 clib_error_t *
3570  unformat_input_t * input,
3571  vlib_cli_command_t * cmd)
3572 {
3573  unformat_input_t _line_input, *line_input = &_line_input;
3574  clib_error_t *error = NULL;
3576  u8 onoff = 0;
3577  u8 input_found = 0;
3578 
3579  /* Get a line of input. */
3580  if (!unformat_user (input, unformat_line_input, line_input))
3581  return clib_error_return (0, "missing argument");
3582 
3583  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3584  {
3585  if (input_found)
3586  {
3587  error = clib_error_return (0, "unknown input `%U'",
3588  format_unformat_error, line_input);
3589  goto done;
3590  }
3591 
3592  if (unformat (line_input, "on"))
3593  {
3594  input_found = 1;
3595  onoff = 1;
3596  }
3597  else if (unformat (line_input, "off"))
3598  {
3599  input_found = 1;
3600  onoff = 0;
3601  }
3602  else
3603  {
3604  error = clib_error_return (0, "unknown input `%U'",
3605  format_unformat_error, line_input);
3606  goto done;
3607  }
3608  }
3609 
3610  vum->debug = onoff;
3611 
3612 done:
3613  unformat_free (line_input);
3614 
3615  return error;
3616 }
3617 
3618 /* *INDENT-OFF* */
3619 VLIB_CLI_COMMAND (debug_vhost_user_command, static) = {
3620  .path = "debug vhost-user",
3621  .short_help = "debug vhost-user <on | off>",
3622  .function = debug_vhost_user_command_fn,
3623 };
3624 /* *INDENT-ON* */
3625 
3626 static clib_error_t *
3628 {
3630 
3631  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3632  {
3633  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3634  ;
3635  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3636  ;
3637  else if (unformat (input, "dont-dump-memory"))
3638  vum->dont_dump_vhost_user_memory = 1;
3639  else
3640  return clib_error_return (0, "unknown input `%U'",
3641  format_unformat_error, input);
3642  }
3643 
3644  return 0;
3645 }
3646 
3647 /* vhost-user { ... } configuration. */
3648 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3649 
3650 void
3652 {
3654  vhost_user_intf_t *vui;
3655 
3656  if (vum->dont_dump_vhost_user_memory)
3657  {
3659  unmap_all_mem_regions (vui);
3660  );
3661  }
3662 }
3663 
3664 /*
3665  * fd.io coding-style-patch-verification: ON
3666  *
3667  * Local Variables:
3668  * eval: (c-set-style "gnu")
3669  * End:
3670  */
unformat_function_t unformat_vnet_hw_interface
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
static clib_error_t * vhost_user_init(vlib_main_t *vm)
Definition: vhost-user.c:1225
unix_file_t * file_pool
Definition: unix.h:89
static void vhost_user_vring_close(vhost_user_intf_t *vui, u32 qid)
Definition: vhost-user.c:577
static void vnet_device_increment_rx_packets(u32 thread_index, u64 count)
Definition: devices.h:109
vmrglw vmrglh hi
static void vhost_user_if_disconnect(vhost_user_intf_t *vui)
Definition: vhost-user.c:603
#define vec_foreach_index(var, v)
Iterate over vector indices.
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
vring_desc_t * desc
Definition: vhost-user.h:198
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: vhost-user.h:45
#define CLIB_UNUSED(x)
Definition: clib.h:79
u32 virtio_ring_flags
The device index.
Definition: vhost-user.h:273
virtio_net_hdr_mrg_rxbuf_t hdr
Length of the first data descriptor.
Definition: vhost-user.h:275
static uword random_default_seed(void)
Default random seed (unix/linux user-mode)
Definition: random.h:91
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:537
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
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:699
#define clib_smp_swap(addr, new)
Definition: smp.h:45
unix_file_function_t * read_function
Definition: unix.h:62
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost-user.h:306
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:2763
#define VHOST_USER_DOWN_DISCARD_COUNT
Definition: vhost-user.c:72
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:211
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:290
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:468
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:295
#define VHOST_VRING_IDX_TX(qid)
Definition: vhost-user.h:24
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
static clib_error_t * vhost_user_socket_error(unix_file_t *uf)
Definition: vhost-user.c:1181
void vhost_user_rx_trace(vhost_trace_t *t, vhost_user_intf_t *vui, u16 qid, vlib_buffer_t *b, vhost_user_vring_t *txvq)
Definition: vhost-user.c:1296
u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:241
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define PREDICT_TRUE(x)
Definition: clib.h:98
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
unix_main_t unix_main
Definition: main.c:60
#define NULL
Definition: clib.h:55
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:192
#define foreach_virtio_trace_flags
Definition: vhost-user.c:105
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:459
vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N]
Definition: vhost-user.h:288
static void vhost_user_term_if(vhost_user_intf_t *vui)
Disables and reset interface structure.
Definition: vhost-user.c:2615
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
vring_avail_t * avail
Definition: vhost-user.h:199
static uword vhost_user_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f)
Definition: vhost-user.c:1852
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VHOST_USER_EVENT_START_TIMER
Definition: vhost-user.h:216
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1267
struct _vlib_node_registration vlib_node_registration_t
static_always_inline u32 vhost_user_input_copy(vhost_user_intf_t *vui, vhost_copy_t *cpy, u16 copy_len, u32 *map_hint)
Definition: vhost-user.c:1363
#define VHOST_USER_MSG_HDR_SZ
Definition: vhost-user.h:20
static clib_error_t * vhost_user_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: vhost-user.c:2468
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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:3133
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:78
unformat_function_t unformat_vnet_sw_interface
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:397
static char * vhost_user_input_func_error_strings[]
Definition: vhost-user.c:159
static char * vhost_user_tx_func_error_strings[]
Definition: vhost-user.c:137
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
vring_used_t * used
Definition: vhost-user.h:200
format_function_t format_vnet_sw_if_index_name
vhost_trace_t * current_trace
Definition: vhost-user.h:292
static uword vlib_process_suspend_time_is_zero(f64 dt)
Returns TRUE if a process suspend time is less than 1us.
Definition: node_funcs.h:436
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static int vhost_user_name_renumber(vnet_hw_interface_t *hi, u32 new_dev_instance)
Definition: vhost-user.c:193
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)
Definition: vhost-user.c:2801
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:104
#define VHOST_VRING_F_LOG
Definition: vhost-user.h:32
vnet_hw_interface_rx_mode
Definition: interface.h:51
VNET_DEVICE_CLASS(vhost_user_dev_class, static)
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:87
static u8 * format_vhost_user_interface_name(u8 *s, va_list *args)
Definition: vhost-user.c:176
#define static_always_inline
Definition: clib.h:85
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
Definition: buffer_funcs.h:170
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
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:542
static clib_error_t * ip4_init(vlib_main_t *vm)
Definition: ip4_input.c:464
static uword format_get_indent(u8 *s)
Definition: format.h:72
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:653
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
void * log_base_addr
Definition: vhost-user.h:251
static_always_inline void vnet_device_input_set_interrupt_pending(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:135
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:89
#define foreach_protocol_feature
#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
vhost_user_tx_func_error_t
Definition: vhost-user.c:129
unsigned long u64
Definition: types.h:89
static void unmap_all_mem_regions(vhost_user_intf_t *vui)
Definition: vhost-user.c:304
static void vhost_user_set_interrupt_pending(vhost_user_intf_t *vui, u32 ifq)
Definition: vhost-user.c:474
vhost_user_input_func_error_t
Definition: vhost-user.c:151
#define vlib_call_init_function(vm, x)
Definition: init.h:162
#define VHOST_USER_TX_COPY_THRESHOLD
Definition: vhost-user.c:98
static clib_error_t * vhost_user_socket_read(unix_file_t *uf)
Definition: vhost-user.c:667
static uword pointer_to_uword(const void *p)
Definition: types.h:131
#define UNIX_GET_FD(unixfd_idx)
Definition: vhost-user.c:100
unformat_function_t unformat_line_input
Definition: format.h:281
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:2723
static uword vhost_user_send_interrupt_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost-user.c:2313
VLIB_DEVICE_TX_FUNCTION_MULTIARCH(vhost_user_dev_class, vhost_user_tx)
Definition: vhost-user.c:2499
#define VHOST_USER_EVENT_STOP_TIMER
Definition: vhost-user.h:217
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
static void vhost_user_vring_unlock(vhost_user_intf_t *vui, u32 qid)
Unlock the vring lock.
Definition: vhost-user.c:549
format_function_t format_vnet_sw_interface_name
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
static_always_inline uword vnet_get_device_input_thread_index(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:126
u16 state
Input node state.
Definition: node.h:456
u32 file_descriptor
Definition: unix.h:52
vlib_main_t * vlib_main
Definition: vnet.h:78
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:71
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:946
uword private_data
Definition: unix.h:59
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost-user.c:2644
static void * map_user_mem(vhost_user_intf_t *vui, uword addr)
Definition: vhost-user.c:279
u32 random
Pseudo random iterator.
Definition: vhost-user.h:309
static_always_inline void vhost_user_log_dirty_pages(vhost_user_intf_t *vui, u64 addr, u64 len)
Definition: vhost-user.c:655
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:271
struct _unformat_input_t unformat_input_t
#define VIRTQ_DESC_F_INDIRECT
Definition: vhost-user.h:27
#define clib_error_return_unix(e, args...)
Definition: error.h:102
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
int vnet_hw_interface_get_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode *mode)
Definition: devices.c:292
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:402
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:241
format_function_t format_vnet_hw_interface_rx_mode
void vhost_user_tx_trace(vhost_trace_t *t, vhost_user_intf_t *vui, u16 qid, vlib_buffer_t *b, vhost_user_vring_t *rxvq)
Definition: vhost-user.c:1899
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:119
#define vhost_user_log_dirty_ring(vui, vq, member)
Definition: vhost-user.c:660
static vlib_node_registration_t vhost_user_process_node
(constructor) VLIB_REGISTER_NODE (vhost_user_process_node)
Definition: vhost-user.c:2603
void vhost_user_unmap_all(void)
Definition: vhost-user.c:3651
char sock_filename[256]
Definition: vhost-user.h:226
vnet_main_t vnet_main
Definition: misc.c:43
#define VLIB_FRAME_SIZE
Definition: node.h:329
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:652
u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:242
static void vhost_user_send_call(vlib_main_t *vm, vhost_user_vring_t *vq)
Definition: vhost-user.c:1343
#define VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE
Definition: interface.h:423
u32 node_index
Node index.
Definition: node.h:441
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:238
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:366
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost-user.c:3082
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
static clib_error_t * vhost_user_exit(vlib_main_t *vm)
Definition: vhost-user.c:2701
static void vhost_user_tx_thread_placement(vhost_user_intf_t *vui)
Definition: vhost-user.c:340
static void vhost_user_vring_init(vhost_user_intf_t *vui, u32 qid)
Definition: vhost-user.c:555
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:91
static vlib_node_registration_t vhost_user_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (vhost_user_send_interrupt_node)
Definition: vhost-user.c:2402
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost-user.h:300
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)
Definition: vhost-user.c:2861
u16 device_index
The interface queue index (Not the virtio vring idx)
Definition: vhost-user.h:272
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost-user.h:299
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:143
u16 n_vectors
Definition: node.h:345
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:185
static clib_error_t * vhost_user_kickfd_read_ready(unix_file_t *uf)
Definition: vhost-user.c:501
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static_always_inline void vhost_user_log_dirty_pages_2(vhost_user_intf_t *vui, u64 addr, u64 len, u8 is_host_address)
Definition: vhost-user.c:627
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
static int vhost_user_vring_try_lock(vhost_user_intf_t *vui, u32 qid)
Try once to lock the vring.
Definition: vhost-user.c:530
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:116
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)
Definition: vhost-user.c:2919
#define clib_warning(format, args...)
Definition: error.h:59
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
#define clib_memcpy(a, b, c)
Definition: string.h:69
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost-user.h:19
static_always_inline void * map_guest_mem(vhost_user_intf_t *vui, uword addr, u32 *hint)
Definition: vhost-user.c:210
clib_error_t * debug_vhost_user_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost-user.c:3569
static u32 vlib_buffer_alloc_from_free_list(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 free_list_index)
Allocate buffers from specific freelist into supplied array.
Definition: buffer_funcs.h:269
u32 nregions
Definition: vhost-user.h:75
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1166
#define ARRAY_LEN(x)
Definition: clib.h:59
static uword vhost_user_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: vhost-user.c:1983
u16 first_desc_len
Runtime queue flags.
Definition: vhost-user.h:274
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD
Definition: vhost-user.h:31
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:50
static void vhost_user_input_rewind_buffers(vlib_main_t *vm, vhost_cpu_t *cpu, vlib_buffer_t *b_head)
Definition: vhost-user.c:1450
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:560
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:468
u32 rx_buffers[VHOST_USER_RX_BUFFERS_N]
Definition: vhost-user.h:285
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:227
#define ASSERT(truth)
#define VHOST_USER_RX_BUFFER_STARVATION
Definition: vhost-user.c:78
unsigned int u32
Definition: types.h:88
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
static long get_huge_page_size(int fd)
Definition: vhost-user.c:296
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
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:3035
static void clib_mem_free(void *p)
Definition: mem.h:176
#define VIRTQ_DESC_F_NEXT
Definition: vhost-user.h:26
volatile u32 * vring_locks[VHOST_VRING_MAX_N]
Definition: vhost-user.h:246
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:246
mhash_t if_index_by_sock_name
Definition: vhost-user.h:297
virtio_trace_flag_t
Definition: vhost-user.c:111
#define clib_error_report(e)
Definition: error.h:113
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:286
static u8 * format_vhost_trace(u8 *s, va_list *va)
Definition: vhost-user.c:1258
#define VHOST_USER_RX_COPY_THRESHOLD
Definition: vhost-user.c:88
static void vhost_user_vring_lock(vhost_user_intf_t *vui, u32 qid)
Spin until the vring is successfully locked.
Definition: vhost-user.c:539
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static void vhost_user_rx_thread_placement()
Unassign existing interface/queue to thread mappings and re-assign new interface/queue to thread mapp...
Definition: vhost-user.c:380
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
static void vhost_user_update_iface_state(vhost_user_intf_t *vui)
Definition: vhost-user.c:456
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:103
#define foreach_vhost_user_tx_func_error
Definition: vhost-user.c:120
void * region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:239
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
static clib_error_t * vhost_user_socksvr_accept_ready(unix_file_t *uf)
Definition: vhost-user.c:1197
static u32 vhost_user_if_input(vlib_main_t *vm, vhost_user_main_t *vum, vhost_user_intf_t *vui, u16 qid, vlib_node_runtime_t *node, vnet_hw_interface_rx_mode mode)
Definition: vhost-user.c:1469
static clib_error_t * vhost_user_config(vlib_main_t *vm, unformat_input_t *input)
Definition: vhost-user.c:3627
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
#define VRING_USED_F_NO_NOTIFY
Definition: vhost-user.h:44
#define VHOST_USER_RX_BUFFERS_N
Definition: vhost-user.h:279
unsigned char u8
Definition: types.h:56
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:444
vhost_user_vring_t vrings[VHOST_VRING_MAX_N]
Definition: vhost-user.h:245
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:496
#define VHOST_VRING_MAX_N
Definition: vhost-user.h:22
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
#define VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE
Definition: node.h:263
#define clib_unix_warning(format, args...)
Definition: error.h:68
Definition: unix.h:49
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
Definition: vhost-user.c:118
u32 rx_buffers_len
Definition: vhost-user.h:284
#define DBG_SOCK(args...)
Definition: vhost-user.c:54
u32 vhost_user_rx_discard_packet(vlib_main_t *vm, vhost_user_intf_t *vui, vhost_user_vring_t *txvq, u32 discard_max)
Try to discard packets from the tx ring (VPP RX path).
Definition: vhost-user.c:1411
static vhost_user_main_t vhost_user_main
Definition: vhost-user.c:166
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:117
#define vnet_buffer(b)
Definition: buffer.h:304
#define DBG_VQ(args...)
Definition: vhost-user.c:64
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:159
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:1198
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:227
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:144
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE
Definition: node.h:262
u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:240
static clib_error_t * vhost_user_callfd_read_ready(unix_file_t *uf)
Definition: vhost-user.c:490
#define vec_foreach(var, vec)
Vector iterator.
#define foreach_vhost_user_input_func_error
Definition: vhost-user.c:143
u16 flags
Copy of main node flags.
Definition: node.h:454
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:101
vhost_vring_addr_t addr
Definition: vhost-user.h:82
virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE]
Definition: vhost-user.h:287
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
u32 flags
Definition: vhost-user.h:76
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:485
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
static_always_inline u32 vhost_user_tx_copy(vhost_user_intf_t *vui, vhost_copy_t *cpy, u16 copy_len, u32 *map_hint)
Definition: vhost-user.c:1935
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
static clib_error_t * vhost_user_interface_rx_mode_change(vnet_main_t *vnm, u32 hw_if_index, u32 qid, vnet_hw_interface_rx_mode mode)
Definition: vhost-user.c:2410
#define VHOST_USER_PROTOCOL_F_MQ
Definition: vhost-user.h:30
#define VHOST_LOG_PAGE
Definition: vhost-user.c:625
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
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:2970
VNET_HW_INTERFACE_CLASS(vhost_interface_class, static)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
Definition: defs.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
int dont_dump_vhost_user_memory
Definition: vhost-user.h:303
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost-user.h:23
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146