From 0b5bb073128b3000f383927a997d4a40d274f569 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Wed, 5 Aug 2015 15:48:25 +0900 Subject: fix policy dynamic update handle dynamic policy peer binding Signed-off-by: FUJITA Tomonori --- config/serve.go | 21 +++++++++++++-------- gobgpd/main.go | 8 +++++++- server/server.go | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/config/serve.go b/config/serve.go index 74529a16..a3beea30 100644 --- a/config/serve.go +++ b/config/serve.go @@ -35,16 +35,16 @@ func ReadConfigfileServe(path string, configCh chan BgpConfigSet, reloadCh chan } } -func inSlice(n Neighbor, b []Neighbor) bool { - for _, nb := range b { +func inSlice(n Neighbor, b []Neighbor) int { + for i, nb := range b { if nb.NeighborConfig.NeighborAddress.String() == n.NeighborConfig.NeighborAddress.String() { - return true + return i } } - return false + return -1 } -func UpdateConfig(curC *Bgp, newC *Bgp) (*Bgp, []Neighbor, []Neighbor) { +func UpdateConfig(curC *Bgp, newC *Bgp) (*Bgp, []Neighbor, []Neighbor, []Neighbor) { bgpConfig := Bgp{} if curC == nil { bgpConfig.Global = newC.Global @@ -55,21 +55,26 @@ func UpdateConfig(curC *Bgp, newC *Bgp) (*Bgp, []Neighbor, []Neighbor) { } added := []Neighbor{} deleted := []Neighbor{} + updated := []Neighbor{} for _, n := range newC.Neighbors.NeighborList { - if inSlice(n, curC.Neighbors.NeighborList) == false { + if idx := inSlice(n, curC.Neighbors.NeighborList); idx < 0 { added = append(added, n) + } else { + if !reflect.DeepEqual(n.ApplyPolicy, curC.Neighbors.NeighborList[idx].ApplyPolicy) { + updated = append(updated, n) + } } } for _, n := range curC.Neighbors.NeighborList { - if inSlice(n, newC.Neighbors.NeighborList) == false { + if inSlice(n, newC.Neighbors.NeighborList) < 0 { deleted = append(deleted, n) } } bgpConfig.Neighbors.NeighborList = newC.Neighbors.NeighborList - return &bgpConfig, added, deleted + return &bgpConfig, added, deleted, updated } func CheckPolicyDifference(currentPolicy *RoutingPolicy, newPolicy *RoutingPolicy) bool { diff --git a/gobgpd/main.go b/gobgpd/main.go index cf327f61..7a3b3ef2 100644 --- a/gobgpd/main.go +++ b/gobgpd/main.go @@ -167,14 +167,16 @@ func main() { case newConfig := <-configCh: var added []config.Neighbor var deleted []config.Neighbor + var updated []config.Neighbor if bgpConfig == nil { bgpServer.SetGlobalType(newConfig.Bgp.Global) bgpConfig = &newConfig.Bgp added = newConfig.Bgp.Neighbors.NeighborList deleted = []config.Neighbor{} + updated = []config.Neighbor{} } else { - bgpConfig, added, deleted = config.UpdateConfig(bgpConfig, &newConfig.Bgp) + bgpConfig, added, deleted, updated = config.UpdateConfig(bgpConfig, &newConfig.Bgp) } if policyConfig == nil { @@ -195,6 +197,10 @@ func main() { log.Infof("Peer %v is deleted", p.NeighborConfig.NeighborAddress) bgpServer.PeerDelete(p) } + for _, p := range updated { + log.Infof("Peer %v is updated", p.NeighborConfig.NeighborAddress) + bgpServer.PeerUpdate(p) + } case sig := <-sigCh: switch sig { case syscall.SIGHUP: diff --git a/server/server.go b/server/server.go index 78a15da9..46184697 100644 --- a/server/server.go +++ b/server/server.go @@ -73,6 +73,7 @@ type BgpServer struct { globalTypeCh chan config.Global addedPeerCh chan config.Neighbor deletedPeerCh chan config.Neighbor + updatedPeerCh chan config.Neighbor GrpcReqCh chan *GrpcRequest listenPort int policyUpdateCh chan config.RoutingPolicy @@ -91,6 +92,7 @@ func NewBgpServer(port int, roaURL string) *BgpServer { b.globalTypeCh = make(chan config.Global) b.addedPeerCh = make(chan config.Neighbor) b.deletedPeerCh = make(chan config.Neighbor) + b.updatedPeerCh = make(chan config.Neighbor) b.GrpcReqCh = make(chan *GrpcRequest, 1) b.policyUpdateCh = make(chan config.RoutingPolicy) b.localRibMap = make(map[string]*LocalRib) @@ -300,6 +302,15 @@ func (server *BgpServer) Serve() { } else { log.Info("Can't delete a peer configuration for ", addr) } + case config := <-server.updatedPeerCh: + addr := config.NeighborConfig.NeighborAddress.String() + peer := server.neighborMap[addr] + if peer.conf.RouteServer.RouteServerClient == true { + peer.conf.ApplyPolicy = config.ApplyPolicy + loc := server.localRibMap[addr] + loc.setPolicy(peer, server.policyMap) + peer.setDistributePolicy(server.policyMap) + } case e := <-incoming: peer, found := server.neighborMap[e.MsgSrc] if !found { @@ -695,6 +706,10 @@ func (server *BgpServer) PeerDelete(peer config.Neighbor) { server.deletedPeerCh <- peer } +func (server *BgpServer) PeerUpdate(peer config.Neighbor) { + server.updatedPeerCh <- peer +} + func (server *BgpServer) UpdatePolicy(policy config.RoutingPolicy) { server.policyUpdateCh <- policy } -- cgit v1.2.3