From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MXW2I-0005II-Ml for qemu-devel@nongnu.org; Sun, 02 Aug 2009 04:03:18 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MXW2C-0005Ds-Hv for qemu-devel@nongnu.org; Sun, 02 Aug 2009 04:03:17 -0400 Received: from [199.232.76.173] (port=34550 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MXW2C-0005Da-BG for qemu-devel@nongnu.org; Sun, 02 Aug 2009 04:03:12 -0400 Received: from fmmailgate03.web.de ([217.72.192.234]:53090) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MXW2B-0004La-EP for qemu-devel@nongnu.org; Sun, 02 Aug 2009 04:03:11 -0400 Message-ID: <4A75483E.8000200@web.de> Date: Sun, 02 Aug 2009 10:03:10 +0200 From: Jan Kiszka MIME-Version: 1.0 Subject: Re: [Qemu-devel] [PATCH] slirp: Read host DNS config on demand References: <1249089022.20690.11.camel@localhost.localdomain> <20090802074352.GB3339@redhat.com> In-Reply-To: <20090802074352.GB3339@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig93E86FDE973A9BBCC3220808" Sender: jan.kiszka@web.de List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: qemu-devel@nongnu.org, Ed Swierk This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig93E86FDE973A9BBCC3220808 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Michael S. Tsirkin wrote: > On Fri, Jul 31, 2009 at 06:10:22PM -0700, Ed Swierk wrote: >> Currently the qemu user-mode networking stack reads the host DNS >> configuration (/etc/resolv.conf or the Windows equivalent) only once >> when qemu starts. This causes name lookups in the guest to fail if th= e >> host is moved to a different network from which the original DNS serve= rs >> are unreachable, a common occurrence when the host is a laptop. >> >> This patch changes the slirp code to read the host DNS configuration o= n >> demand, caching the results for 10 seconds to avoid unnecessary overhe= ad >> if name lookups occur in rapid succession. >> >> Signed-off-by: Ed Swierk >> Acked-by: Jan Kiszka >=20 > Introducing a random delay until update might surprise users. > Would it make sense to use something like inotify instead? IMHO, 10 s is below the user surprise threshold for a dynamically changing network link. Moreover, inotify does not exist on all platforms.= Let's keep things simple for the first step, we could still further improve it later on. 10 s are already an improvement over the current infinite timeout. Jan >=20 >> --- >> diff --git a/slirp/ip_icmp.c b/slirp/ip_icmp.c >> index 95a4b39..751a8e2 100644 >> --- a/slirp/ip_icmp.c >> +++ b/slirp/ip_icmp.c >> @@ -127,7 +127,8 @@ icmp_input(struct mbuf *m, int hlen) >> slirp->vnetwork_addr.s_addr) { >> /* It's an alias */ >> if (so->so_faddr.s_addr =3D=3D slirp->vnameserver_addr.s_addr) { >> - addr.sin_addr =3D dns_addr; >> + if (get_dns_addr(&addr.sin_addr) < 0) >> + addr.sin_addr =3D loopback_addr; >> } else { >> addr.sin_addr =3D loopback_addr; >> } >> diff --git a/slirp/libslirp.h b/slirp/libslirp.h >> index 93087ed..67c70e3 100644 >> --- a/slirp/libslirp.h >> +++ b/slirp/libslirp.h >> @@ -8,6 +8,8 @@ >> struct Slirp; >> typedef struct Slirp Slirp; >> =20 >> +int get_dns_addr(struct in_addr *pdns_addr); >> + >> Slirp *slirp_init(int restricted, struct in_addr vnetwork, >> struct in_addr vnetmask, struct in_addr vhost, >> const char *vhostname, const char *tftp_path, >> diff --git a/slirp/main.h b/slirp/main.h >> index 28d92d8..e87b068 100644 >> --- a/slirp/main.h >> +++ b/slirp/main.h >> @@ -32,7 +32,6 @@ extern u_int curtime; >> extern fd_set *global_readfds, *global_writefds, *global_xfds; >> 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; >> diff --git a/slirp/slirp.c b/slirp/slirp.c >> index e883f82..987aad9 100644 >> --- a/slirp/slirp.c >> +++ b/slirp/slirp.c >> @@ -29,8 +29,6 @@ >> =20 >> /* host address */ >> struct in_addr our_addr; >> -/* host dns address */ >> -struct in_addr dns_addr; >> /* host loopback address */ >> struct in_addr loopback_addr; >> =20 >> @@ -51,9 +49,12 @@ static int do_slowtimo; >> TAILQ_HEAD(slirp_instances, Slirp) slirp_instances =3D >> TAILQ_HEAD_INITIALIZER(slirp_instances); >> =20 >> +struct in_addr dns_addr =3D { 0 }; >> +u_int dns_addr_time =3D 0; >> + >> #ifdef _WIN32 >> =20 >> -static int get_dns_addr(struct in_addr *pdns_addr) >> +int get_dns_addr(struct in_addr *pdns_addr) >> { >> FIXED_INFO *FixedInfo=3DNULL; >> ULONG BufLen; >> @@ -61,6 +62,11 @@ static int get_dns_addr(struct in_addr *pdns_addr) >> IP_ADDR_STRING *pIPAddr; >> struct in_addr tmp_addr; >> =20 >> + if (dns_addr.s_addr !=3D 0 && (curtime - dns_addr_time) < 10000) = { >> + *pdns_addr =3D dns_addr; >> + return 0; >> + } >> + >> FixedInfo =3D (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO))= ; >> BufLen =3D sizeof(FIXED_INFO); >> =20 >> @@ -84,6 +90,8 @@ static int get_dns_addr(struct in_addr *pdns_addr) >> pIPAddr =3D &(FixedInfo->DnsServerList); >> inet_aton(pIPAddr->IpAddress.String, &tmp_addr); >> *pdns_addr =3D tmp_addr; >> + dns_addr =3D tmp_addr; >> + dns_addr_time =3D curtime; >> if (FixedInfo) { >> GlobalFree(FixedInfo); >> FixedInfo =3D NULL; >> @@ -98,7 +106,7 @@ static void winsock_cleanup(void) >> =20 >> #else >> =20 >> -static int get_dns_addr(struct in_addr *pdns_addr) >> +int get_dns_addr(struct in_addr *pdns_addr) >> { >> char buff[512]; >> char buff2[257]; >> @@ -106,6 +114,11 @@ static int get_dns_addr(struct in_addr *pdns_addr= ) >> int found =3D 0; >> struct in_addr tmp_addr; >> =20 >> + if (dns_addr.s_addr !=3D 0 && (curtime - dns_addr_time) < 10000) = { >> + *pdns_addr =3D dns_addr; >> + return 0; >> + } >> + >> f =3D fopen("/etc/resolv.conf", "r"); >> if (!f) >> return -1; >> @@ -120,8 +133,11 @@ static int get_dns_addr(struct in_addr *pdns_addr= ) >> if (tmp_addr.s_addr =3D=3D loopback_addr.s_addr) >> tmp_addr =3D our_addr; >> /* If it's the first one, set it to dns_addr */ >> - if (!found) >> + if (!found) { >> *pdns_addr =3D tmp_addr; >> + dns_addr =3D tmp_addr; >> + dns_addr_time =3D curtime; >> + } >> #ifdef DEBUG >> else >> lprint(", "); >> @@ -177,11 +193,6 @@ static void slirp_init_once(void) >> if (our_addr.s_addr =3D=3D 0) { >> our_addr =3D loopback_addr; >> } >> - >> - /* FIXME: This address may change during runtime */ >> - if (get_dns_addr(&dns_addr) < 0) { >> - dns_addr =3D loopback_addr; >> - } >> } >> =20 >> static void slirp_state_save(QEMUFile *f, void *opaque); >> diff --git a/slirp/socket.c b/slirp/socket.c >> index d8fbe89..207109c 100644 >> --- a/slirp/socket.c >> +++ b/slirp/socket.c >> @@ -552,7 +552,8 @@ sosendto(struct socket *so, struct mbuf *m) >> slirp->vnetwork_addr.s_addr) { >> /* It's an alias */ >> if (so->so_faddr.s_addr =3D=3D slirp->vnameserver_addr.s_addr) { >> - addr.sin_addr =3D dns_addr; >> + if (get_dns_addr(&addr.sin_addr) < 0) >> + addr.sin_addr =3D loopback_addr; >> } else { >> addr.sin_addr =3D loopback_addr; >> } >> diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c >> index 51b3834..0417345 100644 >> --- a/slirp/tcp_subr.c >> +++ b/slirp/tcp_subr.c >> @@ -340,7 +340,8 @@ int tcp_fconnect(struct socket *so) >> slirp->vnetwork_addr.s_addr) { >> /* It's an alias */ >> if (so->so_faddr.s_addr =3D=3D slirp->vnameserver_addr.s_addr) = { >> - addr.sin_addr =3D dns_addr; >> + if (get_dns_addr(&addr.sin_addr) < 0) >> + addr.sin_addr =3D loopback_addr; >> } else { >> addr.sin_addr =3D loopback_addr; >> } >> >> >> --------------enig93E86FDE973A9BBCC3220808 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iEYEARECAAYFAkp1SD4ACgkQniDOoMHTA+kG9QCfe7AMgGWV7ZJcgAjeFvCb0hyK 6H8AnjrjJGr8mvnZHyvPFxv2C7K8R89z =xYEe -----END PGP SIGNATURE----- --------------enig93E86FDE973A9BBCC3220808--