Fixing the BandwidthController unit test

The BandwidthController unit test cannot directly use gCtrl->trafficCtrl
to check if the bpf stats feature is avaliable. Rewrite the
getBpfStatsStatus() function of BandwidthController so it is also
functioning when in BandwidthController unit tests.

Bug: 72111305
Test: ./netd_unit_test
Change-Id: Ie60ed384b7f8d42b8e1f3417949d46895bea6572
diff --git a/server/BandwidthController.cpp b/server/BandwidthController.cpp
index e79a595..ffedaf6 100644
--- a/server/BandwidthController.cpp
+++ b/server/BandwidthController.cpp
@@ -207,10 +207,7 @@
  */
 
 const std::vector<std::string> getBasicAccountingCommands() {
-    bool useBpf = android::net::gCtls->trafficCtrl.checkBpfStatsEnable();
-    //TODO: remove this when xt_bpf kernel support is ready
-    useBpf = false;
-
+    bool useBpf = BandwidthController::getBpfStatsStatus();
     const std::vector<std::string> ipt_basic_accounting_commands = {
         "*filter",
         // Prevents IPSec double counting (ESP and UDP-encap-ESP respectively)
@@ -269,6 +266,14 @@
 
 }  // namespace
 
+bool BandwidthController::getBpfStatsStatus() {
+    bool useBpf = (access(XT_BPF_INGRESS_PROG_PATH, F_OK) != -1) &&
+                  (access(XT_BPF_EGRESS_PROG_PATH, F_OK) != -1);
+    // TODO: remove this when xt_bpf kernel support is ready
+    useBpf = false;
+    return useBpf;
+}
+
 BandwidthController::BandwidthController() {
 }
 
diff --git a/server/BandwidthController.h b/server/BandwidthController.h
index c8dad99..efacdce 100644
--- a/server/BandwidthController.h
+++ b/server/BandwidthController.h
@@ -33,6 +33,7 @@
     BandwidthController();
 
     int setupIptablesHooks();
+    static bool getBpfStatsStatus();
 
     int enableBandwidthControl(bool force);
     int disableBandwidthControl();
diff --git a/server/BandwidthControllerTest.cpp b/server/BandwidthControllerTest.cpp
index d4a1a7d..938cea0 100644
--- a/server/BandwidthControllerTest.cpp
+++ b/server/BandwidthControllerTest.cpp
@@ -34,6 +34,7 @@
 #include "BandwidthController.h"
 #include "Fwmark.h"
 #include "IptablesBaseTest.h"
+#include "bpf/BpfUtils.h"
 #include "tun_interface.h"
 
 using ::testing::ByMove;
@@ -45,6 +46,8 @@
 
 using android::base::Join;
 using android::base::StringPrintf;
+using android::bpf::XT_BPF_EGRESS_PROG_PATH;
+using android::bpf::XT_BPF_INGRESS_PROG_PATH;
 using android::net::TunInterface;
 using android::netdutils::status::ok;
 using android::netdutils::UniqueFile;
@@ -177,6 +180,7 @@
     std::string expectedClean = "";
 
     uint32_t uidBillingMask = Fwmark::getUidBillingMask();
+    bool useBpf = BandwidthController::getBpfStatsStatus();
     std::string expectedAccounting =
         "*filter\n"
         "-A bw_INPUT -p esp -j RETURN\n" +
@@ -196,15 +200,27 @@
         "*raw\n"
         "-A bw_raw_PREROUTING -i " IPSEC_IFACE_PREFIX "+ -j RETURN\n"
         "-A bw_raw_PREROUTING -m policy --pol ipsec --dir in -j RETURN\n"
-        "-A bw_raw_PREROUTING -m owner --socket-exists\n"
+        "-A bw_raw_PREROUTING -m owner --socket-exists\n";
+    if (useBpf) {
+        expectedAccounting += StringPrintf("-A bw_raw_PREROUTING -m bpf --object-pinned %s\n",
+                                           XT_BPF_INGRESS_PROG_PATH);
+    } else {
+        expectedAccounting += "\n";
+    }
+    expectedAccounting +=
         "COMMIT\n"
         "*mangle\n"
         "-A bw_mangle_POSTROUTING -o " IPSEC_IFACE_PREFIX "+ -j RETURN\n"
         "-A bw_mangle_POSTROUTING -m policy --pol ipsec --dir out -j RETURN\n"
         "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" +
-        StringPrintf("-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x%x\n", uidBillingMask) +
-        "COMMIT\n";
-
+        StringPrintf("-A bw_mangle_POSTROUTING -j MARK --set-mark 0x0/0x%x\n", uidBillingMask);
+    if (useBpf) {
+        expectedAccounting += StringPrintf("-A bw_mangle_POSTROUTING -m bpf --object-pinned %s\n",
+                                           XT_BPF_EGRESS_PROG_PATH);
+    } else {
+        expectedAccounting += "\n";
+    }
+    expectedAccounting += "COMMIT\n";
     mBw.enableBandwidthControl(false);
     expectSetupCommands(expectedClean, expectedAccounting);
 }