4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <linux/vhost.h>
37 #include <linux/virtio_net.h>
38 #include <fuse/cuse_lowlevel.h>
42 #include <sys/eventfd.h>
44 #include <sys/types.h>
50 #include "rte_virtio_net.h"
51 #include "vhost-net.h"
52 #include "virtio-net-cdev.h"
53 #include "virtio-net.h"
55 /* Line size for reading maps file. */
56 static const uint32_t BUFSIZE = PATH_MAX;
58 /* Size of prot char array in procmap. */
61 /* Number of elements in procmap struct. */
64 /* Structure containing information gathered from maps file. */
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. */
77 * Locate the file containing QEMU's memory space and
78 * map it to our address space.
81 host_memory_map(pid_t pid, uint64_t addr,
82 uint64_t *mapped_address, uint64_t *mapped_size)
84 struct dirent *dptr = NULL;
85 struct procmap procmap;
89 char memfile[PATH_MAX];
90 char mapfile[PATH_MAX];
91 char procdir[PATH_MAX];
92 char resolved_path[PATH_MAX];
99 char *str, *sp, *in[PROCMAP_SZ];
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);
107 fmap = fopen(mapfile, "r");
109 RTE_LOG(ERR, VHOST_CONFIG,
110 "Failed to open maps file for pid %d\n",
115 /* Read through maps file until we find out base_address. */
116 while (fgets(line, BUFSIZE, fmap) != 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)) {
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') ||
137 procmap.len = strtoull(in[1], &end, 16);
138 if ((in[1] == '\0') || (end == NULL) || (*end != '\0') ||
144 procmap.pgoff = strtoull(in[3], &end, 16);
145 if ((in[3] == '\0') || (end == NULL) || (*end != '\0') ||
151 procmap.maj = strtoul(in[4], &end, 16);
152 if ((in[4] == '\0') || (end == NULL) || (*end != '\0') ||
158 procmap.min = strtoul(in[5], &end, 16);
159 if ((in[5] == '\0') || (end == NULL) || (*end != '\0') ||
165 procmap.ino = strtoul(in[6], &end, 16);
166 if ((in[6] == '\0') || (end == NULL) || (*end != '\0') ||
172 memcpy(&procmap.prot, in[2], PROT_SZ);
173 memcpy(&procmap.fname, in[7], PATH_MAX);
175 if (procmap.va_start == addr) {
176 procmap.len = procmap.len - procmap.va_start;
184 RTE_LOG(ERR, VHOST_CONFIG,
185 "Failed to find memory file in pid %d maps file\n",
190 /* Find the guest memory file among the process fds. */
191 dp = opendir(procdir);
193 RTE_LOG(ERR, VHOST_CONFIG,
194 "Cannot open pid %d process directory\n",
201 /* Read the fd directory contents. */
202 while (NULL != (dptr = readdir(dp))) {
203 snprintf(memfile, PATH_MAX, "/proc/%u/fd/%s",
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");
212 if (strncmp(resolved_path, procmap.fname,
213 strnlen(procmap.fname, PATH_MAX)) == 0) {
222 RTE_LOG(ERR, VHOST_CONFIG,
223 "Failed to find memory file for pid %d\n",
227 /* Open the shared memory file and map the memory into this process. */
228 fd = open(memfile, O_RDWR);
231 RTE_LOG(ERR, VHOST_CONFIG,
232 "Failed to open %s for pid %d\n",
237 map = mmap(0, (size_t)procmap.len, PROT_READ|PROT_WRITE,
238 MAP_POPULATE|MAP_SHARED, fd, 0);
241 if (map == MAP_FAILED) {
242 RTE_LOG(ERR, VHOST_CONFIG,
243 "Error mapping the file %s for pid %d\n",
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;
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);
261 cuse_set_mem_table(struct vhost_device_ctx ctx,
262 const struct vhost_memory *mem_regions_addr, uint32_t nregions)
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;
272 dev = get_device(ctx);
276 if (dev->mem && dev->mem->mapped_address) {
277 munmap((void *)(uintptr_t)dev->mem->mapped_address,
278 (size_t)dev->mem->mapped_size);
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",
292 pregion = &dev->mem->regions[0];
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;
305 LOG_DEBUG(VHOST_CONFIG,
306 "REGION: %u - GPA: %p - QVA: %p - SIZE (%"PRIu64")\n",
308 (void *)(uintptr_t)pregion[idx].guest_phys_address,
309 (void *)(uintptr_t)pregion[idx].userspace_address,
310 pregion[idx].memory_size);
312 /*set the base address mapping*/
313 if (pregion[idx].guest_phys_address == 0x0) {
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) {
323 dev->mem->mapped_address = mapped_address;
324 dev->mem->base_address = base_address;
325 dev->mem->mapped_size = mapped_size;
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");
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)))
347 if (valid_regions != nregions) {
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) *
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;
367 dev->mem->nregions = valid_regions;