1 /*
2  * Copyright (C) 2015 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.netlink;
18 
19 import android.system.Os;
20 import android.system.OsConstants;
21 
22 import java.nio.ByteBuffer;
23 
24 
25 /**
26  * struct nda_cacheinfo
27  *
28  * see: <linux_src>/include/uapi/linux/neighbour.h
29  *
30  * @hide
31  */
32 public class StructNdaCacheInfo {
33     // Already aligned.
34     public static final int STRUCT_SIZE = 16;
35 
hasAvailableSpace(ByteBuffer byteBuffer)36     private static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
37         return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
38     }
39 
parse(ByteBuffer byteBuffer)40     public static StructNdaCacheInfo parse(ByteBuffer byteBuffer) {
41         if (!hasAvailableSpace(byteBuffer)) { return null; }
42 
43         // The ByteOrder must have already been set by the caller.  In most
44         // cases ByteOrder.nativeOrder() is correct, with the possible
45         // exception of usage within unittests.
46         final StructNdaCacheInfo struct = new StructNdaCacheInfo();
47         struct.ndm_used = byteBuffer.getInt();
48         struct.ndm_confirmed = byteBuffer.getInt();
49         struct.ndm_updated = byteBuffer.getInt();
50         struct.ndm_refcnt = byteBuffer.getInt();
51         return struct;
52     }
53 
54     // TODO: investigate whether this can change during device runtime and
55     // decide what (if anything) should be done about that.
56     private static final long CLOCK_TICKS_PER_SECOND = Os.sysconf(OsConstants._SC_CLK_TCK);
57 
ticksToMilliSeconds(int intClockTicks)58     private static long ticksToMilliSeconds(int intClockTicks) {
59         final long longClockTicks = (long) intClockTicks & 0xffffffff;
60         return (longClockTicks * 1000) / CLOCK_TICKS_PER_SECOND;
61     }
62 
63     /**
64      * Explanatory notes, for reference.
65      *
66      * Before being returned to user space, the neighbor entry times are
67      * converted to clock_t's like so:
68      *
69      *     ndm_used      = jiffies_to_clock_t(now - neigh->used);
70      *     ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
71      *     ndm_updated   = jiffies_to_clock_t(now - neigh->updated);
72      *
73      * meaning that these values are expressed as "clock ticks ago".  To
74      * convert these clock ticks to seconds divide by sysconf(_SC_CLK_TCK).
75      * When _SC_CLK_TCK is 100, for example, the ndm_* times are expressed
76      * in centiseconds.
77      *
78      * These values are unsigned, but fortunately being expressed as "some
79      * clock ticks ago", these values are typically very small (and
80      * 2^31 centiseconds = 248 days).
81      *
82      * By observation, it appears that:
83      *     ndm_used: the last time ARP/ND took place for this neighbor
84      *     ndm_confirmed: the last time ARP/ND succeeded for this neighbor OR
85      *                    higher layer confirmation (TCP or MSG_CONFIRM)
86      *                    was received
87      *     ndm_updated: the time when the current NUD state was entered
88      */
89     public int ndm_used;
90     public int ndm_confirmed;
91     public int ndm_updated;
92     public int ndm_refcnt;
93 
StructNdaCacheInfo()94     public StructNdaCacheInfo() {}
95 
lastUsed()96     public long lastUsed() {
97         return ticksToMilliSeconds(ndm_used);
98     }
99 
lastConfirmed()100     public long lastConfirmed() {
101         return ticksToMilliSeconds(ndm_confirmed);
102     }
103 
lastUpdated()104     public long lastUpdated() {
105         return ticksToMilliSeconds(ndm_updated);
106     }
107 
108     @Override
toString()109     public String toString() {
110         return "NdaCacheInfo{ "
111                 + "ndm_used{" + lastUsed() + "}, "
112                 + "ndm_confirmed{" + lastConfirmed() + "}, "
113                 + "ndm_updated{" + lastUpdated() + "}, "
114                 + "ndm_refcnt{" + ndm_refcnt + "} "
115                 + "}";
116     }
117 }
118