diff options
| author | Maciej Żenczykowski <maze@google.com> | 2021-07-06 12:38:26 +0000 |
|---|---|---|
| committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-07-06 12:38:26 +0000 |
| commit | 98a3ee88e227de9c86785849e250084bfa08c642 (patch) | |
| tree | 3a8021a617e59df89614369eed5ea479ae040794 | |
| parent | e59c966e12de5d869cc853920607017244bfd2b7 (diff) | |
| parent | 2a51516a63c31076988e81a4126585bfd80b7eb4 (diff) | |
bpf - struct bpf_map_def - add min/max kernel version. am: 2a51516a63
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/bpf/+/15212803
Change-Id: Ifb8ba64bf84680d79aad6d8cdd5fface4671d2e1
| -rw-r--r-- | libbpf_android/Loader.cpp | 36 | ||||
| -rw-r--r-- | progs/include/bpf_helpers.h | 10 | ||||
| -rw-r--r-- | progs/include/bpf_map_def.h | 9 |
3 files changed, 40 insertions, 15 deletions
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index b1af34f..aa1f3c0 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -28,10 +28,9 @@ #include <sys/utsname.h> #include <unistd.h> -// This is BpfLoader 0.1, we need to define this prior to including bpf_map_def.h -// to get the 0.1 struct definitions +// This is BpfLoader v0.2 #define BPFLOADER_VERSION_MAJOR 0u -#define BPFLOADER_VERSION_MINOR 1u +#define BPFLOADER_VERSION_MINOR 2u #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR) #include "../progs/include/bpf_map_def.h" @@ -494,6 +493,7 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& memset(&m, 0, sizeof(m)); // Then we set non-zero defaults m.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0 + m.max_kver = 0xFFFFFFFFu; // matches KVER_INF from bpf_helpers.h // Then we copy over the structure prefix from the ELF file. memcpy(&m, dataPtr, trimmedSize); // Move to next struct in the ELF file @@ -503,14 +503,9 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& ret = getSectionSymNames(elfFile, "maps", mapNames); if (ret) return ret; - for (int i = 0; i < (int)mapNames.size(); i++) { - unique_fd fd; - int saved_errno; - // Format of pin location is /sys/fs/bpf/<prefix>map_<filename>_<mapname> - string mapPinLoc = - string(BPF_FS_PATH) + prefix + "map_" + fname + "_" + string(mapNames[i]); - bool reuse = false; + unsigned kvers = kernelVersion(); + for (int i = 0; i < (int)mapNames.size(); i++) { if (BPFLOADER_VERSION < md[i].bpfloader_min_ver) { ALOGI("skipping map %s which requires bpfloader min ver 0x%05x\n", mapNames[i].c_str(), md[i].bpfloader_min_ver); @@ -525,6 +520,27 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& continue; } + if (kvers < md[i].min_kver) { + ALOGI("skipping map %s which requires kernel version 0x%x >= 0x%x\n", + mapNames[i].c_str(), kvers, md[i].min_kver); + mapFds.push_back(unique_fd()); + continue; + } + + if (kvers >= md[i].max_kver) { + ALOGI("skipping map %s which requires kernel version 0x%x < 0x%x\n", + mapNames[i].c_str(), kvers, md[i].max_kver); + mapFds.push_back(unique_fd()); + continue; + } + + // Format of pin location is /sys/fs/bpf/<prefix>map_<filename>_<mapname> + string mapPinLoc = + string(BPF_FS_PATH) + prefix + "map_" + fname + "_" + string(mapNames[i]); + bool reuse = false; + unique_fd fd; + int saved_errno; + if (access(mapPinLoc.c_str(), F_OK) == 0) { fd.reset(bpf_obj_get(mapPinLoc.c_str())); saved_errno = errno; diff --git a/progs/include/bpf_helpers.h b/progs/include/bpf_helpers.h index 6e5fe69..abd19c6 100644 --- a/progs/include/bpf_helpers.h +++ b/progs/include/bpf_helpers.h @@ -59,6 +59,10 @@ * implemented in the kernel sources. */ +#define KVER_NONE 0 +#define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c)) +#define KVER_INF 0xFFFFFFFFu + /* generic functions */ /* @@ -110,6 +114,8 @@ static int (*bpf_map_delete_elem_unsafe)(const struct bpf_map_def* map, .mode = (md), \ .bpfloader_min_ver = DEFAULT_BPFLOADER_MIN_VER, \ .bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER, \ + .min_kver = KVER_NONE, \ + .max_kver = KVER_INF, \ }; \ \ static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \ @@ -147,10 +153,6 @@ static unsigned long long (*bpf_get_current_pid_tgid)(void) = (void*) BPF_FUNC_g static unsigned long long (*bpf_get_current_uid_gid)(void) = (void*) BPF_FUNC_get_current_uid_gid; static unsigned long long (*bpf_get_smp_processor_id)(void) = (void*) BPF_FUNC_get_smp_processor_id; -#define KVER_NONE 0 -#define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c)) -#define KVER_INF 0xFFFFFFFF - #define DEFINE_BPF_PROG_KVER_RANGE_OPT(SECTION_NAME, prog_uid, prog_gid, the_prog, min_kv, max_kv, \ opt) \ const struct bpf_prog_def SEC("progs") the_prog##_def = { \ diff --git a/progs/include/bpf_map_def.h b/progs/include/bpf_map_def.h index f51b1c4..647c813 100644 --- a/progs/include/bpf_map_def.h +++ b/progs/include/bpf_map_def.h @@ -141,10 +141,15 @@ struct bpf_map_def { // The following fields were added in version 0.1 unsigned int bpfloader_min_ver; // if missing, defaults to 0, ie. v0.0 unsigned int bpfloader_max_ver; // if missing, defaults to 0x10000, ie. v1.0 + + // The following fields were added in version 0.2 + // kernelVersion() must be >= min_kver and < max_kver + unsigned int min_kver; + unsigned int max_kver; }; // This needs to be updated whenever the above structure definition is expanded. -_Static_assert(sizeof(struct bpf_map_def) == 40, "sizeof struct bpf_map_def != 40"); +_Static_assert(sizeof(struct bpf_map_def) == 48, "sizeof struct bpf_map_def != 48"); _Static_assert(__alignof__(struct bpf_map_def) == 4, "__alignof__ struct bpf_map_def != 4"); _Static_assert(_Alignof(struct bpf_map_def) == 4, "_Alignof struct bpf_map_def != 4"); @@ -162,6 +167,8 @@ struct bpf_prog_def { // The following fields were added in version 0.1 unsigned int bpfloader_min_ver; // if missing, defaults to 0, ie. v0.0 unsigned int bpfloader_max_ver; // if missing, defaults to 0x10000, ie. v1.0 + + // No new fields in version 0.2 }; // This needs to be updated whenever the above structure definition is expanded. |
