FD.io VPP  v17.04-9-g99c0734
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) & ~(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) & ~(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) & ~(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 }
1579 
1580 static u32
1582  vhost_user_main_t * vum,
1583  vhost_user_intf_t * vui,
1584  u16 qid, vlib_node_runtime_t * node)
1585 {
1586  vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1587  u16 n_rx_packets = 0;
1588  u32 n_rx_bytes = 0;
1589  u16 n_left;
1590  u32 n_left_to_next, *to_next;
1592  u32 n_trace = vlib_get_trace_count (vm, node);
1593  u16 qsz_mask;
1594  u32 map_hint = 0;
1595  u16 cpu_index = os_get_cpu_number ();
1596  u16 copy_len = 0;
1597 
1598  {
1599  /* do we have pending interrupts ? */
1600  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1601  f64 now = vlib_time_now (vm);
1602 
1603  if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1604  vhost_user_send_call (vm, txvq);
1605 
1606  if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1607  vhost_user_send_call (vm, rxvq);
1608  }
1609 
1610  if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1611  return 0;
1612 
1613  n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1614 
1615  /* nothing to do */
1616  if (PREDICT_FALSE (n_left == 0))
1617  return 0;
1618 
1619  if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1620  {
1621  /*
1622  * Discard input packet if interface is admin down or vring is not
1623  * enabled.
1624  * "For example, for a networking device, in the disabled state
1625  * client must not supply any new RX packets, but must process
1626  * and discard any TX packets."
1627  */
1628  vhost_user_rx_discard_packet (vm, vui, txvq,
1630  return 0;
1631  }
1632 
1633  if (PREDICT_FALSE (n_left == txvq->qsz))
1634  {
1635  /*
1636  * Informational error logging when VPP is not
1637  * receiving packets fast enough.
1638  */
1639  vlib_error_count (vm, node->node_index,
1640  VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1641  }
1642 
1643  qsz_mask = txvq->qsz - 1;
1644 
1645  if (n_left > VLIB_FRAME_SIZE)
1646  n_left = VLIB_FRAME_SIZE;
1647 
1648  /*
1649  * For small packets (<2kB), we will not need more than one vlib buffer
1650  * per packet. In case packets are bigger, we will just yeld at some point
1651  * in the loop and come back later. This is not an issue as for big packet,
1652  * processing cost really comes from the memory copy.
1653  */
1654  if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len < n_left + 1))
1655  {
1656  u32 curr_len = vum->cpus[cpu_index].rx_buffers_len;
1657  vum->cpus[cpu_index].rx_buffers_len +=
1659  vum->cpus[cpu_index].rx_buffers +
1660  curr_len,
1661  VHOST_USER_RX_BUFFERS_N - curr_len,
1663 
1664  if (PREDICT_FALSE
1665  (vum->cpus[cpu_index].rx_buffers_len <
1667  {
1668  /* In case of buffer starvation, discard some packets from the queue
1669  * and log the event.
1670  * We keep doing best effort for the remaining packets. */
1671  u32 flush = (n_left + 1 > vum->cpus[cpu_index].rx_buffers_len) ?
1672  n_left + 1 - vum->cpus[cpu_index].rx_buffers_len : 1;
1673  flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1674 
1675  n_left -= flush;
1677  interface_main.sw_if_counters +
1679  os_get_cpu_number (),
1680  vui->sw_if_index, flush);
1681 
1683  VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1684  }
1685  }
1686 
1687  while (n_left > 0)
1688  {
1689  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1690 
1691  while (n_left > 0 && n_left_to_next > 0)
1692  {
1693  vlib_buffer_t *b_head, *b_current;
1694  u32 bi_current;
1695  u16 desc_current;
1696  u32 desc_data_offset;
1697  vring_desc_t *desc_table = txvq->desc;
1698 
1699  if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len <= 1))
1700  {
1701  /* Not enough rx_buffers
1702  * Note: We yeld on 1 so we don't need to do an additional
1703  * check for the next buffer prefetch.
1704  */
1705  n_left = 0;
1706  break;
1707  }
1708 
1709  desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1710  vum->cpus[cpu_index].rx_buffers_len--;
1711  bi_current = (vum->cpus[cpu_index].rx_buffers)
1712  [vum->cpus[cpu_index].rx_buffers_len];
1713  b_head = b_current = vlib_get_buffer (vm, bi_current);
1714  to_next[0] = bi_current; //We do that now so we can forget about bi_current
1715  to_next++;
1716  n_left_to_next--;
1717 
1719  (vum->cpus[cpu_index].rx_buffers)
1720  [vum->cpus[cpu_index].
1721  rx_buffers_len - 1], LOAD);
1722 
1723  /* Just preset the used descriptor id and length for later */
1724  txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1725  txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1726  vhost_user_log_dirty_ring (vui, txvq,
1727  ring[txvq->last_used_idx & qsz_mask]);
1728 
1729  /* The buffer should already be initialized */
1732 
1733  if (PREDICT_FALSE (n_trace))
1734  {
1735  //TODO: next_index is not exactly known at that point
1736  vlib_trace_buffer (vm, node, next_index, b_head,
1737  /* follow_chain */ 0);
1738  vhost_trace_t *t0 =
1739  vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1740  vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1741  n_trace--;
1742  vlib_set_trace_count (vm, node, n_trace);
1743  }
1744 
1745  /* This depends on the setup but is very consistent
1746  * So I think the CPU branch predictor will make a pretty good job
1747  * at optimizing the decision. */
1748  if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1749  {
1750  desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1751  &map_hint);
1752  desc_current = 0;
1753  if (PREDICT_FALSE (desc_table == 0))
1754  {
1755  //FIXME: Handle error by shutdown the queue
1756  goto out;
1757  }
1758  }
1759 
1760  if (PREDICT_TRUE (vui->is_any_layout) ||
1761  (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1762  {
1763  /* ANYLAYOUT or single buffer */
1764  desc_data_offset = vui->virtio_net_hdr_sz;
1765  }
1766  else
1767  {
1768  /* CSR case without ANYLAYOUT, skip 1st buffer */
1769  desc_data_offset = desc_table[desc_current].len;
1770  }
1771 
1772  while (1)
1773  {
1774  /* Get more input if necessary. Or end of packet. */
1775  if (desc_data_offset == desc_table[desc_current].len)
1776  {
1777  if (PREDICT_FALSE (desc_table[desc_current].flags &
1778  VIRTQ_DESC_F_NEXT))
1779  {
1780  desc_current = desc_table[desc_current].next;
1781  desc_data_offset = 0;
1782  }
1783  else
1784  {
1785  goto out;
1786  }
1787  }
1788 
1789  /* Get more output if necessary. Or end of packet. */
1790  if (PREDICT_FALSE
1791  (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1792  {
1793  if (PREDICT_FALSE
1794  (vum->cpus[cpu_index].rx_buffers_len == 0))
1795  {
1796  /* Cancel speculation */
1797  to_next--;
1798  n_left_to_next++;
1799 
1800  /*
1801  * Checking if there are some left buffers.
1802  * If not, just rewind the used buffers and stop.
1803  * Note: Scheduled copies are not cancelled. This is
1804  * not an issue as they would still be valid. Useless,
1805  * but valid.
1806  */
1808  &vum->cpus[cpu_index],
1809  b_head);
1810  n_left = 0;
1811  goto stop;
1812  }
1813 
1814  /* Get next output */
1815  vum->cpus[cpu_index].rx_buffers_len--;
1816  u32 bi_next =
1817  (vum->cpus[cpu_index].rx_buffers)[vum->cpus
1818  [cpu_index].rx_buffers_len];
1819  b_current->next_buffer = bi_next;
1820  b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1821  bi_current = bi_next;
1822  b_current = vlib_get_buffer (vm, bi_current);
1823  }
1824 
1825  /* Prepare a copy order executed later for the data */
1826  vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
1827  copy_len++;
1828  u32 desc_data_l =
1829  desc_table[desc_current].len - desc_data_offset;
1830  cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1831  cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1832  cpy->dst = (uword) vlib_buffer_get_current (b_current);
1833  cpy->src = desc_table[desc_current].addr + desc_data_offset;
1834 
1835  desc_data_offset += cpy->len;
1836 
1837  b_current->current_length += cpy->len;
1839  }
1840 
1841  out:
1842  CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1843 
1844  n_rx_bytes += b_head->total_length_not_including_first_buffer;
1845  n_rx_packets++;
1846 
1848  b_head->current_length;
1849 
1850  /* consume the descriptor and return it as used */
1851  txvq->last_avail_idx++;
1852  txvq->last_used_idx++;
1853 
1855 
1856  vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1857  vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1858  b_head->error = 0;
1859 
1860  {
1862 
1863  /* redirect if feature path enabled */
1865  b_head);
1866 
1867  u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1868  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1869  to_next, n_left_to_next,
1870  bi, next0);
1871  }
1872 
1873  n_left--;
1874 
1875  /*
1876  * Although separating memory copies from virtio ring parsing
1877  * is beneficial, we can offer to perform the copies from time
1878  * to time in order to free some space in the ring.
1879  */
1880  if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1881  {
1882  if (PREDICT_FALSE
1883  (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1884  copy_len, &map_hint)))
1885  {
1886  clib_warning
1887  ("Memory mapping error on interface hw_if_index=%d "
1888  "(Shutting down - Switch interface down and up to restart)",
1889  vui->hw_if_index);
1890  vui->admin_up = 0;
1891  copy_len = 0;
1892  break;
1893  }
1894  copy_len = 0;
1895 
1896  /* give buffers back to driver */
1898  txvq->used->idx = txvq->last_used_idx;
1899  vhost_user_log_dirty_ring (vui, txvq, idx);
1900  }
1901  }
1902  stop:
1903  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1904  }
1905 
1906  /* Do the memory copies */
1907  if (PREDICT_FALSE
1908  (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1909  copy_len, &map_hint)))
1910  {
1911  clib_warning ("Memory mapping error on interface hw_if_index=%d "
1912  "(Shutting down - Switch interface down and up to restart)",
1913  vui->hw_if_index);
1914  vui->admin_up = 0;
1915  }
1916 
1917  /* give buffers back to driver */
1919  txvq->used->idx = txvq->last_used_idx;
1920  vhost_user_log_dirty_ring (vui, txvq, idx);
1921 
1922  /* interrupt (call) handling */
1923  if ((txvq->callfd_idx != ~0) &&
1924  !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1925  {
1926  txvq->n_since_last_int += n_rx_packets;
1927 
1928  if (txvq->n_since_last_int > vum->coalesce_frames)
1929  vhost_user_send_call (vm, txvq);
1930  }
1931 
1932  /* increase rx counters */
1936  os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1937 
1938  vnet_device_increment_rx_packets (cpu_index, n_rx_packets);
1939 
1940  return n_rx_packets;
1941 }
1942 
1943 static uword
1945  vlib_node_runtime_t * node, vlib_frame_t * f)
1946 {
1948  uword n_rx_packets = 0;
1949  u32 cpu_index = os_get_cpu_number ();
1951  vhost_user_intf_t *vui;
1952  vhost_cpu_t *vhc;
1953 
1954  vhc = &vum->cpus[cpu_index];
1956  {
1957  vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
1958  {
1959  vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1960  n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1961  }
1962  }
1963  else
1964  {
1965  int i;
1966 
1967  /* *INDENT-OFF* */
1969  int qid = i & 0xff;
1970 
1971  clib_bitmap_set (vhc->pending_input_bitmap, i, 0);
1972  vui = pool_elt_at_index (vum->vhost_user_interfaces, i >> 8);
1973  n_rx_packets += vhost_user_if_input (vm, vum, vui, qid, node);
1974  }));
1975  /* *INDENT-ON* */
1976  }
1977  return n_rx_packets;
1978 }
1979 
1980 /* *INDENT-OFF* */
1982  .function = vhost_user_input,
1983  .type = VLIB_NODE_TYPE_INPUT,
1984  .name = "vhost-user-input",
1985  .sibling_of = "device-input",
1986 
1987  /* Will be enabled if/when hardware is detected. */
1988  .state = VLIB_NODE_STATE_DISABLED,
1989 
1990  .format_buffer = format_ethernet_header_with_length,
1991  .format_trace = format_vhost_trace,
1992 
1993  .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1994  .error_strings = vhost_user_input_func_error_strings,
1995 };
1996 
1998 /* *INDENT-ON* */
1999 
2000 
2001 void
2003  vhost_user_intf_t * vui, u16 qid,
2004  vlib_buffer_t * b, vhost_user_vring_t * rxvq)
2005 {
2007  u32 qsz_mask = rxvq->qsz - 1;
2008  u32 last_avail_idx = rxvq->last_avail_idx;
2009  u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
2010  vring_desc_t *hdr_desc = 0;
2011  u32 hint = 0;
2012 
2013  memset (t, 0, sizeof (*t));
2014  t->device_index = vui - vum->vhost_user_interfaces;
2015  t->qid = qid;
2016 
2017  hdr_desc = &rxvq->desc[desc_current];
2018  if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
2019  {
2020  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
2021  /* Header is the first here */
2022  hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
2023  }
2024  if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
2025  {
2026  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
2027  }
2028  if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
2029  !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
2030  {
2031  t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
2032  }
2033 
2034  t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
2035 }
2036 
2039  u16 copy_len, u32 * map_hint)
2040 {
2041  void *dst0, *dst1, *dst2, *dst3;
2042  if (PREDICT_TRUE (copy_len >= 4))
2043  {
2044  if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
2045  return 1;
2046  if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
2047  return 1;
2048  while (PREDICT_TRUE (copy_len >= 4))
2049  {
2050  dst0 = dst2;
2051  dst1 = dst3;
2052 
2053  if (PREDICT_FALSE
2054  (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
2055  return 1;
2056  if (PREDICT_FALSE
2057  (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
2058  return 1;
2059 
2060  CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
2061  CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
2062 
2063  clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
2064  clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
2065 
2066  vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
2067  vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
2068  copy_len -= 2;
2069  cpy += 2;
2070  }
2071  }
2072  while (copy_len)
2073  {
2074  if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
2075  return 1;
2076  clib_memcpy (dst0, (void *) cpy->src, cpy->len);
2077  vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
2078  copy_len -= 1;
2079  cpy += 1;
2080  }
2081  return 0;
2082 }
2083 
2084 
2085 static uword
2087  vlib_node_runtime_t * node, vlib_frame_t * frame)
2088 {
2089  u32 *buffers = vlib_frame_args (frame);
2090  u32 n_left = frame->n_vectors;
2092  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
2093  vhost_user_intf_t *vui =
2095  u32 qid = ~0;
2096  vhost_user_vring_t *rxvq;
2097  u16 qsz_mask;
2098  u8 error;
2099  u32 cpu_index = os_get_cpu_number ();
2100  u32 map_hint = 0;
2101  u8 retry = 8;
2102  u16 copy_len;
2103  u16 tx_headers_len;
2104 
2105  if (PREDICT_FALSE (!vui->admin_up))
2106  {
2107  error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2108  goto done3;
2109  }
2110 
2111  if (PREDICT_FALSE (!vui->is_up))
2112  {
2113  error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
2114  goto done3;
2115  }
2116 
2117  qid =
2119  (vui->per_cpu_tx_qid, os_get_cpu_number ()));
2120  rxvq = &vui->vrings[qid];
2121  if (PREDICT_FALSE (vui->use_tx_spinlock))
2122  vhost_user_vring_lock (vui, qid);
2123 
2124  qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
2125 
2126 retry:
2127  error = VHOST_USER_TX_FUNC_ERROR_NONE;
2128  tx_headers_len = 0;
2129  copy_len = 0;
2130  while (n_left > 0)
2131  {
2132  vlib_buffer_t *b0, *current_b0;
2133  u16 desc_head, desc_index, desc_len;
2134  vring_desc_t *desc_table;
2135  uword buffer_map_addr;
2136  u32 buffer_len;
2137  u16 bytes_left;
2138 
2139  if (PREDICT_TRUE (n_left > 1))
2140  vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2141 
2142  b0 = vlib_get_buffer (vm, buffers[0]);
2143 
2145  {
2146  vum->cpus[cpu_index].current_trace =
2147  vlib_add_trace (vm, node, b0,
2148  sizeof (*vum->cpus[cpu_index].current_trace));
2149  vhost_user_tx_trace (vum->cpus[cpu_index].current_trace,
2150  vui, qid / 2, b0, rxvq);
2151  }
2152 
2153  if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2154  {
2155  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2156  goto done;
2157  }
2158 
2159  desc_table = rxvq->desc;
2160  desc_head = desc_index =
2161  rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2162 
2163  /* Go deeper in case of indirect descriptor
2164  * I don't know of any driver providing indirect for RX. */
2165  if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2166  {
2167  if (PREDICT_FALSE
2168  (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2169  {
2170  error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2171  goto done;
2172  }
2173  if (PREDICT_FALSE
2174  (!(desc_table =
2175  map_guest_mem (vui, rxvq->desc[desc_index].addr,
2176  &map_hint))))
2177  {
2178  error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2179  goto done;
2180  }
2181  desc_index = 0;
2182  }
2183 
2184  desc_len = vui->virtio_net_hdr_sz;
2185  buffer_map_addr = desc_table[desc_index].addr;
2186  buffer_len = desc_table[desc_index].len;
2187 
2188  {
2189  // Get a header from the header array
2190  virtio_net_hdr_mrg_rxbuf_t *hdr =
2191  &vum->cpus[cpu_index].tx_headers[tx_headers_len];
2192  tx_headers_len++;
2193  hdr->hdr.flags = 0;
2194  hdr->hdr.gso_type = 0;
2195  hdr->num_buffers = 1; //This is local, no need to check
2196 
2197  // Prepare a copy order executed later for the header
2198  vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2199  copy_len++;
2200  cpy->len = vui->virtio_net_hdr_sz;
2201  cpy->dst = buffer_map_addr;
2202  cpy->src = (uword) hdr;
2203  }
2204 
2205  buffer_map_addr += vui->virtio_net_hdr_sz;
2206  buffer_len -= vui->virtio_net_hdr_sz;
2207  bytes_left = b0->current_length;
2208  current_b0 = b0;
2209  while (1)
2210  {
2211  if (buffer_len == 0)
2212  { //Get new output
2213  if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2214  {
2215  //Next one is chained
2216  desc_index = desc_table[desc_index].next;
2217  buffer_map_addr = desc_table[desc_index].addr;
2218  buffer_len = desc_table[desc_index].len;
2219  }
2220  else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2221  {
2222  virtio_net_hdr_mrg_rxbuf_t *hdr =
2223  &vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2224 
2225  //Move from available to used buffer
2226  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2227  desc_head;
2228  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2229  desc_len;
2230  vhost_user_log_dirty_ring (vui, rxvq,
2231  ring[rxvq->last_used_idx &
2232  qsz_mask]);
2233 
2234  rxvq->last_avail_idx++;
2235  rxvq->last_used_idx++;
2236  hdr->num_buffers++;
2237  desc_len = 0;
2238 
2239  if (PREDICT_FALSE
2240  (rxvq->last_avail_idx == rxvq->avail->idx))
2241  {
2242  //Dequeue queued descriptors for this packet
2243  rxvq->last_used_idx -= hdr->num_buffers - 1;
2244  rxvq->last_avail_idx -= hdr->num_buffers - 1;
2245  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2246  goto done;
2247  }
2248 
2249  desc_table = rxvq->desc;
2250  desc_head = desc_index =
2251  rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2252  if (PREDICT_FALSE
2253  (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2254  {
2255  //It is seriously unlikely that a driver will put indirect descriptor
2256  //after non-indirect descriptor.
2257  if (PREDICT_FALSE
2258  (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2259  {
2260  error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2261  goto done;
2262  }
2263  if (PREDICT_FALSE
2264  (!(desc_table =
2265  map_guest_mem (vui,
2266  rxvq->desc[desc_index].addr,
2267  &map_hint))))
2268  {
2269  error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2270  goto done;
2271  }
2272  desc_index = 0;
2273  }
2274  buffer_map_addr = desc_table[desc_index].addr;
2275  buffer_len = desc_table[desc_index].len;
2276  }
2277  else
2278  {
2279  error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2280  goto done;
2281  }
2282  }
2283 
2284  {
2285  vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2286  copy_len++;
2287  cpy->len = bytes_left;
2288  cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2289  cpy->dst = buffer_map_addr;
2290  cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2291  current_b0->current_length - bytes_left;
2292 
2293  bytes_left -= cpy->len;
2294  buffer_len -= cpy->len;
2295  buffer_map_addr += cpy->len;
2296  desc_len += cpy->len;
2297 
2298  CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2299  }
2300 
2301  // Check if vlib buffer has more data. If not, get more or break.
2302  if (PREDICT_TRUE (!bytes_left))
2303  {
2304  if (PREDICT_FALSE
2305  (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2306  {
2307  current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2308  bytes_left = current_b0->current_length;
2309  }
2310  else
2311  {
2312  //End of packet
2313  break;
2314  }
2315  }
2316  }
2317 
2318  //Move from available to used ring
2319  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2320  rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2321  vhost_user_log_dirty_ring (vui, rxvq,
2322  ring[rxvq->last_used_idx & qsz_mask]);
2323  rxvq->last_avail_idx++;
2324  rxvq->last_used_idx++;
2325 
2327  {
2328  vum->cpus[cpu_index].current_trace->hdr =
2329  vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2330  }
2331 
2332  n_left--; //At the end for error counting when 'goto done' is invoked
2333  buffers++;
2334  }
2335 
2336 done:
2337  //Do the memory copies
2338  if (PREDICT_FALSE
2339  (vhost_user_tx_copy (vui, vum->cpus[cpu_index].copy,
2340  copy_len, &map_hint)))
2341  {
2342  clib_warning ("Memory mapping error on interface hw_if_index=%d "
2343  "(Shutting down - Switch interface down and up to restart)",
2344  vui->hw_if_index);
2345  vui->admin_up = 0;
2346  }
2347 
2349  rxvq->used->idx = rxvq->last_used_idx;
2350  vhost_user_log_dirty_ring (vui, rxvq, idx);
2351 
2352  /*
2353  * When n_left is set, error is always set to something too.
2354  * In case error is due to lack of remaining buffers, we go back up and
2355  * retry.
2356  * The idea is that it is better to waste some time on packets
2357  * that have been processed already than dropping them and get
2358  * more fresh packets with a good likelyhood that they will be dropped too.
2359  * This technique also gives more time to VM driver to pick-up packets.
2360  * In case the traffic flows from physical to virtual interfaces, this
2361  * technique will end-up leveraging the physical NIC buffer in order to
2362  * absorb the VM's CPU jitter.
2363  */
2364  if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2365  {
2366  retry--;
2367  goto retry;
2368  }
2369 
2370  /* interrupt (call) handling */
2371  if ((rxvq->callfd_idx != ~0) &&
2372  !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
2373  {
2374  rxvq->n_since_last_int += frame->n_vectors - n_left;
2375 
2376  if (rxvq->n_since_last_int > vum->coalesce_frames)
2377  vhost_user_send_call (vm, rxvq);
2378  }
2379 
2380  vhost_user_vring_unlock (vui, qid);
2381 
2382 done3:
2383  if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2384  {
2385  vlib_error_count (vm, node->node_index, error, n_left);
2389  os_get_cpu_number (), vui->sw_if_index, n_left);
2390  }
2391 
2392  vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2393  return frame->n_vectors;
2394 }
2395 
2396 static clib_error_t *
2398  u32 flags)
2399 {
2400  vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2401  uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2403  vhost_user_intf_t *vui =
2405 
2406  vui->admin_up = is_up;
2407 
2408  if (is_up)
2411 
2412  return /* no error */ 0;
2413 }
2414 
2415 /* *INDENT-OFF* */
2416 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2417  .name = "vhost-user",
2418  .tx_function = vhost_user_tx,
2419  .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2420  .tx_function_error_strings = vhost_user_tx_func_error_strings,
2421  .format_device_name = format_vhost_user_interface_name,
2422  .name_renumber = vhost_user_name_renumber,
2423  .admin_up_down_function = vhost_user_interface_admin_up_down,
2424  .format_tx_trace = format_vhost_trace,
2425 };
2426 
2428  vhost_user_tx)
2429 /* *INDENT-ON* */
2430 
2431 static uword
2432 vhost_user_process (vlib_main_t * vm,
2434 {
2436  vhost_user_intf_t *vui;
2437  struct sockaddr_un sun;
2438  int sockfd;
2439  unix_file_t template = { 0 };
2440  f64 timeout = 3153600000.0 /* 100 years */ ;
2441  uword *event_data = 0;
2442 
2443  sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
2444  sun.sun_family = AF_UNIX;
2445  template.read_function = vhost_user_socket_read;
2446  template.error_function = vhost_user_socket_error;
2447 
2448  if (sockfd < 0)
2449  return 0;
2450 
2451  while (1)
2452  {
2454  vlib_process_get_events (vm, &event_data);
2455  vec_reset_length (event_data);
2456 
2457  timeout = 3.0;
2458 
2459  /* *INDENT-OFF* */
2460  pool_foreach (vui, vum->vhost_user_interfaces, {
2461 
2462  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2463  if (vui->unix_file_index == ~0)
2464  {
2465  /* try to connect */
2466  strncpy (sun.sun_path, (char *) vui->sock_filename,
2467  sizeof (sun.sun_path) - 1);
2468 
2469  /* Avoid hanging VPP if the other end does not accept */
2470  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2471  clib_unix_warning ("fcntl");
2472 
2473  if (connect (sockfd, (struct sockaddr *) &sun,
2474  sizeof (struct sockaddr_un)) == 0)
2475  {
2476  /* Set the socket to blocking as it was before */
2477  if (fcntl(sockfd, F_SETFL, 0) < 0)
2478  clib_unix_warning ("fcntl2");
2479 
2480  vui->sock_errno = 0;
2481  template.file_descriptor = sockfd;
2482  template.private_data =
2483  vui - vhost_user_main.vhost_user_interfaces;
2484  vui->unix_file_index = unix_file_add (&unix_main, &template);
2485 
2486  //Re-open for next connect
2487  if ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
2488  clib_warning("Critical: Could not open unix socket");
2489  return 0;
2490  }
2491  }
2492  else
2493  {
2494  vui->sock_errno = errno;
2495  }
2496  }
2497  else
2498  {
2499  /* check if socket is alive */
2500  int error = 0;
2501  socklen_t len = sizeof (error);
2502  int fd = UNIX_GET_FD(vui->unix_file_index);
2503  int retval =
2504  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2505 
2506  if (retval)
2507  {
2508  DBG_SOCK ("getsockopt returned %d", retval);
2509  vhost_user_if_disconnect (vui);
2510  }
2511  }
2512  }
2513  });
2514  /* *INDENT-ON* */
2515  }
2516  return 0;
2517 }
2518 
2519 /* *INDENT-OFF* */
2521  .function = vhost_user_process,
2522  .type = VLIB_NODE_TYPE_PROCESS,
2523  .name = "vhost-user-process",
2524 };
2525 /* *INDENT-ON* */
2526 
2527 /**
2528  * Disables and reset interface structure.
2529  * It can then be either init again, or removed from used interfaces.
2530  */
2531 static void
2533 {
2534  int q;
2535 
2536  // Delete configured thread pinning
2537  vec_reset_length (vui->workers);
2538  // disconnect interface sockets
2541 
2542  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2543  {
2544  clib_mem_free ((void *) vui->vring_locks[q]);
2545  }
2546 
2547  if (vui->unix_server_index != ~0)
2548  {
2549  //Close server socket
2551  vui->unix_server_index);
2552  unix_file_del (&unix_main, uf);
2553  vui->unix_server_index = ~0;
2554  }
2555 }
2556 
2557 int
2559 {
2561  vhost_user_intf_t *vui;
2562  int rv = 0;
2563  vnet_hw_interface_t *hwif;
2564 
2565  if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2566  hwif->dev_class_index != vhost_user_dev_class.index)
2567  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2568 
2569  DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2570  hwif->name, hwif->dev_instance);
2571 
2573 
2574  // Disable and reset interface
2575  vhost_user_term_if (vui);
2576 
2577  // Reset renumbered iface
2578  if (hwif->dev_instance <
2581 
2582  // Delete ethernet interface
2584 
2585  // Back to pool
2586  pool_put (vum->vhost_user_interfaces, vui);
2587 
2588  return rv;
2589 }
2590 
2591 /**
2592  * Open server unix socket on specified sock_filename.
2593  */
2594 static int
2595 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2596 {
2597  int rv = 0;
2598  struct sockaddr_un un = { };
2599  int fd;
2600  /* create listening socket */
2601  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2602  return VNET_API_ERROR_SYSCALL_ERROR_1;
2603 
2604  un.sun_family = AF_UNIX;
2605  strncpy ((char *) un.sun_path, (char *) sock_filename,
2606  sizeof (un.sun_path) - 1);
2607 
2608  /* remove if exists */
2609  unlink ((char *) sock_filename);
2610 
2611  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2612  {
2613  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2614  goto error;
2615  }
2616 
2617  if (listen (fd, 1) == -1)
2618  {
2619  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2620  goto error;
2621  }
2622 
2623  *sock_fd = fd;
2624  return 0;
2625 
2626 error:
2627  close (fd);
2628  return rv;
2629 }
2630 
2631 /**
2632  * Create ethernet interface for vhost user interface.
2633  */
2634 static void
2636  vhost_user_intf_t * vui, u8 * hwaddress)
2637 {
2639  u8 hwaddr[6];
2640  clib_error_t *error;
2641 
2642  /* create hw and sw interface */
2643  if (hwaddress)
2644  {
2645  clib_memcpy (hwaddr, hwaddress, 6);
2646  }
2647  else
2648  {
2649  random_u32 (&vum->random);
2650  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2651  hwaddr[0] = 2;
2652  hwaddr[1] = 0xfe;
2653  }
2654 
2656  (vnm,
2657  vhost_user_dev_class.index,
2658  vui - vum->vhost_user_interfaces /* device instance */ ,
2659  hwaddr /* ethernet address */ ,
2660  &vui->hw_if_index, 0 /* flag change */ );
2661 
2662  if (error)
2663  clib_error_report (error);
2664 
2667 }
2668 
2669 /*
2670  * Initialize vui with specified attributes
2671  */
2672 static void
2674  vhost_user_intf_t * vui,
2675  int server_sock_fd,
2676  const char *sock_filename,
2677  u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
2678 {
2679  vnet_sw_interface_t *sw;
2680  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2681  int q;
2682 
2683  if (server_sock_fd != -1)
2684  {
2685  unix_file_t template = { 0 };
2687  template.file_descriptor = server_sock_fd;
2688  template.private_data = vui - vhost_user_main.vhost_user_interfaces; //hw index
2689  vui->unix_server_index = unix_file_add (&unix_main, &template);
2690  }
2691  else
2692  {
2693  vui->unix_server_index = ~0;
2694  }
2695 
2696  vui->sw_if_index = sw->sw_if_index;
2697  strncpy (vui->sock_filename, sock_filename,
2698  ARRAY_LEN (vui->sock_filename) - 1);
2699  vui->sock_errno = 0;
2700  vui->is_up = 0;
2701  vui->feature_mask = feature_mask;
2702  vui->unix_file_index = ~0;
2703  vui->log_base_addr = 0;
2704  vui->operation_mode = operation_mode;
2705 
2706  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2707  vhost_user_vring_init (vui, q);
2708 
2710 
2711  if (sw_if_index)
2712  *sw_if_index = vui->sw_if_index;
2713 
2714  for (q = 0; q < VHOST_VRING_MAX_N; q++)
2715  {
2718  memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2719  }
2720 
2722  vlib_get_thread_main ()->n_vlib_mains - 1);
2724 }
2725 
2726 static uword
2729 {
2730  vhost_user_intf_t *vui;
2731  f64 timeout = 3153600000.0 /* 100 years */ ;
2732  uword event_type, *event_data = 0;
2735  vhost_cpu_t *vhc;
2736  f64 now, poll_time_remaining;
2737 
2738  while (1)
2739  {
2740  poll_time_remaining =
2742  event_type = vlib_process_get_events (vm, &event_data);
2743  vec_reset_length (event_data);
2744 
2745  /*
2746  * Use the remaining timeout if it is less than coalesce time to avoid
2747  * resetting the existing timer in the middle of expiration
2748  */
2749  timeout = poll_time_remaining;
2750  if (vlib_process_suspend_time_is_zero (timeout) ||
2751  (timeout > vum->coalesce_time))
2752  timeout = vum->coalesce_time;
2753 
2754  now = vlib_time_now (vm);
2755  switch (event_type)
2756  {
2758  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2759  break;
2760  /* fall through */
2761 
2762  case ~0:
2763  vec_foreach (vhc, vum->cpus)
2764  {
2765  u32 cpu_index = vhc - vum->cpus;
2766  f64 next_timeout;
2767 
2768  next_timeout = timeout;
2769  vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
2770  {
2771  vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
2772  vhost_user_vring_t *rxvq =
2773  &vui->vrings[VHOST_VRING_IDX_RX (vhiq->qid)];
2774  vhost_user_vring_t *txvq =
2775  &vui->vrings[VHOST_VRING_IDX_TX (vhiq->qid)];
2776 
2777  if (txvq->n_since_last_int)
2778  {
2779  if (now >= txvq->int_deadline)
2780  vhost_user_send_call (vm, txvq);
2781  else
2782  next_timeout = txvq->int_deadline - now;
2783  }
2784 
2785  if (rxvq->n_since_last_int)
2786  {
2787  if (now >= rxvq->int_deadline)
2788  vhost_user_send_call (vm, rxvq);
2789  else
2790  next_timeout = rxvq->int_deadline - now;
2791  }
2792 
2793  if ((next_timeout < timeout) && (next_timeout > 0.0))
2794  timeout = next_timeout;
2795  }
2796  }
2797  break;
2798 
2799  default:
2800  clib_warning ("BUG: unhandled event type %d", event_type);
2801  break;
2802  }
2803  }
2804  return 0;
2805 }
2806 
2807 /* *INDENT-OFF* */
2810  .type = VLIB_NODE_TYPE_PROCESS,
2811  .name = "vhost-user-send-interrupt-process",
2812 };
2813 /* *INDENT-ON* */
2814 
2815 int
2817  const char *sock_filename,
2818  u8 is_server,
2819  u32 * sw_if_index,
2820  u64 feature_mask,
2821  u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
2822  u8 operation_mode)
2823 {
2824  vhost_user_intf_t *vui = NULL;
2825  u32 sw_if_idx = ~0;
2826  int rv = 0;
2827  int server_sock_fd = -1;
2829 
2830  if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2831  (operation_mode != VHOST_USER_INTERRUPT_MODE))
2832  return VNET_API_ERROR_UNIMPLEMENTED;
2833 
2834  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2835  {
2836  return VNET_API_ERROR_INVALID_ARGUMENT;
2837  }
2838 
2839  if (is_server)
2840  {
2841  if ((rv =
2842  vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2843  {
2844  return rv;
2845  }
2846  }
2847 
2848  pool_get (vhost_user_main.vhost_user_interfaces, vui);
2849 
2850  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2851  vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2852  feature_mask, &sw_if_idx, operation_mode);
2853 
2854  if (renumber)
2855  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2856 
2857  if (sw_if_index)
2858  *sw_if_index = sw_if_idx;
2859 
2860  // Process node must connect
2862 
2863  if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2864  !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2865  (vum->coalesce_frames > 0))
2866  {
2867  vum->interrupt_mode = 1;
2870  }
2871  return rv;
2872 }
2873 
2874 int
2876  const char *sock_filename,
2877  u8 is_server,
2878  u32 sw_if_index,
2879  u64 feature_mask, u8 renumber, u32 custom_dev_instance,
2880  u8 operation_mode)
2881 {
2883  vhost_user_intf_t *vui = NULL;
2884  u32 sw_if_idx = ~0;
2885  int server_sock_fd = -1;
2886  int rv = 0;
2887  vnet_hw_interface_t *hwif;
2888 
2889  if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2890  (operation_mode != VHOST_USER_INTERRUPT_MODE))
2891  return VNET_API_ERROR_UNIMPLEMENTED;
2892  if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2893  hwif->dev_class_index != vhost_user_dev_class.index)
2894  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2895 
2897 
2898  // First try to open server socket
2899  if (is_server)
2900  if ((rv = vhost_user_init_server_sock (sock_filename,
2901  &server_sock_fd)) != 0)
2902  return rv;
2903 
2904  vhost_user_term_if (vui);
2905  vhost_user_vui_init (vnm, vui, server_sock_fd,
2906  sock_filename, feature_mask, &sw_if_idx,
2907  operation_mode);
2908 
2909  if (renumber)
2910  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2911 
2912  // Process node must connect
2914 
2915  if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2916  !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2917  (vum->coalesce_frames > 0))
2918  {
2919  vum->interrupt_mode = 1;
2922  }
2923  return rv;
2924 }
2925 
2926 static uword
2928 {
2929  u8 *operation_mode = va_arg (*args, u8 *);
2930  uword rc = 1;
2931 
2932  if (unformat (input, "interrupt"))
2933  *operation_mode = VHOST_USER_INTERRUPT_MODE;
2934  else if (unformat (input, "polling"))
2935  *operation_mode = VHOST_USER_POLLING_MODE;
2936  else
2937  rc = 0;
2938 
2939  return rc;
2940 }
2941 
2942 clib_error_t *
2944  unformat_input_t * input,
2945  vlib_cli_command_t * cmd)
2946 {
2947  unformat_input_t _line_input, *line_input = &_line_input;
2948  u8 *sock_filename = NULL;
2949  u32 sw_if_index;
2950  u8 is_server = 0;
2951  u64 feature_mask = (u64) ~ (0ULL);
2952  u8 renumber = 0;
2953  u32 custom_dev_instance = ~0;
2954  u8 hwaddr[6];
2955  u8 *hw = NULL;
2956  clib_error_t *error = NULL;
2957  u8 operation_mode = VHOST_USER_POLLING_MODE;
2958 
2959  /* Get a line of input. */
2960  if (!unformat_user (input, unformat_line_input, line_input))
2961  return 0;
2962 
2963  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2964  {
2965  if (unformat (line_input, "socket %s", &sock_filename))
2966  ;
2967  else if (unformat (line_input, "server"))
2968  is_server = 1;
2969  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2970  ;
2971  else
2972  if (unformat
2973  (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2974  hw = hwaddr;
2975  else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2976  {
2977  renumber = 1;
2978  }
2979  else if (unformat (line_input, "mode %U",
2980  unformat_vhost_user_operation_mode, &operation_mode))
2981  ;
2982  else
2983  {
2984  error = clib_error_return (0, "unknown input `%U'",
2985  format_unformat_error, line_input);
2986  goto done;
2987  }
2988  }
2989 
2990  vnet_main_t *vnm = vnet_get_main ();
2991 
2992  int rv;
2993  if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2994  is_server, &sw_if_index, feature_mask,
2995  renumber, custom_dev_instance, hw,
2996  operation_mode)))
2997  {
2998  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
2999  goto done;
3000  }
3001 
3003  sw_if_index);
3004 
3005 done:
3006  vec_free (sock_filename);
3007  unformat_free (line_input);
3008 
3009  return error;
3010 }
3011 
3012 clib_error_t *
3014  unformat_input_t * input,
3015  vlib_cli_command_t * cmd)
3016 {
3017  unformat_input_t _line_input, *line_input = &_line_input;
3018  u32 sw_if_index = ~0;
3019  vnet_main_t *vnm = vnet_get_main ();
3020  clib_error_t *error = NULL;
3021 
3022  /* Get a line of input. */
3023  if (!unformat_user (input, unformat_line_input, line_input))
3024  return 0;
3025 
3026  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3027  {
3028  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3029  ;
3030  else if (unformat
3031  (line_input, "%U", unformat_vnet_sw_interface, vnm,
3032  &sw_if_index))
3033  {
3034  vnet_hw_interface_t *hwif =
3035  vnet_get_sup_hw_interface (vnm, sw_if_index);
3036  if (hwif == NULL ||
3037  vhost_user_dev_class.index != hwif->dev_class_index)
3038  {
3039  error = clib_error_return (0, "Not a vhost interface");
3040  goto done;
3041  }
3042  }
3043  else
3044  {
3045  error = clib_error_return (0, "unknown input `%U'",
3046  format_unformat_error, line_input);
3047  goto done;
3048  }
3049  }
3050 
3051  vhost_user_delete_if (vnm, vm, sw_if_index);
3052 
3053 done:
3054  unformat_free (line_input);
3055 
3056  return error;
3057 }
3058 
3059 int
3061  vhost_user_intf_details_t ** out_vuids)
3062 {
3063  int rv = 0;
3065  vhost_user_intf_t *vui;
3066  vhost_user_intf_details_t *r_vuids = NULL;
3068  u32 *hw_if_indices = 0;
3070  u8 *s = NULL;
3071  int i;
3072 
3073  if (!out_vuids)
3074  return -1;
3075 
3077  vec_add1 (hw_if_indices, vui->hw_if_index);
3078  );
3079 
3080  for (i = 0; i < vec_len (hw_if_indices); i++)
3081  {
3082  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3084 
3085  vec_add2 (r_vuids, vuid, 1);
3086  vuid->operation_mode = vui->operation_mode;
3087  vuid->sw_if_index = vui->sw_if_index;
3088  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3089  vuid->features = vui->features;
3090  vuid->num_regions = vui->nregions;
3091  vuid->is_server = vui->unix_server_index != ~0;
3092  vuid->sock_errno = vui->sock_errno;
3093  strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3094  ARRAY_LEN (vuid->sock_filename) - 1);
3095 
3096  s = format (s, "%v%c", hi->name, 0);
3097 
3098  strncpy ((char *) vuid->if_name, (char *) s,
3099  ARRAY_LEN (vuid->if_name) - 1);
3100  _vec_len (s) = 0;
3101  }
3102 
3103  vec_free (s);
3104  vec_free (hw_if_indices);
3105 
3106  *out_vuids = r_vuids;
3107 
3108  return rv;
3109 }
3110 
3111 static u8 *
3113 {
3114  int operation_mode = va_arg (*va, int);
3115 
3116  switch (operation_mode)
3117  {
3119  s = format (s, "%s", "polling");
3120  break;
3122  s = format (s, "%s", "interrupt");
3123  break;
3124  default:
3125  s = format (s, "%s", "invalid");
3126  }
3127  return s;
3128 }
3129 
3130 clib_error_t *
3132  unformat_input_t * input,
3133  vlib_cli_command_t * cmd)
3134 {
3135  clib_error_t *error = 0;
3136  vnet_main_t *vnm = vnet_get_main ();
3138  vhost_user_intf_t *vui;
3139  u32 hw_if_index, *hw_if_indices = 0;
3141  vhost_cpu_t *vhc;
3143  u32 ci;
3144 
3145  int i, j, q;
3146  int show_descr = 0;
3147  struct feat_struct
3148  {
3149  u8 bit;
3150  char *str;
3151  };
3152  struct feat_struct *feat_entry;
3153 
3154  static struct feat_struct feat_array[] = {
3155 #define _(s,b) { .str = #s, .bit = b, },
3157 #undef _
3158  {.str = NULL}
3159  };
3160 
3161 #define foreach_protocol_feature \
3162  _(VHOST_USER_PROTOCOL_F_MQ) \
3163  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3164 
3165  static struct feat_struct proto_feat_array[] = {
3166 #define _(s) { .str = #s, .bit = s},
3168 #undef _
3169  {.str = NULL}
3170  };
3171 
3172  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3173  {
3174  if (unformat
3175  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3176  {
3177  vec_add1 (hw_if_indices, hw_if_index);
3178  }
3179  else if (unformat (input, "descriptors") || unformat (input, "desc"))
3180  show_descr = 1;
3181  else
3182  {
3183  error = clib_error_return (0, "unknown input `%U'",
3184  format_unformat_error, input);
3185  goto done;
3186  }
3187  }
3188  if (vec_len (hw_if_indices) == 0)
3189  {
3191  vec_add1 (hw_if_indices, vui->hw_if_index);
3192  );
3193  }
3194  vlib_cli_output (vm, "Virtio vhost-user interfaces");
3195  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
3196  vum->coalesce_frames, vum->coalesce_time);
3197 
3198  for (i = 0; i < vec_len (hw_if_indices); i++)
3199  {
3200  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3202  vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3203  hi->name, hw_if_indices[i]);
3204 
3205  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3206  " features mask (0x%llx): \n"
3207  " features (0x%llx): \n",
3208  vui->virtio_net_hdr_sz, vui->feature_mask,
3209  vui->features);
3210 
3211  feat_entry = (struct feat_struct *) &feat_array;
3212  while (feat_entry->str)
3213  {
3214  if (vui->features & (1ULL << feat_entry->bit))
3215  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3216  feat_entry->bit);
3217  feat_entry++;
3218  }
3219 
3220  vlib_cli_output (vm, " protocol features (0x%llx)",
3221  vui->protocol_features);
3222  feat_entry = (struct feat_struct *) &proto_feat_array;
3223  while (feat_entry->str)
3224  {
3225  if (vui->protocol_features & (1ULL << feat_entry->bit))
3226  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3227  feat_entry->bit);
3228  feat_entry++;
3229  }
3230 
3231  vlib_cli_output (vm, "\n");
3232 
3233  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3234  vui->sock_filename,
3235  (vui->unix_server_index != ~0) ? "server" : "client",
3236  strerror (vui->sock_errno));
3237 
3238  vlib_cli_output (vm, " configured mode: %U\n",
3240  vlib_cli_output (vm, " rx placement: ");
3241  vec_foreach (vhc, vum->cpus)
3242  {
3243  vec_foreach (vhiq, vhc->rx_queues)
3244  {
3245  if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
3246  {
3247  vlib_cli_output (vm, " thread %d on vring %d\n",
3248  vhc - vum->cpus,
3249  VHOST_VRING_IDX_TX (vhiq->qid));
3250  vlib_cli_output (vm, " mode: %U\n",
3252  vhc->operation_mode);
3253  }
3254  }
3255  }
3256 
3257  vlib_cli_output (vm, " tx placement: %s\n",
3258  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3259 
3261  {
3262  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3263  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3264  }
3265 
3266  vlib_cli_output (vm, "\n");
3267 
3268  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3269 
3270  if (vui->nregions)
3271  {
3272  vlib_cli_output (vm,
3273  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3274  vlib_cli_output (vm,
3275  " ====== ===== ================== ================== ================== ================== ==================\n");
3276  }
3277  for (j = 0; j < vui->nregions; j++)
3278  {
3279  vlib_cli_output (vm,
3280  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3281  j, vui->region_mmap_fd[j],
3282  vui->regions[j].guest_phys_addr,
3283  vui->regions[j].memory_size,
3284  vui->regions[j].userspace_addr,
3285  vui->regions[j].mmap_offset,
3287  }
3288  for (q = 0; q < VHOST_VRING_MAX_N; q++)
3289  {
3290  if (!vui->vrings[q].started)
3291  continue;
3292 
3293  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3294  (q & 1) ? "RX" : "TX",
3295  vui->vrings[q].enabled ? "" : " disabled");
3296 
3297  vlib_cli_output (vm,
3298  " qsz %d last_avail_idx %d last_used_idx %d\n",
3299  vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3300  vui->vrings[q].last_used_idx);
3301 
3302  if (vui->vrings[q].avail && vui->vrings[q].used)
3303  vlib_cli_output (vm,
3304  " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3305  vui->vrings[q].avail->flags,
3306  vui->vrings[q].avail->idx,
3307  vui->vrings[q].used->flags,
3308  vui->vrings[q].used->idx);
3309 
3310  int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3311  int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3312  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3313  kickfd, callfd, vui->vrings[q].errfd);
3314 
3315  if (show_descr)
3316  {
3317  vlib_cli_output (vm, "\n descriptor table:\n");
3318  vlib_cli_output (vm,
3319  " id addr len flags next user_addr\n");
3320  vlib_cli_output (vm,
3321  " ===== ================== ===== ====== ===== ==================\n");
3322  for (j = 0; j < vui->vrings[q].qsz; j++)
3323  {
3324  u32 mem_hint = 0;
3325  vlib_cli_output (vm,
3326  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3327  j, vui->vrings[q].desc[j].addr,
3328  vui->vrings[q].desc[j].len,
3329  vui->vrings[q].desc[j].flags,
3330  vui->vrings[q].desc[j].next,
3332  (vui,
3333  vui->vrings[q].desc[j].
3334  addr, &mem_hint)));
3335  }
3336  }
3337  }
3338  vlib_cli_output (vm, "\n");
3339  }
3340 done:
3341  vec_free (hw_if_indices);
3342  return error;
3343 }
3344 
3345 /*
3346  * CLI functions
3347  */
3348 
3349 /*?
3350  * Create a vHost User interface. Once created, a new virtual interface
3351  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3352  * is the next free index.
3353  *
3354  * There are several parameters associated with a vHost interface:
3355  *
3356  * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3357  * VPP to manage the vHost interface. If socket does not already exist, VPP will
3358  * create the socket.
3359  *
3360  * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3361  * linux socket. If not provided, VPP will be the client.
3362  *
3363  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3364  * startup. By default, all supported features will be advertised. Otherwise,
3365  * provide the set of features desired.
3366  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3367  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3368  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3369  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3370  * - 0x004000000 (26) - VHOST_F_LOG_ALL
3371  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3372  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3373  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3374  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3375  *
3376  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3377  * X:X:X:X:X:X unix or X.X.X cisco format.
3378  *
3379  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3380  * in the name to be specified. If instance already exists, name will be used
3381  * anyway and multiple instances will have the same name. Use with caution.
3382  *
3383  * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3384  * the input thread polling policy.
3385  *
3386  * @cliexpar
3387  * Example of how to create a vhost interface with VPP as the client and all features enabled:
3388  * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3389  * VirtualEthernet0/0/0
3390  * @cliexend
3391  * Example of how to create a vhost interface with VPP as the server and with just
3392  * multiple queues enabled:
3393  * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3394  * VirtualEthernet0/0/1
3395  * @cliexend
3396  * Once the vHost interface is created, enable the interface using:
3397  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3398 ?*/
3399 /* *INDENT-OFF* */
3400 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3401  .path = "create vhost-user",
3402  .short_help = "create vhost-user socket <socket-filename> [server] "
3403  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] "
3404  "[mode {interrupt | polling}]",
3405  .function = vhost_user_connect_command_fn,
3406 };
3407 /* *INDENT-ON* */
3408 
3409 /*?
3410  * Delete a vHost User interface using the interface name or the
3411  * software interface index. Use the '<em>show interface</em>'
3412  * command to determine the software interface index. On deletion,
3413  * the linux socket will not be deleted.
3414  *
3415  * @cliexpar
3416  * Example of how to delete a vhost interface by name:
3417  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3418  * Example of how to delete a vhost interface by software interface index:
3419  * @cliexcmd{delete vhost-user sw_if_index 1}
3420 ?*/
3421 /* *INDENT-OFF* */
3422 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3423  .path = "delete vhost-user",
3424  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3425  .function = vhost_user_delete_command_fn,
3426 };
3427 
3428 /*?
3429  * Display the attributes of a single vHost User interface (provide interface
3430  * name), multiple vHost User interfaces (provide a list of interface names seperated
3431  * by spaces) or all Vhost User interfaces (omit an interface name to display all
3432  * vHost interfaces).
3433  *
3434  * @cliexpar
3435  * @parblock
3436  * Example of how to display a vhost interface:
3437  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3438  * Virtio vhost-user interfaces
3439  * Global:
3440  * coalesce frames 32 time 1e-3
3441  * Interface: VirtualEthernet0/0/0 (ifindex 1)
3442  * virtio_net_hdr_sz 12
3443  * features mask (0xffffffffffffffff):
3444  * features (0x50408000):
3445  * VIRTIO_NET_F_MRG_RXBUF (15)
3446  * VIRTIO_NET_F_MQ (22)
3447  * VIRTIO_F_INDIRECT_DESC (28)
3448  * VHOST_USER_F_PROTOCOL_FEATURES (30)
3449  * protocol features (0x3)
3450  * VHOST_USER_PROTOCOL_F_MQ (0)
3451  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3452  *
3453  * socket filename /tmp/vhost1.sock type client errno "Success"
3454  *
3455  * rx placement:
3456  * thread 1 on vring 1
3457  * thread 1 on vring 5
3458  * thread 2 on vring 3
3459  * thread 2 on vring 7
3460  * tx placement: spin-lock
3461  * thread 0 on vring 0
3462  * thread 1 on vring 2
3463  * thread 2 on vring 0
3464  *
3465  * Memory regions (total 2)
3466  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3467  * ====== ===== ================== ================== ================== ================== ==================
3468  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3469  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3470  *
3471  * Virtqueue 0 (TX)
3472  * qsz 256 last_avail_idx 0 last_used_idx 0
3473  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3474  * kickfd 62 callfd 64 errfd -1
3475  *
3476  * Virtqueue 1 (RX)
3477  * qsz 256 last_avail_idx 0 last_used_idx 0
3478  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3479  * kickfd 65 callfd 66 errfd -1
3480  *
3481  * Virtqueue 2 (TX)
3482  * qsz 256 last_avail_idx 0 last_used_idx 0
3483  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3484  * kickfd 63 callfd 70 errfd -1
3485  *
3486  * Virtqueue 3 (RX)
3487  * qsz 256 last_avail_idx 0 last_used_idx 0
3488  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3489  * kickfd 72 callfd 74 errfd -1
3490  *
3491  * Virtqueue 4 (TX disabled)
3492  * qsz 256 last_avail_idx 0 last_used_idx 0
3493  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3494  * kickfd 76 callfd 78 errfd -1
3495  *
3496  * Virtqueue 5 (RX disabled)
3497  * qsz 256 last_avail_idx 0 last_used_idx 0
3498  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3499  * kickfd 80 callfd 82 errfd -1
3500  *
3501  * Virtqueue 6 (TX disabled)
3502  * qsz 256 last_avail_idx 0 last_used_idx 0
3503  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3504  * kickfd 84 callfd 86 errfd -1
3505  *
3506  * Virtqueue 7 (RX disabled)
3507  * qsz 256 last_avail_idx 0 last_used_idx 0
3508  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3509  * kickfd 88 callfd 90 errfd -1
3510  *
3511  * @cliexend
3512  *
3513  * The optional '<em>descriptors</em>' parameter will display the same output as
3514  * the previous example but will include the descriptor table for each queue.
3515  * The output is truncated below:
3516  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3517  * Virtio vhost-user interfaces
3518  * Global:
3519  * coalesce frames 32 time 1e-3
3520  * Interface: VirtualEthernet0/0/0 (ifindex 1)
3521  * virtio_net_hdr_sz 12
3522  * features mask (0xffffffffffffffff):
3523  * features (0x50408000):
3524  * VIRTIO_NET_F_MRG_RXBUF (15)
3525  * VIRTIO_NET_F_MQ (22)
3526  * :
3527  * Virtqueue 0 (TX)
3528  * qsz 256 last_avail_idx 0 last_used_idx 0
3529  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3530  * kickfd 62 callfd 64 errfd -1
3531  *
3532  * descriptor table:
3533  * id addr len flags next user_addr
3534  * ===== ================== ===== ====== ===== ==================
3535  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3536  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3537  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3538  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3539  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3540  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3541  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3542  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3543  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3544  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3545  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3546  * :
3547  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3548  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3549  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3550  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3551  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3552  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3553  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3554  *
3555  * Virtqueue 1 (RX)
3556  * qsz 256 last_avail_idx 0 last_used_idx 0
3557  * :
3558  * @cliexend
3559  * @endparblock
3560 ?*/
3561 /* *INDENT-OFF* */
3562 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3563  .path = "show vhost-user",
3564  .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3565  .function = show_vhost_user_command_fn,
3566 };
3567 /* *INDENT-ON* */
3568 
3569 static clib_error_t *
3571 {
3573 
3574  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3575  {
3576  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3577  ;
3578  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3579  ;
3580  else if (unformat (input, "dont-dump-memory"))
3581  vum->dont_dump_vhost_user_memory = 1;
3582  else
3583  return clib_error_return (0, "unknown input `%U'",
3584  format_unformat_error, input);
3585  }
3586 
3587  return 0;
3588 }
3589 
3590 /* vhost-user { ... } configuration. */
3591 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3592 
3593 void
3595 {
3597  vhost_user_intf_t *vui;
3598 
3599  if (vum->dont_dump_vhost_user_memory)
3600  {
3602  unmap_all_mem_regions (vui);
3603  );
3604  }
3605 }
3606 
3607 static clib_error_t *
3609  unformat_input_t * input, vlib_cli_command_t * cmd)
3610 {
3611  unformat_input_t _line_input, *line_input = &_line_input;
3612  u32 worker_thread_index;
3613  u32 sw_if_index;
3614  u8 del = 0;
3615  int rv;
3616  clib_error_t *error = NULL;
3617 
3618  /* Get a line of input. */
3619  if (!unformat_user (input, unformat_line_input, line_input))
3620  return 0;
3621 
3622  if (!unformat
3623  (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3624  &sw_if_index, &worker_thread_index))
3625  {
3626  error = clib_error_return (0, "unknown input `%U'",
3627  format_unformat_error, line_input);
3628  goto done;
3629  }
3630 
3631  if (unformat (line_input, "del"))
3632  del = 1;
3633 
3634  if ((rv =
3635  vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
3636  {
3637  error = clib_error_return (0, "vhost_user_thread_placement returned %d",
3638  rv);
3639  goto done;
3640  }
3641 
3642 done:
3643  unformat_free (line_input);
3644 
3645  return error;
3646 }
3647 
3648 
3649 /*?
3650  * This command is used to move the RX processing for the given
3651  * interfaces to the provided thread. If the '<em>del</em>' option is used,
3652  * the forced thread assignment is removed and the thread assigment is
3653  * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3654  * to see the thread assignment.
3655  *
3656  * @cliexpar
3657  * Example of how to move the RX processing for a given interface to a given thread:
3658  * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3659  * Example of how to remove the forced thread assignment for a given interface:
3660  * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3661 ?*/
3662 /* *INDENT-OFF* */
3663 VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3664  .path = "vhost thread",
3665  .short_help = "vhost thread <iface> <worker-index> [del]",
3666  .function = vhost_thread_command_fn,
3667 };
3668 /* *INDENT-ON* */
3669 
3670 /*
3671  * fd.io coding-style-patch-verification: ON
3672  *
3673  * Local Variables:
3674  * eval: (c-set-style "gnu")
3675  * End:
3676  */
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:2635
#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:2532
#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:1944
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:1239
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:2397
#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:2816
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:3608
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:3131
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:3112
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:2875
#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:2595
static uword vhost_user_send_interrupt_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost-user.c:2727
VLIB_DEVICE_TX_FUNCTION_MULTIARCH(vhost_user_dev_class, vhost_user_tx)
Definition: vhost-user.c:2427
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:2558
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:2673
#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:2002
#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:2520
void vhost_user_unmap_all(void)
Definition: vhost-user.c:3594
#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:3060
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:2927
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:2808
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:2086
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:3013
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:3570
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:1581
#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:2038
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:2943
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