blob: 81e891a90499d382ca20c73bf5a81af6f6457817 [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
19import android.net.ConnectivityMetricsEvent;
20import android.net.metrics.ApfProgramEvent;
21import android.net.metrics.ApfStats;
22import android.net.metrics.DefaultNetworkEvent;
23import android.net.metrics.DhcpClientEvent;
24import android.net.metrics.DhcpErrorEvent;
25import android.net.metrics.DnsEvent;
26import android.net.metrics.IpManagerEvent;
27import android.net.metrics.IpReachabilityEvent;
28import android.net.metrics.NetworkEvent;
29import android.net.metrics.RaEvent;
30import android.net.metrics.ValidationProbeEvent;
31import android.os.Parcelable;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010032import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi50a84c62016-09-02 09:00:59 +090033import java.io.IOException;
34import java.util.ArrayList;
35import java.util.List;
36
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010037import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
38import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
39import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId;
Hugo Benichi50a84c62016-09-02 09:00:59 +090040
41/** {@hide} */
42final public class IpConnectivityEventBuilder {
43 private IpConnectivityEventBuilder() {
44 }
45
Hugo Benichi0d4a3982016-11-25 11:24:22 +090046 public static byte[] serialize(int dropped, List<IpConnectivityEvent> events)
Hugo Benichi50a84c62016-09-02 09:00:59 +090047 throws IOException {
48 final IpConnectivityLog log = new IpConnectivityLog();
Hugo Benichi0d4a3982016-11-25 11:24:22 +090049 log.events = events.toArray(new IpConnectivityEvent[events.size()]);
Hugo Benichi50a84c62016-09-02 09:00:59 +090050 log.droppedEvents = dropped;
Hugo Benichid680d4c2016-10-13 13:16:16 +090051 if ((log.events.length > 0) || (dropped > 0)) {
52 // Only write version number if log has some information at all.
53 log.version = IpConnectivityMetrics.VERSION;
54 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090055 return IpConnectivityLog.toByteArray(log);
56 }
57
Hugo Benichi0d4a3982016-11-25 11:24:22 +090058 public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090059 final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
60 for (ConnectivityMetricsEvent in : eventsIn) {
61 final IpConnectivityEvent out = toProto(in);
62 if (out == null) {
63 continue;
64 }
65 eventsOut.add(out);
66 }
Hugo Benichi0d4a3982016-11-25 11:24:22 +090067 return eventsOut;
Hugo Benichi50a84c62016-09-02 09:00:59 +090068 }
69
70 public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
71 final IpConnectivityEvent out = new IpConnectivityEvent();
72 if (!setEvent(out, ev.data)) {
73 return null;
74 }
75 out.timeMs = ev.timestamp;
76 return out;
77 }
78
79 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
80 if (in instanceof DhcpErrorEvent) {
81 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
82 return true;
83 }
84
85 if (in instanceof DhcpClientEvent) {
86 setDhcpClientEvent(out, (DhcpClientEvent) in);
87 return true;
88 }
89
90 if (in instanceof DnsEvent) {
91 setDnsEvent(out, (DnsEvent) in);
92 return true;
93 }
94
95 if (in instanceof IpManagerEvent) {
96 setIpManagerEvent(out, (IpManagerEvent) in);
97 return true;
98 }
99
100 if (in instanceof IpReachabilityEvent) {
101 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
102 return true;
103 }
104
105 if (in instanceof DefaultNetworkEvent) {
106 setDefaultNetworkEvent(out, (DefaultNetworkEvent) in);
107 return true;
108 }
109
110 if (in instanceof NetworkEvent) {
111 setNetworkEvent(out, (NetworkEvent) in);
112 return true;
113 }
114
115 if (in instanceof ValidationProbeEvent) {
116 setValidationProbeEvent(out, (ValidationProbeEvent) in);
117 return true;
118 }
119
120 if (in instanceof ApfProgramEvent) {
121 setApfProgramEvent(out, (ApfProgramEvent) in);
122 return true;
123 }
124
125 if (in instanceof ApfStats) {
126 setApfStats(out, (ApfStats) in);
127 return true;
128 }
129
130 if (in instanceof RaEvent) {
131 setRaEvent(out, (RaEvent) in);
132 return true;
133 }
134
135 return false;
136 }
137
138 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100139 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
140 dhcpEvent.ifName = in.ifName;
141 dhcpEvent.setErrorCode(in.errorCode);
142 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900143 }
144
145 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100146 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
147 dhcpEvent.ifName = in.ifName;
148 dhcpEvent.setStateTransition(in.msg);
149 dhcpEvent.durationMs = in.durationMs;
150 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900151 }
152
153 private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900154 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
155 new IpConnectivityLogClass.DNSLookupBatch();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100156 dnsLookupBatch.networkId = netIdOf(in.netId);
157 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
158 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
159 dnsLookupBatch.latenciesMs = in.latenciesMs;
160 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900161 }
162
163 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900164 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
165 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100166 ipProvisioningEvent.ifName = in.ifName;
167 ipProvisioningEvent.eventType = in.eventType;
168 ipProvisioningEvent.latencyMs = (int) in.durationMs;
169 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900170 }
171
172 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900173 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
174 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100175 ipReachabilityEvent.ifName = in.ifName;
176 ipReachabilityEvent.eventType = in.eventType;
177 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900178 }
179
180 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900181 IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent =
182 new IpConnectivityLogClass.DefaultNetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100183 defaultNetworkEvent.networkId = netIdOf(in.netId);
184 defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
185 defaultNetworkEvent.transportTypes = in.transportTypes;
186 defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
187 out.setDefaultNetworkEvent(defaultNetworkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900188 }
189
190 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900191 IpConnectivityLogClass.NetworkEvent networkEvent =
192 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100193 networkEvent.networkId = netIdOf(in.netId);
194 networkEvent.eventType = in.eventType;
195 networkEvent.latencyMs = (int) in.durationMs;
196 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900197 }
198
199 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900200 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
201 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100202 validationProbeEvent.networkId = netIdOf(in.netId);
203 validationProbeEvent.latencyMs = (int) in.durationMs;
204 validationProbeEvent.probeType = in.probeType;
205 validationProbeEvent.probeResult = in.returnCode;
206 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900207 }
208
209 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900210 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
211 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100212 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900213 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100214 apfProgramEvent.filteredRas = in.filteredRas;
215 apfProgramEvent.currentRas = in.currentRas;
216 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900217 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100218 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900219 }
220 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100221 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900222 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100223 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900224 }
225
226 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900227 IpConnectivityLogClass.ApfStatistics apfStatistics =
228 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100229 apfStatistics.durationMs = in.durationMs;
230 apfStatistics.receivedRas = in.receivedRas;
231 apfStatistics.matchingRas = in.matchingRas;
232 apfStatistics.droppedRas = in.droppedRas;
233 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
234 apfStatistics.parseErrors = in.parseErrors;
235 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900236 apfStatistics.programUpdatesAll = in.programUpdatesAll;
237 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100238 apfStatistics.maxProgramSize = in.maxProgramSize;
239 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900240 }
241
242 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100243 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
244 raEvent.routerLifetime = in.routerLifetime;
245 raEvent.prefixValidLifetime = in.prefixValidLifetime;
246 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
247 raEvent.routeInfoLifetime = in.routeInfoLifetime;
248 raEvent.rdnssLifetime = in.rdnssLifetime;
249 raEvent.dnsslLifetime = in.dnsslLifetime;
250 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900251 }
252
253 private static int[] bytesToInts(byte[] in) {
254 final int[] out = new int[in.length];
255 for (int i = 0; i < in.length; i++) {
256 out[i] = in[i] & 0xFF;
257 }
258 return out;
259 }
260
261 private static NetworkId netIdOf(int netid) {
262 final NetworkId ni = new NetworkId();
263 ni.networkId = netid;
264 return ni;
265 }
266
267 private static int ipSupportOf(DefaultNetworkEvent in) {
268 if (in.prevIPv4 && in.prevIPv6) {
269 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
270 }
271 if (in.prevIPv6) {
272 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
273 }
274 if (in.prevIPv4) {
275 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
276 }
277 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
278 }
279
280 private static boolean isBitSet(int flags, int bit) {
281 return (flags & (1 << bit)) != 0;
282 }
283}