summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2020-04-28 20:22:36 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2020-04-28 20:22:36 +0000
commit79646f383f44e14681390fb0d8abfd2aef9ae6a1 (patch)
treeb2bc5e1ee0c03ad028ac3ccfd1917416181f61d9
parent05669b5596cfbbb756362464b6b9cd7e58c316e0 (diff)
parentefae2596a8880c95811a5301d89c7aa195bdda1e (diff)
downloadbpf-android10-mainline-tzdata-release.tar.gz
Change-Id: Iad4aead3a8eb2469a457b80a304133f98f00922a
-rw-r--r--bpfloader/BpfLoader.cpp6
-rw-r--r--libbpf_android/Android.bp1
-rw-r--r--libbpf_android/BpfMapTest.cpp37
-rw-r--r--libbpf_android/BpfUtils.cpp26
-rw-r--r--libbpf_android/Loader.cpp4
-rw-r--r--libbpf_android/include/bpf/BpfMap.h13
-rw-r--r--progs/include/bpf_helpers.h28
7 files changed, 60 insertions, 55 deletions
diff --git a/bpfloader/BpfLoader.cpp b/bpfloader/BpfLoader.cpp
index c9bed5e..01c02cc 100644
--- a/bpfloader/BpfLoader.cpp
+++ b/bpfloader/BpfLoader.cpp
@@ -86,6 +86,12 @@ void loadAllElfObjects(void) {
}
int main() {
+ std::string value = android::base::GetProperty("bpf.progs_loaded", "");
+ if (value == "1") {
+ ALOGI("Property bpf.progs_loaded is set, progs already loaded.\n");
+ return 0;
+ }
+
if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) {
// Load all ELF objects, create programs and maps, and pin them
loadAllElfObjects();
diff --git a/libbpf_android/Android.bp b/libbpf_android/Android.bp
index c369c8c..da5c6a6 100644
--- a/libbpf_android/Android.bp
+++ b/libbpf_android/Android.bp
@@ -83,7 +83,6 @@ cc_test {
"libnetdutils",
"libutils",
],
- require_root: true,
}
cc_test {
diff --git a/libbpf_android/BpfMapTest.cpp b/libbpf_android/BpfMapTest.cpp
index 94d273e..895dda6 100644
--- a/libbpf_android/BpfMapTest.cpp
+++ b/libbpf_android/BpfMapTest.cpp
@@ -52,13 +52,7 @@ constexpr const char PINNED_MAP_PATH[] = "/sys/fs/bpf/testMap";
class BpfMapTest : public testing::Test {
protected:
BpfMapTest() {}
-
- // SetUp() will always populate this with a map, but only some tests will use it.
- // They may use it once via 'mMapFd.release()', or multiple times via 'dup(mMapFd)'
- // to initialize a BpfMap object.
- // If it's not used or only dup'ed then TearDown() will close() it, otherwise
- // whoever got ownership via mMapFd.release() will close() it - possibly much earlier.
- unique_fd mMapFd;
+ int mMapFd;
void SetUp() {
SKIP_IF_BPF_NOT_SUPPORTED;
@@ -67,9 +61,9 @@ class BpfMapTest : public testing::Test {
if (!access(PINNED_MAP_PATH, R_OK)) {
EXPECT_EQ(0, remove(PINNED_MAP_PATH));
}
- mMapFd.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
- BPF_F_NO_PREALLOC));
- ASSERT_LE(0, mMapFd);
+ mMapFd = createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
+ BPF_F_NO_PREALLOC);
+ EXPECT_LE(0, mMapFd);
}
void TearDown() {
@@ -78,8 +72,7 @@ class BpfMapTest : public testing::Test {
if (!access(PINNED_MAP_PATH, R_OK)) {
EXPECT_EQ(0, remove(PINNED_MAP_PATH));
}
-
- mMapFd.reset(-1); // close(mMapFd) if still open
+ close(mMapFd);
}
void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
@@ -124,7 +117,7 @@ TEST_F(BpfMapTest, constructor) {
BpfMap<uint32_t, uint32_t> testMap1;
checkMapInvalid(testMap1);
- BpfMap<uint32_t, uint32_t> testMap2(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
checkMapValid(testMap2);
BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
@@ -134,7 +127,7 @@ TEST_F(BpfMapTest, constructor) {
TEST_F(BpfMapTest, basicHelpers) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap(mMapFd);
uint32_t key = TEST_KEY1;
uint32_t value_write = TEST_VALUE1;
writeToMapAndCheck(testMap, key, value_write);
@@ -151,21 +144,21 @@ TEST_F(BpfMapTest, reset) {
SKIP_IF_BPF_NOT_SUPPORTED;
BpfMap<uint32_t, uint32_t> testMap;
- testMap.reset(mMapFd.release());
+ testMap.reset(mMapFd);
uint32_t key = TEST_KEY1;
uint32_t value_write = TEST_VALUE1;
writeToMapAndCheck(testMap, key, value_write);
-
testMap.reset();
checkMapInvalid(testMap);
- ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
+ unique_fd invalidFd(mMapFd);
+ ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
ASSERT_EQ(EBADF, errno);
}
TEST_F(BpfMapTest, moveConstructor) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap1(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
BpfMap<uint32_t, uint32_t> testMap2;
testMap2 = std::move(testMap1);
uint32_t key = TEST_KEY1;
@@ -195,7 +188,7 @@ TEST_F(BpfMapTest, SetUpMap) {
TEST_F(BpfMapTest, iterate) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap(mMapFd);
populateMap(TEST_MAP_SIZE, testMap);
int totalCount = 0;
int totalSum = 0;
@@ -215,7 +208,7 @@ TEST_F(BpfMapTest, iterate) {
TEST_F(BpfMapTest, iterateWithValue) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap(mMapFd);
populateMap(TEST_MAP_SIZE, testMap);
int totalCount = 0;
int totalSum = 0;
@@ -237,7 +230,7 @@ TEST_F(BpfMapTest, iterateWithValue) {
TEST_F(BpfMapTest, mapIsEmpty) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap(mMapFd);
expectMapEmpty(testMap);
uint32_t key = TEST_KEY1;
uint32_t value_write = TEST_VALUE1;
@@ -269,7 +262,7 @@ TEST_F(BpfMapTest, mapIsEmpty) {
TEST_F(BpfMapTest, mapClear) {
SKIP_IF_BPF_NOT_SUPPORTED;
- BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
+ BpfMap<uint32_t, uint32_t> testMap(mMapFd);
populateMap(TEST_MAP_SIZE, testMap);
auto isEmpty = testMap.isEmpty();
ASSERT_TRUE(isOk(isEmpty));
diff --git a/libbpf_android/BpfUtils.cpp b/libbpf_android/BpfUtils.cpp
index 4a4761b..b3ff108 100644
--- a/libbpf_android/BpfUtils.cpp
+++ b/libbpf_android/BpfUtils.cpp
@@ -265,22 +265,28 @@ BpfLevel getBpfSupportLevel() {
int kernel_version_major;
int kernel_version_minor;
- // Check the device kernel version
+ uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
+ if (api_level == 0) {
+ ALOGE("Cannot determine initial API level of the device");
+ api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
+ }
+
+ // Check if the device is shipped originally with android P.
+ if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE;
+
int ret = uname(&buf);
- if (ret) return BpfLevel::NONE;
+ if (ret) {
+ return BpfLevel::NONE;
+ }
char dummy;
ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
+ // Check the device kernel version
if (ret < 2) return BpfLevel::NONE;
- if (kernel_version_major > 4) // 5.4+ R+
- return BpfLevel::EXTENDED;
- if (kernel_version_major == 4 && kernel_version_minor >= 19) // 4.19 Q+
- return BpfLevel::EXTENDED;
- if (kernel_version_major == 4 && kernel_version_minor >= 14) // 4.14 P+
+ if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14))
return BpfLevel::EXTENDED;
- if (kernel_version_major == 4 && kernel_version_minor >= 9) // 4.9 P+
- return BpfLevel::BASIC;
+ if (kernel_version_major == 4 && kernel_version_minor >= 9) return BpfLevel::BASIC;
- return BpfLevel::NONE; // 4.4-P
+ return BpfLevel::NONE;
}
} // namespace bpf
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp
index 16b4a2a..d02ed87 100644
--- a/libbpf_android/Loader.cpp
+++ b/libbpf_android/Loader.cpp
@@ -524,8 +524,8 @@ static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const
fd = bpf_prog_load(cs[i].type, cs[i].name.c_str(), (struct bpf_insn*)cs[i].data.data(),
cs[i].data.size(), license.c_str(), kvers, 0,
log_buf.data(), log_buf.size());
- ALOGD("bpf_prog_load lib call for %s (%s) returned: %d (%s)\n", elfPath,
- cs[i].name.c_str(), fd, std::strerror(errno));
+ ALOGD("New bpf core prog_load for %s (%s) returned: %d\n", elfPath, cs[i].name.c_str(),
+ fd);
if (fd <= 0)
ALOGE("bpf_prog_load: log_buf contents: %s\n", (char *)log_buf.data());
diff --git a/libbpf_android/include/bpf/BpfMap.h b/libbpf_android/include/bpf/BpfMap.h
index a894b57..d47698e 100644
--- a/libbpf_android/include/bpf/BpfMap.h
+++ b/libbpf_android/include/bpf/BpfMap.h
@@ -46,19 +46,8 @@ namespace bpf {
template <class Key, class Value>
class BpfMap {
public:
- BpfMap<Key, Value>() {};
+ BpfMap<Key, Value>() : mMapFd(-1){};
explicit BpfMap<Key, Value>(int fd) : mMapFd(fd){};
-
- // We could technically implement this constructor either with
- // : mMapFd(dup(fd)) {} // fd valid in caller, we have our own local copy
- // or
- // : mMapFd(fd.release()) {} // fd no longer valid in caller, we 'stole' it
- //
- // However, I think we're much better off with a compile time failure, since
- // it's better for whoever passes in a unique_fd to think twice about whether
- // they're trying to pass in ownership or not.
- explicit BpfMap<Key, Value>(base::unique_fd fd) = delete;
-
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) {
diff --git a/progs/include/bpf_helpers.h b/progs/include/bpf_helpers.h
index e933021..408a981 100644
--- a/progs/include/bpf_helpers.h
+++ b/progs/include/bpf_helpers.h
@@ -18,7 +18,7 @@
* Type-unsafe bpf map functions - avoid if possible.
*
* Using these it is possible to pass in keys/values of the wrong type/size,
- * or, for 'bpf_map_lookup_elem_unsafe' receive into a pointer to the wrong type.
+ * or, for 'unsafe_bpf_map_lookup_elem' receive into a pointer to the wrong type.
* You will not get a compile time failure, and for certain types of errors you
* might not even get a failure from the kernel's ebpf verifier during program load,
* instead stuff might just not work right at runtime.
@@ -36,11 +36,11 @@
* This will make sure that if you change the type of a map you'll get compile
* errors at any spots you forget to update with the new type.
*/
-static void* (*bpf_map_lookup_elem_unsafe)(void* map, void* key) = (void*)BPF_FUNC_map_lookup_elem;
-static int (*bpf_map_update_elem_unsafe)(void* map, void* key, void* value,
+static void* (*unsafe_bpf_map_lookup_elem)(void* map, void* key) = (void*)BPF_FUNC_map_lookup_elem;
+static int (*unsafe_bpf_map_update_elem)(void* map, void* key, void* value,
unsigned long long flags) = (void*)
BPF_FUNC_map_update_elem;
-static int (*bpf_map_delete_elem_unsafe)(void* map, void* key) = (void*)BPF_FUNC_map_delete_elem;
+static int (*unsafe_bpf_map_delete_elem)(void* map, void* key) = (void*)BPF_FUNC_map_delete_elem;
/* type safe macro to declare a map and related accessor functions */
#define DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
@@ -56,25 +56,37 @@ static int (*bpf_map_delete_elem_unsafe)(void* map, void* key) = (void*)BPF_FUNC
\
static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \
TypeOfKey* k) { \
- return bpf_map_lookup_elem_unsafe(&the_map, k); \
+ return unsafe_bpf_map_lookup_elem(&the_map, k); \
}; \
\
static inline __always_inline __unused int bpf_##the_map##_update_elem( \
TypeOfKey* k, TypeOfValue* v, unsigned long long flags) { \
- return bpf_map_update_elem_unsafe(&the_map, k, v, flags); \
+ return unsafe_bpf_map_update_elem(&the_map, k, v, flags); \
}; \
\
static inline __always_inline __unused int bpf_##the_map##_delete_elem(TypeOfKey* k) { \
- return bpf_map_delete_elem_unsafe(&the_map, k); \
+ return unsafe_bpf_map_delete_elem(&the_map, k); \
};
static int (*bpf_probe_read)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read;
-static int (*bpf_probe_read_str)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read_str;
static unsigned long long (*bpf_ktime_get_ns)(void) = (void*) BPF_FUNC_ktime_get_ns;
static int (*bpf_trace_printk)(const char* fmt, int fmt_size, ...) = (void*) BPF_FUNC_trace_printk;
static unsigned long long (*bpf_get_current_pid_tgid)(void) = (void*) BPF_FUNC_get_current_pid_tgid;
static unsigned long long (*bpf_get_current_uid_gid)(void) = (void*) BPF_FUNC_get_current_uid_gid;
static unsigned long long (*bpf_get_smp_processor_id)(void) = (void*) BPF_FUNC_get_smp_processor_id;
+/* networking */
+static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie;
+static uint32_t (*bpf_get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid;
+static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to,
+ int len) = (void*)BPF_FUNC_skb_load_bytes;
+
+static int (*bpf_skb_change_proto)(struct __sk_buff* skb, __be16 proto,
+ __u64 flags) = (void*)BPF_FUNC_skb_change_proto;
+static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
+ __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
+static int (*bpf_l4_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
+ __u64 flags) = (void*)BPF_FUNC_l4_csum_replace;
+static int (*bpf_redirect)(__u32 ifindex, __u64 flags) = (void*)BPF_FUNC_redirect;
/*
* Map structure to be used by Android eBPF C programs. The Android eBPF loader