From patchwork Tue Apr 27 13:57:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 92257 X-Patchwork-Delegate: andrew.rybchenko@oktetlabs.ru Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id E3279A0A02; Tue, 27 Apr 2021 15:58:29 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0C39D41254; Tue, 27 Apr 2021 15:58:26 +0200 (CEST) Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) by mails.dpdk.org (Postfix) with ESMTP id ECB4B406A3 for ; Tue, 27 Apr 2021 15:58:23 +0200 (CEST) Received: by mail-wr1-f50.google.com with SMTP id j5so58621238wrn.4 for ; Tue, 27 Apr 2021 06:58:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WdnoC2V6KCWhpBILoO1Y+5wep/dMmiSUiNQ7fW6rD2k=; b=hjtcN+WPTQg1brME+hk+RkEpCtZK+bAqd6wpCqWkx0DB7gW25m42+IwuhuzrFNrD+h nHRgltUlt0DvbrWcrSME+Gj+Pj1SA9E9MQ//k0W1crKkVHB/jH9eFEzMGxL0mlQgsyFf SwsfQhXUTQQfWUf87cXsymWtubvEsZPXA/eO++8aLFY0GehhoPnB5P5ZbOCGizMPEAkB 9/F1U1PYvQ5FBGZwDvFlOX5nVGxahVPjeGoXjqfTDKKp64ldMvBgvMghGQjCKNDPeK5d SlT+wemq+lypfD8BdGaGPYPlYqFT7g/9Z8R1uijEkUb+3tmwk9Nl1eq25oJyp0Iea61N dFVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WdnoC2V6KCWhpBILoO1Y+5wep/dMmiSUiNQ7fW6rD2k=; b=gXzsPNa9gTqGvMzgyKuiNrkUuKBkARyAKGEqDr2q+y5iLs/4sf/OeCiZGxtmJGyJFn s9jRRUP/8/QlpGwJKSV3dv/1kbnTN3jInLKeANimIHs3NyErR4hKOw7lfrjfrwzSeNL3 B3yOoZas82Yrhbq6p1y6TXf+oH2AB3nznmeLPx4A/WQvoPt3uUjAfbXzgGIXx0jmfzL7 zjSvjGYZ6dDnZAYAESIN4Gj3d+Nb+e/Viorvk5vA9pkNRmsq2lsx9IY6jpROW9ZfiIiW EdJH+d84P9eQD1Envi/+MoWGeosrwKt9DqeZZOnhJYPAX+vKMwofyol5JQLYFi8pD8ds PgXg== X-Gm-Message-State: AOAM531yiYLW3mvkiSoiCoicLio9ioUCZIXj5ccdnlrUXj2m+cV+5DRy 5L1pUxd36diYMRX2uJOiRg1P8IqKWrCoDw== X-Google-Smtp-Source: ABdhPJyDiNvOSeRQiY3lpA2f5rrPnQ4x8g1GriWgTxIx+ouCRuln7Xy0oU/LGg8p2aSxZnwiPcxXNg== X-Received: by 2002:a5d:43c1:: with SMTP id v1mr10974663wrr.248.1619531903771; Tue, 27 Apr 2021 06:58:23 -0700 (PDT) Received: from gojira.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h9sm21072213wmb.35.2021.04.27.06.58.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 06:58:23 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Cc: Keith Wiles , Hongzhi Guo , =?utf-8?q?Morten_Br=C3=B8rup?= , Thomas Monjalon , stable@dpdk.org Date: Tue, 27 Apr 2021 15:57:52 +0200 Message-Id: <20210427135755.927-2-olivier.matz@6wind.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210427135755.927-1-olivier.matz@6wind.com> References: <20210427135755.927-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" When packet type is IPV4_EXT, the checksum is always marked as good in the mbuf offload flags. Since we know the header lengths, we can easily call rte_ipv4_udptcp_cksum() in this case too. Fixes: 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support") Cc: stable@dpdk.org Signed-off-by: Olivier Matz --- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c index 68baa18523..e7b185a4b5 100644 --- a/drivers/net/tap/rte_eth_tap.c +++ b/drivers/net/tap/rte_eth_tap.c @@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) /* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs > 1) return; - if (l3 == RTE_PTYPE_L3_IPV4) { + if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) { if (l4 == RTE_PTYPE_L4_UDP) { udp_hdr = (struct rte_udp_hdr *)l4_hdr; if (udp_hdr->dgram_cksum == 0) { @@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); - } else if (l3 == RTE_PTYPE_L3_IPV6) { + } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ? From patchwork Tue Apr 27 13:57:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 92258 X-Patchwork-Delegate: andrew.rybchenko@oktetlabs.ru Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 03840A0A02; Tue, 27 Apr 2021 15:58:38 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6C6D641266; Tue, 27 Apr 2021 15:58:27 +0200 (CEST) Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by mails.dpdk.org (Postfix) with ESMTP id 9159D41243 for ; Tue, 27 Apr 2021 15:58:24 +0200 (CEST) Received: by mail-wm1-f48.google.com with SMTP id i129so4926664wma.3 for ; Tue, 27 Apr 2021 06:58:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yWA5ktoUNnaZAuLRrsuiTyEQoYhVA/XUN6xF47YxwOY=; b=Rh84MnLqJWTCgM3WaftKrOxPzKMDEfYOEkd4wC98JfmVfZLpJ+CQqvRjAVxVn9pXnx kZijZRGy+xxhf/uFckqwa+lCwD7FG6KSkF4n48xb4+Ncw6pbvHrIP9n58T9DMbwxZqzL glHNJEZPgrOBxr3NlSm0XKLjn/9yPIiQokemXZ1Mlp5KUz/WHp0XVX7ekWsZ2nEKMcme gw5HZKOuReFlu2Rep3PjWI0wPzIlAyFIDl4AMPbdbqeIhjpTPgGCYXsMXjIIbDhKsEU0 fHryJWk6ihJcYi30EX6nTCXIwSk9jArtB4rvf9KGuKAFm0UaSZ+CAqMcfFjx78Ndy5Ly p50g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yWA5ktoUNnaZAuLRrsuiTyEQoYhVA/XUN6xF47YxwOY=; b=h6a9a9jzKgSAxFMGtHCPsOcbtMvg65CoWNfHe2evXfpED8+h26oYPBXqtYsmFZ8Z5P BdIokWL5pu/kyffYbwkDRAYi8q8nEIdVbCx6cFUfIDUFoMDUt/b8LsUOPeY4ZlvQD944 QmG7IotFMrh0Tf6YT9RF4WqXde5Jjwzq+m7TWTvKcQfqd4kOqevWc8O9F8zIDMG2Z+Oc hDUfwTJLmYkyqESNiKiOoqSiAMqHQSf4PXQ7ZoqCdbLKp2YS/M8ht5W/yp94fumeBEG/ 6zOfjijb6CEdzxYHW7aTfE2Rw9rTYG5nwGldNShbiw50PTZQlSdYbscxTigdriTZVqUF Hs6w== X-Gm-Message-State: AOAM533QseAFvC/LQEz610jUHQuxEyIhm5LWDIXB51y7AHJUz6ceQSal m37zAmV0v8dN+tZ/yLnTa4Q4R0p0nJnblA== X-Google-Smtp-Source: ABdhPJym2bUtTpsMOpXoANZVDpa/50qFMGr40yfqg43kcyxTz8m6rnZBTfmbSRlCKeluepGG6SJewQ== X-Received: by 2002:a05:600c:48a8:: with SMTP id j40mr7375054wmp.114.1619531904383; Tue, 27 Apr 2021 06:58:24 -0700 (PDT) Received: from gojira.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h9sm21072213wmb.35.2021.04.27.06.58.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 06:58:24 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Cc: Keith Wiles , Hongzhi Guo , =?utf-8?q?Morten_Br=C3=B8rup?= , Thomas Monjalon , stable@dpdk.org Date: Tue, 27 Apr 2021 15:57:53 +0200 Message-Id: <20210427135755.927-3-olivier.matz@6wind.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210427135755.927-1-olivier.matz@6wind.com> References: <20210427135755.927-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH 2/4] net/tap: fix Rx cksum flags on TCP packets X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Since commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() or rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum. This new behavior broke the checksum verification in tap driver for TCP packets: these packets are marked with PKT_RX_L4_CKSUM_BAD. Fix this by checking the 2 possible values. A next commit will introduce a checksum verification helper to simplify this a bit. Fixes: d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0") Cc: stable@dpdk.org Signed-off-by: Olivier Matz Acked-by: Andrew Rybchenko --- drivers/net/tap/rte_eth_tap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c index e7b185a4b5..71282e8065 100644 --- a/drivers/net/tap/rte_eth_tap.c +++ b/drivers/net/tap/rte_eth_tap.c @@ -346,6 +346,8 @@ tap_verify_csum(struct rte_mbuf *mbuf) return; } if (l4 == RTE_PTYPE_L4_UDP || l4 == RTE_PTYPE_L4_TCP) { + int cksum_ok; + l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len); /* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs > 1) @@ -363,13 +365,13 @@ tap_verify_csum(struct rte_mbuf *mbuf) return; } } - cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); + cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum = ~rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); + cksum = rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); } - mbuf->ol_flags |= cksum ? - PKT_RX_L4_CKSUM_BAD : - PKT_RX_L4_CKSUM_GOOD; + cksum_ok = (cksum == 0) || (cksum == 0xffff); + mbuf->ol_flags |= cksum_ok ? + PKT_RX_L4_CKSUM_GOOD : PKT_RX_L4_CKSUM_BAD; } } From patchwork Tue Apr 27 13:57:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 92259 X-Patchwork-Delegate: andrew.rybchenko@oktetlabs.ru Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 93E69A0A02; Tue, 27 Apr 2021 15:58:44 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id AF95D4126F; Tue, 27 Apr 2021 15:58:28 +0200 (CEST) Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) by mails.dpdk.org (Postfix) with ESMTP id 342B241254 for ; Tue, 27 Apr 2021 15:58:25 +0200 (CEST) Received: by mail-wr1-f50.google.com with SMTP id a4so59593625wrr.2 for ; Tue, 27 Apr 2021 06:58:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Oa3ka7bDPcxBK5oC6uLX2escOC1pza5afzxHlNpJ7Mw=; b=jhJaDSqxIJ0d0u4tLvTCXeEKxzpv6xbYYUmKl9MpCvsU5XNDwwTaA4EA5T9D6gqrfe rn6usy/fsWdXYCN+X24nrIHfqzdJ8V3R+5A6n1X0nbzNLhk/S3NZ0T1hyrzcsTpycxrc /7UNFYHCSwFyXTZRAmOJ3AsGBPG3b6xB6mE/VUsf4/ofOK9h22Pjnrf2w21IUT85VtZl 1H4dG2DSz7IcpUJc3cZQjuD4AMWG7lSox98tUUffiQlg2eD2oHEwk5aUQw+1PFGsjag9 72qLat/rQWys9FNshpJCVnHtAxfI5oOrOEEFA0/E5iX88oB6JkJ0njkQOUN6+ymVc3lY QVeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Oa3ka7bDPcxBK5oC6uLX2escOC1pza5afzxHlNpJ7Mw=; b=b61eAMHtUOgF9Hn7B4bsI/INle29FwhhPiCSX6qfmnZ4XNizQGWNL9qT+zzw038TpO XYS1b4UEVZTsopMYbYwemCTUC13bIZjz/vaCzfjY9VBYufVyJOqVj4jtgaP3piGr48Bp i6QIu1o4Pyhe9ea4SpdnqdvHnhRq2OADoQmFP0Z2mfMUzydNpeo4vXxEb6EQgzfi93l6 exE6FyXDvC2ZtkGX7KS/+9EjKbDCv13ec3AUf9jWKqWp4H+aZmMjaIekSEE5e5Fum1Ny 05jeKXWnPXrEj1628WexFFwScUOuzgJygZN6D2WlOkDD9mYlK4i7CERsCvdhtMiO/Bq8 3XKQ== X-Gm-Message-State: AOAM532jUWyKirjQ9qSXfdVCqgWybi3yaIFpD/HxGmGVb8byY3omc3PS a6DQdt39nqFyoCECj6MFufnLt6NBVYhgCw== X-Google-Smtp-Source: ABdhPJxY5L5W2BKiSeJ8KOkNqzBwVMpzx5mHN/2BBVPYWo7hdmCzKOtsZDyB3ZrFIWym9NIumwmoGQ== X-Received: by 2002:a5d:6510:: with SMTP id x16mr9589070wru.345.1619531904986; Tue, 27 Apr 2021 06:58:24 -0700 (PDT) Received: from gojira.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h9sm21072213wmb.35.2021.04.27.06.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 06:58:24 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Cc: Keith Wiles , Hongzhi Guo , =?utf-8?q?Morten_Br=C3=B8rup?= , Thomas Monjalon Date: Tue, 27 Apr 2021 15:57:54 +0200 Message-Id: <20210427135755.927-4-olivier.matz@6wind.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210427135755.927-1-olivier.matz@6wind.com> References: <20210427135755.927-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Since commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() and rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum. Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet. Use this new helper in net/tap driver. Signed-off-by: Olivier Matz Acked-by: Morten Brørup Reviewed-by: Morten Brørup Acked-by: Morten Brørup Reviewed-by: Morten Brørup --- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-) diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c index 71282e8065..b14d5a1d55 100644 --- a/drivers/net/tap/rte_eth_tap.c +++ b/drivers/net/tap/rte_eth_tap.c @@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf) return; } } - cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify(l3_hdr, + l4_hdr); } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum = rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify(l3_hdr, + l4_hdr); } - cksum_ok = (cksum == 0) || (cksum == 0xffff); mbuf->ol_flags |= cksum_ok ? PKT_RX_L4_CKSUM_GOOD : PKT_RX_L4_CKSUM_BAD; } diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h index 8c189009b0..ef84bcc5bf 100644 --- a/lib/net/rte_ip.h +++ b/lib/net/rte_ip.h @@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) } /** - * Process the IPv4 UDP or TCP checksum. - * - * The IP and layer 4 checksum must be set to 0 in the packet by - * the caller. - * - * @param ipv4_hdr - * The pointer to the contiguous IPv4 header. - * @param l4_hdr - * The pointer to the beginning of the L4 header. - * @return - * The complemented checksum to set in the IP packet. + * @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) +__rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) { uint32_t cksum; uint32_t l3_len, l4_len; @@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0); cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); - cksum = (~cksum) & 0xffff; + + return (uint16_t)cksum; +} + +/** + * Process the IPv4 UDP or TCP checksum. + * + * The IP and layer 4 checksum must be set to 0 in the packet by + * the caller. + * + * @param ipv4_hdr + * The pointer to the contiguous IPv4 header. + * @param l4_hdr + * The pointer to the beginning of the L4 header. + * @return + * The complemented checksum to set in the IP packet. + */ +static inline uint16_t +rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) +{ + uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr); + + cksum = ~cksum; + /* - * Per RFC 768:If the computed checksum is zero for UDP, + * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP) cksum = 0xffff; - return (uint16_t)cksum; + return cksum; +} + +/** + * Validate the IPv4 UDP or TCP checksum. + * + * @param ipv4_hdr + * The pointer to the contiguous IPv4 header. + * @param l4_hdr + * The pointer to the beginning of the L4 header. + * @return + * Return 0 if the checksum is correct, else -1. + */ +__rte_experimental +static inline int +rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr, + const void *l4_hdr) +{ + uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr); + + if (cksum != 0xffff) + return -1; + + return 0; } /** @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags) return __rte_raw_cksum_reduce(sum); } +/** + * @internal Calculate the non-complemented IPv4 L4 checksum + */ +static inline uint16_t +__rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) +{ + uint32_t cksum; + uint32_t l4_len; + + l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len); + + cksum = rte_raw_cksum(l4_hdr, l4_len); + cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0); + + cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); + + return (uint16_t)cksum; +} + /** * Process the IPv6 UDP or TCP checksum. * @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags) static inline uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) { - uint32_t cksum; - uint32_t l4_len; - - l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len); + uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr); - cksum = rte_raw_cksum(l4_hdr, l4_len); - cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0); + cksum = ~cksum; - cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); - cksum = (~cksum) & 0xffff; /* * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP) cksum = 0xffff; - return (uint16_t)cksum; + return cksum; +} + +/** + * Validate the IPv6 UDP or TCP checksum. + * + * The function accepts a 0 checksum, since it can exceptionally happen. See 8.1 + * (Upper-Layer Checksums) in RFC 8200. + * + * @param ipv6_hdr + * The pointer to the contiguous IPv6 header. + * @param l4_hdr + * The pointer to the beginning of the L4 header. + * @return + * Return 0 if the checksum is correct, else -1. + */ +__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr, + const void *l4_hdr) +{ + uint16_t cksum; + + cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr); + if (cksum != 0xffff) + return -1; + + return 0; } /** IPv6 fragment extension header. */ From patchwork Tue Apr 27 13:57:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 92260 X-Patchwork-Delegate: andrew.rybchenko@oktetlabs.ru Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 78899A0A02; Tue, 27 Apr 2021 15:58:50 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id ECA7041274; Tue, 27 Apr 2021 15:58:29 +0200 (CEST) Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by mails.dpdk.org (Postfix) with ESMTP id D320641254 for ; Tue, 27 Apr 2021 15:58:25 +0200 (CEST) Received: by mail-wm1-f42.google.com with SMTP id a22-20020a05600c2256b0290142870824e9so1117995wmm.0 for ; Tue, 27 Apr 2021 06:58:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+9q+Rz0axJ0UiuBfvKKPwN53KvGUF/JONezKfjojE6Y=; b=HfO0Y8F2PTtlIJK8hAtC3zWzEzMdF2cJYMMglz7zuZfeXdg/Qb53Ki2jrpX66ezbEY AoWS8NeCWnLPzdXQYHnc0d1NB1OAulyAc+yGfCqyG+qvdUBsPtX6lOB+u2tF4qaR9MwS oXNnx5n54FXGtlk0PXMkusp8v332CL+/SLtOAcOxIVrObDD6+0c6777GU2OBFhOzEhqs a3YH06wZTA7euQQEErfUl8Vp+5jAyfPryFlCx011nDqdLl8/ix7j+wuLh7Wmtna2mWRN SEz3DC6176yLhLzYIdUj4FQt+IeSZsgVKPq+g/wELJFj1y0vE0ux8wu8DFG21GvSwCv1 08Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+9q+Rz0axJ0UiuBfvKKPwN53KvGUF/JONezKfjojE6Y=; b=Cvk/BfiK7YuRgpryUO7UMLulq0atwmtYtzwY5UmlvWDEYel2atCmFNXcImQc1nZRie HkfccmOYXTchwDK+G/FqVmUb7+QIiyLM/74AuFetb65om84LimAXwxQ7wCa+MyNUX+kR id651JWFPNInPuHGH0m9344pmQa2QFtcE5gRRxWtpRfCoIzDM1tbM7EZq3OPeSBfvOZB omN9muN0mz3pFODlMsdVqUggciu5X2HRBxQ+Kip91bWqxAQIT5MJWzPzrRIGp9RL55zs nbEcbeeHUs2eP3Tgdwoaor94EgEE3COvvxVmeIpTjS7TPywh7e0Uh/eKMDXAuiwXD8JD Dusg== X-Gm-Message-State: AOAM532YUwFZPj8m/aVN7Zjx7i+pmaoevWPaIv4LV4RAmE4ewgsb7wBg keeQojrRw1gZioax5ko+n4AP9sGsOxBtzw== X-Google-Smtp-Source: ABdhPJz0kPFbw+HGIT3EmE+6wit4iCypa1vvFJlnkWAk3Ernb7vGRheAfnmmLNI8Sle+oQH7Jk6JdA== X-Received: by 2002:a1c:1d91:: with SMTP id d139mr17107029wmd.101.1619531905627; Tue, 27 Apr 2021 06:58:25 -0700 (PDT) Received: from gojira.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h9sm21072213wmb.35.2021.04.27.06.58.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 06:58:25 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Cc: Keith Wiles , Hongzhi Guo , =?utf-8?q?Morten_Br=C3=B8rup?= , Thomas Monjalon Date: Tue, 27 Apr 2021 15:57:55 +0200 Message-Id: <20210427135755.927-5-olivier.matz@6wind.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210427135755.927-1-olivier.matz@6wind.com> References: <20210427135755.927-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH 4/4] test/cksum: new test for L3/L4 checksum API X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add a simple unit test for checksum API. Signed-off-by: Olivier Matz --- MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 app/test/test_cksum.c diff --git a/MAINTAINERS b/MAINTAINERS index 44f3d322ed..9fe7c92eac 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1309,6 +1309,7 @@ Packet processing Network headers M: Olivier Matz F: lib/net/ +F: app/test/test_cksum.c Packet CRC M: Jasvinder Singh diff --git a/app/test/autotest_data.py b/app/test/autotest_data.py index 097638941f..2871ed8994 100644 --- a/app/test/autotest_data.py +++ b/app/test/autotest_data.py @@ -585,6 +585,12 @@ "Func": default_autotest, "Report": None, }, + { + "Name": "Checksum autotest", + "Command": "cksum_autotest", + "Func": default_autotest, + "Report": None, + }, # #Please always keep all dump tests at the end and together! # diff --git a/app/test/meson.build b/app/test/meson.build index 08c82d3d23..28d8a9a111 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -17,6 +17,7 @@ test_sources = files( 'test_bitmap.c', 'test_bpf.c', 'test_byteorder.c', + 'test_cksum.c', 'test_cmdline.c', 'test_cmdline_cirbuf.c', 'test_cmdline_etheraddr.c', @@ -189,6 +190,7 @@ fast_tests = [ ['atomic_autotest', false], ['bitops_autotest', true], ['byteorder_autotest', true], + ['cksum_autotest', true], ['cmdline_autotest', true], ['common_autotest', true], ['cpuflags_autotest', true], diff --git a/app/test/test_cksum.c b/app/test/test_cksum.c new file mode 100644 index 0000000000..cd983d7c01 --- /dev/null +++ b/app/test/test_cksum.c @@ -0,0 +1,271 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2021 6WIND S.A. + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include "test.h" + +#define MEMPOOL_CACHE_SIZE 0 +#define MBUF_DATA_SIZE 256 +#define NB_MBUF 128 + +/* + * Test L3/L4 checksum API. + */ + +#define GOTO_FAIL(str, ...) do { \ + printf("cksum test FAILED (l.%d): <" str ">\n", \ + __LINE__, ##__VA_ARGS__); \ + goto fail; \ + } while (0) + +/* generated in scapy with Ether()/IP()/TCP())) */ +static const char test_cksum_ipv4_tcp[] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, + 0x7c, 0xcd, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, + 0x00, 0x01, 0x00, 0x14, 0x00, 0x50, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, + 0x20, 0x00, 0x91, 0x7c, 0x00, 0x00, + +}; + +/* generated in scapy with Ether()/IPv6()/TCP()) */ +static const char test_cksum_ipv6_tcp[] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x60, 0x00, + 0x00, 0x00, 0x00, 0x14, 0x06, 0x40, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x14, + 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0x8f, 0x7d, + 0x00, 0x00, +}; + +/* generated in scapy with Ether()/IP()/UDP()/Raw('x')) */ +static const char test_cksum_ipv4_udp[] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, + 0x7c, 0xcd, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, + 0x00, 0x01, 0x00, 0x35, 0x00, 0x35, 0x00, 0x09, + 0x89, 0x6f, 0x78, +}; + +/* generated in scapy with Ether()/IPv6()/UDP()/Raw('x')) */ +static const char test_cksum_ipv6_udp[] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x86, 0xdd, 0x60, 0x00, + 0x00, 0x00, 0x00, 0x09, 0x11, 0x40, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x35, + 0x00, 0x35, 0x00, 0x09, 0x87, 0x70, 0x78, +}; + +/* generated in scapy with Ether()/IP(options='\x00')/UDP()/Raw('x')) */ +static const char test_cksum_ipv4_opts_udp[] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x46, 0x00, + 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, + 0x7b, 0xc9, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, + 0x00, 0x35, 0x00, 0x09, 0x89, 0x6f, 0x78, +}; + +/* test l3/l4 checksum api */ +static int +test_l4_cksum(struct rte_mempool *pktmbuf_pool, const char *pktdata, size_t len) +{ + struct rte_net_hdr_lens hdr_lens; + struct rte_mbuf *m = NULL; + uint32_t packet_type; + uint16_t prev_cksum; + void *l3_hdr; + void *l4_hdr; + uint32_t l3; + uint32_t l4; + char *data; + + m = rte_pktmbuf_alloc(pktmbuf_pool); + if (m == NULL) + GOTO_FAIL("Cannot allocate mbuf"); + + data = rte_pktmbuf_append(m, len); + if (data == NULL) + GOTO_FAIL("Cannot append data"); + + memcpy(data, pktdata, len); + + packet_type = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK); + l3 = packet_type & RTE_PTYPE_L3_MASK; + l4 = packet_type & RTE_PTYPE_L4_MASK; + + l3_hdr = rte_pktmbuf_mtod_offset(m, void *, hdr_lens.l2_len); + l4_hdr = rte_pktmbuf_mtod_offset(m, void *, + hdr_lens.l2_len + hdr_lens.l3_len); + + if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) { + struct rte_ipv4_hdr *ip = l3_hdr; + + /* verify IPv4 checksum */ + if (rte_ipv4_cksum(l3_hdr) != 0) + GOTO_FAIL("invalid IPv4 checksum verification"); + + /* verify bad IPv4 checksum */ + ip->hdr_checksum++; + if (rte_ipv4_cksum(l3_hdr) == 0) + GOTO_FAIL("invalid IPv4 bad checksum verification"); + ip->hdr_checksum--; + + /* recalculate IPv4 checksum */ + prev_cksum = ip->hdr_checksum; + ip->hdr_checksum = 0; + ip->hdr_checksum = rte_ipv4_cksum(ip); + if (ip->hdr_checksum != prev_cksum) + GOTO_FAIL("invalid IPv4 checksum calculation"); + + /* verify L4 checksum */ + if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) != 0) + GOTO_FAIL("invalid L4 checksum verification"); + + if (l4 == RTE_PTYPE_L4_TCP) { + struct rte_tcp_hdr *tcp = l4_hdr; + + /* verify bad TCP checksum */ + tcp->cksum++; + if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0) + GOTO_FAIL("invalid bad TCP checksum verification"); + tcp->cksum--; + + /* recalculate TCP checksum */ + prev_cksum = tcp->cksum; + tcp->cksum = 0; + tcp->cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); + if (tcp->cksum != prev_cksum) + GOTO_FAIL("invalid TCP checksum calculation"); + + } else if (l4 == RTE_PTYPE_L4_UDP) { + struct rte_udp_hdr *udp = l4_hdr; + + /* verify bad UDP checksum */ + udp->dgram_cksum++; + if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0) + GOTO_FAIL("invalid bad UDP checksum verification"); + udp->dgram_cksum--; + + /* recalculate UDP checksum */ + prev_cksum = udp->dgram_cksum; + udp->dgram_cksum = 0; + udp->dgram_cksum = rte_ipv4_udptcp_cksum(l3_hdr, + l4_hdr); + if (udp->dgram_cksum != prev_cksum) + GOTO_FAIL("invalid TCP checksum calculation"); + } + } else if (l3 == RTE_PTYPE_L3_IPV6 || l3 == RTE_PTYPE_L3_IPV6_EXT) { + if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) != 0) + GOTO_FAIL("invalid L4 checksum verification"); + + if (l4 == RTE_PTYPE_L4_TCP) { + struct rte_tcp_hdr *tcp = l4_hdr; + + /* verify bad TCP checksum */ + tcp->cksum++; + if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0) + GOTO_FAIL("invalid bad TCP checksum verification"); + tcp->cksum--; + + /* recalculate TCP checksum */ + prev_cksum = tcp->cksum; + tcp->cksum = 0; + tcp->cksum = rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); + if (tcp->cksum != prev_cksum) + GOTO_FAIL("invalid TCP checksum calculation"); + + } else if (l4 == RTE_PTYPE_L4_UDP) { + struct rte_udp_hdr *udp = l4_hdr; + + /* verify bad UDP checksum */ + udp->dgram_cksum++; + if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0) + GOTO_FAIL("invalid bad UDP checksum verification"); + udp->dgram_cksum--; + + /* recalculate UDP checksum */ + prev_cksum = udp->dgram_cksum; + udp->dgram_cksum = 0; + udp->dgram_cksum = rte_ipv6_udptcp_cksum(l3_hdr, + l4_hdr); + if (udp->dgram_cksum != prev_cksum) + GOTO_FAIL("invalid TCP checksum calculation"); + } + } + + rte_pktmbuf_free(m); + + return 0; + +fail: + if (m) + rte_pktmbuf_free(m); + + return -1; +} + +static int +test_cksum(void) +{ + struct rte_mempool *pktmbuf_pool = NULL; + + /* create pktmbuf pool if it does not exist */ + pktmbuf_pool = rte_pktmbuf_pool_create("test_cksum_mbuf_pool", + NB_MBUF, MEMPOOL_CACHE_SIZE, 0, MBUF_DATA_SIZE, + SOCKET_ID_ANY); + + if (pktmbuf_pool == NULL) + GOTO_FAIL("cannot allocate mbuf pool"); + + if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_tcp, + sizeof(test_cksum_ipv4_tcp)) < 0) + GOTO_FAIL("checksum error on ipv4_tcp"); + + if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv6_tcp, + sizeof(test_cksum_ipv6_tcp)) < 0) + GOTO_FAIL("checksum error on ipv6_tcp"); + + if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_udp, + sizeof(test_cksum_ipv4_udp)) < 0) + GOTO_FAIL("checksum error on ipv4_udp"); + + if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv6_udp, + sizeof(test_cksum_ipv6_udp)) < 0) + GOTO_FAIL("checksum error on ipv6_udp"); + + if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_opts_udp, + sizeof(test_cksum_ipv4_opts_udp)) < 0) + GOTO_FAIL("checksum error on ipv4_opts_udp"); + + rte_mempool_free(pktmbuf_pool); + + return 0; + +fail: + rte_mempool_free(pktmbuf_pool); + + return -1; +} +#undef GOTO_FAIL + +REGISTER_TEST_COMMAND(cksum_autotest, test_cksum);