Skip to content

libxdp: cache kernel feature probes - #594

Open
sairex53 wants to merge 1 commit into
xdp-project:mainfrom
sairex53:codex/issue-478-precache-kernel-features
Open

sairex53 wants to merge 1 commit into
xdp-project:mainfrom
sairex53:codex/issue-478-precache-kernel-features

Conversation

@sairex53

Copy link
Copy Markdown

Summary

Add libxdp_cache_kernel_features() so applications can probe and cache XDP frags and device-bound support during single-threaded startup. Dispatcher attachments then reuse those results instead of repeating unsupported-feature probes and their libbpf diagnostics.

Document the API and its startup requirements, export it in the LIBXDP_1.7.0 ABI, and add a regression test for repeated calls.

Fixes #478.

Testing

  • make -s on Fedora 44 ARM64
  • test_kernel_feature_cache passed when run directly
  • Shared and static API link checks passed
  • git diff --check passed

The full privileged test runner was not run because this SSH account lacks sudo and the required namespace privileges.

Signed-off-by: Eduard Zateev <hackerowskiy@gmail.com>

@tohojo tohojo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with adding the API, but I don't think we should add a whole new code path for it. Rather, we can transparently introduce caching to the existing probe method, something like:

diff --git i/lib/libxdp/libxdp.c w/lib/libxdp/libxdp.c
index fa57b00329e1..7877e1da1b17 100644
--- i/lib/libxdp/libxdp.c
+++ w/lib/libxdp/libxdp.c
@@ -1694,7 +1694,7 @@ static const char *get_bpf_flag_name(__u32 flag)
        }
 }
 
-static bool kernel_has_bpf_flag(__u32 flag)
+static bool kernel_probe_bpf_flag(__u32 flag)
 {
        struct xdp_program *test_prog;
        bool ret = false;
@@ -1723,6 +1723,38 @@ static bool kernel_has_bpf_flag(__u32 flag)
        return ret;
 }
 
+/* We cache the two flags defined as bits 5 and 6 in the kernel headers */
+#define FEATURE_CACHE_OFFSET  5
+#define FEATURE_CACHE_SIZE   2
+#define FEATURE_PROBED       (1U << 0)
+#define FEATURE_AVAILABLE    (1U << 1)
+static unsigned int feature_cache[FEATURE_CACHE_SIZE];
+
+static bool kernel_has_bpf_flag(__u32 flag) {
+       unsigned int value, slot = __builtin_ctz(flag) - FEATURE_CACHE_OFFSET;
+
+       if (slot >= FEATURE_CACHE_SIZE) {
+               pr_warn("Invalid probe flag %u - please report this as a bug!\n", flag);
+               return false;
+       }
+
+       value = __atomic_load_n(&feature_cache[slot], __ATOMIC_ACQUIRE);
+       if (value & FEATURE_PROBED)
+               goto out;
+
+       value = FEATURE_PROBED;
+       if (kernel_probe_bpf_flag(flag))
+               value |= FEATURE_AVAILABLE;
+
+       /* We assume probing always returns the same result, so we don't bother
+        * with a cmpxchg() when setting this.
+        */
+       __atomic_store_n(&feature_cache[slot], value, __ATOMIC_RELEASE);
+
+out:
+       return !!(value & FEATURE_AVAILABLE);
+}
+
 static bool kernel_has_frags_support(void)
 {
        return kernel_has_bpf_flag(BPF_F_XDP_HAS_FRAGS);

The new function then becomes simply:

void libxdp_cache_kernel_features(void)
{
	(void)kernel_has_frags_support();
	(void)kernel_has_dev_bound();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide libxdp function to pre-cache kernel feature probing

2 participants