Use iptables-restore to set the incoming packet mark rule.

This speeds up network switching because one rule needs to be
added/removed per interface.

Bug: 28362720
Test: bullhead builds, boots
Test: netd_{unit,integration}_test pass
Test: watch -n1 "adb shell iptables -v -n -t mangle -L INPUT" while switching networks
Change-Id: Ie536db6a50d018c88bb03c5f069965e99e0d162e
diff --git a/server/RouteControllerTest.cpp b/server/RouteControllerTest.cpp
index 6bcb231..a8468a6 100644
--- a/server/RouteControllerTest.cpp
+++ b/server/RouteControllerTest.cpp
@@ -18,13 +18,21 @@
 
 #include <gtest/gtest.h>
 
+#include "IptablesBaseTest.h"
 #include "NetlinkCommands.h"
 #include "RouteController.h"
 
 namespace android {
 namespace net {
 
-TEST(RouteControllerTest, TestGetRulePriority) {
+class RouteControllerTest : public IptablesBaseTest {
+public:
+    RouteControllerTest() {
+        RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
+    }
+};
+
+TEST_F(RouteControllerTest, TestGetRulePriority) {
     // Expect a rule dump for these two families to contain at least the following priorities.
     for (int family : {AF_INET, AF_INET6 }) {
         std::set<uint32_t> expectedPriorities = {
@@ -53,7 +61,7 @@
     }
 }
 
-TEST(RouteControllerTest, TestRouteFlush) {
+TEST_F(RouteControllerTest, TestRouteFlush) {
     // Pick a table number that's not used by the system.
     const uint32_t table1 = 500;
     const uint32_t table2 = 600;
@@ -76,5 +84,14 @@
               modifyIpRoute(RTM_DELROUTE, table2, "lo", "192.0.2.4/32", NULL));
 }
 
+TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
+    static constexpr int TEST_NETID = 30;
+    EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, true));
+    expectIptablesRestoreCommands({ "-t mangle -A INPUT -i netdtest0 -j MARK --set-mark 0x3001e" });
+
+    EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, false));
+    expectIptablesRestoreCommands({ "-t mangle -D INPUT -i netdtest0 -j MARK --set-mark 0x3001e" });
+}
+
 }  // namespace net
 }  // namespace android