From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202502 header.b=lN2dx456; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 186C85A061D for ; Mon, 03 Feb 2025 03:17:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202502; t=1738549000; bh=XoYVw4P8mWwuMvc7jL1b8BdIjkZtJI2lPL1U9bkge5g=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lN2dx456W6dioSkzNNe6VRkscGQPfHN5fcbcX6/pcBfPlhyzTvRk6TVVmIIRAVD8z KV9h0dIbD4t6Dy43kK0TSYjNKMAF7HiWBLO1sP0p6g9b4twMu62ZJ/kdmp7154iaRq xGPL076mQ4vD7DEI6MXSI1jpu8mWSjINLffbEXGuySGGCkMq1+o//biGxu1UImmRY+ 8TQ8JAxtu4E8jno0h/AIahPyfpHXwYnxRkDIzDh9OblTdyS1HvU55Lskd/Vbv0lRCR dJV3IK2TewaswkrtmDpSG1GPZeRN8QqUKLD9j25whZedf+hzeSm2JloRL19dR8exZt LRqFt1M+colNg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4YmVVc4Vxlz4wy9; Mon, 3 Feb 2025 13:16:40 +1100 (AEDT) Date: Mon, 3 Feb 2025 13:05:33 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v3 18/20] tcp: Get our socket port using getsockname() when connecting from guest Message-ID: References: <20250131193953.3034031-1-sbrivio@redhat.com> <20250131193953.3034031-19-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="oJbtuRQuAHFeNXOW" Content-Disposition: inline In-Reply-To: <20250131193953.3034031-19-sbrivio@redhat.com> Message-ID-Hash: MQ25EB4TNPGJWDFGHVRMWYPHKEOBZXQT X-Message-ID-Hash: MQ25EB4TNPGJWDFGHVRMWYPHKEOBZXQT X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --oJbtuRQuAHFeNXOW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jan 31, 2025 at 08:39:51PM +0100, Stefano Brivio wrote: > For migration only: we need to store 'oport', our socket-side port, > as we establish a connection from the guest, so that we can bind the > same oport as source port in the migration target. >=20 > Use getsockname() to fetch that. >=20 > Signed-off-by: Stefano Brivio > --- > flow.c | 4 ++-- > flow_table.h | 4 ++-- > tcp.c | 24 +++++++++++++++++++++++- > 3 files changed, 27 insertions(+), 5 deletions(-) >=20 > diff --git a/flow.c b/flow.c > index 5638ff1..506cbac 100644 > --- a/flow.c > +++ b/flow.c > @@ -411,8 +411,8 @@ const struct flowside *flow_initiate_sa(union flow *f= low, uint8_t pif, > * > * Return: pointer to the target flowside information > */ > -const struct flowside *flow_target(const struct ctx *c, union flow *flow, > - uint8_t proto) > +struct flowside *flow_target(const struct ctx *c, union flow *flow, > + uint8_t proto) > { > char estr[INANY_ADDRSTRLEN], fstr[INANY_ADDRSTRLEN]; > struct flow_common *f =3D &flow->f; > diff --git a/flow_table.h b/flow_table.h > index 633805d..b107107 100644 > --- a/flow_table.h > +++ b/flow_table.h > @@ -178,8 +178,8 @@ const struct flowside *flow_target_af(union flow *flo= w, uint8_t pif, > sa_family_t af, > const void *saddr, in_port_t sport, > const void *daddr, in_port_t dport); > -const struct flowside *flow_target(const struct ctx *c, union flow *flow, > - uint8_t proto); > +struct flowside *flow_target(const struct ctx *c, union flow *flow, > + uint8_t proto); > =20 > union flow *flow_set_type(union flow *flow, enum flow_type type); > #define FLOW_SET_TYPE(flow_, t_, var_) (&flow_set_type((flow_), (t_))->v= ar_) > diff --git a/tcp.c b/tcp.c > index 0bd2a02..4fd405b 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -1471,6 +1471,8 @@ static void tcp_bind_outbound(const struct ctx *c, > * @opts: Pointer to start of options > * @optlen: Bytes in options: caller MUST ensure available length > * @now: Current timestamp > + * > + * #syscalls:vu getsockname > */ > static void tcp_conn_from_tap(const struct ctx *c, sa_family_t af, > const void *saddr, const void *daddr, > @@ -1479,9 +1481,10 @@ static void tcp_conn_from_tap(const struct ctx *c,= sa_family_t af, > { > in_port_t srcport =3D ntohs(th->source); > in_port_t dstport =3D ntohs(th->dest); > - const struct flowside *ini, *tgt; > + const struct flowside *ini; > struct tcp_tap_conn *conn; > union sockaddr_inany sa; > + struct flowside *tgt; > union flow *flow; > int s =3D -1, mss; > uint64_t hash; > @@ -1586,6 +1589,25 @@ static void tcp_conn_from_tap(const struct ctx *c,= sa_family_t af, > } > =20 > tcp_epoll_ctl(c, conn); > + > + if (c->mode =3D=3D MODE_VU) { /* To rebind to same oport after migratio= n */ I suspect we'll want this local side information in more places in future, but this is fine for now. > + if (af =3D=3D AF_INET) { > + struct sockaddr_in s_in; > + socklen_t sl; > + > + sl =3D sizeof(s_in); > + getsockname(s, (struct sockaddr *)&s_in, &sl); > + tgt->oport =3D ntohs(s_in.sin_port); Since we're already doing the getsockname() we should also update tgt->oaddr, and that might matter in cases where the host has multiple local addresses. > + } else { > + struct sockaddr_in6 s_in6; > + socklen_t sl; > + > + sl =3D sizeof(s_in6); > + getsockname(s, (struct sockaddr *)&s_in6, &sl); > + tgt->oport =3D ntohs(s_in6.sin6_port); > + } We should add an inany_getsockname() or something helper for this. In fact I'm pretty sure I wrote one at some point, but it was lost in the shuffles of various flow table iterations. > + } > + > FLOW_ACTIVATE(conn); > return; > =20 --=20 David Gibson (he or they) | 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 --oJbtuRQuAHFeNXOW Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmegJGwACgkQzQJF27ox 2GdaUQ//THmHPuA/9VnvVDKSiCzZFvzsHK1CwW0gJ8wMj2oXNoyxi7+09hNmXNGG 9WtDa2gH2+KuF362aXxQGBB9WDI+1lnaohyRO8Q0VIins/Sq3/G5M1jxmPdDl4VL tRiZu23VxE0BDhhVUXCLjGmlBmU9YoaosFXlEpNYSmcpUgYLNmHKQMO8O3TEakI7 7R0kRL59vsa3qXc6dIMOteBcWmm5k7iViaqTBSpqeiFX7xL+xvn36hcKGDHnO7tr cAl/VOrZVCx8CU3Ef6S8eg/s8Hw2E3swZYZ3m4ufe2rlKj7NPK90h2x9Bs3hEo46 Kq2aD7mMzLzTkPq8IwRh0IpITvL+aP8a23oTFz5hdST1Ex0ZZ3UaRe30u54KnWfZ 97RPw6EmkXCEVMdZu+F4GBD5oq3lXEw1aiMnGexdK8qSuC2DgLZL2KS0T09KfPq5 baomQotDMAMXYCE8f9VaYuUPPZT23Cg8AWoQ2G3MnOI5dBIVfHdXAu/sFs+eNJF5 XfRMcN86xGQ28AbwnPctDCpYIYXlAjFpfuQEUF2r7rH/5ZD7dULZ4ZP7smsRN66e YPxcg9UveGBXsQ8UkjWMYvfuSXwtnKEZ4kjmSN2Z5zioFBaV2nDTXj3NOc/vVWTX bofvrNjw8f7IsOjy6mUqIkkZI1JUEIgwFV9rclEqQxZGaPQ94kc= =lhb0 -----END PGP SIGNATURE----- --oJbtuRQuAHFeNXOW--