vhost: support vhost-user
[dpdk.git] / lib / librte_vhost / vhost_user / virtio-net-user.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/mman.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45
46 #include "virtio-net.h"
47 #include "virtio-net-user.h"
48 #include "vhost-net-user.h"
49 #include "vhost-net.h"
50
51 struct orig_region_map {
52         int fd;
53         uint64_t mapped_address;
54         uint64_t mapped_size;
55         uint64_t blksz;
56 };
57
58 #define orig_region(ptr, nregions) \
59         ((struct orig_region_map *)RTE_PTR_ADD((ptr), \
60                 sizeof(struct virtio_memory) + \
61                 sizeof(struct virtio_memory_regions) * (nregions)))
62
63 static uint64_t
64 get_blk_size(int fd)
65 {
66         struct stat stat;
67
68         fstat(fd, &stat);
69         return (uint64_t)stat.st_blksize;
70 }
71
72 static void
73 free_mem_region(struct virtio_net *dev)
74 {
75         struct orig_region_map *region;
76         unsigned int idx;
77         uint64_t alignment;
78
79         if (!dev || !dev->mem)
80                 return;
81
82         region = orig_region(dev->mem, dev->mem->nregions);
83         for (idx = 0; idx < dev->mem->nregions; idx++) {
84                 if (region[idx].mapped_address) {
85                         alignment = region[idx].blksz;
86                         munmap((void *)
87                                 RTE_ALIGN_FLOOR(
88                                         region[idx].mapped_address, alignment),
89                                 RTE_ALIGN_CEIL(
90                                         region[idx].mapped_size, alignment));
91                         close(region[idx].fd);
92                 }
93         }
94 }
95
96 int
97 user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
98 {
99         struct VhostUserMemory memory = pmsg->payload.memory;
100         struct virtio_memory_regions *pregion;
101         uint64_t mapped_address, mapped_size;
102         struct virtio_net *dev;
103         unsigned int idx = 0;
104         struct orig_region_map *pregion_orig;
105         uint64_t alignment;
106
107         /* unmap old memory regions one by one*/
108         dev = get_device(ctx);
109         if (dev == NULL)
110                 return -1;
111
112         if (dev->mem) {
113                 free_mem_region(dev);
114                 free(dev->mem);
115                 dev->mem = NULL;
116         }
117
118         dev->mem = calloc(1,
119                 sizeof(struct virtio_memory) +
120                 sizeof(struct virtio_memory_regions) * memory.nregions +
121                 sizeof(struct orig_region_map) * memory.nregions);
122         if (dev->mem == NULL) {
123                 RTE_LOG(ERR, VHOST_CONFIG,
124                         "(%"PRIu64") Failed to allocate memory for dev->mem\n",
125                         dev->device_fh);
126                 return -1;
127         }
128         dev->mem->nregions = memory.nregions;
129
130         pregion_orig = orig_region(dev->mem, memory.nregions);
131         for (idx = 0; idx < memory.nregions; idx++) {
132                 pregion = &dev->mem->regions[idx];
133                 pregion->guest_phys_address =
134                         memory.regions[idx].guest_phys_addr;
135                 pregion->guest_phys_address_end =
136                         memory.regions[idx].guest_phys_addr +
137                         memory.regions[idx].memory_size;
138                 pregion->memory_size =
139                         memory.regions[idx].memory_size;
140                 pregion->userspace_address =
141                         memory.regions[idx].userspace_addr;
142
143                 /* This is ugly */
144                 mapped_size = memory.regions[idx].memory_size +
145                         memory.regions[idx].mmap_offset;
146                 mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
147                         mapped_size,
148                         PROT_READ | PROT_WRITE, MAP_SHARED,
149                         pmsg->fds[idx],
150                         0);
151
152                 RTE_LOG(INFO, VHOST_CONFIG,
153                         "mapped region %d fd:%d to %p sz:0x%"PRIx64" off:0x%"PRIx64"\n",
154                         idx, pmsg->fds[idx], (void *)mapped_address,
155                         mapped_size, memory.regions[idx].mmap_offset);
156
157                 if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
158                         RTE_LOG(ERR, VHOST_CONFIG,
159                                 "mmap qemu guest failed.\n");
160                         goto err_mmap;
161                 }
162
163                 pregion_orig[idx].mapped_address = mapped_address;
164                 pregion_orig[idx].mapped_size = mapped_size;
165                 pregion_orig[idx].blksz = get_blk_size(pmsg->fds[idx]);
166                 pregion_orig[idx].fd = pmsg->fds[idx];
167
168                 mapped_address +=  memory.regions[idx].mmap_offset;
169
170                 pregion->address_offset = mapped_address -
171                         pregion->guest_phys_address;
172
173                 if (memory.regions[idx].guest_phys_addr == 0) {
174                         dev->mem->base_address =
175                                 memory.regions[idx].userspace_addr;
176                         dev->mem->mapped_address =
177                                 pregion->address_offset;
178                 }
179
180                 LOG_DEBUG(VHOST_CONFIG,
181                         "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
182                         idx,
183                         (void *)(uintptr_t)pregion->guest_phys_address,
184                         (void *)(uintptr_t)pregion->userspace_address,
185                          pregion->memory_size);
186         }
187
188         return 0;
189
190 err_mmap:
191         while (idx--) {
192                 alignment = pregion_orig[idx].blksz;
193                 munmap((void *)RTE_ALIGN_FLOOR(
194                         pregion_orig[idx].mapped_address, alignment),
195                         RTE_ALIGN_CEIL(pregion_orig[idx].mapped_size,
196                                         alignment));
197                 close(pregion_orig[idx].fd);
198         }
199         free(dev->mem);
200         dev->mem = NULL;
201         return -1;
202 }
203
204 static int
205 virtio_is_ready(struct virtio_net *dev)
206 {
207         struct vhost_virtqueue *rvq, *tvq;
208
209         /* mq support in future.*/
210         rvq = dev->virtqueue[VIRTIO_RXQ];
211         tvq = dev->virtqueue[VIRTIO_TXQ];
212         if (rvq && tvq && rvq->desc && tvq->desc &&
213                 (rvq->kickfd != (eventfd_t)-1) &&
214                 (rvq->callfd != (eventfd_t)-1) &&
215                 (tvq->kickfd != (eventfd_t)-1) &&
216                 (tvq->callfd != (eventfd_t)-1)) {
217                 RTE_LOG(INFO, VHOST_CONFIG,
218                         "virtio is now ready for processing.\n");
219                 return 1;
220         }
221         RTE_LOG(INFO, VHOST_CONFIG,
222                 "virtio isn't ready for processing.\n");
223         return 0;
224 }
225
226 void
227 user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
228 {
229         struct vhost_vring_file file;
230
231         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
232         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
233                 file.fd = -1;
234         else
235                 file.fd = pmsg->fds[0];
236         RTE_LOG(INFO, VHOST_CONFIG,
237                 "vring call idx:%d file:%d\n", file.index, file.fd);
238         ops->set_vring_call(ctx, &file);
239 }
240
241
242 /*
243  *  In vhost-user, when we receive kick message, will test whether virtio
244  *  device is ready for packet processing.
245  */
246 void
247 user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
248 {
249         struct vhost_vring_file file;
250         struct virtio_net *dev = get_device(ctx);
251
252         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
253         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
254                 file.fd = -1;
255         else
256                 file.fd = pmsg->fds[0];
257         RTE_LOG(INFO, VHOST_CONFIG,
258                 "vring kick idx:%d file:%d\n", file.index, file.fd);
259         ops->set_vring_kick(ctx, &file);
260
261         if (virtio_is_ready(dev) &&
262                 !(dev->flags & VIRTIO_DEV_RUNNING))
263                         notify_ops->new_device(dev);
264 }
265
266 /*
267  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
268  */
269 int
270 user_get_vring_base(struct vhost_device_ctx ctx,
271         struct vhost_vring_state *state)
272 {
273         struct virtio_net *dev = get_device(ctx);
274
275         /* We have to stop the queue (virtio) if it is running. */
276         if (dev->flags & VIRTIO_DEV_RUNNING)
277                 notify_ops->destroy_device(dev);
278
279         /* Here we are safe to get the last used index */
280         ops->get_vring_base(ctx, state->index, state);
281
282         RTE_LOG(INFO, VHOST_CONFIG,
283                 "vring base idx:%d file:%d\n", state->index, state->num);
284         /*
285          * Based on current qemu vhost-user implementation, this message is
286          * sent and only sent in vhost_vring_stop.
287          * TODO: cleanup the vring, it isn't usable since here.
288          */
289         if (((int)dev->virtqueue[VIRTIO_RXQ]->callfd) >= 0) {
290                 close(dev->virtqueue[VIRTIO_RXQ]->callfd);
291                 dev->virtqueue[VIRTIO_RXQ]->callfd = (eventfd_t)-1;
292         }
293         if (((int)dev->virtqueue[VIRTIO_TXQ]->callfd) >= 0) {
294                 close(dev->virtqueue[VIRTIO_TXQ]->callfd);
295                 dev->virtqueue[VIRTIO_TXQ]->callfd = (eventfd_t)-1;
296         }
297
298         return 0;
299 }
300
301 void
302 user_destroy_device(struct vhost_device_ctx ctx)
303 {
304         struct virtio_net *dev = get_device(ctx);
305
306         if (dev && (dev->flags & VIRTIO_DEV_RUNNING))
307                 notify_ops->destroy_device(dev);
308
309         if (dev && dev->mem) {
310                 free_mem_region(dev);
311                 free(dev->mem);
312                 dev->mem = NULL;
313         }
314 }