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