types/netlogtype: new package for network logging types (#6092)
The netlog.Message type is useful to depend on from other packages,
but doing so would transitively cause gvisor and other large packages
to be linked in.
Avoid this problem by moving all network logging types to a single package.
We also update staticcheck to take in:
003d277bcf
Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@ -9,8 +9,8 @@ package tunstats
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"tailscale.com/net/flowtrack"
|
||||
"tailscale.com/net/packet"
|
||||
"tailscale.com/types/netlogtype"
|
||||
)
|
||||
|
||||
// Statistics maintains counters for every connection.
|
||||
@ -18,36 +18,19 @@ import (
|
||||
// The zero value is ready for use.
|
||||
type Statistics struct {
|
||||
mu sync.Mutex
|
||||
m map[flowtrack.Tuple]Counts
|
||||
}
|
||||
|
||||
// Counts are statistics about a particular connection.
|
||||
type Counts struct {
|
||||
TxPackets uint64 `json:"txPkts,omitempty"`
|
||||
TxBytes uint64 `json:"txBytes,omitempty"`
|
||||
RxPackets uint64 `json:"rxPkts,omitempty"`
|
||||
RxBytes uint64 `json:"rxBytes,omitempty"`
|
||||
}
|
||||
|
||||
// Add adds the counts from both c1 and c2.
|
||||
func (c1 Counts) Add(c2 Counts) Counts {
|
||||
c1.TxPackets += c2.TxPackets
|
||||
c1.TxBytes += c2.TxBytes
|
||||
c1.RxPackets += c2.RxPackets
|
||||
c1.RxBytes += c2.RxBytes
|
||||
return c1
|
||||
m map[netlogtype.Connection]netlogtype.Counts
|
||||
}
|
||||
|
||||
// UpdateTx updates the counters for a transmitted IP packet
|
||||
// The source and destination of the packet directly correspond with
|
||||
// the source and destination in flowtrack.Tuple.
|
||||
// the source and destination in netlogtype.Connection.
|
||||
func (s *Statistics) UpdateTx(b []byte) {
|
||||
s.update(b, false)
|
||||
}
|
||||
|
||||
// UpdateRx 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 flowtrack.Tuple.
|
||||
// the source and destination in netlogtype.Connection.
|
||||
func (s *Statistics) UpdateRx(b []byte) {
|
||||
s.update(b, true)
|
||||
}
|
||||
@ -55,17 +38,17 @@ func (s *Statistics) UpdateRx(b []byte) {
|
||||
func (s *Statistics) update(b []byte, receive bool) {
|
||||
var p packet.Parsed
|
||||
p.Decode(b)
|
||||
tuple := flowtrack.Tuple{Proto: p.IPProto, Src: p.Src, Dst: p.Dst}
|
||||
conn := netlogtype.Connection{Proto: p.IPProto, Src: p.Src, Dst: p.Dst}
|
||||
if receive {
|
||||
tuple.Src, tuple.Dst = tuple.Dst, tuple.Src
|
||||
conn.Src, conn.Dst = conn.Dst, conn.Src
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.m == nil {
|
||||
s.m = make(map[flowtrack.Tuple]Counts)
|
||||
s.m = make(map[netlogtype.Connection]netlogtype.Counts)
|
||||
}
|
||||
cnts := s.m[tuple]
|
||||
cnts := s.m[conn]
|
||||
if receive {
|
||||
cnts.RxPackets++
|
||||
cnts.RxBytes += uint64(len(b))
|
||||
@ -73,15 +56,15 @@ func (s *Statistics) update(b []byte, receive bool) {
|
||||
cnts.TxPackets++
|
||||
cnts.TxBytes += uint64(len(b))
|
||||
}
|
||||
s.m[tuple] = cnts
|
||||
s.m[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() map[flowtrack.Tuple]Counts {
|
||||
func (s *Statistics) Extract() map[netlogtype.Connection]netlogtype.Counts {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
m := s.m
|
||||
s.m = make(map[flowtrack.Tuple]Counts)
|
||||
s.m = make(map[netlogtype.Connection]netlogtype.Counts)
|
||||
return m
|
||||
}
|
||||
|
Reference in New Issue
Block a user