AtlatestRepositorysigil-http3
sigil-http3 / tree / tools / tracetrace.sgl
1
;;; Diagnostic-only module copied into a private QUIC clone for this spike.2
;;; No packet acceptance, cryptography, migration or close policy is changed.3
(define-library (sigil quic spike trace)4
(import (sigil io)5
(sigil time)6
(sigil process)7
(sigil quic bytes)8
(sigil quic address)9
(sigil quic frame)10
(sigil quic error))11
(export spike-trace! spike-ingress! spike-frame! spike-close! spike-hex12
spike-remember-reset! spike-reset-check!)13
(begin14
(define enabled? (equal? (getenv "HTTP3_SPIKE_TRACE") "1"))15
(define origin (current-jiffy))16
(define records 0)17
(define tuples '())18
(define reset-tokens '())20
(define (hex-prefix bytes maximum)21
(if (bytevector? bytes)22
(let ((text (make-string (* 2 (min maximum (bytevector-length bytes))) #\0))23
(digits "0123456789abcdef"))24
(do ((index 0 (+ index 1))) ((= index (quotient (string-length text) 2)) text)25
(let ((byte (bytevector-u8-ref bytes index)))26
(string-set! text (* 2 index) (string-ref digits (quotient byte 16)))27
(string-set! text (+ (* 2 index) 1) (string-ref digits (modulo byte 16))))))28
"unknown"))30
(define (spike-hex bytes) (hex-prefix bytes 20))32
(define (spike-trace! kind fields)33
(when (and enabled? (<= records 8192))34
(let ((now (current-second)))35
(if (= records 8192)36
(println "QUIC-SPIKE utc=~aT~aZ event=trace-exhausted limit=8192"37
(format-date now #t) (format-time now #t))38
(println "QUIC-SPIKE utc=~aT~aZ mono-ms=~a seq=~a event=~a fields=~a"39
(format-date now #t) (format-time now #t)40
(quotient (* 1000 (- (current-jiffy) origin)) (jiffies-per-second))41
records kind fields))42
(set! records (+ records 1)))))44
(define (spike-ingress! bytes host port short-length)45
(when enabled?46
(let* ((tuple (list host port))47
(new? (not (member tuple tuples)))48
(size (bytevector-length bytes))49
(long? (and (> size 0) (>= (bytevector-u8-ref bytes 0) 128)))50
(cid-size (if long?51
(and (>= size 6) (bytevector-u8-ref bytes 5))52
short-length))53
(start (if long? 6 1))54
(dcid (and cid-size (<= 0 cid-size 20) (<= (+ start cid-size) size)55
(bytevector-copy bytes start (+ start cid-size)))))56
(when (and new? (< (length tuples) 32))57
(set! tuples (cons tuple tuples)))58
(spike-trace! 'udp-in59
(list 'host host 'port port 'new-tuple new? 'bytes size60
'header (if long? 'long 'short) 'dcid (hex-prefix dcid 20)61
'short-dcid-length-source (if short-length 'current-local-id 'unknown))))))63
(define (spike-remember-reset! token)64
(when (and enabled? (bytevector? token) (= (bytevector-length token) 16)65
(< (length reset-tokens) 16)66
(not (any (lambda (old) (bytes=? old token)) reset-tokens)))67
(set! reset-tokens (cons (bytevector-copy token) reset-tokens))))69
(define (spike-reset-check! bytes)70
(when (and enabled? (>= (bytevector-length bytes) 21)71
(< (bytevector-u8-ref bytes 0) 128))72
(let ((tail (bytevector-copy bytes (- (bytevector-length bytes) 16))))73
(spike-trace! 'stateless-reset-diagnostic74
(list 'known-peer-tokens (length reset-tokens)75
'known-token-match (any (lambda (token) (bytes=? token tail)) reset-tokens)76
'handling 'diagnostic-only-stack-reset-handling-unsupported)))))78
(define (spike-frame! direction frame address now)79
(when (and enabled? (memv (quic-frame-type frame) '(24 25 26 27 28 29)))80
(let* ((type (quic-frame-type frame))81
(details82
(case type83
((24)84
(when (eq? direction 'rx-decoded)85
(spike-remember-reset! (frame-ref frame 'token)))86
(list 'sequence (frame-ref frame 'sequence)87
'retire-prior-to (frame-ref frame 'retire-prior-to)88
'cid (hex-prefix (frame-ref frame 'connection-id) 20)))89
((25) (list 'sequence (frame-ref frame 'sequence)))90
((26 27) (list 'data (hex-prefix (frame-ref frame 'data) 8)))91
((28 29)92
(list 'error (frame-ref frame 'error)93
'trigger (and (= type 28) (frame-ref frame 'frame-type))94
'reason-hex-prefix (hex-prefix (frame-ref frame 'reason) 48))))))95
(spike-trace! 'frame96
(list 'direction direction 'type type 'connection-ms now97
'host (and address (guard (exn (else 'opaque-address)) (quic-address-host address)))98
'port (and address (guard (exn (else #f)) (quic-address-port address))) 'details details)))))100
(define (spike-close! failure peer? silent? idle? now)101
(spike-trace! 'connection-close102
(list 'connection-ms now 'peer-close peer? 'silent silent? 'idle-expired idle?103
'reason104
(cond105
(idle? 'idle-expiry)106
((quic-error? failure) (list 'transport-error (quic-error-code failure)107
'reason-hex (hex-prefix (string->utf8 (quic-error-reason failure)) 48)))108
((quic-application-error? failure)109
(list 'application-error (quic-application-error-code failure)110
'reason-hex (hex-prefix (string->utf8 (quic-application-error-reason failure)) 48)))111
((quic-frame? failure) (list 'peer-frame (quic-frame-type failure)112
'error (frame-ref failure 'error)))113
((error-object? failure)114
(list 'exception-hex (hex-prefix (string->utf8 (error-object-message failure)) 48)))115
(silent? 'explicit-abort)116
(else 'other)))))118
(spike-trace! 'capabilities119
'(limit 8192 tuple-inventory 32 reset-token-inventory 16120
stateless-reset-send unsupported stateless-reset-receive diagnostic-token-match-only))))