Skip to contentSkip to conversation

PATCH, RFC: Slirp improvements

Qemu-devel Blue Swirl 1 participant 2 messages Oct 20, 2007 – Oct 23, 2007

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

Hi,

This patch makes the SLIRP library able to handle more than one instance of itself by putting all globals inside a structure. Qemu needs some rearrangement to make the VLANs separate. The patch is loosely based on VirtualBox SLIRP changes but without the aHungarian nNotation ugliness. There are some other changes too, for example addition of "info slirp" monitor command. I tried to change the mbuf/packet chaining to support 64-bit pointers on x86-64 but the linking code is pretty hard to understand.

Any comments?

slirp/bootp.c +52 -42
Index: qemu/slirp/bootp.c
===================================================================
@@ -25,20 +25,6 @@
 
 /* XXX: only DHCP is supported */
 
-#define NB_ADDR 16
-
-#define START_ADDR 15
-
-#define LEASE_TIME (24 * 3600)
-
-typedef struct {
-    uint8_t allocated;
-    uint8_t macaddr[6];
-} BOOTPClient;
-
-BOOTPClient bootp_clients[NB_ADDR];
-
-const char *bootp_filename;
 
 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
@@ -49,37 +35,49 @@
 #define dprintf(fmt, args...)
 #endif
 
-static BOOTPClient *get_new_addr(struct in_addr *paddr)
+static BOOTPClient *get_new_addr(slirp_state_t *s, struct in_addr *paddr)
 {
     BOOTPClient *bc;
     int i;
 
     for(i = 0; i < NB_ADDR; i++) {
-        if (!bootp_clients[i].allocated)
+        if (!s->bootp_clients[i].allocated)
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &s->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
+    paddr->s_addr = htonl(ntohl(s->special_addr.s_addr) | (i + START_ADDR));
     return bc;
 }
 
-static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
+static void release_addr(slirp_state_t *s, struct in_addr *paddr)
+{
+    int i;
+
+    i = ntohl(paddr->s_addr) - START_ADDR - ntohl(s->special_addr.s_addr);
+    if (i >= NB_ADDR)
+        return;
+    memset(s->bootp_clients[i].macaddr, '\0', 6);
+    s->bootp_clients[i].allocated = 0;
+}
+
+static BOOTPClient *find_addr(slirp_state_t *s, 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))
+        if (!memcmp(macaddr, s->bootp_clients[i].macaddr, 6))
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &s->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
+    paddr->s_addr = htonl(ntohl(s->special_addr.s_addr) | (i + START_ADDR));
     return bc;
 }
 
@@ -124,7 +122,7 @@
     }
 }
 
-static void bootp_reply(struct bootp_t *bp)
+static void bootp_reply(slirp_state_t *s, struct bootp_t *bp)
 {
     BOOTPClient *bc;
     struct mbuf *m;
@@ -141,29 +139,39 @@
     if (dhcp_msg_type == 0)
         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
 
+    if (dhcp_msg_type == DHCPRELEASE) {
+        release_addr(s, &bp->bp_ciaddr);
+        dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
+        /* This message is not to be answered in any way. */
+        return;
+    }
     if (dhcp_msg_type != DHCPDISCOVER &&
         dhcp_msg_type != DHCPREQUEST)
         return;
     /* XXX: this is a hack to get the client mac address */
-    memcpy(client_ethaddr, bp->bp_hwaddr, 6);
+    memcpy(s->client_ethaddr, bp->bp_hwaddr, 6);
 
-    if ((m = m_get()) == NULL)
+    if ((m = m_get(s)) == NULL)
         return;
-    m->m_data += if_maxlinkhdr;
+    m->m_data += s->if_maxlinkhdr;
     rbp = (struct bootp_t *)m->m_data;
     m->m_data += sizeof(struct udpiphdr);
     memset(rbp, 0, sizeof(struct bootp_t));
 
     if (dhcp_msg_type == DHCPDISCOVER) {
-    new_addr:
-        bc = get_new_addr(&daddr.sin_addr);
+        /* Do not allocate a new lease for clients that forgot that they had a lease. */
+        bc = find_addr(s, &daddr.sin_addr, bp->bp_hwaddr);
         if (!bc) {
-            dprintf("no address left\n");
-            return;
+    new_addr:
+            bc = get_new_addr(s, &daddr.sin_addr);
+            if (!bc) {
+                dprintf("no address left\n");
+                return;
+            }
+            memcpy(bc->macaddr, s->client_ethaddr, 6);
         }
-        memcpy(bc->macaddr, client_ethaddr, 6);
     } else {
-        bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
+        bc = find_addr(s, &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) */
@@ -171,12 +179,12 @@
         }
     }
 
-    if (bootp_filename)
-        snprintf(rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
+    if (s->bootp_filename)
+        snprintf(rbp->bp_file, sizeof(rbp->bp_file), "%s", s->bootp_filename);
 
     dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
 
-    saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
+    saddr.sin_addr.s_addr = htonl(ntohl(s->special_addr.s_addr) | CTL_ALIAS);
     saddr.sin_port = htons(BOOTP_SERVER);
 
     daddr.sin_port = htons(BOOTP_CLIENT);
@@ -225,7 +233,7 @@
 
         *q++ = RFC1533_DNS;
         *q++ = 4;
-        dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
+        dns_addr.s_addr = htonl(ntohl(s->special_addr.s_addr) | CTL_DNS);
         memcpy(q, &dns_addr, 4);
         q += 4;
 
@@ -235,11 +243,11 @@
         memcpy(q, &val, 4);
         q += 4;
 
-        if (*slirp_hostname) {
-            val = strlen(slirp_hostname);
+        if (*s->slirp_hostname) {
+            val = strlen(s->slirp_hostname);
             *q++ = RFC1533_HOSTNAME;
             *q++ = val;
-            memcpy(q, slirp_hostname, val);
+            memcpy(q, s->slirp_hostname, val);
             q += val;
         }
     }
@@ -247,14 +255,16 @@
 
     m->m_len = sizeof(struct bootp_t) -
         sizeof(struct ip) - sizeof(struct udphdr);
-    udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
+    /* Reply to the broadcast address, as some clients perform paranoid checks. */
+    daddr.sin_addr.s_addr = INADDR_BROADCAST;
+    udp_output2(s, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
 }
 
-void bootp_input(struct mbuf *m)
+void bootp_input(slirp_state_t *s, struct mbuf *m)
 {
     struct bootp_t *bp = mtod(m, struct bootp_t *);
 
     if (bp->bp_op == BOOTP_REQUEST) {
-        bootp_reply(bp);
+        bootp_reply(s, bp);
     }
 }
slirp/bootp.h +2 -1
Index: qemu/slirp/bootp.h
===================================================================
@@ -71,6 +71,7 @@
 #define DHCPOFFER		2
 #define DHCPREQUEST		3
 #define DHCPACK			5
+#define DHCPRELEASE		7
 
 #define RFC1533_VENDOR_MAJOR	0
 #define RFC1533_VENDOR_MINOR	0
@@ -110,4 +111,4 @@
     uint8_t bp_vend[DHCP_OPT_LEN];
 };
 
-void bootp_input(struct mbuf *m);
+void bootp_input(slirp_state_t *s, struct mbuf *m);
slirp/ctl.h +1 -3
Index: qemu/slirp/ctl.h
===================================================================
@@ -2,6 +2,4 @@
 #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"
+#define CTL_BROADCAST   255
slirp/if.c +41 -55

225 lines not on this page · raw message

slirp/ip.h +0 -4
Index: qemu/slirp/ip.h
===================================================================
@@ -305,9 +305,5 @@
 	u_long	ips_unaligned;		/* times the ip packet was not aligned */
 };
 
-extern struct	ipstat	ipstat;
-extern struct	ipq	ipq;			/* ip reass. queue */
-extern u_int16_t	ip_id;				/* ip packet ctr, for ids */
-extern int	ip_defttl;			/* default IP ttl */
 
 #endif
slirp/ip_icmp.c +35 -41

183 lines not on this page · raw message

slirp/ip_icmp.h +4 -3
Index: qemu/slirp/ip_icmp.h
===================================================================
@@ -157,8 +157,9 @@
 	(type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
 	(type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
 
-void icmp_input _P((struct mbuf *, int));
-void icmp_error _P((struct mbuf *, u_char, u_char, int, char *));
-void icmp_reflect _P((struct mbuf *));
+void icmp_input(slirp_state_t *s, struct mbuf *m, int hlen);
+void icmp_error(slirp_state_t *s, struct mbuf *msrc, u_char type, u_char code,
+                int minsize, const char *message);
+void icmp_reflect(slirp_state_t *s, struct mbuf *m);
 
 #endif
slirp/ip_input.c +71 -71

343 lines not on this page · raw message

slirp/ip_output.c +16 -19
Index: qemu/slirp/ip_output.c
===================================================================
@@ -44,7 +44,6 @@
 
 #include <slirp.h>
 
-u_int16_t ip_id;
 
 /*
  * IP output.  The packet in mbuf chain m contains a skeletal IP
@@ -53,9 +52,7 @@
  * The mbuf opt, if present, will not be freed.
  */
 int
-ip_output(so, m0)
-	struct socket *so;
-	struct mbuf *m0;
+ip_output(slirp_state_t *s, struct socket *so, struct mbuf *m0)
 {
 	register struct ip *ip;
 	register struct mbuf *m = m0;
@@ -78,16 +75,16 @@
 	 */
 	ip->ip_v = IPVERSION;
 	ip->ip_off &= IP_DF;
-	ip->ip_id = htons(ip_id++);
+	ip->ip_id = htons(s->ip_id++);
 	ip->ip_hl = hlen >> 2;
-	ipstat.ips_localout++;
+	s->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) {
+/*	if (s->if_queued > s->if_thresh && towrite <= 0) {
  *		error = ENOBUFS;
  *		goto bad;
  *	}
@@ -96,13 +93,13 @@
 	/*
 	 * If small enough for interface, can just send directly.
 	 */
-	if ((u_int16_t)ip->ip_len <= if_mtu) {
+	if ((u_int16_t)ip->ip_len <= s->if_mtu) {
 		ip->ip_len = htons((u_int16_t)ip->ip_len);
 		ip->ip_off = htons((u_int16_t)ip->ip_off);
 		ip->ip_sum = 0;
 		ip->ip_sum = cksum(m, hlen);
 
-		if_output(so, m);
+		if_output(s, so, m);
 		goto done;
 	}
 
@@ -112,11 +109,11 @@
 	 */
 	if (ip->ip_off & IP_DF) {
 		error = -1;
-		ipstat.ips_cantfrag++;
+		s->ipstat.ips_cantfrag++;
 		goto bad;
 	}
 
-	len = (if_mtu - hlen) &~ 7;       /* ip databytes per packet */
+	len = (s->if_mtu - hlen) &~ 7;       /* ip databytes per packet */
 	if (len < 8) {
 		error = -1;
 		goto bad;
@@ -134,13 +131,13 @@
 	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(s);
 	  if (m == 0) {
 	    error = -1;
-	    ipstat.ips_odropped++;
+	    s->ipstat.ips_odropped++;
 	    goto sendorfree;
 	  }
-	  m->m_data += if_maxlinkhdr;
+	  m->m_data += s->if_maxlinkhdr;
 	  mhip = mtod(m, struct ip *);
 	  *mhip = *ip;
 
@@ -170,7 +167,7 @@
 	  mhip->ip_sum = cksum(m, mhlen);
 	  *mnext = m;
 	  mnext = &m->m_nextpkt;
-	  ipstat.ips_ofragments++;
+	  s->ipstat.ips_ofragments++;
 	}
 	/*
 	 * Update first fragment by trimming what's been copied out
@@ -187,19 +184,19 @@
 		m0 = m->m_nextpkt;
 		m->m_nextpkt = 0;
 		if (error == 0)
-			if_output(so, m);
+			if_output(s, so, m);
 		else
-			m_freem(m);
+			m_freem(s, m);
 	}
 
 	if (error == 0)
-		ipstat.ips_fragmented++;
+		s->ipstat.ips_fragmented++;
     }
 
 done:
 	return (error);
 
 bad:
-	m_freem(m0);
+	m_freem(s, m0);
 	goto done;
 }
slirp/libslirp.h +17 -11
Index: qemu/slirp/libslirp.h
===================================================================
@@ -5,26 +5,32 @@
 extern "C" {
 #endif
 
-void slirp_init(void);
+typedef struct slirp_state_t slirp_state_t;
 
-void slirp_select_fill(int *pnfds,
+slirp_state_t *slirp_init(void *user, const char *slirp_hostname,
+                          const char *tftp_prefix, const char *network);
+void slirp_term(slirp_state_t *s);
+void slirp_link_up(slirp_state_t *s);
+void slirp_link_down(slirp_state_t *s);
+
+void slirp_select_fill(slirp_state_t *s, 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(slirp_state_t *s, fd_set *readfds, fd_set *writefds,
+                       fd_set *xfds);
 
-void slirp_input(const uint8_t *pkt, int pkt_len);
+void slirp_input(slirp_state_t *s, 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 *user);
+void slirp_output(void *user, const uint8_t *pkt, int pkt_len);
 
-int slirp_redir(int is_udp, int host_port,
+int slirp_redir(slirp_state_t *s, int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
-int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
-                   int guest_port);
+int slirp_add_exec(slirp_state_t *s, int do_pty, const char *args,
+                   int addr_low_byte, int guest_port);
 
-extern const char *tftp_prefix;
-extern char slirp_hostname[33];
+void slirp_stats(slirp_state_t *s);
 
 #ifdef __cplusplus
 }
slirp/main.h +1 -26
Index: qemu/slirp/main.h
===================================================================
@@ -11,14 +11,6 @@
 
 #define TOWRITEMAX 512
 
-extern struct timeval tt;
-extern int link_up;
-extern int slirp_socket;
-extern int slirp_socket_unit;
-extern int slirp_socket_port;
-extern u_int32_t slirp_socket_addr;
-extern char *slirp_socket_passwd;
-extern int ctty_closed;
 
 /*
  * Get the difference in 2 times from updtim()
@@ -28,27 +20,10 @@
  */
 #define TIME_DIFF(x,y) (x)-(y) < 0 ? ~0-(y)+(x) : (x)-(y)
 
-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 our_addr;
-extern struct in_addr loopback_addr;
-extern struct in_addr dns_addr;
-extern char *username;
-extern char *socket_path;
-extern int towrite_max;
-extern int ppp_exit;
-extern int so_options;
-extern int tcp_keepintvl;
-extern uint8_t client_ethaddr[6];
 
 #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_state_t *s, const uint8_t *ip_data, int ip_data_len);
slirp/mbuf.c +27 -40

144 lines not on this page · raw message

slirp/mbuf.h +5 -20
Index: qemu/slirp/mbuf.h
===================================================================
@@ -120,28 +120,13 @@
 #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));
-void msize_init _P((void));
-struct mbuf * m_get _P((void));
-void m_free _P((struct mbuf *));
-void m_cat _P((register struct mbuf *, register struct mbuf *));
+void m_init(slirp_state_t *s);
+struct mbuf *m_get(slirp_state_t *s);
+void m_free(slirp_state_t *s, struct mbuf *m);
+void m_cat(slirp_state_t *s, register struct mbuf *m, register struct mbuf *n);
 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(slirp_state_t *s, void *dat);
 
 #endif
slirp/misc.c +50 -57

275 lines not on this page · raw message

slirp/misc.h +4 -11
Index: qemu/slirp/misc.h
===================================================================
@@ -16,13 +16,6 @@
 	struct ex_list *ex_next;
 };
 
-extern struct ex_list *exec_list;
-extern u_int curtime, time_fasttimo, last_slowtimo, detach_time, detach_wait;
-
-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
@@ -69,12 +62,12 @@
 
 int show_x _P((char *, struct socket *));
 void redir_x _P((u_int32_t, int, int, int));
-void getouraddr _P((void));
-inline  void slirp_insque  _P((void *, void *));
-inline  void slirp_remque  _P((void *));
+void getouraddr(slirp_state_t *s);
+void slirp_insque(slirp_state_t *s, void *a, void *b);
+void slirp_remque(slirp_state_t *s, void *a);
 int add_exec _P((struct ex_list **, int, char *, int, int));
 int slirp_openpty _P((int *, int *));
-int fork_exec _P((struct socket *, char *, int));
+int fork_exec(slirp_state_t *s, struct socket *so, const char *ex, int do_pty);
 void snooze_hup _P((int));
 void snooze _P((void));
 void relay _P((int));
slirp/sbuf.c +8 -10
Index: qemu/slirp/sbuf.c
===================================================================
@@ -7,6 +7,8 @@
 
 #include <slirp.h>
 
+static void sbappendsb(struct sbuf *sb, struct mbuf *m);
+
 /* Done as a macro in socket.h */
 /* int
  * sbspace(struct sockbuff *sb)
@@ -72,9 +74,7 @@
  * (the socket is non-blocking, so we won't hang)
  */
 void
-sbappend(so, m)
-	struct socket *so;
-	struct mbuf *m;
+sbappend(slirp_state_t *s, struct socket *so, struct mbuf *m)
 {
 	int ret = 0;
 
@@ -85,7 +85,7 @@
 
 	/* Shouldn't happen, but...  e.g. foreign host closes connection */
 	if (m->m_len <= 0) {
-		m_free(m);
+		m_free(s, m);
 		return;
 	}
 
@@ -96,7 +96,7 @@
 	 */
 	if (so->so_urgc) {
 		sbappendsb(&so->so_rcv, m);
-		m_free(m);
+		m_free(s, m);
 		sosendoob(so);
 		return;
 	}
@@ -126,17 +126,15 @@
 		sbappendsb(&so->so_rcv, m);
 	} /* else */
 	/* Whatever happened, we free the mbuf */
-	m_free(m);
+	m_free(s, m);
 }
 
 /*
  * Copy the data from m into sb
  * The caller is responsible to make sure there's enough room
  */
-void
-sbappendsb(sb, m)
-	 struct sbuf *sb;
-	 struct mbuf *m;
+static void
+sbappendsb(struct sbuf *sb, struct mbuf *m)
 {
 	int len, n,  nn;
 
slirp/sbuf.h +1 -2
Index: qemu/slirp/sbuf.h
===================================================================
@@ -24,8 +24,7 @@
 void sbfree _P((struct sbuf *));
 void sbdrop _P((struct sbuf *, int));
 void sbreserve _P((struct sbuf *, int));
-void sbappend _P((struct socket *, struct mbuf *));
-void sbappendsb _P((struct sbuf *, struct mbuf *));
+void sbappend(slirp_state_t *s, struct socket *so, struct mbuf *m);
 void sbcopy _P((struct sbuf *, int, int, char *));
 
 #endif
slirp/slirp.c +158 -116

542 lines not on this page · raw message

slirp/slirp.h +138 -40

233 lines not on this page · raw message

slirp/socket.c +90 -79

395 lines not on this page · raw message

slirp/socket.h +9 -16
Index: qemu/slirp/socket.h
===================================================================
@@ -71,9 +71,6 @@
 #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 */
 
-extern struct socket tcb;
-
-
 #if defined(DECLARE_IOVEC) && !defined(HAVE_READV)
 struct iovec {
 	char *iov_base;
@@ -81,24 +78,20 @@
 };
 #endif
 
-void so_init _P((void));
 struct socket * solookup _P((struct socket *, struct in_addr, u_int, struct in_addr, u_int));
 struct socket * socreate _P((void));
-void sofree _P((struct socket *));
-int soread _P((struct socket *));
-void sorecvoob _P((struct socket *));
+void sofree(slirp_state_t *s, struct socket *so);
+int soread(slirp_state_t *s, struct socket *so);
+void sorecvoob(slirp_state_t *s, struct socket *so);
 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));
-void sorwakeup _P((struct socket *));
-void sowwakeup _P((struct socket *));
+int sowrite(slirp_state_t *s, struct socket *so);
+void sorecvfrom(slirp_state_t *s, struct socket *so);
+int sosendto(slirp_state_t *s, struct socket *so, struct mbuf *m);
+struct socket *solisten(slirp_state_t *s, u_int port, u_int32_t laddr,
+                        u_int lport, int flags);
 void soisfconnecting _P((register struct socket *));
 void soisfconnected _P((register struct socket *));
-void sofcantrcvmore _P((struct  socket *));
-void sofcantsendmore _P((struct socket *));
 void soisfdisconnected _P((struct socket *));
-void sofwdrain _P((struct socket *));
+void sofwdrain(slirp_state_t *s, struct socket *so);
 
 #endif /* _SOCKET_H_ */
slirp/tcp.h +1 -7
Index: qemu/slirp/tcp.h
===================================================================
@@ -42,10 +42,6 @@
 #define      PR_SLOWHZ       2               /* 2 slow timeouts per second (approx) */
 #define      PR_FASTHZ       5               /* 5 fast timeouts per second (not important) */
 
-extern int tcp_rcvspace;
-extern int tcp_sndspace;
-extern struct socket *tcp_last_so;
-
 #define TCP_SNDSPACE 8192
 #define TCP_RCVSPACE 8192
 
@@ -170,8 +166,6 @@
 
 #define TCP_ISSINCR     (125*1024)      /* increment for tcp_iss each second */
 
-extern tcp_seq tcp_iss;                /* tcp initial send seq # */
-
-extern char *tcpstates[];
+extern const char * const tcpstates[];
 
 #endif
slirp/tcp_input.c +147 -148

49 lines not on this page · raw message

slirp/tcp_output.c +22 -23

45 lines not on this page · raw message

slirp/tcp_subr.c +96 -130

226 lines not on this page · raw message

slirp/tcp_timer.c +37 -46

83 lines not on this page · raw message

slirp/tcp_timer.h +6 -8
Index: qemu/slirp/tcp_timer.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tcp_var.h +4 -4
Index: qemu/slirp/tcp_var.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tftp.c +55 -65

120 lines not on this page · raw message

slirp/tftp.h +1 -1
Index: qemu/slirp/tftp.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/udp.c +63 -73

136 lines not on this page · raw message

slirp/udp.h +9 -13
Index: qemu/slirp/udp.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/debug.c +117 -90

207 lines not on this page · raw message

slirp/debug.h +0 -10
Index: qemu/slirp/debug.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/icmp_var.h +0 -2
Index: qemu/slirp/icmp_var.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/if.h +0 -14
Index: qemu/slirp/if.h
===================================================================

Lines of this file are not shown. View the raw message.

vl.c +33 -25

58 lines not on this page · raw message

vl.h +3 -1
Index: qemu/vl.h
===================================================================

Lines of this file are not shown. View the raw message.

monitor.c +4 -0
Index: qemu/monitor.c
===================================================================

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

author
#1 lore raw

I tried harder to change the SLIRP queue stuff to something saner by hiding the pointer access inside inlined functions. Still when I changed the 32 bit pointers to native 64 bit (or moved the pointers outside the packet), qemu crashes. Must be some devilishly hidden access somewhere. I already fixed one, which involved recycling port number fields for buffer pointers. IMHO there should be some kind of corporeal punishments mandated by law for writing this kind of mess. Maybe it would even be easier to create a new userland IP stack based on Linux from scratch instead of fixing this.

I think I'll just commit in the log part, unless there are objections.

monitor.c +4 -0
Index: qemu/monitor.c
===================================================================
@@ -1365,6 +1365,10 @@
     { "cpustats", "", do_info_cpu_stats,
       "", "show CPU statistics", },
 #endif
+#if defined(CONFIG_SLIRP)
+    { "slirp", "", do_info_slirp,
+      "", "show SLIRP statistics", },
+#endif
     { NULL, NULL, },
 };
 
slirp/bootp.c +52 -42

189 lines not on this page · raw message

slirp/bootp.h +2 -1
Index: qemu/slirp/bootp.h
===================================================================
@@ -71,6 +71,7 @@
 #define DHCPOFFER		2
 #define DHCPREQUEST		3
 #define DHCPACK			5
+#define DHCPRELEASE		7
 
 #define RFC1533_VENDOR_MAJOR	0
 #define RFC1533_VENDOR_MINOR	0
@@ -110,4 +111,4 @@
     uint8_t bp_vend[DHCP_OPT_LEN];
 };
 
-void bootp_input(struct mbuf *m);
+void bootp_input(slirp_state_t *s, struct mbuf *m);
slirp/ctl.h +1 -3
Index: qemu/slirp/ctl.h
===================================================================
@@ -2,6 +2,4 @@
 #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"
+#define CTL_BROADCAST   255
slirp/debug.c +117 -90

337 lines not on this page · raw message

slirp/debug.h +0 -10
Index: qemu/slirp/debug.h
===================================================================
@@ -37,14 +37,4 @@
 #endif
 
 void debug_init _P((char *, int));
-//void ttystats _P((struct ttys *));
-void allttystats _P((void));
-void ipstats _P((void));
-void vjstats _P((void));
-void tcpstats _P((void));
-void udpstats _P((void));
-void icmpstats _P((void));
-void mbufstats _P((void));
-void sockstats _P((void));
-void slirp_exit _P((int));
 
slirp/icmp_var.h +0 -2
Index: qemu/slirp/icmp_var.h
===================================================================
@@ -64,6 +64,4 @@
 	{ "stats", CTLTYPE_STRUCT }, \
 }
 
-extern struct icmpstat icmpstat;
-
 #endif
slirp/if.c +53 -77

247 lines not on this page · raw message

slirp/if.h +0 -16
Index: qemu/slirp/if.h
===================================================================
@@ -13,22 +13,6 @@
 #define IF_AUTOCOMP	0x04	/* Autodetect (default) */
 #define IF_NOCIDCOMP	0x08	/* CID compression */
 
-/* Needed for FreeBSD */
-#undef if_mtu
-extern int	if_mtu;
-extern int	if_mru;	/* MTU and MRU */
-extern int	if_comp;	/* Flags for compression */
-extern int	if_maxlinkhdr;
-extern int	if_queued;	/* Number of packets queued so far */
-extern int	if_thresh;	/* Number of packets queued before we start sending
-				 * (to prevent allocing too many mbufs) */
-
-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))
-
 /* Interface statistics */
 struct slirp_ifstats {
 	u_int out_pkts;		/* Output packets */
slirp/ip.h +192 -10

238 lines not on this page · raw message

slirp/ip_icmp.c +35 -41

183 lines not on this page · raw message

slirp/ip_icmp.h +4 -3
Index: qemu/slirp/ip_icmp.h
===================================================================
@@ -157,8 +157,9 @@
 	(type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
 	(type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
 
-void icmp_input _P((struct mbuf *, int));
-void icmp_error _P((struct mbuf *, u_char, u_char, int, char *));
-void icmp_reflect _P((struct mbuf *));
+void icmp_input(slirp_state_t *s, struct mbuf *m, int hlen);
+void icmp_error(slirp_state_t *s, struct mbuf *msrc, u_char type, u_char code,
+                int minsize, const char *message);
+void icmp_reflect(slirp_state_t *s, struct mbuf *m);
 
 #endif
slirp/ip_input.c +103 -96

418 lines not on this page · raw message

slirp/ip_output.c +20 -23

126 lines not on this page · raw message

slirp/libslirp.h +17 -11
Index: qemu/slirp/libslirp.h
===================================================================
@@ -5,26 +5,32 @@
 extern "C" {
 #endif
 
-void slirp_init(void);
+typedef struct slirp_state_t slirp_state_t;
 
-void slirp_select_fill(int *pnfds,
+slirp_state_t *slirp_init(void *user, const char *slirp_hostname,
+                          const char *tftp_prefix, const char *network);
+void slirp_term(slirp_state_t *s);
+void slirp_link_up(slirp_state_t *s);
+void slirp_link_down(slirp_state_t *s);
+
+void slirp_select_fill(slirp_state_t *s, 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(slirp_state_t *s, fd_set *readfds, fd_set *writefds,
+                       fd_set *xfds);
 
-void slirp_input(const uint8_t *pkt, int pkt_len);
+void slirp_input(slirp_state_t *s, 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 *user);
+void slirp_output(void *user, const uint8_t *pkt, int pkt_len);
 
-int slirp_redir(int is_udp, int host_port,
+int slirp_redir(slirp_state_t *s, int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
-int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
-                   int guest_port);
+int slirp_add_exec(slirp_state_t *s, int do_pty, const char *args,
+                   int addr_low_byte, int guest_port);
 
-extern const char *tftp_prefix;
-extern char slirp_hostname[33];
+void slirp_stats(slirp_state_t *s);
 
 #ifdef __cplusplus
 }
slirp/main.h +1 -26
Index: qemu/slirp/main.h
===================================================================
@@ -11,14 +11,6 @@
 
 #define TOWRITEMAX 512
 
-extern struct timeval tt;
-extern int link_up;
-extern int slirp_socket;
-extern int slirp_socket_unit;
-extern int slirp_socket_port;
-extern u_int32_t slirp_socket_addr;
-extern char *slirp_socket_passwd;
-extern int ctty_closed;
 
 /*
  * Get the difference in 2 times from updtim()
@@ -28,27 +20,10 @@
  */
 #define TIME_DIFF(x,y) (x)-(y) < 0 ? ~0-(y)+(x) : (x)-(y)
 
-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 our_addr;
-extern struct in_addr loopback_addr;
-extern struct in_addr dns_addr;
-extern char *username;
-extern char *socket_path;
-extern int towrite_max;
-extern int ppp_exit;
-extern int so_options;
-extern int tcp_keepintvl;
-extern uint8_t client_ethaddr[6];
 
 #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_state_t *s, const uint8_t *ip_data, int ip_data_len);
slirp/mbuf.c +36 -46

157 lines not on this page · raw message

slirp/mbuf.h +67 -32

130 lines not on this page · raw message

slirp/misc.c +55 -63

288 lines not on this page · raw message

slirp/misc.h +5 -12
Index: qemu/slirp/misc.h
===================================================================
@@ -12,17 +12,10 @@
 	int ex_pty;			/* Do we want a pty? */
 	int ex_addr;			/* The last byte of the address */
 	int ex_fport;                   /* Port to telnet to */
-	char *ex_exec;                  /* Command line of what to exec */
+	const char *ex_exec;            /* Command line of what to exec */
 	struct ex_list *ex_next;
 };
 
-extern struct ex_list *exec_list;
-extern u_int curtime, time_fasttimo, last_slowtimo, detach_time, detach_wait;
-
-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
@@ -69,12 +62,12 @@
 
 int show_x _P((char *, struct socket *));
 void redir_x _P((u_int32_t, int, int, int));
-void getouraddr _P((void));
-inline  void slirp_insque  _P((void *, void *));
-inline  void slirp_remque  _P((void *));
+void getouraddr(slirp_state_t *s);
+void slirp_insque(slirp_state_t *s, void *a, void *b);
+void slirp_remque(slirp_state_t *s, void *a);
 int add_exec _P((struct ex_list **, int, char *, int, int));
 int slirp_openpty _P((int *, int *));
-int fork_exec _P((struct socket *, char *, int));
+int fork_exec(slirp_state_t *s, struct socket *so, const char *ex, int do_pty);
 void snooze_hup _P((int));
 void snooze _P((void));
 void relay _P((int));
slirp/sbuf.c +8 -10
Index: qemu/slirp/sbuf.c
===================================================================
@@ -7,6 +7,8 @@
 
 #include <slirp.h>
 
+static void sbappendsb(struct sbuf *sb, struct mbuf *m);
+
 /* Done as a macro in socket.h */
 /* int
  * sbspace(struct sockbuff *sb)
@@ -72,9 +74,7 @@
  * (the socket is non-blocking, so we won't hang)
  */
 void
-sbappend(so, m)
-	struct socket *so;
-	struct mbuf *m;
+sbappend(slirp_state_t *s, struct socket *so, struct mbuf *m)
 {
 	int ret = 0;
 
@@ -85,7 +85,7 @@
 
 	/* Shouldn't happen, but...  e.g. foreign host closes connection */
 	if (m->m_len <= 0) {
-		m_free(m);
+		m_free(s, m);
 		return;
 	}
 
@@ -96,7 +96,7 @@
 	 */
 	if (so->so_urgc) {
 		sbappendsb(&so->so_rcv, m);
-		m_free(m);
+		m_free(s, m);
 		sosendoob(so);
 		return;
 	}
@@ -126,17 +126,15 @@
 		sbappendsb(&so->so_rcv, m);
 	} /* else */
 	/* Whatever happened, we free the mbuf */
-	m_free(m);
+	m_free(s, m);
 }
 
 /*
  * Copy the data from m into sb
  * The caller is responsible to make sure there's enough room
  */
-void
-sbappendsb(sb, m)
-	 struct sbuf *sb;
-	 struct mbuf *m;
+static void
+sbappendsb(struct sbuf *sb, struct mbuf *m)
 {
 	int len, n,  nn;
 
slirp/sbuf.h +1 -2
Index: qemu/slirp/sbuf.h
===================================================================
@@ -24,8 +24,7 @@
 void sbfree _P((struct sbuf *));
 void sbdrop _P((struct sbuf *, int));
 void sbreserve _P((struct sbuf *, int));
-void sbappend _P((struct socket *, struct mbuf *));
-void sbappendsb _P((struct sbuf *, struct mbuf *));
+void sbappend(slirp_state_t *s, struct socket *so, struct mbuf *m);
 void sbcopy _P((struct sbuf *, int, int, char *));
 
 #endif
slirp/slirp.c +159 -121

422 lines not on this page · raw message

slirp/slirp.h +167 -42

209 lines not on this page · raw message

slirp/socket.c +90 -79

169 lines not on this page · raw message

slirp/socket.h +9 -16
Index: qemu/slirp/socket.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tcp.h +1 -7
Index: qemu/slirp/tcp.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tcp_input.c +161 -160

321 lines not on this page · raw message

slirp/tcp_output.c +22 -23

45 lines not on this page · raw message

slirp/tcp_subr.c +110 -137

247 lines not on this page · raw message

slirp/tcp_timer.c +37 -46

83 lines not on this page · raw message

slirp/tcp_timer.h +6 -8
Index: qemu/slirp/tcp_timer.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tcp_var.h +17 -20
Index: qemu/slirp/tcp_var.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/tcpip.h +50 -2

52 lines not on this page · raw message

slirp/tftp.c +55 -65

120 lines not on this page · raw message

slirp/tftp.h +1 -1
Index: qemu/slirp/tftp.h
===================================================================

Lines of this file are not shown. View the raw message.

slirp/udp.c +71 -78

149 lines not on this page · raw message

slirp/udp.h +20 -15
Index: qemu/slirp/udp.h
===================================================================

Lines of this file are not shown. View the raw message.

vl.c +33 -25

58 lines not on this page · raw message

vl.h +3 -1
Index: qemu/vl.h
===================================================================

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.