mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-15 14:56:52 +01:00
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>
65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package tio
|
|
|
|
import "io"
|
|
|
|
// singleQueue adapts a legacy one-datagram-per-Read source into a Queue.
|
|
// Read fills a private scratch buffer and returns exactly one Packet whose
|
|
// Bytes borrow from that buffer, valid only until the next Read, per the Queue contract.
|
|
// Single-reader like every Queue; Write is exactly as safe for concurrent use as the underlying source's Write.
|
|
type singleQueue struct {
|
|
rw io.ReadWriter
|
|
closer io.Closer // nil: Close is a no-op (the source is shared and owned elsewhere)
|
|
buf []byte
|
|
ret [1]Packet
|
|
}
|
|
|
|
// 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. The Queue implements BatchWriter when rwc does.
|
|
func NewSingleQueue(rwc io.ReadWriteCloser, bufSize int) Queue {
|
|
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,
|
|
// e.g. several queues sharing one device. Close on the returned Queue is a
|
|
// no-op so one queue can't tear the shared source out from under its
|
|
// siblings; the owner remains responsible for closing the source itself.
|
|
func NewSingleQueueNoClose(rw io.ReadWriter, bufSize int) Queue {
|
|
return &singleQueue{rw: rw, buf: make([]byte, bufSize)}
|
|
}
|
|
|
|
func (q *singleQueue) Read() ([]Packet, error) {
|
|
n, err := q.rw.Read(q.buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q.ret[0] = Packet{Bytes: q.buf[:n]}
|
|
return q.ret[:], nil
|
|
}
|
|
|
|
func (q *singleQueue) Write(p []byte) (int, error) {
|
|
return q.rw.Write(p)
|
|
}
|
|
|
|
func (q *singleQueue) Close() error {
|
|
if q.closer == nil {
|
|
return nil
|
|
}
|
|
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)
|
|
}
|