diff options
author | Jan Kiszka <jan.kiszka@siemens.com> | 2009-06-24 14:42:31 +0200 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2009-06-29 08:52:49 -0500 |
commit | 9f8bd0421dc03b2640ac2d0a4d702354a218b2ab (patch) | |
tree | b866bf7d4864fac7a8313bdf04cca2f997242951 | |
parent | 460fec67ee3807bb2eb189587ffe803a48f317e5 (diff) | |
download | qemu-9f8bd0421dc03b2640ac2d0a4d702354a218b2ab.zip qemu-9f8bd0421dc03b2640ac2d0a4d702354a218b2ab.tar.gz qemu-9f8bd0421dc03b2640ac2d0a4d702354a218b2ab.tar.bz2 |
slirp: Use internal state in interface
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.
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
-rw-r--r-- | net.c | 105 | ||||
-rw-r--r-- | slirp/if.c | 2 | ||||
-rw-r--r-- | slirp/libslirp.h | 35 | ||||
-rw-r--r-- | slirp/misc.c | 3 | ||||
-rw-r--r-- | slirp/slirp.c | 46 | ||||
-rw-r--r-- | slirp/slirp.h | 1 |
6 files changed, 108 insertions, 84 deletions
@@ -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 */ @@ -2498,7 +2521,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)); @@ -2507,7 +2530,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 @@ -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; /* diff --git a/slirp/libslirp.h b/slirp/libslirp.h index 3bda691..521de3e 100644 --- a/slirp/libslirp.h +++ b/slirp/libslirp.h @@ -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 */ diff --git a/slirp/misc.c b/slirp/misc.c index c2b66fd..e9f08fd 100644 --- a/slirp/misc.c +++ b/slirp/misc.c @@ -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; diff --git a/slirp/slirp.c b/slirp/slirp.c index 25bc8a4..7e86124 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -187,11 +187,11 @@ static void slirp_init_once(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, 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) { Slirp *slirp = &slirp_instance; @@ -226,7 +226,11 @@ void slirp_init(int restricted, struct in_addr vnetwork, slirp->vdhcp_startaddr = vdhcp_start; slirp->vnameserver_addr = vnameserver; + slirp->opaque = opaque; + register_savevm("slirp", 0, 2, slirp_state_save, slirp_state_load, slirp); + + return slirp; } #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) @@ -635,7 +639,7 @@ static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) rah->ar_sip = ah->ar_tip; memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); rah->ar_tip = ah->ar_sip; - slirp_output(arp_reply, sizeof(arp_reply)); + slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply)); } break; case ARPOP_REPLY: @@ -650,9 +654,8 @@ static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) } } -void slirp_input(const uint8_t *pkt, int pkt_len) +void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) { - Slirp *slirp = &slirp_instance; struct mbuf *m; int proto; @@ -724,7 +727,7 @@ void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len) /* target IP */ rah->ar_tip = iph->ip_dst.s_addr; slirp->client_ipaddr = iph->ip_dst; - slirp_output(arp_req, sizeof(arp_req)); + slirp_output(slirp->opaque, arp_req, sizeof(arp_req)); } else { memcpy(eh->h_dest, slirp->client_ethaddr, ETH_ALEN); memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); @@ -732,14 +735,14 @@ void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len) memcpy(&eh->h_source[2], &slirp->vhost_addr, 4); eh->h_proto = htons(ETH_P_IP); memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len); - slirp_output(buf, ip_data_len + ETH_HLEN); + slirp_output(slirp->opaque, buf, ip_data_len + ETH_HLEN); } } /* Drop host forwarding rule, return 0 if found. */ -int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) +int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, + int host_port) { - Slirp *slirp = &slirp_instance; struct socket *so; struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); struct sockaddr_in addr; @@ -761,11 +764,9 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) return -1; } -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_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, + int host_port, struct in_addr guest_addr, int guest_port) { - Slirp *slirp = &slirp_instance; - if (!guest_addr.s_addr) { guest_addr = slirp->vdhcp_startaddr; } @@ -781,11 +782,9 @@ int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port, return 0; } -int slirp_add_exec(int do_pty, const void *args, struct in_addr guest_addr, - int guest_port) +int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, + struct in_addr guest_addr, int guest_port) { - Slirp *slirp = &slirp_instance; - if (!guest_addr.s_addr) { guest_addr.s_addr = slirp->vnetwork_addr.s_addr | (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr); @@ -824,9 +823,9 @@ slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port) return NULL; } -size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port) +size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, + int guest_port) { - Slirp *slirp = &slirp_instance; struct iovec iov[2]; struct socket *so; @@ -841,10 +840,9 @@ size_t slirp_socket_can_recv(struct in_addr guest_addr, int guest_port) return sopreprbuf(so, iov, NULL); } -void slirp_socket_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) { - Slirp *slirp = &slirp_instance; int ret; struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); diff --git a/slirp/slirp.h b/slirp/slirp.h index b1e8d4e..cb1a746 100644 --- a/slirp/slirp.h +++ b/slirp/slirp.h @@ -256,6 +256,7 @@ struct Slirp { char *tftp_prefix; struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX]; + void *opaque; }; extern Slirp slirp_instance; |