From be2524ee3e158a0d248d730f727da80bab09aed8 Mon Sep 17 00:00:00 2001 From: Satoshi Fujimoto Date: Tue, 13 Jun 2017 14:04:50 +0900 Subject: config: Use State to get NeighborAddress instead of Config By introducing dynamic neighbor feature, GoBGP can have dynamic neighbors, and corresponding Config.NeighboAddress can be empty. This patch sets State.NeighborAddress for all neighbors, and gets the neighbor's address from them. Signed-off-by: Satoshi Fujimoto --- api/grpc_server.go | 2 +- client/client.go | 4 +- config/default.go | 11 +++--- config/serve.go | 2 +- config/util.go | 11 ++++++ gobgp/cmd/common.go | 4 +- gobgp/cmd/monitor.go | 2 +- gobgp/cmd/neighbor.go | 17 +++++---- gobgpd/main.go | 6 +-- server/bmp.go | 2 +- server/fsm.go | 100 +++++++++++++++++++++++++------------------------- server/mrt.go | 4 +- server/peer.go | 12 +++--- server/server.go | 31 +++++++++++----- table/destination.go | 2 +- table/path.go | 2 +- 16 files changed, 119 insertions(+), 93 deletions(-) diff --git a/api/grpc_server.go b/api/grpc_server.go index 213d32cf..c973e24b 100644 --- a/api/grpc_server.go +++ b/api/grpc_server.go @@ -862,7 +862,6 @@ func (s *Server) DeleteVrf(ctx context.Context, arg *DeleteVrfRequest) (*DeleteV func NewNeighborFromAPIStruct(a *Peer) (*config.Neighbor, error) { pconf := &config.Neighbor{} if a.Conf != nil { - pconf.Config.NeighborAddress = a.Conf.NeighborAddress pconf.Config.PeerAs = a.Conf.PeerAs pconf.Config.LocalAs = a.Conf.LocalAs pconf.Config.AuthPassword = a.Conf.AuthPassword @@ -993,6 +992,7 @@ func NewNeighborFromAPIStruct(a *Peer) (*config.Neighbor, error) { pconf.State.AdjTable.Advertised = a.Info.Advertised pconf.State.PeerAs = a.Info.PeerAs pconf.State.PeerType = config.IntToPeerTypeMap[int(a.Info.PeerType)] + pconf.State.NeighborAddress = a.Info.NeighborAddress if a.Info.Messages != nil { if a.Info.Messages.Sent != nil { diff --git a/client/client.go b/client/client.go index 4a94ac4a..b9faf6ba 100644 --- a/client/client.go +++ b/client/client.go @@ -136,14 +136,14 @@ func (cli *Client) getNeighbor(name string, afi int, vrf string, enableAdvertise neighbors := make([]*config.Neighbor, 0, len(ret.Peers)) for _, p := range ret.Peers { - if name != "" && name != p.Conf.NeighborAddress && name != p.Conf.NeighborInterface { + if name != "" && name != p.Info.NeighborAddress && name != p.Conf.NeighborInterface { continue } if vrf != "" && name != p.Conf.Vrf { continue } if afi > 0 { - v6 := net.ParseIP(p.Conf.NeighborAddress).To4() == nil + v6 := net.ParseIP(p.Info.NeighborAddress).To4() == nil if afi == bgp.AFI_IP && v6 || afi == bgp.AFI_IP6 && !v6 { continue } diff --git a/config/default.go b/config/default.go index 3b0db592..1ff57adb 100644 --- a/config/default.go +++ b/config/default.go @@ -131,6 +131,7 @@ func setDefaultNeighborConfigValuesWithViper(v *viper.Viper, n *Neighbor, asn ui } n.State.PeerAs = n.Config.PeerAs + n.State.NeighborAddress = n.Config.NeighborAddress n.AsPathOptions.State.AllowOwnAs = n.AsPathOptions.Config.AllowOwnAs if !v.IsSet("neighbor.timers.config.connect-retry") && n.Timers.Config.ConnectRetry == 0 { @@ -154,14 +155,14 @@ func setDefaultNeighborConfigValuesWithViper(v *viper.Viper, n *Neighbor, asn ui if err != nil { return err } - n.Config.NeighborAddress = addr + n.State.NeighborAddress = addr } if n.Transport.Config.LocalAddress == "" { - if n.Config.NeighborAddress == "" { + if n.State.NeighborAddress == "" { return fmt.Errorf("no neighbor address/interface specified") } - ipAddr, err := net.ResolveIPAddr("ip", n.Config.NeighborAddress) + ipAddr, err := net.ResolveIPAddr("ip", n.State.NeighborAddress) if err != nil { return err } @@ -184,8 +185,8 @@ func setDefaultNeighborConfigValuesWithViper(v *viper.Viper, n *Neighbor, asn ui defaultAfiSafi(AFI_SAFI_TYPE_IPV4_UNICAST, true), defaultAfiSafi(AFI_SAFI_TYPE_IPV6_UNICAST, true), } - } else if ipAddr, err := net.ResolveIPAddr("ip", n.Config.NeighborAddress); err != nil { - return fmt.Errorf("invalid neighbor address: %s", n.Config.NeighborAddress) + } else if ipAddr, err := net.ResolveIPAddr("ip", n.State.NeighborAddress); err != nil { + return fmt.Errorf("invalid neighbor address: %s", n.State.NeighborAddress) } else if ipAddr.IP.To4() != nil { n.AfiSafis = []AfiSafi{defaultAfiSafi(AFI_SAFI_TYPE_IPV4_UNICAST, true)} } else { diff --git a/config/serve.go b/config/serve.go index 2df31558..8dfda75e 100644 --- a/config/serve.go +++ b/config/serve.go @@ -76,7 +76,7 @@ func ReadConfigfileServe(path, format string, configCh chan *BgpConfigSet) { func inSlice(n Neighbor, b []Neighbor) int { for i, nb := range b { - if nb.Config.NeighborAddress == n.Config.NeighborAddress { + if nb.State.NeighborAddress == n.State.NeighborAddress { return i } } diff --git a/config/util.go b/config/util.go index 36999411..8ecf56d5 100644 --- a/config/util.go +++ b/config/util.go @@ -140,3 +140,14 @@ func ParseMaskLength(prefix, mask string) (int, int, error) { } return min, max, nil } + +func ExtractNeighborAddress(c *Neighbor) (string, error) { + addr := c.State.NeighborAddress + if addr == "" { + addr = c.Config.NeighborAddress + if addr == "" { + return "", fmt.Errorf("NeighborAddress is not configured") + } + } + return addr, nil +} diff --git a/gobgp/cmd/common.go b/gobgp/cmd/common.go index a104c567..124c2b99 100644 --- a/gobgp/cmd/common.go +++ b/gobgp/cmd/common.go @@ -178,8 +178,8 @@ func (n neighbors) Swap(i, j int) { } func (n neighbors) Less(i, j int) bool { - p1 := n[i].Config.NeighborAddress - p2 := n[j].Config.NeighborAddress + p1 := n[i].State.NeighborAddress + p2 := n[j].State.NeighborAddress p1Isv4 := !strings.Contains(p1, ":") p2Isv4 := !strings.Contains(p2, ":") if p1Isv4 != p2Isv4 { diff --git a/gobgp/cmd/monitor.go b/gobgp/cmd/monitor.go index 661b6c24..9d2d841f 100644 --- a/gobgp/cmd/monitor.go +++ b/gobgp/cmd/monitor.go @@ -89,7 +89,7 @@ func NewMonitorCmd() *cobra.Command { j, _ := json.Marshal(s) fmt.Println(string(j)) } else { - addr := s.Config.NeighborAddress + addr := s.State.NeighborAddress if s.Config.NeighborInterface != "" { addr = fmt.Sprintf("%s(%s)", addr, s.Config.NeighborInterface) } diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 261e3d58..ffd835fc 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -77,7 +77,7 @@ func showNeighbors(vrf string) error { if globalOpts.Quiet { for _, p := range m { - fmt.Println(p.Config.NeighborAddress) + fmt.Println(p.State.NeighborAddress) } return nil } @@ -92,7 +92,7 @@ func showNeighbors(vrf string) error { for _, n := range m { if i := len(n.Config.NeighborInterface); i > maxaddrlen { maxaddrlen = i - } else if j := len(n.Config.NeighborAddress); j > maxaddrlen { + } else if j := len(n.State.NeighborAddress); j > maxaddrlen { maxaddrlen = j } if l := len(getASN(n)); l > maxaslen { @@ -142,7 +142,7 @@ func showNeighbors(vrf string) error { } for i, n := range m { - neigh := n.Config.NeighborAddress + neigh := n.State.NeighborAddress if n.Config.NeighborInterface != "" { neigh = n.Config.NeighborInterface } @@ -163,7 +163,7 @@ func showNeighbor(args []string) error { return nil } - fmt.Printf("BGP neighbor is %s, remote AS %s", p.Config.NeighborAddress, getASN(p)) + fmt.Printf("BGP neighbor is %s, remote AS %s", p.State.NeighborAddress, getASN(p)) if p.RouteReflector.Config.RouteReflectorClient { fmt.Printf(", route-reflector-client\n") @@ -854,6 +854,7 @@ func modNeighbor(cmdType string, args []string) error { peer.Config.NeighborInterface = m["interface"][0] } else { peer.Config.NeighborAddress = m[""][0] + peer.State.NeighborAddress = m[""][0] } if len(m["vrf"]) == 1 { peer.Config.Vrf = m["vrf"][0] @@ -952,7 +953,7 @@ func NewNeighborCmd() *cobra.Command { if err != nil { exitWithError(err) } - addr = peer.Config.NeighborAddress + addr = peer.State.NeighborAddress } err := f(cmd.Use, addr, args[:len(args)-1]) if err != nil { @@ -983,7 +984,7 @@ func NewNeighborCmd() *cobra.Command { if err != nil { exitWithError(err) } - remoteIP := peer.Config.NeighborAddress + remoteIP := peer.State.NeighborAddress for _, v := range []string{CMD_IN, CMD_IMPORT, CMD_EXPORT} { if err := showNeighborPolicy(remoteIP, v, 4); err != nil { exitWithError(err) @@ -1000,7 +1001,7 @@ func NewNeighborCmd() *cobra.Command { if err != nil { exitWithError(err) } - remoteIP := peer.Config.NeighborAddress + remoteIP := peer.State.NeighborAddress err = showNeighborPolicy(remoteIP, cmd.Use, 0) if err != nil { exitWithError(err) @@ -1016,7 +1017,7 @@ func NewNeighborCmd() *cobra.Command { if err != nil { exitWithError(err) } - remoteIP := peer.Config.NeighborAddress + remoteIP := peer.State.NeighborAddress args = args[:len(args)-1] if err = modNeighborPolicy(remoteIP, cmd.Use, subcmd.Use, args); err != nil { exitWithError(err) diff --git a/gobgpd/main.go b/gobgpd/main.go index f1697715..4b4b5047 100644 --- a/gobgpd/main.go +++ b/gobgpd/main.go @@ -267,19 +267,19 @@ func main() { updatePolicy = updatePolicy || u } for i, p := range added { - log.Infof("Peer %v is added", p.Config.NeighborAddress) + log.Infof("Peer %v is added", p.State.NeighborAddress) if err := bgpServer.AddNeighbor(&added[i]); err != nil { log.Warn(err) } } for i, p := range deleted { - log.Infof("Peer %v is deleted", p.Config.NeighborAddress) + log.Infof("Peer %v is deleted", p.State.NeighborAddress) if err := bgpServer.DeleteNeighbor(&deleted[i]); err != nil { log.Warn(err) } } for i, p := range updated { - log.Infof("Peer %v is updated", p.Config.NeighborAddress) + log.Infof("Peer %v is updated", p.State.NeighborAddress) u, err := bgpServer.UpdateNeighbor(&updated[i]) if err != nil { log.Warn(err) diff --git a/server/bmp.go b/server/bmp.go index 4d76822b..1e12e372 100644 --- a/server/bmp.go +++ b/server/bmp.go @@ -289,7 +289,7 @@ func bmpPeerRoute(t uint8, policy bool, pd uint64, peeri *table.PeerInfo, timest func bmpPeerStats(peerType uint8, peerDist uint64, timestamp int64, neighConf *config.Neighbor) *bmp.BMPMessage { var peerFlags uint8 = 0 - ph := bmp.NewBMPPeerHeader(peerType, peerFlags, peerDist, neighConf.Config.NeighborAddress, neighConf.State.PeerAs, neighConf.State.RemoteRouterId, float64(timestamp)) + ph := bmp.NewBMPPeerHeader(peerType, peerFlags, peerDist, neighConf.State.NeighborAddress, neighConf.State.PeerAs, neighConf.State.RemoteRouterId, float64(timestamp)) return bmp.NewBMPStatisticsReport( *ph, []bmp.BMPStatsTLVInterface{ diff --git a/server/fsm.go b/server/fsm.go index 3817bd23..2c01c14d 100644 --- a/server/fsm.go +++ b/server/fsm.go @@ -227,7 +227,7 @@ func NewFSM(gConf *config.Global, pConf *config.Neighbor, policy *table.RoutingP func (fsm *FSM) StateChange(nextState bgp.FSMState) { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "old": fsm.state.String(), "new": nextState.String(), "reason": fsm.reason, @@ -298,12 +298,12 @@ func (fsm *FSM) sendNotificationFromErrorMsg(e *bgp.MessageError) error { fsm.h.conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "Data": e, }).Warn("sent notification") return nil } - return fmt.Errorf("can't send notification to %s since TCP connection is not established", fsm.pConf.Config.NeighborAddress) + return fmt.Errorf("can't send notification to %s since TCP connection is not established", fsm.pConf.State.NeighborAddress) } func (fsm *FSM) sendNotification(code, subType uint8, data []byte, msg string) error { @@ -323,7 +323,7 @@ func (fsm *FSM) connectLoop() error { timer.Stop() connect := func() { - addr := fsm.pConf.Config.NeighborAddress + addr := fsm.pConf.State.NeighborAddress port := int(bgp.BGP_PORT) if fsm.pConf.Transport.Config.RemotePort != 0 { port = int(fsm.pConf.Transport.Config.RemotePort) @@ -342,7 +342,7 @@ func (fsm *FSM) connectLoop() error { if e != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, }).Warnf("failed to resolve ltcpaddr: %s", e) return } @@ -358,13 +358,13 @@ func (fsm *FSM) connectLoop() error { conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, }).Warn("active conn is closed to avoid being blocked") } } else { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, }).Debugf("failed to connect: %s", err) } @@ -378,7 +378,7 @@ func (fsm *FSM) connectLoop() error { case <-fsm.t.Dying(): log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, }).Debug("stop connect loop") return nil case <-timer.C: @@ -429,7 +429,7 @@ func (h *FSMHandler) idle() (bgp.FSMState, FsmStateReason) { if fsm.pConf.GracefulRestart.State.PeerRestarting { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("graceful restart timer expired") return bgp.BGP_FSM_IDLE, FSM_RESTART_TIMER_EXPIRED @@ -441,7 +441,7 @@ func (h *FSMHandler) idle() (bgp.FSMState, FsmStateReason) { conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("Closed an accepted connection") case <-idleHoldTimer.C: @@ -449,7 +449,7 @@ func (h *FSMHandler) idle() (bgp.FSMState, FsmStateReason) { if fsm.adminState == ADMIN_STATE_UP { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "Duration": fsm.idleHoldTime, }).Debug("IdleHoldTimer expired") fsm.idleHoldTime = HOLDTIME_IDLE @@ -503,7 +503,7 @@ func (h *FSMHandler) active() (bgp.FSMState, FsmStateReason) { if fsm.pConf.GracefulRestart.State.PeerRestarting { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("graceful restart timer expired") return bgp.BGP_FSM_IDLE, FSM_RESTART_TIMER_EXPIRED @@ -519,7 +519,7 @@ func (h *FSMHandler) active() (bgp.FSMState, FsmStateReason) { case ADMIN_STATE_UP: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "AdminState": stateOp.State.String(), }).Panic("code logic bug") @@ -658,13 +658,13 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { h.fsm.bgpMessageStateUpdate(0, true) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, + "Key": h.fsm.pConf.State.NeighborAddress, "State": h.fsm.state.String(), "error": err, }).Warn("malformed BGP Header") fmsg := &FsmMsg{ MsgType: FSM_MSG_BGP_MESSAGE, - MsgSrc: h.fsm.pConf.Config.NeighborAddress, + MsgSrc: h.fsm.pConf.State.NeighborAddress, MsgData: err, Version: h.fsm.version, } @@ -687,14 +687,14 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { } fmsg := &FsmMsg{ MsgType: FSM_MSG_BGP_MESSAGE, - MsgSrc: h.fsm.pConf.Config.NeighborAddress, + MsgSrc: h.fsm.pConf.State.NeighborAddress, timestamp: now, Version: h.fsm.version, } if err != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, + "Key": h.fsm.pConf.State.NeighborAddress, "State": h.fsm.state.String(), "error": err, }).Warn("malformed BGP message") @@ -717,7 +717,7 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { if err != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, + "Key": h.fsm.pConf.State.NeighborAddress, "State": h.fsm.state.String(), "error": err, }).Warn("malformed BGP update message") @@ -752,7 +752,7 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { } fmsg.PathList = table.ProcessMessage(m, h.fsm.peerInfo, fmsg.timestamp) - id := h.fsm.pConf.Config.NeighborAddress + id := h.fsm.pConf.State.NeighborAddress for _, path := range fmsg.PathList { if path.IsEOR() { continue @@ -779,7 +779,7 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { communication, rest := decodeAdministrativeCommunication(body.Data) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, + "Key": h.fsm.pConf.State.NeighborAddress, "Code": body.ErrorCode, "Subcode": body.ErrorSubcode, "Communicated-Reason": communication, @@ -788,7 +788,7 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { } else { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, + "Key": h.fsm.pConf.State.NeighborAddress, "Code": body.ErrorCode, "Subcode": body.ErrorSubcode, "Data": body.Data, @@ -880,14 +880,14 @@ func (h *FSMHandler) opensent() (bgp.FSMState, FsmStateReason) { conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("Closed an accepted connection") case <-fsm.gracefulRestartTimer.C: if fsm.pConf.GracefulRestart.State.PeerRestarting { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("graceful restart timer expired") h.conn.Close() @@ -919,7 +919,7 @@ func (h *FSMHandler) opensent() (bgp.FSMState, FsmStateReason) { fsm.pConf.State.PeerType = typ log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Infof("skiped asn negotiation: peer-as: %d, peer-type: %s", peerAs, typ) } else { @@ -974,7 +974,7 @@ func (h *FSMHandler) opensent() (bgp.FSMState, FsmStateReason) { if fsm.pConf.GracefulRestart.State.PeerRestarting && cap.Flags&0x08 == 0 { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("restart flag is not set") // send notification? @@ -1018,7 +1018,7 @@ func (h *FSMHandler) opensent() (bgp.FSMState, FsmStateReason) { default: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Data": e.MsgData, }).Panic("unknown msg type") @@ -1040,7 +1040,7 @@ func (h *FSMHandler) opensent() (bgp.FSMState, FsmStateReason) { case ADMIN_STATE_UP: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "AdminState": stateOp.State.String(), }).Panic("code logic bug") @@ -1091,14 +1091,14 @@ func (h *FSMHandler) openconfirm() (bgp.FSMState, FsmStateReason) { conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("Closed an accepted connection") case <-fsm.gracefulRestartTimer.C: if fsm.pConf.GracefulRestart.State.PeerRestarting { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("graceful restart timer expired") h.conn.Close() @@ -1130,7 +1130,7 @@ func (h *FSMHandler) openconfirm() (bgp.FSMState, FsmStateReason) { default: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Data": e.MsgData, }).Panic("unknown msg type") @@ -1152,7 +1152,7 @@ func (h *FSMHandler) openconfirm() (bgp.FSMState, FsmStateReason) { case ADMIN_STATE_UP: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "AdminState": stateOp.State.String(), }).Panic("code logic bug") @@ -1170,7 +1170,7 @@ func (h *FSMHandler) sendMessageloop() error { if fsm.twoByteAsTrans && m.Header.Type == bgp.BGP_MSG_UPDATE { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Data": m, }).Debug("update for 2byte AS peer") @@ -1181,7 +1181,7 @@ func (h *FSMHandler) sendMessageloop() error { if err != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Data": err, }).Warn("failed to serialize") @@ -1197,7 +1197,7 @@ func (h *FSMHandler) sendMessageloop() error { if err != nil { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Data": err, }).Warn("failed to send") @@ -1214,7 +1214,7 @@ func (h *FSMHandler) sendMessageloop() error { communication, rest := decodeAdministrativeCommunication(body.Data) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Code": body.ErrorCode, "Subcode": body.ErrorSubcode, @@ -1224,7 +1224,7 @@ func (h *FSMHandler) sendMessageloop() error { } else { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Code": body.ErrorCode, "Subcode": body.ErrorSubcode, @@ -1238,7 +1238,7 @@ func (h *FSMHandler) sendMessageloop() error { update := m.Body.(*bgp.BGPUpdate) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "nlri": update.NLRI, "withdrawals": update.WithdrawnRoutes, @@ -1247,7 +1247,7 @@ func (h *FSMHandler) sendMessageloop() error { default: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "data": m, }).Debug("sent") @@ -1323,7 +1323,7 @@ func (h *FSMHandler) established() (bgp.FSMState, FsmStateReason) { conn.Close() log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("Closed an accepted connection") case err := <-h.errorCh: @@ -1333,7 +1333,7 @@ func (h *FSMHandler) established() (bgp.FSMState, FsmStateReason) { err = FSM_GRACEFUL_RESTART log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Info("peer graceful restart") fsm.gracefulRestartTimer.Reset(time.Duration(fsm.pConf.GracefulRestart.State.PeerRestartTime) * time.Second) @@ -1342,7 +1342,7 @@ func (h *FSMHandler) established() (bgp.FSMState, FsmStateReason) { case <-holdTimer.C: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("hold timer expired") m := bgp.NewBGPNotificationMessage(bgp.BGP_ERROR_HOLD_TIMER_EXPIRED, 0, nil) @@ -1399,7 +1399,7 @@ func (h *FSMHandler) loop() error { if nextState == bgp.BGP_FSM_ESTABLISHED && oldState == bgp.BGP_FSM_OPENCONFIRM { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Info("Peer Up") } @@ -1413,14 +1413,14 @@ func (h *FSMHandler) loop() error { } log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "Reason": reason, }).Info("Peer Down") } e := time.AfterFunc(time.Second*120, func() { - log.WithFields(log.Fields{"Topic": "Peer"}).Fatalf("failed to free the fsm.h.t for %s %s %s", fsm.pConf.Config.NeighborAddress, oldState, nextState) + log.WithFields(log.Fields{"Topic": "Peer"}).Fatalf("failed to free the fsm.h.t for %s %s %s", fsm.pConf.State.NeighborAddress, oldState, nextState) }) h.t.Wait() e.Stop() @@ -1429,7 +1429,7 @@ func (h *FSMHandler) loop() error { if nextState >= bgp.BGP_FSM_IDLE { e := &FsmMsg{ MsgType: FSM_MSG_STATE_CHANGE, - MsgSrc: fsm.pConf.Config.NeighborAddress, + MsgSrc: fsm.pConf.State.NeighborAddress, MsgData: nextState, Version: h.fsm.version, } @@ -1443,7 +1443,7 @@ func (h *FSMHandler) changeAdminState(s AdminState) error { if fsm.adminState != s { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), "AdminState": s.String(), }).Debug("admin state changed") @@ -1455,19 +1455,19 @@ func (h *FSMHandler) changeAdminState(s AdminState) error { case ADMIN_STATE_UP: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Info("Administrative start") case ADMIN_STATE_DOWN: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Info("Administrative shutdown") case ADMIN_STATE_PFX_CT: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Info("Administrative shutdown(Prefix limit reached)") } @@ -1475,7 +1475,7 @@ func (h *FSMHandler) changeAdminState(s AdminState) error { } else { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, + "Key": fsm.pConf.State.NeighborAddress, "State": fsm.state.String(), }).Warn("cannot change to the same state") diff --git a/server/mrt.go b/server/mrt.go index f4a22658..e2cfa6fc 100644 --- a/server/mrt.go +++ b/server/mrt.go @@ -109,7 +109,7 @@ func (m *mrtWriter) loop() error { t := uint32(time.Now().Unix()) peers := make([]*mrt.Peer, 0, len(m.Neighbor)) for _, pconf := range m.Neighbor { - peers = append(peers, mrt.NewPeer(pconf.State.RemoteRouterId, pconf.Config.NeighborAddress, pconf.Config.PeerAs, true)) + peers = append(peers, mrt.NewPeer(pconf.State.RemoteRouterId, pconf.State.NeighborAddress, pconf.Config.PeerAs, true)) } if bm, err := mrt.NewMRTMessage(t, mrt.TABLE_DUMPv2, mrt.PEER_INDEX_TABLE, mrt.NewPeerIndexTable(m.RouterId, "", peers)); err != nil { break @@ -119,7 +119,7 @@ func (m *mrtWriter) loop() error { idx := func(p *table.Path) uint16 { for i, pconf := range m.Neighbor { - if p.GetSource().Address.String() == pconf.Config.NeighborAddress { + if p.GetSource().Address.String() == pconf.State.NeighborAddress { return uint16(i) } } diff --git a/server/peer.go b/server/peer.go index 8adc6a22..4e5b7435 100644 --- a/server/peer.go +++ b/server/peer.go @@ -44,11 +44,11 @@ func NewPeerGroup(c *config.PeerGroup) *PeerGroup { } func (pg *PeerGroup) AddMember(c config.Neighbor) { - pg.members[c.Config.NeighborAddress] = c + pg.members[c.State.NeighborAddress] = c } func (pg *PeerGroup) DeleteMember(c config.Neighbor) { - delete(pg.members, c.Config.NeighborAddress) + delete(pg.members, c.State.NeighborAddress) } type Peer struct { @@ -71,7 +71,7 @@ func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, p prefixLimitWarned: make(map[bgp.RouteFamily]bool), } if peer.isRouteServerClient() { - peer.tableId = conf.Config.NeighborAddress + peer.tableId = conf.State.NeighborAddress } else { peer.tableId = table.GLOBAL_RIB_NAME } @@ -81,7 +81,7 @@ func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, p } func (peer *Peer) ID() string { - return peer.fsm.pConf.Config.NeighborAddress + return peer.fsm.pConf.State.NeighborAddress } func (peer *Peer) TableID() string { @@ -360,7 +360,7 @@ func (peer *Peer) processOutgoingPaths(paths, olds []*table.Path) []*table.Path if peer.fsm.pConf.GracefulRestart.State.LocalRestarting { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, }).Debug("now syncing, suppress sending updates") return nil } @@ -478,7 +478,7 @@ func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bg update := m.Body.(*bgp.BGPUpdate) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, "nlri": update.NLRI, "withdrawals": update.WithdrawnRoutes, "attributes": update.PathAttributes, diff --git a/server/server.go b/server/server.go index ac2f20ef..7428d47e 100644 --- a/server/server.go +++ b/server/server.go @@ -801,7 +801,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { deferralExpiredFunc := func(family bgp.RouteFamily) func() { return func() { server.mgmtOperation(func() error { - server.softResetOut(peer.fsm.pConf.Config.NeighborAddress, family, true) + server.softResetOut(peer.fsm.pConf.State.NeighborAddress, family, true) return nil }, false) } @@ -864,6 +864,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { // clear counter if peer.fsm.adminState == ADMIN_STATE_DOWN { peer.fsm.pConf.State = config.NeighborState{} + peer.fsm.pConf.State.NeighborAddress = peer.fsm.pConf.Config.NeighborAddress peer.fsm.pConf.Timers.State = config.TimersState{} } peer.startFSMHandler(server.fsmincomingCh, server.fsmStateCh) @@ -970,7 +971,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { pathList := peer.adjRibIn.DropStale(peer.configuredRFlist()) log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, }).Debugf("withdraw %d stale routes", len(pathList)) server.propagateUpdate(peer, pathList) } @@ -1000,7 +1001,7 @@ func (server *BgpServer) handleFSMMessage(peer *Peer, e *FsmMsg) { default: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, "Data": e.MsgData, }).Panic("unknown msg type") } @@ -1061,7 +1062,7 @@ func (s *BgpServer) UpdatePolicy(policy config.RoutingPolicy) error { for _, peer := range s.neighborMap { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, }).Info("call set policy") ap[peer.ID()] = peer.fsm.pConf.ApplyPolicy } @@ -1622,7 +1623,11 @@ func (server *BgpServer) addPeerGroup(c *config.PeerGroup) error { } func (server *BgpServer) addNeighbor(c *config.Neighbor) error { - addr := c.Config.NeighborAddress + addr, err := config.ExtractNeighborAddress(c) + if err != nil { + return err + } + if _, y := server.neighborMap[addr]; y { return fmt.Errorf("Can't overwrite the existing peer: %s", addr) } @@ -1735,7 +1740,11 @@ func (server *BgpServer) deleteNeighbor(c *config.Neighbor, code, subcode uint8) } } - addr := c.Config.NeighborAddress + addr, err := config.ExtractNeighborAddress(c) + if err != nil { + return err + } + if intf := c.Config.NeighborInterface; intf != "" { var err error addr, err = config.GetIPv6LinkLocalNeighborAddress(intf) @@ -1850,7 +1859,11 @@ func (s *BgpServer) updateNeighbor(c *config.Neighbor) (needsSoftResetIn bool, e } } - addr := c.Config.NeighborAddress + addr, err := config.ExtractNeighborAddress(c) + if err != nil { + return err + } + peer, ok := s.neighborMap[addr] if !ok { return needsSoftResetIn, fmt.Errorf("Neighbor that has %v doesn't exist.", addr) @@ -1998,10 +2011,10 @@ func (s *BgpServer) setAdminState(addr, communication string, enable bool) error case peer.fsm.adminStateCh <- *stateOp: log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.fsm.pConf.Config.NeighborAddress, + "Key": peer.fsm.pConf.State.NeighborAddress, }).Debug(message) default: - log.Warning("previous request is still remaining. : ", peer.fsm.pConf.Config.NeighborAddress) + log.Warning("previous request is still remaining. : ", peer.fsm.pConf.State.NeighborAddress) } } if enable { diff --git a/table/destination.go b/table/destination.go index 21857e44..af74f8fd 100644 --- a/table/destination.go +++ b/table/destination.go @@ -127,7 +127,7 @@ func (i *PeerInfo) String() string { func NewPeerInfo(g *config.Global, p *config.Neighbor) *PeerInfo { id := net.ParseIP(string(p.RouteReflector.Config.RouteReflectorClusterId)).To4() // exclude zone info - naddr, _ := net.ResolveIPAddr("ip", p.Config.NeighborAddress) + naddr, _ := net.ResolveIPAddr("ip", p.State.NeighborAddress) return &PeerInfo{ AS: p.Config.PeerAs, LocalAS: g.Config.As, diff --git a/table/path.go b/table/path.go index bca2fe94..b97ca40c 100644 --- a/table/path.go +++ b/table/path.go @@ -258,7 +258,7 @@ func UpdatePathAttrs(global *config.Global, peer *config.Neighbor, info *PeerInf } else { log.WithFields(log.Fields{ "Topic": "Peer", - "Key": peer.Config.NeighborAddress, + "Key": peer.State.NeighborAddress, }).Warnf("invalid peer type: %d", peer.State.PeerType) } return path -- cgit v1.2.3