refactor bpf_map_def into a single header file

Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iafd6016f4f24cc8fa263939c67e1a096aa91b125
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp
index f4533cb..3a25fbd 100644
--- a/libbpf_android/Loader.cpp
+++ b/libbpf_android/Loader.cpp
@@ -27,6 +27,7 @@
 #include <sys/utsname.h>
 #include <unistd.h>
 
+#include "../progs/include/bpf_map_def.h"
 #include "LoaderUtils.h"
 #include "include/libbpf_android.h"
 
@@ -88,17 +89,6 @@
     unique_fd prog_fd; /* fd after loading */
 } codeSection;
 
-/* Common with the eBPF C program */
-struct bpf_map_def {
-    enum bpf_map_type type;
-    unsigned int key_size;
-    unsigned int value_size;
-    unsigned int max_entries;
-    unsigned int map_flags;
-    unsigned int inner_map_idx;
-    unsigned int numa_node;
-};
-
 static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
     elfFile.seekg(0);
     if (elfFile.fail()) return -1;
diff --git a/progs/include/bpf_helpers.h b/progs/include/bpf_helpers.h
index 454db2d..825947f 100644
--- a/progs/include/bpf_helpers.h
+++ b/progs/include/bpf_helpers.h
@@ -4,6 +4,8 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+#include "bpf_map_def.h"
+
 /* place things in different elf sections */
 #define SEC(NAME) __attribute__((section(NAME), used))
 
@@ -83,23 +85,3 @@
 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;
-
-/*
- * Map structure to be used by Android eBPF C programs. The Android eBPF loader
- * uses this structure from eBPF object to create maps at boot time.
- *
- * The eBPF C program should define structure in the maps section using
- * SEC("maps") otherwise it will be ignored by the eBPF loader.
- *
- * For example:
- * struct bpf_map_def SEC("maps") mymap { .type=... , .key_size=... }
- */
-struct bpf_map_def {
-    unsigned int type;
-    unsigned int key_size;
-    unsigned int value_size;
-    unsigned int max_entries;
-    unsigned int map_flags;
-    unsigned int pad1;
-    unsigned int pad2;
-};
diff --git a/progs/include/bpf_map_def.h b/progs/include/bpf_map_def.h
new file mode 100644
index 0000000..86708fc
--- /dev/null
+++ b/progs/include/bpf_map_def.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2020 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
+
+/* This file is separate because it's included both by eBPF programs (via include
+ * in bpf_helpers.h) and directly by the boot time bpfloader (Loader.cpp).
+ */
+
+#include <linux/bpf.h>
+
+/*
+ * Map structure to be used by Android eBPF C programs. The Android eBPF loader
+ * uses this structure from eBPF object to create maps at boot time.
+ *
+ * The eBPF C program should define structure in the maps section using
+ * SEC("maps") otherwise it will be ignored by the eBPF loader.
+ *
+ * For example:
+ *   const struct bpf_map_def SEC("maps") mymap { .type=... , .key_size=... }
+ *
+ * See 'bpf_helpers.h' for helpful macros for eBPF program use.
+ */
+struct bpf_map_def {
+    enum bpf_map_type type;
+    unsigned int key_size;
+    unsigned int value_size;
+    unsigned int max_entries;
+    unsigned int map_flags;
+
+    // The following are not supported by the Android bpfloader:
+    //   unsigned int inner_map_idx;
+    //   unsigned int numa_node;
+};