Skip to content

Commit

Permalink
BMP: Support pre policy monitoring (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
taktv6 authored Jul 26, 2022
1 parent de1f21f commit 6041589
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion protocols/bgp/server/bmp_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func (r *Router) processRouteMonitoringMsg(msg *bmppkt.RouteMonitoringMsg) {
s := n.fsm.state.(*establishedState)
opt := s.fsm.decodeOptions()
opt.Use32BitASN = !msg.PerPeerHeader.GetAFlag()
s.msgReceived(msg.BGPUpdate, opt)

s.msgReceived(msg.BGPUpdate, opt, msg.PerPeerHeader.GetLFlag())
}

func (r *Router) processInitiationMsg(msg *bmppkt.InitiationMessage) {
Expand Down
15 changes: 9 additions & 6 deletions protocols/bgp/server/fsm_address_family.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,30 @@ func (f *fsmAddressFamily) dispose() {
f.initialized = false
}

func (f *fsmAddressFamily) processUpdate(u *packet.BGPUpdate) {
func (f *fsmAddressFamily) processUpdate(u *packet.BGPUpdate, postPolicy bool) {
if f.safi != packet.SAFIUnicast {
return
}

f.multiProtocolUpdates(u)
if f.afi == packet.AFIIPv4 {
f.withdraws(u)
f.updates(u)
f.withdraws(u, postPolicy)
f.updates(u, postPolicy)
}
}

func (f *fsmAddressFamily) withdraws(u *packet.BGPUpdate) {
func (f *fsmAddressFamily) withdraws(u *packet.BGPUpdate, postPolicy bool) {
for r := u.WithdrawnRoutes; r != nil; r = r.Next {
f.adjRIBIn.RemovePath(r.Prefix, nil)
f.adjRIBIn.RemovePath(r.Prefix, &route.Path{
PostPolicy: postPolicy,
})
}
}

func (f *fsmAddressFamily) updates(u *packet.BGPUpdate) {
func (f *fsmAddressFamily) updates(u *packet.BGPUpdate, postPolicy bool) {
for r := u.NLRI; r != nil; r = r.Next {
path := f.newRoutePath()
path.PostPolicy = postPolicy
f.processAttributes(u.PathAttributes, path)

f.adjRIBIn.AddPath(r.Prefix, path)
Expand Down
12 changes: 6 additions & 6 deletions protocols/bgp/server/fsm_established.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s establishedState) run() (state, string) {
case <-time.After(time.Second):
return s.checkHoldtimer()
case recvMsg := <-s.fsm.msgRecvCh:
return s.msgReceived(recvMsg, opt)
return s.msgReceived(recvMsg, opt, false)
}
}
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *establishedState) keepaliveTimerExpired() (state, string) {
return newEstablishedState(s.fsm), s.fsm.reason
}

func (s *establishedState) msgReceived(data []byte, opt *packet.DecodeOptions) (state, string) {
func (s *establishedState) msgReceived(data []byte, opt *packet.DecodeOptions, postPolicy bool) (state, string) {
msg, err := packet.Decode(bytes.NewBuffer(data), opt)
if err != nil {
switch bgperr := err.(type) {
Expand All @@ -179,7 +179,7 @@ func (s *establishedState) msgReceived(data []byte, opt *packet.DecodeOptions) (
fmt.Println(data)
return s.notification()
case packet.UpdateMsg:
return s.update(msg.Body.(*packet.BGPUpdate))
return s.update(msg.Body.(*packet.BGPUpdate), postPolicy)
case packet.KeepaliveMsg:
return s.keepaliveReceived()
default:
Expand All @@ -195,19 +195,19 @@ func (s *establishedState) notification() (state, string) {
return newIdleState(s.fsm), "Received NOTIFICATION"
}

func (s *establishedState) update(u *packet.BGPUpdate) (state, string) {
func (s *establishedState) update(u *packet.BGPUpdate, postPolicy bool) (state, string) {
atomic.AddUint64(&s.fsm.counters.updatesReceived, 1)

if s.fsm.holdTime != 0 {
s.fsm.updateLastUpdateOrKeepalive()
}

if s.fsm.ipv4Unicast != nil {
s.fsm.ipv4Unicast.processUpdate(u)
s.fsm.ipv4Unicast.processUpdate(u, postPolicy)
}

if s.fsm.ipv6Unicast != nil {
s.fsm.ipv6Unicast.processUpdate(u)
s.fsm.ipv6Unicast.processUpdate(u, postPolicy)
}

afi, safi := s.updateAddressFamily(u)
Expand Down
5 changes: 5 additions & 0 deletions protocols/bmp/packet/per_peer_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (p *PerPeerHeader) GetIPVersion() uint8 {
return 4
}

// GetLFlag checks if the L flag is set
func (p *PerPeerHeader) GetLFlag() bool {
return p.PeerFlags&0b01000000 == 0b01000000
}

// GetAFlag checks if the A flag is set
func (p *PerPeerHeader) GetAFlag() bool {
return p.PeerFlags&0b00100000 == 0b00100000
Expand Down
1 change: 1 addition & 0 deletions route/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// Path represents a network path
type Path struct {
Type uint8
PostPolicy bool // PostPolicy fields is a hack used in BMP to differentiate between pre/post policy routes (L flag of the per peer header)
StaticPath *StaticPath
BGPPath *BGPPath
FIBPath *FIBPath
Expand Down

0 comments on commit 6041589

Please sign in to comment.