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; |
| 32 | import com.android.server.connectivity.metrics.IpConnectivityLogClass; |
| 33 | import java.io.IOException; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
| 37 | import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent; |
| 38 | import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog; |
| 39 | import static com.android.server.connectivity.metrics.IpConnectivityLogClass.NetworkId; |
| 40 | |
| 41 | /** {@hide} */ |
| 42 | final 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; |
| 51 | return IpConnectivityLog.toByteArray(log); |
| 52 | } |
| 53 | |
| 54 | public static IpConnectivityEvent[] toProto(List<ConnectivityMetricsEvent> eventsIn) { |
| 55 | final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size()); |
| 56 | for (ConnectivityMetricsEvent in : eventsIn) { |
| 57 | final IpConnectivityEvent out = toProto(in); |
| 58 | if (out == null) { |
| 59 | continue; |
| 60 | } |
| 61 | eventsOut.add(out); |
| 62 | } |
| 63 | return eventsOut.toArray(new IpConnectivityEvent[eventsOut.size()]); |
| 64 | } |
| 65 | |
| 66 | public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) { |
| 67 | final IpConnectivityEvent out = new IpConnectivityEvent(); |
| 68 | if (!setEvent(out, ev.data)) { |
| 69 | return null; |
| 70 | } |
| 71 | out.timeMs = ev.timestamp; |
| 72 | return out; |
| 73 | } |
| 74 | |
| 75 | private static boolean setEvent(IpConnectivityEvent out, Parcelable in) { |
| 76 | if (in instanceof DhcpErrorEvent) { |
| 77 | setDhcpErrorEvent(out, (DhcpErrorEvent) in); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | if (in instanceof DhcpClientEvent) { |
| 82 | setDhcpClientEvent(out, (DhcpClientEvent) in); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | if (in instanceof DnsEvent) { |
| 87 | setDnsEvent(out, (DnsEvent) in); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (in instanceof IpManagerEvent) { |
| 92 | setIpManagerEvent(out, (IpManagerEvent) in); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | if (in instanceof IpReachabilityEvent) { |
| 97 | setIpReachabilityEvent(out, (IpReachabilityEvent) in); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | if (in instanceof DefaultNetworkEvent) { |
| 102 | setDefaultNetworkEvent(out, (DefaultNetworkEvent) in); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | if (in instanceof NetworkEvent) { |
| 107 | setNetworkEvent(out, (NetworkEvent) in); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if (in instanceof ValidationProbeEvent) { |
| 112 | setValidationProbeEvent(out, (ValidationProbeEvent) in); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | if (in instanceof ApfProgramEvent) { |
| 117 | setApfProgramEvent(out, (ApfProgramEvent) in); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | if (in instanceof ApfStats) { |
| 122 | setApfStats(out, (ApfStats) in); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | if (in instanceof RaEvent) { |
| 127 | setRaEvent(out, (RaEvent) in); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) { |
| 135 | out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent(); |
| 136 | out.dhcpEvent.ifName = in.ifName; |
| 137 | out.dhcpEvent.errorCode = in.errorCode; |
| 138 | } |
| 139 | |
| 140 | private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) { |
| 141 | out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent(); |
| 142 | out.dhcpEvent.ifName = in.ifName; |
| 143 | out.dhcpEvent.stateTransition = in.msg; |
| 144 | out.dhcpEvent.durationMs = in.durationMs; |
| 145 | } |
| 146 | |
| 147 | private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) { |
| 148 | out.dnsLookupBatch = new IpConnectivityLogClass.DNSLookupBatch(); |
| 149 | out.dnsLookupBatch.networkId = netIdOf(in.netId); |
| 150 | out.dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes); |
| 151 | out.dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes); |
| 152 | out.dnsLookupBatch.latenciesMs = in.latenciesMs; |
| 153 | } |
| 154 | |
| 155 | private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) { |
| 156 | out.ipProvisioningEvent = new IpConnectivityLogClass.IpProvisioningEvent(); |
| 157 | out.ipProvisioningEvent.ifName = in.ifName; |
| 158 | out.ipProvisioningEvent.eventType = in.eventType; |
| 159 | out.ipProvisioningEvent.latencyMs = (int) in.durationMs; |
| 160 | } |
| 161 | |
| 162 | private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) { |
| 163 | out.ipReachabilityEvent = new IpConnectivityLogClass.IpReachabilityEvent(); |
| 164 | out.ipReachabilityEvent.ifName = in.ifName; |
| 165 | out.ipReachabilityEvent.eventType = in.eventType; |
| 166 | } |
| 167 | |
| 168 | private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) { |
| 169 | out.defaultNetworkEvent = new IpConnectivityLogClass.DefaultNetworkEvent(); |
| 170 | out.defaultNetworkEvent.networkId = netIdOf(in.netId); |
| 171 | out.defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId); |
| 172 | out.defaultNetworkEvent.transportTypes = in.transportTypes; |
| 173 | out.defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in); |
| 174 | } |
| 175 | |
| 176 | private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) { |
| 177 | out.networkEvent = new IpConnectivityLogClass.NetworkEvent(); |
| 178 | out.networkEvent.networkId = netIdOf(in.netId); |
| 179 | out.networkEvent.eventType = in.eventType; |
| 180 | out.networkEvent.latencyMs = (int) in.durationMs; |
| 181 | } |
| 182 | |
| 183 | private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) { |
| 184 | out.validationProbeEvent = new IpConnectivityLogClass.ValidationProbeEvent(); |
| 185 | out.validationProbeEvent.networkId = netIdOf(in.netId); |
| 186 | out.validationProbeEvent.latencyMs = (int) in.durationMs; |
| 187 | out.validationProbeEvent.probeType = in.probeType; |
| 188 | out.validationProbeEvent.probeResult = in.returnCode; |
| 189 | } |
| 190 | |
| 191 | private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) { |
| 192 | out.apfProgramEvent = new IpConnectivityLogClass.ApfProgramEvent(); |
| 193 | out.apfProgramEvent.lifetime = in.lifetime; |
| 194 | out.apfProgramEvent.filteredRas = in.filteredRas; |
| 195 | out.apfProgramEvent.currentRas = in.currentRas; |
| 196 | out.apfProgramEvent.programLength = in.programLength; |
| 197 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) { |
| 198 | out.apfProgramEvent.dropMulticast = true; |
| 199 | } |
| 200 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) { |
| 201 | out.apfProgramEvent.hasIpv4Addr = true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private static void setApfStats(IpConnectivityEvent out, ApfStats in) { |
| 206 | out.apfStatistics = new IpConnectivityLogClass.ApfStatistics(); |
| 207 | out.apfStatistics.durationMs = in.durationMs; |
| 208 | out.apfStatistics.receivedRas = in.receivedRas; |
| 209 | out.apfStatistics.matchingRas = in.matchingRas; |
| 210 | out.apfStatistics.droppedRas = in.droppedRas; |
| 211 | out.apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas; |
| 212 | out.apfStatistics.parseErrors = in.parseErrors; |
| 213 | out.apfStatistics.programUpdates = in.programUpdates; |
| 214 | out.apfStatistics.maxProgramSize = in.maxProgramSize; |
| 215 | } |
| 216 | |
| 217 | private static void setRaEvent(IpConnectivityEvent out, RaEvent in) { |
| 218 | out.raEvent = new IpConnectivityLogClass.RaEvent(); |
| 219 | out.raEvent.routerLifetime = in.routerLifetime; |
| 220 | out.raEvent.prefixValidLifetime = in.prefixValidLifetime; |
| 221 | out.raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime; |
| 222 | out.raEvent.routeInfoLifetime = in.routeInfoLifetime; |
| 223 | out.raEvent.rdnssLifetime = in.rdnssLifetime; |
| 224 | out.raEvent.dnsslLifetime = in.dnsslLifetime; |
| 225 | } |
| 226 | |
| 227 | private static int[] bytesToInts(byte[] in) { |
| 228 | final int[] out = new int[in.length]; |
| 229 | for (int i = 0; i < in.length; i++) { |
| 230 | out[i] = in[i] & 0xFF; |
| 231 | } |
| 232 | return out; |
| 233 | } |
| 234 | |
| 235 | private static NetworkId netIdOf(int netid) { |
| 236 | final NetworkId ni = new NetworkId(); |
| 237 | ni.networkId = netid; |
| 238 | return ni; |
| 239 | } |
| 240 | |
| 241 | private static int ipSupportOf(DefaultNetworkEvent in) { |
| 242 | if (in.prevIPv4 && in.prevIPv6) { |
| 243 | return IpConnectivityLogClass.DefaultNetworkEvent.DUAL; |
| 244 | } |
| 245 | if (in.prevIPv6) { |
| 246 | return IpConnectivityLogClass.DefaultNetworkEvent.IPV6; |
| 247 | } |
| 248 | if (in.prevIPv4) { |
| 249 | return IpConnectivityLogClass.DefaultNetworkEvent.IPV4; |
| 250 | } |
| 251 | return IpConnectivityLogClass.DefaultNetworkEvent.NONE; |
| 252 | } |
| 253 | |
| 254 | private static boolean isBitSet(int flags, int bit) { |
| 255 | return (flags & (1 << bit)) != 0; |
| 256 | } |
| 257 | } |