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