blob: d5fa26c49251c961ca8ae0cf56a893f7b7ca2b63 [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) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100154 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch = new IpConnectivityLogClass.DNSLookupBatch();
155 dnsLookupBatch.networkId = netIdOf(in.netId);
156 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
157 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
158 dnsLookupBatch.latenciesMs = in.latenciesMs;
159 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900160 }
161
162 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100163 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent = new IpConnectivityLogClass.IpProvisioningEvent();
164 ipProvisioningEvent.ifName = in.ifName;
165 ipProvisioningEvent.eventType = in.eventType;
166 ipProvisioningEvent.latencyMs = (int) in.durationMs;
167 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900168 }
169
170 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100171 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent = new IpConnectivityLogClass.IpReachabilityEvent();
172 ipReachabilityEvent.ifName = in.ifName;
173 ipReachabilityEvent.eventType = in.eventType;
174 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900175 }
176
177 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100178 IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent = new IpConnectivityLogClass.DefaultNetworkEvent();
179 defaultNetworkEvent.networkId = netIdOf(in.netId);
180 defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
181 defaultNetworkEvent.transportTypes = in.transportTypes;
182 defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
183 out.setDefaultNetworkEvent(defaultNetworkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900184 }
185
186 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100187 IpConnectivityLogClass.NetworkEvent networkEvent = new IpConnectivityLogClass.NetworkEvent();
188 networkEvent.networkId = netIdOf(in.netId);
189 networkEvent.eventType = in.eventType;
190 networkEvent.latencyMs = (int) in.durationMs;
191 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900192 }
193
194 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100195 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent = new IpConnectivityLogClass.ValidationProbeEvent();
196 validationProbeEvent.networkId = netIdOf(in.netId);
197 validationProbeEvent.latencyMs = (int) in.durationMs;
198 validationProbeEvent.probeType = in.probeType;
199 validationProbeEvent.probeResult = in.returnCode;
200 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900201 }
202
203 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100204 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent = new IpConnectivityLogClass.ApfProgramEvent();
205 apfProgramEvent.lifetime = in.lifetime;
206 apfProgramEvent.filteredRas = in.filteredRas;
207 apfProgramEvent.currentRas = in.currentRas;
208 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900209 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100210 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900211 }
212 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100213 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900214 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100215 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900216 }
217
218 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100219 IpConnectivityLogClass.ApfStatistics apfStatistics = new IpConnectivityLogClass.ApfStatistics();
220 apfStatistics.durationMs = in.durationMs;
221 apfStatistics.receivedRas = in.receivedRas;
222 apfStatistics.matchingRas = in.matchingRas;
223 apfStatistics.droppedRas = in.droppedRas;
224 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
225 apfStatistics.parseErrors = in.parseErrors;
226 apfStatistics.programUpdates = in.programUpdates;
227 apfStatistics.maxProgramSize = in.maxProgramSize;
228 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900229 }
230
231 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100232 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
233 raEvent.routerLifetime = in.routerLifetime;
234 raEvent.prefixValidLifetime = in.prefixValidLifetime;
235 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
236 raEvent.routeInfoLifetime = in.routeInfoLifetime;
237 raEvent.rdnssLifetime = in.rdnssLifetime;
238 raEvent.dnsslLifetime = in.dnsslLifetime;
239 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900240 }
241
242 private static int[] bytesToInts(byte[] in) {
243 final int[] out = new int[in.length];
244 for (int i = 0; i < in.length; i++) {
245 out[i] = in[i] & 0xFF;
246 }
247 return out;
248 }
249
250 private static NetworkId netIdOf(int netid) {
251 final NetworkId ni = new NetworkId();
252 ni.networkId = netid;
253 return ni;
254 }
255
256 private static int ipSupportOf(DefaultNetworkEvent in) {
257 if (in.prevIPv4 && in.prevIPv6) {
258 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
259 }
260 if (in.prevIPv6) {
261 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
262 }
263 if (in.prevIPv4) {
264 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
265 }
266 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
267 }
268
269 private static boolean isBitSet(int flags, int bit) {
270 return (flags & (1 << bit)) != 0;
271 }
272}