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