selftests: bpf: introduce a common routine for reading procfs

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>
master
Maciej Fijalkowski 2026-04-02 17:49:55 +02:00 committed by Jakub Kicinski
parent 36ee60b569
commit c5866a6be4
4 changed files with 66 additions and 24 deletions

View 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;
}

View 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;

View 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"))

View 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) {