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