diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/server/fsm.go | 8 | ||||
-rw-r--r-- | pkg/server/fsm_test.go | 4 | ||||
-rw-r--r-- | pkg/server/peer.go | 88 | ||||
-rw-r--r-- | pkg/server/server.go | 62 | ||||
-rw-r--r-- | pkg/server/server_test.go | 29 |
5 files changed, 97 insertions, 94 deletions
diff --git a/pkg/server/fsm.go b/pkg/server/fsm.go index 4830feb2..6a8fd942 100644 --- a/pkg/server/fsm.go +++ b/pkg/server/fsm.go @@ -408,8 +408,8 @@ func (fsm *FSM) connectLoop() error { fsm.lock.RLock() tick := int(fsm.pConf.Timers.Config.ConnectRetry) fsm.lock.RUnlock() - if tick < MIN_CONNECT_RETRY { - tick = MIN_CONNECT_RETRY + if tick < minConnectRetry { + tick = minConnectRetry } r := rand.New(rand.NewSource(time.Now().UnixNano())) @@ -438,7 +438,7 @@ func (fsm *FSM) connectLoop() error { d := tcpDialer{ Dialer: net.Dialer{ LocalAddr: laddr, - Timeout: time.Duration(MIN_CONNECT_RETRY-1) * time.Second, + Timeout: time.Duration(minConnectRetry-1) * time.Second, }, AuthPassword: fsm.pConf.Config.AuthPassword, } @@ -493,7 +493,7 @@ func (fsm *FSM) connectLoop() error { go connect() } case <-fsm.getActiveCh: - timer.Reset(time.Duration(r.Intn(MIN_CONNECT_RETRY)+MIN_CONNECT_RETRY) * time.Second) + timer.Reset(time.Duration(r.Intn(minConnectRetry)+minConnectRetry) * time.Second) } } } diff --git a/pkg/server/fsm_test.go b/pkg/server/fsm_test.go index dad6563a..e7daa9b6 100644 --- a/pkg/server/fsm_test.go +++ b/pkg/server/fsm_test.go @@ -308,8 +308,8 @@ func TestCheckOwnASLoop(t *testing.T) { assert.False(hasOwnASLoop(65200, 0, aspath)) } -func makePeerAndHandler() (*Peer, *FSMHandler) { - p := &Peer{ +func makePeerAndHandler() (*peer, *FSMHandler) { + p := &peer{ fsm: NewFSM(&config.Global{}, &config.Neighbor{}, table.NewRoutingPolicy()), outgoing: channels.NewInfiniteChannel(), } diff --git a/pkg/server/peer.go b/pkg/server/peer.go index 632e14a4..28bf0fee 100644 --- a/pkg/server/peer.go +++ b/pkg/server/peer.go @@ -29,37 +29,37 @@ import ( ) const ( - FLOP_THRESHOLD = time.Second * 30 - MIN_CONNECT_RETRY = 10 + flopThreshold = time.Second * 30 + minConnectRetry = 10 ) -type PeerGroup struct { +type peerGroup struct { Conf *config.PeerGroup members map[string]config.Neighbor dynamicNeighbors map[string]*config.DynamicNeighbor } -func NewPeerGroup(c *config.PeerGroup) *PeerGroup { - return &PeerGroup{ +func newPeerGroup(c *config.PeerGroup) *peerGroup { + return &peerGroup{ Conf: c, members: make(map[string]config.Neighbor), dynamicNeighbors: make(map[string]*config.DynamicNeighbor), } } -func (pg *PeerGroup) AddMember(c config.Neighbor) { +func (pg *peerGroup) AddMember(c config.Neighbor) { pg.members[c.State.NeighborAddress] = c } -func (pg *PeerGroup) DeleteMember(c config.Neighbor) { +func (pg *peerGroup) DeleteMember(c config.Neighbor) { delete(pg.members, c.State.NeighborAddress) } -func (pg *PeerGroup) AddDynamicNeighbor(c *config.DynamicNeighbor) { +func (pg *peerGroup) AddDynamicNeighbor(c *config.DynamicNeighbor) { pg.dynamicNeighbors[c.Config.Prefix] = c } -func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGroup, loc *table.TableManager, policy *table.RoutingPolicy) *Peer { +func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGroup, loc *table.TableManager, policy *table.RoutingPolicy) *peer { conf := config.Neighbor{ Config: config.NeighborConfig{ PeerGroup: pg.Config.PeerGroupName, @@ -87,14 +87,14 @@ func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGro }).Debugf("Can't set default config: %s", err) return nil } - peer := NewPeer(g, &conf, loc, policy) + peer := newPeer(g, &conf, loc, policy) peer.fsm.lock.Lock() peer.fsm.state = bgp.BGP_FSM_ACTIVE peer.fsm.lock.Unlock() return peer } -type Peer struct { +type peer struct { tableId string fsm *FSM adjRibIn *table.AdjRib @@ -105,8 +105,8 @@ type Peer struct { llgrEndChs []chan struct{} } -func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, policy *table.RoutingPolicy) *Peer { - peer := &Peer{ +func newPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, policy *table.RoutingPolicy) *peer { + peer := &peer{ outgoing: channels.NewInfiniteChannel(), localRib: loc, policy: policy, @@ -123,45 +123,45 @@ func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, p return peer } -func (peer *Peer) AS() uint32 { +func (peer *peer) AS() uint32 { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.State.PeerAs } -func (peer *Peer) ID() string { +func (peer *peer) ID() string { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.State.NeighborAddress } -func (peer *Peer) TableID() string { +func (peer *peer) TableID() string { return peer.tableId } -func (peer *Peer) isIBGPPeer() bool { +func (peer *peer) isIBGPPeer() bool { return peer.fsm.pConf.State.PeerAs == peer.fsm.gConf.Config.As } -func (peer *Peer) isRouteServerClient() bool { +func (peer *peer) isRouteServerClient() bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.RouteServer.Config.RouteServerClient } -func (peer *Peer) isRouteReflectorClient() bool { +func (peer *peer) isRouteReflectorClient() bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.RouteReflector.Config.RouteReflectorClient } -func (peer *Peer) isGracefulRestartEnabled() bool { +func (peer *peer) isGracefulRestartEnabled() bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.GracefulRestart.State.Enabled } -func (peer *Peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode { +func (peer *peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() if mode, y := peer.fsm.rfMap[family]; y { @@ -170,21 +170,21 @@ func (peer *Peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode { return bgp.BGP_ADD_PATH_NONE } -func (peer *Peer) isAddPathReceiveEnabled(family bgp.RouteFamily) bool { +func (peer *peer) isAddPathReceiveEnabled(family bgp.RouteFamily) bool { return (peer.getAddPathMode(family) & bgp.BGP_ADD_PATH_RECEIVE) > 0 } -func (peer *Peer) isAddPathSendEnabled(family bgp.RouteFamily) bool { +func (peer *peer) isAddPathSendEnabled(family bgp.RouteFamily) bool { return (peer.getAddPathMode(family) & bgp.BGP_ADD_PATH_SEND) > 0 } -func (peer *Peer) isDynamicNeighbor() bool { +func (peer *peer) isDynamicNeighbor() bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() return peer.fsm.pConf.Config.NeighborAddress == "" && peer.fsm.pConf.Config.NeighborInterface == "" } -func (peer *Peer) recvedAllEOR() bool { +func (peer *peer) recvedAllEOR() bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() for _, a := range peer.fsm.pConf.AfiSafis { @@ -195,14 +195,14 @@ func (peer *Peer) recvedAllEOR() bool { return true } -func (peer *Peer) configuredRFlist() []bgp.RouteFamily { +func (peer *peer) configuredRFlist() []bgp.RouteFamily { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() rfs, _ := config.AfiSafis(peer.fsm.pConf.AfiSafis).ToRfList() return rfs } -func (peer *Peer) negotiatedRFList() []bgp.RouteFamily { +func (peer *peer) negotiatedRFList() []bgp.RouteFamily { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() l := make([]bgp.RouteFamily, 0, len(peer.fsm.rfMap)) @@ -212,7 +212,7 @@ func (peer *Peer) negotiatedRFList() []bgp.RouteFamily { return l } -func (peer *Peer) toGlobalFamilies(families []bgp.RouteFamily) []bgp.RouteFamily { +func (peer *peer) toGlobalFamilies(families []bgp.RouteFamily) []bgp.RouteFamily { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() if peer.fsm.pConf.Config.Vrf != "" { @@ -256,7 +256,7 @@ func classifyFamilies(all, part []bgp.RouteFamily) ([]bgp.RouteFamily, []bgp.Rou return a, b } -func (peer *Peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) { +func (peer *peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) { peer.fsm.lock.RLock() list := []bgp.RouteFamily{} for _, a := range peer.fsm.pConf.AfiSafis { @@ -268,7 +268,7 @@ func (peer *Peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteF return classifyFamilies(peer.configuredRFlist(), list) } -func (peer *Peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) { +func (peer *peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) { peer.fsm.lock.RLock() list := []bgp.RouteFamily{} for _, a := range peer.fsm.pConf.AfiSafis { @@ -280,7 +280,7 @@ func (peer *Peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) { return classifyFamilies(peer.configuredRFlist(), list) } -func (peer *Peer) isLLGREnabledFamily(family bgp.RouteFamily) bool { +func (peer *peer) isLLGREnabledFamily(family bgp.RouteFamily) bool { peer.fsm.lock.RLock() llgrEnabled := peer.fsm.pConf.GracefulRestart.Config.LongLivedEnabled peer.fsm.lock.RUnlock() @@ -296,7 +296,7 @@ func (peer *Peer) isLLGREnabledFamily(family bgp.RouteFamily) bool { return false } -func (peer *Peer) llgrRestartTime(family bgp.RouteFamily) uint32 { +func (peer *peer) llgrRestartTime(family bgp.RouteFamily) uint32 { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() for _, a := range peer.fsm.pConf.AfiSafis { @@ -307,7 +307,7 @@ func (peer *Peer) llgrRestartTime(family bgp.RouteFamily) uint32 { return 0 } -func (peer *Peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool { +func (peer *peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool { peer.fsm.lock.RLock() defer peer.fsm.lock.RUnlock() all := true @@ -323,7 +323,7 @@ func (peer *Peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool { return all } -func (peer *Peer) markLLGRStale(fs []bgp.RouteFamily) []*table.Path { +func (peer *peer) markLLGRStale(fs []bgp.RouteFamily) []*table.Path { paths := peer.adjRibIn.PathList(fs, true) for i, p := range paths { doStale := true @@ -343,7 +343,7 @@ func (peer *Peer) markLLGRStale(fs []bgp.RouteFamily) []*table.Path { return paths } -func (peer *Peer) stopPeerRestarting() { +func (peer *peer) stopPeerRestarting() { peer.fsm.lock.Lock() defer peer.fsm.lock.Unlock() peer.fsm.pConf.GracefulRestart.State.PeerRestarting = false @@ -354,7 +354,7 @@ func (peer *Peer) stopPeerRestarting() { } -func (peer *Peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path { +func (peer *peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path { if peer.ID() != path.GetSource().Address.String() { return path } @@ -389,7 +389,7 @@ func (peer *Peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path { return nil } -func (peer *Peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig) *bgp.BGPMessage { +func (peer *peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig) *bgp.BGPMessage { if maxPrefixes := int(c.MaxPrefixes); maxPrefixes > 0 { count := peer.adjRibIn.Count([]bgp.RouteFamily{k}) pct := int(c.ShutdownThresholdPct) @@ -414,7 +414,7 @@ func (peer *Peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig) } -func (peer *Peer) updatePrefixLimitConfig(c []config.AfiSafi) error { +func (peer *peer) updatePrefixLimitConfig(c []config.AfiSafi) error { peer.fsm.lock.RLock() x := peer.fsm.pConf.AfiSafis peer.fsm.lock.RUnlock() @@ -451,7 +451,7 @@ func (peer *Peer) updatePrefixLimitConfig(c []config.AfiSafi) error { return nil } -func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bgp.BGPMessage) { +func (peer *peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bgp.BGPMessage) { m := e.MsgData.(*bgp.BGPMessage) update := m.Body.(*bgp.BGPUpdate) log.WithFields(log.Fields{ @@ -526,18 +526,18 @@ func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bg return nil, nil, nil } -func (peer *Peer) startFSMHandler(incoming *channels.InfiniteChannel, stateCh chan *FsmMsg) { +func (peer *peer) startFSMHandler(incoming *channels.InfiniteChannel, stateCh chan *FsmMsg) { handler := NewFSMHandler(peer.fsm, incoming, stateCh, peer.outgoing) peer.fsm.lock.Lock() peer.fsm.h = handler peer.fsm.lock.Unlock() } -func (peer *Peer) StaleAll(rfList []bgp.RouteFamily) []*table.Path { +func (peer *peer) StaleAll(rfList []bgp.RouteFamily) []*table.Path { return peer.adjRibIn.StaleAll(rfList) } -func (peer *Peer) PassConn(conn *net.TCPConn) { +func (peer *peer) PassConn(conn *net.TCPConn) { select { case peer.fsm.connCh <- conn: default: @@ -549,11 +549,11 @@ func (peer *Peer) PassConn(conn *net.TCPConn) { } } -func (peer *Peer) DropAll(rfList []bgp.RouteFamily) { +func (peer *peer) DropAll(rfList []bgp.RouteFamily) { peer.adjRibIn.Drop(rfList) } -func (peer *Peer) stopFSM() error { +func (peer *peer) stopFSM() error { failed := false peer.fsm.lock.RLock() addr := peer.fsm.pConf.State.NeighborAddress diff --git a/pkg/server/server.go b/pkg/server/server.go index 33c892aa..f9b1007c 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -105,8 +105,8 @@ type BgpServer struct { mgmtCh chan *mgmtOp policy *table.RoutingPolicy listeners []*tcpListener - neighborMap map[string]*Peer - peerGroupMap map[string]*PeerGroup + neighborMap map[string]*peer + peerGroupMap map[string]*peerGroup globalRib *table.TableManager rsRib *table.TableManager roaManager *roaManager @@ -121,8 +121,8 @@ type BgpServer struct { func NewBgpServer() *BgpServer { roaManager, _ := newROAManager(0) s := &BgpServer{ - neighborMap: make(map[string]*Peer), - peerGroupMap: make(map[string]*PeerGroup), + neighborMap: make(map[string]*peer), + peerGroupMap: make(map[string]*peerGroup), policy: table.NewRoutingPolicy(), roaManager: roaManager, mgmtCh: make(chan *mgmtOp, 1), @@ -331,10 +331,10 @@ func (server *BgpServer) Serve() { } } -func (server *BgpServer) matchLongestDynamicNeighborPrefix(a string) *PeerGroup { +func (server *BgpServer) matchLongestDynamicNeighborPrefix(a string) *peerGroup { ipAddr := net.ParseIP(a) longestMask := net.CIDRMask(0, 32).String() - var longestPG *PeerGroup + var longestPG *peerGroup for _, pg := range server.peerGroupMap { for _, d := range pg.dynamicNeighbors { _, netAddr, _ := net.ParseCIDR(d.Config.Prefix) @@ -349,7 +349,7 @@ func (server *BgpServer) matchLongestDynamicNeighborPrefix(a string) *PeerGroup return longestPG } -func sendFsmOutgoingMsg(peer *Peer, paths []*table.Path, notification *bgp.BGPMessage, stayIdle bool) { +func sendFsmOutgoingMsg(peer *peer, paths []*table.Path, notification *bgp.BGPMessage, stayIdle bool) { peer.outgoing.In() <- &FsmOutgoingMsg{ Paths: paths, Notification: notification, @@ -357,7 +357,7 @@ func sendFsmOutgoingMsg(peer *Peer, paths []*table.Path, notification *bgp.BGPMe } } -func isASLoop(peer *Peer, path *table.Path) bool { +func isASLoop(peer *peer, path *table.Path) bool { for _, as := range path.GetAsList() { if as == peer.AS() { return true @@ -366,7 +366,7 @@ func isASLoop(peer *Peer, path *table.Path) bool { return false } -func filterpath(peer *Peer, path, old *table.Path) *table.Path { +func filterpath(peer *peer, path, old *table.Path) *table.Path { if path == nil { return nil } @@ -476,7 +476,7 @@ func filterpath(peer *Peer, path, old *table.Path) *table.Path { return path } -func (s *BgpServer) filterpath(peer *Peer, path, old *table.Path) *table.Path { +func (s *BgpServer) filterpath(peer *peer, path, old *table.Path) *table.Path { // Special handling for RTM NLRI. if path != nil && path.GetRouteFamily() == bgp.RF_RTC_UC && !path.IsWithdraw { // If the given "path" is locally generated and the same with "old", we @@ -634,7 +634,7 @@ func (server *BgpServer) notifyBestWatcher(best []*table.Path, multipath [][]*ta server.notifyWatcher(WATCH_EVENT_TYPE_BEST_PATH, w) } -func (s *BgpServer) toConfig(peer *Peer, getAdvertised bool) *config.Neighbor { +func (s *BgpServer) toConfig(peer *peer, getAdvertised bool) *config.Neighbor { // create copy which can be access to without mutex peer.fsm.lock.RLock() conf := *peer.fsm.pConf @@ -693,7 +693,7 @@ func (s *BgpServer) toConfig(peer *Peer, getAdvertised bool) *config.Neighbor { return &conf } -func (server *BgpServer) notifyPrePolicyUpdateWatcher(peer *Peer, pathList []*table.Path, msg *bgp.BGPMessage, timestamp time.Time, payload []byte) { +func (server *BgpServer) notifyPrePolicyUpdateWatcher(peer *peer, pathList []*table.Path, msg *bgp.BGPMessage, timestamp time.Time, payload []byte) { if !server.isWatched(WATCH_EVENT_TYPE_PRE_UPDATE) || peer == nil { return } @@ -723,7 +723,7 @@ func (server *BgpServer) notifyPrePolicyUpdateWatcher(peer *Peer, pathList []*ta server.notifyWatcher(WATCH_EVENT_TYPE_PRE_UPDATE, ev) } -func (server *BgpServer) notifyPostPolicyUpdateWatcher(peer *Peer, pathList []*table.Path) { +func (server *BgpServer) notifyPostPolicyUpdateWatcher(peer *peer, pathList []*table.Path) { if !server.isWatched(WATCH_EVENT_TYPE_POST_UPDATE) || peer == nil { return } @@ -751,7 +751,7 @@ func (server *BgpServer) notifyPostPolicyUpdateWatcher(peer *Peer, pathList []*t server.notifyWatcher(WATCH_EVENT_TYPE_POST_UPDATE, ev) } -func newWatchEventPeerState(peer *Peer, m *FsmMsg) *WatchEventPeerState { +func newWatchEventPeerState(peer *peer, m *FsmMsg) *WatchEventPeerState { _, rport := peer.fsm.RemoteHostPort() laddr, lport := peer.fsm.LocalHostPort() sentOpen := buildopen(peer.fsm.gConf, peer.fsm.pConf) @@ -780,7 +780,7 @@ func newWatchEventPeerState(peer *Peer, m *FsmMsg) *WatchEventPeerState { return e } -func (server *BgpServer) broadcastPeerState(peer *Peer, oldState bgp.FSMState, e *FsmMsg) { +func (server *BgpServer) broadcastPeerState(peer *peer, oldState bgp.FSMState, e *FsmMsg) { peer.fsm.lock.RLock() newState := peer.fsm.state peer.fsm.lock.RUnlock() @@ -789,7 +789,7 @@ func (server *BgpServer) broadcastPeerState(peer *Peer, oldState bgp.FSMState, e } } -func (server *BgpServer) notifyMessageWatcher(peer *Peer, timestamp time.Time, msg *bgp.BGPMessage, isSent bool) { +func (server *BgpServer) notifyMessageWatcher(peer *peer, timestamp time.Time, msg *bgp.BGPMessage, isSent bool) { // validation should be done in the caller of this function peer.fsm.lock.RLock() _, y := peer.fsm.capMap[bgp.BGP_CAP_FOUR_OCTET_AS_NUMBER] @@ -811,14 +811,14 @@ func (server *BgpServer) notifyMessageWatcher(peer *Peer, timestamp time.Time, m } } -func (server *BgpServer) notifyRecvMessageWatcher(peer *Peer, timestamp time.Time, msg *bgp.BGPMessage) { +func (server *BgpServer) notifyRecvMessageWatcher(peer *peer, timestamp time.Time, msg *bgp.BGPMessage) { if peer == nil || !server.isWatched(WATCH_EVENT_TYPE_RECV_MSG) { return } server.notifyMessageWatcher(peer, timestamp, msg, false) } -func (s *BgpServer) getBestFromLocal(peer *Peer, rfList []bgp.RouteFamily) ([]*table.Path, []*table.Path) { +func (s *BgpServer) getBestFromLocal(peer *peer, rfList []bgp.RouteFamily) ([]*table.Path, []*table.Path) { pathList := []*table.Path{} filtered := []*table.Path{} for _, family := range peer.toGlobalFamilies(rfList) { @@ -844,7 +844,7 @@ func (s *BgpServer) getBestFromLocal(peer *Peer, rfList []bgp.RouteFamily) ([]*t return pathList, filtered } -func (s *BgpServer) processOutgoingPaths(peer *Peer, paths, olds []*table.Path) []*table.Path { +func (s *BgpServer) processOutgoingPaths(peer *peer, paths, olds []*table.Path) []*table.Path { peer.fsm.lock.RLock() notEstablished := peer.fsm.state != bgp.BGP_FSM_ESTABLISHED localRestarting := peer.fsm.pConf.GracefulRestart.State.LocalRestarting @@ -876,7 +876,7 @@ func (s *BgpServer) processOutgoingPaths(peer *Peer, paths, olds []*table.Path) return outgoing } -func (s *BgpServer) handleRouteRefresh(peer *Peer, e *FsmMsg) []*table.Path { +func (s *BgpServer) handleRouteRefresh(peer *peer, e *FsmMsg) []*table.Path { m := e.MsgData.(*bgp.BGPMessage) rr := m.Body.(*bgp.BGPRouteRefresh) rf := bgp.AfiSafiToRouteFamily(rr.AFI, rr.SAFI) @@ -912,7 +912,7 @@ func (s *BgpServer) handleRouteRefresh(peer *Peer, e *FsmMsg) []*table.Path { return accepted } -func (server *BgpServer) propagateUpdate(peer *Peer, pathList []*table.Path) { +func (server *BgpServer) propagateUpdate(peer *peer, pathList []*table.Path) { rs := peer != nil && peer.isRouteServerClient() vrf := false if peer != nil { @@ -1023,7 +1023,7 @@ func (server *BgpServer) propagateUpdate(peer *Peer, pathList []*table.Path) { } } -func (server *BgpServer) dropPeerAllRoutes(peer *Peer, families []bgp.RouteFamily) { +func (server *BgpServer) dropPeerAllRoutes(peer *peer, families []bgp.RouteFamily) { peer.fsm.lock.RLock() peerInfo := peer.fsm.peerInfo peer.fsm.lock.RUnlock() @@ -1058,7 +1058,7 @@ func dstsToPaths(id string, as uint32, dsts []*table.Update) ([]*table.Path, []* return bestList, oldList, mpathList } -func (server *BgpServer) propagateUpdateToNeighbors(source *Peer, newPath *table.Path, dsts []*table.Update, needOld bool) { +func (server *BgpServer) propagateUpdateToNeighbors(source *peer, newPath *table.Path, dsts []*table.Update, needOld bool) { if table.SelectionOptions.DisableBestPathSelection { return } @@ -1125,7 +1125,7 @@ func (server *BgpServer) propagateUpdateToNeighbors(source *Peer, newPath *table } } -func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { +func (server *BgpServer) handleFSMMessage(peer *peer, e *FsmMsg) { switch e.MsgType { case FSM_MSG_STATE_CHANGE: nextState := e.MsgData.(bgp.FSMState) @@ -1144,7 +1144,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { if oldState == bgp.BGP_FSM_ESTABLISHED { t := time.Now() peer.fsm.lock.Lock() - if t.Sub(time.Unix(peer.fsm.pConf.Timers.State.Uptime, 0)) < FLOP_THRESHOLD { + if t.Sub(time.Unix(peer.fsm.pConf.Timers.State.Uptime, 0)) < flopThreshold { peer.fsm.pConf.State.Flops++ } graceful := peer.fsm.reason.Type == FSM_GRACEFUL_RESTART @@ -2052,7 +2052,7 @@ func (s *BgpServer) DeleteVrf(ctx context.Context, r *api.DeleteVrfRequest) erro }, true) } -func familiesForSoftreset(peer *Peer, family bgp.RouteFamily) []bgp.RouteFamily { +func familiesForSoftreset(peer *peer, family bgp.RouteFamily) []bgp.RouteFamily { if family == bgp.RouteFamily(0) { configured := peer.configuredRFlist() families := make([]bgp.RouteFamily, 0, len(configured)) @@ -2493,7 +2493,7 @@ func (server *BgpServer) addPeerGroup(c *config.PeerGroup) error { "Name": name, }).Info("Add a peer group configuration") - server.peerGroupMap[c.Config.PeerGroupName] = NewPeerGroup(c) + server.peerGroupMap[c.Config.PeerGroupName] = newPeerGroup(c) return nil } @@ -2561,7 +2561,7 @@ func (server *BgpServer) addNeighbor(c *config.Neighbor) error { if c.RouteServer.Config.RouteServerClient { rib = server.rsRib } - peer := NewPeer(&server.bgpConfig.Global, c, rib, server.policy) + peer := newPeer(&server.bgpConfig.Global, c, rib, server.policy) server.policy.Reset(nil, map[string]config.ApplyPolicy{peer.ID(): c.ApplyPolicy}) server.neighborMap[addr] = peer if name := c.Config.PeerGroup; name != "" { @@ -2828,18 +2828,18 @@ func (s *BgpServer) UpdateNeighbor(ctx context.Context, r *api.UpdatePeerRequest return &api.UpdatePeerResponse{NeedsSoftResetIn: doSoftReset}, err } -func (s *BgpServer) addrToPeers(addr string) (l []*Peer, err error) { +func (s *BgpServer) addrToPeers(addr string) (l []*peer, err error) { if len(addr) == 0 { for _, p := range s.neighborMap { l = append(l, p) } return l, nil } - peer, found := s.neighborMap[addr] + p, found := s.neighborMap[addr] if !found { return l, fmt.Errorf("Neighbor that has %v doesn't exist.", addr) } - return []*Peer{peer}, nil + return []*peer{p}, nil } func (s *BgpServer) sendNotification(op, addr string, subcode uint8, data []byte) error { diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 8d17c03d..a5141e76 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -358,13 +358,13 @@ func TestNumGoroutineWithAddDeleteNeighbor(t *testing.T) { assert.Equal(num, runtime.NumGoroutine()) } -func newPeerandInfo(myAs, as uint32, address string, rib *table.TableManager) (*Peer, *table.PeerInfo) { +func newPeerandInfo(myAs, as uint32, address string, rib *table.TableManager) (*peer, *table.PeerInfo) { nConf := &config.Neighbor{Config: config.NeighborConfig{PeerAs: as, NeighborAddress: address}} gConf := &config.Global{Config: config.GlobalConfig{As: myAs}} config.SetDefaultNeighborConfigValues(nConf, nil, gConf) policy := table.NewRoutingPolicy() policy.Reset(&config.RoutingPolicy{}, nil) - p := NewPeer( + p := newPeer( &config.Global{Config: config.GlobalConfig{As: myAs}}, nConf, rib, @@ -804,7 +804,7 @@ func TestFamiliesForSoftreset(t *testing.T) { }, } } - peer := &Peer{ + peer := &peer{ fsm: &FSM{ pConf: &config.Neighbor{ AfiSafis: []config.AfiSafi{f(bgp.RF_RTC_UC), f(bgp.RF_IPv4_UC), f(bgp.RF_IPv6_UC)}, @@ -825,31 +825,30 @@ func TestFamiliesForSoftreset(t *testing.T) { assert.NotContains(t, families, bgp.RF_RTC_UC) } -func runNewServer(ctx context.Context, as uint32, routerId string, listenPort int32) (*BgpServer, error) { +func runNewServer(ctx context.Context, as uint32, routerID string, listenPort int32) (*BgpServer, context.CancelFunc, error) { s := NewBgpServer() ctxInner, cancelInner := context.WithCancel(ctx) go s.Serve() go func() { <-ctxInner.Done() - stopCtx, _ := context.WithTimeout(context.Background(), 10*time.Second) + stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) if err := s.StopBgp(stopCtx, &api.StopBgpRequest{}); err != nil { log.Fatalf("Failed to stop server %s: %s", s.bgpConfig.Global.Config.RouterId, err) } + cancel() }() err := s.StartBgp(ctx, &api.StartBgpRequest{ Global: &api.Global{ As: as, - RouterId: routerId, + RouterId: routerID, ListenPort: listenPort, }, }) if err != nil { - cancelInner() - return nil, err + s = nil } - - return s, nil + return s, cancelInner, err } func peerServers(t *testing.T, ctx context.Context, servers []*BgpServer, families []config.AfiSafiType) error { @@ -936,14 +935,16 @@ func TestDoNotReactToDuplicateRTCMemberships(t *testing.T) { defer cancel() log.SetLevel(log.DebugLevel) - s1, err := runNewServer(ctx, 1, "1.1.1.1", 10179) + s1, cf1, err := runNewServer(ctx, 1, "1.1.1.1", 10179) if err != nil { t.Fatal(err) } - s2, err := runNewServer(ctx, 1, "2.2.2.2", 20179) + defer func() { cf1() }() + s2, cf2, err := runNewServer(ctx, 1, "2.2.2.2", 20179) if err != nil { t.Fatal(err) } + defer func() { cf2() }() addVrf(t, ctx, s1, "vrf1", "111:111", 1) addVrf(t, ctx, s2, "vrf1", "111:111", 1) @@ -1013,7 +1014,7 @@ func TestDoNotReactToDuplicateRTCMemberships(t *testing.T) { s1Peer := s2.neighborMap["127.0.0.1"] s2.propagateUpdate(s1Peer, []*table.Path{rtcPath}) - awaitUpdateCtx, _ := context.WithTimeout(ctx, time.Second) + awaitUpdateCtx, cancel := context.WithTimeout(ctx, time.Second) for done := false; !done; { select { case ev := <-watcher.Event(): @@ -1026,9 +1027,11 @@ func TestDoNotReactToDuplicateRTCMemberships(t *testing.T) { } } } + //case <-timer.C: case <-awaitUpdateCtx.Done(): log.Infof("await update done") done = true } } + cancel() } |