From cd5bdf5382a97378bd036ef7841e5aaa3413f827 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Wed, 23 Jul 2025 12:32:08 +0900 Subject: [PATCH] NetBpfLoad: Add method to pin maps loaded by libbpf Bug: 428792764 Flag: EXEMPT no-op Test: TH Change-Id: Ifd66cda409f738f721e370b884931627f70d0db7 --- bpf/loader/NetBpfLoad.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index 319e7a6598..5d26b762ec 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -1483,6 +1483,39 @@ __unused static int prepareLoadProgs(const struct bpf_object* obj, const vector< return 0; } +__unused static int pinMaps(const char* const elfPath, const struct bpf_object* obj, + const vector& md, const vector& mapNames, + const char* const prefix) { + int ret; + string objName = pathToObjName(string(elfPath)); + + for (int i = 0; i < (int)mapNames.size(); i++) { + struct bpf_map* m = bpf_object__find_map_by_name(obj, mapNames[i].c_str()); + if (!m) { + ALOGE("bpf_object does not contain map: %s", mapNames[i].c_str()); + return -1; + } + // This map was skipped + if (!bpf_map__autocreate(m)) continue; + + domain pin_subdir = getDomainFromPinSubdir(md[i].pin_subdir); + if (specified(pin_subdir)) { + ALOGE("map %s pin_subdir [%-32s] -> %d -> '%s'", mapNames[i].c_str(), md[i].pin_subdir, + static_cast(pin_subdir), lookupPinSubdir(pin_subdir)); + return -1; + } + string mapPinLoc = buildMapPinLoc(pin_subdir, prefix, md[i], objName, mapNames[i]); + if (access(mapPinLoc.c_str(), F_OK) == 0) { + ALOGE("Reusing map is not supported: %s", mapNames[i].c_str()); + return -1; + } + + ret = pinMap(bpf_map__fd(m), mapNames[i], md[i], objName, mapPinLoc); + if (ret) return ret; + } + return 0; +} + int loadProg(const char* const elfPath, const unsigned int bpfloader_ver, const char* const prefix) { vector license;