slirp: Kill slirp_is_inited

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.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
diff --git a/net.c b/net.c
index fac6457..5f8edda 100644
--- a/net.c
+++ b/net.c
@@ -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 @@
     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
diff --git a/net.h b/net.h
index 2cc6c13..6702f42 100644
--- a/net.h
+++ b/net.h
@@ -138,7 +138,6 @@
 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);
diff --git a/slirp/libslirp.h b/slirp/libslirp.h
index 8117187..b0496d5 100644
--- a/slirp/libslirp.h
+++ b/slirp/libslirp.h
@@ -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_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 @@
                        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
diff --git a/slirp/slirp.c b/slirp/slirp.c
index 20c691f..cf52d36 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -271,6 +271,10 @@
     int nfds;
     int tmp_time;
 
+    if (!link_up) {
+        return;
+    }
+
     /* fail safe */
     global_readfds = NULL;
     global_writefds = NULL;
@@ -281,7 +285,7 @@
 	 * 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 @@
 				UPD_NFDS(so->s);
 			}
 		}
-	}
 
 	/*
 	 * Setup timeout to use minimum CPU usage, especially when idle
@@ -413,11 +416,16 @@
         *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 @@
 	/*
 	 * 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 @@
 			tcp_slowtimo();
 			last_slowtimo = curtime;
 		}
-	}
 
 	/*
 	 * Check sockets
 	 */
-	if (link_up) {
+	if (!select_error) {
 		/*
 		 * Check TCP sockets
 		 */
@@ -576,7 +582,7 @@
 	/*
 	 * See if we can start outputting
 	 */
-	if (if_queued && link_up)
+	if (if_queued)
 	   if_start();
 
 	/* clear global file descriptor sets.
diff --git a/vl.c b/vl.c
index 23add31..eb2449a 100644
--- a/vl.c
+++ b/vl.c
@@ -165,9 +165,7 @@
 
 #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 @@
     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 @@
                 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) {