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; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 26 | import android.os.Process; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 27 | import android.provider.Settings; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 28 | import android.text.TextUtils; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 29 | import android.text.format.DateUtils; |
| 30 | import android.util.ArrayMap; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 31 | import android.util.Base64; |
| 32 | import android.util.Log; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 33 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 34 | import com.android.internal.annotations.GuardedBy; |
| 35 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 36 | import com.android.internal.util.RingBuffer; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 37 | import com.android.internal.util.TokenBucket; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 38 | import com.android.server.LocalServices; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 39 | import com.android.server.SystemService; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 40 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 41 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 42 | import java.io.FileDescriptor; |
| 43 | import java.io.IOException; |
| 44 | import java.io.PrintWriter; |
| 45 | import java.util.ArrayList; |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 46 | import java.util.Arrays; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 47 | import java.util.List; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 48 | import java.util.function.ToIntFunction; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 49 | |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 50 | /** |
| 51 | * Event buffering service for core networking and connectivity metrics. |
| 52 | * |
| 53 | * {@hide} |
| 54 | */ |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 55 | final public class IpConnectivityMetrics extends SystemService { |
| 56 | private static final String TAG = IpConnectivityMetrics.class.getSimpleName(); |
| 57 | private static final boolean DBG = false; |
| 58 | |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 59 | // The logical version numbers of ipconnectivity.proto, corresponding to the |
| 60 | // "version" field of IpConnectivityLog. |
| 61 | private static final int NYC = 0; |
| 62 | private static final int NYC_MR1 = 1; |
| 63 | private static final int NYC_MR2 = 2; |
| 64 | public static final int VERSION = NYC_MR2; |
| 65 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 66 | private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME; |
| 67 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 68 | // Default size of the event rolling log for bug report dumps. |
| 69 | private static final int DEFAULT_LOG_SIZE = 500; |
| 70 | // Default size of the event buffer for metrics reporting. |
| 71 | // Once the buffer is full, incoming events are dropped. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 72 | private static final int DEFAULT_BUFFER_SIZE = 2000; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 73 | // Maximum size of the event buffer. |
| 74 | private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 75 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 76 | private static final int MAXIMUM_CONNECT_LATENCY_RECORDS = 20000; |
| 77 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 78 | private static final int ERROR_RATE_LIMITED = -1; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 79 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 80 | // Lock ensuring that concurrent manipulations of the event buffers are correct. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 81 | // There are three concurrent operations to synchronize: |
| 82 | // - appending events to the buffer. |
| 83 | // - iterating throught the buffer. |
| 84 | // - flushing the buffer content and replacing it by a new buffer. |
| 85 | private final Object mLock = new Object(); |
| 86 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 87 | // Implementation instance of IIpConnectivityMetrics.aidl. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 88 | @VisibleForTesting |
| 89 | public final Impl impl = new Impl(); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 90 | // Subservice listening to Netd events via INetdEventListener.aidl. |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 91 | @VisibleForTesting |
| 92 | NetdEventListenerService mNetdListener; |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 93 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 94 | // Rolling log of the most recent events. This log is used for dumping |
| 95 | // connectivity events in bug reports. |
| 96 | @GuardedBy("mLock") |
| 97 | private final RingBuffer<ConnectivityMetricsEvent> mEventLog = |
| 98 | new RingBuffer(ConnectivityMetricsEvent.class, DEFAULT_LOG_SIZE); |
| 99 | // Buffer of connectivity events used for metrics reporting. This buffer |
| 100 | // does not rotate automatically and instead saturates when it becomes full. |
| 101 | // It is flushed at metrics reporting. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 102 | @GuardedBy("mLock") |
| 103 | private ArrayList<ConnectivityMetricsEvent> mBuffer; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 104 | // Total number of events dropped from mBuffer since last metrics reporting. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 105 | @GuardedBy("mLock") |
| 106 | private int mDropped; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 107 | // Capacity of mBuffer |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 108 | @GuardedBy("mLock") |
| 109 | private int mCapacity; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 110 | // A list of rate limiting counters keyed by connectivity event types for |
| 111 | // metrics reporting mBuffer. |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 112 | @GuardedBy("mLock") |
| 113 | private final ArrayMap<Class<?>, TokenBucket> mBuckets = makeRateLimitingBuckets(); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 114 | |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 115 | private final ToIntFunction<Context> mCapacityGetter; |
| 116 | |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 117 | @VisibleForTesting |
| 118 | final DefaultNetworkMetrics mDefaultNetworkMetrics = new DefaultNetworkMetrics(); |
| 119 | |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 120 | public IpConnectivityMetrics(Context ctx, ToIntFunction<Context> capacityGetter) { |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 121 | super(ctx); |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 122 | mCapacityGetter = capacityGetter; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 123 | initBuffer(); |
| 124 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 125 | |
| 126 | public IpConnectivityMetrics(Context ctx) { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 127 | this(ctx, READ_BUFFER_SIZE); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onStart() { |
| 132 | if (DBG) Log.d(TAG, "onStart"); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void onBootPhase(int phase) { |
| 137 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 138 | if (DBG) Log.d(TAG, "onBootPhase"); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 139 | mNetdListener = new NetdEventListenerService(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 140 | |
| 141 | publishBinderService(SERVICE_NAME, impl); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 142 | publishBinderService(mNetdListener.SERVICE_NAME, mNetdListener); |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 143 | |
| 144 | LocalServices.addService(Logger.class, new LoggerImpl()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | @VisibleForTesting |
| 149 | public int bufferCapacity() { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 150 | return mCapacityGetter.applyAsInt(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private void initBuffer() { |
| 154 | synchronized (mLock) { |
| 155 | mDropped = 0; |
| 156 | mCapacity = bufferCapacity(); |
| 157 | mBuffer = new ArrayList<>(mCapacity); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private int append(ConnectivityMetricsEvent event) { |
| 162 | if (DBG) Log.d(TAG, "logEvent: " + event); |
| 163 | synchronized (mLock) { |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 164 | mEventLog.append(event); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 165 | final int left = mCapacity - mBuffer.size(); |
| 166 | if (event == null) { |
| 167 | return left; |
| 168 | } |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 169 | if (isRateLimited(event)) { |
| 170 | // Do not count as a dropped event. TODO: consider adding separate counter |
| 171 | return ERROR_RATE_LIMITED; |
| 172 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 173 | if (left == 0) { |
| 174 | mDropped++; |
| 175 | return 0; |
| 176 | } |
| 177 | mBuffer.add(event); |
| 178 | return left - 1; |
| 179 | } |
| 180 | } |
| 181 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 182 | private boolean isRateLimited(ConnectivityMetricsEvent event) { |
| 183 | TokenBucket tb = mBuckets.get(event.data.getClass()); |
| 184 | return (tb != null) && !tb.get(); |
| 185 | } |
| 186 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 187 | private String flushEncodedOutput() { |
| 188 | final ArrayList<ConnectivityMetricsEvent> events; |
| 189 | final int dropped; |
| 190 | synchronized (mLock) { |
| 191 | events = mBuffer; |
| 192 | dropped = mDropped; |
| 193 | initBuffer(); |
| 194 | } |
| 195 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 196 | final List<IpConnectivityEvent> protoEvents = IpConnectivityEventBuilder.toProto(events); |
| 197 | |
Hugo Benichi | 1193a9c | 2017-10-19 14:58:15 +0900 | [diff] [blame] | 198 | mDefaultNetworkMetrics.flushEvents(protoEvents); |
| 199 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 200 | if (mNetdListener != null) { |
| 201 | mNetdListener.flushStatistics(protoEvents); |
| 202 | } |
| 203 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 204 | final byte[] data; |
| 205 | try { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 206 | data = IpConnectivityEventBuilder.serialize(dropped, protoEvents); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 207 | } catch (IOException e) { |
| 208 | Log.e(TAG, "could not serialize events", e); |
| 209 | return ""; |
| 210 | } |
| 211 | |
| 212 | return Base64.encodeToString(data, Base64.DEFAULT); |
| 213 | } |
| 214 | |
| 215 | /** |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 216 | * Clear the event buffer and prints its content as a protobuf serialized byte array |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 217 | * inside a base64 encoded string. |
| 218 | */ |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 219 | private void cmdFlush(PrintWriter pw) { |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 220 | pw.print(flushEncodedOutput()); |
| 221 | } |
| 222 | |
| 223 | /** |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 224 | * Print the content of the rolling event buffer in human readable format. |
| 225 | * Also print network dns/connect statistics and recent default network events. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 226 | */ |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 227 | private void cmdList(PrintWriter pw) { |
| 228 | pw.println("metrics events:"); |
| 229 | final List<ConnectivityMetricsEvent> events = getEvents(); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 230 | for (ConnectivityMetricsEvent ev : events) { |
| 231 | pw.println(ev.toString()); |
| 232 | } |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 233 | pw.println(""); |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 234 | if (mNetdListener != null) { |
| 235 | mNetdListener.list(pw); |
| 236 | } |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 237 | pw.println(""); |
Hugo Benichi | 1193a9c | 2017-10-19 14:58:15 +0900 | [diff] [blame] | 238 | mDefaultNetworkMetrics.listEvents(pw); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 241 | /* |
| 242 | * Print the content of the rolling event buffer in text proto format. |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 243 | */ |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 244 | private void cmdListAsProto(PrintWriter pw) { |
| 245 | final List<ConnectivityMetricsEvent> events = getEvents(); |
| 246 | for (IpConnectivityEvent ev : IpConnectivityEventBuilder.toProto(events)) { |
| 247 | pw.print(ev.toString()); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 248 | } |
| 249 | if (mNetdListener != null) { |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 250 | mNetdListener.listAsProtos(pw); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 251 | } |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 252 | mDefaultNetworkMetrics.listEventsAsProto(pw); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 255 | /* |
| 256 | * Return a copy of metrics events stored in buffer for metrics uploading. |
| 257 | */ |
| 258 | private List<ConnectivityMetricsEvent> getEvents() { |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 259 | synchronized (mLock) { |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 260 | return Arrays.asList(mEventLog.toArray()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 261 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | public final class Impl extends IIpConnectivityMetrics.Stub { |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 265 | // Dump and flushes the metrics event buffer in base64 encoded serialized proto output. |
| 266 | static final String CMD_FLUSH = "flush"; |
| 267 | // Dump the rolling buffer of metrics event in human readable proto text format. |
| 268 | static final String CMD_PROTO = "proto"; |
| 269 | // Dump the rolling buffer of metrics event and pretty print events using a human readable |
| 270 | // format. Also print network dns/connect statistics and default network event time series. |
| 271 | static final String CMD_LIST = "list"; |
Erik Kline | c172c7d | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 272 | // By default any other argument will fall into the default case which is the equivalent |
| 273 | // of calling both the "list" and "ipclient" commands. This includes most notably bug |
| 274 | // reports collected by dumpsys.cpp with the "-a" argument. |
| 275 | static final String CMD_DEFAULT = ""; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 276 | |
| 277 | @Override |
| 278 | public int logEvent(ConnectivityMetricsEvent event) { |
| 279 | enforceConnectivityInternalPermission(); |
| 280 | return append(event); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 285 | enforceDumpPermission(); |
| 286 | if (DBG) Log.d(TAG, "dumpsys " + TextUtils.join(" ", args)); |
| 287 | final String cmd = (args.length > 0) ? args[0] : CMD_DEFAULT; |
| 288 | switch (cmd) { |
| 289 | case CMD_FLUSH: |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 290 | cmdFlush(pw); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 291 | return; |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 292 | case CMD_PROTO: |
| 293 | cmdListAsProto(pw); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 294 | return; |
Erik Kline | c172c7d | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 295 | case CMD_LIST: |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 296 | default: |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 297 | cmdList(pw); |
| 298 | return; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | private void enforceConnectivityInternalPermission() { |
| 303 | enforcePermission(android.Manifest.permission.CONNECTIVITY_INTERNAL); |
| 304 | } |
| 305 | |
| 306 | private void enforceDumpPermission() { |
| 307 | enforcePermission(android.Manifest.permission.DUMP); |
| 308 | } |
| 309 | |
| 310 | private void enforcePermission(String what) { |
| 311 | getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics"); |
| 312 | } |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 313 | |
| 314 | private void enforceNetdEventListeningPermission() { |
| 315 | final int uid = Binder.getCallingUid(); |
| 316 | if (uid != Process.SYSTEM_UID) { |
| 317 | throw new SecurityException(String.format("Uid %d has no permission to listen for" |
| 318 | + " netd events.", uid)); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | @Override |
Ricky Wai | 77518fb | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 323 | public boolean addNetdEventCallback(int callerType, INetdEventCallback callback) { |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 324 | enforceNetdEventListeningPermission(); |
| 325 | if (mNetdListener == null) { |
| 326 | return false; |
| 327 | } |
Ricky Wai | 77518fb | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 328 | return mNetdListener.addNetdEventCallback(callerType, callback); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Ricky Wai | 77518fb | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 332 | public boolean removeNetdEventCallback(int callerType) { |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 333 | enforceNetdEventListeningPermission(); |
| 334 | if (mNetdListener == null) { |
| 335 | // if the service is null, we aren't registered anyway |
| 336 | return true; |
| 337 | } |
Ricky Wai | 77518fb | 2017-10-27 14:46:01 +0100 | [diff] [blame] | 338 | return mNetdListener.removeNetdEventCallback(callerType); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 339 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 340 | }; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 341 | |
| 342 | private static final ToIntFunction<Context> READ_BUFFER_SIZE = (ctx) -> { |
| 343 | int size = Settings.Global.getInt(ctx.getContentResolver(), |
| 344 | Settings.Global.CONNECTIVITY_METRICS_BUFFER_SIZE, DEFAULT_BUFFER_SIZE); |
| 345 | if (size <= 0) { |
| 346 | return DEFAULT_BUFFER_SIZE; |
| 347 | } |
| 348 | return Math.min(size, MAXIMUM_BUFFER_SIZE); |
| 349 | }; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 350 | |
| 351 | private static ArrayMap<Class<?>, TokenBucket> makeRateLimitingBuckets() { |
| 352 | ArrayMap<Class<?>, TokenBucket> map = new ArrayMap<>(); |
| 353 | // one token every minute, 50 tokens max: burst of ~50 events every hour. |
| 354 | map.put(ApfProgramEvent.class, new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50)); |
| 355 | return map; |
| 356 | } |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 357 | |
| 358 | /** Direct non-Binder interface for event producer clients within the system servers. */ |
| 359 | public interface Logger { |
| 360 | DefaultNetworkMetrics defaultNetworkMetrics(); |
| 361 | } |
| 362 | |
| 363 | private class LoggerImpl implements Logger { |
| 364 | public DefaultNetworkMetrics defaultNetworkMetrics() { |
| 365 | return mDefaultNetworkMetrics; |
| 366 | } |
| 367 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 368 | } |