Merge "bpfloader: refactor C++ loader to just one function" into main

This commit is contained in:
Treehugger Robot 2025-06-17 05:56:59 -07:00 • committed by Android (Google) Code Review
commit 32075d5ef9
3 changed files with 7 additions and 19 deletions

View file

@ -936,23 +936,19 @@ int createSysFsBpfSubDir(const char* const prefix) {
// ----- extern C stuff for rust below here -----
void initLogging() {
void vendorBpfLoader() {
// since we only ever get called from mainline NetBpfLoad
// (see packages/modules/Connectivity/netbpfload/NetBpfLoad.cpp around line 516)
// and there no arguments, so we can just pretend/assume this is the case.
const char* argv[] = {"/system/bin/bpfloader", NULL};
android::base::InitLogging(const_cast<char**>(argv), &android::base::KernelLogger);
}
void createBpfFsSubDirectories() {
for (const auto& location : android::bpf::locations) {
if (android::bpf::createSysFsBpfSubDir(location.prefix)) {
exit(120);
}
}
}
void legacyBpfLoader() {
// Load all ELF objects, create programs and maps, and pin them
for (const auto& location : android::bpf::locations) {
if (android::bpf::loadAllElfObjects(location)) {
@ -965,9 +961,7 @@ void legacyBpfLoader() {
exit(121);
}
}
}
void execNetBpfLoadDone() {
const char* args[] = {"/apex/com.android.tethering/bin/netbpfload", "done", NULL};
execve(args[0], (char**)args, environ);
ALOGE("FATAL: execve(): %d[%s]", errno, strerror(errno));

View file

@ -640,14 +640,11 @@ fn main() {
load_libbpf_progs();
info!("Loading legacy BPF progs");
// SAFETY: Linking in the existing legacy bpfloader functionality.
// Any of the four following bindgen functions can abort() or exit()
// on failure and execNetBpfLoadDone() execve()'s.
// SAFETY: Linking in the existing legacy vendor bpfloader functionality.
// The following bindgen function can abort() or exit() on failure,
// but will usually execve().
unsafe {
bpf_android_bindgen::initLogging();
bpf_android_bindgen::createBpfFsSubDirectories();
bpf_android_bindgen::legacyBpfLoader();
bpf_android_bindgen::execNetBpfLoadDone();
bpf_android_bindgen::vendorBpfLoader();
}
}

View file

@ -47,11 +47,8 @@ extern "C" {
#define __noreturn
#endif // __cplusplus
// The C++ portion of the BpfLoader is exposed as 3 functions to be called in order.
void initLogging();
void createBpfFsSubDirectories();
void legacyBpfLoader();
__noreturn void execNetBpfLoadDone();
// The C++ portion of the BpfLoader is exposed as a terminal function.
__noreturn void vendorBpfLoader();
#ifdef __cplusplus
} // extern C