diff --git a/main.go b/main.go index 22cba26fdfb466f2e554538d3f81f864d8c18b79..7220ba4a6c669dd44368ccbc81c16e72c534d0d7 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func main() { go func() { for { - fmt.Print(rib.Print()) + fmt.Printf("LocRIB count: %d\n", rib.Count()) time.Sleep(time.Second * 10) } }() diff --git a/protocols/bgp/server/update_helper.go b/protocols/bgp/server/update_helper.go index 34f784a1de6f22cae74839dbf348865e58472c06..e0a24a6e3bbf1369b59b25d04f1f6c3c16930844 100644 --- a/protocols/bgp/server/update_helper.go +++ b/protocols/bgp/server/update_helper.go @@ -82,7 +82,6 @@ func serializeAndSendUpdate(out io.Writer, update serializeAbleUpdate) error { return nil } - fmt.Printf("Sending Update: %v\n", updateBytes) _, err = out.Write(updateBytes) if err != nil { return fmt.Errorf("Failed sending Update: %v", err) diff --git a/routingtable/adjRIBOut/adj_rib_out.go b/routingtable/adjRIBOut/adj_rib_out.go index b667ac7778ac50b544274970490b7acb4ffffab2..19b44b8bd2595cc550a960ace7c9c00f1fdab21a 100644 --- a/routingtable/adjRIBOut/adj_rib_out.go +++ b/routingtable/adjRIBOut/adj_rib_out.go @@ -72,12 +72,10 @@ func (a *AdjRIBOut) AddPath(pfx bnet.Prefix, p *route.Path) error { a.removePathsFromClients(pfx, oldPaths) } - fmt.Printf("Adding path: %s\n", p.Print()) pathID, err := a.pathIDManager.addPath(p) if err != nil { return fmt.Errorf("Unable to get path ID: %v", err) } - fmt.Printf("New path ID: %d\n", pathID) p.BGPPath.PathIdentifier = pathID a.rt.AddPath(pfx, p) diff --git a/routingtable/locRIB/loc_rib.go b/routingtable/locRIB/loc_rib.go index 90004e273867035b71fef699be7511c1e690b947..bc73817555e146681e347d546099e8972d87d58d 100644 --- a/routingtable/locRIB/loc_rib.go +++ b/routingtable/locRIB/loc_rib.go @@ -27,6 +27,14 @@ func New() *LocRIB { return a } +//Count routes from the LocRIP +func (a *LocRIB) Count() uint64 { + a.mu.RLock() + defer a.mu.RUnlock() + + return uint64(len(a.rt.Dump())) +} + // UpdateNewClient sends current state to a new client func (a *LocRIB) UpdateNewClient(client routingtable.RouteTableClient) error { a.mu.RLock() diff --git a/routingtable/table.go b/routingtable/table.go index fce8a4f1a26ac25ca5b124ec2b17810c698fe3ec..9ccb10161cde2c6f1424c0fbce851a0d1bda19dc 100644 --- a/routingtable/table.go +++ b/routingtable/table.go @@ -2,6 +2,7 @@ package routingtable import ( "sync" + "sync/atomic" "github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/route" @@ -9,8 +10,9 @@ import ( // RoutingTable is a binary trie that stores prefixes and their paths type RoutingTable struct { - root *node - mu sync.RWMutex + routeCount int64 + root *node + mu sync.RWMutex } // NewRoutingTable creates a new routing table @@ -18,6 +20,11 @@ func NewRoutingTable() *RoutingTable { return &RoutingTable{} } +// GetRouteCount gets the amount of stored routes +func (rt *RoutingTable) GetRouteCount() int64 { + return atomic.LoadInt64(&rt.routeCount) +} + // AddPath adds a path to the routing table func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { rt.mu.Lock() @@ -29,6 +36,7 @@ func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { func (rt *RoutingTable) addPath(pfx net.Prefix, p *route.Path) error { if rt.root == nil { rt.root = newNode(pfx, p, pfx.Pfxlen(), false) + atomic.AddInt64(&rt.routeCount, 1) return nil } @@ -55,11 +63,13 @@ func (rt *RoutingTable) ReplacePath(pfx net.Prefix, p *route.Path) []*route.Path } // RemovePath removes a path from the trie -func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) bool { +func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) { rt.mu.Lock() defer rt.mu.Unlock() - return rt.removePath(pfx, p) + if rt.removePath(pfx, p) { + atomic.AddInt64(&rt.routeCount, -1) + } } func (rt *RoutingTable) removePath(pfx net.Prefix, p *route.Path) bool { diff --git a/routingtable/trie.go b/routingtable/trie.go index 730d10155284863b49c475185e34fc9db3ca0798..6fe5f4853cd4bd795c21926f6835ca5e711eeced 100644 --- a/routingtable/trie.go +++ b/routingtable/trie.go @@ -27,7 +27,7 @@ func newNode(pfx net.Prefix, path *route.Path, skip uint8, dummy bool) *node { return n } -func (n *node) removePath(pfx net.Prefix, p *route.Path) (success bool) { +func (n *node) removePath(pfx net.Prefix, p *route.Path) (final bool) { if n == nil { return false } @@ -37,14 +37,13 @@ func (n *node) removePath(pfx net.Prefix, p *route.Path) (success bool) { return } - nPaths := len(n.route.Paths()) nPathsAfterDel := n.route.RemovePath(p) if len(n.route.Paths()) == 0 { // FIXME: Can this node actually be removed from the trie entirely? n.dummy = true } - return nPathsAfterDel < nPaths + return nPathsAfterDel == 0 } b := getBitUint32(pfx.Addr(), n.route.Pfxlen()+1)