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