Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
| 19 | import android.net.ConnectivityMetricsEvent; |
| 20 | import android.net.metrics.ApfProgramEvent; |
| 21 | import android.net.metrics.ApfStats; |
| 22 | import android.net.metrics.DefaultNetworkEvent; |
| 23 | import android.net.metrics.DhcpClientEvent; |
| 24 | import android.net.metrics.DhcpErrorEvent; |
| 25 | import android.net.metrics.DnsEvent; |
| 26 | import android.net.metrics.IpManagerEvent; |
| 27 | import android.net.metrics.IpReachabilityEvent; |
| 28 | import android.net.metrics.NetworkEvent; |
| 29 | import android.net.metrics.RaEvent; |
| 30 | import android.net.metrics.ValidationProbeEvent; |
| 31 | import android.os.Parcelable; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 32 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 37 | import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
| 38 | import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog; |
| 39 | import static com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 40 | |
| 41 | /** {@hide} */ |
| 42 | final public class IpConnectivityEventBuilder { |
| 43 | private IpConnectivityEventBuilder() { |
| 44 | } |
| 45 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame^] | 46 | public static byte[] serialize(int dropped, List<IpConnectivityEvent> events) |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 47 | throws IOException { |
| 48 | final IpConnectivityLog log = new IpConnectivityLog(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame^] | 49 | log.events = events.toArray(new IpConnectivityEvent[events.size()]); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 50 | log.droppedEvents = dropped; |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 51 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 55 | return IpConnectivityLog.toByteArray(log); |
| 56 | } |
| 57 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame^] | 58 | public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) { |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 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 | } |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame^] | 67 | return eventsOut; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 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) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 139 | IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent(); |
| 140 | dhcpEvent.ifName = in.ifName; |
| 141 | dhcpEvent.setErrorCode(in.errorCode); |
| 142 | out.setDhcpEvent(dhcpEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 146 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 154 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 163 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 171 | IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent = new IpConnectivityLogClass.IpReachabilityEvent(); |
| 172 | ipReachabilityEvent.ifName = in.ifName; |
| 173 | ipReachabilityEvent.eventType = in.eventType; |
| 174 | out.setIpReachabilityEvent(ipReachabilityEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 178 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 187 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 195 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 204 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 209 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 210 | apfProgramEvent.dropMulticast = true; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 211 | } |
| 212 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 213 | apfProgramEvent.hasIpv4Addr = true; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 214 | } |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 215 | out.setApfProgramEvent(apfProgramEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private static void setApfStats(IpConnectivityEvent out, ApfStats in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 219 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private static void setRaEvent(IpConnectivityEvent out, RaEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 232 | 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 Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 240 | } |
| 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 | } |