NetBpfLoad: use libbpf based on mainline beta flag, part 2

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<decltype(useLibBpf), const bool>::value, "useLibBpf must be a boolean.");

^ which we check here

static_assert(useLibBpf == com::android::tethering::readonly:🎏: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 <maze@google.com>
Change-Id: Id5cd8c7d91b424765d6f469806434b3e9587f074
This commit is contained in:
Maciej Żenczykowski 2025-09-12 03:35:37 -07:00
commit 1c59188cf0
2 changed files with 8 additions and 6 deletions

View file

@ -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<decltype(useLibBpf), const bool>::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) {

View file

@ -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",