You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
4.9 KiB
Go
184 lines
4.9 KiB
Go
package networkobserve
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
const HardMaxRingCapacity = 262_144
|
|
|
|
func NewGeneration() (string, error) {
|
|
var raw [16]byte
|
|
if _, err := rand.Read(raw[:]); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(raw[:]), nil
|
|
}
|
|
|
|
type Ring struct {
|
|
mu sync.RWMutex
|
|
generation string
|
|
capacity int
|
|
nextCursor uint64
|
|
windows []Window
|
|
latestByVM map[string]Window
|
|
countsByVM map[string]int
|
|
}
|
|
|
|
func NewRing(generation string, capacity int) (*Ring, error) {
|
|
if !generationPattern.MatchString(generation) || capacity <= 0 || capacity > HardMaxRingCapacity {
|
|
return nil, errors.New("invalid network observation ring configuration")
|
|
}
|
|
return &Ring{
|
|
generation: generation, capacity: capacity, windows: make([]Window, 0, capacity),
|
|
latestByVM: make(map[string]Window), countsByVM: make(map[string]int),
|
|
}, nil
|
|
}
|
|
|
|
func (ring *Ring) Generation() string { return ring.generation }
|
|
|
|
func (ring *Ring) Append(windows []Window) error {
|
|
ring.mu.Lock()
|
|
defer ring.mu.Unlock()
|
|
if len(windows) > ring.capacity {
|
|
return errors.New("network observation append exceeds ring capacity")
|
|
}
|
|
prepared := make([]Window, len(windows))
|
|
nextCursor := ring.nextCursor
|
|
for index := range windows {
|
|
window := cloneWindow(windows[index])
|
|
nextCursor++
|
|
if nextCursor == 0 {
|
|
return errors.New("network observation cursor exhausted")
|
|
}
|
|
window.Cursor = nextCursor
|
|
windowID, err := StableWindowID(ring.generation, window.Cursor, window.VMUUID, window.BucketStart)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
window.WindowID = windowID
|
|
prepared[index] = window
|
|
}
|
|
ring.nextCursor = nextCursor
|
|
ring.windows = append(ring.windows, prepared...)
|
|
for _, window := range prepared {
|
|
ring.countsByVM[window.VMUUID]++
|
|
ring.latestByVM[window.VMUUID] = cloneWindow(window)
|
|
}
|
|
if overflow := len(ring.windows) - ring.capacity; overflow > 0 {
|
|
removedLatest := make(map[string]struct{})
|
|
for index := 0; index < overflow; index++ {
|
|
removed := ring.windows[index]
|
|
ring.countsByVM[removed.VMUUID]--
|
|
if ring.countsByVM[removed.VMUUID] == 0 {
|
|
delete(ring.countsByVM, removed.VMUUID)
|
|
delete(ring.latestByVM, removed.VMUUID)
|
|
} else if latest, ok := ring.latestByVM[removed.VMUUID]; ok && latest.Cursor == removed.Cursor {
|
|
removedLatest[removed.VMUUID] = struct{}{}
|
|
}
|
|
}
|
|
copy(ring.windows, ring.windows[overflow:])
|
|
for index := len(ring.windows) - overflow; index < len(ring.windows); index++ {
|
|
ring.windows[index] = Window{}
|
|
}
|
|
ring.windows = ring.windows[:len(ring.windows)-overflow]
|
|
for uuid := range removedLatest {
|
|
for index := len(ring.windows) - 1; index >= 0; index-- {
|
|
if ring.windows[index].VMUUID == uuid {
|
|
ring.latestByVM[uuid] = cloneWindow(ring.windows[index])
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ring *Ring) Batch(capability Capability, after uint64, limit int) (Batch, error) {
|
|
if err := capability.Validate(); err != nil {
|
|
return Batch{}, err
|
|
}
|
|
if limit <= 0 || limit > MaxBatchWindows {
|
|
return Batch{}, errors.New("network observation batch limit is invalid")
|
|
}
|
|
ring.mu.RLock()
|
|
defer ring.mu.RUnlock()
|
|
result := Batch{ContractVersion: ContractVersion, CollectorGeneration: ring.generation, Capability: capability}
|
|
if len(ring.windows) == 0 || !capability.Available {
|
|
return result, result.Validate()
|
|
}
|
|
result.OldestCursor = ring.windows[0].Cursor
|
|
start := 0
|
|
if after > 0 {
|
|
if after < result.OldestCursor-1 {
|
|
result.GapBefore = true
|
|
} else {
|
|
start = sortSearchWindow(ring.windows, after)
|
|
}
|
|
}
|
|
if start >= len(ring.windows) {
|
|
return result, result.Validate()
|
|
}
|
|
end := min(start+limit, len(ring.windows))
|
|
result.Windows = make([]Window, end-start)
|
|
for i := start; i < end; i++ {
|
|
result.Windows[i-start] = cloneWindow(ring.windows[i])
|
|
}
|
|
result.NextCursor = result.Windows[len(result.Windows)-1].Cursor
|
|
result.HasMore = end < len(ring.windows)
|
|
if err := result.Validate(); err != nil {
|
|
return Batch{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func sortSearchWindow(windows []Window, after uint64) int {
|
|
left, right := 0, len(windows)
|
|
for left < right {
|
|
middle := left + (right-left)/2
|
|
if windows[middle].Cursor <= after {
|
|
left = middle + 1
|
|
} else {
|
|
right = middle
|
|
}
|
|
}
|
|
return left
|
|
}
|
|
|
|
func (ring *Ring) LatestForVM(uuid string) (Window, bool) {
|
|
ring.mu.RLock()
|
|
defer ring.mu.RUnlock()
|
|
window, ok := ring.latestByVM[uuid]
|
|
return cloneWindow(window), ok
|
|
}
|
|
|
|
func cloneWindow(value Window) Window {
|
|
value.QualityFlags = slices.Clone(value.QualityFlags)
|
|
if value.Interface != nil {
|
|
copy := cloneInterface(*value.Interface)
|
|
value.Interface = ©
|
|
}
|
|
value.IPv4 = cloneFamily(value.IPv4)
|
|
value.IPv6 = cloneFamily(value.IPv6)
|
|
return value
|
|
}
|
|
|
|
func cloneFamily(value *FamilyObservation) *FamilyObservation {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
result := &FamilyObservation{}
|
|
if value.Inbound != nil {
|
|
copy := *value.Inbound
|
|
result.Inbound = ©
|
|
}
|
|
if value.Outbound != nil {
|
|
copy := *value.Outbound
|
|
result.Outbound = ©
|
|
}
|
|
return result
|
|
}
|