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