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>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <linux/if_tun.h>
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"
60 /* Line size for reading maps file. */
61 static const uint32_t BUFSIZE = PATH_MAX;
63 /* Size of prot char array in procmap. */
66 /* Number of elements in procmap struct. */
69 /* Structure containing information gathered from maps file. */
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. */
82 * Locate the file containing QEMU's memory space and
83 * map it to our address space.
86 host_memory_map(pid_t pid, uint64_t addr,
87 uint64_t *mapped_address, uint64_t *mapped_size)
89 struct dirent *dptr = NULL;
90 struct procmap procmap;
94 char memfile[PATH_MAX];
95 char mapfile[PATH_MAX];
96 char procdir[PATH_MAX];
97 char resolved_path[PATH_MAX];
104 char *str, *sp, *in[PROCMAP_SZ];
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);
112 fmap = fopen(mapfile, "r");
114 RTE_LOG(ERR, VHOST_CONFIG,
115 "Failed to open maps file for pid %d\n",
120 /* Read through maps file until we find out base_address. */
121 while (fgets(line, BUFSIZE, fmap) != 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)) {
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') ||
142 procmap.len = strtoull(in[1], &end, 16);
143 if ((in[1] == '\0') || (end == NULL) || (*end != '\0') ||
149 procmap.pgoff = strtoull(in[3], &end, 16);
150 if ((in[3] == '\0') || (end == NULL) || (*end != '\0') ||
156 procmap.maj = strtoul(in[4], &end, 16);
157 if ((in[4] == '\0') || (end == NULL) || (*end != '\0') ||
163 procmap.min = strtoul(in[5], &end, 16);
164 if ((in[5] == '\0') || (end == NULL) || (*end != '\0') ||
170 procmap.ino = strtoul(in[6], &end, 16);
171 if ((in[6] == '\0') || (end == NULL) || (*end != '\0') ||
177 memcpy(&procmap.prot, in[2], PROT_SZ);
178 memcpy(&procmap.fname, in[7], PATH_MAX);
180 if (procmap.va_start == addr) {
181 procmap.len = procmap.len - procmap.va_start;
189 RTE_LOG(ERR, VHOST_CONFIG,
190 "Failed to find memory file in pid %d maps file\n",
195 /* Find the guest memory file among the process fds. */
196 dp = opendir(procdir);
198 RTE_LOG(ERR, VHOST_CONFIG,
199 "Cannot open pid %d process directory\n",
206 /* Read the fd directory contents. */
207 while (NULL != (dptr = readdir(dp))) {
208 snprintf(memfile, PATH_MAX, "/proc/%u/fd/%s",
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");
217 if (strncmp(resolved_path, procmap.fname,
218 strnlen(procmap.fname, PATH_MAX)) == 0) {
227 RTE_LOG(ERR, VHOST_CONFIG,
228 "Failed to find memory file for pid %d\n",
232 /* Open the shared memory file and map the memory into this process. */
233 fd = open(memfile, O_RDWR);
236 RTE_LOG(ERR, VHOST_CONFIG,
237 "Failed to open %s for pid %d\n",
242 map = mmap(0, (size_t)procmap.len, PROT_READ|PROT_WRITE,
243 MAP_POPULATE|MAP_SHARED, fd, 0);
246 if (map == MAP_FAILED) {
247 RTE_LOG(ERR, VHOST_CONFIG,
248 "Error mapping the file %s for pid %d\n",
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;
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);
266 cuse_set_mem_table(struct vhost_device_ctx ctx,
267 const struct vhost_memory *mem_regions_addr, uint32_t nregions)
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;
277 dev = get_device(ctx);
281 if (dev->mem && dev->mem->mapped_address) {
282 munmap((void *)(uintptr_t)dev->mem->mapped_address,
283 (size_t)dev->mem->mapped_size);
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",
297 pregion = &dev->mem->regions[0];
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;
310 LOG_DEBUG(VHOST_CONFIG,
311 "REGION: %u - GPA: %p - QVA: %p - SIZE (%"PRIu64")\n",
313 (void *)(uintptr_t)pregion[idx].guest_phys_address,
314 (void *)(uintptr_t)pregion[idx].userspace_address,
315 pregion[idx].memory_size);
317 /*set the base address mapping*/
318 if (pregion[idx].guest_phys_address == 0x0) {
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) {
328 dev->mem->mapped_address = mapped_address;
329 dev->mem->base_address = base_address;
330 dev->mem->mapped_size = mapped_size;
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");
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)))
352 if (valid_regions != nregions) {
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) *
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;
372 dev->mem->nregions = valid_regions;
378 * Function to get the tap device name from the provided file descriptor and
379 * save it in the device structure.
382 get_ifname(struct vhost_device_ctx ctx, struct virtio_net *dev, int tap_fd, int pid)
389 fd_tap = eventfd_copy(tap_fd, pid);
393 ret = ioctl(fd_tap, TUNGETIFF, &ifr);
395 if (close(fd_tap) < 0)
396 RTE_LOG(ERR, VHOST_CONFIG,
397 "(%"PRIu64") fd close failed\n",
401 ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
402 vhost_set_ifname(ctx, ifr.ifr_name, ifr_size);
404 RTE_LOG(ERR, VHOST_CONFIG,
405 "(%"PRIu64") TUNGETIFF ioctl failed\n",
411 int cuse_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
413 struct virtio_net *dev;
415 dev = get_device(ctx);
419 if (!(dev->flags & VIRTIO_DEV_RUNNING) && file->fd != VIRTIO_DEV_STOPPED)
420 get_ifname(ctx, dev, file->fd, ctx.pid);
422 return vhost_set_backend(ctx, file);
426 vhost_backend_cleanup(struct virtio_net *dev)
428 /* Unmap QEMU memory file if mapped. */
430 munmap((void *)(uintptr_t)dev->mem->mapped_address,
431 (size_t)dev->mem->mapped_size);