9dea69c47d2b2933813940f120793dad96c04b21
[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 <sys/mman.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42
43
44 #include <sys/socket.h>
45 #include <linux/if_tun.h>
46 #include <linux/if.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_virtio_net.h>
53
54 #include "vhost_cuse/eventfd_copy.h"
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 /* Features supported by this lib. */
72 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
73                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
74                                 (1ULL << VIRTIO_NET_F_CTRL_RX))
75 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
76
77
78 /*
79  * Converts QEMU virtual address to Vhost virtual address. This function is
80  * used to convert the ring addresses to our address space.
81  */
82 static uint64_t
83 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
84 {
85         struct virtio_memory_regions *region;
86         uint64_t vhost_va = 0;
87         uint32_t regionidx = 0;
88
89         /* Find the region where the address lives. */
90         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
91                 region = &dev->mem->regions[regionidx];
92                 if ((qemu_va >= region->userspace_address) &&
93                         (qemu_va <= region->userspace_address +
94                         region->memory_size)) {
95                         vhost_va = qemu_va + region->guest_phys_address +
96                                 region->address_offset -
97                                 region->userspace_address;
98                         break;
99                 }
100         }
101         return vhost_va;
102 }
103
104
105 /*
106  * Retrieves an entry from the devices configuration linked list.
107  */
108 static struct virtio_net_config_ll *
109 get_config_ll_entry(struct vhost_device_ctx ctx)
110 {
111         struct virtio_net_config_ll *ll_dev = ll_root;
112
113         /* Loop through linked list until the device_fh is found. */
114         while (ll_dev != NULL) {
115                 if (ll_dev->dev.device_fh == ctx.fh)
116                         return ll_dev;
117                 ll_dev = ll_dev->next;
118         }
119
120         return NULL;
121 }
122
123 /*
124  * Searches the configuration core linked list and
125  * retrieves the device if it exists.
126  */
127 struct virtio_net *
128 get_device(struct vhost_device_ctx ctx)
129 {
130         struct virtio_net_config_ll *ll_dev;
131
132         ll_dev = get_config_ll_entry(ctx);
133
134         if (ll_dev)
135                 return &ll_dev->dev;
136
137         RTE_LOG(ERR, VHOST_CONFIG,
138                 "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
139         return NULL;
140 }
141
142 /*
143  * Add entry containing a device to the device configuration linked list.
144  */
145 static void
146 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
147 {
148         struct virtio_net_config_ll *ll_dev = ll_root;
149
150         /* If ll_dev == NULL then this is the first device so go to else */
151         if (ll_dev) {
152                 /* If the 1st device_fh != 0 then we insert our device here. */
153                 if (ll_dev->dev.device_fh != 0) {
154                         new_ll_dev->dev.device_fh = 0;
155                         new_ll_dev->next = ll_dev;
156                         ll_root = new_ll_dev;
157                 } else {
158                         /*
159                          * Increment through the ll until we find un unused
160                          * device_fh. Insert the device at that entry.
161                          */
162                         while ((ll_dev->next != NULL) &&
163                                 (ll_dev->dev.device_fh ==
164                                         (ll_dev->next->dev.device_fh - 1)))
165                                 ll_dev = ll_dev->next;
166
167                         new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
168                         new_ll_dev->next = ll_dev->next;
169                         ll_dev->next = new_ll_dev;
170                 }
171         } else {
172                 ll_root = new_ll_dev;
173                 ll_root->dev.device_fh = 0;
174         }
175
176 }
177
178 /*
179  * Unmap any memory, close any file descriptors and
180  * free any memory owned by a device.
181  */
182 static void
183 cleanup_device(struct virtio_net *dev)
184 {
185         /* Unmap QEMU memory file if mapped. */
186         if (dev->mem) {
187                 munmap((void *)(uintptr_t)dev->mem->mapped_address,
188                         (size_t)dev->mem->mapped_size);
189                 free(dev->mem);
190         }
191
192         /* Close any event notifiers opened by device. */
193         if (dev->virtqueue[VIRTIO_RXQ]->callfd)
194                 close((int)dev->virtqueue[VIRTIO_RXQ]->callfd);
195         if (dev->virtqueue[VIRTIO_RXQ]->kickfd)
196                 close((int)dev->virtqueue[VIRTIO_RXQ]->kickfd);
197         if (dev->virtqueue[VIRTIO_TXQ]->callfd)
198                 close((int)dev->virtqueue[VIRTIO_TXQ]->callfd);
199         if (dev->virtqueue[VIRTIO_TXQ]->kickfd)
200                 close((int)dev->virtqueue[VIRTIO_TXQ]->kickfd);
201 }
202
203 /*
204  * Release virtqueues and device memory.
205  */
206 static void
207 free_device(struct virtio_net_config_ll *ll_dev)
208 {
209         /* Free any malloc'd memory */
210         free(ll_dev->dev.virtqueue[VIRTIO_RXQ]);
211         free(ll_dev->dev.virtqueue[VIRTIO_TXQ]);
212         free(ll_dev);
213 }
214
215 /*
216  * Remove an entry from the device configuration linked list.
217  */
218 static struct virtio_net_config_ll *
219 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
220         struct virtio_net_config_ll *ll_dev_last)
221 {
222         /* First remove the device and then clean it up. */
223         if (ll_dev == ll_root) {
224                 ll_root = ll_dev->next;
225                 cleanup_device(&ll_dev->dev);
226                 free_device(ll_dev);
227                 return ll_root;
228         } else {
229                 if (likely(ll_dev_last != NULL)) {
230                         ll_dev_last->next = ll_dev->next;
231                         cleanup_device(&ll_dev->dev);
232                         free_device(ll_dev);
233                         return ll_dev_last->next;
234                 } else {
235                         cleanup_device(&ll_dev->dev);
236                         free_device(ll_dev);
237                         RTE_LOG(ERR, VHOST_CONFIG,
238                                 "Remove entry from config_ll failed\n");
239                         return NULL;
240                 }
241         }
242 }
243
244 /*
245  *  Initialise all variables in device structure.
246  */
247 static void
248 init_device(struct virtio_net *dev)
249 {
250         uint64_t vq_offset;
251
252         /*
253          * Virtqueues have already been malloced so
254          * we don't want to set them to NULL.
255          */
256         vq_offset = offsetof(struct virtio_net, mem);
257
258         /* Set everything to 0. */
259         memset((void *)(uintptr_t)((uint64_t)(uintptr_t)dev + vq_offset), 0,
260                 (sizeof(struct virtio_net) - (size_t)vq_offset));
261         memset(dev->virtqueue[VIRTIO_RXQ], 0, sizeof(struct vhost_virtqueue));
262         memset(dev->virtqueue[VIRTIO_TXQ], 0, sizeof(struct vhost_virtqueue));
263
264         dev->virtqueue[VIRTIO_RXQ]->kickfd = (eventfd_t)-1;
265         dev->virtqueue[VIRTIO_RXQ]->callfd = (eventfd_t)-1;
266         dev->virtqueue[VIRTIO_TXQ]->kickfd = (eventfd_t)-1;
267         dev->virtqueue[VIRTIO_TXQ]->callfd = (eventfd_t)-1;
268
269         /* Backends are set to -1 indicating an inactive device. */
270         dev->virtqueue[VIRTIO_RXQ]->backend = VIRTIO_DEV_STOPPED;
271         dev->virtqueue[VIRTIO_TXQ]->backend = VIRTIO_DEV_STOPPED;
272 }
273
274 /*
275  * Function is called from the CUSE open function. The device structure is
276  * initialised and a new entry is added to the device configuration linked
277  * list.
278  */
279 static int
280 new_device(struct vhost_device_ctx ctx)
281 {
282         struct virtio_net_config_ll *new_ll_dev;
283         struct vhost_virtqueue *virtqueue_rx, *virtqueue_tx;
284
285         /* Setup device and virtqueues. */
286         new_ll_dev = malloc(sizeof(struct virtio_net_config_ll));
287         if (new_ll_dev == NULL) {
288                 RTE_LOG(ERR, VHOST_CONFIG,
289                         "(%"PRIu64") Failed to allocate memory for dev.\n",
290                         ctx.fh);
291                 return -1;
292         }
293
294         virtqueue_rx = malloc(sizeof(struct vhost_virtqueue));
295         if (virtqueue_rx == NULL) {
296                 free(new_ll_dev);
297                 RTE_LOG(ERR, VHOST_CONFIG,
298                         "(%"PRIu64") Failed to allocate memory for rxq.\n",
299                         ctx.fh);
300                 return -1;
301         }
302
303         virtqueue_tx = malloc(sizeof(struct vhost_virtqueue));
304         if (virtqueue_tx == NULL) {
305                 free(virtqueue_rx);
306                 free(new_ll_dev);
307                 RTE_LOG(ERR, VHOST_CONFIG,
308                         "(%"PRIu64") Failed to allocate memory for txq.\n",
309                         ctx.fh);
310                 return -1;
311         }
312
313         new_ll_dev->dev.virtqueue[VIRTIO_RXQ] = virtqueue_rx;
314         new_ll_dev->dev.virtqueue[VIRTIO_TXQ] = virtqueue_tx;
315
316         /* Initialise device and virtqueues. */
317         init_device(&new_ll_dev->dev);
318
319         new_ll_dev->next = NULL;
320
321         /* Add entry to device configuration linked list. */
322         add_config_ll_entry(new_ll_dev);
323
324         return new_ll_dev->dev.device_fh;
325 }
326
327 /*
328  * Function is called from the CUSE release function. This function will
329  * cleanup the device and remove it from device configuration linked list.
330  */
331 static void
332 destroy_device(struct vhost_device_ctx ctx)
333 {
334         struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
335         struct virtio_net_config_ll *ll_dev_cur = ll_root;
336
337         /* Find the linked list entry for the device to be removed. */
338         ll_dev_cur_ctx = get_config_ll_entry(ctx);
339         while (ll_dev_cur != NULL) {
340                 /*
341                  * If the device is found or
342                  * a device that doesn't exist is found then it is removed.
343                  */
344                 if (ll_dev_cur == ll_dev_cur_ctx) {
345                         /*
346                          * If the device is running on a data core then call
347                          * the function to remove it from the data core.
348                          */
349                         if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
350                                 notify_ops->destroy_device(&(ll_dev_cur->dev));
351                         ll_dev_cur = rm_config_ll_entry(ll_dev_cur,
352                                         ll_dev_last);
353                 } else {
354                         ll_dev_last = ll_dev_cur;
355                         ll_dev_cur = ll_dev_cur->next;
356                 }
357         }
358 }
359
360 /*
361  * Called from CUSE IOCTL: VHOST_SET_OWNER
362  * This function just returns success at the moment unless
363  * the device hasn't been initialised.
364  */
365 static int
366 set_owner(struct vhost_device_ctx ctx)
367 {
368         struct virtio_net *dev;
369
370         dev = get_device(ctx);
371         if (dev == NULL)
372                 return -1;
373
374         return 0;
375 }
376
377 /*
378  * Called from CUSE IOCTL: VHOST_RESET_OWNER
379  */
380 static int
381 reset_owner(struct vhost_device_ctx ctx)
382 {
383         struct virtio_net_config_ll *ll_dev;
384
385         ll_dev = get_config_ll_entry(ctx);
386
387         cleanup_device(&ll_dev->dev);
388         init_device(&ll_dev->dev);
389
390         return 0;
391 }
392
393 /*
394  * Called from CUSE IOCTL: VHOST_GET_FEATURES
395  * The features that we support are requested.
396  */
397 static int
398 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
399 {
400         struct virtio_net *dev;
401
402         dev = get_device(ctx);
403         if (dev == NULL)
404                 return -1;
405
406         /* Send our supported features. */
407         *pu = VHOST_FEATURES;
408         return 0;
409 }
410
411 /*
412  * Called from CUSE IOCTL: VHOST_SET_FEATURES
413  * We receive the negotiated features supported by us and the virtio device.
414  */
415 static int
416 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
417 {
418         struct virtio_net *dev;
419
420         dev = get_device(ctx);
421         if (dev == NULL)
422                 return -1;
423         if (*pu & ~VHOST_FEATURES)
424                 return -1;
425
426         /* Store the negotiated feature list for the device. */
427         dev->features = *pu;
428
429         /* Set the vhost_hlen depending on if VIRTIO_NET_F_MRG_RXBUF is set. */
430         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
431                 LOG_DEBUG(VHOST_CONFIG,
432                         "(%"PRIu64") Mergeable RX buffers enabled\n",
433                         dev->device_fh);
434                 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
435                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
436                 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
437                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
438         } else {
439                 LOG_DEBUG(VHOST_CONFIG,
440                         "(%"PRIu64") Mergeable RX buffers disabled\n",
441                         dev->device_fh);
442                 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
443                         sizeof(struct virtio_net_hdr);
444                 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
445                         sizeof(struct virtio_net_hdr);
446         }
447         return 0;
448 }
449
450 /*
451  * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
452  * The virtio device sends us the size of the descriptor ring.
453  */
454 static int
455 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
456 {
457         struct virtio_net *dev;
458
459         dev = get_device(ctx);
460         if (dev == NULL)
461                 return -1;
462
463         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
464         dev->virtqueue[state->index]->size = state->num;
465
466         return 0;
467 }
468
469 /*
470  * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
471  * The virtio device sends us the desc, used and avail ring addresses.
472  * This function then converts these to our address space.
473  */
474 static int
475 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
476 {
477         struct virtio_net *dev;
478         struct vhost_virtqueue *vq;
479
480         dev = get_device(ctx);
481         if (dev == NULL)
482                 return -1;
483
484         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
485         vq = dev->virtqueue[addr->index];
486
487         /* The addresses are converted from QEMU virtual to Vhost virtual. */
488         vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
489                         addr->desc_user_addr);
490         if (vq->desc == 0) {
491                 RTE_LOG(ERR, VHOST_CONFIG,
492                         "(%"PRIu64") Failed to find desc ring address.\n",
493                         dev->device_fh);
494                 return -1;
495         }
496
497         vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
498                         addr->avail_user_addr);
499         if (vq->avail == 0) {
500                 RTE_LOG(ERR, VHOST_CONFIG,
501                         "(%"PRIu64") Failed to find avail ring address.\n",
502                         dev->device_fh);
503                 return -1;
504         }
505
506         vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
507                         addr->used_user_addr);
508         if (vq->used == 0) {
509                 RTE_LOG(ERR, VHOST_CONFIG,
510                         "(%"PRIu64") Failed to find used ring address.\n",
511                         dev->device_fh);
512                 return -1;
513         }
514
515         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
516                         dev->device_fh, vq->desc);
517         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
518                         dev->device_fh, vq->avail);
519         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
520                         dev->device_fh, vq->used);
521
522         return 0;
523 }
524
525 /*
526  * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
527  * The virtio device sends us the available ring last used index.
528  */
529 static int
530 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
531 {
532         struct virtio_net *dev;
533
534         dev = get_device(ctx);
535         if (dev == NULL)
536                 return -1;
537
538         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
539         dev->virtqueue[state->index]->last_used_idx = state->num;
540         dev->virtqueue[state->index]->last_used_idx_res = state->num;
541
542         return 0;
543 }
544
545 /*
546  * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
547  * We send the virtio device our available ring last used index.
548  */
549 static int
550 get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
551         struct vhost_vring_state *state)
552 {
553         struct virtio_net *dev;
554
555         dev = get_device(ctx);
556         if (dev == NULL)
557                 return -1;
558
559         state->index = index;
560         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
561         state->num = dev->virtqueue[state->index]->last_used_idx;
562
563         return 0;
564 }
565
566
567 /*
568  * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
569  * The virtio device sends an eventfd to interrupt the guest. This fd gets
570  * copied into our process space.
571  */
572 static int
573 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
574 {
575         struct virtio_net *dev;
576         struct vhost_virtqueue *vq;
577
578         dev = get_device(ctx);
579         if (dev == NULL)
580                 return -1;
581
582         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
583         vq = dev->virtqueue[file->index];
584
585         if ((int)vq->kickfd >= 0)
586                 close((int)vq->kickfd);
587
588         vq->kickfd = file->fd;
589
590         return 0;
591 }
592
593 /*
594  * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
595  * The virtio device sends an eventfd that it can use to notify us.
596  * This fd gets copied into our process space.
597  */
598 static int
599 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
600 {
601         struct virtio_net *dev;
602         struct vhost_virtqueue *vq;
603
604         dev = get_device(ctx);
605         if (dev == NULL)
606                 return -1;
607
608         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
609         vq = dev->virtqueue[file->index];
610
611         if ((int)vq->callfd >= 0)
612                 close((int)vq->callfd);
613
614         vq->callfd = file->fd;
615
616         return 0;
617 }
618
619 /*
620  * Function to get the tap device name from the provided file descriptor and
621  * save it in the device structure.
622  */
623 static int
624 get_ifname(struct virtio_net *dev, int tap_fd, int pid)
625 {
626         int fd_tap;
627         struct ifreq ifr;
628         uint32_t size, ifr_size;
629         int ret;
630
631     fd_tap = eventfd_copy(tap_fd, pid);
632     if (fd_tap < 0)
633         return -1;
634
635         ret = ioctl(fd_tap, TUNGETIFF, &ifr);
636
637         if (close(fd_tap) < 0)
638                 RTE_LOG(ERR, VHOST_CONFIG,
639                         "(%"PRIu64") fd close failed\n",
640                         dev->device_fh);
641
642         if (ret >= 0) {
643                 ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
644                 size = ifr_size > sizeof(dev->ifname) ?
645                                 sizeof(dev->ifname) : ifr_size;
646
647                 strncpy(dev->ifname, ifr.ifr_name, size);
648         } else
649                 RTE_LOG(ERR, VHOST_CONFIG,
650                         "(%"PRIu64") TUNGETIFF ioctl failed\n",
651                         dev->device_fh);
652
653         return 0;
654 }
655
656 /*
657  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
658  * To complete device initialisation when the virtio driver is loaded,
659  * we are provided with a valid fd for a tap device (not used by us).
660  * If this happens then we can add the device to a data core.
661  * When the virtio driver is removed we get fd=-1.
662  * At that point we remove the device from the data core.
663  * The device will still exist in the device configuration linked list.
664  */
665 static int
666 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
667 {
668         struct virtio_net *dev;
669
670         dev = get_device(ctx);
671         if (dev == NULL)
672                 return -1;
673
674         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
675         dev->virtqueue[file->index]->backend = file->fd;
676
677         /*
678          * If the device isn't already running and both backend fds are set,
679          * we add the device.
680          */
681         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
682                 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
683                         ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
684                         get_ifname(dev, file->fd, ctx.pid);
685                         return notify_ops->new_device(dev);
686                 }
687         /* Otherwise we remove it. */
688         } else
689                 if (file->fd == VIRTIO_DEV_STOPPED)
690                         notify_ops->destroy_device(dev);
691         return 0;
692 }
693
694 /*
695  * Function pointers are set for the device operations to allow CUSE to call
696  * functions when an IOCTL, device_add or device_release is received.
697  */
698 static const struct vhost_net_device_ops vhost_device_ops = {
699         .new_device = new_device,
700         .destroy_device = destroy_device,
701
702         .get_features = get_features,
703         .set_features = set_features,
704
705         .set_vring_num = set_vring_num,
706         .set_vring_addr = set_vring_addr,
707         .set_vring_base = set_vring_base,
708         .get_vring_base = get_vring_base,
709
710         .set_vring_kick = set_vring_kick,
711         .set_vring_call = set_vring_call,
712
713         .set_backend = set_backend,
714
715         .set_owner = set_owner,
716         .reset_owner = reset_owner,
717 };
718
719 /*
720  * Called by main to setup callbacks when registering CUSE device.
721  */
722 struct vhost_net_device_ops const *
723 get_virtio_net_callbacks(void)
724 {
725         return &vhost_device_ops;
726 }
727
728 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
729         uint16_t queue_id, int enable)
730 {
731         if (enable) {
732                 RTE_LOG(ERR, VHOST_CONFIG,
733                         "guest notification isn't supported.\n");
734                 return -1;
735         }
736
737         dev->virtqueue[queue_id]->used->flags =
738                 enable ? 0 : VRING_USED_F_NO_NOTIFY;
739         return 0;
740 }
741
742 uint64_t rte_vhost_feature_get(void)
743 {
744         return VHOST_FEATURES;
745 }
746
747 int rte_vhost_feature_disable(uint64_t feature_mask)
748 {
749         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
750         return 0;
751 }
752
753 int rte_vhost_feature_enable(uint64_t feature_mask)
754 {
755         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
756                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
757                 return 0;
758         }
759         return -1;
760 }
761
762 /*
763  * Register ops so that we can add/remove device to data core.
764  */
765 int
766 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
767 {
768         notify_ops = ops;
769
770         return 0;
771 }