blob: 5dee91de0e77688fb1da1ef9e91d9b4aa7ab4251 [file] [log] [blame]
Hugo Benichi50a84c62016-09-02 09:00:59 +09001/*
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
17package com.android.server.connectivity;
18
Hugo Benichi73fd4d12017-03-15 23:05:01 +090019import static android.net.NetworkCapabilities.MAX_TRANSPORT;
20import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH;
21import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
22import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
23import static android.net.NetworkCapabilities.TRANSPORT_VPN;
24import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
25import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090026
Hugo Benichi50a84c62016-09-02 09:00:59 +090027import android.net.ConnectivityMetricsEvent;
28import android.net.metrics.ApfProgramEvent;
29import android.net.metrics.ApfStats;
30import android.net.metrics.DefaultNetworkEvent;
31import android.net.metrics.DhcpClientEvent;
32import android.net.metrics.DhcpErrorEvent;
33import android.net.metrics.DnsEvent;
Hugo Benichi5eb90532017-03-23 18:38:22 +090034import android.net.metrics.ConnectStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090035import android.net.metrics.IpManagerEvent;
36import android.net.metrics.IpReachabilityEvent;
37import android.net.metrics.NetworkEvent;
38import android.net.metrics.RaEvent;
39import android.net.metrics.ValidationProbeEvent;
40import android.os.Parcelable;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090041import android.util.SparseArray;
Hugo Benichi5eb90532017-03-23 18:38:22 +090042import android.util.SparseIntArray;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010043import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi5eb90532017-03-23 18:38:22 +090044import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
45import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
46import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId;
47import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.Pair;
Hugo Benichi50a84c62016-09-02 09:00:59 +090048import java.io.IOException;
49import java.util.ArrayList;
50import java.util.List;
51
Hugo Benichi50a84c62016-09-02 09:00:59 +090052
53/** {@hide} */
54final public class IpConnectivityEventBuilder {
55 private IpConnectivityEventBuilder() {
56 }
57
Hugo Benichi0d4a3982016-11-25 11:24:22 +090058 public static byte[] serialize(int dropped, List<IpConnectivityEvent> events)
Hugo Benichi50a84c62016-09-02 09:00:59 +090059 throws IOException {
60 final IpConnectivityLog log = new IpConnectivityLog();
Hugo Benichi0d4a3982016-11-25 11:24:22 +090061 log.events = events.toArray(new IpConnectivityEvent[events.size()]);
Hugo Benichi50a84c62016-09-02 09:00:59 +090062 log.droppedEvents = dropped;
Hugo Benichid680d4c2016-10-13 13:16:16 +090063 if ((log.events.length > 0) || (dropped > 0)) {
64 // Only write version number if log has some information at all.
65 log.version = IpConnectivityMetrics.VERSION;
66 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090067 return IpConnectivityLog.toByteArray(log);
68 }
69
Hugo Benichi0d4a3982016-11-25 11:24:22 +090070 public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090071 final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
72 for (ConnectivityMetricsEvent in : eventsIn) {
73 final IpConnectivityEvent out = toProto(in);
74 if (out == null) {
75 continue;
76 }
77 eventsOut.add(out);
78 }
Hugo Benichi0d4a3982016-11-25 11:24:22 +090079 return eventsOut;
Hugo Benichi50a84c62016-09-02 09:00:59 +090080 }
81
82 public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
Hugo Benichi5eb90532017-03-23 18:38:22 +090083 final IpConnectivityEvent out = buildEvent(ev.netId, ev.transports, ev.ifname);
84 out.timeMs = ev.timestamp;
Hugo Benichi50a84c62016-09-02 09:00:59 +090085 if (!setEvent(out, ev.data)) {
86 return null;
87 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090088 return out;
89 }
90
Hugo Benichi5eb90532017-03-23 18:38:22 +090091 public static IpConnectivityEvent toProto(ConnectStats in) {
92 IpConnectivityLogClass.ConnectStatistics stats =
93 new IpConnectivityLogClass.ConnectStatistics();
94 stats.connectCount = in.connectCount;
95 stats.connectBlockingCount = in.connectBlockingCount;
96 stats.ipv6AddrCount = in.ipv6ConnectCount;
97 stats.latenciesMs = in.latencies.toArray();
98 stats.errnosCounters = toPairArray(in.errnos);
99 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
100 out.setConnectStatistics(stats);
101 return out;
102 }
103
104
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900105 public static IpConnectivityEvent toProto(DnsEvent in) {
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900106 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
107 new IpConnectivityLogClass.DNSLookupBatch();
108 in.resize(in.eventCount);
109 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
110 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
111 dnsLookupBatch.latenciesMs = in.latenciesMs;
Hugo Benichi5eb90532017-03-23 18:38:22 +0900112 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900113 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900114 return out;
115 }
116
Hugo Benichi5eb90532017-03-23 18:38:22 +0900117 private static IpConnectivityEvent buildEvent(int netId, long transports, String ifname) {
118 final IpConnectivityEvent ev = new IpConnectivityEvent();
119 ev.networkId = netId;
120 ev.transports = transports;
121 if (ifname != null) {
122 ev.ifName = ifname;
123 }
124 inferLinkLayer(ev);
125 return ev;
126 }
127
Hugo Benichi50a84c62016-09-02 09:00:59 +0900128 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
129 if (in instanceof DhcpErrorEvent) {
130 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
131 return true;
132 }
133
134 if (in instanceof DhcpClientEvent) {
135 setDhcpClientEvent(out, (DhcpClientEvent) in);
136 return true;
137 }
138
Hugo Benichi50a84c62016-09-02 09:00:59 +0900139 if (in instanceof IpManagerEvent) {
140 setIpManagerEvent(out, (IpManagerEvent) in);
141 return true;
142 }
143
144 if (in instanceof IpReachabilityEvent) {
145 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
146 return true;
147 }
148
149 if (in instanceof DefaultNetworkEvent) {
150 setDefaultNetworkEvent(out, (DefaultNetworkEvent) in);
151 return true;
152 }
153
154 if (in instanceof NetworkEvent) {
155 setNetworkEvent(out, (NetworkEvent) in);
156 return true;
157 }
158
159 if (in instanceof ValidationProbeEvent) {
160 setValidationProbeEvent(out, (ValidationProbeEvent) in);
161 return true;
162 }
163
164 if (in instanceof ApfProgramEvent) {
165 setApfProgramEvent(out, (ApfProgramEvent) in);
166 return true;
167 }
168
169 if (in instanceof ApfStats) {
170 setApfStats(out, (ApfStats) in);
171 return true;
172 }
173
174 if (in instanceof RaEvent) {
175 setRaEvent(out, (RaEvent) in);
176 return true;
177 }
178
179 return false;
180 }
181
182 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100183 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100184 dhcpEvent.setErrorCode(in.errorCode);
185 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900186 }
187
188 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100189 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100190 dhcpEvent.setStateTransition(in.msg);
191 dhcpEvent.durationMs = in.durationMs;
192 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900193 }
194
Hugo Benichi50a84c62016-09-02 09:00:59 +0900195 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900196 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
197 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100198 ipProvisioningEvent.eventType = in.eventType;
199 ipProvisioningEvent.latencyMs = (int) in.durationMs;
200 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900201 }
202
203 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900204 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
205 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100206 ipReachabilityEvent.eventType = in.eventType;
207 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900208 }
209
210 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900211 IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent =
212 new IpConnectivityLogClass.DefaultNetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100213 defaultNetworkEvent.networkId = netIdOf(in.netId);
214 defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
215 defaultNetworkEvent.transportTypes = in.transportTypes;
216 defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
217 out.setDefaultNetworkEvent(defaultNetworkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900218 }
219
220 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900221 IpConnectivityLogClass.NetworkEvent networkEvent =
222 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100223 networkEvent.networkId = netIdOf(in.netId);
224 networkEvent.eventType = in.eventType;
225 networkEvent.latencyMs = (int) in.durationMs;
226 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900227 }
228
229 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900230 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
231 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100232 validationProbeEvent.latencyMs = (int) in.durationMs;
233 validationProbeEvent.probeType = in.probeType;
234 validationProbeEvent.probeResult = in.returnCode;
235 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900236 }
237
238 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900239 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
240 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100241 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900242 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100243 apfProgramEvent.filteredRas = in.filteredRas;
244 apfProgramEvent.currentRas = in.currentRas;
245 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900246 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100247 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900248 }
249 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100250 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900251 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100252 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900253 }
254
255 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900256 IpConnectivityLogClass.ApfStatistics apfStatistics =
257 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100258 apfStatistics.durationMs = in.durationMs;
259 apfStatistics.receivedRas = in.receivedRas;
260 apfStatistics.matchingRas = in.matchingRas;
261 apfStatistics.droppedRas = in.droppedRas;
262 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
263 apfStatistics.parseErrors = in.parseErrors;
264 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900265 apfStatistics.programUpdatesAll = in.programUpdatesAll;
266 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100267 apfStatistics.maxProgramSize = in.maxProgramSize;
268 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900269 }
270
271 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100272 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
273 raEvent.routerLifetime = in.routerLifetime;
274 raEvent.prefixValidLifetime = in.prefixValidLifetime;
275 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
276 raEvent.routeInfoLifetime = in.routeInfoLifetime;
277 raEvent.rdnssLifetime = in.rdnssLifetime;
278 raEvent.dnsslLifetime = in.dnsslLifetime;
279 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900280 }
281
282 private static int[] bytesToInts(byte[] in) {
283 final int[] out = new int[in.length];
284 for (int i = 0; i < in.length; i++) {
285 out[i] = in[i] & 0xFF;
286 }
287 return out;
288 }
289
Hugo Benichi5eb90532017-03-23 18:38:22 +0900290 private static Pair[] toPairArray(SparseIntArray counts) {
291 final int s = counts.size();
292 Pair[] pairs = new Pair[s];
293 for (int i = 0; i < s; i++) {
294 Pair p = new Pair();
295 p.key = counts.keyAt(i);
296 p.value = counts.valueAt(i);
297 pairs[i] = p;
298 }
299 return pairs;
300 }
301
Hugo Benichi50a84c62016-09-02 09:00:59 +0900302 private static NetworkId netIdOf(int netid) {
303 final NetworkId ni = new NetworkId();
304 ni.networkId = netid;
305 return ni;
306 }
307
308 private static int ipSupportOf(DefaultNetworkEvent in) {
309 if (in.prevIPv4 && in.prevIPv6) {
310 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
311 }
312 if (in.prevIPv6) {
313 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
314 }
315 if (in.prevIPv4) {
316 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
317 }
318 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
319 }
320
321 private static boolean isBitSet(int flags, int bit) {
322 return (flags & (1 << bit)) != 0;
323 }
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900324
325 private static void inferLinkLayer(IpConnectivityEvent ev) {
326 int linkLayer = IpConnectivityLogClass.UNKNOWN;
327 if (ev.transports != 0) {
328 linkLayer = transportsToLinkLayer(ev.transports);
329 } else if (ev.ifName != null) {
330 linkLayer = ifnameToLinkLayer(ev.ifName);
331 }
332 if (linkLayer == IpConnectivityLogClass.UNKNOWN) {
333 return;
334 }
335 ev.linkLayer = linkLayer;
336 ev.ifName = "";
337 }
338
339 private static int transportsToLinkLayer(long transports) {
340 switch (Long.bitCount(transports)) {
341 case 0:
342 return IpConnectivityLogClass.UNKNOWN;
343 case 1:
344 int t = Long.numberOfTrailingZeros(transports);
345 return transportToLinkLayer(t);
346 default:
347 return IpConnectivityLogClass.MULTIPLE;
348 }
349 }
350
351 private static int transportToLinkLayer(int transport) {
352 if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) {
353 return TRANSPORT_LINKLAYER_MAP[transport];
354 }
355 return IpConnectivityLogClass.UNKNOWN;
356 }
357
358 private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1];
359 static {
360 TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR;
361 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI;
362 TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH;
363 TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET;
364 TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN;
365 // TODO: change mapping TRANSPORT_WIFI_AWARE -> WIFI_AWARE
366 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.UNKNOWN;
367 };
368
369 private static int ifnameToLinkLayer(String ifname) {
370 // Do not try to catch all interface names with regexes, instead only catch patterns that
371 // are cheap to check, and otherwise fallback on postprocessing in aggregation layer.
372 for (int i = 0; i < IFNAME_LINKLAYER_MAP.size(); i++) {
373 String pattern = IFNAME_LINKLAYER_MAP.valueAt(i);
374 if (ifname.startsWith(pattern)) {
375 return IFNAME_LINKLAYER_MAP.keyAt(i);
376 }
377 }
378 return IpConnectivityLogClass.UNKNOWN;
379 }
380
381 private static final SparseArray<String> IFNAME_LINKLAYER_MAP = new SparseArray<String>();
382 static {
383 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.CELLULAR, "rmnet");
384 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.WIFI, "wlan");
385 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.BLUETOOTH, "bt-pan");
386 // TODO: rekey to USB
387 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.ETHERNET, "usb");
388 // TODO: add mappings for nan -> WIFI_AWARE and p2p -> WIFI_P2P
389 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900390}