DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Stephen Hemminger" <stephen@networkplumber.org>,
	"Bruce Richardson" <bruce.richardson@intel.com>
Cc: <dev@dpdk.org>, "Jerin Jacob" <jerinj@marvell.com>,
	"Aman Singh" <aman.deep.singh@intel.com>,
	"Konstantin Ananyev" <konstantin.v.ananyev@yandex.ru>
Subject: RE: [PATCH 5/6] net: add smaller IPv4 cksum function for simple cases
Date: Fri, 18 Oct 2024 02:32:22 +0200	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9F7F5@smartserver.smartshare.dk> (raw)
In-Reply-To: <20241017123454.302a7203@hermes.local>

> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, 17 October 2024 21.35
> 
> On Thu, 17 Oct 2024 20:03:13 +0100
> Bruce Richardson <bruce.richardson@intel.com> wrote:
> 
> > On Thu, Oct 17, 2024 at 07:15:10PM +0200, Morten Brørup wrote:
> > > > +/**
> > > > + * Process the IPv4 checksum of an IPv4 header without any
> extensions.
> > > > + *
> > > > + * The checksum field does NOT have to be set by the caller, the
> field
> > > > + * is skipped by the calculation.
> > > > + *
> > > > + * @param ipv4_hdr
> > > > + *   The pointer to the contiguous IPv4 header.
> > > > + * @return
> > > > + *   The complemented checksum to set in the IP packet.
> > > > + */
> > > > +__rte_experimental
> > > > +static inline uint16_t
> > > > +rte_ipv4_cksum_simple(const struct rte_ipv4_hdr *ipv4_hdr)
> > > > +{
> > > > +	const uint16_t *v16_h;
> > > > +	uint32_t ip_cksum;
> > > > +
> > > > +	/*
> > > > +	 * Compute the sum of successive 16-bit words of the IPv4
> header,
> > > > +	 * skipping the checksum field of the header.
> > > > +	 */
> > > > +	v16_h = (const unaligned_uint16_t *)&ipv4_hdr->version_ihl;
> > > > +	ip_cksum = v16_h[0] + v16_h[1] + v16_h[2] + v16_h[3] +
> > > > +		v16_h[4] + v16_h[6] + v16_h[7] + v16_h[8] + v16_h[9];
> > > > +
> > > > +	/* reduce 32 bit checksum to 16 bits and complement it */
> > > > +	ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
> > > > +	ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
> > > > +	ip_cksum = (~ip_cksum) & 0x0000FFFF;
> > > > +	return (ip_cksum == 0) ? 0xFFFF : (uint16_t) ip_cksum;
> > >
> > > The zero exception does not apply to the checksum stored in the IP
> header, only to the checksum in the UDP header.
> > >
> >
> > I was wondering about that, because I didn't see it mentioned
> anywhere in
> > the RFCs I consulted, but on the other hand all the implementations
> in the
> > code seemed to have the check for zero.

Copy-pasted including the bug from the originating code.

> >
> > > > +}
> > >
> > > Besides that, for the series,
> >
> > So, just to confirm, the zero check at the end of the new
> ip_cksum_simple
> > function should be removed and we always return the computed value
> > directly?

Agree.

> 
> Depends on usage.
>   - if the computed value is zero, then 0xffff should be placed in
>     the IP header.

Not for the IP header, no.
For the UDP header, yes.

The Linux kernel doesn't do it to the IP header:
https://elixir.bootlin.com/linux/v6.12-rc3/source/net/ipv4/ip_output.c#L96
https://elixir.bootlin.com/linux/v6.12-rc3/source/lib/checksum.c#L108

>   - often code use ip checksum code to see if incoming checksum is
> good.
>     in that case zero means the checksum is valid.

I don't remember all the details, but when checking the checksum, there's something about adding 0xffff or 0x0000 yields the same result. E.g.

1 + 0xffff = 0x10000, and then 0x1 + 0x0000 = 0x0001.
1 + 0x0000 = 0x0001.

So, when checking, it doesn't matter if the header checksum field is 0xffff or 0x0000.

There are other rules for the UDP header's checksum field, because 0x0000 there has the special meaning that the UDP checksum is not present/valid. (And AFAIR, not for IPv6, because the checksum is not optional for IPv6 UDP packets.)

The above calculation is also the reason why it is safe to substitute 0x0000 by 0xffff in the UDP header; it will still yield the same result when checking the checksum.


  reply	other threads:[~2024-10-18  0:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 14:22 [PATCH 0/6] Reduce scope address-of-packed-member warning Bruce Richardson
2024-10-17 14:22 ` [PATCH 1/6] ip_frag: remove use of unaligned variable Bruce Richardson
2024-10-17 16:26   ` Stephen Hemminger
2024-10-17 16:42   ` Konstantin Ananyev
2024-10-17 14:22 ` [PATCH 2/6] efd: remove unnecessary packed attributes Bruce Richardson
2024-10-17 16:22   ` Stephen Hemminger
2024-10-17 14:22 ` [PATCH 3/6] bus/ifpga: remove packed attribute Bruce Richardson
2024-10-17 14:52   ` Xu, Rosen
2024-10-17 16:25   ` Stephen Hemminger
2024-10-17 14:22 ` [PATCH 4/6] pipeline: " Bruce Richardson
2024-10-17 14:24   ` Bruce Richardson
2024-10-17 16:25   ` Stephen Hemminger
2024-10-17 14:22 ` [PATCH 5/6] net: add smaller IPv4 cksum function for simple cases Bruce Richardson
2024-10-17 16:24   ` Stephen Hemminger
2024-10-17 17:01     ` Bruce Richardson
2024-10-17 17:15   ` Morten Brørup
2024-10-17 19:03     ` Bruce Richardson
2024-10-17 19:34       ` Stephen Hemminger
2024-10-18  0:32         ` Morten Brørup [this message]
2024-10-17 14:22 ` [PATCH 6/6] build: limit scope of packed member warning disabling Bruce Richardson
2024-10-17 16:21 ` [PATCH 0/6] Reduce scope address-of-packed-member warning Stephen Hemminger
2024-10-17 17:02   ` Bruce Richardson

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=98CBD80474FA8B44BF855DF32C47DC35E9F7F5@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=aman.deep.singh@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=jerinj@marvell.com \
    --cc=konstantin.v.ananyev@yandex.ru \
    --cc=stephen@networkplumber.org \
    /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.
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 NNTP newsgroup(s).