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 <jni.h> 27 #include <ScopedUtfChars.h> 28 #include <utils/misc.h> 29 #include <utils/Log.h> 30 31 namespace android { 32 33 static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt"; 34 static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats"; 35 36 // NOTE: keep these in sync with TrafficStats.java 37 static const uint64_t UNKNOWN = -1; 38 39 enum StatsType { 40 RX_BYTES = 0, 41 RX_PACKETS = 1, 42 TX_BYTES = 2, 43 TX_PACKETS = 3, 44 TCP_RX_PACKETS = 4, 45 TCP_TX_PACKETS = 5 46 }; 47 48 struct Stats { 49 uint64_t rxBytes; 50 uint64_t rxPackets; 51 uint64_t txBytes; 52 uint64_t txPackets; 53 uint64_t tcpRxPackets; 54 uint64_t tcpTxPackets; 55 }; 56 57 static uint64_t getStatsType(struct Stats* stats, StatsType type) { 58 switch (type) { 59 case RX_BYTES: 60 return stats->rxBytes; 61 case RX_PACKETS: 62 return stats->rxPackets; 63 case TX_BYTES: 64 return stats->txBytes; 65 case TX_PACKETS: 66 return stats->txPackets; 67 case TCP_RX_PACKETS: 68 return stats->tcpRxPackets; 69 case TCP_TX_PACKETS: 70 return stats->tcpTxPackets; 71 default: 72 return UNKNOWN; 73 } 74 } 75 76 static int parseIfaceStats(const char* iface, struct Stats* stats) { 77 FILE *fp = fopen(QTAGUID_IFACE_STATS, "r"); 78 if (fp == NULL) { 79 return -1; 80 } 81 82 char buffer[384]; 83 char cur_iface[32]; 84 bool foundTcp = false; 85 uint64_t rxBytes, rxPackets, txBytes, txPackets, tcpRxPackets, tcpTxPackets; 86 87 while (fgets(buffer, sizeof(buffer), fp) != NULL) { 88 int matched = sscanf(buffer, "%31s %llu %llu %llu %llu " 89 "%*u %llu %*u %*u %*u %*u " 90 "%*u %llu %*u %*u %*u %*u", cur_iface, &rxBytes, 91 &rxPackets, &txBytes, &txPackets, &tcpRxPackets, &tcpTxPackets); 92 if (matched >= 5) { 93 if (matched == 7) { 94 foundTcp = true; 95 } 96 if (!iface || !strcmp(iface, cur_iface)) { 97 stats->rxBytes += rxBytes; 98 stats->rxPackets += rxPackets; 99 stats->txBytes += txBytes; 100 stats->txPackets += txPackets; 101 if (matched == 7) { 102 stats->tcpRxPackets += tcpRxPackets; 103 stats->tcpTxPackets += tcpTxPackets; 104 } 105 } 106 } 107 } 108 109 if (!foundTcp) { 110 stats->tcpRxPackets = UNKNOWN; 111 stats->tcpTxPackets = UNKNOWN; 112 } 113 114 if (fclose(fp) != 0) { 115 return -1; 116 } 117 return 0; 118 } 119 120 static int parseUidStats(const uint32_t uid, struct Stats* stats) { 121 FILE *fp = fopen(QTAGUID_UID_STATS, "r"); 122 if (fp == NULL) { 123 return -1; 124 } 125 126 char buffer[384]; 127 char iface[32]; 128 uint32_t idx, cur_uid, set; 129 uint64_t tag, rxBytes, rxPackets, txBytes, txPackets; 130 131 while (fgets(buffer, sizeof(buffer), fp) != NULL) { 132 if (sscanf(buffer, "%d %31s 0x%llx %u %u %llu %llu %llu %llu", &idx, 133 iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets, &txBytes, 134 &txPackets) == 9) { 135 if (uid == cur_uid && tag == 0L) { 136 stats->rxBytes += rxBytes; 137 stats->rxPackets += rxPackets; 138 stats->txBytes += txBytes; 139 stats->txPackets += txPackets; 140 } 141 } 142 } 143 144 if (fclose(fp) != 0) { 145 return -1; 146 } 147 return 0; 148 } 149 150 static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) { 151 struct Stats stats; 152 memset(&stats, 0, sizeof(Stats)); 153 if (parseIfaceStats(NULL, &stats) == 0) { 154 return getStatsType(&stats, (StatsType) type); 155 } else { 156 return UNKNOWN; 157 } 158 } 159 160 static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) { 161 ScopedUtfChars iface8(env, iface); 162 if (iface8.c_str() == NULL) { 163 return UNKNOWN; 164 } 165 166 struct Stats stats; 167 memset(&stats, 0, sizeof(Stats)); 168 if (parseIfaceStats(iface8.c_str(), &stats) == 0) { 169 return getStatsType(&stats, (StatsType) type); 170 } else { 171 return UNKNOWN; 172 } 173 } 174 175 static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) { 176 struct Stats stats; 177 memset(&stats, 0, sizeof(Stats)); 178 if (parseUidStats(uid, &stats) == 0) { 179 return getStatsType(&stats, (StatsType) type); 180 } else { 181 return UNKNOWN; 182 } 183 } 184 185 static JNINativeMethod gMethods[] = { 186 {"nativeGetTotalStat", "(I)J", (void*) getTotalStat}, 187 {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*) getIfaceStat}, 188 {"nativeGetUidStat", "(II)J", (void*) getUidStat}, 189 }; 190 191 int register_android_net_TrafficStats(JNIEnv* env) { 192 return AndroidRuntime::registerNativeMethods(env, "android/net/TrafficStats", 193 gMethods, NELEM(gMethods)); 194 } 195 196 } 197