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