Skip to content

Commit

Permalink
fix data race when getting capabilities from neighbor config
Browse files Browse the repository at this point in the history
Func capabilitiesFromConfig was always taken under the read lock.
However, when graceful restart is enabled for some families, this
function writes to the neighbor config which creates a data race.
  • Loading branch information
hardeker authored and fujita committed Apr 29, 2024
1 parent d0bf813 commit 7ec4af4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/server/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,9 @@ func open2Cap(open *bgp.BGPOpen, n *oc.Neighbor) (map[bgp.BGPCapabilityCode][]bg
func (h *fsmHandler) opensent(ctx context.Context) (bgp.FSMState, *fsmStateReason) {
fsm := h.fsm

fsm.lock.RLock()
fsm.lock.Lock()
m := buildopen(fsm.gConf, fsm.pConf)
fsm.lock.RUnlock()
fsm.lock.Unlock()

b, _ := m.Serialize()
fsm.conn.Write(b)
Expand Down
10 changes: 8 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,11 @@ func (s *BgpServer) toConfig(peer *peer, getAdvertised bool) *oc.Neighbor {

conf.State.RemoteCapabilityList = remoteCap

peer.fsm.lock.RLock()
peer.fsm.lock.Lock()
conf.State.LocalCapabilityList = capabilitiesFromConfig(peer.fsm.pConf)
peer.fsm.lock.Unlock()

peer.fsm.lock.RLock()
conf.State.SessionState = oc.IntToSessionStateMap[int(peer.fsm.state)]
conf.State.AdminState = oc.IntToAdminStateMap[int(peer.fsm.adminState)]
state := peer.fsm.state
Expand Down Expand Up @@ -948,12 +951,15 @@ func newWatchEventPeer(peer *peer, m *fsmMsg, oldState bgp.FSMState, t PeerEvent
var laddr string
var rport, lport uint16

peer.fsm.lock.Lock()
sentOpen := buildopen(peer.fsm.gConf, peer.fsm.pConf)
peer.fsm.lock.Unlock()

peer.fsm.lock.RLock()
if peer.fsm.conn != nil {
_, rport = peer.fsm.RemoteHostPort()
laddr, lport = peer.fsm.LocalHostPort()
}
sentOpen := buildopen(peer.fsm.gConf, peer.fsm.pConf)
recvOpen := peer.fsm.recvOpen
e := &watchEventPeer{
Type: t,
Expand Down

0 comments on commit 7ec4af4

Please sign in to comment.