blob: 3302dea53506ff25ebdcfb9b736af89c5bd25feb [file] [log] [blame]
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001/*
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
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -080017#define LOG_TAG "NetworkStatsNative"
Dan Egnor2b4abcd2010-04-07 17:30:50 -070018
19#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
Mark Salyzyn85394032014-04-16 10:28:37 -070022#include <inttypes.h>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070023#include <sys/stat.h>
24#include <sys/types.h>
25
Andreas Gampeed6b9df2014-11-20 22:02:20 -080026#include "core_jni_helpers.h"
Dan Egnor2b4abcd2010-04-07 17:30:50 -070027#include <jni.h>
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/ScopedUtfChars.h>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070029#include <utils/misc.h>
30#include <utils/Log.h>
31
Chenbo Fengaedd6a32017-11-20 18:23:46 -080032#include "android-base/unique_fd.h"
33#include "bpf/BpfNetworkStats.h"
34#include "bpf/BpfUtils.h"
35
36using android::bpf::Stats;
37using android::bpf::hasBpfSupport;
38using android::bpf::bpfGetUidStats;
39using android::bpf::bpfGetIfaceStats;
40
Dan Egnor2b4abcd2010-04-07 17:30:50 -070041namespace android {
42
Jeff Sharkey4b17a132013-02-05 21:32:33 -080043static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt";
Jeff Sharkey92be93a2013-01-15 17:25:09 -080044static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats";
Ashish Sharmac39c1d42011-01-27 15:52:38 -080045
Jeff Sharkey234766a2012-04-10 19:48:07 -070046// NOTE: keep these in sync with TrafficStats.java
Jeff Sharkey4b17a132013-02-05 21:32:33 -080047static const uint64_t UNKNOWN = -1;
48
49enum StatsType {
Jeff Sharkey234766a2012-04-10 19:48:07 -070050 RX_BYTES = 0,
51 RX_PACKETS = 1,
52 TX_BYTES = 2,
Jeff Sharkey4b17a132013-02-05 21:32:33 -080053 TX_PACKETS = 3,
54 TCP_RX_PACKETS = 4,
55 TCP_TX_PACKETS = 5
Jeff Sharkey234766a2012-04-10 19:48:07 -070056};
57
Jeff Sharkey4b17a132013-02-05 21:32:33 -080058static 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 Sharkey4b17a132013-02-05 21:32:33 -080077static int parseIfaceStats(const char* iface, struct Stats* stats) {
78 FILE *fp = fopen(QTAGUID_IFACE_STATS, "r");
79 if (fp == NULL) {
80 return -1;
Jeff Sharkey234766a2012-04-10 19:48:07 -070081 }
Kazuhiro Ondo64ba5ea2011-07-05 16:12:10 -050082
Jeff Sharkey8669a162013-02-07 15:02:55 -080083 char buffer[384];
Jeff Sharkey234766a2012-04-10 19:48:07 -070084 char cur_iface[32];
Jeff Sharkey4b17a132013-02-05 21:32:33 -080085 bool foundTcp = false;
86 uint64_t rxBytes, rxPackets, txBytes, txPackets, tcpRxPackets, tcpTxPackets;
Kazuhiro Ondo64ba5ea2011-07-05 16:12:10 -050087
Jeff Sharkey4b17a132013-02-05 21:32:33 -080088 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
Mark Salyzyn85394032014-04-16 10:28:37 -070089 int matched = sscanf(buffer, "%31s %" SCNu64 " %" SCNu64 " %" SCNu64
90 " %" SCNu64 " " "%*u %" SCNu64 " %*u %*u %*u %*u "
91 "%*u %" SCNu64 " %*u %*u %*u %*u", cur_iface, &rxBytes,
Jeff Sharkey4b17a132013-02-05 21:32:33 -080092 &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 Sharkey234766a2012-04-10 19:48:07 -0700106 }
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700107 }
108 }
109
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800110 if (!foundTcp) {
111 stats->tcpRxPackets = UNKNOWN;
112 stats->tcpTxPackets = UNKNOWN;
113 }
114
115 if (fclose(fp) != 0) {
116 return -1;
117 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700118 return 0;
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700119}
120
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800121static 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 Sharkey8669a162013-02-07 15:02:55 -0800127 char buffer[384];
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800128 char iface[32];
129 uint32_t idx, cur_uid, set;
130 uint64_t tag, rxBytes, rxPackets, txBytes, txPackets;
131
Jeff Sharkey8669a162013-02-07 15:02:55 -0800132 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
Mark Salyzyn85394032014-04-16 10:28:37 -0700133 if (sscanf(buffer,
134 "%" SCNu32 " %31s 0x%" SCNx64 " %u %u %" SCNu64 " %" SCNu64
135 " %" SCNu64 " %" SCNu64 "",
136 &idx, iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets,
137 &txBytes, &txPackets) == 9) {
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800138 if (uid == cur_uid && tag == 0L) {
139 stats->rxBytes += rxBytes;
140 stats->rxPackets += rxPackets;
141 stats->txBytes += txBytes;
142 stats->txPackets += txPackets;
143 }
144 }
145 }
146
147 if (fclose(fp) != 0) {
148 return -1;
149 }
150 return 0;
151}
152
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800153static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type, jboolean useBpfStats) {
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800154 struct Stats stats;
155 memset(&stats, 0, sizeof(Stats));
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800156
157 if (useBpfStats) {
158 if (bpfGetIfaceStats(NULL, &stats) == 0) {
159 return getStatsType(&stats, (StatsType) type);
160 } else {
161 return UNKNOWN;
162 }
163 }
164
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800165 if (parseIfaceStats(NULL, &stats) == 0) {
166 return getStatsType(&stats, (StatsType) type);
167 } else {
168 return UNKNOWN;
169 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800170}
171
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800172static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
173 jboolean useBpfStats) {
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800174 ScopedUtfChars iface8(env, iface);
175 if (iface8.c_str() == NULL) {
176 return UNKNOWN;
177 }
178
179 struct Stats stats;
180 memset(&stats, 0, sizeof(Stats));
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800181
182 if (useBpfStats) {
183 if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
184 return getStatsType(&stats, (StatsType) type);
185 } else {
186 return UNKNOWN;
187 }
188 }
189
JP Abgrall62f16bf2013-03-01 23:38:40 -0800190 if (parseIfaceStats(iface8.c_str(), &stats) == 0) {
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800191 return getStatsType(&stats, (StatsType) type);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700192 } else {
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800193 return UNKNOWN;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700194 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800195}
196
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800197static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type, jboolean useBpfStats) {
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800198 struct Stats stats;
199 memset(&stats, 0, sizeof(Stats));
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800200
201 if (useBpfStats) {
202 if (bpfGetUidStats(uid, &stats) == 0) {
203 return getStatsType(&stats, (StatsType) type);
204 } else {
205 return UNKNOWN;
206 }
207 }
208
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800209 if (parseUidStats(uid, &stats) == 0) {
210 return getStatsType(&stats, (StatsType) type);
211 } else {
212 return UNKNOWN;
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800213 }
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700214}
215
Daniel Micay76f6a862015-09-19 17:31:01 -0400216static const JNINativeMethod gMethods[] = {
Chenbo Fengaedd6a32017-11-20 18:23:46 -0800217 {"nativeGetTotalStat", "(IZ)J", (void*) getTotalStat},
218 {"nativeGetIfaceStat", "(Ljava/lang/String;IZ)J", (void*) getIfaceStat},
219 {"nativeGetUidStat", "(IIZ)J", (void*) getUidStat},
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700220};
221
Chenbo Fengd3d9c4e2017-11-14 17:54:17 -0800222int register_android_server_net_NetworkStatsService(JNIEnv* env) {
223 jclass netStatsService = env->FindClass("com/android/server/net/NetworkStatsService");
224 jfieldID rxBytesId = env->GetStaticFieldID(netStatsService, "TYPE_RX_BYTES", "I");
225 jfieldID rxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_RX_PACKETS", "I");
226 jfieldID txBytesId = env->GetStaticFieldID(netStatsService, "TYPE_TX_BYTES", "I");
227 jfieldID txPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TX_PACKETS", "I");
228 jfieldID tcpRxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_RX_PACKETS", "I");
229 jfieldID tcpTxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_TX_PACKETS", "I");
230
231 env->SetStaticIntField(netStatsService, rxBytesId, RX_BYTES);
232 env->SetStaticIntField(netStatsService, rxPacketsId, RX_PACKETS);
233 env->SetStaticIntField(netStatsService, txBytesId, TX_BYTES);
234 env->SetStaticIntField(netStatsService, txPacketsId, TX_PACKETS);
235 env->SetStaticIntField(netStatsService, tcpRxPacketsId, TCP_RX_PACKETS);
236 env->SetStaticIntField(netStatsService, tcpTxPacketsId, TCP_TX_PACKETS);
237
238 return jniRegisterNativeMethods(env, "com/android/server/net/NetworkStatsService", gMethods,
239 NELEM(gMethods));
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700240}
241
242}