Skip to content
Snippets Groups Projects
Commit fd45a9a4 authored by Oliver Herms's avatar Oliver Herms
Browse files

Making routing table thread safe

parent c68656cf
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,9 @@ func NewRoutingTable() *RoutingTable { ...@@ -20,6 +20,9 @@ func NewRoutingTable() *RoutingTable {
// AddPath adds a path to the routing table // AddPath adds a path to the routing table
func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error {
rt.mu.Lock()
defer rt.mu.Unlock()
if rt.root == nil { if rt.root == nil {
rt.root = newNode(pfx, p, pfx.Pfxlen(), false) rt.root = newNode(pfx, p, pfx.Pfxlen(), false)
return nil return nil
...@@ -31,12 +34,18 @@ func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error { ...@@ -31,12 +34,18 @@ func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error {
// RemovePath removes a path from the trie // RemovePath removes a path from the trie
func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) error { func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) error {
rt.mu.Lock()
defer rt.mu.Unlock()
rt.root.removePath(pfx, p) rt.root.removePath(pfx, p)
return nil return nil
} }
// LPM performs a longest prefix match for pfx on lpm // LPM performs a longest prefix match for pfx on lpm
func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) { func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) {
rt.mu.RLock()
defer rt.mu.RUnlock()
if rt.root == nil { if rt.root == nil {
return nil return nil
} }
...@@ -47,6 +56,9 @@ func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) { ...@@ -47,6 +56,9 @@ func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) {
// Get get's the route for pfx from the LPM // Get get's the route for pfx from the LPM
func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route { func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route {
rt.mu.RLock()
defer rt.mu.RUnlock()
if rt.root == nil { if rt.root == nil {
return nil return nil
} }
...@@ -60,6 +72,9 @@ func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route { ...@@ -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 // GetLonger get's prefix pfx and all it's more specifics from the LPM
func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*route.Route) { func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*route.Route) {
rt.mu.RLock()
defer rt.mu.RUnlock()
if rt.root == nil { if rt.root == nil {
return []*route.Route{} return []*route.Route{}
} }
...@@ -69,6 +84,9 @@ func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*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 // Dump dumps all routes in table rt into a slice
func (rt *RoutingTable) Dump() []*route.Route { func (rt *RoutingTable) Dump() []*route.Route {
rt.mu.RLock()
defer rt.mu.RUnlock()
res := make([]*route.Route, 0) res := make([]*route.Route, 0)
return rt.root.dump(res) return rt.root.dump(res)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment