libbpf_android: remove libnetdutils dep

Remove libnetdutils dependency because bpf is being used for things other
than networking these days, and we don't want to make libnetdutils
vendor-available in the future. libbase provides an alternative type now.

Bug: 140330870
Test: atest libbpf_android_test netd_integration_test netd_unit_test
    libnetdbpf_test bpf_module_test
Change-Id: I72ae8cd7f58a49bfc7dcb914a332a4c4bad5dea5
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 @@
         "libutils",
         "libprocessgroup",
         "liblog",
-        "libnetdutils",
         "libbpf",
     ],
     header_libs: [
@@ -81,7 +80,6 @@
         "libbpf_android",
         "libbase",
         "liblog",
-        "libnetdutils",
         "libutils",
     ],
     require_root: true,
@@ -105,7 +103,6 @@
         "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 @@
 
             UNUSED(key);
             UNUSED(map);
-            return android::netdutils::status::ok;
+            return base::Result<void>();
         };
 
-        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 @@
 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 @@
     }
 
     void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& 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<uint32_t> value) {
-        ASSERT_TRUE(isOk(value.status()));
+    void checkValueAndStatus(uint32_t refValue, Result<uint32_t> value) {
+        ASSERT_TRUE(value);
         ASSERT_EQ(refValue, value.value());
     }
 
     void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& 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<uint32_t, uint32_t>& map) {
-        auto isEmpty = map.isEmpty();
-        ASSERT_TRUE(isOk(isEmpty));
+        Result<bool> isEmpty = map.isEmpty();
+        ASSERT_TRUE(isEmpty);
         ASSERT_TRUE(isEmpty.value());
     }
 };
@@ -138,11 +138,11 @@
     uint32_t key = TEST_KEY1;
     uint32_t value_write = TEST_VALUE1;
     writeToMapAndCheck(testMap, key, value_write);
-    StatusOr<uint32_t> value_read = testMap.readValue(key);
+    Result<uint32_t> value_read = testMap.readValue(key);
     checkValueAndStatus(value_write, value_read);
-    StatusOr<uint32_t> key_read = testMap.getFirstKey();
+    Result<uint32_t> 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 @@
     EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
     checkMapValid(testMap1);
     BpfMap<uint32_t, uint32_t> 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<uint32_t> value_read = testMap2.readValue(key);
+    Result<uint32_t> value_read = testMap2.readValue(key);
     checkValueAndStatus(value, value_read);
 }
 
@@ -206,7 +206,7 @@
         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 @@
         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 @@
     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<bool> 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<unsigned int, unsigned int>&) -> netdutils::Status {
-            entriesSeen++;
-            return netdutils::status::ok;
-        }));
+    EXPECT_TRUE(testMap.iterate(
+            [&entriesSeen](const unsigned int&,
+                           const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
+                entriesSeen++;
+                return {};
+            }));
     EXPECT_EQ(0, entriesSeen);
-    EXPECT_OK(testMap.iterateWithValue(
-        [&entriesSeen](const unsigned int&, const unsigned int&,
-                       const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
-            entriesSeen++;
-            return netdutils::status::ok;
-        }));
+    EXPECT_TRUE(testMap.iterateWithValue(
+            [&entriesSeen](const unsigned int&, const unsigned int&,
+                           const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
+                entriesSeen++;
+                return {};
+            }));
     EXPECT_EQ(0, entriesSeen);
 }
 
@@ -271,10 +271,10 @@
 
     BpfMap<uint32_t, uint32_t> 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<bool> 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 <linux/bpf.h>
 
+#include <android-base/result.h>
 #include <android-base/stringprintf.h>
 #include <android-base/unique_fd.h>
 #include <utils/Log.h>
 #include "bpf/BpfUtils.h"
-#include "netdutils/Status.h"
-#include "netdutils/StatusOr.h"
 
 namespace android {
 namespace bpf {
@@ -68,73 +67,69 @@
         }
     }
 
-    netdutils::StatusOr<Key> getFirstKey() const {
+    base::Result<Key> 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<Key> getNextKey(const Key& key) const {
+    base::Result<Key> 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<void> 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<Value> readValue(const Key key) const {
+    base::Result<Value> 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<void> 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<void> 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<netdutils::Status(const Key& key, const BpfMap<Key, Value>& map)>&
-            filter) const;
+    base::Result<void> iterate(
+            const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
+                    filter) const;
 
     // Iterate through the map and get each <key, value> pair, handle each <key,
     // value> pair based on the filter without modification of map content.
-    netdutils::Status iterateWithValue(
-        const std::function<netdutils::Status(const Key& key, const Value& value,
-                                              const BpfMap<Key, Value>& map)>& filter) const;
+    base::Result<void> iterateWithValue(
+            const std::function<base::Result<void>(const Key& key, const Value& value,
+                                                   const BpfMap<Key, Value>& map)>& filter) const;
 
     // Iterate through the map and handle each key retrieved based on the filter
-    netdutils::Status iterate(
-        const std::function<netdutils::Status(const Key& key, BpfMap<Key, Value>& map)>& filter);
+    base::Result<void> iterate(
+            const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>&
+                    filter);
 
     // Iterate through the map and get each <key, value> pair, handle each <key,
     // value> pair based on the filter.
-    netdutils::Status iterateWithValue(
-        const std::function<netdutils::Status(const Key& key, const Value& value,
-                                              BpfMap<Key, Value>& map)>& filter);
+    base::Result<void> iterateWithValue(
+            const std::function<base::Result<void>(const Key& key, const Value& value,
+                                                   BpfMap<Key, Value>& map)>& filter);
 
     const base::unique_fd& getMap() const { return mMapFd; };
 
@@ -152,23 +147,25 @@
 
     // 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<Key, Value>& 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<void> clear() {
+        const auto deleteAllEntries = [](const Key& key,
+                                         BpfMap<Key, Value>& map) -> base::Result<void> {
+            base::Result<void> 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<bool> isEmpty() const {
+    base::Result<bool> 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 @@
 };
 
 template <class Key, class Value>
-netdutils::Status BpfMap<Key, Value>::init(const char* path) {
+base::Result<void> 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));
+        return base::ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path);
     }
-    return netdutils::status::ok;
+    return {};
 }
 
 template <class Key, class Value>
-netdutils::Status BpfMap<Key, Value>::iterate(
-    const std::function<netdutils::Status(const Key& key, const BpfMap<Key, Value>& map)>& filter)
-    const {
-    netdutils::StatusOr<Key> curKey = getFirstKey();
-    while (isOk(curKey)) {
-        const netdutils::StatusOr<Key>& nextKey = getNextKey(curKey.value());
-        RETURN_IF_NOT_OK(filter(curKey.value(), *this));
+base::Result<void> BpfMap<Key, Value>::iterate(
+        const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
+                filter) const {
+    base::Result<Key> curKey = getFirstKey();
+    while (curKey) {
+        const base::Result<Key>& nextKey = getNextKey(curKey.value());
+        base::Result<void> 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 <class Key, class Value>
-netdutils::Status BpfMap<Key, Value>::iterateWithValue(
-    const std::function<netdutils::Status(const Key& key, const Value& value,
-                                          const BpfMap<Key, Value>& map)>& filter) const {
-    netdutils::StatusOr<Key> curKey = getFirstKey();
-    while (isOk(curKey)) {
-        const netdutils::StatusOr<Key>& 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<void> BpfMap<Key, Value>::iterateWithValue(
+        const std::function<base::Result<void>(const Key& key, const Value& value,
+                                               const BpfMap<Key, Value>& map)>& filter) const {
+    base::Result<Key> curKey = getFirstKey();
+    while (curKey) {
+        const base::Result<Key>& nextKey = getNextKey(curKey.value());
+        base::Result<Value> curValue = this->readValue(curKey.value());
+        if (!curValue) return curValue.error();
+        base::Result<void> 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 <class Key, class Value>
-netdutils::Status BpfMap<Key, Value>::iterate(
-    const std::function<netdutils::Status(const Key& key, BpfMap<Key, Value>& map)>& filter) {
-    netdutils::StatusOr<Key> curKey = getFirstKey();
-    while (isOk(curKey)) {
-        const netdutils::StatusOr<Key>& nextKey = getNextKey(curKey.value());
-        RETURN_IF_NOT_OK(filter(curKey.value(), *this));
+base::Result<void> BpfMap<Key, Value>::iterate(
+        const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& filter) {
+    base::Result<Key> curKey = getFirstKey();
+    while (curKey) {
+        const base::Result<Key>& nextKey = getNextKey(curKey.value());
+        base::Result<void> 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 <class Key, class Value>
-netdutils::Status BpfMap<Key, Value>::iterateWithValue(
-    const std::function<netdutils::Status(const Key& key, const Value& value,
-                                          BpfMap<Key, Value>& map)>& filter) {
-    netdutils::StatusOr<Key> curKey = getFirstKey();
-    while (isOk(curKey)) {
-        const netdutils::StatusOr<Key>& 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<void> BpfMap<Key, Value>::iterateWithValue(
+        const std::function<base::Result<void>(const Key& key, const Value& value,
+                                               BpfMap<Key, Value>& map)>& filter) {
+    base::Result<Key> curKey = getFirstKey();
+    while (curKey) {
+        const base::Result<Key>& nextKey = getNextKey(curKey.value());
+        base::Result<Value> curValue = this->readValue(curKey.value());
+        if (!curValue) return curValue.error();
+        base::Result<void> 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 <string.h>
 #include <sys/socket.h>
 
+#include <string>
+
 #include "android-base/unique_fd.h"
-#include "netdutils/Slice.h"
-#include "netdutils/StatusOr.h"
 
 #define BPF_PASS 1
 #define BPF_DROP 0