blob: f05acefadb3dbb24f4aa8234e6a6714d08a1939d [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;
26import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
27import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
28import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId;
29
Hugo Benichi50a84c62016-09-02 09:00:59 +090030import android.net.ConnectivityMetricsEvent;
31import android.net.metrics.ApfProgramEvent;
32import android.net.metrics.ApfStats;
33import android.net.metrics.DefaultNetworkEvent;
34import android.net.metrics.DhcpClientEvent;
35import android.net.metrics.DhcpErrorEvent;
36import android.net.metrics.DnsEvent;
37import android.net.metrics.IpManagerEvent;
38import android.net.metrics.IpReachabilityEvent;
39import android.net.metrics.NetworkEvent;
40import android.net.metrics.RaEvent;
41import android.net.metrics.ValidationProbeEvent;
42import android.os.Parcelable;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090043import android.util.SparseArray;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010044import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi50a84c62016-09-02 09:00:59 +090045import java.io.IOException;
46import java.util.ArrayList;
47import java.util.List;
48
Hugo Benichi50a84c62016-09-02 09:00:59 +090049
50/** {@hide} */
51final public class IpConnectivityEventBuilder {
52 private IpConnectivityEventBuilder() {
53 }
54
Hugo Benichi0d4a3982016-11-25 11:24:22 +090055 public static byte[] serialize(int dropped, List<IpConnectivityEvent> events)
Hugo Benichi50a84c62016-09-02 09:00:59 +090056 throws IOException {
57 final IpConnectivityLog log = new IpConnectivityLog();
Hugo Benichi0d4a3982016-11-25 11:24:22 +090058 log.events = events.toArray(new IpConnectivityEvent[events.size()]);
Hugo Benichi50a84c62016-09-02 09:00:59 +090059 log.droppedEvents = dropped;
Hugo Benichid680d4c2016-10-13 13:16:16 +090060 if ((log.events.length > 0) || (dropped > 0)) {
61 // Only write version number if log has some information at all.
62 log.version = IpConnectivityMetrics.VERSION;
63 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090064 return IpConnectivityLog.toByteArray(log);
65 }
66
Hugo Benichi0d4a3982016-11-25 11:24:22 +090067 public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090068 final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
69 for (ConnectivityMetricsEvent in : eventsIn) {
70 final IpConnectivityEvent out = toProto(in);
71 if (out == null) {
72 continue;
73 }
74 eventsOut.add(out);
75 }
Hugo Benichi0d4a3982016-11-25 11:24:22 +090076 return eventsOut;
Hugo Benichi50a84c62016-09-02 09:00:59 +090077 }
78
79 public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
80 final IpConnectivityEvent out = new IpConnectivityEvent();
81 if (!setEvent(out, ev.data)) {
82 return null;
83 }
84 out.timeMs = ev.timestamp;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090085 out.networkId = ev.netId;
86 out.transports = ev.transports;
87 if (ev.ifname != null) {
88 out.ifName = ev.ifname;
89 }
90 inferLinkLayer(out);
Hugo Benichi50a84c62016-09-02 09:00:59 +090091 return out;
92 }
93
Hugo Benichi2a5cfb92017-03-22 22:21:44 +090094 public static IpConnectivityEvent toProto(DnsEvent in) {
95 final IpConnectivityEvent out = new IpConnectivityEvent();
96 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
97 new IpConnectivityLogClass.DNSLookupBatch();
98 in.resize(in.eventCount);
99 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
100 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
101 dnsLookupBatch.latenciesMs = in.latenciesMs;
102 out.setDnsLookupBatch(dnsLookupBatch);
103 out.networkId = in.netId;
104 out.transports = in.transports;
105 inferLinkLayer(out);
106 return out;
107 }
108
Hugo Benichi50a84c62016-09-02 09:00:59 +0900109 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
110 if (in instanceof DhcpErrorEvent) {
111 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
112 return true;
113 }
114
115 if (in instanceof DhcpClientEvent) {
116 setDhcpClientEvent(out, (DhcpClientEvent) in);
117 return true;
118 }
119
Hugo Benichi50a84c62016-09-02 09:00:59 +0900120 if (in instanceof IpManagerEvent) {
121 setIpManagerEvent(out, (IpManagerEvent) in);
122 return true;
123 }
124
125 if (in instanceof IpReachabilityEvent) {
126 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
127 return true;
128 }
129
130 if (in instanceof DefaultNetworkEvent) {
131 setDefaultNetworkEvent(out, (DefaultNetworkEvent) in);
132 return true;
133 }
134
135 if (in instanceof NetworkEvent) {
136 setNetworkEvent(out, (NetworkEvent) in);
137 return true;
138 }
139
140 if (in instanceof ValidationProbeEvent) {
141 setValidationProbeEvent(out, (ValidationProbeEvent) in);
142 return true;
143 }
144
145 if (in instanceof ApfProgramEvent) {
146 setApfProgramEvent(out, (ApfProgramEvent) in);
147 return true;
148 }
149
150 if (in instanceof ApfStats) {
151 setApfStats(out, (ApfStats) in);
152 return true;
153 }
154
155 if (in instanceof RaEvent) {
156 setRaEvent(out, (RaEvent) in);
157 return true;
158 }
159
160 return false;
161 }
162
163 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100164 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100165 dhcpEvent.setErrorCode(in.errorCode);
166 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900167 }
168
169 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100170 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100171 dhcpEvent.setStateTransition(in.msg);
172 dhcpEvent.durationMs = in.durationMs;
173 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900174 }
175
Hugo Benichi50a84c62016-09-02 09:00:59 +0900176 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900177 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
178 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100179 ipProvisioningEvent.eventType = in.eventType;
180 ipProvisioningEvent.latencyMs = (int) in.durationMs;
181 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900182 }
183
184 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900185 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
186 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100187 ipReachabilityEvent.eventType = in.eventType;
188 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900189 }
190
191 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900192 IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent =
193 new IpConnectivityLogClass.DefaultNetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100194 defaultNetworkEvent.networkId = netIdOf(in.netId);
195 defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
196 defaultNetworkEvent.transportTypes = in.transportTypes;
197 defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
198 out.setDefaultNetworkEvent(defaultNetworkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900199 }
200
201 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900202 IpConnectivityLogClass.NetworkEvent networkEvent =
203 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100204 networkEvent.networkId = netIdOf(in.netId);
205 networkEvent.eventType = in.eventType;
206 networkEvent.latencyMs = (int) in.durationMs;
207 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900208 }
209
210 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900211 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
212 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100213 validationProbeEvent.latencyMs = (int) in.durationMs;
214 validationProbeEvent.probeType = in.probeType;
215 validationProbeEvent.probeResult = in.returnCode;
216 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900217 }
218
219 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900220 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
221 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100222 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900223 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100224 apfProgramEvent.filteredRas = in.filteredRas;
225 apfProgramEvent.currentRas = in.currentRas;
226 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900227 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100228 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900229 }
230 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100231 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900232 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100233 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900234 }
235
236 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900237 IpConnectivityLogClass.ApfStatistics apfStatistics =
238 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100239 apfStatistics.durationMs = in.durationMs;
240 apfStatistics.receivedRas = in.receivedRas;
241 apfStatistics.matchingRas = in.matchingRas;
242 apfStatistics.droppedRas = in.droppedRas;
243 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
244 apfStatistics.parseErrors = in.parseErrors;
245 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900246 apfStatistics.programUpdatesAll = in.programUpdatesAll;
247 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100248 apfStatistics.maxProgramSize = in.maxProgramSize;
249 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900250 }
251
252 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100253 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
254 raEvent.routerLifetime = in.routerLifetime;
255 raEvent.prefixValidLifetime = in.prefixValidLifetime;
256 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
257 raEvent.routeInfoLifetime = in.routeInfoLifetime;
258 raEvent.rdnssLifetime = in.rdnssLifetime;
259 raEvent.dnsslLifetime = in.dnsslLifetime;
260 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900261 }
262
263 private static int[] bytesToInts(byte[] in) {
264 final int[] out = new int[in.length];
265 for (int i = 0; i < in.length; i++) {
266 out[i] = in[i] & 0xFF;
267 }
268 return out;
269 }
270
271 private static NetworkId netIdOf(int netid) {
272 final NetworkId ni = new NetworkId();
273 ni.networkId = netid;
274 return ni;
275 }
276
277 private static int ipSupportOf(DefaultNetworkEvent in) {
278 if (in.prevIPv4 && in.prevIPv6) {
279 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
280 }
281 if (in.prevIPv6) {
282 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
283 }
284 if (in.prevIPv4) {
285 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
286 }
287 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
288 }
289
290 private static boolean isBitSet(int flags, int bit) {
291 return (flags & (1 << bit)) != 0;
292 }
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900293
294 private static void inferLinkLayer(IpConnectivityEvent ev) {
295 int linkLayer = IpConnectivityLogClass.UNKNOWN;
296 if (ev.transports != 0) {
297 linkLayer = transportsToLinkLayer(ev.transports);
298 } else if (ev.ifName != null) {
299 linkLayer = ifnameToLinkLayer(ev.ifName);
300 }
301 if (linkLayer == IpConnectivityLogClass.UNKNOWN) {
302 return;
303 }
304 ev.linkLayer = linkLayer;
305 ev.ifName = "";
306 }
307
308 private static int transportsToLinkLayer(long transports) {
309 switch (Long.bitCount(transports)) {
310 case 0:
311 return IpConnectivityLogClass.UNKNOWN;
312 case 1:
313 int t = Long.numberOfTrailingZeros(transports);
314 return transportToLinkLayer(t);
315 default:
316 return IpConnectivityLogClass.MULTIPLE;
317 }
318 }
319
320 private static int transportToLinkLayer(int transport) {
321 if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) {
322 return TRANSPORT_LINKLAYER_MAP[transport];
323 }
324 return IpConnectivityLogClass.UNKNOWN;
325 }
326
327 private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1];
328 static {
329 TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR;
330 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI;
331 TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH;
332 TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET;
333 TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN;
334 // TODO: change mapping TRANSPORT_WIFI_AWARE -> WIFI_AWARE
335 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.UNKNOWN;
336 };
337
338 private static int ifnameToLinkLayer(String ifname) {
339 // Do not try to catch all interface names with regexes, instead only catch patterns that
340 // are cheap to check, and otherwise fallback on postprocessing in aggregation layer.
341 for (int i = 0; i < IFNAME_LINKLAYER_MAP.size(); i++) {
342 String pattern = IFNAME_LINKLAYER_MAP.valueAt(i);
343 if (ifname.startsWith(pattern)) {
344 return IFNAME_LINKLAYER_MAP.keyAt(i);
345 }
346 }
347 return IpConnectivityLogClass.UNKNOWN;
348 }
349
350 private static final SparseArray<String> IFNAME_LINKLAYER_MAP = new SparseArray<String>();
351 static {
352 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.CELLULAR, "rmnet");
353 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.WIFI, "wlan");
354 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.BLUETOOTH, "bt-pan");
355 // TODO: rekey to USB
356 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.ETHERNET, "usb");
357 // TODO: add mappings for nan -> WIFI_AWARE and p2p -> WIFI_P2P
358 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900359}