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