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