From 6c0ce9f17a1f3a3fb0ed4123d123a01abd30a239 Mon Sep 17 00:00:00 2001 From: Connor O'Brien Date: Thu, 1 Dec 2022 20:08:17 -0800 Subject: [PATCH] bpfloader: pass whole struct Location to loadProg() Simplify the loadProg() interface by passing struct Location instead of passing its fields as separate arguments. Move struct Location into libbpf_android.h to accommodate the change. Change-Id: I39834b2645d38ba4c2eb5ea901a3da0f56a1912c Signed-off-by: Connor O'Brien --- bpfloader/BpfLoader.cpp | 17 +++-------------- libbpf_android/BpfLoadTest.cpp | 12 +++++++++--- libbpf_android/Loader.cpp | 13 +++++++------ libbpf_android/include/libbpf_android.h | 12 +++++++++--- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/bpfloader/BpfLoader.cpp b/bpfloader/BpfLoader.cpp index fd261b5..cdb16f4 100644 --- a/bpfloader/BpfLoader.cpp +++ b/bpfloader/BpfLoader.cpp @@ -118,15 +118,8 @@ constexpr bpf_prog_type kVendorAllowedProgTypes[] = { BPF_PROG_TYPE_SOCKET_FILTER, }; -struct Location { - const char* const dir; - const char* const prefix; - unsigned long long allowedDomainBitmask; - const bpf_prog_type* allowedProgTypes = nullptr; - size_t allowedProgTypesLength = 0; -}; -const Location locations[] = { +const android::bpf::Location locations[] = { // S+ Tethering mainline module (network_stack): tether offload { .dir = "/apex/com.android.tethering/etc/bpf/", @@ -187,7 +180,7 @@ const Location locations[] = { }, }; -int loadAllElfObjects(const Location& location) { +int loadAllElfObjects(const android::bpf::Location& location) { int retVal = 0; DIR* dir; struct dirent* ent; @@ -201,11 +194,7 @@ int loadAllElfObjects(const Location& location) { progPath += s; bool critical; - int ret = android::bpf::loadProg(progPath.c_str(), &critical, - location.prefix, - location.allowedDomainBitmask, - location.allowedProgTypes, - location.allowedProgTypesLength); + int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); if (ret) { if (critical) retVal = ret; ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); diff --git a/libbpf_android/BpfLoadTest.cpp b/libbpf_android/BpfLoadTest.cpp index 8e853b9..0c4e6ee 100644 --- a/libbpf_android/BpfLoadTest.cpp +++ b/libbpf_android/BpfLoadTest.cpp @@ -53,9 +53,15 @@ class BpfLoadTest : public TestWithParam { bpf_prog_type kAllowed[] = { BPF_PROG_TYPE_UNSPEC, }; - EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, "", 0, kAllowed, - arraysize(kAllowed)), - -1); + + Location loc = { + .dir = "", + .prefix = "", + .allowedDomainBitmask = 0, + .allowedProgTypes = kAllowed, + .allowedProgTypesLength = arraysize(kAllowed), + }; + EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1); EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0); EXPECT_EQ(false, critical); diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index bc7ea76..5fe2e10 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -1137,9 +1137,7 @@ static int loadCodeSections(const char* elfPath, vector& cs, const return 0; } -int loadProg(const char* elfPath, bool* isCritical, const char* prefix, - const unsigned long long allowedDomainBitmask, const bpf_prog_type* allowed, - size_t numAllowed) { +int loadProg(const char* elfPath, bool* isCritical, const Location& location) { vector license; vector critical; vector cs; @@ -1212,7 +1210,8 @@ int loadProg(const char* elfPath, bool* isCritical, const char* prefix, return -1; } - ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef, allowed, numAllowed); + ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef, location.allowedProgTypes, + location.allowedProgTypesLength); if (ret) { ALOGE("Couldn't read all code sections in %s", elfPath); return ret; @@ -1221,7 +1220,8 @@ int loadProg(const char* elfPath, bool* isCritical, const char* prefix, /* Just for future debugging */ if (0) dumpAllCs(cs); - ret = createMaps(elfPath, elfFile, mapFds, prefix, allowedDomainBitmask, sizeOfBpfMapDef); + ret = createMaps(elfPath, elfFile, mapFds, location.prefix, location.allowedDomainBitmask, + sizeOfBpfMapDef); if (ret) { ALOGE("Failed to create maps: (ret=%d) in %s", ret, elfPath); return ret; @@ -1232,7 +1232,8 @@ int loadProg(const char* elfPath, bool* isCritical, const char* prefix, applyMapRelo(elfFile, mapFds, cs); - ret = loadCodeSections(elfPath, cs, string(license.data()), prefix, allowedDomainBitmask); + ret = loadCodeSections(elfPath, cs, string(license.data()), location.prefix, + location.allowedDomainBitmask); if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d", ret); return ret; diff --git a/libbpf_android/include/libbpf_android.h b/libbpf_android/include/libbpf_android.h index 2218215..1856798 100644 --- a/libbpf_android/include/libbpf_android.h +++ b/libbpf_android/include/libbpf_android.h @@ -76,10 +76,16 @@ static constexpr bool inDomainBitmask(domain d, unsigned long long v) { return domainToBitmask(d) & v; } +struct Location { + const char* const dir = ""; + const char* const prefix = ""; + unsigned long long allowedDomainBitmask = 0; + const bpf_prog_type* allowedProgTypes = nullptr; + size_t allowedProgTypesLength = 0; +}; + // BPF loader implementation. Loads an eBPF ELF object -int loadProg(const char* elfPath, bool* isCritical, const char* prefix = "", - const unsigned long long allowedDomainBitmask = 0, - const bpf_prog_type* allowed = nullptr, size_t numAllowed = 0); +int loadProg(const char* elfPath, bool* isCritical, const Location &location = {}); // Exposed for testing unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal);