xsk: Add option to calculate TX checksum in SW
authorStanislav Fomichev <sdf@google.com>
Mon, 27 Nov 2023 19:03:14 +0000 (11:03 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 29 Nov 2023 22:59:40 +0000 (14:59 -0800)
For XDP_COPY mode, add a UMEM option XDP_UMEM_TX_SW_CSUM
to call skb_checksum_help in transmit path. Might be useful
to debugging issues with real hardware. I also use this mode
in the selftests.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/r/20231127190319.1190813-9-sdf@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Documentation/networking/xsk-tx-metadata.rst
include/net/xsk_buff_pool.h
include/uapi/linux/if_xdp.h
net/xdp/xdp_umem.c
net/xdp/xsk.c
net/xdp/xsk_buff_pool.c
tools/include/uapi/linux/if_xdp.h

index 4f376560b23f2b818f66362c91f72606c605cf7c..97ecfa480d00b4f52c868914f656dbf549f38a01 100644 (file)
@@ -50,6 +50,15 @@ packet's ``struct xdp_desc`` descriptor should set ``XDP_TX_METADATA``
 bit in the ``options`` field. Also note that in a multi-buffer packet
 only the first chunk should carry the metadata.
 
+Software TX Checksum
+====================
+
+For development and testing purposes its possible to pass
+``XDP_UMEM_TX_SW_CSUM`` flag to ``XDP_UMEM_REG`` UMEM registration call.
+In this case, when running in ``XDK_COPY`` mode, the TX checksum
+is calculated on the CPU. Do not enable this option in production because
+it will negatively affect performance.
+
 Querying Device Capabilities
 ============================
 
index 97f5cc10d79e73907330b4c1fad30704a5c61059..8d48d37ab7c0f6e6674ab8c01b977e6ceacbb416 100644 (file)
@@ -83,6 +83,7 @@ struct xsk_buff_pool {
        bool uses_need_wakeup;
        bool dma_need_sync;
        bool unaligned;
+       bool tx_sw_csum;
        void *addrs;
        /* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
         * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
index 95de66d5a26c8407c57cb71038fdbde35deaa0ae..d31698410410735a7e25bae54ce3a04265934fe2 100644 (file)
 #define XDP_USE_SG     (1 << 4)
 
 /* Flags for xsk_umem_config flags */
-#define XDP_UMEM_UNALIGNED_CHUNK_FLAG (1 << 0)
+#define XDP_UMEM_UNALIGNED_CHUNK_FLAG  (1 << 0)
+
+/* Force checksum calculation in software. Can be used for testing or
+ * working around potential HW issues. This option causes performance
+ * degradation and only works in XDP_COPY mode.
+ */
+#define XDP_UMEM_TX_SW_CSUM            (1 << 1)
 
 struct sockaddr_xdp {
        __u16 sxdp_family;
index 946a687fb8e83eb9491d39df3bed99f525cb8144..caa340134b0e1d5fd3d67197c15bfc4314a91637 100644 (file)
@@ -148,6 +148,11 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
        return 0;
 }
 
+#define XDP_UMEM_FLAGS_VALID ( \
+               XDP_UMEM_UNALIGNED_CHUNK_FLAG | \
+               XDP_UMEM_TX_SW_CSUM | \
+       0)
+
 static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
 {
        bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
@@ -167,7 +172,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
                return -EINVAL;
        }
 
-       if (mr->flags & ~XDP_UMEM_UNALIGNED_CHUNK_FLAG)
+       if (mr->flags & ~XDP_UMEM_FLAGS_VALID)
                return -EINVAL;
 
        if (!unaligned_chunks && !is_power_of_2(chunk_size))
index d66ba9d6154f9e346b020b141940d7c4f7d210bb..281d49b4fca4eaf77b712d8923eafdf9f0ffe395 100644 (file)
@@ -744,6 +744,12 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                                skb->csum_start = hr + meta->request.csum_start;
                                skb->csum_offset = meta->request.csum_offset;
                                skb->ip_summed = CHECKSUM_PARTIAL;
+
+                               if (unlikely(xs->pool->tx_sw_csum)) {
+                                       err = skb_checksum_help(skb);
+                                       if (err)
+                                               goto free_err;
+                               }
                        }
                }
        }
index 386eddcdf8374e12ec6b910ad938ba798ce7bb53..4f6f538a5462773f8bf1eedf5e78bc992550434a 100644 (file)
@@ -86,6 +86,7 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
        pool->umem = umem;
        pool->addrs = umem->addrs;
        pool->tx_metadata_len = umem->tx_metadata_len;
+       pool->tx_sw_csum = umem->flags & XDP_UMEM_TX_SW_CSUM;
        INIT_LIST_HEAD(&pool->free_list);
        INIT_LIST_HEAD(&pool->xskb_list);
        INIT_LIST_HEAD(&pool->xsk_tx_list);
index d0882edc164205de0220d92dd69368216008b849..638c606dfa74f3848500ac3d8d85a668bad015fe 100644 (file)
 #define XDP_USE_SG     (1 << 4)
 
 /* Flags for xsk_umem_config flags */
-#define XDP_UMEM_UNALIGNED_CHUNK_FLAG (1 << 0)
+#define XDP_UMEM_UNALIGNED_CHUNK_FLAG  (1 << 0)
+
+/* Force checksum calculation in software. Can be used for testing or
+ * working around potential HW issues. This option causes performance
+ * degradation and only works in XDP_COPY mode.
+ */
+#define XDP_UMEM_TX_SW_CSUM            (1 << 1)
 
 struct sockaddr_xdp {
        __u16 sxdp_family;