removing non-existing file from the build
[folly.git] / folly / wangle / acceptor / TransportInfo.h
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
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.
8  *
9  */
10 #pragma once
11
12 #include <folly/wangle/ssl/SSLUtil.h>
13
14 #include <chrono>
15 #include <netinet/tcp.h>
16 #include <string>
17
18 namespace folly {
19 class AsyncSocket;
20
21 /**
22  * A structure that encapsulates byte counters related to the HTTP headers.
23  */
24 struct HTTPHeaderSize {
25   /**
26    * The number of bytes used to represent the header after compression or
27    * before decompression. If header compression is not supported, the value
28    * is set to 0.
29    */
30   size_t compressed{0};
31
32   /**
33    * The number of bytes used to represent the serialized header before
34    * compression or after decompression, in plain-text format.
35    */
36   size_t uncompressed{0};
37 };
38
39 struct TransportInfo {
40   /*
41    * timestamp of when the connection handshake was completed
42    */
43   std::chrono::steady_clock::time_point acceptTime{};
44
45   /*
46    * connection RTT (Round-Trip Time)
47    */
48   std::chrono::microseconds rtt{0};
49
50 #if defined(__linux__) || defined(__FreeBSD__)
51   /*
52    * TCP information as fetched from getsockopt(2)
53    */
54   tcp_info tcpinfo {
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
57 #else
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
60   };
61 #endif  // defined(__linux__) || defined(__FreeBSD__)
62
63   /*
64    * time for setting the connection, from the moment in was accepted until it
65    * is established.
66    */
67   std::chrono::milliseconds setupTime{0};
68
69   /*
70    * time for setting up the SSL connection or SSL handshake
71    */
72   std::chrono::milliseconds sslSetupTime{0};
73
74   /*
75    * The name of the SSL ciphersuite used by the transaction's
76    * transport.  Returns null if the transport is not SSL.
77    */
78   std::shared_ptr<std::string> sslCipher{nullptr};
79
80   /*
81    * The SSL server name used by the transaction's
82    * transport.  Returns null if the transport is not SSL.
83    */
84   std::shared_ptr<std::string> sslServerName{nullptr};
85
86   /*
87    * list of ciphers sent by the client
88    */
89   std::shared_ptr<std::string> sslClientCiphers{nullptr};
90
91   /*
92    * list of compression methods sent by the client
93    */
94   std::shared_ptr<std::string> sslClientComprMethods{nullptr};
95
96   /*
97    * list of TLS extensions sent by the client
98    */
99   std::shared_ptr<std::string> sslClientExts{nullptr};
100
101   /*
102    * hash of all the SSL parameters sent by the client
103    */
104   std::shared_ptr<std::string> sslSignature{nullptr};
105
106   /*
107    * list of ciphers supported by the server
108    */
109   std::shared_ptr<std::string> sslServerCiphers{nullptr};
110
111   /*
112    * guessed "(os) (browser)" based on SSL Signature
113    */
114   std::shared_ptr<std::string> guessedUserAgent{nullptr};
115
116   /**
117    * The result of SSL NPN negotiation.
118    */
119   std::shared_ptr<std::string> sslNextProtocol{nullptr};
120
121   /*
122    * total number of bytes sent over the connection
123    */
124   int64_t totalBytes{0};
125
126   /**
127    * If the client passed through one of our L4 proxies (using PROXY Protocol),
128    * then this will contain the IP address of the proxy host.
129    */
130   std::shared_ptr<folly::SocketAddress> clientAddrOriginal;
131
132   /**
133    * header bytes read
134    */
135   HTTPHeaderSize ingressHeader;
136
137   /*
138    * header bytes written
139    */
140   HTTPHeaderSize egressHeader;
141
142   /*
143    * Here is how the timeToXXXByte variables are planned out:
144    * 1. All timeToXXXByte variables are measuring the ByteEvent from reqStart_
145    * 2. You can get the timing between two ByteEvents by calculating their
146    *    differences. For example:
147    *    timeToLastBodyByteAck - timeToFirstByte
148    *    => Total time to deliver the body
149    * 3. The calculation in point (2) is typically done outside acceptor
150    *
151    * Future plan:
152    * We should log the timestamps (TimePoints) and allow
153    * the consumer to calculate the latency whatever it
154    * wants instead of calculating them in wangle, for the sake of flexibility.
155    * For example:
156    * 1. TimePoint reqStartTimestamp;
157    * 2. TimePoint firstHeaderByteSentTimestamp;
158    * 3. TimePoint firstBodyByteTimestamp;
159    * 3. TimePoint lastBodyByteTimestamp;
160    * 4. TimePoint lastBodyByteAckTimestamp;
161    */
162
163   /*
164    * time to first header byte written to the kernel send buffer
165    * NOTE: It is not 100% accurate since TAsyncSocket does not do
166    * do callback on partial write.
167    */
168   int32_t timeToFirstHeaderByte{-1};
169
170   /*
171    * time to first body byte written to the kernel send buffer
172    */
173   int32_t timeToFirstByte{-1};
174
175   /*
176    * time to last body byte written to the kernel send buffer
177    */
178   int32_t timeToLastByte{-1};
179
180   /*
181    * time to TCP Ack received for the last written body byte
182    */
183   int32_t timeToLastBodyByteAck{-1};
184
185   /*
186    * time it took the client to ACK the last byte, from the moment when the
187    * kernel sent the last byte to the client and until it received the ACK
188    * for that byte
189    */
190   int32_t lastByteAckLatency{-1};
191
192   /*
193    * time spent inside wangle
194    */
195   int32_t proxyLatency{-1};
196
197   /*
198    * time between connection accepted and client message headers completed
199    */
200   int32_t clientLatency{-1};
201
202   /*
203    * latency for communication with the server
204    */
205   int32_t serverLatency{-1};
206
207   /*
208    * time used to get a usable connection.
209    */
210   int32_t connectLatency{-1};
211
212   /*
213    * body bytes written
214    */
215   uint32_t egressBodySize{0};
216
217   /*
218    * value of errno in case of getsockopt() error
219    */
220   int tcpinfoErrno{0};
221
222   /*
223    * bytes read & written during SSL Setup
224    */
225   uint32_t sslSetupBytesWritten{0};
226   uint32_t sslSetupBytesRead{0};
227
228   /**
229    * SSL error detail
230    */
231   uint32_t sslError{0};
232
233   /**
234    * body bytes read
235    */
236   uint32_t ingressBodySize{0};
237
238   /*
239    * The SSL version used by the transaction's transport, in
240    * OpenSSL's format: 4 bits for the major version, followed by 4 bits
241    * for the minor version.  Returns zero for non-SSL.
242    */
243   uint16_t sslVersion{0};
244
245   /*
246    * The SSL certificate size.
247    */
248   uint16_t sslCertSize{0};
249
250   /**
251    * response status code
252    */
253   uint16_t statusCode{0};
254
255   /*
256    * The SSL mode for the transaction's transport: new session,
257    * resumed session, or neither (non-SSL).
258    */
259   SSLResumeEnum sslResume{SSLResumeEnum::NA};
260
261   /*
262    * true if the tcpinfo was successfully read from the kernel
263    */
264   bool validTcpinfo{false};
265
266   /*
267    * true if the connection is SSL, false otherwise
268    */
269   bool ssl{false};
270
271   /*
272    * get the RTT value in milliseconds
273    */
274   std::chrono::milliseconds getRttMs() const {
275     return std::chrono::duration_cast<std::chrono::milliseconds>(rtt);
276   }
277
278   /*
279    * initialize the fields related with tcp_info
280    */
281   bool initWithSocket(const AsyncSocket* sock);
282
283   /*
284    * Get the kernel's estimate of round-trip time (RTT) to the transport's peer
285    * in microseconds. Returns -1 on error.
286    */
287   static int64_t readRTT(const AsyncSocket* sock);
288
289 #if defined(__linux__) || defined(__FreeBSD__)
290   /*
291    * perform the getsockopt(2) syscall to fetch TCP info for a given socket
292    */
293   static bool readTcpInfo(struct tcp_info* tcpinfo,
294                           const AsyncSocket* sock);
295 #endif
296 };
297
298 } // folly