BpfMap.getOrCreate(size, path, type) -> BpfMap.init(path)

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 <maze@google.com>
Change-Id: Idffbce5b28b723943c6495cfafdecddc7a0f4e67
This commit is contained in:
Maciej Żenczykowski 2019-04-01 10:41:13 -07:00
parent 04d88b7576
commit 52108bf52c
2 changed files with 8 additions and 28 deletions

View file

@ -174,7 +174,7 @@ TEST_F(BpfMapTest, SetUpMap) {
EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
checkMapValid(testMap1);
BpfMap<uint32_t, uint32_t> 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;

View file

@ -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 <class Key, class Value>
netdutils::Status BpfMap<Key, Value>::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<Key, Value>::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;
}