diff --git a/libbpf_android/BpfMapTest.cpp b/libbpf_android/BpfMapTest.cpp index 74fc2b8..cfa9d88 100644 --- a/libbpf_android/BpfMapTest.cpp +++ b/libbpf_android/BpfMapTest.cpp @@ -174,7 +174,7 @@ TEST_F(BpfMapTest, SetUpMap) { EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK)); checkMapValid(testMap1); BpfMap testMap2; - EXPECT_OK(testMap2.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH)); + EXPECT_OK(testMap2.init(PINNED_MAP_PATH)); checkMapValid(testMap2); uint32_t key = TEST_KEY1; uint32_t value = TEST_VALUE1; diff --git a/libbpf_android/include/bpf/BpfMap.h b/libbpf_android/include/bpf/BpfMap.h index a274b01..d47698e 100644 --- a/libbpf_android/include/bpf/BpfMap.h +++ b/libbpf_android/include/bpf/BpfMap.h @@ -100,10 +100,8 @@ class BpfMap { return netdutils::status::ok; } - // Function that tries to get map from a pinned path, if the map doesn't - // exist yet, create a new one and pinned to the path. - netdutils::Status getOrCreate(const uint32_t maxEntries, const char* path, - const bpf_map_type mapType); + // Function that tries to get map from a pinned path. + netdutils::Status init(const char* path); // Iterate through the map and handle each key retrieved based on the filter // without modification of map content. @@ -168,31 +166,13 @@ class BpfMap { }; template -netdutils::Status BpfMap::getOrCreate(const uint32_t maxEntries, const char* path, - bpf_map_type mapType) { - int ret = access(path, R_OK); - /* Check the pinned location first to check if the map is already there. - * otherwise create a new one. - */ - if (ret == 0) { - mMapFd = base::unique_fd(mapRetrieve(path, 0)); - if (mMapFd == -1) { - reset(); - return netdutils::statusFromErrno( +netdutils::Status BpfMap::init(const char* path) { + mMapFd = base::unique_fd(mapRetrieve(path, 0)); + if (mMapFd == -1) { + reset(); + return netdutils::statusFromErrno( errno, base::StringPrintf("pinned map not accessible or does not exist: (%s)\n", path)); - } - } else if (ret == -1 && errno == ENOENT) { - mMapFd = base::unique_fd( - createMap(mapType, sizeof(Key), sizeof(Value), maxEntries, BPF_F_NO_PREALLOC)); - if (mMapFd == -1) { - reset(); - return netdutils::statusFromErrno(errno, - base::StringPrintf("map create failed!: %s", path)); - } - } else { - return netdutils::statusFromErrno( - errno, base::StringPrintf("pinned map not accessible: %s", path)); } return netdutils::status::ok; }