From aa295c835574952ff91f2c60183e5357d2664e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Tue, 16 Jun 2020 17:02:48 -0700 Subject: implement support for functions which may optionally fail to load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is useful for critical functions with fallbacks, but may even be useful for non-critical functions, where a function in the middle of the file may fail to load, but you still want other (later) functions to be attempted. Critical applies to the entire .c file (or to be more correct to the entire resulting .o). Optional applies to a specific section of that .o (ie. a specific individual function). This new optional attribute is necessary to be able to declare a .c/.o file critical even if *some* of the individual functions might fail to load due to missing kernel patches. (Note: we currently have no way to specify a map as optional) Critical guarantees that all non-optional programs, and all maps, have been created, pinned, chowned, and chmoded successfully (or that they already existed). For an example of use see: system/netd/bpf_progs/offload.c (while at it also add retrieveProgram() and mapRetrieve{RW,RO,WO}() helpers to BpfUtils.h) Test: builds, atest, see paired netd change for extra details Bug: 150040815 Signed-off-by: Maciej Żenczykowski Change-Id: I50b292c061b05fc8f4b4b8574f128345c45c78db --- libbpf_android/Loader.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'libbpf_android/Loader.cpp') diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index 8f5318d..5a65c1b 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -564,8 +564,9 @@ static int loadCodeSections(const char* elfPath, vector& cs, const progPinLoc += '_'; progPinLoc += name; if (access(progPinLoc.c_str(), F_OK) == 0) { - fd = bpf_obj_get(progPinLoc.c_str()); - ALOGD("New bpf prog load reusing prog %s, ret: %d\n", progPinLoc.c_str(), fd); + fd = retrieveProgram(progPinLoc.c_str()); + ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)\n", progPinLoc.c_str(), fd, + (fd < 0 ? std::strerror(errno) : "no error")); reuse = true; } else { vector log_buf(BPF_LOAD_LOG_SZ, 0); @@ -579,9 +580,15 @@ static int loadCodeSections(const char* elfPath, vector& cs, const if (fd < 0) { std::vector lines = android::base::Split(log_buf.data(), "\n"); - ALOGE("bpf_prog_load - BEGIN log_buf contents:"); - for (const auto& line : lines) ALOGE("%s", line.c_str()); - ALOGE("bpf_prog_load - END log_buf contents."); + ALOGW("bpf_prog_load - BEGIN log_buf contents:"); + for (const auto& line : lines) ALOGW("%s", line.c_str()); + ALOGW("bpf_prog_load - END log_buf contents."); + + if (cs[i].prog_def->optional) { + ALOGW("failed program is marked optional - continuing..."); + continue; + } + ALOGE("non-optional program failed to load."); } } -- cgit v1.2.3