diff options
Diffstat (limited to 'server/server.go')
-rw-r--r-- | server/server.go | 268 |
1 files changed, 187 insertions, 81 deletions
diff --git a/server/server.go b/server/server.go index 79316daa..48af87da 100644 --- a/server/server.go +++ b/server/server.go @@ -92,27 +92,29 @@ type BgpServer struct { fsmStateCh chan *FsmMsg acceptCh chan *net.TCPConn - mgmtCh chan *mgmtOp - policy *table.RoutingPolicy - listeners []*TCPListener - neighborMap map[string]*Peer - globalRib *table.TableManager - roaManager *roaManager - shutdown bool - watcherMap map[WatchEventType][]*Watcher - zclient *zebraClient - bmpManager *bmpClientManager - mrtManager *mrtManager + mgmtCh chan *mgmtOp + policy *table.RoutingPolicy + listeners []*TCPListener + neighborMap map[string]*Peer + peerGroupMap map[string]*PeerGroup + globalRib *table.TableManager + roaManager *roaManager + shutdown bool + watcherMap map[WatchEventType][]*Watcher + zclient *zebraClient + bmpManager *bmpClientManager + mrtManager *mrtManager } func NewBgpServer() *BgpServer { roaManager, _ := NewROAManager(0) s := &BgpServer{ - neighborMap: make(map[string]*Peer), - policy: table.NewRoutingPolicy(), - roaManager: roaManager, - mgmtCh: make(chan *mgmtOp, 1), - watcherMap: make(map[WatchEventType][]*Watcher), + neighborMap: make(map[string]*Peer), + peerGroupMap: make(map[string]*PeerGroup), + policy: table.NewRoutingPolicy(), + roaManager: roaManager, + mgmtCh: make(chan *mgmtOp, 1), + watcherMap: make(map[WatchEventType][]*Watcher), } s.bmpManager = newBmpClientManager(s) s.mrtManager = newMrtManager(s) @@ -1603,17 +1605,38 @@ func (s *BgpServer) GetNeighbor(address string, getAdvertised bool) (l []*config return l } -func (server *BgpServer) addNeighbor(c *config.Neighbor) error { - - if err := config.SetDefaultNeighborConfigValues(c, server.bgpConfig.Global.Config.As); err != nil { - return err +func (server *BgpServer) addPeerGroup(c *config.PeerGroup) error { + name := c.Config.PeerGroupName + if _, y := server.peerGroupMap[name]; y { + return fmt.Errorf("Can't overwrite the existing peer-group: %s", name) } + log.WithFields(log.Fields{ + "Topic": "Peer", + "Name": name, + }).Info("Add a peer group configuration") + + server.peerGroupMap[c.Config.PeerGroupName] = NewPeerGroup(c) + + return nil +} + +func (server *BgpServer) addNeighbor(c *config.Neighbor) error { addr := c.Config.NeighborAddress if _, y := server.neighborMap[addr]; y { return fmt.Errorf("Can't overwrite the existing peer: %s", addr) } + if c.Config.PeerGroup != "" { + if err := config.OverwriteNeighborConfigWithPeerGroup(c, server.peerGroupMap[c.Config.PeerGroup].Conf); err != nil { + return err + } + } + + if err := config.SetDefaultNeighborConfigValues(c, server.bgpConfig.Global.Config.As); err != nil { + return err + } + if vrf := c.Config.Vrf; vrf != "" { if c.RouteServer.Config.RouteServerClient { return fmt.Errorf("route server client can't be enslaved to VRF") @@ -1668,18 +1691,50 @@ func (server *BgpServer) addNeighbor(c *config.Neighbor) error { } } server.neighborMap[addr] = peer + if name := c.Config.PeerGroup; name != "" { + server.peerGroupMap[name].AddMember(*c) + } peer.startFSMHandler(server.fsmincomingCh, server.fsmStateCh) server.broadcastPeerState(peer, bgp.BGP_FSM_IDLE) return nil } +func (s *BgpServer) AddPeerGroup(c *config.PeerGroup) error { + return s.mgmtOperation(func() error { + return s.addPeerGroup(c) + }, true) +} + func (s *BgpServer) AddNeighbor(c *config.Neighbor) error { return s.mgmtOperation(func() error { return s.addNeighbor(c) }, true) } +func (server *BgpServer) deletePeerGroup(pg *config.PeerGroup) error { + name := pg.Config.PeerGroupName + + if _, y := server.peerGroupMap[name]; !y { + return fmt.Errorf("Can't delete a peer-group %s which does not exist", name) + } + + log.WithFields(log.Fields{ + "Topic": "Peer", + "Name": name, + }).Info("Delete a peer group configuration") + + delete(server.peerGroupMap, name) + return nil +} + func (server *BgpServer) deleteNeighbor(c *config.Neighbor, code, subcode uint8) error { + if c.Config.PeerGroup != "" { + _, y := server.peerGroupMap[c.Config.PeerGroup] + if y { + server.peerGroupMap[c.Config.PeerGroup].DeleteMember(*c) + } + } + addr := c.Config.NeighborAddress if intf := c.Config.NeighborInterface; intf != "" { var err error @@ -1742,90 +1797,141 @@ func (server *BgpServer) deleteNeighbor(c *config.Neighbor, code, subcode uint8) return nil } +func (s *BgpServer) DeletePeerGroup(c *config.PeerGroup) error { + return s.mgmtOperation(func() error { + name := c.Config.PeerGroupName + for _, n := range s.neighborMap { + if n.fsm.pConf.Config.PeerGroup == name { + return fmt.Errorf("failed to delete peer-group %s: neighbor %s is in use", name, n.ID()) + } + } + return s.deletePeerGroup(c) + }, true) +} + func (s *BgpServer) DeleteNeighbor(c *config.Neighbor) error { return s.mgmtOperation(func() error { return s.deleteNeighbor(c, bgp.BGP_ERROR_CEASE, bgp.BGP_ERROR_SUB_PEER_DECONFIGURED) }, true) } -func (s *BgpServer) UpdateNeighbor(c *config.Neighbor) (needsSoftResetIn bool, err error) { - err = s.mgmtOperation(func() error { - addr := c.Config.NeighborAddress - peer, ok := s.neighborMap[addr] - if !ok { - return fmt.Errorf("Neighbor that has %v doesn't exist.", addr) +func (s *BgpServer) updatePeerGroup(pg *config.PeerGroup) (needsSoftResetIn bool, err error) { + name := pg.Config.PeerGroupName + + _, ok := s.peerGroupMap[name] + if !ok { + return false, fmt.Errorf("Peer-group %s doesn't exist.", name) + } + s.peerGroupMap[name].Conf = pg + + for _, n := range s.peerGroupMap[name].members { + c := n + u, err := s.updateNeighbor(&c) + if err != nil { + return needsSoftResetIn, err } + needsSoftResetIn = needsSoftResetIn || u + } + return needsSoftResetIn, nil +} - if !peer.fsm.pConf.ApplyPolicy.Equal(&c.ApplyPolicy) { - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": addr, - }).Info("Update ApplyPolicy") - s.policy.Reset(nil, map[string]config.ApplyPolicy{peer.ID(): c.ApplyPolicy}) - peer.fsm.pConf.ApplyPolicy = c.ApplyPolicy - needsSoftResetIn = true +func (s *BgpServer) UpdatePeerGroup(pg *config.PeerGroup) (needsSoftResetIn bool, err error) { + err = s.mgmtOperation(func() error { + needsSoftResetIn, err = s.updatePeerGroup(pg) + return err + }, true) + return needsSoftResetIn, err +} + +func (s *BgpServer) updateNeighbor(c *config.Neighbor) (needsSoftResetIn bool, err error) { + if c.Config.PeerGroup != "" { + if err := config.OverwriteNeighborConfigWithPeerGroup(c, s.peerGroupMap[c.Config.PeerGroup].Conf); err != nil { + return needsSoftResetIn, err } - original := peer.fsm.pConf + } + + addr := c.Config.NeighborAddress + peer, ok := s.neighborMap[addr] + if !ok { + return needsSoftResetIn, fmt.Errorf("Neighbor that has %v doesn't exist.", addr) + } + + if !peer.fsm.pConf.ApplyPolicy.Equal(&c.ApplyPolicy) { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": addr, + }).Info("Update ApplyPolicy") + s.policy.Reset(nil, map[string]config.ApplyPolicy{peer.ID(): c.ApplyPolicy}) + peer.fsm.pConf.ApplyPolicy = c.ApplyPolicy + needsSoftResetIn = true + } + original := peer.fsm.pConf - if !original.AsPathOptions.Config.Equal(&c.AsPathOptions.Config) { + if !original.AsPathOptions.Config.Equal(&c.AsPathOptions.Config) { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": peer.ID(), + }).Info("Update aspath options") + peer.fsm.pConf.AsPathOptions = c.AsPathOptions + needsSoftResetIn = true + } + + if !original.Config.Equal(&c.Config) || !original.Transport.Config.Equal(&c.Transport.Config) || config.CheckAfiSafisChange(original.AfiSafis, c.AfiSafis) { + sub := uint8(bgp.BGP_ERROR_SUB_OTHER_CONFIGURATION_CHANGE) + if original.Config.AdminDown != c.Config.AdminDown { + sub = bgp.BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN + state := "Admin Down" + if c.Config.AdminDown == false { + state = "Admin Up" + } log.WithFields(log.Fields{ "Topic": "Peer", "Key": peer.ID(), - }).Info("Update aspath options") - peer.fsm.pConf.AsPathOptions = c.AsPathOptions - needsSoftResetIn = true - } - - if !original.Config.Equal(&c.Config) || !original.Transport.Config.Equal(&c.Transport.Config) || config.CheckAfiSafisChange(original.AfiSafis, c.AfiSafis) { - sub := uint8(bgp.BGP_ERROR_SUB_OTHER_CONFIGURATION_CHANGE) - if original.Config.AdminDown != c.Config.AdminDown { - sub = bgp.BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN - state := "Admin Down" - if c.Config.AdminDown == false { - state = "Admin Up" - } - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": peer.ID(), - "State": state, - }).Info("Update admin-state configuration") - } else if original.Config.PeerAs != c.Config.PeerAs { - sub = bgp.BGP_ERROR_SUB_PEER_DECONFIGURED - } - if err = s.deleteNeighbor(peer.fsm.pConf, bgp.BGP_ERROR_CEASE, sub); err != nil { - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": addr, - }).Error(err) - return err - } - err = s.addNeighbor(c) - if err != nil { - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": addr, - }).Error(err) - } - return err + "State": state, + }).Info("Update admin-state configuration") + } else if original.Config.PeerAs != c.Config.PeerAs { + sub = bgp.BGP_ERROR_SUB_PEER_DECONFIGURED } - - if !original.Timers.Config.Equal(&c.Timers.Config) { + if err = s.deleteNeighbor(peer.fsm.pConf, bgp.BGP_ERROR_CEASE, sub); err != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.ID(), - }).Info("Update timer configuration") - peer.fsm.pConf.Timers.Config = c.Timers.Config + "Key": addr, + }).Error(err) + return needsSoftResetIn, err } - - err = peer.updatePrefixLimitConfig(c.AfiSafis) + err = s.addNeighbor(c) if err != nil { log.WithFields(log.Fields{ "Topic": "Peer", "Key": addr, }).Error(err) - // rollback to original state - peer.fsm.pConf = original } + return needsSoftResetIn, err + } + + if !original.Timers.Config.Equal(&c.Timers.Config) { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": peer.ID(), + }).Info("Update timer configuration") + peer.fsm.pConf.Timers.Config = c.Timers.Config + } + + err = peer.updatePrefixLimitConfig(c.AfiSafis) + if err != nil { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": addr, + }).Error(err) + // rollback to original state + peer.fsm.pConf = original + } + return needsSoftResetIn, err +} + +func (s *BgpServer) UpdateNeighbor(c *config.Neighbor) (needsSoftResetIn bool, err error) { + err = s.mgmtOperation(func() error { + needsSoftResetIn, err = s.updateNeighbor(c) return err }, true) return needsSoftResetIn, err |