Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b0499db authored by Devi Sandeep Endluri V V's avatar Devi Sandeep Endluri V V Committed by Linux Build Service Account
Browse files

server: add network stat plugin framework

Enable custom network statistics plugin framework. Use platform
specific custom implementation to compute network stats
and enforce quota for user
Change-Id: Ie711ec0bbd58bf72c2b3450708768a02cfbba2f8

server: modify network stat plugin framework
Add setUpstream function from Tethering to handle IPv6-only
Tethering.
Change-Id: I38392d0678df027c54469564746ee5d9897f5829

frameworks: Avoid calling API if jar loading fails
Even if the ConnectivityExt jar file is not
present, APIs are getting called resulting in page
faults. Avoid calling APIs if ConnectivityExt jar
file is not present or the jar loading fails.
Change-Id: Ie0983c03f8d35aa11cb86e19244b6aac529ac099

server: enhance network stat plugin framework
Add peekTetherStats function for third party apps to query
current hardware statistics.
This API is necessary to allow third party applications to
inquire about hardware statistics without affecting
NetworkStatsService accounting.
Change-Id: I90633585c8af6983f64ac78a77ee0419731a2a44

server: Whitelist non-INTERNET/non-CELLULAR Network for metering
Avoid setting a quota or counting statistics for non-internet
and non-cellular Networks.  Fixes an issue with metering feature
where mobile data usage is incorrectly applied to IMS or other
non-internet PDN.
Change-Id: Ib68f5e560ea1dd230747e4f367081501b70cfc60

Related CRs: 878013 959359 936622 984228 998263 984189
CRs-Fixed: 994082

Change-Id: Ie711ec0bbd58bf72c2b3450708768a02cfbba2f8
parent e3464af6
Loading
Loading
Loading
Loading
+136 −0
Original line number Diff line number Diff line
/*
 *Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
 *
 *Redistribution and use in source and binary forms, with or without
 *modification, are permitted provided that the following conditions are
 *met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of The Linux Foundation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 *ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 *BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 *IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.server;

import dalvik.system.PathClassLoader;

import java.io.File;
import java.lang.reflect.Constructor;

import android.util.Slog;
import android.net.Network;
import android.net.NetworkStats;
import android.util.Log;

public class NetPluginDelegate {

    private static final String TAG = "ConnectivityExtension";
    private static final boolean LOGV = false;

    private static Class tetherExtensionClass = null;
    private static Object tetherExtensionObj = null;

    public static void getTetherStats(NetworkStats uidStats, NetworkStats devStats,
            NetworkStats xtStats) {
        if (LOGV) Slog.v(TAG, "getTetherStats() E");
        if(!loadTetherExtJar()) return;
        try {
            tetherExtensionClass.getMethod("getTetherStats", NetworkStats.class,
                    NetworkStats.class, NetworkStats.class).invoke(tetherExtensionObj, uidStats,
                    devStats, xtStats);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "error in invoke method");
        }
        if (LOGV) Slog.v(TAG, "getTetherStats() X");
    }

    public static NetworkStats peekTetherStats() {
        if (LOGV) Slog.v(TAG, "peekTetherStats() E");
        NetworkStats ret_val = null;
        if(!loadTetherExtJar()) return ret_val;
        try {
            ret_val = (NetworkStats) tetherExtensionClass.getMethod("peekTetherStats")
                    .invoke(tetherExtensionObj);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "error in invoke method");
        }
        if (LOGV) Slog.v(TAG, "peekTetherStats() X");
        return ret_val;
    }

    public static void setQuota(String iface, long quota) {
        if (LOGV) Slog.v(TAG, "setQuota(" + iface + ", " + quota + ") E");
        if(!loadTetherExtJar()) return;
        try {
            tetherExtensionClass.getMethod("setQuota", String.class, long.class).invoke(
                    tetherExtensionObj, iface, quota);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "Error calling setQuota Method on extension jar");
        }
        if (LOGV) Slog.v(TAG, "setQuota(" + iface + ", " + quota + ") X");
    }

    public static void setUpstream(Network net) {
        if (LOGV) Slog.v(TAG, "setUpstream(" + net + ") E");
        if(!loadTetherExtJar()) return;
        try {
            tetherExtensionClass.getMethod("setUpstream", Network.class).invoke(
                    tetherExtensionObj, net);
        } catch (Exception e) {
            e.printStackTrace();
            Log.w(TAG, "Error calling setUpstream Method on extension jar");
        }
        if (LOGV) Slog.v(TAG, "setUpstream(" + net + ") X");
    }


    private static boolean loadTetherExtJar() {
        final String realProvider = "com.qualcomm.qti.tetherstatsextension.TetherStatsReporting";
        final String realProviderPath = "/system/framework/ConnectivityExt.jar";
        if (tetherExtensionClass != null && tetherExtensionObj != null) {
            return true;
        }
        boolean pathExists = new File(realProviderPath).exists();
        if (!pathExists) {
            Log.w(TAG, "ConnectivityExt jar file not present");
            return false;
        }

        if (tetherExtensionClass == null && tetherExtensionObj == null) {
            if (LOGV) Slog.v(TAG, "loading ConnectivityExt jar");
            try {
                PathClassLoader classLoader = new PathClassLoader(realProviderPath,
                        ClassLoader.getSystemClassLoader());

                tetherExtensionClass = classLoader.loadClass(realProvider);
                tetherExtensionObj = tetherExtensionClass.newInstance();
                if (LOGV) Slog.v(TAG, "ConnectivityExt jar loaded");
            } catch (Exception e) {
                e.printStackTrace();
                Log.w(TAG, "unable to load ConnectivityExt jar");
                return false;
            }
        }
        return true;
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ import com.android.internal.util.Protocol;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.server.IoThread;
import com.android.server.NetPluginDelegate;
import com.android.server.net.BaseNetworkObserver;

import java.io.FileDescriptor;
@@ -1730,6 +1731,8 @@ public class Tethering extends BaseNetworkObserver {
                }

                if (upType != ConnectivityManager.TYPE_NONE) {
                    Network network = getConnectivityManager().getNetworkForType(upType);
                    NetPluginDelegate.setUpstream(network);
                    LinkProperties linkProperties =
                            getConnectivityManager().getLinkProperties(upType);
                    if (linkProperties != null) {
@@ -1748,7 +1751,6 @@ public class Tethering extends BaseNetworkObserver {
                    }

                    if (iface != null) {
                        Network network = getConnectivityManager().getNetworkForType(upType);
                        if (network == null) {
                            Log.e(TAG, "No Network for upstream type " + upType + "!");
                        }
+9 −1
Original line number Diff line number Diff line
@@ -116,6 +116,7 @@ import android.net.INetworkPolicyListener;
import android.net.INetworkPolicyManager;
import android.net.INetworkStatsService;
import android.net.LinkProperties;
import android.net.NetworkCapabilities;
import android.net.NetworkIdentity;
import android.net.NetworkInfo;
import android.net.NetworkPolicy;
@@ -171,6 +172,7 @@ import com.android.server.DeviceIdleController;
import com.android.server.EventLogTags;
import com.android.server.LocalServices;
import com.android.server.SystemConfig;
import com.android.server.NetPluginDelegate;

import libcore.io.IoUtils;

@@ -1214,7 +1216,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        final ArrayList<Pair<String, NetworkIdentity>> connIdents = new ArrayList<>(states.length);
        final ArraySet<String> connIfaces = new ArraySet<String>(states.length);
        for (NetworkState state : states) {
            if (state.networkInfo != null && state.networkInfo.isConnected()) {
            if (state.networkInfo != null && state.networkInfo.isConnected()
                        && (state.networkCapabilities == null
                        || !state.networkCapabilities.hasTransport(
                                    NetworkCapabilities.TRANSPORT_CELLULAR)
                        || state.networkCapabilities.hasCapability(
                                    NetworkCapabilities.NET_CAPABILITY_INTERNET))) {
                final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);

                final String baseIface = state.linkProperties.getInterfaceName();
@@ -3270,6 +3277,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    private void setInterfaceQuota(String iface, long quotaBytes) {
        try {
            mNetworkManager.setInterfaceQuota(iface, quotaBytes);
            NetPluginDelegate.setQuota(iface, quotaBytes);
        } catch (IllegalStateException e) {
            Log.wtf(TAG, "problem setting interface quota", e);
        } catch (RemoteException e) {
+8 −1
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsService;
import android.net.INetworkStatsSession;
import android.net.LinkProperties;
import android.net.NetworkCapabilities;
import android.net.NetworkIdentity;
import android.net.NetworkInfo;
import android.net.NetworkState;
@@ -120,6 +121,7 @@ import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FileRotator;
import com.android.internal.util.IndentingPrintWriter;
import com.android.server.EventLogTags;
import com.android.server.NetPluginDelegate;
import com.android.server.connectivity.Tethering;

import java.io.File;
@@ -957,7 +959,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {

        final ArraySet<String> mobileIfaces = new ArraySet<>();
        for (NetworkState state : states) {
            if (state.networkInfo.isConnected()) {
            if (state.networkInfo.isConnected() && (state.networkCapabilities == null
                        || !state.networkCapabilities.hasTransport(
                                    NetworkCapabilities.TRANSPORT_CELLULAR)
                        || state.networkCapabilities.hasCapability(
                                    NetworkCapabilities.NET_CAPABILITY_INTERNET))) {
                final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType());
                final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);

@@ -1016,6 +1022,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
                devSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
        mXtRecorder.recordSnapshotLocked(
                xtSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
        NetPluginDelegate.getTetherStats(uidSnapshot, xtSnapshot, devSnapshot);

        // For per-UID stats, pass the VPN info so VPN traffic is reattributed to responsible apps.
        VpnInfo[] vpnArray = mConnManager.getAllVpnInfo();