Find a refused packet's tunnel from its header, ask the lighthouse when probing, and address review

This commit is contained in:
JackDoan committed 2026-10-20 14:17:13 -05:00
1 parent 0f07faab0e
commit b69cf4ecb4
11 files changed
+306 -41

No files matched your search

+12 -12
View File
@@ -4,16 +4,20 @@ import "net/netip"
const SendBatchCap = 128
// FailureHeadLen is how much of a refused packet a Failure keeps, nebula's header, which says whose packet it was
const FailureHeadLen = 16
// batchWriter is the minimal subset of udp.Conn needed by SendBatch to flush.
type batchWriter interface {
WriteBatch(bufs [][]byte, addrs []netip.AddrPort, failed func(i int, err error)) (int, error)
}
// Failure is a packet the underlay would not send, with the tag it was committed under
// Failure is a packet the underlay would not send
type Failure struct {
Tag uint32
Dst netip.AddrPort
Err error
// Head is the start of the packet, zero padded if the packet was shorter
Head [FailureHeadLen]byte
Dst netip.AddrPort
Err error
}
// SendBatch accumulates encrypted UDP packets and flushes them via WriteBatch.
@@ -23,7 +27,6 @@ type SendBatch struct {
out batchWriter
bufs [][]byte
dsts []netip.AddrPort
tags []uint32
arena *Arena
failures []Failure
@@ -37,7 +40,6 @@ func NewSendBatch(out batchWriter, batchCap, arenaSize int) *SendBatch {
out: out,
bufs: make([][]byte, 0, batchCap),
dsts: make([]netip.AddrPort, 0, batchCap),
tags: make([]uint32, 0, batchCap),
arena: NewArena(arenaSize),
}
b.recordFailure = b.failed
@@ -45,7 +47,9 @@ func NewSendBatch(out batchWriter, batchCap, arenaSize int) *SendBatch {
}
func (b *SendBatch) failed(i int, err error) {
b.failures = append(b.failures, Failure{Tag: b.tags[i], Dst: b.dsts[i], Err: err})
f := Failure{Dst: b.dsts[i], Err: err}
copy(f.Head[:], b.bufs[i])
b.failures = append(b.failures, f)
}
func (b *SendBatch) Reserve(sz int) []byte {
@@ -57,12 +61,9 @@ func (b *SendBatch) Reserve(sz int) []byte {
// bounding how long the first packet of a large read batch waits.
func (b *SendBatch) Len() int { return len(b.bufs) }
// Commit queues pkt for dst. tag comes back in Failures if the underlay refuses the packet, so the caller can tell
// whose packet it was.
func (b *SendBatch) Commit(pkt []byte, dst netip.AddrPort, tag uint32) {
func (b *SendBatch) Commit(pkt []byte, dst netip.AddrPort) {
b.bufs = append(b.bufs, pkt)
b.dsts = append(b.dsts, dst)
b.tags = append(b.tags, tag)
}
// Flush writes every queued packet and reports how many actually went out. A short count means some destinations
@@ -79,7 +80,6 @@ func (b *SendBatch) Flush() (int, error) {
clear(b.bufs)
b.bufs = b.bufs[:0]
b.dsts = b.dsts[:0]
b.tags = b.tags[:0]
b.arena.Reset()
return written, err
}
+31 -15
View File
@@ -36,7 +36,7 @@ func TestSendBatchReserveCommitFlush(t *testing.T) {
t.Fatalf("slot %d: cap=%d want 32", i, cap(slot))
}
pkt := append(slot[:0], byte(i), byte(i+1), byte(i+2))
b.Commit(pkt, ap, 0)
b.Commit(pkt, ap)
}
if _, err := b.Flush(); err != nil {
t.Fatalf("Flush: %v", err)
@@ -77,7 +77,7 @@ func TestSendBatchSlotsDoNotOverlap(t *testing.T) {
for i := range 3 {
s := b.Reserve(8)
pkt := append(s[:0], byte(0xA0+i), byte(0xB0+i))
b.Commit(pkt, ap, 0)
b.Commit(pkt, ap)
}
if _, err := b.Flush(); err != nil {
t.Fatalf("Flush: %v", err)
@@ -98,11 +98,11 @@ func TestSendBatchGrowPreservesCommitted(t *testing.T) {
s1 := b.Reserve(4)
pkt1 := append(s1[:0], 0x11, 0x22, 0x33, 0x44)
b.Commit(pkt1, ap, 0)
b.Commit(pkt1, ap)
s2 := b.Reserve(8) // exceeds remaining cap, triggers grow
pkt2 := append(s2[:0], 0xA, 0xB, 0xC, 0xD, 0xE)
b.Commit(pkt2, ap, 0)
b.Commit(pkt2, ap)
// pkt1 must still be intact even though backing reallocated.
if pkt1[0] != 0x11 || pkt1[3] != 0x44 {
@@ -140,11 +140,24 @@ func (w *failingBatchWriter) WriteBatch(bufs [][]byte, _ []netip.AddrPort, faile
return written, nil
}
func TestSendBatchReportsFailedTags(t *testing.T) {
func TestSendBatchReportsFailures(t *testing.T) {
errNoRoute := errors.New("no route")
fw := &failingBatchWriter{refuse: map[byte]error{2: errNoRoute, 3: errNoRoute}}
b := NewSendBatch(fw, 4, 128)
b := NewSendBatch(fw, 4, 64)
// pkt is n bytes starting with the refuse key, each later byte its offset plus 0x10 so the head is recognizable
pkt := func(key byte, n int) []byte {
p := b.Reserve(n)[:n]
p[0] = key
for i := 1; i < n; i++ {
p[i] = byte(0x10 + i)
}
return p
}
head := func(p []byte) (h [FailureHeadLen]byte) {
copy(h[:], p)
return h
}
dsts := []netip.AddrPort{
netip.MustParseAddrPort("10.0.0.1:4242"),
@@ -152,10 +165,12 @@ func TestSendBatchReportsFailedTags(t *testing.T) {
netip.MustParseAddrPort("[fd00::3]:4242"),
netip.MustParseAddrPort("10.0.0.4:4242"),
}
for i, dst := range dsts {
pkt := append(b.Reserve(8)[:0], byte(i+1))
b.Commit(pkt, dst, uint32(100+i))
}
long := pkt(2, FailureHeadLen+8) // only its head is kept
short := pkt(3, 4) // zero padded
b.Commit(pkt(1, 20), dsts[0])
b.Commit(long, dsts[1])
b.Commit(short, dsts[2])
b.Commit(pkt(1, 20), dsts[3])
written, err := b.Flush()
if err != nil {
@@ -164,22 +179,23 @@ func TestSendBatchReportsFailedTags(t *testing.T) {
if written != 2 {
t.Errorf("written = %d, want 2", written)
}
want := []Failure{{101, dsts[1], errNoRoute}, {102, dsts[2], errNoRoute}}
want := []Failure{{head(long), dsts[1], errNoRoute}, {head(short), dsts[2], errNoRoute}}
if got := b.Failures(); !slices.Equal(got, want) {
t.Errorf("failures = %v, want %v", got, want)
}
// Tags do not leak into the next flush
b.Commit(append(b.Reserve(8)[:0], 2), dsts[0], 7)
// Failures do not leak into the next flush
again := pkt(2, 20)
b.Commit(again, dsts[0])
if _, err := b.Flush(); err != nil {
t.Fatalf("Flush: %v", err)
}
if want := []Failure{{7, dsts[0], errNoRoute}}; !slices.Equal(b.Failures(), want) {
if want := []Failure{{head(again), dsts[0], errNoRoute}}; !slices.Equal(b.Failures(), want) {
t.Errorf("second flush failures = %v, want %v", b.Failures(), want)
}
// A clean flush forgets the last one's failures
b.Commit(append(b.Reserve(8)[:0], 1), dsts[0], 8)
b.Commit(pkt(1, 20), dsts[0])
if _, err := b.Flush(); err != nil {
t.Fatalf("Flush: %v", err)
}