-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiniteStateMachine.h
1128 lines (1081 loc) · 47.9 KB
/
FiniteStateMachine.h
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by zach on 10/17/2020.
//
#ifndef BGP_FINITESTATEMACHINE_H
#define BGP_FINITESTATEMACHINE_H
#include <cstdint>
#include <utility>
#include <functional>
#include "Log.h"
enum BgpSessionState {
Idle,
Connect,
Active,
OpenSent,
OpenConfirm,
Established
};
std::string BgpSessionStateToString(const BgpSessionState sessionState) {
switch (sessionState) {
case Idle:
return "Idle";
case Connect:
return "Connect";
case Active:
return "Active";
case OpenSent:
return "OpenSent";
case OpenConfirm:
return "OpenConfirm";
case Established:
return "Established";
default:
return "InvalidBgpSessionState";
}
}
enum SessionAttributeFlagBits : uint16_t {
AcceptConnectionsUnconfiguredPeers = 0x0001,
AllowAutomaticStart = 0x0002,
AllowAutomaticStop = 0x0004,
CollisionDetectEstablishedState = 0x0008,
DampPeerOscillations = 0x0010,
DelayOpen = 0x0020,
PassiveTcpEstablishment = 0x0040,
SendNotificationWithoutOpen = 0x0080,
TrackTcpState = 0x0100
};
enum FsmEventType : uint8_t {
UnknownFsmEventType,
// Administrative Events
ManualStart,
ManualStop,
AutomaticStart,
ManualStartWithPassiveTcpEstablishment,
AutomaticStartWithPassiveTcpEstablishment,
AutomaticStartWithDampPeerOscillations,
AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment,
AutomaticStop,
// Timer Events
ConnectRetryTimerExpires,
HoldTimerExpires,
KeepaliveTimerExpires,
DelayOpenTimerExpires,
IdleHoldTimerExpires,
// TCP Connection Events
TcpConnectionValid,
TcpConnectionRequestInvalid,
TcpConnectionRequestAcked,
TcpConnectionConfirmed,
TcpConnectionFails,
// BGP Message Events
BgpOpenMessageReceived,
BgpOpenWithDelayOpenTimerRunning,
BgpHeaderError,
BgpOpenMessageError,
BgpOpenCollisionDump,
BgpNotificationMessageVersionError,
BgpNotificationMessageReceived,
BgpKeepaliveMessageReceived,
BgpUpdateMessageReceived,
BgpUpdateMessageError
};
std::string FsmEventTypeToString(const FsmEventType eventType) {
switch (eventType) {
case UnknownFsmEventType:
return "UnknownFsmEventType";
case ManualStart:
return "ManualStart";
case ManualStop:
return "ManualStop";
case AutomaticStart:
return "AutomaticStart";
case ManualStartWithPassiveTcpEstablishment:
return "ManualStartWithPassiveTcpEstablishment";
case AutomaticStartWithPassiveTcpEstablishment:
return "AutomaticStartWithPassiveTcpEstablishment";
case AutomaticStartWithDampPeerOscillations:
return "AutomaticStartWithDampPeerOscillations";
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
return "AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment";
case AutomaticStop:
return "AutomaticStop";
case ConnectRetryTimerExpires:
return "ConnectRetryTimerExpires";
case HoldTimerExpires:
return "HoldTimerExpires";
case KeepaliveTimerExpires:
return "KeepaliveTimerExpires";
case DelayOpenTimerExpires:
return "DelayOpenTimerExpires";
case IdleHoldTimerExpires:
return "IdleHoldTimerExpires";
case TcpConnectionValid:
return "TcpConnectionValid";
case TcpConnectionRequestInvalid:
return "TcpConnectionRequestInvalid";
case TcpConnectionRequestAcked:
return "TcpConnectionRequestAcked";
case TcpConnectionConfirmed:
return "TcpConnectionConfirmed";
case TcpConnectionFails:
return "TcpConnectionFails";
case BgpOpenMessageReceived:
return "BgpOpenMessageReceived";
case BgpOpenWithDelayOpenTimerRunning:
return "BgpOpenWithDelayOpenTimerRunning";
case BgpHeaderError:
return "BgpHeaderError";
case BgpOpenMessageError:
return "BgpOpenMessageError";
case BgpOpenCollisionDump:
return "BgpOpenCollisionDump";
case BgpNotificationMessageVersionError:
return "BgpNotificationMessageVersionError";
case BgpNotificationMessageReceived:
return "BgpNotificationMessageReceived";
case BgpKeepaliveMessageReceived:
return "BgpKeepaliveMessageReceived";
case BgpUpdateMessageReceived:
return "BgpUpdateMessageReceived";
case BgpUpdateMessageError:
return "BgpUpdateMessageError";
default:
return "InvalidFsmEventType";
}
}
struct BgpFiniteStateMachine;
static void HandleTimerEvent(const std::shared_ptr<BgpFiniteStateMachine>& fsm, FsmEventType eventType);
struct BgpSessionTimer {
uint16_t InitialValue{};
std::atomic<uint16_t> Value;
std::thread Thread;
std::atomic<bool> Active;
std::shared_ptr<BgpFiniteStateMachine> Parent;
FsmEventType ExpireEventType;
BgpSessionTimer() = default;
BgpSessionTimer(const uint16_t initialValue, std::shared_ptr<BgpFiniteStateMachine> parent,
const FsmEventType expireEventType)
: InitialValue(initialValue),
Parent(std::move(parent)),
ExpireEventType(expireEventType) {
Reset();
}
BgpSessionTimer &operator=(const BgpSessionTimer &other) {
if (this == &other)
return *this;
InitialValue = other.InitialValue;
Value.store(other.Value.load(std::memory_order_acquire), std::memory_order_release);
Active.store(other.Active.load(std::memory_order_acquire), std::memory_order_release);
Parent = other.Parent;
ExpireEventType = other.ExpireEventType;
return *this;
}
void Start() {
if (Active.load(std::memory_order_acquire)) {
Stop();
}
Active.store(true, std::memory_order_release);
Thread = std::thread(BgpSessionTimer::TimerThread, this);
}
static void TimerThread(BgpSessionTimer* timer) {
while (timer->Active.load(std::memory_order_acquire)) {
if (timer->Value.load(std::memory_order_acquire) <= 0) {
timer->Active.store(false, std::memory_order_release);
HandleTimerEvent(timer->Parent, timer->ExpireEventType);
} else {
// TODO: Support better/higher precision timers, this loop likely doesn't run once per second.
std::this_thread::sleep_for(std::chrono::seconds(1));
timer->Value.store(timer->Value - 1, std::memory_order_release);
}
}
}
void Stop() {
Active.store(false, std::memory_order_release);
if (Thread.joinable()) {
Thread.join();
}
}
void Restart() {
Stop();
Start();
}
void Restart(const uint16_t initialValue) {
Stop();
Reset(initialValue);
Start();
}
void Reset() {
Stop();
Value.store(InitialValue, std::memory_order_release);
}
void Reset(const uint16_t initialValue) {
Stop();
Value.store(initialValue, std::memory_order_release);
}
};
struct BgpFiniteStateMachine {
// TODO: [9]
uint32_t LocalIpAddress;
uint32_t RemoteIpAddress;
// TODO: [3]
uint16_t LocalAsn;
uint16_t RemoteAsn;
uint32_t LocalRouterId;
uint32_t RemoteRouterId;
BgpSessionState State = Idle;
uint16_t ConnectRetryCounter = 0;
SessionAttributeFlagBits Attributes;
// Timer initial values
uint16_t ConnectRetryTime = 120;
uint16_t HoldTime = 90;
uint16_t KeepaliveTime = 90 / 3;
uint16_t MinASOriginationIntervalTime;
uint16_t MinRouteAdvertisementIntervalTime;
uint16_t DelayOpenTime;
uint16_t IdleHoldTime;
// Timers
BgpSessionTimer ConnectRetryTimer;
BgpSessionTimer HoldTimer;
BgpSessionTimer KeepaliveTimer;
BgpSessionTimer MinASOriginationIntervalTimer;
BgpSessionTimer MinRouteAdvertisementIntervalTimer;
BgpSessionTimer DelayOpenTimer;
BgpSessionTimer IdleHoldTimer;
std::function<void(std::vector<uint8_t>)> SendMessageToPeer = [](auto bytes) { std::stringstream message; message << "Empty std::function Bgp::FiniteStateMachine::SendMessageToPeer called."; logging::ERROR(message.str()); };
std::vector<BgpCapability> Capabilities;
BgpFiniteStateMachine(const uint32_t localIpAddress, const uint32_t remoteIpAddress, const uint16_t localAsn,
const uint16_t remoteAsn, const uint32_t localRouterId, const uint32_t remoteRouterId,
const SessionAttributeFlagBits attributes, const uint16_t connectRetryTime = 120,
const uint16_t holdTime = 90, const uint16_t keepaliveTime = 90 / 3,
const uint16_t minAsOriginationIntervalTime = 0,
const uint16_t minRouteAdvertisementIntervalTime = 0, const uint16_t delayOpenTime = 0,
const uint16_t idleHoldTime = 0,
std::function<void(std::vector<uint8_t>)> sendMessageToPeer = nullptr,
const std::vector<BgpCapability> capabilities = {}) : LocalIpAddress(localIpAddress),
RemoteIpAddress(remoteIpAddress),
LocalAsn(localAsn),
RemoteAsn(remoteAsn),
LocalRouterId(localRouterId),
RemoteRouterId(remoteRouterId),
Attributes(attributes),
ConnectRetryTime(connectRetryTime),
HoldTime(holdTime),
KeepaliveTime(keepaliveTime),
MinASOriginationIntervalTime(
minAsOriginationIntervalTime),
MinRouteAdvertisementIntervalTime(
minRouteAdvertisementIntervalTime),
DelayOpenTime(delayOpenTime),
IdleHoldTime(idleHoldTime),
ConnectRetryTimer(connectRetryTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
ConnectRetryTimerExpires),
HoldTimer(holdTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
HoldTimerExpires),
KeepaliveTimer(keepaliveTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
KeepaliveTimerExpires),
MinASOriginationIntervalTimer(
minAsOriginationIntervalTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
UnknownFsmEventType),
MinRouteAdvertisementIntervalTimer(
minRouteAdvertisementIntervalTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
UnknownFsmEventType),
DelayOpenTimer(delayOpenTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
DelayOpenTimerExpires),
IdleHoldTimer(idleHoldTime,
std::shared_ptr<BgpFiniteStateMachine>(
this),
IdleHoldTimerExpires),
SendMessageToPeer(
std::move(sendMessageToPeer)),
Capabilities(capabilities) {
}
BgpFiniteStateMachine(const BgpFiniteStateMachine &other) {
this->LocalIpAddress = other.LocalIpAddress;
this->RemoteIpAddress = other.RemoteIpAddress;
this->LocalAsn = other.LocalAsn;
this->RemoteAsn = other.RemoteAsn;
this->LocalRouterId = other.LocalRouterId;
this->RemoteRouterId = other.RemoteRouterId;
this->State = other.State;
this->ConnectRetryCounter = other.ConnectRetryCounter;
this->Attributes = other.Attributes;
this->ConnectRetryTime = other.ConnectRetryTime;
this->HoldTime = other.HoldTime;
this->KeepaliveTime = other.KeepaliveTime;
this->MinASOriginationIntervalTime = other.MinASOriginationIntervalTime;
this->MinRouteAdvertisementIntervalTime = other.MinRouteAdvertisementIntervalTime;
this->DelayOpenTime = other.DelayOpenTime;
this->IdleHoldTime = other.IdleHoldTime;
this->ConnectRetryTimer = other.ConnectRetryTimer;
this->HoldTimer = other.HoldTimer;
this->KeepaliveTimer = other.KeepaliveTimer;
this->MinASOriginationIntervalTimer = other.MinASOriginationIntervalTimer;
this->MinRouteAdvertisementIntervalTimer = other.MinRouteAdvertisementIntervalTimer;
this->DelayOpenTimer = other.DelayOpenTimer;
this->IdleHoldTimer = other.IdleHoldTimer;
this->SendMessageToPeer = other.SendMessageToPeer;
this->Capabilities = other.Capabilities;
}
uint16_t ApplyJitter(const uint16_t value) {
return static_cast<uint16_t>(static_cast<float>(75 + rand() % 100) / 100.0f * static_cast<float>(value));
}
void Start() {
State = Idle;
ConnectRetryCounter = 0;
ConnectRetryTimer.InitialValue = ConnectRetryTime;
ConnectRetryTimer.Reset();
}
void HandleEvent(const FsmEventType eventType) {
std::stringstream message;
message << "Handling FSM event " << FsmEventTypeToString(eventType) << " in state " << BgpSessionStateToString(State);
logging::DEBUG(message.str());
switch (State) {
case Idle:
HandleEventInIdleState(eventType);
break;
case Connect:
HandleEventInConnectState(eventType);
break;
case Active:
HandleEventInActiveState(eventType);
break;
case OpenSent:
HandleEventInOpenSentState(eventType);
break;
case OpenConfirm:
HandleEventInOpenConfirmState(eventType);
break;
case Established:
HandleEventInEstablishedState(eventType);
break;
}
}
void HandleEventInIdleState(const FsmEventType eventType) {
switch (eventType) {
case ManualStop:
case AutomaticStop:
case ConnectRetryTimerExpires:
case HoldTimerExpires:
case KeepaliveTimerExpires:
case DelayOpenTimerExpires:
break;
case ManualStart:
case AutomaticStart:
ConnectRetryCounter = 0;
ConnectRetryTimer.Start();
// Initiate TCP connection to peer
// Listen for TCP connection from peer
State = Connect;
break;
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
ConnectRetryCounter = 0;
ConnectRetryTimer.Start();
// Listen for TCP connection from peer
State = Active;
break;
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
case IdleHoldTimerExpires:
// TODO: implement damp peer oscillation
break;
default:
break;
}
}
void HandleEventInConnectState(const FsmEventType eventType) {
switch (eventType) {
case AutomaticStart:
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
break;
case ManualStop:
// Drop the TCP connection
ConnectRetryCounter = 0;
ConnectRetryTimer.Stop();
State = Idle;
break;
case ConnectRetryTimerExpires:
// Drop the TCP connection
ConnectRetryTimer.Restart();
DelayOpenTimer.Restart();
// Initiate TCP connection to peer
// Continue to listen for TCP connection that may be initiated from peer
break;
case DelayOpenTimerExpires:
// Send OPEN message to peer
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
HoldTimer.Restart(UINT16_MAX);
State = OpenSent;
break;
case TcpConnectionValid:
// Process TCP connection from peer
break;
case TcpConnectionRequestInvalid:
// Reject TCP connection from peer
break;
case TcpConnectionRequestAcked:
case TcpConnectionConfirmed:
if (Attributes & DelayOpen) {
ConnectRetryTimer.Reset(0);
DelayOpenTimer.Restart();
} else {
ConnectRetryTimer.Reset(0);
// Complete BGP initialization
// Send OPEN message to peer
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
HoldTimer.Restart(UINT16_MAX);
State = OpenSent;
}
case TcpConnectionFails:
if (DelayOpenTimer.Active.load(std::memory_order_acquire)) {
ConnectRetryTimer.Restart();
DelayOpenTimer.Reset(0);
// Continue to listen for connection that may be initiated by peer
State = Active;
} else {
ConnectRetryTimer.Reset(0);
// Drop TCP connection
State = Idle;
}
break;
case BgpOpenWithDelayOpenTimerRunning: {
ConnectRetryTimer.Reset(0);
// Complete BGP initialization
DelayOpenTimer.Reset(0);
// Send OPEN message
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
// Send KEEPALIVE message
std::vector<uint8_t> keepaliveMessageBytes(18);
auto messageHeader = generateBgpHeader(0, MessageType::Keepalive);
keepaliveMessageBytes.insert(keepaliveMessageBytes.begin(), messageHeader.begin(), messageHeader.end());
SendMessageToPeer(keepaliveMessageBytes);
if (HoldTimer.InitialValue != 0) {
KeepaliveTimer.Start();
HoldTimer.Restart();
} else {
KeepaliveTimer.Restart();
HoldTimer.Reset(0);
}
State = OpenConfirm;
break;
}
case BgpHeaderError:
case BgpOpenMessageError: {
if (Attributes & SendNotificationWithoutOpen) {
// Send NOTIFICATION message
BgpNotificationMessage message;
message.Error.Code = eventType == BgpHeaderError ? MessageHeaderError : OpenMessageError;
message.Error.Subcode =
eventType == BgpHeaderError ? UnspecificMessageHeaderError : UnspecificOpenMessageError;
SendMessageToPeer(flattenBgpNotificationMessage(message));
}
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
}
case BgpNotificationMessageVersionError: {
if (DelayOpenTimer.Active.load(std::memory_order_acquire)) {
ConnectRetryTimer.Reset(0);
DelayOpenTimer.Reset(0);
// Release all resources
// Drop TCP connection
State = Idle;
} else {
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
}
break;
}
default:
break;
}
}
void HandleEventInActiveState(FsmEventType eventType) {
switch (eventType) {
case ManualStart:
case AutomaticStart:
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
break;
case ManualStop:
if (DelayOpenTimer.Active.load(std::memory_order_acquire) && Attributes & SendNotificationWithoutOpen) {
// Send NOTIFICATION with CEASE
BgpNotificationMessage message;
message.Error.Code = CeaseError;
message.Error.Subcode = AdministrativeShutdown;
SendMessageToPeer(flattenBgpNotificationMessage(message));
}
// Release resources
DelayOpenTimer.Stop();
// Drop TCP connection
ConnectRetryCounter = 0;
ConnectRetryTimer.Reset(0);
State = Idle;
break;
case ConnectRetryTimerExpires:
ConnectRetryTimer.Restart();
// Initiate TCP connection to peer
// Continue to listen for TCP connection from peer
State = Connect;
break;
case DelayOpenTimerExpires:
ConnectRetryTimer.Reset(0);
DelayOpenTimer.Reset(0);
// Complete BGP initialization
// Send OPEN message
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
HoldTimer.Restart(UINT16_MAX);
State = OpenSent;
break;
case TcpConnectionValid:
// Process TCP connection from peer
break;
case TcpConnectionRequestInvalid:
// Reject TCP connection from peer
break;
case TcpConnectionRequestAcked:
case TcpConnectionConfirmed:
logging::TRACE("BgpFiniteStateMachine::ActiveState::TcpConnectionConfirmed");
if (Attributes & DelayOpen) {
ConnectRetryTimer.Reset(0);
DelayOpenTimer.Restart();
} else {
ConnectRetryTimer.Reset(0);
// Complete BGP initialization
// Send OPEN message to peer
logging::TRACE("BgpFiniteStateMachine::ActiveState::TcpConnectionConfirmed::SendOPENMessageToPeer");
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
logging::TRACE("BgpFiniteStateMachine::ActiveState::TcpConnectionConfirmed::SendOPENMessageToPeer::Complete");
HoldTimer.Restart(UINT16_MAX);
State = OpenSent;
}
break;
case TcpConnectionFails:
ConnectRetryTimer.Restart();
DelayOpenTimer.Reset(0);
// Release all resources
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpOpenWithDelayOpenTimerRunning:
ConnectRetryTimer.Reset(0);
// Complete BGP initialization
DelayOpenTimer.Reset(0);
// Send OPEN message
SendMessageToPeer(flattenBgpOpenMessage({0x04, LocalAsn, HoldTime, LocalRouterId, Capabilities}));
// Send KEEPALIVE message
SendKeepaliveMessage();
if (HoldTimer.InitialValue != 0) {
KeepaliveTimer.Start();
HoldTimer.Restart();
} else {
KeepaliveTimer.Restart();
HoldTimer.Reset(0);
}
State = OpenConfirm;
break;
case BgpHeaderError:
case BgpOpenMessageError:
if (Attributes & SendNotificationWithoutOpen) {
// Send NOTIFICATION message
SendNotificationMessage(OpenMessageError, 0x00);
}
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpNotificationMessageVersionError:
if (DelayOpenTimer.Active.load(std::memory_order_acquire)) {
ConnectRetryTimer.Reset(0);
DelayOpenTimer.Reset(0);
// Release all resources
// Drop TCP connection
State = Idle;
} else {
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
}
break;
case AutomaticStop:
case HoldTimerExpires:
case KeepaliveTimerExpires:
case IdleHoldTimerExpires:
case BgpOpenMessageReceived:
case BgpOpenCollisionDump:
case BgpNotificationMessageReceived:
case BgpKeepaliveMessageReceived:
case BgpUpdateMessageReceived:
case BgpUpdateMessageError:
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
default:
break;
}
}
void SendNotificationMessage(uint8_t Code, uint8_t Subcode) {
BgpNotificationMessage message;
message.Error.Code = Code;
message.Error.Subcode = Subcode;
SendMessageToPeer(flattenBgpNotificationMessage(message));
}
void SendKeepaliveMessage() {
std::vector<uint8_t> messageBytes;
auto headerBytes = generateBgpHeader(0x00, Keepalive);
messageBytes.insert(messageBytes.end(), headerBytes.begin(), headerBytes.end());
SendMessageToPeer(messageBytes);
}
void HandleEventInOpenSentState(const FsmEventType eventType) {
switch (eventType) {
case ManualStart:
case AutomaticStart:
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
break;
case ManualStop: {
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, AdministrativeShutdown);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
ConnectRetryCounter = 0;
State = Idle;
break;
}
case AutomaticStop: {
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, AdministrativeShutdown);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
}
case HoldTimerExpires:
// Send NOTIFICATION with Hold Timer Expired
SendNotificationMessage(HoldTimerExpired, 0x00);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case TcpConnectionValid:
case TcpConnectionRequestAcked:
case TcpConnectionConfirmed:
// A second TCP connection may be in progress. Track this with connection collision processing until an OPEN message is received
case TcpConnectionRequestInvalid:
break;
case TcpConnectionFails:
// Close BGP connection
ConnectRetryTimer.Restart();
// Continue to listen for a connection that may be initiated by the remote peer
State = Active;
break;
case BgpOpenMessageReceived:
// TODO: check message fields for correctness. Either do that here, and send an event 21 or 22 if there's an error, or do it at a higher level and propagate the event to this handler.
DelayOpenTimer.Reset(0);
ConnectRetryTimer.Reset(0);
// Send KEEPALIVE message
SendKeepaliveMessage();
if (HoldTimer.InitialValue > 0) {
KeepaliveTimer.Restart();
HoldTimer.Restart();
}
State = OpenConfirm;
// TODO: implement collision detection
break;
case BgpHeaderError:
case BgpOpenMessageError:
// Send NOTIFICATION with error code
SendNotificationMessage(OpenMessageError, UnspecificOpenMessageError);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpOpenCollisionDump:
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, ConnectionCollisionResolution);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpNotificationMessageVersionError:
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
State = Idle;
break;
case ConnectRetryTimerExpires:
case KeepaliveTimerExpires:
case DelayOpenTimerExpires:
case IdleHoldTimerExpires:
case BgpOpenWithDelayOpenTimerRunning:
case BgpNotificationMessageReceived:
case BgpKeepaliveMessageReceived:
case BgpUpdateMessageReceived:
case BgpUpdateMessageError:
// Send NOTIFICATION with FSM error
SendNotificationMessage(FSMError, ReceivedUnexpectedMessageInOpenSentState);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
default:
break;
}
}
void HandleEventInOpenConfirmState(FsmEventType eventType) {
switch (eventType) {
case ManualStart:
case AutomaticStart:
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
break;
case ManualStop:
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, AdministrativeShutdown);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
ConnectRetryCounter = 0;
State = Idle;
break;
case AutomaticStop:
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, AdministrativeShutdown);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case HoldTimerExpires:
// Send NOTIFICATION with Hold Timer Expired
SendNotificationMessage(HoldTimerExpired, 0x00);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case KeepaliveTimerExpires:
// Send KEEPALIVE
SendKeepaliveMessage();
KeepaliveTimer.Restart();
break;
case TcpConnectionValid:
case TcpConnectionRequestAcked:
case TcpConnectionConfirmed:
// Need to track second TCP connection
break;
case TcpConnectionRequestInvalid:
// Ignore second connection
break;
case TcpConnectionFails:
case BgpNotificationMessageReceived:
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpNotificationMessageVersionError:
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
State = Idle;
break;
case BgpOpenMessageReceived:
if (0) {
// TODO: implement collision detection
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, ConnectionCollisionResolution);
// Release all resources
// Drop TCP connection (TCP FIN)
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
}
break;
case BgpHeaderError:
case BgpOpenMessageError:
// Send NOTIFICATION message
SendNotificationMessage(OpenMessageError, 0x00);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpOpenCollisionDump:
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, ConnectionCollisionResolution);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
case BgpKeepaliveMessageReceived:
HoldTimer.Restart();
State = Established;
break;
case ConnectRetryTimerExpires:
case DelayOpenTimerExpires:
case IdleHoldTimerExpires:
case BgpOpenWithDelayOpenTimerRunning:
case BgpUpdateMessageReceived:
case BgpUpdateMessageError:
// Send NOTIFICATION with FSM error
SendNotificationMessage(FSMError, ReceivedUnexpectedMessageInOpenConfirmState);
ConnectRetryTimer.Reset(0);
// Release all resources
// Drop TCP connection
++ConnectRetryCounter;
if (Attributes & DampPeerOscillations) {
// TODO: support for damp peer oscillations
}
State = Idle;
break;
}
}
void HandleEventInEstablishedState(FsmEventType eventType) {
switch (eventType) {
case ManualStart:
case AutomaticStart:
case ManualStartWithPassiveTcpEstablishment:
case AutomaticStartWithPassiveTcpEstablishment:
case AutomaticStartWithDampPeerOscillations:
case AutomaticStartWithDampPeerOscillationsAndPassiveTcpEstablishment:
break;
case ManualStop:
// Send NOTIFICATION with CEASE
SendNotificationMessage(CeaseError, AdministrativeShutdown);
ConnectRetryTimer.Reset(0);
// Delete all routes associated with this connection
// Release all resources
// Drop TCP connection
ConnectRetryCounter = 0;
State = Idle;
break;
case AutomaticStop:
// Send NOTIFICATION with CEASE