libbgp  0.6
A C++ BGP Library.
bgp-fsm.h
Go to the documentation of this file.
1 
11 #ifndef BGP_FSM_H_
12 #define BGP_FSM_H_
13 #define BGP_FSM_BUFFER_SIZE 4096
14 
15 #include "clock.h"
16 #include "bgp-rib4.h"
17 #include "bgp-rib6.h"
18 #include "bgp-config.h"
19 #include "bgp-sink.h"
20 #include "route-event-receiver.h"
21 #include "bgp.h"
22 #include <stdint.h>
23 #include <unistd.h>
24 #include <mutex>
25 
26 namespace libbgp {
27 
32 enum BgpState {
33  IDLE,
34  OPEN_SENT,
35  OPEN_CONFIRM,
36  ESTABLISHED,
37  BROKEN
38 };
39 
44 class BgpFsm : public RouteEventReceiver {
45 public:
46  BgpFsm(const BgpConfig &config);
47  ~BgpFsm();
48 
54  uint32_t getAsn() const;
55 
61  uint32_t getBgpId() const;
62 
70  uint32_t getPeerAsn() const;
71 
79  uint32_t getPeerBgpId() const;
80 
88  uint16_t getHoldTimer() const;
89 
95  BgpRib4& getRib4() const;
96 
102  BgpRib6& getRib6() const;
103 
109  BgpState getState() const;
110 
118  int start();
119 
126  int stop();
127 
145  int run(const uint8_t *buffer, const size_t buffer_size);
146 
161  int tick();
162 
163  // soft reset: send Administrative Reset and go to idle
164  // return value:
165  // -1: fatal_error, FSM now BROKEN, check errbuf.
166  // 0: success, NOTIFY sent, FSM not IDLE
167 
178  int resetSoft();
179 
180  // hard reset: go to idle
187  void resetHard();
188 
189 private:
190  bool rib4_local;
191  bool rib6_local;
192  bool clock_local;
193  bool log_local;
194  bool rev_bus_exist;
195 
196  bool handleRouteEvent(const RouteEvent &ev);
197  bool handleRouteCollisionEvent(const RouteCollisionEvent &ev);
198  bool handleRoute4WithdrawEvent(const Route4WithdrawEvent &ev);
199  bool handleRoute4AddEvent(const Route4AddEvent &ev);
200  bool handleRoute6WithdrawEvent(const Route6WithdrawEvent &ev);
201  bool handleRoute6AddEvent(const Route6AddEvent &ev);
202 
203  int validateState(uint8_t type);
204  int fsmEvalIdle(const BgpMessage *msg);
205  int fsmEvalOpenSent(const BgpMessage *msg);
206  int fsmEvalOpenConfirm(const BgpMessage *msg);
207  int fsmEvalEstablished(const BgpMessage *msg);
208 
209  // dropAll: drop all routes from peer and notify other FSMs w/ route event
210  // bus (if exists) (called on FSM go from ESTABLISED to IDLE)
211  void dropAllRoutes();
212 
213  // setState: set the state of FSM. additional operations may be performed.
214  void setState(BgpState state);
215 
216  // validAddr4: valid an IPv4 address (nexthop/bgp_id), addr in network btye
217  bool validAddr4(uint32_t addr) const;
218 
219  // validAddr6: valid an IPv6 address.
220  bool validAddr6(const uint8_t addr[16]) const;
221 
222  // resloveCollison: resloving collsion
223  // return value:
224  // -1: fatal_error, FSM now BROKEN, check errbuf.
225  // 0: there is a collision and this FSM should be dispose, FSM is now
226  // IDLE and should be disposed
227  // 1: there is a collision and the other FSM should be dispose.
228  int resloveCollision(uint32_t peer_bgp_id, bool is_new);
229 
230  // since we handle open recv event in both idle and opensent, make it a func
231  int openRecv(const BgpOpenMessage *open);
232 
233  bool writeMessage(const BgpMessage &msg);
234 
235  // automaically change IPv4 nexthop for outgoing routes if needed
236  void alterNexthop4 (BgpUpdateMessage &update);
237 
238  // automaically change IPv4 nexthop for outgoing routes if needed
239  void alterNexthop6 (const uint8_t* &nh_global, const uint8_t* &nh_local);
240 
241  // prepare update message for advertisement (prepend my_asn, remove
242  // non-trans attrs)
243  void prepareUpdateMessage(BgpUpdateMessage &update);
244 
245  BgpSink in_sink;
246  BgpState state;
247  BgpConfig config;
248  BgpRib4 *rib4;
249  BgpRib6 *rib6;
250  Clock *clock;
251  BgpLogHandler *logger;
252 
253  std::recursive_mutex out_buffer_mutex;
254 
255  // pointer to output buffer
256  uint8_t *out_buffer;
257 
258  // peer's bgp id
259  uint32_t peer_bgp_id;
260 
261  // negotiated hold_timer
262  uint16_t hold_timer;
263 
264  // time last event sent
265  uint64_t last_sent;
266 
267  // time last event received
268  uint64_t last_recv;
269 
270  // true if both peer & local support 4B ASN
271  bool use_4b_asn;
272 
273  bool send_ipv4_routes;
274  bool send_ipv6_routes;
275  bool ibgp;
276 
277  uint32_t peer_asn;
278 
279 };
280 
302 }
303 
304 #endif // BGP_FSM_H_
The BGP FSM configuration object.
int run(const uint8_t *buffer, const size_t buffer_size)
Run the FSM on buffer.
Definition: bgp-fsm.cc:179
The BgpRib4 (IPv4 BGP Routing Information Base) class.
Definition: bgp-rib4.h:87
BgpRib4 & getRib4() const
Get the IPv4 Routing Information Base.
Definition: bgp-fsm.cc:106
The BgpOpenMessage class.
int stop()
Stop the FSM. (Any -> Idle)
Definition: bgp-fsm.cc:157
uint32_t getBgpId() const
Get local BGP ID.
Definition: bgp-fsm.cc:90
uint32_t getPeerAsn() const
Get peer ASN.
Definition: bgp-fsm.cc:94
void resetHard()
Perform a hard reset.
Definition: bgp-fsm.cc:320
The BgpSink class.
Definition: bgp-sink.h:29
Probe for collision detection.
Definition: route-event.h:184
The libbgp clock interface.
Route event receiver.
The BgpFsm class.
Definition: bgp-fsm.h:44
BgpState
BGP Finite State Machine status.
Definition: bgp-fsm.h:32
uint32_t getPeerBgpId() const
Get peer BGP ID.
Definition: bgp-fsm.cc:98
An Route6WithdrawEvent.
Definition: route-event.h:166
The BgpMessage base class.
Definition: bgp-message.h:35
An Route4WithdrawEvent.
Definition: route-event.h:95
The BGP sink.
The BgpRib6 (IPv6 BGP Routing Information Base) class.
Definition: bgp-rib6.h:98
An Route4AddEvent.
Definition: route-event.h:54
An Route6AddEvent.
Definition: route-event.h:113
Definition: bgp-afi.h:14
The IPv4 BGP Routing Information Base.
The BGP FSM configuration object.
Definition: bgp-config.h:28
The BgpLogHandler class.
int start()
send OPEN message to peer. (IDLE -> OpenSent)
Definition: bgp-fsm.cc:118
uint32_t getAsn() const
Get local ASN.
Definition: bgp-fsm.cc:86
BgpState getState() const
Get current FSM state.
Definition: bgp-fsm.cc:114
uint16_t getHoldTimer() const
Get the negotiated hold timer.
Definition: bgp-fsm.cc:102
The RouteEventReceiver interface.
The Clock interface.
Definition: clock.h:31
BgpRib6 & getRib6() const
Get the IPv6 Routing Information Base.
Definition: bgp-fsm.cc:110
The BGP Routing Information Base for IPv6.
int resetSoft()
Perform a soft reset.
Definition: bgp-fsm.cc:313
int tick()
Tick the clock (Check for time-based events)
Definition: bgp-fsm.cc:290
BGP protocol serializers/deserializers.
The BgpUpdateMessage class.
The RouteEvent base.
Definition: route-event.h:39