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