Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +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 android.net.metrics; |
| 18 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 20 | import android.annotation.SystemApi; |
| 21 | import android.annotation.TestApi; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 25 | import android.text.TextUtils; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 26 | import android.util.SparseArray; |
| 27 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 28 | import com.android.internal.util.MessageUtils; |
| 29 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.BitSet; |
| 34 | import java.util.List; |
| 35 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 36 | /** |
| 37 | * An event logged when there is a change or event that requires updating the |
| 38 | * the APF program in place with a new APF program. |
| 39 | * {@hide} |
| 40 | */ |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 41 | @TestApi |
| 42 | @SystemApi |
| 43 | public final class ApfProgramEvent implements IpConnectivityLog.Event { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 44 | |
| 45 | // Bitflag constants describing what an Apf program filters. |
| 46 | // Bits are indexeds from LSB to MSB, starting at index 0. |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 47 | /** @hide */ |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 48 | public static final int FLAG_MULTICAST_FILTER_ON = 0; |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 49 | /** @hide */ |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 50 | public static final int FLAG_HAS_IPV4_ADDRESS = 1; |
| 51 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 52 | /** {@hide} */ |
| 53 | @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS}) |
| 54 | @Retention(RetentionPolicy.SOURCE) |
| 55 | public @interface Flags {} |
| 56 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 57 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 58 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 59 | public final long lifetime; // Maximum computed lifetime of the program in seconds |
| 60 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 61 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 62 | public final long actualLifetime; // Effective program lifetime in seconds |
| 63 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 65 | public final int filteredRas; // Number of RAs filtered by the APF program |
| 66 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 67 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 68 | public final int currentRas; // Total number of current RAs at generation time |
| 69 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 70 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 71 | public final int programLength; // Length of the APF program in bytes |
| 72 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 74 | public final int flags; // Bitfield compound of FLAG_* constants |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 75 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 76 | private ApfProgramEvent(long lifetime, long actualLifetime, int filteredRas, int currentRas, |
| 77 | int programLength, int flags) { |
| 78 | this.lifetime = lifetime; |
| 79 | this.actualLifetime = actualLifetime; |
| 80 | this.filteredRas = filteredRas; |
| 81 | this.currentRas = currentRas; |
| 82 | this.programLength = programLength; |
| 83 | this.flags = flags; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private ApfProgramEvent(Parcel in) { |
| 87 | this.lifetime = in.readLong(); |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 88 | this.actualLifetime = in.readLong(); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 89 | this.filteredRas = in.readInt(); |
| 90 | this.currentRas = in.readInt(); |
| 91 | this.programLength = in.readInt(); |
| 92 | this.flags = in.readInt(); |
| 93 | } |
| 94 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 95 | /** |
| 96 | * Utility to create an instance of {@link ApfProgramEvent}. |
| 97 | */ |
| 98 | public static class Builder { |
| 99 | private long mLifetime; |
| 100 | private long mActualLifetime; |
| 101 | private int mFilteredRas; |
| 102 | private int mCurrentRas; |
| 103 | private int mProgramLength; |
| 104 | private int mFlags; |
| 105 | |
| 106 | /** |
| 107 | * Set the maximum computed lifetime of the program in seconds. |
| 108 | */ |
| 109 | public Builder setLifetime(long lifetime) { |
| 110 | mLifetime = lifetime; |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set the effective program lifetime in seconds. |
| 116 | */ |
| 117 | public Builder setActualLifetime(long lifetime) { |
| 118 | mActualLifetime = lifetime; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set the number of RAs filtered by the APF program. |
| 124 | */ |
| 125 | public Builder setFilteredRas(int filteredRas) { |
| 126 | mFilteredRas = filteredRas; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set the total number of current RAs at generation time. |
| 132 | */ |
| 133 | public Builder setCurrentRas(int currentRas) { |
| 134 | mCurrentRas = currentRas; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set the length of the APF program in bytes. |
| 140 | */ |
| 141 | public Builder setProgramLength(int programLength) { |
| 142 | mProgramLength = programLength; |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Set the flags describing what an Apf program filters. |
| 148 | */ |
| 149 | public Builder setFlags(boolean hasIPv4, boolean multicastFilterOn) { |
| 150 | mFlags = flagsFor(hasIPv4, multicastFilterOn); |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Build a new {@link ApfProgramEvent}. |
| 156 | */ |
| 157 | public ApfProgramEvent build() { |
| 158 | return new ApfProgramEvent(mLifetime, mActualLifetime, mFilteredRas, mCurrentRas, |
| 159 | mProgramLength, mFlags); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** @hide */ |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 164 | @Override |
| 165 | public void writeToParcel(Parcel out, int flags) { |
| 166 | out.writeLong(lifetime); |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 167 | out.writeLong(actualLifetime); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 168 | out.writeInt(filteredRas); |
| 169 | out.writeInt(currentRas); |
| 170 | out.writeInt(programLength); |
| 171 | out.writeInt(flags); |
| 172 | } |
| 173 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 174 | /** @hide */ |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 175 | @Override |
| 176 | public int describeContents() { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public String toString() { |
| 182 | String lifetimeString = (lifetime < Long.MAX_VALUE) ? lifetime + "s" : "forever"; |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 183 | return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas, |
| 184 | programLength, actualLifetime, lifetimeString, namesOf(flags)); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 185 | } |
| 186 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 187 | /** @hide */ |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 188 | public static final Parcelable.Creator<ApfProgramEvent> CREATOR |
| 189 | = new Parcelable.Creator<ApfProgramEvent>() { |
| 190 | public ApfProgramEvent createFromParcel(Parcel in) { |
| 191 | return new ApfProgramEvent(in); |
| 192 | } |
| 193 | |
| 194 | public ApfProgramEvent[] newArray(int size) { |
| 195 | return new ApfProgramEvent[size]; |
| 196 | } |
| 197 | }; |
| 198 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame^] | 199 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 200 | @UnsupportedAppUsage |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 201 | public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 202 | int bitfield = 0; |
| 203 | if (hasIPv4) { |
| 204 | bitfield |= (1 << FLAG_HAS_IPV4_ADDRESS); |
| 205 | } |
| 206 | if (multicastFilterOn) { |
| 207 | bitfield |= (1 << FLAG_MULTICAST_FILTER_ON); |
| 208 | } |
| 209 | return bitfield; |
| 210 | } |
| 211 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 212 | private static String namesOf(@Flags int bitfield) { |
| 213 | List<String> names = new ArrayList<>(Integer.bitCount(bitfield)); |
| 214 | BitSet set = BitSet.valueOf(new long[]{bitfield & Integer.MAX_VALUE}); |
| 215 | // Only iterate over flag bits which are set. |
| 216 | for (int bit = set.nextSetBit(0); bit >= 0; bit = set.nextSetBit(bit+1)) { |
| 217 | names.add(Decoder.constants.get(bit)); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 218 | } |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame] | 219 | return TextUtils.join("|", names); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | final static class Decoder { |
| 223 | static final SparseArray<String> constants = |
| 224 | MessageUtils.findMessageNames( |
| 225 | new Class[]{ApfProgramEvent.class}, new String[]{"FLAG_"}); |
| 226 | } |
| 227 | } |