Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.connectivity; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.net.ConnectivityMetricsEvent; |
| 21 | import android.net.IIpConnectivityMetrics; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame^] | 22 | import android.net.INetdEventCallback; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 23 | import android.net.metrics.ApfProgramEvent; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 24 | import android.net.metrics.IpConnectivityLog; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame^] | 25 | import android.os.Binder; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.Parcelable; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame^] | 28 | import android.os.Process; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 29 | import android.provider.Settings; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 30 | import android.text.TextUtils; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 31 | import android.text.format.DateUtils; |
| 32 | import android.util.ArrayMap; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 33 | import android.util.Base64; |
| 34 | import android.util.Log; |
| 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 37 | import com.android.internal.util.TokenBucket; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 38 | import com.android.server.SystemService; |
| 39 | import java.io.FileDescriptor; |
| 40 | import java.io.IOException; |
| 41 | import java.io.PrintWriter; |
| 42 | import java.util.ArrayList; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 43 | import java.util.function.ToIntFunction; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 44 | |
| 45 | import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent; |
| 46 | |
| 47 | /** {@hide} */ |
| 48 | final public class IpConnectivityMetrics extends SystemService { |
| 49 | private static final String TAG = IpConnectivityMetrics.class.getSimpleName(); |
| 50 | private static final boolean DBG = false; |
| 51 | |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 52 | // The logical version numbers of ipconnectivity.proto, corresponding to the |
| 53 | // "version" field of IpConnectivityLog. |
| 54 | private static final int NYC = 0; |
| 55 | private static final int NYC_MR1 = 1; |
| 56 | private static final int NYC_MR2 = 2; |
| 57 | public static final int VERSION = NYC_MR2; |
| 58 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 59 | private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME; |
| 60 | |
| 61 | // Default size of the event buffer. Once the buffer is full, incoming events are dropped. |
| 62 | private static final int DEFAULT_BUFFER_SIZE = 2000; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 63 | // Maximum size of the event buffer. |
| 64 | private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 65 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 66 | private static final int ERROR_RATE_LIMITED = -1; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 67 | |
| 68 | // Lock ensuring that concurrent manipulations of the event buffer are correct. |
| 69 | // There are three concurrent operations to synchronize: |
| 70 | // - appending events to the buffer. |
| 71 | // - iterating throught the buffer. |
| 72 | // - flushing the buffer content and replacing it by a new buffer. |
| 73 | private final Object mLock = new Object(); |
| 74 | |
| 75 | @VisibleForTesting |
| 76 | public final Impl impl = new Impl(); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 77 | private NetdEventListenerService mNetdListener; |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 78 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 79 | @GuardedBy("mLock") |
| 80 | private ArrayList<ConnectivityMetricsEvent> mBuffer; |
| 81 | @GuardedBy("mLock") |
| 82 | private int mDropped; |
| 83 | @GuardedBy("mLock") |
| 84 | private int mCapacity; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 85 | @GuardedBy("mLock") |
| 86 | private final ArrayMap<Class<?>, TokenBucket> mBuckets = makeRateLimitingBuckets(); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 87 | |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 88 | private final ToIntFunction<Context> mCapacityGetter; |
| 89 | |
| 90 | public IpConnectivityMetrics(Context ctx, ToIntFunction<Context> capacityGetter) { |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 91 | super(ctx); |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 92 | mCapacityGetter = capacityGetter; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 93 | initBuffer(); |
| 94 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 95 | |
| 96 | public IpConnectivityMetrics(Context ctx) { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 97 | this(ctx, READ_BUFFER_SIZE); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onStart() { |
| 102 | if (DBG) Log.d(TAG, "onStart"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onBootPhase(int phase) { |
| 107 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 108 | if (DBG) Log.d(TAG, "onBootPhase"); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 109 | mNetdListener = new NetdEventListenerService(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 110 | |
| 111 | publishBinderService(SERVICE_NAME, impl); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 112 | publishBinderService(mNetdListener.SERVICE_NAME, mNetdListener); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | @VisibleForTesting |
| 117 | public int bufferCapacity() { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 118 | return mCapacityGetter.applyAsInt(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private void initBuffer() { |
| 122 | synchronized (mLock) { |
| 123 | mDropped = 0; |
| 124 | mCapacity = bufferCapacity(); |
| 125 | mBuffer = new ArrayList<>(mCapacity); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private int append(ConnectivityMetricsEvent event) { |
| 130 | if (DBG) Log.d(TAG, "logEvent: " + event); |
| 131 | synchronized (mLock) { |
| 132 | final int left = mCapacity - mBuffer.size(); |
| 133 | if (event == null) { |
| 134 | return left; |
| 135 | } |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 136 | if (isRateLimited(event)) { |
| 137 | // Do not count as a dropped event. TODO: consider adding separate counter |
| 138 | return ERROR_RATE_LIMITED; |
| 139 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 140 | if (left == 0) { |
| 141 | mDropped++; |
| 142 | return 0; |
| 143 | } |
| 144 | mBuffer.add(event); |
| 145 | return left - 1; |
| 146 | } |
| 147 | } |
| 148 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 149 | private boolean isRateLimited(ConnectivityMetricsEvent event) { |
| 150 | TokenBucket tb = mBuckets.get(event.data.getClass()); |
| 151 | return (tb != null) && !tb.get(); |
| 152 | } |
| 153 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 154 | private String flushEncodedOutput() { |
| 155 | final ArrayList<ConnectivityMetricsEvent> events; |
| 156 | final int dropped; |
| 157 | synchronized (mLock) { |
| 158 | events = mBuffer; |
| 159 | dropped = mDropped; |
| 160 | initBuffer(); |
| 161 | } |
| 162 | |
| 163 | final byte[] data; |
| 164 | try { |
| 165 | data = IpConnectivityEventBuilder.serialize(dropped, events); |
| 166 | } catch (IOException e) { |
| 167 | Log.e(TAG, "could not serialize events", e); |
| 168 | return ""; |
| 169 | } |
| 170 | |
| 171 | return Base64.encodeToString(data, Base64.DEFAULT); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Clears the event buffer and prints its content as a protobuf serialized byte array |
| 176 | * inside a base64 encoded string. |
| 177 | */ |
| 178 | private void cmdFlush(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 179 | pw.print(flushEncodedOutput()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Prints the content of the event buffer, either using the events ASCII representation |
| 184 | * or using protobuf text format. |
| 185 | */ |
| 186 | private void cmdList(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 187 | final ArrayList<ConnectivityMetricsEvent> events; |
| 188 | synchronized (mLock) { |
| 189 | events = new ArrayList(mBuffer); |
| 190 | } |
| 191 | |
| 192 | if (args.length > 1 && args[1].equals("proto")) { |
| 193 | for (IpConnectivityEvent ev : IpConnectivityEventBuilder.toProto(events)) { |
| 194 | pw.print(ev.toString()); |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | for (ConnectivityMetricsEvent ev : events) { |
| 200 | pw.println(ev.toString()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void cmdStats(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 205 | synchronized (mLock) { |
| 206 | pw.println("Buffered events: " + mBuffer.size()); |
| 207 | pw.println("Buffer capacity: " + mCapacity); |
| 208 | pw.println("Dropped events: " + mDropped); |
| 209 | } |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 210 | if (mNetdListener != null) { |
| 211 | mNetdListener.dump(pw); |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 212 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void cmdDefault(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 216 | if (args.length == 0) { |
| 217 | pw.println("No command"); |
| 218 | return; |
| 219 | } |
| 220 | pw.println("Unknown command " + TextUtils.join(" ", args)); |
| 221 | } |
| 222 | |
| 223 | public final class Impl extends IIpConnectivityMetrics.Stub { |
| 224 | static final String CMD_FLUSH = "flush"; |
| 225 | static final String CMD_LIST = "list"; |
| 226 | static final String CMD_STATS = "stats"; |
Hugo Benichi | 51d14cb | 2016-10-19 13:48:40 +0900 | [diff] [blame] | 227 | static final String CMD_DUMPSYS = "-a"; // dumpsys.cpp dumps services with "-a" as arguments |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 228 | static final String CMD_DEFAULT = CMD_STATS; |
| 229 | |
| 230 | @Override |
| 231 | public int logEvent(ConnectivityMetricsEvent event) { |
| 232 | enforceConnectivityInternalPermission(); |
| 233 | return append(event); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 238 | enforceDumpPermission(); |
| 239 | if (DBG) Log.d(TAG, "dumpsys " + TextUtils.join(" ", args)); |
| 240 | final String cmd = (args.length > 0) ? args[0] : CMD_DEFAULT; |
| 241 | switch (cmd) { |
| 242 | case CMD_FLUSH: |
| 243 | cmdFlush(fd, pw, args); |
| 244 | return; |
Hugo Benichi | 51d14cb | 2016-10-19 13:48:40 +0900 | [diff] [blame] | 245 | case CMD_DUMPSYS: |
| 246 | // Fallthrough to CMD_LIST when dumpsys.cpp dumps services states (bug reports) |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 247 | case CMD_LIST: |
| 248 | cmdList(fd, pw, args); |
| 249 | return; |
| 250 | case CMD_STATS: |
| 251 | cmdStats(fd, pw, args); |
| 252 | return; |
| 253 | default: |
| 254 | cmdDefault(fd, pw, args); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private void enforceConnectivityInternalPermission() { |
| 259 | enforcePermission(android.Manifest.permission.CONNECTIVITY_INTERNAL); |
| 260 | } |
| 261 | |
| 262 | private void enforceDumpPermission() { |
| 263 | enforcePermission(android.Manifest.permission.DUMP); |
| 264 | } |
| 265 | |
| 266 | private void enforcePermission(String what) { |
| 267 | getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics"); |
| 268 | } |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame^] | 269 | |
| 270 | private void enforceNetdEventListeningPermission() { |
| 271 | final int uid = Binder.getCallingUid(); |
| 272 | if (uid != Process.SYSTEM_UID) { |
| 273 | throw new SecurityException(String.format("Uid %d has no permission to listen for" |
| 274 | + " netd events.", uid)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public boolean registerNetdEventCallback(INetdEventCallback callback) { |
| 280 | enforceNetdEventListeningPermission(); |
| 281 | if (mNetdListener == null) { |
| 282 | return false; |
| 283 | } |
| 284 | return mNetdListener.registerNetdEventCallback(callback); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public boolean unregisterNetdEventCallback() { |
| 289 | enforceNetdEventListeningPermission(); |
| 290 | if (mNetdListener == null) { |
| 291 | // if the service is null, we aren't registered anyway |
| 292 | return true; |
| 293 | } |
| 294 | return mNetdListener.unregisterNetdEventCallback(); |
| 295 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 296 | }; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 297 | |
| 298 | private static final ToIntFunction<Context> READ_BUFFER_SIZE = (ctx) -> { |
| 299 | int size = Settings.Global.getInt(ctx.getContentResolver(), |
| 300 | Settings.Global.CONNECTIVITY_METRICS_BUFFER_SIZE, DEFAULT_BUFFER_SIZE); |
| 301 | if (size <= 0) { |
| 302 | return DEFAULT_BUFFER_SIZE; |
| 303 | } |
| 304 | return Math.min(size, MAXIMUM_BUFFER_SIZE); |
| 305 | }; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 306 | |
| 307 | private static ArrayMap<Class<?>, TokenBucket> makeRateLimitingBuckets() { |
| 308 | ArrayMap<Class<?>, TokenBucket> map = new ArrayMap<>(); |
| 309 | // one token every minute, 50 tokens max: burst of ~50 events every hour. |
| 310 | map.put(ApfProgramEvent.class, new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50)); |
| 311 | return map; |
| 312 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 313 | } |