From 1c59188cf037896be7ca81568354bdac0a2a113f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Fri, 12 Sep 2025 03:35:37 -0700 Subject: [PATCH] NetBpfLoad: use libbpf based on mainline beta flag, part 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Disable shared version of libtetheringflags - since the flags are readonly there is no real content to this, and it should be statically linked for compile/link time optimization (which should basically optimize the entire library out) Use C++ magic to compile time verify the macro and flag are in sync. The way this works: constexpr auto useLibBpf = COM_ANDROID_TETHERING_READONLY_FLAGS_USE_LIBBPF; ^ this basically requires the macro to resolve to true/false, so that 'auto' resolves to 'const bool' static_assert(std::is_same::value, "useLibBpf must be a boolean."); ^ which we check here static_assert(useLibBpf == com::android::tethering::readonly::flags::use_libbpf(), "useLibBpf flag mismatch"); ^ and this then requires the macro and flag to be identical *and* compile time known Test: TreeHugger Flag: EXEMPT PURE_REFACTOR Signed-off-by: Maciej Żenczykowski Change-Id: Id5cd8c7d91b424765d6f469806434b3e9587f074 --- bpf/loader/NetBpfLoad.cpp | 12 ++++++------ common/Android.bp | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index e448f06b16..dda9c0db7c 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -90,6 +90,11 @@ using std::vector; namespace android { namespace bpf { +// This verifies the macro and the C++ api are in sync, and that they're compile time known. +constexpr auto useLibBpf = COM_ANDROID_TETHERING_READONLY_FLAGS_USE_LIBBPF; +static_assert(std::is_same::value, "useLibBpf must be a boolean."); +static_assert(useLibBpf == com::android::tethering::readonly::flags::use_libbpf(), "useLibBpf flag mismatch"); + // Due to support for RiscV not yet having been released, // there is no minimum supported api level for it. // @@ -1389,16 +1394,11 @@ static bool loadObject(const unsigned int bpfloader_ver, return true; } -#ifndef COM_ANDROID_TETHERING_READONLY_FLAGS_USE_LIBBPF -#error "COM_ANDROID_TETHERING_READONLY_FLAGS_USE_LIBBPF must be defined" -#endif -#define USE_LIBBPF COM_ANDROID_TETHERING_READONLY_FLAGS_USE_LIBBPF - #define APEXROOT "/apex/com.android.tethering" #define BPFROOT APEXROOT "/etc/bpf/mainline/" static bool loadAllObjects(const unsigned int bpfloader_ver) { - bool libbpf = isAtLeast25Q3 || USE_LIBBPF; + bool libbpf = isAtLeast25Q3 || useLibBpf; if (!loadObject(bpfloader_ver, BPFROOT "offload.o")) return false; if (!loadObject(bpfloader_ver, BPFROOT "test.o", libbpf)) return false; if (isAtLeastT) { diff --git a/common/Android.bp b/common/Android.bp index 5310e6dc8f..3b49d3529a 100644 --- a/common/Android.bp +++ b/common/Android.bp @@ -64,6 +64,8 @@ aconfig_declarations { cc_aconfig_library { name: "libtetheringflags", aconfig_declarations: "tethering.readonly.flags", + mode: "force-read-only", + shared: { enabled: false }, min_sdk_version: "30", apex_available: [ "com.android.tethering",