am c993be52: Add tcp buffer size conduit to NetworkAgent.

* commit 'c993be52b3e9a45a882ddc305a133c1859f9d50d':
  Add tcp buffer size conduit to NetworkAgent.
diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java
index dcb2892..3d6a132 100644
--- a/core/java/android/net/LinkProperties.java
+++ b/core/java/android/net/LinkProperties.java
@@ -55,6 +55,8 @@
     private ArrayList<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
     private ProxyInfo mHttpProxy;
     private int mMtu;
+    // in the format "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max"
+    private String mTcpBufferSizes;
 
     private static final int MIN_MTU    = 68;
     private static final int MIN_MTU_V6 = 1280;
@@ -105,6 +107,7 @@
                 addStackedLink(l);
             }
             setMtu(source.getMtu());
+            mTcpBufferSizes = source.mTcpBufferSizes;
         }
     }
 
@@ -351,6 +354,29 @@
         return mMtu;
     }
 
+    /**
+     * Sets the tcp buffers sizes to be used when this link is the system default.
+     * Should be of the form "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max".
+     *
+     * @param tcpBufferSizes The tcp buffers sizes to use.
+     *
+     * @hide
+     */
+    public void setTcpBufferSizes(String tcpBufferSizes) {
+        mTcpBufferSizes = tcpBufferSizes;
+    }
+
+    /**
+     * Gets the tcp buffer sizes.
+     *
+     * @return the tcp buffer sizes to use when this link is the system default.
+     *
+     * @hide
+     */
+    public String getTcpBufferSizes() {
+        return mTcpBufferSizes;
+    }
+
     private RouteInfo routeWithInterface(RouteInfo route) {
         return new RouteInfo(
             route.getDestination(),
@@ -509,6 +535,7 @@
         mHttpProxy = null;
         mStackedLinks.clear();
         mMtu = 0;
+        mTcpBufferSizes = null;
     }
 
     /**
@@ -534,6 +561,11 @@
 
         String mtu = " MTU: " + mMtu;
 
+        String tcpBuffSizes = "";
+        if (mTcpBufferSizes != null) {
+            tcpBuffSizes = " TcpBufferSizes: " + mTcpBufferSizes;
+        }
+
         String routes = " Routes: [";
         for (RouteInfo route : mRoutes) routes += route.toString() + ",";
         routes += "] ";
@@ -548,7 +580,7 @@
             stacked += "] ";
         }
         return "{" + ifaceName + linkAddresses + routes + dns + domainName + mtu
-            + proxy + stacked + "}";
+            + tcpBuffSizes + proxy + stacked + "}";
     }
 
     /**
@@ -756,6 +788,17 @@
         return getMtu() == target.getMtu();
     }
 
+    /**
+     * Compares this {@code LinkProperties} Tcp buffer sizes against the target.
+     *
+     * @param target LinkProperties to compare.
+     * @return {@code true} if both are identical, {@code false} otherwise.
+     * @hide
+     */
+    public boolean isIdenticalTcpBufferSizes(LinkProperties target) {
+        return Objects.equals(mTcpBufferSizes, target.mTcpBufferSizes);
+    }
+
     @Override
     /**
      * Compares this {@code LinkProperties} instance against the target
@@ -788,7 +831,8 @@
                 isIdenticalRoutes(target) &&
                 isIdenticalHttpProxy(target) &&
                 isIdenticalStackedLinks(target) &&
-                isIdenticalMtu(target);
+                isIdenticalMtu(target) &&
+                isIdenticalTcpBufferSizes(target);
     }
 
     /**
@@ -923,7 +967,8 @@
                 + mRoutes.size() * 41
                 + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode())
                 + mStackedLinks.hashCode() * 47)
-                + mMtu * 51;
+                + mMtu * 51
+                + ((null == mTcpBufferSizes) ? 0 : mTcpBufferSizes.hashCode());
     }
 
     /**
@@ -942,6 +987,7 @@
         }
         dest.writeString(mDomains);
         dest.writeInt(mMtu);
+        dest.writeString(mTcpBufferSizes);
         dest.writeInt(mRoutes.size());
         for(RouteInfo route : mRoutes) {
             dest.writeParcelable(route, flags);
@@ -981,6 +1027,7 @@
                 }
                 netProp.setDomains(in.readString());
                 netProp.setMtu(in.readInt());
+                netProp.setTcpBufferSizes(in.readString());
                 addressCount = in.readInt();
                 for (int i=0; i<addressCount; i++) {
                     netProp.addRoute((RouteInfo)in.readParcelable(null));
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 92953cc..5bef4bf 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -264,6 +264,8 @@
     private INetworkManagementService mNetd;
     private INetworkPolicyManager mPolicyManager;
 
+    private String mCurrentTcpBufferSizes;
+
     private static final int ENABLED  = 1;
     private static final int DISABLED = 0;
 
@@ -1553,30 +1555,40 @@
         }
     }
 
-    /**
-     * Reads the network specific TCP buffer sizes from SystemProperties
-     * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
-     * wide use
-     */
-    private void updateNetworkSettings(NetworkStateTracker nt) {
-        String key = nt.getTcpBufferSizesPropName();
-        String bufferSizes = key == null ? null : SystemProperties.get(key);
+    private static final String DEFAULT_TCP_BUFFER_SIZES = "4096,87380,110208,4096,16384,110208";
 
-        if (TextUtils.isEmpty(bufferSizes)) {
-            if (VDBG) log(key + " not found in system properties. Using defaults");
-
-            // Setting to default values so we won't be stuck to previous values
-            key = "net.tcp.buffersize.default";
-            bufferSizes = SystemProperties.get(key);
+    private void updateTcpBufferSizes(NetworkAgentInfo nai) {
+        if (isDefaultNetwork(nai) == false) {
+            return;
         }
 
-        // Set values in kernel
-        if (bufferSizes.length() != 0) {
-            if (VDBG) {
-                log("Setting TCP values: [" + bufferSizes
-                        + "] which comes from [" + key + "]");
-            }
-            setBufferSize(bufferSizes);
+        String tcpBufferSizes = nai.linkProperties.getTcpBufferSizes();
+        String[] values = null;
+        if (tcpBufferSizes != null) {
+            values = tcpBufferSizes.split(",");
+        }
+
+        if (values == null || values.length != 6) {
+            if (VDBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults");
+            tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES;
+            values = tcpBufferSizes.split(",");
+        }
+
+        if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return;
+
+        try {
+            if (VDBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes);
+
+            final String prefix = "/sys/kernel/ipv4/tcp_";
+            FileUtils.stringToFile(prefix + "rmem_min", values[0]);
+            FileUtils.stringToFile(prefix + "rmem_def", values[1]);
+            FileUtils.stringToFile(prefix + "rmem_max", values[2]);
+            FileUtils.stringToFile(prefix + "wmem_min", values[3]);
+            FileUtils.stringToFile(prefix + "wmem_def", values[4]);
+            FileUtils.stringToFile(prefix + "wmem_max", values[5]);
+            mCurrentTcpBufferSizes = tcpBufferSizes;
+        } catch (IOException e) {
+            loge("Can't set TCP buffer sizes:" + e);
         }
 
         final String defaultRwndKey = "net.tcp.default_init_rwnd";
@@ -1589,33 +1601,6 @@
         }
     }
 
-    /**
-     * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
-     * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
-     *
-     * @param bufferSizes in the format of "readMin, readInitial, readMax,
-     *        writeMin, writeInitial, writeMax"
-     */
-    private void setBufferSize(String bufferSizes) {
-        try {
-            String[] values = bufferSizes.split(",");
-
-            if (values.length == 6) {
-              final String prefix = "/sys/kernel/ipv4/tcp_";
-                FileUtils.stringToFile(prefix + "rmem_min", values[0]);
-                FileUtils.stringToFile(prefix + "rmem_def", values[1]);
-                FileUtils.stringToFile(prefix + "rmem_max", values[2]);
-                FileUtils.stringToFile(prefix + "wmem_min", values[3]);
-                FileUtils.stringToFile(prefix + "wmem_def", values[4]);
-                FileUtils.stringToFile(prefix + "wmem_max", values[5]);
-            } else {
-                loge("Invalid buffersize string: " + bufferSizes);
-            }
-        } catch (IOException e) {
-            loge("Can't set tcp buffer sizes:" + e);
-        }
-    }
-
     private void flushVmDnsCache() {
         /*
          * Tell the VMs to toss their DNS caches
@@ -1978,12 +1963,6 @@
                     */
                     break;
                 }
-                case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: {
-                    info = (NetworkInfo) msg.obj;
-                    int type = info.getType();
-                    if (mNetConfigs[type].isDefault()) updateNetworkSettings(mNetTrackers[type]);
-                    break;
-                }
             }
         }
     }
@@ -4152,6 +4131,7 @@
 
         updateInterfaces(newLp, oldLp, netId);
         updateMtu(newLp, oldLp);
+        updateTcpBufferSizes(networkAgent);
         // TODO - figure out what to do for clat
 //        for (LinkProperties lp : newLp.getStackedLinks()) {
 //            updateMtu(lp, null);
@@ -4377,6 +4357,7 @@
             loge("Exception setting default network :" + e);
         }
         handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy());
+        updateTcpBufferSizes(newNetwork);
     }
 
     private void handleConnectionValidated(NetworkAgentInfo newNetwork) {
@@ -4434,6 +4415,7 @@
                         isNewDefault = true;
                         updateActiveDefaultNetwork(newNetwork);
                         if (newNetwork.linkProperties != null) {
+                            updateTcpBufferSizes(newNetwork);
                             setDefaultDnsSystemProperties(
                                     newNetwork.linkProperties.getDnsServers());
                         } else {
@@ -4492,8 +4474,6 @@
                                 1000);
                     }
                 }
-                // TODO - read the tcp buffer size config string from somewhere
-                // updateNetworkSettings();
             }
 
             // Notify battery stats service about this network, both the normal