Newer
Older
"time"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable"
log "github.com/sirupsen/logrus"
)
const (
// Administrative events
ManualStart = 1
ManualStop = 2
AutomaticStart = 3
ManualStartWithPassiveTcpEstablishment = 4
AutomaticStartWithPassiveTcpEstablishment = 5
AutomaticStop = 8
Cease = 100
)
type state interface {
run() (state, string)
}
// FSM2 implements the BGP finite state machine (RFC4271)
type FSM2 struct {
peer *Peer
eventCh chan int
con net.Conn
conCh chan net.Conn
initiateCon chan struct{}
conErrCh chan error
delayOpen bool
delayOpenTime time.Duration
delayOpenTimer *time.Timer
connectRetryTime time.Duration
connectRetryTimer *time.Timer
connectRetryCounter int
stopMsgRecvCh chan struct{}
capAddPathSend bool
capAddPathRecv bool
updateSender routingtable.RouteTableClient
neighborID uint32
state state
reason string
active bool
}
// NewPassiveFSM2 initiates a new passive FSM
func NewPassiveFSM2(peer *Peer, con *net.TCPConn) *FSM2 {
fsm := newFSM2(peer)
fsm.con = con
fsm.state = newIdleState(fsm)
return fsm
}
// NewActiveFSM2 initiates a new passive FSM
func NewActiveFSM2(peer *Peer) *FSM2 {
fsm := newFSM2(peer)
fsm.active = true
fsm.state = newIdleState(fsm)
return fsm
}
func newFSM2(peer *Peer) *FSM2 {
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
connectRetryTime: time.Minute,
peer: peer,
eventCh: make(chan int),
conCh: make(chan net.Conn),
conErrCh: make(chan error),
initiateCon: make(chan struct{}),
msgRecvCh: make(chan []byte),
msgRecvFailCh: make(chan error),
stopMsgRecvCh: make(chan struct{}),
rib: peer.rib,
}
}
func (fsm *FSM2) start() {
go fsm.run()
go fsm.tcpConnector()
return
}
func (fsm *FSM2) activate() {
fsm.eventCh <- AutomaticStart
}
func (fsm *FSM2) run() {
next, reason := fsm.state.run()
for {
newState := stateName(next)
oldState := stateName(fsm.state)
if oldState != newState {
log.WithFields(log.Fields{
"peer": fsm.peer.addr.String(),
"last_state": oldState,
"new_state": newState,
"reason": reason,
}).Info("FSM: Neighbor state change")
}
if newState == "cease" {
return
}
fsm.stateMu.Lock()
fsm.state = next
fsm.stateMu.Unlock()
next, reason = fsm.state.run()
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
func stateName(s state) string {
switch s.(type) {
case *idleState:
return "idle"
case *connectState:
return "connect"
case *activeState:
return "active"
case *openSentState:
return "openSent"
case *openConfirmState:
return "openConfirm"
case *establishedState:
return "established"
case *ceaseState:
return "cease"
default:
panic(fmt.Sprintf("Unknown state: %v", s))
}
}
func (fsm *FSM2) cease() {
fsm.eventCh <- Cease
}
func (fsm *FSM2) tcpConnector() error {
for {
select {
case <-fsm.initiateCon:
c, err := net.DialTCP("tcp", &net.TCPAddr{IP: fsm.local}, &net.TCPAddr{IP: fsm.peer.addr, Port: BGPPORT})
if err != nil {
select {
case fsm.conErrCh <- err:
continue
case <-time.NewTimer(time.Second * 30).C:
continue
}
}
select {
case fsm.conCh <- c:
continue
case <-time.NewTimer(time.Second * 30).C:
c.Close()
continue
}
}
}
}
func (fsm *FSM2) tcpConnect() {
fsm.initiateCon <- struct{}{}
}
func (fsm *FSM2) msgReceiver() error {
for {
msg, err := recvMsg(fsm.con)
if err != nil {
fsm.msgRecvFailCh <- err
return nil
}
fsm.msgRecvCh <- msg
}
<-fsm.connectRetryTimer.C
}
}
func (fsm *FSM2) resetConnectRetryCounter() {
fsm.connectRetryCounter = 0
}
func (fsm *FSM2) sendOpen() error {
msg := packet.SerializeOpenMsg(&packet.BGPOpen{
Version: BGPVersion,
AS: uint16(fsm.peer.localASN),
HoldTime: uint16(fsm.peer.holdTime / time.Second),
BGPIdentifier: fsm.peer.server.routerID,
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
OptParams: fsm.peer.optOpenParams,
})
_, err := fsm.con.Write(msg)
if err != nil {
return fmt.Errorf("Unable to send OPEN message: %v", err)
}
return nil
}
func (fsm *FSM2) sendNotification(errorCode uint8, errorSubCode uint8) error {
msg := packet.SerializeNotificationMsg(&packet.BGPNotification{})
_, err := fsm.con.Write(msg)
if err != nil {
return fmt.Errorf("Unable to send NOTIFICATION message: %v", err)
}
return nil
}
func (fsm *FSM2) sendKeepalive() error {
msg := packet.SerializeKeepaliveMsg()
_, err := fsm.con.Write(msg)
if err != nil {
return fmt.Errorf("Unable to send KEEPALIVE message: %v", err)
}
return nil
}
func stopTimer(t *time.Timer) {
if !t.Stop() {
select {
case <-t.C:
default:
}
}
}