Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_vhost / vhost_cuse / virtio-net-cdev.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 <dirent.h>
36 #include <linux/vhost.h>
37 #include <linux/virtio_net.h>
38 #include <fuse/cuse_lowlevel.h>
39 #include <stddef.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <sys/eventfd.h>
43 #include <sys/mman.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <linux/if_tun.h>
49 #include <linux/if.h>
50 #include <errno.h>
51
52 #include <rte_log.h>
53
54 #include "rte_virtio_net.h"
55 #include "vhost-net.h"
56 #include "virtio-net-cdev.h"
57 #include "virtio-net.h"
58 #include "eventfd_copy.h"
59
60 /* Line size for reading maps file. */
61 static const uint32_t BUFSIZE = PATH_MAX;
62
63 /* Size of prot char array in procmap. */
64 #define PROT_SZ 5
65
66 /* Number of elements in procmap struct. */
67 #define PROCMAP_SZ 8
68
69 /* Structure containing information gathered from maps file. */
70 struct procmap {
71         uint64_t va_start;      /* Start virtual address in file. */
72         uint64_t len;           /* Size of file. */
73         uint64_t pgoff;         /* Not used. */
74         uint32_t maj;           /* Not used. */
75         uint32_t min;           /* Not used. */
76         uint32_t ino;           /* Not used. */
77         char prot[PROT_SZ];     /* Not used. */
78         char fname[PATH_MAX];   /* File name. */
79 };
80
81 /*
82  * Locate the file containing QEMU's memory space and
83  * map it to our address space.
84  */
85 static int
86 host_memory_map(pid_t pid, uint64_t addr,
87         uint64_t *mapped_address, uint64_t *mapped_size)
88 {
89         struct dirent *dptr = NULL;
90         struct procmap procmap;
91         DIR *dp = NULL;
92         int fd;
93         int i;
94         char memfile[PATH_MAX];
95         char mapfile[PATH_MAX];
96         char procdir[PATH_MAX];
97         char resolved_path[PATH_MAX];
98         char *path = NULL;
99         FILE *fmap;
100         void *map;
101         uint8_t found = 0;
102         char line[BUFSIZE];
103         char dlm[] = "-   :   ";
104         char *str, *sp, *in[PROCMAP_SZ];
105         char *end = NULL;
106
107         /* Path where mem files are located. */
108         snprintf(procdir, PATH_MAX, "/proc/%u/fd/", pid);
109         /* Maps file used to locate mem file. */
110         snprintf(mapfile, PATH_MAX, "/proc/%u/maps", pid);
111
112         fmap = fopen(mapfile, "r");
113         if (fmap == NULL) {
114                 RTE_LOG(ERR, VHOST_CONFIG,
115                         "Failed to open maps file for pid %d\n",
116                         pid);
117                 return -1;
118         }
119
120         /* Read through maps file until we find out base_address. */
121         while (fgets(line, BUFSIZE, fmap) != 0) {
122                 str = line;
123                 errno = 0;
124                 /* Split line into fields. */
125                 for (i = 0; i < PROCMAP_SZ; i++) {
126                         in[i] = strtok_r(str, &dlm[i], &sp);
127                         if ((in[i] == NULL) || (errno != 0)) {
128                                 fclose(fmap);
129                                 return -1;
130                         }
131                         str = NULL;
132                 }
133
134                 /* Convert/Copy each field as needed. */
135                 procmap.va_start = strtoull(in[0], &end, 16);
136                 if ((in[0] == '\0') || (end == NULL) || (*end != '\0') ||
137                         (errno != 0)) {
138                         fclose(fmap);
139                         return -1;
140                 }
141
142                 procmap.len = strtoull(in[1], &end, 16);
143                 if ((in[1] == '\0') || (end == NULL) || (*end != '\0') ||
144                         (errno != 0)) {
145                         fclose(fmap);
146                         return -1;
147                 }
148
149                 procmap.pgoff = strtoull(in[3], &end, 16);
150                 if ((in[3] == '\0') || (end == NULL) || (*end != '\0') ||
151                         (errno != 0)) {
152                         fclose(fmap);
153                         return -1;
154                 }
155
156                 procmap.maj = strtoul(in[4], &end, 16);
157                 if ((in[4] == '\0') || (end == NULL) || (*end != '\0') ||
158                         (errno != 0)) {
159                         fclose(fmap);
160                         return -1;
161                 }
162
163                 procmap.min = strtoul(in[5], &end, 16);
164                 if ((in[5] == '\0') || (end == NULL) || (*end != '\0') ||
165                         (errno != 0)) {
166                         fclose(fmap);
167                         return -1;
168                 }
169
170                 procmap.ino = strtoul(in[6], &end, 16);
171                 if ((in[6] == '\0') || (end == NULL) || (*end != '\0') ||
172                         (errno != 0)) {
173                         fclose(fmap);
174                         return -1;
175                 }
176
177                 memcpy(&procmap.prot, in[2], PROT_SZ);
178                 memcpy(&procmap.fname, in[7], PATH_MAX);
179
180                 if (procmap.va_start == addr) {
181                         procmap.len = procmap.len - procmap.va_start;
182                         found = 1;
183                         break;
184                 }
185         }
186         fclose(fmap);
187
188         if (!found) {
189                 RTE_LOG(ERR, VHOST_CONFIG,
190                         "Failed to find memory file in pid %d maps file\n",
191                         pid);
192                 return -1;
193         }
194
195         /* Find the guest memory file among the process fds. */
196         dp = opendir(procdir);
197         if (dp == NULL) {
198                 RTE_LOG(ERR, VHOST_CONFIG,
199                         "Cannot open pid %d process directory\n",
200                         pid);
201                 return -1;
202         }
203
204         found = 0;
205
206         /* Read the fd directory contents. */
207         while (NULL != (dptr = readdir(dp))) {
208                 snprintf(memfile, PATH_MAX, "/proc/%u/fd/%s",
209                                 pid, dptr->d_name);
210                 path = realpath(memfile, resolved_path);
211                 if ((path == NULL) && (strlen(resolved_path) == 0)) {
212                         RTE_LOG(ERR, VHOST_CONFIG,
213                                 "Failed to resolve fd directory\n");
214                         closedir(dp);
215                         return -1;
216                 }
217                 if (strncmp(resolved_path, procmap.fname,
218                         strnlen(procmap.fname, PATH_MAX)) == 0) {
219                         found = 1;
220                         break;
221                 }
222         }
223
224         closedir(dp);
225
226         if (found == 0) {
227                 RTE_LOG(ERR, VHOST_CONFIG,
228                         "Failed to find memory file for pid %d\n",
229                         pid);
230                 return -1;
231         }
232         /* Open the shared memory file and map the memory into this process. */
233         fd = open(memfile, O_RDWR);
234
235         if (fd == -1) {
236                 RTE_LOG(ERR, VHOST_CONFIG,
237                         "Failed to open %s for pid %d\n",
238                         memfile, pid);
239                 return -1;
240         }
241
242         map = mmap(0, (size_t)procmap.len, PROT_READ|PROT_WRITE,
243                         MAP_POPULATE|MAP_SHARED, fd, 0);
244         close(fd);
245
246         if (map == MAP_FAILED) {
247                 RTE_LOG(ERR, VHOST_CONFIG,
248                         "Error mapping the file %s for pid %d\n",
249                         memfile, pid);
250                 return -1;
251         }
252
253         /* Store the memory address and size in the device data structure */
254         *mapped_address = (uint64_t)(uintptr_t)map;
255         *mapped_size = procmap.len;
256
257         LOG_DEBUG(VHOST_CONFIG,
258                 "Mem File: %s->%s - Size: %llu - VA: %p\n",
259                 memfile, resolved_path,
260                 (unsigned long long)*mapped_size, map);
261
262         return 0;
263 }
264
265 int
266 cuse_set_mem_table(struct vhost_device_ctx ctx,
267         const struct vhost_memory *mem_regions_addr, uint32_t nregions)
268 {
269         uint64_t size = offsetof(struct vhost_memory, regions);
270         uint32_t idx, valid_regions;
271         struct virtio_memory_regions *pregion;
272         struct vhost_memory_region *mem_regions = (void *)(uintptr_t)
273                 ((uint64_t)(uintptr_t)mem_regions_addr + size);
274         uint64_t base_address = 0, mapped_address, mapped_size;
275         struct virtio_net *dev;
276
277         dev = get_device(ctx);
278         if (dev == NULL)
279                 return -1;
280
281         if (dev->mem && dev->mem->mapped_address) {
282                 munmap((void *)(uintptr_t)dev->mem->mapped_address,
283                         (size_t)dev->mem->mapped_size);
284                 free(dev->mem);
285                 dev->mem = NULL;
286         }
287
288         dev->mem = calloc(1, sizeof(struct virtio_memory) +
289                 sizeof(struct virtio_memory_regions) * nregions);
290         if (dev->mem == NULL) {
291                 RTE_LOG(ERR, VHOST_CONFIG,
292                         "(%"PRIu64") Failed to allocate memory for dev->mem\n",
293                         dev->device_fh);
294                 return -1;
295         }
296
297         pregion = &dev->mem->regions[0];
298
299         for (idx = 0; idx < nregions; idx++) {
300                 pregion[idx].guest_phys_address =
301                         mem_regions[idx].guest_phys_addr;
302                 pregion[idx].guest_phys_address_end =
303                         pregion[idx].guest_phys_address +
304                         mem_regions[idx].memory_size;
305                 pregion[idx].memory_size =
306                         mem_regions[idx].memory_size;
307                 pregion[idx].userspace_address =
308                         mem_regions[idx].userspace_addr;
309
310                 LOG_DEBUG(VHOST_CONFIG,
311                         "REGION: %u - GPA: %p - QVA: %p - SIZE (%"PRIu64")\n",
312                         idx,
313                         (void *)(uintptr_t)pregion[idx].guest_phys_address,
314                         (void *)(uintptr_t)pregion[idx].userspace_address,
315                         pregion[idx].memory_size);
316
317                 /*set the base address mapping*/
318                 if (pregion[idx].guest_phys_address == 0x0) {
319                         base_address =
320                                 pregion[idx].userspace_address;
321                         /* Map VM memory file */
322                         if (host_memory_map(ctx.pid, base_address,
323                                 &mapped_address, &mapped_size) != 0) {
324                                 free(dev->mem);
325                                 dev->mem = NULL;
326                                 return -1;
327                         }
328                         dev->mem->mapped_address = mapped_address;
329                         dev->mem->base_address = base_address;
330                         dev->mem->mapped_size = mapped_size;
331                 }
332         }
333
334         /* Check that we have a valid base address. */
335         if (base_address == 0) {
336                 RTE_LOG(ERR, VHOST_CONFIG,
337                         "Failed to find base address of qemu memory file.\n");
338                 free(dev->mem);
339                 dev->mem = NULL;
340                 return -1;
341         }
342
343         valid_regions = nregions;
344         for (idx = 0; idx < nregions; idx++) {
345                 if ((pregion[idx].userspace_address < base_address) ||
346                         (pregion[idx].userspace_address >
347                         (base_address + mapped_size)))
348                         valid_regions--;
349         }
350
351
352         if (valid_regions != nregions) {
353                 valid_regions = 0;
354                 for (idx = nregions; 0 != idx--; ) {
355                         if ((pregion[idx].userspace_address < base_address) ||
356                         (pregion[idx].userspace_address >
357                         (base_address + mapped_size))) {
358                                 memmove(&pregion[idx], &pregion[idx + 1],
359                                         sizeof(struct virtio_memory_regions) *
360                                         valid_regions);
361                         } else
362                                 valid_regions++;
363                 }
364         }
365
366         for (idx = 0; idx < valid_regions; idx++) {
367                 pregion[idx].address_offset =
368                         mapped_address - base_address +
369                         pregion[idx].userspace_address -
370                         pregion[idx].guest_phys_address;
371         }
372         dev->mem->nregions = valid_regions;
373
374         return 0;
375 }
376
377 /*
378  * Function to get the tap device name from the provided file descriptor and
379  * save it in the device structure.
380  */
381 static int
382 get_ifname(struct vhost_device_ctx ctx, struct virtio_net *dev, int tap_fd, int pid)
383 {
384         int fd_tap;
385         struct ifreq ifr;
386         uint32_t ifr_size;
387         int ret;
388
389         fd_tap = eventfd_copy(tap_fd, pid);
390         if (fd_tap < 0)
391                 return -1;
392
393         ret = ioctl(fd_tap, TUNGETIFF, &ifr);
394
395         if (close(fd_tap) < 0)
396                 RTE_LOG(ERR, VHOST_CONFIG,
397                         "(%"PRIu64") fd close failed\n",
398                         dev->device_fh);
399
400         if (ret >= 0) {
401                 ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
402                 vhost_set_ifname(ctx, ifr.ifr_name, ifr_size);
403         } else
404                 RTE_LOG(ERR, VHOST_CONFIG,
405                         "(%"PRIu64") TUNGETIFF ioctl failed\n",
406                         dev->device_fh);
407
408         return 0;
409 }
410
411 int cuse_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
412 {
413         struct virtio_net *dev;
414
415         dev = get_device(ctx);
416         if (dev == NULL)
417                 return -1;
418
419         if (!(dev->flags & VIRTIO_DEV_RUNNING) && file->fd != VIRTIO_DEV_STOPPED)
420                 get_ifname(ctx, dev, file->fd, ctx.pid);
421
422         return vhost_set_backend(ctx, file);
423 }
424
425 void
426 vhost_backend_cleanup(struct virtio_net *dev)
427 {
428         /* Unmap QEMU memory file if mapped. */
429         if (dev->mem) {
430                 munmap((void *)(uintptr_t)dev->mem->mapped_address,
431                         (size_t)dev->mem->mapped_size);
432                 free(dev->mem);
433                 dev->mem = NULL;
434         }
435 }