diff --git a/routingtable/routingtable/table.go b/routingtable/routingtable/table.go index a75ba5cc78d297280ac782524e15041e952c9e41..6b8bcb6b39200b0710aa3de0899ce12c248ed846 100644 --- a/routingtable/routingtable/table.go +++ b/routingtable/routingtable/table.go @@ -20,6 +20,9 @@ func NewRoutingTable() *RoutingTable { // AddPath adds a path to the routing table func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { + rt.mu.Lock() + defer rt.mu.Unlock() + if rt.root == nil { rt.root = newNode(pfx, p, pfx.Pfxlen(), false) return nil @@ -31,12 +34,18 @@ func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { // RemovePath removes a path from the trie func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) error { + rt.mu.Lock() + defer rt.mu.Unlock() + rt.root.removePath(pfx, p) return nil } // LPM performs a longest prefix match for pfx on lpm func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) { + rt.mu.RLock() + defer rt.mu.RUnlock() + if rt.root == nil { return nil } @@ -47,6 +56,9 @@ func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) { // Get get's the route for pfx from the LPM func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route { + rt.mu.RLock() + defer rt.mu.RUnlock() + if rt.root == nil { return nil } @@ -60,6 +72,9 @@ func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route { // GetLonger get's prefix pfx and all it's more specifics from the LPM func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*route.Route) { + rt.mu.RLock() + defer rt.mu.RUnlock() + if rt.root == nil { return []*route.Route{} } @@ -69,6 +84,9 @@ func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*route.Route) { // Dump dumps all routes in table rt into a slice func (rt *RoutingTable) Dump() []*route.Route { + rt.mu.RLock() + defer rt.mu.RUnlock() + res := make([]*route.Route, 0) return rt.root.dump(res) }