Add ASN info lookup and periodic routing table statistics
- Add handle and description columns to asns table - Look up ASN info using asinfo package when creating new ASNs - Remove noisy debug logging for individual route updates - Add IPv4/IPv6 route counters and update rate tracking - Log routing table statistics every 15 seconds with IPv4/IPv6 breakdown - Track updates per second for both IPv4 and IPv6 routes separately
This commit is contained in:
@@ -3,7 +3,9 @@ package routingtable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -37,15 +39,23 @@ type RoutingTable struct {
|
||||
byPrefix map[uuid.UUID]map[RouteKey]*Route // Routes indexed by prefix ID
|
||||
byOriginASN map[uuid.UUID]map[RouteKey]*Route // Routes indexed by origin ASN ID
|
||||
byPeerASN map[int]map[RouteKey]*Route // Routes indexed by peer ASN
|
||||
|
||||
// Metrics tracking
|
||||
ipv4Routes int
|
||||
ipv6Routes int
|
||||
ipv4Updates uint64 // Updates counter for rate calculation
|
||||
ipv6Updates uint64 // Updates counter for rate calculation
|
||||
lastMetricsReset time.Time
|
||||
}
|
||||
|
||||
// New creates a new empty routing table
|
||||
func New() *RoutingTable {
|
||||
return &RoutingTable{
|
||||
routes: make(map[RouteKey]*Route),
|
||||
byPrefix: make(map[uuid.UUID]map[RouteKey]*Route),
|
||||
byOriginASN: make(map[uuid.UUID]map[RouteKey]*Route),
|
||||
byPeerASN: make(map[int]map[RouteKey]*Route),
|
||||
routes: make(map[RouteKey]*Route),
|
||||
byPrefix: make(map[uuid.UUID]map[RouteKey]*Route),
|
||||
byOriginASN: make(map[uuid.UUID]map[RouteKey]*Route),
|
||||
byPeerASN: make(map[int]map[RouteKey]*Route),
|
||||
lastMetricsReset: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +73,12 @@ func (rt *RoutingTable) AddRoute(route *Route) {
|
||||
// If route already exists, remove it from indexes first
|
||||
if existingRoute, exists := rt.routes[key]; exists {
|
||||
rt.removeFromIndexes(key, existingRoute)
|
||||
// Decrement counter for existing route
|
||||
if isIPv6(existingRoute.Prefix) {
|
||||
rt.ipv6Routes--
|
||||
} else {
|
||||
rt.ipv4Routes--
|
||||
}
|
||||
}
|
||||
|
||||
// Add to main map
|
||||
@@ -70,6 +86,15 @@ func (rt *RoutingTable) AddRoute(route *Route) {
|
||||
|
||||
// Update indexes
|
||||
rt.addToIndexes(key, route)
|
||||
|
||||
// Update metrics
|
||||
if isIPv6(route.Prefix) {
|
||||
rt.ipv6Routes++
|
||||
atomic.AddUint64(&rt.ipv6Updates, 1)
|
||||
} else {
|
||||
rt.ipv4Routes++
|
||||
atomic.AddUint64(&rt.ipv4Updates, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveRoute removes a route from the routing table
|
||||
@@ -94,6 +119,15 @@ func (rt *RoutingTable) RemoveRoute(prefixID, originASNID uuid.UUID, peerASN int
|
||||
// Remove from main map
|
||||
delete(rt.routes, key)
|
||||
|
||||
// Update metrics
|
||||
if isIPv6(route.Prefix) {
|
||||
rt.ipv6Routes--
|
||||
atomic.AddUint64(&rt.ipv6Updates, 1)
|
||||
} else {
|
||||
rt.ipv4Routes--
|
||||
atomic.AddUint64(&rt.ipv4Updates, 1)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -102,25 +136,38 @@ func (rt *RoutingTable) WithdrawRoutesByPrefixAndPeer(prefixID uuid.UUID, peerAS
|
||||
rt.mu.Lock()
|
||||
defer rt.mu.Unlock()
|
||||
|
||||
count := 0
|
||||
prefixRoutes, exists := rt.byPrefix[prefixID]
|
||||
if !exists {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Find all routes for this prefix
|
||||
if prefixRoutes, exists := rt.byPrefix[prefixID]; exists {
|
||||
// Collect keys to delete (can't delete while iterating)
|
||||
var keysToDelete []RouteKey
|
||||
for key, route := range prefixRoutes {
|
||||
if route.PeerASN == peerASN {
|
||||
keysToDelete = append(keysToDelete, key)
|
||||
}
|
||||
// Collect keys to delete (can't delete while iterating)
|
||||
var keysToDelete []RouteKey
|
||||
for key, route := range prefixRoutes {
|
||||
if route.PeerASN == peerASN {
|
||||
keysToDelete = append(keysToDelete, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the routes
|
||||
count := 0
|
||||
for _, key := range keysToDelete {
|
||||
route, exists := rt.routes[key]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
// Delete the routes
|
||||
for _, key := range keysToDelete {
|
||||
if route, exists := rt.routes[key]; exists {
|
||||
rt.removeFromIndexes(key, route)
|
||||
delete(rt.routes, key)
|
||||
count++
|
||||
}
|
||||
rt.removeFromIndexes(key, route)
|
||||
delete(rt.routes, key)
|
||||
count++
|
||||
|
||||
// Update metrics
|
||||
if isIPv6(route.Prefix) {
|
||||
rt.ipv6Routes--
|
||||
atomic.AddUint64(&rt.ipv6Updates, 1)
|
||||
} else {
|
||||
rt.ipv4Routes--
|
||||
atomic.AddUint64(&rt.ipv4Updates, 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +281,47 @@ func (rt *RoutingTable) Stats() map[string]int {
|
||||
return stats
|
||||
}
|
||||
|
||||
// DetailedStats contains detailed routing table statistics
|
||||
type DetailedStats struct {
|
||||
IPv4Routes int
|
||||
IPv6Routes int
|
||||
IPv4UpdatesRate float64
|
||||
IPv6UpdatesRate float64
|
||||
TotalRoutes int
|
||||
UniquePrefixes int
|
||||
UniqueOrigins int
|
||||
UniquePeers int
|
||||
}
|
||||
|
||||
// GetDetailedStats returns detailed statistics including IPv4/IPv6 breakdown and update rates
|
||||
func (rt *RoutingTable) GetDetailedStats() DetailedStats {
|
||||
rt.mu.Lock()
|
||||
defer rt.mu.Unlock()
|
||||
|
||||
// Calculate update rates
|
||||
elapsed := time.Since(rt.lastMetricsReset).Seconds()
|
||||
ipv4Updates := atomic.LoadUint64(&rt.ipv4Updates)
|
||||
ipv6Updates := atomic.LoadUint64(&rt.ipv6Updates)
|
||||
|
||||
stats := DetailedStats{
|
||||
IPv4Routes: rt.ipv4Routes,
|
||||
IPv6Routes: rt.ipv6Routes,
|
||||
IPv4UpdatesRate: float64(ipv4Updates) / elapsed,
|
||||
IPv6UpdatesRate: float64(ipv6Updates) / elapsed,
|
||||
TotalRoutes: len(rt.routes),
|
||||
UniquePrefixes: len(rt.byPrefix),
|
||||
UniqueOrigins: len(rt.byOriginASN),
|
||||
UniquePeers: len(rt.byPeerASN),
|
||||
}
|
||||
|
||||
// Reset counters for next period
|
||||
atomic.StoreUint64(&rt.ipv4Updates, 0)
|
||||
atomic.StoreUint64(&rt.ipv6Updates, 0)
|
||||
rt.lastMetricsReset = time.Now()
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
// Clear removes all routes from the routing table
|
||||
func (rt *RoutingTable) Clear() {
|
||||
rt.mu.Lock()
|
||||
@@ -243,6 +331,11 @@ func (rt *RoutingTable) Clear() {
|
||||
rt.byPrefix = make(map[uuid.UUID]map[RouteKey]*Route)
|
||||
rt.byOriginASN = make(map[uuid.UUID]map[RouteKey]*Route)
|
||||
rt.byPeerASN = make(map[int]map[RouteKey]*Route)
|
||||
rt.ipv4Routes = 0
|
||||
rt.ipv6Routes = 0
|
||||
atomic.StoreUint64(&rt.ipv4Updates, 0)
|
||||
atomic.StoreUint64(&rt.ipv6Updates, 0)
|
||||
rt.lastMetricsReset = time.Now()
|
||||
}
|
||||
|
||||
// Helper methods for index management
|
||||
@@ -297,3 +390,8 @@ func (rt *RoutingTable) removeFromIndexes(key RouteKey, route *Route) {
|
||||
func (k RouteKey) String() string {
|
||||
return fmt.Sprintf("%s/%s/%d", k.PrefixID, k.OriginASNID, k.PeerASN)
|
||||
}
|
||||
|
||||
// isIPv6 returns true if the prefix is an IPv6 address
|
||||
func isIPv6(prefix string) bool {
|
||||
return strings.Contains(prefix, ":")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user