Skip to content
Snippets Groups Projects
Unverified Commit f8b1767b authored by takt's avatar takt Committed by GitHub
Browse files

Cleanup BMP server locking and make VRF id human readable (#276)

parent 5c059836
Branches
Tags v0.0.2-pre5
No related merge requests found
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"github.com/bio-routing/bio-rd/routingtable" "github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter" "github.com/bio-routing/bio-rd/routingtable/filter"
"github.com/bio-routing/bio-rd/routingtable/locRIB" "github.com/bio-routing/bio-rd/routingtable/locRIB"
"github.com/bio-routing/bio-rd/routingtable/vrf"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
...@@ -60,7 +61,7 @@ func (s Server) getRIB(rtr string, vrfID uint64, ipVersion netapi.IP_Version) (* ...@@ -60,7 +61,7 @@ func (s Server) getRIB(rtr string, vrfID uint64, ipVersion netapi.IP_Version) (*
v := r.GetVRF(vrfID) v := r.GetVRF(vrfID)
if v == nil { if v == nil {
return nil, fmt.Errorf("Unable to get VRF %d", vrfID) return nil, fmt.Errorf("Unable to get VRF %s", vrf.RouteDistinguisherHumanReadable(vrfID))
} }
var rib *locRIB.LocRIB var rib *locRIB.LocRIB
......
...@@ -241,7 +241,7 @@ func (r *Router) processPeerDownNotification(msg *bmppkt.PeerDownNotification) { ...@@ -241,7 +241,7 @@ func (r *Router) processPeerDownNotification(msg *bmppkt.PeerDownNotification) {
r.logger.WithFields(log.Fields{ r.logger.WithFields(log.Fields{
"address": r.address.String(), "address": r.address.String(),
"router": r.name, "router": r.name,
"peer_distinguisher": msg.PerPeerHeader.PeerDistinguisher, "peer_distinguisher": vrf.RouteDistinguisherHumanReadable(msg.PerPeerHeader.PeerDistinguisher),
"peer_address": addrToNetIP(msg.PerPeerHeader.PeerAddress).String(), "peer_address": addrToNetIP(msg.PerPeerHeader.PeerAddress).String(),
}).Infof("peer down notification received") }).Infof("peer down notification received")
atomic.AddUint64(&r.counters.peerDownNotificationMessages, 1) atomic.AddUint64(&r.counters.peerDownNotificationMessages, 1)
...@@ -257,7 +257,7 @@ func (r *Router) processPeerUpNotification(msg *bmppkt.PeerUpNotification) error ...@@ -257,7 +257,7 @@ func (r *Router) processPeerUpNotification(msg *bmppkt.PeerUpNotification) error
r.logger.WithFields(log.Fields{ r.logger.WithFields(log.Fields{
"address": r.address.String(), "address": r.address.String(),
"router": r.name, "router": r.name,
"peer_distinguisher": msg.PerPeerHeader.PeerDistinguisher, "peer_distinguisher": vrf.RouteDistinguisherHumanReadable(msg.PerPeerHeader.PeerDistinguisher),
"peer_address": addrToNetIP(msg.PerPeerHeader.PeerAddress).String(), "peer_address": addrToNetIP(msg.PerPeerHeader.PeerAddress).String(),
}).Infof("peer up notification received") }).Infof("peer up notification received")
......
...@@ -26,7 +26,6 @@ type BMPServer struct { ...@@ -26,7 +26,6 @@ type BMPServer struct {
routers map[string]*Router routers map[string]*Router
routersMu sync.RWMutex routersMu sync.RWMutex
ribClients map[string]map[afiClient]struct{} ribClients map[string]map[afiClient]struct{}
gloablMu sync.RWMutex
metrics *bmpMetricsService metrics *bmpMetricsService
} }
...@@ -52,9 +51,6 @@ func conString(host string, port uint16) string { ...@@ -52,9 +51,6 @@ func conString(host string, port uint16) string {
// AddRouter adds a router to which we connect with BMP // AddRouter adds a router to which we connect with BMP
func (b *BMPServer) AddRouter(addr net.IP, port uint16) { func (b *BMPServer) AddRouter(addr net.IP, port uint16) {
b.gloablMu.Lock()
defer b.gloablMu.Unlock()
r := newRouter(addr, port) r := newRouter(addr, port)
b.addRouter(r) b.addRouter(r)
...@@ -116,18 +112,26 @@ func (b *BMPServer) AddRouter(addr net.IP, port uint16) { ...@@ -116,18 +112,26 @@ func (b *BMPServer) AddRouter(addr net.IP, port uint16) {
} }
func (b *BMPServer) addRouter(r *Router) { func (b *BMPServer) addRouter(r *Router) {
b.routersMu.Lock()
defer b.routersMu.Unlock()
b.routers[fmt.Sprintf("%s", r.address.String())] = r b.routers[fmt.Sprintf("%s", r.address.String())] = r
} }
// RemoveRouter removes a BMP monitored router func (b *BMPServer) deleteRouter(addr net.IP) {
func (b *BMPServer) RemoveRouter(addr net.IP, port uint16) { b.routersMu.Lock()
b.gloablMu.Lock() defer b.routersMu.Unlock()
defer b.gloablMu.Unlock()
delete(b.routers, addr.String())
}
// RemoveRouter removes a BMP monitored router
func (b *BMPServer) RemoveRouter(addr net.IP) {
id := addr.String() id := addr.String()
r := b.routers[id] r := b.routers[id]
r.stop <- struct{}{} r.stop <- struct{}{}
delete(b.routers, id)
b.deleteRouter(addr)
} }
func (b *BMPServer) getRouters() []*Router { func (b *BMPServer) getRouters() []*Router {
...@@ -183,12 +187,8 @@ func (b *BMPServer) GetRouter(name string) *Router { ...@@ -183,12 +187,8 @@ func (b *BMPServer) GetRouter(name string) *Router {
b.routersMu.RLock() b.routersMu.RLock()
defer b.routersMu.RUnlock() defer b.routersMu.RUnlock()
for x := range b.routers { if _, ok := b.routers[name]; ok {
if x != name { return b.routers[name]
continue
}
return b.routers[x]
} }
return nil return nil
......
...@@ -137,3 +137,13 @@ func (v *VRF) Dispose() { ...@@ -137,3 +137,13 @@ func (v *VRF) Dispose() {
delete(v.ribNames, ribName) delete(v.ribNames, ribName)
} }
} }
// RouteDistinguisherHumanReadable converts 64bit route distinguisher to human readable string form
func RouteDistinguisherHumanReadable(rdi uint64) string {
asn := rdi >> 32
netIDMask := uint64(0x00000000ffffffff)
netID := rdi & netIDMask
return fmt.Sprintf("%d:%d", asn, netID)
}
...@@ -71,3 +71,27 @@ func TestUnregister(t *testing.T) { ...@@ -71,3 +71,27 @@ func TestUnregister(t *testing.T) {
assert.False(t, found, "vrf must not be in global registry") assert.False(t, found, "vrf must not be in global registry")
} }
func TestRouteDistinguisherHumanReadable(t *testing.T) {
tests := []struct {
name string
rdi uint64
expected string
}{
{
name: "Test #1",
rdi: 0,
expected: "0:0",
},
{
name: "Test #2",
rdi: 220434901565105,
expected: "51324:65201",
},
}
for _, test := range tests {
res := RouteDistinguisherHumanReadable(test.rdi)
assert.Equal(t, test.expected, res, test.name)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment