From 42a0e1ee846636a6a38cd214792ebe8bd1048cc8 Mon Sep 17 00:00:00 2001 From: Robert Greenwalt Date: Wed, 19 Mar 2014 17:56:12 -0700 Subject: [PATCH] First pass on multinetwork framework Starting to switch netd to use NetId. Adding the Network identifying class bug:13550136 Change-Id: Ie0db4fb17c9300bfafb63329adfa02339911b33d --- .../java/android/net/ConnectivityManager.java | 5 ++ core/java/android/net/Network.java | 59 ++++++++++++++++ .../android/server/ConnectivityService.java | 68 ++++++++++++++++--- 3 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 core/java/android/net/Network.java diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 30d70434a2..3e0025043a 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -408,6 +408,11 @@ public class ConnectivityManager { */ public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000; + /** + * @hide + */ + public final static int INVALID_NET_ID = 0; + private final IConnectivityManager mService; private final String mPackageName; diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java new file mode 100644 index 0000000000..f82bc22483 --- /dev/null +++ b/core/java/android/net/Network.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.os.Parcelable; +import android.os.Parcel; + + +/** + * Identifies the Network. + * @hide + */ +public class Network implements Parcelable { + + public final int netId; + + public Network(int netId) { + this.netId = netId; + } + + public Network(Network that) { + this.netId = that.netId; + } + + // implement the Parcelable interface + public int describeContents() { + return 0; + } + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(netId); + } + + public static final Creator CREATOR = + new Creator() { + public Network createFromParcel(Parcel in) { + int netId = in.readInt(); + + return new Network(netId); + } + + public Network[] newArray(int size) { + return new Network[size]; + } + }; +} diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index dfffa8a463..4ea33db475 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -65,6 +65,7 @@ import android.net.LinkProperties; import android.net.LinkProperties.CompareResult; import android.net.LinkQualityInfo; import android.net.MobileDataStateTracker; +import android.net.Network; import android.net.NetworkConfig; import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; @@ -165,6 +166,8 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; +import static android.net.ConnectivityManager.INVALID_NET_ID; + /** * @hide */ @@ -442,6 +445,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { TelephonyManager mTelephonyManager; + private final static int MIN_NET_ID = 10; // some reserved marks + private final static int MAX_NET_ID = 65535; + private int mNextNetId = MIN_NET_ID; + public ConnectivityService(Context context, INetworkManagementService netd, INetworkStatsService statsService, INetworkPolicyManager policyManager) { // Currently, omitting a NetworkFactory will create one internally @@ -706,6 +713,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); } + private synchronized int nextNetId() { + int netId = mNextNetId; + if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; + return netId; + } + /** * Factory that creates {@link NetworkStateTracker} instances using given * {@link NetworkConfig}. @@ -1984,6 +1997,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { int prevNetType = info.getType(); mNetTrackers[prevNetType].setTeardownRequested(false); + int thisNetId = mNetTrackers[prevNetType].getNetwork().netId; // Remove idletimer previously setup in {@code handleConnect} if (mNetConfigs[prevNetType].isDefault()) { @@ -2069,6 +2083,13 @@ public class ConnectivityService extends IConnectivityManager.Stub { sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), getConnectivityChangeDelay()); } + try { + mNetd.removeNetwork(thisNetId); + } catch (Exception e) { + loge("Exception removing network: " + e); + } finally { + mNetTrackers[prevNetType].setNetId(INVALID_NET_ID); + } } private void tryFailover(int prevNetType) { @@ -2336,17 +2357,23 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (mNetConfigs[newNetType].isDefault()) { if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { if (isNewNetTypePreferredOverCurrentNetType(newNetType)) { - // tear down the other - NetworkStateTracker otherNet = - mNetTrackers[mActiveDefaultNetwork]; - if (DBG) { - log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + - " teardown"); - } - if (!teardown(otherNet)) { - loge("Network declined teardown request"); - teardown(thisNet); - return; + String teardownPolicy = SystemProperties.get("net.teardownPolicy"); + if (TextUtils.equals(teardownPolicy, "keep") == false) { + // tear down the other + NetworkStateTracker otherNet = + mNetTrackers[mActiveDefaultNetwork]; + if (DBG) { + log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + + " teardown"); + } + if (!teardown(otherNet)) { + loge("Network declined teardown request"); + teardown(thisNet); + return; + } + } else { + //TODO - remove + loge("network teardown skipped due to net.teardownPolicy setting"); } } else { // don't accept this one @@ -2358,6 +2385,15 @@ public class ConnectivityService extends IConnectivityManager.Stub { return; } } + int thisNetId = nextNetId(); + thisNet.setNetId(thisNetId); + try { + mNetd.createNetwork(thisNetId, thisIface); + } catch (Exception e) { + loge("Exception creating network :" + e); + teardown(thisNet); + return; + } setupDataActivityTracking(newNetType); synchronized (ConnectivityService.this) { // have a new default network, release the transition wakelock in a second @@ -2380,6 +2416,16 @@ public class ConnectivityService extends IConnectivityManager.Stub { // Don't do this - if we never sign in stay, grey //reportNetworkCondition(mActiveDefaultNetwork, 100); updateNetworkSettings(thisNet); + } else { + int thisNetId = nextNetId(); + thisNet.setNetId(thisNetId); + try { + mNetd.createNetwork(thisNetId, thisIface); + } catch (Exception e) { + loge("Exception creating network :" + e); + teardown(thisNet); + return; + } } thisNet.setTeardownRequested(false); updateMtuSizeSettings(thisNet);