OpenSDN source code
state_machine.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "bgp/state_machine.h"
7 
8 #include <boost/statechart/custom_reaction.hpp>
9 #include <boost/statechart/state.hpp>
10 #include <boost/statechart/state_machine.hpp>
11 #include <boost/statechart/transition.hpp>
12 
13 #include <algorithm>
14 #include <list>
15 #include <string>
16 #include <typeinfo>
17 #include <atomic>
18 
19 #include "base/task_annotations.h"
20 #include "bgp/bgp_log.h"
21 #include "bgp/bgp_peer.h"
22 #include "bgp/bgp_peer_close.h"
23 #include "bgp/bgp_peer_types.h"
24 #include "bgp/bgp_server.h"
25 #include "bgp/bgp_session.h"
27 
28 using std::min;
29 using std::ostream;
30 using std::ostringstream;
31 using std::string;
32 
33 namespace mpl = boost::mpl;
34 namespace sc = boost::statechart;
35 
36 const int StateMachine::kOpenTime = 15; // seconds
37 const int StateMachine::kConnectInterval = 30; // seconds
38 const int StateMachine::kHoldTime = 90; // seconds
39 const int StateMachine::kOpenSentHoldTime = 240; // seconds
41  getenv("CONTRAIL_BGP_IDLE_HOLD_TIME_MSECS") ?
42  strtol(getenv("CONTRAIL_BGP_IDLE_HOLD_TIME_MSECS"), NULL, 0) : 5000;
43 const int StateMachine::kMaxIdleHoldTime = 100 * 1000; // milliseconds
44 const int StateMachine::kJitter = 10; // percentage
45 
46 #define SM_LOG(level, _Msg) \
47  do { \
48  ostringstream out; \
49  out << _Msg; \
50  if (LoggingDisabled()) break; \
51  BGP_LOG_SERVER(peer_, (BgpTable *) 0); \
52  BGP_LOG(BgpPeerStateMachine, level, \
53  BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_NA, \
54  peer_ ? peer_->ToUVEKey() : "", \
55  out.str()); \
56  } while (false)
57 
58 #define SM_LOG_NOTICE(_Msg) \
59  do { \
60  ostringstream out; \
61  out << _Msg; \
62  if (LoggingDisabled()) break; \
63  BGP_LOG_SERVER(peer_, (BgpTable *) 0); \
64  BGP_LOG_NOTICE(BgpPeerStateMachine, \
65  BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_NA, \
66  peer_ ? peer_->ToUVEKey() : "", \
67  out.str()); \
68  } while (false)
69 
70 namespace fsm {
71 
72 // Events for the state machine. These are listed in roughly the same order
73 // as the RFC - Administrative, Timer, Tcp and Message.
74 
75 struct EvStart : sc::event<EvStart> {
76  EvStart() {
77  }
78  static const char *Name() {
79  return "EvStart";
80  }
81 };
82 
83 struct EvStop : sc::event<EvStop> {
84  explicit EvStop(int subcode) : subcode(subcode) {
85  }
86  static const char *Name() {
87  return "EvStop";
88  }
89  int subcode;
90 };
91 
92 struct EvIdleHoldTimerExpired : sc::event<EvIdleHoldTimerExpired> {
93  explicit EvIdleHoldTimerExpired(Timer *timer) : timer_(timer) {
94  }
95  static const char *Name() {
96  return "EvIdleHoldTimerExpired";
97  }
98  bool validate(StateMachine *state_machine) const {
99  return !timer_->cancelled();
100  }
101 
103 };
104 
105 struct EvConnectTimerExpired : sc::event<EvConnectTimerExpired> {
106  explicit EvConnectTimerExpired(Timer *timer) : timer_(timer) {
107  }
108  static const char *Name() {
109  return "EvConnectTimerExpired";
110  }
111  bool validate(StateMachine *state_machine) const {
112  if (timer_->cancelled()) {
113  return false;
114  } else if (state_machine->get_state() == StateMachine::ACTIVE) {
115  return (state_machine->passive_session() == NULL);
116  } else if (state_machine->get_state() == StateMachine::CONNECT) {
117  return (!state_machine->active_session() ||
118  !state_machine->active_session()->IsEstablished());
119  }
120  return false;
121  }
122 
124 };
125 
126 struct EvOpenTimerExpired : sc::event<EvOpenTimerExpired> {
127  explicit EvOpenTimerExpired(Timer *timer) : timer_(timer) {
128  }
129  static const char *Name() {
130  return "EvOpenTimerExpired";
131  }
132  bool validate(StateMachine *state_machine) const {
133  if (timer_->cancelled()) {
134  return false;
135  } else {
136  return (state_machine->passive_session() != NULL);
137  }
138  }
139 
141 };
142 
143 struct EvHoldTimerExpired : sc::event<EvHoldTimerExpired> {
144  explicit EvHoldTimerExpired(Timer *timer) : timer_(timer) {
145  }
146  static const char *Name() {
147  return "EvHoldTimerExpired";
148  }
149  bool validate(StateMachine *state_machine) const {
150  if (timer_->cancelled()) {
151  return false;
152  } else if (state_machine->get_state() == StateMachine::OPENSENT) {
153  return true;
154  } else {
155  return (state_machine->peer()->session() != NULL);
156  }
157  }
158 
160 };
161 
162 struct EvTcpConnected : sc::event<EvTcpConnected> {
164  }
165  static const char *Name() {
166  return "EvTcpConnected";
167  }
168  bool validate(StateMachine *state_machine) const {
169  return (state_machine->active_session() == session);
170  }
171 
173 };
174 
175 struct EvTcpConnectFail : sc::event<EvTcpConnectFail> {
177  }
178  static const char *Name() {
179  return "EvTcpConnectFail";
180  }
181  bool validate(StateMachine *state_machine) const {
182  return (state_machine->active_session() == session);
183  }
184 
186 };
187 
188 struct EvTcpPassiveOpen : sc::event<EvTcpPassiveOpen> {
190  }
191  static const char *Name() {
192  return "EvTcpPassiveOpen";
193  }
194 
196 };
197 
198 struct EvTcpClose : sc::event<EvTcpClose> {
200  }
201  static const char *Name() {
202  return "EvTcpClose";
203  }
204  bool validate(StateMachine *state_machine) const {
205  return ((state_machine->peer()->session() == session) ||
206  (state_machine->active_session() == session) ||
207  (state_machine->passive_session() == session));
208  }
209 
211 };
212 
213 // Used to defer the session delete after all events currently on the queue.
214 struct EvTcpDeleteSession : sc::event<EvTcpDeleteSession> {
216  }
217  static const char *Name() {
218  return "EvTcpDeleteSession";
219  }
220 
222 };
223 
224 struct EvBgpHeaderError : sc::event<EvBgpHeaderError> {
225  EvBgpHeaderError(BgpSession *session, int subcode, const uint8_t *_data,
226  size_t data_size)
228  if (_data)
229  data = std::string((const char *)_data, data_size);
230  }
231  static const char *Name() {
232  return "EvBgpHeaderError";
233  }
234 
236  int subcode;
237  std::string data;
238 };
239 
240 struct EvBgpOpen : sc::event<EvBgpOpen> {
242  : session(session), msg(msg) {
243  BGP_LOG_PEER(Message, session->peer(), SandeshLevel::SYS_INFO,
245  "Open " << msg->ToString());
246  }
247  static const char *Name() {
248  return "EvBgpOpen";
249  }
250  bool validate(StateMachine *state_machine) const {
251  return ((state_machine->peer()->session() == session) ||
252  (state_machine->active_session() == session) ||
253  (state_machine->passive_session() == session));
254  }
255 
257  boost::shared_ptr<const BgpProto::OpenMessage> msg;
258 };
259 
260 struct EvBgpOpenError : sc::event<EvBgpOpenError> {
262  const uint8_t *_data = NULL, size_t data_size = 0)
265  // For unsupported version, we need to send our version in the
266  // data field.
267  char version = 4;
268  data.push_back(version);
269  } else if (_data) {
270  data = std::string((const char *)_data, data_size);
271  }
272  }
273  static const char *Name() {
274  return "EvBgpOpenError";
275  }
276 
278  int subcode;
279  std::string data;
280 };
281 
282 struct EvBgpKeepalive : sc::event<EvBgpKeepalive> {
284  const StateMachine *state_machine = session->peer()->state_machine();
285  SandeshLevel::type log_level;
286  if (state_machine->get_state() == StateMachine::ESTABLISHED) {
287  log_level = Sandesh::LoggingUtLevel();
288  } else {
289  log_level = SandeshLevel::SYS_INFO;
290  }
291  BGP_LOG_PEER(Message, session->peer(), log_level,
292  BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_IN, "Keepalive");
293  }
294  static const char *Name() {
295  return "EvBgpKeepalive";
296  }
297  bool validate(StateMachine *state_machine) const {
298  return !session->IsClosed();
299  }
300 
302 };
303 
304 struct EvBgpNotification : sc::event<EvBgpNotification> {
306  : session(session), msg(msg) {
307  string peer_key =
308  session->peer() ? session->peer()->ToUVEKey() : session->ToString();
309  session->LogNotification(msg->error, msg->subcode, BGP_PEER_DIR_IN,
310  peer_key, *msg);
311  }
312  static const char *Name() {
313  return "EvBgpNotification";
314  }
315  bool validate(StateMachine *state_machine) const {
316  return ((state_machine->peer()->session() == session) ||
317  (state_machine->active_session() == session) ||
318  (state_machine->passive_session() == session));
319  }
320 
322  boost::shared_ptr<const BgpProto::Notification> msg;
323 };
324 
325 struct EvBgpUpdate : sc::event<EvBgpUpdate> {
327  size_t msgsize) : session(session), msg(msg), msgsize(msgsize) {
328  }
329  static const char *Name() {
330  return "EvBgpUpdate";
331  }
332 
334  boost::shared_ptr<const BgpProto::Update> msg;
335  size_t msgsize;
336 };
337 
338 struct EvBgpUpdateError : sc::event<EvBgpUpdateError> {
341  }
342  static const char *Name() {
343  return "EvBgpUpdateError";
344  }
345 
347  int subcode;
348  std::string data;
349 };
350 
351 // States for the BGP state machine.
352 struct Idle;
353 struct Active;
354 struct Connect;
355 struct OpenSent;
356 struct OpenConfirm;
357 struct Established;
358 
359 template <typename Ev, int code = 0>
361  typedef sc::transition<Ev, Idle, StateMachine,
362  &StateMachine::OnIdle<Ev, code> > reaction;
363 };
364 
365 template <>
367  typedef sc::transition<EvBgpNotification, Idle, StateMachine,
369 };
370 
371 template <typename Ev>
372 struct IdleCease {
373  typedef sc::transition<Ev, Idle, StateMachine,
374  &StateMachine::OnIdleCease<Ev> > reaction;
375 };
376 
377 template <typename Ev>
378 struct IdleFsmError {
379  typedef sc::transition<Ev, Idle, StateMachine,
380  &StateMachine::OnIdle<Ev, BgpProto::Notification::FSMErr> > reaction;
381 };
382 
383 template <typename Ev, int code>
384 struct IdleError {
385  typedef sc::transition<Ev, Idle, StateMachine,
386  &StateMachine::OnIdleError<Ev, code> > reaction;
387 };
388 
389 //
390 // We start out in Idle and progress when we get EvStart. We also come back
391 // to Idle when there's any kind of error that we need to recover from.
392 //
393 struct Idle : sc::state<Idle, StateMachine> {
394  typedef mpl::list<
396  sc::custom_reaction<EvStart>,
397  sc::custom_reaction<EvIdleHoldTimerExpired>,
398  sc::custom_reaction<EvTcpPassiveOpen>
400 
401  // Increment the flap count after setting the state. This is friendly to
402  // tests that first wait for the flap count to go up and then wait for the
403  // state to reach ESTABLISHED again. Incrementing the flap count before
404  // setting the state could cause tests to break if they look at the old
405  // state (which is still ESTABLISHED) and assume that it's the new state.
406  // This could also be solved by using a mutex but it's not really needed.
407  explicit Idle(my_context ctx) : my_base(ctx) {
408  StateMachine *state_machine = &context<StateMachine>();
409  BgpPeer *peer = state_machine->peer();
410  BgpSession *session = peer->session();
411  peer->clear_session();
412  state_machine->set_active_session(NULL);
413  state_machine->set_passive_session(NULL);
414  state_machine->DeleteSession(session);
415  state_machine->CancelOpenTimer();
416  state_machine->CancelIdleHoldTimer();
417  state_machine->set_state(StateMachine::IDLE);
418  }
419 
420  ~Idle() {
421  StateMachine *state_machine = &context<StateMachine>();
422  state_machine->CancelIdleHoldTimer();
423  }
424 
425  // Start idle hold timer if it's enabled, else go to Active right away.
426  sc::result react(const EvStart &event) {
427  StateMachine *state_machine = &context<StateMachine>();
428  if (state_machine->idle_hold_time()) {
429  state_machine->StartIdleHoldTimer();
430  } else {
431  return transit<Active>();
432  }
433  return discard_event();
434  }
435 
436  // The idle hold timer expired, go to Active.
437  sc::result react(const EvIdleHoldTimerExpired &event) {
438  return transit<Active>();
439  }
440 
441  // Delete the session and ignore event.
442  sc::result react(const EvTcpPassiveOpen &event) {
443  StateMachine *state_machine = &context<StateMachine>();
444  BgpSession *session = event.session;
445  state_machine->DeleteSession(session);
446  return discard_event();
447  }
448 };
449 
450 //
451 // In Active state, we wait for the connect timer timer to expire before we
452 // move to Connect and start the active session. If we get a passive session
453 // we accept it and wait for our delayed open timer to expire.
454 //
455 struct Active : sc::state<Active, StateMachine> {
456  typedef mpl::list<
458  sc::custom_reaction<EvConnectTimerExpired>,
459  sc::custom_reaction<EvOpenTimerExpired>,
460  sc::custom_reaction<EvTcpPassiveOpen>,
461  sc::custom_reaction<EvTcpClose>,
462  sc::custom_reaction<EvBgpOpen>,
473 
474  // Start the connect timer if the peer is not passive and we don't have
475  // a passive session. There may a passive session if we got here from
476  // Connect or OpenSent.
477  explicit Active(my_context ctx) : my_base(ctx) {
478  StateMachine *state_machine = &context<StateMachine>();
479  BgpPeer *peer = state_machine->peer();
480  if (!peer->IsPassive() && !state_machine->passive_session())
481  state_machine->StartConnectTimer(state_machine->GetConnectTime());
482  state_machine->set_state(StateMachine::ACTIVE);
483  }
484 
485  // Stop the connect timer. If we are going to Connect state, the timer
486  // will be started again from the constructor for that state.
488  StateMachine *state_machine = &context<StateMachine>();
489  state_machine->CancelConnectTimer();
490  }
491 
492  // The connect timer expired, go to Connect if the peer is not passive.
493  sc::result react(const EvConnectTimerExpired &event) {
494  StateMachine *state_machine = &context<StateMachine>();
495  BgpPeer *peer = state_machine->peer();
496  if (peer->IsPassive()) {
497  return discard_event();
498  } else {
499  return transit<Connect>();
500  }
501  }
502 
503  // Send an OPEN message on the passive session and go to OpenSent.
504  sc::result react(const EvOpenTimerExpired &event) {
505  StateMachine *state_machine = &context<StateMachine>();
506  BgpSession *session = state_machine->passive_session();
507  if (session) {
508  BgpPeer *peer = state_machine->peer();
509  peer->SendOpen(session);
510  return transit<OpenSent>();
511  }
512  return discard_event();
513  }
514 
515  // Cancel the connect timer since we now have a passive session. Note
516  // that we get rid of any existing passive session if we get another
517  // one. Also start the open timer in order to implement a delayed open
518  // on the passive session.
519  sc::result react(const EvTcpPassiveOpen &event) {
520  StateMachine *state_machine = &context<StateMachine>();
521  state_machine->set_passive_session(event.session);
522  state_machine->CancelConnectTimer();
523  state_machine->StartOpenTimer(StateMachine::kOpenTime);
524  return discard_event();
525  }
526 
527  // Start the connect timer since we don't have a passive session anymore.
528  sc::result react(const EvTcpClose &event) {
529  StateMachine *state_machine = &context<StateMachine>();
530  if (event.session == state_machine->passive_session()) {
531  state_machine->set_passive_session(NULL);
532  state_machine->CancelOpenTimer();
533  state_machine->StartConnectTimer(state_machine->GetConnectTime());
534  }
535  return discard_event();
536  }
537 
538  // We received an OPEN message on the passive session. Send OPEN message
539  // and go to OpenConfirm.
540  sc::result react(const EvBgpOpen &event) {
541  StateMachine *state_machine = &context<StateMachine>();
542  BgpPeer *peer = state_machine->peer();
543  BgpSession *session = state_machine->passive_session();
544 
545  // If EvTcpPassiveOpen was received in IDLE state and the following
546  // open message happens to be processed when we are in Active state,
547  // we may not have the passive session for open message. Ignore the
548  // event in that case.
549  if (!session)
550  return discard_event();
551 
552  // Ignore the OPEN if it was received on a stale passive session.
553  // This can happen if we got another passive session between the
554  // original passive session and the OPEN message on that session.
555  if (session != event.session)
556  return discard_event();
557 
558  // Send OPEN and go to OpenConfirm.
559  int local_holdtime = state_machine->GetConfiguredHoldTime();
560  state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
561  state_machine->AssignSession(false);
562  peer->SendOpen(session);
563  if (!peer->SetCapabilities(event.msg.get()))
564  return discard_event();
565  return transit<OpenConfirm>();
566  }
567 };
568 
569 //
570 // In Connect state, we wait for the active session to come up. We also accept
571 // a passive session if we get one and start a delayed open timer.
572 //
573 struct Connect : sc::state<Connect, StateMachine> {
574  typedef mpl::list<
576  sc::custom_reaction<EvConnectTimerExpired>,
577  sc::custom_reaction<EvOpenTimerExpired>,
578  sc::custom_reaction<EvTcpConnected>,
579  sc::custom_reaction<EvTcpConnectFail>,
580  sc::custom_reaction<EvTcpPassiveOpen>,
581  sc::custom_reaction<EvTcpClose>,
582  sc::custom_reaction<EvBgpOpen>,
593 
594  explicit Connect(my_context ctx) : my_base(ctx) {
595  StateMachine *state_machine = &context<StateMachine>();
596  state_machine->connect_attempts_inc();
597  state_machine->StartConnectTimer(state_machine->GetConnectTime());
598  state_machine->StartSession();
599  state_machine->set_state(StateMachine::CONNECT);
600  }
601 
603  StateMachine *state_machine = &context<StateMachine>();
604  state_machine->CancelConnectTimer();
605  }
606 
607  // Get rid of the active session and go back to Active.
608  sc::result react(const EvConnectTimerExpired &event) {
609  StateMachine *state_machine = &context<StateMachine>();
610  BgpPeer *peer = state_machine->peer();
611  state_machine->set_active_session(NULL);
613  return transit<Active>();
614  }
615 
616  // The open timer for the passive session expired. Since the active
617  // session has not yet come up we get rid of it and decide to use the
618  // passive session. Send an OPEN on the passive session and move to
619  // OpenSent.
620  sc::result react(const EvOpenTimerExpired &event) {
621  StateMachine *state_machine = &context<StateMachine>();
622  BgpPeer *peer = state_machine->peer();
623  peer->SendOpen(state_machine->passive_session());
624  state_machine->set_active_session(NULL);
625  return transit<OpenSent>();
626  }
627 
628  // The active session is up. Send an OPEN right away and go to OpenSent.
629  // Note that we may also have the open timer running if we have a passive
630  // session. Things will eventually get resolved in the OpenSent state.
631  sc::result react(const EvTcpConnected &event) {
632  StateMachine *state_machine = &context<StateMachine>();
633  BgpPeer *peer = state_machine->peer();
634  BgpSession *session = state_machine->active_session();
635  peer->SendOpen(session);
636  return transit<OpenSent>();
637  }
638 
639  // Delete the active session and go to Active. Note that we may still
640  // have a passive session.
641  sc::result react(const EvTcpConnectFail &event) {
642  StateMachine *state_machine = &context<StateMachine>();
643  state_machine->set_active_session(NULL);
644  return transit<Active>();
645  }
646 
647  // Start the open timer in order to implement a delayed open on passive
648  // session. Note that we get rid of any existing passive session if we
649  // had one.
650  sc::result react(const EvTcpPassiveOpen &event) {
651  StateMachine *state_machine = &context<StateMachine>();
652  state_machine->set_passive_session(event.session);
653  state_machine->StartOpenTimer(StateMachine::kOpenTime);
654  return discard_event();
655  }
656 
657  // Either the active or passive session got closed.
658  sc::result react(const EvTcpClose &event) {
659  StateMachine *state_machine = &context<StateMachine>();
660  if (event.session == state_machine->passive_session()) {
661  // Get rid of the passive session and cancel the open timer.
662  // Stay in Connect and wait for the active session to come up.
663  state_machine->set_passive_session(NULL);
664  state_machine->CancelOpenTimer();
665  return discard_event();
666  } else {
667  // Get rid of the active session and go to Active. Note that we
668  // may still have a passive session at this point.
669  assert(event.session == state_machine->active_session());
670  state_machine->set_active_session(NULL);
671  return transit<Active>();
672  }
673  }
674 
675  // We received an OPEN message on the passive session. Send OPEN message
676  // and go to OpenConfirm.
677  sc::result react(const EvBgpOpen &event) {
678  StateMachine *state_machine = &context<StateMachine>();
679  BgpPeer *peer = state_machine->peer();
680  BgpSession *session = state_machine->passive_session();
681 
682  // If EvTcpPassiveOpen was received in IDLE state and the following
683  // open message happens to be processed when we are in Connect state,
684  // we may not have the passive session for open message. Ignore the
685  // event in that case.
686  if (!session)
687  return discard_event();
688 
689  // Ignore the OPEN if it was received on a stale passive session.
690  // This can happen if we got another passive session between the
691  // original passive session and the OPEN message on that session.
692  if (session != event.session)
693  return discard_event();
694 
695  // Send OPEN and go to OpenConfirm. Since we've decided to use the
696  // passive session, we get rid of the active one.
697  int local_holdtime = state_machine->GetConfiguredHoldTime();
698  state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
699  state_machine->set_active_session(NULL);
700  state_machine->AssignSession(false);
701  peer->SendOpen(session);
702  if (!peer->SetCapabilities(event.msg.get()))
703  return discard_event();
704  return transit<OpenConfirm>();
705  }
706 };
707 
708 //
709 // In the OpenSent state, we wait for the other end to send an OPEN message.
710 // The state machine reaches OpenSent after sending an immediate OPEN message
711 // on the active connection or a delayed OPEN on a passive connection. In the
712 // former case there may be both a passive and active session. In the latter,
713 // there is only a passive connection.
714 //
715 struct OpenSent : sc::state<OpenSent, StateMachine> {
716  typedef mpl::list<
718  sc::custom_reaction<EvOpenTimerExpired>,
721  sc::custom_reaction<EvTcpPassiveOpen>,
722  sc::custom_reaction<EvTcpClose>,
723  sc::custom_reaction<EvBgpOpen>,
724  sc::custom_reaction<EvBgpNotification>,
734 
735  // Start the hold timer to ensure that we don't get stuck in OpenSent if
736  // the other end never sends an OPEN message.
737  explicit OpenSent(my_context ctx) : my_base(ctx) {
738  StateMachine *state_machine = &context<StateMachine>();
740  state_machine->StartHoldTimer();
741  state_machine->set_state(StateMachine::OPENSENT);
742  }
743 
744  // Cancel the hold timer. If we go to OpenConfirm, the timer will get
745  // started again from the constructor for that state.
747  StateMachine *state_machine = &context<StateMachine>();
748  state_machine->CancelHoldTimer();
749  }
750 
751  // Send an OPEN message on the passive session. This means that we must
752  // have got to OpenSent because we sent an OPEN on the active session.
753  // Stay in OpenSent and wait for the other end to send an OPEN message.
754  sc::result react(const EvOpenTimerExpired &event) {
755  StateMachine *state_machine = &context<StateMachine>();
756  BgpPeer *peer = state_machine->peer();
757  peer->SendOpen(state_machine->passive_session());
758  return discard_event();
759  }
760 
761  // Update the passive session and start the open timer. Note that any
762  // existing passive session will get deleted.
763  sc::result react(const EvTcpPassiveOpen &event) {
764  StateMachine *state_machine = &context<StateMachine>();
765  state_machine->set_passive_session(event.session);
766  state_machine->StartOpenTimer(StateMachine::kOpenTime);
767 
768  // If we don't have an active session, we need to go back to Active
769  // since we haven't sent an OPEN message on the new passive session.
770  // If we have active session, it means that we sent an OPEN message
771  // on it already, so we can stay in OpenSent.
772  if (!state_machine->active_session()) {
773  return transit<Active>();
774  } else {
775  return discard_event();
776  }
777  }
778 
779  // Either the passive or the active session closed.
780  sc::result react(const EvTcpClose &event) {
781  StateMachine *state_machine = &context<StateMachine>();
782  if (event.session == state_machine->active_session()) {
783  // Since the active session was closed, we go back to Active if
784  // don't have a passive session or if we haven't yet sent an OPEN
785  // on the passive session.
786  state_machine->set_active_session(NULL);
787  if (state_machine->passive_session() == NULL ||
788  state_machine->OpenTimerRunning()) {
789  return transit<Active>();
790  }
791  } else {
792  // Since the passive session was closed, we cancel the open timer.
793  // We need to go back to Active if don't have a active session.
794  state_machine->set_passive_session(NULL);
795  state_machine->CancelOpenTimer();
796  if (state_machine->active_session() == NULL)
797  return transit<Active>();
798  }
799 
800  return discard_event();
801  }
802 
803  // This one is pretty involved.
804  sc::result react(const EvBgpOpen &event) {
805  StateMachine *state_machine = &context<StateMachine>();
806  BgpPeer *peer = state_machine->peer();
807  BgpSession *session = NULL;
808 
809  if (state_machine->passive_session() &&
810  state_machine->active_session()) {
811  // Need to resolve connection collision.
812  uint32_t local_bgp_id = peer->server()->bgp_identifier();
813  if (event.msg->identifier > local_bgp_id) {
814  // Passive connection wins, close the active session.
815  peer->SendNotification(state_machine->active_session(),
818  "Connection collision - closing active session");
819  state_machine->set_active_session(NULL);
820 
821  // If we haven't already sent an OPEN message on the passive
822  // session, cancel the open timer and send the OPEN message.
823  session = state_machine->passive_session();
824  if (state_machine->OpenTimerRunning()) {
825  state_machine->CancelOpenTimer();
826  peer->SendOpen(session);
827  }
828 
829  // If the OPEN was not received on the passive session, stay
830  // in OpenSent and wait for the other end to send the OPEN on
831  // on the passive session.
832  // If the OPEN was received on the passive session, we assign
833  // the passive session to the peer and fall through to go to
834  // OpenConfirm.
835  if (event.session != session) {
836  return discard_event();
837  } else {
838  state_machine->AssignSession(false);
839  }
840  } else {
841  // Active connection wins, close the passive session.
842  peer->SendNotification(state_machine->passive_session(),
845  "Connection collision - closing passive session");
846  state_machine->set_passive_session(NULL);
847  state_machine->CancelOpenTimer();
848 
849  // If the OPEN was not received on the active session, stay
850  // in OpenSent and wait for the other end to send the OPEN on
851  // on the active session.
852  // If the OPEN was received on the active session, we assign
853  // the active session to the peer and fall through to go to
854  // OpenConfirm.
855  session = state_machine->active_session();
856  if (event.session != session) {
857  return discard_event();
858  } else {
859  state_machine->AssignSession(true);
860  }
861  }
862  } else if (state_machine->passive_session()) {
863  // If the OPEN was not received on the passive session, stay
864  // in OpenSent and wait for the other end to send the OPEN on
865  // on the passive session.
866  // If the OPEN was received on the passive session, we assign
867  // the passive session to the peer and fall through to go to
868  // OpenConfirm.
869  session = state_machine->passive_session();
870  if (event.session != session) {
871  return discard_event();
872  } else {
873  state_machine->AssignSession(false);
874  }
875  } else if (state_machine->active_session()) {
876  // If the OPEN was not received on the active session, stay
877  // in OpenSent and wait for the other end to send the OPEN on
878  // on the active session.
879  // If the OPEN was received on the active session, we assign
880  // the active session to the peer and fall through to go to
881  // OpenConfirm.
882  session = state_machine->active_session();
883  if (event.session != session) {
884  return discard_event();
885  } else {
886  state_machine->AssignSession(true);
887  }
888  }
889 
890  int local_holdtime = state_machine->GetConfiguredHoldTime();
891  state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
892  if (!peer->SetCapabilities(event.msg.get()))
893  return discard_event();
894  return transit<OpenConfirm>();
895  }
896 
897  // Notification received on one of the sessions for this state machine.
898  sc::result react(const EvBgpNotification &event) {
899  StateMachine *state_machine = &context<StateMachine>();
900 
901  // Ignore if the NOTIFICATION came in on a stale session.
902  if (!state_machine->ProcessNotificationEvent(event.session))
903  return discard_event();
904 
905  // The call to ProcessNotificationEvent above would have closed
906  // the session on the which the message was received.
907  if (state_machine->active_session()) {
908  // Since we still have an active session, the passive session
909  // has been closed, so we cancel the open timer. We stay in
910  // OpenSent since we still have an active session on which we
911  // have already sent an OPEN message.
912  state_machine->CancelOpenTimer();
913  return discard_event();
914  } else if (state_machine->passive_session()) {
915  // Since we still have the passive session, the active session
916  // has been closed. If the open timer is still running, we go
917  // back to Active because we don't have an active session now.
918  // If the open timer has already expired, we stay in OpenSent
919  // since we have sent an OPEN on the passive session.
920  if (state_machine->OpenTimerRunning()) {
921  return transit<Active>();
922  } else {
923  return discard_event();
924  }
925  } else {
926  // We have neither an active or passive session. Go to Idle.
927  return transit<Idle, StateMachine, EvBgpNotification>(
928  &StateMachine::OnIdle<EvBgpNotification, 0>, event);
929  }
930  }
931 };
932 
933 //
934 // In OpenConfirm, we wait for the other end to send a KEEPALIVE.
935 //
936 struct OpenConfirm : sc::state<OpenConfirm, StateMachine> {
937  typedef mpl::list<
942  sc::custom_reaction<EvTcpPassiveOpen>,
945  sc::custom_reaction<EvBgpNotification>,
946  sc::custom_reaction<EvBgpKeepalive>,
955 
956  // Send a KEEPALIVE and start the keepalive timer on the peer. Also start
957  // the hold timer based on the negotiated hold time value.
958  explicit OpenConfirm(my_context ctx) : my_base(ctx) {
959  StateMachine *state_machine = &context<StateMachine>();
960  BgpPeer *peer = state_machine->peer();
961  peer->SendKeepalive(false);
962  peer->StartKeepaliveTimer();
963  state_machine->CancelOpenTimer();
964  state_machine->StartHoldTimer();
965  state_machine->set_state(StateMachine::OPENCONFIRM);
966  }
967 
968  // Cancel the hold timer. If we go to Established, the timer will get
969  // started again from the constructor for that state.
971  StateMachine *state_machine = &context<StateMachine>();
972  state_machine->CancelHoldTimer();
973  }
974 
975  // Send a notification, delete the new session and stay in OpenConfirm.
976  sc::result react(const EvTcpPassiveOpen &event) {
977  StateMachine *state_machine = &context<StateMachine>();
978  BgpPeer *peer = state_machine->peer();
979  BgpSession *session = event.session;
980  peer->SendNotification(session,
983  "Connection rejected - unexpected passive session");
984  state_machine->DeleteSession(session);
985  return discard_event();
986  }
987 
988  // Ignore the notification if it's for a stale session, else go to Idle.
989  sc::result react(const EvBgpNotification &event) {
990  StateMachine *state_machine = &context<StateMachine>();
991  if (!state_machine->ProcessNotificationEvent(event.session))
992  return discard_event();
993 
994  return transit<Idle, StateMachine, EvBgpNotification>(
995  &StateMachine::OnIdle<EvBgpNotification, 0>, event);
996  }
997 
998  // Go to Established. The hold timer will be started in the constructor
999  // for that state.
1000  sc::result react(const EvBgpKeepalive &event) {
1001  // If GR timers started running just at the same time when the peer
1002  // came back up, then gracefully close the session.
1003  StateMachine *state_machine = &context<StateMachine>();
1004  if (state_machine->IsPeerCloseInProgress()) {
1005  return transit<Idle, StateMachine, EvBgpKeepalive>(
1006  &StateMachine::OnIdle<EvBgpKeepalive, 0>, event);
1007  }
1008  return transit<Established>();
1009  }
1010 };
1011 
1012 //
1013 // Established is the final state for an operation peer.
1014 //
1015 struct Established : sc::state<Established, StateMachine> {
1016  typedef mpl::list<
1021  sc::custom_reaction<EvTcpPassiveOpen>,
1025  sc::custom_reaction<EvBgpKeepalive>,
1026  sc::custom_reaction<EvBgpUpdate>,
1033 
1034  explicit Established(my_context ctx) : my_base(ctx) {
1035  StateMachine *state_machine = &context<StateMachine>();
1036  BgpPeer *peer = state_machine->peer();
1037  state_machine->connect_attempts_clear();
1038  state_machine->StartHoldTimer();
1039  state_machine->set_state(StateMachine::ESTABLISHED);
1040  peer->NotifyEstablished(true);
1041  peer->RegisterAllTables();
1042  }
1043 
1045  StateMachine *state_machine = &context<StateMachine>();
1046  BgpPeer *peer = state_machine->peer();
1047  peer->NotifyEstablished(false);
1048  state_machine->CancelHoldTimer();
1049  }
1050 
1051  // A new TCP session request should cause the previous BGP session to be
1052  // closed in case GR Helper mode is active or if peer router type is BGPaaS.
1053  sc::result react(const EvTcpPassiveOpen &event) {
1054  StateMachine *state_machine = &context<StateMachine>();
1055  BgpSession *session = event.session;
1056  state_machine->DeleteSession(session);
1057  if (state_machine->IsCloseGraceful() ||
1058  state_machine->IsRouterTypeBGPaaS()) {
1059  state_machine->Shutdown(BgpProto::Notification::Unknown);
1060  }
1061  return discard_event();
1062  }
1063 
1064  // Restart the hold timer.
1065  sc::result react(const EvBgpKeepalive &event) {
1066  StateMachine *state_machine = &context<StateMachine>();
1067  state_machine->StartHoldTimer();
1068  return discard_event();
1069  }
1070 
1071  // Restart the hold timer and process the update.
1072  sc::result react(const EvBgpUpdate &event) {
1073  StateMachine *state_machine = &context<StateMachine>();
1074  state_machine->StartHoldTimer();
1075  state_machine->peer()->ProcessUpdate(event.msg.get(), event.msgsize);
1076  return discard_event();
1077  }
1078 };
1079 
1080 } // namespace fsm
1081 
1083  : work_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1084  peer->GetTaskInstance(),
1085  boost::bind(&StateMachine::DequeueEvent, this, _1)),
1086  peer_(peer),
1087  active_session_(NULL),
1088  passive_session_(NULL),
1089  connect_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1090  "Connect timer",
1091  TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1092  peer->GetTaskInstance())),
1093  open_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1094  "Open timer",
1095  TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1096  peer->GetTaskInstance())),
1097  hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1098  "Hold timer",
1099  TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1100  peer->GetTaskInstance())),
1101  idle_hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1102  "Idle hold timer",
1103  TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1104  peer->GetTaskInstance())),
1105  hold_time_(GetConfiguredHoldTime()),
1106  idle_hold_time_(0),
1107  attempts_(0),
1108  deleted_(false),
1109  state_(IDLE),
1110  last_state_(IDLE) {
1111  seed_ = peer_->bgp_identifier();
1112  initiate();
1113 }
1114 
1120 }
1121 
1122 //
1123 // Delete timers after state machine is terminated so that there is no
1124 // possible reference to the timers being deleted any more
1125 //
1128  terminate();
1129  DeleteAllTimers();
1130 }
1131 
1133  Enqueue(fsm::EvStart());
1134 }
1135 
1137  return peer_->IsCloseInProgress();
1138 }
1139 
1140 void StateMachine::Shutdown(int subcode) {
1141  if (peer_->IsDeleted()) {
1143  boost::bind(&StateMachine::DequeueEventDone, this, _1));
1144  }
1145  Enqueue(fsm::EvStop(subcode));
1146 }
1147 
1148 void StateMachine::SetAdminState(bool down, int subcode) {
1149  if (down) {
1150  Enqueue(fsm::EvStop(subcode));
1151  } else {
1152  // Reset all previous state.
1154  reset_last_info();
1156  if (!IsPeerCloseInProgress())
1157  Enqueue(fsm::EvStart());
1158  }
1159 }
1160 
1162  return work_queue_.IsQueueEmpty();
1163 }
1164 
1168  peer_->peer_stats()->Clear();
1169  }
1170 }
1171 
1172 void StateMachine::PeerClose(int code, int subcode) {
1173  UpdateFlapCount();
1174  peer_->Close(peer_->AttemptGRHelperMode(code, subcode));
1177  reset_hold_time();
1178 }
1179 
1180 template <typename Ev, int code>
1181 void StateMachine::OnIdle(const Ev &event) {
1182  SendNotification(peer_->session(), code);
1183  PeerClose(code, 0);
1184 }
1185 
1186 template <typename Ev>
1187 void StateMachine::OnIdleCease(const Ev &event) {
1189  event.subcode);
1190  PeerClose(BgpProto::Notification::Cease, event.subcode);
1191 }
1192 
1193 //
1194 // The template below must only be called for EvBgpHeaderError, EvBgpOpenError
1195 // or EvBgpUpdateError.
1196 //
1197 template <typename Ev, int code>
1198 void StateMachine::OnIdleError(const Ev &event) {
1199  SendNotification(event.session, code, event.subcode, event.data);
1200  PeerClose(code, event.subcode);
1201 }
1202 
1203 // Close the peer. No need to send a notification as peer has already closed
1204 // this session by sending us a notification message.
1206  PeerClose(event.msg->error, event.msg->subcode);
1207  set_last_notification_in(event.msg->error, event.msg->subcode,
1208  event.Name());
1209 }
1210 
1212  int backoff = min(attempts_, 6);
1213  return std::min(backoff ? 1 << (backoff - 1) : 0, kConnectInterval);
1214 }
1215 
1218 
1219  // Add up to +/- kJitter percentage to reduce connection collisions.
1220  int ms = seconds ? seconds * 1000 : 50;
1221  ms = (ms * (100 - kJitter)) / 100;
1222  ms += (ms * (rand_r(&seed_) % (kJitter * 2))) / 100;
1223  connect_timer_->Start(ms,
1224  boost::bind(&StateMachine::ConnectTimerExpired, this),
1225  boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
1226 }
1227 
1230 }
1231 
1233  return connect_timer_->running();
1234 }
1235 
1236 void StateMachine::StartOpenTimer(int seconds) {
1237  open_timer_->Cancel();
1238  open_timer_->Start(seconds * 1000,
1239  boost::bind(&StateMachine::OpenTimerExpired, this),
1240  boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
1241 }
1242 
1244  open_timer_->Cancel();
1245 }
1246 
1248  return open_timer_->running();
1249 }
1250 
1252  if (idle_hold_time_ <= 0)
1253  return;
1254 
1257  boost::bind(&StateMachine::IdleHoldTimerExpired, this),
1258  boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
1259 }
1260 
1263 }
1264 
1266  return idle_hold_timer_->running();
1267 }
1268 
1270  if (hold_time_ <= 0)
1271  return;
1272 
1273  hold_timer_->Cancel();
1274  hold_timer_->Start(hold_time_ * 1000,
1275  boost::bind(&StateMachine::HoldTimerExpired, this),
1276  boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
1277 }
1278 
1280  hold_timer_->Cancel();
1281 }
1282 
1284  return hold_timer_->running();
1285 }
1286 
1287 // Test Only APIs : Start
1288 
1290  connect_timer_->Fire();
1291 }
1292 
1294  open_timer_->Fire();
1295 }
1296 
1298  hold_timer_->Fire();
1299 }
1300 
1303 }
1304 
1305 // Test Only APIs : END
1306 
1307 //
1308 // Create an active session.
1309 //
1311  BgpSession *session = peer_->CreateSession();
1312  if (!session)
1313  return;
1314  set_active_session(session);
1315  session->set_observer(
1316  boost::bind(&StateMachine::OnSessionEvent, this, _1, _2));
1317  peer_->server()->session_manager()->Connect(session,
1318  peer_->peer_key().endpoint);
1319 }
1320 
1321 //
1322 // Post a pseudo event to delete the underlying TcpSession.
1323 //
1324 // This ensures that any references to the TcpSession from pending events on
1325 // the state machine queue are still valid. Since we remove the TCP observer
1326 // before posting the delete event, we are guaranteed that we won't receive
1327 // any more events on the TcpSession.
1328 //
1330  if (!session)
1331  return;
1332  session->set_observer(NULL);
1333  session->Close();
1334  Enqueue(fsm::EvTcpDeleteSession(session));
1335 }
1336 
1337 //
1338 // Transfer the ownership of the session from state machine to the peer.
1339 // This is called after we have resolved any connection collision issues
1340 // and decided that we want to reach ESTABLISHED state via the session.
1341 //
1342 void StateMachine::AssignSession(bool active) {
1343  if (active) {
1345  active_session_ = NULL;
1346  } else {
1348  passive_session_ = NULL;
1349  }
1350 }
1351 
1354  active_session_ = session;
1355 }
1356 
1359  passive_session_ = session;
1360 }
1361 
1363  return active_session_;
1364 }
1365 
1367  return passive_session_;
1368 }
1369 
1370 void StateMachine::SendNotification(BgpSession *session, int code, int subcode,
1371  const std::string &data) {
1372  // Prefer the passive session if available since it's operational.
1373  if (!session)
1374  session = passive_session_;
1375  if (!session)
1376  session = active_session_;
1377  if (session && code != 0)
1378  peer_->SendNotification(session, code, subcode, data);
1379 }
1380 
1381 //
1382 // Process notification message.
1383 //
1384 // Typically we close the session. However, during connection collisions, we
1385 // could receive notifications on sessions that are not currently assigned to
1386 // the peer. In such cases, we discard the event and let the state machine
1387 // continue in the other session which is currently assigned to the peer.
1388 //
1390  // If this is a notification event that does not belong to the session,
1391  // ignore. If either session is not present, continue normal processing
1392  // of the notification.
1393  if (session && peer_->session() && peer_->session() != session) {
1394  return false;
1395  }
1396 
1397  // TransitToIdle<EvBgpNotification>::reaction,
1398  if (active_session() == session) {
1399  set_active_session(NULL);
1400  } else {
1401  set_passive_session(NULL);
1402  }
1403 
1404  return true;
1405 }
1406 
1409  return false;
1410 }
1411 
1414  return false;
1415 }
1416 
1418  boost::system::error_code error;
1419 
1420  // Reset hold timer if there is data already present in the socket.
1421  if (peer() && peer()->session() && peer()->session()->socket() &&
1422  peer()->session()->socket()->available(error) > 0) {
1423  return true;
1424  }
1427  return false;
1428 }
1429 
1432  return false;
1433 }
1434 
1436  return peer_->IsCloseGraceful();
1437 }
1438 
1440  return peer_->IsRouterTypeBGPaaS();
1441 }
1442 
1443 //
1444 // Concurrency: ASIO thread.
1445 // Feed TCP session events into the state machine.
1446 //
1448  TcpSession *session, TcpSession::Event event) {
1449  BgpSession *bgp_session = static_cast<BgpSession *>(session);
1450  switch (event) {
1452  Enqueue(fsm::EvTcpConnected(bgp_session));
1453  break;
1455  Enqueue(fsm::EvTcpConnectFail(bgp_session));
1457  break;
1458  case TcpSession::CLOSE:
1459  Enqueue(fsm::EvTcpClose(bgp_session));
1460  break;
1461  default:
1462  break;
1463  }
1464 }
1465 
1466 //
1467 // Receive TCP Passive Open.
1468 // Set the observer and start async read on the session. Note that we disable
1469 // read on connect when we accept passive sessions.
1470 //
1472  CHECK_CONCURRENCY("bgp::Config");
1473  Enqueue(fsm::EvTcpPassiveOpen(session));
1474  session->set_observer(boost::bind(&StateMachine::OnSessionEvent,
1475  this, _1, _2));
1476  session->AsyncReadStart();
1477  return true;
1478 }
1479 
1481  BgpProto::BgpMessage *msg) {
1482  BgpPeer *peer = session->peer();
1483  if (peer)
1486  static_cast<BgpProto::Notification *>(msg)));
1487 }
1488 
1489 //
1490 // Handle incoming message on the session.
1491 //
1493  size_t msgsize) {
1494  switch (msg->type) {
1495  case BgpProto::OPEN: {
1496  BgpProto::OpenMessage *open_msg =
1497  static_cast<BgpProto::OpenMessage *>(msg);
1498  BgpPeer *peer = session->peer();
1499  peer->inc_rx_open();
1500  if (int subcode = open_msg->Validate(peer)) {
1501  Enqueue(fsm::EvBgpOpenError(session, subcode));
1502  peer->inc_open_error();
1503  } else {
1504  Enqueue(fsm::EvBgpOpen(session, open_msg));
1505  msg = NULL;
1506  }
1507  break;
1508  }
1509  case BgpProto::KEEPALIVE: {
1510  BgpPeer *peer = session->peer();
1511  Enqueue(fsm::EvBgpKeepalive(session));
1512  if (peer) peer->inc_rx_keepalive();
1513  break;
1514  }
1515  case BgpProto::NOTIFICATION: {
1516  OnNotificationMessage(session, msg);
1517  msg = NULL;
1518  break;
1519  }
1520  case BgpProto::UPDATE: {
1521  BgpProto::Update *update = static_cast<BgpProto::Update *>(msg);
1522  BgpPeer *peer = NULL;
1523  if (session)
1524  peer = session->peer();
1525  if (peer)
1526  peer->inc_rx_update();
1527 
1528  std::string data;
1529  int subcode;
1530  if (peer && (subcode = update->Validate(peer, &data))) {
1531  Enqueue(fsm::EvBgpUpdateError(session, subcode, data));
1533  } else {
1534  Enqueue(fsm::EvBgpUpdate(session, update, msgsize));
1535  msg = NULL;
1536  }
1537  break;
1538  }
1539  default:
1540  SM_LOG_NOTICE("Unknown message type " << msg->type);
1541  break;
1542  }
1543 
1544  delete msg;
1545 }
1546 
1547 //
1548 // Handle errors in incoming message on the session.
1549 //
1551  const ParseErrorContext *context) {
1552  switch (context->error_code) {
1554  Enqueue(fsm::EvBgpHeaderError(session, context->error_subcode,
1555  context->data, context->data_size));
1556  break;
1557  }
1559  Enqueue(fsm::EvBgpOpenError(session, context->error_subcode,
1560  context->data, context->data_size));
1561  break;
1563  Enqueue(fsm::EvBgpUpdateError(session, context->error_subcode,
1564  std::string((const char *)context->data, context->data_size)));
1565  break;
1566  default:
1567  break;
1568  }
1569 }
1570 
1571 static const std::string state_names[] = {
1572  "Idle",
1573  "Active",
1574  "Connect",
1575  "OpenSent",
1576  "OpenConfirm",
1577  "Established"
1578 };
1579 
1580 const string &StateMachine::StateName() const {
1581  return state_names[state_];
1582 }
1583 
1584 const string &StateMachine::LastStateName() const {
1585  return state_names[last_state_];
1586 }
1587 
1588 const std::string StateMachine::last_state_change_at() const {
1590 }
1591 
1593  return last_state_change_at_;
1594 }
1595 
1596 ostream &operator<<(ostream &out, const StateMachine::State &state) {
1597  out << state_names[state];
1598  return out;
1599 }
1600 
1601 // This class determines whether a given class has a method called 'validate'.
1602 template <typename Ev>
1603 struct HasValidate {
1604  template <typename T, bool (T::*)(StateMachine *) const> struct SFINAE {};
1605  template <typename T> static char Test(SFINAE<T, &T::validate>*);
1606  template <typename T> static int Test(...);
1607  static const bool Has = sizeof(Test<Ev>(0)) == sizeof(char);
1608 };
1609 
1610 template <typename Ev, bool has_validate>
1611 struct ValidateFn {
1612  EvValidate operator()(const Ev *event) {
1613  return NULL;
1614  }
1615 };
1616 
1617 template <typename Ev>
1618 struct ValidateFn<Ev, true> {
1619  EvValidate operator()(const Ev *event) {
1620  return boost::bind(&Ev::validate, event, _1);
1621  }
1622 };
1623 
1624 template <typename Ev>
1625 bool StateMachine::Enqueue(const Ev &event) {
1626  LogEvent(TYPE_NAME(event), "Enqueue");
1627  EventContainer ec;
1628  ec.event = event.intrusive_from_this();
1630  static_cast<const Ev *>(ec.event.get()));
1631  work_queue_.Enqueue(ec);
1632 
1633  return true;
1634 }
1635 
1636 void StateMachine::LogEvent(string event_name, string msg,
1637  SandeshLevel::type log_level) {
1638  // Reduce log level for keepalive and update messages.
1639  if (get_state() == ESTABLISHED &&
1640  (event_name == "fsm::EvBgpKeepalive" ||
1641  event_name == "fsm::EvBgpUpdate")) {
1642  log_level = Sandesh::LoggingUtLevel();
1643  }
1644  SM_LOG(log_level, msg << " " << event_name << " in state " << StateName());
1645 }
1646 
1648  const fsm::EvTcpDeleteSession *deferred_delete =
1649  dynamic_cast<const fsm::EvTcpDeleteSession *>(ec.event.get());
1650  if (deferred_delete != NULL) {
1651  LogEvent(TYPE_NAME(*ec.event), "Dequeue");
1653  deferred_delete->session);
1654  return true;
1655  }
1656 
1658  if (ec.validate.empty() || ec.validate(this)) {
1659  LogEvent(TYPE_NAME(*ec.event), "Dequeue");
1660  process_event(*ec.event);
1661  } else {
1662  LogEvent(TYPE_NAME(*ec.event), "Discard", SandeshLevel::SYS_INFO);
1663  }
1664  ec.event.reset();
1665 
1666  return true;
1667 }
1668 
1670  peer_->RetryDelete();
1671 }
1672 
1673 void StateMachine::SetDataCollectionKey(BgpPeerInfo *peer_info) const {
1674  peer_->SetDataCollectionKey(peer_info);
1675 }
1676 
1677 const std::string StateMachine::last_notification_in_error() const {
1680  last_notification_in_.second));
1681 }
1682 
1686  last_notification_out_.second));
1687 }
1688 
1689 //
1690 // Return the configured hold time in seconds.
1691 //
1693  static std::atomic<bool> env_checked = std::atomic<bool>();
1694  static std::atomic<int> env_hold_time = std::atomic<int>();
1695 
1696  // For testing only - configure through environment variable.
1697  if (!env_checked) {
1698  char *keepalive_time_str = getenv("BGP_KEEPALIVE_SECONDS");
1699  if (keepalive_time_str) {
1700  env_hold_time = strtoul(keepalive_time_str, NULL, 0) * 3;
1701  env_checked = true;
1702  return env_hold_time;
1703  } else {
1704  env_checked = true;
1705  }
1706  } else if (env_hold_time) {
1707  return env_hold_time;
1708  }
1709 
1710  // Use the configured hold-time from peer if available.
1711  if (peer_ && peer_->hold_time())
1712  return peer_->hold_time();
1713 
1714  // Use the configured hold-time from server if available.
1715  if (peer_ && peer_->server()->hold_time())
1716  return peer_->server()->hold_time();
1717 
1718  // Use hard coded default.
1719  return kHoldTime;
1720 }
1721 
1722 void StateMachine::BGPPeerInfoSend(const BgpPeerInfoData &peer_info) {
1723  assert(!peer_info.get_name().empty());
1724  BGP_UVE_SEND(BGPPeerInfo, peer_info);
1725 }
1726 
1727 void StateMachine::set_last_event(const std::string &event) {
1728  last_event_ = event;
1730 
1731  // Skip keepalive and update events after we've reached established state.
1732  if (state_ == ESTABLISHED &&
1733  (event == "fsm::EvBgpKeepalive" || event == "fsm::EvBgpUpdate")) {
1734  return;
1735  }
1736 
1737  BgpPeerInfoData peer_info;
1738  peer_info.set_name(peer()->ToUVEKey());
1739  PeerEventInfo event_info;
1740  event_info.set_last_event(last_event_);
1741  event_info.set_last_event_at(last_event_at_);
1742  peer_info.set_event_info(event_info);
1743  BGPPeerInfoSend(peer_info);
1744 }
1745 
1746 void StateMachine::set_last_notification_out(int code, int subcode,
1747  const string &reason) {
1748  last_notification_out_ = std::make_pair(code, subcode);
1751 
1752  BgpPeerInfoData peer_info;
1753  peer_info.set_name(peer()->ToUVEKey());
1754  peer_info.set_notification_out_at(last_notification_out_at_);
1755  peer_info.set_notification_out(BgpProto::Notification::toString(
1756  static_cast<BgpProto::Notification::Code>(code), subcode));
1757  BGPPeerInfoSend(peer_info);
1758 }
1759 
1760 void StateMachine::set_last_notification_in(int code, int subcode,
1761  const string &reason) {
1762  last_notification_in_ = std::make_pair(code, subcode);
1764  last_notification_in_error_ = reason;
1765 
1766  BgpPeerInfoData peer_info;
1767  peer_info.set_name(peer()->ToUVEKey());
1768  peer_info.set_notification_in_at(last_notification_in_at_);
1769  peer_info.set_notification_in(BgpProto::Notification::toString(
1770  static_cast<BgpProto::Notification::Code>(code), subcode));
1771  BGPPeerInfoSend(peer_info);
1772 }
1773 
1775  if (state == state_)
1776  return;
1777  last_state_ = state_; state_ = state;
1779 
1780  BgpPeerInfoData peer_info;
1781  peer_info.set_name(peer()->ToUVEKey());
1782  PeerStateInfo state_info;
1783  state_info.set_state(StateName());
1784  state_info.set_last_state(LastStateName());
1785  state_info.set_last_state_at(last_state_change_at_);
1786  peer_info.set_state_info(state_info);
1787  BGPPeerInfoSend(peer_info);
1788 }
1789 
1790 void StateMachine::set_hold_time(int hold_time) {
1792 
1793  BgpPeerInfoData peer_info;
1794  peer_info.set_name(peer()->ToUVEKey());
1795  peer_info.set_hold_time(hold_time_);
1796  BGPPeerInfoSend(peer_info);
1797 }
1798 
1801 
1802  BgpPeerInfoData peer_info;
1803  peer_info.set_name(peer()->ToUVEKey());
1804  peer_info.set_hold_time(hold_time_);
1805  BGPPeerInfoSend(peer_info);
1806 }
1807 
1809  last_notification_in_ = std::make_pair(0, 0);
1811  last_notification_in_error_ = std::string();
1812  last_notification_out_ = std::make_pair(0, 0);
1814  last_notification_out_error_ = std::string();
1815  last_state_ = IDLE;
1816  last_event_ = "";
1818  last_event_at_ = 0;
1819 
1820  BgpPeerInfoData peer_info;
1821  peer_info.set_name(peer()->ToUVEKey());
1822  PeerStateInfo state_info;
1823  state_info.set_state(StateName());
1824  state_info.set_last_state(LastStateName());
1825  state_info.set_last_state_at(last_state_change_at_);
1826  peer_info.set_state_info(state_info);
1827 
1828  PeerEventInfo event_info;
1829  event_info.set_last_event(last_event_);
1830  event_info.set_last_event_at(last_event_at_);
1831  peer_info.set_event_info(event_info);
1832  BGPPeerInfoSend(peer_info);
1833 }
#define BGP_LOG_FLAG_SYSLOG
Definition: bgp_log.h:43
#define BGP_LOG_PEER(type, peer, level, flags, dir, arg)
Definition: bgp_log.h:160
#define BGP_PEER_DIR_IN
Definition: bgp_log.h:140
#define BGP_UVE_SEND(type, object)
Definition: bgp_log.h:47
void clear_session()
Definition: bgp_peer.cc:2325
const BgpPeerKey & peer_key() const
Definition: bgp_peer.h:166
StateMachine * state_machine()
Definition: bgp_peer.h:328
virtual const std::string & ToUVEKey() const
Definition: bgp_peer.h:103
uint16_t hold_time() const
Definition: bgp_peer.h:184
bool IsRouterTypeBGPaaS() const
Definition: bgp_peer.h:360
void Close(bool graceful)
Definition: bgp_peer.cc:1269
void inc_rx_update()
Definition: bgp_peer.cc:2681
bool IsCloseInProgress() const
Definition: bgp_peer.cc:1322
void inc_update_error()
Definition: bgp_peer.cc:2777
void increment_flap_count()
Definition: bgp_peer.cc:2821
void reset_flap_count()
Definition: bgp_peer.cc:2841
bool IsDeleted() const
Definition: bgp_peer.cc:1314
virtual BgpServer * server()
Definition: bgp_peer.h:159
virtual void SetDataCollectionKey(BgpPeerInfo *peer_info) const
Definition: bgp_peer.cc:2481
void inc_connect_timer_expired()
Definition: bgp_peer.cc:2765
void inc_connect_error()
Definition: bgp_peer.cc:2761
void SendKeepalive(bool from_timer)
Definition: bgp_peer.cc:1578
BgpSession * session()
Definition: bgp_peer.cc:2335
virtual uint32_t bgp_identifier() const
Definition: bgp_peer.cc:1223
void SendNotification(BgpSession *, int code, int subcode=0, const std::string &data=std::string())
Definition: bgp_peer.cc:1700
void set_session(BgpSession *session)
Definition: bgp_peer.cc:2319
void SendOpen(TcpSession *session)
Definition: bgp_peer.cc:1502
void RegisterAllTables()
Definition: bgp_peer.cc:1432
void inc_hold_timer_expired()
Definition: bgp_peer.cc:2769
bool SetCapabilities(const BgpProto::OpenMessage *msg)
Definition: bgp_peer.cc:1708
void StartKeepaliveTimer()
Definition: bgp_peer.cc:2295
void inc_rx_notification()
Definition: bgp_peer.cc:2697
BgpSession * CreateSession()
Definition: bgp_peer.cc:1340
void RetryDelete()
Definition: bgp_peer.cc:2471
void ProcessUpdate(const BgpProto::Update *msg, size_t msgsize=0)
Definition: bgp_peer.cc:1951
void inc_rx_keepalive()
Definition: bgp_peer.cc:2665
void inc_rx_open()
Definition: bgp_peer.cc:2657
virtual IPeerDebugStats * peer_stats()
Definition: bgp_peer.cc:1298
void inc_open_error()
Definition: bgp_peer.cc:2773
bool IsCloseGraceful() const
Definition: bgp_peer.cc:324
void NotifyEstablished(bool established)
Definition: bgp_peer.cc:798
bool AttemptGRHelperMode(int code, int subcode) const
Definition: bgp_peer.cc:1685
bool IsPassive() const
Definition: bgp_peer.h:224
@ NOTIFICATION
Definition: bgp_proto.h:25
@ UPDATE
Definition: bgp_proto.h:24
@ KEEPALIVE
Definition: bgp_proto.h:26
uint32_t bgp_identifier() const
Definition: bgp_server.h:216
BgpSessionManager * session_manager()
Definition: bgp_server.h:102
uint32_t hold_time() const
Definition: bgp_server.h:223
BgpPeer * peer()
Definition: bgp_session.h:54
void LogNotification(int code, int subcode, const std::string &direction, const std::string &peer_key, const BgpProto::Notification &msg) const
Definition: bgp_session.cc:98
virtual void Clear()=0
static SandeshLevel::type LoggingUtLevel()
Definition: cpp/sandesh.h:225
void CancelIdleHoldTimer()
BgpSession * active_session()
void connect_attempts_inc()
void DeleteAllTimers()
bool HoldTimerExpired()
void reset_last_info()
void connect_attempts_clear()
const std::string last_notification_in_error() const
BgpSession * active_session_
void OnIdleError(const Ev &event)
bool IdleHoldTimerExpired()
static const int kOpenSentHoldTime
Definition: state_machine.h:83
std::string last_event_
void CancelHoldTimer()
bool OpenTimerRunning()
virtual bool IsCloseGraceful() const
void OnMessage(BgpSession *session, BgpProto::BgpMessage *msg, size_t msgsize=0)
uint64_t last_notification_in_at_
void set_last_notification_out(int code, int subcode, const std::string &reason)
std::pair< int, int > last_notification_in_
virtual const int GetIdleHoldTimeMSecs() const
bool DequeueEvent(EventContainer ec)
virtual void OnNotificationMessage(BgpSession *session, BgpProto::BgpMessage *msg)
void set_state(State state)
uint64_t last_state_change_at_
virtual void DeleteSession(BgpSession *session)
bool ProcessNotificationEvent(BgpSession *session)
const std::string last_state_change_at() const
std::string last_notification_out_error_
void reset_hold_time()
void OnMessageError(BgpSession *session, const ParseErrorContext *context)
static const int kConnectInterval
Definition: state_machine.h:81
void SendNotification(BgpSession *session, int code, int subcode=0, const std::string &data=std::string())
bool ConnectTimerExpired()
BgpSession * passive_session_
int GetConfiguredHoldTime() const
bool HoldTimerRunning()
void FireHoldTimer()
std::string last_notification_in_error_
StateMachine(BgpPeer *peer)
virtual void OnSessionEvent(TcpSession *session, TcpSession::Event event)
void CancelConnectTimer()
WorkQueue< EventContainer > work_queue_
int hold_time() const
void OnIdleCease(const Ev &event)
BgpSession * passive_session()
static const int kIdleHoldTime
Definition: state_machine.h:84
static const int kHoldTime
Definition: state_machine.h:82
void SetAdminState(bool down, int subcode)
void FireIdleHoldTimer()
void SetDataCollectionKey(BgpPeerInfo *peer_info) const
void set_hold_time(int hold_time)
uint64_t last_event_at_
BgpPeer * peer()
bool IsQueueEmpty() const
void set_idle_hold_time(int idle_hold_time)
const std::string last_notification_out_error() const
void FireOpenTimer()
void AssignSession(bool active)
bool Enqueue(const Ev &event)
void set_passive_session(BgpSession *session)
const uint64_t last_state_change_usecs_at() const
const std::string & StateName() const
void PeerClose(int code, int subcode)
virtual ~StateMachine()
void set_last_event(const std::string &event)
Timer * open_timer_
uint64_t last_notification_out_at_
bool ConnectTimerRunning()
virtual void StartOpenTimer(int seconds)
Timer * connect_timer_
void FireConnectTimer()
void Shutdown(int subcode)
bool IdleHoldTimerRunning()
void LogEvent(std::string event_name, std::string msg, SandeshLevel::type log_level=SandeshLevel::SYS_DEBUG)
State get_state() const
void DequeueEventDone(bool done)
int GetConnectTime() const
virtual bool IsPeerCloseInProgress() const
virtual bool IsRouterTypeBGPaaS() const
void BGPPeerInfoSend(const BgpPeerInfoData &peer_info)
bool OpenTimerExpired()
virtual void StartIdleHoldTimer()
void CancelOpenTimer()
void set_active_session(BgpSession *session)
bool PassiveOpen(BgpSession *session)
BgpPeer * peer_
void OnIdle(const Ev &event)
unsigned int seed_
Timer * idle_hold_timer_
static const int kOpenTime
Definition: state_machine.h:80
void OnIdleNotification(const fsm::EvBgpNotification &event)
int idle_hold_time() const
const std::string & LastStateName() const
void reset_idle_hold_time()
void TimerErrorHanlder(std::string name, std::string error)
static const int kJitter
Definition: state_machine.h:86
virtual void StartConnectTimer(int seconds)
virtual void StartHoldTimer()
void set_last_notification_in(int code, int subcode, const std::string &reason)
Timer * hold_timer_
std::pair< int, int > last_notification_out_
void UpdateFlapCount()
static const int kMaxIdleHoldTime
Definition: state_machine.h:85
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
virtual void Connect(TcpSession *session, Endpoint remote)
Definition: tcp_server.cc:477
virtual void DeleteSession(TcpSession *session)
Definition: tcp_server.cc:200
@ CONNECT_COMPLETE
Definition: tcp_session.h:47
@ CONNECT_FAILED
Definition: tcp_session.h:48
bool IsClosed() const
Definition: tcp_session.h:122
virtual std::string ToString() const
Definition: tcp_session.h:80
void set_observer(EventObserver observer)
Definition: tcp_session.cc:220
bool IsEstablished() const
Definition: tcp_session.h:117
virtual void AsyncReadStart()
Definition: tcp_session.cc:176
void Close()
Definition: tcp_session.cc:356
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
Definition: timer.h:58
bool Cancel()
Definition: timer.cc:150
bool running() const
Definition: timer.h:90
bool cancelled() const
Definition: timer.h:104
void Fire()
Definition: timer.h:122
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
void SetExitCallback(TaskExitCallback on_exit)
Definition: queue_task.h:304
bool IsQueueEmpty() const
Definition: queue_task.h:353
uint8_t type
Definition: load_balance.h:2
#define TYPE_NAME(_type)
Definition: logging.h:33
#define SM_LOG(level, _Msg)
ostream & operator<<(ostream &out, const StateMachine::State &state)
#define SM_LOG_NOTICE(_Msg)
static const std::string state_names[]
boost::function< bool(StateMachine *)> EvValidate
Definition: state_machine.h:34
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
boost::asio::ip::tcp::endpoint endpoint
Definition: bgp_peer_key.h:25
MessageType type
Definition: bgp_proto.h:54
static const std::string toString(Code code, int subcode)
Definition: bgp_proto.cc:348
int Validate(BgpPeer *peer) const
Definition: bgp_proto.cc:107
int Validate(const BgpPeer *, std::string *data)
Definition: bgp_proto.cc:408
static const bool Has
static char Test(SFINAE< T, &T::validate > *)
static int Test(...)
const uint8_t * data
Definition: parse_object.h:90
boost::intrusive_ptr< const sc::event_base > event
EvValidate operator()(const Ev *event)
EvValidate operator()(const Ev *event)
sc::result react(const EvOpenTimerExpired &event)
sc::result react(const EvTcpPassiveOpen &event)
sc::result react(const EvConnectTimerExpired &event)
mpl::list< IdleCease< EvStop >::reaction, sc::custom_reaction< EvConnectTimerExpired >, sc::custom_reaction< EvOpenTimerExpired >, sc::custom_reaction< EvTcpPassiveOpen >, sc::custom_reaction< EvTcpClose >, sc::custom_reaction< EvBgpOpen >, TransitToIdle< EvBgpNotification >::reaction, IdleFsmError< EvBgpKeepalive >::reaction, IdleFsmError< EvBgpUpdate >::reaction, IdleError< EvBgpHeaderError, BgpProto::Notification::MsgHdrErr >::reaction, IdleError< EvBgpOpenError, BgpProto::Notification::OpenMsgErr >::reaction, IdleError< EvBgpUpdateError, BgpProto::Notification::UpdateMsgErr >::reaction > reactions
sc::result react(const EvTcpClose &event)
Active(my_context ctx)
sc::result react(const EvBgpOpen &event)
mpl::list< IdleCease< EvStop >::reaction, sc::custom_reaction< EvConnectTimerExpired >, sc::custom_reaction< EvOpenTimerExpired >, sc::custom_reaction< EvTcpConnected >, sc::custom_reaction< EvTcpConnectFail >, sc::custom_reaction< EvTcpPassiveOpen >, sc::custom_reaction< EvTcpClose >, sc::custom_reaction< EvBgpOpen >, TransitToIdle< EvBgpNotification >::reaction, IdleFsmError< EvBgpKeepalive >::reaction, IdleFsmError< EvBgpUpdate >::reaction, IdleError< EvBgpHeaderError, BgpProto::Notification::MsgHdrErr >::reaction, IdleError< EvBgpOpenError, BgpProto::Notification::OpenMsgErr >::reaction, IdleError< EvBgpUpdateError, BgpProto::Notification::UpdateMsgErr >::reaction > reactions
sc::result react(const EvTcpConnected &event)
sc::result react(const EvBgpOpen &event)
sc::result react(const EvTcpPassiveOpen &event)
Connect(my_context ctx)
sc::result react(const EvConnectTimerExpired &event)
sc::result react(const EvTcpConnectFail &event)
sc::result react(const EvOpenTimerExpired &event)
sc::result react(const EvTcpClose &event)
sc::result react(const EvTcpPassiveOpen &event)
sc::result react(const EvBgpKeepalive &event)
sc::result react(const EvBgpUpdate &event)
mpl::list< IdleCease< EvStop >::reaction, IdleFsmError< EvOpenTimerExpired >::reaction, TransitToIdle< EvHoldTimerExpired, BgpProto::Notification::HoldTimerExp >::reaction, sc::custom_reaction< EvTcpPassiveOpen >, TransitToIdle< EvTcpClose >::reaction, IdleFsmError< EvBgpOpen >::reaction, TransitToIdle< EvBgpNotification >::reaction, sc::custom_reaction< EvBgpKeepalive >, sc::custom_reaction< EvBgpUpdate >, IdleError< EvBgpHeaderError, BgpProto::Notification::MsgHdrErr >::reaction, IdleFsmError< EvBgpOpenError >::reaction, IdleError< EvBgpUpdateError, BgpProto::Notification::UpdateMsgErr >::reaction > reactions
Established(my_context ctx)
EvBgpHeaderError(BgpSession *session, int subcode, const uint8_t *_data, size_t data_size)
static const char * Name()
static const char * Name()
bool validate(StateMachine *state_machine) const
BgpSession * session
EvBgpKeepalive(BgpSession *session)
boost::shared_ptr< const BgpProto::Notification > msg
EvBgpNotification(BgpSession *session, const BgpProto::Notification *msg)
bool validate(StateMachine *state_machine) const
static const char * Name()
static const char * Name()
BgpSession * session
EvBgpOpenError(BgpSession *session, int subcode, const uint8_t *_data=NULL, size_t data_size=0)
bool validate(StateMachine *state_machine) const
static const char * Name()
boost::shared_ptr< const BgpProto::OpenMessage > msg
BgpSession * session
EvBgpOpen(BgpSession *session, const BgpProto::OpenMessage *msg)
EvBgpUpdateError(BgpSession *session, int subcode, std::string data)
static const char * Name()
BgpSession * session
EvBgpUpdate(BgpSession *session, const BgpProto::Update *msg, size_t msgsize)
static const char * Name()
boost::shared_ptr< const BgpProto::Update > msg
bool validate(StateMachine *state_machine) const
static const char * Name()
EvConnectTimerExpired(Timer *timer)
bool validate(StateMachine *state_machine) const
static const char * Name()
EvHoldTimerExpired(Timer *timer)
bool validate(StateMachine *state_machine) const
static const char * Name()
EvIdleHoldTimerExpired(Timer *timer)
EvOpenTimerExpired(Timer *timer)
bool validate(StateMachine *state_machine) const
static const char * Name()
static const char * Name()
static const char * Name()
EvStop(int subcode)
BgpSession * session
bool validate(StateMachine *state_machine) const
static const char * Name()
EvTcpClose(BgpSession *session)
EvTcpConnectFail(BgpSession *session)
static const char * Name()
bool validate(StateMachine *state_machine) const
bool validate(StateMachine *state_machine) const
EvTcpConnected(BgpSession *session)
static const char * Name()
BgpSession * session
static const char * Name()
EvTcpDeleteSession(BgpSession *session)
EvTcpPassiveOpen(BgpSession *session)
static const char * Name()
sc::transition< Ev, Idle, StateMachine, &StateMachine::OnIdleCease< Ev > > reaction
sc::transition< Ev, Idle, StateMachine, &StateMachine::OnIdleError< Ev, code > > reaction
sc::transition< Ev, Idle, StateMachine, &StateMachine::OnIdle< Ev, BgpProto::Notification::FSMErr > > reaction
sc::result react(const EvIdleHoldTimerExpired &event)
Idle(my_context ctx)
mpl::list< IdleCease< EvStop >::reaction, sc::custom_reaction< EvStart >, sc::custom_reaction< EvIdleHoldTimerExpired >, sc::custom_reaction< EvTcpPassiveOpen > > reactions
sc::result react(const EvTcpPassiveOpen &event)
sc::result react(const EvStart &event)
mpl::list< IdleCease< EvStop >::reaction, IdleFsmError< EvOpenTimerExpired >::reaction, TransitToIdle< EvHoldTimerExpired, BgpProto::Notification::HoldTimerExp >::reaction, sc::custom_reaction< EvTcpPassiveOpen >, TransitToIdle< EvTcpClose >::reaction, IdleFsmError< EvBgpOpen >::reaction, sc::custom_reaction< EvBgpNotification >, sc::custom_reaction< EvBgpKeepalive >, IdleFsmError< EvBgpUpdate >::reaction, IdleError< EvBgpHeaderError, BgpProto::Notification::MsgHdrErr >::reaction, IdleError< EvBgpOpenError, BgpProto::Notification::OpenMsgErr >::reaction, IdleError< EvBgpUpdateError, BgpProto::Notification::UpdateMsgErr >::reaction > reactions
OpenConfirm(my_context ctx)
sc::result react(const EvBgpKeepalive &event)
sc::result react(const EvTcpPassiveOpen &event)
sc::result react(const EvBgpNotification &event)
sc::result react(const EvBgpNotification &event)
sc::result react(const EvTcpPassiveOpen &event)
OpenSent(my_context ctx)
mpl::list< IdleCease< EvStop >::reaction, sc::custom_reaction< EvOpenTimerExpired >, TransitToIdle< EvHoldTimerExpired, BgpProto::Notification::HoldTimerExp >::reaction, sc::custom_reaction< EvTcpPassiveOpen >, sc::custom_reaction< EvTcpClose >, sc::custom_reaction< EvBgpOpen >, sc::custom_reaction< EvBgpNotification >, IdleFsmError< EvBgpKeepalive >::reaction, IdleFsmError< EvBgpUpdate >::reaction, IdleError< EvBgpHeaderError, BgpProto::Notification::MsgHdrErr >::reaction, IdleError< EvBgpOpenError, BgpProto::Notification::OpenMsgErr >::reaction, IdleError< EvBgpUpdateError, BgpProto::Notification::UpdateMsgErr >::reaction > reactions
sc::result react(const EvBgpOpen &event)
sc::result react(const EvTcpClose &event)
sc::result react(const EvOpenTimerExpired &event)
sc::transition< EvBgpNotification, Idle, StateMachine, &StateMachine::OnIdleNotification > reaction
sc::transition< Ev, Idle, StateMachine, &StateMachine::OnIdle< Ev, code > > reaction
#define CHECK_CONCURRENCY(...)
static boost::posix_time::ptime UTCUsecToPTime(uint64_t tusec)
Definition: time_util.h:39
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14