proxygen
TransportInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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  */
17 
18 #include <sys/types.h>
21 
22 using std::chrono::microseconds;
23 using std::map;
24 using std::string;
25 
26 namespace wangle {
27 
29 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
30  if (!TransportInfo::readTcpInfo(&tcpinfo, sock)) {
31  tcpinfoErrno = errno;
32  return false;
33  }
34 #ifdef __APPLE__
35  rtt = microseconds(tcpinfo.tcpi_srtt * 1000);
36  rtt_var = tcpinfo.tcpi_rttvar * 1000;
37  rto = tcpinfo.tcpi_rto * 1000;
38  rtx_tm = -1;
39  mss = tcpinfo.tcpi_maxseg;
40  cwndBytes = tcpinfo.tcpi_snd_cwnd;
41  if (mss > 0) {
42  cwnd = (cwndBytes + mss - 1) / mss;
43  }
44 #else
45  rtt = microseconds(tcpinfo.tcpi_rtt);
46  rtt_var = tcpinfo.tcpi_rttvar;
47  rto = tcpinfo.tcpi_rto;
48  rtx_tm = tcpinfo.tcpi_retransmits;
49  mss = tcpinfo.tcpi_snd_mss;
50  cwnd = tcpinfo.tcpi_snd_cwnd;
51  cwndBytes = cwnd * mss;
52 #endif // __APPLE__
53  ssthresh = tcpinfo.tcpi_snd_ssthresh;
54 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 17
55  rtx = tcpinfo.tcpi_total_retrans;
56 #else
57  rtx = -1;
58 #endif // __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 17
59  validTcpinfo = true;
60 #else
61  tcpinfoErrno = EINVAL;
62  rtt = microseconds(-1);
63  rtt_var = -1;
64  rtx = -1;
65  rtx_tm = -1;
66  rto = -1;
67  cwnd = -1;
68  mss = -1;
69  ssthresh = -1;
70 #endif
71  return true;
72 }
73 
75 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
76  tcp_info tcpinfo;
77  if (!TransportInfo::readTcpInfo(&tcpinfo, sock)) {
78  return -1;
79  }
80 #endif
81 #if defined(__linux__) || defined(__FreeBSD__)
82  return tcpinfo.tcpi_rtt;
83 #elif defined(__APPLE__)
84  return tcpinfo.tcpi_srtt;
85 #else
86  return -1;
87 #endif
88 }
89 
90 #ifdef __APPLE__
91 #define TCP_INFO TCP_CONNECTION_INFO
92 #endif
93 
94 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
95 bool TransportInfo::readTcpInfo(tcp_info* tcpinfo,
96  const folly::AsyncSocket* sock) {
97  socklen_t len = sizeof(tcp_info);
98  if (!sock) {
99  return false;
100  }
101  if (getsockopt(sock->getFd(), IPPROTO_TCP,
102  TCP_INFO, (void*) tcpinfo, &len) < 0) {
103  VLOG(4) << "Error calling getsockopt(): " << strerror(errno);
104  return false;
105  }
106  return true;
107 }
108 #endif
109 
110 } // namespace wangle
static int64_t readRTT(const folly::AsyncSocket *sock)
bool initWithSocket(const folly::AsyncSocket *sock)
static Map map(mapCap)
int getsockopt(NetworkSocket s, int level, int optname, void *optval, socklen_t *optlen)
Definition: NetOps.cpp:112
virtual int getFd() const
Definition: AsyncSocket.h:335
std::chrono::microseconds rtt
Definition: TransportInfo.h:70
const char * string
Definition: Conv.cpp:212