Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 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 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 20 | #include <chrono> |
| 21 | #include <condition_variable> |
| 22 | #include <mutex> |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 23 | #include <unordered_map> |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 24 | |
| 25 | #include <android-base/thread_annotations.h> |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 26 | #include "utils/String16.h" |
| 27 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 28 | #include "Fwmark.h" |
| 29 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace net { |
| 32 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 33 | using std::chrono::milliseconds; |
| 34 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 35 | class DumpWriter; |
| 36 | |
| 37 | class TcpSocketMonitor { |
| 38 | public: |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 39 | using time_point = std::chrono::time_point<std::chrono::steady_clock>; |
| 40 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 41 | static const String16 DUMP_KEYWORD; |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 42 | 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 Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 73 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 74 | TcpSocketMonitor(); |
| 75 | ~TcpSocketMonitor(); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 76 | |
| 77 | void dump(DumpWriter& dw); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 78 | void setPollingInterval(milliseconds duration); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 79 | void resumePolling(); |
| 80 | void suspendPolling(); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 81 | |
| 82 | private: |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 83 | void poll(); |
| 84 | void waitForNextPoll(); |
| 85 | bool isRunning(); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 86 | 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 Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 88 | |
| 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 Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 93 | // The thread that polls sock_diag continously. |
| 94 | std::thread mPollingThread; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 95 | // The duration of a sleep between polls. Can be updated by the instance owner for dynamically |
| 96 | // adjusting the polling rate. |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 97 | milliseconds mNextSleepDurationMs GUARDED_BY(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 98 | // The time of the last successful poll operation. |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 99 | time_point mLastPoll GUARDED_BY(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 100 | // 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 Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 104 | // 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 Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace net |
| 115 | } // namespace android |
| 116 | |
| 117 | #endif /* TCP_SOCKET_MONITOR_H */ |