diff options
-rw-r--r-- | server/peer.go | 47 | ||||
-rw-r--r-- | server/server.go | 67 | ||||
-rw-r--r-- | table/table_manager.go | 11 |
3 files changed, 95 insertions, 30 deletions
diff --git a/server/peer.go b/server/peer.go index c237aff7..77e6a01b 100644 --- a/server/peer.go +++ b/server/peer.go @@ -56,15 +56,16 @@ type Peer struct { adjRib *table.AdjRib // peer and rib are always not one-to-one so should not be // here but it's the simplest and works our first target. - rib *table.TableManager - rfMap map[bgp.RouteFamily]bool - capMap map[bgp.BGPCapabilityCode]bgp.ParameterCapabilityInterface - peerInfo *table.PeerInfo - siblings map[string]*serverMsgDataPeer - outgoing chan *bgp.BGPMessage + rib *table.TableManager + isGlobalRib bool + rfMap map[bgp.RouteFamily]bool + capMap map[bgp.BGPCapabilityCode]bgp.ParameterCapabilityInterface + peerInfo *table.PeerInfo + siblings map[string]*serverMsgDataPeer + outgoing chan *bgp.BGPMessage } -func NewPeer(g config.Global, peer config.Neighbor, serverMsgCh chan *serverMsg, peerMsgCh chan *peerMsg, peerList []*serverMsgDataPeer) *Peer { +func NewPeer(g config.Global, peer config.Neighbor, serverMsgCh chan *serverMsg, peerMsgCh chan *peerMsg, peerList []*serverMsgDataPeer, isGlobalRib bool) *Peer { p := &Peer{ globalConfig: g, peerConfig: peer, @@ -73,6 +74,7 @@ func NewPeer(g config.Global, peer config.Neighbor, serverMsgCh chan *serverMsg, peerMsgCh: peerMsgCh, rfMap: make(map[bgp.RouteFamily]bool), capMap: make(map[bgp.BGPCapabilityCode]bgp.ParameterCapabilityInterface), + isGlobalRib: isGlobalRib, } p.siblings = make(map[string]*serverMsgDataPeer) for _, s := range peerList { @@ -325,12 +327,25 @@ func (peer *Peer) sendUpdateMsgFromPaths(pList []table.Path) { func (peer *Peer) handlePeerMsg(m *peerMsg) { switch m.msgType { case PEER_MSG_PATH: - pList, _ := peer.rib.ProcessPaths(m.msgData.([]table.Path)) - peer.sendUpdateMsgFromPaths(pList) + pList := m.msgData.([]table.Path) + if peer.peerConfig.RouteServer.RouteServerClient || peer.isGlobalRib { + pList, _ = peer.rib.ProcessPaths(pList) + } + + if peer.isGlobalRib { + peer.sendPathsToSiblings(pList) + } else { + peer.sendUpdateMsgFromPaths(pList) + } + case PEER_MSG_PEER_DOWN: for _, rf := range peer.configuredRFlist() { pList, _ := peer.rib.DeletePathsforPeer(m.msgData.(*table.PeerInfo), rf) - peer.sendUpdateMsgFromPaths(pList) + if peer.peerConfig.RouteServer.RouteServerClient { + peer.sendUpdateMsgFromPaths(pList) + } else if peer.isGlobalRib { + peer.sendPathsToSiblings(pList) + } } } } @@ -341,7 +356,11 @@ func (peer *Peer) handleServerMsg(m *serverMsg) { d := m.msgData.(*serverMsgDataPeer) peer.siblings[d.address.String()] = d for _, rf := range peer.configuredRFlist() { - peer.sendPathsToSiblings(peer.adjRib.GetInPathList(rf)) + if peer.peerConfig.RouteServer.RouteServerClient { + peer.sendPathsToSiblings(peer.adjRib.GetInPathList(rf)) + } else if peer.isGlobalRib { + peer.sendPathsToSiblings(peer.rib.GetPathList(rf)) + } } case SRV_MSG_PEER_DELETED: d := m.msgData.(*table.PeerInfo) @@ -349,7 +368,11 @@ func (peer *Peer) handleServerMsg(m *serverMsg) { delete(peer.siblings, d.Address.String()) for _, rf := range peer.configuredRFlist() { pList, _ := peer.rib.DeletePathsforPeer(d, rf) - peer.sendUpdateMsgFromPaths(pList) + if peer.peerConfig.RouteServer.RouteServerClient { + peer.sendUpdateMsgFromPaths(pList) + } else { + peer.sendPathsToSiblings(pList) + } } } else { log.Warning("can not find peer: ", d.Address.String()) diff --git a/server/server.go b/server/server.go index f92a1294..19d7eeba 100644 --- a/server/server.go +++ b/server/server.go @@ -47,10 +47,11 @@ type serverMsgDataPeer struct { } type peerMapInfo struct { - peer *Peer - serverMsgCh chan *serverMsg - peerMsgCh chan *peerMsg - peerMsgData *serverMsgDataPeer + peer *Peer + serverMsgCh chan *serverMsg + peerMsgCh chan *peerMsg + peerMsgData *serverMsgDataPeer + isRouteServerClient bool } type BgpServer struct { @@ -61,6 +62,7 @@ type BgpServer struct { RestReqCh chan *api.RestRequest listenPort int peerMap map[string]peerMapInfo + globalRib *Peer } func NewBgpServer(port int) *BgpServer { @@ -101,7 +103,16 @@ func listenAndAccept(proto string, port int, ch chan *net.TCPConn) (*net.TCPList } func (server *BgpServer) Serve() { - server.bgpConfig.Global = <-server.globalTypeCh + g := <-server.globalTypeCh + server.bgpConfig.Global = g + + globalSch := make(chan *serverMsg, 8) + globalPch := make(chan *peerMsg, 4096) + neighConf := config.Neighbor{ + NeighborAddress: g.RouterId, + AfiSafiList: g.AfiSafiList, + } + server.globalRib = NewPeer(g, neighConf, globalSch, globalPch, nil, true) listenerMap := make(map[string]*net.TCPListener) acceptCh := make(chan *net.TCPConn) @@ -150,13 +161,21 @@ func (server *BgpServer) Serve() { SetTcpMD5SigSockopts(int(f.Fd()), addr, peer.AuthPassword) sch := make(chan *serverMsg, 8) pch := make(chan *peerMsg, 4096) - l := make([]*serverMsgDataPeer, len(server.peerMap)) - i := 0 - for _, v := range server.peerMap { - l[i] = v.peerMsgData - i++ + var l []*serverMsgDataPeer + if peer.RouteServer.RouteServerClient { + for _, v := range server.peerMap { + if v.isRouteServerClient { + l = append(l, v.peerMsgData) + } + } + } else { + globalRib := &serverMsgDataPeer{ + address: server.bgpConfig.Global.RouterId, + peerMsgCh: globalPch, + } + l = []*serverMsgDataPeer{globalRib} } - p := NewPeer(server.bgpConfig.Global, peer, sch, pch, l) + p := NewPeer(server.bgpConfig.Global, peer, sch, pch, l, false) d := &serverMsgDataPeer{ address: peer.NeighborAddress, peerMsgCh: pch, @@ -165,11 +184,17 @@ func (server *BgpServer) Serve() { msgType: SRV_MSG_PEER_ADDED, msgData: d, } - sendServerMsgToAll(server.peerMap, msg) + if peer.RouteServer.RouteServerClient { + sendServerMsgToRSClients(server.peerMap, msg) + } else { + globalSch <- msg + } + server.peerMap[peer.NeighborAddress.String()] = peerMapInfo{ - peer: p, - serverMsgCh: sch, - peerMsgData: d, + peer: p, + serverMsgCh: sch, + peerMsgData: d, + isRouteServerClient: peer.RouteServer.RouteServerClient, } case peer := <-server.deletedPeerCh: addr := peer.NeighborAddress.String() @@ -184,7 +209,11 @@ func (server *BgpServer) Serve() { msgType: SRV_MSG_PEER_DELETED, msgData: info.peer.peerInfo, } - sendServerMsgToAll(server.peerMap, msg) + if info.isRouteServerClient { + sendServerMsgToRSClients(server.peerMap, msg) + } else { + globalSch <- msg + } } else { log.Info("Can't delete a peer configuration for ", addr) } @@ -194,9 +223,11 @@ func (server *BgpServer) Serve() { } } -func sendServerMsgToAll(peerMap map[string]peerMapInfo, msg *serverMsg) { +func sendServerMsgToRSClients(peerMap map[string]peerMapInfo, msg *serverMsg) { for _, info := range peerMap { - info.serverMsgCh <- msg + if info.isRouteServerClient { + info.serverMsgCh <- msg + } } } diff --git a/table/table_manager.go b/table/table_manager.go index c8ff0833..d34e3780 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -261,6 +261,17 @@ func (manager *TableManager) ProcessPaths(pathList []Path) ([]Path, error) { return manager.calculate(destinationList) } +func (manager *TableManager) GetPathList(rf bgp.RouteFamily) []Path { + if _, ok := manager.Tables[rf]; !ok { + return []Path{} + } + var paths []Path + for _, dest := range manager.Tables[rf].getDestinations() { + paths = append(paths, dest.getBestPath()) + } + return paths +} + // process BGPUpdate message // this function processes only BGPUpdate func (manager *TableManager) ProcessUpdate(fromPeer *PeerInfo, message *bgp.BGPMessage) ([]Path, error) { |