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

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-03-26 21:47:28 -07:00 committed by Dave Anderson
parent 9ea5cbf81f
commit 2b4bfeda1a
3 changed files with 21 additions and 26 deletions

View File

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

View File

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

View File

@ -131,18 +131,14 @@ func (e *userspaceEngine) GetInternals() (*tstun.TUN, *magicsock.Conn) {
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.
type Config struct {
// TUN is the TUN device used by the engine.
TUN tun.Device
// RouterGen is the function used to instantiate the router.
// If nil, wgengine/router.New is used.
RouterGen RouterGen
// Router is the interface to OS networking APIs used to interface
// the OS with the Engine.
Router router.Router
// LinkMonitor optionally provides an existing link monitor to re-use.
// 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)")
return NewUserspaceEngine(logf, Config{
TUN: tstun.NewFakeTUN(),
RouterGen: router.NewFake,
Router: router.NewFake(logf),
ListenPort: listenPort,
Fake: true,
})
@ -173,13 +169,21 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
if conf.TUN == nil {
return nil, errors.New("TUN is required")
}
if conf.RouterGen == nil {
conf.RouterGen = router.New
}
var closePool closeOnErrorPool
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)
closePool.add(tsTUNDev)
@ -189,6 +193,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
reqCh: make(chan struct{}, 1),
waitCh: make(chan struct{}),
tundev: tsTUNDev,
router: conf.Router,
pingers: make(map[wgkey.Key]*pinger),
}
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)
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() {
up := false
for event := range e.tundev.Events() {