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 <cutils/qtaguid.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <jni.h>
25 #include <nativehelper/ScopedUtfChars.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <utils/Log.h>
29 #include <utils/misc.h>
30 
31 #include "bpf/BpfUtils.h"
32 #include "netdbpf/BpfNetworkStats.h"
33 #include "netdbpf/NetworkTraceHandler.h"
34 
35 using android::bpf::bpfGetUidStats;
36 using android::bpf::bpfGetIfaceStats;
37 using android::bpf::bpfRegisterIface;
38 using android::bpf::NetworkTraceHandler;
39 
40 namespace android {
41 
nativeRegisterIface(JNIEnv * env,jclass clazz,jstring iface)42 static void nativeRegisterIface(JNIEnv* env, jclass clazz, jstring iface) {
43     ScopedUtfChars iface8(env, iface);
44     if (iface8.c_str() == nullptr) return;
45     bpfRegisterIface(iface8.c_str());
46 }
47 
statsValueToEntry(JNIEnv * env,StatsValue * stats)48 static jobject statsValueToEntry(JNIEnv* env, StatsValue* stats) {
49     // Find the Java class that represents the structure
50     jclass gEntryClass = env->FindClass("android/net/NetworkStats$Entry");
51     if (gEntryClass == nullptr) {
52         return nullptr;
53     }
54 
55     // Find the constructor.
56     jmethodID constructorID = env->GetMethodID(gEntryClass, "<init>", "()V");
57     if (constructorID == nullptr) {
58         return nullptr;
59     }
60 
61     // Create a new instance of the Java class
62     jobject result = env->NewObject(gEntryClass, constructorID);
63     if (result == nullptr) {
64         return nullptr;
65     }
66 
67     // Set the values of the structure fields in the Java object
68     env->SetLongField(result, env->GetFieldID(gEntryClass, "rxBytes", "J"), stats->rxBytes);
69     env->SetLongField(result, env->GetFieldID(gEntryClass, "txBytes", "J"), stats->txBytes);
70     env->SetLongField(result, env->GetFieldID(gEntryClass, "rxPackets", "J"), stats->rxPackets);
71     env->SetLongField(result, env->GetFieldID(gEntryClass, "txPackets", "J"), stats->txPackets);
72 
73     return result;
74 }
75 
nativeGetTotalStat(JNIEnv * env,jclass clazz)76 static jobject nativeGetTotalStat(JNIEnv* env, jclass clazz) {
77     StatsValue stats = {};
78 
79     if (bpfGetIfaceStats(nullptr, &stats) == 0) {
80         return statsValueToEntry(env, &stats);
81     } else {
82         return nullptr;
83     }
84 }
85 
nativeGetIfaceStat(JNIEnv * env,jclass clazz,jstring iface)86 static jobject nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface) {
87     ScopedUtfChars iface8(env, iface);
88     if (iface8.c_str() == nullptr) {
89         return nullptr;
90     }
91 
92     StatsValue stats = {};
93 
94     if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
95         return statsValueToEntry(env, &stats);
96     } else {
97         return nullptr;
98     }
99 }
100 
nativeGetUidStat(JNIEnv * env,jclass clazz,jint uid)101 static jobject nativeGetUidStat(JNIEnv* env, jclass clazz, jint uid) {
102     StatsValue stats = {};
103 
104     if (bpfGetUidStats(uid, &stats) == 0) {
105         return statsValueToEntry(env, &stats);
106     } else {
107         return nullptr;
108     }
109 }
110 
nativeInitNetworkTracing(JNIEnv * env,jclass clazz)111 static void nativeInitNetworkTracing(JNIEnv* env, jclass clazz) {
112     NetworkTraceHandler::InitPerfettoTracing();
113 }
114 
115 static const JNINativeMethod gMethods[] = {
116         {
117             "nativeRegisterIface",
118             "(Ljava/lang/String;)V",
119             (void*)nativeRegisterIface
120         },
121         {
122             "nativeGetTotalStat",
123             "()Landroid/net/NetworkStats$Entry;",
124             (void*)nativeGetTotalStat
125         },
126         {
127             "nativeGetIfaceStat",
128             "(Ljava/lang/String;)Landroid/net/NetworkStats$Entry;",
129             (void*)nativeGetIfaceStat
130         },
131         {
132             "nativeGetUidStat",
133             "(I)Landroid/net/NetworkStats$Entry;",
134             (void*)nativeGetUidStat
135         },
136         {
137             "nativeInitNetworkTracing",
138             "()V",
139             (void*)nativeInitNetworkTracing
140         },
141 };
142 
register_android_server_net_NetworkStatsService(JNIEnv * env)143 int register_android_server_net_NetworkStatsService(JNIEnv* env) {
144     return jniRegisterNativeMethods(env,
145             "android/net/connectivity/com/android/server/net/NetworkStatsService", gMethods,
146             NELEM(gMethods));
147 }
148 
149 }
150