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