Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.22) id 19zukt-0005bX-Do for qemu-devel@nongnu.org; Thu, 18 Sep 2003 05:07:15 -0400 Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.22) id 19zukq-0005a4-RU for qemu-devel@nongnu.org; Thu, 18 Sep 2003 05:07:13 -0400 Received: from [66.70.73.150] (helo=lists.samba.org) by monty-python.gnu.org with esmtp (Exim 4.22) id 19zueH-000321-PS for qemu-devel@nongnu.org; Thu, 18 Sep 2003 05:00:25 -0400 From: Rusty Russell Date: Thu, 18 Sep 2003 10:55:06 +1000 Sender: rusty@bach.samba.org Message-Id: <20030918090014.39A322C13D@lists.samba.org> Subject: [Qemu-devel] [PATCH] -tun-fd option Reply-To: qemu-devel@nongnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fabrice Bellard Cc: qemu-devel@nongnu.org Hi all, Played a bit with getting qemu to work with uml_switch, but I found it too baroque. This approach worked really well for me: get a helper to open and configure the tun file descriptor and hand it off to vl. Like so: (in practice I use a variant of this which takes a uid, since I ended up running it as root from an init script). #include #include #include #include #include #include #include #include #include #include /* Tiny code to open tap/tun device, and hand the fd to vl. Run setuid root, drops privs. */ int main(int argc, char *argv[]) { struct ifreq ifr; int fd; if (argc == 1) { fprintf(stderr, "Usage: tundev vl ...\n"); exit(1); } fd = open("/dev/net/tun", O_RDWR); if (fd < 0) { perror("Could not open /dev/net/tun"); exit(1); } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ); if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) { perror("Could not get tun device"); exit(1); } printf("Got tun device %s on fd %i\n", ifr.ifr_name, fd); /* Drop effective uid. */ setuid(getuid()); /* Insert -tun-fd=3 arg. */ argv[0] = argv[1]; asprintf(&argv[1], "-tun-fd=%d", fd); execvp(argv[0], argv); perror("Exec of vl failed"); exit(1); } -- Anyone who quotes me in their sig is an idiot. -- Rusty Russell. diff -ur qemu-0.4.3/vl.c qemu-0.4.3-umlswitch/qemu-0.4.3-umlswitch/vl.c --- qemu-0.4.3/vl.c 2003-07-14 08:12:05.000000000 +1000 +++ qemu-0.4.3/vl.c 2003-09-16 16:33:49.000000000 +1000 @@ -2635,6 +2639,7 @@ "-snapshot write to temporary files instead of disk image files\n" "-m megs set virtual RAM size to megs MB\n" "-n script set network init script [default=%s]\n" + "-tun-fd=fd this fd talks to tap/tun, use it.\n" "\n" "Debug options:\n" "-s wait gdb connection to port %d\n" @@ -2652,6 +2657,7 @@ { "hda", 1, NULL, 0, }, { "hdb", 1, NULL, 0, }, { "snapshot", 0, NULL, 0, }, + { "tun-fd", 1, NULL, 0, }, { NULL, 0, NULL, 0 }, }; @@ -2695,6 +2702,9 @@ case 3: snapshot = 1; break; + case 4: + net_fd = atoi(optarg); + break; } break; case 'h': @@ -2739,7 +2749,8 @@ } /* init network tun interface */ - net_init(); + if (net_fd < 0) + net_init(); /* init the memory */ tmpdir = getenv("VLTMPDIR");