This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fsm.go
660 lines (596 loc) · 16.1 KB
/
fsm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
140
141
142
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package bgpls
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"sync"
"time"
)
// FSMState describes the state of a neighbor's fsm
type FSMState uint8
// FSMState values
const (
DisabledState FSMState = iota
IdleState
ConnectState
ActiveState
OpenSentState
OpenConfirmState
EstablishedState
)
func (s FSMState) String() string {
switch s {
case DisabledState:
return "disabled"
case IdleState:
return "idle"
case ConnectState:
return "connect"
case ActiveState:
return "active"
case OpenSentState:
return "openSent"
case OpenConfirmState:
return "openConfirm"
case EstablishedState:
return "established"
default:
return "unknown state"
}
}
var (
errInvalidStateTransition = errors.New("invalid state transition")
)
var (
// A HoldTimer value of 4 minutes is suggested.
longHoldTime = time.Minute * 4
)
const (
// The exact value of the ConnectRetryTimer is a local matter, but it
// SHOULD be sufficiently large to allow TCP initialization.
connectRetryTime = time.Second * 5
)
type fsm interface {
idle() FSMState
connect() FSMState
openSent() FSMState
openConfirm() FSMState
established() FSMState
terminate()
}
type standardFSM struct {
port int
events chan Event
disable chan interface{}
neighborConfig *NeighborConfig
routerID net.IP
localASN uint32
conn net.Conn
readerErr chan error
closeReader chan struct{}
readerClosed chan struct{}
msgCh chan Message
keepAliveTime time.Duration
keepAliveTimer *time.Timer
holdTime time.Duration
holdTimer *time.Timer
connectRetryTimer *time.Timer
running bool
outboundConnErr chan error
outboundConn chan net.Conn
cancelOutboundDial context.CancelFunc
*sync.Mutex
}
func newFSM(c *NeighborConfig, events chan Event, routerID net.IP, localASN uint32, port int) fsm {
f := &standardFSM{
port: port,
events: events,
disable: make(chan interface{}),
neighborConfig: c,
routerID: routerID,
localASN: localASN,
keepAliveTime: time.Duration(int64(c.HoldTime) / 3).Truncate(time.Second),
keepAliveTimer: time.NewTimer(0),
holdTime: c.HoldTime,
holdTimer: time.NewTimer(0),
connectRetryTimer: time.NewTimer(0),
Mutex: &sync.Mutex{},
}
// drain all timers so they can be reset
drainTimers(f.keepAliveTimer, f.holdTimer, f.connectRetryTimer)
f.running = true
go f.loop()
return f
}
func (f *standardFSM) terminate() {
f.Lock()
defer f.Unlock()
if !f.running {
return
}
f.disable <- nil
<-f.disable
f.running = false
}
func (f *standardFSM) dialNeighbor() {
dialer := &net.Dialer{}
ctx, cancel := context.WithCancel(context.Background())
f.outboundConnErr = make(chan error)
f.outboundConn = make(chan net.Conn)
f.cancelOutboundDial = cancel
go func() {
conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(f.neighborConfig.Address.String(), strconv.Itoa(f.port)))
if err != nil {
f.outboundConnErr <- err
return
}
f.outboundConn <- conn
}()
}
func (f *standardFSM) startReader() {
f.readerErr = make(chan error)
f.closeReader = make(chan struct{})
f.readerClosed = make(chan struct{})
f.msgCh = make(chan Message)
go f.read()
}
func (f *standardFSM) idle() FSMState {
// starts the ConnectRetryTimer with the initial value
f.connectRetryTimer.Reset(connectRetryTime)
// initiates a TCP connection to the other BGP peer
f.dialNeighbor()
// changes its state to Connect
return ConnectState
}
// cleanupConnAndReader closes the connection,
// the reader close signal channel, and the messages channel
func (f *standardFSM) cleanupConnAndReader() {
f.conn.Close()
close(f.closeReader)
<-f.readerClosed
close(f.msgCh)
}
func (f *standardFSM) connect() FSMState {
Loop:
for {
select {
case <-f.disable:
drainTimers(f.connectRetryTimer)
// drain the dialer and transition to DisabledState
f.cancelOutboundDial()
select {
case <-f.outboundConn:
case <-f.outboundConnErr:
}
return DisabledState
case <-f.connectRetryTimer.C:
/*
In response to the ConnectRetryTimer_Expires event (Event 9), the
local system:
- drops the TCP connection,
- restarts the ConnectRetryTimer,
- stops the DelayOpenTimer and resets the timer to zero,
- initiates a TCP connection to the other BGP peer,
- continues to listen for a connection that may be initiated by
the remote BGP peer, and
- stays in the Connect state.
*/
f.cancelOutboundDial()
// canceling races with the dialer so it must be drained
select {
case conn := <-f.outboundConn:
f.conn = conn
f.startReader()
break Loop
case <-f.outboundConnErr:
}
// timer already drained
f.connectRetryTimer.Reset(connectRetryTime)
f.dialNeighbor()
case err := <-f.outboundConnErr:
/*
If the TCP connection fails (Event 18), the local system checks
the DelayOpenTimer. If the DelayOpenTimer is running, the local
system:
- restarts the ConnectRetryTimer with the initial value,
- stops the DelayOpenTimer and resets its value to zero,
- continues to listen for a connection that may be initiated by
the remote BGP peer, and
- changes its state to Active.
*/
drainTimers(f.connectRetryTimer)
next := f.handleErr(fmt.Errorf("error connecting to neighbor: %v", err), ActiveState)
if next != DisabledState {
f.connectRetryTimer.Reset(connectRetryTime)
}
return next
case conn := <-f.outboundConn:
/*
If the TCP connection succeeds (Event 16 or Event 17), the local
system checks the DelayOpen attribute prior to processing.
...
If the DelayOpen attribute is set to FALSE, the local system:
- stops the ConnectRetryTimer (if running) and sets the
ConnectRetryTimer to zero,
- completes BGP initialization
- sends an OPEN message to its peer,
- sets the HoldTimer to a large value, and
- changes its state to OpenSent.
*/
drainTimers(f.connectRetryTimer)
f.conn = conn
f.startReader()
break Loop
}
}
o, err := newOpenMessage(f.localASN, f.holdTime, f.routerID)
if err != nil {
f.cleanupConnAndReader()
return f.handleErr(fmt.Errorf("error creating open message: %v", err), IdleState)
}
b, err := o.serialize()
if err != nil {
panic("bug serializing open message")
}
_, err = f.conn.Write(b)
if err != nil {
f.cleanupConnAndReader()
return f.handleErr(fmt.Errorf("error sending open message: %v", err), IdleState)
}
f.holdTimer.Reset(longHoldTime)
return OpenSentState
}
func (f *standardFSM) active() FSMState {
select {
case <-f.disable:
drainTimers(f.connectRetryTimer)
return DisabledState
case <-f.connectRetryTimer.C:
/*
In response to a ConnectRetryTimer_Expires event (Event 9), the
local system:
- restarts the ConnectRetryTimer (with initial value),
- initiates a TCP connection to the other BGP peer,
- continues to listen for a TCP connection that may be initiated
by a remote BGP peer, and
- changes its state to Connect.
*/
f.connectRetryTimer.Reset(connectRetryTime)
f.dialNeighbor()
return ConnectState
}
}
// sendEvent sends the provided event on the events channel and
// returns the provided FSMState unless a disable signal is received
// in which case DisabledState is returned
func (f *standardFSM) sendEvent(e Event, nextState FSMState) FSMState {
select {
case f.events <- e:
return nextState
case <-f.disable:
return DisabledState
}
}
// handlerErr checks the provided err to see if a notification can be unwrapped
// and if so, sends it to the neighbor.
//
// The provided FSMState is returned unless a disable signal is received while
// trying to send on the events channel in which case DisabledState is returned.
func (f *standardFSM) handleErr(err error, nextState FSMState) FSMState {
if err, ok := err.(*errWithNotification); ok {
f.sendNotification(err.code, err.subcode, err.data)
}
return f.sendEvent(newEventNeighborErr(f.neighborConfig, err), nextState)
}
func (f *standardFSM) handleHoldTimerExpired() FSMState {
/*
If the HoldTimer_Expires (Event 10), the local system:
- sends a NOTIFICATION message with the error code Hold Timer
Expired,
- sets the ConnectRetryTimer to zero,
- releases all BGP resources,
- drops the TCP connection,
- increments the ConnectRetryCounter,
- (optionally) performs peer oscillation damping if the
DampPeerOscillations attribute is set to TRUE, and
- changes its state to Idle.
*/
f.sendHoldTimerExpired()
f.cleanupConnAndReader()
return f.sendEvent(newEventNeighborHoldTimerExpired(f.neighborConfig), IdleState)
}
func (f *standardFSM) read() {
defer close(f.readerClosed)
for {
select {
case <-f.closeReader:
return
default:
buff := make([]byte, 4096)
n, err := f.conn.Read(buff)
if err != nil {
select {
case f.readerErr <- err:
case <-f.closeReader:
}
return
}
buff = buff[:n]
msgs, err := messagesFromBytes(buff)
if err != nil {
select {
case f.readerErr <- err:
case <-f.closeReader:
}
return
}
for _, m := range msgs {
select {
case f.msgCh <- m:
case <-f.closeReader:
return
}
}
}
}
}
func (f *standardFSM) sendHoldTimerExpired() error {
return f.sendNotification(NotifErrCodeHoldTimerExpired, 0, nil)
}
// handleUnexpectedMessageType sends the appropriate notification message to the
// neighbor and generates an EventNeighborErr
func (f *standardFSM) handleUnexpectedMessageType(received MessageType, next FSMState) FSMState {
b := make([]byte, 1)
b[0] = uint8(received)
f.sendNotification(NotifErrCodeMessageHeader, NotifErrSubcodeBadType, b)
return f.sendEvent(newEventNeighborErr(f.neighborConfig, fmt.Errorf("unexpected message type: %s", received)), next)
}
func (f *standardFSM) openSent() FSMState {
select {
case <-f.disable:
f.sendCease()
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return DisabledState
case err := <-f.readerErr:
/*
If a TcpConnectionFails event (Event 18) is received, the local
system:
- closes the BGP connection,
- restarts the ConnectRetryTimer,
- continues to listen for a connection that may be initiated by
the remote BGP peer, and
- changes its state to Active.
*/
var next FSMState
// check if err is connection related or not - Active vs Idle
_, isOpError := err.(*net.OpError)
if isOpError {
next = f.handleErr(err, ActiveState)
if next != DisabledState {
f.connectRetryTimer.Reset(connectRetryTime)
}
} else {
next = f.handleErr(err, IdleState)
}
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
case <-f.holdTimer.C:
return f.handleHoldTimerExpired()
case m := <-f.msgCh:
open, isOpen := m.(*openMessage)
if !isOpen {
var next FSMState
notif, isNotif := m.(*NotificationMessage)
if isNotif {
next = f.sendEvent(newEventNeighborNotificationReceived(f.neighborConfig, notif), IdleState)
} else {
next = f.handleUnexpectedMessageType(m.MessageType(), IdleState)
}
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
err := validateOpenMessage(open, f.neighborConfig.ASN)
if err != nil {
next := f.handleErr(err, IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
if float64(open.holdTime) < f.holdTime.Seconds() {
f.holdTime = time.Duration(int64(open.holdTime) * int64(time.Second))
f.keepAliveTime = (f.holdTime / 3).Truncate(time.Second)
}
err = f.sendKeepAlive()
if err != nil {
next := f.handleErr(err, IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
f.drainAndResetHoldTimer()
return OpenConfirmState
}
}
func (f *standardFSM) sendKeepAlive() error {
ka := &keepAliveMessage{}
b, err := ka.serialize()
if err != nil {
panic("bug serializing keepalive message")
}
_, err = f.conn.Write(b)
return err
}
func (f *standardFSM) openConfirm() FSMState {
for {
select {
case <-f.disable:
f.sendCease()
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return DisabledState
case err := <-f.readerErr:
next := f.handleErr(err, IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
case <-f.holdTimer.C:
return f.handleHoldTimerExpired()
case m := <-f.msgCh:
_, isKeepAlive := m.(*keepAliveMessage)
if !isKeepAlive {
next := f.handleUnexpectedMessageType(m.MessageType(), IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
f.drainAndResetHoldTimer()
// does not need to be drained
f.keepAliveTimer.Reset(f.keepAliveTime)
return EstablishedState
}
}
}
func (f *standardFSM) established() FSMState {
for {
select {
case <-f.disable:
f.sendCease()
drainTimers(f.keepAliveTimer, f.holdTimer)
f.cleanupConnAndReader()
return DisabledState
case err := <-f.readerErr:
next := f.handleErr(err, IdleState)
drainTimers(f.keepAliveTimer, f.holdTimer)
f.cleanupConnAndReader()
return next
case <-f.holdTimer.C:
drainTimers(f.keepAliveTimer)
return f.handleHoldTimerExpired()
case <-f.keepAliveTimer.C:
err := f.sendKeepAlive()
if err != nil {
next := f.handleErr(err, IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
// does not need to be drained
f.keepAliveTimer.Reset(f.keepAliveTime)
case m := <-f.msgCh:
switch m := m.(type) {
case *keepAliveMessage:
f.drainAndResetHoldTimer()
case *UpdateMessage:
f.drainAndResetHoldTimer()
next := f.sendEvent(newEventNeighborUpdateReceived(f.neighborConfig, m), EstablishedState)
if next == DisabledState {
f.sendCease()
drainTimers(f.keepAliveTimer, f.holdTimer)
f.cleanupConnAndReader()
return next
}
case *NotificationMessage:
drainTimers(f.keepAliveTimer, f.holdTimer)
f.cleanupConnAndReader()
return f.sendEvent(newEventNeighborNotificationReceived(f.neighborConfig, m), IdleState)
case *openMessage:
next := f.handleUnexpectedMessageType(m.MessageType(), IdleState)
drainTimers(f.holdTimer)
f.cleanupConnAndReader()
return next
}
}
}
}
func (f *standardFSM) loop() {
var current FSMState
next := IdleState
for {
if next != DisabledState {
next = f.sendEvent(newEventNeighborStateTransition(f.neighborConfig, next), next)
}
current = next
switch current {
case DisabledState:
f.disable <- nil
return
case IdleState:
next = f.idle()
case ConnectState:
next = f.connect()
case ActiveState:
next = f.active()
case OpenSentState:
next = f.openSent()
case OpenConfirmState:
next = f.openConfirm()
case EstablishedState:
next = f.established()
}
err := validTransition(current, next)
if err != nil {
panic(fmt.Sprintf("invalid state transition for neighbor:%s %s to %s", f.neighborConfig.Address, current, next))
}
}
}
func drainTimers(timers ...*time.Timer) {
for _, t := range timers {
if !t.Stop() {
<-t.C
}
}
}
func (f *standardFSM) drainAndResetHoldTimer() {
drainTimers(f.holdTimer)
f.holdTimer.Reset(f.holdTime)
}
func (f *standardFSM) sendCease() error {
return f.sendNotification(NotifErrCodeCease, 0, nil)
}
func (f *standardFSM) sendNotification(code NotifErrCode, subcode NotifErrSubcode, data []byte) error {
n := &NotificationMessage{
Code: code,
Subcode: subcode,
Data: data,
}
b, err := n.serialize()
if err != nil {
return err
}
_, err = f.conn.Write(b)
return err
}
func validTransition(current, next FSMState) error {
switch next {
case DisabledState:
return nil
case IdleState:
return nil
case ConnectState:
if current == IdleState || current == ActiveState {
return nil
}
case ActiveState:
if current == ConnectState || current == OpenSentState {
return nil
}
case OpenSentState:
if current == ConnectState || current == ActiveState {
return nil
}
case OpenConfirmState:
if current == OpenSentState {
return nil
}
case EstablishedState:
if current == OpenConfirmState {
return nil
}
}
return errors.New("invalid state transition")
}