From: Arnd Bergmann <arnd@arndb.de>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devices
Date: Thu, 26 Nov 2009 20:34:04 +0100 [thread overview]
Message-ID: <200911262034.04440.arnd@arndb.de> (raw)
With the upcoming macvtap, we will want to open devices other than
/dev/net/tun but no longer need to call TUNSETIFF.
This makes it possible to do 'qemu -net tap,ifname=/dev/tap/macvtap0'
to refer to a chardev in addition to the current way of doing
'qemu -net tap,ifname=tap0' to refer to a tap network interface
set with TUNSETIFF. This is consistent with what we do on BSD.
This is guaranteed not to cause conflicts with existing usage,
because network devices on Linux cannot start with a '/'.
Alternatively, I could do a patch to use the -net tap,name=
parameter which documented to have a similar purpose but not
currently implemented at all.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
diff --git a/net/tap-linux.c b/net/tap-linux.c
index 0f621a2..21f0167 100644
--- a/net/tap-linux.c
+++ b/net/tap-linux.c
@@ -36,10 +36,20 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required
{
struct ifreq ifr;
int fd, ret;
+ int open_named;
+ char *devname;
+
+ if (ifname_size >= 1 && *ifname == "/") {
+ open_named = 1;
+ devname = ifname;
+ } else {
+ open_named = 0;
+ devname = "/dev/net/tun";
+ }
- TFR(fd = open("/dev/net/tun", O_RDWR));
+ TFR(fd = open(devname, O_RDWR));
if (fd < 0) {
- fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
+ fprintf(stderr, "warning: could not open %s: no virtual network emulation\n", devname);
return -1;
}
memset(&ifr, 0, sizeof(ifr));
@@ -62,17 +72,20 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required
}
}
- if (ifname[0] != '\0')
- pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
- else
- pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
- ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
- if (ret != 0) {
- fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
- close(fd);
- return -1;
+ if (!open_named) {
+ if (ifname[0] != '\0')
+ pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
+ else
+ pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
+ ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
+ if (ret != 0) {
+ fprintf(stderr, "warning: could not configure %s: no virtual "
+ "network emulation\n", devname);
+ close(fd);
+ return -1;
+ }
+ pstrcpy(ifname, ifname_size, ifr.ifr_name);
}
- pstrcpy(ifname, ifname_size, ifr.ifr_name);
fcntl(fd, F_SETFL, O_NONBLOCK);
return fd;
}
next reply other threads:[~2009-11-26 19:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-26 19:34 Arnd Bergmann [this message]
2009-12-02 15:46 ` [Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devices Anthony Liguori
2009-12-03 13:33 ` Arnd Bergmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200911262034.04440.arnd@arndb.de \
--to=arnd@arndb.de \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).