Merge "BpfLoader v0.25 - add support for obj@ver.o" am: d6c6835f27 am: 032c305dd8

Original change: https://android-review.googlesource.com/c/platform/system/bpf/+/2147468

Change-Id: Id9d38b9ed2b5709182262873c914b9268316488e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot 2022-07-07 20:08:25 +00:00 committed by Automerger Merge Worker
commit c8f29c0862

View file

@ -30,9 +30,9 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
// This is BpfLoader v0.24 // This is BpfLoader v0.25
#define BPFLOADER_VERSION_MAJOR 0u #define BPFLOADER_VERSION_MAJOR 0u
#define BPFLOADER_VERSION_MINOR 24u #define BPFLOADER_VERSION_MINOR 25u
#define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR) #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR)
#include "bpf/BpfUtils.h" #include "bpf/BpfUtils.h"
@ -135,15 +135,14 @@ domain getDomainFromPinSubdir(const char s[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE]) {
return domain::unrecognized; return domain::unrecognized;
} }
static string pathToFilename(const string& path, bool noext = false) { static string pathToObjName(const string& path) {
vector<string> spath = android::base::Split(path, "/"); // extract everything after the final slash, ie. this is the filename 'foo@1.o' or 'bar.o'
string ret = spath.back(); string filename = android::base::Split(path, "/").back();
// strip off everything from the final period onwards (strip '.o' suffix), ie. 'foo@1' or 'bar'
if (noext) { string name = filename.substr(0, filename.find_last_of('.'));
size_t lastindex = ret.find_last_of('.'); // strip any potential @1 suffix, this will leave us with just 'foo' or 'bar'
return ret.substr(0, lastindex); // this can be used to provide duplicate programs (mux based on the bpfloader version)
} return name.substr(0, name.find_last_of('@'));
return ret;
} }
typedef struct { typedef struct {
@ -695,7 +694,7 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>&
vector<struct bpf_map_def> md; vector<struct bpf_map_def> md;
vector<string> mapNames; vector<string> mapNames;
std::unordered_map<string, std::pair<uint32_t, uint32_t>> btfTypeIdMap; std::unordered_map<string, std::pair<uint32_t, uint32_t>> btfTypeIdMap;
string fname = pathToFilename(string(elfPath), true); string objName = pathToObjName(string(elfPath));
ret = readSectionByName("maps", elfFile, mdData); ret = readSectionByName("maps", elfFile, mdData);
if (ret == -2) return 0; // no maps to read if (ret == -2) return 0; // no maps to read
@ -812,11 +811,11 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>&
pin_subdir, lookupPinSubdir(pin_subdir)); pin_subdir, lookupPinSubdir(pin_subdir));
} }
// Format of pin location is /sys/fs/bpf/<pin_subdir|prefix>map_<filename>_<mapname> // Format of pin location is /sys/fs/bpf/<pin_subdir|prefix>map_<objName>_<mapName>
// except that maps shared across .o's have empty <filename> // except that maps shared across .o's have empty <objName>
// Note: <filename> refers to the extension-less basename of the .o file. // Note: <objName> refers to the extension-less basename of the .o file (without @ suffix).
string mapPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "map_" + string mapPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "map_" +
(md[i].shared ? "" : fname) + "_" + mapNames[i]; (md[i].shared ? "" : objName) + "_" + mapNames[i];
bool reuse = false; bool reuse = false;
unique_fd fd; unique_fd fd;
int saved_errno; int saved_errno;
@ -855,7 +854,7 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>&
if (!reuse) { if (!reuse) {
if (specified(selinux_context)) { if (specified(selinux_context)) {
string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) + string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
"tmp_map_" + fname + "_" + mapNames[i]; "tmp_map_" + objName + "_" + mapNames[i];
ret = bpf_obj_pin(fd, createLoc.c_str()); ret = bpf_obj_pin(fd, createLoc.c_str());
if (ret) { if (ret) {
int err = errno; int err = errno;
@ -987,7 +986,7 @@ static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const
if (!kvers) return -1; if (!kvers) return -1;
string fname = pathToFilename(string(elfPath), true); string objName = pathToObjName(string(elfPath));
for (int i = 0; i < (int)cs.size(); i++) { for (int i = 0; i < (int)cs.size(); i++) {
string name = cs[i].name; string name = cs[i].name;
@ -1046,9 +1045,9 @@ static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const
bool reuse = false; bool reuse = false;
// Format of pin location is // Format of pin location is
// /sys/fs/bpf/<prefix>prog_<filename>_<mapname> // /sys/fs/bpf/<prefix>prog_<objName>_<progName>
string progPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "prog_" + string progPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "prog_" +
fname + '_' + string(name); objName + '_' + string(name);
if (access(progPinLoc.c_str(), F_OK) == 0) { if (access(progPinLoc.c_str(), F_OK) == 0) {
fd = retrieveProgram(progPinLoc.c_str()); fd = retrieveProgram(progPinLoc.c_str());
ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)", progPinLoc.c_str(), fd, ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)", progPinLoc.c_str(), fd,
@ -1093,7 +1092,7 @@ static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const
if (!reuse) { if (!reuse) {
if (specified(selinux_context)) { if (specified(selinux_context)) {
string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) + string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
"tmp_prog_" + fname + '_' + string(name); "tmp_prog_" + objName + '_' + string(name);
ret = bpf_obj_pin(fd, createLoc.c_str()); ret = bpf_obj_pin(fd, createLoc.c_str());
if (ret) { if (ret) {
int err = errno; int err = errno;