dpdk: remove support for dpdk 2.2
[vpp.git] / vnet / vnet / devices / dpdk / vhost_user.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <assert.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <sys/stat.h>
19 #include <sys/vfs.h>
20
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23
24 #include <vnet/vnet.h>
25 #include <vppinfra/vec.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/format.h>
28
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/devices/dpdk/dpdk.h>
31
32 #include <vnet/devices/virtio/vhost-user.h>
33
34 #define VHOST_USER_DEBUG_SOCKET 0
35
36 #if VHOST_USER_DEBUG_SOCKET == 1
37 #define DBG_SOCK(args...) clib_warning(args);
38 #else
39 #define DBG_SOCK(args...)
40 #endif
41
42 #if DPDK_VHOST_USER
43
44 /* *INDENT-OFF* */
45 static const char *vhost_message_str[] __attribute__ ((unused)) =
46 {
47     [VHOST_USER_NONE] = "VHOST_USER_NONE",
48     [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
49     [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
50     [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
51     [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
52     [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
53     [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
54     [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
55     [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
56     [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
57     [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
58     [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
59     [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
60     [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
61     [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
62     [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
63     [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
64     [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
65     [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
66 };
67 /* *INDENT-ON* */
68
69 static int dpdk_vhost_user_set_vring_enable (u32 hw_if_index,
70                                              u8 idx, int enable);
71
72 /*
73  * DPDK vhost-user functions
74  */
75
76 /* portions taken from dpdk
77  *   BSD LICENSE
78  *
79  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
80  *   All rights reserved.
81  *
82  *   Redistribution and use in source and binary forms, with or without
83  *   modification, are permitted provided that the following conditions
84  *   are met:
85  *
86  *     * Redistributions of source code must retain the above copyright
87  *       notice, this list of conditions and the following disclaimer.
88  *     * Redistributions in binary form must reproduce the above copyright
89  *       notice, this list of conditions and the following disclaimer in
90  *       the documentation and/or other materials provided with the
91  *       distribution.
92  *     * Neither the name of Intel Corporation nor the names of its
93  *       contributors may be used to endorse or promote products derived
94  *       from this software without specific prior written permission.
95  *
96  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
97  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
98  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
99  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
100  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
101  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
102  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
103  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
104  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
105  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
106  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107  */
108
109
110 static uword
111 qva_to_vva (struct virtio_net *dev, uword qemu_va)
112 {
113   struct virtio_memory_regions *region;
114   uword vhost_va = 0;
115   uint32_t regionidx = 0;
116
117   /* Find the region where the address lives. */
118   for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++)
119     {
120       region = &dev->mem->regions[regionidx];
121       if ((qemu_va >= region->userspace_address) &&
122           (qemu_va <= region->userspace_address + region->memory_size))
123         {
124           vhost_va = qemu_va + region->guest_phys_address +
125             region->address_offset - region->userspace_address;
126           break;
127         }
128     }
129   return vhost_va;
130 }
131
132 static dpdk_device_t *
133 dpdk_vhost_user_device_from_hw_if_index (u32 hw_if_index)
134 {
135   vnet_main_t *vnm = vnet_get_main ();
136   dpdk_main_t *dm = &dpdk_main;
137   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
138   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
139
140   if ((xd->flags DPDK_DEVICE_FLAG_VHOST_USER) == 0)
141     return 0;
142
143   return xd;
144 }
145
146 static dpdk_device_t *
147 dpdk_vhost_user_device_from_sw_if_index (u32 sw_if_index)
148 {
149   vnet_main_t *vnm = vnet_get_main ();
150   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
151   ASSERT (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
152
153   return dpdk_vhost_user_device_from_hw_if_index (sw->hw_if_index);
154 }
155
156 static void
157 stop_processing_packets (u32 hw_if_index, u8 idx)
158 {
159   dpdk_device_t *xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index);
160   assert (xd);
161   xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
162 }
163
164 static void
165 disable_interface (dpdk_device_t * xd)
166 {
167   u8 idx;
168   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
169   for (idx = 0; idx < numqs; idx++)
170     xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
171
172   xd->vu_is_running = 0;
173 }
174
175 static inline void *
176 map_guest_mem (dpdk_device_t * xd, uword addr)
177 {
178   dpdk_vu_intf_t *vui = xd->vu_intf;
179   struct virtio_memory *mem = xd->vu_vhost_dev.mem;
180   int i;
181   for (i = 0; i < mem->nregions; i++)
182     {
183       if ((mem->regions[i].guest_phys_address <= addr) &&
184           ((mem->regions[i].guest_phys_address +
185             mem->regions[i].memory_size) > addr))
186         {
187           return (void *) ((uword) vui->region_addr[i] + addr -
188                            (uword) mem->regions[i].guest_phys_address);
189         }
190     }
191   DBG_SOCK ("failed to map guest mem addr %lx", addr);
192   return 0;
193 }
194
195 static clib_error_t *
196 dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 * hwaddr)
197 {
198   dpdk_main_t *dm = &dpdk_main;
199   vlib_main_t *vm = vlib_get_main ();
200   vlib_thread_main_t *tm = vlib_get_thread_main ();
201   vnet_sw_interface_t *sw;
202   clib_error_t *error;
203   dpdk_device_and_queue_t *dq;
204   int num_qpairs = 1;
205   dpdk_vu_intf_t *vui = NULL;
206
207   num_qpairs = dm->use_rss < 1 ? 1 : tm->n_vlib_mains;
208
209   dpdk_device_t *xd = NULL;
210   u8 addr[6];
211   int j;
212
213   vlib_worker_thread_barrier_sync (vm);
214
215   int inactive_cnt = vec_len (dm->vu_inactive_interfaces_device_index);
216   // if there are any inactive ifaces
217   if (inactive_cnt > 0)
218     {
219       // take last
220       u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
221       if (vec_len (dm->devices) > vui_idx)
222         {
223           xd = vec_elt_at_index (dm->devices, vui_idx);
224           if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
225             {
226               DBG_SOCK
227                 ("reusing inactive vhost-user interface sw_if_index %d",
228                  xd->vlib_sw_if_index);
229             }
230           else
231             {
232               clib_warning
233                 ("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
234                  xd->vlib_sw_if_index);
235               // reset so new interface is created
236               xd = NULL;
237             }
238         }
239       // "remove" from inactive list
240       _vec_len (dm->vu_inactive_interfaces_device_index) -= 1;
241     }
242
243   if (xd)
244     {
245       // existing interface used - do not overwrite if_id if not needed
246       if (if_id != (u32) ~ 0)
247         xd->vu_if_id = if_id;
248
249       // reset virtqueues
250       vui = xd->vu_intf;
251       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++)
252         {
253           memset (xd->vu_vhost_dev.virtqueue[j], 0,
254                   sizeof (struct vhost_virtqueue));
255           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
256           xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
257           xd->vu_vhost_dev.virtqueue[j]->backend = -1;
258           vui->vrings[j].packets = 0;
259           vui->vrings[j].bytes = 0;
260         }
261
262       // reset lockp
263       dpdk_device_lock_free (xd);
264       dpdk_device_lock_init (xd);
265
266       // reset tx vectors
267       for (j = 0; j < tm->n_vlib_mains; j++)
268         {
269           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
270                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
271           vec_reset_length (xd->tx_vectors[j]);
272         }
273
274       // reset rx vector
275       for (j = 0; j < xd->rx_q_used; j++)
276         {
277           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
278                                 CLIB_CACHE_LINE_BYTES);
279           vec_reset_length (xd->rx_vectors[j]);
280         }
281     }
282   else
283     {
284       // vui was not retrieved from inactive ifaces - create new
285       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
286       xd->flags |= DPDK_DEVICE_FLAG_VHOST_USER;
287       xd->rx_q_used = num_qpairs;
288       xd->tx_q_used = num_qpairs;
289       xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
290
291       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
292                             CLIB_CACHE_LINE_BYTES);
293
294       if (if_id == (u32) ~ 0)
295         xd->vu_if_id = dm->next_vu_if_id++;
296       else
297         xd->vu_if_id = if_id;
298
299       xd->device_index = xd - dm->devices;
300       xd->per_interface_next_index = ~0;
301       xd->vu_intf = clib_mem_alloc (sizeof (*(xd->vu_intf)));
302
303       xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof (struct virtio_memory) +
304                                              VHOST_MEMORY_MAX_NREGIONS *
305                                              sizeof (struct
306                                                      virtio_memory_regions));
307
308       /* Will be set when guest sends VHOST_USER_SET_MEM_TABLE cmd */
309       xd->vu_vhost_dev.mem->nregions = 0;
310
311       /*
312        * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
313        * We need to allocate numq pairs.
314        */
315       vui = xd->vu_intf;
316       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++)
317         {
318           xd->vu_vhost_dev.virtqueue[j] =
319             clib_mem_alloc (sizeof (struct vhost_virtqueue));
320           memset (xd->vu_vhost_dev.virtqueue[j], 0,
321                   sizeof (struct vhost_virtqueue));
322           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
323           xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
324           xd->vu_vhost_dev.virtqueue[j]->backend = -1;
325           vui->vrings[j].packets = 0;
326           vui->vrings[j].bytes = 0;
327         }
328
329       dpdk_device_lock_init (xd);
330
331       DBG_SOCK
332         ("tm->n_vlib_mains: %d. TX %d, RX: %d, num_qpairs: %d, Lock: %p",
333          tm->n_vlib_mains, xd->tx_q_used, xd->rx_q_used, num_qpairs,
334          xd->lockp);
335
336       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
337                             CLIB_CACHE_LINE_BYTES);
338
339       for (j = 0; j < tm->n_vlib_mains; j++)
340         {
341           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
342                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
343           vec_reset_length (xd->tx_vectors[j]);
344         }
345
346       // reset rx vector
347       for (j = 0; j < xd->rx_q_used; j++)
348         {
349           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
350                                 CLIB_CACHE_LINE_BYTES);
351           vec_reset_length (xd->rx_vectors[j]);
352         }
353
354     }
355   /*
356    * Generate random MAC address for the interface
357    */
358   if (hwaddr)
359     {
360       clib_memcpy (addr, hwaddr, sizeof (addr));
361     }
362   else
363     {
364       f64 now = vlib_time_now (vm);
365       u32 rnd;
366       rnd = (u32) (now * 1e6);
367       rnd = random_u32 (&rnd);
368
369       clib_memcpy (addr + 2, &rnd, sizeof (rnd));
370       addr[0] = 2;
371       addr[1] = 0xfe;
372     }
373
374   error = ethernet_register_interface
375     (dm->vnet_main, dpdk_device_class.index, xd->device_index,
376      /* ethernet address */ addr,
377      &xd->vlib_hw_if_index, 0);
378
379   if (error)
380     return error;
381
382   sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
383   xd->vlib_sw_if_index = sw->sw_if_index;
384
385   *hw_if_index = xd->vlib_hw_if_index;
386
387   DBG_SOCK ("xd->device_index: %d, dm->input_cpu_count: %d, "
388             "dm->input_cpu_first_index: %d\n", xd->device_index,
389             dm->input_cpu_count, dm->input_cpu_first_index);
390
391   int q, next_cpu = 0;
392   for (q = 0; q < num_qpairs; q++)
393     {
394       int cpu = dm->input_cpu_first_index + (next_cpu % dm->input_cpu_count);
395
396       unsigned lcore = vlib_worker_threads[cpu].lcore_id;
397       vec_validate (xd->cpu_socket_id_by_queue, q);
398       xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
399
400       vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
401       dq->device = xd->device_index;
402       dq->queue_id = q;
403       DBG_SOCK ("CPU for %d = %d. QID: %d", *hw_if_index, cpu, dq->queue_id);
404
405       // start polling if it was not started yet (because of no phys ifaces)
406       if (tm->n_vlib_mains == 1
407           && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
408         vlib_node_set_state (vm, dpdk_input_node.index,
409                              VLIB_NODE_STATE_POLLING);
410
411       if (tm->n_vlib_mains > 1)
412         vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
413                              VLIB_NODE_STATE_POLLING);
414       next_cpu++;
415     }
416
417   vlib_worker_thread_barrier_release (vm);
418   return 0;
419 }
420
421 static long
422 get_huge_page_size (int fd)
423 {
424   struct statfs s;
425   fstatfs (fd, &s);
426   return s.f_bsize;
427 }
428
429 static clib_error_t *
430 dpdk_vhost_user_set_protocol_features (u32 hw_if_index, u64 prot_features)
431 {
432   dpdk_device_t *xd;
433   xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index);
434   assert (xd);
435   xd->vu_vhost_dev.protocol_features = prot_features;
436   return 0;
437 }
438
439 static clib_error_t *
440 dpdk_vhost_user_get_features (u32 hw_if_index, u64 * features)
441 {
442   *features = rte_vhost_feature_get ();
443
444 #define OFFLOAD_FEATURES ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
445                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
446                 (1ULL << VIRTIO_NET_F_CSUM)    | \
447                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
448                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
449                 (1ULL << VIRTIO_NET_F_GUEST_TSO6))
450
451   /* These are not suppoted as bridging/tunneling VHOST
452    * interfaces with hardware interfaces/drivers that does
453    * not support offloading breaks L4 traffic.
454    */
455   *features &= (~OFFLOAD_FEATURES);
456
457   DBG_SOCK ("supported features: 0x%lx", *features);
458   return 0;
459 }
460
461 static clib_error_t *
462 dpdk_vhost_user_set_features (u32 hw_if_index, u64 features)
463 {
464   dpdk_device_t *xd;
465   u16 hdr_len = sizeof (struct virtio_net_hdr);
466
467
468   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
469     {
470       clib_warning ("not a vhost-user interface");
471       return 0;
472     }
473
474   xd->vu_vhost_dev.features = features;
475
476   if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
477     hdr_len = sizeof (struct virtio_net_hdr_mrg_rxbuf);
478
479   int numqs = VIRTIO_QNUM;
480   u8 idx;
481   int prot_feature = features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
482   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
483   for (idx = 0; idx < numqs; idx++)
484     {
485       xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
486       /*
487        * Spec says, if F_PROTOCOL_FEATURE is not set by the
488        * slave, then all the vrings should start off as
489        * enabled. If slave negotiates F_PROTOCOL_FEATURE, then
490        * slave is responsible to enable it.
491        */
492       if (!prot_feature)
493         dpdk_vhost_user_set_vring_enable (hw_if_index, idx, 1);
494     }
495
496   return 0;
497 }
498
499 static clib_error_t *
500 dpdk_vhost_user_set_mem_table (u32 hw_if_index, vhost_user_memory_t * vum,
501                                int fd[])
502 {
503   struct virtio_memory *mem;
504   int i;
505   dpdk_device_t *xd;
506   dpdk_vu_intf_t *vui;
507
508   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
509     {
510       clib_warning ("not a vhost-user interface");
511       return 0;
512     }
513
514   vui = xd->vu_intf;
515   mem = xd->vu_vhost_dev.mem;
516
517   mem->nregions = vum->nregions;
518
519   for (i = 0; i < mem->nregions; i++)
520     {
521       u64 mapped_size, mapped_address;
522
523       mem->regions[i].guest_phys_address = vum->regions[i].guest_phys_addr;
524       mem->regions[i].guest_phys_address_end =
525         vum->regions[i].guest_phys_addr + vum->regions[i].memory_size;
526       mem->regions[i].memory_size = vum->regions[i].memory_size;
527       mem->regions[i].userspace_address = vum->regions[i].userspace_addr;
528
529       mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
530       mapped_address =
531         pointer_to_uword (mmap
532                           (NULL, mapped_size, PROT_READ | PROT_WRITE,
533                            MAP_SHARED, fd[i], 0));
534
535       if (uword_to_pointer (mapped_address, void *) == MAP_FAILED)
536         {
537           clib_warning ("mmap error");
538           return 0;
539         }
540
541       mapped_address += vum->regions[i].mmap_offset;
542       vui->region_addr[i] = mapped_address;
543       vui->region_fd[i] = fd[i];
544       vui->region_offset[i] = vum->regions[i].mmap_offset;
545       mem->regions[i].address_offset =
546         mapped_address - mem->regions[i].guest_phys_address;
547
548       DBG_SOCK ("map memory region %d addr 0x%lx off 0x%lx len 0x%lx",
549                 i, vui->region_addr[i], vui->region_offset[i], mapped_size);
550
551       if (vum->regions[i].guest_phys_addr == 0)
552         {
553           mem->base_address = vum->regions[i].userspace_addr;
554           mem->mapped_address = mem->regions[i].address_offset;
555         }
556     }
557
558   disable_interface (xd);
559   return 0;
560 }
561
562 static clib_error_t *
563 dpdk_vhost_user_set_vring_num (u32 hw_if_index, u8 idx, u32 num)
564 {
565   dpdk_device_t *xd;
566   struct vhost_virtqueue *vq;
567
568   DBG_SOCK ("idx %u num %u", idx, num);
569
570   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
571     {
572       clib_warning ("not a vhost-user interface");
573       return 0;
574     }
575   vq = xd->vu_vhost_dev.virtqueue[idx];
576   vq->size = num;
577
578   stop_processing_packets (hw_if_index, idx);
579
580   return 0;
581 }
582
583 static clib_error_t *
584 dpdk_vhost_user_set_vring_addr (u32 hw_if_index, u8 idx, uword desc,
585                                 uword used, uword avail, uword log)
586 {
587   dpdk_device_t *xd;
588   struct vhost_virtqueue *vq;
589
590   DBG_SOCK ("idx %u desc 0x%lx used 0x%lx avail 0x%lx log 0x%lx",
591             idx, desc, used, avail, log);
592
593   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
594     {
595       clib_warning ("not a vhost-user interface");
596       return 0;
597     }
598   vq = xd->vu_vhost_dev.virtqueue[idx];
599
600   vq->desc = (struct vring_desc *) qva_to_vva (&xd->vu_vhost_dev, desc);
601   vq->used = (struct vring_used *) qva_to_vva (&xd->vu_vhost_dev, used);
602   vq->avail = (struct vring_avail *) qva_to_vva (&xd->vu_vhost_dev, avail);
603   vq->log_guest_addr = log;
604
605   if (!(vq->desc && vq->used && vq->avail))
606     {
607       clib_warning ("falied to set vring addr");
608     }
609
610   if (vq->last_used_idx != vq->used->idx)
611     {
612       clib_warning ("last_used_idx (%u) and vq->used->idx (%u) mismatches; "
613                     "some packets maybe resent for Tx and dropped for Rx",
614                     vq->last_used_idx, vq->used->idx);
615       vq->last_used_idx = vq->used->idx;
616       vq->last_used_idx_res = vq->used->idx;
617     }
618
619   /*
620    * Inform the guest that there is no need to inform (kick) the
621    * host when it adds buffers. kick results in vmexit and will
622    * incur performance degradation.
623    *
624    * The below function sets a flag in used table. Therefore,
625    * should be initialized after initializing vq->used.
626    */
627   rte_vhost_enable_guest_notification (&xd->vu_vhost_dev, idx, 0);
628   stop_processing_packets (hw_if_index, idx);
629
630   return 0;
631 }
632
633 static clib_error_t *
634 dpdk_vhost_user_get_vring_base (u32 hw_if_index, u8 idx, u32 * num)
635 {
636   dpdk_device_t *xd;
637   struct vhost_virtqueue *vq;
638
639   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
640     {
641       clib_warning ("not a vhost-user interface");
642       return 0;
643     }
644
645   vq = xd->vu_vhost_dev.virtqueue[idx];
646   *num = vq->last_used_idx;
647
648 /*
649  * From spec:
650  * Client must start ring upon receiving a kick
651  * (that is, detecting that file descriptor is readable)
652  * on the descriptor specified by VHOST_USER_SET_VRING_KICK,
653  * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
654  */
655   DBG_SOCK ("Stopping vring Q %u of device %d", idx, hw_if_index);
656   dpdk_vu_intf_t *vui = xd->vu_intf;
657
658   /* if there is old fd, delete it */
659   if (vui->vrings[idx].callfd > 0)
660     {
661       unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
662                                            vui->vrings[idx].callfd_idx);
663       unix_file_del (&unix_main, uf);
664     }
665
666   vui->vrings[idx].enabled = 0; /* Reset local copy */
667   vui->vrings[idx].callfd = -1; /* Reset FD */
668   vq->enabled = 0;
669   vq->desc = NULL;
670   vq->used = NULL;
671   vq->avail = NULL;
672   vq->log_guest_addr = 0;
673
674   /* Check if all Qs are disabled */
675   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
676   for (idx = 0; idx < numqs; idx++)
677     {
678       if (xd->vu_vhost_dev.virtqueue[idx]->enabled)
679         break;
680     }
681
682   /* If all vrings are disabed then disable device */
683   if (idx == numqs)
684     {
685       DBG_SOCK ("Device %d disabled", hw_if_index);
686       xd->vu_is_running = 0;
687     }
688
689   return 0;
690 }
691
692 static clib_error_t *
693 dpdk_vhost_user_set_vring_base (u32 hw_if_index, u8 idx, u32 num)
694 {
695   dpdk_device_t *xd;
696   struct vhost_virtqueue *vq;
697
698   DBG_SOCK ("idx %u num %u", idx, num);
699
700   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
701     {
702       clib_warning ("not a vhost-user interface");
703       return 0;
704     }
705
706   vq = xd->vu_vhost_dev.virtqueue[idx];
707   vq->last_used_idx = num;
708   vq->last_used_idx_res = num;
709
710   stop_processing_packets (hw_if_index, idx);
711
712   return 0;
713 }
714
715 static clib_error_t *
716 dpdk_vhost_user_set_vring_kick (u32 hw_if_index, u8 idx, int fd)
717 {
718   dpdk_main_t *dm = &dpdk_main;
719   dpdk_device_t *xd;
720   dpdk_vu_vring *vring;
721   struct vhost_virtqueue *vq0, *vq1, *vq;
722   int index, vu_is_running = 0;
723
724   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
725     {
726       clib_warning ("not a vhost-user interface");
727       return 0;
728     }
729
730   vq = xd->vu_vhost_dev.virtqueue[idx];
731   vq->kickfd = fd;
732
733   vring = &xd->vu_intf->vrings[idx];
734   vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
735
736   /*
737    * Set xd->vu_is_running if at least one pair of
738    * RX/TX queues are enabled.
739    */
740   int numqs = VIRTIO_QNUM;
741   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
742
743   for (index = 0; index < numqs; index += 2)
744     {
745       vq0 = xd->vu_vhost_dev.virtqueue[index];  /* RX */
746       vq1 = xd->vu_vhost_dev.virtqueue[index + 1];      /* TX */
747       if (vq0->enabled && vq1->enabled)
748         {
749           vu_is_running = 1;
750           break;
751         }
752     }
753   DBG_SOCK ("SET_VRING_KICK - idx %d, running %d, fd: %d",
754             idx, vu_is_running, fd);
755
756   xd->vu_is_running = vu_is_running;
757   if (xd->vu_is_running && xd->admin_up)
758     {
759       vnet_hw_interface_set_flags (dm->vnet_main,
760                                    xd->vlib_hw_if_index,
761                                    VNET_HW_INTERFACE_FLAG_LINK_UP |
762                                    ETH_LINK_FULL_DUPLEX);
763     }
764
765   return 0;
766 }
767
768 static int
769 dpdk_vhost_user_set_vring_enable (u32 hw_if_index, u8 idx, int enable)
770 {
771   dpdk_device_t *xd;
772   struct vhost_virtqueue *vq;
773   dpdk_vu_intf_t *vui;
774
775   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
776     {
777       clib_warning ("not a vhost-user interface");
778       return 0;
779     }
780
781   vui = xd->vu_intf;
782   /*
783    * Guest vhost driver wrongly enables queue before
784    * setting the vring address. Therefore, save a
785    * local copy. Reflect it in vq structure if addresses
786    * are set. If not, vq will be enabled when vring
787    * is kicked.
788    */
789   vui->vrings[idx].enabled = enable;    /* Save local copy */
790
791   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
792   while (numqs--)
793     {
794       if (!vui->vrings[numqs].enabled)
795         break;
796     }
797
798   if (numqs == -1)              /* All Qs are enabled */
799     xd->need_txlock = 0;
800   else
801     xd->need_txlock = 1;
802
803   vq = xd->vu_vhost_dev.virtqueue[idx];
804   if (vq->desc && vq->avail && vq->used)
805     xd->vu_vhost_dev.virtqueue[idx]->enabled = enable;
806
807   return 0;
808 }
809
810 static clib_error_t *
811 dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
812 {
813   __attribute__ ((unused)) int n;
814   u8 buff[8];
815   n = read (uf->file_descriptor, ((char *) &buff), 8);
816   return 0;
817 }
818
819 static clib_error_t *
820 dpdk_vhost_user_set_vring_call (u32 hw_if_index, u8 idx, int fd)
821 {
822   dpdk_device_t *xd;
823   struct vhost_virtqueue *vq;
824   unix_file_t template = { 0 };
825
826   DBG_SOCK ("SET_VRING_CALL - idx %d, fd %d", idx, fd);
827
828   if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_index)))
829     {
830       clib_warning ("not a vhost-user interface");
831       return 0;
832     }
833
834   dpdk_vu_intf_t *vui = xd->vu_intf;
835
836   /* if there is old fd, delete it */
837   if (vui->vrings[idx].callfd > -1)
838     {
839       unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
840                                            vui->vrings[idx].callfd_idx);
841       unix_file_del (&unix_main, uf);
842     }
843   vui->vrings[idx].callfd = fd;
844   template.read_function = dpdk_vhost_user_callfd_read_ready;
845   template.file_descriptor = fd;
846   vui->vrings[idx].callfd_idx = unix_file_add (&unix_main, &template);
847
848   vq = xd->vu_vhost_dev.virtqueue[idx];
849   vq->callfd = -1;              /* We use locally saved vring->callfd; */
850
851   return 0;
852 }
853
854 u8
855 dpdk_vhost_user_want_interrupt (dpdk_device_t * xd, int idx)
856 {
857   dpdk_vu_intf_t *vui = xd->vu_intf;
858   ASSERT (vui != NULL);
859
860   if (PREDICT_FALSE (vui->num_vrings <= 0))
861     return 0;
862
863   dpdk_vu_vring *vring = &(vui->vrings[idx]);
864   struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
865
866   /* return if vm is interested in interrupts */
867   return (vring->callfd > -1)
868     && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
869 }
870
871 void
872 dpdk_vhost_user_send_interrupt (vlib_main_t * vm, dpdk_device_t * xd, int idx)
873 {
874   dpdk_main_t *dm = &dpdk_main;
875   dpdk_vu_intf_t *vui = xd->vu_intf;
876   ASSERT (vui != NULL);
877
878   if (PREDICT_FALSE (vui->num_vrings <= 0))
879     return;
880
881   dpdk_vu_vring *vring = &(vui->vrings[idx]);
882   struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
883
884   /* if vm is interested in interrupts */
885   if ((vring->callfd > -1)
886       && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
887     {
888       eventfd_write (vring->callfd, (eventfd_t) 1);
889       vring->n_since_last_int = 0;
890       vring->int_deadline =
891         vlib_time_now (vm) + dm->conf->vhost_coalesce_time;
892     }
893 }
894
895 /*
896  * vhost-user interface management functions
897  */
898
899 // initialize vui with specified attributes
900 static void
901 dpdk_vhost_user_vui_init (vnet_main_t * vnm,
902                           dpdk_device_t * xd, int sockfd,
903                           const char *sock_filename,
904                           u8 is_server, u64 feature_mask, u32 * sw_if_index)
905 {
906   int q;
907   dpdk_vu_intf_t *vui = xd->vu_intf;
908   memset (vui, 0, sizeof (*vui));
909
910   vui->unix_fd = sockfd;
911   vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
912   DBG_SOCK ("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
913   vui->sock_is_server = is_server;
914   strncpy (vui->sock_filename, sock_filename,
915            ARRAY_LEN (vui->sock_filename) - 1);
916   vui->sock_errno = 0;
917   vui->is_up = 0;
918   vui->feature_mask = feature_mask;
919   vui->active = 1;
920   vui->unix_file_index = ~0;
921
922   for (q = 0; q < vui->num_vrings; q++)
923     {
924       vui->vrings[q].enabled = 0;
925       vui->vrings[q].callfd = -1;
926       vui->vrings[q].kickfd = -1;
927     }
928
929   vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
930
931   if (sw_if_index)
932     *sw_if_index = xd->vlib_sw_if_index;
933 }
934
935 // register vui and start polling on it
936 static void
937 dpdk_vhost_user_vui_register (vlib_main_t * vm, dpdk_device_t * xd)
938 {
939   dpdk_main_t *dm = &dpdk_main;
940   dpdk_vu_intf_t *vui = xd->vu_intf;
941
942   hash_set (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd,
943             xd->vlib_sw_if_index);
944 }
945
946 static void
947 dpdk_unmap_all_mem_regions (dpdk_device_t * xd)
948 {
949   int i, r;
950   dpdk_vu_intf_t *vui = xd->vu_intf;
951   struct virtio_memory *mem = xd->vu_vhost_dev.mem;
952
953   for (i = 0; i < mem->nregions; i++)
954     {
955       if (vui->region_addr[i] != -1)
956         {
957
958           long page_sz = get_huge_page_size (vui->region_fd[i]);
959
960           ssize_t map_sz = RTE_ALIGN_CEIL (mem->regions[i].memory_size +
961                                            vui->region_offset[i], page_sz);
962
963           r =
964             munmap ((void *) (vui->region_addr[i] - vui->region_offset[i]),
965                     map_sz);
966
967           DBG_SOCK
968             ("unmap memory region %d addr 0x%lx off 0x%lx len 0x%lx page_sz 0x%x",
969              i, vui->region_addr[i], vui->region_offset[i], map_sz, page_sz);
970
971           vui->region_addr[i] = -1;
972
973           if (r == -1)
974             {
975               clib_unix_warning ("failed to unmap memory region");
976             }
977           close (vui->region_fd[i]);
978         }
979     }
980   mem->nregions = 0;
981 }
982
983 static inline void
984 dpdk_vhost_user_if_disconnect (dpdk_device_t * xd)
985 {
986   dpdk_vu_intf_t *vui = xd->vu_intf;
987   vnet_main_t *vnm = vnet_get_main ();
988   dpdk_main_t *dm = &dpdk_main;
989   struct vhost_virtqueue *vq;
990   int q;
991
992   xd->admin_up = 0;
993   vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
994
995   if (vui->unix_file_index != ~0)
996     {
997       unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
998       vui->unix_file_index = ~0;
999     }
1000
1001   hash_unset (dm->vu_sw_if_index_by_sock_fd, vui->unix_fd);
1002   hash_unset (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd);
1003   close (vui->unix_fd);
1004   vui->unix_fd = -1;
1005   vui->is_up = 0;
1006
1007   for (q = 0; q < vui->num_vrings; q++)
1008     {
1009       vq = xd->vu_vhost_dev.virtqueue[q];
1010       if (vui->vrings[q].callfd > -1)
1011         {
1012           unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1013                                                vui->vrings[q].callfd_idx);
1014           unix_file_del (&unix_main, uf);
1015         }
1016
1017       if (vui->vrings[q].kickfd > -1)
1018         {
1019           close (vui->vrings[q].kickfd);
1020           vui->vrings[q].kickfd = -1;
1021         }
1022
1023       vui->vrings[q].enabled = 0;       /* Reset local copy */
1024       vui->vrings[q].callfd = -1;       /* Reset FD */
1025       vq->enabled = 0;
1026       vq->log_guest_addr = 0;
1027       vq->desc = NULL;
1028       vq->used = NULL;
1029       vq->avail = NULL;
1030     }
1031   xd->vu_is_running = 0;
1032
1033   dpdk_unmap_all_mem_regions (xd);
1034   DBG_SOCK ("interface ifindex %d disconnected", xd->vlib_sw_if_index);
1035 }
1036
1037 static clib_error_t *
1038 dpdk_vhost_user_socket_read (unix_file_t * uf)
1039 {
1040   int n;
1041   int fd, number_of_fds = 0;
1042   int fds[VHOST_MEMORY_MAX_NREGIONS];
1043   vhost_user_msg_t msg;
1044   struct msghdr mh;
1045   struct iovec iov[1];
1046   dpdk_main_t *dm = &dpdk_main;
1047   dpdk_device_t *xd;
1048   dpdk_vu_intf_t *vui;
1049   struct cmsghdr *cmsg;
1050   uword *p;
1051   u8 q;
1052   vnet_main_t *vnm = vnet_get_main ();
1053
1054   p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
1055   if (p == 0)
1056     {
1057       DBG_SOCK ("FD %d doesn't belong to any interface", uf->file_descriptor);
1058       return 0;
1059     }
1060   else
1061     xd = dpdk_vhost_user_device_from_sw_if_index (p[0]);
1062
1063   ASSERT (xd != NULL);
1064   vui = xd->vu_intf;
1065
1066   char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
1067
1068   memset (&mh, 0, sizeof (mh));
1069   memset (control, 0, sizeof (control));
1070
1071   /* set the payload */
1072   iov[0].iov_base = (void *) &msg;
1073   iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
1074
1075   mh.msg_iov = iov;
1076   mh.msg_iovlen = 1;
1077   mh.msg_control = control;
1078   mh.msg_controllen = sizeof (control);
1079
1080   n = recvmsg (uf->file_descriptor, &mh, 0);
1081
1082   if (n != VHOST_USER_MSG_HDR_SZ)
1083     goto close_socket;
1084
1085   if (mh.msg_flags & MSG_CTRUNC)
1086     {
1087       goto close_socket;
1088     }
1089
1090   cmsg = CMSG_FIRSTHDR (&mh);
1091
1092   if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
1093       (cmsg->cmsg_type == SCM_RIGHTS) &&
1094       (cmsg->cmsg_len - CMSG_LEN (0) <=
1095        VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
1096     {
1097       number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
1098       clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
1099     }
1100
1101   /* version 1, no reply bit set */
1102   if ((msg.flags & 7) != 1)
1103     {
1104       DBG_SOCK ("malformed message received. closing socket");
1105       goto close_socket;
1106     }
1107
1108   {
1109     int rv __attribute__ ((unused));
1110     /* $$$$ pay attention to rv */
1111     rv = read (uf->file_descriptor, ((char *) &msg) + n, msg.size);
1112   }
1113
1114   DBG_SOCK ("VPP VHOST message %s", vhost_message_str[msg.request]);
1115   switch (msg.request)
1116     {
1117     case VHOST_USER_GET_FEATURES:
1118       DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES", xd->vlib_hw_if_index);
1119
1120       msg.flags |= VHOST_USER_REPLY_MASK;
1121
1122       dpdk_vhost_user_get_features (xd->vlib_hw_if_index, &msg.u64);
1123       msg.u64 &= vui->feature_mask;
1124       msg.size = sizeof (msg.u64);
1125       break;
1126
1127     case VHOST_USER_SET_FEATURES:
1128       DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016lx",
1129                 xd->vlib_hw_if_index, msg.u64);
1130
1131       dpdk_vhost_user_set_features (xd->vlib_hw_if_index, msg.u64);
1132       break;
1133
1134     case VHOST_USER_SET_MEM_TABLE:
1135       DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
1136                 xd->vlib_hw_if_index, msg.memory.nregions);
1137
1138       if ((msg.memory.nregions < 1) ||
1139           (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
1140         {
1141
1142           DBG_SOCK ("number of mem regions must be between 1 and %i",
1143                     VHOST_MEMORY_MAX_NREGIONS);
1144
1145           goto close_socket;
1146         }
1147
1148       if (msg.memory.nregions != number_of_fds)
1149         {
1150           DBG_SOCK ("each memory region must have FD");
1151           goto close_socket;
1152         }
1153
1154       /* Unmap previously configured memory if necessary */
1155       dpdk_unmap_all_mem_regions (xd);
1156
1157       dpdk_vhost_user_set_mem_table (xd->vlib_hw_if_index, &msg.memory, fds);
1158       break;
1159
1160     case VHOST_USER_SET_VRING_NUM:
1161       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
1162                 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1163
1164       if ((msg.state.num > 32768) ||    /* maximum ring size is 32768 */
1165           (msg.state.num == 0) ||       /* it cannot be zero */
1166           (msg.state.num % 2))  /* must be power of 2 */
1167         goto close_socket;
1168
1169       dpdk_vhost_user_set_vring_num (xd->vlib_hw_if_index, msg.state.index,
1170                                      msg.state.num);
1171       break;
1172
1173     case VHOST_USER_SET_VRING_ADDR:
1174       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
1175                 xd->vlib_hw_if_index, msg.state.index);
1176
1177       dpdk_vhost_user_set_vring_addr (xd->vlib_hw_if_index, msg.state.index,
1178                                       msg.addr.desc_user_addr,
1179                                       msg.addr.used_user_addr,
1180                                       msg.addr.avail_user_addr,
1181                                       msg.addr.log_guest_addr);
1182       break;
1183
1184     case VHOST_USER_SET_OWNER:
1185       DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", xd->vlib_hw_if_index);
1186       break;
1187
1188     case VHOST_USER_RESET_OWNER:
1189       DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", xd->vlib_hw_if_index);
1190       break;
1191
1192     case VHOST_USER_SET_VRING_CALL:
1193       q = (u8) (msg.u64 & 0xFF);
1194
1195       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %lx, idx: %d",
1196                 xd->vlib_hw_if_index, msg.u64, q);
1197
1198       if (!(msg.u64 & 0x100))
1199         {
1200           if (number_of_fds != 1)
1201             goto close_socket;
1202           fd = fds[0];
1203         }
1204       else
1205         {
1206           fd = -1;
1207         }
1208       dpdk_vhost_user_set_vring_call (xd->vlib_hw_if_index, q, fd);
1209
1210       break;
1211
1212     case VHOST_USER_SET_VRING_KICK:
1213
1214       q = (u8) (msg.u64 & 0xFF);
1215
1216       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %lx, idx: %d",
1217                 xd->vlib_hw_if_index, msg.u64, q);
1218
1219       if (!(msg.u64 & 0x100))
1220         {
1221           if (number_of_fds != 1)
1222             goto close_socket;
1223
1224           if (vui->vrings[q].kickfd > -1)
1225             close (vui->vrings[q].kickfd);
1226
1227           vui->vrings[q].kickfd = fds[0];
1228         }
1229       else
1230         vui->vrings[q].kickfd = -1;
1231
1232       dpdk_vhost_user_set_vring_kick (xd->vlib_hw_if_index, q,
1233                                       vui->vrings[q].kickfd);
1234       break;
1235
1236     case VHOST_USER_SET_VRING_ERR:
1237
1238       q = (u8) (msg.u64 & 0xFF);
1239
1240       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %lx, idx: %d",
1241                 xd->vlib_hw_if_index, msg.u64, q);
1242
1243       if (!(msg.u64 & 0x100))
1244         {
1245           if (number_of_fds != 1)
1246             goto close_socket;
1247
1248           fd = fds[0];
1249         }
1250       else
1251         fd = -1;
1252
1253       vui->vrings[q].errfd = fd;
1254       break;
1255
1256     case VHOST_USER_SET_VRING_BASE:
1257       DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1258                 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1259
1260       dpdk_vhost_user_set_vring_base (xd->vlib_hw_if_index, msg.state.index,
1261                                       msg.state.num);
1262       break;
1263
1264     case VHOST_USER_GET_VRING_BASE:
1265       DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1266                 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1267
1268       msg.flags |= VHOST_USER_REPLY_MASK;
1269       msg.size = sizeof (msg.state);
1270
1271       dpdk_vhost_user_get_vring_base (xd->vlib_hw_if_index, msg.state.index,
1272                                       &msg.state.num);
1273       break;
1274
1275     case VHOST_USER_NONE:
1276       DBG_SOCK ("if %d msg VHOST_USER_NONE", xd->vlib_hw_if_index);
1277       break;
1278
1279     case VHOST_USER_SET_LOG_BASE:
1280       DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", xd->vlib_hw_if_index);
1281
1282       if (msg.size != sizeof (msg.log))
1283         {
1284           DBG_SOCK
1285             ("invalid msg size for VHOST_USER_SET_LOG_BASE: %u instead of %lu",
1286              msg.size, sizeof (msg.log));
1287           goto close_socket;
1288         }
1289
1290       if (!
1291           (xd->vu_vhost_dev.protocol_features & (1 <<
1292                                                  VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1293         {
1294           DBG_SOCK
1295             ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1296           goto close_socket;
1297         }
1298
1299       fd = fds[0];
1300       /* align size to 2M page */
1301       long page_sz = get_huge_page_size (fd);
1302       ssize_t map_sz =
1303         RTE_ALIGN_CEIL (msg.log.size + msg.log.offset, page_sz);
1304
1305       void *addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1306                          MAP_SHARED, fd, 0);
1307
1308       DBG_SOCK ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped %p",
1309                 map_sz, msg.log.offset, fd, addr);
1310
1311       if (addr == MAP_FAILED)
1312         {
1313           clib_warning ("failed to map memory. errno is %d", errno);
1314           goto close_socket;
1315         }
1316
1317       xd->vu_vhost_dev.log_base += pointer_to_uword (addr) + msg.log.offset;
1318       xd->vu_vhost_dev.log_size = msg.log.size;
1319       msg.flags |= VHOST_USER_REPLY_MASK;
1320       msg.size = sizeof (msg.u64);
1321       break;
1322
1323     case VHOST_USER_SET_LOG_FD:
1324       DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", xd->vlib_hw_if_index);
1325       break;
1326
1327     case VHOST_USER_GET_PROTOCOL_FEATURES:
1328       DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1329                 xd->vlib_hw_if_index);
1330
1331       msg.flags |= VHOST_USER_REPLY_MASK;
1332       msg.u64 = VHOST_USER_PROTOCOL_FEATURES;
1333       DBG_SOCK ("VHOST_USER_PROTOCOL_FEATURES: %llx",
1334                 VHOST_USER_PROTOCOL_FEATURES);
1335       msg.size = sizeof (msg.u64);
1336       break;
1337
1338     case VHOST_USER_SET_PROTOCOL_FEATURES:
1339       DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES",
1340                 xd->vlib_hw_if_index);
1341
1342       DBG_SOCK ("VHOST_USER_SET_PROTOCOL_FEATURES: 0x%lx", msg.u64);
1343       dpdk_vhost_user_set_protocol_features (xd->vlib_hw_if_index, msg.u64);
1344       break;
1345
1346     case VHOST_USER_SET_VRING_ENABLE:
1347       DBG_SOCK ("%d VPP VHOST_USER_SET_VRING_ENABLE IDX: %d, Enable: %d",
1348                 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1349       dpdk_vhost_user_set_vring_enable
1350         (xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1351       break;
1352
1353     case VHOST_USER_GET_QUEUE_NUM:
1354       DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM:", xd->vlib_hw_if_index);
1355
1356       msg.flags |= VHOST_USER_REPLY_MASK;
1357       msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
1358       msg.size = sizeof (msg.u64);
1359       break;
1360
1361     default:
1362       DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1363                 msg.request);
1364       goto close_socket;
1365     }
1366
1367   /* if we have pointers to descriptor table, go up */
1368   if (!vui->is_up &&
1369       xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
1370       xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc)
1371     {
1372
1373       DBG_SOCK ("interface %d connected", xd->vlib_sw_if_index);
1374
1375       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1376                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1377       vui->is_up = 1;
1378       xd->admin_up = 1;
1379     }
1380
1381   /* if we need to reply */
1382   if (msg.flags & VHOST_USER_REPLY_MASK)
1383     {
1384       n =
1385         send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1386       if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1387         goto close_socket;
1388     }
1389
1390   return 0;
1391
1392 close_socket:
1393   DBG_SOCK ("error: close_socket");
1394   dpdk_vhost_user_if_disconnect (xd);
1395   return 0;
1396 }
1397
1398 static clib_error_t *
1399 dpdk_vhost_user_socket_error (unix_file_t * uf)
1400 {
1401   dpdk_main_t *dm = &dpdk_main;
1402   dpdk_device_t *xd;
1403   uword *p;
1404
1405   p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
1406   if (p == 0)
1407     {
1408       DBG_SOCK ("FD %d doesn't belong to any interface", uf->file_descriptor);
1409       return 0;
1410     }
1411   else
1412     xd = dpdk_vhost_user_device_from_sw_if_index (p[0]);
1413
1414   dpdk_vhost_user_if_disconnect (xd);
1415   return 0;
1416 }
1417
1418 static clib_error_t *
1419 dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
1420 {
1421   int client_fd, client_len;
1422   struct sockaddr_un client;
1423   unix_file_t template = { 0 };
1424   dpdk_main_t *dm = &dpdk_main;
1425   dpdk_device_t *xd = NULL;
1426   dpdk_vu_intf_t *vui;
1427   uword *p;
1428
1429   p = hash_get (dm->vu_sw_if_index_by_listener_fd, uf->file_descriptor);
1430   if (p == 0)
1431     {
1432       DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
1433       return 0;
1434     }
1435
1436   xd = dpdk_vhost_user_device_from_sw_if_index (p[0]);
1437   ASSERT (xd != NULL);
1438   vui = xd->vu_intf;
1439
1440   client_len = sizeof (client);
1441   client_fd = accept (uf->file_descriptor,
1442                       (struct sockaddr *) &client,
1443                       (socklen_t *) & client_len);
1444
1445   if (client_fd < 0)
1446     return clib_error_return_unix (0, "accept");
1447
1448   template.read_function = dpdk_vhost_user_socket_read;
1449   template.error_function = dpdk_vhost_user_socket_error;
1450   template.file_descriptor = client_fd;
1451   vui->unix_file_index = unix_file_add (&unix_main, &template);
1452
1453   vui->client_fd = client_fd;
1454   hash_set (dm->vu_sw_if_index_by_sock_fd, vui->client_fd,
1455             xd->vlib_sw_if_index);
1456
1457   return 0;
1458 }
1459
1460 // init server socket on specified sock_filename
1461 static int
1462 dpdk_vhost_user_init_server_sock (const char *sock_filename, int *sockfd)
1463 {
1464   int rv = 0;
1465   struct sockaddr_un un = { };
1466   int fd;
1467   /* create listening socket */
1468   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1469
1470   if (fd < 0)
1471     {
1472       return VNET_API_ERROR_SYSCALL_ERROR_1;
1473     }
1474
1475   un.sun_family = AF_UNIX;
1476   strcpy ((char *) un.sun_path, (char *) sock_filename);
1477
1478   /* remove if exists */
1479   unlink ((char *) sock_filename);
1480
1481   if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1482     {
1483       rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1484       goto error;
1485     }
1486
1487   if (listen (fd, 1) == -1)
1488     {
1489       rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1490       goto error;
1491     }
1492
1493   unix_file_t template = { 0 };
1494   template.read_function = dpdk_vhost_user_socksvr_accept_ready;
1495   template.file_descriptor = fd;
1496   unix_file_add (&unix_main, &template);
1497   *sockfd = fd;
1498   return rv;
1499
1500 error:
1501   close (fd);
1502   return rv;
1503 }
1504
1505 /*
1506  * vhost-user interface control functions used from vpe api
1507  */
1508
1509 int
1510 dpdk_vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
1511                            const char *sock_filename,
1512                            u8 is_server,
1513                            u32 * sw_if_index,
1514                            u64 feature_mask,
1515                            u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1516 {
1517   dpdk_main_t *dm = &dpdk_main;
1518   dpdk_device_t *xd;
1519   u32 hw_if_idx = ~0;
1520   int sockfd = -1;
1521   int rv = 0;
1522
1523   // using virtio vhost user?
1524   if (dm->conf->use_virtio_vhost)
1525     {
1526       return vhost_user_create_if (vnm, vm, sock_filename, is_server,
1527                                    sw_if_index, feature_mask, renumber,
1528                                    custom_dev_instance, hwaddr);
1529     }
1530
1531   if (is_server)
1532     {
1533       if ((rv =
1534            dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
1535         {
1536           return rv;
1537         }
1538     }
1539
1540   if (renumber)
1541     {
1542       // set next vhost-user if id if custom one is higher or equal
1543       if (custom_dev_instance >= dm->next_vu_if_id)
1544         dm->next_vu_if_id = custom_dev_instance + 1;
1545
1546       dpdk_create_vhost_user_if_internal (&hw_if_idx, custom_dev_instance,
1547                                           hwaddr);
1548     }
1549   else
1550     dpdk_create_vhost_user_if_internal (&hw_if_idx, (u32) ~ 0, hwaddr);
1551   DBG_SOCK ("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1552
1553   xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_idx);
1554   ASSERT (xd != NULL);
1555
1556   dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1557                             feature_mask, sw_if_index);
1558
1559   dpdk_vhost_user_vui_register (vm, xd);
1560   return rv;
1561 }
1562
1563 int
1564 dpdk_vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
1565                            const char *sock_filename,
1566                            u8 is_server,
1567                            u32 sw_if_index,
1568                            u64 feature_mask,
1569                            u8 renumber, u32 custom_dev_instance)
1570 {
1571   dpdk_main_t *dm = &dpdk_main;
1572   dpdk_device_t *xd;
1573   dpdk_vu_intf_t *vui = NULL;
1574   u32 sw_if_idx = ~0;
1575   int sockfd = -1;
1576   int rv = 0;
1577
1578   // using virtio vhost user?
1579   if (dm->conf->use_virtio_vhost)
1580     {
1581       return vhost_user_modify_if (vnm, vm, sock_filename, is_server,
1582                                    sw_if_index, feature_mask, renumber,
1583                                    custom_dev_instance);
1584     }
1585
1586   xd = dpdk_vhost_user_device_from_sw_if_index (sw_if_index);
1587
1588   if (xd == NULL)
1589     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1590
1591   vui = xd->vu_intf;
1592
1593   // interface is inactive
1594   vui->active = 0;
1595   // disconnect interface sockets
1596   dpdk_vhost_user_if_disconnect (xd);
1597
1598   if (is_server)
1599     {
1600       if ((rv =
1601            dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
1602         {
1603           return rv;
1604         }
1605     }
1606
1607   dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1608                             feature_mask, &sw_if_idx);
1609
1610   if (renumber)
1611     {
1612       vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1613     }
1614
1615   dpdk_vhost_user_vui_register (vm, xd);
1616
1617   return rv;
1618 }
1619
1620 int
1621 dpdk_vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
1622                            u32 sw_if_index)
1623 {
1624   dpdk_main_t *dm = &dpdk_main;
1625   dpdk_device_t *xd = NULL;
1626   dpdk_vu_intf_t *vui;
1627   int rv = 0;
1628
1629   // using virtio vhost user?
1630   if (dm->conf->use_virtio_vhost)
1631     {
1632       return vhost_user_delete_if (vnm, vm, sw_if_index);
1633     }
1634
1635   xd = dpdk_vhost_user_device_from_sw_if_index (sw_if_index);
1636
1637   if (xd == NULL)
1638     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1639
1640   vui = xd->vu_intf;
1641
1642   // interface is inactive
1643   vui->active = 0;
1644   // disconnect interface sockets
1645   dpdk_vhost_user_if_disconnect (xd);
1646   // add to inactive interface list
1647   vec_add1 (dm->vu_inactive_interfaces_device_index, xd->device_index);
1648
1649   ethernet_delete_interface (vnm, xd->vlib_hw_if_index);
1650   DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d",
1651             sw_if_index);
1652
1653   return rv;
1654 }
1655
1656 int
1657 dpdk_vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
1658                           vhost_user_intf_details_t ** out_vuids)
1659 {
1660   int rv = 0;
1661   dpdk_main_t *dm = &dpdk_main;
1662   dpdk_device_t *xd;
1663   dpdk_vu_intf_t *vui;
1664   struct virtio_net *vhost_dev;
1665   vhost_user_intf_details_t *r_vuids = NULL;
1666   vhost_user_intf_details_t *vuid = NULL;
1667   u32 *hw_if_indices = 0;
1668   vnet_hw_interface_t *hi;
1669   u8 *s = NULL;
1670   int i;
1671
1672   if (!out_vuids)
1673     return -1;
1674
1675   // using virtio vhost user?
1676   if (dm->conf->use_virtio_vhost)
1677     {
1678       return vhost_user_dump_ifs (vnm, vm, out_vuids);
1679     }
1680
1681   vec_foreach (xd, dm->devices)
1682   {
1683     if ((xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) && xd->vu_intf->active)
1684       vec_add1 (hw_if_indices, xd->vlib_hw_if_index);
1685   }
1686
1687   for (i = 0; i < vec_len (hw_if_indices); i++)
1688     {
1689       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1690       xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_indices[i]);
1691       if (!xd)
1692         {
1693           clib_warning ("invalid vhost-user interface hw_if_index %d",
1694                         hw_if_indices[i]);
1695           continue;
1696         }
1697
1698       vui = xd->vu_intf;
1699       ASSERT (vui != NULL);
1700       vhost_dev = &xd->vu_vhost_dev;
1701       u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1702                                vhost_dev->virtqueue[0]->vhost_hlen : 0);
1703
1704       vec_add2 (r_vuids, vuid, 1);
1705       vuid->sw_if_index = xd->vlib_sw_if_index;
1706       vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1707       vuid->features = vhost_dev->features;
1708       vuid->is_server = vui->sock_is_server;
1709       vuid->num_regions =
1710         (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1711       vuid->sock_errno = vui->sock_errno;
1712       strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1713                ARRAY_LEN (vuid->sock_filename) - 1);
1714
1715       s = format (s, "%v%c", hi->name, 0);
1716
1717       strncpy ((char *) vuid->if_name, (char *) s,
1718                ARRAY_LEN (vuid->if_name) - 1);
1719       _vec_len (s) = 0;
1720     }
1721
1722   vec_free (s);
1723   vec_free (hw_if_indices);
1724
1725   *out_vuids = r_vuids;
1726
1727   return rv;
1728 }
1729
1730 /*
1731  * Processing functions called from dpdk process fn
1732  */
1733
1734 typedef struct
1735 {
1736   struct sockaddr_un sun;
1737   int sockfd;
1738   unix_file_t template;
1739   uword *event_data;
1740 } dpdk_vu_process_state;
1741
1742 void
1743 dpdk_vhost_user_process_init (void **ctx)
1744 {
1745   dpdk_vu_process_state *state =
1746     clib_mem_alloc (sizeof (dpdk_vu_process_state));
1747   memset (state, 0, sizeof (*state));
1748   state->sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1749   state->sun.sun_family = AF_UNIX;
1750   state->template.read_function = dpdk_vhost_user_socket_read;
1751   state->template.error_function = dpdk_vhost_user_socket_error;
1752   state->event_data = 0;
1753   *ctx = state;
1754 }
1755
1756 void
1757 dpdk_vhost_user_process_cleanup (void *ctx)
1758 {
1759   clib_mem_free (ctx);
1760 }
1761
1762 uword
1763 dpdk_vhost_user_process_if (vlib_main_t * vm, dpdk_device_t * xd, void *ctx)
1764 {
1765   dpdk_main_t *dm = &dpdk_main;
1766   dpdk_vu_process_state *state = (dpdk_vu_process_state *) ctx;
1767   dpdk_vu_intf_t *vui = xd->vu_intf;
1768
1769   if (vui->sock_is_server || !vui->active)
1770     return 0;
1771
1772   if (vui->unix_fd == -1)
1773     {
1774       /* try to connect */
1775       strncpy (state->sun.sun_path, (char *) vui->sock_filename,
1776                sizeof (state->sun.sun_path) - 1);
1777
1778       if (connect
1779           (state->sockfd, (struct sockaddr *) &(state->sun),
1780            sizeof (struct sockaddr_un)) == 0)
1781         {
1782           vui->sock_errno = 0;
1783           vui->unix_fd = state->sockfd;
1784           state->template.file_descriptor = state->sockfd;
1785           vui->unix_file_index =
1786             unix_file_add (&unix_main, &(state->template));
1787           hash_set (dm->vu_sw_if_index_by_sock_fd, state->sockfd,
1788                     xd->vlib_sw_if_index);
1789
1790           state->sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1791           if (state->sockfd < 0)
1792             return -1;
1793         }
1794       else
1795         {
1796           vui->sock_errno = errno;
1797         }
1798     }
1799   else
1800     {
1801       /* check if socket is alive */
1802       int error = 0;
1803       socklen_t len = sizeof (error);
1804       int retval =
1805         getsockopt (vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1806
1807       if (retval)
1808         dpdk_vhost_user_if_disconnect (xd);
1809     }
1810   return 0;
1811 }
1812 #endif
1813
1814 /*
1815  * CLI functions
1816  */
1817
1818 static clib_error_t *
1819 dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
1820                                     unformat_input_t * input,
1821                                     vlib_cli_command_t * cmd)
1822 {
1823 #if DPDK_VHOST_USER
1824   dpdk_main_t *dm = &dpdk_main;
1825   unformat_input_t _line_input, *line_input = &_line_input;
1826   u8 *sock_filename = NULL;
1827   u32 sw_if_index;
1828   u8 is_server = 0;
1829   u64 feature_mask = (u64) ~ 0;
1830   u8 renumber = 0;
1831   u32 custom_dev_instance = ~0;
1832   u8 hwaddr[6];
1833   u8 *hw = NULL;
1834
1835   if (dm->conf->use_virtio_vhost)
1836     {
1837 #endif
1838       return vhost_user_connect_command_fn (vm, input, cmd);
1839 #if DPDK_VHOST_USER
1840     }
1841
1842   /* Get a line of input. */
1843   if (!unformat_user (input, unformat_line_input, line_input))
1844     return 0;
1845
1846   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1847     {
1848       if (unformat (line_input, "socket %s", &sock_filename))
1849         ;
1850       else if (unformat (line_input, "server"))
1851         is_server = 1;
1852       else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1853         ;
1854       else
1855         if (unformat
1856             (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1857         hw = hwaddr;
1858       else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1859         {
1860           renumber = 1;
1861         }
1862       else
1863         return clib_error_return (0, "unknown input `%U'",
1864                                   format_unformat_error, input);
1865     }
1866   unformat_free (line_input);
1867
1868   vnet_main_t *vnm = vnet_get_main ();
1869   if (sock_filename == NULL)
1870     return clib_error_return (0, "missing socket file");
1871
1872   dpdk_vhost_user_create_if (vnm, vm, (char *) sock_filename,
1873                              is_server, &sw_if_index, feature_mask,
1874                              renumber, custom_dev_instance, hw);
1875
1876   vec_free (sock_filename);
1877   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
1878                    sw_if_index);
1879   return 0;
1880 #endif
1881 }
1882
1883 /* *INDENT-OFF* */
1884 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1885     .path = "create vhost-user",
1886     .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1887     .function = dpdk_vhost_user_connect_command_fn,
1888 };
1889 /* *INDENT-ON* */
1890
1891 static clib_error_t *
1892 dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
1893                                    unformat_input_t * input,
1894                                    vlib_cli_command_t * cmd)
1895 {
1896   dpdk_main_t *dm = &dpdk_main;
1897   clib_error_t *error = 0;
1898   unformat_input_t _line_input, *line_input = &_line_input;
1899   u32 sw_if_index = ~0;
1900
1901   if (dm->conf->use_virtio_vhost)
1902     {
1903       return vhost_user_delete_command_fn (vm, input, cmd);
1904     }
1905
1906   /* Get a line of input. */
1907   if (!unformat_user (input, unformat_line_input, line_input))
1908     return 0;
1909
1910   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1911     {
1912       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1913         ;
1914       else
1915         return clib_error_return (0, "unknown input `%U'",
1916                                   format_unformat_error, input);
1917     }
1918   unformat_free (line_input);
1919
1920   if (sw_if_index == ~0)
1921     {
1922       error = clib_error_return (0, "invalid sw_if_index",
1923                                  format_unformat_error, input);
1924       return error;
1925     }
1926
1927   vnet_main_t *vnm = vnet_get_main ();
1928
1929 #if DPDK_VHOST_USER
1930   dpdk_vhost_user_delete_if (vnm, vm, sw_if_index);
1931 #else
1932   vhost_user_delete_if (vnm, vm, sw_if_index);
1933 #endif
1934
1935   return 0;
1936 }
1937
1938 /* *INDENT-OFF* */
1939 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1940     .path = "delete vhost-user",
1941     .short_help = "delete vhost-user sw_if_index <nn>",
1942     .function = dpdk_vhost_user_delete_command_fn,
1943 };
1944 /* *INDENT-ON* */
1945
1946 #define foreach_dpdk_vhost_feature      \
1947  _ (VIRTIO_NET_F_MRG_RXBUF)             \
1948  _ (VIRTIO_NET_F_CTRL_VQ)               \
1949  _ (VIRTIO_NET_F_CTRL_RX)
1950
1951 static clib_error_t *
1952 show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
1953                                  unformat_input_t * input,
1954                                  vlib_cli_command_t * cmd)
1955 {
1956 #if DPDK_VHOST_USER
1957   clib_error_t *error = 0;
1958   dpdk_main_t *dm = &dpdk_main;
1959   vnet_main_t *vnm = vnet_get_main ();
1960   dpdk_device_t *xd;
1961   dpdk_vu_intf_t *vui;
1962   struct virtio_net *vhost_dev;
1963   u32 hw_if_index, *hw_if_indices = 0;
1964   vnet_hw_interface_t *hi;
1965   int i, j, q;
1966   int show_descr = 0;
1967   struct virtio_memory *mem;
1968   struct feat_struct
1969   {
1970     u8 bit;
1971     char *str;
1972   };
1973   struct feat_struct *feat_entry;
1974
1975   static struct feat_struct feat_array[] = {
1976 #define _(f) { .str = #f, .bit = f, },
1977     foreach_dpdk_vhost_feature
1978 #undef _
1979     {.str = NULL}
1980   };
1981
1982   if (dm->conf->use_virtio_vhost)
1983     {
1984 #endif
1985       return show_vhost_user_command_fn (vm, input, cmd);
1986 #if DPDK_VHOST_USER
1987     }
1988
1989   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1990     {
1991       if (unformat
1992           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1993         {
1994           vec_add1 (hw_if_indices, hw_if_index);
1995           vlib_cli_output (vm, "add %d", hw_if_index);
1996         }
1997       else if (unformat (input, "descriptors") || unformat (input, "desc"))
1998         show_descr = 1;
1999       else
2000         {
2001           error = clib_error_return (0, "unknown input `%U'",
2002                                      format_unformat_error, input);
2003           goto done;
2004         }
2005     }
2006   if (vec_len (hw_if_indices) == 0)
2007     {
2008       vec_foreach (xd, dm->devices)
2009       {
2010         if ((xd->flags DPDK_DEVICE_FLAG_VHOST_USER) && xd->vu_intf->active)
2011           vec_add1 (hw_if_indices, xd->vlib_hw_if_index);
2012       }
2013     }
2014
2015   vlib_cli_output (vm, "DPDK vhost-user interfaces");
2016   vlib_cli_output (vm, "Global:\n  coalesce frames %d time %e\n\n",
2017                    dm->conf->vhost_coalesce_frames,
2018                    dm->conf->vhost_coalesce_time);
2019
2020   for (i = 0; i < vec_len (hw_if_indices); i++)
2021     {
2022       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2023
2024       if (!(xd = dpdk_vhost_user_device_from_hw_if_index (hw_if_indices[i])))
2025         {
2026           error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
2027                                      hi->name);
2028           goto done;
2029         }
2030       vui = xd->vu_intf;
2031       vhost_dev = &xd->vu_vhost_dev;
2032       mem = vhost_dev->mem;
2033       u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
2034                                vhost_dev->virtqueue[0]->vhost_hlen : 0);
2035
2036       vlib_cli_output (vm, "Interface: %v (ifindex %d)",
2037                        hi->name, hw_if_indices[i]);
2038
2039       vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
2040                        virtio_net_hdr_sz, xd->vu_vhost_dev.features);
2041
2042       feat_entry = (struct feat_struct *) &feat_array;
2043       while (feat_entry->str)
2044         {
2045           if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
2046             vlib_cli_output (vm, "   %s (%d)", feat_entry->str,
2047                              feat_entry->bit);
2048           feat_entry++;
2049         }
2050
2051       vlib_cli_output (vm, "\n");
2052
2053       vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2054                        vui->sock_filename,
2055                        vui->sock_is_server ? "server" : "client",
2056                        strerror (vui->sock_errno));
2057
2058       vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
2059
2060       if (mem->nregions)
2061         {
2062           vlib_cli_output (vm,
2063                            " region fd    guest_phys_addr    memory_size        userspace_addr     mmap_offset        mmap_addr\n");
2064           vlib_cli_output (vm,
2065                            " ====== ===== ================== ================== ================== ================== ==================\n");
2066         }
2067       for (j = 0; j < mem->nregions; j++)
2068         {
2069           vlib_cli_output (vm,
2070                            "  %d     %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2071                            j, vui->region_fd[j],
2072                            mem->regions[j].guest_phys_address,
2073                            mem->regions[j].memory_size,
2074                            mem->regions[j].userspace_address,
2075                            mem->regions[j].address_offset,
2076                            vui->region_addr[j]);
2077         }
2078       for (q = 0; q < vui->num_vrings; q++)
2079         {
2080           struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
2081           const char *qtype = (q & 1) ? "TX" : "RX";
2082
2083           vlib_cli_output (vm, "\n Virtqueue %d (%s)\n", q / 2, qtype);
2084
2085           vlib_cli_output (vm,
2086                            "  qsz %d last_used_idx %d last_used_idx_res %d\n",
2087                            vq->size, vq->last_used_idx,
2088                            vq->last_used_idx_res);
2089
2090           if (vq->avail && vq->used)
2091             vlib_cli_output (vm,
2092                              "  avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
2093                              vq->avail->flags, vq->avail->idx,
2094                              vq->used->flags, vq->used->idx);
2095
2096           vlib_cli_output (vm, "  kickfd %d callfd %d errfd %d enabled %d\n",
2097                            vq->kickfd, vq->callfd, vui->vrings[q].errfd,
2098                            vq->enabled);
2099
2100           if (show_descr && vq->enabled)
2101             {
2102               vlib_cli_output (vm, "\n  descriptor table:\n");
2103               vlib_cli_output (vm,
2104                                "   id          addr         len  flags  next      user_addr\n");
2105               vlib_cli_output (vm,
2106                                "  ===== ================== ===== ====== ===== ==================\n");
2107               for (j = 0; j < vq->size; j++)
2108                 {
2109                   vlib_cli_output (vm,
2110                                    "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
2111                                    j, vq->desc[j].addr, vq->desc[j].len,
2112                                    vq->desc[j].flags, vq->desc[j].next,
2113                                    pointer_to_uword (map_guest_mem
2114                                                      (xd, vq->desc[j].addr)));
2115                 }
2116             }
2117         }
2118       vlib_cli_output (vm, "\n");
2119     }
2120 done:
2121   vec_free (hw_if_indices);
2122   return error;
2123 #endif
2124 }
2125
2126 /* *INDENT-OFF* */
2127 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2128     .path = "show vhost-user",
2129     .short_help = "show vhost-user interface",
2130     .function = show_dpdk_vhost_user_command_fn,
2131 };
2132 /* *INDENT-ON* */
2133
2134 /*
2135  * fd.io coding-style-patch-verification: ON
2136  *
2137  * Local Variables:
2138  * eval: (c-set-style "gnu")
2139  * End:
2140  */