]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests: bpf: introduce a common routine for reading procfs
authorMaciej Fijalkowski <maciej.fijalkowski@intel.com>
Thu, 2 Apr 2026 15:49:55 +0000 (17:49 +0200)
committerJakub Kicinski <kuba@kernel.org>
Tue, 7 Apr 2026 01:43:51 +0000 (18:43 -0700)
Parametrize current way of getting MAX_SKB_FRAGS value from {sys,proc}fs
so that it can be re-used to get cache line size of system's CPU. All
that just to mimic and compute size of kernel's struct skb_shared_info
which for xsk and test suite interpret as tailroom.

Introduce two variables to ifobject struct that will carry count of skb
frags and tailroom size. Do the reading and computing once, at the
beginning of test suite execution in xskxceiver, but for test_progs such
way is not possible as in this environment each test setups and torns
down ifobject structs.

Reviewed-by: Björn Töpel <bjorn@kernel.org>
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Link: https://patch.msgid.link/20260402154958.562179-6-maciej.fijalkowski@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/bpf/prog_tests/test_xsk.c
tools/testing/selftests/bpf/prog_tests/test_xsk.h
tools/testing/selftests/bpf/prog_tests/xsk.c
tools/testing/selftests/bpf/xskxceiver.c

index 7e38ec6e656b97ac878b20b64441069eb4d531f2..62118ffba661f024642e57e6a56810a89e6ccc9f 100644 (file)
@@ -179,25 +179,6 @@ int xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem
        return xsk_socket__create(&xsk->xsk, ifobject->ifindex, 0, umem->umem, rxr, txr, &cfg);
 }
 
-#define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags"
-static unsigned int get_max_skb_frags(void)
-{
-       unsigned int max_skb_frags = 0;
-       FILE *file;
-
-       file = fopen(MAX_SKB_FRAGS_PATH, "r");
-       if (!file) {
-               ksft_print_msg("Error opening %s\n", MAX_SKB_FRAGS_PATH);
-               return 0;
-       }
-
-       if (fscanf(file, "%u", &max_skb_frags) != 1)
-               ksft_print_msg("Error reading %s\n", MAX_SKB_FRAGS_PATH);
-
-       fclose(file);
-       return max_skb_frags;
-}
-
 static int set_ring_size(struct ifobject *ifobj)
 {
        int ret;
@@ -2242,11 +2223,7 @@ int testapp_too_many_frags(struct test_spec *test)
        if (test->mode == TEST_MODE_ZC) {
                max_frags = test->ifobj_tx->xdp_zc_max_segs;
        } else {
-               max_frags = get_max_skb_frags();
-               if (!max_frags) {
-                       ksft_print_msg("Can't get MAX_SKB_FRAGS from system, using default (17)\n");
-                       max_frags = 17;
-               }
+               max_frags = test->ifobj_tx->max_skb_frags;
                max_frags += 1;
        }
 
index 8fc78a057de0bed1792be8f91c0c5594c1122fdf..1ab8aee4ce564066e8e17a74b452a19d5c47b823 100644 (file)
@@ -31,6 +31,9 @@
 #define SOCK_RECONF_CTR                        10
 #define USLEEP_MAX                     10000
 
+#define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags"
+#define SMP_CACHE_BYTES_PATH "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
+
 extern bool opt_verbose;
 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
 
@@ -45,6 +48,24 @@ static inline u64 ceil_u64(u64 a, u64 b)
        return (a + b - 1) / b;
 }
 
+static inline unsigned int read_procfs_val(const char *path)
+{
+       unsigned int read_val = 0;
+       FILE *file;
+
+       file = fopen(path, "r");
+       if (!file) {
+               ksft_print_msg("Error opening %s\n", path);
+               return 0;
+       }
+
+       if (fscanf(file, "%u", &read_val) != 1)
+               ksft_print_msg("Error reading %s\n", path);
+
+       fclose(file);
+       return read_val;
+}
+
 /* Simple test */
 enum test_mode {
        TEST_MODE_SKB,
@@ -115,6 +136,8 @@ struct ifobject {
        int mtu;
        u32 bind_flags;
        u32 xdp_zc_max_segs;
+       u32 umem_tailroom;
+       u32 max_skb_frags;
        bool tx_on;
        bool rx_on;
        bool use_poll;
index dd4c35c0e428b4ff77c06f1c5889aed92f34984c..6e2f63ee2a6c37bc07d05ae6b65f35ebd44ae0c6 100644 (file)
@@ -62,6 +62,7 @@ int configure_ifobj(struct ifobject *tx, struct ifobject *rx)
 
 static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode)
 {
+       u32 max_frags, umem_tailroom, cache_line_size;
        struct ifobject *ifobj_tx, *ifobj_rx;
        struct test_spec test;
        int ret;
@@ -84,6 +85,24 @@ static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode)
                ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending;
        }
 
+       cache_line_size = read_procfs_val(SMP_CACHE_BYTES_PATH);
+       if (!cache_line_size)
+               cache_line_size = 64;
+
+       max_frags = read_procfs_val(MAX_SKB_FRAGS_PATH);
+       if (!max_frags)
+               max_frags = 17;
+
+       ifobj_tx->max_skb_frags = max_frags;
+       ifobj_rx->max_skb_frags = max_frags;
+
+       /* 48 bytes is a part of skb_shared_info w/o frags array;
+        * 16 bytes is sizeof(skb_frag_t)
+        */
+       umem_tailroom = ALIGN(48 + (max_frags * 16), cache_line_size);
+       ifobj_tx->umem_tailroom = umem_tailroom;
+       ifobj_rx->umem_tailroom = umem_tailroom;
+
        if (!ASSERT_OK(init_iface(ifobj_rx, worker_testapp_validate_rx), "init RX"))
                goto delete_rx;
        if (!ASSERT_OK(init_iface(ifobj_tx, worker_testapp_validate_tx), "init TX"))
index 05b3cebc5ca93fde2b5859193777643c3d87c46b..7dad8556a722e4d5955c8f11fc8afe3d58be413a 100644 (file)
@@ -80,6 +80,7 @@
 #include <linux/mman.h>
 #include <linux/netdev.h>
 #include <linux/ethtool.h>
+#include <linux/align.h>
 #include <arpa/inet.h>
 #include <net/if.h>
 #include <locale.h>
@@ -333,6 +334,7 @@ static void print_tests(void)
 int main(int argc, char **argv)
 {
        const size_t total_tests = ARRAY_SIZE(tests) + ARRAY_SIZE(ci_skip_tests);
+       u32 cache_line_size, max_frags, umem_tailroom;
        struct pkt_stream *rx_pkt_stream_default;
        struct pkt_stream *tx_pkt_stream_default;
        struct ifobject *ifobj_tx, *ifobj_rx;
@@ -354,6 +356,27 @@ int main(int argc, char **argv)
 
        setlocale(LC_ALL, "");
 
+       cache_line_size = read_procfs_val(SMP_CACHE_BYTES_PATH);
+       if (!cache_line_size) {
+               ksft_print_msg("Can't get SMP_CACHE_BYTES from system, using default (64)\n");
+               cache_line_size = 64;
+       }
+
+       max_frags = read_procfs_val(MAX_SKB_FRAGS_PATH);
+       if (!max_frags) {
+               ksft_print_msg("Can't get MAX_SKB_FRAGS from system, using default (17)\n");
+               max_frags = 17;
+       }
+       ifobj_tx->max_skb_frags = max_frags;
+       ifobj_rx->max_skb_frags = max_frags;
+
+       /* 48 bytes is a part of skb_shared_info w/o frags array;
+        * 16 bytes is sizeof(skb_frag_t)
+        */
+       umem_tailroom = ALIGN(48 + (max_frags * 16), cache_line_size);
+       ifobj_tx->umem_tailroom = umem_tailroom;
+       ifobj_rx->umem_tailroom = umem_tailroom;
+
        parse_command_line(ifobj_tx, ifobj_rx, argc, argv);
 
        if (opt_print_tests) {