vhost: introduce last available index for dequeue
[dpdk.git] / lib / librte_vhost / vhost_user.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/mman.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <assert.h>
43 #ifdef RTE_LIBRTE_VHOST_NUMA
44 #include <numaif.h>
45 #endif
46
47 #include <rte_common.h>
48 #include <rte_malloc.h>
49 #include <rte_log.h>
50
51 #include "vhost.h"
52 #include "vhost_user.h"
53
54 static const char *vhost_message_str[VHOST_USER_MAX] = {
55         [VHOST_USER_NONE] = "VHOST_USER_NONE",
56         [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
57         [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
58         [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
59         [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
60         [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
61         [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
62         [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
63         [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
64         [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
65         [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
66         [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
67         [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
68         [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
69         [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
70         [VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
71         [VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
72         [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
73         [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
74         [VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
75 };
76
77 static uint64_t
78 get_blk_size(int fd)
79 {
80         struct stat stat;
81         int ret;
82
83         ret = fstat(fd, &stat);
84         return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
85 }
86
87 static void
88 free_mem_region(struct virtio_net *dev)
89 {
90         uint32_t i;
91         struct virtio_memory_region *reg;
92
93         if (!dev || !dev->mem)
94                 return;
95
96         for (i = 0; i < dev->mem->nregions; i++) {
97                 reg = &dev->mem->regions[i];
98                 if (reg->host_user_addr) {
99                         munmap(reg->mmap_addr, reg->mmap_size);
100                         close(reg->fd);
101                 }
102         }
103 }
104
105 void
106 vhost_backend_cleanup(struct virtio_net *dev)
107 {
108         if (dev->mem) {
109                 free_mem_region(dev);
110                 rte_free(dev->mem);
111                 dev->mem = NULL;
112         }
113         if (dev->log_addr) {
114                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
115                 dev->log_addr = 0;
116         }
117 }
118
119 /*
120  * This function just returns success at the moment unless
121  * the device hasn't been initialised.
122  */
123 static int
124 vhost_user_set_owner(void)
125 {
126         return 0;
127 }
128
129 static int
130 vhost_user_reset_owner(struct virtio_net *dev)
131 {
132         if (dev->flags & VIRTIO_DEV_RUNNING) {
133                 dev->flags &= ~VIRTIO_DEV_RUNNING;
134                 notify_ops->destroy_device(dev->vid);
135         }
136
137         cleanup_device(dev, 0);
138         reset_device(dev);
139         return 0;
140 }
141
142 /*
143  * The features that we support are requested.
144  */
145 static uint64_t
146 vhost_user_get_features(void)
147 {
148         return VHOST_FEATURES;
149 }
150
151 /*
152  * We receive the negotiated features supported by us and the virtio device.
153  */
154 static int
155 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
156 {
157         if (features & ~VHOST_FEATURES)
158                 return -1;
159
160         dev->features = features;
161         if (dev->features &
162                 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
163                 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
164         } else {
165                 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
166         }
167         LOG_DEBUG(VHOST_CONFIG,
168                 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
169                 dev->vid,
170                 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
171                 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
172
173         return 0;
174 }
175
176 /*
177  * The virtio device sends us the size of the descriptor ring.
178  */
179 static int
180 vhost_user_set_vring_num(struct virtio_net *dev,
181                          struct vhost_vring_state *state)
182 {
183         dev->virtqueue[state->index]->size = state->num;
184
185         return 0;
186 }
187
188 /*
189  * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
190  * same numa node as the memory of vring descriptor.
191  */
192 #ifdef RTE_LIBRTE_VHOST_NUMA
193 static struct virtio_net*
194 numa_realloc(struct virtio_net *dev, int index)
195 {
196         int oldnode, newnode;
197         struct virtio_net *old_dev;
198         struct vhost_virtqueue *old_vq, *vq;
199         int ret;
200
201         /*
202          * vq is allocated on pairs, we should try to do realloc
203          * on first queue of one queue pair only.
204          */
205         if (index % VIRTIO_QNUM != 0)
206                 return dev;
207
208         old_dev = dev;
209         vq = old_vq = dev->virtqueue[index];
210
211         ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
212                             MPOL_F_NODE | MPOL_F_ADDR);
213
214         /* check if we need to reallocate vq */
215         ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
216                              MPOL_F_NODE | MPOL_F_ADDR);
217         if (ret) {
218                 RTE_LOG(ERR, VHOST_CONFIG,
219                         "Unable to get vq numa information.\n");
220                 return dev;
221         }
222         if (oldnode != newnode) {
223                 RTE_LOG(INFO, VHOST_CONFIG,
224                         "reallocate vq from %d to %d node\n", oldnode, newnode);
225                 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
226                                        newnode);
227                 if (!vq)
228                         return dev;
229
230                 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
231                 rte_free(old_vq);
232         }
233
234         /* check if we need to reallocate dev */
235         ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
236                             MPOL_F_NODE | MPOL_F_ADDR);
237         if (ret) {
238                 RTE_LOG(ERR, VHOST_CONFIG,
239                         "Unable to get dev numa information.\n");
240                 goto out;
241         }
242         if (oldnode != newnode) {
243                 RTE_LOG(INFO, VHOST_CONFIG,
244                         "reallocate dev from %d to %d node\n",
245                         oldnode, newnode);
246                 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
247                 if (!dev) {
248                         dev = old_dev;
249                         goto out;
250                 }
251
252                 memcpy(dev, old_dev, sizeof(*dev));
253                 rte_free(old_dev);
254         }
255
256 out:
257         dev->virtqueue[index] = vq;
258         dev->virtqueue[index + 1] = vq + 1;
259         vhost_devices[dev->vid] = dev;
260
261         return dev;
262 }
263 #else
264 static struct virtio_net*
265 numa_realloc(struct virtio_net *dev, int index __rte_unused)
266 {
267         return dev;
268 }
269 #endif
270
271 /*
272  * Converts QEMU virtual address to Vhost virtual address. This function is
273  * used to convert the ring addresses to our address space.
274  */
275 static uint64_t
276 qva_to_vva(struct virtio_net *dev, uint64_t qva)
277 {
278         struct virtio_memory_region *reg;
279         uint32_t i;
280
281         /* Find the region where the address lives. */
282         for (i = 0; i < dev->mem->nregions; i++) {
283                 reg = &dev->mem->regions[i];
284
285                 if (qva >= reg->guest_user_addr &&
286                     qva <  reg->guest_user_addr + reg->size) {
287                         return qva - reg->guest_user_addr +
288                                reg->host_user_addr;
289                 }
290         }
291
292         return 0;
293 }
294
295 /*
296  * The virtio device sends us the desc, used and avail ring addresses.
297  * This function then converts these to our address space.
298  */
299 static int
300 vhost_user_set_vring_addr(struct virtio_net *dev, struct vhost_vring_addr *addr)
301 {
302         struct vhost_virtqueue *vq;
303
304         if (dev->mem == NULL)
305                 return -1;
306
307         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
308         vq = dev->virtqueue[addr->index];
309
310         /* The addresses are converted from QEMU virtual to Vhost virtual. */
311         vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
312                         addr->desc_user_addr);
313         if (vq->desc == 0) {
314                 RTE_LOG(ERR, VHOST_CONFIG,
315                         "(%d) failed to find desc ring address.\n",
316                         dev->vid);
317                 return -1;
318         }
319
320         dev = numa_realloc(dev, addr->index);
321         vq = dev->virtqueue[addr->index];
322
323         vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
324                         addr->avail_user_addr);
325         if (vq->avail == 0) {
326                 RTE_LOG(ERR, VHOST_CONFIG,
327                         "(%d) failed to find avail ring address.\n",
328                         dev->vid);
329                 return -1;
330         }
331
332         vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
333                         addr->used_user_addr);
334         if (vq->used == 0) {
335                 RTE_LOG(ERR, VHOST_CONFIG,
336                         "(%d) failed to find used ring address.\n",
337                         dev->vid);
338                 return -1;
339         }
340
341         if (vq->last_used_idx != vq->used->idx) {
342                 RTE_LOG(WARNING, VHOST_CONFIG,
343                         "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
344                         "some packets maybe resent for Tx and dropped for Rx\n",
345                         vq->last_used_idx, vq->used->idx);
346                 vq->last_used_idx  = vq->used->idx;
347                 vq->last_avail_idx = vq->used->idx;
348         }
349
350         vq->log_guest_addr = addr->log_guest_addr;
351
352         LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
353                         dev->vid, vq->desc);
354         LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
355                         dev->vid, vq->avail);
356         LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
357                         dev->vid, vq->used);
358         LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
359                         dev->vid, vq->log_guest_addr);
360
361         return 0;
362 }
363
364 /*
365  * The virtio device sends us the available ring last used index.
366  */
367 static int
368 vhost_user_set_vring_base(struct virtio_net *dev,
369                           struct vhost_vring_state *state)
370 {
371         dev->virtqueue[state->index]->last_used_idx  = state->num;
372         dev->virtqueue[state->index]->last_avail_idx = state->num;
373
374         return 0;
375 }
376
377 static void
378 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
379                    uint64_t host_phys_addr, uint64_t size)
380 {
381         struct guest_page *page, *last_page;
382
383         if (dev->nr_guest_pages == dev->max_guest_pages) {
384                 dev->max_guest_pages *= 2;
385                 dev->guest_pages = realloc(dev->guest_pages,
386                                         dev->max_guest_pages * sizeof(*page));
387         }
388
389         if (dev->nr_guest_pages > 0) {
390                 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
391                 /* merge if the two pages are continuous */
392                 if (host_phys_addr == last_page->host_phys_addr +
393                                       last_page->size) {
394                         last_page->size += size;
395                         return;
396                 }
397         }
398
399         page = &dev->guest_pages[dev->nr_guest_pages++];
400         page->guest_phys_addr = guest_phys_addr;
401         page->host_phys_addr  = host_phys_addr;
402         page->size = size;
403 }
404
405 static void
406 add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
407                 uint64_t page_size)
408 {
409         uint64_t reg_size = reg->size;
410         uint64_t host_user_addr  = reg->host_user_addr;
411         uint64_t guest_phys_addr = reg->guest_phys_addr;
412         uint64_t host_phys_addr;
413         uint64_t size;
414
415         host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
416         size = page_size - (guest_phys_addr & (page_size - 1));
417         size = RTE_MIN(size, reg_size);
418
419         add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
420         host_user_addr  += size;
421         guest_phys_addr += size;
422         reg_size -= size;
423
424         while (reg_size > 0) {
425                 host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
426                                                   host_user_addr);
427                 add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
428                                    page_size);
429
430                 host_user_addr  += page_size;
431                 guest_phys_addr += page_size;
432                 reg_size -= page_size;
433         }
434 }
435
436 #ifdef RTE_LIBRTE_VHOST_DEBUG
437 /* TODO: enable it only in debug mode? */
438 static void
439 dump_guest_pages(struct virtio_net *dev)
440 {
441         uint32_t i;
442         struct guest_page *page;
443
444         for (i = 0; i < dev->nr_guest_pages; i++) {
445                 page = &dev->guest_pages[i];
446
447                 RTE_LOG(INFO, VHOST_CONFIG,
448                         "guest physical page region %u\n"
449                         "\t guest_phys_addr: %" PRIx64 "\n"
450                         "\t host_phys_addr : %" PRIx64 "\n"
451                         "\t size           : %" PRIx64 "\n",
452                         i,
453                         page->guest_phys_addr,
454                         page->host_phys_addr,
455                         page->size);
456         }
457 }
458 #else
459 #define dump_guest_pages(dev)
460 #endif
461
462 static int
463 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
464 {
465         struct VhostUserMemory memory = pmsg->payload.memory;
466         struct virtio_memory_region *reg;
467         void *mmap_addr;
468         uint64_t mmap_size;
469         uint64_t mmap_offset;
470         uint64_t alignment;
471         uint32_t i;
472         int fd;
473
474         /* Remove from the data plane. */
475         if (dev->flags & VIRTIO_DEV_RUNNING) {
476                 dev->flags &= ~VIRTIO_DEV_RUNNING;
477                 notify_ops->destroy_device(dev->vid);
478         }
479
480         if (dev->mem) {
481                 free_mem_region(dev);
482                 rte_free(dev->mem);
483                 dev->mem = NULL;
484         }
485
486         dev->nr_guest_pages = 0;
487         if (!dev->guest_pages) {
488                 dev->max_guest_pages = 8;
489                 dev->guest_pages = malloc(dev->max_guest_pages *
490                                                 sizeof(struct guest_page));
491         }
492
493         dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct virtio_memory) +
494                 sizeof(struct virtio_memory_region) * memory.nregions, 0);
495         if (dev->mem == NULL) {
496                 RTE_LOG(ERR, VHOST_CONFIG,
497                         "(%d) failed to allocate memory for dev->mem\n",
498                         dev->vid);
499                 return -1;
500         }
501         dev->mem->nregions = memory.nregions;
502
503         for (i = 0; i < memory.nregions; i++) {
504                 fd  = pmsg->fds[i];
505                 reg = &dev->mem->regions[i];
506
507                 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
508                 reg->guest_user_addr = memory.regions[i].userspace_addr;
509                 reg->size            = memory.regions[i].memory_size;
510                 reg->fd              = fd;
511
512                 mmap_offset = memory.regions[i].mmap_offset;
513                 mmap_size   = reg->size + mmap_offset;
514
515                 /* mmap() without flag of MAP_ANONYMOUS, should be called
516                  * with length argument aligned with hugepagesz at older
517                  * longterm version Linux, like 2.6.32 and 3.2.72, or
518                  * mmap() will fail with EINVAL.
519                  *
520                  * to avoid failure, make sure in caller to keep length
521                  * aligned.
522                  */
523                 alignment = get_blk_size(fd);
524                 if (alignment == (uint64_t)-1) {
525                         RTE_LOG(ERR, VHOST_CONFIG,
526                                 "couldn't get hugepage size through fstat\n");
527                         goto err_mmap;
528                 }
529                 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
530
531                 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
532                                  MAP_SHARED | MAP_POPULATE, fd, 0);
533
534                 if (mmap_addr == MAP_FAILED) {
535                         RTE_LOG(ERR, VHOST_CONFIG,
536                                 "mmap region %u failed.\n", i);
537                         goto err_mmap;
538                 }
539
540                 reg->mmap_addr = mmap_addr;
541                 reg->mmap_size = mmap_size;
542                 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
543                                       mmap_offset;
544
545                 add_guest_pages(dev, reg, alignment);
546
547                 RTE_LOG(INFO, VHOST_CONFIG,
548                         "guest memory region %u, size: 0x%" PRIx64 "\n"
549                         "\t guest physical addr: 0x%" PRIx64 "\n"
550                         "\t guest virtual  addr: 0x%" PRIx64 "\n"
551                         "\t host  virtual  addr: 0x%" PRIx64 "\n"
552                         "\t mmap addr : 0x%" PRIx64 "\n"
553                         "\t mmap size : 0x%" PRIx64 "\n"
554                         "\t mmap align: 0x%" PRIx64 "\n"
555                         "\t mmap off  : 0x%" PRIx64 "\n",
556                         i, reg->size,
557                         reg->guest_phys_addr,
558                         reg->guest_user_addr,
559                         reg->host_user_addr,
560                         (uint64_t)(uintptr_t)mmap_addr,
561                         mmap_size,
562                         alignment,
563                         mmap_offset);
564         }
565
566         dump_guest_pages(dev);
567
568         return 0;
569
570 err_mmap:
571         free_mem_region(dev);
572         rte_free(dev->mem);
573         dev->mem = NULL;
574         return -1;
575 }
576
577 static int
578 vq_is_ready(struct vhost_virtqueue *vq)
579 {
580         return vq && vq->desc   &&
581                vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
582                vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
583 }
584
585 static int
586 virtio_is_ready(struct virtio_net *dev)
587 {
588         struct vhost_virtqueue *rvq, *tvq;
589         uint32_t i;
590
591         for (i = 0; i < dev->virt_qp_nb; i++) {
592                 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
593                 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
594
595                 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
596                         RTE_LOG(INFO, VHOST_CONFIG,
597                                 "virtio is not ready for processing.\n");
598                         return 0;
599                 }
600         }
601
602         RTE_LOG(INFO, VHOST_CONFIG,
603                 "virtio is now ready for processing.\n");
604         return 1;
605 }
606
607 static void
608 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
609 {
610         struct vhost_vring_file file;
611         struct vhost_virtqueue *vq;
612         uint32_t cur_qp_idx;
613
614         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
615         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
616                 file.fd = VIRTIO_INVALID_EVENTFD;
617         else
618                 file.fd = pmsg->fds[0];
619         RTE_LOG(INFO, VHOST_CONFIG,
620                 "vring call idx:%d file:%d\n", file.index, file.fd);
621
622         /*
623          * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
624          * we get, so we do vring queue pair allocation here.
625          */
626         cur_qp_idx = file.index / VIRTIO_QNUM;
627         if (cur_qp_idx + 1 > dev->virt_qp_nb) {
628                 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
629                         return;
630         }
631
632         vq = dev->virtqueue[file.index];
633         assert(vq != NULL);
634
635         if (vq->callfd >= 0)
636                 close(vq->callfd);
637
638         vq->callfd = file.fd;
639 }
640
641 /*
642  *  In vhost-user, when we receive kick message, will test whether virtio
643  *  device is ready for packet processing.
644  */
645 static void
646 vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
647 {
648         struct vhost_vring_file file;
649         struct vhost_virtqueue *vq;
650
651         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
652         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
653                 file.fd = VIRTIO_INVALID_EVENTFD;
654         else
655                 file.fd = pmsg->fds[0];
656         RTE_LOG(INFO, VHOST_CONFIG,
657                 "vring kick idx:%d file:%d\n", file.index, file.fd);
658
659         vq = dev->virtqueue[file.index];
660         if (vq->kickfd >= 0)
661                 close(vq->kickfd);
662         vq->kickfd = file.fd;
663
664         if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
665                 if (notify_ops->new_device(dev->vid) == 0)
666                         dev->flags |= VIRTIO_DEV_RUNNING;
667         }
668 }
669
670 /*
671  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
672  */
673 static int
674 vhost_user_get_vring_base(struct virtio_net *dev,
675                           struct vhost_vring_state *state)
676 {
677         /* We have to stop the queue (virtio) if it is running. */
678         if (dev->flags & VIRTIO_DEV_RUNNING) {
679                 dev->flags &= ~VIRTIO_DEV_RUNNING;
680                 notify_ops->destroy_device(dev->vid);
681         }
682
683         /* Here we are safe to get the last used index */
684         state->num = dev->virtqueue[state->index]->last_used_idx;
685
686         RTE_LOG(INFO, VHOST_CONFIG,
687                 "vring base idx:%d file:%d\n", state->index, state->num);
688         /*
689          * Based on current qemu vhost-user implementation, this message is
690          * sent and only sent in vhost_vring_stop.
691          * TODO: cleanup the vring, it isn't usable since here.
692          */
693         if (dev->virtqueue[state->index]->kickfd >= 0)
694                 close(dev->virtqueue[state->index]->kickfd);
695
696         dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
697
698         return 0;
699 }
700
701 /*
702  * when virtio queues are ready to work, qemu will send us to
703  * enable the virtio queue pair.
704  */
705 static int
706 vhost_user_set_vring_enable(struct virtio_net *dev,
707                             struct vhost_vring_state *state)
708 {
709         int enable = (int)state->num;
710
711         RTE_LOG(INFO, VHOST_CONFIG,
712                 "set queue enable: %d to qp idx: %d\n",
713                 enable, state->index);
714
715         if (notify_ops->vring_state_changed)
716                 notify_ops->vring_state_changed(dev->vid, state->index, enable);
717
718         dev->virtqueue[state->index]->enabled = enable;
719
720         return 0;
721 }
722
723 static void
724 vhost_user_set_protocol_features(struct virtio_net *dev,
725                                  uint64_t protocol_features)
726 {
727         if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
728                 return;
729
730         dev->protocol_features = protocol_features;
731 }
732
733 static int
734 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
735 {
736         int fd = msg->fds[0];
737         uint64_t size, off;
738         void *addr;
739
740         if (fd < 0) {
741                 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
742                 return -1;
743         }
744
745         if (msg->size != sizeof(VhostUserLog)) {
746                 RTE_LOG(ERR, VHOST_CONFIG,
747                         "invalid log base msg size: %"PRId32" != %d\n",
748                         msg->size, (int)sizeof(VhostUserLog));
749                 return -1;
750         }
751
752         size = msg->payload.log.mmap_size;
753         off  = msg->payload.log.mmap_offset;
754         RTE_LOG(INFO, VHOST_CONFIG,
755                 "log mmap size: %"PRId64", offset: %"PRId64"\n",
756                 size, off);
757
758         /*
759          * mmap from 0 to workaround a hugepage mmap bug: mmap will
760          * fail when offset is not page size aligned.
761          */
762         addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
763         close(fd);
764         if (addr == MAP_FAILED) {
765                 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
766                 return -1;
767         }
768
769         /*
770          * Free previously mapped log memory on occasionally
771          * multiple VHOST_USER_SET_LOG_BASE.
772          */
773         if (dev->log_addr) {
774                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
775         }
776         dev->log_addr = (uint64_t)(uintptr_t)addr;
777         dev->log_base = dev->log_addr + off;
778         dev->log_size = size;
779
780         return 0;
781 }
782
783 /*
784  * An rarp packet is constructed and broadcasted to notify switches about
785  * the new location of the migrated VM, so that packets from outside will
786  * not be lost after migration.
787  *
788  * However, we don't actually "send" a rarp packet here, instead, we set
789  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
790  */
791 static int
792 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
793 {
794         uint8_t *mac = (uint8_t *)&msg->payload.u64;
795
796         RTE_LOG(DEBUG, VHOST_CONFIG,
797                 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
798                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
799         memcpy(dev->mac.addr_bytes, mac, 6);
800
801         /*
802          * Set the flag to inject a RARP broadcast packet at
803          * rte_vhost_dequeue_burst().
804          *
805          * rte_smp_wmb() is for making sure the mac is copied
806          * before the flag is set.
807          */
808         rte_smp_wmb();
809         rte_atomic16_set(&dev->broadcast_rarp, 1);
810
811         return 0;
812 }
813
814 /* return bytes# of read on success or negative val on failure. */
815 static int
816 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
817 {
818         int ret;
819
820         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
821                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
822         if (ret <= 0)
823                 return ret;
824
825         if (msg && msg->size) {
826                 if (msg->size > sizeof(msg->payload)) {
827                         RTE_LOG(ERR, VHOST_CONFIG,
828                                 "invalid msg size: %d\n", msg->size);
829                         return -1;
830                 }
831                 ret = read(sockfd, &msg->payload, msg->size);
832                 if (ret <= 0)
833                         return ret;
834                 if (ret != (int)msg->size) {
835                         RTE_LOG(ERR, VHOST_CONFIG,
836                                 "read control message failed\n");
837                         return -1;
838                 }
839         }
840
841         return ret;
842 }
843
844 static int
845 send_vhost_message(int sockfd, struct VhostUserMsg *msg)
846 {
847         int ret;
848
849         if (!msg)
850                 return 0;
851
852         msg->flags &= ~VHOST_USER_VERSION_MASK;
853         msg->flags |= VHOST_USER_VERSION;
854         msg->flags |= VHOST_USER_REPLY_MASK;
855
856         ret = send_fd_message(sockfd, (char *)msg,
857                 VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
858
859         return ret;
860 }
861
862 int
863 vhost_user_msg_handler(int vid, int fd)
864 {
865         struct virtio_net *dev;
866         struct VhostUserMsg msg;
867         int ret;
868
869         dev = get_device(vid);
870         if (dev == NULL)
871                 return -1;
872
873         ret = read_vhost_message(fd, &msg);
874         if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
875                 if (ret < 0)
876                         RTE_LOG(ERR, VHOST_CONFIG,
877                                 "vhost read message failed\n");
878                 else if (ret == 0)
879                         RTE_LOG(INFO, VHOST_CONFIG,
880                                 "vhost peer closed\n");
881                 else
882                         RTE_LOG(ERR, VHOST_CONFIG,
883                                 "vhost read incorrect message\n");
884
885                 return -1;
886         }
887
888         RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
889                 vhost_message_str[msg.request]);
890         switch (msg.request) {
891         case VHOST_USER_GET_FEATURES:
892                 msg.payload.u64 = vhost_user_get_features();
893                 msg.size = sizeof(msg.payload.u64);
894                 send_vhost_message(fd, &msg);
895                 break;
896         case VHOST_USER_SET_FEATURES:
897                 vhost_user_set_features(dev, msg.payload.u64);
898                 break;
899
900         case VHOST_USER_GET_PROTOCOL_FEATURES:
901                 msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
902                 msg.size = sizeof(msg.payload.u64);
903                 send_vhost_message(fd, &msg);
904                 break;
905         case VHOST_USER_SET_PROTOCOL_FEATURES:
906                 vhost_user_set_protocol_features(dev, msg.payload.u64);
907                 break;
908
909         case VHOST_USER_SET_OWNER:
910                 vhost_user_set_owner();
911                 break;
912         case VHOST_USER_RESET_OWNER:
913                 vhost_user_reset_owner(dev);
914                 break;
915
916         case VHOST_USER_SET_MEM_TABLE:
917                 vhost_user_set_mem_table(dev, &msg);
918                 break;
919
920         case VHOST_USER_SET_LOG_BASE:
921                 vhost_user_set_log_base(dev, &msg);
922
923                 /* it needs a reply */
924                 msg.size = sizeof(msg.payload.u64);
925                 send_vhost_message(fd, &msg);
926                 break;
927         case VHOST_USER_SET_LOG_FD:
928                 close(msg.fds[0]);
929                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
930                 break;
931
932         case VHOST_USER_SET_VRING_NUM:
933                 vhost_user_set_vring_num(dev, &msg.payload.state);
934                 break;
935         case VHOST_USER_SET_VRING_ADDR:
936                 vhost_user_set_vring_addr(dev, &msg.payload.addr);
937                 break;
938         case VHOST_USER_SET_VRING_BASE:
939                 vhost_user_set_vring_base(dev, &msg.payload.state);
940                 break;
941
942         case VHOST_USER_GET_VRING_BASE:
943                 ret = vhost_user_get_vring_base(dev, &msg.payload.state);
944                 msg.size = sizeof(msg.payload.state);
945                 send_vhost_message(fd, &msg);
946                 break;
947
948         case VHOST_USER_SET_VRING_KICK:
949                 vhost_user_set_vring_kick(dev, &msg);
950                 break;
951         case VHOST_USER_SET_VRING_CALL:
952                 vhost_user_set_vring_call(dev, &msg);
953                 break;
954
955         case VHOST_USER_SET_VRING_ERR:
956                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
957                         close(msg.fds[0]);
958                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
959                 break;
960
961         case VHOST_USER_GET_QUEUE_NUM:
962                 msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
963                 msg.size = sizeof(msg.payload.u64);
964                 send_vhost_message(fd, &msg);
965                 break;
966
967         case VHOST_USER_SET_VRING_ENABLE:
968                 vhost_user_set_vring_enable(dev, &msg.payload.state);
969                 break;
970         case VHOST_USER_SEND_RARP:
971                 vhost_user_send_rarp(dev, &msg);
972                 break;
973
974         default:
975                 break;
976
977         }
978
979         return 0;
980 }