2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-01-11 20:46:45 +00:00
|
|
|
//
|
|
|
|
// Original implementation (from same author) from which this was derived was:
|
|
|
|
// https://github.com/golang/groupcache/blob/5b532d6fd5efaf7fa130d4e859a2fde0fc3a9e1b/lru/lru.go
|
|
|
|
// ... which was Apache licensed:
|
|
|
|
// https://github.com/golang/groupcache/blob/master/LICENSE
|
|
|
|
|
|
|
|
// Package flowtrack contains types for tracking TCP/UDP flows by 4-tuples.
|
|
|
|
package flowtrack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2021-01-12 04:17:43 +00:00
|
|
|
"fmt"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
"net/netip"
|
2021-01-11 20:46:45 +00:00
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2021-01-11 20:46:45 +00:00
|
|
|
)
|
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
// Tuple is a 5-tuple of proto, source and destination IP and port.
|
2021-01-11 20:46:45 +00:00
|
|
|
type Tuple struct {
|
2022-10-06 03:40:49 +01:00
|
|
|
Proto ipproto.Proto `json:"proto"`
|
|
|
|
Src netip.AddrPort `json:"src"`
|
|
|
|
Dst netip.AddrPort `json:"dst"`
|
2021-01-11 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 04:17:43 +00:00
|
|
|
func (t Tuple) String() string {
|
2021-03-20 04:05:51 +00:00
|
|
|
return fmt.Sprintf("(%v %v => %v)", t.Proto, t.Src, t.Dst)
|
2021-01-12 04:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 20:46:45 +00:00
|
|
|
// Cache is an LRU cache keyed by Tuple.
|
|
|
|
//
|
|
|
|
// The zero value is valid to use.
|
|
|
|
//
|
|
|
|
// It is not safe for concurrent access.
|
2023-01-11 21:45:34 +00:00
|
|
|
type Cache[Value any] struct {
|
2021-01-11 20:46:45 +00:00
|
|
|
// MaxEntries is the maximum number of cache entries before
|
|
|
|
// an item is evicted. Zero means no limit.
|
|
|
|
MaxEntries int
|
|
|
|
|
|
|
|
ll *list.List
|
|
|
|
m map[Tuple]*list.Element // of *entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// entry is the container/list element type.
|
2023-01-11 21:45:34 +00:00
|
|
|
type entry[Value any] struct {
|
2021-01-11 20:46:45 +00:00
|
|
|
key Tuple
|
2023-01-11 21:45:34 +00:00
|
|
|
value Value
|
2021-01-11 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 15:36:48 +01:00
|
|
|
// Add adds a value to the cache, set or updating its associated
|
2021-01-11 20:46:45 +00:00
|
|
|
// value.
|
|
|
|
//
|
|
|
|
// If MaxEntries is non-zero and the length of the cache is greater
|
|
|
|
// after any addition, the least recently used value is evicted.
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) Add(key Tuple, value Value) {
|
2021-01-11 20:46:45 +00:00
|
|
|
if c.m == nil {
|
|
|
|
c.m = make(map[Tuple]*list.Element)
|
|
|
|
c.ll = list.New()
|
|
|
|
}
|
|
|
|
if ee, ok := c.m[key]; ok {
|
|
|
|
c.ll.MoveToFront(ee)
|
2023-01-11 21:45:34 +00:00
|
|
|
ee.Value.(*entry[Value]).value = value
|
2021-01-11 20:46:45 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-11 21:45:34 +00:00
|
|
|
ele := c.ll.PushFront(&entry[Value]{key, value})
|
2021-01-11 20:46:45 +00:00
|
|
|
c.m[key] = ele
|
|
|
|
if c.MaxEntries != 0 && c.Len() > c.MaxEntries {
|
|
|
|
c.RemoveOldest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get looks up a key's value from the cache, also reporting
|
|
|
|
// whether it was present.
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) Get(key Tuple) (value *Value, ok bool) {
|
2021-01-11 20:46:45 +00:00
|
|
|
if ele, hit := c.m[key]; hit {
|
|
|
|
c.ll.MoveToFront(ele)
|
2023-01-11 21:45:34 +00:00
|
|
|
return &ele.Value.(*entry[Value]).value, true
|
2021-01-11 20:46:45 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the provided key from the cache if it was present.
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) Remove(key Tuple) {
|
2021-01-11 20:46:45 +00:00
|
|
|
if ele, hit := c.m[key]; hit {
|
|
|
|
c.removeElement(ele)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOldest removes the oldest item from the cache, if any.
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) RemoveOldest() {
|
2021-01-11 20:46:45 +00:00
|
|
|
if c.ll != nil {
|
|
|
|
if ele := c.ll.Back(); ele != nil {
|
|
|
|
c.removeElement(ele)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) removeElement(e *list.Element) {
|
2021-01-11 20:46:45 +00:00
|
|
|
c.ll.Remove(e)
|
2023-01-11 21:45:34 +00:00
|
|
|
delete(c.m, e.Value.(*entry[Value]).key)
|
2021-01-11 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of items in the cache.
|
2023-01-11 21:45:34 +00:00
|
|
|
func (c *Cache[Value]) Len() int { return len(c.m) }
|