All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@siemens.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 06/11] slirp: Reorder initialization
Date: Fri, 08 May 2009 12:34:18 +0200	[thread overview]
Message-ID: <20090508103418.6080.24111.stgit@mchn012c.ww002.siemens.net> (raw)
In-Reply-To: <20090508103416.6080.44298.stgit@mchn012c.ww002.siemens.net>

This patch reorders the initialization of slirp itself as well as its
associated features smb and redirection. So far the first reference to
slirp triggered the initialization, independent of the actual -net user
option which may carry additional parameters. Now we save any request to
add a smb export or some redirections until the actual initialization of
the stack. This also allows to move a few parameters that were passed
via global variable into the argument list of net_slirp_init.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---

 net.c            |  119 ++++++++++++++++++++++++++++++++++++++++--------------
 slirp/libslirp.h |    2 -
 slirp/slirp.c    |    2 -
 3 files changed, 90 insertions(+), 33 deletions(-)

diff --git a/net.c b/net.c
index 658f884..b20a907 100644
--- a/net.c
+++ b/net.c
@@ -551,11 +551,21 @@ static void config_error(Monitor *mon, const char *fmt, ...)
 
 /* slirp network adapter */
 
+struct slirp_config_str {
+    struct slirp_config_str *next;
+    const char *str;
+};
+
 static int slirp_inited;
-static int slirp_restrict;
-static char *slirp_ip;
+static struct slirp_config_str *slirp_redirs;
+#ifndef _WIN32
+static const char *slirp_smb_export;
+#endif
 static VLANClientState *slirp_vc;
 
+static void slirp_smb(const char *exported_dir);
+static void slirp_redirection(Monitor *mon, const char *redir_str);
+
 int slirp_can_output(void)
 {
     return !slirp_vc || qemu_can_send_packet(slirp_vc);
@@ -593,7 +603,8 @@ static void net_slirp_cleanup(VLANClientState *vc)
     slirp_in_use = 0;
 }
 
-static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
+static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
+                          int restricted, const char *ip)
 {
     if (slirp_in_use) {
         /* slirp only supports a single instance so far */
@@ -601,31 +612,41 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
     }
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init(slirp_restrict, slirp_ip);
+        slirp_init(restricted, ip);
+
+        while (slirp_redirs) {
+            struct slirp_config_str *config = slirp_redirs;
+
+            slirp_redirection(NULL, config->str);
+            slirp_redirs = config->next;
+            qemu_free(config);
+        }
+#ifndef _WIN32
+        if (slirp_smb_export) {
+            slirp_smb(slirp_smb_export);
+        }
+#endif
     }
-    slirp_vc = qemu_new_vlan_client(vlan, model, name,
-                                    slirp_receive, NULL, net_slirp_cleanup, NULL);
+
+    slirp_vc = qemu_new_vlan_client(vlan, model, name, slirp_receive,
+                                    NULL, net_slirp_cleanup, NULL);
     slirp_vc->info_str[0] = '\0';
     slirp_in_use = 1;
     return 0;
 }
 
-void net_slirp_redir(Monitor *mon, const char *redir_str)
+static void slirp_redirection(Monitor *mon, const char *redir_str)
 {
-    int is_udp;
-    char buf[256], *r;
-    const char *p;
     struct in_addr guest_addr;
     int host_port, guest_port;
-
-    if (!slirp_inited) {
-        slirp_inited = 1;
-        slirp_init(slirp_restrict, slirp_ip);
-    }
+    const char *p;
+    char buf[256], *r;
+    int is_udp;
 
     p = redir_str;
-    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
+    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
         goto fail_syntax;
+    }
     if (!strcmp(buf, "tcp") || buf[0] == '\0') {
         is_udp = 0;
     } else if (!strcmp(buf, "udp")) {
@@ -634,23 +655,28 @@ void net_slirp_redir(Monitor *mon, const char *redir_str)
         goto fail_syntax;
     }
 
-    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
+    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
         goto fail_syntax;
+    }
     host_port = strtol(buf, &r, 0);
-    if (r == buf)
+    if (r == buf) {
         goto fail_syntax;
+    }
 
-    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
+    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
         goto fail_syntax;
+    }
     if (buf[0] == '\0') {
         pstrcpy(buf, sizeof(buf), "10.0.2.15");
     }
-    if (!inet_aton(buf, &guest_addr))
+    if (!inet_aton(buf, &guest_addr)) {
         goto fail_syntax;
+    }
 
     guest_port = strtol(p, &r, 0);
-    if (r == p)
+    if (r == p) {
         goto fail_syntax;
+    }
 
     if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
         config_error(mon, "could not set up redirection '%s'\n", redir_str);
@@ -661,6 +687,25 @@ void net_slirp_redir(Monitor *mon, const char *redir_str)
     config_error(mon, "invalid redirection format '%s'\n", redir_str);
 }
 
+void net_slirp_redir(Monitor *mon, const char *redir_str)
+{
+    struct slirp_config_str *config;
+
+    if (!slirp_inited) {
+        if (mon) {
+            monitor_printf(mon, "user mode network stack not in use\n");
+        } else {
+            config = qemu_malloc(sizeof(*config));
+            config->str = redir_str;
+            config->next = slirp_redirs;
+            slirp_redirs = config;
+        }
+        return;
+    }
+
+    slirp_redirection(mon, redir_str);
+}
+
 #ifndef _WIN32
 
 static char smb_dir[1024];
@@ -696,18 +741,12 @@ static void smb_exit(void)
     erase_dir(smb_dir);
 }
 
-/* automatic user mode samba server configuration */
-void net_slirp_smb(const char *exported_dir)
+static void slirp_smb(const char *exported_dir)
 {
     char smb_conf[1024];
     char smb_cmdline[1024];
     FILE *f;
 
-    if (!slirp_inited) {
-        slirp_inited = 1;
-        slirp_init(slirp_restrict, slirp_ip);
-    }
-
     /* XXX: better tmp dir construction */
     snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
     if (mkdir(smb_dir, 0700) < 0) {
@@ -751,7 +790,21 @@ void net_slirp_smb(const char *exported_dir)
     slirp_add_exec(0, smb_cmdline, 4, 139);
 }
 
+/* automatic user mode samba server configuration */
+void net_slirp_smb(const char *exported_dir)
+{
+    if (slirp_smb_export) {
+        fprintf(stderr, "-smb given twice\n");
+        exit(1);
+    }
+    slirp_smb_export = exported_dir;
+    if (slirp_inited) {
+        slirp_smb(exported_dir);
+    }
+}
+
 #endif /* !defined(_WIN32) */
+
 void do_info_slirp(Monitor *mon)
 {
     slirp_stats();
@@ -1885,6 +1938,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         static const char * const slirp_params[] = {
             "vlan", "name", "hostname", "restrict", "ip", NULL
         };
+        int restricted = 0;
+        char *ip = NULL;
+
         if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
             config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
             ret = -1;
@@ -1894,13 +1950,14 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
             pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
         }
         if (get_param_value(buf, sizeof(buf), "restrict", p)) {
-            slirp_restrict = (buf[0] == 'y') ? 1 : 0;
+            restricted = (buf[0] == 'y') ? 1 : 0;
         }
         if (get_param_value(buf, sizeof(buf), "ip", p)) {
-            slirp_ip = strdup(buf);
+            ip = qemu_strdup(buf);
         }
         vlan->nb_host_devs++;
-        ret = net_slirp_init(vlan, device, name);
+        ret = net_slirp_init(vlan, device, name, restricted, ip);
+        qemu_free(ip);
     } else if (!strcmp(device, "channel")) {
         long port;
         char name[20], *devname;
diff --git a/slirp/libslirp.h b/slirp/libslirp.h
index a1cd70e..c0781c3 100644
--- a/slirp/libslirp.h
+++ b/slirp/libslirp.h
@@ -5,7 +5,7 @@
 extern "C" {
 #endif
 
-void slirp_init(int restricted, char *special_ip);
+void slirp_init(int restricted, const char *special_ip);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
diff --git a/slirp/slirp.c b/slirp/slirp.c
index 4ca35b2..73b3704 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -171,7 +171,7 @@ static void slirp_cleanup(void)
 static void slirp_state_save(QEMUFile *f, void *opaque);
 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
 
-void slirp_init(int restricted, char *special_ip)
+void slirp_init(int restricted, const char *special_ip)
 {
     //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
 

  parent reply	other threads:[~2009-05-08 10:42 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-08 10:34 [Qemu-devel] [PATCH 00/11] Networking fixes and slirp enhancements Jan Kiszka
2009-05-08 10:34 ` [Qemu-devel] [PATCH 04/11] net: Real fix for check_params users Jan Kiszka
2009-05-19  7:57   ` Mark McLoughlin
2009-05-19  9:34     ` Jan Kiszka
2009-05-19  9:57       ` Mark McLoughlin
2009-05-28 15:04   ` Mark McLoughlin
2009-05-28 15:05     ` [Qemu-devel] [PATCH 1/3] Revert "Fix output of uninitialized strings" Mark McLoughlin
2009-05-28 15:06       ` [Qemu-devel] [PATCH 2/3] net: Real fix for check_params users Mark McLoughlin
2009-05-28 15:06         ` [Qemu-devel] [PATCH 3/3] net: fix error reporting for some net parameter checks Mark McLoughlin
2009-05-28 15:56           ` [Qemu-devel] " Jan Kiszka
2009-05-28 15:22     ` [Qemu-devel] [PATCH 04/11] net: Real fix for check_params users Kevin Wolf
2009-05-08 10:34 ` [Qemu-devel] [PATCH 01/11] net: Don't deliver to disabled interfaces in qemu_sendv_packet Jan Kiszka
2009-05-08 15:20   ` Mark McLoughlin
2009-05-08 22:27     ` [Qemu-devel] "FLOSS bounty" ( FB )for running QEMU on SheevaPlug AGSCalabrese
2009-05-08 22:47       ` Marek Vasut
2009-05-08 22:58       ` Paul Brook
2009-05-08 10:34 ` [Qemu-devel] [PATCH 02/11] net: Fix and improved ordered packet delivery Jan Kiszka
2009-05-08 15:24   ` Mark McLoughlin
2009-05-08 10:34 ` [Qemu-devel] [PATCH 03/11] slirp: Avoid zombie processes after fork_exec Jan Kiszka
2009-05-08 10:34 ` Jan Kiszka [this message]
2009-05-08 10:34 ` [Qemu-devel] [PATCH 10/11] slirp: Rework external configuration interface Jan Kiszka
2009-05-28 15:07   ` Mark McLoughlin
2009-05-28 15:55     ` Jan Kiszka
2009-05-28 17:23       ` Mark McLoughlin
2009-05-28 20:41         ` Jan Kiszka
2009-05-08 10:34 ` [Qemu-devel] [PATCH 07/11] Introduce get_next_param_value Jan Kiszka
2009-05-08 10:34 ` [Qemu-devel] [PATCH 05/11] net: Improve parameter error reporting Jan Kiszka
2009-05-08 10:34 ` [Qemu-devel] [PATCH 08/11] slirp: Move smb, redir, tftp and bootp parameters and -net channel Jan Kiszka
2009-05-28 15:07   ` Mark McLoughlin
2009-05-28 15:52     ` Jan Kiszka
2009-05-29 11:42     ` Paul Brook
2009-05-29 14:19       ` Jan Kiszka
2009-05-29 15:36         ` Paul Brook
2009-05-08 10:34 ` [Qemu-devel] [PATCH 09/11] slirp: Rework internal configuration Jan Kiszka
2009-05-08 10:34 ` [Qemu-devel] [PATCH 11/11] slirp: Bind support for host forwarding rules Jan Kiszka
2009-05-08 16:25 ` [Qemu-devel] [PATCH 00/11] Networking fixes and slirp enhancements Anthony Liguori
2009-05-08 17:01   ` Jan Kiszka
2009-05-09  7:41     ` [Qemu-devel] [PATCH 08/11 v2] slirp: Move smb, redir, tftp and bootp parameters and -net channel Jan Kiszka

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=20090508103418.6080.24111.stgit@mchn012c.ww002.siemens.net \
    --to=jan.kiszka@siemens.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.