From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id DDF715A0272 for ; Fri, 8 Mar 2024 02:20:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709860848; bh=Yqa7xbzKQUwx1b7n+MVXErzvPVN3wj0PfFQLdLS+EZU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bvZBsXTwwaoxyTogBGuxX4hvQAjff+XxBe1aG4ljg5lmWRQ9J/HYLLLVECrLxZNPG 0TDyKhdqBBheBUYiPIpyNS1GTcOIP2hZM4sg6gAWmNSWZA1wThiVAIZnDqDyF+g/l5 2Uizq5SMp1g9VPT7dXBaF2hCLPqrwMz1mrcaRa6mZ7kcPZpUK4wQ1x4od+lCOBKbfG b/ZWp51bVp7Ecv8yZpD1md1mlTXVQATaImvrvJ1fFfvBYErBe2J6wAlrbbY1O+Lgtr s1MzQhk07i76Tgzpjd/TogDtR1MX/vlbkAaa6emgZnoPMunsLVGF9TuxGbj/gX7TJI 8t8ArzPGEt9CA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TrSzN3zqmz4wcF; Fri, 8 Mar 2024 12:20:48 +1100 (AEDT) Date: Fri, 8 Mar 2024 12:20:30 +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: <20240229095625.557367ab@elisabeth> <20240229151553.60d5cf18@elisabeth> <20240301075651.42ec7145@elisabeth> <20240304120040.1cebc230@elisabeth> <20240304234717.5f697efd@elisabeth> <20240308010845.4a15b5a5@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="rkxQje/6EhL8/JjH" Content-Disposition: inline In-Reply-To: <20240308010845.4a15b5a5@elisabeth> Message-ID-Hash: EPZ4FIQZT5BFJU3C56MBDRFQ73OD5N7C X-Message-ID-Hash: EPZ4FIQZT5BFJU3C56MBDRFQ73OD5N7C 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: --rkxQje/6EhL8/JjH Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Mar 08, 2024 at 01:08:45AM +0100, Stefano Brivio wrote: > On Wed, 6 Mar 2024 16:09:23 +1100 > David Gibson wrote: >=20 > > On Mon, Mar 04, 2024 at 11:47:17PM +0100, Stefano Brivio wrote: > > > On Mon, 4 Mar 2024 12:00:40 +0100 > > > Stefano Brivio wrote: > > > =20 > > > > On Mon, 4 Mar 2024 12:54:12 +1100 > > > > David Gibson wrote: > > > > =20 > > > > > On Fri, Mar 01, 2024 at 07:56:51AM +0100, Stefano Brivio wrote: = =20 > > > > > > 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 wrot= e: =20 > > > > > > > > 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 Brivi= o 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 V= ivier wrote: =20 > > > > > > > > > > > > > On 2/19/24 04:08, David Gibson wrote: = =20 > > > > > > > > > > > > > > On Sat, Feb 17, 2024 at 04:07:23PM +0100, Laure= nt Vivier wrote: =20 > > > > > > > > > > > > > > > > > > > > > > > > > > > > [...] > > > > > > > > > > > > > > =20 > > > > > > > > > > > > > > > +/** > > > > > > > > > > > > > > > + * proto_ipv6_header_psum() - Calculates the= partial checksum 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 hea= der > > > > > > > > > > > > > > > + */ > > > > > > > > > > > > > > > +uint32_t proto_ipv6_header_psum(uint16_t pay= load_len, uint8_t protocol, > > > > > > > > > > > > > > > + struct in6_addr saddr, struct in6_addr d= addr) =20 > > > > > > > > > > > > > >=20 > > > > > > > > > > > > > > Hrm, this is passing 2 16-byte IPv6 addresses b= y value, which might > > > > > > > > > > > > > > not be what we want. =20 > > > > > > > > > > > > >=20 > > > > > > > > > > > > > The idea here is to avoid the pointer alignment p= roblem (&ip6h->saddr and > > > > > > > > > > > > > &ip6h->daddr can be misaligned). =20 > > > > > > > > > > > >=20 > > > > > > > > > > > > Ah, right. That's a neat idea, but I'm not sure it= really helps: I > > > > > > > > > > > > think it will just move the misaligned access from = inside the function > > > > > > > > > > > > to the call site, where we try to marshal the param= eter from something > > > > > > > > > > > > unaligned. =20 > > > > > > > > > > >=20 > > > > > > > > > > > I haven't tested this yet, but note that this is gene= rally okay: the > > > > > > > > > > > problem is *dereferencing* an unaligned pointer. But = if you load memory > > > > > > > > > > > from an aligned pointer, and extract a value from thi= s memory, it's all > > > > > > > > > > > fine. =20 > > > > > > > > > >=20 > > > > > > > > > > Right, that's kind of what I'm getting at. Assuming th= is value starts > > > > > > > > > > in an unaligned buffer, then in order to pass this by v= alue the caller > > > > > > > > > > will need to load from that unaligned pointer. AFAIK, = the compiler > > > > > > > > > > will base the type of loads only on the pointed to type= , which isn't > > > > > > > > > > changed whether we dereference in the caller or the cal= lee. > > > > > > > > > > =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 in= to 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 in= to s3 > > > > > > > > > > > lw $4, 0($3) # load word from memory at 1004 + 0 in= to 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 caller > > > > > > > > > > will necessarily induce the compiler to generate this r= ather than the > > > > > > > > > > former. =20 > > > > > > > > >=20 > > > > > > > > > Oh, oops, I didn't realise this was the case (I haven't r= eviewed 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 on= ly, not > > > > > > > > pointers to headers. =20 > > > > > > >=20 > > > > > > > Ok... but my point remains, I'm not seeing that passing the a= ddress by > > > > > > > value actually helps - it just seems to change whether we nee= d to > > > > > > > handle the unaligned load in the caller or the callee. = =20 > > > > > >=20 > > > > > > For UDP and IPv4 (from 6/9): > > > > > >=20 > > > > > > + b->iph.check =3D csum_ip4_header(b->iph.tot_len, IPPROT= O_UDP, > > > > > > + b->iph.saddr, b->iph.dad= dr); > > > > > >=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.paylo= ad_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. = =20 > > > > >=20 > > > > > No... the loads are still from b->ip6h.saddr, b->ip6h.daddr and > > > > > b->ip6h.payload_len. =20 > > > >=20 > > > > It depends how we define "loading from" -- the problem, in general,= is > > > > not the memory location per se, the problem is dereferencing memory > > > > pointers. > > > >=20 > > > > I plan to try an example on MIPS in a bit [...] =20 > > >=20 > > > Actually, armhf first (for clarity): > > >=20 > > > $ cat align.c > > > #include > > > #include > > >=20 > > > struct disarray { > > > uint8_t oops; > > > uint32_t v1; > > > uint32_t v2; > > > } __attribute__((packed, aligned(__alignof__(unsigned int)))); > > >=20 > > > void f1(uint32_t *v1) { > > > *v1 +=3D 42; > > > } > > >=20 > > > uint32_t f2(uint32_t v2) { > > > return v2++; > > > } > > >=20 > > > int main() > > > { > > > struct disarray d =3D { 0x55, 0xaa, 0xaa }; > > >=20 > > > f1(&d.v1); > > > f2(d.v2); > > >=20 > > > fprintf(stdout, "%08x %08x", d.v1, d.v2); > > > } > > >=20 > > > $ arm-linux-gnueabihf-gcc-12 -g -O0 -fno-stack-protector -fomit-frame= -pointer -mno-unaligned-access -o align align.c > > > align.c: In function =E2=80=98main=E2=80=99: > > > align.c:22:8: warning: taking address of packed member of =E2=80=98st= ruct disarray=E2=80=99 may result in an unaligned pointer value [-Waddress-= of-packed-member] > > > 22 | f1(&d.v1); > > > | ^~~~~ > > >=20 > > > $ arm-linux-gnueabihf-objdump -S --disassemble=3Dmain align > > > [...] > > > f1(&d.v1); > > > 562: ab01 add r3, sp, #4 > > > 564: 3301 adds r3, #1 > > > 566: 4618 mov r0, r3 > > > 568: f7ff ffde bl 528 > > > [...] > > >=20 > > > before the call to f1(), the address in r3 is not aligned (we just > > > added #1), despite -mno-unaligned-access. I guess gcc can only warn > > > about that, but not fix it. > > >=20 > > > This: > > > https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html > > >=20 > > > says: > > > -munaligned-access > > > -mno-unaligned-access > > >=20 > > > Enables (or disables) reading and writing of 16- and 32- bit valu= es from addresses that are not 16- or 32- bit aligned. By default unaligned= access is disabled for all pre-ARMv6, all ARMv6-M and for ARMv8-M Baseline= architectures, and enabled for all other architectures. If unaligned acces= s is not enabled then words in packed data structures are accessed a byte a= t a time.=20 > > >=20 > > > Implying, I guess, that on those architectures unaligned accesses > > > shouldn't be done. I think Thumb mode also has issues with this, by > > > the way.=20 > > >=20 > > > And in f1() we just have a ldr from that address (passed on r0): > > > void f1(uint32_t *v1) { > > > 528: b082 sub sp, #8 > > > 52a: 9001 str r0, [sp, #4] > > > *v1 +=3D 42; > > > 52c: 9b01 ldr r3, [sp, #4] > > > 52e: 681b ldr r3, [r3, #0] > > > 530: f103 022a add.w r2, r3, #42 @ 0x2a > > >=20 > > > $ arm-linux-gnueabihf-objdump -S --disassemble=3Df1 align > > > [...] > > > *v1 +=3D 42; > > > 52c: 9b01 ldr r3, [sp, #4] > > > 52e: 681b ldr r3, [r3, #0] > > > 530: f103 022a add.w r2, r3, #42 @ 0x2a > > >=20 > > > ...but the call to f2() is fine: we load with offset 8 from the stack > > > pointer, shift word right, load from offset 12, shift word left, OR: > > >=20 > > > $ arm-linux-gnueabihf-objdump -S --disassemble=3Dmain align > > > [...] > > > f2(d.v2); > > > 56c: 9b02 ldr r3, [sp, #8] > > > 56e: 0a1b lsrs r3, r3, #8 > > > 570: f89d 200c ldrb.w r2, [sp, #12] > > > 574: 0612 lsls r2, r2, #24 > > > 576: 4313 orrs r3, r2 > > > 578: 4618 mov r0, r3 > > > 57a: f7ff ffe0 bl 53e > > > [...] =20 > >=20 > > Huh. Ok, so I guess the compiler realises it's doing a load from a > > packed structure and generates the necessary fixup code. I thought it > > would only consider the type of the actually loaded value. >=20 > Well, it can't just do that, because otherwise we couldn't use packed > structures on any architecture that doesn't support unaligned accesses, > right? Hm.. yeah, I guess not, I hadn't thought it through that way. I find it difficult to reason about what will happen with packed structures, since they explicitly break rules which are otherwise pretty much universal. > Once you pass a pointer to an unaligned value, though, the fixup > information is lost, and the compiler models a function as simply taking > a given pointer with a given type: the model doesn't include information > as to where the value is stored. Right. Everything you've said about this now makes sense to me with this realisation. --=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 --rkxQje/6EhL8/JjH Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXqZ90ACgkQzQJF27ox 2Gd6ng//XEGBYNGayUR2DVjBQXAbwhYR1sVLfOiiie5Nxa1Nai1+4gJ2tHBahGTK TlCm2vl+XExeaYHygbIvEc5RyAzOvPglR3vBxTykvPm0hqMWioPREaOlCDH52Ree 8XQpADH01pgCVD0zvWkD2uezvle9mdxEuT6rtHcnk/umNPNdx4ZT88Pask5frwL2 hsM0xhdqsxvRzOesX6BGpq9X5HrCcsP3OxVXrO8KV/RJoUkhWjPpXgS4o/ApULM5 htOY1qWZeDQ2/5sEfYj2OyoxQ8wjE7e6yWLlUVrVS895rkbyp3TiJfLnYX8uhg1q cSdIvfXsWtBzkxw2GQQPN0DclUJ15dIaGAJTLWPegVlMgb3ZCbX2RNYXjCfjPcD4 J45InJsEHLPDz/YkpErutjMQMqRRdV+Pr/keQVNA0D4PzDo5eRT4/nh0DHjPa/kY gvTCSTbqN3NqRf0PCJINpRzG3ghMJlYm2Zhgr9XeTjsYOfe6s+7ErhVU4BHtYZ/v 9vl+DQrFxL2ddX57Vw9RmjwvuVlge4nftIKWV0cJ9oO1tAOkbKpC0nrRFYFMjuLJ MzsYS/yufgwvq1PVFW/DIdXOTjv3U67pggH7QqdidGH8+/DfwTjPLIXHArieAhZw pOMCv9t/biw54RKFw72vfHr0txUhvzo21eAOQrJEcUZ3DNvrSWc= =64TW -----END PGP SIGNATURE----- --rkxQje/6EhL8/JjH--