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