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