From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20964606A325 for ; Wed, 2 Dec 2026 17:20:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=XPpCU9lE Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CED8540685; Wed, 2 Dec 2026 18:19:34 +0100 (CET) Received: from mail-dl1-f54.google.com (mail-dl1-f54.google.com [74.125.82.54]) by mails.dpdk.org (Postfix) with ESMTPS id 7B7E6402E1 for ; Wed, 2 Dec 2026 18:19:32 +0100 (CET) Received: by mail-dl1-f54.google.com with SMTP id a92af1059eb24-1b546061d58so6329967c88.1 for ; Wed, 02 Dec 2026 09:19:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796231971; x=1796836771; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+HOuOZDruqo0xAD29/4ZEVyj1ZIut2TkfRftJJBQtq4=; b=XPpCU9lEcWS+nbBv6Ri8TW6h0uhl5s9nIFyi/ofvcCKB3B0WaYniO2VVYoYQs0lRWG gpRi/f0DhoY6/nNVpTNfnh6LNQJoEitsb6D5/fUo09BFxNd69b6l1R1Ged9eDwaFE7cz +g2BG05hx6LquLb0NpkY0RHH/pUqt3IYrQ/pIyVsOoDywCz5pxrSdp3i1HEDkC1IZzgR ofIHFFRkTUan6SK2dkkl5Wz5CBGIx7L0+0t001km6tpNa7Y/bBny6B/7TqiHzEaQeaIt CvbcQKRDlpXg9hj7ADidMw4onRUVxFPXQM2YiPSVFcE55xZSslPuxcNzyq49TCT7EFJ7 /wSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796231971; x=1796836771; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+HOuOZDruqo0xAD29/4ZEVyj1ZIut2TkfRftJJBQtq4=; b=q1QaQcnEU3rYBwb0DLEnH1PuaZeIFpM0DQbOQc4zeppi0sasNX91c1fHM3mbqTYGKh 3hbzFT9AyaDZd6uFGW9AJS47XB7U4DYt/PHg4aNx9Ci59sxDeDZbUIFf6A89H85+ssUe Lqbg4fCoK3h27eZ5xZtkB6f5fr+nYNSSt4MM6aDIVgB+vfx9OjsdNkiQtmn4vU457MCr tlSDctinj2SIKMb37jXnII43prtkKU4r31xkdKSWGykr9uLIMT1HDhPoId4rNfH/rRc1 KZmzZ6uobvoW6lpkf2teBInqA2VHOwYny4fZ1IMK04Dy++3j513GwaqCWYtfFMqS6C2G LomA== X-Gm-Message-State: APuU4Z/aqQAG5bj6KqPE840xjjV04toxPJD97Ge3+IXMCrdKWcsWPXeR 6I+np7YcBzx2/TdUrQNUtL73YzVjgYFjqcKeZjlH6YpOjD3zQlvBv4pcIEQmMQ== X-Gm-Gg: AZFdTZdJajRNbgGmA/8EkVQ/OrI5vuI/kV/Y17Y7C7Ca66vj5sC5RkV2lBNvXg9DhzV ipo2BrmU1qSnumyuxIbHUSJx89kb1+XYL9XyjLnX+WHRyPEWs01rtuTtqsqLPmu+z+mvbCag52X 6GAv/VmBVi42e3iuW0tXS7N+JcoDcDaylr9YHBDGErO/bp4J5umgeSkTldFeJWGCHG7EmHvsUle j4ChmejUVyFCY6BivlfP2H0VQ/PlwyvLXIxVVYDr23NbxUD55kQ+4L4hoOtlHU6Qw/ngwSH1V/+ ApiJ3XyC4RCNnD4NZd50v+o3FChLsekHrc6jZ643sK4R+HNOTo2rLWgzokut5RhTXSwixMfV6MO Q+KYyKXUpHIDLAuyQdOJUG7JV08btdBFWZ6uHzx7w/xrslRrjdCppCI6b4DwdwsOXqa4msoX7aK SKvp/s3AG6VmhVcqWJAtHk/lRKpvNdZUsMoRSpkFsQ6SLYy92PktHx7aCrAZknU0wtHDK5S2+up J1I20L1V24uBW996BFg8oBxhhMHhTDaD71DPyWdEz6i0bjMjT3oCkLth+6wTBzrBBQUI0yfeaME Zw6KlBEf06kIIdfYVc0Il4e8IUKzq/+BFAU/LIRAiwspanneVqGiU43IACEl0wcO74H1riXL5w= = X-Received: by 2002:a05:7022:1103:b0:1b3:b3de:9232 with SMTP id a92af1059eb24-1b5568a15aamr35014621c88.9.1796231970702; Wed, 02 Dec 2026 09:19:30 -0800 (PST) Received: from mr41p01nt-relayp04.apple.com ([17.199.97.123]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55682d301sm97449533c88.10.2026.12.02.09.19.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Dec 2026 09:19:30 -0800 (PST) From: Scott Mitchell To: dev@dpdk.org Cc: stephen@networkplumber.org, thomas@monjalon.net, david.marchand@redhat.com, mb@smartsharesystems.com, Scott Mitchell Subject: [PATCH v8 8/8] net/af_packet: add Tx checksum offload Date: Wed, 2 Dec 2026 09:18:04 -0800 Message-ID: <20261202171804.50287-9-scott.k.mitch1@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261202171804.50287-1-scott.k.mitch1@gmail.com> References: <20261201222405.88922-1-scott.k.mitch1@gmail.com> <20261202171804.50287-1-scott.k.mitch1@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add software IPv4, UDP and TCP Tx checksum offload. Every packet is already copied into its ring frame, so compute the checksums in the frame after the copy. The checksum code does not modify the mbuf, so shared, indirect and multi-segment mbufs need no special handling. IPv6 extension headers are supported. Packets with tunnel offload flags are dropped, since their header lengths describe the inner packet. Signed-off-by: Scott Mitchell --- app/test/test_pmd_af_packet.c | 330 ++++++++++++++++++++++ doc/guides/nics/features/af_packet.ini | 2 + doc/guides/rel_notes/release_27_03.rst | 4 + drivers/net/af_packet/rte_eth_af_packet.c | 106 ++++++- 4 files changed, 439 insertions(+), 3 deletions(-) diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c index e2c1eb1161..b42121d8ee 100644 --- a/app/test/test_pmd_af_packet.c +++ b/app/test/test_pmd_af_packet.c @@ -24,10 +24,13 @@ #include #include #include +#include #include #include #include #include +#include +#include #include "test.h" @@ -95,6 +98,12 @@ struct tag_count { #define TX_FULL_MIN_FRAMECNT 16u #define TX_FULL_MAX_FRAMECNT 64u +/* Frames built by build_cksum_frame() */ +#define CKSUM_DST_PORT 0x5678 +#define CKSUM_PAYLOAD_LEN 32 +#define CKSUM_BAD 0xdead +#define CKSUM_TCI 0x123 + static struct rte_mempool *mp; static uint16_t port_id = RTE_MAX_ETHPORTS; static int tap_fd = -1; @@ -2075,6 +2084,326 @@ test_af_packet_tx_ring_full(void) return ret; } +/* + * Helper: Build an IPv4 or IPv6 UDP/TCP frame that asks for software + * checksums, with garbage in the checksum fields and id as L4 source port. + */ +static struct rte_mbuf * +build_cksum_frame(uint8_t proto, bool ipv6, uint16_t id) +{ + const size_t l3_len = ipv6 ? sizeof(struct rte_ipv6_hdr) : + sizeof(struct rte_ipv4_hdr); + const size_t l4_len = proto == IPPROTO_UDP ? sizeof(struct rte_udp_hdr) : + sizeof(struct rte_tcp_hdr); + const size_t len = sizeof(struct rte_ether_hdr) + l3_len + l4_len + + CKSUM_PAYLOAD_LEN; + struct rte_ether_hdr *eth; + struct rte_mbuf *m; + uint8_t *l3, *l4; + + m = rte_pktmbuf_alloc(mp); + if (m == NULL) + return NULL; + eth = (struct rte_ether_hdr *)rte_pktmbuf_append(m, len); + if (eth == NULL) { + rte_pktmbuf_free(m); + return NULL; + } + memset(eth, 0xa5, len); + memset(ð->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN); + rte_ether_addr_copy(&DEV_MAC_TAG, ð->src_addr); + l3 = (uint8_t *)(eth + 1); + l4 = l3 + l3_len; + + if (ipv6) { + struct rte_ipv6_hdr *ip6 = (struct rte_ipv6_hdr *)l3; + + eth->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6); + memset(ip6, 0, sizeof(*ip6)); + ip6->vtc_flow = rte_cpu_to_be_32(6u << 28); + ip6->payload_len = rte_cpu_to_be_16(l4_len + CKSUM_PAYLOAD_LEN); + ip6->proto = proto; + ip6->hop_limits = 64; + ip6->src_addr = (struct rte_ipv6_addr)RTE_IPV6(0xfd00, 0, 0, 0, 0, 0, 0, 1); + ip6->dst_addr = (struct rte_ipv6_addr)RTE_IPV6(0xfd00, 0, 0, 0, 0, 0, 0, 2); + m->ol_flags = RTE_MBUF_F_TX_IPV6; + } else { + struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)l3; + + eth->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4); + memset(ip, 0, sizeof(*ip)); + ip->version_ihl = RTE_IPV4_VHL_DEF; + ip->total_length = rte_cpu_to_be_16(l3_len + l4_len + CKSUM_PAYLOAD_LEN); + ip->time_to_live = 64; + ip->next_proto_id = proto; + ip->hdr_checksum = CKSUM_BAD; + ip->src_addr = rte_cpu_to_be_32(RTE_IPV4(10, 0, 0, 1)); + ip->dst_addr = rte_cpu_to_be_32(RTE_IPV4(10, 0, 0, 2)); + m->ol_flags = RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM; + } + + if (proto == IPPROTO_UDP) { + struct rte_udp_hdr *udp = (struct rte_udp_hdr *)l4; + + udp->src_port = rte_cpu_to_be_16(id); + udp->dst_port = rte_cpu_to_be_16(CKSUM_DST_PORT); + udp->dgram_len = rte_cpu_to_be_16(l4_len + CKSUM_PAYLOAD_LEN); + udp->dgram_cksum = CKSUM_BAD; + m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM; + } else { + struct rte_tcp_hdr *tcp = (struct rte_tcp_hdr *)l4; + + memset(tcp, 0, sizeof(*tcp)); + tcp->src_port = rte_cpu_to_be_16(id); + tcp->dst_port = rte_cpu_to_be_16(CKSUM_DST_PORT); + tcp->data_off = (sizeof(*tcp) / 4) << 4; + tcp->tcp_flags = RTE_TCP_ACK_FLAG; + tcp->cksum = CKSUM_BAD; + m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM; + } + m->l2_len = sizeof(*eth); + m->l3_len = l3_len; + return m; +} + +/* Helper: Move the bytes of single-segment m from offset at into a second segment. */ +static int +split_mbuf(struct rte_mbuf *m, uint16_t at) +{ + uint16_t n = rte_pktmbuf_data_len(m) - at; + struct rte_mbuf *tail; + void *dst; + + tail = rte_pktmbuf_alloc(mp); + if (tail == NULL) + return -1; + dst = rte_pktmbuf_append(tail, n); + if (dst == NULL) { + rte_pktmbuf_free(tail); + return -1; + } + memcpy(dst, rte_pktmbuf_mtod_offset(m, void *, at), n); + rte_pktmbuf_trim(m, n); + if (rte_pktmbuf_chain(m, tail) != 0) { + rte_pktmbuf_free(tail); + return -1; + } + return 0; +} + +/* Helper: Insert an 8-byte Hop-by-Hop header after the IPv6 header of single-segment m. */ +static int +add_ipv6_hopopts(struct rte_mbuf *m) +{ + const uint16_t l4_len = rte_pktmbuf_data_len(m) - m->l2_len - m->l3_len; + struct rte_ipv6_hdr *ip6; + uint8_t *ext; + + if (rte_pktmbuf_append(m, 8) == NULL) + return -1; + ip6 = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len); + ext = (uint8_t *)(ip6 + 1); + memmove(ext + 8, ext, l4_len); + /* next header, length 0 (8 bytes), then a 4-byte PadN option */ + ext[0] = ip6->proto; + ext[1] = 0; + ext[2] = 1; + ext[3] = 4; + memset(ext + 4, 0, 4); + ip6->proto = IPPROTO_HOPOPTS; + ip6->payload_len = rte_cpu_to_be_16(rte_be_to_cpu_16(ip6->payload_len) + 8); + m->l3_len += 8; + return 0; +} + +/* + * Helper: Check a frame from build_cksum_frame(), which may carry a VLAN tag. + * Returns its id, -1 if it is some other frame, -2 if a checksum is wrong. + */ +static int +check_cksum_frame(const uint8_t *frame, size_t len) +{ + const struct rte_ether_hdr *eth = (const struct rte_ether_hdr *)frame; + size_t off = sizeof(*eth); + uint16_t type = eth->ether_type; + const struct rte_udp_hdr *l4; + const void *l3; + int bad; + + if (len < off || !rte_is_same_ether_addr(ð->src_addr, &DEV_MAC_TAG)) + return -1; + if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) { + const struct rte_vlan_hdr *vh = (const struct rte_vlan_hdr *)(frame + off); + + off += sizeof(*vh); + if (len < off || rte_be_to_cpu_16(vh->vlan_tci) != CKSUM_TCI) + return -1; + type = vh->eth_proto; + } + l3 = frame + off; + if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { + off += sizeof(struct rte_ipv4_hdr); + } else if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) { + off += sizeof(struct rte_ipv6_hdr); + if (len < off + 2) + return -1; + /* Hop-by-Hop header from add_ipv6_hopopts() */ + if (((const struct rte_ipv6_hdr *)l3)->proto == IPPROTO_HOPOPTS) + off += 8 * (frame[off + 1] + 1); + } else { + return -1; + } + l4 = (const struct rte_udp_hdr *)(frame + off); + if (len < off + sizeof(*l4) || l4->dst_port != rte_cpu_to_be_16(CKSUM_DST_PORT)) + return -1; + + if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { + bad = rte_ipv4_cksum(l3) != 0 || rte_ipv4_udptcp_cksum_verify(l3, l4) != 0; + } else { + /* verify with the L4 length and protocol, past any extension header */ + struct rte_ipv6_hdr ph = *(const struct rte_ipv6_hdr *)l3; + const size_t ext_len = (const uint8_t *)l4 - (const uint8_t *)l3 - sizeof(ph); + + if (ext_len != 0) + ph.proto = ((const uint8_t *)l3)[sizeof(ph)]; + ph.payload_len = rte_cpu_to_be_16(rte_be_to_cpu_16(ph.payload_len) - ext_len); + bad = rte_ipv6_udptcp_cksum_verify(&ph, l4) != 0; + } + if (bad) { + printf("Bad checksum in frame %u\n", rte_be_to_cpu_16(l4->src_port)); + return -2; + } + return rte_be_to_cpu_16(l4->src_port); +} + +/* + * Test: Software TX checksum offload. Checksums are filled in for IPv4 and + * IPv6, UDP and TCP, with an IPv6 extension header, with a VLAN inserted, + * with headers split across segments and for a clone whose data must not + * change. Frames whose IP length doesn't fit the packet, or with tunnel + * offload flags, are dropped. + */ +static int +test_af_packet_tx_cksum(void) +{ + const uint64_t offloads = RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | + RTE_ETH_TX_OFFLOAD_UDP_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_CKSUM | + RTE_ETH_TX_OFFLOAD_VLAN_INSERT | RTE_ETH_TX_OFFLOAD_MULTI_SEGS; + enum { + V4_UDP, V4_TCP, V6_UDP, V6_TCP, V6_EXT, V4_VLAN, V4_SPLIT, V6_CLONE, + V4_BAD, V4_TUNNEL, NB_CASES /* frames from V4_BAD on are dropped */ + }; + const char *name = "net_af_packet_cksum"; + struct rte_eth_dev_info dev_info; + struct rte_eth_stats stats; + struct rte_mbuf *m[NB_CASES] = { NULL }; + struct rte_mbuf *orig = NULL; + const struct rte_tcp_hdr *orig_tcp; + uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE]; + unsigned int seen = 0, i; + const unsigned int want = (1u << V4_BAD) - 1; + uint64_t elapsed; + uint16_t pid; + ssize_t len; + int flags, ret, id; + + if (!tap_created) { + printf("SKIPPED: TAP interface not available (need root)\n"); + return TEST_SKIPPED; + } + + ret = create_test_port(name, "iface=" TAP_DEV_NAME, offloads, &pid); + TEST_ASSERT(ret == 0, "Failed to create port"); + + flags = fcntl(tap_fd, F_GETFL, 0); + fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK); + tap_drain(); + + ret = TEST_FAILED; + if (rte_eth_dev_info_get(pid, &dev_info) != 0 || + (dev_info.tx_offload_capa & offloads) != offloads) { + printf("Checksum offloads not advertised\n"); + goto out; + } + + m[V4_UDP] = build_cksum_frame(IPPROTO_UDP, false, V4_UDP); + m[V4_TCP] = build_cksum_frame(IPPROTO_TCP, false, V4_TCP); + m[V6_UDP] = build_cksum_frame(IPPROTO_UDP, true, V6_UDP); + m[V6_TCP] = build_cksum_frame(IPPROTO_TCP, true, V6_TCP); + m[V6_EXT] = build_cksum_frame(IPPROTO_UDP, true, V6_EXT); + m[V4_VLAN] = build_cksum_frame(IPPROTO_TCP, false, V4_VLAN); + m[V4_SPLIT] = build_cksum_frame(IPPROTO_UDP, false, V4_SPLIT); + orig = build_cksum_frame(IPPROTO_TCP, true, V6_CLONE); + m[V4_BAD] = build_cksum_frame(IPPROTO_UDP, false, V4_BAD); + m[V4_TUNNEL] = build_cksum_frame(IPPROTO_UDP, false, V4_TUNNEL); + if (orig != NULL) + m[V6_CLONE] = rte_pktmbuf_clone(orig, mp); + for (i = 0; i < NB_CASES; i++) + if (m[i] == NULL) + break; + if (i < NB_CASES) { + rte_pktmbuf_free_bulk(m, NB_CASES); + goto out; + } + + m[V4_VLAN]->ol_flags |= RTE_MBUF_F_TX_VLAN; + m[V4_VLAN]->vlan_tci = CKSUM_TCI; + m[V4_TUNNEL]->ol_flags |= RTE_MBUF_F_TX_TUNNEL_VXLAN; + /* V4_SPLIT is split inside the IPv4 header */ + if (add_ipv6_hopopts(m[V6_EXT]) != 0 || + split_mbuf(m[V4_SPLIT], m[V4_SPLIT]->l2_len + 10) != 0) { + rte_pktmbuf_free_bulk(m, NB_CASES); + goto out; + } + rte_pktmbuf_mtod_offset(m[V4_BAD], struct rte_ipv4_hdr *, + m[V4_BAD]->l2_len)->total_length = rte_cpu_to_be_16(1500); + + rte_eth_stats_reset(pid); + if (do_tx_burst(pid, 0, m, NB_CASES) != NB_CASES) { + printf("TX burst was short\n"); + goto out; + } + + orig_tcp = rte_pktmbuf_mtod_offset(orig, const struct rte_tcp_hdr *, + orig->l2_len + orig->l3_len); + if (orig_tcp->cksum != CKSUM_BAD) { + printf("TX modified the data of a cloned mbuf\n"); + goto out; + } + + for (elapsed = 0; seen != want && elapsed < TAP_READ_TIMEOUT_US;) { + len = read(tap_fd, buf, sizeof(buf)); + if (len <= 0) { + rte_delay_us_block(STATS_POLL_INTERVAL_US); + elapsed += STATS_POLL_INTERVAL_US; + continue; + } + id = check_cksum_frame(buf, len); + if (id == -2 || id >= V4_BAD) { + if (id >= V4_BAD) + printf("Frame %d should have been dropped\n", id); + goto out; + } + if (id >= 0 && id < NB_CASES) + seen |= 1u << id; + } + if (seen != want) { + printf("Missing frames: seen 0x%x, want 0x%x\n", seen, want); + goto out; + } + if (rte_eth_stats_get(pid, &stats) != 0 || stats.oerrors != NB_CASES - V4_BAD) { + printf("oerrors=%" PRIu64 ", expected %d\n", stats.oerrors, NB_CASES - V4_BAD); + goto out; + } + ret = TEST_SUCCESS; +out: + rte_pktmbuf_free(orig); + tap_drain(); + fcntl(tap_fd, F_SETFL, flags); + destroy_test_port(name, pid); + return ret; +} + static struct unit_test_suite af_packet_test_suite = { .suite_name = "AF_PACKET PMD Unit Test Suite", .setup = test_af_packet_setup, @@ -2104,6 +2433,7 @@ static struct unit_test_suite af_packet_test_suite = { TEST_CASE(test_af_packet_tx_vlan_oversize), TEST_CASE(test_af_packet_tx_mbuf_accounting), TEST_CASE(test_af_packet_tx_ring_full), + TEST_CASE(test_af_packet_tx_cksum), TEST_CASE(test_af_packet_qdisc_bypass), TEST_CASE(test_af_packet_multi_queue), TEST_CASE(test_af_packet_fanout_modes), diff --git a/doc/guides/nics/features/af_packet.ini b/doc/guides/nics/features/af_packet.ini index b1cbc54aca..275a120f0f 100644 --- a/doc/guides/nics/features/af_packet.ini +++ b/doc/guides/nics/features/af_packet.ini @@ -6,4 +6,6 @@ [Features] Promiscuous mode = Y MTU update = Y +L3 checksum offload = Y +L4 checksum offload = Y Stats per queue = Y diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index a91813faca..dd9e8a57bb 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -55,6 +55,10 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Updated af_packet net driver.** + + * Added software TX checksum offload for IPv4, UDP and TCP. + Removed Items ------------- diff --git a/drivers/net/af_packet/rte_eth_af_packet.c b/drivers/net/af_packet/rte_eth_af_packet.c index 1535a93dca..bf847ab602 100644 --- a/drivers/net/af_packet/rte_eth_af_packet.c +++ b/drivers/net/af_packet/rte_eth_af_packet.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -331,6 +334,94 @@ tx_ring_status_available(uint32_t tp_status) return tp_status == TP_STATUS_AVAILABLE; } +#define AF_PACKET_TX_CKSUM_FLAGS (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK) + +/* + * Fill in the checksums m asks for in its copy at data; the mbuf is not + * written. Returns false if m's offload fields don't match the packet. + */ +static inline bool +af_packet_tx_cksum(uint8_t *data, const struct rte_mbuf *m) +{ + const uint64_t ol_flags = m->ol_flags; + const uint32_t l4_off = m->l2_len + m->l3_len; + void *l3 = data + m->l2_len; + uint8_t *l4 = data + l4_off; + unaligned_uint16_t *l4_cksum; + uint32_t l4_hdr_len, l4_end; + uint8_t l4_proto; + + /* l2_len/l3_len describe inner headers for tunnels, which aren't supported */ + if (unlikely(ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)) + return false; + + switch (ol_flags & RTE_MBUF_F_TX_L4_MASK) { + case RTE_MBUF_F_TX_UDP_CKSUM: + l4_cksum = (unaligned_uint16_t *)(l4 + offsetof(struct rte_udp_hdr, dgram_cksum)); + l4_hdr_len = sizeof(struct rte_udp_hdr); + l4_proto = IPPROTO_UDP; + break; + case RTE_MBUF_F_TX_TCP_CKSUM: + l4_cksum = (unaligned_uint16_t *)(l4 + offsetof(struct rte_tcp_hdr, cksum)); + l4_hdr_len = sizeof(struct rte_tcp_hdr); + l4_proto = IPPROTO_TCP; + break; + case RTE_MBUF_F_TX_L4_NO_CKSUM: + l4_cksum = NULL; + l4_hdr_len = 0; + l4_proto = 0; + break; + default: + return false; + } + + /* lib/net takes the L4 length from the IP header; keep it in the packet */ + if (ol_flags & RTE_MBUF_F_TX_IPV4) { + struct rte_ipv4_hdr *ip = l3; + + if (unlikely(m->l3_len < sizeof(*ip) || l4_off + l4_hdr_len > m->pkt_len || + rte_ipv4_hdr_len(ip) != m->l3_len)) + return false; + l4_end = m->l2_len + rte_be_to_cpu_16(ip->total_length); + if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) { + ip->hdr_checksum = 0; + ip->hdr_checksum = rte_ipv4_cksum(ip); + } + } else if ((ol_flags & (RTE_MBUF_F_TX_IPV6 | RTE_MBUF_F_TX_IP_CKSUM)) == + RTE_MBUF_F_TX_IPV6) { + const struct rte_ipv6_hdr *ip6 = l3; + + /* l3_len includes any extension headers */ + if (unlikely(m->l3_len < sizeof(*ip6) || l4_off + l4_hdr_len > m->pkt_len)) + return false; + l4_end = m->l2_len + sizeof(*ip6) + rte_be_to_cpu_16(ip6->payload_len); + } else { + return false; + } + + if (l4_cksum != NULL) { + if (unlikely(l4_end < l4_off + l4_hdr_len || l4_end > m->pkt_len)) + return false; + *l4_cksum = 0; + if (ol_flags & RTE_MBUF_F_TX_IPV4) { + *l4_cksum = rte_ipv4_udptcp_cksum(l3, l4); + } else { + const struct rte_ipv6_hdr *ip6 = l3; + struct rte_ipv6_hdr ph; + + /* after extension headers: pseudo-header of the L4 length and protocol */ + if (unlikely(m->l3_len != sizeof(ph))) { + ph = *ip6; + ph.payload_len = rte_cpu_to_be_16(l4_end - l4_off); + ph.proto = l4_proto; + ip6 = &ph; + } + *l4_cksum = rte_ipv6_udptcp_cksum(ip6, l4); + } + } + return true; +} + /* * Callback to handle sending packets through a real NIC. */ @@ -339,7 +430,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) { struct tpacket2_hdr *ppd; struct rte_mbuf *mbuf; - uint8_t *pbuf; + uint8_t *data, *pbuf; unsigned int framecount, framenum; struct pkt_tx_queue *pkt_q = queue; uint16_t num_tx = 0; @@ -366,7 +457,8 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) break; /* copy the tx frame data */ - pbuf = (uint8_t *)ppd + ETH_AF_PACKET_TX_FRAME_OVERHEAD; + data = (uint8_t *)ppd + ETH_AF_PACKET_TX_FRAME_OVERHEAD; + pbuf = data; struct rte_mbuf *tmp_mbuf = mbuf; while (tmp_mbuf) { @@ -376,6 +468,11 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) tmp_mbuf = tmp_mbuf->next; } + /* drop if checksums can't be done; the frame is reused */ + if ((mbuf->ol_flags & AF_PACKET_TX_CKSUM_FLAGS) != 0 && + unlikely(!af_packet_tx_cksum(data, mbuf))) + continue; + ppd->tp_len = mbuf->pkt_len; ppd->tp_snaplen = mbuf->pkt_len; @@ -480,7 +577,10 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) dev_info->max_tx_queues = (uint16_t)internals->nb_queues; dev_info->min_rx_bufsize = 0; dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS | - RTE_ETH_TX_OFFLOAD_VLAN_INSERT; + RTE_ETH_TX_OFFLOAD_VLAN_INSERT | + RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | + RTE_ETH_TX_OFFLOAD_UDP_CKSUM | + RTE_ETH_TX_OFFLOAD_TCP_CKSUM; dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP | RTE_ETH_RX_OFFLOAD_TIMESTAMP | RTE_ETH_RX_OFFLOAD_SCATTER; -- 2.54.0 (Apple Git-157)