public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v3 7/9] checksum: introduce functions to compute the header part checksum for TCP/UDP
Date: Wed, 28 Feb 2024 14:26:18 +0100	[thread overview]
Message-ID: <04c99072-02ea-46a9-aac6-23116cb05fa1@redhat.com> (raw)
In-Reply-To: <ZdLGJ_fS3uANondm@zatzit>

On 2/19/24 04:08, David Gibson wrote:
> On Sat, Feb 17, 2024 at 04:07:23PM +0100, Laurent Vivier wrote:
>> The TCP and UDP checksums are computed using the data in the TCP/UDP
>> payload but also some informations in the IP header (protocol,
>> length, source and destination addresses).
>>
>> We add two functions, proto_ipv4_header_psum() and
>> proto_ipv6_header_psum(), to compute the checksum of the IP
>> header part.
>>
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>>
>> Notes:
>>      v3:
>>        - function parameters provide tot_len, saddr, daddr and protocol
>>          rather than an iphdr/ipv6hdr
>>      
>>      v2:
>>        - move new function to checksum.c
>>        - use _psum rather than _checksum in the name
>>        - replace csum_udp4() and csum_udp6() by the new function
>>
>>   checksum.c | 67 ++++++++++++++++++++++++++++++++++++++++++------------
>>   checksum.h |  4 ++++
>>   tcp.c      | 44 ++++++++++++++++-------------------
>>   udp.c      | 10 ++++----
>>   4 files changed, 81 insertions(+), 44 deletions(-)
>>
>> diff --git a/checksum.c b/checksum.c
>> index 511b296a9a80..55bf1340a257 100644
>> --- a/checksum.c
>> +++ b/checksum.c
>> @@ -134,6 +134,30 @@ uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol,
>>   	return ~csum_fold(sum);
>>   }
>>   
>> +/**
>> + * proto_ipv4_header_psum() - Calculates the partial checksum of an
>> + * 			      IPv4 header for UDP or TCP
>> + * @tot_len:	Payload length
>> + * @proto:	Protocol number
>> + * @saddr:	Source address
>> + * @daddr:	Destination address
>> + * @proto:	proto Protocol number
>> + * Returns:	Partial checksum of the IPv4 header
>> + */
>> +uint32_t proto_ipv4_header_psum(uint16_t tot_len, uint8_t protocol,
>> +				uint32_t saddr, uint32_t daddr)
>> +{
>> +	uint32_t psum = htons(protocol);
>> +
>> +	psum += (saddr >> 16) & 0xffff;
>> +	psum += saddr & 0xffff;
>> +	psum += (daddr >> 16) & 0xffff;
>> +	psum += daddr & 0xffff;
>> +	psum += htons(ntohs(tot_len) - 20);
>> +
>> +	return psum;
>> +}
>> +
>>   /**
>>    * csum_udp4() - Calculate and set checksum for a UDP over IPv4 packet
>>    * @udp4hr:	UDP header, initialised apart from checksum
>> @@ -150,14 +174,10 @@ void csum_udp4(struct udphdr *udp4hr,
>>   	udp4hr->check = 0;
>>   
>>   	if (UDP4_REAL_CHECKSUMS) {
>> -		/* UNTESTED: if we did want real UDPv4 checksums, this
>> -		 * is roughly what we'd need */
>> -		uint32_t psum = csum_fold(saddr.s_addr)
>> -			+ csum_fold(daddr.s_addr)
>> -			+ htons(len + sizeof(*udp4hr))
>> -			+ htons(IPPROTO_UDP);
>> -		/* Add in partial checksum for the UDP header alone */
>> -		psum += sum_16b(udp4hr, sizeof(*udp4hr));
>> +		uint32_t psum = proto_ipv4_header_psum(len, IPPROTO_UDP,
>> +						       saddr.s_addr,
>> +						       daddr.s_addr);
>> +		psum = csum_unfolded(udp4hr, sizeof(struct udphdr), psum);
>>   		udp4hr->check = csum(payload, len, psum);
>>   	}
>>   }
>> @@ -180,6 +200,26 @@ void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len)
>>   	icmp4hr->checksum = csum(payload, len, psum);
>>   }
>>   
>> +/**
>> + * 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).

Is it a better solution to copy the content of ip6h->saddr and ip6h->daddr to some local 
variables and then provide the pointers of the local variables to proto_ipv6_header_psum()?

> 
>> +{
>> +	uint32_t sum = htons(protocol) + payload_len;
> 
> Uh.. doesn't that need to be htons(payload_len + sizeof(struct
> ipv6hdr)) rather than simply payload_len?
> 

payload_len is:

- b->ip6h.payload_len (from udp_update_hdr6())
- ip6h->payload_len (from tcp_update_check_tcp6())

and in ip6h payload_len is:

- htons(udp6_l2_mh_sock[n].msg_len + sizeof(b->uh)) (from udp_update_hdr6())
- htons(plen + sizeof(struct tcphdr)) (from tcp_fill_ipv6_header())

So this is correct... but

csum_udp6() uses "len" from tap_udp6_send(), so there is a bug here.

but there is also a problem with proto_ipv4_header_psum() that need host endianness and 
tcp_update_check_tcp4() provides network endianness...

The first idea was to use the value from ip6h payload_len as it is already computed.
But mixing network endianness and host endianness appears to be a bad idea...

Thanks,
Laurent


  reply	other threads:[~2024-02-28 13:26 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-17 15:07 [PATCH v3 0/9] Add vhost-user support to passt (part 1) Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 1/9] iov: add some functions to manage iovec Laurent Vivier
2024-02-19  2:45   ` David Gibson
2024-02-17 15:07 ` [PATCH v3 2/9] pcap: add pcap_iov() Laurent Vivier
2024-02-19  2:50   ` David Gibson
2024-02-17 15:07 ` [PATCH v3 3/9] checksum: align buffers Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 4/9] checksum: add csum_iov() Laurent Vivier
2024-02-19  2:52   ` David Gibson
2024-02-17 15:07 ` [PATCH v3 5/9] util: move IP stuff from util.[ch] to ip.[ch] Laurent Vivier
2024-02-19  2:59   ` David Gibson
2024-02-17 15:07 ` [PATCH v3 6/9] checksum: use csum_ip4_header() in udp.c and tcp.c Laurent Vivier
2024-02-19  3:01   ` David Gibson
2024-02-29 16:24   ` Stefano Brivio
2024-02-29 23:10     ` David Gibson
2024-03-01  7:58       ` Stefano Brivio
2024-03-01 12:23         ` David Gibson
2024-03-04 17:04           ` Stefano Brivio
2024-02-17 15:07 ` [PATCH v3 7/9] checksum: introduce functions to compute the header part checksum for TCP/UDP Laurent Vivier
2024-02-19  3:08   ` David Gibson
2024-02-28 13:26     ` Laurent Vivier [this message]
2024-02-29  0:38       ` David Gibson
2024-02-29  7:05         ` Stefano Brivio
2024-02-29  8:49           ` David Gibson
2024-02-29  8:56             ` Stefano Brivio
2024-02-29 14:15               ` Stefano Brivio
2024-02-29 23:09                 ` David Gibson
2024-03-01  6:56                   ` Stefano Brivio
2024-03-04  1:54                     ` David Gibson
2024-03-04 11:00                       ` Stefano Brivio
2024-03-04 22:47                         ` Stefano Brivio
2024-03-06  5:09                           ` David Gibson
2024-03-08  0:08                             ` Stefano Brivio
2024-03-08  1:20                               ` David Gibson
2024-02-17 15:07 ` [PATCH v3 8/9] tap: make tap_update_mac() generic Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 9/9] tcp: Introduce ipv4_fill_headers()/ipv6_fill_headers() Laurent Vivier
2024-02-19  3:14   ` David Gibson
2024-02-29 16:29   ` Stefano Brivio
2024-02-29 16:31 ` [PATCH v3 0/9] Add vhost-user support to passt (part 1) 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=04c99072-02ea-46a9-aac6-23116cb05fa1@redhat.com \
    --to=lvivier@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /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).