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/server/TrafficController.cpp b/server/TrafficController.cpp
index 86f1f53..0de2370 100644
--- a/server/TrafficController.cpp
+++ b/server/TrafficController.cpp
@@ -368,7 +368,7 @@
 
     uint64_t sock_cookie = getSocketCookie(sockFd);
     if (sock_cookie == NONEXISTENT_COOKIE) return -errno;
-    UidTag newKey = {.uid = (uint32_t)uid, .tag = tag};
+    UidTagValue newKey = {.uid = (uint32_t)uid, .tag = tag};
 
     uint32_t totalEntryCount = 0;
     uint32_t perUidEntryCount = 0;
@@ -487,8 +487,9 @@
 
     // First we go through the cookieTagMap to delete the target uid tag combination. Or delete all
     // the tags related to the uid if the tag is 0.
-    const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key, const UidTag& value,
-                                                       BpfMap<uint64_t, UidTag>& map) {
+    const auto deleteMatchedCookieEntries = [uid, tag](const uint64_t& key,
+                                                       const UidTagValue& value,
+                                                       BpfMap<uint64_t, UidTagValue>& map) {
         if (value.uid == uid && (value.tag == tag || tag == 0)) {
             Status res = map.deleteValue(key);
             if (isOk(res) || (res.code() == ENOENT)) {
@@ -991,8 +992,8 @@
 
     // Print CookieTagMap content.
     dumpBpfMap("mCookieTagMap", dw, "");
-    const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTag& value,
-                                          const BpfMap<uint64_t, UidTag>&) {
+    const auto printCookieTagInfo = [&dw](const uint64_t& key, const UidTagValue& value,
+                                          const BpfMap<uint64_t, UidTagValue>&) {
         dw.println("cookie=%" PRIu64 " tag=0x%x uid=%u", key, value.tag, value.uid);
         return netdutils::status::ok;
     };
diff --git a/server/TrafficController.h b/server/TrafficController.h
index 7ec0279..741ceb7 100644
--- a/server/TrafficController.h
+++ b/server/TrafficController.h
@@ -32,10 +32,6 @@
 #include "utils/String16.h"
 
 using android::bpf::BpfMap;
-using android::bpf::IfaceValue;
-using android::bpf::StatsKey;
-using android::bpf::StatsValue;
-using android::bpf::UidTag;
 
 namespace android {
 namespace net {
@@ -136,9 +132,9 @@
      * that receives them, then the kernel will drop some of these sockets and we
      * won't delete their tags.
      * Map Key: uint64_t socket cookie
-     * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
+     * Map Value: UidTagValue, contains a uint32 uid and a uint32 tag.
      */
-    BpfMap<uint64_t, UidTag> mCookieTagMap GUARDED_BY(mMutex);
+    BpfMap<uint64_t, UidTagValue> mCookieTagMap GUARDED_BY(mMutex);
 
     /*
      * mUidCounterSetMap: Store the counterSet of a specific uid.
@@ -159,9 +155,9 @@
      * mStatsMapA/mStatsMapB: Store the traffic statistics for a specific
      * combination of uid, tag, iface and counterSet. These two maps contain
      * both tagged and untagged traffic.
-     * Map Key: Struct StatsKey contains the uid, tag, counterSet and ifaceIndex
+     * Map Key: StatsKey contains the uid, tag, counterSet and ifaceIndex
      * information.
-     * Map Value: struct Stats, contains packet count and byte count of each
+     * Map Value: Stats, contains packet count and byte count of each
      * transport protocol on egress and ingress direction.
      */
     BpfMap<StatsKey, StatsValue> mStatsMapA GUARDED_BY(mMutex);
diff --git a/server/TrafficControllerTest.cpp b/server/TrafficControllerTest.cpp
index c97c104..26dbe0c 100644
--- a/server/TrafficControllerTest.cpp
+++ b/server/TrafficControllerTest.cpp
@@ -65,7 +65,7 @@
     TrafficControllerTest()
         : mTc(TEST_PER_UID_STATS_ENTRIES_LIMIT, TEST_TOTAL_UID_STATS_ENTRIES_LIMIT) {}
     TrafficController mTc;
-    BpfMap<uint64_t, UidTag> mFakeCookieTagMap;
+    BpfMap<uint64_t, UidTagValue> mFakeCookieTagMap;
     BpfMap<uint32_t, uint8_t> mFakeUidCounterSetMap;
     BpfMap<uint32_t, StatsValue> mFakeAppUidStatsMap;
     BpfMap<StatsKey, StatsValue> mFakeStatsMapA;
@@ -78,20 +78,20 @@
         SKIP_IF_BPF_NOT_SUPPORTED;
         ASSERT_EQ(0, setrlimitForTest());
 
-        mFakeCookieTagMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint64_t),
-                                          sizeof(struct UidTag), TEST_MAP_SIZE, 0));
+        mFakeCookieTagMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint64_t), sizeof(UidTagValue),
+                                          TEST_MAP_SIZE, 0));
         ASSERT_VALID(mFakeCookieTagMap);
 
         mFakeUidCounterSetMap.reset(
             createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint8_t), TEST_MAP_SIZE, 0));
         ASSERT_VALID(mFakeUidCounterSetMap);
 
-        mFakeAppUidStatsMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
-                                            sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
+        mFakeAppUidStatsMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(StatsValue),
+                                            TEST_MAP_SIZE, 0));
         ASSERT_VALID(mFakeAppUidStatsMap);
 
-        mFakeStatsMapA.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(struct StatsKey),
-                                       sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
+        mFakeStatsMapA.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(StatsKey), sizeof(StatsValue),
+                                       TEST_MAP_SIZE, 0));
         ASSERT_VALID(mFakeStatsMapA);
 
         mFakeConfigurationMap.reset(
@@ -141,7 +141,7 @@
     }
 
     void expectUidTag(uint64_t cookie, uid_t uid, uint32_t tag) {
-        StatusOr<UidTag> tagResult = mFakeCookieTagMap.readValue(cookie);
+        StatusOr<UidTagValue> tagResult = mFakeCookieTagMap.readValue(cookie);
         EXPECT_TRUE(isOk(tagResult));
         EXPECT_EQ(uid, tagResult.value().uid);
         EXPECT_EQ(tag, tagResult.value().tag);
@@ -150,7 +150,7 @@
     void expectNoTag(uint64_t cookie) { EXPECT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie))); }
 
     void populateFakeStats(uint64_t cookie, uint32_t uid, uint32_t tag, StatsKey* key) {
-        UidTag cookieMapkey = {.uid = (uint32_t)uid, .tag = tag};
+        UidTagValue cookieMapkey = {.uid = (uint32_t)uid, .tag = tag};
         EXPECT_TRUE(isOk(mFakeCookieTagMap.writeValue(cookie, cookieMapkey, BPF_ANY)));
         *key = {.uid = uid, .tag = tag, .counterSet = TEST_COUNTERSET, .ifaceIndex = 1};
         StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
@@ -277,7 +277,7 @@
 
     void expectFakeStatsUnchanged(uint64_t cookie, uint32_t tag, uint32_t uid,
                                   StatsKey tagStatsMapKey) {
-        StatusOr<UidTag> cookieMapResult = mFakeCookieTagMap.readValue(cookie);
+        StatusOr<UidTagValue> cookieMapResult = mFakeCookieTagMap.readValue(cookie);
         EXPECT_TRUE(isOk(cookieMapResult));
         EXPECT_EQ(uid, cookieMapResult.value().uid);
         EXPECT_EQ(tag, cookieMapResult.value().tag);
@@ -545,7 +545,7 @@
     populateFakeStats(cookie2, uid, tag2, &tagStatsMapKey2);
     ASSERT_EQ(0, mTc.deleteTagData(TEST_TAG, TEST_UID, callingUid));
     ASSERT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie1)));
-    StatusOr<UidTag> cookieMapResult = mFakeCookieTagMap.readValue(cookie2);
+    StatusOr<UidTagValue> cookieMapResult = mFakeCookieTagMap.readValue(cookie2);
     ASSERT_TRUE(isOk(cookieMapResult));
     ASSERT_EQ(TEST_UID, cookieMapResult.value().uid);
     ASSERT_EQ(TEST_TAG + 1, cookieMapResult.value().tag);