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