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