diff options
-rw-r--r-- | server/peer.go | 24 | ||||
-rw-r--r-- | server/server.go | 20 | ||||
-rw-r--r-- | table/table_manager.go | 70 | ||||
-rw-r--r-- | table/table_manager_test.go | 4 |
4 files changed, 56 insertions, 62 deletions
diff --git a/server/peer.go b/server/peer.go index b5a62b88..ea1d8098 100644 --- a/server/peer.go +++ b/server/peer.go @@ -115,11 +115,9 @@ func (peer *Peer) updateAccepted(accepted uint32) { func (peer *Peer) getAccepted(rfList []bgp.RouteFamily) []*table.Path { var pathList []*table.Path - for _, rf := range rfList { - for _, path := range peer.adjRib.GetInPathList(rf) { - if path.Filtered == false { - pathList = append(pathList, path) - } + for _, path := range peer.adjRib.GetInPathList(rfList) { + if path.Filtered == false { + pathList = append(pathList, path) } } return pathList @@ -205,7 +203,7 @@ func (peer *Peer) handleBGPmessage(m *bgp.BGPMessage) ([]*table.Path, bool, []*b break } if _, ok := peer.capMap[bgp.BGP_CAP_ROUTE_REFRESH]; ok { - pathList = peer.adjRib.GetOutPathList(rf) + pathList = peer.adjRib.GetOutPathList([]bgp.RouteFamily{rf}) } else { log.WithFields(log.Fields{ "Topic": "Peer", @@ -327,12 +325,11 @@ func (peer *Peer) ToApiStruct() *api.Peer { received := uint32(0) accepted := uint32(0) if f.state == bgp.BGP_FSM_ESTABLISHED { - for _, rf := range peer.configuredRFlist() { - advertized += uint32(peer.adjRib.GetOutCount(rf)) - received += uint32(peer.adjRib.GetInCount(rf)) - } + rfList := peer.configuredRFlist() + advertized = uint32(peer.adjRib.GetOutCount(rfList)) + received = uint32(peer.adjRib.GetInCount(rfList)) if peer.staleAccepted { - accepted = uint32(len(peer.getAccepted(peer.configuredRFlist()))) + accepted = uint32(len(peer.getAccepted(rfList))) peer.updateAccepted(accepted) } else { accepted = peer.accepted @@ -464,8 +461,9 @@ func (peer *Peer) ApplyPolicy(d table.PolicyDirection, paths []*table.Path) ([]* return newpaths, filteredPaths } -func (peer *Peer) DropAll(rf bgp.RouteFamily) { - peer.adjRib.DropAll(rf) +func (peer *Peer) DropAll(rfList []bgp.RouteFamily) { + peer.adjRib.DropIn(rfList) + peer.adjRib.DropOut(rfList) peer.staleAccepted = false peer.accepted = 0 } diff --git a/server/server.go b/server/server.go index 391dc40c..417d09d7 100644 --- a/server/server.go +++ b/server/server.go @@ -308,14 +308,10 @@ func (server *BgpServer) Serve() { case c := <-server.bmpConnCh: bmpMsgList := []*bgp.BMPMessage{} for _, targetPeer := range server.neighborMap { - pathList := make([]*table.Path, 0) if targetPeer.fsm.state != bgp.BGP_FSM_ESTABLISHED { continue } - for _, rf := range targetPeer.configuredRFlist() { - pathList = append(pathList, targetPeer.adjRib.GetInPathList(rf)...) - } - for _, p := range pathList { + for _, p := range targetPeer.adjRib.GetInPathList(targetPeer.configuredRFlist()) { // avoid to merge for timestamp u := table.CreateUpdateMsgFromPaths([]*table.Path{p}) bmpMsgList = append(bmpMsgList, bmpPeerRoute(bgp.BMP_PEER_TYPE_GLOBAL, false, 0, targetPeer.peerInfo, p.GetTimestamp().Unix(), u[0])) @@ -716,9 +712,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *fsmMsg, incoming chan * peer.conf.NeighborState.Flops++ } - for _, rf := range peer.configuredRFlist() { - peer.DropAll(rf) - } + peer.DropAll(peer.configuredRFlist()) msgs = append(msgs, server.dropPeerAllRoutes(peer)...) } @@ -1395,10 +1389,10 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { var paths []*table.Path if grpcReq.RequestType == REQ_ADJ_RIB_IN { - paths = peer.adjRib.GetInPathList(rf) + paths = peer.adjRib.GetInPathList([]bgp.RouteFamily{rf}) log.Debugf("RouteFamily=%v adj-rib-in found : %d", rf.String(), len(paths)) } else { - paths = peer.adjRib.GetOutPathList(rf) + paths = peer.adjRib.GetOutPathList([]bgp.RouteFamily{rf}) log.Debugf("RouteFamily=%v adj-rib-out found : %d", rf.String(), len(paths)) } @@ -1471,7 +1465,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } for _, peer := range peers { - pathList := peer.adjRib.GetInPathList(grpcReq.RouteFamily) + pathList := peer.adjRib.GetInPathList([]bgp.RouteFamily{grpcReq.RouteFamily}) if peer.isRouteServerClient() { pathList, _ = peer.ApplyPolicy(table.POLICY_DIRECTION_IN, pathList) } @@ -1493,9 +1487,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { logOp(grpcReq.Name, "Neighbor soft reset out") } for _, peer := range peers { - for _, rf := range peer.configuredRFlist() { - peer.adjRib.DropOut(rf) - } + peer.adjRib.DropOut(peer.configuredRFlist()) pathList, filtered := peer.getBestFromLocal() if len(pathList) > 0 { diff --git a/table/table_manager.go b/table/table_manager.go index acd23676..f52f1ae9 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -540,55 +540,59 @@ func (adj *AdjRib) UpdateOut(pathList []*Path) { adj.update(adj.adjRibOut, pathList) } -func (adj *AdjRib) getPathList(rib map[string]*Path) []*Path { - pathList := make([]*Path, 0, len(rib)) - for _, rr := range rib { - pathList = append(pathList, rr) +func (adj *AdjRib) GetInPathList(rfList []bgp.RouteFamily) []*Path { + pathList := make([]*Path, 0, adj.GetInCount(rfList)) + for _, rf := range rfList { + for _, rr := range adj.adjRibIn[rf] { + pathList = append(pathList, rr) + } } return pathList } -func (adj *AdjRib) GetInPathList(rf bgp.RouteFamily) []*Path { - if _, ok := adj.adjRibIn[rf]; !ok { - return []*Path{} +func (adj *AdjRib) GetOutPathList(rfList []bgp.RouteFamily) []*Path { + pathList := make([]*Path, 0, adj.GetOutCount(rfList)) + for _, rf := range rfList { + for _, rr := range adj.adjRibOut[rf] { + pathList = append(pathList, rr) + } } - return adj.getPathList(adj.adjRibIn[rf]) + return pathList } -func (adj *AdjRib) GetOutPathList(rf bgp.RouteFamily) []*Path { - if _, ok := adj.adjRibOut[rf]; !ok { - return []*Path{} - } - return adj.getPathList(adj.adjRibOut[rf]) -} +func (adj *AdjRib) GetInCount(rfList []bgp.RouteFamily) int { + count := 0 + for _, rf := range rfList { + if _, ok := adj.adjRibIn[rf]; ok { + count += len(adj.adjRibIn[rf]) -func (adj *AdjRib) GetInCount(rf bgp.RouteFamily) int { - if _, ok := adj.adjRibIn[rf]; !ok { - return 0 + } } - return len(adj.adjRibIn[rf]) + return count } -func (adj *AdjRib) GetOutCount(rf bgp.RouteFamily) int { - if _, ok := adj.adjRibOut[rf]; !ok { - return 0 +func (adj *AdjRib) GetOutCount(rfList []bgp.RouteFamily) int { + count := 0 + for _, rf := range rfList { + if _, ok := adj.adjRibOut[rf]; ok { + count += len(adj.adjRibOut[rf]) + } } - return len(adj.adjRibOut[rf]) + return count } -func (adj *AdjRib) DropIn(rf bgp.RouteFamily) { - if _, ok := adj.adjRibIn[rf]; ok { - adj.adjRibIn[rf] = make(map[string]*Path) +func (adj *AdjRib) DropIn(rfList []bgp.RouteFamily) { + for _, rf := range rfList { + if _, ok := adj.adjRibIn[rf]; ok { + adj.adjRibIn[rf] = make(map[string]*Path) + } } } -func (adj *AdjRib) DropOut(rf bgp.RouteFamily) { - if _, ok := adj.adjRibIn[rf]; ok { - adj.adjRibOut[rf] = make(map[string]*Path) +func (adj *AdjRib) DropOut(rfList []bgp.RouteFamily) { + for _, rf := range rfList { + if _, ok := adj.adjRibIn[rf]; ok { + adj.adjRibOut[rf] = make(map[string]*Path) + } } } - -func (adj *AdjRib) DropAll(rf bgp.RouteFamily) { - adj.DropIn(rf) - adj.DropOut(rf) -} diff --git a/table/table_manager_test.go b/table/table_manager_test.go index 122d60a1..a836347a 100644 --- a/table/table_manager_test.go +++ b/table/table_manager_test.go @@ -2154,7 +2154,7 @@ func TestProcessBGPUpdate_Timestamp(t *testing.T) { //t2 = path2.timestamp adjRib.UpdateIn(pList2) - inList := adjRib.GetInPathList(bgp.RF_IPv4_UC) + inList := adjRib.GetInPathList([]bgp.RouteFamily{bgp.RF_IPv4_UC}) assert.Equal(t, len(inList), 1) assert.Equal(t, inList[0].GetTimestamp(), t1) @@ -2171,7 +2171,7 @@ func TestProcessBGPUpdate_Timestamp(t *testing.T) { t3 := pList3[0].GetTimestamp() adjRib.UpdateIn(pList3) - inList = adjRib.GetInPathList(bgp.RF_IPv4_UC) + inList = adjRib.GetInPathList([]bgp.RouteFamily{bgp.RF_IPv4_UC}) assert.Equal(t, len(inList), 1) assert.Equal(t, inList[0].GetTimestamp(), t3) } |