Files
tailscale/net/tstun/fake.go
Alex Valiushko 1eb2bb62f3 net/tstun: implement tun.MultiQueueDevice (#21665)
Wrapper presents one read queue per queue of the underlying device, and
forwards the flow it is given on the write side.

pollVector still multiplexes injected packets onto the device's own
read, which is queue 0. Wrapper.Read is now queues[0].Read and behaves
exactly as it did. Locally-terminated traffic stays on flow 0.

This is a no-op change: the Wrapper presents a single queue and
wireguard-go runs the single reader goroutine it ran before.

Updates tailscale/corp#37878

Change-Id: I844e95cf430075e237ad7e83bdbaa8e43e067ccb
Signed-off-by: Alex Valiushko <[email protected]>
2026-10-07 12:09:16 -07:00

88 lines
2.0 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tstun
import (
"io"
"os"
"github.com/tailscale/wireguard-go/tun"
)
type fakeTUN struct {
evchan chan tun.Event
closechan chan struct{}
queues []tun.Queue
}
// NewFake returns a tun.Device that does nothing.
func NewFake() tun.Device { return newFakeMQ(1) }
// newFakeMQ returns a fake device with n read queues.
func newFakeMQ(n int) *fakeTUN {
if n < 1 {
panic("newFakeMQ() with less than one queue requested")
}
t := &fakeTUN{
evchan: make(chan tun.Event),
closechan: make(chan struct{}),
}
for range n {
t.queues = append(t.queues, &fakeQueue{closechan: t.closechan})
}
return t
}
type fakeQueue struct {
closechan chan struct{}
}
func (q *fakeQueue) File() *os.File {
panic("fakeTUN.File() called, which makes no sense")
}
func (q *fakeQueue) Read(slab []byte, packets []tun.ReadPacket) (int, error) {
<-q.closechan
return 0, io.EOF
}
func (t *fakeTUN) File() *os.File {
return t.queues[0].File()
}
func (t *fakeTUN) Close() error {
close(t.closechan)
close(t.evchan)
return nil
}
func (t *fakeTUN) Read(slab []byte, packets []tun.ReadPacket) (int, error) {
return t.queues[0].Read(slab, packets)
}
func (t *fakeTUN) Queues() []tun.Queue { return t.queues }
func (t *fakeTUN) WriteTo(_ int, b [][]byte, n int) (int, error) {
return t.Write(b, n)
}
func (t *fakeTUN) Write(b [][]byte, n int) (int, error) {
select {
case <-t.closechan:
return 0, ErrClosed
default:
}
return 1, nil
}
// FakeTUNName is the name of the fake TUN device.
const FakeTUNName = "FakeTUN"
func (t *fakeTUN) Flush() error { return nil }
func (t *fakeTUN) MTU() (int, error) { return 1500, nil }
func (t *fakeTUN) Name() (string, error) { return FakeTUNName, nil }
func (t *fakeTUN) Events() <-chan tun.Event { return t.evchan }
func (t *fakeTUN) BatchSize() int { return 1 }
func (t *fakeTUN) IsFakeTun() bool { return true }