public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 02/10] tcp: Helper to encode IPv4-mapped IPv6 addresses
Date: Tue, 8 Nov 2022 11:46:57 +1100	[thread overview]
Message-ID: <Y2mnAVDH+rX+YzQ2@yekko> (raw)
In-Reply-To: <20221107190819.397599a9@elisabeth>

[-- Attachment #1: Type: text/plain, Size: 4059 bytes --]

On Mon, Nov 07, 2022 at 07:08:19PM +0100, Stefano Brivio wrote:
> On Fri,  4 Nov 2022 19:43:25 +1100
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > In the tcp_conn structure we have space for an IPv6 address, and use
> > IPv4-mapped IPv6 addresses to describe IPv4 connections.  We open code
> > the construction of those IPv4-mapped address in two places.
> > 
> > Avoid the duplication with a helper function.
> 
> Much nicer, indeed.
> 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  tcp.c  |  9 ++-------
> >  util.c | 20 ++++++++++++++++++++
> >  util.h |  1 +
> >  3 files changed, 23 insertions(+), 7 deletions(-)
> > 
> > diff --git a/tcp.c b/tcp.c
> > index 3d48d6e..26dd268 100644
> > --- a/tcp.c
> > +++ b/tcp.c
> > @@ -2217,9 +2217,7 @@ static void tcp_conn_from_tap(struct ctx *c, int af, const void *addr,
> >  		sa = (struct sockaddr *)&addr4;
> >  		sl = sizeof(addr4);
> >  
> > -		memset(&conn->a.a4.zero, 0,    sizeof(conn->a.a4.zero));
> > -		memset(&conn->a.a4.one,  0xff, sizeof(conn->a.a4.one));
> > -		memcpy(&conn->a.a4.a,    addr, sizeof(conn->a.a4.a));
> > +		encode_ip4mapped_ip6(&conn->a.a6, addr);
> >  	} else {
> >  		sa = (struct sockaddr *)&addr6;
> >  		sl = sizeof(addr6);
> > @@ -2902,15 +2900,12 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref,
> >  
> >  		memcpy(&sa4, &sa, sizeof(sa4));
> >  
> > -		memset(&conn->a.a4.zero,   0, sizeof(conn->a.a4.zero));
> > -		memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one));
> > -
> >  		if (IN4_IS_ADDR_LOOPBACK(&sa4.sin_addr) ||
> >  		    IN4_IS_ADDR_UNSPECIFIED(&sa4.sin_addr) ||
> >  		    IN4_ARE_ADDR_EQUAL(&sa4.sin_addr, &c->ip4.addr_seen))
> >  			sa4.sin_addr = c->ip4.gw;
> >  
> > -		conn->a.a4.a = sa4.sin_addr;
> > +		encode_ip4mapped_ip6(&conn->a.a6, &sa4.sin_addr);
> >  
> >  		conn->sock_port = ntohs(sa4.sin_port);
> >  		conn->tap_port = ref.r.p.tcp.tcp.index;
> > diff --git a/util.c b/util.c
> > index 514bd44..257d0b6 100644
> > --- a/util.c
> > +++ b/util.c
> > @@ -482,3 +482,23 @@ int write_file(const char *path, const char *buf)
> >  	close(fd);
> >  	return len == 0 ? 0 : -1;
> >  }
> > +
> > +struct ip4mapped_ip6 {
> > +	uint8_t zero[10];
> > +	uint8_t one[2];
> > +	struct in_addr a4;
> > +};
> 
> Document fields even if they're obvious. Can we reuse this part in
> struct conn instead of using struct in6_addr there as you do later in
> 7/10? I don't have a strong preference though.

Yeah, we could.  Originally I was trying to make this even more
hidden, with the decode function returning a struct in_addr *, that
would be NULL if the IPv6 address wasn't v4-mapped.  Unfortunately I
seemed to hit more weird compiler aliasing issues that caused hard to
predict failures.

Hrm... your comment gives me some thoughts on maybe a better way to do
this, I'll look into it.

> 
> > +
> > +/**
> > + * encode_ip4mapped_ip6() - Convert an IPv4 address to an IPv4-mapped IPv6 address
> > + * @ip6:	Buffer to store the IPv4-mapped IPv6 address
> > + * @ip4:	IPv4 address, network order
> > + */
> > +void encode_ip4mapped_ip6(struct in6_addr *ip6, const void *ip4)
> > +{
> > +	struct ip4mapped_ip6 *a = (struct ip4mapped_ip6 *)ip6;
> > +
> > +	memset(&a->zero, 0, sizeof(a->zero));
> > +	memset(&a->one, 0xff, sizeof(a->one));
> > +	memcpy(&a->a4, ip4, sizeof(a->a4));
> > +}
> > diff --git a/util.h b/util.h
> > index 2d4e1ff..f7d6a6f 100644
> > --- a/util.h
> > +++ b/util.h
> > @@ -209,5 +209,6 @@ void write_pidfile(int fd, pid_t pid);
> >  int __daemon(int pidfile_fd, int devnull_fd);
> >  int fls(unsigned long x);
> >  int write_file(const char *path, const char *buf);
> > +void encode_ip4mapped_ip6(struct in6_addr *ip6, const void *ip4);
> >  
> >  #endif /* UTIL_H */
> 

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2022-11-08  1:02 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04  8:43 [PATCH 00/10] RFC: Preliminaries for using share IPv4 & IPv6 sockets David Gibson
2022-11-04  8:43 ` [PATCH 01/10] tcp: no v6 flag in ref David Gibson
2022-11-07 18:07   ` Stefano Brivio
2022-11-08  0:35     ` David Gibson
2022-11-04  8:43 ` [PATCH 02/10] tcp: Helper to encode IPv4-mapped IPv6 addresses David Gibson
2022-11-07 18:08   ` Stefano Brivio
2022-11-08  0:46     ` David Gibson [this message]
2022-11-04  8:43 ` [PATCH 03/10] tcp: Partially unify IPv4 and IPv6 paths in tcp_hash_match() David Gibson
2022-11-07 18:08   ` Stefano Brivio
2022-11-08  0:51     ` David Gibson
2022-11-04  8:43 ` [PATCH 04/10] tcp: Hash IPv4 and IPv4-mapped-IPv6 addresses the same David Gibson
2022-11-04  8:43 ` [PATCH 05/10] tcp: Take tcp_hash_insert() address from struct tcp_conn David Gibson
2022-11-04  8:43 ` [PATCH 06/10] tcp: Unify IPv4 and IPv6 paths for hashing and matching David Gibson
2022-11-04  8:43 ` [PATCH 07/10] tcp: Remove ugly address union from struct tcp_conn David Gibson
2022-11-07 18:08   ` Stefano Brivio
2022-11-08  0:54     ` David Gibson
2022-11-04  8:43 ` [PATCH 08/10] tcp: Unify initial sequence numbers for IPv4 and IPv6 David Gibson
2022-11-04  8:43 ` [PATCH 09/10] tcp: Have tcp_seq_init() take its parameters from struct tcp_conn David Gibson
2022-11-04  8:43 ` [PATCH 10/10] tcp: Fix small error in tcp_seq_init() time handling David Gibson
2022-11-07 18:08   ` Stefano Brivio
2022-11-08  0:59     ` David Gibson
2022-11-04  8:47 ` [PATCH 00/10] RFC: Preliminaries for using share IPv4 & IPv6 sockets Stefano Brivio

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=Y2mnAVDH+rX+YzQ2@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).