xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/channel/tcp_tracer.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_LIB_CHANNEL_TCP_TRACER_H
20 #define GRPC_SRC_CORE_LIB_CHANNEL_TCP_TRACER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include <string>
28 
29 #include "absl/time/time.h"
30 #include "absl/types/optional.h"
31 
32 namespace grpc_core {
33 
34 // Interface for TCP tracer implementations. Created by CallTracerInterface.
35 class TcpTracerInterface {
36  public:
37   enum class Type {
38     kUnknown = 0,
39     // When the sendmsg system call or its variants returned for the traced byte
40     // offset.
41     kSendMsg,
42     // When the traced byte offset is enqueued in kernel schedulers (aka,
43     // qdiscs). There can be multiple schedulers.
44     kScheduled,
45     // When the traced byte offset is handed over to the NIC.
46     kSent,
47     // When the acknowledgement for the traced byte offset was received.
48     kAcked,
49     // When the connection is closed. This is not associated with a byte offset.
50     kClosed,
51   };
52 
53   struct ConnectionMetrics {
54     // Congestion control name.
55     std::string congestion_ctrl;
56     // Delivery rate in Bps.
57     absl::optional<uint64_t> delivery_rate;
58     // Total bytes retransmitted so far.
59     absl::optional<uint64_t> data_retx;
60     // Total bytes sent so far.
61     absl::optional<uint64_t> data_sent;
62     // Total packets lost so far.
63     // Includes lost or spuriously retransmitted packets.
64     absl::optional<uint32_t> packet_retx;
65     // Total packets spuriously retransmitted so far.
66     absl::optional<uint32_t> packet_spurious_retx;
67     // Total packets sent so far.
68     absl::optional<uint32_t> packet_sent;
69     // Total packets delivered so far.
70     absl::optional<uint32_t> packet_delivered;
71     // Total packets delivered so far with ECE marked.
72     // This metric is smaller than or equal to packet_delivered.
73     absl::optional<uint32_t> packet_delivered_ce;
74     // Total bytes in write queue but not sent.
75     absl::optional<uint64_t> data_notsent;
76     // Minimum RTT observed in usec.
77     absl::optional<uint32_t> min_rtt;
78     // Smoothed RTT in usec
79     absl::optional<uint32_t> srtt;
80     // TTL or hop limit of a packet received
81     // Only available with ACKED timestamps.
82     absl::optional<uint32_t> ttl;
83     // Represents the number of recurring retransmissions of
84     // the first sequence that is not acknowledged yet.
85     absl::optional<uint32_t> recurring_retrans;
86     // Network RTT using hardware timestamps (in usec).
87     // A value of -1 indicates that net_rtt could not be measured.
88     absl::optional<int32_t> net_rtt_usec;
89     // Timeout-triggered rehash attempts.
90     absl::optional<uint32_t> timeout_rehash;
91     // Rehash due to ECN congestion.
92     absl::optional<uint32_t> ecn_rehash;
93     // Earliest departure time (CLOCK_MONOTONIC).
94     // Only available with SCHEDULED and SENT timestamps.
95     absl::optional<uint64_t> edt;
96     // If the delivery rate is limited by the application, this is set to
97     // true.
98     absl::optional<bool> is_delivery_rate_app_limited;
99     // Pacing rate of the connection in Bps.
100     absl::optional<uint64_t> pacing_rate;
101     // Send congestion window in packets.
102     absl::optional<uint32_t> congestion_window;
103     // Maximum degree of reordering (i.e., maximum number of packets reodered)
104     // on the connection.
105     absl::optional<uint32_t> reordering;
106     // Cumulative duration (in usec) that the transport protocol
107     // was busy sending time.
108     absl::optional<uint64_t> busy_usec;
109     // Cumulative duration (in usec) that the transport protocol
110     // was limited by the receive window size.
111     absl::optional<uint64_t> rwnd_limited_usec;
112     // Cumulative duration (in usec) that the transport protocol
113     // was limited by the send buffer size.
114     absl::optional<uint64_t> sndbuf_limited_usec;
115     // Slow start size threshold in packets.
116     // Set to TCP_INFINITE_SSTHRESH when still in slow start.
117     absl::optional<uint32_t> snd_ssthresh;
118     // The extra time it takes for the receiver to generate the
119     // acknowledgement after receiving the last packet. This metric is not
120     // cumulative. Only available with ACKED timestamps.
121     absl::optional<uint32_t> time_to_ack_usec;
122     // Last socket error code. Only populated for CLOSED timestamps.
123     absl::optional<uint32_t> socket_errno;
124     // Peer's receive window after scaling (tcpi_snd_wnd).
125     // Only available with SENDMSG timestamps.
126     absl::optional<uint32_t> peer_rwnd;
127     // Receive queue drops.
128     absl::optional<uint32_t> rcvq_drops;
129     // The NIC Rx delay reported by the remote host.
130     absl::optional<uint32_t> nic_rx_delay_usec;
131   };
132 
133   virtual ~TcpTracerInterface() = default;
134   // Records a per-message event with an optional snapshot of connection
135   // metrics.
136   virtual void RecordEvent(Type type, absl::Time time, size_t byte_offset,
137                            absl::optional<ConnectionMetrics> metrics) = 0;
138   // Records a snapshot of connection metrics.
139   virtual void RecordConnectionMetrics(ConnectionMetrics metrics) = 0;
140 };
141 
142 }  // namespace grpc_core
143 
144 #endif  // GRPC_SRC_CORE_LIB_CHANNEL_TCP_TRACER_H
145