Simplify handling scripts: parse all these "no" and '\0' once, and
then keep simpler logic for net_tap_open() and net_init_tap_one(): NULL
means no script to run, otherwise run script.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
net/tap.c | 45 +++++++++++++++++++++++++--------------------
1 file changed, 25 insertions(+), 20 deletions(-)
diff --git a/net/tap.c b/net/tap.c
index e42c7ca044..084ee4f649 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -88,6 +88,21 @@ static void launch_script(const char *setup_script, const char *ifname,
static void tap_send(void *opaque);
static void tap_writable(void *opaque);
+static char *tap_parse_script(const char *script_arg, const char *default_path)
+{
+ g_autofree char *res = g_strdup(script_arg);
+
+ if (!res) {
+ res = get_relocated_path(default_path);
+ }
+
+ if (res[0] == '\0' || strcmp(res, "no") == 0) {
+ return NULL;
+ }
+
+ return g_steal_pointer(&res);
+}
+
static void tap_update_fd_handler(TAPState *s)
{
qemu_set_fd_handler(s->fd,
@@ -655,9 +670,7 @@ static int net_tap_open(int *vnet_hdr, bool vnet_hdr_required,
return -1;
}
- if (setup_script &&
- setup_script[0] != '\0' &&
- strcmp(setup_script, "no") != 0) {
+ if (setup_script) {
launch_script(setup_script, ifname, fd, &err);
if (err) {
error_propagate(errp, err);
@@ -693,9 +706,9 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
qemu_set_info_str(&s->nc, "helper=%s", tap->helper);
} else {
qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname,
- script, downscript);
+ script ?: "no", downscript ?: "no");
- if (strcmp(downscript, "no") != 0) {
+ if (downscript) {
snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
snprintf(s->down_script_arg, sizeof(s->down_script_arg),
"%s", ifname);
@@ -950,10 +963,10 @@ free_fail:
return -1;
}
} else {
- const char *script = tap->script;
- const char *downscript = tap->downscript;
- g_autofree char *default_script = NULL;
- g_autofree char *default_downscript = NULL;
+ g_autofree char *script =
+ tap_parse_script(tap->script, DEFAULT_NETWORK_SCRIPT);
+ g_autofree char *downscript =
+ tap_parse_script(tap->script, DEFAULT_NETWORK_DOWN_SCRIPT);
bool vnet_hdr_required = tap->has_vnet_hdr && tap->vnet_hdr;
if (tap->vhostfds) {
@@ -961,14 +974,6 @@ free_fail:
return -1;
}
- if (!script) {
- script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
- }
- if (!downscript) {
- downscript = default_downscript =
- get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
- }
-
if (tap->ifname) {
pstrcpy(ifname, sizeof ifname, tap->ifname);
} else {
@@ -978,7 +983,7 @@ free_fail:
for (i = 0; i < queues; i++) {
vnet_hdr = tap->has_vnet_hdr ? tap->vnet_hdr : 1;
fd = net_tap_open(&vnet_hdr, vnet_hdr_required,
- i >= 1 ? "no" : script,
+ i >= 1 ? NULL : script,
ifname, sizeof ifname, queues > 1, errp);
if (fd == -1) {
return -1;
@@ -993,8 +998,8 @@ free_fail:
}
net_init_tap_one(tap, peer, "tap", name, ifname,
- i >= 1 ? "no" : script,
- i >= 1 ? "no" : downscript,
+ i >= 1 ? NULL : script,
+ i >= 1 ? NULL : downscript,
vhostfdname, vnet_hdr, fd, &err);
if (err) {
error_propagate(errp, err);
--
2.48.1