blob: 3eb80140a45121acf747091261efb2f478c28565 [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#define LOG_TAG "TcpSocketMonitor"
18
19#include "TcpSocketMonitor.h"
20#include "DumpWriter.h"
21
Hugo Benichicbaa36b2018-01-17 12:11:43 +090022#include "Fwmark.h"
Hugo Benichi7b314e12018-01-15 21:54:00 +090023#include "SockDiag.h"
24
25#include <arpa/inet.h>
26#include <netinet/tcp.h>
27#include <linux/tcp.h>
28
29namespace android {
30namespace net {
31
32constexpr const char* getTcpStateName(int t) {
33 switch (t) {
34 case TCP_ESTABLISHED:
35 return "ESTABLISHED";
36 case TCP_SYN_SENT:
37 return "SYN-SENT";
38 case TCP_SYN_RECV:
39 return "SYN-RECV";
40 case TCP_FIN_WAIT1:
41 return "FIN-WAIT-1";
42 case TCP_FIN_WAIT2:
43 return "FIN-WAIT-2";
44 case TCP_TIME_WAIT:
45 return "TIME-WAIT";
46 case TCP_CLOSE:
47 return "CLOSE";
48 case TCP_CLOSE_WAIT:
49 return "CLOSE-WAIT";
50 case TCP_LAST_ACK:
51 return "LAST-ACK";
52 case TCP_LISTEN:
53 return "LISTEN";
54 case TCP_CLOSING:
55 return "CLOSING";
56 default:
57 return "UNKNOWN";
58 }
59}
60
Hugo Benichicbaa36b2018-01-17 12:11:43 +090061static void tcpInfoPrint(DumpWriter &dw, Fwmark mark, const struct inet_diag_msg *sockinfo,
Hugo Benichi7b314e12018-01-15 21:54:00 +090062 const struct tcp_info *tcpinfo) {
63 char saddr[INET6_ADDRSTRLEN] = {};
64 char daddr[INET6_ADDRSTRLEN] = {};
65 inet_ntop(sockinfo->idiag_family, &(sockinfo->id.idiag_src), saddr, sizeof(saddr));
66 inet_ntop(sockinfo->idiag_family, &(sockinfo->id.idiag_dst), daddr, sizeof(daddr));
67
68 dw.println(
Hugo Benichicbaa36b2018-01-17 12:11:43 +090069 "netId=%d uid=%u mark=0x%x saddr=%s daddr=%s sport=%u dport=%u tcp_state=%s(%u) "
Hugo Benichi7b314e12018-01-15 21:54:00 +090070 "rqueue=%u wqueue=%u rtt=%gms var_rtt=%gms rcv_rtt=%gms unacked=%u snd_cwnd=%u",
Hugo Benichicbaa36b2018-01-17 12:11:43 +090071 mark.netId,
Hugo Benichi7b314e12018-01-15 21:54:00 +090072 sockinfo->idiag_uid,
Hugo Benichicbaa36b2018-01-17 12:11:43 +090073 mark.intValue,
Hugo Benichi7b314e12018-01-15 21:54:00 +090074 saddr,
75 daddr,
76 ntohs(sockinfo->id.idiag_sport),
77 ntohs(sockinfo->id.idiag_dport),
78 getTcpStateName(sockinfo->idiag_state), sockinfo->idiag_state,
79 sockinfo->idiag_rqueue,
80 sockinfo->idiag_wqueue,
81 tcpinfo != nullptr ? tcpinfo->tcpi_rtt/1000.0 : 0,
82 tcpinfo != nullptr ? tcpinfo->tcpi_rttvar/1000.0 : 0,
83 tcpinfo != nullptr ? tcpinfo->tcpi_rcv_rtt/1000.0 : 0,
84 tcpinfo != nullptr ? tcpinfo->tcpi_unacked : 0,
85 tcpinfo != nullptr ? tcpinfo->tcpi_snd_cwnd : 0);
86}
87
88const String16 TcpSocketMonitor::DUMP_KEYWORD = String16("tcp_socket_info");
89
90void TcpSocketMonitor::dump(DumpWriter& dw) {
91 dw.println("TcpSocketMonitor");
92 dw.incIndent();
93
94 SockDiag sd;
95 if (!sd.open()) {
96 ALOGE("Error opening sock diag for dumping TCP socket info");
97 return;
98 }
99
Hugo Benichicbaa36b2018-01-17 12:11:43 +0900100 const auto tcpInfoReader = [&dw](Fwmark mark, const struct inet_diag_msg *sockinfo,
Hugo Benichi7b314e12018-01-15 21:54:00 +0900101 const struct tcp_info *tcpinfo) {
Hugo Benichicbaa36b2018-01-17 12:11:43 +0900102 tcpInfoPrint(dw, mark, sockinfo, tcpinfo);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900103 };
104
105 if (int ret = sd.getLiveTcpInfos(tcpInfoReader)) {
106 ALOGE("Failed to dump TCP socket info: %s", strerror(-ret));
107 return;
108 }
109
110 dw.decIndent();
111}
112
113} // namespace net
114} // namespace android