wgengine: make Tun optional again, default to fake.

This makes setup more explicit in prod codepaths, without
requiring a bunch of arguments or helpers for tests and
userspace mode.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-03-28 19:25:01 -07:00
parent e18c3a7d84
commit 0fb738760f
3 changed files with 17 additions and 11 deletions

View File

@ -29,7 +29,6 @@
"time"
"github.com/go-multierror/multierror"
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/ipn/ipnserver"
"tailscale.com/logpolicy"
"tailscale.com/net/socks5"
@ -333,15 +332,13 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine.
LinkMonitor: linkMon,
}
isUserspace = name == "userspace-networking"
var dev tun.Device
if isUserspace {
dev = tstun.NewFake()
} else {
dev, err = tstun.New(logf, name)
if !isUserspace {
dev, err := tstun.New(logf, name)
if err != nil {
tstun.Diagnose(logf, name)
return nil, false, err
}
conf.Tun = dev
r, err := router.New(logf, dev)
if err != nil {
dev.Close()
@ -349,7 +346,7 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine.
}
conf.Router = r
}
e, err = wgengine.NewUserspaceEngine(logf, dev, conf)
e, err = wgengine.NewUserspaceEngine(logf, conf)
if err != nil {
return nil, isUserspace, err
}

View File

@ -170,7 +170,8 @@ func startIPNServer(ctx context.Context, logid string) error {
dev.Close()
return nil, err
}
eng, err := wgengine.NewUserspaceEngine(logf, dev, wgengine.Config{
eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{
Tun: dev,
Router: r,
ListenPort: 41641,
})

View File

@ -133,6 +133,11 @@ func (e *userspaceEngine) GetInternals() (*tstun.Wrapper, *magicsock.Conn) {
// Config is the engine configuration.
type Config struct {
// Tun is the device used by the Engine to exchange packets with
// the OS.
// If nil, a fake Device that does nothing is used.
Tun tun.Device
// Router interfaces the Engine to the OS network stack.
// If nil, a fake Router that does nothing is used.
Router router.Router
@ -152,7 +157,7 @@ type Config struct {
func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) {
logf("Starting userspace wireguard engine (with fake TUN device)")
return NewUserspaceEngine(logf, tstun.NewFake(), Config{
return NewUserspaceEngine(logf, Config{
ListenPort: listenPort,
Fake: true,
})
@ -160,15 +165,18 @@ func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error)
// NewUserspaceEngine creates the named tun device and returns a
// Tailscale Engine running on it.
func NewUserspaceEngine(logf logger.Logf, dev tun.Device, conf Config) (_ Engine, reterr error) {
func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) {
var closePool closeOnErrorPool
defer closePool.closeAllIfError(&reterr)
if conf.Tun == nil {
conf.Tun = tstun.NewFake()
}
if conf.Router == nil {
conf.Router = router.NewFake(logf)
}
tsTUNDev := tstun.Wrap(logf, dev)
tsTUNDev := tstun.Wrap(logf, conf.Tun)
closePool.add(tsTUNDev)
e := &userspaceEngine{