blob: 2dfee11815a458f0ff1f8b6aa950e88af94d1785 [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;
Hugo Benichi27a51172017-07-18 11:35:18 +090023import static android.net.NetworkCapabilities.TRANSPORT_LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090024import static android.net.NetworkCapabilities.TRANSPORT_VPN;
25import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
26import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090027
Hugo Benichi380a0632017-10-20 09:25:29 +090028import android.net.ConnectivityManager;
Hugo Benichi50a84c62016-09-02 09:00:59 +090029import android.net.ConnectivityMetricsEvent;
30import android.net.metrics.ApfProgramEvent;
31import android.net.metrics.ApfStats;
32import android.net.metrics.DefaultNetworkEvent;
33import android.net.metrics.DhcpClientEvent;
34import android.net.metrics.DhcpErrorEvent;
35import android.net.metrics.DnsEvent;
Hugo Benichi5eb90532017-03-23 18:38:22 +090036import android.net.metrics.ConnectStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090037import android.net.metrics.IpManagerEvent;
38import android.net.metrics.IpReachabilityEvent;
39import android.net.metrics.NetworkEvent;
40import android.net.metrics.RaEvent;
41import android.net.metrics.ValidationProbeEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090042import android.net.metrics.WakeupStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090043import android.os.Parcelable;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090044import android.util.SparseArray;
Hugo Benichi5eb90532017-03-23 18:38:22 +090045import android.util.SparseIntArray;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010046import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi5eb90532017-03-23 18:38:22 +090047import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
48import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
49import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId;
50import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.Pair;
Hugo Benichi50a84c62016-09-02 09:00:59 +090051import java.io.IOException;
52import java.util.ArrayList;
53import java.util.List;
54
Hugo Benichi50a84c62016-09-02 09:00:59 +090055
56/** {@hide} */
57final public class IpConnectivityEventBuilder {
58 private IpConnectivityEventBuilder() {
59 }
60
Hugo Benichi0d4a3982016-11-25 11:24:22 +090061 public static byte[] serialize(int dropped, List<IpConnectivityEvent> events)
Hugo Benichi50a84c62016-09-02 09:00:59 +090062 throws IOException {
63 final IpConnectivityLog log = new IpConnectivityLog();
Hugo Benichi0d4a3982016-11-25 11:24:22 +090064 log.events = events.toArray(new IpConnectivityEvent[events.size()]);
Hugo Benichi50a84c62016-09-02 09:00:59 +090065 log.droppedEvents = dropped;
Hugo Benichid680d4c2016-10-13 13:16:16 +090066 if ((log.events.length > 0) || (dropped > 0)) {
67 // Only write version number if log has some information at all.
68 log.version = IpConnectivityMetrics.VERSION;
69 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090070 return IpConnectivityLog.toByteArray(log);
71 }
72
Hugo Benichi0d4a3982016-11-25 11:24:22 +090073 public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090074 final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
75 for (ConnectivityMetricsEvent in : eventsIn) {
76 final IpConnectivityEvent out = toProto(in);
77 if (out == null) {
78 continue;
79 }
80 eventsOut.add(out);
81 }
Hugo Benichi0d4a3982016-11-25 11:24:22 +090082 return eventsOut;
Hugo Benichi50a84c62016-09-02 09:00:59 +090083 }
84
85 public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
Hugo Benichi5eb90532017-03-23 18:38:22 +090086 final IpConnectivityEvent out = buildEvent(ev.netId, ev.transports, ev.ifname);
87 out.timeMs = ev.timestamp;
Hugo Benichi50a84c62016-09-02 09:00:59 +090088 if (!setEvent(out, ev.data)) {
89 return null;
90 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090091 return out;
92 }
93
Hugo Benichi5eb90532017-03-23 18:38:22 +090094 public static IpConnectivityEvent toProto(ConnectStats in) {
95 IpConnectivityLogClass.ConnectStatistics stats =
96 new IpConnectivityLogClass.ConnectStatistics();
97 stats.connectCount = in.connectCount;
98 stats.connectBlockingCount = in.connectBlockingCount;
99 stats.ipv6AddrCount = in.ipv6ConnectCount;
100 stats.latenciesMs = in.latencies.toArray();
101 stats.errnosCounters = toPairArray(in.errnos);
102 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
103 out.setConnectStatistics(stats);
104 return out;
105 }
106
107
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900108 public static IpConnectivityEvent toProto(DnsEvent in) {
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900109 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
110 new IpConnectivityLogClass.DNSLookupBatch();
111 in.resize(in.eventCount);
112 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
113 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
114 dnsLookupBatch.latenciesMs = in.latenciesMs;
Hugo Benichi5eb90532017-03-23 18:38:22 +0900115 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900116 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900117 return out;
118 }
119
Hugo Benichi60c9f632017-09-05 13:34:48 +0900120 public static IpConnectivityEvent toProto(WakeupStats in) {
121 IpConnectivityLogClass.WakeupStats wakeupStats =
122 new IpConnectivityLogClass.WakeupStats();
123 in.updateDuration();
124 wakeupStats.durationSec = in.durationSec;
125 wakeupStats.totalWakeups = in.totalWakeups;
126 wakeupStats.rootWakeups = in.rootWakeups;
127 wakeupStats.systemWakeups = in.systemWakeups;
128 wakeupStats.nonApplicationWakeups = in.nonApplicationWakeups;
129 wakeupStats.applicationWakeups = in.applicationWakeups;
Hugo Benichi175b5742017-09-19 13:15:26 +0900130 wakeupStats.noUidWakeups = in.noUidWakeups;
Hugo Benichid921bce2017-10-12 21:33:40 +0900131 wakeupStats.l2UnicastCount = in.l2UnicastCount;
132 wakeupStats.l2MulticastCount = in.l2MulticastCount;
133 wakeupStats.l2BroadcastCount = in.l2BroadcastCount;
134 wakeupStats.ethertypeCounts = toPairArray(in.ethertypes);
135 wakeupStats.ipNextHeaderCounts = toPairArray(in.ipNextHeaders);
Hugo Benichi60c9f632017-09-05 13:34:48 +0900136 final IpConnectivityEvent out = buildEvent(0, 0, in.iface);
137 out.setWakeupStats(wakeupStats);
138 return out;
139 }
140
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900141 public static IpConnectivityEvent toProto(DefaultNetworkEvent in) {
142 IpConnectivityLogClass.DefaultNetworkEvent ev =
143 new IpConnectivityLogClass.DefaultNetworkEvent();
Hugo Benichi380a0632017-10-20 09:25:29 +0900144 ev.finalScore = in.finalScore;
145 ev.initialScore = in.initialScore;
146 ev.ipSupport = ipSupportOf(in);
147 ev.defaultNetworkDurationMs = in.durationMs;
148 ev.validationDurationMs = in.validatedMs;
149 ev.previousDefaultNetworkLinkLayer = transportsToLinkLayer(in.previousTransports);
150 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
151 if (in.transports == 0) {
152 // Set link layer to NONE for events representing the absence of a default network.
153 out.linkLayer = IpConnectivityLogClass.NONE;
154 }
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900155 out.setDefaultNetworkEvent(ev);
156 return out;
157 }
158
Hugo Benichi5eb90532017-03-23 18:38:22 +0900159 private static IpConnectivityEvent buildEvent(int netId, long transports, String ifname) {
160 final IpConnectivityEvent ev = new IpConnectivityEvent();
161 ev.networkId = netId;
162 ev.transports = transports;
163 if (ifname != null) {
164 ev.ifName = ifname;
165 }
166 inferLinkLayer(ev);
167 return ev;
168 }
169
Hugo Benichi50a84c62016-09-02 09:00:59 +0900170 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
171 if (in instanceof DhcpErrorEvent) {
172 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
173 return true;
174 }
175
176 if (in instanceof DhcpClientEvent) {
177 setDhcpClientEvent(out, (DhcpClientEvent) in);
178 return true;
179 }
180
Hugo Benichi50a84c62016-09-02 09:00:59 +0900181 if (in instanceof IpManagerEvent) {
182 setIpManagerEvent(out, (IpManagerEvent) in);
183 return true;
184 }
185
186 if (in instanceof IpReachabilityEvent) {
187 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
188 return true;
189 }
190
Hugo Benichi50a84c62016-09-02 09:00:59 +0900191 if (in instanceof NetworkEvent) {
192 setNetworkEvent(out, (NetworkEvent) in);
193 return true;
194 }
195
196 if (in instanceof ValidationProbeEvent) {
197 setValidationProbeEvent(out, (ValidationProbeEvent) in);
198 return true;
199 }
200
201 if (in instanceof ApfProgramEvent) {
202 setApfProgramEvent(out, (ApfProgramEvent) in);
203 return true;
204 }
205
206 if (in instanceof ApfStats) {
207 setApfStats(out, (ApfStats) in);
208 return true;
209 }
210
211 if (in instanceof RaEvent) {
212 setRaEvent(out, (RaEvent) in);
213 return true;
214 }
215
216 return false;
217 }
218
219 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100220 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100221 dhcpEvent.setErrorCode(in.errorCode);
222 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900223 }
224
225 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100226 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100227 dhcpEvent.setStateTransition(in.msg);
228 dhcpEvent.durationMs = in.durationMs;
229 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900230 }
231
Hugo Benichi50a84c62016-09-02 09:00:59 +0900232 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900233 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
234 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100235 ipProvisioningEvent.eventType = in.eventType;
236 ipProvisioningEvent.latencyMs = (int) in.durationMs;
237 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900238 }
239
240 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900241 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
242 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100243 ipReachabilityEvent.eventType = in.eventType;
244 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900245 }
246
Hugo Benichi50a84c62016-09-02 09:00:59 +0900247 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900248 IpConnectivityLogClass.NetworkEvent networkEvent =
249 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100250 networkEvent.networkId = netIdOf(in.netId);
251 networkEvent.eventType = in.eventType;
252 networkEvent.latencyMs = (int) in.durationMs;
253 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900254 }
255
256 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900257 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
258 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100259 validationProbeEvent.latencyMs = (int) in.durationMs;
260 validationProbeEvent.probeType = in.probeType;
261 validationProbeEvent.probeResult = in.returnCode;
262 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900263 }
264
265 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900266 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
267 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100268 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900269 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100270 apfProgramEvent.filteredRas = in.filteredRas;
271 apfProgramEvent.currentRas = in.currentRas;
272 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900273 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100274 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900275 }
276 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100277 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900278 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100279 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900280 }
281
282 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900283 IpConnectivityLogClass.ApfStatistics apfStatistics =
284 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100285 apfStatistics.durationMs = in.durationMs;
286 apfStatistics.receivedRas = in.receivedRas;
287 apfStatistics.matchingRas = in.matchingRas;
288 apfStatistics.droppedRas = in.droppedRas;
289 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
290 apfStatistics.parseErrors = in.parseErrors;
291 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900292 apfStatistics.programUpdatesAll = in.programUpdatesAll;
293 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100294 apfStatistics.maxProgramSize = in.maxProgramSize;
295 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900296 }
297
298 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100299 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
300 raEvent.routerLifetime = in.routerLifetime;
301 raEvent.prefixValidLifetime = in.prefixValidLifetime;
302 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
303 raEvent.routeInfoLifetime = in.routeInfoLifetime;
304 raEvent.rdnssLifetime = in.rdnssLifetime;
305 raEvent.dnsslLifetime = in.dnsslLifetime;
306 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900307 }
308
309 private static int[] bytesToInts(byte[] in) {
310 final int[] out = new int[in.length];
311 for (int i = 0; i < in.length; i++) {
312 out[i] = in[i] & 0xFF;
313 }
314 return out;
315 }
316
Hugo Benichi5eb90532017-03-23 18:38:22 +0900317 private static Pair[] toPairArray(SparseIntArray counts) {
318 final int s = counts.size();
319 Pair[] pairs = new Pair[s];
320 for (int i = 0; i < s; i++) {
321 Pair p = new Pair();
322 p.key = counts.keyAt(i);
323 p.value = counts.valueAt(i);
324 pairs[i] = p;
325 }
326 return pairs;
327 }
328
Hugo Benichi50a84c62016-09-02 09:00:59 +0900329 private static NetworkId netIdOf(int netid) {
330 final NetworkId ni = new NetworkId();
331 ni.networkId = netid;
332 return ni;
333 }
334
335 private static int ipSupportOf(DefaultNetworkEvent in) {
Hugo Benichi380a0632017-10-20 09:25:29 +0900336 if (in.ipv4 && in.ipv6) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900337 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
338 }
Hugo Benichi380a0632017-10-20 09:25:29 +0900339 if (in.ipv6) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900340 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
341 }
Hugo Benichi380a0632017-10-20 09:25:29 +0900342 if (in.ipv4) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900343 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
344 }
345 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
346 }
347
348 private static boolean isBitSet(int flags, int bit) {
349 return (flags & (1 << bit)) != 0;
350 }
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900351
352 private static void inferLinkLayer(IpConnectivityEvent ev) {
353 int linkLayer = IpConnectivityLogClass.UNKNOWN;
354 if (ev.transports != 0) {
355 linkLayer = transportsToLinkLayer(ev.transports);
356 } else if (ev.ifName != null) {
357 linkLayer = ifnameToLinkLayer(ev.ifName);
358 }
359 if (linkLayer == IpConnectivityLogClass.UNKNOWN) {
360 return;
361 }
362 ev.linkLayer = linkLayer;
363 ev.ifName = "";
364 }
365
366 private static int transportsToLinkLayer(long transports) {
367 switch (Long.bitCount(transports)) {
368 case 0:
369 return IpConnectivityLogClass.UNKNOWN;
370 case 1:
371 int t = Long.numberOfTrailingZeros(transports);
372 return transportToLinkLayer(t);
373 default:
374 return IpConnectivityLogClass.MULTIPLE;
375 }
376 }
377
378 private static int transportToLinkLayer(int transport) {
379 if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) {
380 return TRANSPORT_LINKLAYER_MAP[transport];
381 }
382 return IpConnectivityLogClass.UNKNOWN;
383 }
384
385 private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1];
386 static {
387 TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR;
388 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI;
389 TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH;
390 TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET;
391 TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN;
Hugo Benichi27a51172017-07-18 11:35:18 +0900392 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.WIFI_NAN;
393 TRANSPORT_LINKLAYER_MAP[TRANSPORT_LOWPAN] = IpConnectivityLogClass.LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900394 };
395
396 private static int ifnameToLinkLayer(String ifname) {
397 // Do not try to catch all interface names with regexes, instead only catch patterns that
398 // are cheap to check, and otherwise fallback on postprocessing in aggregation layer.
Hugo Benichi27a51172017-07-18 11:35:18 +0900399 for (int i = 0; i < KNOWN_PREFIX; i++) {
400 String pattern = IFNAME_PREFIXES[i];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900401 if (ifname.startsWith(pattern)) {
Hugo Benichi27a51172017-07-18 11:35:18 +0900402 return IFNAME_LINKLAYERS[i];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900403 }
404 }
405 return IpConnectivityLogClass.UNKNOWN;
406 }
407
Hugo Benichi27a51172017-07-18 11:35:18 +0900408 private static final int KNOWN_PREFIX = 7;
409 private static final String[] IFNAME_PREFIXES = new String[KNOWN_PREFIX];
410 private static final int[] IFNAME_LINKLAYERS = new int[KNOWN_PREFIX];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900411 static {
Hugo Benichi27a51172017-07-18 11:35:18 +0900412 // Ordered from most likely link layer to least likely.
413 IFNAME_PREFIXES[0] = "rmnet";
414 IFNAME_LINKLAYERS[0] = IpConnectivityLogClass.CELLULAR;
415
416 IFNAME_PREFIXES[1] = "wlan";
417 IFNAME_LINKLAYERS[1] = IpConnectivityLogClass.WIFI;
418
419 IFNAME_PREFIXES[2] = "bt-pan";
420 IFNAME_LINKLAYERS[2] = IpConnectivityLogClass.BLUETOOTH;
421
422 IFNAME_PREFIXES[3] = "p2p";
423 IFNAME_LINKLAYERS[3] = IpConnectivityLogClass.WIFI_P2P;
424
425 IFNAME_PREFIXES[4] = "aware";
426 IFNAME_LINKLAYERS[4] = IpConnectivityLogClass.WIFI_NAN;
427
428 IFNAME_PREFIXES[5] = "eth";
429 IFNAME_LINKLAYERS[5] = IpConnectivityLogClass.ETHERNET;
430
431 IFNAME_PREFIXES[6] = "wpan";
432 IFNAME_LINKLAYERS[6] = IpConnectivityLogClass.LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900433 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900434}