• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #ifndef TCP_SOCKET_MONITOR_H
18 #define TCP_SOCKET_MONITOR_H
19 
20 #include <chrono>
21 #include <condition_variable>
22 #include <mutex>
23 #include <thread>
24 #include <unordered_map>
25 
26 #include <android-base/thread_annotations.h>
27 #include "utils/String16.h"
28 
29 #include "Fwmark.h"
30 
31 struct inet_diag_msg;
32 struct tcp_info;
33 
34 namespace android {
35 namespace net {
36 
37 using std::chrono::milliseconds;
38 
39 class DumpWriter;
40 
41 class TcpSocketMonitor {
42   public:
43     using time_point = std::chrono::time_point<std::chrono::steady_clock>;
44 
45     static const String16 DUMP_KEYWORD;
46     static const milliseconds kDefaultPollingInterval;
47 
48     // A subset of fields found in struct inet_diag_msg and struct tcp_info.
49     struct TcpStats {
50         // Number of packets sent. Tracks struct tcp_sock data_segs_out.
51         // Not available on 3.18 kernels.
52         uint32_t sent;
53         // Number of packets lost. Tracks struct tcp_sock lost_out.
54         uint32_t lost;
55         // Smoothed round trip time. Tracks struct tcp_sock srtt_us.
56         uint32_t rttUs;
57         // Milliseconds difference between the last packet sent and last ack received.
58         int32_t sentAckDiffMs;
59         // Number of socket stats aggregated in this TcpStats entry.
60         int32_t nSockets;
61     };
62 
63     // Socket metadata used for computing TcpStats diff across sock_diag dumps.
64     struct SocketEntry {
65         // Number of packets sent. Tracks struct tcp_sock data_segs_out.
66         // Not available on 3.18 kernels.
67         uint32_t sent;
68         // Number of packets lost. Tracks struct tcp_sock lost_out.
69         uint32_t lost;
70         // Last update timestamp for that socket.
71         time_point lastUpdate;
72         // Socket mark.
73         Fwmark mark;
74         // The uid owning the socket.
75         uint32_t uid;
76     };
77 
78     TcpSocketMonitor();
79     ~TcpSocketMonitor();
80 
81     void dump(DumpWriter& dw);
82     void setPollingInterval(milliseconds duration);
83     void resumePolling();
84     void suspendPolling();
85 
86   private:
87     void poll();
88     void waitForNextPoll();
89     bool isRunning();
90     void updateSocketStats(time_point now, Fwmark mark, const struct inet_diag_msg *sockinfo,
91                            const struct tcp_info *tcpinfo, uint32_t tcpinfoLen) REQUIRES(mLock);
92 
93     // Lock guarding all reads and writes to member variables.
94     std::mutex mLock;
95     // Used by the polling thread for sleeping between poll operations.
96     std::condition_variable mCv;
97     // The thread that polls sock_diag continously.
98     std::thread mPollingThread;
99     // The duration of a sleep between polls. Can be updated by the instance owner for dynamically
100     // adjusting the polling rate.
101     milliseconds mNextSleepDurationMs GUARDED_BY(mLock);
102     // The time of the last successful poll operation.
103     time_point mLastPoll GUARDED_BY(mLock);
104     // True if the polling thread should sleep until notified.
105     bool mIsSuspended GUARDED_BY(mLock);
106     // True while the polling thread should poll.
107     bool mIsRunning GUARDED_BY(mLock);
108     // Map of SocketEntry structs keyed by socket cookie. This map tracks per-socket data needed for
109     // computing diffs between sock_diag dumps. Entries for closed sockets are continously cleaned
110     // after every dump operation based on timestamps of last updates.
111     std::unordered_map<uint64_t, SocketEntry> mSocketEntries GUARDED_BY(mLock);
112     // Map of TcpStats entries aggregated per network and keyed per network id.
113     // This map tracks per-network data for a single sock_diag dump and is cleared before every dump
114     // operation.
115     std::unordered_map<uint32_t, TcpStats> mNetworkStats GUARDED_BY(mLock);
116 };
117 
118 }  // namespace net
119 }  // namespace android
120 
121 #endif /* TCP_SOCKET_MONITOR_H */
122