summaryrefslogtreecommitdiffhomepage
path: root/server/fsm.go
diff options
context:
space:
mode:
authorHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-07-15 19:53:53 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-07-31 16:26:17 +0900
commit0b4b806c06f0ee50bb1bc1b30c38399553687abd (patch)
tree6898ec9eeb21a26668012c1c7743772da7b72214 /server/fsm.go
parent2ad42ead02cce21cf71b3dde0731e8f5c6d4ac6a (diff)
config: use the latest openconfig yang
Diffstat (limited to 'server/fsm.go')
-rw-r--r--server/fsm.go147
1 files changed, 74 insertions, 73 deletions
diff --git a/server/fsm.go b/server/fsm.go
index 86390354..6adfc800 100644
--- a/server/fsm.go
+++ b/server/fsm.go
@@ -66,8 +66,8 @@ func (s AdminState) String() string {
type FSM struct {
t tomb.Tomb
- globalConfig *config.Global
- peerConfig *config.Neighbor
+ gConf *config.Global
+ pConf *config.Neighbor
state bgp.FSMState
conn net.Conn
connCh chan net.Conn
@@ -81,57 +81,58 @@ type FSM struct {
}
func (fsm *FSM) bgpMessageStateUpdate(MessageType uint8, isIn bool) {
- state := &fsm.peerConfig.BgpNeighborCommonState
+ state := &fsm.pConf.NeighborState.Messages
+ timer := &fsm.pConf.Timers
if isIn {
- state.TotalIn++
+ state.Received.Total++
} else {
- state.TotalOut++
+ state.Sent.Total++
}
switch MessageType {
case bgp.BGP_MSG_OPEN:
if isIn {
- state.OpenIn++
+ state.Received.Open++
} else {
- state.OpenOut++
+ state.Sent.Open++
}
case bgp.BGP_MSG_UPDATE:
if isIn {
- state.UpdateIn++
- state.UpdateRecvTime = time.Now().Unix()
+ state.Received.Update++
+ timer.TimersState.UpdateRecvTime = time.Now().Unix()
} else {
- state.UpdateOut++
+ state.Sent.Update++
}
case bgp.BGP_MSG_NOTIFICATION:
if isIn {
- state.NotifyIn++
+ state.Received.Notification++
} else {
- state.NotifyOut++
+ state.Sent.Notification++
}
case bgp.BGP_MSG_KEEPALIVE:
if isIn {
- state.KeepaliveIn++
+ state.Received.Keepalive++
} else {
- state.KeepaliveOut++
+ state.Sent.Keepalive++
}
case bgp.BGP_MSG_ROUTE_REFRESH:
if isIn {
- state.RefreshIn++
+ state.Received.Refresh++
} else {
- state.RefreshOut++
+ state.Sent.Refresh++
}
default:
if isIn {
- state.DiscardedIn++
+ state.Received.Discarded++
} else {
- state.DiscardedOut++
+ state.Sent.Discarded++
}
}
}
-func NewFSM(gConfig *config.Global, pConfig *config.Neighbor) *FSM {
+func NewFSM(gConf *config.Global, pConf *config.Neighbor) *FSM {
fsm := &FSM{
- globalConfig: gConfig,
- peerConfig: pConfig,
+ gConf: gConf,
+ pConf: pConf,
state: bgp.BGP_FSM_IDLE,
connCh: make(chan net.Conn),
opensentHoldTime: float64(HOLDTIME_OPENSENT),
@@ -146,22 +147,22 @@ func NewFSM(gConfig *config.Global, pConfig *config.Neighbor) *FSM {
func (fsm *FSM) StateChange(nextState bgp.FSMState) {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"old": fsm.state.String(),
"new": nextState.String(),
}).Debug("state changed")
fsm.state = nextState
switch nextState {
case bgp.BGP_FSM_ESTABLISHED:
- fsm.peerConfig.BgpNeighborCommonState.Uptime = time.Now().Unix()
- fsm.peerConfig.BgpNeighborCommonState.EstablishedCount++
+ fsm.pConf.Timers.TimersState.Uptime = time.Now().Unix()
+ fsm.pConf.NeighborState.EstablishedCount++
case bgp.BGP_FSM_ACTIVE:
- if !fsm.peerConfig.TransportOptions.PassiveMode {
+ if !fsm.pConf.Transport.TransportConfig.PassiveMode {
fsm.getActiveCh <- struct{}{}
}
fallthrough
default:
- fsm.peerConfig.BgpNeighborCommonState.Downtime = time.Now().Unix()
+ fsm.pConf.Timers.TimersState.Downtime = time.Now().Unix()
}
}
@@ -188,7 +189,7 @@ func (fsm *FSM) sendNotificatonFromErrorMsg(conn net.Conn, e *bgp.MessageError)
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": e,
}).Warn("sent notification")
}
@@ -200,7 +201,7 @@ func (fsm *FSM) sendNotification(conn net.Conn, code, subType uint8, data []byte
func (fsm *FSM) connectLoop() error {
var tick int
- if tick = int(fsm.peerConfig.Timers.ConnectRetry); tick < MIN_CONNECT_RETRY {
+ if tick = int(fsm.pConf.Timers.TimersConfig.ConnectRetry); tick < MIN_CONNECT_RETRY {
tick = MIN_CONNECT_RETRY
}
@@ -210,7 +211,7 @@ func (fsm *FSM) connectLoop() error {
connect := func() {
if fsm.state == bgp.BGP_FSM_ACTIVE {
var host string
- addr := fsm.peerConfig.NeighborAddress
+ addr := fsm.pConf.NeighborConfig.NeighborAddress
if addr.To4() != nil {
host = addr.String() + ":" + strconv.Itoa(bgp.BGP_PORT)
@@ -220,7 +221,7 @@ func (fsm *FSM) connectLoop() error {
conn, err := net.DialTimeout("tcp", host, time.Duration(MIN_CONNECT_RETRY-1)*time.Second)
if err == nil {
- isEBGP := fsm.globalConfig.As != fsm.peerConfig.PeerAs
+ isEBGP := fsm.gConf.GlobalConfig.As != fsm.pConf.NeighborConfig.PeerAs
if isEBGP {
ttl := 1
SetTcpTTLSockopts(conn.(*net.TCPConn), ttl)
@@ -229,7 +230,7 @@ func (fsm *FSM) connectLoop() error {
} else {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Debugf("failed to connect: %s", err)
}
}
@@ -240,7 +241,7 @@ func (fsm *FSM) connectLoop() error {
case <-fsm.t.Dying():
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Debug("stop connect loop")
ticker.Stop()
return nil
@@ -291,14 +292,14 @@ func (h *FSMHandler) idle() bgp.FSMState {
conn.Close()
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Warn("Closed an accepted connection")
case <-idleHoldTimer.C:
if fsm.adminState == ADMIN_STATE_UP {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Duration": fsm.idleHoldTime,
}).Debug("IdleHoldTimer expired")
fsm.idleHoldTime = HOLDTIME_IDLE
@@ -350,7 +351,7 @@ func (h *FSMHandler) active() bgp.FSMState {
case ADMIN_STATE_UP:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"State": fsm.state,
"AdminState": s.String(),
}).Panic("code logic bug")
@@ -360,27 +361,27 @@ func (h *FSMHandler) active() bgp.FSMState {
}
}
-func capabilitiesFromConfig(global *config.Global, peerConf *config.Neighbor) []bgp.ParameterCapabilityInterface {
+func capabilitiesFromConfig(gConf *config.Global, pConf *config.Neighbor) []bgp.ParameterCapabilityInterface {
caps := make([]bgp.ParameterCapabilityInterface, 0, 4)
caps = append(caps, bgp.NewCapRouteRefresh())
- for _, rf := range peerConf.AfiSafiList {
+ for _, rf := range pConf.AfiSafis.AfiSafiList {
k, _ := bgp.GetRouteFamily(rf.AfiSafiName)
afi, safi := bgp.RouteFamilyToAfiSafi(k)
caps = append(caps, bgp.NewCapMultiProtocol(afi, safi))
}
- caps = append(caps, bgp.NewCapFourOctetASNumber(global.As))
+ caps = append(caps, bgp.NewCapFourOctetASNumber(gConf.GlobalConfig.As))
return caps
}
-func buildopen(global *config.Global, peerConf *config.Neighbor) *bgp.BGPMessage {
- caps := capabilitiesFromConfig(global, peerConf)
+func buildopen(gConf *config.Global, pConf *config.Neighbor) *bgp.BGPMessage {
+ caps := capabilitiesFromConfig(gConf, pConf)
opt := bgp.NewOptionParameterCapability(caps)
- holdTime := uint16(peerConf.Timers.HoldTime)
- as := global.As
+ holdTime := uint16(pConf.Timers.TimersConfig.HoldTime)
+ as := gConf.GlobalConfig.As
if as > (1<<16)-1 {
as = bgp.AS_TRANS
}
- return bgp.NewBGPOpenMessage(uint16(as), holdTime, global.RouterId.String(),
+ return bgp.NewBGPOpenMessage(uint16(as), holdTime, gConf.GlobalConfig.RouterId.String(),
[]bgp.OptionParameterInterface{opt})
}
@@ -406,12 +407,12 @@ func (h *FSMHandler) recvMessageWithError() error {
h.fsm.bgpMessageStateUpdate(0, true)
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": h.fsm.peerConfig.NeighborAddress,
+ "Key": h.fsm.pConf.NeighborConfig.NeighborAddress,
"error": err,
}).Warn("malformed BGP Header")
h.msgCh <- &fsmMsg{
MsgType: FSM_MSG_BGP_MESSAGE,
- MsgSrc: h.fsm.peerConfig.NeighborAddress.String(),
+ MsgSrc: h.fsm.pConf.NeighborConfig.NeighborAddress.String(),
MsgData: err,
}
return err
@@ -434,18 +435,18 @@ func (h *FSMHandler) recvMessageWithError() error {
if err != nil {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": h.fsm.peerConfig.NeighborAddress,
+ "Key": h.fsm.pConf.NeighborConfig.NeighborAddress,
"error": err,
}).Warn("malformed BGP message")
fmsg = &fsmMsg{
MsgType: FSM_MSG_BGP_MESSAGE,
- MsgSrc: h.fsm.peerConfig.NeighborAddress.String(),
+ MsgSrc: h.fsm.pConf.NeighborConfig.NeighborAddress.String(),
MsgData: err,
}
} else {
fmsg = &fsmMsg{
MsgType: FSM_MSG_BGP_MESSAGE,
- MsgSrc: h.fsm.peerConfig.NeighborAddress.String(),
+ MsgSrc: h.fsm.pConf.NeighborConfig.NeighborAddress.String(),
MsgData: m,
}
if h.fsm.state == bgp.BGP_FSM_ESTABLISHED {
@@ -472,7 +473,7 @@ func (h *FSMHandler) recvMessage() error {
func (h *FSMHandler) opensent() bgp.FSMState {
fsm := h.fsm
- m := buildopen(fsm.globalConfig, fsm.peerConfig)
+ m := buildopen(fsm.gConf, fsm.pConf)
b, _ := m.Serialize()
fsm.conn.Write(b)
fsm.bgpMessageStateUpdate(m.Header.Type, false)
@@ -500,7 +501,7 @@ func (h *FSMHandler) opensent() bgp.FSMState {
conn.Close()
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Warn("Closed an accepted connection")
case e := <-h.msgCh:
switch e.MsgData.(type) {
@@ -508,7 +509,7 @@ func (h *FSMHandler) opensent() bgp.FSMState {
m := e.MsgData.(*bgp.BGPMessage)
if m.Header.Type == bgp.BGP_MSG_OPEN {
body := m.Body.(*bgp.BGPOpen)
- err := bgp.ValidateOpenMsg(body, fsm.peerConfig.PeerAs)
+ err := bgp.ValidateOpenMsg(body, fsm.pConf.NeighborConfig.PeerAs)
if err != nil {
fsm.sendNotificatonFromErrorMsg(h.conn, err.(*bgp.MessageError))
return bgp.BGP_FSM_IDLE
@@ -516,7 +517,7 @@ func (h *FSMHandler) opensent() bgp.FSMState {
e := &fsmMsg{
MsgType: FSM_MSG_BGP_MESSAGE,
- MsgSrc: fsm.peerConfig.NeighborAddress.String(),
+ MsgSrc: fsm.pConf.NeighborConfig.NeighborAddress.String(),
MsgData: m,
}
h.incoming <- e
@@ -536,7 +537,7 @@ func (h *FSMHandler) opensent() bgp.FSMState {
default:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": e.MsgData,
}).Panic("unknonw msg type")
}
@@ -557,7 +558,7 @@ func (h *FSMHandler) opensent() bgp.FSMState {
case ADMIN_STATE_UP:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"State": fsm.state,
"AdminState": s.String(),
}).Panic("code logic bug")
@@ -571,8 +572,8 @@ func keepaliveTicker(fsm *FSM) *time.Ticker {
if fsm.negotiatedHoldTime == 0 {
return &time.Ticker{}
}
- sec := time.Second * time.Duration(fsm.peerConfig.Timers.KeepaliveInterval)
- if fsm.negotiatedHoldTime < fsm.peerConfig.Timers.HoldTime {
+ sec := time.Second * time.Duration(fsm.pConf.Timers.TimersConfig.KeepaliveInterval)
+ if fsm.negotiatedHoldTime < fsm.pConf.Timers.TimersConfig.HoldTime {
sec = time.Second * time.Duration(fsm.negotiatedHoldTime) / 3
}
if sec == 0 {
@@ -610,7 +611,7 @@ func (h *FSMHandler) openconfirm() bgp.FSMState {
conn.Close()
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Warn("Closed an accepted connection")
case <-ticker.C:
m := bgp.NewBGPKeepAliveMessage()
@@ -636,7 +637,7 @@ func (h *FSMHandler) openconfirm() bgp.FSMState {
default:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": e.MsgData,
}).Panic("unknonw msg type")
}
@@ -657,7 +658,7 @@ func (h *FSMHandler) openconfirm() bgp.FSMState {
case ADMIN_STATE_UP:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"State": fsm.state,
"AdminState": s.String(),
}).Panic("code logic bug")
@@ -667,7 +668,7 @@ func (h *FSMHandler) openconfirm() bgp.FSMState {
}
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Panic("code logic bug")
return 0
}
@@ -681,7 +682,7 @@ func (h *FSMHandler) sendMessageloop() error {
if err != nil {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": err,
}).Warn("failed to serialize")
fsm.bgpMessageStateUpdate(0, false)
@@ -696,7 +697,7 @@ func (h *FSMHandler) sendMessageloop() error {
if err != nil {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": err,
}).Warn("failed to send")
h.errorCh <- true
@@ -708,7 +709,7 @@ func (h *FSMHandler) sendMessageloop() error {
if m.Header.Type == bgp.BGP_MSG_NOTIFICATION {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Data": m,
}).Warn("sent notification")
@@ -719,7 +720,7 @@ func (h *FSMHandler) sendMessageloop() error {
} else {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"data": m,
}).Debug("sent")
}
@@ -791,7 +792,7 @@ func (h *FSMHandler) established() bgp.FSMState {
conn.Close()
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Warn("Closed an accepted connection")
case <-h.errorCh:
h.conn.Close()
@@ -801,7 +802,7 @@ func (h *FSMHandler) established() bgp.FSMState {
case <-holdTimer.C:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"data": bgp.BGP_FSM_ESTABLISHED,
}).Warn("hold timer expired")
m := bgp.NewBGPNotificationMessage(bgp.BGP_ERROR_HOLD_TIMER_EXPIRED, 0, nil)
@@ -849,14 +850,14 @@ func (h *FSMHandler) loop() error {
if nextState == bgp.BGP_FSM_ESTABLISHED && oldState == bgp.BGP_FSM_OPENCONFIRM {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
}).Info("Peer Up")
}
if oldState == bgp.BGP_FSM_ESTABLISHED {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"Reason": h.reason,
}).Info("Peer Down")
}
@@ -865,7 +866,7 @@ func (h *FSMHandler) loop() error {
if nextState >= bgp.BGP_FSM_IDLE {
e := &fsmMsg{
MsgType: FSM_MSG_STATE_CHANGE,
- MsgSrc: fsm.peerConfig.NeighborAddress.String(),
+ MsgSrc: fsm.pConf.NeighborConfig.NeighborAddress.String(),
MsgData: nextState,
}
h.incoming <- e
@@ -878,7 +879,7 @@ func (h *FSMHandler) changeAdminState(s AdminState) error {
if fsm.adminState != s {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"AdminState": s.String(),
}).Debug("admin state changed")
@@ -888,14 +889,14 @@ func (h *FSMHandler) changeAdminState(s AdminState) error {
case ADMIN_STATE_UP:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": h.fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"FSMState": fsm.state.String(),
}).Info("Administrative start")
case ADMIN_STATE_DOWN:
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": h.fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"FSMState": fsm.state.String(),
}).Info("Administrative shutdown")
}
@@ -903,7 +904,7 @@ func (h *FSMHandler) changeAdminState(s AdminState) error {
} else {
log.WithFields(log.Fields{
"Topic": "Peer",
- "Key": fsm.peerConfig.NeighborAddress,
+ "Key": fsm.pConf.NeighborConfig.NeighborAddress,
"FSMState": fsm.state.String(),
}).Warn("cannot change to the same state")