2021-01-11 20:46:45 +00:00
|
|
|
// Copyright (c) 2021 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.
|
|
|
|
//
|
|
|
|
// 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 04:14:09 +00: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 02:40:49 +00: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.
|
|
|
|
type Cache struct {
|
|
|
|
// 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.
|
|
|
|
type entry struct {
|
|
|
|
key Tuple
|
2022-03-16 23:27:57 +00:00
|
|
|
value any
|
2021-01-11 20:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 14:36:48 +00: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.
|
2022-03-16 23:27:57 +00:00
|
|
|
func (c *Cache) Add(key Tuple, value any) {
|
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)
|
|
|
|
ee.Value.(*entry).value = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ele := c.ll.PushFront(&entry{key, value})
|
|
|
|
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.
|
2022-03-16 23:27:57 +00:00
|
|
|
func (c *Cache) Get(key Tuple) (value any, ok bool) {
|
2021-01-11 20:46:45 +00:00
|
|
|
if ele, hit := c.m[key]; hit {
|
|
|
|
c.ll.MoveToFront(ele)
|
|
|
|
return ele.Value.(*entry).value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the provided key from the cache if it was present.
|
|
|
|
func (c *Cache) Remove(key Tuple) {
|
|
|
|
if ele, hit := c.m[key]; hit {
|
|
|
|
c.removeElement(ele)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOldest removes the oldest item from the cache, if any.
|
|
|
|
func (c *Cache) RemoveOldest() {
|
|
|
|
if c.ll != nil {
|
|
|
|
if ele := c.ll.Back(); ele != nil {
|
|
|
|
c.removeElement(ele)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) removeElement(e *list.Element) {
|
|
|
|
c.ll.Remove(e)
|
|
|
|
delete(c.m, e.Value.(*entry).key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of items in the cache.
|
|
|
|
func (c *Cache) Len() int { return len(c.m) }
|