c45ed1c5e36d369dc1121df6e7980d99bec62586
[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 = VIRTIO_UNINITIALIZED_EVENTFD;
171         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
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 == NULL)
297                 return;
298
299         if (dev->flags & VIRTIO_DEV_RUNNING) {
300                 dev->flags &= ~VIRTIO_DEV_RUNNING;
301                 notify_ops->destroy_device(dev);
302         }
303
304         cleanup_device(dev, 1);
305         free_device(dev);
306
307         vhost_devices[ctx.fh] = NULL;
308 }
309
310 void
311 vhost_set_ifname(struct vhost_device_ctx ctx,
312         const char *if_name, unsigned int if_len)
313 {
314         struct virtio_net *dev;
315         unsigned int len;
316
317         dev = get_device(ctx);
318         if (dev == NULL)
319                 return;
320
321         len = if_len > sizeof(dev->ifname) ?
322                 sizeof(dev->ifname) : if_len;
323
324         strncpy(dev->ifname, if_name, len);
325         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
326 }
327
328
329 /*
330  * Called from CUSE IOCTL: VHOST_SET_OWNER
331  * This function just returns success at the moment unless
332  * the device hasn't been initialised.
333  */
334 int
335 vhost_set_owner(struct vhost_device_ctx ctx)
336 {
337         struct virtio_net *dev;
338
339         dev = get_device(ctx);
340         if (dev == NULL)
341                 return -1;
342
343         return 0;
344 }
345
346 /*
347  * Called from CUSE IOCTL: VHOST_RESET_OWNER
348  */
349 int
350 vhost_reset_owner(struct vhost_device_ctx ctx)
351 {
352         struct virtio_net *dev;
353
354         dev = get_device(ctx);
355         if (dev == NULL)
356                 return -1;
357
358         if (dev->flags & VIRTIO_DEV_RUNNING) {
359                 dev->flags &= ~VIRTIO_DEV_RUNNING;
360                 notify_ops->destroy_device(dev);
361         }
362
363         cleanup_device(dev, 0);
364         reset_device(dev);
365         return 0;
366 }
367
368 /*
369  * Called from CUSE IOCTL: VHOST_GET_FEATURES
370  * The features that we support are requested.
371  */
372 int
373 vhost_get_features(struct vhost_device_ctx ctx, uint64_t *pu)
374 {
375         struct virtio_net *dev;
376
377         dev = get_device(ctx);
378         if (dev == NULL)
379                 return -1;
380
381         /* Send our supported features. */
382         *pu = VHOST_FEATURES;
383         return 0;
384 }
385
386 /*
387  * Called from CUSE IOCTL: VHOST_SET_FEATURES
388  * We receive the negotiated features supported by us and the virtio device.
389  */
390 int
391 vhost_set_features(struct vhost_device_ctx ctx, uint64_t *pu)
392 {
393         struct virtio_net *dev;
394         uint16_t vhost_hlen;
395         uint16_t i;
396
397         dev = get_device(ctx);
398         if (dev == NULL)
399                 return -1;
400         if (*pu & ~VHOST_FEATURES)
401                 return -1;
402
403         dev->features = *pu;
404         if (dev->features &
405                 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
406                 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
407         } else {
408                 vhost_hlen = sizeof(struct virtio_net_hdr);
409         }
410         LOG_DEBUG(VHOST_CONFIG,
411                 "(%"PRIu64") Mergeable RX buffers %s, virtio 1 %s\n",
412                 dev->device_fh,
413                 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
414                 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
415
416         for (i = 0; i < dev->virt_qp_nb; i++) {
417                 uint16_t base_idx = i * VIRTIO_QNUM;
418
419                 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
420                 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
421         }
422
423         return 0;
424 }
425
426 /*
427  * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
428  * The virtio device sends us the size of the descriptor ring.
429  */
430 int
431 vhost_set_vring_num(struct vhost_device_ctx ctx,
432         struct vhost_vring_state *state)
433 {
434         struct virtio_net *dev;
435
436         dev = get_device(ctx);
437         if (dev == NULL)
438                 return -1;
439
440         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
441         dev->virtqueue[state->index]->size = state->num;
442
443         return 0;
444 }
445
446 /*
447  * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
448  * same numa node as the memory of vring descriptor.
449  */
450 #ifdef RTE_LIBRTE_VHOST_NUMA
451 static struct virtio_net*
452 numa_realloc(struct virtio_net *dev, int index)
453 {
454         int oldnode, newnode;
455         struct virtio_net *old_dev;
456         struct vhost_virtqueue *old_vq, *vq;
457         int ret;
458
459         /*
460          * vq is allocated on pairs, we should try to do realloc
461          * on first queue of one queue pair only.
462          */
463         if (index % VIRTIO_QNUM != 0)
464                 return dev;
465
466         old_dev = dev;
467         vq = old_vq = dev->virtqueue[index];
468
469         ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
470                             MPOL_F_NODE | MPOL_F_ADDR);
471
472         /* check if we need to reallocate vq */
473         ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
474                              MPOL_F_NODE | MPOL_F_ADDR);
475         if (ret) {
476                 RTE_LOG(ERR, VHOST_CONFIG,
477                         "Unable to get vq numa information.\n");
478                 return dev;
479         }
480         if (oldnode != newnode) {
481                 RTE_LOG(INFO, VHOST_CONFIG,
482                         "reallocate vq from %d to %d node\n", oldnode, newnode);
483                 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
484                                        newnode);
485                 if (!vq)
486                         return dev;
487
488                 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
489                 rte_free(old_vq);
490         }
491
492         /* check if we need to reallocate dev */
493         ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
494                             MPOL_F_NODE | MPOL_F_ADDR);
495         if (ret) {
496                 RTE_LOG(ERR, VHOST_CONFIG,
497                         "Unable to get dev numa information.\n");
498                 goto out;
499         }
500         if (oldnode != newnode) {
501                 RTE_LOG(INFO, VHOST_CONFIG,
502                         "reallocate dev from %d to %d node\n",
503                         oldnode, newnode);
504                 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
505                 if (!dev) {
506                         dev = old_dev;
507                         goto out;
508                 }
509
510                 memcpy(dev, old_dev, sizeof(*dev));
511                 rte_free(old_dev);
512         }
513
514 out:
515         dev->virtqueue[index] = vq;
516         dev->virtqueue[index + 1] = vq + 1;
517         vhost_devices[dev->device_fh] = dev;
518
519         return dev;
520 }
521 #else
522 static struct virtio_net*
523 numa_realloc(struct virtio_net *dev, int index __rte_unused)
524 {
525         return dev;
526 }
527 #endif
528
529 /*
530  * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
531  * The virtio device sends us the desc, used and avail ring addresses.
532  * This function then converts these to our address space.
533  */
534 int
535 vhost_set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
536 {
537         struct virtio_net *dev;
538         struct vhost_virtqueue *vq;
539
540         dev = get_device(ctx);
541         if ((dev == NULL) || (dev->mem == NULL))
542                 return -1;
543
544         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
545         vq = dev->virtqueue[addr->index];
546
547         /* The addresses are converted from QEMU virtual to Vhost virtual. */
548         vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
549                         addr->desc_user_addr);
550         if (vq->desc == 0) {
551                 RTE_LOG(ERR, VHOST_CONFIG,
552                         "(%"PRIu64") Failed to find desc ring address.\n",
553                         dev->device_fh);
554                 return -1;
555         }
556
557         dev = numa_realloc(dev, addr->index);
558         vq = dev->virtqueue[addr->index];
559
560         vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
561                         addr->avail_user_addr);
562         if (vq->avail == 0) {
563                 RTE_LOG(ERR, VHOST_CONFIG,
564                         "(%"PRIu64") Failed to find avail ring address.\n",
565                         dev->device_fh);
566                 return -1;
567         }
568
569         vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
570                         addr->used_user_addr);
571         if (vq->used == 0) {
572                 RTE_LOG(ERR, VHOST_CONFIG,
573                         "(%"PRIu64") Failed to find used ring address.\n",
574                         dev->device_fh);
575                 return -1;
576         }
577
578         vq->log_guest_addr = addr->log_guest_addr;
579
580         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
581                         dev->device_fh, vq->desc);
582         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
583                         dev->device_fh, vq->avail);
584         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
585                         dev->device_fh, vq->used);
586         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") log_guest_addr: %"PRIx64"\n",
587                         dev->device_fh, vq->log_guest_addr);
588
589         return 0;
590 }
591
592 /*
593  * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
594  * The virtio device sends us the available ring last used index.
595  */
596 int
597 vhost_set_vring_base(struct vhost_device_ctx ctx,
598         struct vhost_vring_state *state)
599 {
600         struct virtio_net *dev;
601
602         dev = get_device(ctx);
603         if (dev == NULL)
604                 return -1;
605
606         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
607         dev->virtqueue[state->index]->last_used_idx = state->num;
608         dev->virtqueue[state->index]->last_used_idx_res = state->num;
609
610         return 0;
611 }
612
613 /*
614  * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
615  * We send the virtio device our available ring last used index.
616  */
617 int
618 vhost_get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
619         struct vhost_vring_state *state)
620 {
621         struct virtio_net *dev;
622
623         dev = get_device(ctx);
624         if (dev == NULL)
625                 return -1;
626
627         state->index = index;
628         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
629         state->num = dev->virtqueue[state->index]->last_used_idx;
630
631         return 0;
632 }
633
634
635 /*
636  * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
637  * The virtio device sends an eventfd to interrupt the guest. This fd gets
638  * copied into our process space.
639  */
640 int
641 vhost_set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
642 {
643         struct virtio_net *dev;
644         struct vhost_virtqueue *vq;
645         uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
646
647         dev = get_device(ctx);
648         if (dev == NULL)
649                 return -1;
650
651         /*
652          * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
653          * we get, so we do vring queue pair allocation here.
654          */
655         if (cur_qp_idx + 1 > dev->virt_qp_nb) {
656                 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
657                         return -1;
658         }
659
660         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
661         vq = dev->virtqueue[file->index];
662         assert(vq != NULL);
663
664         if (vq->callfd >= 0)
665                 close(vq->callfd);
666
667         vq->callfd = file->fd;
668
669         return 0;
670 }
671
672 /*
673  * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
674  * The virtio device sends an eventfd that it can use to notify us.
675  * This fd gets copied into our process space.
676  */
677 int
678 vhost_set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
679 {
680         struct virtio_net *dev;
681         struct vhost_virtqueue *vq;
682
683         dev = get_device(ctx);
684         if (dev == NULL)
685                 return -1;
686
687         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
688         vq = dev->virtqueue[file->index];
689
690         if (vq->kickfd >= 0)
691                 close(vq->kickfd);
692
693         vq->kickfd = file->fd;
694
695         return 0;
696 }
697
698 /*
699  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
700  * To complete device initialisation when the virtio driver is loaded,
701  * we are provided with a valid fd for a tap device (not used by us).
702  * If this happens then we can add the device to a data core.
703  * When the virtio driver is removed we get fd=-1.
704  * At that point we remove the device from the data core.
705  * The device will still exist in the device configuration linked list.
706  */
707 int
708 vhost_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
709 {
710         struct virtio_net *dev;
711
712         dev = get_device(ctx);
713         if (dev == NULL)
714                 return -1;
715
716         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
717         dev->virtqueue[file->index]->backend = file->fd;
718
719         /*
720          * If the device isn't already running and both backend fds are set,
721          * we add the device.
722          */
723         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
724                 if (dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED &&
725                     dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED) {
726                         if (notify_ops->new_device(dev) < 0)
727                                 return -1;
728                         dev->flags |= VIRTIO_DEV_RUNNING;
729                 }
730         } else if (file->fd == VIRTIO_DEV_STOPPED) {
731                 dev->flags &= ~VIRTIO_DEV_RUNNING;
732                 notify_ops->destroy_device(dev);
733         }
734
735         return 0;
736 }
737
738 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
739         uint16_t queue_id, int enable)
740 {
741         if (enable) {
742                 RTE_LOG(ERR, VHOST_CONFIG,
743                         "guest notification isn't supported.\n");
744                 return -1;
745         }
746
747         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
748         return 0;
749 }
750
751 uint64_t rte_vhost_feature_get(void)
752 {
753         return VHOST_FEATURES;
754 }
755
756 int rte_vhost_feature_disable(uint64_t feature_mask)
757 {
758         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
759         return 0;
760 }
761
762 int rte_vhost_feature_enable(uint64_t feature_mask)
763 {
764         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
765                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
766                 return 0;
767         }
768         return -1;
769 }
770
771 /*
772  * Register ops so that we can add/remove device to data core.
773  */
774 int
775 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
776 {
777         notify_ops = ops;
778
779         return 0;
780 }