1 /*
2  * Copyright (C) 2020 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.networkstack.tethering;
18 
19 import com.android.net.module.util.Struct;
20 import com.android.net.module.util.Struct.Field;
21 import com.android.net.module.util.Struct.Type;
22 
23 /** The key of BpfMap which is used for tethering stats. */
24 public class TetherStatsValue extends Struct {
25     // Use the signed long variable to store the uint64 stats from stats BPF map.
26     // U63 is enough for each data element even at 5Gbps for ~468 years.
27     // 2^63 / (5 * 1000 * 1000 * 1000) * 8 / 86400 / 365 = 468.
28     @Field(order = 0, type = Type.U63)
29     public final long rxPackets;
30     @Field(order = 1, type = Type.U63)
31     public final long rxBytes;
32     @Field(order = 2, type = Type.U63)
33     public final long rxErrors;
34     @Field(order = 3, type = Type.U63)
35     public final long txPackets;
36     @Field(order = 4, type = Type.U63)
37     public final long txBytes;
38     @Field(order = 5, type = Type.U63)
39     public final long txErrors;
40 
TetherStatsValue(final long rxPackets, final long rxBytes, final long rxErrors, final long txPackets, final long txBytes, final long txErrors)41     public TetherStatsValue(final long rxPackets, final long rxBytes, final long rxErrors,
42             final long txPackets, final long txBytes, final long txErrors) {
43         this.rxPackets = rxPackets;
44         this.rxBytes = rxBytes;
45         this.rxErrors = rxErrors;
46         this.txPackets = txPackets;
47         this.txBytes = txBytes;
48         this.txErrors = txErrors;
49     }
50 
51     // TODO: remove equals, hashCode and toString once aosp/1536721 is merged.
52     @Override
equals(Object obj)53     public boolean equals(Object obj) {
54         if (this == obj) return true;
55 
56         if (!(obj instanceof TetherStatsValue)) return false;
57 
58         final TetherStatsValue that = (TetherStatsValue) obj;
59 
60         return rxPackets == that.rxPackets
61                 && rxBytes == that.rxBytes
62                 && rxErrors == that.rxErrors
63                 && txPackets == that.txPackets
64                 && txBytes == that.txBytes
65                 && txErrors == that.txErrors;
66     }
67 
68     @Override
hashCode()69     public int hashCode() {
70         return Long.hashCode(rxPackets) ^ Long.hashCode(rxBytes) ^ Long.hashCode(rxErrors)
71                 ^ Long.hashCode(txPackets) ^ Long.hashCode(txBytes) ^ Long.hashCode(txErrors);
72     }
73 
74     @Override
toString()75     public String toString() {
76         return String.format("rxPackets: %s, rxBytes: %s, rxErrors: %s, txPackets: %s, "
77                 + "txBytes: %s, txErrors: %s", rxPackets, rxBytes, rxErrors, txPackets,
78                 txBytes, txErrors);
79     }
80 }
81