From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id B8F935A0271 for ; Mon, 4 Mar 2024 12:01:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709550080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uWbNCU5hng6VAN8M2zMpsSPJvmkdrihJsUaGXXnkia4=; b=MF2UFMggdKhRY9tRvN6vFvBr4uGtq74t5G/4+r1sGpbyREe9AqNrAXDqENQcd46ZGO/NkH 82nhFQERip7fcjZ0DpjV37EsoKXwzbZCp35Z3bVIL1P7SkSadzdMqbOLNjX63Ap3QBa5hy Bdq9GAnNCg3jp0MSYFvymjiHhDVMRa8= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-bq52BjdKP6Sm3Wro32gSTg-1; Mon, 04 Mar 2024 06:01:19 -0500 X-MC-Unique: bq52BjdKP6Sm3Wro32gSTg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a449be9db59so173034166b.1 for ; Mon, 04 Mar 2024 03:01:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709550078; x=1710154878; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=uWbNCU5hng6VAN8M2zMpsSPJvmkdrihJsUaGXXnkia4=; b=e5G9f2pZIBdyO5D9rmoSk0CIWoaPPscRSxEvglRuquDhdl64E+kuCJIwbyVkQbu/gv vhW3UgAhhZbQtU8K4MnD66yKJJgn6/eXn9XHgsn7JKd6EUp9TK2+28NoITE1J3g9iEUN bDcddHZ71wn/6lZtOmiWZxTVTm9OM9TpoQNqcXjrqNLHgqPYX/9WXQYWMFnwyMDZTmdG vYMMv4zSGax0j25evjCetYzkxeKkiJAbFoj/HOfNbaPXCJPP1PArjpMvF2c5JG/f6Gpl JesZssTGFRpBzl6ErAhkl6vAHMj6BrhJk5QldkAUccdJyyKVWxNsvu+OLvtwHDZcqF/t rtsw== X-Forwarded-Encrypted: i=1; AJvYcCU9ve9kcZxLgqlF/0VsoeRdPLdkTVsAiOvqLPwVXiHUwjeDdv1ZHcilXi+Zu2tZNgzhmtflzJfvvpOm24ls1/k2sPch X-Gm-Message-State: AOJu0YzVIUgA0nH1PJiK/VE7HXo1x3XxCzAgvyxgROy5SA/guBrINl/s LD7Cpmej3UHpYRQAPR6VgZ6QYRkBBKryhqHWcCWmoW/9kV9uJLFIURS/8OkxhdPs/rlym5otIFy lXZ0WjQtVTH+BrrJFSiV8IgXdjnF1MDtVlhCOctwnq54fmFVxzw== X-Received: by 2002:a17:906:1959:b0:a3e:93d0:3443 with SMTP id b25-20020a170906195900b00a3e93d03443mr5347289eje.34.1709550077637; Mon, 04 Mar 2024 03:01:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEr8u804PvUm7Pgmrafzi0ULpLge7VkEiHqFD5gpmBRdlVig7eC36gVwBzaZE0t3b8RDimMTQ== X-Received: by 2002:a17:906:1959:b0:a3e:93d0:3443 with SMTP id b25-20020a170906195900b00a3e93d03443mr5347263eje.34.1709550077118; Mon, 04 Mar 2024 03:01:17 -0800 (PST) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id o23-20020a17090611d700b00a457f85ad0csm216531eja.137.2024.03.04.03.01.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Mar 2024 03:01:16 -0800 (PST) Date: Mon, 4 Mar 2024 12:00:40 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH v3 7/9] checksum: introduce functions to compute the header part checksum for TCP/UDP Message-ID: <20240304120040.1cebc230@elisabeth> In-Reply-To: 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> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.36; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 4INMMMXJ2VEZYJ5POZ2TCU7NAW75UUF3 X-Message-ID-Hash: 4INMMMXJ2VEZYJ5POZ2TCU7NAW75UUF3 X-MailFrom: sbrivio@redhat.com 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: On Mon, 4 Mar 2024 12:54:12 +1100 David Gibson wrote: > 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: > > > > > 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: > > > > > > > > > On Thu, 29 Feb 2024 19:49:09 +1100 > > > > > David Gibson wrote: > > > > > > > > > > > On Thu, Feb 29, 2024 at 08:05:09AM +0100, Stefano Brivio wrote: > > > > > > > On Thu, 29 Feb 2024 11:38:53 +1100 > > > > > > > David Gibson wrote: > > > > > > > > > > > > > > > On Wed, Feb 28, 2024 at 02:26:18PM +0100, Laurent Vivier wrote: > > > > > > > > > On 2/19/24 04:08, David Gibson wrote: > > > > > > > > > > On Sat, Feb 17, 2024 at 04:07:23PM +0100, Laurent Vivier wrote: > > > > > > > > > > > > > > > > > > > > [...] > > > > > > > > > > > > > > > > > > > > > +/** > > > > > > > > > > > + * 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 header > > > > > > > > > > > + */ > > > > > > > > > > > +uint32_t proto_ipv6_header_psum(uint16_t payload_len, uint8_t protocol, > > > > > > > > > > > + struct in6_addr saddr, struct in6_addr daddr) > > > > > > > > > > > > > > > > > > > > Hrm, this is passing 2 16-byte IPv6 addresses by value, which might > > > > > > > > > > not be what we want. > > > > > > > > > > > > > > > > > > The idea here is to avoid the pointer alignment problem (&ip6h->saddr and > > > > > > > > > &ip6h->daddr can be misaligned). > > > > > > > > > > > > > > > > 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 parameter from something > > > > > > > > unaligned. > > > > > > > > > > > > > > I haven't tested this yet, but note that this is generally okay: the > > > > > > > problem is *dereferencing* an unaligned pointer. But if you load memory > > > > > > > from an aligned pointer, and extract a value from this memory, it's all > > > > > > > fine. > > > > > > > > > > > > Right, that's kind of what I'm getting at. Assuming this value starts > > > > > > in an unaligned buffer, then in order to pass this by value 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 callee. > > > > > > > > > > > > > > > > > > > > Speaking MIPS, this is not safe on all CPU models: > > > > > > > > > > > > > > la $1, 1002 # s1 now contains the value 1002 > > > > > > > lw $2, 0($1) # load word from memory at 1002 + 0 into s2 > > > > > > > > > > > > > > but this is: > > > > > > > > > > > > > > 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 > > > > > > > > > > > > Right, but I don't think merely moving the dereference to the caller > > > > > > will necessarily induce the compiler to generate this rather than the > > > > > > former. > > > > > > > > > > Oh, oops, I didn't realise this was the case (I haven't reviewed the > > > > > patch yet). > > > > > > > > ...no, that's not the case. Dereferencing 'iph' from > > > > struct tcp[46]_l2_buf_t is fine: > > > > > > > > struct tcp4_l2_buf_t { > > > > uint8_t pad[2]; /* 0 2 */ > > > > struct tap_hdr taph; /* 2 18 */ > > > > struct iphdr iph; /* 20 20 */ > > > > [...] > > > > } __attribute__((__packed__)); > > > > > > > > struct tcp6_l2_buf_t { > > > > uint8_t pad[2]; /* 0 2 */ > > > > struct tap_hdr taph; /* 2 18 */ > > > > struct ipv6hdr ip6h; /* 20 40 */ > > > > [...] > > > > } __attribute__((__packed__)); > > > > > > > > The problematic structures are the UDP buffers: > > > > > > > > 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))); > > > > > > > > and for UDP, this patch is dereferencing buffer pointers only, not > > > > pointers to headers. > > > > > > 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. > > > > For UDP and IPv4 (from 6/9): > > > > + b->iph.check = csum_ip4_header(b->iph.tot_len, IPPROTO_UDP, > > + b->iph.saddr, b->iph.daddr); > > > > and for IPv6 (this patch): > > > > + b->uh.check = csum(&b->uh, ntohs(b->ip6h.payload_len), > > + proto_ipv6_header_psum(b->ip6h.payload_len, > > + IPPROTO_UDP, > > + b->ip6h.saddr, > > + b->ip6h.daddr)); > > > > 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. It depends how we define "loading from" -- the problem, in general, is not the memory location per se, the problem is dereferencing memory pointers. I plan to try an example on MIPS in a bit, but meanwhile, this is what I mean: lw $2, 0($1) and $1 needs to be aligned. Then, the compiler needs to know if this: lw $2, 333($1) is fine, or if there needs to be a load from another address. However, we need to give the chance to the compiler to use an aligned pointer (that is, 'b', not '&b->iph'). -- Stefano