net/tstun: use mono.Time

There's a call to Now once per packet.
Move to mono.Now.

Though the current implementation provides high precision,
we document it to be coarse, to preserve the ability
to switch to a coarse monotonic time later.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder 2021-07-21 10:43:53 -07:00 committed by Josh Bleecher Snyder
parent 142670b8c2
commit c2202cc27c
3 changed files with 12 additions and 10 deletions

View File

@ -130,6 +130,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/tailcfg from tailscale.com/control/controlclient+ tailscale.com/tailcfg from tailscale.com/control/controlclient+
W tailscale.com/tsconst from tailscale.com/net/interfaces W tailscale.com/tsconst from tailscale.com/net/interfaces
tailscale.com/tstime from tailscale.com/wgengine/magicsock tailscale.com/tstime from tailscale.com/wgengine/magicsock
💣 tailscale.com/tstime/mono from tailscale.com/net/tstun
tailscale.com/types/empty from tailscale.com/control/controlclient+ tailscale.com/types/empty from tailscale.com/control/controlclient+
tailscale.com/types/flagtype from tailscale.com/cmd/tailscaled tailscale.com/types/flagtype from tailscale.com/cmd/tailscaled
tailscale.com/types/ipproto from tailscale.com/net/flowtrack+ tailscale.com/types/ipproto from tailscale.com/net/flowtrack+

View File

@ -18,6 +18,7 @@
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/net/packet" "tailscale.com/net/packet"
"tailscale.com/tstime/mono"
"tailscale.com/types/ipproto" "tailscale.com/types/ipproto"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/wgengine/filter" "tailscale.com/wgengine/filter"
@ -64,7 +65,7 @@ type Wrapper struct {
closeOnce sync.Once closeOnce sync.Once
lastActivityAtomic int64 // unix seconds of last send or receive lastActivityAtomic mono.Time // time of last send or receive
destIPActivity atomic.Value // of map[netaddr.IP]func() destIPActivity atomic.Value // of map[netaddr.IP]func()
@ -165,6 +166,7 @@ func Wrap(logf logger.Logf, tdev tun.Device) *Wrapper {
go tun.pumpEvents() go tun.pumpEvents()
// The buffer starts out consumed. // The buffer starts out consumed.
tun.bufferConsumed <- struct{}{} tun.bufferConsumed <- struct{}{}
tun.noteActivity()
return tun return tun
} }
@ -364,16 +366,15 @@ func (t *Wrapper) filterOut(p *packet.Parsed) filter.Response {
// noteActivity records that there was a read or write at the current time. // noteActivity records that there was a read or write at the current time.
func (t *Wrapper) noteActivity() { func (t *Wrapper) noteActivity() {
atomic.StoreInt64(&t.lastActivityAtomic, time.Now().Unix()) t.lastActivityAtomic.StoreAtomic(mono.Now())
} }
// IdleDuration reports how long it's been since the last read or write to this device. // IdleDuration reports how long it's been since the last read or write to this device.
// //
// Its value is only accurate to roughly second granularity. // Its value should only be presumed accurate to roughly 10ms granularity.
// If there's never been activity, the duration is since 1970. // If there's never been activity, the duration is since the wrapper was created.
func (t *Wrapper) IdleDuration() time.Duration { func (t *Wrapper) IdleDuration() time.Duration {
sec := atomic.LoadInt64(&t.lastActivityAtomic) return mono.Since(t.lastActivityAtomic.LoadAtomic())
return time.Since(time.Unix(sec, 0))
} }
func (t *Wrapper) Read(buf []byte, offset int) (int, error) { func (t *Wrapper) Read(buf []byte, offset int) (int, error) {

View File

@ -10,13 +10,13 @@
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"testing" "testing"
"unsafe" "unsafe"
"golang.zx2c4.com/wireguard/tun/tuntest" "golang.zx2c4.com/wireguard/tun/tuntest"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/net/packet" "tailscale.com/net/packet"
"tailscale.com/tstime/mono"
"tailscale.com/types/ipproto" "tailscale.com/types/ipproto"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/wgengine/filter" "tailscale.com/wgengine/filter"
@ -335,9 +335,9 @@ func TestFilter(t *testing.T) {
// data was actually filtered. // data was actually filtered.
// If it stays zero, nothing made it through // If it stays zero, nothing made it through
// to the wrapped TUN. // to the wrapped TUN.
atomic.StoreInt64(&tun.lastActivityAtomic, 0) tun.lastActivityAtomic.StoreAtomic(0)
_, err = tun.Write(tt.data, 0) _, err = tun.Write(tt.data, 0)
filtered = atomic.LoadInt64(&tun.lastActivityAtomic) == 0 filtered = tun.lastActivityAtomic.LoadAtomic() == 0
} else { } else {
chtun.Outbound <- tt.data chtun.Outbound <- tt.data
n, err = tun.Read(buf[:], 0) n, err = tun.Read(buf[:], 0)
@ -416,7 +416,7 @@ func TestAtomic64Alignment(t *testing.T) {
} }
c := new(Wrapper) c := new(Wrapper)
atomic.StoreInt64(&c.lastActivityAtomic, 123) c.lastActivityAtomic.StoreAtomic(mono.Now())
} }
func TestPeerAPIBypass(t *testing.T) { func TestPeerAPIBypass(t *testing.T) {