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