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