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 "eventfd_copy.h"
59 /* Line size for reading maps file. */
60 static const uint32_t BUFSIZE = PATH_MAX;
62 /* Size of prot char array in procmap. */
65 /* Number of elements in procmap struct. */
68 /* Structure containing information gathered from maps file. */
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. */
81 * Locate the file containing QEMU's memory space and
82 * map it to our address space.
85 host_memory_map(pid_t pid, uint64_t addr,
86 uint64_t *mapped_address, uint64_t *mapped_size)
88 struct dirent *dptr = NULL;
89 struct procmap procmap;
93 char memfile[PATH_MAX];
94 char mapfile[PATH_MAX];
95 char procdir[PATH_MAX];
96 char resolved_path[PATH_MAX];
103 char *str, *sp, *in[PROCMAP_SZ];
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);
111 fmap = fopen(mapfile, "r");
113 RTE_LOG(ERR, VHOST_CONFIG,
114 "Failed to open maps file for pid %d\n",
119 /* Read through maps file until we find out base_address. */
120 while (fgets(line, BUFSIZE, fmap) != 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)) {
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') ||
141 procmap.len = strtoull(in[1], &end, 16);
142 if ((in[1] == '\0') || (end == NULL) || (*end != '\0') ||
148 procmap.pgoff = strtoull(in[3], &end, 16);
149 if ((in[3] == '\0') || (end == NULL) || (*end != '\0') ||
155 procmap.maj = strtoul(in[4], &end, 16);
156 if ((in[4] == '\0') || (end == NULL) || (*end != '\0') ||
162 procmap.min = strtoul(in[5], &end, 16);
163 if ((in[5] == '\0') || (end == NULL) || (*end != '\0') ||
169 procmap.ino = strtoul(in[6], &end, 16);
170 if ((in[6] == '\0') || (end == NULL) || (*end != '\0') ||
176 memcpy(&procmap.prot, in[2], PROT_SZ);
177 memcpy(&procmap.fname, in[7], PATH_MAX);
179 if (procmap.va_start == addr) {
180 procmap.len = procmap.len - procmap.va_start;
188 RTE_LOG(ERR, VHOST_CONFIG,
189 "Failed to find memory file in pid %d maps file\n",
194 /* Find the guest memory file among the process fds. */
195 dp = opendir(procdir);
197 RTE_LOG(ERR, VHOST_CONFIG,
198 "Cannot open pid %d process directory\n",
205 /* Read the fd directory contents. */
206 while (NULL != (dptr = readdir(dp))) {
207 snprintf(memfile, PATH_MAX, "/proc/%u/fd/%s",
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");
216 if (strncmp(resolved_path, procmap.fname,
217 strnlen(procmap.fname, PATH_MAX)) == 0) {
226 RTE_LOG(ERR, VHOST_CONFIG,
227 "Failed to find memory file for pid %d\n",
231 /* Open the shared memory file and map the memory into this process. */
232 fd = open(memfile, O_RDWR);
235 RTE_LOG(ERR, VHOST_CONFIG,
236 "Failed to open %s for pid %d\n",
241 map = mmap(0, (size_t)procmap.len, PROT_READ|PROT_WRITE,
242 MAP_POPULATE|MAP_SHARED, fd, 0);
245 if (map == MAP_FAILED) {
246 RTE_LOG(ERR, VHOST_CONFIG,
247 "Error mapping the file %s for pid %d\n",
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;
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);
265 cuse_set_mem_table(struct vhost_cuse_device_ctx ctx,
266 const struct vhost_memory *mem_regions_addr, uint32_t nregions)
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;
276 dev = get_device(ctx.vid);
280 if (dev->mem && dev->mem->mapped_address) {
281 munmap((void *)(uintptr_t)dev->mem->mapped_address,
282 (size_t)dev->mem->mapped_size);
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",
296 pregion = &dev->mem->regions[0];
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;
309 LOG_DEBUG(VHOST_CONFIG,
310 "REGION: %u - GPA: %p - QVA: %p - SIZE (%"PRIu64")\n",
312 (void *)(uintptr_t)pregion[idx].guest_phys_address,
313 (void *)(uintptr_t)pregion[idx].userspace_address,
314 pregion[idx].memory_size);
316 /*set the base address mapping*/
317 if (pregion[idx].guest_phys_address == 0x0) {
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) {
327 dev->mem->mapped_address = mapped_address;
328 dev->mem->base_address = base_address;
329 dev->mem->mapped_size = mapped_size;
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");
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)))
351 if (valid_regions != nregions) {
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) *
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;
371 dev->mem->nregions = valid_regions;
377 * Function to get the tap device name from the provided file descriptor and
378 * save it in the device structure.
381 get_ifname(int vid, int tap_fd, int pid)
388 fd_tap = eventfd_copy(tap_fd, pid);
392 ret = ioctl(fd_tap, TUNGETIFF, &ifr);
394 if (close(fd_tap) < 0)
395 RTE_LOG(ERR, VHOST_CONFIG, "(%d) fd close failed\n", vid);
398 ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name));
399 vhost_set_ifname(vid, ifr.ifr_name, ifr_size);
401 RTE_LOG(ERR, VHOST_CONFIG,
402 "(%d) TUNGETIFF ioctl failed\n", vid);
408 cuse_set_backend(struct vhost_cuse_device_ctx ctx,
409 struct vhost_vring_file *file)
411 struct virtio_net *dev;
413 dev = get_device(ctx.vid);
417 if (!(dev->flags & VIRTIO_DEV_RUNNING) && file->fd != VIRTIO_DEV_STOPPED)
418 get_ifname(ctx.vid, file->fd, ctx.pid);
420 return vhost_set_backend(ctx.vid, file);
424 vhost_backend_cleanup(struct virtio_net *dev)
426 /* Unmap QEMU memory file if mapped. */
428 munmap((void *)(uintptr_t)dev->mem->mapped_address,
429 (size_t)dev->mem->mapped_size);