Initial checkin for bpf project

Copied the files from system/netd into system/bpf. No modification is
made to the files.

Test: this doesn't build since the Android.bp file is missing
Bug: 112334572

Change-Id: Ia090a429dd416e07c46766784fb3dd2037201525
diff --git a/libbpf/include/bpf/BpfMap.h b/libbpf/include/bpf/BpfMap.h
new file mode 100644
index 0000000..20db43e
--- /dev/null
+++ b/libbpf/include/bpf/BpfMap.h
@@ -0,0 +1,284 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BPF_BPFMAP_H
+#define BPF_BPFMAP_H
+
+#include <linux/bpf.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 {
+
+// This is a class wrapper for eBPF maps. The eBPF map is a special in-kernel
+// data structure that stores data in <Key, Value> pairs. It can be read/write
+// from userspace by passing syscalls with the map file descriptor. This class
+// is used to generalize the procedure of interacting with eBPF maps and hide
+// the implementation detail from other process. Besides the basic syscalls
+// wrapper, it also provides some useful helper functions as well as an iterator
+// nested class to iterate the map more easily.
+//
+// NOTE: A kernel eBPF map may be accessed by both kernel and userspace
+// processes at the same time. Or if the map is pinned as a virtual file, it can
+// be obtained by multiple eBPF map class object and accessed concurrently.
+// Though the map class object and the underlying kernel map are thread safe, it
+// is not safe to iterate over a map while another thread or process is deleting
+// from it. In this case the iteration can return duplicate entries.
+template <class Key, class Value>
+class BpfMap {
+  public:
+    BpfMap<Key, Value>() : mMapFd(-1){};
+    BpfMap<Key, Value>(int fd) : mMapFd(fd){};
+    BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags) {
+        int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags);
+        if (map_fd < 0) {
+            mMapFd.reset(-1);
+        } else {
+            mMapFd.reset(map_fd);
+        }
+    }
+
+    netdutils::Status pinToPath(const std::string path) {
+        int ret = bpfFdPin(mMapFd, path.c_str());
+        if (ret) {
+            return netdutils::statusFromErrno(errno,
+                                              base::StringPrintf("pin to %s failed", path.c_str()));
+        }
+        mPinnedPath = path;
+        return netdutils::status::ok;
+    }
+
+    netdutils::StatusOr<Key> getFirstKey() const {
+        Key firstKey;
+        if (getFirstMapKey(mMapFd, &firstKey)) {
+            return netdutils::statusFromErrno(
+                errno, base::StringPrintf("Get firstKey map %d failed", mMapFd.get()));
+        }
+        return firstKey;
+    }
+
+    netdutils::StatusOr<Key> getNextKey(const Key& key) const {
+        Key nextKey;
+        if (getNextMapKey(mMapFd, const_cast<Key*>(&key), &nextKey)) {
+            return netdutils::statusFromErrno(
+                errno, base::StringPrintf("Get next key of map %d failed", mMapFd.get()));
+        }
+        return nextKey;
+    }
+
+    netdutils::Status writeValue(const Key& key, const Value& value, uint64_t flags) {
+        if (writeToMapEntry(mMapFd, const_cast<Key*>(&key), const_cast<Value*>(&value), flags)) {
+            return netdutils::statusFromErrno(
+                errno, base::StringPrintf("write to map %d failed", mMapFd.get()));
+        }
+        return netdutils::status::ok;
+    }
+
+    netdutils::StatusOr<Value> readValue(const Key key) const {
+        Value value;
+        if (findMapEntry(mMapFd, const_cast<Key*>(&key), &value)) {
+            return netdutils::statusFromErrno(
+                errno, base::StringPrintf("read value of map %d failed", mMapFd.get()));
+        }
+        return value;
+    }
+
+    netdutils::Status deleteValue(const Key& key) {
+        if (deleteMapEntry(mMapFd, const_cast<Key*>(&key))) {
+            return netdutils::statusFromErrno(
+                errno, base::StringPrintf("delete entry from map %d failed", mMapFd.get()));
+        }
+        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);
+
+    // 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;
+
+    // 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;
+
+    // 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);
+
+    // 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);
+
+    const base::unique_fd& getMap() const { return mMapFd; };
+
+    const std::string getPinnedPath() const { return mPinnedPath; };
+
+    // Move constructor
+    void operator=(BpfMap<Key, Value>&& other) noexcept {
+        mMapFd = std::move(other.mMapFd);
+        if (!other.mPinnedPath.empty()) {
+            mPinnedPath = other.mPinnedPath;
+        } else {
+            mPinnedPath.clear();
+        }
+        other.reset();
+    }
+
+    void reset(int fd = -1) {
+        mMapFd.reset(fd);
+        mPinnedPath.clear();
+    }
+
+    bool isValid() const { return mMapFd != -1; }
+
+    // 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()));
+            }
+            return netdutils::status::ok;
+        };
+        RETURN_IF_NOT_OK(iterate(deleteAllEntries));
+        return netdutils::status::ok;
+    }
+
+    netdutils::StatusOr<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);
+        return false;
+    }
+
+  private:
+    base::unique_fd mMapFd;
+    std::string mPinnedPath;
+};
+
+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(
+                errno,
+                base::StringPrintf("pinned map not accessible or does not exist: (%s)\n", path));
+        }
+        mPinnedPath = 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));
+        }
+        netdutils::Status pinStatus = pinToPath(path);
+        if (!isOk(pinStatus)) {
+            reset();
+            return pinStatus;
+        }
+        mPinnedPath = path;
+    } else {
+        return netdutils::statusFromErrno(
+            errno, base::StringPrintf("pinned map not accessible: %s", path));
+    }
+    return netdutils::status::ok;
+}
+
+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));
+        curKey = nextKey;
+    }
+    return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status();
+}
+
+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));
+        curKey = nextKey;
+    }
+    return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status();
+}
+
+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));
+        curKey = nextKey;
+    }
+    return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status();
+}
+
+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));
+        curKey = nextKey;
+    }
+    return curKey.status().code() == ENOENT ? netdutils::status::ok : curKey.status();
+}
+
+}  // namespace bpf
+}  // namespace android
+
+#endif
diff --git a/libbpf/include/bpf/BpfUtils.h b/libbpf/include/bpf/BpfUtils.h
new file mode 100644
index 0000000..ed31758
--- /dev/null
+++ b/libbpf/include/bpf/BpfUtils.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BPF_BPFUTILS_H
+#define BPF_BPFUTILS_H
+
+#include <linux/bpf.h>
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/unistd.h>
+#include <net/if.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+
+#include "android-base/unique_fd.h"
+#include "netdutils/Slice.h"
+#include "netdutils/StatusOr.h"
+
+#define BPF_PASS 1
+#define BPF_DROP 0
+
+#define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x))
+#define DEFAULT_LOG_LEVEL 1
+
+#define MAP_LD_CMD_HEAD 0x18
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
+
+// The BPF instruction bytes that we need to replace. x is a placeholder (e.g., COOKIE_TAG_MAP).
+#define BPF_MAP_SEARCH_PATTERN(x)                                                               \
+    {                                                                                           \
+        0x18, 0x01, 0x00, 0x00,                                                                 \
+        (x)[0], (x)[1], (x)[2], (x)[3],                                                         \
+        0x00, 0x00, 0x00, 0x00,                                                                 \
+        (x)[4], (x)[5], (x)[6], (x)[7]                                                          \
+    }
+
+// The bytes we'll replace them with. x is the actual fd number for the map at runtime.
+// The second byte is changed from 0x01 to 0x11 since 0x11 is the special command used
+// for bpf map fd loading. The original 0x01 is only a normal load command.
+#define BPF_MAP_REPLACE_PATTERN(x)                                                              \
+    {                                                                                           \
+        0x18, 0x11, 0x00, 0x00,                                                                 \
+        (x)[0], (x)[1], (x)[2], (x)[3],                                                         \
+        0x00, 0x00, 0x00, 0x00,                                                                 \
+        (x)[4], (x)[5], (x)[6], (x)[7]                                                          \
+    }
+
+#define MAP_CMD_SIZE 16
+
+namespace android {
+namespace bpf {
+
+struct UidTag {
+    uint32_t uid;
+    uint32_t tag;
+};
+
+struct StatsKey {
+    uint32_t uid;
+    uint32_t tag;
+    uint32_t counterSet;
+    uint32_t ifaceIndex;
+};
+
+struct StatsValue {
+    uint64_t rxPackets;
+    uint64_t rxBytes;
+    uint64_t txPackets;
+    uint64_t txBytes;
+};
+
+struct Stats {
+    uint64_t rxBytes;
+    uint64_t rxPackets;
+    uint64_t txBytes;
+    uint64_t txPackets;
+    uint64_t tcpRxPackets;
+    uint64_t tcpTxPackets;
+};
+
+struct IfaceValue {
+    char name[IFNAMSIZ];
+};
+
+struct BpfProgInfo {
+    bpf_attach_type attachType;
+    const char* path;
+    const char* name;
+    bpf_prog_type loadType;
+    base::unique_fd fd;
+};
+
+int mapRetrieve(const char* pathname, uint32_t flags);
+
+struct BpfMapInfo {
+    std::array<uint8_t, MAP_CMD_SIZE> search;
+    std::array<uint8_t, MAP_CMD_SIZE> replace;
+    const int fd;
+    std::string path;
+
+    BpfMapInfo(uint64_t dummyFd, const char* mapPath)
+        : BpfMapInfo(dummyFd, android::bpf::mapRetrieve(mapPath, 0)) {}
+
+    BpfMapInfo(uint64_t dummyFd, int realFd, const char* mapPath = "") : fd(realFd), path(mapPath) {
+        search = BPF_MAP_SEARCH_PATTERN((uint8_t*) &dummyFd);
+        replace = BPF_MAP_REPLACE_PATTERN((uint8_t*) &realFd);
+    }
+};
+
+#ifndef DEFAULT_OVERFLOWUID
+#define DEFAULT_OVERFLOWUID 65534
+#endif
+
+constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
+
+constexpr const int OVERFLOW_COUNTERSET = 2;
+
+constexpr const uint64_t NONEXISTENT_COOKIE = 0;
+
+constexpr const int MINIMUM_API_REQUIRED = 28;
+
+int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
+              uint32_t max_entries, uint32_t map_flags);
+int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
+int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
+int deleteMapEntry(const base::unique_fd& map_fd, void* key);
+int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
+int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
+int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license,
+                uint32_t kern_version, netdutils::Slice bpf_log);
+int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
+int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
+int detachProgram(bpf_attach_type type, uint32_t cg_fd);
+uint64_t getSocketCookie(int sockFd);
+bool hasBpfSupport();
+int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
+                          const std::vector<BpfMapInfo>& mapPatterns);
+
+#define SKIP_IF_BPF_NOT_SUPPORTED     \
+    do {                              \
+        if (!hasBpfSupport()) return; \
+    } while (0)
+
+constexpr int BPF_CONTINUE = 0;
+constexpr int BPF_DELETED = 1;
+
+bool operator==(const StatsValue& lhs, const StatsValue& rhs);
+bool operator==(const UidTag& lhs, const UidTag& rhs);
+bool operator==(const StatsKey& lhs, const StatsKey& rhs);
+
+}  // namespace bpf
+}  // namespace android
+
+#endif