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 <connoro@google.com>
This commit is contained in:
Connor O'Brien 2022-12-01 20:08:17 -08:00
parent 90a866ed34
commit 6c0ce9f17a
4 changed files with 28 additions and 26 deletions

View file

@ -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));

View file

@ -53,9 +53,15 @@ class BpfLoadTest : public TestWithParam<std::string> {
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);

View file

@ -1137,9 +1137,7 @@ static int loadCodeSections(const char* elfPath, vector<codeSection>& 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<char> license;
vector<char> critical;
vector<codeSection> 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;

View file

@ -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);