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