vhost-user: prepare multiple queue setup
[dpdk.git] / lib / librte_vhost / virtio-net.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <linux/vhost.h>
35 #include <linux/virtio_net.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <assert.h>
40 #include <sys/mman.h>
41 #include <unistd.h>
42 #ifdef RTE_LIBRTE_VHOST_NUMA
43 #include <numaif.h>
44 #endif
45
46 #include <sys/socket.h>
47
48 #include <rte_ethdev.h>
49 #include <rte_log.h>
50 #include <rte_string_fns.h>
51 #include <rte_memory.h>
52 #include <rte_malloc.h>
53 #include <rte_virtio_net.h>
54
55 #include "vhost-net.h"
56 #include "virtio-net.h"
57
58 /*
59  * Device linked list structure for configuration.
60  */
61 struct virtio_net_config_ll {
62         struct virtio_net dev;                  /* Virtio device.*/
63         struct virtio_net_config_ll *next;      /* Next dev on linked list.*/
64 };
65
66 /* device ops to add/remove device to/from data core. */
67 struct virtio_net_device_ops const *notify_ops;
68 /* root address of the linked list of managed virtio devices */
69 static struct virtio_net_config_ll *ll_root;
70
71 #define VHOST_USER_F_PROTOCOL_FEATURES  30
72
73 /* Features supported by this lib. */
74 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
75                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
76                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
77                                 (1ULL << VHOST_F_LOG_ALL)      | \
78                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
79 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
80
81
82 /*
83  * Converts QEMU virtual address to Vhost virtual address. This function is
84  * used to convert the ring addresses to our address space.
85  */
86 static uint64_t
87 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
88 {
89         struct virtio_memory_regions *region;
90         uint64_t vhost_va = 0;
91         uint32_t regionidx = 0;
92
93         /* Find the region where the address lives. */
94         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
95                 region = &dev->mem->regions[regionidx];
96                 if ((qemu_va >= region->userspace_address) &&
97                         (qemu_va <= region->userspace_address +
98                         region->memory_size)) {
99                         vhost_va = qemu_va + region->guest_phys_address +
100                                 region->address_offset -
101                                 region->userspace_address;
102                         break;
103                 }
104         }
105         return vhost_va;
106 }
107
108
109 /*
110  * Retrieves an entry from the devices configuration linked list.
111  */
112 static struct virtio_net_config_ll *
113 get_config_ll_entry(struct vhost_device_ctx ctx)
114 {
115         struct virtio_net_config_ll *ll_dev = ll_root;
116
117         /* Loop through linked list until the device_fh is found. */
118         while (ll_dev != NULL) {
119                 if (ll_dev->dev.device_fh == ctx.fh)
120                         return ll_dev;
121                 ll_dev = ll_dev->next;
122         }
123
124         return NULL;
125 }
126
127 /*
128  * Searches the configuration core linked list and
129  * retrieves the device if it exists.
130  */
131 struct virtio_net *
132 get_device(struct vhost_device_ctx ctx)
133 {
134         struct virtio_net_config_ll *ll_dev;
135
136         ll_dev = get_config_ll_entry(ctx);
137
138         if (ll_dev)
139                 return &ll_dev->dev;
140
141         RTE_LOG(ERR, VHOST_CONFIG,
142                 "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
143         return NULL;
144 }
145
146 /*
147  * Add entry containing a device to the device configuration linked list.
148  */
149 static void
150 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
151 {
152         struct virtio_net_config_ll *ll_dev = ll_root;
153
154         /* If ll_dev == NULL then this is the first device so go to else */
155         if (ll_dev) {
156                 /* If the 1st device_fh != 0 then we insert our device here. */
157                 if (ll_dev->dev.device_fh != 0) {
158                         new_ll_dev->dev.device_fh = 0;
159                         new_ll_dev->next = ll_dev;
160                         ll_root = new_ll_dev;
161                 } else {
162                         /*
163                          * Increment through the ll until we find un unused
164                          * device_fh. Insert the device at that entry.
165                          */
166                         while ((ll_dev->next != NULL) &&
167                                 (ll_dev->dev.device_fh ==
168                                         (ll_dev->next->dev.device_fh - 1)))
169                                 ll_dev = ll_dev->next;
170
171                         new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
172                         new_ll_dev->next = ll_dev->next;
173                         ll_dev->next = new_ll_dev;
174                 }
175         } else {
176                 ll_root = new_ll_dev;
177                 ll_root->dev.device_fh = 0;
178         }
179
180 }
181
182 static void
183 cleanup_vq(struct vhost_virtqueue *vq)
184 {
185         if (vq->callfd >= 0)
186                 close(vq->callfd);
187         if (vq->kickfd >= 0)
188                 close(vq->kickfd);
189 }
190
191 /*
192  * Unmap any memory, close any file descriptors and
193  * free any memory owned by a device.
194  */
195 static void
196 cleanup_device(struct virtio_net *dev)
197 {
198         uint32_t i;
199
200         /* Unmap QEMU memory file if mapped. */
201         if (dev->mem) {
202                 munmap((void *)(uintptr_t)dev->mem->mapped_address,
203                         (size_t)dev->mem->mapped_size);
204                 free(dev->mem);
205         }
206
207         for (i = 0; i < dev->virt_qp_nb; i++) {
208                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ]);
209                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ]);
210         }
211 }
212
213 /*
214  * Release virtqueues and device memory.
215  */
216 static void
217 free_device(struct virtio_net_config_ll *ll_dev)
218 {
219         uint32_t i;
220
221         for (i = 0; i < ll_dev->dev.virt_qp_nb; i++)
222                 rte_free(ll_dev->dev.virtqueue[i * VIRTIO_QNUM]);
223
224         rte_free(ll_dev);
225 }
226
227 /*
228  * Remove an entry from the device configuration linked list.
229  */
230 static struct virtio_net_config_ll *
231 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
232         struct virtio_net_config_ll *ll_dev_last)
233 {
234         /* First remove the device and then clean it up. */
235         if (ll_dev == ll_root) {
236                 ll_root = ll_dev->next;
237                 cleanup_device(&ll_dev->dev);
238                 free_device(ll_dev);
239                 return ll_root;
240         } else {
241                 if (likely(ll_dev_last != NULL)) {
242                         ll_dev_last->next = ll_dev->next;
243                         cleanup_device(&ll_dev->dev);
244                         free_device(ll_dev);
245                         return ll_dev_last->next;
246                 } else {
247                         cleanup_device(&ll_dev->dev);
248                         free_device(ll_dev);
249                         RTE_LOG(ERR, VHOST_CONFIG,
250                                 "Remove entry from config_ll failed\n");
251                         return NULL;
252                 }
253         }
254 }
255
256 static void
257 init_vring_queue(struct vhost_virtqueue *vq)
258 {
259         memset(vq, 0, sizeof(struct vhost_virtqueue));
260
261         vq->kickfd = -1;
262         vq->callfd = -1;
263
264         /* Backends are set to -1 indicating an inactive device. */
265         vq->backend = -1;
266 }
267
268 static void
269 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
270 {
271         init_vring_queue(dev->virtqueue[qp_idx * VIRTIO_QNUM + VIRTIO_RXQ]);
272         init_vring_queue(dev->virtqueue[qp_idx * VIRTIO_QNUM + VIRTIO_TXQ]);
273 }
274
275 static int
276 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
277 {
278         struct vhost_virtqueue *virtqueue = NULL;
279         uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
280         uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
281
282         virtqueue = rte_malloc(NULL,
283                                sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
284         if (virtqueue == NULL) {
285                 RTE_LOG(ERR, VHOST_CONFIG,
286                         "Failed to allocate memory for virt qp:%d.\n", qp_idx);
287                 return -1;
288         }
289
290         dev->virtqueue[virt_rx_q_idx] = virtqueue;
291         dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
292
293         init_vring_queue_pair(dev, qp_idx);
294
295         dev->virt_qp_nb += 1;
296
297         return 0;
298 }
299
300 /*
301  *  Initialise all variables in device structure.
302  */
303 static void
304 init_device(struct virtio_net *dev)
305 {
306         int vq_offset;
307         uint32_t i;
308
309         /*
310          * Virtqueues have already been malloced so
311          * we don't want to set them to NULL.
312          */
313         vq_offset = offsetof(struct virtio_net, virtqueue);
314         memset(dev, 0, vq_offset);
315
316         for (i = 0; i < dev->virt_qp_nb; i++)
317                 init_vring_queue_pair(dev, i);
318 }
319
320 /*
321  * Function is called from the CUSE open function. The device structure is
322  * initialised and a new entry is added to the device configuration linked
323  * list.
324  */
325 static int
326 new_device(struct vhost_device_ctx ctx)
327 {
328         struct virtio_net_config_ll *new_ll_dev;
329
330         /* Setup device and virtqueues. */
331         new_ll_dev = rte_malloc(NULL, sizeof(struct virtio_net_config_ll), 0);
332         if (new_ll_dev == NULL) {
333                 RTE_LOG(ERR, VHOST_CONFIG,
334                         "(%"PRIu64") Failed to allocate memory for dev.\n",
335                         ctx.fh);
336                 return -1;
337         }
338
339         /* Initialise device and virtqueues. */
340         init_device(&new_ll_dev->dev);
341
342         new_ll_dev->next = NULL;
343
344         /* Add entry to device configuration linked list. */
345         add_config_ll_entry(new_ll_dev);
346
347         return new_ll_dev->dev.device_fh;
348 }
349
350 /*
351  * Function is called from the CUSE release function. This function will
352  * cleanup the device and remove it from device configuration linked list.
353  */
354 static void
355 destroy_device(struct vhost_device_ctx ctx)
356 {
357         struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
358         struct virtio_net_config_ll *ll_dev_cur = ll_root;
359
360         /* Find the linked list entry for the device to be removed. */
361         ll_dev_cur_ctx = get_config_ll_entry(ctx);
362         while (ll_dev_cur != NULL) {
363                 /*
364                  * If the device is found or
365                  * a device that doesn't exist is found then it is removed.
366                  */
367                 if (ll_dev_cur == ll_dev_cur_ctx) {
368                         /*
369                          * If the device is running on a data core then call
370                          * the function to remove it from the data core.
371                          */
372                         if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
373                                 notify_ops->destroy_device(&(ll_dev_cur->dev));
374                         ll_dev_cur = rm_config_ll_entry(ll_dev_cur,
375                                         ll_dev_last);
376                 } else {
377                         ll_dev_last = ll_dev_cur;
378                         ll_dev_cur = ll_dev_cur->next;
379                 }
380         }
381 }
382
383 static void
384 set_ifname(struct vhost_device_ctx ctx,
385         const char *if_name, unsigned int if_len)
386 {
387         struct virtio_net *dev;
388         unsigned int len;
389
390         dev = get_device(ctx);
391         if (dev == NULL)
392                 return;
393
394         len = if_len > sizeof(dev->ifname) ?
395                 sizeof(dev->ifname) : if_len;
396
397         strncpy(dev->ifname, if_name, len);
398 }
399
400
401 /*
402  * Called from CUSE IOCTL: VHOST_SET_OWNER
403  * This function just returns success at the moment unless
404  * the device hasn't been initialised.
405  */
406 static int
407 set_owner(struct vhost_device_ctx ctx)
408 {
409         struct virtio_net *dev;
410
411         dev = get_device(ctx);
412         if (dev == NULL)
413                 return -1;
414
415         return 0;
416 }
417
418 /*
419  * Called from CUSE IOCTL: VHOST_RESET_OWNER
420  */
421 static int
422 reset_owner(struct vhost_device_ctx ctx)
423 {
424         struct virtio_net *dev;
425         uint64_t device_fh;
426
427         dev = get_device(ctx);
428         if (dev == NULL)
429                 return -1;
430
431         device_fh = dev->device_fh;
432         cleanup_device(dev);
433         init_device(dev);
434         dev->device_fh = device_fh;
435         return 0;
436 }
437
438 /*
439  * Called from CUSE IOCTL: VHOST_GET_FEATURES
440  * The features that we support are requested.
441  */
442 static int
443 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
444 {
445         struct virtio_net *dev;
446
447         dev = get_device(ctx);
448         if (dev == NULL)
449                 return -1;
450
451         /* Send our supported features. */
452         *pu = VHOST_FEATURES;
453         return 0;
454 }
455
456 /*
457  * Called from CUSE IOCTL: VHOST_SET_FEATURES
458  * We receive the negotiated features supported by us and the virtio device.
459  */
460 static int
461 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
462 {
463         struct virtio_net *dev;
464         uint16_t vhost_hlen;
465         uint16_t i;
466
467         dev = get_device(ctx);
468         if (dev == NULL)
469                 return -1;
470         if (*pu & ~VHOST_FEATURES)
471                 return -1;
472
473         dev->features = *pu;
474         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
475                 LOG_DEBUG(VHOST_CONFIG,
476                         "(%"PRIu64") Mergeable RX buffers enabled\n",
477                         dev->device_fh);
478                 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
479         } else {
480                 LOG_DEBUG(VHOST_CONFIG,
481                         "(%"PRIu64") Mergeable RX buffers disabled\n",
482                         dev->device_fh);
483                 vhost_hlen = sizeof(struct virtio_net_hdr);
484         }
485
486         for (i = 0; i < dev->virt_qp_nb; i++) {
487                 uint16_t base_idx = i * VIRTIO_QNUM;
488
489                 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
490                 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
491         }
492
493         return 0;
494 }
495
496 /*
497  * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
498  * The virtio device sends us the size of the descriptor ring.
499  */
500 static int
501 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
502 {
503         struct virtio_net *dev;
504
505         dev = get_device(ctx);
506         if (dev == NULL)
507                 return -1;
508
509         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
510         dev->virtqueue[state->index]->size = state->num;
511
512         return 0;
513 }
514
515 /*
516  * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
517  * same numa node as the memory of vring descriptor.
518  */
519 #ifdef RTE_LIBRTE_VHOST_NUMA
520 static struct virtio_net*
521 numa_realloc(struct virtio_net *dev, int index)
522 {
523         int oldnode, newnode;
524         struct virtio_net_config_ll *old_ll_dev, *new_ll_dev = NULL;
525         struct vhost_virtqueue *old_vq, *new_vq = NULL;
526         int ret;
527         int realloc_dev = 0, realloc_vq = 0;
528
529         old_ll_dev = (struct virtio_net_config_ll *)dev;
530         old_vq = dev->virtqueue[index];
531
532         ret  = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
533                         MPOL_F_NODE | MPOL_F_ADDR);
534         ret = ret | get_mempolicy(&oldnode, NULL, 0, old_ll_dev,
535                         MPOL_F_NODE | MPOL_F_ADDR);
536         if (ret) {
537                 RTE_LOG(ERR, VHOST_CONFIG,
538                         "Unable to get vring desc or dev numa information.\n");
539                 return dev;
540         }
541         if (oldnode != newnode)
542                 realloc_dev = 1;
543
544         ret = get_mempolicy(&oldnode, NULL, 0, old_vq,
545                         MPOL_F_NODE | MPOL_F_ADDR);
546         if (ret) {
547                 RTE_LOG(ERR, VHOST_CONFIG,
548                         "Unable to get vq numa information.\n");
549                 return dev;
550         }
551         if (oldnode != newnode)
552                 realloc_vq = 1;
553
554         if (realloc_dev == 0 && realloc_vq == 0)
555                 return dev;
556
557         if (realloc_dev)
558                 new_ll_dev = rte_malloc_socket(NULL,
559                         sizeof(struct virtio_net_config_ll), 0, newnode);
560         if (realloc_vq)
561                 new_vq = rte_malloc_socket(NULL,
562                         sizeof(struct vhost_virtqueue), 0, newnode);
563         if (!new_ll_dev && !new_vq)
564                 return dev;
565
566         if (realloc_vq)
567                 memcpy(new_vq, old_vq, sizeof(*new_vq));
568         if (realloc_dev)
569                 memcpy(new_ll_dev, old_ll_dev, sizeof(*new_ll_dev));
570         (new_ll_dev ? new_ll_dev : old_ll_dev)->dev.virtqueue[index] =
571                 new_vq ? new_vq : old_vq;
572         if (realloc_vq)
573                 rte_free(old_vq);
574         if (realloc_dev) {
575                 if (ll_root == old_ll_dev)
576                         ll_root = new_ll_dev;
577                 else {
578                         struct virtio_net_config_ll *prev = ll_root;
579                         while (prev->next != old_ll_dev)
580                                 prev = prev->next;
581                         prev->next = new_ll_dev;
582                         new_ll_dev->next = old_ll_dev->next;
583                 }
584                 rte_free(old_ll_dev);
585         }
586
587         return realloc_dev ? &new_ll_dev->dev : dev;
588 }
589 #else
590 static struct virtio_net*
591 numa_realloc(struct virtio_net *dev, int index __rte_unused)
592 {
593         return dev;
594 }
595 #endif
596
597 /*
598  * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
599  * The virtio device sends us the desc, used and avail ring addresses.
600  * This function then converts these to our address space.
601  */
602 static int
603 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
604 {
605         struct virtio_net *dev;
606         struct vhost_virtqueue *vq;
607
608         dev = get_device(ctx);
609         if (dev == NULL)
610                 return -1;
611
612         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
613         vq = dev->virtqueue[addr->index];
614
615         /* The addresses are converted from QEMU virtual to Vhost virtual. */
616         vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
617                         addr->desc_user_addr);
618         if (vq->desc == 0) {
619                 RTE_LOG(ERR, VHOST_CONFIG,
620                         "(%"PRIu64") Failed to find desc ring address.\n",
621                         dev->device_fh);
622                 return -1;
623         }
624
625         dev = numa_realloc(dev, addr->index);
626         vq = dev->virtqueue[addr->index];
627
628         vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
629                         addr->avail_user_addr);
630         if (vq->avail == 0) {
631                 RTE_LOG(ERR, VHOST_CONFIG,
632                         "(%"PRIu64") Failed to find avail ring address.\n",
633                         dev->device_fh);
634                 return -1;
635         }
636
637         vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
638                         addr->used_user_addr);
639         if (vq->used == 0) {
640                 RTE_LOG(ERR, VHOST_CONFIG,
641                         "(%"PRIu64") Failed to find used ring address.\n",
642                         dev->device_fh);
643                 return -1;
644         }
645
646         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
647                         dev->device_fh, vq->desc);
648         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
649                         dev->device_fh, vq->avail);
650         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
651                         dev->device_fh, vq->used);
652
653         return 0;
654 }
655
656 /*
657  * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
658  * The virtio device sends us the available ring last used index.
659  */
660 static int
661 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
662 {
663         struct virtio_net *dev;
664
665         dev = get_device(ctx);
666         if (dev == NULL)
667                 return -1;
668
669         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
670         dev->virtqueue[state->index]->last_used_idx = state->num;
671         dev->virtqueue[state->index]->last_used_idx_res = state->num;
672
673         return 0;
674 }
675
676 /*
677  * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
678  * We send the virtio device our available ring last used index.
679  */
680 static int
681 get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
682         struct vhost_vring_state *state)
683 {
684         struct virtio_net *dev;
685
686         dev = get_device(ctx);
687         if (dev == NULL)
688                 return -1;
689
690         state->index = index;
691         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
692         state->num = dev->virtqueue[state->index]->last_used_idx;
693
694         return 0;
695 }
696
697
698 /*
699  * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
700  * The virtio device sends an eventfd to interrupt the guest. This fd gets
701  * copied into our process space.
702  */
703 static int
704 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
705 {
706         struct virtio_net *dev;
707         struct vhost_virtqueue *vq;
708         uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
709
710         dev = get_device(ctx);
711         if (dev == NULL)
712                 return -1;
713
714         /*
715          * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
716          * we get, so we do vring queue pair allocation here.
717          */
718         if (cur_qp_idx + 1 > dev->virt_qp_nb) {
719                 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
720                         return -1;
721         }
722
723         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
724         vq = dev->virtqueue[file->index];
725         assert(vq != NULL);
726
727         if (vq->callfd >= 0)
728                 close(vq->callfd);
729
730         vq->callfd = file->fd;
731
732         return 0;
733 }
734
735 /*
736  * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
737  * The virtio device sends an eventfd that it can use to notify us.
738  * This fd gets copied into our process space.
739  */
740 static int
741 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
742 {
743         struct virtio_net *dev;
744         struct vhost_virtqueue *vq;
745
746         dev = get_device(ctx);
747         if (dev == NULL)
748                 return -1;
749
750         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
751         vq = dev->virtqueue[file->index];
752
753         if (vq->kickfd >= 0)
754                 close(vq->kickfd);
755
756         vq->kickfd = file->fd;
757
758         return 0;
759 }
760
761 /*
762  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
763  * To complete device initialisation when the virtio driver is loaded,
764  * we are provided with a valid fd for a tap device (not used by us).
765  * If this happens then we can add the device to a data core.
766  * When the virtio driver is removed we get fd=-1.
767  * At that point we remove the device from the data core.
768  * The device will still exist in the device configuration linked list.
769  */
770 static int
771 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
772 {
773         struct virtio_net *dev;
774
775         dev = get_device(ctx);
776         if (dev == NULL)
777                 return -1;
778
779         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
780         dev->virtqueue[file->index]->backend = file->fd;
781
782         /*
783          * If the device isn't already running and both backend fds are set,
784          * we add the device.
785          */
786         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
787                 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
788                         ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
789                         return notify_ops->new_device(dev);
790                 }
791         /* Otherwise we remove it. */
792         } else
793                 if (file->fd == VIRTIO_DEV_STOPPED)
794                         notify_ops->destroy_device(dev);
795         return 0;
796 }
797
798 /*
799  * Function pointers are set for the device operations to allow CUSE to call
800  * functions when an IOCTL, device_add or device_release is received.
801  */
802 static const struct vhost_net_device_ops vhost_device_ops = {
803         .new_device = new_device,
804         .destroy_device = destroy_device,
805
806         .set_ifname = set_ifname,
807
808         .get_features = get_features,
809         .set_features = set_features,
810
811         .set_vring_num = set_vring_num,
812         .set_vring_addr = set_vring_addr,
813         .set_vring_base = set_vring_base,
814         .get_vring_base = get_vring_base,
815
816         .set_vring_kick = set_vring_kick,
817         .set_vring_call = set_vring_call,
818
819         .set_backend = set_backend,
820
821         .set_owner = set_owner,
822         .reset_owner = reset_owner,
823 };
824
825 /*
826  * Called by main to setup callbacks when registering CUSE device.
827  */
828 struct vhost_net_device_ops const *
829 get_virtio_net_callbacks(void)
830 {
831         return &vhost_device_ops;
832 }
833
834 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
835         uint16_t queue_id, int enable)
836 {
837         if (enable) {
838                 RTE_LOG(ERR, VHOST_CONFIG,
839                         "guest notification isn't supported.\n");
840                 return -1;
841         }
842
843         dev->virtqueue[queue_id]->used->flags =
844                 enable ? 0 : VRING_USED_F_NO_NOTIFY;
845         return 0;
846 }
847
848 uint64_t rte_vhost_feature_get(void)
849 {
850         return VHOST_FEATURES;
851 }
852
853 int rte_vhost_feature_disable(uint64_t feature_mask)
854 {
855         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
856         return 0;
857 }
858
859 int rte_vhost_feature_enable(uint64_t feature_mask)
860 {
861         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
862                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
863                 return 0;
864         }
865         return -1;
866 }
867
868 /*
869  * Register ops so that we can add/remove device to data core.
870  */
871 int
872 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
873 {
874         notify_ops = ops;
875
876         return 0;
877 }