blob: 8601005c8af47b818303aa537980b9973b9208b4 [file] [log] [blame]
Hugo Benichi4fc3ee52016-06-02 11:20:27 +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 android.net.metrics;
18
Hugo Benichicf6b12f2016-07-04 11:28:05 +090019import android.annotation.IntDef;
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090020import android.annotation.SystemApi;
21import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090023import android.os.Parcel;
24import android.os.Parcelable;
Hugo Benichicf6b12f2016-07-04 11:28:05 +090025import android.text.TextUtils;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090026import android.util.SparseArray;
27
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090028import com.android.internal.util.MessageUtils;
29
Hugo Benichicf6b12f2016-07-04 11:28:05 +090030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32import java.util.ArrayList;
33import java.util.BitSet;
34import java.util.List;
35
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090036/**
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 VAN7b84fb32019-01-19 21:13:24 +090041@TestApi
42@SystemApi
43public final class ApfProgramEvent implements IpConnectivityLog.Event {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090044
45 // Bitflag constants describing what an Apf program filters.
46 // Bits are indexeds from LSB to MSB, starting at index 0.
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090047 /** @hide */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090048 public static final int FLAG_MULTICAST_FILTER_ON = 0;
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090049 /** @hide */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090050 public static final int FLAG_HAS_IPV4_ADDRESS = 1;
51
Hugo Benichicf6b12f2016-07-04 11:28:05 +090052 /** {@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 VAN7b84fb32019-01-19 21:13:24 +090057 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010058 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090059 public final long lifetime; // Maximum computed lifetime of the program in seconds
60 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010061 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090062 public final long actualLifetime; // Effective program lifetime in seconds
63 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010064 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090065 public final int filteredRas; // Number of RAs filtered by the APF program
66 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010067 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090068 public final int currentRas; // Total number of current RAs at generation time
69 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010070 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090071 public final int programLength; // Length of the APF program in bytes
72 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010073 @UnsupportedAppUsage
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090074 public final int flags; // Bitfield compound of FLAG_* constants
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090075
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090076 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 Benichi4fc3ee52016-06-02 11:20:27 +090084 }
85
86 private ApfProgramEvent(Parcel in) {
87 this.lifetime = in.readLong();
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090088 this.actualLifetime = in.readLong();
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090089 this.filteredRas = in.readInt();
90 this.currentRas = in.readInt();
91 this.programLength = in.readInt();
92 this.flags = in.readInt();
93 }
94
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090095 /**
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 Benichi4fc3ee52016-06-02 11:20:27 +0900164 @Override
165 public void writeToParcel(Parcel out, int flags) {
166 out.writeLong(lifetime);
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900167 out.writeLong(actualLifetime);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900168 out.writeInt(filteredRas);
169 out.writeInt(currentRas);
170 out.writeInt(programLength);
171 out.writeInt(flags);
172 }
173
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900174 /** @hide */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900175 @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 Benichi22d9b2d2017-02-22 13:02:27 +0900183 return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas,
184 programLength, actualLifetime, lifetimeString, namesOf(flags));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900185 }
186
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900187 /** @hide */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900188 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 VAN7b84fb32019-01-19 21:13:24 +0900199 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100200 @UnsupportedAppUsage
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900201 public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900202 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 Benichicf6b12f2016-07-04 11:28:05 +0900212 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 Benichi4fc3ee52016-06-02 11:20:27 +0900218 }
Hugo Benichi0d1c65b2016-06-22 17:01:43 +0900219 return TextUtils.join("|", names);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900220 }
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}