1 /*
2 * Copyright (C) 2013 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 "NetworkStats"
18
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <vector>
24
25 #include <jni.h>
26
27 #include <nativehelper/JNIHelp.h>
28 #include <nativehelper/ScopedUtfChars.h>
29 #include <nativehelper/ScopedLocalRef.h>
30 #include <nativehelper/ScopedPrimitiveArray.h>
31
32 #include <utils/Log.h>
33 #include <utils/misc.h>
34
35 #include "android-base/unique_fd.h"
36 #include "bpf/BpfUtils.h"
37 #include "netdbpf/BpfNetworkStats.h"
38
39 using android::bpf::parseBpfNetworkStatsDetail;
40 using android::bpf::stats_line;
41
42 namespace android {
43
44 static jclass gStringClass;
45
46 static struct {
47 jfieldID size;
48 jfieldID capacity;
49 jfieldID iface;
50 jfieldID uid;
51 jfieldID set;
52 jfieldID tag;
53 jfieldID metered;
54 jfieldID roaming;
55 jfieldID defaultNetwork;
56 jfieldID rxBytes;
57 jfieldID rxPackets;
58 jfieldID txBytes;
59 jfieldID txPackets;
60 jfieldID operations;
61 } gNetworkStatsClassInfo;
62
get_string_array(JNIEnv * env,jobject obj,jfieldID field,int size,bool grow)63 static jobjectArray get_string_array(JNIEnv* env, jobject obj, jfieldID field, int size, bool grow)
64 {
65 if (!grow) {
66 jobjectArray array = (jobjectArray)env->GetObjectField(obj, field);
67 if (array != NULL) {
68 return array;
69 }
70 }
71 return env->NewObjectArray(size, gStringClass, NULL);
72 }
73
get_int_array(JNIEnv * env,jobject obj,jfieldID field,int size,bool grow)74 static jintArray get_int_array(JNIEnv* env, jobject obj, jfieldID field, int size, bool grow)
75 {
76 if (!grow) {
77 jintArray array = (jintArray)env->GetObjectField(obj, field);
78 if (array != NULL) {
79 return array;
80 }
81 }
82 return env->NewIntArray(size);
83 }
84
get_long_array(JNIEnv * env,jobject obj,jfieldID field,int size,bool grow)85 static jlongArray get_long_array(JNIEnv* env, jobject obj, jfieldID field, int size, bool grow)
86 {
87 if (!grow) {
88 jlongArray array = (jlongArray)env->GetObjectField(obj, field);
89 if (array != NULL) {
90 return array;
91 }
92 }
93 return env->NewLongArray(size);
94 }
95
statsLinesToNetworkStats(JNIEnv * env,jclass clazz,jobject stats,std::vector<stats_line> & lines)96 static int statsLinesToNetworkStats(JNIEnv* env, jclass clazz, jobject stats,
97 std::vector<stats_line>& lines) {
98 int size = lines.size();
99
100 bool grow = size > env->GetIntField(stats, gNetworkStatsClassInfo.capacity);
101
102 ScopedLocalRef<jobjectArray> iface(env, get_string_array(env, stats,
103 gNetworkStatsClassInfo.iface, size, grow));
104 if (iface.get() == NULL) return -1;
105 ScopedIntArrayRW uid(env, get_int_array(env, stats,
106 gNetworkStatsClassInfo.uid, size, grow));
107 if (uid.get() == NULL) return -1;
108 ScopedIntArrayRW set(env, get_int_array(env, stats,
109 gNetworkStatsClassInfo.set, size, grow));
110 if (set.get() == NULL) return -1;
111 ScopedIntArrayRW tag(env, get_int_array(env, stats,
112 gNetworkStatsClassInfo.tag, size, grow));
113 if (tag.get() == NULL) return -1;
114 ScopedIntArrayRW metered(env, get_int_array(env, stats,
115 gNetworkStatsClassInfo.metered, size, grow));
116 if (metered.get() == NULL) return -1;
117 ScopedIntArrayRW roaming(env, get_int_array(env, stats,
118 gNetworkStatsClassInfo.roaming, size, grow));
119 if (roaming.get() == NULL) return -1;
120 ScopedIntArrayRW defaultNetwork(env, get_int_array(env, stats,
121 gNetworkStatsClassInfo.defaultNetwork, size, grow));
122 if (defaultNetwork.get() == NULL) return -1;
123 ScopedLongArrayRW rxBytes(env, get_long_array(env, stats,
124 gNetworkStatsClassInfo.rxBytes, size, grow));
125 if (rxBytes.get() == NULL) return -1;
126 ScopedLongArrayRW rxPackets(env, get_long_array(env, stats,
127 gNetworkStatsClassInfo.rxPackets, size, grow));
128 if (rxPackets.get() == NULL) return -1;
129 ScopedLongArrayRW txBytes(env, get_long_array(env, stats,
130 gNetworkStatsClassInfo.txBytes, size, grow));
131 if (txBytes.get() == NULL) return -1;
132 ScopedLongArrayRW txPackets(env, get_long_array(env, stats,
133 gNetworkStatsClassInfo.txPackets, size, grow));
134 if (txPackets.get() == NULL) return -1;
135 ScopedLongArrayRW operations(env, get_long_array(env, stats,
136 gNetworkStatsClassInfo.operations, size, grow));
137 if (operations.get() == NULL) return -1;
138
139 for (int i = 0; i < size; i++) {
140 ScopedLocalRef<jstring> ifaceString(env, env->NewStringUTF(lines[i].iface));
141 env->SetObjectArrayElement(iface.get(), i, ifaceString.get());
142
143 uid[i] = lines[i].uid;
144 set[i] = lines[i].set;
145 tag[i] = lines[i].tag;
146 // Metered, roaming and defaultNetwork are populated in Java-land.
147 rxBytes[i] = lines[i].rxBytes;
148 rxPackets[i] = lines[i].rxPackets;
149 txBytes[i] = lines[i].txBytes;
150 txPackets[i] = lines[i].txPackets;
151 }
152
153 env->SetIntField(stats, gNetworkStatsClassInfo.size, size);
154 if (grow) {
155 env->SetIntField(stats, gNetworkStatsClassInfo.capacity, size);
156 env->SetObjectField(stats, gNetworkStatsClassInfo.iface, iface.get());
157 env->SetObjectField(stats, gNetworkStatsClassInfo.uid, uid.getJavaArray());
158 env->SetObjectField(stats, gNetworkStatsClassInfo.set, set.getJavaArray());
159 env->SetObjectField(stats, gNetworkStatsClassInfo.tag, tag.getJavaArray());
160 env->SetObjectField(stats, gNetworkStatsClassInfo.metered, metered.getJavaArray());
161 env->SetObjectField(stats, gNetworkStatsClassInfo.roaming, roaming.getJavaArray());
162 env->SetObjectField(stats, gNetworkStatsClassInfo.defaultNetwork,
163 defaultNetwork.getJavaArray());
164 env->SetObjectField(stats, gNetworkStatsClassInfo.rxBytes, rxBytes.getJavaArray());
165 env->SetObjectField(stats, gNetworkStatsClassInfo.rxPackets, rxPackets.getJavaArray());
166 env->SetObjectField(stats, gNetworkStatsClassInfo.txBytes, txBytes.getJavaArray());
167 env->SetObjectField(stats, gNetworkStatsClassInfo.txPackets, txPackets.getJavaArray());
168 env->SetObjectField(stats, gNetworkStatsClassInfo.operations, operations.getJavaArray());
169 }
170 return 0;
171 }
172
readNetworkStatsDetail(JNIEnv * env,jclass clazz,jobject stats)173 static int readNetworkStatsDetail(JNIEnv* env, jclass clazz, jobject stats) {
174 std::vector<stats_line> lines;
175
176 if (parseBpfNetworkStatsDetail(&lines) < 0)
177 return -1;
178
179 return statsLinesToNetworkStats(env, clazz, stats, lines);
180 }
181
readNetworkStatsDev(JNIEnv * env,jclass clazz,jobject stats)182 static int readNetworkStatsDev(JNIEnv* env, jclass clazz, jobject stats) {
183 std::vector<stats_line> lines;
184
185 if (parseBpfNetworkStatsDev(&lines) < 0)
186 return -1;
187
188 return statsLinesToNetworkStats(env, clazz, stats, lines);
189 }
190
191 static const JNINativeMethod gMethods[] = {
192 { "nativeReadNetworkStatsDetail", "(Landroid/net/NetworkStats;)I",
193 (void*) readNetworkStatsDetail },
194 { "nativeReadNetworkStatsDev", "(Landroid/net/NetworkStats;)I",
195 (void*) readNetworkStatsDev },
196 };
197
register_android_server_net_NetworkStatsFactory(JNIEnv * env)198 int register_android_server_net_NetworkStatsFactory(JNIEnv* env) {
199 int err = jniRegisterNativeMethods(env,
200 "android/net/connectivity/com/android/server/net/NetworkStatsFactory", gMethods,
201 NELEM(gMethods));
202 gStringClass = env->FindClass("java/lang/String");
203 gStringClass = static_cast<jclass>(env->NewGlobalRef(gStringClass));
204
205 jclass clazz = env->FindClass("android/net/NetworkStats");
206 gNetworkStatsClassInfo.size = env->GetFieldID(clazz, "size", "I");
207 gNetworkStatsClassInfo.capacity = env->GetFieldID(clazz, "capacity", "I");
208 gNetworkStatsClassInfo.iface = env->GetFieldID(clazz, "iface", "[Ljava/lang/String;");
209 gNetworkStatsClassInfo.uid = env->GetFieldID(clazz, "uid", "[I");
210 gNetworkStatsClassInfo.set = env->GetFieldID(clazz, "set", "[I");
211 gNetworkStatsClassInfo.tag = env->GetFieldID(clazz, "tag", "[I");
212 gNetworkStatsClassInfo.metered = env->GetFieldID(clazz, "metered", "[I");
213 gNetworkStatsClassInfo.roaming = env->GetFieldID(clazz, "roaming", "[I");
214 gNetworkStatsClassInfo.defaultNetwork = env->GetFieldID(clazz, "defaultNetwork", "[I");
215 gNetworkStatsClassInfo.rxBytes = env->GetFieldID(clazz, "rxBytes", "[J");
216 gNetworkStatsClassInfo.rxPackets = env->GetFieldID(clazz, "rxPackets", "[J");
217 gNetworkStatsClassInfo.txBytes = env->GetFieldID(clazz, "txBytes", "[J");
218 gNetworkStatsClassInfo.txPackets = env->GetFieldID(clazz, "txPackets", "[J");
219 gNetworkStatsClassInfo.operations = env->GetFieldID(clazz, "operations", "[J");
220
221 return err;
222 }
223
224 }
225