diff --git a/libbpf_android/Android.bp b/libbpf_android/Android.bp index 8c69b24..cd2342e 100644 --- a/libbpf_android/Android.bp +++ b/libbpf_android/Android.bp @@ -47,7 +47,6 @@ cc_library { "libutils", "libprocessgroup", "liblog", - "libnetdutils", "libbpf", ], header_libs: [ @@ -81,7 +80,6 @@ cc_test { "libbpf_android", "libbase", "liblog", - "libnetdutils", "libutils", ], require_root: true, @@ -105,7 +103,6 @@ cc_test { "libbpf", "libbase", "liblog", - "libnetdutils", "libutils", ], diff --git a/libbpf_android/BpfLoadTest.cpp b/libbpf_android/BpfLoadTest.cpp index 6d637e9..d8d6da6 100644 --- a/libbpf_android/BpfLoadTest.cpp +++ b/libbpf_android/BpfLoadTest.cpp @@ -81,10 +81,10 @@ class BpfLoadTest : public testing::Test { UNUSED(key); UNUSED(map); - return android::netdutils::status::ok; + return base::Result(); }; - EXPECT_OK(m.iterateWithValue(iterFunc)); + EXPECT_TRUE(m.iterateWithValue(iterFunc)); EXPECT_EQ(non_zero, 1); } }; diff --git a/libbpf_android/BpfMapTest.cpp b/libbpf_android/BpfMapTest.cpp index 94d273e..445aa6a 100644 --- a/libbpf_android/BpfMapTest.cpp +++ b/libbpf_android/BpfMapTest.cpp @@ -41,8 +41,8 @@ using ::testing::Test; namespace android { namespace bpf { +using base::Result; using base::unique_fd; -using netdutils::StatusOr; constexpr uint32_t TEST_MAP_SIZE = 10; constexpr uint32_t TEST_KEY1 = 1; @@ -93,27 +93,27 @@ class BpfMapTest : public testing::Test { } void writeToMapAndCheck(BpfMap& map, uint32_t key, uint32_t value) { - ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY))); + ASSERT_TRUE(map.writeValue(key, value, BPF_ANY)); uint32_t value_read; ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read)); checkValueAndStatus(value, value_read); } - void checkValueAndStatus(uint32_t refValue, StatusOr value) { - ASSERT_TRUE(isOk(value.status())); + void checkValueAndStatus(uint32_t refValue, Result value) { + ASSERT_TRUE(value); ASSERT_EQ(refValue, value.value()); } void populateMap(uint32_t total, BpfMap& map) { for (uint32_t key = 0; key < total; key++) { uint32_t value = key * 10; - EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY))); + EXPECT_TRUE(map.writeValue(key, value, BPF_ANY)); } } void expectMapEmpty(BpfMap& map) { - auto isEmpty = map.isEmpty(); - ASSERT_TRUE(isOk(isEmpty)); + Result isEmpty = map.isEmpty(); + ASSERT_TRUE(isEmpty); ASSERT_TRUE(isEmpty.value()); } }; @@ -138,11 +138,11 @@ TEST_F(BpfMapTest, basicHelpers) { uint32_t key = TEST_KEY1; uint32_t value_write = TEST_VALUE1; writeToMapAndCheck(testMap, key, value_write); - StatusOr value_read = testMap.readValue(key); + Result value_read = testMap.readValue(key); checkValueAndStatus(value_write, value_read); - StatusOr key_read = testMap.getFirstKey(); + Result key_read = testMap.getFirstKey(); checkValueAndStatus(key, key_read); - ASSERT_TRUE(isOk(testMap.deleteValue(key))); + ASSERT_TRUE(testMap.deleteValue(key)); ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read)); ASSERT_EQ(ENOENT, errno); } @@ -183,12 +183,12 @@ TEST_F(BpfMapTest, SetUpMap) { EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK)); checkMapValid(testMap1); BpfMap testMap2; - EXPECT_OK(testMap2.init(PINNED_MAP_PATH)); + EXPECT_TRUE(testMap2.init(PINNED_MAP_PATH)); checkMapValid(testMap2); uint32_t key = TEST_KEY1; uint32_t value = TEST_VALUE1; writeToMapAndCheck(testMap1, key, value); - StatusOr value_read = testMap2.readValue(key); + Result value_read = testMap2.readValue(key); checkValueAndStatus(value, value_read); } @@ -206,7 +206,7 @@ TEST_F(BpfMapTest, iterate) { totalSum += key; return map.deleteValue(key); }; - EXPECT_OK(testMap.iterate(iterateWithDeletion)); + EXPECT_TRUE(testMap.iterate(iterateWithDeletion)); EXPECT_EQ((int)TEST_MAP_SIZE, totalCount); EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum); expectMapEmpty(testMap); @@ -228,7 +228,7 @@ TEST_F(BpfMapTest, iterateWithValue) { totalSum += value; return map.deleteValue(key); }; - EXPECT_OK(testMap.iterateWithValue(iterateWithDeletion)); + EXPECT_TRUE(testMap.iterateWithValue(iterateWithDeletion)); EXPECT_EQ((int)TEST_MAP_SIZE, totalCount); EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum); expectMapEmpty(testMap); @@ -242,27 +242,27 @@ TEST_F(BpfMapTest, mapIsEmpty) { uint32_t key = TEST_KEY1; uint32_t value_write = TEST_VALUE1; writeToMapAndCheck(testMap, key, value_write); - auto isEmpty = testMap.isEmpty(); - ASSERT_TRUE(isOk(isEmpty)); + Result isEmpty = testMap.isEmpty(); + ASSERT_TRUE(isEmpty); ASSERT_FALSE(isEmpty.value()); - ASSERT_TRUE(isOk(testMap.deleteValue(key))); + ASSERT_TRUE(testMap.deleteValue(key)); ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write)); ASSERT_EQ(ENOENT, errno); expectMapEmpty(testMap); int entriesSeen = 0; - EXPECT_OK(testMap.iterate( - [&entriesSeen](const unsigned int&, - const BpfMap&) -> netdutils::Status { - entriesSeen++; - return netdutils::status::ok; - })); + EXPECT_TRUE(testMap.iterate( + [&entriesSeen](const unsigned int&, + const BpfMap&) -> Result { + entriesSeen++; + return {}; + })); EXPECT_EQ(0, entriesSeen); - EXPECT_OK(testMap.iterateWithValue( - [&entriesSeen](const unsigned int&, const unsigned int&, - const BpfMap&) -> netdutils::Status { - entriesSeen++; - return netdutils::status::ok; - })); + EXPECT_TRUE(testMap.iterateWithValue( + [&entriesSeen](const unsigned int&, const unsigned int&, + const BpfMap&) -> Result { + entriesSeen++; + return {}; + })); EXPECT_EQ(0, entriesSeen); } @@ -271,10 +271,10 @@ TEST_F(BpfMapTest, mapClear) { BpfMap testMap(mMapFd.release()); populateMap(TEST_MAP_SIZE, testMap); - auto isEmpty = testMap.isEmpty(); - ASSERT_TRUE(isOk(isEmpty)); - ASSERT_FALSE(isEmpty.value()); - ASSERT_TRUE(isOk(testMap.clear())); + Result isEmpty = testMap.isEmpty(); + ASSERT_TRUE(isEmpty); + ASSERT_FALSE(*isEmpty); + ASSERT_TRUE(testMap.clear()); expectMapEmpty(testMap); } diff --git a/libbpf_android/include/bpf/BpfMap.h b/libbpf_android/include/bpf/BpfMap.h index 80ae62b..31efd58 100644 --- a/libbpf_android/include/bpf/BpfMap.h +++ b/libbpf_android/include/bpf/BpfMap.h @@ -19,12 +19,11 @@ #include +#include #include #include #include #include "bpf/BpfUtils.h" -#include "netdutils/Status.h" -#include "netdutils/StatusOr.h" namespace android { namespace bpf { @@ -68,73 +67,69 @@ class BpfMap { } } - netdutils::StatusOr getFirstKey() const { + base::Result getFirstKey() const { Key firstKey; if (getFirstMapKey(mMapFd, &firstKey)) { - return netdutils::statusFromErrno( - errno, base::StringPrintf("Get firstKey map %d failed", mMapFd.get())); + return base::ErrnoErrorf("Get firstKey map {} failed", mMapFd.get()); } return firstKey; } - netdutils::StatusOr getNextKey(const Key& key) const { + base::Result getNextKey(const Key& key) const { Key nextKey; if (getNextMapKey(mMapFd, &key, &nextKey)) { - return netdutils::statusFromErrno( - errno, base::StringPrintf("Get next key of map %d failed", mMapFd.get())); + return base::ErrnoErrorf("Get next key of map {} failed", mMapFd.get()); } return nextKey; } - netdutils::Status writeValue(const Key& key, const Value& value, uint64_t flags) { + base::Result writeValue(const Key& key, const Value& value, uint64_t flags) { if (writeToMapEntry(mMapFd, &key, &value, flags)) { - return netdutils::statusFromErrno( - errno, base::StringPrintf("write to map %d failed", mMapFd.get())); + return base::ErrnoErrorf("Write to map {} failed", mMapFd.get()); } - return netdutils::status::ok; + return {}; } - netdutils::StatusOr readValue(const Key key) const { + base::Result readValue(const Key key) const { Value value; if (findMapEntry(mMapFd, &key, &value)) { - return netdutils::statusFromErrno( - errno, base::StringPrintf("read value of map %d failed", mMapFd.get())); + return base::ErrnoErrorf("Read value of map {} failed", mMapFd.get()); } return value; } - netdutils::Status deleteValue(const Key& key) { + base::Result deleteValue(const Key& key) { if (deleteMapEntry(mMapFd, &key)) { - return netdutils::statusFromErrno( - errno, base::StringPrintf("delete entry from map %d failed", mMapFd.get())); + return base::ErrnoErrorf("Delete entry from map {} failed", mMapFd.get()); } - return netdutils::status::ok; + return {}; } // Function that tries to get map from a pinned path. - netdutils::Status init(const char* path); + base::Result init(const char* path); // Iterate through the map and handle each key retrieved based on the filter // without modification of map content. - netdutils::Status iterate( - const std::function& map)>& - filter) const; + base::Result iterate( + const std::function(const Key& key, const BpfMap& map)>& + filter) const; // Iterate through the map and get each pair, handle each pair based on the filter without modification of map content. - netdutils::Status iterateWithValue( - const std::function& map)>& filter) const; + base::Result iterateWithValue( + const std::function(const Key& key, const Value& value, + const BpfMap& map)>& filter) const; // Iterate through the map and handle each key retrieved based on the filter - netdutils::Status iterate( - const std::function& map)>& filter); + base::Result iterate( + const std::function(const Key& key, BpfMap& map)>& + filter); // Iterate through the map and get each pair, handle each pair based on the filter. - netdutils::Status iterateWithValue( - const std::function& map)>& filter); + base::Result iterateWithValue( + const std::function(const Key& key, const Value& value, + BpfMap& map)>& filter); const base::unique_fd& getMap() const { return mMapFd; }; @@ -152,23 +147,25 @@ class BpfMap { // It is only safe to call this method if it is guaranteed that nothing will concurrently // iterate over the map in any process. - netdutils::Status clear() { - const auto deleteAllEntries = [](const Key& key, BpfMap& map) { - netdutils::Status res = map.deleteValue(key); - if (!isOk(res) && (res.code() != ENOENT)) { - ALOGE("Failed to delete data %s\n", strerror(res.code())); + base::Result clear() { + const auto deleteAllEntries = [](const Key& key, + BpfMap& map) -> base::Result { + base::Result res = map.deleteValue(key); + if (!res && res.error().code() != ENOENT) { + ALOGE("Failed to delete data %s", strerror(res.error().code())); } - return netdutils::status::ok; + return {}; }; - RETURN_IF_NOT_OK(iterate(deleteAllEntries)); - return netdutils::status::ok; + return iterate(deleteAllEntries); } - netdutils::StatusOr isEmpty() const { + base::Result isEmpty() const { auto key = this->getFirstKey(); - // Return error code ENOENT means the map is empty - if (!isOk(key) && key.status().code() == ENOENT) return true; - RETURN_IF_NOT_OK(key); + if (!key) { + // Return error code ENOENT means the map is empty + if (key.error().code() == ENOENT) return true; + return key.error(); + } return false; } @@ -177,70 +174,76 @@ class BpfMap { }; template -netdutils::Status BpfMap::init(const char* path) { +base::Result 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)); + return base::ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); } - return netdutils::status::ok; + return {}; } template -netdutils::Status BpfMap::iterate( - const std::function& map)>& filter) - const { - netdutils::StatusOr curKey = getFirstKey(); - while (isOk(curKey)) { - const netdutils::StatusOr& nextKey = getNextKey(curKey.value()); - RETURN_IF_NOT_OK(filter(curKey.value(), *this)); +base::Result BpfMap::iterate( + const std::function(const Key& key, const BpfMap& map)>& + filter) const { + base::Result curKey = getFirstKey(); + while (curKey) { + const base::Result& nextKey = getNextKey(curKey.value()); + base::Result status = filter(curKey.value(), *this); + if (!status) return status; curKey = nextKey; } - return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status(); + if (curKey.error().code() == ENOENT) return {}; + return curKey.error(); } template -netdutils::Status BpfMap::iterateWithValue( - const std::function& map)>& filter) const { - netdutils::StatusOr curKey = getFirstKey(); - while (isOk(curKey)) { - const netdutils::StatusOr& nextKey = getNextKey(curKey.value()); - Value curValue; - ASSIGN_OR_RETURN(curValue, this->readValue(curKey.value())); - RETURN_IF_NOT_OK(filter(curKey.value(), curValue, *this)); +base::Result BpfMap::iterateWithValue( + const std::function(const Key& key, const Value& value, + const BpfMap& map)>& filter) const { + base::Result curKey = getFirstKey(); + while (curKey) { + const base::Result& nextKey = getNextKey(curKey.value()); + base::Result curValue = this->readValue(curKey.value()); + if (!curValue) return curValue.error(); + base::Result status = filter(curKey.value(), curValue.value(), *this); + if (!status) return status; curKey = nextKey; } - return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status(); + if (curKey.error().code() == ENOENT) return {}; + return curKey.error(); } template -netdutils::Status BpfMap::iterate( - const std::function& map)>& filter) { - netdutils::StatusOr curKey = getFirstKey(); - while (isOk(curKey)) { - const netdutils::StatusOr& nextKey = getNextKey(curKey.value()); - RETURN_IF_NOT_OK(filter(curKey.value(), *this)); +base::Result BpfMap::iterate( + const std::function(const Key& key, BpfMap& map)>& filter) { + base::Result curKey = getFirstKey(); + while (curKey) { + const base::Result& nextKey = getNextKey(curKey.value()); + base::Result status = filter(curKey.value(), *this); + if (!status) return status; curKey = nextKey; } - return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status(); + if (curKey.error().code() == ENOENT) return {}; + return curKey.error(); } template -netdutils::Status BpfMap::iterateWithValue( - const std::function& map)>& filter) { - netdutils::StatusOr curKey = getFirstKey(); - while (isOk(curKey)) { - const netdutils::StatusOr& nextKey = getNextKey(curKey.value()); - Value curValue; - ASSIGN_OR_RETURN(curValue, this->readValue(curKey.value())); - RETURN_IF_NOT_OK(filter(curKey.value(), curValue, *this)); +base::Result BpfMap::iterateWithValue( + const std::function(const Key& key, const Value& value, + BpfMap& map)>& filter) { + base::Result curKey = getFirstKey(); + while (curKey) { + const base::Result& nextKey = getNextKey(curKey.value()); + base::Result curValue = this->readValue(curKey.value()); + if (!curValue) return curValue.error(); + base::Result status = filter(curKey.value(), curValue.value(), *this); + if (!status) return status; curKey = nextKey; } - return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status(); + if (curKey.error().code() == ENOENT) return {}; + return curKey.error(); } } // namespace bpf diff --git a/libbpf_android/include/bpf/BpfUtils.h b/libbpf_android/include/bpf/BpfUtils.h index c884867..965840f 100644 --- a/libbpf_android/include/bpf/BpfUtils.h +++ b/libbpf_android/include/bpf/BpfUtils.h @@ -26,9 +26,9 @@ #include #include +#include + #include "android-base/unique_fd.h" -#include "netdutils/Slice.h" -#include "netdutils/StatusOr.h" #define BPF_PASS 1 #define BPF_DROP 0