refactor bpf kernel program
Refactor the program into several inline function and resolve the
duplication of ingress program and egress program. The Program compiled
from the new inline function should be no difference compare to the old
ones.
Bug: 30950746
Test: bpf program can be loaded.
atest android.net.cts.TrafficStatsTest
atest android.app.usage.cts.NetworkUsageStatsTest
Change-Id: I8091e8bdf7f3fe118c26907028e3f845eb41ab65
diff --git a/bpfloader/bpf_ingress.c b/bpfloader/bpf_ingress.c
index 0a5c5b8..d25877e 100644
--- a/bpfloader/bpf_ingress.c
+++ b/bpfloader/bpf_ingress.c
@@ -23,49 +23,5 @@
ELF_SEC(BPF_PROG_SEC_NAME)
int bpf_cgroup_ingress(struct __sk_buff* skb) {
- uint64_t cookie = get_socket_cookie(skb);
- struct uid_tag* utag = find_map_entry(COOKIE_TAG_MAP, &cookie);
- uint32_t uid, tag;
- if (utag) {
- uid = utag->uid;
- tag = utag->tag;
- } else {
- uid = get_socket_uid(skb);
- tag = 0;
- }
-
- struct stats_key key = {.uid = uid, .tag = tag, .counterSet = 0, .ifaceIndex = skb->ifindex};
-
- uint32_t* counterSet;
- counterSet = find_map_entry(UID_COUNTERSET_MAP, &uid);
- if (counterSet) key.counterSet = *counterSet;
-
- int ret;
- if (tag) {
- struct stats_value* tagValue;
- tagValue = find_map_entry(TAG_STATS_MAP, &key);
- if (!tagValue) {
- struct stats_value newValue = {};
- write_to_map_entry(TAG_STATS_MAP, &key, &newValue, BPF_NOEXIST);
- tagValue = find_map_entry(TAG_STATS_MAP, &key);
- }
- if (tagValue) {
- __sync_fetch_and_add(&tagValue->rxPackets, 1);
- __sync_fetch_and_add(&tagValue->rxBytes, skb->len);
- }
- }
-
- key.tag = 0;
- struct stats_value* value;
- value = find_map_entry(UID_STATS_MAP, &key);
- if (!value) {
- struct stats_value newValue = {};
- write_to_map_entry(UID_STATS_MAP, &key, &newValue, BPF_NOEXIST);
- value = find_map_entry(UID_STATS_MAP, &key);
- }
- if (value) {
- __sync_fetch_and_add(&value->rxPackets, 1);
- __sync_fetch_and_add(&value->rxBytes, skb->len);
- }
- return BPF_PASS;
+ return bpf_traffic_account(skb, BPF_INGRESS);
}