blob: 4991b2b9c2926706c0a4e04fefff07c8d5b61a4c [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
94 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
95 if (in instanceof DhcpErrorEvent) {
96 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
97 return true;
98 }
99
100 if (in instanceof DhcpClientEvent) {
101 setDhcpClientEvent(out, (DhcpClientEvent) in);
102 return true;
103 }
104
105 if (in instanceof DnsEvent) {
106 setDnsEvent(out, (DnsEvent) in);
107 return true;
108 }
109
110 if (in instanceof IpManagerEvent) {
111 setIpManagerEvent(out, (IpManagerEvent) in);
112 return true;
113 }
114
115 if (in instanceof IpReachabilityEvent) {
116 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
117 return true;
118 }
119
120 if (in instanceof DefaultNetworkEvent) {
121 setDefaultNetworkEvent(out, (DefaultNetworkEvent) in);
122 return true;
123 }
124
125 if (in instanceof NetworkEvent) {
126 setNetworkEvent(out, (NetworkEvent) in);
127 return true;
128 }
129
130 if (in instanceof ValidationProbeEvent) {
131 setValidationProbeEvent(out, (ValidationProbeEvent) in);
132 return true;
133 }
134
135 if (in instanceof ApfProgramEvent) {
136 setApfProgramEvent(out, (ApfProgramEvent) in);
137 return true;
138 }
139
140 if (in instanceof ApfStats) {
141 setApfStats(out, (ApfStats) in);
142 return true;
143 }
144
145 if (in instanceof RaEvent) {
146 setRaEvent(out, (RaEvent) in);
147 return true;
148 }
149
150 return false;
151 }
152
153 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100154 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
155 dhcpEvent.ifName = in.ifName;
156 dhcpEvent.setErrorCode(in.errorCode);
157 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900158 }
159
160 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100161 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
162 dhcpEvent.ifName = in.ifName;
163 dhcpEvent.setStateTransition(in.msg);
164 dhcpEvent.durationMs = in.durationMs;
165 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900166 }
167
168 private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900169 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
170 new IpConnectivityLogClass.DNSLookupBatch();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100171 dnsLookupBatch.networkId = netIdOf(in.netId);
172 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
173 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
174 dnsLookupBatch.latenciesMs = in.latenciesMs;
175 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900176 }
177
178 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900179 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
180 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100181 ipProvisioningEvent.ifName = in.ifName;
182 ipProvisioningEvent.eventType = in.eventType;
183 ipProvisioningEvent.latencyMs = (int) in.durationMs;
184 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900185 }
186
187 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900188 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
189 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100190 ipReachabilityEvent.ifName = in.ifName;
191 ipReachabilityEvent.eventType = in.eventType;
192 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900193 }
194
195 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900196 IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent =
197 new IpConnectivityLogClass.DefaultNetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100198 defaultNetworkEvent.networkId = netIdOf(in.netId);
199 defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
200 defaultNetworkEvent.transportTypes = in.transportTypes;
201 defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
202 out.setDefaultNetworkEvent(defaultNetworkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900203 }
204
205 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900206 IpConnectivityLogClass.NetworkEvent networkEvent =
207 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100208 networkEvent.networkId = netIdOf(in.netId);
209 networkEvent.eventType = in.eventType;
210 networkEvent.latencyMs = (int) in.durationMs;
211 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900212 }
213
214 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900215 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
216 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100217 validationProbeEvent.networkId = netIdOf(in.netId);
218 validationProbeEvent.latencyMs = (int) in.durationMs;
219 validationProbeEvent.probeType = in.probeType;
220 validationProbeEvent.probeResult = in.returnCode;
221 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900222 }
223
224 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900225 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
226 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100227 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900228 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100229 apfProgramEvent.filteredRas = in.filteredRas;
230 apfProgramEvent.currentRas = in.currentRas;
231 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900232 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100233 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900234 }
235 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100236 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900237 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100238 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900239 }
240
241 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900242 IpConnectivityLogClass.ApfStatistics apfStatistics =
243 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100244 apfStatistics.durationMs = in.durationMs;
245 apfStatistics.receivedRas = in.receivedRas;
246 apfStatistics.matchingRas = in.matchingRas;
247 apfStatistics.droppedRas = in.droppedRas;
248 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
249 apfStatistics.parseErrors = in.parseErrors;
250 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900251 apfStatistics.programUpdatesAll = in.programUpdatesAll;
252 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100253 apfStatistics.maxProgramSize = in.maxProgramSize;
254 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900255 }
256
257 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100258 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
259 raEvent.routerLifetime = in.routerLifetime;
260 raEvent.prefixValidLifetime = in.prefixValidLifetime;
261 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
262 raEvent.routeInfoLifetime = in.routeInfoLifetime;
263 raEvent.rdnssLifetime = in.rdnssLifetime;
264 raEvent.dnsslLifetime = in.dnsslLifetime;
265 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900266 }
267
268 private static int[] bytesToInts(byte[] in) {
269 final int[] out = new int[in.length];
270 for (int i = 0; i < in.length; i++) {
271 out[i] = in[i] & 0xFF;
272 }
273 return out;
274 }
275
276 private static NetworkId netIdOf(int netid) {
277 final NetworkId ni = new NetworkId();
278 ni.networkId = netid;
279 return ni;
280 }
281
282 private static int ipSupportOf(DefaultNetworkEvent in) {
283 if (in.prevIPv4 && in.prevIPv6) {
284 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
285 }
286 if (in.prevIPv6) {
287 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
288 }
289 if (in.prevIPv4) {
290 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
291 }
292 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
293 }
294
295 private static boolean isBitSet(int flags, int bit) {
296 return (flags & (1 << bit)) != 0;
297 }
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900298
299 private static void inferLinkLayer(IpConnectivityEvent ev) {
300 int linkLayer = IpConnectivityLogClass.UNKNOWN;
301 if (ev.transports != 0) {
302 linkLayer = transportsToLinkLayer(ev.transports);
303 } else if (ev.ifName != null) {
304 linkLayer = ifnameToLinkLayer(ev.ifName);
305 }
306 if (linkLayer == IpConnectivityLogClass.UNKNOWN) {
307 return;
308 }
309 ev.linkLayer = linkLayer;
310 ev.ifName = "";
311 }
312
313 private static int transportsToLinkLayer(long transports) {
314 switch (Long.bitCount(transports)) {
315 case 0:
316 return IpConnectivityLogClass.UNKNOWN;
317 case 1:
318 int t = Long.numberOfTrailingZeros(transports);
319 return transportToLinkLayer(t);
320 default:
321 return IpConnectivityLogClass.MULTIPLE;
322 }
323 }
324
325 private static int transportToLinkLayer(int transport) {
326 if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) {
327 return TRANSPORT_LINKLAYER_MAP[transport];
328 }
329 return IpConnectivityLogClass.UNKNOWN;
330 }
331
332 private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1];
333 static {
334 TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR;
335 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI;
336 TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH;
337 TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET;
338 TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN;
339 // TODO: change mapping TRANSPORT_WIFI_AWARE -> WIFI_AWARE
340 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.UNKNOWN;
341 };
342
343 private static int ifnameToLinkLayer(String ifname) {
344 // Do not try to catch all interface names with regexes, instead only catch patterns that
345 // are cheap to check, and otherwise fallback on postprocessing in aggregation layer.
346 for (int i = 0; i < IFNAME_LINKLAYER_MAP.size(); i++) {
347 String pattern = IFNAME_LINKLAYER_MAP.valueAt(i);
348 if (ifname.startsWith(pattern)) {
349 return IFNAME_LINKLAYER_MAP.keyAt(i);
350 }
351 }
352 return IpConnectivityLogClass.UNKNOWN;
353 }
354
355 private static final SparseArray<String> IFNAME_LINKLAYER_MAP = new SparseArray<String>();
356 static {
357 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.CELLULAR, "rmnet");
358 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.WIFI, "wlan");
359 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.BLUETOOTH, "bt-pan");
360 // TODO: rekey to USB
361 IFNAME_LINKLAYER_MAP.put(IpConnectivityLogClass.ETHERNET, "usb");
362 // TODO: add mappings for nan -> WIFI_AWARE and p2p -> WIFI_P2P
363 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900364}