share eBPF struct definitions between ebpf and C++ netd
This is the main commit in a set of 3 commits across 3 diff git repos.
The other two are:
https://android-review.googlesource.com/c/platform/system/bpf/+/1199649
"remove network specific struct definitions from BpfUtils.h"
and
https://android-review.googlesource.com/c/platform/frameworks/base/+/1200738
"minor change to keep it building"
We move the struct definitions to bpf_shared.h so they can be
shared between C++ netd and C ebpf code.
They also become typedefs and are renamed for more consistent naming.
(there's some weird issue with ebpf compiler on some devices with
non-typedef'ed structs)
Test: builds, atest
Bug: 146787904
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I324c0ab9db0186dcea0ec9ee33140909be285bc4
diff --git a/bpf_progs/netd.c b/bpf_progs/netd.c
index c018e5d..bde12f9 100644
--- a/bpf_progs/netd.c
+++ b/bpf_progs/netd.c
@@ -28,29 +28,6 @@
#include "bpf_net_helpers.h"
#include "netdbpf/bpf_shared.h"
-typedef struct {
- uint32_t uid;
- uint32_t tag;
-} uid_tag;
-
-typedef struct {
- uint32_t uid;
- uint32_t tag;
- uint32_t counterSet;
- uint32_t ifaceIndex;
-} stats_key;
-
-typedef struct {
- uint64_t rxPackets;
- uint64_t rxBytes;
- uint64_t txPackets;
- uint64_t txBytes;
-} stats_value;
-
-typedef struct {
- char name[IFNAMSIZ];
-} IfaceValue;
-
// This is defined for cgroup bpf filter only.
#define BPF_PASS 1
#define BPF_DROP 0
@@ -68,12 +45,12 @@
#define TCP_FLAG_OFF 13
#define RST_OFFSET 2
-DEFINE_BPF_MAP(cookie_tag_map, HASH, uint64_t, uid_tag, COOKIE_UID_MAP_SIZE)
+DEFINE_BPF_MAP(cookie_tag_map, HASH, uint64_t, UidTagValue, COOKIE_UID_MAP_SIZE)
DEFINE_BPF_MAP(uid_counterset_map, HASH, uint32_t, uint8_t, UID_COUNTERSET_MAP_SIZE)
-DEFINE_BPF_MAP(app_uid_stats_map, HASH, uint32_t, stats_value, APP_STATS_MAP_SIZE)
-DEFINE_BPF_MAP(stats_map_A, HASH, stats_key, stats_value, STATS_MAP_SIZE)
-DEFINE_BPF_MAP(stats_map_B, HASH, stats_key, stats_value, STATS_MAP_SIZE)
-DEFINE_BPF_MAP(iface_stats_map, HASH, uint32_t, stats_value, IFACE_STATS_MAP_SIZE)
+DEFINE_BPF_MAP(app_uid_stats_map, HASH, uint32_t, StatsValue, APP_STATS_MAP_SIZE)
+DEFINE_BPF_MAP(stats_map_A, HASH, StatsKey, StatsValue, STATS_MAP_SIZE)
+DEFINE_BPF_MAP(stats_map_B, HASH, StatsKey, StatsValue, STATS_MAP_SIZE)
+DEFINE_BPF_MAP(iface_stats_map, HASH, uint32_t, StatsValue, IFACE_STATS_MAP_SIZE)
DEFINE_BPF_MAP(configuration_map, HASH, uint32_t, uint8_t, CONFIGURATION_MAP_SIZE)
DEFINE_BPF_MAP(uid_owner_map, HASH, uint32_t, UidOwnerValue, UID_OWNER_MAP_SIZE)
@@ -88,10 +65,10 @@
#define DEFINE_UPDATE_STATS(the_stats_map, TypeOfKey) \
static __always_inline inline void update_##the_stats_map(struct __sk_buff* skb, \
int direction, TypeOfKey* key) { \
- stats_value* value; \
+ StatsValue* value; \
value = bpf_##the_stats_map##_lookup_elem(key); \
if (!value) { \
- stats_value newValue = {}; \
+ StatsValue newValue = {}; \
bpf_##the_stats_map##_update_elem(key, &newValue, BPF_NOEXIST); \
value = bpf_##the_stats_map##_lookup_elem(key); \
} \
@@ -108,8 +85,8 @@
DEFINE_UPDATE_STATS(app_uid_stats_map, uint32_t)
DEFINE_UPDATE_STATS(iface_stats_map, uint32_t)
-DEFINE_UPDATE_STATS(stats_map_A, stats_key)
-DEFINE_UPDATE_STATS(stats_map_B, stats_key)
+DEFINE_UPDATE_STATS(stats_map_A, StatsKey)
+DEFINE_UPDATE_STATS(stats_map_B, StatsKey)
static inline bool skip_owner_match(struct __sk_buff* skb) {
int offset = -1;
@@ -192,7 +169,7 @@
}
static __always_inline inline void update_stats_with_config(struct __sk_buff* skb, int direction,
- stats_key* key, uint8_t selectedMap) {
+ StatsKey* key, uint8_t selectedMap) {
if (selectedMap == SELECT_MAP_A) {
update_stats_map_A(skb, direction, key);
} else if (selectedMap == SELECT_MAP_B) {
@@ -210,7 +187,7 @@
}
uint64_t cookie = bpf_get_socket_cookie(skb);
- uid_tag* utag = bpf_cookie_tag_map_lookup_elem(&cookie);
+ UidTagValue* utag = bpf_cookie_tag_map_lookup_elem(&cookie);
uint32_t uid, tag;
if (utag) {
uid = utag->uid;
@@ -220,7 +197,7 @@
tag = 0;
}
- stats_key key = {.uid = uid, .tag = tag, .counterSet = 0, .ifaceIndex = skb->ifindex};
+ StatsKey key = {.uid = uid, .tag = tag, .counterSet = 0, .ifaceIndex = skb->ifindex};
uint8_t* counterSet = bpf_uid_counterset_map_lookup_elem(&uid);
if (counterSet) key.counterSet = (uint32_t)*counterSet;