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.133.124]) by passt.top (Postfix) with ESMTP id D3AE75A0272 for ; Wed, 7 Feb 2024 11:41:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707302503; 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=GTm+ub6BEU3A9IBoW6FU/QeqH7qtVXMNJXKeXMc/k44=; b=AYVgy9UO5Fm76K/kTPONS+bMdzPyF1FpkFR1whNT0NXTG2rGdSoOBBNTCr7xaF98EkKfFP I9rl8oFGvt6UFrdHy91KcvzoY7eg6gXcWnX00GkBXPSHtvbTSdatOTQBOUTJmhTavms0ds 23MpJNWt753fjT/TPZ5Ms6ZA/UHzwsw= 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-58-X34t5IOWPaywxc5TYfIHow-1; Wed, 07 Feb 2024 05:41:42 -0500 X-MC-Unique: X34t5IOWPaywxc5TYfIHow-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a357c92f241so27342166b.0 for ; Wed, 07 Feb 2024 02:41:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707302500; x=1707907300; 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=GTm+ub6BEU3A9IBoW6FU/QeqH7qtVXMNJXKeXMc/k44=; b=Cqkmx+UMGrLeGGprgorLwTLqr2/OWoYieQkIzE3qdkTm1EMBAFC5xPjXDjHXqK5Hxs PVkmQ//rQmHxYnHbvHgzP+kCo8ewCPhZ9W1qtpe7NnC8KFLPw9DpEZ0qNWZhlxNE3/sh dhJikMOGIkLBL8kvsE9YRbKXGPZJ407XyxuKy1YAexwt1u4orTjKA1Y895yxrQDb/23p u0ZNruFWOAk3RXH543LOEa0GutbuJA0y9Yu+dLuz42Q+1X4267+M4s4jKXvDr/tZJGhq wDiqLPtIaourUDVaK2BLd11sPYlHirKmctWM43MMyz3Rzl/IXxGtlEeQn907rt9TNUPg Xufg== X-Gm-Message-State: AOJu0YwVfziPzgn0/EL0RoRh8Mk9/67sEc5Ns6h+k+Recuj5+t1ZTv7g mRuX5AsFt4bCZ7AT/wLtZsIvSUWGtRKGhbaBS6/4WbjeeEMeAEYx132bvT7dd7yv6bvklwQcepN r+5zf0SihfwBLstRatgG2A5USNLWwW9YNX8XuPITI01LhmWpZUsqwW+Rn73sqtrEWQsZqthE/ek 5pKvIYnDrTf1J3/WcQeosfmLQhYdhyjPYU4K8= X-Received: by 2002:a17:906:32c8:b0:a36:4e61:a2cb with SMTP id k8-20020a17090632c800b00a364e61a2cbmr3053612ejk.39.1707302499746; Wed, 07 Feb 2024 02:41:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IEp/dAYJJbQRTCGoQuKnxnOBPtU0wV69/UdwGXUDP9CX0m2EMlvk8EgZ8XOIll/4GXobfqfYQ== X-Received: by 2002:a17:906:32c8:b0:a36:4e61:a2cb with SMTP id k8-20020a17090632c800b00a364e61a2cbmr3053598ejk.39.1707302499161; Wed, 07 Feb 2024 02:41:39 -0800 (PST) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id d2-20020a1709064c4200b00a38156a92fesm608270ejw.186.2024.02.07.02.41.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Feb 2024 02:41:38 -0800 (PST) Date: Wed, 7 Feb 2024 11:41:04 +0100 From: Stefano Brivio To: Laurent Vivier Subject: Re: [PATCH 07/24] ip: introduce functions to compute the header part checksum for TCP/UDP Message-ID: <20240207114104.74b23d72@elisabeth> In-Reply-To: <20240202141151.3762941-8-lvivier@redhat.com> References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-8-lvivier@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.1.1 (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: 2C4BBIJGJZAUXBBQXPS7Q33CFHBXAUC6 X-Message-ID-Hash: 2C4BBIJGJZAUXBBQXPS7Q33CFHBXAUC6 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: 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 Fri, 2 Feb 2024 15:11:34 +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_checksum() and > proto_ipv6_header_checksum(), to compute the checksum of the IP > header part. > > Signed-off-by: Laurent Vivier > --- > ip.h | 24 ++++++++++++++++++++++++ > tcp.c | 40 +++++++++++++++------------------------- > udp.c | 6 ++---- > 3 files changed, 41 insertions(+), 29 deletions(-) > > diff --git a/ip.h b/ip.h > index ff7902c45a95..87cb8dd21d2e 100644 > --- a/ip.h > +++ b/ip.h > @@ -97,4 +97,28 @@ static inline uint16_t ipv4_hdr_checksum(struct iphdr *iph, int proto) > > return ~csum_fold(sum); > } > + > +static inline uint32_t proto_ipv4_header_checksum(struct iphdr *iph, int proto) Function comments, proto can be uint8_t for consistency, and probably all these don't need to be inlines. Interesting readings in case you haven't come across them: https://lwn.net/Kernel/Index/#Inline_functions https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=6d280f4d760e3bcb4a8df302afebf085b65ec982#n962 > +{ > + uint32_t sum = htons(proto); > + > + sum += (iph->saddr >> 16) & 0xffff; > + sum += iph->saddr & 0xffff; > + sum += (iph->daddr >> 16) & 0xffff; > + sum += iph->daddr & 0xffff; > + sum += htons(ntohs(iph->tot_len) - 20); > + > + return sum; > +} > + > +static inline uint32_t proto_ipv6_header_checksum(struct ipv6hdr *ip6h, > + int proto) > +{ > + uint32_t sum = htons(proto) + ip6h->payload_len; > + > + sum += sum_16b(&ip6h->saddr, sizeof(ip6h->saddr)); > + sum += sum_16b(&ip6h->daddr, sizeof(ip6h->daddr)); > + > + return sum; > +} > #endif /* IP_H */ > diff --git a/tcp.c b/tcp.c > index 293ab12d8c21..2fd6bc2eda53 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -938,39 +938,25 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s) > * tcp_update_check_tcp4() - Update TCP checksum from stored one > * @buf: L2 packet buffer with final IPv4 header > */ > -static void tcp_update_check_tcp4(struct tcp4_l2_buf_t *buf) > +static uint16_t tcp_update_check_tcp4(struct iphdr *iph) > { > - uint16_t tlen = ntohs(buf->iph.tot_len) - 20; > - uint32_t sum = htons(IPPROTO_TCP); > + struct tcphdr *th = (void *)(iph + 1); > + uint16_t tlen = ntohs(iph->tot_len) - 20; > + uint32_t sum = proto_ipv4_header_checksum(iph, IPPROTO_TCP); > > - sum += (buf->iph.saddr >> 16) & 0xffff; > - sum += buf->iph.saddr & 0xffff; > - sum += (buf->iph.daddr >> 16) & 0xffff; > - sum += buf->iph.daddr & 0xffff; > - sum += htons(ntohs(buf->iph.tot_len) - 20); > - > - buf->th.check = 0; > - buf->th.check = csum(&buf->th, tlen, sum); > + return csum(th, tlen, sum); > } > > /** > * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6 > * @buf: L2 packet buffer with final IPv6 header > */ > -static void tcp_update_check_tcp6(struct tcp6_l2_buf_t *buf) > +static uint16_t tcp_update_check_tcp6(struct ipv6hdr *ip6h) > { > - int len = ntohs(buf->ip6h.payload_len) + sizeof(struct ipv6hdr); > - > - buf->ip6h.hop_limit = IPPROTO_TCP; > - buf->ip6h.version = 0; > - buf->ip6h.nexthdr = 0; > + struct tcphdr *th = (void *)(ip6h + 1); > + uint32_t sum = proto_ipv6_header_checksum(ip6h, IPPROTO_TCP); > > - buf->th.check = 0; > - buf->th.check = csum(&buf->ip6h, len, 0); > - > - buf->ip6h.hop_limit = 255; > - buf->ip6h.version = 6; > - buf->ip6h.nexthdr = IPPROTO_TCP; > + return csum(th, ntohs(ip6h->payload_len), sum); > } > > /** > @@ -1381,7 +1367,7 @@ do { \ > > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > > - tcp_update_check_tcp4(b); > + b->th.check = tcp_update_check_tcp4(&b->iph); > > tlen = tap_iov_len(c, &b->taph, ip_len); > } else { > @@ -1400,7 +1386,11 @@ do { \ > > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > > - tcp_update_check_tcp6(b); > + b->th.check = tcp_update_check_tcp6(&b->ip6h); > + > + b->ip6h.hop_limit = 255; > + b->ip6h.version = 6; > + b->ip6h.nexthdr = IPPROTO_TCP; > > b->ip6h.flow_lbl[0] = (conn->sock >> 16) & 0xf; > b->ip6h.flow_lbl[1] = (conn->sock >> 8) & 0xff; > diff --git a/udp.c b/udp.c > index 6f867df81c05..96b4e6ca9a85 100644 > --- a/udp.c > +++ b/udp.c > @@ -669,10 +669,8 @@ static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstport, > b->uh.source = b->s_in6.sin6_port; > b->uh.dest = htons(dstport); > b->uh.len = b->ip6h.payload_len; > - > - b->ip6h.hop_limit = IPPROTO_UDP; > - b->ip6h.version = b->ip6h.nexthdr = b->uh.check = 0; > - b->uh.check = csum(&b->ip6h, ip_len, 0); > + b->uh.check = csum(&b->uh, ntohs(b->ip6h.payload_len), > + proto_ipv6_header_checksum(&b->ip6h, IPPROTO_UDP)); > b->ip6h.version = 6; > b->ip6h.nexthdr = IPPROTO_UDP; > b->ip6h.hop_limit = 255; -- Stefano