mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
2e5d08ec4f
Previously, tstun.Wrapper and magicsock.Conn managed their own statistics data structure and relied on an external call to Extract to extract (and reset) the statistics. This makes it difficult to ensure a maximum size on the statistics as the caller has no introspection into whether the number of unique connections is getting too large. Invert the control flow such that a *connstats.Statistics is registered with tstun.Wrapper and magicsock.Conn. Methods on non-nil *connstats.Statistics are called for every packet. This allows the implementation of connstats.Statistics (in the future) to better control when it needs to flush to ensure bounds on maximum sizes. The value registered into tstun.Wrapper and magicsock.Conn could be an interface, but that has two performance detriments: 1. Method calls on interface values are more expensive since they must go through a virtual method dispatch. 2. The implementation would need a sync.Mutex to protect the statistics value instead of using an atomic.Pointer. Given that methods on constats.Statistics are called for every packet, we want reduce the CPU cost on this hot path. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Package connstats maintains statistics about connections
|
|
// flowing through a TUN device (which operate at the IP layer).
|
|
package connstats
|
|
|
|
import (
|
|
"net/netip"
|
|
"sync"
|
|
|
|
"tailscale.com/net/packet"
|
|
"tailscale.com/types/netlogtype"
|
|
)
|
|
|
|
// Statistics maintains counters for every connection.
|
|
// All methods are safe for concurrent use.
|
|
// The zero value is ready for use.
|
|
type Statistics struct {
|
|
mu sync.Mutex
|
|
virtual map[netlogtype.Connection]netlogtype.Counts
|
|
physical map[netlogtype.Connection]netlogtype.Counts
|
|
}
|
|
|
|
// UpdateTxVirtual updates the counters for a transmitted IP packet
|
|
// The source and destination of the packet directly correspond with
|
|
// the source and destination in netlogtype.Connection.
|
|
func (s *Statistics) UpdateTxVirtual(b []byte) {
|
|
s.updateVirtual(b, false)
|
|
}
|
|
|
|
// UpdateRxVirtual updates the counters for a received IP packet.
|
|
// The source and destination of the packet are inverted with respect to
|
|
// the source and destination in netlogtype.Connection.
|
|
func (s *Statistics) UpdateRxVirtual(b []byte) {
|
|
s.updateVirtual(b, true)
|
|
}
|
|
|
|
func (s *Statistics) updateVirtual(b []byte, receive bool) {
|
|
var p packet.Parsed
|
|
p.Decode(b)
|
|
conn := netlogtype.Connection{Proto: p.IPProto, Src: p.Src, Dst: p.Dst}
|
|
if receive {
|
|
conn.Src, conn.Dst = conn.Dst, conn.Src
|
|
}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.virtual == nil {
|
|
s.virtual = make(map[netlogtype.Connection]netlogtype.Counts)
|
|
}
|
|
cnts := s.virtual[conn]
|
|
if receive {
|
|
cnts.RxPackets++
|
|
cnts.RxBytes += uint64(len(b))
|
|
} else {
|
|
cnts.TxPackets++
|
|
cnts.TxBytes += uint64(len(b))
|
|
}
|
|
s.virtual[conn] = cnts
|
|
}
|
|
|
|
// UpdateTxPhysical updates the counters for a transmitted wireguard packet
|
|
// The src is always a Tailscale IP address, representing some remote peer.
|
|
// The dst is a remote IP address and port that corresponds
|
|
// with some physical peer backing the Tailscale IP address.
|
|
func (s *Statistics) UpdateTxPhysical(src netip.Addr, dst netip.AddrPort, n int) {
|
|
s.updatePhysical(src, dst, n, false)
|
|
}
|
|
|
|
// UpdateRxPhysical updates the counters for a received wireguard packet.
|
|
// The src is always a Tailscale IP address, representing some remote peer.
|
|
// The dst is a remote IP address and port that corresponds
|
|
// with some physical peer backing the Tailscale IP address.
|
|
func (s *Statistics) UpdateRxPhysical(src netip.Addr, dst netip.AddrPort, n int) {
|
|
s.updatePhysical(src, dst, n, true)
|
|
}
|
|
|
|
func (s *Statistics) updatePhysical(src netip.Addr, dst netip.AddrPort, n int, receive bool) {
|
|
conn := netlogtype.Connection{Src: netip.AddrPortFrom(src, 0), Dst: dst}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.physical == nil {
|
|
s.physical = make(map[netlogtype.Connection]netlogtype.Counts)
|
|
}
|
|
cnts := s.physical[conn]
|
|
if receive {
|
|
cnts.RxPackets++
|
|
cnts.RxBytes += uint64(n)
|
|
} else {
|
|
cnts.TxPackets++
|
|
cnts.TxBytes += uint64(n)
|
|
}
|
|
s.physical[conn] = cnts
|
|
}
|
|
|
|
// Extract extracts and resets the counters for all active connections.
|
|
// It must be called periodically otherwise the memory used is unbounded.
|
|
func (s *Statistics) Extract() (virtual, physical map[netlogtype.Connection]netlogtype.Counts) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
virtual = s.virtual
|
|
s.virtual = make(map[netlogtype.Connection]netlogtype.Counts)
|
|
physical = s.physical
|
|
s.physical = make(map[netlogtype.Connection]netlogtype.Counts)
|
|
return virtual, physical
|
|
}
|