2022-10-04 22:10:33 +00:00
|
|
|
// 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 tunstats maintains statistics about connections
|
|
|
|
// flowing through a TUN device (which operate at the IP layer).
|
|
|
|
package tunstats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"tailscale.com/net/packet"
|
2022-10-27 21:14:18 +00:00
|
|
|
"tailscale.com/types/netlogtype"
|
2022-10-04 22:10:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-10-27 21:14:18 +00:00
|
|
|
m map[netlogtype.Connection]netlogtype.Counts
|
2022-10-05 20:18:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 22:10:33 +00:00
|
|
|
// UpdateTx updates the counters for a transmitted IP packet
|
|
|
|
// The source and destination of the packet directly correspond with
|
2022-10-27 21:14:18 +00:00
|
|
|
// the source and destination in netlogtype.Connection.
|
2022-10-04 22:10:33 +00:00
|
|
|
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
|
2022-10-27 21:14:18 +00:00
|
|
|
// the source and destination in netlogtype.Connection.
|
2022-10-04 22:10:33 +00:00
|
|
|
func (s *Statistics) UpdateRx(b []byte) {
|
|
|
|
s.update(b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Statistics) update(b []byte, receive bool) {
|
|
|
|
var p packet.Parsed
|
|
|
|
p.Decode(b)
|
2022-10-27 21:14:18 +00:00
|
|
|
conn := netlogtype.Connection{Proto: p.IPProto, Src: p.Src, Dst: p.Dst}
|
2022-10-04 22:10:33 +00:00
|
|
|
if receive {
|
2022-10-27 21:14:18 +00:00
|
|
|
conn.Src, conn.Dst = conn.Dst, conn.Src
|
2022-10-04 22:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.m == nil {
|
2022-10-27 21:14:18 +00:00
|
|
|
s.m = make(map[netlogtype.Connection]netlogtype.Counts)
|
2022-10-04 22:10:33 +00:00
|
|
|
}
|
2022-10-27 21:14:18 +00:00
|
|
|
cnts := s.m[conn]
|
2022-10-04 22:10:33 +00:00
|
|
|
if receive {
|
|
|
|
cnts.RxPackets++
|
|
|
|
cnts.RxBytes += uint64(len(b))
|
|
|
|
} else {
|
|
|
|
cnts.TxPackets++
|
|
|
|
cnts.TxBytes += uint64(len(b))
|
|
|
|
}
|
2022-10-27 21:14:18 +00:00
|
|
|
s.m[conn] = cnts
|
2022-10-04 22:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract extracts and resets the counters for all active connections.
|
|
|
|
// It must be called periodically otherwise the memory used is unbounded.
|
2022-10-27 21:14:18 +00:00
|
|
|
func (s *Statistics) Extract() map[netlogtype.Connection]netlogtype.Counts {
|
2022-10-04 22:10:33 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
m := s.m
|
2022-10-27 21:14:18 +00:00
|
|
|
s.m = make(map[netlogtype.Connection]netlogtype.Counts)
|
2022-10-04 22:10:33 +00:00
|
|
|
return m
|
|
|
|
}
|