Snap for 12704449 from 1279fbf204c3079416ff13f4c1db5545d2a8ff71 to 25Q2-release
Change-Id: Ia9a0266e9431f48ded8f485e020aaa0623b3a2b7
diff --git a/Android.bp b/Android.bp
index dca9aba..14a195b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -33,6 +33,11 @@
},
}
+cc_library_headers {
+ name: "android_bpf_defs",
+ export_include_dirs: ["include/defs"],
+}
+
cc_defaults {
name: "bpf_cc_defaults",
cflags: [
diff --git a/include/defs/android_bpf_defs.h b/include/defs/android_bpf_defs.h
new file mode 100644
index 0000000..35cf057
--- /dev/null
+++ b/include/defs/android_bpf_defs.h
@@ -0,0 +1,59 @@
+#pragma once
+
+#ifdef ENABLE_LIBBPF
+
+// Either vmlinux.h or linux/types.h must be included before bpf/bpf_helpers.h
+#ifdef USE_VMLINUX
+// When using vmlinux.h, you can't use any system level headers.
+#include <vmlinux.h>
+#else
+#include <linux/types.h>
+#endif // USE_VMLINUX
+#include <bpf/bpf_helpers.h>
+
+#define DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid) \
+ struct { \
+ __uint(type, BPF_MAP_TYPE_##TYPE); \
+ __type(key, KeyType); \
+ __type(value, ValueType); \
+ __uint(max_entries, num_entries); \
+ } the_map SEC(".maps"); \
+ \
+ static inline __always_inline __unused ValueType* bpf_##the_map##_lookup_elem( \
+ const KeyType* k) { \
+ return bpf_map_lookup_elem(&the_map, k); \
+ }; \
+ \
+ static inline __always_inline __unused int bpf_##the_map##_update_elem( \
+ const KeyType* k, const ValueType* v, unsigned long long flags) { \
+ return bpf_map_update_elem(&the_map, k, v, flags); \
+ }; \
+ \
+ static inline __always_inline __unused int bpf_##the_map##_delete_elem(const KeyType* k) { \
+ return bpf_map_delete_elem(&the_map, k); \
+ };
+
+#define DEFINE_BPF_MAP_GRW(the_map, TYPE, KeyType, ValueType, num_entries, gid) \
+ DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid)
+#define DEFINE_BPF_MAP_GWO(the_map, TYPE, KeyType, ValueType, num_entries, gid) \
+ DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid)
+#define DEFINE_BPF_MAP_GRO(the_map, TYPE, KeyType, ValueType, num_entries, gid) \
+ DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid)
+
+#define DEFINE_BPF_PROG(SECTION_NAME, prog_uid, prog_gid, the_prog) \
+ SEC(SECTION_NAME) \
+ int the_prog
+
+#define LICENSE(NAME) char _license[] SEC("license") = (NAME)
+
+#else // LIBBPF DISABLED
+
+#include <bpf_helpers.h>
+
+#define bpf_printk(fmt, ...) \
+ ({ \
+ char ____fmt[] = fmt; \
+ bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \
+ })
+
+#endif // ENABLE_LIBBPF
diff --git a/progs/include/test/mock_bpf_helpers.h b/progs/include/test/mock_bpf_helpers.h
deleted file mode 100644
index 141ee4f..0000000
--- a/progs/include/test/mock_bpf_helpers.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (C) 2021 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.
- */
-
-#pragma once
-
-/* Mock BPF helpers to be used for testing of BPF programs loaded by Android */
-
-#include <linux/bpf.h>
-#include <stdbool.h>
-#include <stdint.h>
-
-#include <cutils/android_filesystem_config.h>
-
-typedef void* mock_bpf_map_t;
-
-/* type safe macro to declare a map and related accessor functions */
-#define DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, usr, grp, md) \
- mock_bpf_map_t mock_bpf_map_##the_map; \
- \
- mock_bpf_map_t get_mock_bpf_map_##the_map() { \
- if (mock_bpf_map_##the_map == 0) { \
- mock_bpf_map_##the_map = mock_bpf_map_create(sizeof(TypeOfKey), sizeof(TypeOfValue), \
- BPF_MAP_TYPE_##TYPE); \
- } \
- return mock_bpf_map_##the_map; \
- } \
- __unused TypeOfValue* bpf_##the_map##_lookup_elem(const TypeOfKey* k) { \
- return (TypeOfValue*)mock_bpf_lookup_elem(get_mock_bpf_map_##the_map(), (void*)k); \
- }; \
- \
- __unused int bpf_##the_map##_update_elem(const TypeOfKey* k, const TypeOfValue* v, \
- uint64_t flags) { \
- return mock_bpf_update_elem(get_mock_bpf_map_##the_map(), (void*)k, (void*)v, flags); \
- }; \
- \
- __unused int bpf_##the_map##_delete_elem(const TypeOfKey* k) { \
- return mock_bpf_delete_elem(get_mock_bpf_map_##the_map(), (void*)k); \
- };
-
-#define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
- DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, AID_ROOT, 0600)
-
-#define DEFINE_BPF_MAP_GWO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \
- DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0620)
-
-#define DEFINE_BPF_MAP_GRO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \
- DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0640)
-
-#define DEFINE_BPF_MAP_GRW(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \
- DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0660)
-
-#define DEFINE_BPF_PROG(section, owner, group, name) int name
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-mock_bpf_map_t mock_bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t type);
-void* mock_bpf_lookup_elem(mock_bpf_map_t map, void* key);
-int mock_bpf_update_elem(mock_bpf_map_t map, void* key, void* value, uint64_t flags);
-int mock_bpf_delete_elem(mock_bpf_map_t map, void* key);
-
-uint64_t bpf_ktime_get_ns();
-uint64_t bpf_get_smp_processor_id();
-uint64_t bpf_get_current_uid_gid();
-uint64_t bpf_get_current_pid_tgid();
-
-void mock_bpf_set_ktime_ns(uint64_t time_ns);
-void mock_bpf_set_smp_processor_id(uint32_t cpu);
-void mock_bpf_set_current_uid_gid(uint32_t uid);
-void mock_bpf_set_current_pid_tgid(uint64_t pid_tgid);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-/* place things in different elf sections */
-#define SECTION(NAME) __attribute__((section(NAME), used))
-
-/* Example use: LICENSE("GPL"); or LICENSE("Apache 2.0"); */
-#define LICENSE(NAME) char _license[] SECTION("license") = (NAME)