Skip to content

Commit e3418f3

Browse files
committed
Trace shared-socket QUIC routing
1 parent 38f86bb commit e3418f3

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

lsquic/context/context.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0 OR MIT
22
# Copyright (c) Status Research & Development GmbH
33

4-
import std/[deques, hashes, sets]
4+
import std/[deques, hashes, sets, strutils]
55
import boringssl
66
import chronos
77
import chronos/osdefs
@@ -49,6 +49,14 @@ func hash*(cid: CidKey): Hash =
4949
h = h !& hash(cid.bytes[i])
5050
!$h
5151

52+
func shortLog*(cid: CidKey): string =
53+
result = $cid.len & ":"
54+
for i in 0 ..< min(cid.len.int, 8):
55+
result.add(toHex(cid.bytes[i], 2))
56+
57+
chronicles.formatIt(CidKey):
58+
shortLog(it)
59+
5260
func toCidKey(cid: RawLsquicCid, key: var CidKey): bool =
5361
if cid.len == 0 or cid.len.int > MAX_CID_LEN:
5462
return false
@@ -73,6 +81,8 @@ proc addCids*(
7381
var key: CidKey
7482
if toCidKey(cidsArr[i], key):
7583
quicCtx.ownedCids.incl(key)
84+
trace "Registered CID", cid = key,
85+
cidCount = quicCtx.ownedCids.len
7686

7787
proc removeCids*(
7888
ctx: pointer, peerCtxs: ptr pointer, cids: ptr lsquic_cid_t, nCids: cuint
@@ -86,6 +96,8 @@ proc removeCids*(
8696
var key: CidKey
8797
if toCidKey(cidsArr[i], key):
8898
quicCtx.ownedCids.excl(key)
99+
trace "Removed CID", cid = key,
100+
cidCount = quicCtx.ownedCids.len
89101

90102
proc trackConnectionCid*(
91103
ctx: QuicContext, conn: ptr lsquic_conn_t
@@ -100,6 +112,8 @@ proc trackConnectionCid*(
100112
var key: CidKey
101113
if toCidKey(cast[ptr RawLsquicCid](cid)[], key):
102114
ctx.ownedCids.incl(key)
115+
trace "Tracked connection CID", cid = key,
116+
cidCount = ctx.ownedCids.len
103117

104118
proc ownsCid*(ctx: QuicContext, cid: CidKey): bool {.raises: [].} =
105119
not ctx.isNil and cid in ctx.ownedCids

lsquic/context/io.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import chronos
55
import chronos/osdefs
6+
import chronicles
67
import ./context
78
import ../[lsquic_ffi, datagram]
89
import ../helpers/[openarray, sequninit, transportaddr]
@@ -71,7 +72,8 @@ proc receive*(
7172
local.toSAddr(localAddress, localAddrLen)
7273
remote.toSAddr(remoteAddress, remoteAddrLen)
7374

74-
discard lsquic_engine_packet_in(
75+
trace "Packet in", bytes = datagram.data.len, local, remote
76+
let rc = lsquic_engine_packet_in(
7577
ctx.engine,
7678
datagram.data.toPtr,
7779
datagram.data.len.csize_t,
@@ -80,6 +82,7 @@ proc receive*(
8082
cast[pointer](ctx),
8183
datagram.ecn,
8284
)
85+
trace "Packet in returned", rc
8386

8487
ctx.processWhenReady()
8588

@@ -88,6 +91,7 @@ proc sendPacketsOut*(
8891
): cint {.cdecl.} =
8992
let quicCtx = cast[QuicContext](ctx)
9093
var sent = 0
94+
trace "Sending packets out", nspecs
9195
let specsArr = cast[ptr UncheckedArray[struct_lsquic_out_spec]](specs)
9296
for i in 0 ..< nspecs.int:
9397
let curr = specsArr[i]
@@ -118,6 +122,7 @@ proc sendPacketsOut*(
118122
nil, # no overlapped
119123
)
120124
if res != 0:
125+
trace "WSASendTo failed", sent, nspecs
121126
break
122127
else:
123128
let msg =
@@ -144,8 +149,10 @@ proc sendPacketsOut*(
144149

145150
let res = sendmsg(SocketHandle(quicCtx.fd), msg.addr, 0)
146151
if res < 0:
152+
trace "sendmsg failed", sent, nspecs
147153
break
148154

149155
sent.inc
150156

157+
trace "Sent packets out", sent, nspecs
151158
sent.cint

lsquic/endpoint.nim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,27 @@ proc receiveDatagram(
7979

8080
var cid: CidKey
8181
if endpoint.packetDcid(data, cid):
82-
if not endpoint.clientContext.isNil and endpoint.clientContext.ownsCid(cid):
82+
let clientOwns =
83+
not endpoint.clientContext.isNil and endpoint.clientContext.ownsCid(cid)
84+
let serverOwns =
85+
not endpoint.serverContext.isNil and endpoint.serverContext.ownsCid(cid)
86+
trace "Routing datagram by CID", bytes = data.len, cid,
87+
clientOwns, serverOwns, local, remote
88+
if clientOwns:
89+
trace "Routing datagram to client context", cid
8390
endpoint.clientContext.receive(Datagram(data: data), local, remote)
8491
return
8592

86-
if not endpoint.serverContext.isNil and endpoint.serverContext.ownsCid(cid):
93+
if serverOwns:
94+
trace "Routing datagram to server context", cid
8795
endpoint.serverContext.receive(Datagram(data: data), local, remote)
8896
return
8997

9098
# Unknown CIDs can be new handshakes; each engine gets a chance to claim them.
9199
# Endpoints can have both contexts; each engine needs to see the packet.
100+
trace "Routing datagram with unknown CID", bytes = data.len,
101+
hasClientContext = not endpoint.clientContext.isNil,
102+
hasServerContext = not endpoint.serverContext.isNil, local, remote
92103
if not endpoint.clientContext.isNil:
93104
endpoint.clientContext.receive(Datagram(data: data), local, remote)
94105
if not endpoint.serverContext.isNil:

0 commit comments

Comments
 (0)