All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Samuel Thibault <samuel.thibault@gnu.org>, qemu-devel@nongnu.org
Cc: zhanghailiang <zhang.zhanghailiang@huawei.com>,
	Li Zhijian <lizhijian@cn.fujitsu.com>,
	Stefan Hajnoczi <stefanha@gmail.com>,
	Jason Wang <jasowang@redhat.com>,
	Dave Gilbert <dgilbert@redhat.com>,
	Vasiliy Tolstov <v.tolstov@selfip.ru>,
	Huangpeng <peter.huangpeng@huawei.com>,
	Gonglei <arei.gonglei@huawei.com>,
	Jan Kiszka <jan.kiszka@siemens.com>,
	Yang Hongyang <yanghy@cn.fujitsu.com>,
	Guillaume Subiron <maethor@subiron.org>
Subject: Re: [Qemu-devel] [PATCH 05/18] slirp: Factorizing address translation
Date: Mon, 14 Dec 2015 12:41:00 +0100	[thread overview]
Message-ID: <566EAACC.6040405@redhat.com> (raw)
In-Reply-To: <20151211231431.GD2764@var.home>

On 12/12/15 00:14, Samuel Thibault wrote:
> Hello,
> 
> Thomas, this is the last refactoring patch which doesn't have a review
> yet, right?

Right. It somehow showed up out of order in my e-mail program, so I
missed it on Friday, sorry. So here's a review...

> 
> Samuel Thibault, on Fri 11 Dec 2015 01:15:17 +0100, wrote:
>> From: Guillaume Subiron <maethor@subiron.org>
>>
>> This patch factorizes some duplicate code into a new function,
>> sotranslate_out(). This function perform the address translation when a
>> packet is transmitted to the host network. If the paquet is destinated

s/paquet/packet/ and s/destinated/destined/ (I think)

>> to the host, the loopback address is used, and if the paquet is
>> destinated to the virtual DNS, the real DNS address is used. This code
>> is just a copy of the existant, but factorized and ready to manage the

s/existant/existent/

>> IPv6 case.
>>
>> On the same model, the major part of udp_output() code is moved into a
>> new sotranslate_in(). This function is directly used in sorecvfrom(),
>> like sotranslate_out() in sosendto().
>> udp_output() becoming useless, it is removed and udp_output2() is
>> renamed into udp_output(). This adds consistency with the udp6_output()
>> function introduced by further patches.
>>
>> Lastly, this factorizes some duplicate code into sotranslate_accept(), which
>> performs the address translation when a connection is established on the host
>> for port forwarding: if it comes from localhost, the host virtual address is
>> used instead.
>>
>> Signed-off-by: Guillaume Subiron <maethor@subiron.org>
>> Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
>> ---
[...]
>> diff --git a/slirp/socket.c b/slirp/socket.c
>> index bf603c9..97948e8 100644
>> --- a/slirp/socket.c
>> +++ b/slirp/socket.c
>> @@ -438,6 +438,7 @@ void
>>  sorecvfrom(struct socket *so)
>>  {
>>  	struct sockaddr_storage addr;
>> +	struct sockaddr_storage saddr, daddr;
>>  	socklen_t addrlen = sizeof(struct sockaddr_storage);
>>  
>>  	DEBUG_CALL("sorecvfrom");
>> @@ -525,11 +526,17 @@ sorecvfrom(struct socket *so)
>>  
>>  	    /*
>>  	     * If this packet was destined for CTL_ADDR,
>> -	     * make it look like that's where it came from, done by udp_output
>> +	     * make it look like that's where it came from
>>  	     */
>> +	    saddr = addr;
>> +	    sotranslate_in(so, &saddr);
>> +	    daddr = so->lhost.ss;
>> +
>>  	    switch (so->so_ffamily) {
>>  	    case AF_INET:
>> -	        udp_output(so, m, (struct sockaddr_in *) &addr);
>> +	        udp_output(so, m, (struct sockaddr_in *) &saddr,
>> +	                   (struct sockaddr_in *) &daddr,
>> +	                   so->so_iptos);
>>  	        break;
>>  	    default:
>>  	        break;
>> @@ -544,33 +551,20 @@ sorecvfrom(struct socket *so)
>>  int
>>  sosendto(struct socket *so, struct mbuf *m)
>>  {
>> -	Slirp *slirp = so->slirp;
>>  	int ret;
>> -	struct sockaddr_in addr;
>> +	struct sockaddr_storage addr;
>>  
>>  	DEBUG_CALL("sosendto");
>>  	DEBUG_ARG("so = %p", so);
>>  	DEBUG_ARG("m = %p", m);
>>  
>> -        addr.sin_family = AF_INET;
>> -	if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
>> -	    slirp->vnetwork_addr.s_addr) {
>> -	  /* It's an alias */
>> -	  if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
>> -	    if (get_dns_addr(&addr.sin_addr) < 0)
>> -	      addr.sin_addr = loopback_addr;
>> -	  } else {
>> -	    addr.sin_addr = loopback_addr;
>> -	  }
>> -	} else
>> -	  addr.sin_addr = so->so_faddr;
>> -	addr.sin_port = so->so_fport;
>> -
>> -	DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
>> +	addr = so->fhost.ss;
>> +	DEBUG_CALL(" sendto()ing)");
>> +	sotranslate_out(so, &addr);
>>  
>>  	/* Don't care what port we get */
>>  	ret = sendto(so->s, m->m_data, m->m_len, 0,
>> -		     (struct sockaddr *)&addr, sizeof (struct sockaddr));
>> +		     (struct sockaddr *)&addr, sizeof(addr));
>>  	if (ret < 0)
>>  		return -1;
>>  
>> @@ -726,3 +720,84 @@ sofwdrain(struct socket *so)
>>  	else
>>  		sofcantsendmore(so);
>>  }
>> +
>> +/*
>> + * Translate addr in host addr when it is a virtual address
>> + * :TODO:maethor:130314: Manage IPv6

In case you rework this patch, I think you could remove the "TODO" ...
that looks rather like an interim developer's comment that should not be
in the final patch, I think.

>> + */
>> +void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
>> +{
>> +    Slirp *slirp = so->slirp;
>> +    struct sockaddr_in *sin = (struct sockaddr_in *)addr;
>> +
>> +    switch (addr->ss_family) {
>> +    case AF_INET:
>> +        if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
>> +                slirp->vnetwork_addr.s_addr) {
>> +            /* It's an alias */
>> +            if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
>> +                if (get_dns_addr(&sin->sin_addr) < 0) {
>> +                    sin->sin_addr = loopback_addr;
>> +                }
>> +            } else {
>> +                sin->sin_addr = loopback_addr;
>> +            }
>> +        }
>> +
>> +        DEBUG_MISC((dfd, " addr.sin_port=%d, "
>> +            "addr.sin_addr.s_addr=%.16s\n",
>> +            ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
>> +        break;
>> +
>> +    default:
>> +        break;
>> +    }
>> +}
>> +
>> +/* :TODO:maethor:130314: IPv6 */

dito

>> +void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
>> +{
>> +    Slirp *slirp = so->slirp;
>> +    struct sockaddr_in *sin = (struct sockaddr_in *)addr;
>> +
>> +    switch (addr->ss_family) {
>> +    case AF_INET:
>> +        if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
>> +            slirp->vnetwork_addr.s_addr) {
>> +            uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
>> +
>> +            if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
>> +                sin->sin_addr = slirp->vhost_addr;
>> +            } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
>> +                       so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
>> +                sin->sin_addr = so->so_faddr;
>> +            }
>> +        }
>> +        break;
>> +
>> +    default:
>> +        break;
>> +    }
>> +}
>> +
>> +/*
>> + * Translate connections from localhost to the real hostname
>> + * :TODO: IPv6
>> + */
>> +void sotranslate_accept(struct socket *so)
>> +{
>> +    Slirp *slirp = so->slirp;
>> +
>> +    switch (so->so_ffamily) {
>> +    case AF_INET:
>> +        if (so->so_faddr.s_addr == INADDR_ANY ||
>> +            (so->so_faddr.s_addr & loopback_mask) ==
>> +            (loopback_addr.s_addr & loopback_mask)) {
>> +           so->so_faddr = slirp->vhost_addr;
>> +        }
>> +        break;
>> +
>> +    default:
>> +        break;
>> +    }
>> +}
[...]

I'd maybe also split this up into several patches, one for
sotranslate_out(), and one or two for the remaining changes, but it also
looks fine to me as it currently is already.

Reviewed-by: Thomas Huth <thuth@redhat.com>

  reply	other threads:[~2015-12-14 11:41 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-11  0:15 [Qemu-devel] [PATCHv5 00/18] slirp: Adding IPv6 support to Qemu -net user mode Samuel Thibault
2015-12-11  0:15 ` [Qemu-devel] [PATCH 01/18] slirp: goto bad in udp_input if sosendto fails Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 02/18] slirp: Generalizing and neutralizing code before adding IPv6 stuff Samuel Thibault
2015-12-11 13:38     ` Thomas Huth
2015-12-11 13:43       ` Thomas Huth
2015-12-11 13:47         ` Samuel Thibault
2015-12-11 13:52           ` Thomas Huth
2015-12-11 13:49         ` Thomas Huth
2015-12-11 14:01           ` Samuel Thibault
2015-12-11 14:32             ` Thomas Huth
2015-12-11 14:55               ` Samuel Thibault
2015-12-11 15:09                 ` Thomas Huth
2015-12-11 15:40                   ` Laszlo Ersek
2015-12-11 15:41                     ` Samuel Thibault
2015-12-11 16:17                     ` Eric Blake
2015-12-11 18:01                       ` Laszlo Ersek
2015-12-11 13:45       ` Samuel Thibault
2015-12-11 20:10       ` Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 03/18] slirp: Reindent after refactoring Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 04/18] slirp: Make Socket structure IPv6 compatible Samuel Thibault
2015-12-11 14:47     ` Thomas Huth
2015-12-11  0:15   ` [Qemu-devel] [PATCH 05/18] slirp: Factorizing address translation Samuel Thibault
2015-12-11 23:14     ` Samuel Thibault
2015-12-14 11:41       ` Thomas Huth [this message]
2015-12-11  0:15   ` [Qemu-devel] [PATCH 06/18] slirp: Factorizing and cleaning solookup() Samuel Thibault
2015-12-11 15:06     ` Thomas Huth
2015-12-11 19:29       ` Samuel Thibault
2015-12-11 19:38         ` Samuel Thibault
2015-12-11 19:51           ` Samuel Thibault
2015-12-11 20:02             ` Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 07/18] slirp: Make udp_attach IPv6 compatible Samuel Thibault
2015-12-11 15:12     ` Thomas Huth
2015-12-11  0:15   ` [Qemu-devel] [PATCH 08/18] slirp: Adding family argument to tcp_fconnect() Samuel Thibault
2015-12-11 15:26     ` Thomas Huth
2015-12-11  0:15   ` [Qemu-devel] [PATCH 09/18] qemu/timer.h : Adding function to second scale Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 10/18] slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 11/18] slirp: Adding ICMPv6 error sending Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 12/18] slirp: Adding IPv6 UDP support Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 13/18] slirp: Factorizing tcpiphdr structure with an union Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 14/18] slirp: Generalizing and neutralizing various TCP functions before adding IPv6 stuff Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 15/18] slirp: Reindent after refactoring Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 16/18] slirp: Handle IPv6 in TCP functions Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 17/18] slirp: Adding IPv6 address for DNS relay Samuel Thibault
2015-12-11  0:15   ` [Qemu-devel] [PATCH 18/18] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses Samuel Thibault
2015-12-11 11:54   ` [Qemu-devel] [PATCH 01/18] slirp: goto bad in udp_input if sosendto fails Thomas Huth
2015-12-11 12:05     ` Samuel Thibault
  -- strict thread matches above, loose matches on Subject: below --
2015-07-28 22:57 [Qemu-devel] [PATCHv4 00/18] slirp: Adding IPv6 support to Qemu -net user mode Samuel Thibault
2015-07-28 22:57 ` [Qemu-devel] [PATCH 01/18] slirp: goto bad in udp_input if sosendto fails Samuel Thibault
2015-07-28 22:57   ` [Qemu-devel] [PATCH 05/18] slirp: Factorizing address translation Samuel Thibault
2014-03-30 22:22 [Qemu-devel] [PATCHv4 00/18] slirp: Adding IPv6 support to Qemu -net user mode Samuel Thibault
2014-03-30 22:22 ` [Qemu-devel] [PATCH 05/18] slirp: Factorizing address translation Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=566EAACC.6040405@redhat.com \
    --to=thuth@redhat.com \
    --cc=arei.gonglei@huawei.com \
    --cc=dgilbert@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=jasowang@redhat.com \
    --cc=lizhijian@cn.fujitsu.com \
    --cc=maethor@subiron.org \
    --cc=peter.huangpeng@huawei.com \
    --cc=qemu-devel@nongnu.org \
    --cc=samuel.thibault@gnu.org \
    --cc=stefanha@gmail.com \
    --cc=v.tolstov@selfip.ru \
    --cc=yanghy@cn.fujitsu.com \
    --cc=zhang.zhanghailiang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.