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