2 * Copyright (c) 2014, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
12 #include <folly/wangle/ssl/SSLUtil.h>
15 #include <netinet/tcp.h>
22 * A structure that encapsulates byte counters related to the HTTP headers.
24 struct HTTPHeaderSize {
26 * The number of bytes used to represent the header after compression or
27 * before decompression. If header compression is not supported, the value
33 * The number of bytes used to represent the serialized header before
34 * compression or after decompression, in plain-text format.
36 size_t uncompressed{0};
39 struct TransportInfo {
41 * timestamp of when the connection handshake was completed
43 std::chrono::steady_clock::time_point acceptTime{};
46 * connection RTT (Round-Trip Time)
48 std::chrono::microseconds rtt{0};
50 #if defined(__linux__) || defined(__FreeBSD__)
52 * TCP information as fetched from getsockopt(2)
55 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 17
56 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 // 32
58 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 // 29
59 #endif // __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 17
61 #endif // defined(__linux__) || defined(__FreeBSD__)
64 * time for setting the connection, from the moment in was accepted until it
67 std::chrono::milliseconds setupTime{0};
70 * time for setting up the SSL connection or SSL handshake
72 std::chrono::milliseconds sslSetupTime{0};
75 * The name of the SSL ciphersuite used by the transaction's
76 * transport. Returns null if the transport is not SSL.
78 std::shared_ptr<std::string> sslCipher{nullptr};
81 * The SSL server name used by the transaction's
82 * transport. Returns null if the transport is not SSL.
84 std::shared_ptr<std::string> sslServerName{nullptr};
87 * list of ciphers sent by the client
89 std::shared_ptr<std::string> sslClientCiphers{nullptr};
92 * list of compression methods sent by the client
94 std::shared_ptr<std::string> sslClientComprMethods{nullptr};
97 * list of TLS extensions sent by the client
99 std::shared_ptr<std::string> sslClientExts{nullptr};
102 * hash of all the SSL parameters sent by the client
104 std::shared_ptr<std::string> sslSignature{nullptr};
107 * list of ciphers supported by the server
109 std::shared_ptr<std::string> sslServerCiphers{nullptr};
112 * guessed "(os) (browser)" based on SSL Signature
114 std::shared_ptr<std::string> guessedUserAgent{nullptr};
117 * The result of SSL NPN negotiation.
119 std::shared_ptr<std::string> sslNextProtocol{nullptr};
122 * total number of bytes sent over the connection
124 int64_t totalBytes{0};
129 HTTPHeaderSize ingressHeader;
132 * header bytes written
134 HTTPHeaderSize egressHeader;
137 * Here is how the timeToXXXByte variables are planned out:
138 * 1. All timeToXXXByte variables are measuring the ByteEvent from reqStart_
139 * 2. You can get the timing between two ByteEvents by calculating their
140 * differences. For example:
141 * timeToLastBodyByteAck - timeToFirstByte
142 * => Total time to deliver the body
143 * 3. The calculation in point (2) is typically done outside acceptor
146 * We should log the timestamps (TimePoints) and allow
147 * the consumer to calculate the latency whatever it
148 * wants instead of calculating them in wangle, for the sake of flexibility.
150 * 1. TimePoint reqStartTimestamp;
151 * 2. TimePoint firstHeaderByteSentTimestamp;
152 * 3. TimePoint firstBodyByteTimestamp;
153 * 3. TimePoint lastBodyByteTimestamp;
154 * 4. TimePoint lastBodyByteAckTimestamp;
158 * time to first header byte written to the kernel send buffer
159 * NOTE: It is not 100% accurate since TAsyncSocket does not do
160 * do callback on partial write.
162 int32_t timeToFirstHeaderByte{-1};
165 * time to first body byte written to the kernel send buffer
167 int32_t timeToFirstByte{-1};
170 * time to last body byte written to the kernel send buffer
172 int32_t timeToLastByte{-1};
175 * time to TCP Ack received for the last written body byte
177 int32_t timeToLastBodyByteAck{-1};
180 * time it took the client to ACK the last byte, from the moment when the
181 * kernel sent the last byte to the client and until it received the ACK
184 int32_t lastByteAckLatency{-1};
187 * time spent inside wangle
189 int32_t proxyLatency{-1};
192 * time between connection accepted and client message headers completed
194 int32_t clientLatency{-1};
197 * latency for communication with the server
199 int32_t serverLatency{-1};
202 * time used to get a usable connection.
204 int32_t connectLatency{-1};
209 uint32_t egressBodySize{0};
212 * value of errno in case of getsockopt() error
217 * bytes read & written during SSL Setup
219 uint32_t sslSetupBytesWritten{0};
220 uint32_t sslSetupBytesRead{0};
225 uint32_t sslError{0};
230 uint32_t ingressBodySize{0};
233 * The SSL version used by the transaction's transport, in
234 * OpenSSL's format: 4 bits for the major version, followed by 4 bits
235 * for the minor version. Returns zero for non-SSL.
237 uint16_t sslVersion{0};
240 * The SSL certificate size.
242 uint16_t sslCertSize{0};
245 * response status code
247 uint16_t statusCode{0};
250 * The SSL mode for the transaction's transport: new session,
251 * resumed session, or neither (non-SSL).
253 SSLResumeEnum sslResume{SSLResumeEnum::NA};
256 * true if the tcpinfo was successfully read from the kernel
258 bool validTcpinfo{false};
261 * true if the connection is SSL, false otherwise
266 * get the RTT value in milliseconds
268 std::chrono::milliseconds getRttMs() const {
269 return std::chrono::duration_cast<std::chrono::milliseconds>(rtt);
273 * initialize the fields related with tcp_info
275 bool initWithSocket(const AsyncSocket* sock);
278 * Get the kernel's estimate of round-trip time (RTT) to the transport's peer
279 * in microseconds. Returns -1 on error.
281 static int64_t readRTT(const AsyncSocket* sock);
283 #if defined(__linux__) || defined(__FreeBSD__)
285 * perform the getsockopt(2) syscall to fetch TCP info for a given socket
287 static bool readTcpInfo(struct tcp_info* tcpinfo,
288 const AsyncSocket* sock);