Commit d918f23efaf486293b96418fe5deaff8a5583304

Authored by Jan Kiszka
Committed by Anthony Liguori
1 parent 8ec7f4ed

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>
@@ -120,9 +120,7 @@ @@ -120,9 +120,7 @@
120 #include "qemu_socket.h" 120 #include "qemu_socket.h"
121 #include "qemu-log.h" 121 #include "qemu-log.h"
122 122
123 -#if defined(CONFIG_SLIRP)  
124 -#include "libslirp.h"  
125 -#endif 123 +#include "slirp/libslirp.h"
126 124
127 125
128 static VLANState *first_vlan; 126 static VLANState *first_vlan;
@@ -712,11 +710,6 @@ void slirp_output(const uint8_t *pkt, int pkt_len) @@ -712,11 +710,6 @@ void slirp_output(const uint8_t *pkt, int pkt_len)
712 qemu_send_packet(slirp_vc, pkt, pkt_len); 710 qemu_send_packet(slirp_vc, pkt, pkt_len);
713 } 711 }
714 712
715 -int slirp_is_inited(void)  
716 -{  
717 - return slirp_inited;  
718 -}  
719 -  
720 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) 713 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
721 { 714 {
722 #ifdef DEBUG_SLIRP 715 #ifdef DEBUG_SLIRP
@@ -138,7 +138,6 @@ void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str); @@ -138,7 +138,6 @@ void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str);
138 void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str); 138 void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str);
139 void net_slirp_redir(const char *redir_str); 139 void net_slirp_redir(const char *redir_str);
140 void net_cleanup(void); 140 void net_cleanup(void);
141 -int slirp_is_inited(void);  
142 void net_client_check(void); 141 void net_client_check(void);
143 void net_set_boot_mask(int boot_mask); 142 void net_set_boot_mask(int boot_mask);
144 void net_host_device_add(Monitor *mon, const char *device, const char *opts); 143 void net_host_device_add(Monitor *mon, const char *device, const char *opts);
slirp/libslirp.h
@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 3
4 #include <qemu-common.h> 4 #include <qemu-common.h>
5 5
  6 +#ifdef CONFIG_SLIRP
  7 +
6 void slirp_init(int restricted, struct in_addr vnetwork, 8 void slirp_init(int restricted, struct in_addr vnetwork,
7 struct in_addr vnetmask, struct in_addr vhost, 9 struct in_addr vnetmask, struct in_addr vhost,
8 const char *vhostname, const char *tftp_path, 10 const char *vhostname, const char *tftp_path,
@@ -12,7 +14,8 @@ void slirp_init(int restricted, struct in_addr vnetwork, @@ -12,7 +14,8 @@ void slirp_init(int restricted, struct in_addr vnetwork,
12 void slirp_select_fill(int *pnfds, 14 void slirp_select_fill(int *pnfds,
13 fd_set *readfds, fd_set *writefds, fd_set *xfds); 15 fd_set *readfds, fd_set *writefds, fd_set *xfds);
14 16
15 -void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds); 17 +void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
  18 + int select_error);
16 19
17 void slirp_input(const uint8_t *pkt, int pkt_len); 20 void slirp_input(const uint8_t *pkt, int pkt_len);
18 21
@@ -32,4 +35,13 @@ void slirp_socket_recv(struct in_addr guest_addr, int guest_port, @@ -32,4 +35,13 @@ void slirp_socket_recv(struct in_addr guest_addr, int guest_port,
32 const uint8_t *buf, int size); 35 const uint8_t *buf, int size);
33 size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port); 36 size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port);
34 37
  38 +#else /* !CONFIG_SLIRP */
  39 +
  40 +static inline void slirp_select_fill(int *pnfds, fd_set *readfds,
  41 + fd_set *writefds, fd_set *xfds) { }
  42 +
  43 +static inline void slirp_select_poll(fd_set *readfds, fd_set *writefds,
  44 + fd_set *xfds, int select_error) { }
  45 +#endif /* !CONFIG_SLIRP */
  46 +
35 #endif 47 #endif
slirp/slirp.c
@@ -271,6 +271,10 @@ void slirp_select_fill(int *pnfds, @@ -271,6 +271,10 @@ void slirp_select_fill(int *pnfds,
271 int nfds; 271 int nfds;
272 int tmp_time; 272 int tmp_time;
273 273
  274 + if (!link_up) {
  275 + return;
  276 + }
  277 +
274 /* fail safe */ 278 /* fail safe */
275 global_readfds = NULL; 279 global_readfds = NULL;
276 global_writefds = NULL; 280 global_writefds = NULL;
@@ -281,7 +285,7 @@ void slirp_select_fill(int *pnfds, @@ -281,7 +285,7 @@ void slirp_select_fill(int *pnfds,
281 * First, TCP sockets 285 * First, TCP sockets
282 */ 286 */
283 do_slowtimo = 0; 287 do_slowtimo = 0;
284 - if (link_up) { 288 +
285 /* 289 /*
286 * *_slowtimo needs calling if there are IP fragments 290 * *_slowtimo needs calling if there are IP fragments
287 * in the fragment queue, or there are TCP connections active 291 * in the fragment queue, or there are TCP connections active
@@ -375,7 +379,6 @@ void slirp_select_fill(int *pnfds, @@ -375,7 +379,6 @@ void slirp_select_fill(int *pnfds,
375 UPD_NFDS(so->s); 379 UPD_NFDS(so->s);
376 } 380 }
377 } 381 }
378 - }  
379 382
380 /* 383 /*
381 * Setup timeout to use minimum CPU usage, especially when idle 384 * Setup timeout to use minimum CPU usage, especially when idle
@@ -413,11 +416,16 @@ void slirp_select_fill(int *pnfds, @@ -413,11 +416,16 @@ void slirp_select_fill(int *pnfds,
413 *pnfds = nfds; 416 *pnfds = nfds;
414 } 417 }
415 418
416 -void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds) 419 +void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
  420 + int select_error)
417 { 421 {
418 struct socket *so, *so_next; 422 struct socket *so, *so_next;
419 int ret; 423 int ret;
420 424
  425 + if (!link_up) {
  426 + return;
  427 + }
  428 +
421 global_readfds = readfds; 429 global_readfds = readfds;
422 global_writefds = writefds; 430 global_writefds = writefds;
423 global_xfds = xfds; 431 global_xfds = xfds;
@@ -428,7 +436,6 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds) @@ -428,7 +436,6 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
428 /* 436 /*
429 * See if anything has timed out 437 * See if anything has timed out
430 */ 438 */
431 - if (link_up) {  
432 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) { 439 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
433 tcp_fasttimo(); 440 tcp_fasttimo();
434 time_fasttimo = 0; 441 time_fasttimo = 0;
@@ -438,12 +445,11 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds) @@ -438,12 +445,11 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
438 tcp_slowtimo(); 445 tcp_slowtimo();
439 last_slowtimo = curtime; 446 last_slowtimo = curtime;
440 } 447 }
441 - }  
442 448
443 /* 449 /*
444 * Check sockets 450 * Check sockets
445 */ 451 */
446 - if (link_up) { 452 + if (!select_error) {
447 /* 453 /*
448 * Check TCP sockets 454 * Check TCP sockets
449 */ 455 */
@@ -576,7 +582,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds) @@ -576,7 +582,7 @@ void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
576 /* 582 /*
577 * See if we can start outputting 583 * See if we can start outputting
578 */ 584 */
579 - if (if_queued && link_up) 585 + if (if_queued)
580 if_start(); 586 if_start();
581 587
582 /* clear global file descriptor sets. 588 /* clear global file descriptor sets.
@@ -165,9 +165,7 @@ int main(int argc, char **argv) @@ -165,9 +165,7 @@ int main(int argc, char **argv)
165 165
166 #include "qemu_socket.h" 166 #include "qemu_socket.h"
167 167
168 -#if defined(CONFIG_SLIRP)  
169 -#include "libslirp.h"  
170 -#endif 168 +#include "slirp/libslirp.h"
171 169
172 //#define DEBUG_UNUSED_IOPORT 170 //#define DEBUG_UNUSED_IOPORT
173 //#define DEBUG_IOPORT 171 //#define DEBUG_IOPORT
@@ -4266,11 +4264,8 @@ void main_loop_wait(int timeout) @@ -4266,11 +4264,8 @@ void main_loop_wait(int timeout)
4266 tv.tv_sec = timeout / 1000; 4264 tv.tv_sec = timeout / 1000;
4267 tv.tv_usec = (timeout % 1000) * 1000; 4265 tv.tv_usec = (timeout % 1000) * 1000;
4268 4266
4269 -#if defined(CONFIG_SLIRP)  
4270 - if (slirp_is_inited()) {  
4271 - slirp_select_fill(&nfds, &rfds, &wfds, &xfds);  
4272 - }  
4273 -#endif 4267 + slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
  4268 +
4274 qemu_mutex_unlock_iothread(); 4269 qemu_mutex_unlock_iothread();
4275 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); 4270 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4276 qemu_mutex_lock_iothread(); 4271 qemu_mutex_lock_iothread();
@@ -4297,16 +4292,8 @@ void main_loop_wait(int timeout) @@ -4297,16 +4292,8 @@ void main_loop_wait(int timeout)
4297 pioh = &ioh->next; 4292 pioh = &ioh->next;
4298 } 4293 }
4299 } 4294 }
4300 -#if defined(CONFIG_SLIRP)  
4301 - if (slirp_is_inited()) {  
4302 - if (ret < 0) {  
4303 - FD_ZERO(&rfds);  
4304 - FD_ZERO(&wfds);  
4305 - FD_ZERO(&xfds);  
4306 - }  
4307 - slirp_select_poll(&rfds, &wfds, &xfds);  
4308 - }  
4309 -#endif 4295 +
  4296 + slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
4310 4297
4311 /* rearm timer, if not periodic */ 4298 /* rearm timer, if not periodic */
4312 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { 4299 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {