summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-23 05:20:31 -0800
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-23 05:20:31 -0800
commitf33719f78de63f9b51d10605e99d72222410e60d (patch)
treeef668c9187abe3f36042d01d3f12321e799054a3
parentcb48c082ed63590c42ff27315e583649027241e1 (diff)
packet: define FSMState
Then we can use stringer nicely. Now FSMstate shows nicely like: DEBU[0081] Peer (10.0.0.1) state changed from BGP_FSM_ACTIVE to BGP_FSM_OPENSENT Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--api/rest.go2
-rw-r--r--packet/constatnt.go4
-rw-r--r--packet/fsmstate_string.go17
-rw-r--r--server/fsm.go24
-rw-r--r--server/peer.go7
-rw-r--r--server/server.go3
6 files changed, 38 insertions, 19 deletions
diff --git a/api/rest.go b/api/rest.go
index b4b2c314..08588e70 100644
--- a/api/rest.go
+++ b/api/rest.go
@@ -91,7 +91,7 @@ type RestResponseNeighbor struct {
RestResponseDefault
RemoteAddr string
RemoteAs uint32
- NeighborState uint32
+ NeighborState string
UpdateCount int
}
diff --git a/packet/constatnt.go b/packet/constatnt.go
index 6f358ebe..31002c1b 100644
--- a/packet/constatnt.go
+++ b/packet/constatnt.go
@@ -19,8 +19,10 @@ const AS_TRANS = 23456
const BGP_PORT = 179
+type FSMState int
+
const (
- _ = iota
+ _ FSMState = iota
BGP_FSM_IDLE
BGP_FSM_CONNECT
BGP_FSM_ACTIVE
diff --git a/packet/fsmstate_string.go b/packet/fsmstate_string.go
new file mode 100644
index 00000000..26cafe25
--- /dev/null
+++ b/packet/fsmstate_string.go
@@ -0,0 +1,17 @@
+// generated by stringer -type FSMState constatnt.go; DO NOT EDIT
+
+package bgp
+
+import "fmt"
+
+const _FSMState_name = "BGP_FSM_IDLEBGP_FSM_CONNECTBGP_FSM_ACTIVEBGP_FSM_OPENSENTBGP_FSM_OPENCONFIRMBGP_FSM_ESTABLISHED"
+
+var _FSMState_index = [...]uint8{0, 12, 27, 41, 57, 76, 95}
+
+func (i FSMState) String() string {
+ i -= 1
+ if i < 0 || i+1 >= FSMState(len(_FSMState_index)) {
+ return fmt.Sprintf("FSMState(%d)", i+1)
+ }
+ return _FSMState_name[_FSMState_index[i]:_FSMState_index[i+1]]
+}
diff --git a/server/fsm.go b/server/fsm.go
index 62c2495b..7cdd866b 100644
--- a/server/fsm.go
+++ b/server/fsm.go
@@ -29,12 +29,12 @@ type FSM struct {
globalConfig *config.GlobalType
peerConfig *config.NeighborType
keepaliveTicker *time.Ticker
- state int
+ state bgp.FSMState
incoming chan *bgp.BGPMessage
outgoing chan *bgp.BGPMessage
passiveConn *net.TCPConn
passiveConnCh chan *net.TCPConn
- stateCh chan int
+ stateCh chan bgp.FSMState
peerInfo *table.PeerInfo
sourceVerNum int
@@ -86,16 +86,16 @@ func NewFSM(gConfig *config.GlobalType, pConfig *config.NeighborType, connCh cha
outgoing: outgoing,
state: bgp.BGP_FSM_IDLE,
passiveConnCh: connCh,
- stateCh: make(chan int),
+ stateCh: make(chan bgp.FSMState),
sourceVerNum: 1,
}
}
-func (fsm *FSM) StateChanged() chan int {
+func (fsm *FSM) StateChanged() chan bgp.FSMState {
return fsm.stateCh
}
-func (fsm *FSM) StateChange(nextState int) {
+func (fsm *FSM) StateChange(nextState bgp.FSMState) {
log.Debugf("Peer (%v) state changed from %v to %v", fsm.peerConfig.NeighborAddress, fsm.state, nextState)
fsm.state = nextState
}
@@ -139,7 +139,7 @@ func (h *FSMHandler) Stop() error {
return h.t.Wait()
}
-func (h *FSMHandler) idle() int {
+func (h *FSMHandler) idle() bgp.FSMState {
fsm := h.fsm
// TODO: support idle hold timer
@@ -150,7 +150,7 @@ func (h *FSMHandler) idle() int {
return bgp.BGP_FSM_ACTIVE
}
-func (h *FSMHandler) active() int {
+func (h *FSMHandler) active() bgp.FSMState {
fsm := h.fsm
select {
case <-h.t.Dying():
@@ -224,7 +224,7 @@ func (h *FSMHandler) recvMessage() error {
return nil
}
-func (h *FSMHandler) opensent() int {
+func (h *FSMHandler) opensent() bgp.FSMState {
fsm := h.fsm
m := buildopen(fsm.globalConfig, fsm.peerConfig)
b, _ := m.Serialize()
@@ -261,7 +261,7 @@ func (h *FSMHandler) opensent() int {
return nextState
}
-func (h *FSMHandler) openconfirm() int {
+func (h *FSMHandler) openconfirm() bgp.FSMState {
fsm := h.fsm
sec := time.Second * time.Duration(fsm.peerConfig.Timers.KeepaliveInterval)
fsm.keepaliveTicker = time.NewTicker(sec)
@@ -308,7 +308,7 @@ func (h *FSMHandler) sendMessageloop() error {
case <-h.t.Dying():
return nil
case m := <-fsm.outgoing:
- isSend := func(state int, Type uint8) bool {
+ isSend := func(state bgp.FSMState, Type uint8) bool {
switch Type {
case bgp.BGP_MSG_UPDATE:
if state == bgp.BGP_FSM_ESTABLISHED {
@@ -347,7 +347,7 @@ func (h *FSMHandler) recvMessageloop() error {
}
}
-func (h *FSMHandler) established() int {
+func (h *FSMHandler) established() bgp.FSMState {
fsm := h.fsm
h.conn = fsm.passiveConn
h.t.Go(h.sendMessageloop)
@@ -376,7 +376,7 @@ func (h *FSMHandler) established() int {
func (h *FSMHandler) loop() error {
fsm := h.fsm
- nextState := 0
+ nextState := bgp.FSMState(0)
switch fsm.state {
case bgp.BGP_FSM_IDLE:
nextState = h.idle()
diff --git a/server/peer.go b/server/peer.go
index a28e94b6..45fc1cf5 100644
--- a/server/peer.go
+++ b/server/peer.go
@@ -29,7 +29,6 @@ type Peer struct {
t tomb.Tomb
globalConfig config.GlobalType
peerConfig config.NeighborType
- state int
acceptedConnCh chan *net.TCPConn
incoming chan *bgp.BGPMessage
outgoing chan *bgp.BGPMessage
@@ -46,7 +45,6 @@ func NewPeer(g config.GlobalType, peer config.NeighborType, outEventCh chan *mes
p := &Peer{
globalConfig: g,
peerConfig: peer,
- state: bgp.BGP_FSM_IDLE,
acceptedConnCh: make(chan *net.TCPConn),
incoming: make(chan *bgp.BGPMessage, 4096),
outgoing: make(chan *bgp.BGPMessage, 4096),
@@ -54,6 +52,7 @@ func NewPeer(g config.GlobalType, peer config.NeighborType, outEventCh chan *mes
outEventCh: outEventCh,
}
p.fsm = NewFSM(&g, &peer, p.acceptedConnCh, p.incoming, p.outgoing)
+ peer.BgpNeighborCommonState.State = uint32(bgp.BGP_FSM_IDLE)
p.adjRib = table.NewAdjRib()
p.rib = table.NewTableManager()
p.t.Go(p.loop)
@@ -136,8 +135,8 @@ func (peer *Peer) loop() error {
case nextState := <-peer.fsm.StateChanged():
// waits for all goroutines created for the current state
h.Wait()
- oldState := peer.state
- peer.state = nextState
+ oldState := bgp.FSMState(peer.peerConfig.BgpNeighborCommonState.State)
+ peer.peerConfig.BgpNeighborCommonState.State = uint32(nextState)
peer.fsm.StateChange(nextState)
sameState = false
// TODO: check peer's rf
diff --git a/server/server.go b/server/server.go
index f3ed0092..126bd6a9 100644
--- a/server/server.go
+++ b/server/server.go
@@ -20,6 +20,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
+ "github.com/osrg/gobgp/packet"
"net"
"os"
"strconv"
@@ -158,7 +159,7 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) {
peer, found := server.peerMap[remoteAddr]
if found {
c := peer.peerConfig
- result.NeighborState = c.BgpNeighborCommonState.State
+ result.NeighborState = bgp.FSMState(c.BgpNeighborCommonState.State).String()
result.RemoteAddr = c.NeighborAddress.String()
result.RemoteAs = c.PeerAs
} else {