vhost: set/reset device flags internally
[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 <string.h>
38 #include <unistd.h>
39 #include <sys/mman.h>
40 #include <sys/types.h>
41 #include <sys/stat.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
78         if (!dev || !dev->mem)
79                 return;
80
81         region = orig_region(dev->mem, dev->mem->nregions);
82         for (idx = 0; idx < dev->mem->nregions; idx++) {
83                 if (region[idx].mapped_address) {
84                         munmap((void *)(uintptr_t)region[idx].mapped_address,
85                                         region[idx].mapped_size);
86                         close(region[idx].fd);
87                 }
88         }
89 }
90
91 void
92 vhost_backend_cleanup(struct virtio_net *dev)
93 {
94         if (dev->mem) {
95                 free_mem_region(dev);
96                 free(dev->mem);
97                 dev->mem = NULL;
98         }
99 }
100
101 int
102 user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
103 {
104         struct VhostUserMemory memory = pmsg->payload.memory;
105         struct virtio_memory_regions *pregion;
106         uint64_t mapped_address, mapped_size;
107         struct virtio_net *dev;
108         unsigned int idx = 0;
109         struct orig_region_map *pregion_orig;
110         uint64_t alignment;
111
112         /* unmap old memory regions one by one*/
113         dev = get_device(ctx);
114         if (dev == NULL)
115                 return -1;
116
117         /* Remove from the data plane. */
118         if (dev->flags & VIRTIO_DEV_RUNNING) {
119                 dev->flags &= ~VIRTIO_DEV_RUNNING;
120                 notify_ops->destroy_device(dev);
121         }
122
123         if (dev->mem) {
124                 free_mem_region(dev);
125                 free(dev->mem);
126                 dev->mem = NULL;
127         }
128
129         dev->mem = calloc(1,
130                 sizeof(struct virtio_memory) +
131                 sizeof(struct virtio_memory_regions) * memory.nregions +
132                 sizeof(struct orig_region_map) * memory.nregions);
133         if (dev->mem == NULL) {
134                 RTE_LOG(ERR, VHOST_CONFIG,
135                         "(%"PRIu64") Failed to allocate memory for dev->mem\n",
136                         dev->device_fh);
137                 return -1;
138         }
139         dev->mem->nregions = memory.nregions;
140
141         pregion_orig = orig_region(dev->mem, memory.nregions);
142         for (idx = 0; idx < memory.nregions; idx++) {
143                 pregion = &dev->mem->regions[idx];
144                 pregion->guest_phys_address =
145                         memory.regions[idx].guest_phys_addr;
146                 pregion->guest_phys_address_end =
147                         memory.regions[idx].guest_phys_addr +
148                         memory.regions[idx].memory_size;
149                 pregion->memory_size =
150                         memory.regions[idx].memory_size;
151                 pregion->userspace_address =
152                         memory.regions[idx].userspace_addr;
153
154                 /* This is ugly */
155                 mapped_size = memory.regions[idx].memory_size +
156                         memory.regions[idx].mmap_offset;
157
158                 /* mmap() without flag of MAP_ANONYMOUS, should be called
159                  * with length argument aligned with hugepagesz at older
160                  * longterm version Linux, like 2.6.32 and 3.2.72, or
161                  * mmap() will fail with EINVAL.
162                  *
163                  * to avoid failure, make sure in caller to keep length
164                  * aligned.
165                  */
166                 alignment = get_blk_size(pmsg->fds[idx]);
167                 mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
168
169                 mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
170                         mapped_size,
171                         PROT_READ | PROT_WRITE, MAP_SHARED,
172                         pmsg->fds[idx],
173                         0);
174
175                 RTE_LOG(INFO, VHOST_CONFIG,
176                         "mapped region %d fd:%d to:%p sz:0x%"PRIx64" "
177                         "off:0x%"PRIx64" align:0x%"PRIx64"\n",
178                         idx, pmsg->fds[idx], (void *)(uintptr_t)mapped_address,
179                         mapped_size, memory.regions[idx].mmap_offset,
180                         alignment);
181
182                 if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
183                         RTE_LOG(ERR, VHOST_CONFIG,
184                                 "mmap qemu guest failed.\n");
185                         goto err_mmap;
186                 }
187
188                 pregion_orig[idx].mapped_address = mapped_address;
189                 pregion_orig[idx].mapped_size = mapped_size;
190                 pregion_orig[idx].blksz = alignment;
191                 pregion_orig[idx].fd = pmsg->fds[idx];
192
193                 mapped_address +=  memory.regions[idx].mmap_offset;
194
195                 pregion->address_offset = mapped_address -
196                         pregion->guest_phys_address;
197
198                 if (memory.regions[idx].guest_phys_addr == 0) {
199                         dev->mem->base_address =
200                                 memory.regions[idx].userspace_addr;
201                         dev->mem->mapped_address =
202                                 pregion->address_offset;
203                 }
204
205                 LOG_DEBUG(VHOST_CONFIG,
206                         "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
207                         idx,
208                         (void *)(uintptr_t)pregion->guest_phys_address,
209                         (void *)(uintptr_t)pregion->userspace_address,
210                          pregion->memory_size);
211         }
212
213         return 0;
214
215 err_mmap:
216         while (idx--) {
217                 munmap((void *)(uintptr_t)pregion_orig[idx].mapped_address,
218                                 pregion_orig[idx].mapped_size);
219                 close(pregion_orig[idx].fd);
220         }
221         free(dev->mem);
222         dev->mem = NULL;
223         return -1;
224 }
225
226 static int
227 vq_is_ready(struct vhost_virtqueue *vq)
228 {
229         return vq && vq->desc   &&
230                vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
231                vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
232 }
233
234 static int
235 virtio_is_ready(struct virtio_net *dev)
236 {
237         struct vhost_virtqueue *rvq, *tvq;
238         uint32_t i;
239
240         for (i = 0; i < dev->virt_qp_nb; i++) {
241                 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
242                 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
243
244                 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
245                         RTE_LOG(INFO, VHOST_CONFIG,
246                                 "virtio is not ready for processing.\n");
247                         return 0;
248                 }
249         }
250
251         RTE_LOG(INFO, VHOST_CONFIG,
252                 "virtio is now ready for processing.\n");
253         return 1;
254 }
255
256 void
257 user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
258 {
259         struct vhost_vring_file file;
260
261         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
262         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
263                 file.fd = VIRTIO_INVALID_EVENTFD;
264         else
265                 file.fd = pmsg->fds[0];
266         RTE_LOG(INFO, VHOST_CONFIG,
267                 "vring call idx:%d file:%d\n", file.index, file.fd);
268         vhost_set_vring_call(ctx, &file);
269 }
270
271
272 /*
273  *  In vhost-user, when we receive kick message, will test whether virtio
274  *  device is ready for packet processing.
275  */
276 void
277 user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
278 {
279         struct vhost_vring_file file;
280         struct virtio_net *dev = get_device(ctx);
281
282         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
283         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
284                 file.fd = VIRTIO_INVALID_EVENTFD;
285         else
286                 file.fd = pmsg->fds[0];
287         RTE_LOG(INFO, VHOST_CONFIG,
288                 "vring kick idx:%d file:%d\n", file.index, file.fd);
289         vhost_set_vring_kick(ctx, &file);
290
291         if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
292                 if (notify_ops->new_device(dev) == 0)
293                         dev->flags |= VIRTIO_DEV_RUNNING;
294         }
295 }
296
297 /*
298  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
299  */
300 int
301 user_get_vring_base(struct vhost_device_ctx ctx,
302         struct vhost_vring_state *state)
303 {
304         struct virtio_net *dev = get_device(ctx);
305
306         if (dev == NULL)
307                 return -1;
308         /* We have to stop the queue (virtio) if it is running. */
309         if (dev->flags & VIRTIO_DEV_RUNNING)
310                 notify_ops->destroy_device(dev);
311
312         /* Here we are safe to get the last used index */
313         vhost_get_vring_base(ctx, state->index, state);
314
315         RTE_LOG(INFO, VHOST_CONFIG,
316                 "vring base idx:%d file:%d\n", state->index, state->num);
317         /*
318          * Based on current qemu vhost-user implementation, this message is
319          * sent and only sent in vhost_vring_stop.
320          * TODO: cleanup the vring, it isn't usable since here.
321          */
322         if (dev->virtqueue[state->index]->kickfd >= 0)
323                 close(dev->virtqueue[state->index]->kickfd);
324
325         dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
326
327         return 0;
328 }
329
330 /*
331  * when virtio queues are ready to work, qemu will send us to
332  * enable the virtio queue pair.
333  */
334 int
335 user_set_vring_enable(struct vhost_device_ctx ctx,
336                       struct vhost_vring_state *state)
337 {
338         struct virtio_net *dev = get_device(ctx);
339         int enable = (int)state->num;
340
341         RTE_LOG(INFO, VHOST_CONFIG,
342                 "set queue enable: %d to qp idx: %d\n",
343                 enable, state->index);
344
345         if (notify_ops->vring_state_changed) {
346                 notify_ops->vring_state_changed(dev, state->index, enable);
347         }
348
349         dev->virtqueue[state->index]->enabled = enable;
350
351         return 0;
352 }
353
354 void
355 user_set_protocol_features(struct vhost_device_ctx ctx,
356                            uint64_t protocol_features)
357 {
358         struct virtio_net *dev;
359
360         dev = get_device(ctx);
361         if (dev == NULL || protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
362                 return;
363
364         dev->protocol_features = protocol_features;
365 }
366
367 int
368 user_set_log_base(struct vhost_device_ctx ctx,
369                  struct VhostUserMsg *msg)
370 {
371         struct virtio_net *dev;
372         int fd = msg->fds[0];
373         uint64_t size, off;
374         void *addr;
375
376         dev = get_device(ctx);
377         if (!dev)
378                 return -1;
379
380         if (fd < 0) {
381                 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
382                 return -1;
383         }
384
385         if (msg->size != sizeof(VhostUserLog)) {
386                 RTE_LOG(ERR, VHOST_CONFIG,
387                         "invalid log base msg size: %"PRId32" != %d\n",
388                         msg->size, (int)sizeof(VhostUserLog));
389                 return -1;
390         }
391
392         size = msg->payload.log.mmap_size;
393         off  = msg->payload.log.mmap_offset;
394         RTE_LOG(INFO, VHOST_CONFIG,
395                 "log mmap size: %"PRId64", offset: %"PRId64"\n",
396                 size, off);
397
398         /*
399          * mmap from 0 to workaround a hugepage mmap bug: mmap will
400          * fail when offset is not page size aligned.
401          */
402         addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
403         if (addr == MAP_FAILED) {
404                 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
405                 return -1;
406         }
407
408         /* TODO: unmap on stop */
409         dev->log_base = (uint64_t)(uintptr_t)addr + off;
410         dev->log_size = size;
411
412         return 0;
413 }
414
415 /*
416  * An rarp packet is constructed and broadcasted to notify switches about
417  * the new location of the migrated VM, so that packets from outside will
418  * not be lost after migration.
419  *
420  * However, we don't actually "send" a rarp packet here, instead, we set
421  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
422  */
423 int
424 user_send_rarp(struct vhost_device_ctx ctx, struct VhostUserMsg *msg)
425 {
426         struct virtio_net *dev;
427         uint8_t *mac = (uint8_t *)&msg->payload.u64;
428
429         dev = get_device(ctx);
430         if (!dev)
431                 return -1;
432
433         RTE_LOG(DEBUG, VHOST_CONFIG,
434                 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
435                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
436         memcpy(dev->mac.addr_bytes, mac, 6);
437
438         /*
439          * Set the flag to inject a RARP broadcast packet at
440          * rte_vhost_dequeue_burst().
441          *
442          * rte_smp_wmb() is for making sure the mac is copied
443          * before the flag is set.
444          */
445         rte_smp_wmb();
446         rte_atomic16_set(&dev->broadcast_rarp, 1);
447
448         return 0;
449 }