mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-15 19:36:52 +01:00
overlay/darwin: batch tun writes with sendmsg_x
Passthrough.Flush hands its whole lane to a new tio.BatchWriter in one WriteBatch call when the queue implements it, and NewSingleQueue exposes WriteBatch when its source does. On darwin, tun.WriteBatch writes up to 64 packets per sendmsg_x on the utun control socket, which is a connected datagram socket, so xnu takes the batched sendit_x path rather than one sendit per message. utun registers no send_list handler, so the kernel still passes each packet to utun on its own: this saves syscalls and fd locking, not per-packet kernel work. Error handling follows what sendmsg_x reports: - A short count with no error (a packet past the send buffer mid-call, or a later send failing with ENOBUFS): the remainder goes out in the next sendmsg_x. - EMSGSIZE: only returned when the first packet is too large, before the kernel takes any, so that packet alone goes through Write, which reports it, and the rest are batched again. - ENOSYS, EPERM, EOPNOTSUPP: sendmsg_x is private API, so WriteBatch logs once and falls back to writev per packet for good. - Anything else: sendmsg_x returns the error without the count it took, so the call is reported rather than retried, which could deliver packets twice. One loss mode has no userspace fix. sendit_x counts each packet as sent once it is copied into an mbuf; if a later allocation fails, it frees the copied prefix unsent, returns its length, and sendmsg_x drops the ENOBUFS because the count is nonzero. That looks exactly like a real short count, so under mbuf exhaustion WriteBatch silently drops packets that writev would have delivered. The iOS tun path (tun_ios.go) is unchanged. Inside a macOS Network Extension, download goes from 640 to 832 Mbps against the recvmsg_x change alone in three paired rounds, at 15% less CPU per Mbps; the likely cause is that its utun charges more per write than a raw one. Raw dnclient already runs download near line rate, and there the CPU difference is within noise. TestTunWriteBatch* round-trip through a connected AF_UNIX datagram socketpair, which takes sendmsg_x the same way, over both the batched and the fallback path: order, AF prefixes, skipped invalid packets, and an oversize packet mid-call and first-in-call. Dropping Namelen from msghdrX fails them. Mutation testing with gremlins leaves only equivalent survivors in WriteBatch, plus which error is reported first when several fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a33a6356c6
commit
61947ff1c2
7 files changed
+436
-16
No files matched your search
+17
-2
@@ -15,9 +15,13 @@ type singleQueue struct {
|
||||
|
||||
// NewSingleQueue wraps a one-datagram-per-Read ReadWriteCloser (a legacy tun device) into a Queue.
|
||||
// bufSize is the per-queue read scratch size and must be at least the largest datagram the source can return.
|
||||
// Close closes rwc.
|
||||
// Close closes rwc. The Queue implements BatchWriter when rwc does.
|
||||
func NewSingleQueue(rwc io.ReadWriteCloser, bufSize int) Queue {
|
||||
return &singleQueue{rw: rwc, closer: rwc, buf: make([]byte, bufSize)}
|
||||
q := &singleQueue{rw: rwc, closer: rwc, buf: make([]byte, bufSize)}
|
||||
if bw, ok := rwc.(BatchWriter); ok {
|
||||
return &batchSingleQueue{singleQueue: q, bw: bw}
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
// NewSingleQueueNoClose is NewSingleQueue for a source owned by someone else,
|
||||
@@ -47,3 +51,14 @@ func (q *singleQueue) Close() error {
|
||||
}
|
||||
return q.closer.Close()
|
||||
}
|
||||
|
||||
// batchSingleQueue is a singleQueue whose source also implements BatchWriter.
|
||||
// It is a separate type so a plain singleQueue never advertises WriteBatch.
|
||||
type batchSingleQueue struct {
|
||||
*singleQueue
|
||||
bw BatchWriter
|
||||
}
|
||||
|
||||
func (q *batchSingleQueue) WriteBatch(pkts [][]byte) error {
|
||||
return q.bw.WriteBatch(pkts)
|
||||
}
|
||||
@@ -145,3 +145,14 @@ func SupportsGSO(w io.Writer, want GSOProto) (GSOWriter, bool) {
|
||||
return gw, false
|
||||
}
|
||||
}
|
||||
|
||||
// BatchWriter is implemented by Queues that can emit several already-segmented packets in one call.
|
||||
// Backends without a batched write do not implement it and callers loop over Write.
|
||||
//
|
||||
// pkts are borrowed: WriteBatch must not retain them past its return.
|
||||
// A returned error doesn't say which packets were delivered, so callers must not retry the batch.
|
||||
// Safe for concurrent use, like Write.
|
||||
type BatchWriter interface {
|
||||
io.Writer
|
||||
WriteBatch(pkts [][]byte) error
|
||||
}
|
||||
Reference in new issue
Block a user