mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-15 18:06:52 +01:00
overlay/darwin: batch tun writes with sendmsg_x, drain reads, 64 pending packets
tun.WriteBatch writes up to 64 packets per sendmsg_x on the utun control socket, and Passthrough.Flush hands its lane to it whole through the new tio.BatchWriter (Caleb Jasik's #1896). Only the darwin tun implements BatchWriter, so every other platform keeps one Write per packet. tunQueue.Read drains every packet already queued on the utun, up to 64, so a burst reaches the send batch together. xnu lets only one packet wait on a utun control socket by default, so each read found one packet. The tun now raises UTUN_OPT_MAX_PENDING_PACKETS to 64, growing SO_RCVBUF to hold that many packets first; on failure it logs and keeps xnu's default. Tests: #1896's WriteBatch tests over sendmsg_x and the writev fallback, the drain tests, the pending-packet setsockopts, and #1906's framing and zero-alloc Read/Write tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
21075ba32e
commit
81f991f1fd
5 files changed
+726
-15
No files matched your search
@@ -435,3 +435,65 @@ func testPP(pkt []byte) *firewall.ParsedPacket {
|
||||
}
|
||||
return pp
|
||||
}
|
||||
|
||||
// fakeTunBatchWriter records each WriteBatch call and each plain Write separately.
|
||||
type fakeTunBatchWriter struct {
|
||||
writes [][]byte
|
||||
batches [][][]byte
|
||||
}
|
||||
|
||||
func (w *fakeTunBatchWriter) Write(p []byte) (int, error) {
|
||||
w.writes = append(w.writes, bytes.Clone(p))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (w *fakeTunBatchWriter) WriteBatch(pkts [][]byte) error {
|
||||
var b [][]byte
|
||||
for _, p := range pkts {
|
||||
b = append(b, bytes.Clone(p))
|
||||
}
|
||||
w.batches = append(w.batches, b)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestPassthroughUsesWriteBatch pins that a writer implementing tio.BatchWriter gets the whole
|
||||
// passthrough lane in one WriteBatch per Flush, in order, and no plain Writes; an empty Flush
|
||||
// makes no call.
|
||||
func TestPassthroughUsesWriteBatch(t *testing.T) {
|
||||
w := &fakeTunBatchWriter{}
|
||||
m := newTestMultiCoalescer(t, w)
|
||||
k := &keySeq{epoch: 1}
|
||||
|
||||
var want [][]byte
|
||||
for i := range 5 {
|
||||
icmp := make([]byte, 28)
|
||||
icmp[0] = 0x45
|
||||
icmp[3] = 28
|
||||
icmp[9] = 1
|
||||
icmp[20] = byte(i)
|
||||
want = append(want, icmp)
|
||||
if err := m.Commit(icmp, k.next(), testPP(icmp)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := m.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(w.writes) != 0 {
|
||||
t.Fatalf("got %d plain Writes, want 0", len(w.writes))
|
||||
}
|
||||
if len(w.batches) != 1 {
|
||||
t.Fatalf("got %d WriteBatch calls, want 1", len(w.batches))
|
||||
}
|
||||
for i, p := range w.batches[0] {
|
||||
if !bytes.Equal(p, want[i]) {
|
||||
t.Fatalf("packet %d: got % x, want % x", i, p, want[i])
|
||||
}
|
||||
}
|
||||
if len(w.batches[0]) != len(want) {
|
||||
t.Fatalf("got %d packets, want %d", len(w.batches[0]), len(want))
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,23 @@ package batch
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
)
|
||||
|
||||
// Passthrough is MultiCoalescer's verbatim lane: no batching, packets are written at Flush in the
|
||||
// order enqueued.
|
||||
// Passthrough is MultiCoalescer's verbatim lane: no coalescing, packets are written at Flush in the
|
||||
// order enqueued, in one WriteBatch when out implements tio.BatchWriter.
|
||||
type Passthrough struct {
|
||||
out io.Writer
|
||||
bw tio.BatchWriter // nil: Flush writes one packet per Write
|
||||
slots [][]byte
|
||||
}
|
||||
|
||||
func NewPassthrough(w io.Writer) *Passthrough {
|
||||
bw, _ := w.(tio.BatchWriter)
|
||||
return &Passthrough{
|
||||
out: w,
|
||||
bw: bw,
|
||||
slots: make([][]byte, 0, 128),
|
||||
}
|
||||
}
|
||||
@@ -26,10 +31,16 @@ func (p *Passthrough) enqueue(pkt []byte) error {
|
||||
|
||||
func (p *Passthrough) Flush() error {
|
||||
var firstErr error
|
||||
for _, s := range p.slots {
|
||||
_, err := p.out.Write(s)
|
||||
if err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
if p.bw != nil {
|
||||
if len(p.slots) > 0 {
|
||||
firstErr = p.bw.WriteBatch(p.slots)
|
||||
}
|
||||
} else {
|
||||
for _, s := range p.slots {
|
||||
_, err := p.out.Write(s)
|
||||
if err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
}
|
||||
clear(p.slots)
|
||||
|
||||
Reference in new issue
Block a user