proxygen
TransportInfo.h
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  */
16 #pragma once
17 
19 #include <wangle/ssl/SSLUtil.h>
20 
21 #include <chrono>
22 #include <string>
23 
24 #include <folly/SocketAddress.h>
26 
27 namespace folly {
28 
29 class AsyncSocket;
30 
31 }
32 
33 namespace wangle {
34 
44  size_t compressed{0};
45 
50  size_t uncompressed{0};
51 };
52 
57 struct ProtocolInfo {
58  virtual ~ProtocolInfo() = default;
59 };
60 
61 struct TransportInfo {
62  /*
63  * timestamp of when the connection handshake was completed
64  */
65  std::chrono::steady_clock::time_point acceptTime{};
66 
67  /*
68  * connection RTT (Round-Trip Time)
69  */
70  std::chrono::microseconds rtt{0};
71 
72  /*
73  * RTT variance in usecs (microseconds)
74  */
75  int64_t rtt_var{-1};
76 
77  /*
78  * the total number of packets retransmitted during the connection lifetime.
79  */
80  int64_t rtx{-1};
81 
82  /*
83  * the number of packets retransmitted due to timeout
84  */
85  int64_t rtx_tm{-1};
86 
87  /*
88  * retransmission timeout (usec)
89  */
90  int64_t rto{-1};
91 
92  /*
93  * The congestion window size in MSS
94  */
95  int64_t cwnd{-1};
96 
97  /*
98  * The congestion window size in bytes
99  */
100  int64_t cwndBytes{-1};
101 
102  /*
103  * MSS
104  */
105  int64_t mss{-1};
106 
107  /*
108  * slow start threshold
109  */
110  int64_t ssthresh{-1};
111 
112 #ifdef __APPLE__
113  typedef tcp_connection_info tcp_info;
114 #endif
115 
116 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
117  /*
118  * TCP information as fetched from getsockopt(2)
119  */
120  tcp_info tcpinfo {};
121 #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
122 
123  /*
124  * time for setting the connection, from the moment in was accepted until it
125  * is established.
126  */
127  std::chrono::milliseconds setupTime{0};
128 
129  /*
130  * NOTE: Avoid using any fields starting with "ssl" for anything other than
131  * logging, as those field may not be populated for all security protocols.
132  */
133 
134  /*
135  * time for setting up the SSL connection or SSL handshake
136  */
137  std::chrono::milliseconds sslSetupTime{0};
138 
139  /*
140  * The name of the SSL ciphersuite used by the transaction's
141  * transport. Returns null if the transport is not SSL.
142  */
143  std::shared_ptr<std::string> sslCipher{nullptr};
144 
145  /*
146  * The SSL server name used by the transaction's
147  * transport. Returns null if the transport is not SSL.
148  */
149  std::shared_ptr<std::string> sslServerName{nullptr};
150 
151  /*
152  * list of ciphers sent by the client
153  */
154  std::shared_ptr<std::string> sslClientCiphers{nullptr};
155 
156  /*
157  * client ciphers as a series of 4-byte hex strings (e.g., 'cc14')
158  */
159  std::shared_ptr<std::string> sslClientCiphersHex{nullptr};
160 
161  /*
162  * list of compression methods sent by the client
163  */
164  std::shared_ptr<std::string> sslClientComprMethods{nullptr};
165 
166  /*
167  * list of TLS extensions sent by the client
168  */
169  std::shared_ptr<std::string> sslClientExts{nullptr};
170 
171  /*
172  * list of hash and signature algorithms sent by the client
173  */
174  std::shared_ptr<std::string> sslClientSigAlgs{nullptr};
175 
176  /*
177  * list of supported versions sent by client in supported versions extension
178  */
179  std::shared_ptr<std::string> sslClientSupportedVersions{nullptr};
180 
181  /*
182  * hash of all the SSL parameters sent by the client
183  */
184  std::shared_ptr<std::string> sslSignature{nullptr};
185 
186  /*
187  * list of ciphers supported by the server
188  */
189  std::shared_ptr<std::string> sslServerCiphers{nullptr};
190 
191  /*
192  * guessed "(os) (browser)" based on SSL Signature
193  */
194  std::shared_ptr<std::string> guessedUserAgent{nullptr};
195 
199  std::shared_ptr<std::string> appProtocol{nullptr};
200 
201  /*
202  * total number of bytes sent over the connection
203  */
204  int64_t totalBytes{0};
205 
210  std::shared_ptr<folly::SocketAddress> remoteAddr;
211 
216  std::shared_ptr<folly::SocketAddress> localAddr;
217 
222  std::shared_ptr<folly::SocketAddress> clientAddrOriginal;
223 
228 
229  /*
230  * header bytes written
231  */
233 
234  /*
235  * Here is how the timeToXXXByte variables are planned out:
236  * 1. All timeToXXXByte variables are measuring the ByteEvent from reqStart_
237  * 2. You can get the timing between two ByteEvents by calculating their
238  * differences. For example:
239  * timeToLastBodyByteAck - timeToFirstByte
240  * => Total time to deliver the body
241  * 3. The calculation in point (2) is typically done outside acceptor
242  *
243  * Future plan:
244  * We should log the timestamps (TimePoints) and allow
245  * the consumer to calculate the latency whatever it
246  * wants instead of calculating them in wangle, for the sake of flexibility.
247  * For example:
248  * 1. TimePoint reqStartTimestamp;
249  * 2. TimePoint firstHeaderByteSentTimestamp;
250  * 3. TimePoint firstBodyByteTimestamp;
251  * 3. TimePoint lastBodyByteTimestamp;
252  * 4. TimePoint lastBodyByteAckTimestamp;
253  */
254 
255  /*
256  * time to first header byte written to the kernel send buffer
257  * NOTE: It is not 100% accurate since TAsyncSocket does not do
258  * do callback on partial write.
259  */
260  int32_t timeToFirstHeaderByte{-1};
261 
262  /*
263  * time to first body byte written to the kernel send buffer
264  */
265  int32_t timeToFirstByte{-1};
266 
267  /*
268  * time to last body byte written to the kernel send buffer
269  */
270  int32_t timeToLastByte{-1};
271 
272  /*
273  * time to TCP Ack received for the last written body byte
274  */
275  int32_t timeToLastBodyByteAck{-1};
276 
277  /*
278  * time it took the client to ACK the last byte, from the moment when the
279  * kernel sent the last byte to the client and until it received the ACK
280  * for that byte
281  */
282  int32_t lastByteAckLatency{-1};
283 
284  /*
285  * time spent inside wangle
286  */
287  int32_t proxyLatency{-1};
288 
289  /*
290  * time between connection accepted and client message headers completed
291  */
292  int32_t clientLatency{-1};
293 
294  /*
295  * latency for communication with the server
296  */
297  int32_t serverLatency{-1};
298 
299  /*
300  * time used to get a usable connection.
301  */
302  int32_t connectLatency{-1};
303 
304  /*
305  * body bytes written
306  */
307  uint32_t egressBodySize{0};
308 
309  /*
310  * value of errno in case of getsockopt() error
311  */
312  int tcpinfoErrno{0};
313 
314  /*
315  * bytes read & written during SSL Setup
316  */
317  uint32_t sslSetupBytesWritten{0};
318  uint32_t sslSetupBytesRead{0};
319 
324 
328  uint32_t ingressBodySize{0};
329 
330  /*
331  * The SSL version used by the transaction's transport, in
332  * OpenSSL's format: 4 bits for the major version, followed by 4 bits
333  * for the minor version. Returns zero for non-SSL.
334  */
335  uint16_t sslVersion{0};
336 
337  /*
338  * The signature algorithm used in the certificate.
339  */
340  std::shared_ptr<std::string> sslCertSigAlgName{nullptr};
341 
342  /*
343  * The SSL certificate size.
344  */
345  uint16_t sslCertSize{0};
346 
351 
352  /*
353  * The SSL mode for the transaction's transport: new session,
354  * resumed session, or neither (non-SSL).
355  */
356  SSLResumeEnum sslResume{SSLResumeEnum::NA};
357 
358  /*
359  * true if the tcpinfo was successfully read from the kernel
360  */
361  bool validTcpinfo{false};
362 
363  /*
364  * true if the connection is SSL, false otherwise
365  */
366  bool secure{false};
367 
372 
373  /*
374  * Additional protocol info.
375  */
376  std::shared_ptr<ProtocolInfo> protocolInfo{nullptr};
377 
378  /*
379  * Hash of some of TCP/IP headers fields values, sometimes concatenated with
380  * raw signature (that gives the hash).
381  */
382  std::shared_ptr<std::string> tcpSignature{nullptr};
383 
384  /*
385  * Whether or not TCP fast open succeded on this connection. Failure can occur
386  * due to several reasons, including cookies not matching or TFO not being
387  * advertised by the client.
388  */
389  bool tfoSucceded{false};
390 
391  /*
392  * Stores the TokenBindingKeyParameter that was negotiatied during the
393  * handshake. Needed for the validation step of Token Binding.
394  */
396 
397  /*
398  * get the RTT value in milliseconds
399  */
400  std::chrono::milliseconds getRttMs() const {
401  return std::chrono::duration_cast<std::chrono::milliseconds>(rtt);
402  }
403 
404  /*
405  * initialize the fields related with tcp_info
406  */
407  bool initWithSocket(const folly::AsyncSocket* sock);
408 
409  /*
410  * Get the kernel's estimate of round-trip time (RTT) to the transport's peer
411  * in microseconds. Returns -1 on error.
412  */
413  static int64_t readRTT(const folly::AsyncSocket* sock);
414 
415 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
416  /*
417  * perform the getsockopt(2) syscall to fetch TCP info for a given socket
418  */
419  static bool readTcpInfo(tcp_info* tcpinfo,
420  const folly::AsyncSocket* sock);
421 #endif
422 };
423 
424 } // namespace wangle
spdy::GoawayStatusCode statusCode
Definition: SPDYCodec.cpp:110
SSLResumeEnum
Definition: SSLUtil.h:35
std::shared_ptr< folly::SocketAddress > localAddr
std::shared_ptr< folly::SocketAddress > clientAddrOriginal
std::string securityType
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
HTTPHeaderSize ingressHeader
HTTPHeaderSize egressHeader
std::shared_ptr< folly::SocketAddress > remoteAddr
const char * string
Definition: Conv.cpp:212
std::chrono::milliseconds getRttMs() const
folly::Optional< uint8_t > negotiatedTokenBindingKeyParameters