Skip to contentSkip to conversation

Slirp Fixes and Enhancements - Reloaded

PATCH In review Jan Kiszka 3 participants 46 messages Jun 24, 2009 Kbuild

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover Slirp Fixes and Enhancements - Reloaded

The foundation of this series are the patches from my previous posting that didn't make it into upstream. But while reworking them, the series grow far beyond the previous approach, adding lots of fixes and cleanups to the slirp stack and its interfaces.

One major contribution is that the virtual IP addresses used inside slirp VLANs are now (almost) freely configurable. This enables sophisticated virtual network setups, specifically with guests that depends on specific addresses.

In this second step, the series also comes with support for multiple slirp instances per VM. Further contributions are
- proper dynamic slirp cleanup during runtime
- 'info usernet' command to dump slirp connection states
- reworked monitor commands hostfwd_add/remove (formerly known as host_net_redir)
- save/restore fixes for slirp
- first larger code cleanup (more work required...)
- various small fixes

The patches have been successfully tested against basic use cases:
connections to/from the guests, redirections, tftp, multi-instance, save/restore. Moreover, they also passed successfull build tests against mingw32.

Regarding the preferred flow: Mark, will you let this go through your network tree first? Currently the series is based on upstream/master, but I can rebase if required.

Find this series also at git://git.kiszka.org/qemu.git queues/net

Jan Kiszka (41):
slirp: Drop redundant lines from udp_input
slirp: Refactor tcp_ctl
Revert "User networking: Show active connections" Introduce get_next_param_value
slirp: Move smb, redir, tftp and bootp parameters and -net channel
slirp: Rework internal configuration
slirp: Rework external configuration interface
slirp: Fix port comparision in slirp_remove_hostfwd
slirp: Rework monitor commands for host forwarding
slirp: Bind support for host forwarding rules
slirp: Prepare for persistent socket state flags
slirp: Explicitely mark host-forwarding sockets
slirp: Do not allow to remove non-hostfwd sockets
slirp: Mark sockets of incoming TCP connections
slirp: Add info usernet for dumping connection states
slirp: Drop dead code
slirp: Drop statistic code
slirp: Cleanup and basic reanimation of debug code
slirp: Drop unused icmp_var.h
slirp: tftp: Cleanup tftp_prefix check
slirp: tftp: Clean up tftp_send_error
slirp: tftp: Refactor tftp_handle_rrq
slirp: tftp: Rework filename handling
slirp: Factor out one-time initialization
slirp: Make IP packet ID consistent
slirp: Clean up updtime
slirp: Kill slirp_is_inited
slirp: Drop redundant checks from slirp_output
slirp: Clean up timeout handling around slirp_select_fill/poll
slirp: Drop link_up checks from if_output and slirp_socket_can_recv
slirp: Factor out internal state structure
slirp: Use internal state in interface
slirp: Allocate/free stack instance dynamically
slirp: Enable multiple instances
slirp: Save/restore bootp client states
slirp: Use shell to erase smb directory
slirp: Improve error handling in slirp_smb
slirp: Enable multi-instance support for the smb service
net: Provide VLAN client lookup helper
slirp: Make hostfwd_add/remove multi-instance-aware
slirp: Basic VLAN client info_str

 Makefile           |    2 +-
 hw/xen_nic.c       |    2 +-
 monitor.c          |    4 +-
 net.c              |  745 ++++++++++++++++++++++++++++++++++++----------------
 net.h              |   14 +-
 qemu-monitor.hx    |   14 +-
 qemu-options.hx    |  226 +++++++++-------
 slirp/bootp.c      |   92 +++----
 slirp/bootp.h      |    7 +
 slirp/ctl.h        |    7 -
 slirp/debug.c      |  393 ---------------------------
 slirp/debug.h      |   17 +-
 slirp/icmp_var.h   |   67 -----
 slirp/if.c         |  135 ++--------
 slirp/if.h         |   40 +---
 slirp/ip.h         |   40 ---
 slirp/ip_icmp.c    |   42 +--
 slirp/ip_input.c   |  120 +++------
 slirp/ip_output.c  |   37 +---
 slirp/libslirp.h   |   72 +++---
 slirp/main.h       |   10 +-
 slirp/mbuf.c       |   39 +--
 slirp/mbuf.h       |   23 +--
 slirp/misc.c       |  625 ++++++-------------------------------------
 slirp/misc.h       |   16 +-
 slirp/sbuf.c       |    8 -
 slirp/slirp.c      |  500 +++++++++++++++++++----------------
 slirp/slirp.h      |   89 +++++--
 slirp/socket.c     |  128 +++------
 slirp/socket.h     |   15 +-
 slirp/tcp.h        |    7 -
 slirp/tcp_input.c  |  303 +++-------------------
 slirp/tcp_output.c |  115 +--------
 slirp/tcp_subr.c   |  559 ++++++---------------------------------
 slirp/tcp_timer.c  |   46 +---
 slirp/tcp_timer.h  |   10 +-
 slirp/tcp_var.h    |   71 +-----
 slirp/tftp.c       |  168 +++++-------
 slirp/tftp.h       |   10 +
 slirp/udp.c        |  114 ++++-----
 slirp/udp.h        |   28 +--
 sysemu.h           |    2 +
 vl.c               |   48 ++--
 43 files changed, 1624 insertions(+), 3386 deletions(-)
 delete mode 100644 slirp/ctl.h
 delete mode 100644 slirp/debug.c
 delete mode 100644 slirp/icmp_var.h

--
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux

author
#1 lore raw

PATCH 1/41 slirp: Drop redundant lines from udp_input

The socket faddr/fport is already updated a few lines below, so these are completely redundant.

---

 slirp/udp.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
slirp/udp.c +0 -2
index 11e78cd..ba9d5c9 100644
@@ -173,8 +173,6 @@ udp_input(register struct mbuf *m, int iphlen)
 		for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
 			if (tmp->so_lport == uh->uh_sport &&
 			    tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
-				tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
-				tmp->so_fport = uh->uh_dport;
 				so = tmp;
 				break;
 			}
author
#2 lore raw

PATCH 2/41 slirp: Refactor tcp_ctl

In reply to Jan Kiszka

---

 slirp/tcp_subr.c |  109 +++++++++++++++---------------------------------------
 1 files changed, 30 insertions(+), 79 deletions(-)
slirp/tcp_subr.c +30 -79
index 9d020a6..447a27c 100644
@@ -1228,84 +1228,35 @@ do_prompt:
  * Return 0 if this connections is to be closed, 1 otherwise,
  * return 2 if this is a command-line connection
  */
-int
-tcp_ctl(struct socket *so)
+int tcp_ctl(struct socket *so)
 {
-	struct sbuf *sb = &so->so_snd;
-	int command;
- 	struct ex_list *ex_ptr;
-	int do_pty;
-        //	struct socket *tmpso;
-
-	DEBUG_CALL("tcp_ctl");
-	DEBUG_ARG("so = %lx", (long )so);
-
-#if 0
-	/*
-	 * Check if they're authorised
-	 */
-	if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
-		sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
-		sb->sb_wptr += sb->sb_cc;
-		return 0;
-	}
-#endif
-	command = (ntohl(so->so_faddr.s_addr) & 0xff);
-
-	switch(command) {
-	default: /* Check for exec's */
-
-		/*
-		 * Check if it's pty_exec
-		 */
-		for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
-			if (ex_ptr->ex_fport == so->so_fport &&
-			    command == ex_ptr->ex_addr) {
-				if (ex_ptr->ex_pty == 3) {
-					so->s = -1;
-					so->extra = (void *)ex_ptr->ex_exec;
-					return 1;
-				}
-				do_pty = ex_ptr->ex_pty;
-				goto do_exec;
-			}
-		}
-
-		/*
-		 * Nothing bound..
-		 */
-		/* tcp_fconnect(so); */
-
-		/* FALLTHROUGH */
-	case CTL_ALIAS:
-          sb->sb_cc = snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
-                               "Error: No application configured.\r\n");
-	  sb->sb_wptr += sb->sb_cc;
-	  return(0);
-
-	do_exec:
-		DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
-		return(fork_exec(so, ex_ptr->ex_exec, do_pty));
-
-#if 0
-	case CTL_CMD:
-	   for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
-	     if (tmpso->so_emu == EMU_CTL &&
-		 !(tmpso->so_tcpcb?
-		   (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
-		   :0)) {
-	       /* Ooops, control connection already active */
-	       sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
-	       sb->sb_wptr += sb->sb_cc;
-	       return 0;
-	     }
-	   }
-	   so->so_emu = EMU_CTL;
-	   ctl_password_ok = 0;
-	   sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
-	   sb->sb_wptr += sb->sb_cc;
-	   do_echo=-1;
-	   return(2);
-#endif
-	}
+    int command = (ntohl(so->so_faddr.s_addr) & 0xff);
+    struct sbuf *sb = &so->so_snd;
+    struct ex_list *ex_ptr;
+    int do_pty;
+
+    DEBUG_CALL("tcp_ctl");
+    DEBUG_ARG("so = %lx", (long )so);
+
+    if (command != CTL_ALIAS) {
+        /* Check if it's pty_exec */
+        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+            if (ex_ptr->ex_fport == so->so_fport &&
+                command == ex_ptr->ex_addr) {
+                if (ex_ptr->ex_pty == 3) {
+                    so->s = -1;
+                    so->extra = (void *)ex_ptr->ex_exec;
+                    return 1;
+                }
+                do_pty = ex_ptr->ex_pty;
+                DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
+                return fork_exec(so, ex_ptr->ex_exec, do_pty);
+            }
+        }
+    }
+    sb->sb_cc =
+        snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
+                 "Error: No application configured.\r\n");
+    sb->sb_wptr += sb->sb_cc;
+    return 0;
 }
author
#3 lore raw

PATCH 3/41 Revert "User networking: Show active connections"

In reply to Jan Kiszka

This reverts commit 1c6ed9f3379faac83da0ed3e95cbd49003ac0dd1.

It's redundant to slirp statistics, which are going to be split up / reworked later on.

Conflicts:

monitor.c
net.c

---

 net.c            |   44 --------------------------------------------
 qemu-monitor.hx  |    3 +--
 slirp/libslirp.h |    4 ----
 slirp/slirp.c    |   24 ------------------------
 4 files changed, 1 insertions(+), 74 deletions(-)
net.c +0 -44
index 55f70f2..87a6e76 100644
@@ -756,45 +756,6 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
     return 0;
 }
 
-static void net_slirp_redir_print(void *opaque, int is_udp,
-                                  struct in_addr *laddr, u_int lport,
-                                  struct in_addr *faddr, u_int fport)
-{
-    Monitor *mon = (Monitor *)opaque;
-    uint32_t h_addr;
-    uint32_t g_addr;
-    char buf[16];
-
-    h_addr = ntohl(faddr->s_addr);
-    g_addr = ntohl(laddr->s_addr);
-
-    monitor_printf(mon, "  %s |", is_udp ? "udp" : "tcp" );
-    snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
-                                     (h_addr >> 16) & 0xff,
-                                     (h_addr >> 8) & 0xff,
-                                     (h_addr) & 0xff);
-    monitor_printf(mon, " %15s |", buf);
-    monitor_printf(mon, " %5d |", fport);
-
-    snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
-                                     (g_addr >> 16) & 0xff,
-                                     (g_addr >> 8) & 0xff,
-                                     (g_addr) & 0xff);
-    monitor_printf(mon, " %15s |", buf);
-    monitor_printf(mon, " %5d\n", lport);
-
-}
-
-static void net_slirp_redir_list(Monitor *mon)
-{
-    if (!mon)
-        return;
-
-    monitor_printf(mon, " Prot |    Host Addr    | HPort |    Guest Addr   | GPort\n");
-    monitor_printf(mon, "      |                 |       |                 |      \n");
-    slirp_redir_loop(net_slirp_redir_print, mon);
-}
-
 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
 {
     int host_port;
@@ -904,11 +865,6 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
         return;
     }
 
-    if (!strcmp(redir_str, "list")) {
-        net_slirp_redir_list(mon);
-        return;
-    }
-
     slirp_redirection(mon, redir_str);
 }
 
qemu-monitor.hx +1 -2
index fa48527..090255b 100644
@@ -538,8 +538,7 @@ ETEXI
 #ifdef CONFIG_SLIRP
     { "host_net_redir", "ss?", net_slirp_redir,
       "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
-      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
-      "host_net_redir list -- show all redirections" },
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
 #endif
 STEXI
 @item host_net_redir
slirp/libslirp.h +0 -4
index d0df24b..c04f3a2 100644
@@ -18,10 +18,6 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
-void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
-                                  struct in_addr *laddr, u_int lport,              
-                                  struct in_addr *faddr, u_int fport),
-                     void *opaque);
 int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
slirp/slirp.c +0 -24
index 30d4ee2..b3db742 100644
@@ -734,30 +734,6 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
     }
 }
 
-static void _slirp_redir_loop(void (*func)(void *opaque, int is_udp,
-                                           struct in_addr *laddr, u_int lport,
-                                           struct in_addr *faddr, u_int fport),
-                              void *opaque, int is_udp)
-{
-    struct socket *head = (is_udp ? &udb : &tcb);
-    struct socket *so;
-
-    for (so = head->so_next; so != head; so = so->so_next) {
-        func(opaque, is_udp,
-             &so->so_laddr, ntohs(so->so_lport),
-             &so->so_faddr, ntohs(so->so_fport));
-    }
-}
-
-void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
-                                  struct in_addr *laddr, u_int lport,
-                                  struct in_addr *faddr, u_int fport),
-                     void *opaque)
-{
-    _slirp_redir_loop(func, opaque, 0);
-    _slirp_redir_loop(func, opaque, 1);
-}
-
 /* Unlistens a redirection
  *
  * Return value: number of redirs removed */
author
#4 lore raw

PATCH 4/41 Introduce get_next_param_value

In reply to Jan Kiszka

In order to parse multiple instances of the same param=value pair, introduce get_next_param_value which can pass back to string parsing position after reading a parameter value.

---

 sysemu.h |    2 ++
 vl.c     |   17 +++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
sysemu.h +2 -0
index d77de09..fafb231 100644
@@ -273,6 +273,8 @@ void usb_info(Monitor *mon);
 
 int get_param_value(char *buf, int buf_size,
                     const char *tag, const char *str);
+int get_next_param_value(char *buf, int buf_size,
+                         const char *tag, const char **pstr);
 int check_params(char *buf, int buf_size,
                  const char * const *params, const char *str);
 
vl.c +13 -4
index 60a00e1..77e4779 100644
@@ -1812,20 +1812,23 @@ static int socket_init(void)
 }
 #endif
 
-int get_param_value(char *buf, int buf_size,
-                    const char *tag, const char *str)
+int get_next_param_value(char *buf, int buf_size,
+                         const char *tag, const char **pstr)
 {
     const char *p;
     char option[128];
 
-    p = str;
+    p = *pstr;
     for(;;) {
         p = get_opt_name(option, sizeof(option), p, '=');
         if (*p != '=')
             break;
         p++;
         if (!strcmp(tag, option)) {
-            (void)get_opt_value(buf, buf_size, p);
+            *pstr = get_opt_value(buf, buf_size, p);
+            if (**pstr == ',') {
+                (*pstr)++;
+            }
             return strlen(buf);
         } else {
             p = get_opt_value(NULL, 0, p);
@@ -1837,6 +1840,12 @@ int get_param_value(char *buf, int buf_size,
     return 0;
 }
 
+int get_param_value(char *buf, int buf_size,
+                    const char *tag, const char *str)
+{
+    return get_next_param_value(buf, buf_size, tag, &str);
+}
+
 int check_params(char *buf, int buf_size,
                  const char * const *params, const char *str)
 {
author
#5 lore raw

PATCH 5/41 slirp: Move smb, redir, tftp and bootp parameters and -net channel

In reply to Jan Kiszka

So far a couple of slirp-related parameters were expressed via stand-alone command line options. This it inconsistent and unintuitive. Moreover, it prevents both dynamically reconfigured (host_net_add/ delete) and multi-instance slirp.

This patch refactors the configuration by turning -smb, -redir, -tftp and -bootp as well as -net channel into options of "-net user". The old stand-alone command line options are still processed, but no longer advertised. This allows smooth migration of management applications to to the new syntax and also the extension of that syntax later in this series.

---

 net.c            |  175 +++++++++++++++++++++++++++++++++------------
 net.h            |    3 +
 qemu-options.hx  |  209 +++++++++++++++++++++++++++++-------------------------
 slirp/bootp.c    |    2 -
 slirp/libslirp.h |    5 +
 slirp/main.h     |    2 +
 slirp/slirp.c    |   14 +++-
 slirp/tftp.c     |    2 -
 vl.c             |    6 +-
 9 files changed, 265 insertions(+), 153 deletions(-)
net.c +129 -46
index 87a6e76..f7596d2 100644
@@ -669,22 +669,28 @@ static void config_error(Monitor *mon, const char *fmt, ...)
 
 /* slirp network adapter */
 
+#define SLIRP_CFG_REDIR 1
+
 struct slirp_config_str {
     struct slirp_config_str *next;
-    const char *str;
+    int flags;
+    char str[1024];
 };
 
 static int slirp_inited;
-static struct slirp_config_str *slirp_redirs;
-#ifndef _WIN32
-static const char *slirp_smb_export;
-#endif
+static struct slirp_config_str *slirp_configs;
+const char *legacy_tftp_prefix;
+const char *legacy_bootp_filename;
 static VLANClientState *slirp_vc;
 
+static void slirp_redirection(Monitor *mon, const char *redir_str);
+static void vmchannel_init(Monitor *mon, const char *config_str);
+
 #ifndef _WIN32
+static const char *legacy_smb_export;
+
 static void slirp_smb(const char *exported_dir);
 #endif
-static void slirp_redirection(Monitor *mon, const char *redir_str);
 
 int slirp_can_output(void)
 {
@@ -724,27 +730,42 @@ static void net_slirp_cleanup(VLANClientState *vc)
     slirp_in_use = 0;
 }
 
-static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
-                          int restricted, const char *ip)
+static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
+                          const char *name, int restricted, const char *ip,
+                          const char *tftp_export, const char *bootfile,
+                          const char *smb_export)
 {
     if (slirp_in_use) {
         /* slirp only supports a single instance so far */
         return -1;
     }
     if (!slirp_inited) {
+        if (!tftp_export) {
+            tftp_export = legacy_tftp_prefix;
+        }
+        if (!bootfile) {
+            bootfile = legacy_bootp_filename;
+        }
         slirp_inited = 1;
-        slirp_init(restricted, ip);
+        slirp_init(restricted, ip, tftp_export, bootfile);
 
-        while (slirp_redirs) {
-            struct slirp_config_str *config = slirp_redirs;
+        while (slirp_configs) {
+            struct slirp_config_str *config = slirp_configs;
 
-            slirp_redirection(NULL, config->str);
-            slirp_redirs = config->next;
+            if (config->flags & SLIRP_CFG_REDIR) {
+                slirp_redirection(mon, config->str);
+            } else {
+                vmchannel_init(mon, config->str);
+            }
+            slirp_configs = config->next;
             qemu_free(config);
         }
 #ifndef _WIN32
-        if (slirp_smb_export) {
-            slirp_smb(slirp_smb_export);
+        if (!smb_export) {
+            smb_export = legacy_smb_export;
+        }
+        if (smb_export) {
+            slirp_smb(smb_export);
         }
 #endif
     }
@@ -853,9 +874,10 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
             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;
+            pstrcpy(config->str, sizeof(config->str), redir_str);
+            config->flags = SLIRP_CFG_REDIR;
+            config->next = slirp_configs;
+            slirp_configs = config;
         }
         return;
     }
@@ -952,14 +974,14 @@ static void slirp_smb(const char *exported_dir)
     slirp_add_exec(0, smb_cmdline, 4, 139);
 }
 
-/* automatic user mode samba server configuration */
+/* automatic user mode samba server configuration (legacy interface) */
 void net_slirp_smb(const char *exported_dir)
 {
-    if (slirp_smb_export) {
+    if (legacy_smb_export) {
         fprintf(stderr, "-smb given twice\n");
         exit(1);
     }
-    slirp_smb_export = exported_dir;
+    legacy_smb_export = exported_dir;
     if (slirp_inited) {
         slirp_smb(exported_dir);
     }
@@ -989,6 +1011,36 @@ static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
     slirp_socket_recv(4, vmc->port, buf, size);
 }
 
+static void vmchannel_init(Monitor *mon, const char *config_str)
+{
+    struct VMChannel *vmc;
+    char *devname;
+    char name[20];
+    int port;
+
+    port = strtol(config_str, &devname, 10);
+    if (port < 1 || port > 65535 || *devname != ':') {
+        config_error(mon, "invalid vmchannel port number\n");
+        return;
+    }
+    devname++;
+
+    vmc = qemu_malloc(sizeof(struct VMChannel));
+    snprintf(name, sizeof(name), "vmchannel%d", port);
+    vmc->hd = qemu_chr_open(name, devname, NULL);
+    if (!vmc->hd) {
+        config_error(mon, "could not open vmchannel device '%s'\n", devname);
+        qemu_free(vmc);
+        return;
+    }
+    vmc->port = port;
+
+    slirp_add_exec(3, vmc->hd, 4, port);
+    qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
+                          NULL, vmc);
+    return;
+}
+
 #endif /* CONFIG_SLIRP */
 
 #if !defined(_WIN32)
@@ -2184,10 +2236,16 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
 #ifdef CONFIG_SLIRP
     if (!strcmp(device, "user")) {
         static const char * const slirp_params[] = {
-            "vlan", "name", "hostname", "restrict", "ip", NULL
+            "vlan", "name", "hostname", "restrict", "ip", "tftp", "bootfile",
+            "smb", "redir", "channel", NULL
         };
+        struct slirp_config_str *config;
+        char *tftp_export = NULL;
+        char *bootfile = NULL;
+        char *smb_export = NULL;
         int restricted = 0;
         char *ip = NULL;
+        const char *q;
 
         if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
             config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
@@ -2203,34 +2261,59 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         if (get_param_value(buf, sizeof(buf), "ip", p)) {
             ip = qemu_strdup(buf);
         }
+        if (get_param_value(buf, sizeof(buf), "tftp", p)) {
+            tftp_export = qemu_strdup(buf);
+        }
+        if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
+            bootfile = qemu_strdup(buf);
+        }
+        if (get_param_value(buf, sizeof(buf), "smb", p)) {
+            smb_export = qemu_strdup(buf);
+        }
+        q = p;
+        while (1) {
+            config = qemu_malloc(sizeof(*config));
+            if (!get_next_param_value(config->str, sizeof(config->str),
+                                      "redir", &q)) {
+                break;
+            }
+            config->flags = SLIRP_CFG_REDIR;
+            config->next = slirp_configs;
+            slirp_configs = config;
+            config = NULL;
+        }
+        q = p;
+        while (1) {
+            config = qemu_malloc(sizeof(*config));
+            if (!get_next_param_value(config->str, sizeof(config->str),
+                                      "channel", &q)) {
+                break;
+            }
+            config->flags = 0;
+            config->next = slirp_configs;
+            slirp_configs = config;
+            config = NULL;
+        }
+        qemu_free(config);
         vlan->nb_host_devs++;
-        ret = net_slirp_init(vlan, device, name, restricted, ip);
+        ret = net_slirp_init(mon, vlan, device, name, restricted, ip,
+                             tftp_export, bootfile, smb_export);
         qemu_free(ip);
+        qemu_free(tftp_export);
+        qemu_free(bootfile);
+        qemu_free(smb_export);
     } else if (!strcmp(device, "channel")) {
-        long port;
-        char name[20], *devname;
-        struct VMChannel *vmc;
-
-        port = strtol(p, &devname, 10);
-        devname++;
-        if (port < 1 || port > 65535) {
-            config_error(mon, "vmchannel wrong port number\n");
-            ret = -1;
-            goto out;
-        }
-        vmc = malloc(sizeof(struct VMChannel));
-        snprintf(name, 20, "vmchannel%ld", port);
-        vmc->hd = qemu_chr_open(name, devname, NULL);
-        if (!vmc->hd) {
-            config_error(mon, "could not open vmchannel device '%s'\n",
-                         devname);
-            ret = -1;
-            goto out;
+        if (!slirp_inited) {
+            struct slirp_config_str *config;
+
+            config = qemu_malloc(sizeof(*config));
+            pstrcpy(config->str, sizeof(config->str), p);
+            config->flags = 0;
+            config->next = slirp_configs;
+            slirp_configs = config;
+        } else {
+            vmchannel_init(mon, p);
         }
-        vmc->port = port;
-        slirp_add_exec(3, vmc->hd, 4, port);
-        qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
-                NULL, vmc);
         ret = 0;
     } else
 #endif
net.h +3 -0
index a1c4d2c..e705c87 100644
@@ -121,6 +121,9 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
 void net_checksum_calculate(uint8_t *data, int length);
 
 /* from net.c */
+extern const char *legacy_tftp_prefix;
+extern const char *legacy_bootp_filename;
+
 int net_client_init(Monitor *mon, const char *device, const char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
qemu-options.hx +111 -98

233 lines not on this page · raw message

slirp/bootp.c +1 -1
index 4e0082d..a2fd734 100644
@@ -38,7 +38,7 @@ typedef struct {
 
 static BOOTPClient bootp_clients[NB_ADDR];
 
-const char *bootp_filename;
+char *bootp_filename;
 
 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
slirp/libslirp.h +2 -3
index c04f3a2..7dee34b 100644
@@ -5,7 +5,8 @@
 extern "C" {
 #endif
 
-void slirp_init(int restricted, const char *special_ip);
+void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
+                const char *bootfile);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
@@ -24,9 +25,7 @@ int slirp_redir(int is_udp, int host_port,
 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
                    int guest_port);
 
-extern const char *tftp_prefix;
 extern char slirp_hostname[33];
-extern const char *bootp_filename;
 
 void slirp_stats(void);
 void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
slirp/main.h +2 -0
index ed51385..89e722f 100644
@@ -46,6 +46,8 @@ extern int tcp_keepintvl;
 extern uint8_t client_ethaddr[6];
 extern const char *slirp_special_ip;
 extern int slirp_restrict;
+extern char *tftp_prefix;
+extern char *bootp_filename;
 
 #define PROTO_SLIP 0x1
 #ifdef USE_PPP
slirp/slirp.c +13 -1
index b3db742..b0a092c 100644
@@ -171,7 +171,8 @@ 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, const char *special_ip)
+void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
+                const char *bootfile)
 {
     //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
 
@@ -203,6 +204,17 @@ void slirp_init(int restricted, const char *special_ip)
     if (special_ip)
         slirp_special_ip = special_ip;
 
+    qemu_free(tftp_prefix);
+    tftp_prefix = NULL;
+    if (tftp_path) {
+        tftp_prefix = qemu_strdup(tftp_path);
+    }
+    qemu_free(bootp_filename);
+    bootp_filename = NULL;
+    if (bootfile) {
+        bootp_filename = qemu_strdup(bootfile);
+    }
+
     inet_aton(slirp_special_ip, &special_addr);
     alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
     getouraddr();
slirp/tftp.c +1 -1
index 4ad5504..7cb095f 100644
@@ -37,7 +37,7 @@ struct tftp_session {
 
 static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
 
-const char *tftp_prefix;
+char *tftp_prefix;
 
 static void tftp_session_update(struct tftp_session *spt)
 {
vl.c +3 -3
index 77e4779..ae260b1 100644
@@ -5308,14 +5308,14 @@ int main(int argc, char **argv, char **envp)
                 break;
 #ifdef CONFIG_SLIRP
             case QEMU_OPTION_tftp:
-		tftp_prefix = optarg;
+                legacy_tftp_prefix = optarg;
                 break;
             case QEMU_OPTION_bootp:
-                bootp_filename = optarg;
+                legacy_bootp_filename = optarg;
                 break;
 #ifndef _WIN32
             case QEMU_OPTION_smb:
-		net_slirp_smb(optarg);
+                net_slirp_smb(optarg);
                 break;
 #endif
             case QEMU_OPTION_redir:
author
#6 lore raw

PATCH 6/41 slirp: Rework internal configuration

In reply to Jan Kiszka

The user mode IP stack is currently only minimally configurable /wrt to its virtual IP addresses. This is unfortunate if some guest has a fixed idea of which IP addresses to use.

Therefore this patch prepares the stack for fully configurable IP addresses and masks. The user interface and default addresses remain untouched in this step, they will be enhanced in the following patch.

---

 slirp/bootp.c     |   26 ++++------
 slirp/ctl.h       |    7 ---
 slirp/ip_icmp.c   |   15 ++----
 slirp/ip_input.c  |    9 ++-
 slirp/main.h      |    9 ++-
 slirp/misc.c      |    9 ++-
 slirp/misc.h      |    4 +
 slirp/slirp.c     |  142 +++++++++++++++++++++++++++++++----------------------
 slirp/slirp.h     |    1 
 slirp/socket.c    |   13 ++---
 slirp/tcp_input.c |   16 +++---
 slirp/tcp_subr.c  |   17 ++----
 slirp/udp.c       |   23 +++++----
 13 files changed, 149 insertions(+), 142 deletions(-)
 delete mode 100644 slirp/ctl.h
slirp/bootp.c +10 -16
index a2fd734..97c2811 100644
@@ -27,8 +27,6 @@
 
 #define NB_ADDR 16
 
-#define START_ADDR 15
-
 #define LEASE_TIME (24 * 3600)
 
 typedef struct {
@@ -64,7 +62,7 @@ static BOOTPClient *get_new_addr(struct in_addr *paddr,
  found:
     bc = &bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
+    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
@@ -72,12 +70,12 @@ static BOOTPClient *request_addr(const struct in_addr *paddr,
                                  const uint8_t *macaddr)
 {
     uint32_t req_addr = ntohl(paddr->s_addr);
-    uint32_t spec_addr = ntohl(special_addr.s_addr);
+    uint32_t dhcp_addr = ntohl(vdhcp_startaddr.s_addr);
     BOOTPClient *bc;
 
-    if (req_addr >= (spec_addr | START_ADDR) &&
-        req_addr < (spec_addr | (NB_ADDR + START_ADDR))) {
-        bc = &bootp_clients[(req_addr & 0xff) - START_ADDR];
+    if (req_addr >= dhcp_addr &&
+        req_addr < (dhcp_addr + NB_ADDR)) {
+        bc = &bootp_clients[req_addr - dhcp_addr];
         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
             bc->allocated = 1;
             return bc;
@@ -99,7 +97,7 @@ static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
  found:
     bc = &bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
+    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
@@ -156,7 +154,6 @@ static void bootp_reply(const struct bootp_t *bp)
     struct mbuf *m;
     struct bootp_t *rbp;
     struct sockaddr_in saddr, daddr;
-    struct in_addr dns_addr;
     const struct in_addr *preq_addr;
     int dhcp_msg_type, val;
     uint8_t *q;
@@ -218,7 +215,7 @@ static void bootp_reply(const struct bootp_t *bp)
         }
     }
 
-    saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
+    saddr.sin_addr = vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
     daddr.sin_port = htons(BOOTP_CLIENT);
@@ -262,10 +259,8 @@ static void bootp_reply(const struct bootp_t *bp)
 
         *q++ = RFC1533_NETMASK;
         *q++ = 4;
-        *q++ = 0xff;
-        *q++ = 0xff;
-        *q++ = 0xff;
-        *q++ = 0x00;
+        memcpy(q, &vnetwork_mask, 4);
+        q += 4;
 
         if (!slirp_restrict) {
             *q++ = RFC1533_GATEWAY;
@@ -275,8 +270,7 @@ static void bootp_reply(const struct bootp_t *bp)
 
             *q++ = RFC1533_DNS;
             *q++ = 4;
-            dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
-            memcpy(q, &dns_addr, 4);
+            memcpy(q, &vnameserver_addr, 4);
             q += 4;
         }
 
slirp/ctl.h deleted +0 -7
deleted file mode 100644
index 4a8576d..0000000
@@ -1,7 +0,0 @@
-#define CTL_CMD		0
-#define CTL_EXEC	1
-#define CTL_ALIAS	2
-#define CTL_DNS		3
-
-#define CTL_SPECIAL	"10.0.2.0"
-#define CTL_LOCAL	"10.0.2.15"
slirp/ip_icmp.c +6 -9
index 61dcaf8..6e93ee3 100644
@@ -110,7 +110,7 @@ icmp_input(struct mbuf *m, int hlen)
   case ICMP_ECHO:
     icp->icmp_type = ICMP_ECHOREPLY;
     ip->ip_len += hlen;	             /* since ip_input subtracts this */
-    if (ip->ip_dst.s_addr == alias_addr.s_addr) {
+    if (ip->ip_dst.s_addr == vhost_addr.s_addr) {
       icmp_reflect(m);
     } else {
       struct socket *so;
@@ -134,16 +134,13 @@ icmp_input(struct mbuf *m, int hlen)
 
       /* Send the packet */
       addr.sin_family = AF_INET;
-      if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
+      if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
+          vnetwork_addr.s_addr) {
 	/* It's an alias */
-	switch(ntohl(so->so_faddr.s_addr) & 0xff) {
-	case CTL_DNS:
+	if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
 	  addr.sin_addr = dns_addr;
-	  break;
-	case CTL_ALIAS:
-	default:
+	} else {
 	  addr.sin_addr = loopback_addr;
-	  break;
 	}
       } else {
 	addr.sin_addr = so->so_faddr;
@@ -302,7 +299,7 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   ip->ip_ttl = MAXTTL;
   ip->ip_p = IPPROTO_ICMP;
   ip->ip_dst = ip->ip_src;    /* ip adresses */
-  ip->ip_src = alias_addr;
+  ip->ip_src = vhost_addr;
 
   (void ) ip_output((struct socket *)NULL, m);
 
slirp/ip_input.c +5 -4
index c37412e..7a3c88b 100644
@@ -134,18 +134,19 @@ ip_input(struct mbuf *m)
 	}
 
     if (slirp_restrict) {
-        if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
+        if ((ip->ip_dst.s_addr & vnetwork_mask.s_addr) ==
+            vnetwork_addr.s_addr) {
             if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
                 goto bad;
         } else {
-            int host = ntohl(ip->ip_dst.s_addr) & 0xff;
             struct ex_list *ex_ptr;
 
-            if (host == 0xff)
+            if ((ip->ip_dst.s_addr & ~vnetwork_mask.s_addr) ==
+                ~vnetwork_mask.s_addr)
                 goto bad;
 
             for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
-                if (ex_ptr->ex_addr == host)
+                if (ex_ptr->ex_addr.s_addr == ip->ip_dst.s_addr)
                     break;
 
             if (!ex_ptr)
slirp/main.h +5 -4
index 89e722f..edbb9cb 100644
@@ -32,9 +32,11 @@ extern char *slirp_tty;
 extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
-extern struct in_addr ctl_addr;
-extern struct in_addr special_addr;
-extern struct in_addr alias_addr;
+extern struct in_addr vnetwork_addr;
+extern struct in_addr vnetwork_mask;
+extern struct in_addr vhost_addr;
+extern struct in_addr vdhcp_startaddr;
+extern struct in_addr vnameserver_addr;
 extern struct in_addr our_addr;
 extern struct in_addr loopback_addr;
 extern struct in_addr dns_addr;
@@ -44,7 +46,6 @@ extern int towrite_max;
 extern int ppp_exit;
 extern int tcp_keepintvl;
 extern uint8_t client_ethaddr[6];
-extern const char *slirp_special_ip;
 extern int slirp_restrict;
 extern char *tftp_prefix;
 extern char *bootp_filename;
slirp/misc.c +5 -4
index 1391d49..069d8b1 100644
@@ -112,15 +112,16 @@ remque(void *a)
 /* #endif */
 
 
-int
-add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
+int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
+             struct in_addr addr, int port)
 {
 	struct ex_list *tmp_ptr;
 
 	/* First, check if the port is "bound" */
 	for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
-		if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
-		   return -1;
+		if (port == tmp_ptr->ex_fport &&
+		    addr.s_addr == tmp_ptr->ex_addr.s_addr)
+			return -1;
 	}
 
 	tmp_ptr = *ex_ptr;
slirp/misc.h +2 -2
index ab8e3a7..29d5749 100644
@@ -10,7 +10,7 @@
 
 struct ex_list {
 	int ex_pty;			/* Do we want a pty? */
-	int ex_addr;			/* The last byte of the address */
+	struct in_addr ex_addr;		/* Server address */
 	int ex_fport;                   /* Port to telnet to */
 	const char *ex_exec;            /* Command line of what to exec */
 	struct ex_list *ex_next;
@@ -74,7 +74,7 @@ void redir_x _P((u_int32_t, int, int, int));
 void getouraddr _P((void));
 void slirp_insque _P((void *, void *));
 void slirp_remque _P((void *));
-int add_exec _P((struct ex_list **, int, char *, int, int));
+int add_exec _P((struct ex_list **, int, char *, struct in_addr, int));
 int slirp_openpty _P((int *, int *));
 int fork_exec(struct socket *so, const char *ex, int do_pty);
 void snooze_hup _P((int));
slirp/slirp.c +83 -59

274 lines not on this page · raw message

slirp/slirp.h +0 -1
index 8309fe0..101d094 100644
@@ -214,7 +214,6 @@ int inet_aton _P((const char *cp, struct in_addr *ia));
 #include "if.h"
 #include "main.h"
 #include "misc.h"
-#include "ctl.h"
 #ifdef USE_PPP
 #include "ppp/pppd.h"
 #include "ppp/ppp.h"
slirp/socket.c +5 -8
index 82d026c..9f13f03 100644
@@ -555,16 +555,13 @@ sosendto(struct socket *so, struct mbuf *m)
 	DEBUG_ARG("m = %lx", (long)m);
 
         addr.sin_family = AF_INET;
-	if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
+	if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
+	    vnetwork_addr.s_addr) {
 	  /* It's an alias */
-	  switch(ntohl(so->so_faddr.s_addr) & 0xff) {
-	  case CTL_DNS:
+	  if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
 	    addr.sin_addr = dns_addr;
-	    break;
-	  case CTL_ALIAS:
-	  default:
+	  } else {
 	    addr.sin_addr = loopback_addr;
-	    break;
 	  }
 	} else
 	  addr.sin_addr = so->so_faddr;
@@ -652,7 +649,7 @@ solisten(u_int port, u_int32_t laddr, u_int lport, int flags)
 	getsockname(s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
 	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = alias_addr;
+	   so->so_faddr = vhost_addr;
 	else
 	   so->so_faddr = addr.sin_addr;
 
slirp/tcp_input.c +9 -7
index effedfc..ab0840d 100644
@@ -359,11 +359,12 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 
     if (slirp_restrict) {
-        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_fport == ti->ti_dport &&
-                    (ntohl(ti->ti_dst.s_addr) & 0xff) == ex_ptr->ex_addr)
+                ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
                 break;
-
+            }
+        }
         if (!ex_ptr)
             goto drop;
     }
@@ -639,9 +640,10 @@ findso:
 	   * If this is destined for the control address, then flag to
 	   * tcp_ctl once connected, otherwise connect
 	   */
-	  if ((so->so_faddr.s_addr&htonl(0xffffff00)) == special_addr.s_addr) {
-	    int lastbyte=ntohl(so->so_faddr.s_addr) & 0xff;
-	    if (lastbyte!=CTL_ALIAS && lastbyte!=CTL_DNS) {
+	  if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
+	      vnetwork_addr.s_addr) {
+	    if (so->so_faddr.s_addr != vhost_addr.s_addr &&
+		so->so_faddr.s_addr != vnameserver_addr.s_addr) {
 #if 0
 	      if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) {
 		/* Command or exec adress */
@@ -652,7 +654,7 @@ findso:
 		/* May be an add exec */
 		for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
 		  if(ex_ptr->ex_fport == so->so_fport &&
-		     lastbyte == ex_ptr->ex_addr) {
+		     so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
 		    so->so_state |= SS_CTL;
 		    break;
 		  }
slirp/tcp_subr.c +6 -11
index 447a27c..858d1ae 100644
@@ -384,16 +384,12 @@ int tcp_fconnect(struct socket *so)
     setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
 
     addr.sin_family = AF_INET;
-    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
+    if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) {
       /* It's an alias */
-      switch(ntohl(so->so_faddr.s_addr) & 0xff) {
-      case CTL_DNS:
+      if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
 	addr.sin_addr = dns_addr;
-	break;
-      case CTL_ALIAS:
-      default:
+      } else {
 	addr.sin_addr = loopback_addr;
-	break;
       }
     } else
       addr.sin_addr = so->so_faddr;
@@ -478,7 +474,7 @@ tcp_connect(struct socket *inso)
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
 	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = alias_addr;
+	   so->so_faddr = vhost_addr;
 
 	/* Close the accept() socket, set right state */
 	if (inso->so_state & SS_FACCEPTONCE) {
@@ -1230,7 +1226,6 @@ do_prompt:
  */
 int tcp_ctl(struct socket *so)
 {
-    int command = (ntohl(so->so_faddr.s_addr) & 0xff);
     struct sbuf *sb = &so->so_snd;
     struct ex_list *ex_ptr;
     int do_pty;
@@ -1238,11 +1233,11 @@ int tcp_ctl(struct socket *so)
     DEBUG_CALL("tcp_ctl");
     DEBUG_ARG("so = %lx", (long )so);
 
-    if (command != CTL_ALIAS) {
+    if (so->so_faddr.s_addr != vhost_addr.s_addr) {
         /* Check if it's pty_exec */
         for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_fport == so->so_fport &&
-                command == ex_ptr->ex_addr) {
+                so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
                 if (ex_ptr->ex_pty == 3) {
                     so->s = -1;
                     so->extra = (void *)ex_ptr->ex_exec;
slirp/udp.c +13 -10
index ba9d5c9..ff3a39f 100644
@@ -312,12 +312,14 @@ int udp_output(struct socket *so, struct mbuf *m,
     struct sockaddr_in saddr, daddr;
 
     saddr = *addr;
-    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
-        if ((so->so_faddr.s_addr & htonl(0x000000ff)) == htonl(0xff))
-            saddr.sin_addr.s_addr = alias_addr.s_addr;
-        else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
-                 (ntohl(so->so_faddr.s_addr) & 0xff) != CTL_ALIAS)
-            saddr.sin_addr.s_addr = so->so_faddr.s_addr;
+    if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) {
+        if ((so->so_faddr.s_addr & ~vnetwork_mask.s_addr) ==
+            ~vnetwork_mask.s_addr) {
+            saddr.sin_addr = vhost_addr;
+        } else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
+                   so->so_faddr.s_addr != vhost_addr.s_addr) {
+            saddr.sin_addr = so->so_faddr;
+        }
     }
     daddr.sin_addr = so->so_laddr;
     daddr.sin_port = so->so_lport;
@@ -652,11 +654,12 @@ udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
 
 	getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
-	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = alias_addr;
-	else
+	if (addr.sin_addr.s_addr == 0 ||
+	    addr.sin_addr.s_addr == loopback_addr.s_addr) {
+	   so->so_faddr = vhost_addr;
+	} else {
 	   so->so_faddr = addr.sin_addr;
-
+	}
 	so->so_lport = lport;
 	so->so_laddr.s_addr = laddr;
 	if (flags != SS_FACCEPTONCE)
author
#7 lore raw

PATCH 7/41 slirp: Rework external configuration interface

In reply to Jan Kiszka

With the internal IP configuration made more flexible, we can now enhance the user interface. This patch adds a number of new options to "-net user": net (address and mask), host, dhcpstart, dns and smbserver. It also renames "redir" to "hostfwd" and "channel" to "guestfwd" in order to (hopefully) clarify their meanings. The format of guestfwd is extended so that the user can define not only the port but also the virtual server's IP address the forwarding starts from.

---

 net.c            |  316 +++++++++++++++++++++++++++++++++++++++++-------------
 qemu-options.hx  |   52 ++++++---
 slirp/libslirp.h |   24 ++--
 slirp/main.h     |    1 
 slirp/slirp.c    |   52 ++++-----
 5 files changed, 311 insertions(+), 134 deletions(-)
net.c +239 -77
index f7596d2..a12177d 100644
@@ -669,12 +669,14 @@ static void config_error(Monitor *mon, const char *fmt, ...)
 
 /* slirp network adapter */
 
-#define SLIRP_CFG_REDIR 1
+#define SLIRP_CFG_HOSTFWD 1
+#define SLIRP_CFG_LEGACY  2
 
 struct slirp_config_str {
     struct slirp_config_str *next;
     int flags;
     char str[1024];
+    int legacy_format;
 };
 
 static int slirp_inited;
@@ -683,13 +685,14 @@ const char *legacy_tftp_prefix;
 const char *legacy_bootp_filename;
 static VLANClientState *slirp_vc;
 
-static void slirp_redirection(Monitor *mon, const char *redir_str);
-static void vmchannel_init(Monitor *mon, const char *config_str);
+static void slirp_hostfwd(Monitor *mon, const char *redir_str);
+static void slirp_guestfwd(Monitor *mon, const char *config_str,
+                           int legacy_format);
 
 #ifndef _WIN32
 static const char *legacy_smb_export;
 
-static void slirp_smb(const char *exported_dir);
+static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr);
 #endif
 
 int slirp_can_output(void)
@@ -731,31 +734,122 @@ static void net_slirp_cleanup(VLANClientState *vc)
 }
 
 static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
-                          const char *name, int restricted, const char *ip,
-                          const char *tftp_export, const char *bootfile,
-                          const char *smb_export)
+                          const char *name, int restricted,
+                          const char *vnetwork, const char *vhost,
+                          const char *vhostname, const char *tftp_export,
+                          const char *bootfile, const char *vdhcp_start,
+                          const char *vnameserver, const char *smb_export,
+                          const char *vsmbserver)
 {
     if (slirp_in_use) {
         /* slirp only supports a single instance so far */
         return -1;
     }
     if (!slirp_inited) {
+        /* default settings according to historic slirp */
+        struct in_addr net  = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */
+        struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */
+        struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
+        struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
+        struct in_addr dns  = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
+#ifndef _WIN32
+        struct in_addr smbsrv = { .s_addr = 0 };
+#endif
+        char buf[20];
+        uint32_t addr;
+        int shift;
+        char *end;
+
         if (!tftp_export) {
             tftp_export = legacy_tftp_prefix;
         }
         if (!bootfile) {
             bootfile = legacy_bootp_filename;
         }
+
+        if (vnetwork) {
+            if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
+                if (!inet_aton(vnetwork, &net)) {
+                    return -1;
+                }
+                addr = ntohl(net.s_addr);
+                if (!(addr & 0x80000000)) {
+                    mask.s_addr = htonl(0xff000000); /* class A */
+                } else if ((addr & 0xfff00000) == 0xac100000) {
+                    mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
+                } else if ((addr & 0xc0000000) == 0x80000000) {
+                    mask.s_addr = htonl(0xffff0000); /* class B */
+                } else if ((addr & 0xffff0000) == 0xc0a80000) {
+                    mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
+                } else if ((addr & 0xffff0000) == 0xc6120000) {
+                    mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
+                } else if ((addr & 0xe0000000) == 0xe0000000) {
+                    mask.s_addr = htonl(0xffffff00); /* class C */
+                } else {
+                    mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
+                }
+            } else {
+                if (!inet_aton(buf, &net)) {
+                    return -1;
+                }
+                shift = strtol(vnetwork, &end, 10);
+                if (*end != '\0') {
+                    if (!inet_aton(vnetwork, &mask)) {
+                        return -1;
+                    }
+                } else if (shift < 4 || shift > 32) {
+                    return -1;
+                } else {
+                    mask.s_addr = htonl(0xffffffff << (32 - shift));
+                }
+            }
+            net.s_addr &= mask.s_addr;
+            host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
+            dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
+            dns.s_addr  = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
+        }
+
+        if (vhost && !inet_aton(vhost, &host)) {
+            return -1;
+        }
+        if ((host.s_addr & mask.s_addr) != net.s_addr) {
+            return -1;
+        }
+
+        if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
+            return -1;
+        }
+        if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
+            dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
+            return -1;
+        }
+
+        if (vnameserver && !inet_aton(vnameserver, &dns)) {
+            return -1;
+        }
+        if ((dns.s_addr & mask.s_addr) != net.s_addr ||
+            dns.s_addr == host.s_addr) {
+            return -1;
+        }
+
+#ifndef _WIN32
+        if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
+            return -1;
+        }
+#endif
+
+        slirp_init(restricted, net, mask, host, vhostname, tftp_export,
+                   bootfile, dhcp, dns);
         slirp_inited = 1;
-        slirp_init(restricted, ip, tftp_export, bootfile);
 
         while (slirp_configs) {
             struct slirp_config_str *config = slirp_configs;
 
-            if (config->flags & SLIRP_CFG_REDIR) {
-                slirp_redirection(mon, config->str);
+            if (config->flags & SLIRP_CFG_HOSTFWD) {
+                slirp_hostfwd(mon, config->str);
             } else {
-                vmchannel_init(mon, config->str);
+                slirp_guestfwd(mon, config->str,
+                               config->flags & SLIRP_CFG_LEGACY);
             }
             slirp_configs = config->next;
             qemu_free(config);
@@ -765,7 +859,7 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
             smb_export = legacy_smb_export;
         }
         if (smb_export) {
-            slirp_smb(smb_export);
+            slirp_smb(smb_export, smbsrv);
         }
 #endif
     }
@@ -777,7 +871,7 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
     return 0;
 }
 
-static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
+static void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
 {
     int host_port;
     char buf[256] = "";
@@ -803,23 +897,24 @@ static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
 
     host_port = atoi(p);
 
-    n = slirp_redir_rm(is_udp, host_port);
+    n = slirp_remove_hostfwd(is_udp, host_port);
 
-    monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
-                        is_udp ? "udp" : "tcp", host_port);
+    monitor_printf(mon, "removed %d host forwarding rules for %s port %d\n",
+                   n, is_udp ? "udp" : "tcp", host_port);
     return;
 
  fail_syntax:
     monitor_printf(mon, "invalid format\n");
 }
 
-static void slirp_redirection(Monitor *mon, const char *redir_str)
+static void slirp_hostfwd(Monitor *mon, const char *redir_str)
 {
-    struct in_addr guest_addr;
+    struct in_addr guest_addr = { .s_addr = 0 };
     int host_port, guest_port;
     const char *p;
-    char buf[256], *r;
+    char buf[256];
     int is_udp;
+    char *end;
 
     p = redir_str;
     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
@@ -836,33 +931,31 @@ static void slirp_redirection(Monitor *mon, const char *redir_str)
     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
         goto fail_syntax;
     }
-    host_port = strtol(buf, &r, 0);
-    if (r == buf) {
+    host_port = strtol(buf, &end, 0);
+    if (*end != '\0' || host_port < 1 || host_port > 65535) {
         goto fail_syntax;
     }
 
     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 (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
         goto fail_syntax;
     }
 
-    guest_port = strtol(p, &r, 0);
-    if (r == p) {
+    guest_port = strtol(p, &end, 0);
+    if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
         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);
+    if (slirp_add_hostfwd(is_udp, host_port, guest_addr, guest_port) < 0) {
+        config_error(mon, "could not set up host forwarding rule '%s'\n",
+                     redir_str);
     }
     return;
 
  fail_syntax:
-    config_error(mon, "invalid redirection format '%s'\n", redir_str);
+    config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
 }
 
 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
@@ -875,7 +968,7 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
         } else {
             config = qemu_malloc(sizeof(*config));
             pstrcpy(config->str, sizeof(config->str), redir_str);
-            config->flags = SLIRP_CFG_REDIR;
+            config->flags = SLIRP_CFG_HOSTFWD;
             config->next = slirp_configs;
             slirp_configs = config;
         }
@@ -883,11 +976,11 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
     }
 
     if (!strcmp(redir_str, "remove")) {
-        net_slirp_redir_rm(mon, redir_opt2);
+        net_slirp_hostfwd_remove(mon, redir_opt2);
         return;
     }
 
-    slirp_redirection(mon, redir_str);
+    slirp_hostfwd(mon, redir_str);
 }
 
 #ifndef _WIN32
@@ -925,7 +1018,7 @@ static void smb_exit(void)
     erase_dir(smb_dir);
 }
 
-static void slirp_smb(const char *exported_dir)
+static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr)
 {
     char smb_conf[1024];
     char smb_cmdline[1024];
@@ -971,19 +1064,24 @@ static void slirp_smb(const char *exported_dir)
     snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
              SMBD_COMMAND, smb_conf);
 
-    slirp_add_exec(0, smb_cmdline, 4, 139);
+    if (slirp_add_exec(0, smb_cmdline, vserver_addr, 139) < 0) {
+        fprintf(stderr, "conflicting/invalid smbserver address\n");
+        exit(1);
+    }
 }
 
 /* automatic user mode samba server configuration (legacy interface) */
 void net_slirp_smb(const char *exported_dir)
 {
+    struct in_addr vserver_addr = { .s_addr = 0 };
+
     if (legacy_smb_export) {
         fprintf(stderr, "-smb given twice\n");
         exit(1);
     }
     legacy_smb_export = exported_dir;
     if (slirp_inited) {
-        slirp_smb(exported_dir);
+        slirp_smb(exported_dir, vserver_addr);
     }
 }
 
@@ -994,51 +1092,85 @@ void do_info_slirp(Monitor *mon)
     slirp_stats();
 }
 
-struct VMChannel {
+struct GuestFwd {
     CharDriverState *hd;
+    struct in_addr server;
     int port;
 };
 
-static int vmchannel_can_read(void *opaque)
+static int guestfwd_can_read(void *opaque)
 {
-    struct VMChannel *vmc = (struct VMChannel*)opaque;
-    return slirp_socket_can_recv(4, vmc->port);
+    struct GuestFwd *fwd = opaque;
+    return slirp_socket_can_recv(fwd->server, fwd->port);
 }
 
-static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
+static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
 {
-    struct VMChannel *vmc = (struct VMChannel*)opaque;
-    slirp_socket_recv(4, vmc->port, buf, size);
+    struct GuestFwd *fwd = opaque;
+    slirp_socket_recv(fwd->server, fwd->port, buf, size);
 }
 
-static void vmchannel_init(Monitor *mon, const char *config_str)
+static void slirp_guestfwd(Monitor *mon, const char *config_str,
+                           int legacy_format)
 {
-    struct VMChannel *vmc;
-    char *devname;
-    char name[20];
+    struct in_addr server = { .s_addr = 0 };
+    struct GuestFwd *fwd;
+    const char *p;
+    char buf[128];
+    char *end;
     int port;
 
-    port = strtol(config_str, &devname, 10);
-    if (port < 1 || port > 65535 || *devname != ':') {
-        config_error(mon, "invalid vmchannel port number\n");
-        return;
+    p = config_str;
+    if (legacy_format) {
+        if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+            goto fail_syntax;
+        }
+    } else {
+        if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+            goto fail_syntax;
+        }
+        if (strcmp(buf, "tcp") && buf[0] != '\0') {
+            goto fail_syntax;
+        }
+        if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+            goto fail_syntax;
+        }
+        if (buf[0] != '\0' && !inet_aton(buf, &server)) {
+            goto fail_syntax;
+        }
+        if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
+            goto fail_syntax;
+        }
+    }
+    port = strtol(buf, &end, 10);
+    if (*end != '\0' || port < 1 || port > 65535) {
+        goto fail_syntax;
     }
-    devname++;
 
-    vmc = qemu_malloc(sizeof(struct VMChannel));
-    snprintf(name, sizeof(name), "vmchannel%d", port);
-    vmc->hd = qemu_chr_open(name, devname, NULL);
-    if (!vmc->hd) {
-        config_error(mon, "could not open vmchannel device '%s'\n", devname);
-        qemu_free(vmc);
+    fwd = qemu_malloc(sizeof(struct GuestFwd));
+    snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port);
+    fwd->hd = qemu_chr_open(buf, p, NULL);
+    if (!fwd->hd) {
+        config_error(mon, "could not open guest forwarding device '%s'\n",
+                     buf);
+        qemu_free(fwd);
         return;
     }
-    vmc->port = port;
+    fwd->server = server;
+    fwd->port = port;
 
-    slirp_add_exec(3, vmc->hd, 4, port);
-    qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
-                          NULL, vmc);
+    if (slirp_add_exec(3, fwd->hd, server, port) < 0) {
+        config_error(mon, "conflicting/invalid host:port in guest forwarding "
+                     "rule '%s'\n", config_str);
+        qemu_free(fwd);
+        return;
+    }
+    qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
+                          NULL, fwd);
     return;
+
+ fail_syntax:
+    config_error(mon, "invalid guest forwarding rule '%s'\n", config_str);
 }
 
 #endif /* CONFIG_SLIRP */
@@ -2236,15 +2368,21 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
 #ifdef CONFIG_SLIRP
     if (!strcmp(device, "user")) {
         static const char * const slirp_params[] = {
-            "vlan", "name", "hostname", "restrict", "ip", "tftp", "bootfile",
-            "smb", "redir", "channel", NULL
+            "vlan", "name", "hostname", "restrict", "ip", "net", "host",
+            "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver",
+            "hostfwd", "guestfwd", NULL
         };
         struct slirp_config_str *config;
+        int restricted = 0;
+        char *vnet = NULL;
+        char *vhost = NULL;
+        char *vhostname = NULL;
         char *tftp_export = NULL;
         char *bootfile = NULL;
+        char *vdhcp_start = NULL;
+        char *vnamesrv = NULL;
         char *smb_export = NULL;
-        int restricted = 0;
-        char *ip = NULL;
+        char *vsmbsrv = NULL;
         const char *q;
 
         if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
@@ -2252,14 +2390,29 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
             ret = -1;
             goto out;
         }
+        if (get_param_value(buf, sizeof(buf), "ip", p)) {
+            /* emulate legacy parameter */
+            vnet = qemu_malloc(strlen(buf) + strlen("/24") + 1);
+            strcpy(vnet, buf);
+            strcat(vnet, "/24");
+        }
+        if (get_param_value(buf, sizeof(buf), "net", p)) {
+            vnet = qemu_strdup(buf);
+        }
+        if (get_param_value(buf, sizeof(buf), "host", p)) {
+            vhost = qemu_strdup(buf);
+        }
         if (get_param_value(buf, sizeof(buf), "hostname", p)) {
-            pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
+            vhostname = qemu_strdup(buf);
         }
         if (get_param_value(buf, sizeof(buf), "restrict", p)) {
             restricted = (buf[0] == 'y') ? 1 : 0;
         }
-        if (get_param_value(buf, sizeof(buf), "ip", p)) {
-            ip = qemu_strdup(buf);
+        if (get_param_value(buf, sizeof(buf), "dhcpstart", p)) {
+            vdhcp_start = qemu_strdup(buf);
+        }
+        if (get_param_value(buf, sizeof(buf), "dns", p)) {
+            vnamesrv = qemu_strdup(buf);
         }
         if (get_param_value(buf, sizeof(buf), "tftp", p)) {
             tftp_export = qemu_strdup(buf);
@@ -2269,15 +2422,18 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         }
         if (get_param_value(buf, sizeof(buf), "smb", p)) {
             smb_export = qemu_strdup(buf);
+            if (get_param_value(buf, sizeof(buf), "smbserver", p)) {
+                vsmbsrv = qemu_strdup(buf);
+            }
         }
         q = p;
         while (1) {
             config = qemu_malloc(sizeof(*config));
             if (!get_next_param_value(config->str, sizeof(config->str),
-                                      "redir", &q)) {
+                                      "hostfwd", &q)) {
                 break;
             }
-            config->flags = SLIRP_CFG_REDIR;
+            config->flags = SLIRP_CFG_HOSTFWD;
             config->next = slirp_configs;
             slirp_configs = config;
             config = NULL;
@@ -2286,7 +2442,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         while (1) {
             config = qemu_malloc(sizeof(*config));
             if (!get_next_param_value(config->str, sizeof(config->str),
-                                      "channel", &q)) {
+                                      "guestfwd", &q)) {
                 break;
             }
             config->flags = 0;
@@ -2296,23 +2452,29 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         }
         qemu_free(config);
         vlan->nb_host_devs++;
-        ret = net_slirp_init(mon, vlan, device, name, restricted, ip,
-                             tftp_export, bootfile, smb_export);
-        qemu_free(ip);
+        ret = net_slirp_init(mon, vlan, device, name, restricted, vnet, vhost,
+                             vhostname, tftp_export, bootfile, vdhcp_start,
+                             vnamesrv, smb_export, vsmbsrv);
+        qemu_free(vnet);
+        qemu_free(vhost);
+        qemu_free(vhostname);
         qemu_free(tftp_export);
         qemu_free(bootfile);
+        qemu_free(vdhcp_start);
+        qemu_free(vnamesrv);
         qemu_free(smb_export);
+        qemu_free(vsmbsrv);
     } else if (!strcmp(device, "channel")) {
         if (!slirp_inited) {
             struct slirp_config_str *config;
 
             config = qemu_malloc(sizeof(*config));
             pstrcpy(config->str, sizeof(config->str), p);
-            config->flags = 0;
+            config->flags = SLIRP_CFG_LEGACY;
             config->next = slirp_configs;
             slirp_configs = config;
         } else {
-            vmchannel_init(mon, p);
+            slirp_guestfwd(mon, p, 1);
         }
         ret = 0;
     } else
qemu-options.hx +34 -18

107 lines not on this page · raw message

slirp/libslirp.h +13 -11
index 7dee34b..e4c9c99 100644
@@ -5,8 +5,11 @@
 extern "C" {
 #endif
 
-void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
-                const char *bootfile);
+void slirp_init(int restricted, struct in_addr vnetwork,
+                struct in_addr vnetmask, struct in_addr vhost,
+                const char *vhostname, const char *tftp_path,
+                const char *bootfile, struct in_addr vdhcp_start,
+                struct in_addr vnameserver);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
@@ -19,18 +22,17 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
-int slirp_redir_rm(int is_udp, int host_port);
-int slirp_redir(int is_udp, int host_port,
-                struct in_addr guest_addr, int guest_port);
-int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
-                   int guest_port);
+int slirp_add_hostfwd(int is_udp, int host_port,
+                      struct in_addr guest_addr, int guest_port);
+int slirp_remove_hostfwd(int is_udp, int host_port);
 
-extern char slirp_hostname[33];
+int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr,
+                   int guest_port);
 
 void slirp_stats(void);
-void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
-		int size);
-size_t slirp_socket_can_recv(int addr_low_byte, int guest_port);
+void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
+                       const uint8_t *buf, int size);
+size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
 
 #ifdef __cplusplus
 }
slirp/main.h +1 -0
index edbb9cb..891220a 100644
@@ -47,6 +47,7 @@ extern int ppp_exit;
 extern int tcp_keepintvl;
 extern uint8_t client_ethaddr[6];
 extern int slirp_restrict;
+extern char slirp_hostname[33];
 extern char *tftp_prefix;
 extern char *bootp_filename;
 
slirp/slirp.c +24 -28

108 lines not on this page · raw message

author
#8 lore raw

PATCH 8/41 slirp: Fix port comparision in slirp_remove_hostfwd

In reply to Jan Kiszka

For UDP host forwardings, fport is not stable, every outgoing packet of the redirection can modify it. Use getsockname instead to look up the port that is actually used on the host side.

---

 slirp/slirp.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
slirp/slirp.c +6 -2
index a918232..ad88121 100644
@@ -761,12 +761,16 @@ int slirp_remove_hostfwd(int is_udp, int host_port)
 {
     struct socket *so;
     struct socket *head = (is_udp ? &udb : &tcb);
-    int fport = htons(host_port);
+    struct sockaddr_in addr;
+    int port = htons(host_port);
+    socklen_t addr_len;
     int n = 0;
 
  loop_again:
     for (so = head->so_next; so != head; so = so->so_next) {
-        if (so->so_fport == fport) {
+        addr_len = sizeof(addr);
+        if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
+            addr.sin_port == port) {
             close(so->s);
             sofree(so);
             n++;
author
#9 lore raw

PATCH 9/41 slirp: Rework monitor commands for host forwarding

In reply to Jan Kiszka

Improve the monitor interface for adding and removing host forwarding rules by splitting it up in two commands and rename them to hostfwd_add and hostfwd_remove. Also split up the paths taken for legacy -redir support and the monitor add command as the latter will be extended later on.

---

 net.c           |   37 ++++++++++++++++++++-----------------
 net.h           |    4 +++-
 qemu-monitor.hx |    8 +++++---
 vl.c            |    2 +-
 4 files changed, 29 insertions(+), 22 deletions(-)
net.c +20 -17
index a12177d..75a7408 100644
@@ -871,7 +871,7 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
     return 0;
 }
 
-static void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
+void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
 {
     int host_port;
     char buf[256] = "";
@@ -879,8 +879,10 @@ static void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
     int is_udp = 0;
     int n;
 
-    if (!mon)
+    if (!slirp_inited) {
+        monitor_printf(mon, "user mode network stack not in use\n");
         return;
+    }
 
     if (!port_str || !port_str[0])
         goto fail_syntax;
@@ -958,29 +960,30 @@ static void slirp_hostfwd(Monitor *mon, const char *redir_str)
     config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
 }
 
-void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
+void net_slirp_hostfwd_add(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));
-            pstrcpy(config->str, sizeof(config->str), redir_str);
-            config->flags = SLIRP_CFG_HOSTFWD;
-            config->next = slirp_configs;
-            slirp_configs = config;
-        }
+        monitor_printf(mon, "user mode network stack not in use\n");
         return;
     }
 
-    if (!strcmp(redir_str, "remove")) {
-        net_slirp_hostfwd_remove(mon, redir_opt2);
+    slirp_hostfwd(mon, redir_str);
+}
+
+void net_slirp_redir(const char *redir_str)
+{
+    struct slirp_config_str *config;
+
+    if (!slirp_inited) {
+        config = qemu_malloc(sizeof(*config));
+        pstrcpy(config->str, sizeof(config->str), redir_str);
+        config->flags = SLIRP_CFG_HOSTFWD;
+        config->next = slirp_configs;
+        slirp_configs = config;
         return;
     }
 
-    slirp_hostfwd(mon, redir_str);
+    slirp_hostfwd(NULL, redir_str);
 }
 
 #ifndef _WIN32
net.h +3 -1
index e705c87..884c354 100644
@@ -128,7 +128,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2);
+void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str);
+void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str);
+void net_slirp_redir(const char *redir_str);
 void net_cleanup(void);
 int slirp_is_inited(void);
 void net_client_check(void);
qemu-monitor.hx +5 -3
index 090255b..13c98bc 100644
@@ -536,9 +536,11 @@ Remove host VLAN client.
 ETEXI
 
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "ss?", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
-      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
+    { "hostfwd_add", "s", net_slirp_hostfwd_add,
+      "[tcp|udp]:hostport:[guestaddr]:guestport",
+      "redirect TCP or UDP connections from host to guest (requires -net user)" },
+    { "hostfwd_remove", "s", net_slirp_hostfwd_remove,
+      "[tcp|udp]:hostport", "remove host-to-guest TCP or UDP redirection" },
 #endif
 STEXI
 @item host_net_redir
vl.c +1 -1
index ae260b1..c24655e 100644
@@ -5319,7 +5319,7 @@ int main(int argc, char **argv, char **envp)
                 break;
 #endif
             case QEMU_OPTION_redir:
-                net_slirp_redir(NULL, optarg, NULL);
+                net_slirp_redir(optarg);
                 break;
 #endif
             case QEMU_OPTION_bt:
author
#10 lore raw

PATCH 10/41 slirp: Bind support for host forwarding rules

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Extend the hostfwd rule format so that the user can specify on which host interface qemu should listen for incoming connections. If omitted, binding will takes place against all interfaces.

---

 net.c            |   50 ++++++++++++++++++++++++++++++++++++--------------
 net.h            |    2 +-
 qemu-monitor.hx  |    5 +++--
 qemu-options.hx  |   11 ++++++-----
 slirp/libslirp.h |    5 ++---
 slirp/slirp.c    |   11 ++++++-----
 slirp/socket.c   |   10 +++++-----
 slirp/socket.h   |    2 +-
 slirp/tcp_subr.c |   15 ++++++++-------
 slirp/udp.c      |    7 ++++---
 slirp/udp.h      |    2 +-
 11 files changed, 73 insertions(+), 47 deletions(-)
net.c +36 -14
index 75a7408..6753993 100644
@@ -685,7 +685,8 @@ const char *legacy_tftp_prefix;
 const char *legacy_bootp_filename;
 static VLANClientState *slirp_vc;
 
-static void slirp_hostfwd(Monitor *mon, const char *redir_str);
+static void slirp_hostfwd(Monitor *mon, const char *redir_str,
+                          int legacy_format);
 static void slirp_guestfwd(Monitor *mon, const char *config_str,
                            int legacy_format);
 
@@ -846,7 +847,8 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
             struct slirp_config_str *config = slirp_configs;
 
             if (config->flags & SLIRP_CFG_HOSTFWD) {
-                slirp_hostfwd(mon, config->str);
+                slirp_hostfwd(mon, config->str,
+                              config->flags & SLIRP_CFG_LEGACY);
             } else {
                 slirp_guestfwd(mon, config->str,
                                config->flags & SLIRP_CFG_LEGACY);
@@ -871,11 +873,12 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
     return 0;
 }
 
-void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
+void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
 {
+    struct in_addr host_addr = { .s_addr = INADDR_ANY };
     int host_port;
     char buf[256] = "";
-    const char *p = port_str;
+    const char *p = src_str;
     int is_udp = 0;
     int n;
 
@@ -884,7 +887,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
         return;
     }
 
-    if (!port_str || !port_str[0])
+    if (!src_str || !src_str[0])
         goto fail_syntax;
 
     get_str_sep(buf, sizeof(buf), &p, ':');
@@ -897,20 +900,29 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str)
         goto fail_syntax;
     }
 
+    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+        goto fail_syntax;
+    }
+    if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
+        goto fail_syntax;
+    }
+
     host_port = atoi(p);
 
-    n = slirp_remove_hostfwd(is_udp, host_port);
+    n = slirp_remove_hostfwd(is_udp, host_addr, host_port);
 
-    monitor_printf(mon, "removed %d host forwarding rules for %s port %d\n",
-                   n, is_udp ? "udp" : "tcp", host_port);
+    monitor_printf(mon, "removed %d host forwarding rules for %s\n", n,
+                   src_str);
     return;
 
  fail_syntax:
     monitor_printf(mon, "invalid format\n");
 }
 
-static void slirp_hostfwd(Monitor *mon, const char *redir_str)
+static void slirp_hostfwd(Monitor *mon, const char *redir_str,
+                          int legacy_format)
 {
+    struct in_addr host_addr = { .s_addr = INADDR_ANY };
     struct in_addr guest_addr = { .s_addr = 0 };
     int host_port, guest_port;
     const char *p;
@@ -930,7 +942,16 @@ static void slirp_hostfwd(Monitor *mon, const char *redir_str)
         goto fail_syntax;
     }
 
-    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+    if (!legacy_format) {
+        if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+            goto fail_syntax;
+        }
+        if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
+            goto fail_syntax;
+        }
+    }
+
+    if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
         goto fail_syntax;
     }
     host_port = strtol(buf, &end, 0);
@@ -950,7 +971,8 @@ static void slirp_hostfwd(Monitor *mon, const char *redir_str)
         goto fail_syntax;
     }
 
-    if (slirp_add_hostfwd(is_udp, host_port, guest_addr, guest_port) < 0) {
+    if (slirp_add_hostfwd(is_udp, host_addr, host_port,
+                          guest_addr, guest_port) < 0) {
         config_error(mon, "could not set up host forwarding rule '%s'\n",
                      redir_str);
     }
@@ -967,7 +989,7 @@ void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str)
         return;
     }
 
-    slirp_hostfwd(mon, redir_str);
+    slirp_hostfwd(mon, redir_str, 0);
 }
 
 void net_slirp_redir(const char *redir_str)
@@ -977,13 +999,13 @@ void net_slirp_redir(const char *redir_str)
     if (!slirp_inited) {
         config = qemu_malloc(sizeof(*config));
         pstrcpy(config->str, sizeof(config->str), redir_str);
-        config->flags = SLIRP_CFG_HOSTFWD;
+        config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
         config->next = slirp_configs;
         slirp_configs = config;
         return;
     }
 
-    slirp_hostfwd(NULL, redir_str);
+    slirp_hostfwd(NULL, redir_str, 1);
 }
 
 #ifndef _WIN32
net.h +1 -1
index 884c354..6e91828 100644
@@ -129,7 +129,7 @@ void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
 void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str);
-void net_slirp_hostfwd_remove(Monitor *mon, const char *port_str);
+void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str);
 void net_slirp_redir(const char *redir_str);
 void net_cleanup(void);
 int slirp_is_inited(void);
qemu-monitor.hx +3 -2
index 13c98bc..34f6992 100644
@@ -537,10 +537,11 @@ ETEXI
 
 #ifdef CONFIG_SLIRP
     { "hostfwd_add", "s", net_slirp_hostfwd_add,
-      "[tcp|udp]:hostport:[guestaddr]:guestport",
+      "[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport",
       "redirect TCP or UDP connections from host to guest (requires -net user)" },
     { "hostfwd_remove", "s", net_slirp_hostfwd_remove,
-      "[tcp|udp]:hostport", "remove host-to-guest TCP or UDP redirection" },
+      "[tcp|udp]:[hostaddr]:hostport",
+      "remove host-to-guest TCP or UDP redirection" },
 #endif
 STEXI
 @item host_net_redir
qemu-options.hx +6 -5
index efa4a50..517665b 100644
@@ -879,11 +879,12 @@ Note that a SAMBA server must be installed on the host OS in
 @file{/usr/sbin/smbd}. QEMU was tested successfully with smbd versions from
 Red Hat 9, Fedora Core 3 and OpenSUSE 11.x.
 
-@item hostfwd=[tcp|udp]:@var{hostport}:[@var{guestaddr}]:@var{guestport}
+@item hostfwd=[tcp|udp]:[@var{hostaddr}]:@var{hostport}-[@var{guestaddr}]:@var{guestport}
 Redirect incoming TCP or UDP connections to the host port @var{hostport} to
 the guest IP address @var{guestaddr} on guest port @var{guestport}. If
 @var{guestaddr} is not specified, its value is x.x.x.15 (default first address
-given by the built-in DHCP server). If no connection type is specified, TCP is
+given by the built-in DHCP server). By specifying @var{hostaddr}, the rule can
+be bound to a specific host interface. If no connection type is set, TCP is
 used. This option can be given multiple times.
 
 For example, to redirect host X11 connection from screen 1 to guest
@@ -891,7 +892,7 @@ screen 0, use the following:
 
 @example
 # on the host
-qemu -net user,hostfwd=tcp:6001::6000 [...]
+qemu -net user,hostfwd=tcp:127.0.0.1:6001-:6000 [...]
 # this host xterm should open in the guest X11 server
 xterm -display :1
 @end example
@@ -909,8 +910,8 @@ Then when you use on the host @code{telnet localhost 5555}, you
 connect to the guest telnet server.
 
 @item guestfwd=[tcp]:@var{server}:@var{port}-@var{dev}
-Forward guest TCP connections to port @var{port} on the host to character
-device @var{dev}. This option can be given multiple times.
+Forward guest TCP connections to the IP address @var{server} on port @var{port}
+to the character device @var{dev}. This option can be given multiple times.
 
 @end table
 
slirp/libslirp.h +2 -3
index e4c9c99..9be4425 100644
@@ -22,10 +22,9 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
-int slirp_add_hostfwd(int is_udp, int host_port,
+int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,
                       struct in_addr guest_addr, int guest_port);
-int slirp_remove_hostfwd(int is_udp, int host_port);
-
+int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port);
 int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr,
                    int guest_port);
 
slirp/slirp.c +6 -5
index ad88121..ab0a854 100644
@@ -757,7 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
 /* Unlistens a redirection
  *
  * Return value: number of redirs removed */
-int slirp_remove_hostfwd(int is_udp, int host_port)
+int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port)
 {
     struct socket *so;
     struct socket *head = (is_udp ? &udb : &tcb);
@@ -770,6 +770,7 @@ int slirp_remove_hostfwd(int is_udp, int host_port)
     for (so = head->so_next; so != head; so = so->so_next) {
         addr_len = sizeof(addr);
         if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
+            addr.sin_addr.s_addr == host_addr.s_addr &&
             addr.sin_port == port) {
             close(so->s);
             sofree(so);
@@ -781,19 +782,19 @@ int slirp_remove_hostfwd(int is_udp, int host_port)
     return n;
 }
 
-int slirp_add_hostfwd(int is_udp, int host_port,
+int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,
                       struct in_addr guest_addr, int guest_port)
 {
     if (!guest_addr.s_addr) {
         guest_addr = vdhcp_startaddr;
     }
     if (is_udp) {
-        if (!udp_listen(htons(host_port), guest_addr.s_addr,
+        if (!udp_listen(host_addr.s_addr, htons(host_port), guest_addr.s_addr,
                         htons(guest_port), 0))
             return -1;
     } else {
-        if (!solisten(htons(host_port), guest_addr.s_addr,
-                      htons(guest_port), 0))
+        if (!tcp_listen(host_addr.s_addr, htons(host_port), guest_addr.s_addr,
+                        htons(guest_port), 0))
             return -1;
     }
     return 0;
slirp/socket.c +5 -5
index 9f13f03..936021e 100644
@@ -586,17 +586,17 @@ sosendto(struct socket *so, struct mbuf *m)
 }
 
 /*
- * XXX This should really be tcp_listen
+ * Listen for incoming TCP connections
  */
 struct socket *
-solisten(u_int port, u_int32_t laddr, u_int lport, int flags)
+tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags)
 {
 	struct sockaddr_in addr;
 	struct socket *so;
 	int s, opt = 1;
 	socklen_t addrlen = sizeof(addr);
 
-	DEBUG_CALL("solisten");
+	DEBUG_CALL("tcp_listen");
 	DEBUG_ARG("port = %d", port);
 	DEBUG_ARG("laddr = %x", laddr);
 	DEBUG_ARG("lport = %d", lport);
@@ -625,8 +625,8 @@ solisten(u_int port, u_int32_t laddr, u_int lport, int flags)
 	so->so_laddr.s_addr = laddr; /* Ditto */
 
 	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = INADDR_ANY;
-	addr.sin_port = port;
+	addr.sin_addr.s_addr = haddr;
+	addr.sin_port = hport;
 
 	if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
 	    (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
slirp/socket.h +1 -1
index f5adaba..ac36aaa 100644
@@ -82,7 +82,7 @@ int sosendoob _P((struct socket *));
 int sowrite _P((struct socket *));
 void sorecvfrom _P((struct socket *));
 int sosendto _P((struct socket *, struct mbuf *));
-struct socket * solisten _P((u_int, u_int32_t, u_int, int));
+struct socket * tcp_listen _P((u_int32_t, u_int, u_int32_t, u_int, int));
 void soisfconnecting _P((register struct socket *));
 void soisfconnected _P((register struct socket *));
 void soisfdisconnected _P((struct socket *));
slirp/tcp_subr.c +8 -7
index 858d1ae..6fa4223 100644
@@ -970,7 +970,7 @@ do_prompt:
 			laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
 			lport = htons((n5 << 8) | (n6));
 
-			if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
+			if ((so = tcp_listen(INADDR_ANY, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
 			   return 1;
 
 			n6 = ntohs(so->so_fport);
@@ -1002,7 +1002,7 @@ do_prompt:
 			laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
 			lport = htons((n5 << 8) | (n6));
 
-			if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
+			if ((so = tcp_listen(INADDR_ANY, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
 			   return 1;
 
 			n6 = ntohs(so->so_fport);
@@ -1042,7 +1042,7 @@ do_prompt:
 			lport += m->m_data[i] - '0';
 		}
 		if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
-		    (so = solisten(0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
+		    (so = tcp_listen(INADDR_ANY, 0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
                     m->m_len = snprintf(m->m_data, m->m_hdr.mh_size, "%d",
                                         ntohs(so->so_fport)) + 1;
 		return 1;
@@ -1057,7 +1057,7 @@ do_prompt:
 
 		/* The %256s is for the broken mIRC */
 		if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
-			if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
+			if ((so = tcp_listen(INADDR_ANY, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
 				return 1;
 
 			m->m_len = bptr - m->m_data; /* Adjust length */
@@ -1066,7 +1066,7 @@ do_prompt:
                                              (unsigned long)ntohl(so->so_faddr.s_addr),
                                              ntohs(so->so_fport), 1);
 		} else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
-			if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
+			if ((so = tcp_listen(INADDR_ANY, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
 				return 1;
 
 			m->m_len = bptr - m->m_data; /* Adjust length */
@@ -1075,7 +1075,7 @@ do_prompt:
                                              (unsigned long)ntohl(so->so_faddr.s_addr),
                                              ntohs(so->so_fport), n1, 1);
 		} else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
-			if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
+			if ((so = tcp_listen(INADDR_ANY, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
 				return 1;
 
 			m->m_len = bptr - m->m_data; /* Adjust length */
@@ -1190,7 +1190,8 @@ do_prompt:
 
 				/* try to get udp port between 6970 - 7170 */
 				for (p = 6970; p < 7071; p++) {
-					if (udp_listen( htons(p),
+					if (udp_listen(INADDR_ANY,
+						       htons(p),
 						       so->so_laddr.s_addr,
 						       htons(lport),
 						       SS_FACCEPTONCE)) {
slirp/udp.c +4 -3
index ff3a39f..d675ad3 100644
@@ -627,7 +627,8 @@ struct cu_header {
 }
 
 struct socket *
-udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
+udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
+           int flags)
 {
 	struct sockaddr_in addr;
 	struct socket *so;
@@ -642,8 +643,8 @@ udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
 	insque(so,&udb);
 
 	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = INADDR_ANY;
-	addr.sin_port = port;
+	addr.sin_addr.s_addr = haddr;
+	addr.sin_port = hport;
 
 	if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
 		udp_detach(so);
slirp/udp.h +1 -1
index 51a07a2..d4c2bea 100644
@@ -101,7 +101,7 @@ void udp_input _P((register struct mbuf *, int));
 int udp_output _P((struct socket *, struct mbuf *, struct sockaddr_in *));
 int udp_attach _P((struct socket *));
 void udp_detach _P((struct socket *));
-struct socket * udp_listen _P((u_int, u_int32_t, u_int, int));
+struct socket * udp_listen _P((u_int32_t, u_int, u_int32_t, u_int, int));
 int udp_output2(struct socket *so, struct mbuf *m,
                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                 int iptos);
author
#11 lore raw

PATCH 11/41 slirp: Prepare for persistent socket state flags

In reply to Jan Kiszka

This prepares for adding flags to socket.so_state that must not be removed during the lifetime of a socket.

---

 slirp/slirp.c     |    9 ++++++---
 slirp/socket.c    |   22 ++++++++++++++--------
 slirp/socket.h    |    2 ++
 slirp/tcp_input.c |    3 ++-
 slirp/udp.c       |    3 ++-
 5 files changed, 26 insertions(+), 13 deletions(-)
slirp/slirp.c +6 -3
index ab0a854..08c10f2 100644
@@ -495,7 +495,8 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 				continue;
 
 			      /* else failed */
-			      so->so_state = SS_NOFDREF;
+			      so->so_state &= SS_PERSISTENT_MASK;
+			      so->so_state |= SS_NOFDREF;
 			    }
 			    /* else so->so_state &= ~SS_ISFCONNECTING; */
 
@@ -529,7 +530,8 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 			      continue; /* Still connecting, continue */
 
 			    /* else failed */
-			    so->so_state = SS_NOFDREF;
+			    so->so_state &= SS_PERSISTENT_MASK;
+			    so->so_state |= SS_NOFDREF;
 
 			    /* tcp_input will take care of it */
 			  } else {
@@ -540,7 +542,8 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 				  errno == EINPROGRESS || errno == ENOTCONN)
 				continue;
 			      /* else failed */
-			      so->so_state = SS_NOFDREF;
+			      so->so_state &= SS_PERSISTENT_MASK;
+			      so->so_state |= SS_NOFDREF;
 			    } else
 			      so->so_state &= ~SS_ISFCONNECTING;
 
slirp/socket.c +14 -8
index 936021e..6b3da0a 100644
@@ -581,7 +581,8 @@ sosendto(struct socket *so, struct mbuf *m)
 	 */
 	if (so->so_expire)
 		so->so_expire = curtime + SO_EXPIRE;
-	so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
+	so->so_state &= SS_PERSISTENT_MASK;
+	so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
 	return 0;
 }
 
@@ -620,7 +621,8 @@ tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags
 	if (flags & SS_FACCEPTONCE)
 	   so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
 
-	so->so_state = (SS_FACCEPTCONN|flags);
+	so->so_state &= SS_PERSISTENT_MASK;
+	so->so_state |= (SS_FACCEPTCONN | flags);
 	so->so_lport = lport; /* Kept in network format */
 	so->so_laddr.s_addr = laddr; /* Ditto */
 
@@ -715,10 +717,12 @@ sofcantrcvmore(struct socket *so)
 		}
 	}
 	so->so_state &= ~(SS_ISFCONNECTING);
-	if (so->so_state & SS_FCANTSENDMORE)
-	   so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
-	else
+	if (so->so_state & SS_FCANTSENDMORE) {
+	   so->so_state &= SS_PERSISTENT_MASK;
+	   so->so_state |= SS_NOFDREF; /* Don't select it */
+	} else {
 	   so->so_state |= SS_FCANTRCVMORE;
+	}
 }
 
 static void
@@ -734,10 +738,12 @@ sofcantsendmore(struct socket *so)
             }
 	}
 	so->so_state &= ~(SS_ISFCONNECTING);
-	if (so->so_state & SS_FCANTRCVMORE)
-	   so->so_state = SS_NOFDREF; /* as above */
-	else
+	if (so->so_state & SS_FCANTRCVMORE) {
+	   so->so_state &= SS_PERSISTENT_MASK;
+	   so->so_state |= SS_NOFDREF; /* as above */
+	} else {
 	   so->so_state |= SS_FCANTSENDMORE;
+	}
 }
 
 void
slirp/socket.h +2 -0
index ac36aaa..2cf476c 100644
@@ -71,6 +71,8 @@ struct socket {
 #define SS_FACCEPTCONN		0x100	/* Socket is accepting connections from a host on the internet */
 #define SS_FACCEPTONCE		0x200	/* If set, the SS_FACCEPTCONN socket will die after one accept */
 
+#define SS_PERSISTENT_MASK	0xf000	/* Unremovable state bits */
+
 extern struct socket tcb;
 
 struct socket * solookup _P((struct socket *, struct in_addr, u_int, struct in_addr, u_int));
slirp/tcp_input.c +2 -1
index ab0840d..8789303 100644
@@ -1038,7 +1038,8 @@ trimthenstep6:
 		    soisfconnected(so);
 		    so->so_state &= ~SS_CTL;   /* success XXX */
 		  } else if (ret == 2) {
-		    so->so_state = SS_NOFDREF; /* CTL_CMD */
+		    so->so_state &= SS_PERSISTENT_MASK;
+		    so->so_state |= SS_NOFDREF; /* CTL_CMD */
 		  } else {
 		    needoutput = 1;
 		    tp->t_state = TCPS_FIN_WAIT_1;
slirp/udp.c +2 -1
index d675ad3..3722845 100644
@@ -666,7 +666,8 @@ udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
 	if (flags != SS_FACCEPTONCE)
 	   so->so_expire = 0;
 
-	so->so_state = SS_ISFCONNECTED;
+	so->so_state &= SS_PERSISTENT_MASK;
+	so->so_state |= SS_ISFCONNECTED;
 
 	return so;
 }
author
#12 lore raw

PATCH 12/41 slirp: Explicitely mark host-forwarding sockets

In reply to Jan Kiszka

Mark sockets that describe host forwardings. This is required for their (and only their) proper deletion and for pretty-printing.

---

 slirp/slirp.c  |    4 ++--
 slirp/socket.h |    1 +
 slirp/udp.c    |    2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
slirp/slirp.c +2 -2
index 08c10f2..ad35c1f 100644
@@ -793,11 +793,11 @@ int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,
     }
     if (is_udp) {
         if (!udp_listen(host_addr.s_addr, htons(host_port), guest_addr.s_addr,
-                        htons(guest_port), 0))
+                        htons(guest_port), SS_HOSTFWD))
             return -1;
     } else {
         if (!tcp_listen(host_addr.s_addr, htons(host_port), guest_addr.s_addr,
-                        htons(guest_port), 0))
+                        htons(guest_port), SS_HOSTFWD))
             return -1;
     }
     return 0;
slirp/socket.h +1 -0
index 2cf476c..979b191 100644
@@ -72,6 +72,7 @@ struct socket {
 #define SS_FACCEPTONCE		0x200	/* If set, the SS_FACCEPTCONN socket will die after one accept */
 
 #define SS_PERSISTENT_MASK	0xf000	/* Unremovable state bits */
+#define SS_HOSTFWD		0x1000	/* Socket describes host->guest forwarding */
 
 extern struct socket tcb;
 
slirp/udp.c +1 -1
index 3722845..60fe754 100644
@@ -667,7 +667,7 @@ udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
 	   so->so_expire = 0;
 
 	so->so_state &= SS_PERSISTENT_MASK;
-	so->so_state |= SS_ISFCONNECTED;
+	so->so_state |= SS_ISFCONNECTED | flags;
 
 	return so;
 }
author
#13 lore raw

PATCH 13/41 slirp: Do not allow to remove non-hostfwd sockets

In reply to Jan Kiszka

Prevent that the users accidentally shoots down dynamic sockets. This allows to remove looping for removals as there can now only be one match.

---

 net.c         |    8 ++++----
 slirp/slirp.c |   14 +++++---------
 2 files changed, 9 insertions(+), 13 deletions(-)
net.c +4 -4
index 6753993..71aaa1e 100644
@@ -880,7 +880,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
     char buf[256] = "";
     const char *p = src_str;
     int is_udp = 0;
-    int n;
+    int err;
 
     if (!slirp_inited) {
         monitor_printf(mon, "user mode network stack not in use\n");
@@ -909,10 +909,10 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
 
     host_port = atoi(p);
 
-    n = slirp_remove_hostfwd(is_udp, host_addr, host_port);
+    err = slirp_remove_hostfwd(is_udp, host_addr, host_port);
 
-    monitor_printf(mon, "removed %d host forwarding rules for %s\n", n,
-                   src_str);
+    monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
+                   err ? "removed" : "not found");
     return;
 
  fail_syntax:
slirp/slirp.c +5 -9
index ad35c1f..35de4e3 100644
@@ -757,9 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
     }
 }
 
-/* Unlistens a redirection
- *
- * Return value: number of redirs removed */
+/* Drop host forwarding rule, return 0 if found. */
 int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port)
 {
     struct socket *so;
@@ -767,22 +765,20 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port)
     struct sockaddr_in addr;
     int port = htons(host_port);
     socklen_t addr_len;
-    int n = 0;
 
- loop_again:
     for (so = head->so_next; so != head; so = so->so_next) {
         addr_len = sizeof(addr);
-        if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
+        if ((so->so_state & SS_HOSTFWD) &&
+            getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
             addr.sin_addr.s_addr == host_addr.s_addr &&
             addr.sin_port == port) {
             close(so->s);
             sofree(so);
-            n++;
-            goto loop_again;
+            return 0;
         }
     }
 
-    return n;
+    return -1;
 }
 
 int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,
author
#14 lore raw

PATCH 14/41 slirp: Mark sockets of incoming TCP connections

In reply to Jan Kiszka

Required for pretty-printing.

---

 slirp/socket.h   |    1 +
 slirp/tcp_subr.c |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
slirp/socket.h +1 -0
index 979b191..d241a48 100644
@@ -73,6 +73,7 @@ struct socket {
 
 #define SS_PERSISTENT_MASK	0xf000	/* Unremovable state bits */
 #define SS_HOSTFWD		0x1000	/* Socket describes host->guest forwarding */
+#define SS_INCOMING		0x2000	/* Connection was initiated by a host on the internet */
 
 extern struct socket tcb;
 
slirp/tcp_subr.c +1 -0
index 6fa4223..219f1dd 100644
@@ -483,6 +483,7 @@ tcp_connect(struct socket *inso)
 					   /* if it's not FACCEPTONCE, it's already NOFDREF */
 	}
 	so->s = s;
+	so->so_state |= SS_INCOMING;
 
 	so->so_iptos = tcp_tos(so);
 	tp = sototcpcb(so);
author
#15 lore raw

PATCH 15/41 slirp: Add info usernet for dumping connection states

In reply to Jan Kiszka

Break out sockstats from the slirp statistics and present them under the new info category "usernet". This patch also improves the current output /wrt proper reporting connection source and destination.

---

 monitor.c          |    2 +
 net.c              |    6 ++++
 net.h              |    2 +
 qemu-monitor.hx    |    2 +
 slirp/debug.c      |   42 -------------------------
 slirp/libslirp.h   |    4 ++
 slirp/misc.c       |   86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/tcp.h        |    2 -
 slirp/tcp_output.c |   11 -------
 9 files changed, 102 insertions(+), 55 deletions(-)
monitor.c +2 -0
index 6b45f6c..47fc9de 100644
@@ -1735,6 +1735,8 @@ static const mon_cmd_t info_cmds[] = {
 #if defined(CONFIG_SLIRP)
     { "slirp", "", do_info_slirp,
       "", "show SLIRP statistics", },
+    { "usernet", "", do_info_usernet,
+      "", "show user network stack connection states", },
 #endif
     { "migrate", "", do_info_migrate, "", "show migration status" },
     { "balloon", "", do_info_balloon,
net.c +6 -0
index 71aaa1e..8bd3b25 100644
@@ -1198,6 +1198,12 @@ static void slirp_guestfwd(Monitor *mon, const char *config_str,
     config_error(mon, "invalid guest forwarding rule '%s'\n", config_str);
 }
 
+void do_info_usernet(Monitor *mon)
+{
+    monitor_printf(mon, "VLAN %d (%s):\n", slirp_vc->vlan->id, slirp_vc->name);
+    slirp_connection_info(mon);
+}
+
 #endif /* CONFIG_SLIRP */
 
 #if !defined(_WIN32)
net.h +2 -0
index 6e91828..5a608e0 100644
@@ -81,6 +81,8 @@ void qemu_handler_true(void *opaque);
 void do_info_network(Monitor *mon);
 int do_set_link(Monitor *mon, const char *name, const char *up_or_down);
 
+void do_info_usernet(Monitor *mon);
+
 /* NIC info */
 
 #define MAX_NICS 8
qemu-monitor.hx +2 -0
index 34f6992..359bdc6 100644
@@ -87,6 +87,8 @@ show the current VM UUID
 show CPU statistics
 @item info slirp
 show SLIRP statistics (if available)
+@item info usernet
+show user network stack connection states
 @item info migrate
 show migration status
 @item info balloon
slirp/debug.c +0 -42

54 lines not on this page · raw message

slirp/libslirp.h +4 -0
index 9be4425..5c7a8e9 100644
@@ -5,6 +5,8 @@
 extern "C" {
 #endif
 
+#include <qemu-common.h>
+
 void slirp_init(int restricted, struct in_addr vnetwork,
                 struct in_addr vnetmask, struct in_addr vhost,
                 const char *vhostname, const char *tftp_path,
@@ -29,6 +31,8 @@ int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr,
                    int guest_port);
 
 void slirp_stats(void);
+void slirp_connection_info(Monitor *mon);
+
 void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size);
 size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
slirp/misc.c +86 -0

95 lines not on this page · raw message

slirp/tcp.h +0 -2
index 4057032..3399857 100644
@@ -166,6 +166,4 @@ struct tcphdr {
 
 extern tcp_seq tcp_iss;                /* tcp initial send seq # */
 
-extern const char * const tcpstates[];
-
 #endif
slirp/tcp_output.c +0 -11
index 9ed50f5..5daf58e 100644
@@ -40,17 +40,6 @@
 
 #include <slirp.h>
 
-/*
- * Since this is only used in "stats socket", we give meaning
- * names instead of the REAL names
- */
-const char * const tcpstates[] = {
-/*	"CLOSED",       "LISTEN",       "SYN_SENT",     "SYN_RCVD", */
-	"REDIRECT",	"LISTEN",	"SYN_SENT",     "SYN_RCVD",
-	"ESTABLISHED",  "CLOSE_WAIT",   "FIN_WAIT_1",   "CLOSING",
-	"LAST_ACK",     "FIN_WAIT_2",   "TIME_WAIT",
-};
-
 static const u_char  tcp_outflags[TCP_NSTATES] = {
 	TH_RST|TH_ACK, 0,      TH_SYN,        TH_SYN|TH_ACK,
 	TH_ACK,        TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
author
#16 lore raw

PATCH 16/41 slirp: Drop dead code

In reply to Jan Kiszka

After all its years inside the qemu tree, there is no point in keeping the dead code paths of slirp. This patch is a first round of removing usually commented out code parts. More cleanups need to follow (and maybe finally a proper reindention).

---

 slirp/debug.c      |  185 ------------------
 slirp/debug.h      |    2 
 slirp/if.c         |   80 --------
 slirp/if.h         |   14 -
 slirp/ip_icmp.c    |    4 
 slirp/ip_input.c   |   17 --
 slirp/ip_output.c  |   23 --
 slirp/libslirp.h   |    8 -
 slirp/mbuf.c       |    6 -
 slirp/mbuf.h       |    2 
 slirp/misc.c       |  525 ----------------------------------------------------
 slirp/misc.h       |    8 -
 slirp/sbuf.c       |    8 -
 slirp/slirp.c      |   13 -
 slirp/slirp.h      |    7 -
 slirp/socket.c     |   53 -----
 slirp/socket.h     |    4 
 slirp/tcp.h        |    1 
 slirp/tcp_input.c  |  210 +--------------------
 slirp/tcp_output.c |   77 --------
 slirp/tcp_subr.c   |  366 ------------------------------------
 slirp/tcp_timer.c  |   17 --
 slirp/tcp_timer.h  |    6 -
 slirp/tcp_var.h    |    6 -
 slirp/tftp.c       |    2 
 slirp/udp.c        |   28 ---
 26 files changed, 22 insertions(+), 1650 deletions(-)
slirp/debug.c +0 -185
index ac856ca..6462c88 100644
@@ -9,127 +9,9 @@
 #include <slirp.h>
 
 FILE *dfd = NULL;
-#ifdef DEBUG
-int dostats = 1;
-#else
-int dostats = 0;
-#endif
 int slirp_debug = 0;
 
-/* Carry over one item from main.c so that the tty's restored.
- * Only done when the tty being used is /dev/tty --RedWolf */
-#ifndef CONFIG_QEMU
-extern struct termios slirp_tty_settings;
-extern int slirp_tty_restore;
-
-
-void
-debug_init(file, dbg)
-	char *file;
-	int dbg;
-{
-	/* Close the old debugging file */
-	if (dfd)
-	   fclose(dfd);
-
-	dfd = fopen(file,"w");
-	if (dfd != NULL) {
-#if 0
-		fprintf(dfd,"Slirp %s - Debugging Started.\n", SLIRP_VERSION);
-#endif
-		fprintf(dfd,"Debugging Started level %i.\r\n",dbg);
-		fflush(dfd);
-		slirp_debug = dbg;
-	} else {
-		lprint("Error: Debugging file \"%s\" could not be opened: %s\r\n",
-			file, strerror(errno));
-	}
-}
-
-/*
- * Dump a packet in the same format as tcpdump -x
- */
-#ifdef DEBUG
-void
-dump_packet(dat, n)
-	void *dat;
-	int n;
-{
-	u_char *pptr = (u_char *)dat;
-	int j,k;
-
-	n /= 16;
-	n++;
-	DEBUG_MISC((dfd, "PACKET DUMPED: \n"));
-	for(j = 0; j < n; j++) {
-		for(k = 0; k < 6; k++)
-			DEBUG_MISC((dfd, "%02x ", *pptr++));
-		DEBUG_MISC((dfd, "\n"));
-		fflush(dfd);
-	}
-}
-#endif
-#endif
-
 #ifdef LOG_ENABLED
-#if 0
-/*
- * Statistic routines
- *
- * These will print statistics to the screen, the debug file (dfd), or
- * a buffer, depending on "type", so that the stats can be sent over
- * the link as well.
- */
-
-static void
-ttystats(ttyp)
-	struct ttys *ttyp;
-{
-	struct slirp_ifstats *is = &ttyp->ifstats;
-	char buff[512];
-
-	lprint(" \r\n");
-
-	if (IF_COMP & IF_COMPRESS)
-	   strcpy(buff, "on");
-	else if (IF_COMP & IF_NOCOMPRESS)
-	   strcpy(buff, "off");
-	else
-	   strcpy(buff, "off (for now)");
-	lprint("Unit %d:\r\n", ttyp->unit);
-	lprint("  using %s encapsulation (VJ compression is %s)\r\n", (
-#ifdef USE_PPP
-	       ttyp->proto==PROTO_PPP?"PPP":
-#endif
-	       "SLIP"), buff);
-	lprint("  %d baudrate\r\n", ttyp->baud);
-	lprint("  interface is %s\r\n", ttyp->up?"up":"down");
-	lprint("  using fd %d, guardian pid is %d\r\n", ttyp->fd, ttyp->pid);
-#ifndef FULL_BOLT
-	lprint("  towrite is %d bytes\r\n", ttyp->towrite);
-#endif
-	if (ttyp->zeros)
-	   lprint("  %d zeros have been typed\r\n", ttyp->zeros);
-	else if (ttyp->ones)
-	   lprint("  %d ones have been typed\r\n", ttyp->ones);
-	lprint("Interface stats:\r\n");
-	lprint("  %6d output packets sent (%d bytes)\r\n", is->out_pkts, is->out_bytes);
-	lprint("  %6d output packets dropped (%d bytes)\r\n", is->out_errpkts, is->out_errbytes);
-	lprint("  %6d input packets received (%d bytes)\r\n", is->in_pkts, is->in_bytes);
-	lprint("  %6d input packets dropped (%d bytes)\r\n", is->in_errpkts, is->in_errbytes);
-	lprint("  %6d bad input packets\r\n", is->in_mbad);
-}
-
-static void
-allttystats(void)
-{
-	struct ttys *ttyp;
-
-	for (ttyp = ttys; ttyp; ttyp = ttyp->next)
-	   ttystats(ttyp);
-}
-#endif
-
 static void
 ipstats(void)
 {
@@ -154,25 +36,6 @@ ipstats(void)
 	lprint("  %6d total packets delivered\r\n", ipstat.ips_delivered);
 }
 
-#ifndef CONFIG_QEMU
-static void
-vjstats(void)
-{
-	lprint(" \r\n");
-
-	lprint("VJ compression stats:\r\n");
-
-	lprint("  %6d outbound packets (%d compressed)\r\n",
-	       comp_s.sls_packets, comp_s.sls_compressed);
-	lprint("  %6d searches for connection stats (%d misses)\r\n",
-	       comp_s.sls_searches, comp_s.sls_misses);
-	lprint("  %6d inbound uncompressed packets\r\n", comp_s.sls_uncompressedin);
-	lprint("  %6d inbound compressed packets\r\n", comp_s.sls_compressedin);
-	lprint("  %6d inbound unknown type packets\r\n", comp_s.sls_errorin);
-	lprint("  %6d inbound packets tossed due to error\r\n", comp_s.sls_tossed);
-}
-#endif
-
 static void
 tcpstats(void)
 {
@@ -234,11 +97,6 @@ tcpstats(void)
 	lprint("  %6d correct ACK header predictions\r\n", tcpstat.tcps_predack);
 	lprint("  %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
 	lprint("  %6d TCP cache misses\r\n", tcpstat.tcps_socachemiss);
-
-
-/*	lprint("    Packets received too short:		%d\r\n", tcpstat.tcps_rcvshort); */
-/*	lprint("    Segments dropped due to PAWS:	%d\r\n", tcpstat.tcps_pawsdrop); */
-
 }
 
 static void
@@ -293,49 +151,6 @@ mbufstats(void)
 }
 #endif
 
-#ifndef CONFIG_QEMU
-void
-slirp_exit(exit_status)
-	int exit_status;
-{
-	struct ttys *ttyp;
-
-	DEBUG_CALL("slirp_exit");
-	DEBUG_ARG("exit_status = %d", exit_status);
-
-	if (dostats) {
-		lprint_print = (int (*) _P((void *, const char *, va_list)))vfprintf;
-		if (!dfd)
-		   debug_init("slirp_stats", 0xf);
-		lprint_arg = (char **)&dfd;
-
-		ipstats();
-		tcpstats();
-		udpstats();
-		icmpstats();
-		mbufstats();
-		sockstats();
-		allttystats();
-		vjstats();
-	}
-
-	for (ttyp = ttys; ttyp; ttyp = ttyp->next)
-	   tty_detached(ttyp, 1);
-
-	if (slirp_forked) {
-		/* Menendez time */
-		if (kill(getppid(), SIGQUIT) < 0)
-			lprint("Couldn't kill parent process %ld!\n",
-			    (long) getppid());
-    	}
-
-	/* Restore the terminal if we gotta */
-	if(slirp_tty_restore)
-	  tcsetattr(0,TCSANOW, &slirp_tty_settings);  /* NOW DAMMIT! */
-	exit(exit_status);
-}
-#endif
-
 void
 slirp_stats(void)
 {
slirp/debug.h +0 -2
index c43eff7..963ffda 100644
@@ -9,8 +9,6 @@
 #define PRN_SPRINTF	2
 
 extern FILE *dfd;
-extern FILE *lfd;
-extern int dostats;
 extern int slirp_debug;
 
 #define DBG_CALL 0x1
slirp/if.c +0 -80

89 lines not on this page · raw message

slirp/if.h +1 -13
index bed7152..d49bcd5 100644
@@ -17,20 +17,8 @@
 #define IF_MRU 1500
 #define	IF_COMP IF_AUTOCOMP	/* Flags for compression */
 
-#if 0
-/*
- * Set if_maxlinkhdr to 48 because it's 40 bytes for TCP/IP,
- * and 8 bytes for PPP, but need to have it on an 8byte boundary
- */
-#ifdef USE_PPP
-#define IF_MAXLINKHDR 48
-#else
-#define IF_MAXLINKHDR 40
-#endif
-#else
-        /* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
+/* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
 #define IF_MAXLINKHDR (2 + 14 + 40)
-#endif
 
 extern int	if_queued;	/* Number of packets queued so far */
 
slirp/ip_icmp.c +0 -4
index 6e93ee3..c47ab1f 100644
@@ -73,7 +73,6 @@ icmp_input(struct mbuf *m, int hlen)
   register struct icmp *icp;
   register struct ip *ip=mtod(m, struct ip *);
   int icmplen=ip->ip_len;
-  /* int code; */
 
   DEBUG_CALL("icmp_input");
   DEBUG_ARG("m = %lx", (long )m);
@@ -102,9 +101,6 @@ icmp_input(struct mbuf *m, int hlen)
   m->m_len += hlen;
   m->m_data -= hlen;
 
-  /*	icmpstat.icps_inhist[icp->icmp_type]++; */
-  /* code = icp->icmp_code; */
-
   DEBUG_ARG("icmp_type = %d", icp->icmp_type);
   switch (icp->icmp_type) {
   case ICMP_ECHO:
slirp/ip_input.c +0 -17
index 7a3c88b..1fdd8d6 100644
@@ -165,16 +165,6 @@ ip_input(struct mbuf *m)
 	}
 
 	/*
-	 * Process options and, if not destined for us,
-	 * ship it on.  ip_dooptions returns 1 when an
-	 * error was detected (causing an icmp message
-	 * to be sent and the original packet to be freed).
-	 */
-/* We do no IP options */
-/*	if (hlen > sizeof (struct ip) && ip_dooptions(m))
- *		goto next;
- */
-	/*
 	 * If offset or IP_MF are set, must reassemble.
 	 * Otherwise, nothing need be done.
 	 * (We could look in the reassembly queue to see
@@ -396,9 +386,6 @@ insert:
 	  q = (struct ipasfrag *)(m->m_ext + delta);
 	}
 
-	/* DEBUG_ARG("ip = %lx", (long)ip);
-	 * ip=(struct ipasfrag *)m->m_data; */
-
     ip = fragtoip(q);
 	ip->ip_len = next;
 	ip->ip_tos &= ~1;
@@ -505,7 +492,6 @@ ip_dooptions(m)
 	register u_char *cp;
 	register struct ip_timestamp *ipt;
 	register struct in_ifaddr *ia;
-/*	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
 	int opt, optlen, cnt, off, code, type, forward = 0;
 	struct in_addr *sin, dst;
 typedef u_int32_t n_time;
@@ -683,9 +669,6 @@ typedef u_int32_t n_time;
 	}
 	return (0);
 bad:
-	/* ip->ip_len -= ip->ip_hl << 2;   XXX icmp_error adds in hdr length */
-
-/* Not yet */
  	icmp_error(m, type, code, 0, 0);
 
 	STAT(ipstat.ips_badoptions++);
slirp/ip_output.c +0 -23
index 5bce520..94bb0f2 100644
@@ -64,12 +64,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	DEBUG_ARG("so = %lx", (long)so);
 	DEBUG_ARG("m0 = %lx", (long)m0);
 
-	/* We do no options */
-/*	if (opt) {
- *		m = ip_insertoptions(m, opt, &len);
- *		hlen = len;
- *	}
- */
 	ip = mtod(m, struct ip *);
 	/*
 	 * Fill in IP header.
@@ -81,17 +75,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	STAT(ipstat.ips_localout++);
 
 	/*
-	 * Verify that we have any chance at all of being able to queue
-	 *      the packet or packet fragments
-	 */
-	/* XXX Hmmm... */
-/*	if (if_queued > IF_THRESH && towrite <= 0) {
- *		error = ENOBUFS;
- *		goto bad;
- *	}
- */
-
-	/*
 	 * If small enough for interface, can just send directly.
 	 */
 	if ((u_int16_t)ip->ip_len <= IF_MTU) {
@@ -142,12 +125,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	  mhip = mtod(m, struct ip *);
 	  *mhip = *ip;
 
-		/* No options */
-/*		if (hlen > sizeof (struct ip)) {
- *			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
- *			mhip->ip_hl = mhlen >> 2;
- *		}
- */
 	  m->m_len = mhlen;
 	  mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
 	  if (ip->ip_off & IP_MF)
slirp/libslirp.h +0 -8
index 5c7a8e9..c48837c 100644
@@ -1,10 +1,6 @@
 #ifndef _LIBSLIRP_H
 #define _LIBSLIRP_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #include <qemu-common.h>
 
 void slirp_init(int restricted, struct in_addr vnetwork,
@@ -37,8 +33,4 @@ void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size);
 size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif
slirp/mbuf.c +0 -6
index 1d30a63..7963d34 100644
@@ -141,17 +141,11 @@ m_inc(struct mbuf *m, int size)
         if (m->m_flags & M_EXT) {
 	  datasize = m->m_data - m->m_ext;
 	  m->m_ext = (char *)realloc(m->m_ext,size);
-/*		if (m->m_ext == NULL)
- *			return (struct mbuf *)NULL;
- */
 	  m->m_data = m->m_ext + datasize;
         } else {
 	  char *dat;
 	  datasize = m->m_data - m->m_dat;
 	  dat = (char *)malloc(size);
-/*		if (dat == NULL)
- *			return (struct mbuf *)NULL;
- */
 	  memcpy(dat, m->m_dat, m->m_size);
 
 	  m->m_ext = dat;
slirp/mbuf.h +0 -2
index 5527373..e259441 100644
@@ -41,10 +41,8 @@
 /*
  * Macros for type conversion
  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
- * dtom(x) -	convert data pointer within mbuf to mbuf pointer (XXX)
  */
 #define mtod(m,t)	((t)(m)->m_data)
-/* #define	dtom(x)		((struct mbuf *)((int)(x) & ~(M_SIZE-1))) */
 
 /* XXX About mbufs for slirp:
  * Only one mbuf is ever used in a chain, for each "cell" of data.
slirp/misc.c +0 -525

582 lines not on this page · raw message

slirp/misc.h +0 -8
index 29d5749..82dd596 100644
@@ -19,10 +19,6 @@ struct ex_list {
 extern struct ex_list *exec_list;
 extern u_int time_fasttimo, last_slowtimo;
 
-extern int (*lprint_print) _P((void *, const char *, va_list));
-extern char *lprint_ptr, *lprint_ptr2, **lprint_arg;
-extern struct sbuf *lprint_sb;
-
 #ifndef HAVE_STRDUP
 char *strdup _P((const char *));
 #endif
@@ -63,10 +59,6 @@ struct emu_t {
 	struct emu_t *next;
 };
 
-#ifndef CONFIG_QEMU
-extern struct emu_t *tcpemu;
-#endif
-
 extern int x_port, x_server, x_display;
 
 int show_x _P((char *, struct socket *));
slirp/sbuf.c +0 -8
index 5375414..5a1ccbf 100644
@@ -9,14 +9,6 @@
 
 static void sbappendsb(struct sbuf *sb, struct mbuf *m);
 
-/* Done as a macro in socket.h */
-/* int
- * sbspace(struct sockbuff *sb)
- * {
- *	return SB_DATALEN - sb->sb_cc;
- * }
- */
-
 void
 sbfree(struct sbuf *sb)
 {
slirp/slirp.c +0 -13
index 35de4e3..44a53cd 100644
@@ -55,7 +55,6 @@ int slirp_restrict;
 static int do_slowtimo;
 int link_up;
 struct timeval tt;
-FILE *lfd;
 struct ex_list *exec_list;
 
 /* XXX: suppress those select globals */
@@ -96,16 +95,6 @@ static int get_dns_addr(struct in_addr *pdns_addr)
     pIPAddr = &(FixedInfo->DnsServerList);
     inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
     *pdns_addr = tmp_addr;
-#if 0
-    printf( "DNS Servers:\n" );
-    printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
-
-    pIPAddr = FixedInfo -> DnsServerList.Next;
-    while ( pIPAddr ) {
-            printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
-            pIPAddr = pIPAddr ->Next;
-    }
-#endif
     if (FixedInfo) {
         GlobalFree(FixedInfo);
         FixedInfo = NULL;
@@ -179,8 +168,6 @@ void slirp_init(int restricted, struct in_addr vnetwork,
                 const char *bootfile, struct in_addr vdhcp_start,
                 struct in_addr vnameserver)
 {
-    //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
-
 #ifdef _WIN32
     WSADATA Data;
 
slirp/slirp.h +0 -7
index 101d094..0c62fd7 100644
@@ -1,8 +1,6 @@
 #ifndef __COMMON_H__
 #define __COMMON_H__
 
-#define CONFIG_QEMU
-
 //#define DEBUG 1
 
 // Uncomment the following line to enable SLIRP statistics printing in Qemu
@@ -14,9 +12,6 @@
 #define STAT(expr) do { } while(0)
 #endif
 
-#ifndef CONFIG_QEMU
-#include "version.h"
-#endif
 #include "config-host.h"
 #include "slirp_config.h"
 
@@ -223,8 +218,6 @@ int inet_aton _P((const char *cp, struct in_addr *ia));
 #include "tftp.h"
 #include "libslirp.h"
 
-extern struct ttys *ttys_unit[MAX_INTERFACES];
-
 #ifndef NULL
 #define NULL (void *)0
 #endif
slirp/socket.c +0 -53

83 lines not on this page · raw message

slirp/socket.h +0 -4
index d241a48..4db2147 100644
@@ -5,8 +5,6 @@
  * terms and conditions of the copyright.
  */
 
-/* MINE */
-
 #ifndef _SLIRP_SOCKET_H_
 #define _SLIRP_SOCKET_H_
 
@@ -64,7 +62,6 @@ struct socket {
 #define SS_ISFCONNECTED		0x004	/* Socket is connected to peer */
 #define SS_FCANTRCVMORE		0x008	/* Socket can't receive more from peer (for half-closes) */
 #define SS_FCANTSENDMORE	0x010	/* Socket can't send more to peer (for half-closes) */
-/* #define SS_ISFDISCONNECTED	0x020*/	/* Socket has disconnected from peer, in 2MSL state */
 #define SS_FWDRAIN		0x040	/* We received a FIN, drain data and set SS_FCANTSENDMORE */
 
 #define SS_CTL			0x080
@@ -89,7 +86,6 @@ int sosendto _P((struct socket *, struct mbuf *));
 struct socket * tcp_listen _P((u_int32_t, u_int, u_int32_t, u_int, int));
 void soisfconnecting _P((register struct socket *));
 void soisfconnected _P((register struct socket *));
-void soisfdisconnected _P((struct socket *));
 void sofwdrain _P((struct socket *));
 struct iovec; /* For win32 */
 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np);
slirp/tcp.h +0 -1
index 3399857..4a18ed8 100644
@@ -113,7 +113,6 @@ struct tcphdr {
 #undef TCP_NODELAY
 #define	TCP_NODELAY	0x01	/* don't delay send to coalesce packets */
 #undef TCP_MAXSEG
-/* #define	TCP_MAXSEG	0x02 */	/* set maximum segment size */
 
 /*
  * TCP FSM state definitions.
slirp/tcp_input.c +9 -201

414 lines not on this page · raw message

slirp/tcp_output.c +2 -75

127 lines not on this page · raw message

slirp/tcp_subr.c +4 -362

447 lines not on this page · raw message

slirp/tcp_timer.c +1 -16
index 9215eb2..9cacb0f 100644
@@ -102,10 +102,6 @@ tpgone:
 		;
 	}
 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
-#ifdef TCP_COMPAT_42
-	if ((int)tcp_iss < 0)
-		tcp_iss = 0;				/* XXX */
-#endif
 	tcp_now++;					/* for timestamps */
 }
 
@@ -210,7 +206,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 		 * retransmit times until then.
 		 */
 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
-/*			in_losing(tp->t_inpcb); */
 			tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
 			tp->t_srtt = 0;
 		}
@@ -275,7 +270,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 		if (tp->t_state < TCPS_ESTABLISHED)
 			goto dropit;
 
-/*		if (tp->t_socket->so_options & SO_KEEPALIVE && */
 		if ((SO_OPTIONS) && tp->t_state <= TCPS_CLOSE_WAIT) {
 		    	if (tp->t_idle >= TCPTV_KEEP_IDLE + TCP_MAXIDLE)
 				goto dropit;
@@ -292,17 +286,8 @@ tcp_timers(register struct tcpcb *tp, int timer)
 			 * correspondent TCP to respond.
 			 */
 			STAT(tcpstat.tcps_keepprobe++);
-#ifdef TCP_COMPAT_42
-			/*
-			 * The keepalive packet must have nonzero length
-			 * to get a 4.2 host to respond.
-			 */
-			tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
-			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
-#else
 			tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
 			    tp->rcv_nxt, tp->snd_una - 1, 0);
-#endif
 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
 		} else
 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
@@ -310,7 +295,7 @@ tcp_timers(register struct tcpcb *tp, int timer)
 
 	dropit:
 		STAT(tcpstat.tcps_keepdrops++);
-		tp = tcp_drop(tp, 0); /* ETIMEDOUT); */
+		tp = tcp_drop(tp, 0);
 		break;
 	}
 
slirp/tcp_timer.h +0 -6
index 791ee49..f648ed0 100644
@@ -98,7 +98,6 @@
 #define	TCPTV_KEEPCNT	8			/* max probes before drop */
 
 #define	TCPTV_MIN	(  1*PR_SLOWHZ)		/* minimum allowable value */
-/* #define	TCPTV_REXMTMAX	( 64*PR_SLOWHZ)	*/	/* max allowable REXMT value */
 #define TCPTV_REXMTMAX  ( 12*PR_SLOWHZ)		/* max allowable REXMT value */
 
 #define	TCP_LINGERTIME	120			/* linger at most 2 minutes */
@@ -106,11 +105,6 @@
 #define TCP_MAXRXTSHIFT 12                      /* maximum retransmits */
 
 
-#ifdef	TCPTIMERS
-char *tcptimers[] =
-    { "REXMT", "PERSIST", "KEEP", "2MSL" };
-#endif
-
 /*
  * Force a time value to be in a certain range.
  */
slirp/tcp_var.h +1 -5
index d4af1c8..be5741f 100644
@@ -61,9 +61,7 @@ struct tcpcb {
 #define	TF_RCVD_TSTMP	0x0100		/* a timestamp was received in SYN */
 #define	TF_SACK_PERMIT	0x0200		/* other side said I could SACK */
 
-	/* Make it static  for now */
-/*	struct	tcpiphdr *t_template;	/ * skeletal packet for transmit */
-	struct	tcpiphdr t_template;
+	struct	tcpiphdr t_template;    /* static skeletal packet for xmit */
 
 	struct	socket *t_socket;		/* back pointer to socket */
 /*
@@ -199,7 +197,6 @@ struct tcpstat {
 	u_long	tcps_rcvbyte;		/* bytes received in sequence */
 	u_long	tcps_rcvbadsum;		/* packets received with ccksum errs */
 	u_long	tcps_rcvbadoff;		/* packets received with bad offset */
-/*	u_long	tcps_rcvshort;	*/	/* packets received too short */
 	u_long	tcps_rcvduppack;	/* duplicate-only packets received */
 	u_long	tcps_rcvdupbyte;	/* duplicate-only bytes received */
 	u_long	tcps_rcvpartduppack;	/* packets with some duplicate data */
@@ -215,7 +212,6 @@ struct tcpstat {
 	u_long	tcps_rcvackpack;	/* rcvd ack packets */
 	u_long	tcps_rcvackbyte;	/* bytes acked by rcvd acks */
 	u_long	tcps_rcvwinupd;		/* rcvd window update packets */
-/*	u_long	tcps_pawsdrop;	*/	/* segments dropped due to PAWS */
 	u_long	tcps_predack;		/* times hdr predict ok for acks */
 	u_long	tcps_preddat;		/* times hdr predict ok for data pkts */
 	u_long	tcps_socachemiss;	/* tcp_last_so misses */
slirp/tftp.c +1 -1
index 7cb095f..3358e2b 100644
@@ -23,7 +23,7 @@
  */
 
 #include <slirp.h>
-#include "qemu-common.h" // for pstrcpy
+#include "qemu-common.h"
 
 struct tftp_session {
     int in_use;
slirp/udp.c +3 -25
index 60fe754..3798e34 100644
@@ -50,16 +50,6 @@ struct socket udb;
 static u_int8_t udp_tos(struct socket *so);
 static void udp_emu(struct socket *so, struct mbuf *m);
 
-/*
- * UDP protocol implementation.
- * Per RFC 768, August, 1980.
- */
-#ifndef	COMPAT_42
-#define UDPCKSUM 1
-#else
-#define UDPCKSUM 0 /* XXX */
-#endif
-
 struct	socket *udp_last_so = &udb;
 
 void
@@ -76,7 +66,6 @@ udp_input(register struct mbuf *m, int iphlen)
 {
 	register struct ip *ip;
 	register struct udphdr *uh;
-/*	struct mbuf *opts = 0;*/
 	int len;
 	struct ip save_ip;
 	struct socket *so;
@@ -129,14 +118,10 @@ udp_input(register struct mbuf *m, int iphlen)
 	/*
 	 * Checksum extended UDP header and data.
 	 */
-	if (UDPCKSUM && uh->uh_sum) {
+	if (uh->uh_sum) {
       memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
 	  ((struct ipovly *)ip)->ih_x1 = 0;
 	  ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
-	  /* keep uh_sum for ICMP reply
-	   * uh->uh_sum = cksum(m, len + sizeof (struct ip));
-	   * if (uh->uh_sum) {
-	   */
 	  if(cksum(m, len + sizeof(struct ip))) {
 	    STAT(udpstat.udps_badsum++);
 	    goto bad;
@@ -201,7 +186,6 @@ udp_input(register struct mbuf *m, int iphlen)
 	  /*
 	   * Setup fields
 	   */
-	  /* udp_last_so = so; */
 	  so->so_laddr = ip->ip_src;
 	  so->so_lport = uh->uh_sport;
 
@@ -246,7 +230,6 @@ udp_input(register struct mbuf *m, int iphlen)
 	return;
 bad:
 	m_freem(m);
-	/* if (opts) m_freem(opts); */
 	return;
 }
 
@@ -277,7 +260,7 @@ int udp_output2(struct socket *so, struct mbuf *m,
     memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
 	ui->ui_x1 = 0;
 	ui->ui_pr = IPPROTO_UDP;
-	ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
+	ui->ui_len = htons(m->m_len - sizeof(struct ip));
 	/* XXXXX Check for from-one-location sockets, or from-any-location sockets */
         ui->ui_src = saddr->sin_addr;
 	ui->ui_dst = daddr->sin_addr;
@@ -289,10 +272,8 @@ int udp_output2(struct socket *so, struct mbuf *m,
 	 * Stuff checksum and output datagram.
 	 */
 	ui->ui_sum = 0;
-	if (UDPCKSUM) {
-	    if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
+	if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
 		ui->ui_sum = 0xffff;
-	}
 	((struct ip *)ui)->ip_len = m->m_len;
 
 	((struct ip *)ui)->ip_ttl = IPDEFTTL;
@@ -363,8 +344,6 @@ void
 udp_detach(struct socket *so)
 {
 	closesocket(so->s);
-	/* if (so->so_m) m_free(so->so_m);    done by sofree */
-
 	sofree(so);
 }
 
@@ -651,7 +630,6 @@ udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
 		return NULL;
 	}
 	setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
-/*	setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
 
 	getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
author
#17 lore raw

PATCH 17/41 slirp: Drop statistic code

In reply to Jan Kiszka

As agreed on the mailing list, there is no interest in keeping the usually disabled slirp statistics in the tree. So this patch removes them.

---

 monitor.c          |    2 -
 net.c              |    5 --
 qemu-monitor.hx    |    2 -
 slirp/debug.c      |  154 ----------------------------------------------------
 slirp/icmp_var.h   |   18 ------
 slirp/if.h         |   20 -------
 slirp/ip.h         |   37 ------------
 slirp/ip_icmp.c    |   14 -----
 slirp/ip_input.c   |   21 -------
 slirp/ip_output.c  |    7 --
 slirp/libslirp.h   |    1 
 slirp/mbuf.c       |    5 --
 slirp/mbuf.h       |   12 ----
 slirp/slirp.h      |    9 ---
 slirp/tcp_input.c  |   49 +----------------
 slirp/tcp_output.c |   23 --------
 slirp/tcp_subr.c   |    7 --
 slirp/tcp_timer.c  |   11 ----
 slirp/tcp_var.h    |   63 ---------------------
 slirp/udp.c        |   11 ----
 slirp/udp.h        |   20 -------
 21 files changed, 6 insertions(+), 485 deletions(-)
monitor.c +0 -2
index 47fc9de..7fdd3a0 100644
@@ -1733,8 +1733,6 @@ static const mon_cmd_t info_cmds[] = {
       "", "show CPU statistics", },
 #endif
 #if defined(CONFIG_SLIRP)
-    { "slirp", "", do_info_slirp,
-      "", "show SLIRP statistics", },
     { "usernet", "", do_info_usernet,
       "", "show user network stack connection states", },
 #endif
net.c +0 -5
index 8bd3b25..99726cd 100644
@@ -1112,11 +1112,6 @@ void net_slirp_smb(const char *exported_dir)
 
 #endif /* !defined(_WIN32) */
 
-void do_info_slirp(Monitor *mon)
-{
-    slirp_stats();
-}
-
 struct GuestFwd {
     CharDriverState *hd;
     struct in_addr server;
qemu-monitor.hx +0 -2
index 359bdc6..e320f9f 100644
@@ -85,8 +85,6 @@ show the current VM name
 show the current VM UUID
 @item info cpustats
 show CPU statistics
-@item info slirp
-show SLIRP statistics (if available)
 @item info usernet
 show user network stack connection states
 @item info migrate
slirp/debug.c +0 -154

157 lines not on this page · raw message

slirp/icmp_var.h +0 -18
index 99d4c9e..edbb922 100644
@@ -34,20 +34,6 @@
 #define _NETINET_ICMP_VAR_H_
 
 /*
- * Variables related to this implementation
- * of the internet control message protocol.
- */
-struct icmpstat {
-/* statistics related to input messages processed */
-	u_long  icps_received;		/* #ICMP packets received */
-	u_long	icps_tooshort;		/* packet < ICMP_MINLEN */
-	u_long	icps_checksum;		/* bad checksum */
-	u_long	icps_notsupp;		/* #ICMP packets not supported */
-	u_long  icps_badtype;		/* #with bad type feild */
-	u_long	icps_reflect;		/* number of responses */
-};
-
-/*
  * Names for ICMP sysctl objects
  */
 #define	ICMPCTL_MASKREPL	1	/* allow replies to netmask requests */
@@ -60,8 +46,4 @@ struct icmpstat {
 	{ "stats", CTLTYPE_STRUCT }, \
 }
 
-#ifdef LOG_ENABLED
-extern struct icmpstat icmpstat;
-#endif
-
 #endif
slirp/if.h +0 -20
index d49bcd5..2fdc354 100644
@@ -28,24 +28,4 @@ extern	struct mbuf *next_m;
 
 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
 
-#ifdef LOG_ENABLED
-/* Interface statistics */
-struct slirp_ifstats {
-	u_int out_pkts;		/* Output packets */
-	u_int out_bytes;		/* Output bytes */
-	u_int out_errpkts;	/* Output Error Packets */
-	u_int out_errbytes;	/* Output Error Bytes */
-	u_int in_pkts;		/* Input packets */
-	u_int in_bytes;		/* Input bytes */
-	u_int in_errpkts;		/* Input Error Packets */
-	u_int in_errbytes;	/* Input Error Bytes */
-
-	u_int bytes_saved;	/* Number of bytes that compression "saved" */
-				/* ie: number of bytes that didn't need to be sent over the link
-				 * because of compression */
-
-	u_int in_mbad;		/* Bad incoming packets */
-};
-#endif
-
 #endif
slirp/ip.h +0 -37
index 7a7a9b9..e0d7674 100644
@@ -250,43 +250,6 @@ struct ipoption {
 	int8_t	ipopt_list[MAX_IPOPTLEN];	/* options proper */
 };
 
-#ifdef LOG_ENABLED
-/*
- * Structure attached to inpcb.ip_moptions and
- * passed to ip_output when IP multicast options are in use.
- */
-
-struct	ipstat {
-	u_long	ips_total;		/* total packets received */
-	u_long	ips_badsum;		/* checksum bad */
-	u_long	ips_tooshort;		/* packet too short */
-	u_long	ips_toosmall;		/* not enough data */
-	u_long	ips_badhlen;		/* ip header length < data size */
-	u_long	ips_badlen;		/* ip length < ip header length */
-	u_long	ips_fragments;		/* fragments received */
-	u_long	ips_fragdropped;	/* frags dropped (dups, out of space) */
-	u_long	ips_fragtimeout;	/* fragments timed out */
-	u_long	ips_forward;		/* packets forwarded */
-	u_long	ips_cantforward;	/* packets rcvd for unreachable dest */
-	u_long	ips_redirectsent;	/* packets forwarded on same net */
-	u_long	ips_noproto;		/* unknown or unsupported protocol */
-	u_long	ips_delivered;		/* datagrams delivered to upper level*/
-	u_long	ips_localout;		/* total ip packets generated here */
-	u_long	ips_odropped;		/* lost packets due to nobufs, etc. */
-	u_long	ips_reassembled;	/* total packets reassembled ok */
-	u_long	ips_fragmented;		/* datagrams successfully fragmented */
-	u_long	ips_ofragments;		/* output fragments created */
-	u_long	ips_cantfrag;		/* don't fragment flag was set, etc. */
-	u_long	ips_badoptions;		/* error in option processing */
-	u_long	ips_noroute;		/* packets discarded due to no route */
-	u_long	ips_badvers;		/* ip version != 4 */
-	u_long	ips_rawout;		/* total raw ip packets generated */
-	u_long	ips_unaligned;		/* times the ip packet was not aligned */
-};
-
-extern struct	ipstat	ipstat;
-#endif
-
 extern struct	ipq	ipq;			/* ip reass. queue */
 extern u_int16_t	ip_id;				/* ip packet ctr, for ids */
 
slirp/ip_icmp.c +0 -14
index c47ab1f..66b4d23 100644
@@ -33,10 +33,6 @@
 #include "slirp.h"
 #include "ip_icmp.h"
 
-#ifdef LOG_ENABLED
-struct icmpstat icmpstat;
-#endif
-
 /* The message sent when emulating PING */
 /* Be nice and tell them it's just a pseudo-ping packet */
 static const char icmp_ping_msg[] = "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
@@ -78,14 +74,11 @@ icmp_input(struct mbuf *m, int hlen)
   DEBUG_ARG("m = %lx", (long )m);
   DEBUG_ARG("m_len = %d", m->m_len);
 
-  STAT(icmpstat.icps_received++);
-
   /*
    * Locate icmp structure in mbuf, and check
    * that its not corrupted and of at least minimum length.
    */
   if (icmplen < ICMP_MINLEN) {          /* min 8 bytes payload */
-    STAT(icmpstat.icps_tooshort++);
   freeit:
     m_freem(m);
     goto end_error;
@@ -95,7 +88,6 @@ icmp_input(struct mbuf *m, int hlen)
   m->m_data += hlen;
   icp = mtod(m, struct icmp *);
   if (cksum(m, icmplen)) {
-    STAT(icmpstat.icps_checksum++);
     goto freeit;
   }
   m->m_len += hlen;
@@ -159,12 +151,10 @@ icmp_input(struct mbuf *m, int hlen)
   case ICMP_TSTAMP:
   case ICMP_MASKREQ:
   case ICMP_REDIRECT:
-    STAT(icmpstat.icps_notsupp++);
     m_freem(m);
     break;
 
   default:
-    STAT(icmpstat.icps_badtype++);
     m_freem(m);
   } /* swith */
 
@@ -299,8 +289,6 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
 
   (void ) ip_output((struct socket *)NULL, m);
 
-  STAT(icmpstat.icps_reflect++);
-
 end_error:
   return;
 }
@@ -354,6 +342,4 @@ icmp_reflect(struct mbuf *m)
   }
 
   (void ) ip_output((struct socket *)NULL, m);
-
-  STAT(icmpstat.icps_reflect++);
 }
slirp/ip_input.c +1 -20
index 1fdd8d6..0356eb5 100644
@@ -42,10 +42,6 @@
 #include <osdep.h>
 #include "ip_icmp.h"
 
-#ifdef LOG_ENABLED
-struct ipstat ipstat;
-#endif
-
 struct ipq ipq;
 
 static struct ip *ip_reass(register struct ip *ip,
@@ -82,24 +78,19 @@ ip_input(struct mbuf *m)
 	DEBUG_ARG("m = %lx", (long)m);
 	DEBUG_ARG("m_len = %d", m->m_len);
 
-	STAT(ipstat.ips_total++);
-
 	if (m->m_len < sizeof (struct ip)) {
-		STAT(ipstat.ips_toosmall++);
 		return;
 	}
 
 	ip = mtod(m, struct ip *);
 
 	if (ip->ip_v != IPVERSION) {
-		STAT(ipstat.ips_badvers++);
 		goto bad;
 	}
 
 	hlen = ip->ip_hl << 2;
 	if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
-	  STAT(ipstat.ips_badhlen++);                     /* or packet too short */
-	  goto bad;
+	  goto bad;                                  /* or packet too short */
 	}
 
         /* keep ip header intact for ICMP reply
@@ -107,7 +98,6 @@ ip_input(struct mbuf *m)
 	 * if (ip->ip_sum) {
 	 */
 	if(cksum(m,hlen)) {
-	  STAT(ipstat.ips_badsum++);
 	  goto bad;
 	}
 
@@ -116,7 +106,6 @@ ip_input(struct mbuf *m)
 	 */
 	NTOHS(ip->ip_len);
 	if (ip->ip_len < hlen) {
-		STAT(ipstat.ips_badlen++);
 		goto bad;
 	}
 	NTOHS(ip->ip_id);
@@ -129,7 +118,6 @@ ip_input(struct mbuf *m)
 	 * Drop packet if shorter than we expect.
 	 */
 	if (m->m_len < ip->ip_len) {
-		STAT(ipstat.ips_tooshort++);
 		goto bad;
 	}
 
@@ -210,11 +198,9 @@ ip_input(struct mbuf *m)
 		 * attempt reassembly; if it succeeds, proceed.
 		 */
 		if (ip->ip_tos & 1 || ip->ip_off) {
-			STAT(ipstat.ips_fragments++);
 			ip = ip_reass(ip, fp);
                         if (ip == NULL)
 				return;
-			STAT(ipstat.ips_reassembled++);
 			m = dtom(ip);
 		} else
 			if (fp)
@@ -226,7 +212,6 @@ ip_input(struct mbuf *m)
 	/*
 	 * Switch out to protocol's input routine.
 	 */
-	STAT(ipstat.ips_delivered++);
 	switch (ip->ip_p) {
 	 case IPPROTO_TCP:
 		tcp_input(m, hlen, (struct socket *)NULL);
@@ -238,7 +223,6 @@ ip_input(struct mbuf *m)
 		icmp_input(m, hlen);
 		break;
 	 default:
-		STAT(ipstat.ips_noproto++);
 		m_free(m);
 	}
 	return;
@@ -399,7 +383,6 @@ insert:
 	return ip;
 
 dropfrag:
-	STAT(ipstat.ips_fragdropped++);
 	m_freem(m);
         return NULL;
 }
@@ -468,7 +451,6 @@ ip_slowtimo(void)
         struct ipq *fp = container_of(l, struct ipq, ip_link);
         l = l->next;
 		if (--fp->ipq_ttl == 0) {
-			STAT(ipstat.ips_fragtimeout++);
 			ip_freef(fp);
 		}
 	}
@@ -671,7 +653,6 @@ typedef u_int32_t n_time;
 bad:
  	icmp_error(m, type, code, 0, 0);
 
-	STAT(ipstat.ips_badoptions++);
 	return (1);
 }
 
slirp/ip_output.c +0 -7
index 94bb0f2..3031f4d 100644
@@ -72,7 +72,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	ip->ip_off &= IP_DF;
 	ip->ip_id = htons(ip_id++);
 	ip->ip_hl = hlen >> 2;
-	STAT(ipstat.ips_localout++);
 
 	/*
 	 * If small enough for interface, can just send directly.
@@ -93,7 +92,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	 */
 	if (ip->ip_off & IP_DF) {
 		error = -1;
-		STAT(ipstat.ips_cantfrag++);
 		goto bad;
 	}
 
@@ -118,7 +116,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	  m = m_get();
           if (m == NULL) {
 	    error = -1;
-	    STAT(ipstat.ips_odropped++);
 	    goto sendorfree;
 	  }
 	  m->m_data += IF_MAXLINKHDR;
@@ -145,7 +142,6 @@ ip_output(struct socket *so, struct mbuf *m0)
 	  mhip->ip_sum = cksum(m, mhlen);
 	  *mnext = m;
 	  mnext = &m->m_nextpkt;
-	  STAT(ipstat.ips_ofragments++);
 	}
 	/*
 	 * Update first fragment by trimming what's been copied out
@@ -166,9 +162,6 @@ sendorfree:
 		else
 			m_freem(m);
 	}
-
-	if (error == 0)
-		STAT(ipstat.ips_fragmented++);
     }
 
 done:
slirp/libslirp.h +0 -1
index c48837c..8117187 100644
@@ -26,7 +26,6 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port);
 int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr,
                    int guest_port);
 
-void slirp_stats(void);
 void slirp_connection_info(Monitor *mon);
 
 void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
slirp/mbuf.c +1 -4
index 7963d34..888de86 100644
@@ -17,10 +17,9 @@
 
 #include <slirp.h>
 
-int mbuf_alloced = 0;
+static int mbuf_alloced;
 struct mbuf m_freelist, m_usedlist;
 #define MBUF_THRESH 30
-int mbuf_max = 0;
 
 /*
  * Find a nice value for msize
@@ -57,8 +56,6 @@ m_get(void)
 		mbuf_alloced++;
 		if (mbuf_alloced > MBUF_THRESH)
 			flags = M_DOFREE;
-		if (mbuf_alloced > mbuf_max)
-			mbuf_max = mbuf_alloced;
 	} else {
 		m = m_freelist.m_next;
 		remque(m);
slirp/mbuf.h +0 -12
index e259441..c482753 100644
@@ -114,19 +114,7 @@ struct mbuf {
 #define M_DOFREE		0x08	/* when m_free is called on the mbuf, free()
 					 * it rather than putting it on the free list */
 
-/*
- * Mbuf statistics. XXX
- */
-
-struct mbstat {
-	int mbs_alloced;		/* Number of mbufs allocated */
-
-};
-
-extern struct	mbstat mbstat;
-extern int mbuf_alloced;
 extern struct mbuf m_freelist, m_usedlist;
-extern int mbuf_max;
 
 void m_init _P((void));
 struct mbuf * m_get _P((void));
slirp/slirp.h +0 -9
index 0c62fd7..c308582 100644
@@ -3,15 +3,6 @@
 
 //#define DEBUG 1
 
-// Uncomment the following line to enable SLIRP statistics printing in Qemu
-//#define LOG_ENABLED
-
-#ifdef LOG_ENABLED
-#define STAT(expr) expr
-#else
-#define STAT(expr) do { } while(0)
-#endif
-
 #include "config-host.h"
 #include "slirp_config.h"
 
slirp/tcp_input.c +3 -46

197 lines not on this page · raw message

slirp/tcp_output.c +0 -23
index a11f3da..686e90d 100644
@@ -247,8 +247,6 @@ again:
 	/*
 	 * No reason to send a segment, just return.
 	 */
-	STAT(tcpstat.tcps_didnuttin++);
-
 	return (0);
 
 send:
@@ -292,16 +290,6 @@ send:
 	 * the template for sends on this connection.
 	 */
 	if (len) {
-		if (tp->t_force && len == 1)
-			STAT(tcpstat.tcps_sndprobe++);
-		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
-			STAT(tcpstat.tcps_sndrexmitpack++);
-			STAT(tcpstat.tcps_sndrexmitbyte += len);
-		} else {
-			STAT(tcpstat.tcps_sndpack++);
-			STAT(tcpstat.tcps_sndbyte += len);
-		}
-
 		m = m_get();
 		if (m == NULL) {
 			error = 1;
@@ -322,15 +310,6 @@ send:
 		if (off + len == so->so_snd.sb_cc)
 			flags |= TH_PUSH;
 	} else {
-		if (tp->t_flags & TF_ACKNOW)
-			STAT(tcpstat.tcps_sndacks++);
-		else if (flags & (TH_SYN|TH_FIN|TH_RST))
-			STAT(tcpstat.tcps_sndctrl++);
-		else if (SEQ_GT(tp->snd_up, tp->snd_una))
-			STAT(tcpstat.tcps_sndurg++);
-		else
-			STAT(tcpstat.tcps_sndwinup++);
-
 		m = m_get();
 		if (m == NULL) {
 			error = 1;
@@ -436,7 +415,6 @@ send:
 			if (tp->t_rtt == 0) {
 				tp->t_rtt = 1;
 				tp->t_rtseq = startseq;
-				STAT(tcpstat.tcps_segstimed++);
 			}
 		}
 
@@ -481,7 +459,6 @@ send:
 out:
 		return (error);
 	}
-	STAT(tcpstat.tcps_sndtotal++);
 
 	/*
 	 * Data sent (as far as we can tell).
slirp/tcp_subr.c +1 -6
index 892bd83..868382a 100644
@@ -222,9 +222,7 @@ struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
 		tp->t_state = TCPS_CLOSED;
 		(void) tcp_output(tp);
-		STAT(tcpstat.tcps_drops++);
-	} else
-		STAT(tcpstat.tcps_conndrops++);
+	}
 	return (tcp_close(tp));
 }
 
@@ -261,7 +259,6 @@ tcp_close(struct tcpcb *tp)
 	sbfree(&so->so_rcv);
 	sbfree(&so->so_snd);
 	sofree(so);
-	STAT(tcpstat.tcps_closed++);
 	return ((struct tcpcb *)0);
 }
 
@@ -442,8 +439,6 @@ tcp_connect(struct socket *inso)
 
 	tcp_template(tp);
 
-	STAT(tcpstat.tcps_connattempt++);
-
 	tp->t_state = TCPS_SYN_SENT;
 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
 	tp->iss = tcp_iss;
slirp/tcp_timer.c +0 -11
index 9cacb0f..cd71572 100644
@@ -32,10 +32,6 @@
 
 #include <slirp.h>
 
-#ifdef LOG_ENABLED
-struct   tcpstat tcpstat;        /* tcp statistics */
-#endif
-
 u_int32_t        tcp_now;                /* for RFC 1323 timestamps */
 
 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);
@@ -58,7 +54,6 @@ tcp_fasttimo(void)
 		    (tp->t_flags & TF_DELACK)) {
 			tp->t_flags &= ~TF_DELACK;
 			tp->t_flags |= TF_ACKNOW;
-			STAT(tcpstat.tcps_delack++);
 			(void) tcp_output(tp);
 		}
 }
@@ -180,7 +175,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 				 * We tried our best, now the connection must die!
 				 */
 				tp->t_rxtshift = TCP_MAXRXTSHIFT;
-				STAT(tcpstat.tcps_timeoutdrop++);
 				tp = tcp_drop(tp, tp->t_softerror);
 				/* tp->t_softerror : ETIMEDOUT); */ /* XXX */
 				return (tp); /* XXX */
@@ -192,7 +186,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 			 */
 			tp->t_rxtshift = 6;
 		}
-		STAT(tcpstat.tcps_rexmttimeo++);
 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
 		TCPT_RANGESET(tp->t_rxtcur, rexmt,
 		    (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
@@ -254,7 +247,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 	 * Force a byte to be output, if possible.
 	 */
 	case TCPT_PERSIST:
-		STAT(tcpstat.tcps_persisttimeo++);
 		tcp_setpersist(tp);
 		tp->t_force = 1;
 		(void) tcp_output(tp);
@@ -266,7 +258,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 	 * or drop connection if idle for too long.
 	 */
 	case TCPT_KEEP:
-		STAT(tcpstat.tcps_keeptimeo++);
 		if (tp->t_state < TCPS_ESTABLISHED)
 			goto dropit;
 
@@ -285,7 +276,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 			 * by the protocol spec, this requires the
 			 * correspondent TCP to respond.
 			 */
-			STAT(tcpstat.tcps_keepprobe++);
 			tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
 			    tp->rcv_nxt, tp->snd_una - 1, 0);
 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
@@ -294,7 +284,6 @@ tcp_timers(register struct tcpcb *tp, int timer)
 		break;
 
 	dropit:
-		STAT(tcpstat.tcps_keepdrops++);
 		tp = tcp_drop(tp, 0);
 		break;
 	}
slirp/tcp_var.h +0 -63

69 lines not on this page · raw message

slirp/udp.c +0 -11
index 3798e34..240e1bf 100644
@@ -41,10 +41,6 @@
 #include <slirp.h>
 #include "ip_icmp.h"
 
-#ifdef LOG_ENABLED
-struct udpstat udpstat;
-#endif
-
 struct socket udb;
 
 static u_int8_t udp_tos(struct socket *so);
@@ -74,8 +70,6 @@ udp_input(register struct mbuf *m, int iphlen)
 	DEBUG_ARG("m = %lx", (long)m);
 	DEBUG_ARG("iphlen = %d", iphlen);
 
-	STAT(udpstat.udps_ipackets++);
-
 	/*
 	 * Strip IP options, if any; should skip this,
 	 * make available to user, and use on returned packets,
@@ -101,7 +95,6 @@ udp_input(register struct mbuf *m, int iphlen)
 
 	if (ip->ip_len != len) {
 		if (len > ip->ip_len) {
-			STAT(udpstat.udps_badlen++);
 			goto bad;
 		}
 		m_adj(m, len - ip->ip_len);
@@ -123,7 +116,6 @@ udp_input(register struct mbuf *m, int iphlen)
 	  ((struct ipovly *)ip)->ih_x1 = 0;
 	  ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
 	  if(cksum(m, len + sizeof(struct ip))) {
-	    STAT(udpstat.udps_badsum++);
 	    goto bad;
 	  }
 	}
@@ -165,7 +157,6 @@ udp_input(register struct mbuf *m, int iphlen)
 		if (tmp == &udb) {
 		  so = NULL;
 		} else {
-		  STAT(udpstat.udpps_pcbcachemiss++);
 		  udp_last_so = so;
 		}
 	}
@@ -279,8 +270,6 @@ int udp_output2(struct socket *so, struct mbuf *m,
 	((struct ip *)ui)->ip_ttl = IPDEFTTL;
 	((struct ip *)ui)->ip_tos = iptos;
 
-	STAT(udpstat.udps_opackets++);
-
 	error = ip_output(so, m);
 
 	return (error);
slirp/udp.h +0 -20
index d4c2bea..3eed14a 100644
@@ -67,32 +67,12 @@ struct udpiphdr {
 #define ui_ulen         ui_u.uh_ulen
 #define ui_sum          ui_u.uh_sum
 
-#ifdef LOG_ENABLED
-struct udpstat {
-	                                /* input statistics: */
-	        u_long  udps_ipackets;          /* total input packets */
-	        u_long  udps_hdrops;            /* packet shorter than header */
-	        u_long  udps_badsum;            /* checksum error */
-	        u_long  udps_badlen;            /* data length larger than packet */
-	        u_long  udps_noport;            /* no socket on port */
-	        u_long  udps_noportbcast;       /* of above, arrived as broadcast */
-	        u_long  udps_fullsock;          /* not delivered, input socket full */
-	        u_long  udpps_pcbcachemiss;     /* input packets missing pcb cache */
-	                                /* output statistics: */
-	        u_long  udps_opackets;          /* total output packets */
-};
-#endif
-
 /*
  * Names for UDP sysctl objects
  */
 #define UDPCTL_CHECKSUM         1       /* checksum UDP packets */
 #define UDPCTL_MAXID            2
 
-#ifdef LOG_ENABLED
-extern struct udpstat udpstat;
-#endif
-
 extern struct socket udb;
 struct mbuf;
 
author
#18 lore raw

PATCH 18/41 slirp: Cleanup and basic reanimation of debug code

In reply to Jan Kiszka

---

 Makefile       |    2 +-
 slirp/bootp.c  |    2 +-
 slirp/debug.c  |   12 ------------
 slirp/debug.h  |   15 ++++++---------
 slirp/misc.c   |    4 ++++
 slirp/slirp.h  |    2 --
 slirp/socket.c |    3 ++-
 7 files changed, 14 insertions(+), 26 deletions(-)
 delete mode 100644 slirp/debug.c
Makefile +1 -1
index a06c9bf..c627bfb 100644
@@ -190,7 +190,7 @@ ifdef CONFIG_SLIRP
 CPPFLAGS+=-I$(SRC_PATH)/slirp
 SLIRP_OBJS=cksum.o if.o ip_icmp.o ip_input.o ip_output.o \
 slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o \
-tcp_subr.o tcp_timer.o udp.o bootp.o debug.o tftp.o
+tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
 OBJS+=$(addprefix slirp/, $(SLIRP_OBJS))
 endif
 
slirp/bootp.c +1 -1
index 97c2811..fec52c0 100644
@@ -42,7 +42,7 @@ static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
 #ifdef DEBUG
 #define dprintf(fmt, ...) \
-if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd); }
+do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd); } while (0)
 #else
 #define dprintf(fmt, ...)
 #endif
slirp/debug.c deleted +0 -12
deleted file mode 100644
index ad02df7..0000000
@@ -1,12 +0,0 @@
-/*
- * Copyright (c) 1995 Danny Gasparovski.
- * Portions copyright (c) 2000 Kelly Price.
- *
- * Please read the file COPYRIGHT for the
- * terms and conditions of the copyright.
- */
-
-#include <slirp.h>
-
-FILE *dfd = NULL;
-int slirp_debug = 0;
slirp/debug.h +6 -9
index 963ffda..6cfa61e 100644
@@ -5,25 +5,24 @@
  * terms and conditions of the copyright.
  */
 
-#define PRN_STDERR	1
-#define PRN_SPRINTF	2
+//#define DEBUG 1
 
-extern FILE *dfd;
-extern int slirp_debug;
+#ifdef DEBUG
 
 #define DBG_CALL 0x1
 #define DBG_MISC 0x2
 #define DBG_ERROR 0x4
-#define DEBUG_DEFAULT DBG_CALL|DBG_MISC|DBG_ERROR
 
-#ifdef DEBUG
+#define dfd stderr
+
+extern int slirp_debug;
+
 #define DEBUG_CALL(x) if (slirp_debug & DBG_CALL) { fprintf(dfd, "%s...\n", x); fflush(dfd); }
 #define DEBUG_ARG(x, y) if (slirp_debug & DBG_CALL) { fputc(' ', dfd); fprintf(dfd, x, y); fputc('\n', dfd); fflush(dfd); }
 #define DEBUG_ARGS(x) if (slirp_debug & DBG_CALL) { fprintf x ; fflush(dfd); }
 #define DEBUG_MISC(x) if (slirp_debug & DBG_MISC) { fprintf x ; fflush(dfd); }
 #define DEBUG_ERROR(x) if (slirp_debug & DBG_ERROR) {fprintf x ; fflush(dfd); }
 
-
 #else
 
 #define DEBUG_CALL(x)
@@ -33,5 +32,3 @@ extern int slirp_debug;
 #define DEBUG_ERROR(x)
 
 #endif
-
-void debug_init _P((char *, int));
slirp/misc.c +4 -0
index 9587112..ae6f211 100644
@@ -10,6 +10,10 @@
 
 #include "monitor.h"
 
+#ifdef DEBUG
+int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
+#endif
+
 u_int curtime, time_fasttimo, last_slowtimo;
 
 /*
slirp/slirp.h +0 -2
index c308582..1bd7c89 100644
@@ -1,8 +1,6 @@
 #ifndef __COMMON_H__
 #define __COMMON_H__
 
-//#define DEBUG 1
-
 #include "config-host.h"
 #include "slirp_config.h"
 
slirp/socket.c +2 -1
index a911b29..f7b212f 100644
@@ -584,7 +584,8 @@ tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags
 	socklen_t addrlen = sizeof(addr);
 
 	DEBUG_CALL("tcp_listen");
-	DEBUG_ARG("port = %d", port);
+	DEBUG_ARG("haddr = %x", haddr);
+	DEBUG_ARG("hport = %d", hport);
 	DEBUG_ARG("laddr = %x", laddr);
 	DEBUG_ARG("lport = %d", lport);
 	DEBUG_ARG("flags = %x", flags);
author
#19 lore raw

PATCH 19/41 slirp: Drop unused icmp_var.h

In reply to Jan Kiszka

---

 slirp/icmp_var.h |   49 -------------------------------------------------
 slirp/slirp.h    |    1 -
 2 files changed, 0 insertions(+), 50 deletions(-)
 delete mode 100644 slirp/icmp_var.h
slirp/icmp_var.h deleted +0 -49
deleted file mode 100644
index edbb922..0000000
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 1982, 1986, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *	@(#)icmp_var.h	8.1 (Berkeley) 6/10/93
- * icmp_var.h,v 1.4 1995/02/16 00:27:40 wollman Exp
- */
-
-#ifndef _NETINET_ICMP_VAR_H_
-#define _NETINET_ICMP_VAR_H_
-
-/*
- * Names for ICMP sysctl objects
- */
-#define	ICMPCTL_MASKREPL	1	/* allow replies to netmask requests */
-#define	ICMPCTL_STATS		2	/* statistics (read-only) */
-#define ICMPCTL_MAXID		3
-
-#define ICMPCTL_NAMES { \
-	{ 0, 0 }, \
-	{ "maskrepl", CTLTYPE_INT }, \
-	{ "stats", CTLTYPE_STRUCT }, \
-}
-
-#endif
slirp/slirp.h +0 -1
index 1bd7c89..063e5f6 100644
@@ -191,7 +191,6 @@ int inet_aton _P((const char *cp, struct in_addr *ia));
 #include "tcp_var.h"
 #include "tcpip.h"
 #include "udp.h"
-#include "icmp_var.h"
 #include "mbuf.h"
 #include "sbuf.h"
 #include "socket.h"
author
#20 lore raw

PATCH 20/41 slirp: tftp: Cleanup tftp_prefix check

In reply to Jan Kiszka

Perform check for set prefix early (if it's not given, tftp is disabled) and drop redundant second check.

---

 slirp/tftp.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
slirp/tftp.c +7 -8
index 3358e2b..5d5614a 100644
@@ -287,6 +287,12 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
 
   spt = &tftp_sessions[s];
 
+  /* unspecifed prefix means service disabled */
+  if (!tftp_prefix) {
+      tftp_send_error(spt, 2, "Access violation", tp);
+      return;
+  }
+
   src = tp->x.tp_buf;
   dst = spt->filename;
   n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
@@ -333,13 +339,6 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
       return;
   }
 
-  /* only allow exported prefixes */
-
-  if (!tftp_prefix) {
-      tftp_send_error(spt, 2, "Access violation", tp);
-      return;
-  }
-
   /* check if the file exists */
 
   if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
@@ -370,7 +369,7 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
 	  int tsize = atoi(value);
 	  struct stat stat_p;
 
-	  if (tsize == 0 && tftp_prefix) {
+	  if (tsize == 0) {
 	      char buffer[1024];
 	      int len;
 
author
#21 lore raw

PATCH 21/41 slirp: tftp: Clean up tftp_send_error

In reply to Jan Kiszka

The return code of tftp_send_error is not used, drop it. And also make sure to always terminate the session.

---

 slirp/tftp.c |   13 +++++--------
 1 files changed, 5 insertions(+), 8 deletions(-)
slirp/tftp.c +5 -8
index 5d5614a..75aa4b2 100644
@@ -167,11 +167,9 @@ static int tftp_send_oack(struct tftp_session *spt,
     return 0;
 }
 
-
-
-static int tftp_send_error(struct tftp_session *spt,
-			   u_int16_t errorcode, const char *msg,
-			   struct tftp_t *recv_tp)
+static void tftp_send_error(struct tftp_session *spt,
+                            u_int16_t errorcode, const char *msg,
+                            struct tftp_t *recv_tp)
 {
   struct sockaddr_in saddr, daddr;
   struct mbuf *m;
@@ -181,7 +179,7 @@ static int tftp_send_error(struct tftp_session *spt,
   m = m_get();
 
   if (!m) {
-    return -1;
+    goto out;
   }
 
   memset(m->m_data, 0, m->m_size);
@@ -207,9 +205,8 @@ static int tftp_send_error(struct tftp_session *spt,
 
   udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
 
+out:
   tftp_session_terminate(spt);
-
-  return 0;
 }
 
 static int tftp_send_data(struct tftp_session *spt,
author
#22 lore raw

PATCH 22/41 slirp: tftp: Refactor tftp_handle_rrq

In reply to Jan Kiszka

Specifically make the filename extraction more readable, and always report errors back to the client.

---

 slirp/tftp.c |   49 +++++++++++++++++++++----------------------------
 1 files changed, 21 insertions(+), 28 deletions(-)
slirp/tftp.c +21 -28
index 75aa4b2..8351908 100644
@@ -27,7 +27,7 @@
 
 struct tftp_session {
     int in_use;
-    unsigned char filename[TFTP_FILENAME_MAX];
+    char filename[TFTP_FILENAME_MAX];
 
     struct in_addr client_ip;
     u_int16_t client_port;
@@ -273,8 +273,8 @@ static int tftp_send_data(struct tftp_session *spt,
 static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
 {
   struct tftp_session *spt;
-  int s, k, n;
-  u_int8_t *src, *dst;
+  int s, k;
+  char *req_fname;
 
   s = tftp_session_allocate(tp);
 
@@ -290,37 +290,31 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
       return;
   }
 
-  src = tp->x.tp_buf;
-  dst = spt->filename;
-  n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
+  /* skip header fields */
+  k = 0;
+  pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
 
   /* get name */
+  req_fname = spt->filename;
 
-  for (k = 0; k < n; k++) {
-    if (k < TFTP_FILENAME_MAX) {
-      dst[k] = src[k];
-    }
-    else {
+  while (1) {
+    if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
+      tftp_send_error(spt, 2, "Access violation", tp);
       return;
     }
-
-    if (src[k] == '\0') {
+    req_fname[k] = (char)tp->x.tp_buf[k];
+    if (req_fname[k++] == '\0') {
       break;
     }
   }
 
-  if (k >= n) {
-    return;
-  }
-
-  k++;
-
   /* check mode */
-  if ((n - k) < 6) {
+  if ((pktlen - k) < 6) {
+    tftp_send_error(spt, 2, "Access violation", tp);
     return;
   }
 
-  if (memcmp(&src[k], "octet\0", 6) != 0) {
+  if (memcmp(&tp->x.tp_buf[k], "octet\0", 6) != 0) {
       tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
       return;
   }
@@ -337,29 +331,28 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
   }
 
   /* check if the file exists */
-
-  if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
+  if (tftp_read_data(spt, 0, NULL, 0) < 0) {
       tftp_send_error(spt, 1, "File not found", tp);
       return;
   }
 
-  if (src[n - 1] != 0) {
+  if (tp->x.tp_buf[pktlen - 1] != 0) {
       tftp_send_error(spt, 2, "Access violation", tp);
       return;
   }
 
-  while (k < n) {
+  while (k < pktlen) {
       const char *key, *value;
 
-      key = (char *)src + k;
+      key = (const char *)&tp->x.tp_buf[k];
       k += strlen(key) + 1;
 
-      if (k >= n) {
+      if (k >= pktlen) {
 	  tftp_send_error(spt, 2, "Access violation", tp);
 	  return;
       }
 
-      value = (char *)src + k;
+      value = (const char *)&tp->x.tp_buf[k];
       k += strlen(value) + 1;
 
       if (strcmp(key, "tsize") == 0) {
author
#23 lore raw

PATCH 23/41 slirp: tftp: Rework filename handling

In reply to Jan Kiszka

This changes the filename handling from a static buffer in tftp_session for the client-provided name + prefix to a dynamically allocated buffer that keeps the combined path in one place.

---

 slirp/tftp.c |   38 ++++++++++++++++----------------------
 1 files changed, 16 insertions(+), 22 deletions(-)
slirp/tftp.c +16 -22
index 8351908..31a1e25 100644
@@ -27,7 +27,7 @@
 
 struct tftp_session {
     int in_use;
-    char filename[TFTP_FILENAME_MAX];
+    char *filename;
 
     struct in_addr client_ip;
     u_int16_t client_port;
@@ -47,6 +47,7 @@ static void tftp_session_update(struct tftp_session *spt)
 
 static void tftp_session_terminate(struct tftp_session *spt)
 {
+  qemu_free(spt->filename);
   spt->in_use = 0;
 }
 
@@ -62,8 +63,10 @@ static int tftp_session_allocate(struct tftp_t *tp)
         goto found;
 
     /* sessions time out after 5 inactive seconds */
-    if ((int)(curtime - spt->timestamp) > 5000)
+    if ((int)(curtime - spt->timestamp) > 5000) {
+        qemu_free(spt->filename);
         goto found;
+    }
   }
 
   return -1;
@@ -103,15 +106,8 @@ static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
 {
   int fd;
   int bytes_read = 0;
-  char buffer[1024];
-  int n;
-
-  n = snprintf(buffer, sizeof(buffer), "%s/%s",
-	       tftp_prefix, spt->filename);
-  if (n >= sizeof(buffer))
-    return -1;
 
-  fd = open(buffer, O_RDONLY | O_BINARY);
+  fd = open(spt->filename, O_RDONLY | O_BINARY);
 
   if (fd < 0) {
     return -1;
@@ -274,6 +270,7 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
 {
   struct tftp_session *spt;
   int s, k;
+  size_t prefix_len;
   char *req_fname;
 
   s = tftp_session_allocate(tp);
@@ -294,8 +291,13 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
   k = 0;
   pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
 
+  /* prepend tftp_prefix */
+  prefix_len = strlen(tftp_prefix);
+  spt->filename = qemu_malloc(prefix_len + TFTP_FILENAME_MAX + 1);
+  memcpy(spt->filename, tftp_prefix, prefix_len);
+
   /* get name */
-  req_fname = spt->filename;
+  req_fname = spt->filename + prefix_len;
 
   while (1) {
     if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
@@ -322,10 +324,8 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
   k += 6; /* skipping octet */
 
   /* do sanity checks on the filename */
-
-  if ((spt->filename[0] != '/')
-      || (spt->filename[strlen((char *)spt->filename) - 1] == '/')
-      ||  strstr((char *)spt->filename, "/../")) {
+  if (req_fname[0] != '/' || req_fname[strlen(req_fname) - 1] == '/' ||
+      strstr(req_fname, "/../")) {
       tftp_send_error(spt, 2, "Access violation", tp);
       return;
   }
@@ -360,13 +360,7 @@ static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
 	  struct stat stat_p;
 
 	  if (tsize == 0) {
-	      char buffer[1024];
-	      int len;
-
-	      len = snprintf(buffer, sizeof(buffer), "%s/%s",
-			     tftp_prefix, spt->filename);
-
-	      if (stat(buffer, &stat_p) == 0)
+	      if (stat(spt->filename, &stat_p) == 0)
 		  tsize = stat_p.st_size;
 	      else {
 		  tftp_send_error(spt, 1, "File not found", tp);
author
#24 lore raw

PATCH 24/41 slirp: Factor out one-time initialization

In reply to Jan Kiszka

In order to prepare re-initialization and multi-instance slirp, factor out init code that is of global scope and (at least for now) only need to be run once.

This also fixes the potentially uninitialized use of our_addr in get_dns_addr.

---

 slirp/misc.c  |   17 ----------------
 slirp/misc.h  |    1 -
 slirp/slirp.c |   60 +++++++++++++++++++++++++++++++++++++++------------------
 3 files changed, 41 insertions(+), 37 deletions(-)
slirp/misc.c +0 -17
index ae6f211..cffb3a0 100644
@@ -16,23 +16,6 @@ int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
 
 u_int curtime, time_fasttimo, last_slowtimo;
 
-/*
- * Get our IP address and put it in our_addr
- */
-void
-getouraddr(void)
-{
-	char buff[256];
-	struct hostent *he = NULL;
-
-	if (gethostname(buff,256) == 0)
-            he = gethostbyname(buff);
-        if (he)
-            our_addr = *(struct in_addr *)he->h_addr;
-        if (our_addr.s_addr == 0)
-            our_addr.s_addr = loopback_addr.s_addr;
-}
-
 struct quehead {
 	struct quehead *qh_link;
 	struct quehead *qh_rlink;
slirp/misc.h +0 -1
index 82dd596..c21f69b 100644
@@ -63,7 +63,6 @@ extern int x_port, x_server, x_display;
 
 int show_x _P((char *, struct socket *));
 void redir_x _P((u_int32_t, int, int, int));
-void getouraddr _P((void));
 void slirp_insque _P((void *, void *));
 void slirp_remque _P((void *));
 int add_exec _P((struct ex_list **, int, char *, struct in_addr, int));
slirp/slirp.c +41 -19

93 lines not on this page · raw message

author
#25 lore raw

PATCH 25/41 slirp: Make IP packet ID consistent

In reply to Jan Kiszka

Currently, ip_id is always initialized to 0 on slirp startup (despite the broken attempt to derive it from the clock). This is good for reproducibility. But it is not preserved across save/restore. This patch therefore drops the dead initialization code from ip_init and introduces ip_id to the persistent slirp state.

---

 slirp/ip_input.c |    1 -
 slirp/slirp.c    |    8 +++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
slirp/ip_input.c +0 -1
index 0356eb5..b07d3d5 100644
@@ -59,7 +59,6 @@ void
 ip_init(void)
 {
 	ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
-	ip_id = tt.tv_sec & 0xffff;
 	udp_init();
 	tcp_init();
 }
slirp/slirp.c +7 -1
index ea66c0c..02f6fe5 100644
@@ -234,7 +234,7 @@ void slirp_init(int restricted, struct in_addr vnetwork,
     vdhcp_startaddr = vdhcp_start;
     vnameserver_addr = vnameserver;
 
-    register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
+    register_savevm("slirp", 0, 2, slirp_state_save, slirp_state_load, NULL);
 }
 
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
@@ -973,6 +973,8 @@ static void slirp_state_save(QEMUFile *f, void *opaque)
             slirp_socket_save(f, so);
         }
     qemu_put_byte(f, 0);
+
+    qemu_put_be16(f, ip_id);
 }
 
 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
@@ -1103,5 +1105,9 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
         so->extra = (void *)ex_ptr->ex_exec;
     }
 
+    if (version_id >= 2) {
+        ip_id = qemu_get_be16(f);
+    }
+
     return 0;
 }
#26 lore raw

Re: [Qemu-devel] [PATCH 25/41] slirp: Make IP packet ID consistent

Quoted 18 lines · Jan Kiszka

Currently, ip_id is always initialized to 0 on slirp startup (despite the broken attempt to derive it from the clock). This is good for reproducibility. But it is not preserved across save/restore. This patch therefore drops the dead initialization code from ip_init and introduces ip_id to the persistent slirp state.

---

 slirp/ip_input.c |    1 -
 slirp/slirp.c    |    8 +++++++-

 2 files changed, 7 insertions(+), 2 deletions(-)
slirp/ip_input.c +0 -0
index 0356eb5..b07d3d5 100644

slirp/ip_input.c void · line 62

Quoted 4 lines
slirp/ip_input.c +0 -1
@@ -59,7 +59,6 @@ void
  ip_init(void)
  {
        ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
-       ip_id = tt.tv_sec & 0xffff;

You removed the ip_id initialization and now it's never initialized in the code. That sounds wrong.

Best regards,
Filip Navara

author
#27 lore raw

Re: [Qemu-devel] [PATCH 25/41] slirp: Make IP packet ID consistent

Quoted 14 lines · Filip Navara
Quoted 13 lines · Jan Kiszka

Currently, ip_id is always initialized to 0 on slirp startup (despite the broken attempt to derive it from the clock). This is good for reproducibility. But it is not preserved across save/restore. This patch therefore drops the dead initialization code from ip_init and introduces ip_id to the persistent slirp state.

---

 slirp/ip_input.c |    1 -
 slirp/slirp.c    |    8 +++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
Quoted 12 lines

slirp/ip_input.c void · line 62

Quoted 9 lines
slirp/ip_input.c +0 -1
index 0356eb5..b07d3d5 100644
@@ -59,7 +59,6 @@ void
 ip_init(void)
 {
       ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
-       ip_id = tt.tv_sec & 0xffff;

You removed the ip_id initialization and now it's never initialized in the code. That sounds wrong.

Thanks for having a look! All this slirp code is really hairy and no fun to dig through.

But now to your remark: That removal is in fact not changing the behavior. ip_id is also initialized to 0 afterwards. To understand this one has to track the messy use of 'tt' across slirp. It is in fact carrying the time at some point (when select_fill is invoked), but not yet on ip_init.

I was thinking about fixing the above initialization to actually take the current time and derive ip_id, but then I thought it might be better to keep this for network traffic reproducibility across qemu starts.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#28 lore raw

Re: [Qemu-devel] [PATCH 25/41] slirp: Make IP packet ID consistent

Quoted 28 lines · Jan Kiszka
Quoted 14 lines · Filip Navara
Quoted 13 lines · Jan Kiszka

Currently, ip_id is always initialized to 0 on slirp startup (despite the broken attempt to derive it from the clock). This is good for reproducibility. But it is not preserved across save/restore. This patch therefore drops the dead initialization code from ip_init and introduces ip_id to the persistent slirp state.

---

 slirp/ip_input.c |    1 -
 slirp/slirp.c    |    8 +++++++-

 2 files changed, 7 insertions(+), 2 deletions(-)
Quoted 12 lines

slirp/ip_input.c void · line 62

Quoted 9 lines
slirp/ip_input.c +0 -1
index 0356eb5..b07d3d5 100644
@@ -59,7 +59,6 @@ void
  ip_init(void)
  {
        ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
-       ip_id = tt.tv_sec & 0xffff;

You removed the ip_id initialization and now it's never initialized in the code. That sounds wrong.

Quoted 2 lines

Thanks for having a look! All this slirp code is really hairy and no fun to dig through.

Totally agreed. At one point I was considering to replace it with lwip, but I don't have the time necessary for that and it would be hard to keep all the existing services (TFTP, DHCP, SMB) working.

Quoted 5 lines

But now to your remark: That removal is in fact not changing the behavior. ip_id is also initialized to 0 afterwards. To understand this one has to track the messy use of 'tt' across slirp. It is in fact carrying the time at some point (when select_fill is invoked), but not yet on ip_init.

Ah, I didn't see that, makes sense. It actually explains what I saw last week in the packet dump.

Quoted 3 lines

I was thinking about fixing the above initialization to actually take the current time and derive ip_id, but then I thought it might be better to keep this for network traffic reproducibility across qemu starts.

Agreed.

Best regards,
Filip Navara

author
#29 lore raw

PATCH 26/41 slirp: Clean up updtime

In reply to Jan Kiszka

Drop redundant typecasts in both variants and remove the pointless round-up in the UNIX version.

---

 slirp/main.h  |    1 -
 slirp/slirp.c |   13 +++++--------
 2 files changed, 5 insertions(+), 9 deletions(-)
slirp/main.h +0 -1
index 891220a..6fd38f9 100644
@@ -11,7 +11,6 @@
 
 #define TOWRITEMAX 512
 
-extern struct timeval tt;
 extern int link_up;
 extern int slirp_socket;
 extern int slirp_socket_unit;
slirp/slirp.c +5 -8
index 02f6fe5..20c691f 100644
@@ -54,7 +54,6 @@ static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
 int slirp_restrict;
 static int do_slowtimo;
 int link_up;
-struct timeval tt;
 struct ex_list *exec_list;
 
 /* XXX: suppress those select globals */
@@ -250,19 +249,17 @@ static void updtime(void)
     struct _timeb tb;
 
     _ftime(&tb);
-    curtime = (u_int)tb.time * (u_int)1000;
-    curtime += (u_int)tb.millitm;
+
+    curtime = tb.time * 1000 + tb.millitm;
 }
 #else
 static void updtime(void)
 {
-        gettimeofday(&tt, NULL);
+    struct timeval tv;
 
-	curtime = (u_int)tt.tv_sec * (u_int)1000;
-	curtime += (u_int)tt.tv_usec / (u_int)1000;
+    gettimeofday(&tv, NULL);
 
-	if ((tt.tv_usec % 1000) >= 500)
-	   curtime++;
+    curtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 #endif
 
author
#30 lore raw

PATCH 27/41 slirp: Kill slirp_is_inited

In reply to Jan Kiszka

Avoid the need for slirp_is_inited by refactoring the protected slirp_select_* functions. This also avoids the clearing of all fd sets on select errors.

---

 net.c            |    9 +--------
 net.h            |    1 -
 slirp/libslirp.h |   14 +++++++++++++-
 slirp/slirp.c    |   20 +++++++++++++-------
 vl.c             |   23 +++++------------------
 5 files changed, 32 insertions(+), 35 deletions(-)
net.c +1 -8
index 99726cd..b2ec4c1 100644
@@ -120,9 +120,7 @@
 #include "qemu_socket.h"
 #include "qemu-log.h"
 
-#if defined(CONFIG_SLIRP)
-#include "libslirp.h"
-#endif
+#include "slirp/libslirp.h"
 
 
 static VLANState *first_vlan;
@@ -712,11 +710,6 @@ void slirp_output(const uint8_t *pkt, int pkt_len)
     qemu_send_packet(slirp_vc, pkt, pkt_len);
 }
 
-int slirp_is_inited(void)
-{
-    return slirp_inited;
-}
-
 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
 {
 #ifdef DEBUG_SLIRP
net.h +0 -1
index 5a608e0..7fcb49d 100644
@@ -134,7 +134,6 @@ void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str);
 void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str);
 void net_slirp_redir(const char *redir_str);
 void net_cleanup(void);
-int slirp_is_inited(void);
 void net_client_check(void);
 void net_set_boot_mask(int boot_mask);
 void net_host_device_add(Monitor *mon, const char *device, const char *opts);
slirp/libslirp.h +13 -1
index 8117187..b0496d5 100644
@@ -3,6 +3,8 @@
 
 #include <qemu-common.h>
 
+#ifdef CONFIG_SLIRP
+
 void slirp_init(int restricted, struct in_addr vnetwork,
                 struct in_addr vnetmask, struct in_addr vhost,
                 const char *vhostname, const char *tftp_path,
@@ -12,7 +14,8 @@ void slirp_init(int restricted, struct in_addr vnetwork,
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
 
-void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds);
+void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
+                       int select_error);
 
 void slirp_input(const uint8_t *pkt, int pkt_len);
 
@@ -32,4 +35,13 @@ void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size);
 size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
 
+#else /* !CONFIG_SLIRP */
+
+static inline void slirp_select_fill(int *pnfds, fd_set *readfds,
+                                     fd_set *writefds, fd_set *xfds) { }
+
+static inline void slirp_select_poll(fd_set *readfds, fd_set *writefds,
+                                     fd_set *xfds, int select_error) { }
+#endif /* !CONFIG_SLIRP */
+
 #endif
slirp/slirp.c +13 -7
index 20c691f..cf52d36 100644
@@ -271,6 +271,10 @@ void slirp_select_fill(int *pnfds,
     int nfds;
     int tmp_time;
 
+    if (!link_up) {
+        return;
+    }
+
     /* fail safe */
     global_readfds = NULL;
     global_writefds = NULL;
@@ -281,7 +285,7 @@ void slirp_select_fill(int *pnfds,
 	 * First, TCP sockets
 	 */
 	do_slowtimo = 0;
-	if (link_up) {
+
 		/*
 		 * *_slowtimo needs calling if there are IP fragments
 		 * in the fragment queue, or there are TCP connections active
@@ -375,7 +379,6 @@ void slirp_select_fill(int *pnfds,
 				UPD_NFDS(so->s);
 			}
 		}
-	}
 
 	/*
 	 * Setup timeout to use minimum CPU usage, especially when idle
@@ -413,11 +416,16 @@ void slirp_select_fill(int *pnfds,
         *pnfds = nfds;
 }
 
-void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
+void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
+                       int select_error)
 {
     struct socket *so, *so_next;
     int ret;
 
+    if (!link_up) {
+        return;
+    }
+
     global_readfds = readfds;
     global_writefds = writefds;
     global_xfds = xfds;
@@ -428,7 +436,6 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 	/*
 	 * See if anything has timed out
 	 */
-	if (link_up) {
 		if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
 			tcp_fasttimo();
 			time_fasttimo = 0;
@@ -438,12 +445,11 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 			tcp_slowtimo();
 			last_slowtimo = curtime;
 		}
-	}
 
 	/*
 	 * Check sockets
 	 */
-	if (link_up) {
+	if (!select_error) {
 		/*
 		 * Check TCP sockets
 		 */
@@ -576,7 +582,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
 	/*
 	 * See if we can start outputting
 	 */
-	if (if_queued && link_up)
+	if (if_queued)
 	   if_start();
 
 	/* clear global file descriptor sets.
vl.c +5 -18
index c24655e..0f20d07 100644
@@ -165,9 +165,7 @@ int main(int argc, char **argv)
 
 #include "qemu_socket.h"
 
-#if defined(CONFIG_SLIRP)
-#include "libslirp.h"
-#endif
+#include "slirp/libslirp.h"
 
 //#define DEBUG_UNUSED_IOPORT
 //#define DEBUG_IOPORT
@@ -4266,11 +4264,8 @@ void main_loop_wait(int timeout)
     tv.tv_sec = timeout / 1000;
     tv.tv_usec = (timeout % 1000) * 1000;
 
-#if defined(CONFIG_SLIRP)
-    if (slirp_is_inited()) {
-        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
-    }
-#endif
+    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
+
     qemu_mutex_unlock_iothread();
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
     qemu_mutex_lock_iothread();
@@ -4297,16 +4292,8 @@ void main_loop_wait(int timeout)
                 pioh = &ioh->next;
         }
     }
-#if defined(CONFIG_SLIRP)
-    if (slirp_is_inited()) {
-        if (ret < 0) {
-            FD_ZERO(&rfds);
-            FD_ZERO(&wfds);
-            FD_ZERO(&xfds);
-        }
-        slirp_select_poll(&rfds, &wfds, &xfds);
-    }
-#endif
+
+    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
 
     /* rearm timer, if not periodic */
     if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
author
#31 lore raw

PATCH 28/41 slirp: Drop redundant checks from slirp_output

In reply to Jan Kiszka

Slirp doesn't invoke slirp[_can]_output before it is initialized. The motivation for these checks (3b7f5d479c) no longer applies. So drop them.

Note: slirp_vc will become invalid if the slirp stack is removed during runtime. But this is no new bug and will be fixed later.

---

 net.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
net.c +1 -3
index b2ec4c1..3ceaf33 100644
@@ -696,7 +696,7 @@ static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr);
 
 int slirp_can_output(void)
 {
-    return !slirp_vc || qemu_can_send_packet(slirp_vc);
+    return qemu_can_send_packet(slirp_vc);
 }
 
 void slirp_output(const uint8_t *pkt, int pkt_len)
@@ -705,8 +705,6 @@ void slirp_output(const uint8_t *pkt, int pkt_len)
     printf("slirp output:\n");
     hex_dump(stdout, pkt, pkt_len);
 #endif
-    if (!slirp_vc)
-        return;
     qemu_send_packet(slirp_vc, pkt, pkt_len);
 }
 
author
#32 lore raw

PATCH 29/41 slirp: Clean up timeout handling around slirp_select_fill/poll

In reply to Jan Kiszka

Again lot of dead code. Remove it.

---

 slirp/misc.c  |    2 --
 slirp/misc.h  |    1 -
 slirp/slirp.c |   40 ++++------------------------------------
 3 files changed, 4 insertions(+), 39 deletions(-)
slirp/misc.c +0 -2
index cffb3a0..ee14db5 100644
@@ -14,8 +14,6 @@
 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
 #endif
 
-u_int curtime, time_fasttimo, last_slowtimo;
-
 struct quehead {
 	struct quehead *qh_link;
 	struct quehead *qh_rlink;
slirp/misc.h +0 -1
index c21f69b..2f47888 100644
@@ -17,7 +17,6 @@ struct ex_list {
 };
 
 extern struct ex_list *exec_list;
-extern u_int time_fasttimo, last_slowtimo;
 
 #ifndef HAVE_STRDUP
 char *strdup _P((const char *));
slirp/slirp.c +4 -36
index cf52d36..9b51c0b 100644
@@ -52,13 +52,16 @@ static struct in_addr client_ipaddr;
 static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
 
 int slirp_restrict;
-static int do_slowtimo;
 int link_up;
 struct ex_list *exec_list;
 
 /* XXX: suppress those select globals */
 fd_set *global_readfds, *global_writefds, *global_xfds;
 
+u_int curtime;
+static u_int time_fasttimo, last_slowtimo;
+static int do_slowtimo;
+
 char slirp_hostname[33];
 
 #ifdef _WIN32
@@ -267,9 +270,7 @@ void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
 {
     struct socket *so, *so_next;
-    struct timeval timeout;
     int nfds;
-    int tmp_time;
 
     if (!link_up) {
         return;
@@ -380,39 +381,6 @@ void slirp_select_fill(int *pnfds,
 			}
 		}
 
-	/*
-	 * Setup timeout to use minimum CPU usage, especially when idle
-	 */
-
-	/*
-	 * First, see the timeout needed by *timo
-	 */
-	timeout.tv_sec = 0;
-	timeout.tv_usec = -1;
-	/*
-	 * If a slowtimo is needed, set timeout to 500ms from the last
-	 * slow timeout. If a fast timeout is needed, set timeout within
-	 * 200ms of when it was requested.
-	 */
-	if (do_slowtimo) {
-		/* XXX + 10000 because some select()'s aren't that accurate */
-		timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
-		if (timeout.tv_usec < 0)
-		   timeout.tv_usec = 0;
-		else if (timeout.tv_usec > 510000)
-		   timeout.tv_usec = 510000;
-
-		/* Can only fasttimo if we also slowtimo */
-		if (time_fasttimo) {
-			tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
-			if (tmp_time < 0)
-			   tmp_time = 0;
-
-			/* Choose the smallest of the 2 */
-			if (tmp_time < timeout.tv_usec)
-			   timeout.tv_usec = (u_int)tmp_time;
-		}
-	}
         *pnfds = nfds;
 }
 
author
#33 lore raw

PATCH 30/41 slirp: Drop link_up checks from if_output and slirp_socket_can_recv

In reply to Jan Kiszka

link_up is true once slirp is initialized, so these check are really not required.

---

 slirp/if.c    |    5 +----
 slirp/slirp.c |    3 ---
 2 files changed, 1 insertions(+), 7 deletions(-)
slirp/if.c +1 -4
index 1ef4bf9..58ab4c7 100644
@@ -138,10 +138,7 @@ diddit:
 	/*
 	 * This prevents us from malloc()ing too many mbufs
 	 */
-	if (link_up) {
-		/* if_start will check towrite */
-		if_start();
-	}
+	if_start();
 #endif
 }
 
slirp/slirp.c +0 -3
index 9b51c0b..71f79a3 100644
@@ -824,9 +824,6 @@ size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port)
 	struct iovec iov[2];
 	struct socket *so;
 
-    if (!link_up)
-        return 0;
-
 	so = slirp_find_ctl_socket(guest_addr, guest_port);
 
 	if (!so || so->so_state & SS_NOFDREF)
author
#34 lore raw

PATCH 31/41 slirp: Factor out internal state structure

In reply to Jan Kiszka

The essence of this patch is to stuff (almost) all global variables of the slirp stack into the structure Slirp. In this step, we still keep the structure as global variable, directly accessible by the whole stack. Changes to the external interface of slirp will be applied in the following patches.

---

 slirp/bootp.c      |   80 ++++++++++-------------
 slirp/bootp.h      |    7 ++
 slirp/if.c         |   50 +++++++-------
 slirp/if.h         |    6 --
 slirp/ip.h         |    3 -
 slirp/ip_icmp.c    |   19 +++--
 slirp/ip_input.c   |   80 ++++++++++++-----------
 slirp/ip_output.c  |    7 +-
 slirp/libslirp.h   |    3 +
 slirp/main.h       |   12 ---
 slirp/mbuf.c       |   30 ++++-----
 slirp/mbuf.h       |    9 +--
 slirp/misc.c       |    5 +
 slirp/misc.h       |    2 -
 slirp/slirp.c      |  182 +++++++++++++++++++++++++++-------------------------
 slirp/slirp.h      |   66 +++++++++++++++++--
 slirp/socket.c     |   37 +++++++----
 slirp/socket.h     |    9 +--
 slirp/tcp.h        |    4 -
 slirp/tcp_input.c  |   35 +++++-----
 slirp/tcp_output.c |    4 +
 slirp/tcp_subr.c   |   74 +++++++++++++--------
 slirp/tcp_timer.c  |   18 ++---
 slirp/tcp_timer.h  |    4 +
 slirp/tcp_var.h    |    2 -
 slirp/tftp.c       |   65 ++++++++-----------
 slirp/tftp.h       |   10 +++
 slirp/udp.c        |   56 +++++++++-------
 slirp/udp.h        |    8 +-
 29 files changed, 479 insertions(+), 408 deletions(-)
slirp/bootp.c +36 -44
index fec52c0..911ec71 100644
@@ -25,19 +25,8 @@
 
 /* XXX: only DHCP is supported */
 
-#define NB_ADDR 16
-
 #define LEASE_TIME (24 * 3600)
 
-typedef struct {
-    uint8_t allocated;
-    uint8_t macaddr[6];
-} BOOTPClient;
-
-static BOOTPClient bootp_clients[NB_ADDR];
-
-char *bootp_filename;
-
 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
 #ifdef DEBUG
@@ -47,35 +36,35 @@ do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd)
 #define dprintf(fmt, ...)
 #endif
 
-static BOOTPClient *get_new_addr(struct in_addr *paddr,
+static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
                                  const uint8_t *macaddr)
 {
     BOOTPClient *bc;
     int i;
 
-    for(i = 0; i < NB_ADDR; i++) {
-        bc = &bootp_clients[i];
+    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        bc = &slirp->bootp_clients[i];
         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &slirp->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
+    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
-static BOOTPClient *request_addr(const struct in_addr *paddr,
+static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
                                  const uint8_t *macaddr)
 {
     uint32_t req_addr = ntohl(paddr->s_addr);
-    uint32_t dhcp_addr = ntohl(vdhcp_startaddr.s_addr);
+    uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
     BOOTPClient *bc;
 
     if (req_addr >= dhcp_addr &&
-        req_addr < (dhcp_addr + NB_ADDR)) {
-        bc = &bootp_clients[req_addr - dhcp_addr];
+        req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
+        bc = &slirp->bootp_clients[req_addr - dhcp_addr];
         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
             bc->allocated = 1;
             return bc;
@@ -84,20 +73,21 @@ static BOOTPClient *request_addr(const struct in_addr *paddr,
     return NULL;
 }
 
-static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
+static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
+                              const uint8_t *macaddr)
 {
     BOOTPClient *bc;
     int i;
 
-    for(i = 0; i < NB_ADDR; i++) {
-        if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
+    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &slirp->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
+    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
@@ -148,7 +138,7 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
     }
 }
 
-static void bootp_reply(const struct bootp_t *bp)
+static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 {
     BOOTPClient *bc = NULL;
     struct mbuf *m;
@@ -173,10 +163,12 @@ static void bootp_reply(const struct bootp_t *bp)
         dhcp_msg_type != DHCPREQUEST)
         return;
     /* XXX: this is a hack to get the client mac address */
-    memcpy(client_ethaddr, bp->bp_hwaddr, 6);
+    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
 
-    if ((m = m_get()) == NULL)
+    m = m_get(slirp);
+    if (!m) {
         return;
+    }
     m->m_data += IF_MAXLINKHDR;
     rbp = (struct bootp_t *)m->m_data;
     m->m_data += sizeof(struct udpiphdr);
@@ -184,30 +176,30 @@ static void bootp_reply(const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr) {
-            bc = request_addr(preq_addr, client_ethaddr);
+            bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
             if (bc) {
                 daddr.sin_addr = *preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(&daddr.sin_addr, client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
             if (!bc) {
                 dprintf("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, client_ethaddr, 6);
+        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
     } else if (preq_addr) {
-        bc = request_addr(preq_addr, client_ethaddr);
+        bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
         if (bc) {
             daddr.sin_addr = *preq_addr;
-            memcpy(bc->macaddr, client_ethaddr, 6);
+            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
         } else {
             daddr.sin_addr.s_addr = 0;
         }
     } else {
-        bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
+        bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
         if (!bc) {
             /* if never assigned, behaves as if it was already
                assigned (windows fix because it remembers its address) */
@@ -215,7 +207,7 @@ static void bootp_reply(const struct bootp_t *bp)
         }
     }
 
-    saddr.sin_addr = vhost_addr;
+    saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
     daddr.sin_port = htons(BOOTP_CLIENT);
@@ -248,9 +240,9 @@ static void bootp_reply(const struct bootp_t *bp)
             *q++ = DHCPACK;
         }
 
-        if (bootp_filename)
+        if (slirp->bootp_filename)
             snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
-                     bootp_filename);
+                     slirp->bootp_filename);
 
         *q++ = RFC2132_SRV_ID;
         *q++ = 4;
@@ -259,10 +251,10 @@ static void bootp_reply(const struct bootp_t *bp)
 
         *q++ = RFC1533_NETMASK;
         *q++ = 4;
-        memcpy(q, &vnetwork_mask, 4);
+        memcpy(q, &slirp->vnetwork_mask, 4);
         q += 4;
 
-        if (!slirp_restrict) {
+        if (!slirp->restricted) {
             *q++ = RFC1533_GATEWAY;
             *q++ = 4;
             memcpy(q, &saddr.sin_addr, 4);
@@ -270,7 +262,7 @@ static void bootp_reply(const struct bootp_t *bp)
 
             *q++ = RFC1533_DNS;
             *q++ = 4;
-            memcpy(q, &vnameserver_addr, 4);
+            memcpy(q, &slirp->vnameserver_addr, 4);
             q += 4;
         }
 
@@ -280,11 +272,11 @@ static void bootp_reply(const struct bootp_t *bp)
         memcpy(q, &val, 4);
         q += 4;
 
-        if (*slirp_hostname) {
-            val = strlen(slirp_hostname);
+        if (*slirp->client_hostname) {
+            val = strlen(slirp->client_hostname);
             *q++ = RFC1533_HOSTNAME;
             *q++ = val;
-            memcpy(q, slirp_hostname, val);
+            memcpy(q, slirp->client_hostname, val);
             q += val;
         }
     } else {
@@ -315,6 +307,6 @@ void bootp_input(struct mbuf *m)
     struct bootp_t *bp = mtod(m, struct bootp_t *);
 
     if (bp->bp_op == BOOTP_REQUEST) {
-        bootp_reply(bp);
+        bootp_reply(m->slirp, bp);
     }
 }
slirp/bootp.h +7 -0
index 3d80515..30c30ab 100644
@@ -112,4 +112,11 @@ struct bootp_t {
     uint8_t bp_vend[DHCP_OPT_LEN];
 };
 
+typedef struct {
+    uint16_t allocated;
+    uint8_t macaddr[6];
+} BOOTPClient;
+
+#define NB_BOOTP_CLIENTS 16
+
 void bootp_input(struct mbuf *m);
slirp/if.c +23 -27

139 lines not on this page · raw message

slirp/if.h +0 -6
index 2fdc354..2dac1c7 100644
@@ -20,12 +20,6 @@
 /* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
 #define IF_MAXLINKHDR (2 + 14 + 40)
 
-extern int	if_queued;	/* Number of packets queued so far */
-
-extern	struct mbuf if_fastq;                  /* fast queue (for interactive data) */
-extern	struct mbuf if_batchq;                 /* queue for non-interactive data */
-extern	struct mbuf *next_m;
-
 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
 
 #endif
slirp/ip.h +0 -3
index e0d7674..574fc01 100644
@@ -250,7 +250,4 @@ struct ipoption {
 	int8_t	ipopt_list[MAX_IPOPTLEN];	/* options proper */
 };
 
-extern struct	ipq	ipq;			/* ip reass. queue */
-extern u_int16_t	ip_id;				/* ip packet ctr, for ids */
-
 #endif
slirp/ip_icmp.c +12 -7
index 66b4d23..95a4b39 100644
@@ -69,6 +69,7 @@ icmp_input(struct mbuf *m, int hlen)
   register struct icmp *icp;
   register struct ip *ip=mtod(m, struct ip *);
   int icmplen=ip->ip_len;
+  Slirp *slirp = m->slirp;
 
   DEBUG_CALL("icmp_input");
   DEBUG_ARG("m = %lx", (long )m);
@@ -98,12 +99,12 @@ icmp_input(struct mbuf *m, int hlen)
   case ICMP_ECHO:
     icp->icmp_type = ICMP_ECHOREPLY;
     ip->ip_len += hlen;	             /* since ip_input subtracts this */
-    if (ip->ip_dst.s_addr == vhost_addr.s_addr) {
+    if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
       icmp_reflect(m);
     } else {
       struct socket *so;
       struct sockaddr_in addr;
-      if ((so = socreate()) == NULL) goto freeit;
+      if ((so = socreate(slirp)) == NULL) goto freeit;
       if(udp_attach(so) == -1) {
 	DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
 		    errno,strerror(errno)));
@@ -122,10 +123,10 @@ icmp_input(struct mbuf *m, int hlen)
 
       /* Send the packet */
       addr.sin_family = AF_INET;
-      if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
-          vnetwork_addr.s_addr) {
+      if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+          slirp->vnetwork_addr.s_addr) {
 	/* It's an alias */
-	if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
+	if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
 	  addr.sin_addr = dns_addr;
 	} else {
 	  addr.sin_addr = loopback_addr;
@@ -222,7 +223,11 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   }
 
   /* make a copy */
-  if(!(m=m_get())) goto end_error;               /* get mbuf */
+  m = m_get(msrc->slirp);
+  if (!m) {
+      goto end_error;
+  }
+
   { int new_m_size;
     new_m_size=sizeof(struct ip )+ICMP_MINLEN+msrc->m_len+ICMP_MAXDATALEN;
     if(new_m_size>m->m_size) m_inc(m, new_m_size);
@@ -285,7 +290,7 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   ip->ip_ttl = MAXTTL;
   ip->ip_p = IPPROTO_ICMP;
   ip->ip_dst = ip->ip_src;    /* ip adresses */
-  ip->ip_src = vhost_addr;
+  ip->ip_src = m->slirp->vhost_addr;
 
   (void ) ip_output((struct socket *)NULL, m);
 
slirp/ip_input.c +41 -39

204 lines not on this page · raw message

slirp/ip_output.c +3 -4
index 3031f4d..dba2784 100644
@@ -40,8 +40,6 @@
 
 #include <slirp.h>
 
-u_int16_t ip_id;
-
 /* Number of packets queued before we start sending
  * (to prevent allocing too many mbufs) */
 #define IF_THRESH 10
@@ -55,6 +53,7 @@ u_int16_t ip_id;
 int
 ip_output(struct socket *so, struct mbuf *m0)
 {
+	Slirp *slirp = m0->slirp;
 	register struct ip *ip;
 	register struct mbuf *m = m0;
 	register int hlen = sizeof(struct ip );
@@ -70,7 +69,7 @@ ip_output(struct socket *so, struct mbuf *m0)
 	 */
 	ip->ip_v = IPVERSION;
 	ip->ip_off &= IP_DF;
-	ip->ip_id = htons(ip_id++);
+	ip->ip_id = htons(slirp->ip_id++);
 	ip->ip_hl = hlen >> 2;
 
 	/*
@@ -113,7 +112,7 @@ ip_output(struct socket *so, struct mbuf *m0)
 	mhlen = sizeof (struct ip);
 	for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
 	  register struct ip *mhip;
-	  m = m_get();
+	  m = m_get(slirp);
           if (m == NULL) {
 	    error = -1;
 	    goto sendorfree;
slirp/libslirp.h +3 -0
index b0496d5..3bda691 100644
@@ -5,6 +5,9 @@
 
 #ifdef CONFIG_SLIRP
 
+struct Slirp;
+typedef struct Slirp Slirp;
+
 void slirp_init(int restricted, struct in_addr vnetwork,
                 struct in_addr vnetmask, struct in_addr vhost,
                 const char *vhostname, const char *tftp_path,
slirp/main.h +1 -11
index 6fd38f9..90deb58 100644
@@ -31,11 +31,6 @@ extern char *slirp_tty;
 extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
-extern struct in_addr vnetwork_addr;
-extern struct in_addr vnetwork_mask;
-extern struct in_addr vhost_addr;
-extern struct in_addr vdhcp_startaddr;
-extern struct in_addr vnameserver_addr;
 extern struct in_addr our_addr;
 extern struct in_addr loopback_addr;
 extern struct in_addr dns_addr;
@@ -44,16 +39,11 @@ extern char *socket_path;
 extern int towrite_max;
 extern int ppp_exit;
 extern int tcp_keepintvl;
-extern uint8_t client_ethaddr[6];
-extern int slirp_restrict;
-extern char slirp_hostname[33];
-extern char *tftp_prefix;
-extern char *bootp_filename;
 
 #define PROTO_SLIP 0x1
 #ifdef USE_PPP
 #define PROTO_PPP 0x2
 #endif
 
-void if_encap(const uint8_t *ip_data, int ip_data_len);
+void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len);
 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags);
slirp/mbuf.c +15 -15
index 888de86..7652fea 100644
@@ -17,8 +17,6 @@
 
 #include <slirp.h>
 
-static int mbuf_alloced;
-struct mbuf m_freelist, m_usedlist;
 #define MBUF_THRESH 30
 
 /*
@@ -28,10 +26,10 @@ struct mbuf m_freelist, m_usedlist;
 #define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + sizeof(struct m_hdr ) + 6)
 
 void
-m_init(void)
+m_init(Slirp *slirp)
 {
-	m_freelist.m_next = m_freelist.m_prev = &m_freelist;
-	m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
+    slirp->m_freelist.m_next = slirp->m_freelist.m_prev = &slirp->m_freelist;
+    slirp->m_usedlist.m_next = slirp->m_usedlist.m_prev = &slirp->m_usedlist;
 }
 
 /*
@@ -43,26 +41,27 @@ m_init(void)
  * which tells m_free to actually free() it
  */
 struct mbuf *
-m_get(void)
+m_get(Slirp *slirp)
 {
 	register struct mbuf *m;
 	int flags = 0;
 
 	DEBUG_CALL("m_get");
 
-	if (m_freelist.m_next == &m_freelist) {
+	if (slirp->m_freelist.m_next == &slirp->m_freelist) {
 		m = (struct mbuf *)malloc(SLIRP_MSIZE);
 		if (m == NULL) goto end_error;
-		mbuf_alloced++;
-		if (mbuf_alloced > MBUF_THRESH)
+		slirp->mbuf_alloced++;
+		if (slirp->mbuf_alloced > MBUF_THRESH)
 			flags = M_DOFREE;
+		m->slirp = slirp;
 	} else {
-		m = m_freelist.m_next;
+		m = slirp->m_freelist.m_next;
 		remque(m);
 	}
 
 	/* Insert it in the used list */
-	insque(m,&m_usedlist);
+	insque(m,&slirp->m_usedlist);
 	m->m_flags = (flags | M_USEDLIST);
 
 	/* Initialise it */
@@ -97,9 +96,9 @@ m_free(struct mbuf *m)
 	 */
 	if (m->m_flags & M_DOFREE) {
 		free(m);
-		mbuf_alloced--;
+		m->slirp->mbuf_alloced--;
 	} else if ((m->m_flags & M_FREELIST) == 0) {
-		insque(m,&m_freelist);
+		insque(m,&m->slirp->m_freelist);
 		m->m_flags = M_FREELIST; /* Clobber other flags */
 	}
   } /* if(m) */
@@ -194,7 +193,7 @@ m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
  * Fortunately, it's not used often
  */
 struct mbuf *
-dtom(void *dat)
+dtom(Slirp *slirp, void *dat)
 {
 	struct mbuf *m;
 
@@ -202,7 +201,8 @@ dtom(void *dat)
 	DEBUG_ARG("dat = %lx", (long )dat);
 
 	/* bug corrected for M_EXT buffers */
-	for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
+	for (m = slirp->m_usedlist.m_next; m != &slirp->m_usedlist;
+	     m = m->m_next) {
 	  if (m->m_flags & M_EXT) {
 	    if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
 	      return m;
slirp/mbuf.h +4 -5
index c482753..99fca43 100644
@@ -84,6 +84,7 @@ struct m_hdr {
 
 struct mbuf {
 	struct	m_hdr m_hdr;
+	Slirp *slirp;
 	union M_dat {
 		char	m_dat_[1]; /* ANSI don't like 0 sized arrays */
 		char	*m_ext_;
@@ -114,15 +115,13 @@ struct mbuf {
 #define M_DOFREE		0x08	/* when m_free is called on the mbuf, free()
 					 * it rather than putting it on the free list */
 
-extern struct mbuf m_freelist, m_usedlist;
-
-void m_init _P((void));
-struct mbuf * m_get _P((void));
+void m_init _P((Slirp *));
+struct mbuf * m_get _P((Slirp *));
 void m_free _P((struct mbuf *));
 void m_cat _P((register struct mbuf *, register struct mbuf *));
 void m_inc _P((struct mbuf *, int));
 void m_adj _P((struct mbuf *, int));
 int m_copy _P((struct mbuf *, struct mbuf *, int, int));
-struct mbuf * dtom _P((void *));
+struct mbuf * dtom _P((Slirp *, void *));
 
 #endif
slirp/misc.c +3 -2
index ee14db5..c2b66fd 100644
@@ -385,6 +385,7 @@ void slirp_connection_info(Monitor *mon)
         [TCPS_FIN_WAIT_2]   = "FIN_WAIT_2",
         [TCPS_TIME_WAIT]    = "TIME_WAIT",
     };
+    Slirp *slirp = &slirp_instance;
     struct in_addr dst_addr;
     struct sockaddr_in src;
     socklen_t src_len;
@@ -397,7 +398,7 @@ void slirp_connection_info(Monitor *mon)
     monitor_printf(mon, "  Protocol[State]    FD  Source Address  Port   "
                         "Dest. Address  Port RecvQ SendQ\n");
 
-    for (so = tcb.so_next; so != &tcb; so = so->so_next) {
+    for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
         if (so->so_state & SS_HOSTFWD) {
             state = "HOST_FORWARD";
         } else if (so->so_tcpcb) {
@@ -427,7 +428,7 @@ void slirp_connection_info(Monitor *mon)
                        so->so_rcv.sb_cc, so->so_snd.sb_cc);
     }
 
-    for (so = udb.so_next; so != &udb; so = so->so_next) {
+    for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
         if (so->so_state & SS_HOSTFWD) {
             n = snprintf(buf, sizeof(buf), "  UDP[HOST_FORWARD]");
             src_len = sizeof(src);
slirp/misc.h +0 -2
index 2f47888..e43519d 100644
@@ -16,8 +16,6 @@ struct ex_list {
 	struct ex_list *ex_next;
 };
 
-extern struct ex_list *exec_list;
-
 #ifndef HAVE_STRDUP
 char *strdup _P((const char *));
 #endif
slirp/slirp.c +96 -86

439 lines not on this page · raw message

slirp/slirp.h +60 -6

100 lines not on this page · raw message

slirp/socket.c +23 -14
index f7b212f..d8fbe89 100644
@@ -41,7 +41,7 @@ solookup(struct socket *head, struct in_addr laddr, u_int lport,
  * insque() it into the correct linked-list
  */
 struct socket *
-socreate(void)
+socreate(Slirp *slirp)
 {
   struct socket *so;
 
@@ -50,6 +50,7 @@ socreate(void)
     memset(so, 0, sizeof(struct socket));
     so->so_state = SS_NOFDREF;
     so->s = -1;
+    so->slirp = slirp;
   }
   return(so);
 }
@@ -60,15 +61,17 @@ socreate(void)
 void
 sofree(struct socket *so)
 {
+  Slirp *slirp = so->slirp;
+
   if (so->so_emu==EMU_RSH && so->extra) {
 	sofree(so->extra);
 	so->extra=NULL;
   }
-  if (so == tcp_last_so)
-    tcp_last_so = &tcb;
-  else if (so == udp_last_so)
-    udp_last_so = &udb;
-
+  if (so == slirp->tcp_last_so) {
+      slirp->tcp_last_so = &slirp->tcb;
+  } else if (so == slirp->udp_last_so) {
+      slirp->udp_last_so = &slirp->udb;
+  }
   m_free(so->so_m);
 
   if(so->so_next && so->so_prev)
@@ -473,7 +476,10 @@ sorecvfrom(struct socket *so)
           int n;
 #endif
 
-	  if (!(m = m_get())) return;
+	  m = m_get(so->slirp);
+	  if (!m) {
+	      return;
+	  }
 	  m->m_data += IF_MAXLINKHDR;
 
 	  /*
@@ -533,6 +539,7 @@ sorecvfrom(struct socket *so)
 int
 sosendto(struct socket *so, struct mbuf *m)
 {
+	Slirp *slirp = so->slirp;
 	int ret;
 	struct sockaddr_in addr;
 
@@ -541,10 +548,10 @@ sosendto(struct socket *so, struct mbuf *m)
 	DEBUG_ARG("m = %lx", (long)m);
 
         addr.sin_family = AF_INET;
-	if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
-	    vnetwork_addr.s_addr) {
+	if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+	    slirp->vnetwork_addr.s_addr) {
 	  /* It's an alias */
-	  if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
+	  if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
 	    addr.sin_addr = dns_addr;
 	  } else {
 	    addr.sin_addr = loopback_addr;
@@ -576,7 +583,8 @@ sosendto(struct socket *so, struct mbuf *m)
  * Listen for incoming TCP connections
  */
 struct socket *
-tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags)
+tcp_listen(Slirp *slirp, u_int32_t haddr, u_int hport, u_int32_t laddr,
+           u_int lport, int flags)
 {
 	struct sockaddr_in addr;
 	struct socket *so;
@@ -590,7 +598,8 @@ tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags
 	DEBUG_ARG("lport = %d", lport);
 	DEBUG_ARG("flags = %x", flags);
 
-	if ((so = socreate()) == NULL) {
+	so = socreate(slirp);
+	if (!so) {
 	  return NULL;
 	}
 
@@ -599,7 +608,7 @@ tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags
 		free(so);
 		return NULL;
 	}
-	insque(so,&tcb);
+	insque(so, &slirp->tcb);
 
 	/*
 	 * SS_FACCEPTONCE sockets must time out.
@@ -637,7 +646,7 @@ tcp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport, int flags
 	getsockname(s,(struct sockaddr *)&addr,&addrlen);
 	so->so_fport = addr.sin_port;
 	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = vhost_addr;
+	   so->so_faddr = slirp->vhost_addr;
 	else
 	   so->so_faddr = addr.sin_addr;
 
slirp/socket.h +5 -4
index 4db2147..dcf2743 100644
@@ -20,6 +20,8 @@ struct socket {
 
   int s;                           /* The actual socket */
 
+  Slirp *slirp;			   /* managing slirp instance */
+
 			/* XXX union these with not-yet-used sbuf params */
   struct mbuf *so_m;	           /* Pointer to the original SYN packet,
 				    * for non-blocking connect()'s, and
@@ -72,10 +74,8 @@ struct socket {
 #define SS_HOSTFWD		0x1000	/* Socket describes host->guest forwarding */
 #define SS_INCOMING		0x2000	/* Connection was initiated by a host on the internet */
 
-extern struct socket tcb;
-
 struct socket * solookup _P((struct socket *, struct in_addr, u_int, struct in_addr, u_int));
-struct socket * socreate _P((void));
+struct socket * socreate _P((Slirp *));
 void sofree _P((struct socket *));
 int soread _P((struct socket *));
 void sorecvoob _P((struct socket *));
@@ -83,7 +83,8 @@ int sosendoob _P((struct socket *));
 int sowrite _P((struct socket *));
 void sorecvfrom _P((struct socket *));
 int sosendto _P((struct socket *, struct mbuf *));
-struct socket * tcp_listen _P((u_int32_t, u_int, u_int32_t, u_int, int));
+struct socket * tcp_listen _P((Slirp *, u_int32_t, u_int, u_int32_t, u_int,
+                               int));
 void soisfconnecting _P((register struct socket *));
 void soisfconnected _P((register struct socket *));
 void sofwdrain _P((struct socket *));
slirp/tcp.h +0 -4
index 4a18ed8..c65ed43 100644
@@ -38,8 +38,6 @@ typedef	u_int32_t	tcp_seq;
 #define      PR_SLOWHZ       2               /* 2 slow timeouts per second (approx) */
 #define      PR_FASTHZ       5               /* 5 fast timeouts per second (not important) */
 
-extern struct socket *tcp_last_so;
-
 #define TCP_SNDSPACE 8192
 #define TCP_RCVSPACE 8192
 
@@ -163,6 +161,4 @@ struct tcphdr {
 
 #define TCP_ISSINCR     (125*1024)      /* increment for tcp_iss each second */
 
-extern tcp_seq tcp_iss;                /* tcp initial send seq # */
-
 #endif
slirp/tcp_input.c +17 -18
index b7a67e5..47cf0ad 100644
@@ -41,12 +41,7 @@
 #include <slirp.h>
 #include "ip_icmp.h"
 
-struct socket tcb;
-
 #define	TCPREXMTTHRESH 3
-struct	socket *tcp_last_so = &tcb;
-
-tcp_seq tcp_iss;                /* tcp initial send seq # */
 
 #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
 
@@ -233,6 +228,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	u_long tiwin;
 	int ret;
     struct ex_list *ex_ptr;
+    Slirp *slirp;
 
 	DEBUG_CALL("tcp_input");
 	DEBUG_ARGS((dfd," m = %8lx  iphlen = %2d  inso = %lx\n",
@@ -243,6 +239,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	 */
 	if (m == NULL) {
 		so = inso;
+		slirp = so->slirp;
 
 		/* Re-set a few variables */
 		tp = sototcpcb(so);
@@ -254,6 +251,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 
 		goto cont_conn;
 	}
+	slirp = m->slirp;
 
 	/*
 	 * Get IP and TCP header together in first mbuf.
@@ -318,8 +316,8 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 
-    if (slirp_restrict) {
-        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+    if (slirp->restricted) {
+        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
             if (ex_ptr->ex_fport == ti->ti_dport &&
                 ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
                 break;
@@ -332,15 +330,15 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	 * Locate pcb for segment.
 	 */
 findso:
-	so = tcp_last_so;
+	so = slirp->tcp_last_so;
 	if (so->so_fport != ti->ti_dport ||
 	    so->so_lport != ti->ti_sport ||
 	    so->so_laddr.s_addr != ti->ti_src.s_addr ||
 	    so->so_faddr.s_addr != ti->ti_dst.s_addr) {
-		so = solookup(&tcb, ti->ti_src, ti->ti_sport,
+		so = solookup(&slirp->tcb, ti->ti_src, ti->ti_sport,
 			       ti->ti_dst, ti->ti_dport);
 		if (so)
-			tcp_last_so = so;
+			slirp->tcp_last_so = so;
 	}
 
 	/*
@@ -360,7 +358,7 @@ findso:
 	  if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
 	    goto dropwithreset;
 
-	  if ((so = socreate()) == NULL)
+	  if ((so = socreate(slirp)) == NULL)
 	    goto dropwithreset;
 	  if (tcp_attach(so) < 0) {
 	    free(so); /* Not sofree (if it failed, it's not insqued) */
@@ -555,12 +553,13 @@ findso:
 	   * If this is destined for the control address, then flag to
 	   * tcp_ctl once connected, otherwise connect
 	   */
-	  if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
-	      vnetwork_addr.s_addr) {
-	    if (so->so_faddr.s_addr != vhost_addr.s_addr &&
-		so->so_faddr.s_addr != vnameserver_addr.s_addr) {
+	  if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+	      slirp->vnetwork_addr.s_addr) {
+	    if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
+		so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
 		/* May be an add exec */
-		for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
+		for (ex_ptr = slirp->exec_list; ex_ptr;
+		     ex_ptr = ex_ptr->ex_next) {
 		  if(ex_ptr->ex_fport == so->so_fport &&
 		     so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
 		    so->so_state |= SS_CTL;
@@ -631,8 +630,8 @@ findso:
 	  if (iss)
 	    tp->iss = iss;
 	  else
-	    tp->iss = tcp_iss;
-	  tcp_iss += TCP_ISSINCR/2;
+	    tp->iss = slirp->tcp_iss;
+	  slirp->tcp_iss += TCP_ISSINCR/2;
 	  tp->irs = ti->ti_seq;
 	  tcp_sendseqinit(tp);
 	  tcp_rcvseqinit(tp);
slirp/tcp_output.c +2 -2
index 686e90d..0d6011a 100644
@@ -290,7 +290,7 @@ send:
 	 * the template for sends on this connection.
 	 */
 	if (len) {
-		m = m_get();
+		m = m_get(so->slirp);
 		if (m == NULL) {
 			error = 1;
 			goto out;
@@ -310,7 +310,7 @@ send:
 		if (off + len == so->so_snd.sb_cc)
 			flags |= TH_PUSH;
 	} else {
-		m = m_get();
+		m = m_get(so->slirp);
 		if (m == NULL) {
 			error = 1;
 			goto out;
slirp/tcp_subr.c +46 -28

214 lines not on this page · raw message

slirp/tcp_timer.c +8 -10
index cd71572..556e32e 100644
@@ -32,24 +32,22 @@
 
 #include <slirp.h>
 
-u_int32_t        tcp_now;                /* for RFC 1323 timestamps */
-
 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);
 
 /*
  * Fast timeout routine for processing delayed acks
  */
 void
-tcp_fasttimo(void)
+tcp_fasttimo(Slirp *slirp)
 {
 	register struct socket *so;
 	register struct tcpcb *tp;
 
 	DEBUG_CALL("tcp_fasttimo");
 
-	so = tcb.so_next;
+	so = slirp->tcb.so_next;
 	if (so)
-	for (; so != &tcb; so = so->so_next)
+	for (; so != &slirp->tcb; so = so->so_next)
 		if ((tp = (struct tcpcb *)so->so_tcpcb) &&
 		    (tp->t_flags & TF_DELACK)) {
 			tp->t_flags &= ~TF_DELACK;
@@ -64,7 +62,7 @@ tcp_fasttimo(void)
  * causes finite state machine actions if timers expire.
  */
 void
-tcp_slowtimo(void)
+tcp_slowtimo(Slirp *slirp)
 {
 	register struct socket *ip, *ipnxt;
 	register struct tcpcb *tp;
@@ -75,10 +73,10 @@ tcp_slowtimo(void)
 	/*
 	 * Search through tcb's and update active timers.
 	 */
-	ip = tcb.so_next;
+	ip = slirp->tcb.so_next;
 	if (ip == 0)
 	   return;
-	for (; ip != &tcb; ip = ipnxt) {
+	for (; ip != &slirp->tcb; ip = ipnxt) {
 		ipnxt = ip->so_next;
 		tp = sototcpcb(ip);
 		if (tp == 0)
@@ -96,8 +94,8 @@ tcp_slowtimo(void)
 tpgone:
 		;
 	}
-	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
-	tcp_now++;					/* for timestamps */
+	slirp->tcp_iss += TCP_ISSINCR/PR_SLOWHZ;	/* increment iss */
+	slirp->tcp_now++;				/* for timestamps */
 }
 
 /*
slirp/tcp_timer.h +2 -2
index f648ed0..cfb06ff 100644
@@ -120,8 +120,8 @@ extern const int tcp_backoff[];
 
 struct tcpcb;
 
-void tcp_fasttimo _P((void));
-void tcp_slowtimo _P((void));
+void tcp_fasttimo _P((Slirp *));
+void tcp_slowtimo _P((Slirp *));
 void tcp_canceltimers _P((struct tcpcb *));
 
 #endif
slirp/tcp_var.h +0 -2
index db0a9c9..4ffbe04 100644
@@ -158,6 +158,4 @@ struct tcpcb {
 #define	TCP_REXMTVAL(tp) \
 	(((tp)->t_srtt >> TCP_RTT_SHIFT) + (tp)->t_rttvar)
 
-extern u_int32_t	tcp_now;		/* for RFC 1323 timestamps */
-
 #endif
slirp/tftp.c +28 -37

171 lines not on this page · raw message

slirp/tftp.h +10 -0
index 8f2675e..1415c85 100644
@@ -30,4 +30,14 @@ struct tftp_t {
   } x;
 };
 
+struct tftp_session {
+    Slirp *slirp;
+    char *filename;
+
+    struct in_addr client_ip;
+    u_int16_t client_port;
+
+    int timestamp;
+};
+
 void tftp_input(struct mbuf *m);
slirp/udp.c +31 -25

140 lines not on this page · raw message

slirp/udp.h +3 -5
index 3eed14a..cc814a3 100644
@@ -36,8 +36,6 @@
 #define UDP_TTL 0x60
 #define UDP_UDPDATALEN 16192
 
-extern struct socket *udp_last_so;
-
 /*
  * Udp protocol header.
  * Per RFC 768, September, 1981.
@@ -73,15 +71,15 @@ struct udpiphdr {
 #define UDPCTL_CHECKSUM         1       /* checksum UDP packets */
 #define UDPCTL_MAXID            2
 
-extern struct socket udb;
 struct mbuf;
 
-void udp_init _P((void));
+void udp_init _P((Slirp *));
 void udp_input _P((register struct mbuf *, int));
 int udp_output _P((struct socket *, struct mbuf *, struct sockaddr_in *));
 int udp_attach _P((struct socket *));
 void udp_detach _P((struct socket *));
-struct socket * udp_listen _P((u_int32_t, u_int, u_int32_t, u_int, int));
+struct socket * udp_listen _P((Slirp *, u_int32_t, u_int, u_int32_t, u_int,
+                               int));
 int udp_output2(struct socket *so, struct mbuf *m,
                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                 int iptos);
author
#35 lore raw

PATCH 32/41 slirp: Use internal state in interface

In reply to Jan Kiszka

This now also exports the internal state to the slirp users in qemu, returning it from slirp_init and expecting it along with service invocations. Additionally provide an opaque value interface for the callbacks from slirp into the qemu core.

---

 net.c            |  105 +++++++++++++++++++++++++++++++++---------------------
 slirp/if.c       |    2 +
 slirp/libslirp.h |   35 ++++++++++--------
 slirp/misc.c     |    3 +-
 slirp/slirp.c    |   46 +++++++++++-------------
 slirp/slirp.h    |    1 +
 6 files changed, 108 insertions(+), 84 deletions(-)
net.c +64 -41
index 3ceaf33..5a4791b 100644
@@ -677,44 +677,55 @@ struct slirp_config_str {
     int legacy_format;
 };
 
-static int slirp_inited;
+typedef struct SlirpState {
+    VLANClientState *vc;
+    Slirp *slirp;
+} SlirpState;
+
 static struct slirp_config_str *slirp_configs;
 const char *legacy_tftp_prefix;
 const char *legacy_bootp_filename;
-static VLANClientState *slirp_vc;
+static SlirpState *slirp_state;
 
-static void slirp_hostfwd(Monitor *mon, const char *redir_str,
+static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
                           int legacy_format);
-static void slirp_guestfwd(Monitor *mon, const char *config_str,
+static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
                            int legacy_format);
 
 #ifndef _WIN32
 static const char *legacy_smb_export;
 
-static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr);
+static void slirp_smb(SlirpState *s, const char *exported_dir,
+                      struct in_addr vserver_addr);
 #endif
 
-int slirp_can_output(void)
+int slirp_can_output(void *opaque)
 {
-    return qemu_can_send_packet(slirp_vc);
+    SlirpState *s = opaque;
+
+    return qemu_can_send_packet(s->vc);
 }
 
-void slirp_output(const uint8_t *pkt, int pkt_len)
+void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
 {
+    SlirpState *s = opaque;
+
 #ifdef DEBUG_SLIRP
     printf("slirp output:\n");
     hex_dump(stdout, pkt, pkt_len);
 #endif
-    qemu_send_packet(slirp_vc, pkt, pkt_len);
+    qemu_send_packet(s->vc, pkt, pkt_len);
 }
 
 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
 {
+    SlirpState *s = vc->opaque;
+
 #ifdef DEBUG_SLIRP
     printf("slirp input:\n");
     hex_dump(stdout, buf, size);
 #endif
-    slirp_input(buf, size);
+    slirp_input(s->slirp, buf, size);
     return size;
 }
 
@@ -733,11 +744,13 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
                           const char *vnameserver, const char *smb_export,
                           const char *vsmbserver)
 {
+    SlirpState *s = slirp_state;
+
     if (slirp_in_use) {
         /* slirp only supports a single instance so far */
         return -1;
     }
-    if (!slirp_inited) {
+    if (!s) {
         /* default settings according to historic slirp */
         struct in_addr net  = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */
         struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */
@@ -830,18 +843,19 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
         }
 #endif
 
-        slirp_init(restricted, net, mask, host, vhostname, tftp_export,
-                   bootfile, dhcp, dns);
-        slirp_inited = 1;
+        s = qemu_mallocz(sizeof(SlirpState));
+        s->slirp = slirp_init(restricted, net, mask, host, vhostname,
+                              tftp_export, bootfile, dhcp, dns, s);
+        slirp_state = s;
 
         while (slirp_configs) {
             struct slirp_config_str *config = slirp_configs;
 
             if (config->flags & SLIRP_CFG_HOSTFWD) {
-                slirp_hostfwd(mon, config->str,
+                slirp_hostfwd(s, mon, config->str,
                               config->flags & SLIRP_CFG_LEGACY);
             } else {
-                slirp_guestfwd(mon, config->str,
+                slirp_guestfwd(s, mon, config->str,
                                config->flags & SLIRP_CFG_LEGACY);
             }
             slirp_configs = config->next;
@@ -852,14 +866,14 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
             smb_export = legacy_smb_export;
         }
         if (smb_export) {
-            slirp_smb(smb_export, smbsrv);
+            slirp_smb(s, smb_export, smbsrv);
         }
 #endif
     }
 
-    slirp_vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive,
-                                    NULL, net_slirp_cleanup, NULL);
-    slirp_vc->info_str[0] = '\0';
+    s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
+                                 net_slirp_cleanup, s);
+    s->vc->info_str[0] = '\0';
     slirp_in_use = 1;
     return 0;
 }
@@ -873,7 +887,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
     int is_udp = 0;
     int err;
 
-    if (!slirp_inited) {
+    if (!slirp_state) {
         monitor_printf(mon, "user mode network stack not in use\n");
         return;
     }
@@ -900,7 +914,8 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
 
     host_port = atoi(p);
 
-    err = slirp_remove_hostfwd(is_udp, host_addr, host_port);
+    err = slirp_remove_hostfwd(slirp_state->slirp, is_udp,
+                               host_addr, host_port);
 
     monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
                    err ? "removed" : "not found");
@@ -910,7 +925,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
     monitor_printf(mon, "invalid format\n");
 }
 
-static void slirp_hostfwd(Monitor *mon, const char *redir_str,
+static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
                           int legacy_format)
 {
     struct in_addr host_addr = { .s_addr = INADDR_ANY };
@@ -962,8 +977,8 @@ static void slirp_hostfwd(Monitor *mon, const char *redir_str,
         goto fail_syntax;
     }
 
-    if (slirp_add_hostfwd(is_udp, host_addr, host_port,
-                          guest_addr, guest_port) < 0) {
+    if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
+                          guest_port) < 0) {
         config_error(mon, "could not set up host forwarding rule '%s'\n",
                      redir_str);
     }
@@ -975,19 +990,19 @@ static void slirp_hostfwd(Monitor *mon, const char *redir_str,
 
 void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str)
 {
-    if (!slirp_inited) {
+    if (!slirp_state) {
         monitor_printf(mon, "user mode network stack not in use\n");
         return;
     }
 
-    slirp_hostfwd(mon, redir_str, 0);
+    slirp_hostfwd(slirp_state, mon, redir_str, 0);
 }
 
 void net_slirp_redir(const char *redir_str)
 {
     struct slirp_config_str *config;
 
-    if (!slirp_inited) {
+    if (!slirp_state) {
         config = qemu_malloc(sizeof(*config));
         pstrcpy(config->str, sizeof(config->str), redir_str);
         config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
@@ -996,7 +1011,7 @@ void net_slirp_redir(const char *redir_str)
         return;
     }
 
-    slirp_hostfwd(NULL, redir_str, 1);
+    slirp_hostfwd(slirp_state, NULL, redir_str, 1);
 }
 
 #ifndef _WIN32
@@ -1034,7 +1049,8 @@ static void smb_exit(void)
     erase_dir(smb_dir);
 }
 
-static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr)
+static void slirp_smb(SlirpState* s, const char *exported_dir,
+                      struct in_addr vserver_addr)
 {
     char smb_conf[1024];
     char smb_cmdline[1024];
@@ -1080,7 +1096,7 @@ static void slirp_smb(const char *exported_dir, struct in_addr vserver_addr)
     snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
              SMBD_COMMAND, smb_conf);
 
-    if (slirp_add_exec(0, smb_cmdline, vserver_addr, 139) < 0) {
+    if (slirp_add_exec(s->slirp, 0, smb_cmdline, vserver_addr, 139) < 0) {
         fprintf(stderr, "conflicting/invalid smbserver address\n");
         exit(1);
     }
@@ -1096,8 +1112,8 @@ void net_slirp_smb(const char *exported_dir)
         exit(1);
     }
     legacy_smb_export = exported_dir;
-    if (slirp_inited) {
-        slirp_smb(exported_dir, vserver_addr);
+    if (slirp_state) {
+        slirp_smb(slirp_state, exported_dir, vserver_addr);
     }
 }
 
@@ -1107,21 +1123,22 @@ struct GuestFwd {
     CharDriverState *hd;
     struct in_addr server;
     int port;
+    Slirp *slirp;
 };
 
 static int guestfwd_can_read(void *opaque)
 {
     struct GuestFwd *fwd = opaque;
-    return slirp_socket_can_recv(fwd->server, fwd->port);
+    return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
 }
 
 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
 {
     struct GuestFwd *fwd = opaque;
-    slirp_socket_recv(fwd->server, fwd->port, buf, size);
+    slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
 }
 
-static void slirp_guestfwd(Monitor *mon, const char *config_str,
+static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
                            int legacy_format)
 {
     struct in_addr server = { .s_addr = 0 };
@@ -1169,8 +1186,9 @@ static void slirp_guestfwd(Monitor *mon, const char *config_str,
     }
     fwd->server = server;
     fwd->port = port;
+    fwd->slirp = s->slirp;
 
-    if (slirp_add_exec(3, fwd->hd, server, port) < 0) {
+    if (slirp_add_exec(s->slirp, 3, fwd->hd, server, port) < 0) {
         config_error(mon, "conflicting/invalid host:port in guest forwarding "
                      "rule '%s'\n", config_str);
         qemu_free(fwd);
@@ -1186,8 +1204,13 @@ static void slirp_guestfwd(Monitor *mon, const char *config_str,
 
 void do_info_usernet(Monitor *mon)
 {
-    monitor_printf(mon, "VLAN %d (%s):\n", slirp_vc->vlan->id, slirp_vc->name);
-    slirp_connection_info(mon);
+    SlirpState *s = slirp_state;
+
+    if (!s) {
+        return;
+    }
+    monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
+    slirp_connection_info(s->slirp, mon);
 }
 
 #endif /* CONFIG_SLIRP */
@@ -2482,7 +2505,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         qemu_free(smb_export);
         qemu_free(vsmbsrv);
     } else if (!strcmp(device, "channel")) {
-        if (!slirp_inited) {
+        if (!slirp_state) {
             struct slirp_config_str *config;
 
             config = qemu_malloc(sizeof(*config));
@@ -2491,7 +2514,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
             config->next = slirp_configs;
             slirp_configs = config;
         } else {
-            slirp_guestfwd(mon, p, 1);
+            slirp_guestfwd(slirp_state, mon, p, 1);
         }
         ret = 0;
     } else
slirp/if.c +1 -1
index 75b15e4..0f04e13 100644
@@ -162,7 +162,7 @@ if_start(Slirp *slirp)
 
  again:
         /* check if we can really output */
-        if (!slirp_can_output())
+        if (!slirp_can_output(slirp->opaque))
             return;
 
 	/*
slirp/libslirp.h +19 -16
index 3bda691..521de3e 100644
@@ -8,11 +8,11 @@
 struct Slirp;
 typedef struct Slirp Slirp;
 
-void slirp_init(int restricted, struct in_addr vnetwork,
-                struct in_addr vnetmask, struct in_addr vhost,
-                const char *vhostname, const char *tftp_path,
-                const char *bootfile, struct in_addr vdhcp_start,
-                struct in_addr vnameserver);
+Slirp *slirp_init(int restricted, struct in_addr vnetwork,
+                  struct in_addr vnetmask, struct in_addr vhost,
+                  const char *vhostname, const char *tftp_path,
+                  const char *bootfile, struct in_addr vdhcp_start,
+                  struct in_addr vnameserver, void *opaque);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
@@ -20,23 +20,26 @@ void slirp_select_fill(int *pnfds,
 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
                        int select_error);
 
-void slirp_input(const uint8_t *pkt, int pkt_len);
+void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
 
 /* you must provide the following functions: */
-int slirp_can_output(void);
-void slirp_output(const uint8_t *pkt, int pkt_len);
+int slirp_can_output(void *opaque);
+void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len);
 
-int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,
+int slirp_add_hostfwd(Slirp *slirp, int is_udp,
+                      struct in_addr host_addr, int host_port,
                       struct in_addr guest_addr, int guest_port);
-int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port);
-int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr,
-                   int guest_port);
+int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
+                         struct in_addr host_addr, int host_port);
+int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
+                   struct in_addr guest_addr, int guest_port);
 
-void slirp_connection_info(Monitor *mon);
+void slirp_connection_info(Slirp *slirp, Monitor *mon);
 
-void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
-                       const uint8_t *buf, int size);
-size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
+void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr,
+                       int guest_port, const uint8_t *buf, int size);
+size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
+                             int guest_port);
 
 #else /* !CONFIG_SLIRP */
 
slirp/misc.c +1 -2
index c2b66fd..e9f08fd 100644
@@ -370,7 +370,7 @@ fd_block(int fd)
 #endif
 }
 
-void slirp_connection_info(Monitor *mon)
+void slirp_connection_info(Slirp *slirp, Monitor *mon)
 {
     const char * const tcpstates[] = {
         [TCPS_CLOSED]       = "CLOSED",
@@ -385,7 +385,6 @@ void slirp_connection_info(Monitor *mon)
         [TCPS_FIN_WAIT_2]   = "FIN_WAIT_2",
         [TCPS_TIME_WAIT]    = "TIME_WAIT",
     };
-    Slirp *slirp = &slirp_instance;
     struct in_addr dst_addr;
     struct sockaddr_in src;
     socklen_t src_len;
slirp/slirp.c +22 -24

118 lines not on this page · raw message

slirp/slirp.h +1 -0
index b1e8d4e..cb1a746 100644
@@ -256,6 +256,7 @@ struct Slirp {
     char *tftp_prefix;
     struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
 
+    void *opaque;
 };
 
 extern Slirp slirp_instance;
author
#36 lore raw

PATCH 33/41 slirp: Allocate/free stack instance dynamically

In reply to Jan Kiszka

Allocate the internal slirp state dynamically and provide and call slirp_cleanup to properly release it after use. This patch finally unbreaks slirp release and re-instantiation via host_net_* monitor commands.

---

 net.c            |  212 ++++++++++++++++++++++++++----------------------------
 slirp/libslirp.h |    1 
 slirp/main.h     |    1 
 slirp/slirp.c    |   32 +++++---
 slirp/slirp.h    |    2 -
 5 files changed, 124 insertions(+), 124 deletions(-)
net.c +103 -109
index 5a4791b..0c58124 100644
@@ -729,11 +729,13 @@ static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t siz
     return size;
 }
 
-static int slirp_in_use;
-
 static void net_slirp_cleanup(VLANClientState *vc)
 {
-    slirp_in_use = 0;
+    SlirpState *s = vc->opaque;
+
+    slirp_cleanup(s->slirp);
+    slirp_state = NULL;
+    qemu_free(s);
 }
 
 static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
@@ -744,137 +746,129 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
                           const char *vnameserver, const char *smb_export,
                           const char *vsmbserver)
 {
-    SlirpState *s = slirp_state;
-
-    if (slirp_in_use) {
-        /* slirp only supports a single instance so far */
-        return -1;
-    }
-    if (!s) {
-        /* default settings according to historic slirp */
-        struct in_addr net  = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */
-        struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */
-        struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
-        struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
-        struct in_addr dns  = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
+    /* default settings according to historic slirp */
+    struct in_addr net  = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */
+    struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */
+    struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
+    struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
+    struct in_addr dns  = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
 #ifndef _WIN32
-        struct in_addr smbsrv = { .s_addr = 0 };
+    struct in_addr smbsrv = { .s_addr = 0 };
 #endif
-        char buf[20];
-        uint32_t addr;
-        int shift;
-        char *end;
+    SlirpState *s;
+    char buf[20];
+    uint32_t addr;
+    int shift;
+    char *end;
 
-        if (!tftp_export) {
-            tftp_export = legacy_tftp_prefix;
-        }
-        if (!bootfile) {
-            bootfile = legacy_bootp_filename;
-        }
+    if (!tftp_export) {
+        tftp_export = legacy_tftp_prefix;
+    }
+    if (!bootfile) {
+        bootfile = legacy_bootp_filename;
+    }
 
-        if (vnetwork) {
-            if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
-                if (!inet_aton(vnetwork, &net)) {
-                    return -1;
-                }
-                addr = ntohl(net.s_addr);
-                if (!(addr & 0x80000000)) {
-                    mask.s_addr = htonl(0xff000000); /* class A */
-                } else if ((addr & 0xfff00000) == 0xac100000) {
-                    mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
-                } else if ((addr & 0xc0000000) == 0x80000000) {
-                    mask.s_addr = htonl(0xffff0000); /* class B */
-                } else if ((addr & 0xffff0000) == 0xc0a80000) {
-                    mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
-                } else if ((addr & 0xffff0000) == 0xc6120000) {
-                    mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
-                } else if ((addr & 0xe0000000) == 0xe0000000) {
-                    mask.s_addr = htonl(0xffffff00); /* class C */
-                } else {
-                    mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
-                }
+    if (vnetwork) {
+        if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
+            if (!inet_aton(vnetwork, &net)) {
+                return -1;
+            }
+            addr = ntohl(net.s_addr);
+            if (!(addr & 0x80000000)) {
+                mask.s_addr = htonl(0xff000000); /* class A */
+            } else if ((addr & 0xfff00000) == 0xac100000) {
+                mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
+            } else if ((addr & 0xc0000000) == 0x80000000) {
+                mask.s_addr = htonl(0xffff0000); /* class B */
+            } else if ((addr & 0xffff0000) == 0xc0a80000) {
+                mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
+            } else if ((addr & 0xffff0000) == 0xc6120000) {
+                mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
+            } else if ((addr & 0xe0000000) == 0xe0000000) {
+                mask.s_addr = htonl(0xffffff00); /* class C */
             } else {
-                if (!inet_aton(buf, &net)) {
-                    return -1;
-                }
-                shift = strtol(vnetwork, &end, 10);
-                if (*end != '\0') {
-                    if (!inet_aton(vnetwork, &mask)) {
-                        return -1;
-                    }
-                } else if (shift < 4 || shift > 32) {
+                mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
+            }
+        } else {
+            if (!inet_aton(buf, &net)) {
+                return -1;
+            }
+            shift = strtol(vnetwork, &end, 10);
+            if (*end != '\0') {
+                if (!inet_aton(vnetwork, &mask)) {
                     return -1;
-                } else {
-                    mask.s_addr = htonl(0xffffffff << (32 - shift));
                 }
+            } else if (shift < 4 || shift > 32) {
+                return -1;
+            } else {
+                mask.s_addr = htonl(0xffffffff << (32 - shift));
             }
-            net.s_addr &= mask.s_addr;
-            host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
-            dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
-            dns.s_addr  = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
         }
+        net.s_addr &= mask.s_addr;
+        host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
+        dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
+        dns.s_addr  = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
+    }
 
-        if (vhost && !inet_aton(vhost, &host)) {
-            return -1;
-        }
-        if ((host.s_addr & mask.s_addr) != net.s_addr) {
-            return -1;
-        }
+    if (vhost && !inet_aton(vhost, &host)) {
+        return -1;
+    }
+    if ((host.s_addr & mask.s_addr) != net.s_addr) {
+        return -1;
+    }
 
-        if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
-            return -1;
-        }
-        if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
-            dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
-            return -1;
-        }
+    if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
+        return -1;
+    }
+    if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
+        dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
+        return -1;
+    }
 
-        if (vnameserver && !inet_aton(vnameserver, &dns)) {
-            return -1;
-        }
-        if ((dns.s_addr & mask.s_addr) != net.s_addr ||
-            dns.s_addr == host.s_addr) {
-            return -1;
-        }
+    if (vnameserver && !inet_aton(vnameserver, &dns)) {
+        return -1;
+    }
+    if ((dns.s_addr & mask.s_addr) != net.s_addr ||
+        dns.s_addr == host.s_addr) {
+        return -1;
+    }
 
 #ifndef _WIN32
-        if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
-            return -1;
-        }
+    if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
+        return -1;
+    }
 #endif
 
-        s = qemu_mallocz(sizeof(SlirpState));
-        s->slirp = slirp_init(restricted, net, mask, host, vhostname,
-                              tftp_export, bootfile, dhcp, dns, s);
-        slirp_state = s;
+    s = qemu_mallocz(sizeof(SlirpState));
+    s->slirp = slirp_init(restricted, net, mask, host, vhostname,
+                          tftp_export, bootfile, dhcp, dns, s);
+    slirp_state = s;
 
-        while (slirp_configs) {
-            struct slirp_config_str *config = slirp_configs;
+    while (slirp_configs) {
+        struct slirp_config_str *config = slirp_configs;
 
-            if (config->flags & SLIRP_CFG_HOSTFWD) {
-                slirp_hostfwd(s, mon, config->str,
-                              config->flags & SLIRP_CFG_LEGACY);
-            } else {
-                slirp_guestfwd(s, mon, config->str,
-                               config->flags & SLIRP_CFG_LEGACY);
-            }
-            slirp_configs = config->next;
-            qemu_free(config);
+        if (config->flags & SLIRP_CFG_HOSTFWD) {
+            slirp_hostfwd(s, mon, config->str,
+                          config->flags & SLIRP_CFG_LEGACY);
+        } else {
+            slirp_guestfwd(s, mon, config->str,
+                           config->flags & SLIRP_CFG_LEGACY);
         }
+        slirp_configs = config->next;
+        qemu_free(config);
+    }
 #ifndef _WIN32
-        if (!smb_export) {
-            smb_export = legacy_smb_export;
-        }
-        if (smb_export) {
-            slirp_smb(s, smb_export, smbsrv);
-        }
-#endif
+    if (!smb_export) {
+        smb_export = legacy_smb_export;
+    }
+    if (smb_export) {
+        slirp_smb(s, smb_export, smbsrv);
     }
+#endif
 
     s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
                                  net_slirp_cleanup, s);
     s->vc->info_str[0] = '\0';
-    slirp_in_use = 1;
     return 0;
 }
 
slirp/libslirp.h +1 -0
index 521de3e..3bcc392 100644
@@ -13,6 +13,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   const char *vhostname, const char *tftp_path,
                   const char *bootfile, struct in_addr vdhcp_start,
                   struct in_addr vnameserver, void *opaque);
+void slirp_cleanup(Slirp *slirp);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
slirp/main.h +0 -1
index 90deb58..28d92d8 100644
@@ -11,7 +11,6 @@
 
 #define TOWRITEMAX 512
 
-extern int link_up;
 extern int slirp_socket;
 extern int slirp_socket_unit;
 extern int slirp_socket_port;
slirp/slirp.c +19 -13
index 7e86124..43aba3d 100644
@@ -40,8 +40,6 @@ static const uint8_t special_ethaddr[6] = {
 
 static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
 
-int link_up; // FIXME: kill this
-
 /* XXX: suppress those select globals */
 fd_set *global_readfds, *global_writefds, *global_xfds;
 
@@ -49,7 +47,7 @@ u_int curtime;
 static u_int time_fasttimo, last_slowtimo;
 static int do_slowtimo;
 
-Slirp slirp_instance;
+Slirp *slirp_instance;
 
 #ifdef _WIN32
 
@@ -193,11 +191,10 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   const char *bootfile, struct in_addr vdhcp_start,
                   struct in_addr vnameserver, void *opaque)
 {
-    Slirp *slirp = &slirp_instance;
+    Slirp *slirp = qemu_mallocz(sizeof(Slirp));
 
     slirp_init_once();
 
-    link_up = 1;
     slirp->restricted = restricted;
 
     if_init(slirp);
@@ -213,13 +210,9 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
         pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
                 vhostname);
     }
-    qemu_free(slirp->tftp_prefix);
-    slirp->tftp_prefix = NULL;
     if (tftp_path) {
         slirp->tftp_prefix = qemu_strdup(tftp_path);
     }
-    qemu_free(slirp->bootp_filename);
-    slirp->bootp_filename = NULL;
     if (bootfile) {
         slirp->bootp_filename = qemu_strdup(bootfile);
     }
@@ -230,9 +223,22 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     register_savevm("slirp", 0, 2, slirp_state_save, slirp_state_load, slirp);
 
+    slirp_instance = slirp;
+
     return slirp;
 }
 
+void slirp_cleanup(Slirp *slirp)
+{
+    unregister_savevm("slirp", slirp);
+
+    qemu_free(slirp->tftp_prefix);
+    qemu_free(slirp->bootp_filename);
+    qemu_free(slirp);
+
+    slirp_instance = NULL;
+}
+
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
@@ -263,11 +269,11 @@ static void updtime(void)
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
 {
-    Slirp *slirp = &slirp_instance;
+    Slirp *slirp = slirp_instance;
     struct socket *so, *so_next;
     int nfds;
 
-    if (!link_up) {
+    if (!slirp_instance) {
         return;
     }
 
@@ -384,11 +390,11 @@ void slirp_select_fill(int *pnfds,
 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
                        int select_error)
 {
-    Slirp *slirp = &slirp_instance;
+    Slirp *slirp = slirp_instance;
     struct socket *so, *so_next;
     int ret;
 
-    if (!link_up) {
+    if (!slirp_instance) {
         return;
     }
 
slirp/slirp.h +1 -1
index cb1a746..5d8861c 100644
@@ -259,7 +259,7 @@ struct Slirp {
     void *opaque;
 };
 
-extern Slirp slirp_instance;
+extern Slirp *slirp_instance;
 
 #ifndef NULL
 #define NULL (void *)0
#37 lore raw
In reply to Jan Kiszka
Quoted 20 lines · Jan Kiszka

The foundation of this series are the patches from my previous posting that didn't make it into upstream. But while reworking them, the series grow far beyond the previous approach, adding lots of fixes and cleanups to the slirp stack and its interfaces.

One major contribution is that the virtual IP addresses used inside slirp VLANs are now (almost) freely configurable. This enables sophisticated virtual network setups, specifically with guests that depends on specific addresses.

In this second step, the series also comes with support for multiple slirp instances per VM. Further contributions are
- proper dynamic slirp cleanup during runtime
- 'info usernet' command to dump slirp connection states
- reworked monitor commands hostfwd_add/remove (formerly known as host_net_redir)
- save/restore fixes for slirp
- first larger code cleanup (more work required...)
- various small fixes

Quoted 9 lines

The patches have been successfully tested against basic use cases:
connections to/from the guests, redirections, tftp, multi-instance, save/restore. Moreover, they also passed successfull build tests against mingw32.

Regarding the preferred flow: Mark, will you let this go through your network tree first? Currently the series is based on upstream/master, but I can rebase if required.

I don't want to do this through a git pull. I'd rather take this into my queue.

Signature · 3 lines
Regards,

Anthony Liguori
author
#38 lore raw

PATCH 34/41 slirp: Enable multiple instances

In reply to Jan Kiszka

Once again this was a long journey to reach the destination: Allow to instantiate slirp multiple times. But as in the past, the journey was worthwhile, cleaning up, fixing and enhancing various parts of the user space network stack along the way.

What is this particular change good for? Multiple slirps instances allow separated user space networks for guests with multiple NICs. This is already possible, but without any slirp support for the second network, ie. without a chance to talk to that network from the host via IP. We have a legacy guest system here that benefits from this slirp enhancement, allowing us to run both of its NICs purely over unprivileged user space IP stacks.

Another benefit of this patch is that it simply removes an artificial restriction of the configuration space qemu is providing, avoiding another source of surprises that users may face when playing with possible setups.

---

 net.c         |   37 +++++++++++++++++++------------------
 slirp/slirp.c |   23 ++++++++++++++---------
 slirp/slirp.h |    4 ++++
 3 files changed, 37 insertions(+), 27 deletions(-)
net.c +19 -18
index 0c58124..0c1a99b 100644
@@ -678,6 +678,7 @@ struct slirp_config_str {
 };
 
 typedef struct SlirpState {
+    TAILQ_ENTRY(SlirpState) entry;
     VLANClientState *vc;
     Slirp *slirp;
 } SlirpState;
@@ -685,7 +686,8 @@ typedef struct SlirpState {
 static struct slirp_config_str *slirp_configs;
 const char *legacy_tftp_prefix;
 const char *legacy_bootp_filename;
-static SlirpState *slirp_state;
+static TAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
+    TAILQ_HEAD_INITIALIZER(slirp_stacks);
 
 static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
                           int legacy_format);
@@ -734,7 +736,7 @@ static void net_slirp_cleanup(VLANClientState *vc)
     SlirpState *s = vc->opaque;
 
     slirp_cleanup(s->slirp);
-    slirp_state = NULL;
+    TAILQ_REMOVE(&slirp_stacks, s, entry);
     qemu_free(s);
 }
 
@@ -842,7 +844,7 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
     s = qemu_mallocz(sizeof(SlirpState));
     s->slirp = slirp_init(restricted, net, mask, host, vhostname,
                           tftp_export, bootfile, dhcp, dns, s);
-    slirp_state = s;
+    TAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
 
     while (slirp_configs) {
         struct slirp_config_str *config = slirp_configs;
@@ -881,7 +883,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
     int is_udp = 0;
     int err;
 
-    if (!slirp_state) {
+    if (TAILQ_EMPTY(&slirp_stacks)) {
         monitor_printf(mon, "user mode network stack not in use\n");
         return;
     }
@@ -908,7 +910,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
 
     host_port = atoi(p);
 
-    err = slirp_remove_hostfwd(slirp_state->slirp, is_udp,
+    err = slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
                                host_addr, host_port);
 
     monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
@@ -984,19 +986,19 @@ static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
 
 void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str)
 {
-    if (!slirp_state) {
+    if (TAILQ_EMPTY(&slirp_stacks)) {
         monitor_printf(mon, "user mode network stack not in use\n");
         return;
     }
 
-    slirp_hostfwd(slirp_state, mon, redir_str, 0);
+    slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), mon, redir_str, 0);
 }
 
 void net_slirp_redir(const char *redir_str)
 {
     struct slirp_config_str *config;
 
-    if (!slirp_state) {
+    if (TAILQ_EMPTY(&slirp_stacks)) {
         config = qemu_malloc(sizeof(*config));
         pstrcpy(config->str, sizeof(config->str), redir_str);
         config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
@@ -1005,7 +1007,7 @@ void net_slirp_redir(const char *redir_str)
         return;
     }
 
-    slirp_hostfwd(slirp_state, NULL, redir_str, 1);
+    slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1);
 }
 
 #ifndef _WIN32
@@ -1106,8 +1108,8 @@ void net_slirp_smb(const char *exported_dir)
         exit(1);
     }
     legacy_smb_export = exported_dir;
-    if (slirp_state) {
-        slirp_smb(slirp_state, exported_dir, vserver_addr);
+    if (!TAILQ_EMPTY(&slirp_stacks)) {
+        slirp_smb(TAILQ_FIRST(&slirp_stacks), exported_dir, vserver_addr);
     }
 }
 
@@ -1198,13 +1200,12 @@ static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
 
 void do_info_usernet(Monitor *mon)
 {
-    SlirpState *s = slirp_state;
+    SlirpState *s;
 
-    if (!s) {
-        return;
+    TAILQ_FOREACH(s, &slirp_stacks, entry) {
+        monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
+        slirp_connection_info(s->slirp, mon);
     }
-    monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
-    slirp_connection_info(s->slirp, mon);
 }
 
 #endif /* CONFIG_SLIRP */
@@ -2499,7 +2500,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         qemu_free(smb_export);
         qemu_free(vsmbsrv);
     } else if (!strcmp(device, "channel")) {
-        if (!slirp_state) {
+        if (TAILQ_EMPTY(&slirp_stacks)) {
             struct slirp_config_str *config;
 
             config = qemu_malloc(sizeof(*config));
@@ -2508,7 +2509,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
             config->next = slirp_configs;
             slirp_configs = config;
         } else {
-            slirp_guestfwd(slirp_state, mon, p, 1);
+            slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1);
         }
         ret = 0;
     } else
slirp/slirp.c +14 -9
index 43aba3d..fb666e6 100644
@@ -47,7 +47,8 @@ u_int curtime;
 static u_int time_fasttimo, last_slowtimo;
 static int do_slowtimo;
 
-Slirp *slirp_instance;
+TAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
+    TAILQ_HEAD_INITIALIZER(slirp_instances);
 
 #ifdef _WIN32
 
@@ -223,20 +224,20 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     register_savevm("slirp", 0, 2, slirp_state_save, slirp_state_load, slirp);
 
-    slirp_instance = slirp;
+    TAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
 
     return slirp;
 }
 
 void slirp_cleanup(Slirp *slirp)
 {
+    TAILQ_REMOVE(&slirp_instances, slirp, entry);
+
     unregister_savevm("slirp", slirp);
 
     qemu_free(slirp->tftp_prefix);
     qemu_free(slirp->bootp_filename);
     qemu_free(slirp);
-
-    slirp_instance = NULL;
 }
 
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
@@ -269,11 +270,11 @@ static void updtime(void)
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
 {
-    Slirp *slirp = slirp_instance;
+    Slirp *slirp;
     struct socket *so, *so_next;
     int nfds;
 
-    if (!slirp_instance) {
+    if (TAILQ_EMPTY(&slirp_instances)) {
         return;
     }
 
@@ -288,11 +289,12 @@ void slirp_select_fill(int *pnfds,
 	 */
 	do_slowtimo = 0;
 
+	TAILQ_FOREACH(slirp, &slirp_instances, entry) {
 		/*
 		 * *_slowtimo needs calling if there are IP fragments
 		 * in the fragment queue, or there are TCP connections active
 		 */
-		do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
+		do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
 		    (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
 
 		for (so = slirp->tcb.so_next; so != &slirp->tcb;
@@ -383,6 +385,7 @@ void slirp_select_fill(int *pnfds,
 				UPD_NFDS(so->s);
 			}
 		}
+	}
 
         *pnfds = nfds;
 }
@@ -390,11 +393,11 @@ void slirp_select_fill(int *pnfds,
 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
                        int select_error)
 {
-    Slirp *slirp = slirp_instance;
+    Slirp *slirp;
     struct socket *so, *so_next;
     int ret;
 
-    if (!slirp_instance) {
+    if (TAILQ_EMPTY(&slirp_instances)) {
         return;
     }
 
@@ -405,6 +408,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
 	/* Update time */
 	updtime();
 
+    TAILQ_FOREACH(slirp, &slirp_instances, entry) {
 	/*
 	 * See if anything has timed out
 	 */
@@ -559,6 +563,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
 	if (slirp->if_queued) {
 	    if_start(slirp);
 	}
+    }
 
 	/* clear global file descriptor sets.
 	 * these reside on the stack in vl.c
slirp/slirp.h +4 -0
index 5d8861c..920d7a6 100644
@@ -185,6 +185,8 @@ int inet_aton _P((const char *cp, struct in_addr *ia));
 
 #include "debug.h"
 
+#include "sys-queue.h"
+
 #include "libslirp.h"
 #include "ip.h"
 #include "tcp.h"
@@ -207,6 +209,8 @@ int inet_aton _P((const char *cp, struct in_addr *ia));
 #include "tftp.h"
 
 struct Slirp {
+    TAILQ_ENTRY(Slirp) entry;
+
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
     struct in_addr vnetwork_mask;
author
#39 lore raw

PATCH 35/41 slirp: Save/restore bootp client states

In reply to Jan Kiszka

---

 slirp/slirp.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)
slirp/slirp.c +27 -1
index fb666e6..0ce62a3 100644
@@ -222,7 +222,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     slirp->opaque = opaque;
 
-    register_savevm("slirp", 0, 2, slirp_state_save, slirp_state_load, slirp);
+    register_savevm("slirp", 0, 3, slirp_state_save, slirp_state_load, slirp);
 
     TAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
 
@@ -942,6 +942,16 @@ static void slirp_socket_save(QEMUFile *f, struct socket *so)
     slirp_tcp_save(f, so->so_tcpcb);
 }
 
+static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
+{
+    int i;
+
+    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        qemu_put_be16(f, slirp->bootp_clients[i].allocated);
+        qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
+    }
+}
+
 static void slirp_state_save(QEMUFile *f, void *opaque)
 {
     Slirp *slirp = opaque;
@@ -961,6 +971,8 @@ static void slirp_state_save(QEMUFile *f, void *opaque)
     qemu_put_byte(f, 0);
 
     qemu_put_be16(f, slirp->ip_id);
+
+    slirp_bootp_save(f, slirp);
 }
 
 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
@@ -1057,6 +1069,16 @@ static int slirp_socket_load(QEMUFile *f, struct socket *so)
     return 0;
 }
 
+static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
+{
+    int i;
+
+    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        slirp->bootp_clients[i].allocated = qemu_get_be16(f);
+        qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
+    }
+}
+
 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
 {
     Slirp *slirp = opaque;
@@ -1096,5 +1118,9 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
         slirp->ip_id = qemu_get_be16(f);
     }
 
+    if (version_id >= 3) {
+        slirp_bootp_load(f, slirp);
+    }
+
     return 0;
 }
author
#40 lore raw

PATCH 36/41 slirp: Use shell to erase smb directory

In reply to Jan Kiszka

Instead of open-coding this, we can use the power of the shell to remove the smb_dir on exit.

---

 net.c |   30 ++++--------------------------
 1 files changed, 4 insertions(+), 26 deletions(-)
net.c +4 -26
index 0c1a99b..edf7ca6 100644
@@ -1014,35 +1014,13 @@ void net_slirp_redir(const char *redir_str)
 
 static char smb_dir[1024];
 
-static void erase_dir(char *dir_name)
-{
-    DIR *d;
-    struct dirent *de;
-    char filename[1024];
-
-    /* erase all the files in the directory */
-    if ((d = opendir(dir_name)) != NULL) {
-        for(;;) {
-            de = readdir(d);
-            if (!de)
-                break;
-            if (strcmp(de->d_name, ".") != 0 &&
-                strcmp(de->d_name, "..") != 0) {
-                snprintf(filename, sizeof(filename), "%s/%s",
-                         smb_dir, de->d_name);
-                if (unlink(filename) != 0)  /* is it a directory? */
-                    erase_dir(filename);
-            }
-        }
-        closedir(d);
-        rmdir(dir_name);
-    }
-}
-
 /* automatic user mode samba server configuration */
 static void smb_exit(void)
 {
-    erase_dir(smb_dir);
+    char cmd[1024];
+
+    snprintf(cmd, sizeof(cmd), "rm -rf %s", smb_dir);
+    system(cmd);
 }
 
 static void slirp_smb(SlirpState* s, const char *exported_dir,
author
#41 lore raw

PATCH 37/41 slirp: Improve error handling in slirp_smb

In reply to Jan Kiszka

Make sure for invocations from the monitor that slirp_smb properly reports errors and doesn't terminate qemu.

---

 net.c |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 deletions(-)
net.c +12 -10
index edf7ca6..22b7080 100644
@@ -697,7 +697,7 @@ static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
 #ifndef _WIN32
 static const char *legacy_smb_export;
 
-static void slirp_smb(SlirpState *s, const char *exported_dir,
+static void slirp_smb(SlirpState *s, Monitor *mon, const char *exported_dir,
                       struct in_addr vserver_addr);
 #endif
 
@@ -864,7 +864,7 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
         smb_export = legacy_smb_export;
     }
     if (smb_export) {
-        slirp_smb(s, smb_export, smbsrv);
+        slirp_smb(s, mon, smb_export, smbsrv);
     }
 #endif
 
@@ -1023,7 +1023,7 @@ static void smb_exit(void)
     system(cmd);
 }
 
-static void slirp_smb(SlirpState* s, const char *exported_dir,
+static void slirp_smb(SlirpState* s, Monitor *mon, const char *exported_dir,
                       struct in_addr vserver_addr)
 {
     char smb_conf[1024];
@@ -1033,15 +1033,17 @@ static void slirp_smb(SlirpState* s, const char *exported_dir,
     /* XXX: better tmp dir construction */
     snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
     if (mkdir(smb_dir, 0700) < 0) {
-        fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
-        exit(1);
+        config_error(mon, "could not create samba server dir '%s'\n", smb_dir);
+        return;
     }
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
 
     f = fopen(smb_conf, "w");
     if (!f) {
-        fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
-        exit(1);
+        smb_exit();
+        config_error(mon, "could not create samba server "
+                     "configuration file '%s'\n", smb_conf);
+        return;
     }
     fprintf(f,
             "[global]\n"
@@ -1071,8 +1073,7 @@ static void slirp_smb(SlirpState* s, const char *exported_dir,
              SMBD_COMMAND, smb_conf);
 
     if (slirp_add_exec(s->slirp, 0, smb_cmdline, vserver_addr, 139) < 0) {
-        fprintf(stderr, "conflicting/invalid smbserver address\n");
-        exit(1);
+        config_error(mon, "conflicting/invalid smbserver address\n");
     }
 }
 
@@ -1087,7 +1088,8 @@ void net_slirp_smb(const char *exported_dir)
     }
     legacy_smb_export = exported_dir;
     if (!TAILQ_EMPTY(&slirp_stacks)) {
-        slirp_smb(TAILQ_FIRST(&slirp_stacks), exported_dir, vserver_addr);
+        slirp_smb(TAILQ_FIRST(&slirp_stacks), NULL, exported_dir,
+                  vserver_addr);
     }
 }
 
author
#42 lore raw

PATCH 38/41 slirp: Enable multi-instance support for the smb service

In reply to Jan Kiszka

Push the smb state, smb_dir, into SlirpState and construct it in a way that allows multiple smb instances (one per slirp stack). Remove the smb directory on slirp cleanup instead of qemu termination. As VLAN clients are also cleaned up on process termination, no feature is lost.

---

 net.c |   50 ++++++++++++++++++++++++++++++--------------------
 1 files changed, 30 insertions(+), 20 deletions(-)
net.c +30 -20
index 22b7080..917f72f 100644
@@ -681,6 +681,9 @@ typedef struct SlirpState {
     TAILQ_ENTRY(SlirpState) entry;
     VLANClientState *vc;
     Slirp *slirp;
+#ifndef _WIN32
+    char smb_dir[128];
+#endif
 } SlirpState;
 
 static struct slirp_config_str *slirp_configs;
@@ -699,6 +702,9 @@ static const char *legacy_smb_export;
 
 static void slirp_smb(SlirpState *s, Monitor *mon, const char *exported_dir,
                       struct in_addr vserver_addr);
+static void slirp_smb_cleanup(SlirpState *s);
+#else
+static inline void slirp_smb_cleanup(SlirpState *s) { }
 #endif
 
 int slirp_can_output(void *opaque)
@@ -736,6 +742,7 @@ static void net_slirp_cleanup(VLANClientState *vc)
     SlirpState *s = vc->opaque;
 
     slirp_cleanup(s->slirp);
+    slirp_smb_cleanup(s);
     TAILQ_REMOVE(&slirp_stacks, s, entry);
     qemu_free(s);
 }
@@ -1012,35 +1019,38 @@ void net_slirp_redir(const char *redir_str)
 
 #ifndef _WIN32
 
-static char smb_dir[1024];
-
 /* automatic user mode samba server configuration */
-static void smb_exit(void)
+static void slirp_smb_cleanup(SlirpState *s)
 {
-    char cmd[1024];
+    char cmd[128];
 
-    snprintf(cmd, sizeof(cmd), "rm -rf %s", smb_dir);
-    system(cmd);
+    if (s->smb_dir[0] != '\0') {
+        snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
+        system(cmd);
+        s->smb_dir[0] = '\0';
+    }
 }
 
 static void slirp_smb(SlirpState* s, Monitor *mon, const char *exported_dir,
                       struct in_addr vserver_addr)
 {
-    char smb_conf[1024];
-    char smb_cmdline[1024];
+    static int instance;
+    char smb_conf[128];
+    char smb_cmdline[128];
     FILE *f;
 
-    /* XXX: better tmp dir construction */
-    snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
-    if (mkdir(smb_dir, 0700) < 0) {
-        config_error(mon, "could not create samba server dir '%s'\n", smb_dir);
+    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
+             (long)getpid(), instance++);
+    if (mkdir(s->smb_dir, 0700) < 0) {
+        config_error(mon, "could not create samba server dir '%s'\n",
+                     s->smb_dir);
         return;
     }
-    snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
+    snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
 
     f = fopen(smb_conf, "w");
     if (!f) {
-        smb_exit();
+        slirp_smb_cleanup(s);
         config_error(mon, "could not create samba server "
                      "configuration file '%s'\n", smb_conf);
         return;
@@ -1059,20 +1069,20 @@ static void slirp_smb(SlirpState* s, Monitor *mon, const char *exported_dir,
             "path=%s\n"
             "read only=no\n"
             "guest ok=yes\n",
-            smb_dir,
-            smb_dir,
-            smb_dir,
-            smb_dir,
-            smb_dir,
+            s->smb_dir,
+            s->smb_dir,
+            s->smb_dir,
+            s->smb_dir,
+            s->smb_dir,
             exported_dir
             );
     fclose(f);
-    atexit(smb_exit);
 
     snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
              SMBD_COMMAND, smb_conf);
 
     if (slirp_add_exec(s->slirp, 0, smb_cmdline, vserver_addr, 139) < 0) {
+        slirp_smb_cleanup(s);
         config_error(mon, "conflicting/invalid smbserver address\n");
     }
 }
author
#43 lore raw

PATCH 39/41 net: Provide VLAN client lookup helper

In reply to Jan Kiszka

Introduce qemu_find_vlan_client_by_name for VLANClientState lookup based on VLAN ID and client name. This is useful for monitor commands.

---

 hw/xen_nic.c |    2 +-
 net.c        |   44 ++++++++++++++++++++++++++++++++------------
 net.h        |    2 +-
 3 files changed, 34 insertions(+), 14 deletions(-)
hw/xen_nic.c +1 -1
index e4f71f7..4377ed1 100644
@@ -301,7 +301,7 @@ static int net_init(struct XenDevice *xendev)
     if (netdev->mac == NULL)
 	return -1;
 
-    vlan = qemu_find_vlan(netdev->xendev.dev);
+    vlan = qemu_find_vlan(netdev->xendev.dev, 1);
     netdev->vs = qemu_new_vlan_client(vlan, "xen", NULL,
                                       net_rx_ok, net_rx_packet, NULL,
                                       NULL, netdev);
net.c +32 -12

77 lines not on this page · raw message

net.h +1 -1
index 7fcb49d..9fba4c6 100644
@@ -51,7 +51,7 @@ struct VLANState {
     int delivering;
 };
 
-VLANState *qemu_find_vlan(int id);
+VLANState *qemu_find_vlan(int id, int allocate);
 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
                                       const char *model,
                                       const char *name,
author
#44 lore raw

PATCH 40/41 slirp: Make hostfwd_add/remove multi-instance-aware

In reply to Jan Kiszka

Extend the syntax of hostfwd_add/remove to optionally take a tuple of VLAN ID and slirp stack name. If those are omitted, the commands will continue to work on the first registered slirp stack.

---

 net.c           |   62 ++++++++++++++++++++++++++++++++++++++++++++++---------
 net.h           |    6 ++++-
 qemu-monitor.hx |    8 ++++---
 3 files changed, 60 insertions(+), 16 deletions(-)
net.c +52 -10
index 030c29b..573179e 100644
@@ -907,23 +907,56 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
     return 0;
 }
 
-void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
+static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
+                                const char *stack)
+{
+    VLANClientState *vc;
+
+    if (vlan) {
+        vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack);
+        if (!vc) {
+            return NULL;
+        }
+        if (strcmp(vc->model, "user")) {
+            monitor_printf(mon, "invalid device specified\n");
+            return NULL;
+        }
+        return vc->opaque;
+    } else {
+        if (TAILQ_EMPTY(&slirp_stacks)) {
+            monitor_printf(mon, "user mode network stack not in use\n");
+            return NULL;
+        }
+        return TAILQ_FIRST(&slirp_stacks);
+    }
+}
+
+void net_slirp_hostfwd_remove(Monitor *mon, const char *arg1,
+                              const char *arg2, const char *arg3)
 {
     struct in_addr host_addr = { .s_addr = INADDR_ANY };
     int host_port;
     char buf[256] = "";
-    const char *p = src_str;
+    const char *src_str, *p;
+    SlirpState *s;
     int is_udp = 0;
     int err;
 
-    if (TAILQ_EMPTY(&slirp_stacks)) {
-        monitor_printf(mon, "user mode network stack not in use\n");
+    if (arg2) {
+        s = slirp_lookup(mon, arg1, arg2);
+        src_str = arg3;
+    } else {
+        s = slirp_lookup(mon, NULL, NULL);
+        src_str = arg1;
+    }
+    if (!s) {
         return;
     }
 
     if (!src_str || !src_str[0])
         goto fail_syntax;
 
+    p = src_str;
     get_str_sep(buf, sizeof(buf), &p, ':');
 
     if (!strcmp(buf, "tcp") || buf[0] == '\0') {
@@ -966,7 +999,7 @@ static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
     char *end;
 
     p = redir_str;
-    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+    if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
         goto fail_syntax;
     }
     if (!strcmp(buf, "tcp") || buf[0] == '\0') {
@@ -1017,14 +1050,23 @@ static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
     config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
 }
 
-void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str)
+void net_slirp_hostfwd_add(Monitor *mon, const char *arg1,
+                           const char *arg2, const char *arg3)
 {
-    if (TAILQ_EMPTY(&slirp_stacks)) {
-        monitor_printf(mon, "user mode network stack not in use\n");
-        return;
+    const char *redir_str;
+    SlirpState *s;
+
+    if (arg2) {
+        s = slirp_lookup(mon, arg1, arg2);
+        redir_str = arg3;
+    } else {
+        s = slirp_lookup(mon, NULL, NULL);
+        redir_str = arg1;
+    }
+    if (s) {
+        slirp_hostfwd(s, mon, redir_str, 0);
     }
 
-    slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), mon, redir_str, 0);
 }
 
 void net_slirp_redir(const char *redir_str)
net.h +4 -2
index 9fba4c6..4dad82d 100644
@@ -130,8 +130,10 @@ int net_client_init(Monitor *mon, const char *device, const char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str);
-void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str);
+void net_slirp_hostfwd_add(Monitor *mon, const char *arg1,
+                           const char *arg2, const char *arg3);
+void net_slirp_hostfwd_remove(Monitor *mon, const char *arg1,
+                              const char *arg2, const char *arg3);
 void net_slirp_redir(const char *redir_str);
 void net_cleanup(void);
 void net_client_check(void);
qemu-monitor.hx +4 -4
index e320f9f..66c6ad9 100644
@@ -536,11 +536,11 @@ Remove host VLAN client.
 ETEXI
 
 #ifdef CONFIG_SLIRP
-    { "hostfwd_add", "s", net_slirp_hostfwd_add,
-      "[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport",
+    { "hostfwd_add", "ss?s?", net_slirp_hostfwd_add,
+      "[vlan_id name] [tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport",
       "redirect TCP or UDP connections from host to guest (requires -net user)" },
-    { "hostfwd_remove", "s", net_slirp_hostfwd_remove,
-      "[tcp|udp]:[hostaddr]:hostport",
+    { "hostfwd_remove", "ss?s?", net_slirp_hostfwd_remove,
+      "[vlan_id name] [tcp|udp]:[hostaddr]:hostport",
       "remove host-to-guest TCP or UDP redirection" },
 #endif
 STEXI
author
#45 lore raw

PATCH 41/41 slirp: Basic VLAN client info_str

In reply to Jan Kiszka

---

 net.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
net.c +2 -1
index 573179e..fb5fc24 100644
@@ -903,7 +903,8 @@ static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
 
     s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
                                  net_slirp_cleanup, s);
-    s->vc->info_str[0] = '\0';
+    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
+             "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n');
     return 0;
 }