Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "TrafficStats" |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <android_runtime/AndroidRuntime.h> |
| 26 | #include <cutils/logger.h> |
| 27 | #include <jni.h> |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 28 | #include <ScopedUtfChars.h> |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 29 | #include <utils/misc.h> |
| 30 | #include <utils/Log.h> |
| 31 | |
| 32 | namespace android { |
| 33 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 34 | static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt"; |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 35 | static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats"; |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 36 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 37 | // NOTE: keep these in sync with TrafficStats.java |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 38 | static const uint64_t UNKNOWN = -1; |
| 39 | |
| 40 | enum StatsType { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 41 | RX_BYTES = 0, |
| 42 | RX_PACKETS = 1, |
| 43 | TX_BYTES = 2, |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 44 | TX_PACKETS = 3, |
| 45 | TCP_RX_PACKETS = 4, |
| 46 | TCP_TX_PACKETS = 5 |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 49 | struct Stats { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 50 | uint64_t rxBytes; |
| 51 | uint64_t rxPackets; |
| 52 | uint64_t txBytes; |
| 53 | uint64_t txPackets; |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 54 | uint64_t tcpRxPackets; |
| 55 | uint64_t tcpTxPackets; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 58 | static uint64_t getStatsType(struct Stats* stats, StatsType type) { |
| 59 | switch (type) { |
| 60 | case RX_BYTES: |
| 61 | return stats->rxBytes; |
| 62 | case RX_PACKETS: |
| 63 | return stats->rxPackets; |
| 64 | case TX_BYTES: |
| 65 | return stats->txBytes; |
| 66 | case TX_PACKETS: |
| 67 | return stats->txPackets; |
| 68 | case TCP_RX_PACKETS: |
| 69 | return stats->tcpRxPackets; |
| 70 | case TCP_TX_PACKETS: |
| 71 | return stats->tcpTxPackets; |
| 72 | default: |
| 73 | return UNKNOWN; |
| 74 | } |
| 75 | } |
| 76 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 77 | static int parseIfaceStats(const char* iface, struct Stats* stats) { |
| 78 | FILE *fp = fopen(QTAGUID_IFACE_STATS, "r"); |
| 79 | if (fp == NULL) { |
| 80 | return -1; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 81 | } |
Kazuhiro Ondo | 64ba5ea | 2011-07-05 16:12:10 -0500 | [diff] [blame] | 82 | |
Jeff Sharkey | 8669a16 | 2013-02-07 15:02:55 -0800 | [diff] [blame] | 83 | char buffer[384]; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 84 | char cur_iface[32]; |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 85 | bool foundTcp = false; |
| 86 | uint64_t rxBytes, rxPackets, txBytes, txPackets, tcpRxPackets, tcpTxPackets; |
Kazuhiro Ondo | 64ba5ea | 2011-07-05 16:12:10 -0500 | [diff] [blame] | 87 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 88 | while (fgets(buffer, sizeof(buffer), fp) != NULL) { |
| 89 | int matched = sscanf(buffer, "%31s %llu %llu %llu %llu " |
Jeff Sharkey | 9de56d2 | 2013-02-08 16:10:21 -0800 | [diff] [blame] | 90 | "%*u %llu %*u %*u %*u %*u " |
| 91 | "%*u %llu %*u %*u %*u %*u", cur_iface, &rxBytes, |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 92 | &rxPackets, &txBytes, &txPackets, &tcpRxPackets, &tcpTxPackets); |
| 93 | if (matched >= 5) { |
| 94 | if (matched == 7) { |
| 95 | foundTcp = true; |
| 96 | } |
| 97 | if (!iface || !strcmp(iface, cur_iface)) { |
| 98 | stats->rxBytes += rxBytes; |
| 99 | stats->rxPackets += rxPackets; |
| 100 | stats->txBytes += txBytes; |
| 101 | stats->txPackets += txPackets; |
| 102 | if (matched == 7) { |
| 103 | stats->tcpRxPackets += tcpRxPackets; |
| 104 | stats->tcpTxPackets += tcpTxPackets; |
| 105 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 106 | } |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 110 | if (!foundTcp) { |
| 111 | stats->tcpRxPackets = UNKNOWN; |
| 112 | stats->tcpTxPackets = UNKNOWN; |
| 113 | } |
| 114 | |
| 115 | if (fclose(fp) != 0) { |
| 116 | return -1; |
| 117 | } |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 118 | return 0; |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 121 | static int parseUidStats(const uint32_t uid, struct Stats* stats) { |
| 122 | FILE *fp = fopen(QTAGUID_UID_STATS, "r"); |
| 123 | if (fp == NULL) { |
| 124 | return -1; |
| 125 | } |
| 126 | |
Jeff Sharkey | 8669a16 | 2013-02-07 15:02:55 -0800 | [diff] [blame] | 127 | char buffer[384]; |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 128 | char iface[32]; |
| 129 | uint32_t idx, cur_uid, set; |
| 130 | uint64_t tag, rxBytes, rxPackets, txBytes, txPackets; |
| 131 | |
Jeff Sharkey | 8669a16 | 2013-02-07 15:02:55 -0800 | [diff] [blame] | 132 | while (fgets(buffer, sizeof(buffer), fp) != NULL) { |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 133 | if (sscanf(buffer, "%d %31s 0x%llx %u %u %llu %llu %llu %llu", &idx, |
| 134 | iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets, &txBytes, |
| 135 | &txPackets) == 9) { |
| 136 | if (uid == cur_uid && tag == 0L) { |
| 137 | stats->rxBytes += rxBytes; |
| 138 | stats->rxPackets += rxPackets; |
| 139 | stats->txBytes += txBytes; |
| 140 | stats->txPackets += txPackets; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (fclose(fp) != 0) { |
| 146 | return -1; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 151 | static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) { |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 152 | struct Stats stats; |
| 153 | memset(&stats, 0, sizeof(Stats)); |
| 154 | if (parseIfaceStats(NULL, &stats) == 0) { |
| 155 | return getStatsType(&stats, (StatsType) type); |
| 156 | } else { |
| 157 | return UNKNOWN; |
| 158 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 161 | static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) { |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 162 | ScopedUtfChars iface8(env, iface); |
| 163 | if (iface8.c_str() == NULL) { |
| 164 | return UNKNOWN; |
| 165 | } |
| 166 | |
| 167 | struct Stats stats; |
| 168 | memset(&stats, 0, sizeof(Stats)); |
JP Abgrall | 62f16bf | 2013-03-01 23:38:40 -0800 | [diff] [blame] | 169 | if (parseIfaceStats(iface8.c_str(), &stats) == 0) { |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 170 | return getStatsType(&stats, (StatsType) type); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 171 | } else { |
Jeff Sharkey | 4b17a13 | 2013-02-05 21:32:33 -0800 | [diff] [blame] | 172 | return UNKNOWN; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 173 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 176 | static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) { |
| 177 | struct Stats stats; |
| 178 | memset(&stats, 0, sizeof(Stats)); |
| 179 | if (parseUidStats(uid, &stats) == 0) { |
| 180 | return getStatsType(&stats, (StatsType) type); |
| 181 | } else { |
| 182 | return UNKNOWN; |
Ashish Sharma | c39c1d4 | 2011-01-27 15:52:38 -0800 | [diff] [blame] | 183 | } |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static JNINativeMethod gMethods[] = { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 187 | {"nativeGetTotalStat", "(I)J", (void*) getTotalStat}, |
| 188 | {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*) getIfaceStat}, |
Jeff Sharkey | 92be93a | 2013-01-15 17:25:09 -0800 | [diff] [blame] | 189 | {"nativeGetUidStat", "(II)J", (void*) getUidStat}, |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | int register_android_net_TrafficStats(JNIEnv* env) { |
| 193 | return AndroidRuntime::registerNativeMethods(env, "android/net/TrafficStats", |
| 194 | gMethods, NELEM(gMethods)); |
| 195 | } |
| 196 | |
| 197 | } |