merge Tether{Down,Up}stream4{Key,Value} - part 3 - fixups

Test: atest, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia7840698e80ded33d8e0b59efe1ca7267254b892
diff --git a/Tethering/apishim/30/com/android/networkstack/tethering/apishim/api30/BpfCoordinatorShimImpl.java b/Tethering/apishim/30/com/android/networkstack/tethering/apishim/api30/BpfCoordinatorShimImpl.java
index d0a5af4..715e3a5 100644
--- a/Tethering/apishim/30/com/android/networkstack/tethering/apishim/api30/BpfCoordinatorShimImpl.java
+++ b/Tethering/apishim/30/com/android/networkstack/tethering/apishim/api30/BpfCoordinatorShimImpl.java
@@ -134,27 +134,14 @@
     }
 
     @Override
-    public boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
+    public boolean tetherOffloadRuleAdd(boolean downstream, @NonNull Tether4Key key,
             @NonNull Tether4Value value) {
         /* no op */
         return true;
     }
 
     @Override
-    public boolean tetherOffloadRuleRemove(@NonNull Tether4Key key) {
-        /* no op */
-        return true;
-    }
-
-    @Override
-    public boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
-            @NonNull Tether4Value value) {
-        /* no op */
-        return true;
-    }
-
-    @Override
-    public boolean tetherOffloadRuleRemove(@NonNull Tether4Key key) {
+    public boolean tetherOffloadRuleRemove(boolean downstream, @NonNull Tether4Key key) {
         /* no op */
         return true;
     }
diff --git a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java
index e6a2823..389ba7b 100644
--- a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java
+++ b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java
@@ -248,7 +248,7 @@
     }
 
     @Override
-    public boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
+    public boolean tetherOffloadRuleAdd(boolean downstream, @NonNull Tether4Key key,
             @NonNull Tether4Value value) {
         if (!isInitialized()) return false;
 
@@ -257,41 +257,11 @@
             // map pair twice causes the unexpected refresh. Must be fixed before starting the
             // conntrack timeout extension implementation.
             // TODO: consider using insertEntry.
-            mBpfDownstream4Map.updateEntry(key, value);
-        } catch (ErrnoException e) {
-            mLog.e("Could not update entry: ", e);
-            return false;
-        }
-        return true;
-    }
-
-    @Override
-    public boolean tetherOffloadRuleRemove(@NonNull Tether4Key key) {
-        if (!isInitialized()) return false;
-
-        try {
-            mBpfDownstream4Map.deleteEntry(key);
-        } catch (ErrnoException e) {
-            // Silent if the rule did not exist.
-            if (e.errno != OsConstants.ENOENT) {
-                mLog.e("Could not delete entry: ", e);
-                return false;
+            if (downstream) {
+                mBpfDownstream4Map.updateEntry(key, value);
+            } else {
+                mBpfUpstream4Map.updateEntry(key, value);
             }
-        }
-        return true;
-    }
-
-    @Override
-    public boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
-            @NonNull Tether4Value value) {
-        if (!isInitialized()) return false;
-
-        try {
-            // The last used time field of the value is updated by the bpf program. Adding the same
-            // map pair twice causes the unexpected refresh. Must be fixed before starting the
-            // conntrack timeout extension implementation.
-            // TODO: consider using insertEntry.
-            mBpfUpstream4Map.updateEntry(key, value);
         } catch (ErrnoException e) {
             mLog.e("Could not update entry: ", e);
             return false;
@@ -300,11 +270,15 @@
     }
 
     @Override
-    public boolean tetherOffloadRuleRemove(@NonNull Tether4Key key) {
+    public boolean tetherOffloadRuleRemove(boolean downstream, @NonNull Tether4Key key) {
         if (!isInitialized()) return false;
 
         try {
-            mBpfUpstream4Map.deleteEntry(key);
+            if (downstream) {
+                mBpfDownstream4Map.deleteEntry(key);
+            } else {
+                mBpfUpstream4Map.deleteEntry(key);
+            }
         } catch (ErrnoException e) {
             // Silent if the rule did not exist.
             if (e.errno != OsConstants.ENOENT) {
diff --git a/Tethering/apishim/common/com/android/networkstack/tethering/apishim/common/BpfCoordinatorShim.java b/Tethering/apishim/common/com/android/networkstack/tethering/apishim/common/BpfCoordinatorShim.java
index 6bcec18..cbd843b 100644
--- a/Tethering/apishim/common/com/android/networkstack/tethering/apishim/common/BpfCoordinatorShim.java
+++ b/Tethering/apishim/common/com/android/networkstack/tethering/apishim/common/BpfCoordinatorShim.java
@@ -112,25 +112,14 @@
     public abstract TetherStatsValue tetherOffloadGetAndClearStats(int ifIndex);
 
     /**
-     * Adds a tethering IPv4 downstream offload rule to BPF map.
+     * Adds a tethering IPv4 offload rule to appropriate BPF map.
      */
-    public abstract boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
+    public abstract boolean tetherOffloadRuleAdd(boolean downstream, @NonNull Tether4Key key,
             @NonNull Tether4Value value);
 
     /**
-     * Deletes a tethering IPv4 downstream offload rule from the BPF map.
+     * Deletes a tethering IPv4 offload rule from the appropriate BPF map.
      */
-    public abstract boolean tetherOffloadRuleRemove(@NonNull Tether4Key key);
-
-    /**
-     * Adds a tethering IPv4 upstream offload rule to BPF map.
-     */
-    public abstract boolean tetherOffloadRuleAdd(@NonNull Tether4Key key,
-            @NonNull Tether4Value value);
-
-    /**
-     * Deletes a tethering IPv4 upstream offload rule from the BPF map.
-     */
-    public abstract boolean tetherOffloadRuleRemove(@NonNull Tether4Key key);
+    public abstract boolean tetherOffloadRuleRemove(boolean downstream, @NonNull Tether4Key key);
 }
 
diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
index 0c8784b..ffb4557 100644
--- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
+++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
@@ -968,8 +968,8 @@
 
             if (e.msgType == (NetlinkConstants.NFNL_SUBSYS_CTNETLINK << 8
                     | NetlinkConstants.IPCTNL_MSG_CT_DELETE)) {
-                mBpfCoordinatorShim.tetherOffloadRuleRemove(upstream4Key);
-                mBpfCoordinatorShim.tetherOffloadRuleRemove(downstream4Key);
+                mBpfCoordinatorShim.tetherOffloadRuleRemove(false, upstream4Key);
+                mBpfCoordinatorShim.tetherOffloadRuleRemove(true, downstream4Key);
                 return;
             }
 
@@ -978,8 +978,8 @@
             final Tether4Value downstream4Value = makeTether4Value(e,
                     tetherClient, upstreamIndex);
 
-            mBpfCoordinatorShim.tetherOffloadRuleAdd(upstream4Key, upstream4Value);
-            mBpfCoordinatorShim.tetherOffloadRuleAdd(downstream4Key, downstream4Value);
+            mBpfCoordinatorShim.tetherOffloadRuleAdd(false, upstream4Key, upstream4Value);
+            mBpfCoordinatorShim.tetherOffloadRuleAdd(true, downstream4Key, downstream4Value);
         }
     }