-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbgp_fsm_opensent.py
executable file
·158 lines (114 loc) · 5.61 KB
/
bgp_fsm_opensent.py
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
#!/usr/bin/env python3
############################################################################
# #
# PyBGP - Python BGP implementation #
# Copyright (C) 2020 Sebastian Majewski #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# Author's email: [email protected] #
# Github repository: https://github.com/ccie18643/PyBGP #
# #
############################################################################
import bgp_message
async def fsm_opensent(self, event):
""" Finite State Machine - OpenSent state """
if event.name == "Event 2: ManualStop":
self.logger.info(event.name)
# Send the NOTIFICATION with a Cease
await self.send_notification_message(bgp_message.CEASE)
# Set ConnectRetryCounter to zero
self.connect_retry_counter = 0
# Change state to Idle
self.change_state("Idle")
if event.name == "Event 8: AutomaticStop":
self.logger.info(event.name)
# Send the NOTIFICATION with a Cease
await self.send_notification_message(bgp_message.CEASE)
# Increment the ConnectRetryCounter by 1
self.connect_retry_counter += 1
# Change state to Idle
self.change_state("Idle")
if event.name == "Event 10: HoldTimer_Expires":
self.logger.info(event.name)
# Send a NOTIFICATION message with the error code Hold Timer Expired
await self.send_notification_message(bgp_message.HOLD_TIMER_EXPIRED)
# Increment ConnectRetryCounter
self.connect_retry_counter += 1
# Change state to Idle
self.change_state("Idle")
if event.name == "Event 18: TcpConnectionFails":
self.logger.info(event.name)
# Close the TCP connection
self.close_connection()
# Restart the ConnectRetryTimer
self.connect_retry_timer = self.connect_retry_time
# Set the HoldTimer to zero (not required by RFC4271)0
self.hold_timer = 0
# Stop HoldTimer (not required by RFC4271)
self.hold_timer = 0
# Stop KeepaliveTimer (not required by RFC4271)
self.keepalive_timer = 0
# Change state to Active
self.change_state("Active")
if event.name == "Event 19: BGPOpen":
self.logger.info(event.name)
message = event.message
# Set the BGP ConnectRetryTimer to zero
self.connect_retry_timer = 0
# Send a KeepAlive message
await self.send_keepalive_message()
# Set the HoldTimer according to the negotiated value
self.hold_time = min(self.local_hold_time, message.hold_time)
self.hold_timer = self.hold_time
# Set a KeepAliveTimer
self.keepalive_time = self.hold_time // 3
self.keepalive_timer = self.keepalive_time
# Save the peer BGP ID to be used for collision detection
self.peer_id = message.id
# Change state to OpenConfirm
self.change_state("OpenConfirm")
if event.name in {"Event 21: BGPHeaderErr", "Event 22: BGPOpenMsgErr"}:
self.logger.info(event.name)
message = event.message
# Send a NOTIFICATION message with the appropriate error code
await self.send_notification_message(message.message_error_code, message.message_error_subcode, message.message_error_data)
# Increment ConnectRetryCounter
self.connect_retry_counter += 1
# Change state to Idle
self.change_state("Idle")
if event.name == "Event 24: NotifMsgVerErr":
self.logger.info(event.name)
# Change state to Idle
self.change_state("Idle")
if event.name in {
"Event 9: ConnectRetryTimer_Expires",
"Event 11: KeepaliveTimer_Expires",
"Event 12: DelayOpenTimer_Expires",
"Event 13: IdleHoldTimer_Expires",
"Event 20: BGPOpen with DelayOpenTimer running",
"Event 25: NotifMsg",
"Event 26: KeepAliveMsg",
"Event 27: UpdateMsg",
"Event 28: UpdateMsgErr",
}:
self.logger.info(event.name)
# Send the NOTIFICATION with the Error Code Finite State Machine Error
await self.send_notification_message(bgp_message.FINITE_STATE_MACHINE_ERROR)
# Set ConnecRetryTimer to zro
self.connect_retry_timer = 0
# Increment the ConnectRetryCounter by 1
self.connect_retry_counter += 1
# Chhange state to Idle
self.change_state("Idle")