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