[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 05/19] net: refactor tap initialization
From: |
Mark McLoughlin |
Subject: |
[Qemu-devel] [PATCH 05/19] net: refactor tap initialization |
Date: |
Wed, 21 Oct 2009 12:27:44 +0100 |
Re-factor things so that there is only one call site for
net_tap_fd_init().
Two concerns about the QemuOpts usage here - firstly, we set the script
arguments to their default value and, secondly, we set the ifname value
to the name allocated by the kernel if none is supplied. Are we okay
with such things ending up in writeconfig output?
Signed-off-by: Mark McLoughlin <address@hidden>
---
net.c | 97 +++++++++++++++++++++++++++++++++++------------------------------
1 files changed, 52 insertions(+), 45 deletions(-)
diff --git a/net.c b/net.c
index 356a280..fccabdb 100644
--- a/net.c
+++ b/net.c
@@ -1475,7 +1475,6 @@ static TAPState *net_tap_fd_init(VLANState *vlan,
tap_receive, tap_receive_iov,
tap_cleanup, s);
tap_read_poll(s, 1);
- snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
return s;
}
@@ -1724,38 +1723,34 @@ static int launch_script(const char *setup_script,
const char *ifname, int fd)
return -1;
}
-static TAPState *net_tap_init(VLANState *vlan, const char *model,
- const char *name, const char *ifname1,
- const char *setup_script, const char
*down_script)
+static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
{
- TAPState *s;
- int fd, vnet_hdr;
- char ifname[128];
+ int fd;
+ char ifname[128] = {0,};
+ const char *setup_script;
- if (ifname1 != NULL)
- pstrcpy(ifname, sizeof(ifname), ifname1);
- else
- ifname[0] = '\0';
- vnet_hdr = 0;
- TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
- if (fd < 0)
- return NULL;
+ if (qemu_opt_get(opts, "ifname")) {
+ pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
+ }
- if (!setup_script || !strcmp(setup_script, "no"))
- setup_script = "";
- if (setup_script[0] != '\0' &&
- launch_script(setup_script, ifname, fd)) {
- return NULL;
+ *vnet_hdr = 0;
+ TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr));
+ if (fd < 0) {
+ return -1;
}
- s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
- snprintf(s->vc->info_str, sizeof(s->vc->info_str),
- "ifname=%s,script=%s,downscript=%s",
- ifname, setup_script, down_script);
- if (down_script && strcmp(down_script, "no")) {
- snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
- snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
+
+ setup_script = qemu_opt_get(opts, "script");
+ if (setup_script &&
+ setup_script[0] != '\0' &&
+ strcmp(setup_script, "no") != 0 &&
+ launch_script(setup_script, ifname, fd)) {
+ close(fd);
+ return -1;
}
- return s;
+
+ qemu_opt_set(opts, "ifname", ifname);
+
+ return fd;
}
#endif /* !_WIN32 */
@@ -2683,10 +2678,9 @@ static int net_init_tap(QemuOpts *opts,
VLANState *vlan)
{
TAPState *s;
+ int fd, vnet_hdr;
if (qemu_opt_get(opts, "fd")) {
- int fd;
-
if (qemu_opt_get(opts, "ifname") ||
qemu_opt_get(opts, "script") ||
qemu_opt_get(opts, "downscript")) {
@@ -2701,28 +2695,22 @@ static int net_init_tap(QemuOpts *opts,
fcntl(fd, F_SETFL, O_NONBLOCK);
- s = net_tap_fd_init(vlan, "tap", name, fd, tap_probe_vnet_hdr(fd));
- if (!s) {
- close(fd);
- }
+ vnet_hdr = tap_probe_vnet_hdr(fd);
} else {
- const char *ifname, *script, *downscript;
-
- ifname = qemu_opt_get(opts, "ifname");
- script = qemu_opt_get(opts, "script");
- downscript = qemu_opt_get(opts, "downscript");
-
- if (!script) {
- script = DEFAULT_NETWORK_SCRIPT;
+ if (!qemu_opt_get(opts, "script")) {
+ qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
}
- if (!downscript) {
- downscript = DEFAULT_NETWORK_DOWN_SCRIPT;
+
+ if (!qemu_opt_get(opts, "downscript")) {
+ qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
}
- s = net_tap_init(vlan, "tap", name, ifname, script, downscript);
+ fd = net_tap_init(opts, &vnet_hdr);
}
+ s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
if (!s) {
+ close(fd);
return -1;
}
@@ -2730,6 +2718,25 @@ static int net_init_tap(QemuOpts *opts,
return -1;
}
+ if (qemu_opt_get(opts, "fd")) {
+ snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
+ } else {
+ const char *ifname, *script, *downscript;
+
+ ifname = qemu_opt_get(opts, "ifname");
+ script = qemu_opt_get(opts, "script");
+ downscript = qemu_opt_get(opts, "downscript");
+
+ snprintf(s->vc->info_str, sizeof(s->vc->info_str),
+ "ifname=%s,script=%s,downscript=%s",
+ ifname, script, downscript);
+
+ if (strcmp(downscript, "no") != 0) {
+ snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
+ snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s",
ifname);
+ }
+ }
+
if (vlan) {
vlan->nb_host_devs++;
}
--
1.6.2.5
- [Qemu-devel] [PATCH 00/19], Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 03/19] net: make tap_receive() re-use tap_receive_iov() code, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 01/19] net: remove unused includes of if_tun.h and if_tap.h, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 08/19] net: add tap_has_vnet_hdr() and tap_using_vnet_hdr() APIs, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 04/19] net: enable IFF_VNET_HDR on tap fds if available, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 02/19] net: import linux tap ioctl definitions, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 06/19] net: add a vnet_hdr=on|off parameter, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 07/19] net: add a client type code, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 13/19] net: implement tap support for receive_raw(), Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 05/19] net: refactor tap initialization,
Mark McLoughlin <=
- [Qemu-devel] [PATCH 15/19] net: add tap_set_offload(), Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 14/19] virtio-net: add vnet_hdr support, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 19/19] virtio-net: add tap_has_ufo flag to saved state, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 10/19] net: add an API for 'raw' packets, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 09/19] net: add flags parameter to packet queue interface, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 12/19] net: use qemu_send_packet_raw() in qemu_announce_self(), Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 16/19] virtio-net: enable tap offload if guest supports it, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 17/19] Work around dhclient brokenness, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 18/19] Enable UFO on virtio-net and tap devices, Mark McLoughlin, 2009/10/21
- [Qemu-devel] [PATCH 11/19] net: add receive_raw parameter to qemu_new_vlan_client(), Mark McLoughlin, 2009/10/21