wgengine: pass in an explicit router.Router, rather than a generator.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/1596/head
David Anderson 4 years ago committed by Dave Anderson
parent 9ea5cbf81f
commit 2b4bfeda1a

@ -335,7 +335,7 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine.
isUserspace = name == "userspace-networking" isUserspace = name == "userspace-networking"
if isUserspace { if isUserspace {
conf.TUN = tstun.NewFakeTUN() conf.TUN = tstun.NewFakeTUN()
conf.RouterGen = router.NewFake conf.Router = router.NewFake(logf)
} else { } else {
dev, err := tun.New(logf, name) dev, err := tun.New(logf, name)
if err != nil { if err != nil {

@ -5,14 +5,13 @@
package router package router
import ( import (
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/types/logger" "tailscale.com/types/logger"
) )
// NewFakeRouter returns a Router that does nothing when called and // NewFake returns a Router that does nothing when called and always
// always returns nil errors. // returns nil errors.
func NewFake(logf logger.Logf, _ tun.Device) (Router, error) { func NewFake(logf logger.Logf) Router {
return fakeRouter{logf: logf}, nil return fakeRouter{logf: logf}
} }
type fakeRouter struct { type fakeRouter struct {

@ -131,18 +131,14 @@ func (e *userspaceEngine) GetInternals() (*tstun.TUN, *magicsock.Conn) {
return e.tundev, e.magicConn return e.tundev, e.magicConn
} }
// RouterGen is the signature for a function that creates a
// router.Router.
type RouterGen func(logf logger.Logf, tundev tun.Device) (router.Router, error)
// Config is the engine configuration. // Config is the engine configuration.
type Config struct { type Config struct {
// TUN is the TUN device used by the engine. // TUN is the TUN device used by the engine.
TUN tun.Device TUN tun.Device
// RouterGen is the function used to instantiate the router. // Router is the interface to OS networking APIs used to interface
// If nil, wgengine/router.New is used. // the OS with the Engine.
RouterGen RouterGen Router router.Router
// LinkMonitor optionally provides an existing link monitor to re-use. // LinkMonitor optionally provides an existing link monitor to re-use.
// If nil, a new link monitor is created. // If nil, a new link monitor is created.
@ -161,7 +157,7 @@ func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error)
logf("Starting userspace wireguard engine (with fake TUN device)") logf("Starting userspace wireguard engine (with fake TUN device)")
return NewUserspaceEngine(logf, Config{ return NewUserspaceEngine(logf, Config{
TUN: tstun.NewFakeTUN(), TUN: tstun.NewFakeTUN(),
RouterGen: router.NewFake, Router: router.NewFake(logf),
ListenPort: listenPort, ListenPort: listenPort,
Fake: true, Fake: true,
}) })
@ -173,13 +169,21 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
if conf.TUN == nil { if conf.TUN == nil {
return nil, errors.New("TUN is required") return nil, errors.New("TUN is required")
} }
if conf.RouterGen == nil {
conf.RouterGen = router.New
}
var closePool closeOnErrorPool var closePool closeOnErrorPool
defer closePool.closeAllIfError(&reterr) defer closePool.closeAllIfError(&reterr)
// TODO: default to a no-op router, require caller to pass in
// effectful ones.
if conf.Router == nil {
r, err := router.New(logf, conf.TUN)
if err != nil {
return nil, err
}
conf.Router = r
closePool.add(r)
}
tsTUNDev := tstun.WrapTUN(logf, conf.TUN) tsTUNDev := tstun.WrapTUN(logf, conf.TUN)
closePool.add(tsTUNDev) closePool.add(tsTUNDev)
@ -189,6 +193,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
reqCh: make(chan struct{}, 1), reqCh: make(chan struct{}, 1),
waitCh: make(chan struct{}), waitCh: make(chan struct{}),
tundev: tsTUNDev, tundev: tsTUNDev,
router: conf.Router,
pingers: make(map[wgkey.Key]*pinger), pingers: make(map[wgkey.Key]*pinger),
} }
e.localAddrs.Store(map[netaddr.IP]bool{}) e.localAddrs.Store(map[netaddr.IP]bool{})
@ -326,15 +331,6 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
e.wgdev = device.NewDevice(e.tundev, e.wgLogger.DeviceLogger, opts) e.wgdev = device.NewDevice(e.tundev, e.wgLogger.DeviceLogger, opts)
closePool.addFunc(e.wgdev.Close) closePool.addFunc(e.wgdev.Close)
// Pass the underlying tun.(*NativeDevice) to the router:
// routers do not Read or Write, but do access native interfaces.
e.logf("Creating router...")
e.router, err = conf.RouterGen(logf, e.tundev.Unwrap())
if err != nil {
return nil, err
}
closePool.add(e.router)
go func() { go func() {
up := false up := false
for event := range e.tundev.Events() { for event := range e.tundev.Events() {

Loading…
Cancel
Save