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