1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.21
package quic
import (
"context"
"encoding/hex"
"log/slog"
"net/netip"
"time"
)
// Log levels for qlog events.
const (
// QLogLevelFrame includes per-frame information.
// When this level is enabled, packet_sent and packet_received events will
// contain information on individual frames sent/received.
QLogLevelFrame = slog.Level(-6)
// QLogLevelPacket events occur at most once per packet sent or received.
//
// For example: packet_sent, packet_received.
QLogLevelPacket = slog.Level(-4)
// QLogLevelConn events occur multiple times over a connection's lifetime,
// but less often than the frequency of individual packets.
//
// For example: connection_state_updated.
QLogLevelConn = slog.Level(-2)
// QLogLevelEndpoint events occur at most once per connection.
//
// For example: connection_started, connection_closed.
QLogLevelEndpoint = slog.Level(0)
)
func (c *Conn) logEnabled(level slog.Level) bool {
return logEnabled(c.log, level)
}
func logEnabled(log *slog.Logger, level slog.Level) bool {
return log != nil && log.Enabled(context.Background(), level)
}
// slogHexstring returns a slog.Attr for a value of the hexstring type.
//
// https://www.ietf.org/archive/id/draft-ietf-quic-qlog-main-schema-04.html#section-1.1.1
func slogHexstring(key string, value []byte) slog.Attr {
return slog.String(key, hex.EncodeToString(value))
}
func slogAddr(key string, value netip.Addr) slog.Attr {
return slog.String(key, value.String())
}
func (c *Conn) logConnectionStarted(originalDstConnID []byte, peerAddr netip.AddrPort) {
if c.config.QLogLogger == nil ||
!c.config.QLogLogger.Enabled(context.Background(), QLogLevelEndpoint) {
return
}
var vantage string
if c.side == clientSide {
vantage = "client"
originalDstConnID = c.connIDState.originalDstConnID
} else {
vantage = "server"
}
// A qlog Trace container includes some metadata (title, description, vantage_point)
// and a list of Events. The Trace also includes a common_fields field setting field
// values common to all events in the trace.
//
// Trace = {
// ? title: text
// ? description: text
// ? configuration: Configuration
// ? common_fields: CommonFields
// ? vantage_point: VantagePoint
// events: [* Event]
// }
//
// To map this into slog's data model, we start each per-connection trace with a With
// call that includes both the trace metadata and the common fields.
//
// This means that in slog's model, each trace event will also include
// the Trace metadata fields (vantage_point), which is a divergence from the qlog model.
c.log = c.config.QLogLogger.With(
// The group_id permits associating traces taken from different vantage points
// for the same connection.
//
// We use the original destination connection ID as the group ID.
//
// https://www.ietf.org/archive/id/draft-ietf-quic-qlog-main-schema-04.html#section-3.4.6
slogHexstring("group_id", originalDstConnID),
slog.Group("vantage_point",
slog.String("name", "go quic"),
slog.String("type", vantage),
),
)
localAddr := c.endpoint.LocalAddr()
// https://www.ietf.org/archive/id/draft-ietf-quic-qlog-quic-events-03.html#section-4.2
c.log.LogAttrs(context.Background(), QLogLevelEndpoint,
"connectivity:connection_started",
slogAddr("src_ip", localAddr.Addr()),
slog.Int("src_port", int(localAddr.Port())),
slogHexstring("src_cid", c.connIDState.local[0].cid),
slogAddr("dst_ip", peerAddr.Addr()),
slog.Int("dst_port", int(peerAddr.Port())),
slogHexstring("dst_cid", c.connIDState.remote[0].cid),
)
}
func (c *Conn) logConnectionClosed() {
if !c.logEnabled(QLogLevelEndpoint) {
return
}
err := c.lifetime.finalErr
trigger := "error"
switch e := err.(type) {
case *ApplicationError:
// TODO: Distinguish between peer and locally-initiated close.
trigger = "application"
case localTransportError:
switch err {
case errHandshakeTimeout:
trigger = "handshake_timeout"
default:
if e.code == errNo {
trigger = "clean"
}
}
case peerTransportError:
if e.code == errNo {
trigger = "clean"
}
default:
switch err {
case errIdleTimeout:
trigger = "idle_timeout"
case errStatelessReset:
trigger = "stateless_reset"
}
}
// https://www.ietf.org/archive/id/draft-ietf-quic-qlog-quic-events-03.html#section-4.3
c.log.LogAttrs(context.Background(), QLogLevelEndpoint,
"connectivity:connection_closed",
slog.String("trigger", trigger),
)
}
func (c *Conn) logPacketDropped(dgram *datagram) {
c.log.LogAttrs(context.Background(), QLogLevelPacket,
"connectivity:packet_dropped",
)
}
func (c *Conn) logLongPacketReceived(p longPacket, pkt []byte) {
var frames slog.Attr
if c.logEnabled(QLogLevelFrame) {
frames = c.packetFramesAttr(p.payload)
}
c.log.LogAttrs(context.Background(), QLogLevelPacket,
"transport:packet_received",
slog.Group("header",
slog.String("packet_type", p.ptype.qlogString()),
slog.Uint64("packet_number", uint64(p.num)),
slog.Uint64("flags", uint64(pkt[0])),
slogHexstring("scid", p.srcConnID),
slogHexstring("dcid", p.dstConnID),
),
slog.Group("raw",
slog.Int("length", len(pkt)),
),
frames,
)
}
func (c *Conn) log1RTTPacketReceived(p shortPacket, pkt []byte) {
var frames slog.Attr
if c.logEnabled(QLogLevelFrame) {
frames = c.packetFramesAttr(p.payload)
}
dstConnID, _ := dstConnIDForDatagram(pkt)
c.log.LogAttrs(context.Background(), QLogLevelPacket,
"transport:packet_received",
slog.Group("header",
slog.String("packet_type", packetType1RTT.qlogString()),
slog.Uint64("packet_number", uint64(p.num)),
slog.Uint64("flags", uint64(pkt[0])),
slogHexstring("dcid", dstConnID),
),
slog.Group("raw",
slog.Int("length", len(pkt)),
),
frames,
)
}
func (c *Conn) logPacketSent(ptype packetType, pnum packetNumber, src, dst []byte, pktLen int, payload []byte) {
var frames slog.Attr
if c.logEnabled(QLogLevelFrame) {
frames = c.packetFramesAttr(payload)
}
var scid slog.Attr
if len(src) > 0 {
scid = slogHexstring("scid", src)
}
c.log.LogAttrs(context.Background(), QLogLevelPacket,
"transport:packet_sent",
slog.Group("header",
slog.String("packet_type", ptype.qlogString()),
slog.Uint64("packet_number", uint64(pnum)),
scid,
slogHexstring("dcid", dst),
),
slog.Group("raw",
slog.Int("length", pktLen),
),
frames,
)
}
// packetFramesAttr returns the "frames" attribute containing the frames in a packet.
// We currently pass this as a slog Any containing a []slog.Value,
// where each Value is a debugFrame that implements slog.LogValuer.
//
// This isn't tremendously efficient, but avoids the need to put a JSON encoder
// in the quic package or a frame parser in the qlog package.
func (c *Conn) packetFramesAttr(payload []byte) slog.Attr {
var frames []slog.Value
for len(payload) > 0 {
f, n := parseDebugFrame(payload)
if n < 0 {
break
}
payload = payload[n:]
switch f := f.(type) {
case debugFrameAck:
// The qlog ACK frame contains the ACK Delay field as a duration.
// Interpreting the contents of this field as a duration requires
// knowing the peer's ack_delay_exponent transport parameter,
// and it's possible for us to parse an ACK frame before we've
// received that parameter.
//
// We could plumb connection state down into the frame parser,
// but for now let's minimize the amount of code that needs to
// deal with this and convert the unscaled value into a scaled one here.
ackDelay := time.Duration(-1)
if c.peerAckDelayExponent >= 0 {
ackDelay = f.ackDelay.Duration(uint8(c.peerAckDelayExponent))
}
frames = append(frames, slog.AnyValue(debugFrameScaledAck{
ranges: f.ranges,
ackDelay: ackDelay,
}))
default:
frames = append(frames, slog.AnyValue(f))
}
}
return slog.Any("frames", frames)
}
func (c *Conn) logPacketLost(space numberSpace, sent *sentPacket) {
c.log.LogAttrs(context.Background(), QLogLevelPacket,
"recovery:packet_lost",
slog.Group("header",
slog.String("packet_type", sent.ptype.qlogString()),
slog.Uint64("packet_number", uint64(sent.num)),
),
)
}
|