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