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 "NetworkStatsNative"
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include "core_jni_helpers.h"
27 #include <jni.h>
28 #include <nativehelper/ScopedUtfChars.h>
29 #include <utils/misc.h>
30 #include <utils/Log.h>
31
32 #include "android-base/unique_fd.h"
33 #include "bpf/BpfUtils.h"
34 #include "netdbpf/BpfNetworkStats.h"
35
36 using android::bpf::Stats;
37 using android::bpf::bpfGetUidStats;
38 using android::bpf::bpfGetIfaceStats;
39
40 namespace android {
41
42 static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt";
43 static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats";
44
45 // NOTE: keep these in sync with TrafficStats.java
46 static const uint64_t UNKNOWN = -1;
47
48 enum StatsType {
49 RX_BYTES = 0,
50 RX_PACKETS = 1,
51 TX_BYTES = 2,
52 TX_PACKETS = 3,
53 TCP_RX_PACKETS = 4,
54 TCP_TX_PACKETS = 5
55 };
56
getStatsType(struct Stats * stats,StatsType type)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
parseIfaceStats(const char * iface,struct Stats * stats)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 %" SCNu64 " %" SCNu64 " %" SCNu64
89 " %" SCNu64 " " "%*u %" SCNu64 " %*u %*u %*u %*u "
90 "%*u %" SCNu64 " %*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
parseUidStats(const uint32_t uid,struct Stats * stats)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,
133 "%" SCNu32 " %31s 0x%" SCNx64 " %u %u %" SCNu64 " %" SCNu64
134 " %" SCNu64 " %" SCNu64 "",
135 &idx, iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets,
136 &txBytes, &txPackets) == 9) {
137 if (uid == cur_uid && tag == 0L) {
138 stats->rxBytes += rxBytes;
139 stats->rxPackets += rxPackets;
140 stats->txBytes += txBytes;
141 stats->txPackets += txPackets;
142 }
143 }
144 }
145
146 if (fclose(fp) != 0) {
147 return -1;
148 }
149 return 0;
150 }
151
getTotalStat(JNIEnv * env,jclass clazz,jint type,jboolean useBpfStats)152 static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type, jboolean useBpfStats) {
153 struct Stats stats;
154 memset(&stats, 0, sizeof(Stats));
155
156 if (useBpfStats) {
157 if (bpfGetIfaceStats(NULL, &stats) == 0) {
158 return getStatsType(&stats, (StatsType) type);
159 } else {
160 return UNKNOWN;
161 }
162 }
163
164 if (parseIfaceStats(NULL, &stats) == 0) {
165 return getStatsType(&stats, (StatsType) type);
166 } else {
167 return UNKNOWN;
168 }
169 }
170
getIfaceStat(JNIEnv * env,jclass clazz,jstring iface,jint type,jboolean useBpfStats)171 static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
172 jboolean useBpfStats) {
173 ScopedUtfChars iface8(env, iface);
174 if (iface8.c_str() == NULL) {
175 return UNKNOWN;
176 }
177
178 struct Stats stats;
179 memset(&stats, 0, sizeof(Stats));
180
181 if (useBpfStats) {
182 if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
183 return getStatsType(&stats, (StatsType) type);
184 } else {
185 return UNKNOWN;
186 }
187 }
188
189 if (parseIfaceStats(iface8.c_str(), &stats) == 0) {
190 return getStatsType(&stats, (StatsType) type);
191 } else {
192 return UNKNOWN;
193 }
194 }
195
getUidStat(JNIEnv * env,jclass clazz,jint uid,jint type,jboolean useBpfStats)196 static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type, jboolean useBpfStats) {
197 struct Stats stats;
198 memset(&stats, 0, sizeof(Stats));
199
200 if (useBpfStats) {
201 if (bpfGetUidStats(uid, &stats) == 0) {
202 return getStatsType(&stats, (StatsType) type);
203 } else {
204 return UNKNOWN;
205 }
206 }
207
208 if (parseUidStats(uid, &stats) == 0) {
209 return getStatsType(&stats, (StatsType) type);
210 } else {
211 return UNKNOWN;
212 }
213 }
214
215 static const JNINativeMethod gMethods[] = {
216 {"nativeGetTotalStat", "(IZ)J", (void*) getTotalStat},
217 {"nativeGetIfaceStat", "(Ljava/lang/String;IZ)J", (void*) getIfaceStat},
218 {"nativeGetUidStat", "(IIZ)J", (void*) getUidStat},
219 };
220
register_android_server_net_NetworkStatsService(JNIEnv * env)221 int register_android_server_net_NetworkStatsService(JNIEnv* env) {
222 jclass netStatsService = env->FindClass("com/android/server/net/NetworkStatsService");
223 jfieldID rxBytesId = env->GetStaticFieldID(netStatsService, "TYPE_RX_BYTES", "I");
224 jfieldID rxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_RX_PACKETS", "I");
225 jfieldID txBytesId = env->GetStaticFieldID(netStatsService, "TYPE_TX_BYTES", "I");
226 jfieldID txPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TX_PACKETS", "I");
227 jfieldID tcpRxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_RX_PACKETS", "I");
228 jfieldID tcpTxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_TX_PACKETS", "I");
229
230 env->SetStaticIntField(netStatsService, rxBytesId, RX_BYTES);
231 env->SetStaticIntField(netStatsService, rxPacketsId, RX_PACKETS);
232 env->SetStaticIntField(netStatsService, txBytesId, TX_BYTES);
233 env->SetStaticIntField(netStatsService, txPacketsId, TX_PACKETS);
234 env->SetStaticIntField(netStatsService, tcpRxPacketsId, TCP_RX_PACKETS);
235 env->SetStaticIntField(netStatsService, tcpTxPacketsId, TCP_TX_PACKETS);
236
237 return jniRegisterNativeMethods(env, "com/android/server/net/NetworkStatsService", gMethods,
238 NELEM(gMethods));
239 }
240
241 }
242