wgengine: flesh out some docs

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/44/head
Brad Fitzpatrick 5 years ago committed by Brad Fitzpatrick
parent 490e586eac
commit 819daf10e2

@ -5,9 +5,10 @@
package wgengine package wgengine
import ( import (
"github.com/tailscale/wireguard-go/tun"
"io" "io"
"os" "os"
"github.com/tailscale/wireguard-go/tun"
) )
type fakeTun struct { type fakeTun struct {
@ -16,6 +17,9 @@ type fakeTun struct {
closechan chan struct{} closechan chan struct{}
} }
// NewFakeTun returns a fake TUN device that does not depend on the
// operating system or any special permissions.
// It primarily exists for testing.
func NewFakeTun() tun.Device { func NewFakeTun() tun.Device {
return &fakeTun{ return &fakeTun{
datachan: make(chan []byte), datachan: make(chan []byte),

@ -11,7 +11,10 @@ import (
"tailscale.com/logger" "tailscale.com/logger"
) )
func RusagePrefixLog(logf logger.Logf) func(f string, argv ...interface{}) { // RusagePrefixLog returns a Logf func wrapping the provided logf func that adds
// a prefixed log message to each line with the current binary memory usage
// and max RSS.
func RusagePrefixLog(logf logger.Logf) logger.Logf {
return func(f string, argv ...interface{}) { return func(f string, argv ...interface{}) {
var m runtime.MemStats var m runtime.MemStats
runtime.ReadMemStats(&m) runtime.ReadMemStats(&m)

@ -23,7 +23,7 @@ func rusageMaxRSS() float64 {
rss /= 1 << 20 // ru_maxrss is bytes on darwin rss /= 1 << 20 // ru_maxrss is bytes on darwin
} else { } else {
// ru_maxrss is kilobytes elsewhere (linux, openbsd, etc) // ru_maxrss is kilobytes elsewhere (linux, openbsd, etc)
rss /= 1024 rss /= 1 << 10
} }
return rss return rss
} }

@ -67,14 +67,14 @@ func NewUserspaceEngine(logf logger.Logf, tunname string, listenPort uint16, der
tuntap, err := tun.CreateTUN(tunname, device.DefaultMTU) tuntap, err := tun.CreateTUN(tunname, device.DefaultMTU)
if err != nil { if err != nil {
log.Printf("CreateTUN: %v\n", err) logf("CreateTUN: %v\n", err)
return nil, err return nil, err
} }
log.Printf("CreateTUN ok.\n") logf("CreateTUN ok.\n")
e, err := NewUserspaceEngineAdvanced(logf, tuntap, NewUserspaceRouter, listenPort, derp) e, err := NewUserspaceEngineAdvanced(logf, tuntap, NewUserspaceRouter, listenPort, derp)
if err != nil { if err != nil {
log.Printf("NewUserspaceEngineAdv: %v\n", err) logf("NewUserspaceEngineAdv: %v\n", err)
return nil, err return nil, err
} }
return e, err return e, err

@ -5,9 +5,9 @@
package wgengine package wgengine
import ( import (
"bytes"
"log" "log"
"runtime/pprof" "runtime/pprof"
"strings"
"time" "time"
"github.com/tailscale/wireguard-go/wgcfg" "github.com/tailscale/wireguard-go/wgcfg"
@ -45,7 +45,7 @@ func (e *watchdogEngine) watchdogErr(name string, fn func() error) error {
t.Stop() t.Stop()
return err return err
case <-t.C: case <-t.C:
buf := new(bytes.Buffer) buf := new(strings.Builder)
pprof.Lookup("goroutine").WriteTo(buf, 1) pprof.Lookup("goroutine").WriteTo(buf, 1)
e.logf("wgengine watchdog stacks:\n%s", buf.String()) e.logf("wgengine watchdog stacks:\n%s", buf.String())
e.fatalf("wgengine: watchdog timeout on %s", name) e.fatalf("wgengine: watchdog timeout on %s", name)

@ -14,6 +14,10 @@ import (
"tailscale.com/wgengine/filter" "tailscale.com/wgengine/filter"
) )
// ByteCount is the number of bytes that have been sent or received.
//
// TODO: why is this a type? remove?
// TODO: document whether it's payload bytes only or if it includes framing overhead.
type ByteCount int64 type ByteCount int64
type PeerStatus struct { type PeerStatus struct {
@ -22,20 +26,29 @@ type PeerStatus struct {
NodeKey tailcfg.NodeKey NodeKey tailcfg.NodeKey
} }
// Status is the Engine status.
type Status struct { type Status struct {
Peers []PeerStatus Peers []PeerStatus
LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint? LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint?
} }
type StatusCallback func(s *Status, err error) // StatusCallback is the type of status callbacks used by
// Engine.SetStatusCallback.
//
// Exactly one of Status or error is non-nil.
type StatusCallback func(*Status, error)
// RouteSettings is the full WireGuard config data (set of peers keys,
// IP, etc in wgcfg.Config) plus the things that WireGuard doesn't do
// itself, like DNS stuff.
type RouteSettings struct { type RouteSettings struct {
LocalAddr wgcfg.CIDR LocalAddr wgcfg.CIDR // TODO: why is this here? how does it differ from wgcfg.Config's info?
DNS []net.IP DNS []net.IP
DNSDomains []string DNSDomains []string
Cfg wgcfg.Config Cfg wgcfg.Config // TODO: value type here, but pointer below?
} }
// OnlyRelevantParts returns a string minimally describing the route settings.
func (rs *RouteSettings) OnlyRelevantParts() string { func (rs *RouteSettings) OnlyRelevantParts() string {
var peers [][]wgcfg.CIDR var peers [][]wgcfg.CIDR
for _, p := range rs.Cfg.Peers { for _, p := range rs.Cfg.Peers {
@ -45,31 +58,55 @@ func (rs *RouteSettings) OnlyRelevantParts() string {
rs.LocalAddr, rs.DNS, rs.DNSDomains, peers) rs.LocalAddr, rs.DNS, rs.DNSDomains, peers)
} }
// Router is the TODO.
type Router interface { type Router interface {
// Up brings the router up.
// TODO: more than once? after Close?
Up() error Up() error
SetRoutes(rs RouteSettings) error // SetRoutes sets the routes.
// TODO: while running?
SetRoutes(RouteSettings) error
// Close closes the router.
// TODO: return an error? does this block?
Close() Close()
} }
// Engine is the Tailscale WireGuard engine interface.
type Engine interface { type Engine interface {
// Reconfigure wireguard and make sure it's running. // Reconfig reconfigures WireGuard and makes sure it's running.
// This also handles setting up any kernel routes. // This also handles setting up any kernel routes.
//
// The provided DNS domains are not part of wgcfg.Config, as
// WireGuard itself doesn't care about such things.
//
// This is called whenever the tailcontrol (control plane)
// sends an updated network map.
Reconfig(cfg *wgcfg.Config, dnsDomains []string) error Reconfig(cfg *wgcfg.Config, dnsDomains []string) error
// Update the packet filter.
SetFilter(filt *filter.Filter) // SetFilter updates the packet filter.
// Set the function to call when wireguard status changes. SetFilter(*filter.Filter)
SetStatusCallback(cb StatusCallback)
// Request a wireguard status update right away, sent to the callback. // SetStatusCallback sets the function to call when the
// WireGuard status changes.
SetStatusCallback(StatusCallback)
// RequestStatus requests a WireGuard status update right
// away, sent to the callback registered via SetStatusCallback.
RequestStatus() RequestStatus()
// Shut down this wireguard instance, remove any routes it added, etc.
// To bring it up again later, you'll need a new Engine. // Close shuts down this wireguard instance, remove any routes
// it added, etc. To bring it up again later, you'll need a
// new Engine.
Close() Close()
// Wait until the Engine is .Close()ed or aborts with an error.
// You don't have to call this. // Wait waits until the Engine's Close method is called or the
// engine aborts with an error. You don't have to call this.
// TODO: return an error?
Wait() Wait()
// LinkChange informs the engine that the system network // LinkChange informs the engine that the system network
// link has changed. The isExpensive parameter is set on links // link has changed. The isExpensive parameter is set on links
// where sending packets uses substantial power or dollars // where sending packets uses substantial power or money,
// (such as LTE on a phone). // such as mobile data on a phone.
LinkChange(isExpensive bool) LinkChange(isExpensive bool)
} }

Loading…
Cancel
Save