LCOV - code coverage report
Current view: top level - bgp - state_machine.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 595 842 70.7 %
Date: 2026-11-23 02:04:52 Functions: 138 191 72.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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"
      26             : #include "bgp/bgp_session_manager.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
      40             : const int StateMachine::kIdleHoldTime =
      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         838 :     EvStart() {
      77         838 :     }
      78             :     static const char *Name() {
      79             :         return "EvStart";
      80             :     }
      81             : };
      82             : 
      83             : struct EvStop : sc::event<EvStop> {
      84         302 :     explicit EvStop(int subcode) : subcode(subcode) {
      85         302 :     }
      86             :     static const char *Name() {
      87             :         return "EvStop";
      88             :     }
      89             :     int subcode;
      90             : };
      91             : 
      92             : struct EvIdleHoldTimerExpired : sc::event<EvIdleHoldTimerExpired> {
      93         664 :     explicit EvIdleHoldTimerExpired(Timer *timer)  : timer_(timer) {
      94         664 :     }
      95             :     static const char *Name() {
      96             :         return "EvIdleHoldTimerExpired";
      97             :     }
      98         664 :     bool validate(StateMachine *state_machine) const {
      99         664 :         return !timer_->cancelled();
     100             :     }
     101             : 
     102             :     Timer *timer_;
     103             : };
     104             : 
     105             : struct EvConnectTimerExpired : sc::event<EvConnectTimerExpired> {
     106         810 :     explicit EvConnectTimerExpired(Timer *timer) : timer_(timer) {
     107         810 :     }
     108             :     static const char *Name() {
     109             :         return "EvConnectTimerExpired";
     110             :     }
     111         810 :     bool validate(StateMachine *state_machine) const {
     112         810 :         if (timer_->cancelled()) {
     113           4 :             return false;
     114         806 :         } else if (state_machine->get_state() == StateMachine::ACTIVE) {
     115         806 :             return (state_machine->passive_session() == NULL);
     116           0 :         } else if (state_machine->get_state() == StateMachine::CONNECT) {
     117           0 :             return (!state_machine->active_session() ||
     118           0 :                     !state_machine->active_session()->IsEstablished());
     119             :         }
     120           0 :         return false;
     121             :     }
     122             : 
     123             :     Timer *timer_;
     124             : };
     125             : 
     126             : struct EvOpenTimerExpired : sc::event<EvOpenTimerExpired> {
     127           0 :     explicit EvOpenTimerExpired(Timer *timer) : timer_(timer) {
     128           0 :     }
     129             :     static const char *Name() {
     130             :         return "EvOpenTimerExpired";
     131             :     }
     132           0 :     bool validate(StateMachine *state_machine) const {
     133           0 :         if (timer_->cancelled()) {
     134           0 :             return false;
     135             :         } else {
     136           0 :             return (state_machine->passive_session() != NULL);
     137             :         }
     138             :     }
     139             : 
     140             :     Timer *timer_;
     141             : };
     142             : 
     143             : struct EvHoldTimerExpired : sc::event<EvHoldTimerExpired> {
     144          32 :     explicit EvHoldTimerExpired(Timer *timer) : timer_(timer) {
     145          32 :     }
     146             :     static const char *Name() {
     147             :         return "EvHoldTimerExpired";
     148             :     }
     149          32 :     bool validate(StateMachine *state_machine) const {
     150          32 :         if (timer_->cancelled()) {
     151           0 :             return false;
     152          32 :         } else if (state_machine->get_state() == StateMachine::OPENSENT) {
     153           0 :             return true;
     154             :         } else {
     155          32 :             return (state_machine->peer()->session() != NULL);
     156             :         }
     157             :     }
     158             : 
     159             :     Timer *timer_;
     160             : };
     161             : 
     162             : struct EvTcpConnected : sc::event<EvTcpConnected> {
     163         806 :     explicit EvTcpConnected(BgpSession *session) : session(session) {
     164         806 :     }
     165             :     static const char *Name() {
     166             :         return "EvTcpConnected";
     167             :     }
     168         806 :     bool validate(StateMachine *state_machine) const {
     169         806 :         return (state_machine->active_session() == session);
     170             :     }
     171             : 
     172             :     BgpSession *session;
     173             : };
     174             : 
     175             : struct EvTcpConnectFail : sc::event<EvTcpConnectFail> {
     176           0 :     explicit EvTcpConnectFail(BgpSession *session) : session(session) {
     177           0 :     }
     178             :     static const char *Name() {
     179             :         return "EvTcpConnectFail";
     180             :     }
     181           0 :     bool validate(StateMachine *state_machine) const {
     182           0 :         return (state_machine->active_session() == session);
     183             :     }
     184             : 
     185             :     BgpSession *session;
     186             : };
     187             : 
     188             : struct EvTcpPassiveOpen : sc::event<EvTcpPassiveOpen> {
     189         772 :     explicit EvTcpPassiveOpen(BgpSession *session) : session(session) {
     190         772 :     }
     191             :     static const char *Name() {
     192             :         return "EvTcpPassiveOpen";
     193             :     }
     194             : 
     195             :     BgpSession *session;
     196             : };
     197             : 
     198             : struct EvTcpClose : sc::event<EvTcpClose> {
     199         720 :     explicit EvTcpClose(BgpSession *session) : session(session) {
     200         720 :     }
     201             :     static const char *Name() {
     202             :         return "EvTcpClose";
     203             :     }
     204         720 :     bool validate(StateMachine *state_machine) const {
     205         720 :         return ((state_machine->peer()->session() == session) ||
     206        1079 :                 (state_machine->active_session() == session) ||
     207        1079 :                 (state_machine->passive_session() == session));
     208             :     }
     209             : 
     210             :     BgpSession *session;
     211             : };
     212             : 
     213             : // Used to defer the session delete after all events currently on the queue.
     214             : struct EvTcpDeleteSession : sc::event<EvTcpDeleteSession> {
     215        1578 :     explicit EvTcpDeleteSession(BgpSession *session) : session(session) {
     216        1578 :     }
     217             :     static const char *Name() {
     218             :         return "EvTcpDeleteSession";
     219             :     }
     220             : 
     221             :     BgpSession *session;
     222             : };
     223             : 
     224             : struct EvBgpHeaderError : sc::event<EvBgpHeaderError> {
     225           0 :     EvBgpHeaderError(BgpSession *session, int subcode, const uint8_t *_data,
     226             :         size_t data_size)
     227           0 :         : session(session), subcode(subcode) {
     228           0 :         if (_data)
     229           0 :             data = std::string((const char *)_data, data_size);
     230           0 :     }
     231             :     static const char *Name() {
     232             :         return "EvBgpHeaderError";
     233             :     }
     234             : 
     235             :     BgpSession *session;
     236             :     int subcode;
     237             :     std::string data;
     238             : };
     239             : 
     240             : struct EvBgpOpen : sc::event<EvBgpOpen> {
     241         561 :     EvBgpOpen(BgpSession *session, const BgpProto::OpenMessage *msg)
     242         561 :         : session(session), msg(msg) {
     243         561 :         BGP_LOG_PEER(Message, session->peer(), SandeshLevel::SYS_INFO,
     244             :             BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_IN,
     245             :             "Open " << msg->ToString());
     246         561 :     }
     247             :     static const char *Name() {
     248             :         return "EvBgpOpen";
     249             :     }
     250         561 :     bool validate(StateMachine *state_machine) const {
     251         561 :         return ((state_machine->peer()->session() == session) ||
     252         873 :                 (state_machine->active_session() == session) ||
     253         873 :                 (state_machine->passive_session() == session));
     254             :     }
     255             : 
     256             :     BgpSession *session;
     257             :     boost::shared_ptr<const BgpProto::OpenMessage> msg;
     258             : };
     259             : 
     260             : struct EvBgpOpenError : sc::event<EvBgpOpenError> {
     261         101 :     EvBgpOpenError(BgpSession *session, int subcode,
     262             :         const uint8_t *_data = NULL, size_t data_size = 0)
     263         101 :         : session(session), subcode(subcode) {
     264         101 :         if (subcode == BgpProto::Notification::UnsupportedVersion) {
     265             :             // For unsupported version, we need to send our version in the
     266             :             // data field.
     267           0 :             char version = 4;
     268           0 :             data.push_back(version);
     269         101 :         } else if (_data) {
     270           0 :             data = std::string((const char *)_data, data_size);
     271             :         }
     272         101 :     }
     273             :     static const char *Name() {
     274             :         return "EvBgpOpenError";
     275             :     }
     276             : 
     277             :     BgpSession *session;
     278             :     int subcode;
     279             :     std::string data;
     280             : };
     281             : 
     282             : struct EvBgpKeepalive : sc::event<EvBgpKeepalive> {
     283         498 :     explicit EvBgpKeepalive(BgpSession *session) : session(session) {
     284         498 :         const StateMachine *state_machine = session->peer()->state_machine();
     285             :         SandeshLevel::type log_level;
     286         497 :         if (state_machine->get_state() == StateMachine::ESTABLISHED) {
     287           0 :             log_level = Sandesh::LoggingUtLevel();
     288             :         } else {
     289         497 :             log_level = SandeshLevel::SYS_INFO;
     290             :         }
     291         497 :         BGP_LOG_PEER(Message, session->peer(), log_level,
     292             :                      BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_IN, "Keepalive");
     293         498 :     }
     294             :     static const char *Name() {
     295             :         return "EvBgpKeepalive";
     296             :     }
     297         498 :     bool validate(StateMachine *state_machine) const {
     298         498 :         return !session->IsClosed();
     299             :     }
     300             : 
     301             :     BgpSession *session;
     302             : };
     303             : 
     304             : struct EvBgpNotification : sc::event<EvBgpNotification> {
     305         377 :     EvBgpNotification(BgpSession *session, const BgpProto::Notification *msg)
     306         377 :         : session(session), msg(msg) {
     307             :         string peer_key =
     308         377 :             session->peer() ? session->peer()->ToUVEKey() : session->ToString();
     309         377 :         session->LogNotification(msg->error, msg->subcode, BGP_PEER_DIR_IN,
     310             :                                  peer_key, *msg);
     311         377 :     }
     312         234 :     static const char *Name() {
     313         234 :         return "EvBgpNotification";
     314             :     }
     315         377 :     bool validate(StateMachine *state_machine) const {
     316         377 :         return ((state_machine->peer()->session() == session) ||
     317         384 :                 (state_machine->active_session() == session) ||
     318         384 :                 (state_machine->passive_session() == session));
     319             :     }
     320             : 
     321             :     BgpSession *session;
     322             :     boost::shared_ptr<const BgpProto::Notification> msg;
     323             : };
     324             : 
     325             : struct EvBgpUpdate : sc::event<EvBgpUpdate> {
     326        5303 :     EvBgpUpdate(BgpSession *session, const BgpProto::Update *msg,
     327        5303 :         size_t msgsize) : session(session), msg(msg), msgsize(msgsize) {
     328        5303 :     }
     329             :     static const char *Name() {
     330             :         return "EvBgpUpdate";
     331             :     }
     332             : 
     333             :     BgpSession *session;
     334             :     boost::shared_ptr<const BgpProto::Update> msg;
     335             :     size_t msgsize;
     336             : };
     337             : 
     338             : struct EvBgpUpdateError : sc::event<EvBgpUpdateError> {
     339           0 :     EvBgpUpdateError(BgpSession *session, int subcode, std::string data)
     340           0 :         : session(session), subcode(subcode), data(data) {
     341           0 :     }
     342             :     static const char *Name() {
     343             :         return "EvBgpUpdateError";
     344             :     }
     345             : 
     346             :     BgpSession *session;
     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>
     360             : struct TransitToIdle {
     361             :     typedef sc::transition<Ev, Idle, StateMachine,
     362             :         &StateMachine::OnIdle<Ev, code> > reaction;
     363             : };
     364             : 
     365             : template <>
     366             : struct TransitToIdle<EvBgpNotification, 0> {
     367             :     typedef sc::transition<EvBgpNotification, Idle, StateMachine,
     368             :         &StateMachine::OnIdleNotification> reaction;
     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<
     395             :         IdleCease<EvStop>::reaction,
     396             :         sc::custom_reaction<EvStart>,
     397             :         sc::custom_reaction<EvIdleHoldTimerExpired>,
     398             :         sc::custom_reaction<EvTcpPassiveOpen>
     399             :     > reactions;
     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         906 :     explicit Idle(my_context ctx) : my_base(ctx) {
     408         906 :         StateMachine *state_machine = &context<StateMachine>();
     409         906 :         BgpPeer *peer = state_machine->peer();
     410         906 :         BgpSession *session = peer->session();
     411         906 :         peer->clear_session();
     412         906 :         state_machine->set_active_session(NULL);
     413         906 :         state_machine->set_passive_session(NULL);
     414         906 :         state_machine->DeleteSession(session);
     415         906 :         state_machine->CancelOpenTimer();
     416         906 :         state_machine->CancelIdleHoldTimer();
     417         906 :         state_machine->set_state(StateMachine::IDLE);
     418         906 :     }
     419             : 
     420        1812 :     ~Idle() {
     421         906 :         StateMachine *state_machine = &context<StateMachine>();
     422         906 :         state_machine->CancelIdleHoldTimer();
     423        1812 :     }
     424             : 
     425             :     // Start idle hold timer if it's enabled, else go to Active right away.
     426         802 :     sc::result react(const EvStart &event) {
     427         802 :         StateMachine *state_machine = &context<StateMachine>();
     428         802 :         if (state_machine->idle_hold_time()) {
     429         700 :             state_machine->StartIdleHoldTimer();
     430             :         } else {
     431         102 :             return transit<Active>();
     432             :         }
     433         700 :         return discard_event();
     434             :     }
     435             : 
     436             :     // The idle hold timer expired, go to Active.
     437         664 :     sc::result react(const EvIdleHoldTimerExpired &event) {
     438         664 :         return transit<Active>();
     439             :     }
     440             : 
     441             :     // Delete the session and ignore event.
     442         218 :     sc::result react(const EvTcpPassiveOpen &event) {
     443         218 :         StateMachine *state_machine = &context<StateMachine>();
     444         218 :         BgpSession *session = event.session;
     445         218 :         state_machine->DeleteSession(session);
     446         218 :         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<
     457             :         IdleCease<EvStop>::reaction,
     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>,
     463             :         TransitToIdle<EvBgpNotification>::reaction,
     464             :         IdleFsmError<EvBgpKeepalive>::reaction,
     465             :         IdleFsmError<EvBgpUpdate>::reaction,
     466             :         IdleError<EvBgpHeaderError,
     467             :             BgpProto::Notification::MsgHdrErr>::reaction,
     468             :         IdleError<EvBgpOpenError,
     469             :             BgpProto::Notification::OpenMsgErr>::reaction,
     470             :         IdleError<EvBgpUpdateError,
     471             :             BgpProto::Notification::UpdateMsgErr>::reaction
     472             :     > reactions;
     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        1128 :     explicit Active(my_context ctx) : my_base(ctx) {
     478        1128 :         StateMachine *state_machine = &context<StateMachine>();
     479        1128 :         BgpPeer *peer = state_machine->peer();
     480        1128 :         if (!peer->IsPassive() && !state_machine->passive_session())
     481         996 :             state_machine->StartConnectTimer(state_machine->GetConnectTime());
     482        1128 :         state_machine->set_state(StateMachine::ACTIVE);
     483        1128 :     }
     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.
     487        2256 :     ~Active() {
     488        1128 :         StateMachine *state_machine = &context<StateMachine>();
     489        1128 :         state_machine->CancelConnectTimer();
     490        2256 :     }
     491             : 
     492             :     // The connect timer expired, go to Connect if the peer is not passive.
     493         806 :     sc::result react(const EvConnectTimerExpired &event) {
     494         806 :         StateMachine *state_machine = &context<StateMachine>();
     495         806 :         BgpPeer *peer = state_machine->peer();
     496         806 :         if (peer->IsPassive()) {
     497           0 :             return discard_event();
     498             :         } else {
     499         806 :             return transit<Connect>();
     500             :         }
     501             :     }
     502             : 
     503             :     // Send an OPEN message on the passive session and go to OpenSent.
     504           0 :     sc::result react(const EvOpenTimerExpired &event) {
     505           0 :         StateMachine *state_machine = &context<StateMachine>();
     506           0 :         BgpSession *session = state_machine->passive_session();
     507           0 :         if (session) {
     508           0 :             BgpPeer *peer = state_machine->peer();
     509           0 :             peer->SendOpen(session);
     510           0 :             return transit<OpenSent>();
     511             :         }
     512           0 :         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         291 :     sc::result react(const EvTcpPassiveOpen &event) {
     520         291 :         StateMachine *state_machine = &context<StateMachine>();
     521         291 :         state_machine->set_passive_session(event.session);
     522         291 :         state_machine->CancelConnectTimer();
     523         291 :         state_machine->StartOpenTimer(StateMachine::kOpenTime);
     524         291 :         return discard_event();
     525             :     }
     526             : 
     527             :     // Start the connect timer since we don't have a passive session anymore.
     528           0 :     sc::result react(const EvTcpClose &event) {
     529           0 :         StateMachine *state_machine = &context<StateMachine>();
     530           0 :         if (event.session == state_machine->passive_session()) {
     531           0 :             state_machine->set_passive_session(NULL);
     532           0 :             state_machine->CancelOpenTimer();
     533           0 :             state_machine->StartConnectTimer(state_machine->GetConnectTime());
     534             :         }
     535           0 :         return discard_event();
     536             :     }
     537             : 
     538             :     // We received an OPEN message on the passive session. Send OPEN message
     539             :     // and go to OpenConfirm.
     540         191 :     sc::result react(const EvBgpOpen &event) {
     541         191 :         StateMachine *state_machine = &context<StateMachine>();
     542         191 :         BgpPeer *peer = state_machine->peer();
     543         191 :         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         191 :         if (!session)
     550           0 :             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         191 :         if (session != event.session)
     556           0 :             return discard_event();
     557             : 
     558             :         // Send OPEN and go to OpenConfirm.
     559         191 :         int local_holdtime = state_machine->GetConfiguredHoldTime();
     560         191 :         state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
     561         191 :         state_machine->AssignSession(false);
     562         191 :         peer->SendOpen(session);
     563         191 :         if (!peer->SetCapabilities(event.msg.get()))
     564           0 :             return discard_event();
     565         191 :         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<
     575             :         IdleCease<EvStop>::reaction,
     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>,
     583             :         TransitToIdle<EvBgpNotification>::reaction,
     584             :         IdleFsmError<EvBgpKeepalive>::reaction,
     585             :         IdleFsmError<EvBgpUpdate>::reaction,
     586             :         IdleError<EvBgpHeaderError,
     587             :             BgpProto::Notification::MsgHdrErr>::reaction,
     588             :         IdleError<EvBgpOpenError,
     589             :             BgpProto::Notification::OpenMsgErr>::reaction,
     590             :         IdleError<EvBgpUpdateError,
     591             :             BgpProto::Notification::UpdateMsgErr>::reaction
     592             :     > reactions;
     593             : 
     594         806 :     explicit Connect(my_context ctx) : my_base(ctx) {
     595         806 :         StateMachine *state_machine = &context<StateMachine>();
     596         806 :         state_machine->connect_attempts_inc();
     597         806 :         state_machine->StartConnectTimer(state_machine->GetConnectTime());
     598         806 :         state_machine->StartSession();
     599         806 :         state_machine->set_state(StateMachine::CONNECT);
     600         806 :     }
     601             : 
     602        1612 :     ~Connect() {
     603         806 :         StateMachine *state_machine = &context<StateMachine>();
     604         806 :         state_machine->CancelConnectTimer();
     605        1612 :     }
     606             : 
     607             :     // Get rid of the active session and go back to Active.
     608           0 :     sc::result react(const EvConnectTimerExpired &event) {
     609           0 :         StateMachine *state_machine = &context<StateMachine>();
     610           0 :         BgpPeer *peer = state_machine->peer();
     611           0 :         state_machine->set_active_session(NULL);
     612           0 :         peer->inc_connect_timer_expired();
     613           0 :         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           0 :     sc::result react(const EvOpenTimerExpired &event) {
     621           0 :         StateMachine *state_machine = &context<StateMachine>();
     622           0 :         BgpPeer *peer = state_machine->peer();
     623           0 :         peer->SendOpen(state_machine->passive_session());
     624           0 :         state_machine->set_active_session(NULL);
     625           0 :         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         806 :     sc::result react(const EvTcpConnected &event) {
     632         806 :         StateMachine *state_machine = &context<StateMachine>();
     633         806 :         BgpPeer *peer = state_machine->peer();
     634         806 :         BgpSession *session = state_machine->active_session();
     635         806 :         peer->SendOpen(session);
     636         806 :         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           0 :     sc::result react(const EvTcpConnectFail &event) {
     642           0 :         StateMachine *state_machine = &context<StateMachine>();
     643           0 :         state_machine->set_active_session(NULL);
     644           0 :         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          21 :     sc::result react(const EvTcpPassiveOpen &event) {
     651          21 :         StateMachine *state_machine = &context<StateMachine>();
     652          21 :         state_machine->set_passive_session(event.session);
     653          21 :         state_machine->StartOpenTimer(StateMachine::kOpenTime);
     654          21 :         return discard_event();
     655             :     }
     656             : 
     657             :     // Either the active or passive session got closed.
     658           0 :     sc::result react(const EvTcpClose &event) {
     659           0 :         StateMachine *state_machine = &context<StateMachine>();
     660           0 :         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           0 :             state_machine->set_passive_session(NULL);
     664           0 :             state_machine->CancelOpenTimer();
     665           0 :             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           0 :             assert(event.session == state_machine->active_session());
     670           0 :             state_machine->set_active_session(NULL);
     671           0 :             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           0 :     sc::result react(const EvBgpOpen &event) {
     678           0 :         StateMachine *state_machine = &context<StateMachine>();
     679           0 :         BgpPeer *peer = state_machine->peer();
     680           0 :         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           0 :         if (!session)
     687           0 :             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           0 :         if (session != event.session)
     693           0 :             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           0 :         int local_holdtime = state_machine->GetConfiguredHoldTime();
     698           0 :         state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
     699           0 :         state_machine->set_active_session(NULL);
     700           0 :         state_machine->AssignSession(false);
     701           0 :         peer->SendOpen(session);
     702           0 :         if (!peer->SetCapabilities(event.msg.get()))
     703           0 :             return discard_event();
     704           0 :         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<
     717             :         IdleCease<EvStop>::reaction,
     718             :         sc::custom_reaction<EvOpenTimerExpired>,
     719             :         TransitToIdle<EvHoldTimerExpired,
     720             :             BgpProto::Notification::HoldTimerExp>::reaction,
     721             :         sc::custom_reaction<EvTcpPassiveOpen>,
     722             :         sc::custom_reaction<EvTcpClose>,
     723             :         sc::custom_reaction<EvBgpOpen>,
     724             :         sc::custom_reaction<EvBgpNotification>,
     725             :         IdleFsmError<EvBgpKeepalive>::reaction,
     726             :         IdleFsmError<EvBgpUpdate>::reaction,
     727             :         IdleError<EvBgpHeaderError,
     728             :             BgpProto::Notification::MsgHdrErr>::reaction,
     729             :         IdleError<EvBgpOpenError,
     730             :             BgpProto::Notification::OpenMsgErr>::reaction,
     731             :         IdleError<EvBgpUpdateError,
     732             :             BgpProto::Notification::UpdateMsgErr>::reaction
     733             :     > reactions;
     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         806 :     explicit OpenSent(my_context ctx) : my_base(ctx) {
     738         806 :         StateMachine *state_machine = &context<StateMachine>();
     739         806 :         state_machine->set_hold_time(StateMachine::kOpenSentHoldTime);
     740         806 :         state_machine->StartHoldTimer();
     741         806 :         state_machine->set_state(StateMachine::OPENSENT);
     742         806 :     }
     743             : 
     744             :     // Cancel the hold timer.  If we go to OpenConfirm, the timer will get
     745             :     // started again from the constructor for that state.
     746        1612 :     ~OpenSent() {
     747         806 :         StateMachine *state_machine = &context<StateMachine>();
     748         806 :         state_machine->CancelHoldTimer();
     749        1612 :     }
     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           0 :     sc::result react(const EvOpenTimerExpired &event) {
     755           0 :         StateMachine *state_machine = &context<StateMachine>();
     756           0 :         BgpPeer *peer = state_machine->peer();
     757           0 :         peer->SendOpen(state_machine->passive_session());
     758           0 :         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         105 :     sc::result react(const EvTcpPassiveOpen &event) {
     764         105 :         StateMachine *state_machine = &context<StateMachine>();
     765         105 :         state_machine->set_passive_session(event.session);
     766         105 :         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         105 :         if (!state_machine->active_session()) {
     773           0 :             return transit<Active>();
     774             :         } else {
     775         105 :             return discard_event();
     776             :         }
     777             :     }
     778             : 
     779             :     // Either the passive or the active session closed.
     780         361 :     sc::result react(const EvTcpClose &event) {
     781         361 :         StateMachine *state_machine = &context<StateMachine>();
     782         361 :         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         361 :             state_machine->set_active_session(NULL);
     787         361 :             if (state_machine->passive_session() == NULL ||
     788           0 :                 state_machine->OpenTimerRunning()) {
     789         361 :                 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           0 :             state_machine->set_passive_session(NULL);
     795           0 :             state_machine->CancelOpenTimer();
     796           0 :             if (state_machine->active_session() == NULL)
     797           0 :                 return transit<Active>();
     798             :         }
     799             : 
     800           0 :         return discard_event();
     801             :     }
     802             : 
     803             :     // This one is pretty involved.
     804         370 :     sc::result react(const EvBgpOpen &event) {
     805         370 :         StateMachine *state_machine = &context<StateMachine>();
     806         370 :         BgpPeer *peer = state_machine->peer();
     807         370 :         BgpSession *session = NULL;
     808             : 
     809         491 :         if (state_machine->passive_session() &&
     810         121 :             state_machine->active_session()) {
     811             :             // Need to resolve connection collision.
     812         121 :             uint32_t local_bgp_id = peer->server()->bgp_identifier();
     813         121 :             if (event.msg->identifier > local_bgp_id) {
     814             :                 // Passive connection wins, close the active session.
     815          58 :                 peer->SendNotification(state_machine->active_session(),
     816             :                     BgpProto::Notification::Cease,
     817             :                     BgpProto::Notification::ConnectionCollision,
     818             :                     "Connection collision - closing active session");
     819          58 :                 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          58 :                 session = state_machine->passive_session();
     824          58 :                 if (state_machine->OpenTimerRunning()) {
     825          58 :                     state_machine->CancelOpenTimer();
     826          58 :                     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          58 :                 if (event.session != session) {
     836           0 :                     return discard_event();
     837             :                 } else {
     838          58 :                     state_machine->AssignSession(false);
     839             :                 }
     840             :             } else {
     841             :                 // Active connection wins, close the passive session.
     842          63 :                 peer->SendNotification(state_machine->passive_session(),
     843             :                     BgpProto::Notification::Cease,
     844             :                     BgpProto::Notification::ConnectionCollision,
     845             :                     "Connection collision - closing passive session");
     846          63 :                 state_machine->set_passive_session(NULL);
     847          63 :                 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          63 :                 session = state_machine->active_session();
     856          63 :                 if (event.session != session) {
     857          63 :                     return discard_event();
     858             :                 } else {
     859           0 :                     state_machine->AssignSession(true);
     860             :                 }
     861             :             }
     862         249 :         } 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           0 :             session = state_machine->passive_session();
     870           0 :             if (event.session != session) {
     871           0 :                 return discard_event();
     872             :             } else {
     873           0 :                 state_machine->AssignSession(false);
     874             :             }
     875         249 :         } 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         249 :             session = state_machine->active_session();
     883         249 :             if (event.session != session) {
     884           0 :                 return discard_event();
     885             :             } else {
     886         249 :                 state_machine->AssignSession(true);
     887             :             }
     888             :         }
     889             : 
     890         307 :         int local_holdtime = state_machine->GetConfiguredHoldTime();
     891         307 :         state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
     892         307 :         if (!peer->SetCapabilities(event.msg.get()))
     893           0 :             return discard_event();
     894         307 :         return transit<OpenConfirm>();
     895             :     }
     896             : 
     897             :     // Notification received on one of the sessions for this state machine.
     898         136 :     sc::result react(const EvBgpNotification &event) {
     899         136 :         StateMachine *state_machine = &context<StateMachine>();
     900             : 
     901             :         // Ignore if the NOTIFICATION came in on a stale session.
     902         136 :         if (!state_machine->ProcessNotificationEvent(event.session))
     903           0 :             return discard_event();
     904             : 
     905             :         // The call to ProcessNotificationEvent above would have closed
     906             :         // the session on the which the message was received.
     907         136 :         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           0 :             state_machine->CancelOpenTimer();
     913           0 :             return discard_event();
     914         136 :         } 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           1 :             if (state_machine->OpenTimerRunning()) {
     921           1 :                 return transit<Active>();
     922             :             } else {
     923           0 :                 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         135 :                 &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<
     938             :         IdleCease<EvStop>::reaction,
     939             :         IdleFsmError<EvOpenTimerExpired>::reaction,
     940             :         TransitToIdle<EvHoldTimerExpired,
     941             :             BgpProto::Notification::HoldTimerExp>::reaction,
     942             :         sc::custom_reaction<EvTcpPassiveOpen>,
     943             :         TransitToIdle<EvTcpClose>::reaction,
     944             :         IdleFsmError<EvBgpOpen>::reaction,
     945             :         sc::custom_reaction<EvBgpNotification>,
     946             :         sc::custom_reaction<EvBgpKeepalive>,
     947             :         IdleFsmError<EvBgpUpdate>::reaction,
     948             :         IdleError<EvBgpHeaderError,
     949             :             BgpProto::Notification::MsgHdrErr>::reaction,
     950             :         IdleError<EvBgpOpenError,
     951             :             BgpProto::Notification::OpenMsgErr>::reaction,
     952             :         IdleError<EvBgpUpdateError,
     953             :             BgpProto::Notification::UpdateMsgErr>::reaction
     954             :     > reactions;
     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         498 :     explicit OpenConfirm(my_context ctx) : my_base(ctx) {
     959         498 :         StateMachine *state_machine = &context<StateMachine>();
     960         498 :         BgpPeer *peer = state_machine->peer();
     961         498 :         peer->SendKeepalive(false);
     962         498 :         peer->StartKeepaliveTimer();
     963         498 :         state_machine->CancelOpenTimer();
     964         498 :         state_machine->StartHoldTimer();
     965         498 :         state_machine->set_state(StateMachine::OPENCONFIRM);
     966         498 :     }
     967             : 
     968             :     // Cancel the hold timer.  If we go to Established, the timer will get
     969             :     // started again from the constructor for that state.
     970         996 :     ~OpenConfirm() {
     971         498 :         StateMachine *state_machine = &context<StateMachine>();
     972         498 :         state_machine->CancelHoldTimer();
     973         996 :     }
     974             : 
     975             :     // Send a notification, delete the new session and stay in OpenConfirm.
     976           1 :     sc::result react(const EvTcpPassiveOpen &event) {
     977           1 :         StateMachine *state_machine = &context<StateMachine>();
     978           1 :         BgpPeer *peer = state_machine->peer();
     979           1 :         BgpSession *session = event.session;
     980           1 :         peer->SendNotification(session,
     981             :             BgpProto::Notification::Cease,
     982             :             BgpProto::Notification::ConnectionRejected,
     983             :             "Connection rejected - unexpected passive session");
     984           1 :         state_machine->DeleteSession(session);
     985           1 :         return discard_event();
     986             :     }
     987             : 
     988             :     // Ignore the notification if it's for a stale session, else go to Idle.
     989           0 :     sc::result react(const EvBgpNotification &event) {
     990           0 :         StateMachine *state_machine = &context<StateMachine>();
     991           0 :         if (!state_machine->ProcessNotificationEvent(event.session))
     992           0 :             return discard_event();
     993             : 
     994             :         return transit<Idle, StateMachine, EvBgpNotification>(
     995           0 :             &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         498 :     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         498 :         StateMachine *state_machine = &context<StateMachine>();
    1004         498 :         if (state_machine->IsPeerCloseInProgress()) {
    1005             :             return transit<Idle, StateMachine, EvBgpKeepalive>(
    1006           0 :                 &StateMachine::OnIdle<EvBgpKeepalive, 0>, event);
    1007             :         }
    1008         498 :         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<
    1017             :         IdleCease<EvStop>::reaction,
    1018             :         IdleFsmError<EvOpenTimerExpired>::reaction,
    1019             :         TransitToIdle<EvHoldTimerExpired,
    1020             :             BgpProto::Notification::HoldTimerExp>::reaction,
    1021             :         sc::custom_reaction<EvTcpPassiveOpen>,
    1022             :         TransitToIdle<EvTcpClose>::reaction,
    1023             :         IdleFsmError<EvBgpOpen>::reaction,
    1024             :         TransitToIdle<EvBgpNotification>::reaction,
    1025             :         sc::custom_reaction<EvBgpKeepalive>,
    1026             :         sc::custom_reaction<EvBgpUpdate>,
    1027             :         IdleError<EvBgpHeaderError,
    1028             :             BgpProto::Notification::MsgHdrErr>::reaction,
    1029             :         IdleFsmError<EvBgpOpenError>::reaction,
    1030             :         IdleError<EvBgpUpdateError,
    1031             :             BgpProto::Notification::UpdateMsgErr>::reaction
    1032             :     > reactions;
    1033             : 
    1034         498 :     explicit Established(my_context ctx) : my_base(ctx) {
    1035         498 :         StateMachine *state_machine = &context<StateMachine>();
    1036         498 :         BgpPeer *peer = state_machine->peer();
    1037         498 :         state_machine->connect_attempts_clear();
    1038         498 :         state_machine->StartHoldTimer();
    1039         498 :         state_machine->set_state(StateMachine::ESTABLISHED);
    1040         498 :         peer->NotifyEstablished(true);
    1041         498 :         peer->RegisterAllTables();
    1042         498 :     }
    1043             : 
    1044         996 :     ~Established() {
    1045         498 :         StateMachine *state_machine = &context<StateMachine>();
    1046         498 :         BgpPeer *peer = state_machine->peer();
    1047         498 :         peer->NotifyEstablished(false);
    1048         498 :         state_machine->CancelHoldTimer();
    1049         996 :     }
    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         136 :     sc::result react(const EvTcpPassiveOpen &event) {
    1054         136 :         StateMachine *state_machine = &context<StateMachine>();
    1055         136 :         BgpSession *session = event.session;
    1056         136 :         state_machine->DeleteSession(session);
    1057         136 :         if (state_machine->IsCloseGraceful() ||
    1058           0 :                 state_machine->IsRouterTypeBGPaaS()) {
    1059         136 :             state_machine->Shutdown(BgpProto::Notification::Unknown);
    1060             :         }
    1061         136 :         return discard_event();
    1062             :     }
    1063             : 
    1064             :     // Restart the hold timer.
    1065           0 :     sc::result react(const EvBgpKeepalive &event) {
    1066           0 :         StateMachine *state_machine = &context<StateMachine>();
    1067           0 :         state_machine->StartHoldTimer();
    1068           0 :         return discard_event();
    1069             :     }
    1070             : 
    1071             :     // Restart the hold timer and process the update.
    1072        5303 :     sc::result react(const EvBgpUpdate &event) {
    1073        5303 :         StateMachine *state_machine = &context<StateMachine>();
    1074        5303 :         state_machine->StartHoldTimer();
    1075        5303 :         state_machine->peer()->ProcessUpdate(event.msg.get(), event.msgsize);
    1076        5303 :         return discard_event();
    1077             :     }
    1078             : };
    1079             : 
    1080             : }  // namespace fsm
    1081             : 
    1082         102 : StateMachine::StateMachine(BgpPeer *peer)
    1083         102 :     : work_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
    1084             :         peer->GetTaskInstance(),
    1085             :         boost::bind(&StateMachine::DequeueEvent, this, _1)),
    1086         102 :       peer_(peer),
    1087         102 :       active_session_(NULL),
    1088         102 :       passive_session_(NULL),
    1089         102 :       connect_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
    1090             :           "Connect timer",
    1091             :           TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
    1092             :           peer->GetTaskInstance())),
    1093         102 :       open_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
    1094             :           "Open timer",
    1095             :           TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
    1096             :           peer->GetTaskInstance())),
    1097         102 :       hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
    1098             :           "Hold timer",
    1099             :           TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
    1100             :           peer->GetTaskInstance())),
    1101         102 :       idle_hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
    1102             :           "Idle hold timer",
    1103             :           TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
    1104             :           peer->GetTaskInstance())),
    1105         102 :       hold_time_(GetConfiguredHoldTime()),
    1106         102 :       idle_hold_time_(0),
    1107         102 :       attempts_(0),
    1108         102 :       deleted_(false),
    1109         102 :       state_(IDLE),
    1110         204 :       last_state_(IDLE) {
    1111         102 :     seed_ = peer_->bgp_identifier();
    1112         102 :     initiate();
    1113         102 : }
    1114             : 
    1115         102 : void StateMachine::DeleteAllTimers() {
    1116         102 :     TimerManager::DeleteTimer(connect_timer_);
    1117         102 :     TimerManager::DeleteTimer(open_timer_);
    1118         102 :     TimerManager::DeleteTimer(hold_timer_);
    1119         102 :     TimerManager::DeleteTimer(idle_hold_timer_);
    1120         102 : }
    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             : //
    1126         102 : StateMachine::~StateMachine() {
    1127         102 :     work_queue_.Shutdown();
    1128         102 :     terminate();
    1129         102 :     DeleteAllTimers();
    1130         102 : }
    1131             : 
    1132         838 : void StateMachine::Initialize() {
    1133         838 :     Enqueue(fsm::EvStart());
    1134         838 : }
    1135             : 
    1136         498 : bool StateMachine::IsPeerCloseInProgress() const {
    1137         498 :     return peer_->IsCloseInProgress();
    1138             : }
    1139             : 
    1140         302 : void StateMachine::Shutdown(int subcode) {
    1141         302 :     if (peer_->IsDeleted()) {
    1142         102 :         work_queue_.SetExitCallback(
    1143             :             boost::bind(&StateMachine::DequeueEventDone, this, _1));
    1144             :     }
    1145         302 :     Enqueue(fsm::EvStop(subcode));
    1146         302 : }
    1147             : 
    1148           0 : void StateMachine::SetAdminState(bool down, int subcode) {
    1149           0 :     if (down) {
    1150           0 :         Enqueue(fsm::EvStop(subcode));
    1151             :     } else {
    1152             :         // Reset all previous state.
    1153           0 :         reset_idle_hold_time();
    1154           0 :         reset_last_info();
    1155           0 :         peer_->reset_flap_count();
    1156           0 :         if (!IsPeerCloseInProgress())
    1157           0 :             Enqueue(fsm::EvStart());
    1158             :     }
    1159           0 : }
    1160             : 
    1161         204 : bool StateMachine::IsQueueEmpty() const {
    1162         204 :     return work_queue_.IsQueueEmpty();
    1163             : }
    1164             : 
    1165         804 : void StateMachine::UpdateFlapCount() {
    1166         804 :     if (get_state() == StateMachine::ESTABLISHED) {
    1167         498 :         peer_->increment_flap_count();
    1168         498 :         peer_->peer_stats()->Clear();
    1169             :     }
    1170         804 : }
    1171             : 
    1172         804 : void StateMachine::PeerClose(int code, int subcode) {
    1173         804 :     UpdateFlapCount();
    1174         804 :     peer_->Close(peer_->AttemptGRHelperMode(code, subcode));
    1175         906 :     set_idle_hold_time(idle_hold_time() ? idle_hold_time() :
    1176         102 :                                           GetIdleHoldTimeMSecs());
    1177         804 :     reset_hold_time();
    1178         804 : }
    1179             : 
    1180             : template <typename Ev, int code>
    1181         169 : void StateMachine::OnIdle(const Ev &event) {
    1182         169 :     SendNotification(peer_->session(), code);
    1183         169 :     PeerClose(code, 0);
    1184         169 : }
    1185             : 
    1186             : template <typename Ev>
    1187         302 : void StateMachine::OnIdleCease(const Ev &event) {
    1188         604 :     SendNotification(peer_->session(), BgpProto::Notification::Cease,
    1189         302 :                      event.subcode);
    1190         302 :     PeerClose(BgpProto::Notification::Cease, event.subcode);
    1191         302 : }
    1192             : 
    1193             : //
    1194             : // The template below must only be called for EvBgpHeaderError, EvBgpOpenError
    1195             : // or EvBgpUpdateError.
    1196             : //
    1197             : template <typename Ev, int code>
    1198          99 : void StateMachine::OnIdleError(const Ev &event) {
    1199          99 :     SendNotification(event.session, code, event.subcode, event.data);
    1200          99 :     PeerClose(code, event.subcode);
    1201          99 : }
    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.
    1205         234 : void StateMachine::OnIdleNotification(const fsm::EvBgpNotification &event) {
    1206         234 :     PeerClose(event.msg->error, event.msg->subcode);
    1207         234 :     set_last_notification_in(event.msg->error, event.msg->subcode,
    1208             :                              event.Name());
    1209         234 : }
    1210             : 
    1211        1802 : int StateMachine::GetConnectTime() const {
    1212        1802 :     int backoff = min(attempts_, 6);
    1213        1802 :     return std::min(backoff ? 1 << (backoff - 1) : 0, kConnectInterval);
    1214             : }
    1215             : 
    1216           0 : void StateMachine::StartConnectTimer(int seconds) {
    1217           0 :     connect_timer_->Cancel();
    1218             : 
    1219             :     // Add up to +/- kJitter percentage to reduce connection collisions.
    1220           0 :     int ms = seconds ? seconds * 1000 : 50;
    1221           0 :     ms = (ms * (100 - kJitter)) / 100;
    1222           0 :     ms += (ms * (rand_r(&seed_) % (kJitter * 2))) / 100;
    1223           0 :     connect_timer_->Start(ms,
    1224             :         boost::bind(&StateMachine::ConnectTimerExpired, this),
    1225             :         boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
    1226           0 : }
    1227             : 
    1228        2225 : void StateMachine::CancelConnectTimer() {
    1229        2225 :     connect_timer_->Cancel();
    1230        2225 : }
    1231             : 
    1232           0 : bool StateMachine::ConnectTimerRunning() {
    1233           0 :     return connect_timer_->running();
    1234             : }
    1235             : 
    1236           0 : void StateMachine::StartOpenTimer(int seconds) {
    1237           0 :     open_timer_->Cancel();
    1238           0 :     open_timer_->Start(seconds * 1000,
    1239             :         boost::bind(&StateMachine::OpenTimerExpired, this),
    1240             :         boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
    1241           0 : }
    1242             : 
    1243        1525 : void StateMachine::CancelOpenTimer() {
    1244        1525 :     open_timer_->Cancel();
    1245        1525 : }
    1246             : 
    1247          59 : bool StateMachine::OpenTimerRunning() {
    1248          59 :     return open_timer_->running();
    1249             : }
    1250             : 
    1251           0 : void StateMachine::StartIdleHoldTimer() {
    1252           0 :     if (idle_hold_time_ <= 0)
    1253           0 :         return;
    1254             : 
    1255           0 :     idle_hold_timer_->Cancel();
    1256           0 :     idle_hold_timer_->Start(idle_hold_time_,
    1257             :         boost::bind(&StateMachine::IdleHoldTimerExpired, this),
    1258             :         boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
    1259             : }
    1260             : 
    1261        1812 : void StateMachine::CancelIdleHoldTimer() {
    1262        1812 :     idle_hold_timer_->Cancel();
    1263        1812 : }
    1264             : 
    1265           0 : bool StateMachine::IdleHoldTimerRunning() {
    1266           0 :     return idle_hold_timer_->running();
    1267             : }
    1268             : 
    1269        7105 : void StateMachine::StartHoldTimer() {
    1270        7105 :     if (hold_time_ <= 0)
    1271           0 :         return;
    1272             : 
    1273        7105 :     hold_timer_->Cancel();
    1274        7105 :     hold_timer_->Start(hold_time_ * 1000,
    1275             :         boost::bind(&StateMachine::HoldTimerExpired, this),
    1276             :         boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
    1277             : }
    1278             : 
    1279        1802 : void StateMachine::CancelHoldTimer() {
    1280        1802 :     hold_timer_->Cancel();
    1281        1802 : }
    1282             : 
    1283           0 : bool StateMachine::HoldTimerRunning() {
    1284           0 :     return hold_timer_->running();
    1285             : }
    1286             : 
    1287             : // Test Only APIs : Start
    1288             : 
    1289           0 : void StateMachine::FireConnectTimer() {
    1290           0 :     connect_timer_->Fire();
    1291           0 : }
    1292             : 
    1293           0 : void StateMachine::FireOpenTimer() {
    1294           0 :     open_timer_->Fire();
    1295           0 : }
    1296             : 
    1297           0 : void StateMachine::FireHoldTimer() {
    1298           0 :     hold_timer_->Fire();
    1299           0 : }
    1300             : 
    1301           0 : void StateMachine::FireIdleHoldTimer() {
    1302           0 :     idle_hold_timer_->Fire();
    1303           0 : }
    1304             : 
    1305             : // Test Only APIs : END
    1306             : 
    1307             : //
    1308             : // Create an active session.
    1309             : //
    1310         806 : void StateMachine::StartSession() {
    1311         806 :     BgpSession *session = peer_->CreateSession();
    1312         806 :     if (!session)
    1313           0 :         return;
    1314         806 :     set_active_session(session);
    1315         806 :     session->set_observer(
    1316             :         boost::bind(&StateMachine::OnSessionEvent, this, _1, _2));
    1317        1612 :     peer_->server()->session_manager()->Connect(session,
    1318         806 :         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             : //
    1329        4914 : void StateMachine::DeleteSession(BgpSession *session) {
    1330        4914 :     if (!session)
    1331        3336 :         return;
    1332        1578 :     session->set_observer(NULL);
    1333        1578 :     session->Close();
    1334        1578 :     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         498 : void StateMachine::AssignSession(bool active) {
    1343         498 :     if (active) {
    1344         249 :         peer_->set_session(active_session_);
    1345         249 :         active_session_ = NULL;
    1346             :     } else {
    1347         249 :         peer_->set_session(passive_session_);
    1348         249 :         passive_session_ = NULL;
    1349             :     }
    1350         498 : }
    1351             : 
    1352        2267 : void StateMachine::set_active_session(BgpSession *session) {
    1353        2267 :     DeleteSession(active_session_);
    1354        2267 :     active_session_ = session;
    1355        2267 : }
    1356             : 
    1357        1386 : void StateMachine::set_passive_session(BgpSession *session) {
    1358        1386 :     DeleteSession(passive_session_);
    1359        1386 :     passive_session_ = session;
    1360        1386 : }
    1361             : 
    1362        4514 : BgpSession *StateMachine::active_session() {
    1363        4514 :     return active_session_;
    1364             : }
    1365             : 
    1366        3909 : BgpSession *StateMachine::passive_session() {
    1367        3909 :     return passive_session_;
    1368             : }
    1369             : 
    1370         570 : 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         570 :     if (!session)
    1374         207 :         session = passive_session_;
    1375         570 :     if (!session)
    1376         207 :         session = active_session_;
    1377         570 :     if (session && code != 0)
    1378         363 :         peer_->SendNotification(session, code, subcode, data);
    1379         570 : }
    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             : //
    1389         136 : bool StateMachine::ProcessNotificationEvent(BgpSession *session) {
    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         136 :     if (session && peer_->session() && peer_->session() != session) {
    1394           0 :         return false;
    1395             :     }
    1396             : 
    1397             :     // TransitToIdle<EvBgpNotification>::reaction,
    1398         136 :     if (active_session() == session) {
    1399         136 :         set_active_session(NULL);
    1400             :     } else {
    1401           0 :         set_passive_session(NULL);
    1402             :     }
    1403             : 
    1404         136 :     return true;
    1405             : }
    1406             : 
    1407         810 : bool StateMachine::ConnectTimerExpired() {
    1408         810 :     Enqueue(fsm::EvConnectTimerExpired(connect_timer_));
    1409         810 :     return false;
    1410             : }
    1411             : 
    1412           0 : bool StateMachine::OpenTimerExpired() {
    1413           0 :     Enqueue(fsm::EvOpenTimerExpired(open_timer_));
    1414           0 :     return false;
    1415             : }
    1416             : 
    1417          32 : bool StateMachine::HoldTimerExpired() {
    1418          32 :     boost::system::error_code error;
    1419             : 
    1420             :     // Reset hold timer if there is data already present in the socket.
    1421          64 :     if (peer() && peer()->session() && peer()->session()->socket() &&
    1422          32 :         peer()->session()->socket()->available(error) > 0) {
    1423           0 :         return true;
    1424             :     }
    1425          32 :     Enqueue(fsm::EvHoldTimerExpired(hold_timer_));
    1426          32 :     peer_->inc_hold_timer_expired();
    1427          32 :     return false;
    1428             : }
    1429             : 
    1430         664 : bool StateMachine::IdleHoldTimerExpired() {
    1431         664 :     Enqueue(fsm::EvIdleHoldTimerExpired(idle_hold_timer_));
    1432         664 :     return false;
    1433             : }
    1434             : 
    1435         136 : bool StateMachine::IsCloseGraceful() const {
    1436         136 :     return peer_->IsCloseGraceful();
    1437             : }
    1438             : 
    1439           0 : bool StateMachine::IsRouterTypeBGPaaS() const {
    1440           0 :     return peer_->IsRouterTypeBGPaaS();
    1441             : }
    1442             : 
    1443             : //
    1444             : // Concurrency: ASIO thread.
    1445             : // Feed TCP session events into the state machine.
    1446             : //
    1447        1526 : void StateMachine::OnSessionEvent(
    1448             :         TcpSession *session, TcpSession::Event event) {
    1449        1526 :     BgpSession *bgp_session = static_cast<BgpSession *>(session);
    1450        1526 :     switch (event) {
    1451         806 :     case TcpSession::CONNECT_COMPLETE:
    1452         806 :         Enqueue(fsm::EvTcpConnected(bgp_session));
    1453         806 :         break;
    1454           0 :     case TcpSession::CONNECT_FAILED:
    1455           0 :         Enqueue(fsm::EvTcpConnectFail(bgp_session));
    1456           0 :         peer_->inc_connect_error();
    1457           0 :         break;
    1458         720 :     case TcpSession::CLOSE:
    1459         720 :         Enqueue(fsm::EvTcpClose(bgp_session));
    1460         720 :         break;
    1461           0 :     default:
    1462           0 :         break;
    1463             :     }
    1464        1526 : }
    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             : //
    1471         772 : bool StateMachine::PassiveOpen(BgpSession *session) {
    1472         772 :     CHECK_CONCURRENCY("bgp::Config");
    1473         772 :     Enqueue(fsm::EvTcpPassiveOpen(session));
    1474         772 :     session->set_observer(boost::bind(&StateMachine::OnSessionEvent,
    1475             :         this, _1, _2));
    1476         772 :     session->AsyncReadStart();
    1477         772 :     return true;
    1478             : }
    1479             : 
    1480         377 : void StateMachine::OnNotificationMessage(BgpSession *session,
    1481             :                                          BgpProto::BgpMessage *msg) {
    1482         377 :     BgpPeer *peer = session->peer();
    1483         377 :     if (peer)
    1484         377 :         peer->inc_rx_notification();
    1485         377 :     Enqueue(fsm::EvBgpNotification(session,
    1486             :             static_cast<BgpProto::Notification *>(msg)));
    1487         377 : }
    1488             : 
    1489             : //
    1490             : // Handle incoming message on the session.
    1491             : //
    1492        6840 : void StateMachine::OnMessage(BgpSession *session, BgpProto::BgpMessage *msg,
    1493             :     size_t msgsize) {
    1494        6840 :     switch (msg->type) {
    1495         662 :     case BgpProto::OPEN: {
    1496         662 :         BgpProto::OpenMessage *open_msg =
    1497             :             static_cast<BgpProto::OpenMessage *>(msg);
    1498         662 :         BgpPeer *peer = session->peer();
    1499         662 :         peer->inc_rx_open();
    1500         662 :         if (int subcode = open_msg->Validate(peer)) {
    1501         101 :             Enqueue(fsm::EvBgpOpenError(session, subcode));
    1502         101 :             peer->inc_open_error();
    1503             :         } else {
    1504         561 :             Enqueue(fsm::EvBgpOpen(session, open_msg));
    1505         561 :             msg = NULL;
    1506             :         }
    1507         662 :         break;
    1508             :     }
    1509         498 :     case BgpProto::KEEPALIVE: {
    1510         498 :         BgpPeer *peer = session->peer();
    1511         498 :         Enqueue(fsm::EvBgpKeepalive(session));
    1512         498 :         if (peer) peer->inc_rx_keepalive();
    1513         498 :         break;
    1514             :     }
    1515         377 :     case BgpProto::NOTIFICATION: {
    1516         377 :         OnNotificationMessage(session, msg);
    1517         377 :         msg = NULL;
    1518         377 :         break;
    1519             :     }
    1520        5303 :     case BgpProto::UPDATE: {
    1521        5303 :         BgpProto::Update *update = static_cast<BgpProto::Update *>(msg);
    1522        5303 :         BgpPeer *peer = NULL;
    1523        5303 :         if (session)
    1524        5303 :             peer = session->peer();
    1525        5303 :         if (peer)
    1526        5303 :             peer->inc_rx_update();
    1527             : 
    1528        5303 :         std::string data;
    1529             :         int subcode;
    1530        5303 :         if (peer && (subcode = update->Validate(peer, &data))) {
    1531           0 :             Enqueue(fsm::EvBgpUpdateError(session, subcode, data));
    1532           0 :             peer->inc_update_error();
    1533             :         } else {
    1534        5303 :             Enqueue(fsm::EvBgpUpdate(session, update, msgsize));
    1535        5303 :             msg = NULL;
    1536             :         }
    1537        5303 :         break;
    1538        5303 :     }
    1539           0 :     default:
    1540           0 :         SM_LOG_NOTICE("Unknown message type " << msg->type);
    1541           0 :         break;
    1542             :     }
    1543             : 
    1544        6840 :     delete msg;
    1545        6840 : }
    1546             : 
    1547             : //
    1548             : // Handle errors in incoming message on the session.
    1549             : //
    1550           0 : void StateMachine::OnMessageError(BgpSession *session,
    1551             :         const ParseErrorContext *context) {
    1552           0 :     switch (context->error_code) {
    1553           0 :     case BgpProto::Notification::MsgHdrErr: {
    1554           0 :         Enqueue(fsm::EvBgpHeaderError(session, context->error_subcode,
    1555           0 :                 context->data, context->data_size));
    1556           0 :         break;
    1557             :     }
    1558           0 :     case BgpProto::Notification::OpenMsgErr:
    1559           0 :         Enqueue(fsm::EvBgpOpenError(session, context->error_subcode,
    1560           0 :                 context->data, context->data_size));
    1561           0 :         break;
    1562           0 :     case BgpProto::Notification::UpdateMsgErr:
    1563           0 :         Enqueue(fsm::EvBgpUpdateError(session, context->error_subcode,
    1564           0 :                 std::string((const char *)context->data, context->data_size)));
    1565           0 :         break;
    1566           0 :     default:
    1567           0 :         break;
    1568             :     }
    1569           0 : }
    1570             : 
    1571             : static const std::string state_names[] = {
    1572             :     "Idle",
    1573             :     "Active",
    1574             :     "Connect",
    1575             :     "OpenSent",
    1576             :     "OpenConfirm",
    1577             :     "Established"
    1578             : };
    1579             : 
    1580       31224 : const string &StateMachine::StateName() const {
    1581       31224 :     return state_names[state_];
    1582             : }
    1583             : 
    1584        4502 : const string &StateMachine::LastStateName() const {
    1585        4502 :     return state_names[last_state_];
    1586             : }
    1587             : 
    1588           0 : const std::string StateMachine::last_state_change_at() const {
    1589           0 :     return integerToString(UTCUsecToPTime(last_state_change_at_));
    1590             : }
    1591             : 
    1592           0 : const uint64_t StateMachine::last_state_change_usecs_at() const {
    1593           0 :     return last_state_change_at_;
    1594             : }
    1595             : 
    1596           0 : ostream &operator<<(ostream &out, const StateMachine::State &state) {
    1597           0 :     out << state_names[state];
    1598           0 :     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        8894 :     EvValidate operator()(const Ev *event) {
    1613        8894 :         return NULL;
    1614             :     }
    1615             : };
    1616             : 
    1617             : template <typename Ev>
    1618             : struct ValidateFn<Ev, true> {
    1619        4468 :     EvValidate operator()(const Ev *event) {
    1620        4468 :         return boost::bind(&Ev::validate, event, _1);
    1621             :     }
    1622             : };
    1623             : 
    1624             : template <typename Ev>
    1625       13362 : bool StateMachine::Enqueue(const Ev &event) {
    1626       13362 :     LogEvent(TYPE_NAME(event), "Enqueue");
    1627       13362 :     EventContainer ec;
    1628       13362 :     ec.event = event.intrusive_from_this();
    1629       13362 :     ec.validate = ValidateFn<Ev, HasValidate<Ev>::Has>()(
    1630       13362 :         static_cast<const Ev *>(ec.event.get()));
    1631       13362 :     work_queue_.Enqueue(ec);
    1632             : 
    1633       13362 :     return true;
    1634       13362 : }
    1635             : 
    1636       26724 : void StateMachine::LogEvent(string event_name, string msg,
    1637             :                             SandeshLevel::type log_level) {
    1638             :     // Reduce log level for keepalive and update messages.
    1639       52508 :     if (get_state() == ESTABLISHED &&
    1640       25784 :         (event_name == "fsm::EvBgpKeepalive" ||
    1641       12891 :          event_name == "fsm::EvBgpUpdate")) {
    1642       10606 :         log_level = Sandesh::LoggingUtLevel();
    1643             :     }
    1644       26723 :     SM_LOG(log_level, msg << " " << event_name << " in state " << StateName());
    1645       26724 : }
    1646             : 
    1647       13361 : bool StateMachine::DequeueEvent(StateMachine::EventContainer ec) {
    1648             :     const fsm::EvTcpDeleteSession *deferred_delete =
    1649       13361 :         dynamic_cast<const fsm::EvTcpDeleteSession *>(ec.event.get());
    1650       13360 :     if (deferred_delete != NULL) {
    1651        1578 :         LogEvent(TYPE_NAME(*ec.event), "Dequeue");
    1652        1578 :         peer_->server()->session_manager()->DeleteSession(
    1653        1578 :             deferred_delete->session);
    1654        1578 :         return true;
    1655             :     }
    1656             : 
    1657       11782 :     set_last_event(TYPE_NAME(*ec.event));
    1658       11783 :     if (ec.validate.empty() || ec.validate(this)) {
    1659       11413 :         LogEvent(TYPE_NAME(*ec.event), "Dequeue");
    1660       11414 :         process_event(*ec.event);
    1661             :     } else {
    1662         370 :         LogEvent(TYPE_NAME(*ec.event), "Discard", SandeshLevel::SYS_INFO);
    1663             :     }
    1664       11784 :     ec.event.reset();
    1665             : 
    1666       11784 :     return true;
    1667             : }
    1668             : 
    1669         102 : void StateMachine::DequeueEventDone(bool done) {
    1670         102 :     peer_->RetryDelete();
    1671         102 : }
    1672             : 
    1673        6342 : void StateMachine::SetDataCollectionKey(BgpPeerInfo *peer_info) const {
    1674        6342 :     peer_->SetDataCollectionKey(peer_info);
    1675        6341 : }
    1676             : 
    1677           0 : const std::string StateMachine::last_notification_in_error() const {
    1678             :     return (BgpProto::Notification::toString(
    1679           0 :         static_cast<BgpProto::Notification::Code>(last_notification_in_.first),
    1680           0 :         last_notification_in_.second));
    1681             : }
    1682             : 
    1683           0 : const std::string StateMachine::last_notification_out_error() const {
    1684             :     return (BgpProto::Notification::toString(
    1685           0 :         static_cast<BgpProto::Notification::Code>(last_notification_out_.first),
    1686           0 :         last_notification_out_.second));
    1687             : }
    1688             : 
    1689             : //
    1690             : // Return the configured hold time in seconds.
    1691             : //
    1692        2459 : int StateMachine::GetConfiguredHoldTime() const {
    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        2459 :     if (!env_checked) {
    1698           1 :         char *keepalive_time_str = getenv("BGP_KEEPALIVE_SECONDS");
    1699           1 :         if (keepalive_time_str) {
    1700           0 :             env_hold_time = strtoul(keepalive_time_str, NULL, 0) * 3;
    1701           0 :             env_checked = true;
    1702           0 :             return env_hold_time;
    1703             :         } else {
    1704           1 :             env_checked = true;
    1705             :         }
    1706        2458 :     } else if (env_hold_time) {
    1707           0 :         return env_hold_time;
    1708             :     }
    1709             : 
    1710             :     // Use the configured hold-time from peer if available.
    1711        2459 :     if (peer_ && peer_->hold_time())
    1712           0 :         return peer_->hold_time();
    1713             : 
    1714             :     // Use the configured hold-time from server if available.
    1715        2459 :     if (peer_ && peer_->server()->hold_time())
    1716           0 :         return peer_->server()->hold_time();
    1717             : 
    1718             :     // Use hard coded default.
    1719        2459 :     return kHoldTime;
    1720             : }
    1721             : 
    1722       13807 : void StateMachine::BGPPeerInfoSend(const BgpPeerInfoData &peer_info) {
    1723       13807 :     assert(!peer_info.get_name().empty());
    1724       13806 :     BGP_UVE_SEND(BGPPeerInfo, peer_info);
    1725       13810 : }
    1726             : 
    1727       11783 : void StateMachine::set_last_event(const std::string &event) {
    1728       11783 :     last_event_ = event;
    1729       11781 :     last_event_at_ = UTCTimestampUsec();
    1730             : 
    1731             :     // Skip keepalive and update events after we've reached established state.
    1732       23658 :     if (state_ == ESTABLISHED &&
    1733       11875 :         (event == "fsm::EvBgpKeepalive" || event == "fsm::EvBgpUpdate")) {
    1734        5303 :         return;
    1735             :     }
    1736             : 
    1737       12949 :     BgpPeerInfoData peer_info;
    1738        6469 :     peer_info.set_name(peer()->ToUVEKey());
    1739        6475 :     PeerEventInfo event_info;
    1740        6469 :     event_info.set_last_event(last_event_);
    1741        6470 :     event_info.set_last_event_at(last_event_at_);
    1742        6470 :     peer_info.set_event_info(event_info);
    1743        6479 :     BGPPeerInfoSend(peer_info);
    1744        6481 : }
    1745             : 
    1746         485 : void StateMachine::set_last_notification_out(int code, int subcode,
    1747             :     const string &reason) {
    1748         485 :     last_notification_out_ = std::make_pair(code, subcode);
    1749         485 :     last_notification_out_at_ = UTCTimestampUsec();
    1750         485 :     last_notification_out_error_ = reason;
    1751             : 
    1752         970 :     BgpPeerInfoData peer_info;
    1753         485 :     peer_info.set_name(peer()->ToUVEKey());
    1754         485 :     peer_info.set_notification_out_at(last_notification_out_at_);
    1755         485 :     peer_info.set_notification_out(BgpProto::Notification::toString(
    1756             :         static_cast<BgpProto::Notification::Code>(code), subcode));
    1757         485 :     BGPPeerInfoSend(peer_info);
    1758         485 : }
    1759             : 
    1760         234 : void StateMachine::set_last_notification_in(int code, int subcode,
    1761             :     const string &reason) {
    1762         234 :     last_notification_in_ = std::make_pair(code, subcode);
    1763         234 :     last_notification_in_at_ = UTCTimestampUsec();
    1764         234 :     last_notification_in_error_ = reason;
    1765             : 
    1766         468 :     BgpPeerInfoData peer_info;
    1767         234 :     peer_info.set_name(peer()->ToUVEKey());
    1768         234 :     peer_info.set_notification_in_at(last_notification_in_at_);
    1769         234 :     peer_info.set_notification_in(BgpProto::Notification::toString(
    1770             :         static_cast<BgpProto::Notification::Code>(code), subcode));
    1771         234 :     BGPPeerInfoSend(peer_info);
    1772         234 : }
    1773             : 
    1774        4642 : void StateMachine::set_state(State state) {
    1775        4642 :     if (state == state_)
    1776         140 :         return;
    1777        4502 :     last_state_ = state_; state_ = state;
    1778        4502 :     last_state_change_at_ = UTCTimestampUsec();
    1779             : 
    1780        9003 :     BgpPeerInfoData peer_info;
    1781        4501 :     peer_info.set_name(peer()->ToUVEKey());
    1782        4501 :     PeerStateInfo state_info;
    1783        4501 :     state_info.set_state(StateName());
    1784        4502 :     state_info.set_last_state(LastStateName());
    1785        4502 :     state_info.set_last_state_at(last_state_change_at_);
    1786        4502 :     peer_info.set_state_info(state_info);
    1787        4502 :     BGPPeerInfoSend(peer_info);
    1788        4502 : }
    1789             : 
    1790        1304 : void StateMachine::set_hold_time(int hold_time) {
    1791        1304 :     hold_time_ = hold_time;
    1792             : 
    1793        2608 :     BgpPeerInfoData peer_info;
    1794        1304 :     peer_info.set_name(peer()->ToUVEKey());
    1795        1303 :     peer_info.set_hold_time(hold_time_);
    1796        1303 :     BGPPeerInfoSend(peer_info);
    1797        1304 : }
    1798             : 
    1799         804 : void StateMachine::reset_hold_time() {
    1800         804 :     hold_time_ = GetConfiguredHoldTime();
    1801             : 
    1802        1608 :     BgpPeerInfoData peer_info;
    1803         804 :     peer_info.set_name(peer()->ToUVEKey());
    1804         804 :     peer_info.set_hold_time(hold_time_);
    1805         804 :     BGPPeerInfoSend(peer_info);
    1806         804 : }
    1807             : 
    1808           0 : void StateMachine::reset_last_info() {
    1809           0 :     last_notification_in_ = std::make_pair(0, 0);
    1810           0 :     last_notification_in_at_ = 0;
    1811           0 :     last_notification_in_error_ = std::string();
    1812           0 :     last_notification_out_ = std::make_pair(0, 0);
    1813           0 :     last_notification_out_at_ = 0;
    1814           0 :     last_notification_out_error_ = std::string();
    1815           0 :     last_state_ = IDLE;
    1816           0 :     last_event_ = "";
    1817           0 :     last_state_change_at_ = 0;
    1818           0 :     last_event_at_ = 0;
    1819             : 
    1820           0 :     BgpPeerInfoData peer_info;
    1821           0 :     peer_info.set_name(peer()->ToUVEKey());
    1822           0 :     PeerStateInfo state_info;
    1823           0 :     state_info.set_state(StateName());
    1824           0 :     state_info.set_last_state(LastStateName());
    1825           0 :     state_info.set_last_state_at(last_state_change_at_);
    1826           0 :     peer_info.set_state_info(state_info);
    1827             : 
    1828           0 :     PeerEventInfo event_info;
    1829           0 :     event_info.set_last_event(last_event_);
    1830           0 :     event_info.set_last_event_at(last_event_at_);
    1831           0 :     peer_info.set_event_info(event_info);
    1832           0 :     BGPPeerInfoSend(peer_info);
    1833           0 : }

Generated by: LCOV version 1.14