Skip to content

Commit

Permalink
Merge pull request #236 from bio-routing/fix/metricsrace
Browse files Browse the repository at this point in the history
Fix race condition in BGP metrics handling.
  • Loading branch information
hikhvar authored Jan 9, 2020
2 parents f280f19 + 0d6e70a commit 6363f69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 80 deletions.
15 changes: 10 additions & 5 deletions protocols/bgp/server/metrics_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ func metricsForPeer(peer *peer) *metrics.BGPPeerMetrics {
m.UpdatesReceived = fsm.counters.updatesReceived
m.UpdatesSent = fsm.counters.updatesSent

if peer.ipv4 != nil {
m.AddressFamilies = append(m.AddressFamilies, metricsForFamily(fsm.ipv4Unicast))
}
fsm.stateMu.RLock()
defer fsm.stateMu.RUnlock()

if fsm.ribsInitialized {
if peer.ipv4 != nil {
m.AddressFamilies = append(m.AddressFamilies, metricsForFamily(fsm.ipv4Unicast))
}

if peer.ipv6 != nil {
m.AddressFamilies = append(m.AddressFamilies, metricsForFamily(fsm.ipv6Unicast))
if peer.ipv6 != nil {
m.AddressFamilies = append(m.AddressFamilies, metricsForFamily(fsm.ipv6Unicast))
}
}

return m
Expand Down
108 changes: 33 additions & 75 deletions protocols/bgp/server/metrics_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestMetrics(t *testing.T) {
peer *peer
withoutFSM bool
state state
ribsInitialized bool
updatesReceived uint64
updatesSent uint64
ipv4RoutesReceived int64
Expand All @@ -41,6 +42,7 @@ func TestMetrics(t *testing.T) {
vrf: vrf,
},
state: &establishedState{},
ribsInitialized: true,
updatesReceived: 3,
updatesSent: 4,
ipv4RoutesReceived: 5,
Expand Down Expand Up @@ -91,21 +93,12 @@ func TestMetrics(t *testing.T) {
expected: &metrics.BGPMetrics{
Peers: []*metrics.BGPPeerMetrics{
{
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateIdle,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateIdle,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{},
},
},
},
Expand All @@ -124,21 +117,12 @@ func TestMetrics(t *testing.T) {
expected: &metrics.BGPMetrics{
Peers: []*metrics.BGPPeerMetrics{
{
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateActive,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateActive,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{},
},
},
},
Expand All @@ -157,21 +141,12 @@ func TestMetrics(t *testing.T) {
expected: &metrics.BGPMetrics{
Peers: []*metrics.BGPPeerMetrics{
{
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateOpenSent,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateOpenSent,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{},
},
},
},
Expand All @@ -190,21 +165,12 @@ func TestMetrics(t *testing.T) {
expected: &metrics.BGPMetrics{
Peers: []*metrics.BGPPeerMetrics{
{
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateOpenConfirm,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateOpenConfirm,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{},
},
},
},
Expand All @@ -223,21 +189,12 @@ func TestMetrics(t *testing.T) {
expected: &metrics.BGPMetrics{
Peers: []*metrics.BGPPeerMetrics{
{
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateConnect,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{
{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
},
{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
},
},
IP: bnet.IPv4(100).Ptr(),
ASN: 202739,
LocalASN: 201701,
VRF: "inet.0",
State: metrics.StateConnect,
AddressFamilies: []*metrics.BGPAddressFamilyMetrics{},
},
},
},
Expand Down Expand Up @@ -274,6 +231,7 @@ func TestMetrics(t *testing.T) {
test.peer.fsms = append(test.peer.fsms, fsm)

fsm.state = test.state
fsm.ribsInitialized = test.ribsInitialized
fsm.counters.updatesReceived = test.updatesReceived
fsm.counters.updatesSent = test.updatesSent

Expand Down

0 comments on commit 6363f69

Please sign in to comment.