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