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/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