Merge "4.9 kernels must support eBPF (as of Android S)"
diff --git a/libbpf_android/Android.bp b/libbpf_android/Android.bp
index 93dc7c1..cf443da 100644
--- a/libbpf_android/Android.bp
+++ b/libbpf_android/Android.bp
@@ -26,6 +26,8 @@
         },
     },
     sdk_version: "30",
+    header_libs: ["bpf_syscall_wrappers"],
+    export_header_lib_headers: ["bpf_syscall_wrappers"],
 }
 
 cc_library {
diff --git a/libbpf_android/include/bpf/BpfUtils.h b/libbpf_android/include/bpf/BpfUtils.h
index 046d80f..9181933 100644
--- a/libbpf_android/include/bpf/BpfUtils.h
+++ b/libbpf_android/include/bpf/BpfUtils.h
@@ -17,9 +17,7 @@
 #ifndef BPF_BPFUTILS_H
 #define BPF_BPFUTILS_H
 
-#include <linux/bpf.h>
 #include <linux/if_ether.h>
-#include <linux/unistd.h>
 #include <net/if.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,16 +25,7 @@
 
 #include <string>
 
-#ifdef BPF_FD_JUST_USE_INT
-  #define BPF_FD_TYPE int
-  #define BPF_FD_TO_U32(x) static_cast<__u32>(x)
-#else
-  #include <android-base/unique_fd.h>
-  #define BPF_FD_TYPE base::unique_fd&
-  #define BPF_FD_TO_U32(x) static_cast<__u32>((x).get())
-#endif
-
-#define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x))
+#include "BpfSyscallWrappers.h"
 
 namespace android {
 namespace bpf {
@@ -61,113 +50,6 @@
 
 constexpr const int MINIMUM_API_REQUIRED = 28;
 
-/* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
- * of it that we are using.  The kernel's bpf() system call will perform a strict check to ensure
- * all unused portions are zero.  It will fail with E2BIG if we don't fully zero bpf_attr.
- */
-
-inline int bpf(int cmd, const bpf_attr& attr) {
-    return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
-}
-
-inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
-                     uint32_t max_entries, uint32_t map_flags) {
-    return bpf(BPF_MAP_CREATE, {
-                                       .map_type = map_type,
-                                       .key_size = key_size,
-                                       .value_size = value_size,
-                                       .max_entries = max_entries,
-                                       .map_flags = map_flags,
-                               });
-}
-
-inline int writeToMapEntry(const BPF_FD_TYPE map_fd, const void* key, const void* value,
-                           uint64_t flags) {
-    return bpf(BPF_MAP_UPDATE_ELEM, {
-                                            .map_fd = BPF_FD_TO_U32(map_fd),
-                                            .key = ptr_to_u64(key),
-                                            .value = ptr_to_u64(value),
-                                            .flags = flags,
-                                    });
-}
-
-inline int findMapEntry(const BPF_FD_TYPE map_fd, const void* key, void* value) {
-    return bpf(BPF_MAP_LOOKUP_ELEM, {
-                                            .map_fd = BPF_FD_TO_U32(map_fd),
-                                            .key = ptr_to_u64(key),
-                                            .value = ptr_to_u64(value),
-                                    });
-}
-
-inline int deleteMapEntry(const BPF_FD_TYPE map_fd, const void* key) {
-    return bpf(BPF_MAP_DELETE_ELEM, {
-                                            .map_fd = BPF_FD_TO_U32(map_fd),
-                                            .key = ptr_to_u64(key),
-                                    });
-}
-
-inline int getNextMapKey(const BPF_FD_TYPE map_fd, const void* key, void* next_key) {
-    return bpf(BPF_MAP_GET_NEXT_KEY, {
-                                             .map_fd = BPF_FD_TO_U32(map_fd),
-                                             .key = ptr_to_u64(key),
-                                             .next_key = ptr_to_u64(next_key),
-                                     });
-}
-
-inline int getFirstMapKey(const BPF_FD_TYPE map_fd, void* firstKey) {
-    return getNextMapKey(map_fd, NULL, firstKey);
-}
-
-inline int bpfFdPin(const BPF_FD_TYPE map_fd, const char* pathname) {
-    return bpf(BPF_OBJ_PIN, {
-                                    .pathname = ptr_to_u64(pathname),
-                                    .bpf_fd = BPF_FD_TO_U32(map_fd),
-                            });
-}
-
-inline int bpfFdGet(const char* pathname, uint32_t flag) {
-    return bpf(BPF_OBJ_GET, {
-                                    .pathname = ptr_to_u64(pathname),
-                                    .file_flags = flag,
-                            });
-}
-
-inline int mapRetrieve(const char* pathname, uint32_t flag) {
-    return bpfFdGet(pathname, flag);
-}
-
-inline int mapRetrieveRW(const char* pathname) {
-    return mapRetrieve(pathname, 0);
-}
-
-inline int mapRetrieveRO(const char* pathname) {
-    return mapRetrieve(pathname, BPF_F_RDONLY);
-}
-
-inline int mapRetrieveWO(const char* pathname) {
-    return mapRetrieve(pathname, BPF_F_WRONLY);
-}
-
-inline int retrieveProgram(const char* pathname) {
-    return bpfFdGet(pathname, BPF_F_RDONLY);
-}
-
-inline int attachProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd,
-                         const BPF_FD_TYPE cg_fd) {
-    return bpf(BPF_PROG_ATTACH, {
-                                        .target_fd = BPF_FD_TO_U32(cg_fd),
-                                        .attach_bpf_fd = BPF_FD_TO_U32(prog_fd),
-                                        .attach_type = type,
-                                });
-}
-
-inline int detachProgram(bpf_attach_type type, const BPF_FD_TYPE cg_fd) {
-    return bpf(BPF_PROG_DETACH, {
-                                        .target_fd = BPF_FD_TO_U32(cg_fd),
-                                        .attach_type = type,
-                                });
-}
-
 uint64_t getSocketCookie(int sockFd);
 int synchronizeKernelRCU();
 int setrlimitForTest();