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