From 52108bf52ca39f20c50078fdfde5eec5ceac8476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 1 Apr 2019 10:41:13 -0700 Subject: [PATCH] BpfMap.getOrCreate(size, path, type) -> BpfMap.init(path) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don't have the appropriate selinux privs to create the maps anyway. Test: builds (when combined with system/netd change) 'git grep getOrCreate' in system/{bpf,netd} comes up empty atest netd_unit_test netd_integration_test libbpf_android_test Bug: 65674744 Bug: 129654883 Signed-off-by: Maciej Żenczykowski Change-Id: Idffbce5b28b723943c6495cfafdecddc7a0f4e67 --- libbpf_android/BpfMapTest.cpp | 2 +- libbpf_android/include/bpf/BpfMap.h | 34 ++++++----------------------- 2 files changed, 8 insertions(+), 28 deletions(-) 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; }