blob: f1d01e06911a217581abf169d24932936e97ce66 [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;
32import com.android.server.connectivity.metrics.IpConnectivityLogClass;
33import java.io.IOException;
34import java.util.ArrayList;
35import java.util.List;
36
37import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
38import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
39import static com.android.server.connectivity.metrics.IpConnectivityLogClass.NetworkId;
40
41/** {@hide} */
42final public class IpConnectivityEventBuilder {
43 private IpConnectivityEventBuilder() {
44 }
45
46 public static byte[] serialize(int dropped, List<ConnectivityMetricsEvent> events)
47 throws IOException {
48 final IpConnectivityLog log = new IpConnectivityLog();
49 log.events = toProto(events);
50 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
58 public static IpConnectivityEvent[] toProto(List<ConnectivityMetricsEvent> eventsIn) {
59 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 }
67 return eventsOut.toArray(new IpConnectivityEvent[eventsOut.size()]);
68 }
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) {
139 out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
140 out.dhcpEvent.ifName = in.ifName;
141 out.dhcpEvent.errorCode = in.errorCode;
142 }
143
144 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
145 out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
146 out.dhcpEvent.ifName = in.ifName;
147 out.dhcpEvent.stateTransition = in.msg;
148 out.dhcpEvent.durationMs = in.durationMs;
149 }
150
151 private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) {
152 out.dnsLookupBatch = new IpConnectivityLogClass.DNSLookupBatch();
153 out.dnsLookupBatch.networkId = netIdOf(in.netId);
154 out.dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
155 out.dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
156 out.dnsLookupBatch.latenciesMs = in.latenciesMs;
157 }
158
159 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
160 out.ipProvisioningEvent = new IpConnectivityLogClass.IpProvisioningEvent();
161 out.ipProvisioningEvent.ifName = in.ifName;
162 out.ipProvisioningEvent.eventType = in.eventType;
163 out.ipProvisioningEvent.latencyMs = (int) in.durationMs;
164 }
165
166 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
167 out.ipReachabilityEvent = new IpConnectivityLogClass.IpReachabilityEvent();
168 out.ipReachabilityEvent.ifName = in.ifName;
169 out.ipReachabilityEvent.eventType = in.eventType;
170 }
171
172 private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
173 out.defaultNetworkEvent = new IpConnectivityLogClass.DefaultNetworkEvent();
174 out.defaultNetworkEvent.networkId = netIdOf(in.netId);
175 out.defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
176 out.defaultNetworkEvent.transportTypes = in.transportTypes;
177 out.defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
178 }
179
180 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
181 out.networkEvent = new IpConnectivityLogClass.NetworkEvent();
182 out.networkEvent.networkId = netIdOf(in.netId);
183 out.networkEvent.eventType = in.eventType;
184 out.networkEvent.latencyMs = (int) in.durationMs;
185 }
186
187 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
188 out.validationProbeEvent = new IpConnectivityLogClass.ValidationProbeEvent();
189 out.validationProbeEvent.networkId = netIdOf(in.netId);
190 out.validationProbeEvent.latencyMs = (int) in.durationMs;
191 out.validationProbeEvent.probeType = in.probeType;
192 out.validationProbeEvent.probeResult = in.returnCode;
193 }
194
195 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
196 out.apfProgramEvent = new IpConnectivityLogClass.ApfProgramEvent();
197 out.apfProgramEvent.lifetime = in.lifetime;
198 out.apfProgramEvent.filteredRas = in.filteredRas;
199 out.apfProgramEvent.currentRas = in.currentRas;
200 out.apfProgramEvent.programLength = in.programLength;
201 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
202 out.apfProgramEvent.dropMulticast = true;
203 }
204 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
205 out.apfProgramEvent.hasIpv4Addr = true;
206 }
207 }
208
209 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
210 out.apfStatistics = new IpConnectivityLogClass.ApfStatistics();
211 out.apfStatistics.durationMs = in.durationMs;
212 out.apfStatistics.receivedRas = in.receivedRas;
213 out.apfStatistics.matchingRas = in.matchingRas;
214 out.apfStatistics.droppedRas = in.droppedRas;
215 out.apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
216 out.apfStatistics.parseErrors = in.parseErrors;
217 out.apfStatistics.programUpdates = in.programUpdates;
218 out.apfStatistics.maxProgramSize = in.maxProgramSize;
219 }
220
221 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
222 out.raEvent = new IpConnectivityLogClass.RaEvent();
223 out.raEvent.routerLifetime = in.routerLifetime;
224 out.raEvent.prefixValidLifetime = in.prefixValidLifetime;
225 out.raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
226 out.raEvent.routeInfoLifetime = in.routeInfoLifetime;
227 out.raEvent.rdnssLifetime = in.rdnssLifetime;
228 out.raEvent.dnsslLifetime = in.dnsslLifetime;
229 }
230
231 private static int[] bytesToInts(byte[] in) {
232 final int[] out = new int[in.length];
233 for (int i = 0; i < in.length; i++) {
234 out[i] = in[i] & 0xFF;
235 }
236 return out;
237 }
238
239 private static NetworkId netIdOf(int netid) {
240 final NetworkId ni = new NetworkId();
241 ni.networkId = netid;
242 return ni;
243 }
244
245 private static int ipSupportOf(DefaultNetworkEvent in) {
246 if (in.prevIPv4 && in.prevIPv6) {
247 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
248 }
249 if (in.prevIPv6) {
250 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
251 }
252 if (in.prevIPv4) {
253 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
254 }
255 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
256 }
257
258 private static boolean isBitSet(int flags, int bit) {
259 return (flags & (1 << bit)) != 0;
260 }
261}