blob: f408ff050ba184407d230edd2773f523bc094215 [file] [log] [blame]
Hugo Benichi7b314e12018-01-15 21:54:00 +09001/*
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
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090020#include <chrono>
21#include <condition_variable>
22#include <mutex>
Hugo Benichi4596d2f2018-01-19 13:36:29 +090023#include <unordered_map>
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090024
25#include <android-base/thread_annotations.h>
Hugo Benichi7b314e12018-01-15 21:54:00 +090026#include "utils/String16.h"
27
Hugo Benichi4596d2f2018-01-19 13:36:29 +090028#include "Fwmark.h"
29
Hugo Benichi7b314e12018-01-15 21:54:00 +090030namespace android {
31namespace net {
32
Hugo Benichi4596d2f2018-01-19 13:36:29 +090033using std::chrono::milliseconds;
34
Hugo Benichi7b314e12018-01-15 21:54:00 +090035class DumpWriter;
36
37class TcpSocketMonitor {
38 public:
Hugo Benichi4596d2f2018-01-19 13:36:29 +090039 using time_point = std::chrono::time_point<std::chrono::steady_clock>;
40
Hugo Benichi7b314e12018-01-15 21:54:00 +090041 static const String16 DUMP_KEYWORD;
Hugo Benichi4596d2f2018-01-19 13:36:29 +090042 static const milliseconds kDefaultPollingInterval;
43
44 // A subset of fields found in struct inet_diag_msg and struct tcp_info.
45 struct TcpStats {
46 // Number of packets sent. Tracks struct tcp_sock data_segs_out.
47 // Not available on 3.18 kernels.
48 uint32_t sent;
49 // Number of packets lost. Tracks struct tcp_sock lost_out.
50 uint32_t lost;
51 // Smoothed round trip time. Tracks struct tcp_sock srtt_us.
52 uint32_t rttUs;
53 // Milliseconds difference between the last packet sent and last ack received.
54 int32_t sentAckDiffMs;
55 // Number of socket stats aggregated in this TcpStats entry.
56 int32_t nSockets;
57 };
58
59 // Socket metadata used for computing TcpStats diff across sock_diag dumps.
60 struct SocketEntry {
61 // Number of packets sent. Tracks struct tcp_sock data_segs_out.
62 // Not available on 3.18 kernels.
63 uint32_t sent;
64 // Number of packets lost. Tracks struct tcp_sock lost_out.
65 uint32_t lost;
66 // Last update timestamp for that socket.
67 time_point lastUpdate;
68 // Socket mark.
69 Fwmark mark;
70 // The uid owning the socket.
71 uint32_t uid;
72 };
Hugo Benichi7b314e12018-01-15 21:54:00 +090073
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090074 TcpSocketMonitor();
75 ~TcpSocketMonitor();
Hugo Benichi7b314e12018-01-15 21:54:00 +090076
77 void dump(DumpWriter& dw);
Hugo Benichi4596d2f2018-01-19 13:36:29 +090078 void setPollingInterval(milliseconds duration);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090079 void resumePolling();
80 void suspendPolling();
Hugo Benichi7b314e12018-01-15 21:54:00 +090081
82 private:
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090083 void poll();
84 void waitForNextPoll();
85 bool isRunning();
Hugo Benichi4596d2f2018-01-19 13:36:29 +090086 void updateSocketStats(time_point now, Fwmark mark, const struct inet_diag_msg *sockinfo,
87 const struct tcp_info *tcpinfo, uint32_t tcpinfoLen) REQUIRES(mLock);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090088
89 // Lock guarding all reads and writes to member variables.
90 std::mutex mLock;
91 // Used by the polling thread for sleeping between poll operations.
92 std::condition_variable mCv;
Hugo Benichi4596d2f2018-01-19 13:36:29 +090093 // The thread that polls sock_diag continously.
94 std::thread mPollingThread;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090095 // The duration of a sleep between polls. Can be updated by the instance owner for dynamically
96 // adjusting the polling rate.
Hugo Benichi4596d2f2018-01-19 13:36:29 +090097 milliseconds mNextSleepDurationMs GUARDED_BY(mLock);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090098 // The time of the last successful poll operation.
Hugo Benichi4596d2f2018-01-19 13:36:29 +090099 time_point mLastPoll GUARDED_BY(mLock);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900100 // True if the polling thread should sleep until notified.
101 bool mIsSuspended GUARDED_BY(mLock);
102 // True while the polling thread should poll.
103 bool mIsRunning GUARDED_BY(mLock);
Hugo Benichi4596d2f2018-01-19 13:36:29 +0900104 // Map of SocketEntry structs keyed by socket cookie. This map tracks per-socket data needed for
105 // computing diffs between sock_diag dumps. Entries for closed sockets are continously cleaned
106 // after every dump operation based on timestamps of last updates.
107 std::unordered_map<uint64_t, SocketEntry> mSocketEntries GUARDED_BY(mLock);
108 // Map of TcpStats entries aggregated per network and keyed per network id.
109 // This map tracks per-network data for a single sock_diag dump and is cleared before every dump
110 // operation.
111 std::unordered_map<uint32_t, TcpStats> mNetworkStats GUARDED_BY(mLock);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900112};
113
114} // namespace net
115} // namespace android
116
117#endif /* TCP_SOCKET_MONITOR_H */