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