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