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::bpfGetUidStats;
37 using android::bpf::bpfGetIfaceStats;
38 
39 namespace android {
40 
41 static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt";
42 static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats";
43 
44 // NOTE: keep these in sync with TrafficStats.java
45 static const uint64_t UNKNOWN = -1;
46 
47 enum StatsType {
48     RX_BYTES = 0,
49     RX_PACKETS = 1,
50     TX_BYTES = 2,
51     TX_PACKETS = 3,
52     TCP_RX_PACKETS = 4,
53     TCP_TX_PACKETS = 5
54 };
55 
getStatsType(Stats * stats,StatsType type)56 static uint64_t getStatsType(Stats* stats, StatsType type) {
57     switch (type) {
58         case RX_BYTES:
59             return stats->rxBytes;
60         case RX_PACKETS:
61             return stats->rxPackets;
62         case TX_BYTES:
63             return stats->txBytes;
64         case TX_PACKETS:
65             return stats->txPackets;
66         case TCP_RX_PACKETS:
67             return stats->tcpRxPackets;
68         case TCP_TX_PACKETS:
69             return stats->tcpTxPackets;
70         default:
71             return UNKNOWN;
72     }
73 }
74 
parseIfaceStats(const char * iface,Stats * stats)75 static int parseIfaceStats(const char* iface, Stats* stats) {
76     FILE *fp = fopen(QTAGUID_IFACE_STATS, "r");
77     if (fp == NULL) {
78         return -1;
79     }
80 
81     char buffer[384];
82     char cur_iface[32];
83     bool foundTcp = false;
84     uint64_t rxBytes, rxPackets, txBytes, txPackets, tcpRxPackets, tcpTxPackets;
85 
86     while (fgets(buffer, sizeof(buffer), fp) != NULL) {
87         int matched = sscanf(buffer, "%31s %" SCNu64 " %" SCNu64 " %" SCNu64
88                 " %" SCNu64 " " "%*u %" SCNu64 " %*u %*u %*u %*u "
89                 "%*u %" SCNu64 " %*u %*u %*u %*u", cur_iface, &rxBytes,
90                 &rxPackets, &txBytes, &txPackets, &tcpRxPackets, &tcpTxPackets);
91         if (matched >= 5) {
92             if (matched == 7) {
93                 foundTcp = true;
94             }
95             if (!iface || !strcmp(iface, cur_iface)) {
96                 stats->rxBytes += rxBytes;
97                 stats->rxPackets += rxPackets;
98                 stats->txBytes += txBytes;
99                 stats->txPackets += txPackets;
100                 if (matched == 7) {
101                     stats->tcpRxPackets += tcpRxPackets;
102                     stats->tcpTxPackets += tcpTxPackets;
103                 }
104             }
105         }
106     }
107 
108     if (!foundTcp) {
109         stats->tcpRxPackets = UNKNOWN;
110         stats->tcpTxPackets = UNKNOWN;
111     }
112 
113     if (fclose(fp) != 0) {
114         return -1;
115     }
116     return 0;
117 }
118 
parseUidStats(const uint32_t uid,Stats * stats)119 static int parseUidStats(const uint32_t uid, Stats* stats) {
120     FILE *fp = fopen(QTAGUID_UID_STATS, "r");
121     if (fp == NULL) {
122         return -1;
123     }
124 
125     char buffer[384];
126     char iface[32];
127     uint32_t idx, cur_uid, set;
128     uint64_t tag, rxBytes, rxPackets, txBytes, txPackets;
129 
130     while (fgets(buffer, sizeof(buffer), fp) != NULL) {
131         if (sscanf(buffer,
132                 "%" SCNu32 " %31s 0x%" SCNx64 " %u %u %" SCNu64 " %" SCNu64
133                 " %" SCNu64 " %" SCNu64 "",
134                 &idx, iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets,
135                 &txBytes, &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 
getTotalStat(JNIEnv * env,jclass clazz,jint type,jboolean useBpfStats)151 static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type, jboolean useBpfStats) {
152     Stats stats = {};
153 
154     if (useBpfStats) {
155         if (bpfGetIfaceStats(NULL, &stats) == 0) {
156             return getStatsType(&stats, (StatsType) type);
157         } else {
158             return UNKNOWN;
159         }
160     }
161 
162     if (parseIfaceStats(NULL, &stats) == 0) {
163         return getStatsType(&stats, (StatsType) type);
164     } else {
165         return UNKNOWN;
166     }
167 }
168 
getIfaceStat(JNIEnv * env,jclass clazz,jstring iface,jint type,jboolean useBpfStats)169 static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
170                           jboolean useBpfStats) {
171     ScopedUtfChars iface8(env, iface);
172     if (iface8.c_str() == NULL) {
173         return UNKNOWN;
174     }
175 
176     Stats stats = {};
177 
178     if (useBpfStats) {
179         if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
180             return getStatsType(&stats, (StatsType) type);
181         } else {
182             return UNKNOWN;
183         }
184     }
185 
186     if (parseIfaceStats(iface8.c_str(), &stats) == 0) {
187         return getStatsType(&stats, (StatsType) type);
188     } else {
189         return UNKNOWN;
190     }
191 }
192 
getUidStat(JNIEnv * env,jclass clazz,jint uid,jint type,jboolean useBpfStats)193 static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type, jboolean useBpfStats) {
194     Stats stats = {};
195 
196     if (useBpfStats) {
197         if (bpfGetUidStats(uid, &stats) == 0) {
198             return getStatsType(&stats, (StatsType) type);
199         } else {
200             return UNKNOWN;
201         }
202     }
203 
204     if (parseUidStats(uid, &stats) == 0) {
205         return getStatsType(&stats, (StatsType) type);
206     } else {
207         return UNKNOWN;
208     }
209 }
210 
211 static const JNINativeMethod gMethods[] = {
212     {"nativeGetTotalStat", "(IZ)J", (void*) getTotalStat},
213     {"nativeGetIfaceStat", "(Ljava/lang/String;IZ)J", (void*) getIfaceStat},
214     {"nativeGetUidStat", "(IIZ)J", (void*) getUidStat},
215 };
216 
register_android_server_net_NetworkStatsService(JNIEnv * env)217 int register_android_server_net_NetworkStatsService(JNIEnv* env) {
218     jclass netStatsService = env->FindClass("com/android/server/net/NetworkStatsService");
219     jfieldID rxBytesId = env->GetStaticFieldID(netStatsService, "TYPE_RX_BYTES", "I");
220     jfieldID rxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_RX_PACKETS", "I");
221     jfieldID txBytesId = env->GetStaticFieldID(netStatsService, "TYPE_TX_BYTES", "I");
222     jfieldID txPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TX_PACKETS", "I");
223     jfieldID tcpRxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_RX_PACKETS", "I");
224     jfieldID tcpTxPacketsId = env->GetStaticFieldID(netStatsService, "TYPE_TCP_TX_PACKETS", "I");
225 
226     env->SetStaticIntField(netStatsService, rxBytesId, RX_BYTES);
227     env->SetStaticIntField(netStatsService, rxPacketsId, RX_PACKETS);
228     env->SetStaticIntField(netStatsService, txBytesId, TX_BYTES);
229     env->SetStaticIntField(netStatsService, txPacketsId, TX_PACKETS);
230     env->SetStaticIntField(netStatsService, tcpRxPacketsId, TCP_RX_PACKETS);
231     env->SetStaticIntField(netStatsService, tcpTxPacketsId, TCP_TX_PACKETS);
232 
233     return jniRegisterNativeMethods(env, "com/android/server/net/NetworkStatsService", gMethods,
234                                     NELEM(gMethods));
235 }
236 
237 }
238