From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 29DAE5A026F for ; Mon, 4 Mar 2024 02:54:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709517261; bh=9jSSMMss81ZME7XLqTwitFju4z2tWWhy0a92ExTVXtA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qC3VLZSfAZvbAu6pXjY++r4gPxMMOx57KiCwEbDHaZDfxmdKbQ84Liet04nt0SB+Z iKNk5NeRCYozdmqqUt4qozNl0X9oacCiFu9VVeXPGsLx4LEJIOqlaZkjyj0eaBz0ay gxKu3WN44fcj4bE97fMjOZFo7JSsZDtmelE5pp+uctj0PM61ttOOiAKianaotmD00g 3ZCmY6LNT0oUhRFcnKDbwVtzxs2qI9RwrpHN0Yqyi9dEyM4hu0BxQRSDbvRzCDNkf7 yPB+P+zY7jIrJjOaMUaF9fS2rMbGjki3KD9aqh0GbG/SHLAPnQnbb0Z5JqOOxQmd6r 55kS1393m9Frg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tp1vx3GFKz4wp0; Mon, 4 Mar 2024 12:54:21 +1100 (AEDT) Date: Mon, 4 Mar 2024 12:54:12 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v3 7/9] checksum: introduce functions to compute the header part checksum for TCP/UDP Message-ID: References: <20240217150725.661467-8-lvivier@redhat.com> <04c99072-02ea-46a9-aac6-23116cb05fa1@redhat.com> <20240229080509.4f534831@elisabeth> <20240229095625.557367ab@elisabeth> <20240229151553.60d5cf18@elisabeth> <20240301075651.42ec7145@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="rW4uFDvZ/VA7n+/t" Content-Disposition: inline In-Reply-To: <20240301075651.42ec7145@elisabeth> Message-ID-Hash: 374TBZWEIEUGW27PLXAIPOJS4HZAOED7 X-Message-ID-Hash: 374TBZWEIEUGW27PLXAIPOJS4HZAOED7 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: Laurent Vivier , passt-dev@passt.top 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: --rW4uFDvZ/VA7n+/t Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Mar 01, 2024 at 07:56:51AM +0100, Stefano Brivio wrote: > On Fri, 1 Mar 2024 10:09:39 +1100 > David Gibson wrote: >=20 > > On Thu, Feb 29, 2024 at 03:15:53PM +0100, Stefano Brivio wrote: > > > On Thu, 29 Feb 2024 09:56:25 +0100 > > > Stefano Brivio wrote: > > > =20 > > > > On Thu, 29 Feb 2024 19:49:09 +1100 > > > > David Gibson wrote: > > > > =20 > > > > > On Thu, Feb 29, 2024 at 08:05:09AM +0100, Stefano Brivio wrote: = =20 > > > > > > On Thu, 29 Feb 2024 11:38:53 +1100 > > > > > > David Gibson wrote: > > > > > > =20 > > > > > > > On Wed, Feb 28, 2024 at 02:26:18PM +0100, Laurent Vivier wrot= e: =20 > > > > > > > > On 2/19/24 04:08, David Gibson wrote: =20 > > > > > > > > > On Sat, Feb 17, 2024 at 04:07:23PM +0100, Laurent Vivier = wrote: =20 > > > > > > > > > > > > > > > > > > [...] > > > > > > > > > =20 > > > > > > > > > > +/** > > > > > > > > > > + * proto_ipv6_header_psum() - Calculates the partial c= hecksum of an > > > > > > > > > > + * IPv6 header for UDP or TCP > > > > > > > > > > + * @payload_len: Payload length > > > > > > > > > > + * @proto: Protocol number > > > > > > > > > > + * @saddr: Source address > > > > > > > > > > + * @daddr: Destination address > > > > > > > > > > + * Returns: Partial checksum of the IPv6 header > > > > > > > > > > + */ > > > > > > > > > > +uint32_t proto_ipv6_header_psum(uint16_t payload_len, = uint8_t protocol, > > > > > > > > > > + struct in6_addr saddr, struct in6_addr daddr) = =20 > > > > > > > > >=20 > > > > > > > > > Hrm, this is passing 2 16-byte IPv6 addresses by value, w= hich might > > > > > > > > > not be what we want. =20 > > > > > > > >=20 > > > > > > > > The idea here is to avoid the pointer alignment problem (&i= p6h->saddr and > > > > > > > > &ip6h->daddr can be misaligned). =20 > > > > > > >=20 > > > > > > > Ah, right. That's a neat idea, but I'm not sure it really he= lps: I > > > > > > > think it will just move the misaligned access from inside the= function > > > > > > > to the call site, where we try to marshal the parameter from = something > > > > > > > unaligned. =20 > > > > > >=20 > > > > > > I haven't tested this yet, but note that this is generally okay= : the > > > > > > problem is *dereferencing* an unaligned pointer. But if you loa= d memory > > > > > > from an aligned pointer, and extract a value from this memory, = it's all > > > > > > fine. =20 > > > > >=20 > > > > > Right, that's kind of what I'm getting at. Assuming this value s= tarts > > > > > in an unaligned buffer, then in order to pass this by value the c= aller > > > > > will need to load from that unaligned pointer. AFAIK, the compil= er > > > > > will base the type of loads only on the pointed to type, which is= n't > > > > > changed whether we dereference in the caller or the callee. > > > > > =20 > > > > > >=20 > > > > > > Speaking MIPS, this is not safe on all CPU models: > > > > > >=20 > > > > > > la $1, 1002 # s1 now contains the value 1002 > > > > > > lw $2, 0($1) # load word from memory at 1002 + 0 into s2 > > > > > >=20 > > > > > > but this is: > > > > > >=20 > > > > > > la $1, 1000 # s1 now contains the value 1000 > > > > > > la $2, 1004 # s3 now contains the value 1004 > > > > > > lw $3, 0($1) # load word from memory at 1000 + 0 into s3 > > > > > > lw $4, 0($3) # load word from memory at 1004 + 0 into s4 > > > > > > sll $5, $3, 16 # 16-bit shift left s3 into s5 > > > > > > srl $6, $4, 16 # 16-bit shift right s4 into s6 > > > > > > or $2, $5, $6 # OR s5 and s6 into s2 =20 > > > > >=20 > > > > > Right, but I don't think merely moving the dereference to the cal= ler > > > > > will necessarily induce the compiler to generate this rather than= the > > > > > former. =20 > > > >=20 > > > > Oh, oops, I didn't realise this was the case (I haven't reviewed the > > > > patch yet). =20 > > >=20 > > > ...no, that's not the case. Dereferencing 'iph' from > > > struct tcp[46]_l2_buf_t is fine: > > >=20 > > > struct tcp4_l2_buf_t { > > > uint8_t pad[2]; /* 0 = 2 */ > > > struct tap_hdr taph; /* 2 = 18 */ > > > struct iphdr iph; /* 20 = 20 */ > > > [...] > > > } __attribute__((__packed__)); > > >=20 > > > struct tcp6_l2_buf_t { > > > uint8_t pad[2]; /* 0 = 2 */ > > > struct tap_hdr taph; /* 2 = 18 */ > > > struct ipv6hdr ip6h; /* 20 = 40 */ > > > [...] > > > } __attribute__((__packed__)); > > >=20 > > > The problematic structures are the UDP buffers: > > >=20 > > > struct udp4_l2_buf_t { > > > struct sockaddr_in s_in; /* 0 = 16 */ > > > struct tap_hdr taph; /* 16 = 18 */ > > > struct iphdr iph; /* 34 = 20 */ > > > [...] > > > } __attribute__((__aligned__(4))); > > >=20 > > > and for UDP, this patch is dereferencing buffer pointers only, not > > > pointers to headers. =20 > >=20 > > Ok... but my point remains, I'm not seeing that passing the address by > > value actually helps - it just seems to change whether we need to > > handle the unaligned load in the caller or the callee. >=20 > For UDP and IPv4 (from 6/9): >=20 > + b->iph.check =3D csum_ip4_header(b->iph.tot_len, IPPROTO_UDP, > + b->iph.saddr, b->iph.daddr); >=20 > and for IPv6 (this patch): >=20 > + b->uh.check =3D csum(&b->uh, ntohs(b->ip6h.payload_len), > + proto_ipv6_header_psum(b->ip6h.payload_len, > + IPPROTO_UDP, > + b->ip6h.saddr, > + b->ip6h.daddr)); >=20 > these cause loads starting from 'b', which is aligned, instead of > passing 'iph' or 'ip6h', unaligned, and loading from there. No... the loads are still from b->ip6h.saddr, b->ip6h.daddr and b->ip6h.payload_len. Just because we're computing the offset a bit differently doesn't change the load itself. --=20 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 --rW4uFDvZ/VA7n+/t Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXlKcMACgkQzQJF27ox 2GeG0A//Zu7tPoraVzvc8GSFMAxqxr7EBzdBG8lXRF217EjO9xkOfEsNseZRw1BB mOu4pY6/3IRIvCXmyKiZ/Cqs+/CWzJP69yfbu1+BJCeBU/ZkBn7Ql2VRRV/WechO CJfjoPNFX4cQtDFIZyRY5USGCPZQBtXV89xs4+x5Ip5it2uwGxUILupNgfSl7gyq pxHx0sa3Rf6JW1b82nmAmbBfyy4J6wNrJCwquad0GOxIlwuWGXV5RhIgUmaKV95E cp+b4GafgfflJagYmTEhb6ro844k7R98mhxzDCJrAwKX1U0eWU4VmumuzDfGW9wh Biwf9HN3SC8UH2BKMWKuTf4IGoSBBp3n5ZlQr8GxY7NYV2+f6T/vkeX7o1To1Rr/ yv1mFy4iFoXWjOh5T1L5Ys3I68y5ePdJVJ5m1UpJS1gFR/8fvqc6R6NZP/tnX+rQ D4SWrO4ZeZmP6hyd6v9DZpa3W+CmLenXKeqNFs1QphFwl6mCM4pidLNm7RjthlX7 wn9iCRYv7I1Nr24LvwtkkNhkXKnH32r+56JHGvHHQED0BfozZp37legQWQKD/R/v W7g9QHaNuogEAmf5QWYa7DjGCEM+y+sWTUUnVVoFZdKku9uLjEnul5UrvFBBJGe0 J6upw2eMrHBzwuwzZ0WU8tic/zfsbLPaILoc08CRp4sf8Z8KML0= =qWvl -----END PGP SIGNATURE----- --rW4uFDvZ/VA7n+/t--