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 184 : EvStart() {
77 184 : }
78 : static const char *Name() {
79 : return "EvStart";
80 : }
81 : };
82 :
83 : struct EvStop : sc::event<EvStop> {
84 64 : explicit EvStop(int subcode) : subcode(subcode) {
85 64 : }
86 : static const char *Name() {
87 : return "EvStop";
88 : }
89 : int subcode;
90 : };
91 :
92 : struct EvIdleHoldTimerExpired : sc::event<EvIdleHoldTimerExpired> {
93 132 : explicit EvIdleHoldTimerExpired(Timer *timer) : timer_(timer) {
94 132 : }
95 : static const char *Name() {
96 : return "EvIdleHoldTimerExpired";
97 : }
98 132 : bool validate(StateMachine *state_machine) const {
99 132 : return !timer_->cancelled();
100 : }
101 :
102 : Timer *timer_;
103 : };
104 :
105 : struct EvConnectTimerExpired : sc::event<EvConnectTimerExpired> {
106 148 : explicit EvConnectTimerExpired(Timer *timer) : timer_(timer) {
107 146 : }
108 : static const char *Name() {
109 : return "EvConnectTimerExpired";
110 : }
111 148 : bool validate(StateMachine *state_machine) const {
112 148 : if (timer_->cancelled()) {
113 0 : return false;
114 148 : } else if (state_machine->get_state() == StateMachine::ACTIVE) {
115 148 : 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 0 : explicit EvHoldTimerExpired(Timer *timer) : timer_(timer) {
145 0 : }
146 : static const char *Name() {
147 : return "EvHoldTimerExpired";
148 : }
149 0 : bool validate(StateMachine *state_machine) const {
150 0 : if (timer_->cancelled()) {
151 0 : return false;
152 0 : } else if (state_machine->get_state() == StateMachine::OPENSENT) {
153 0 : return true;
154 : } else {
155 0 : return (state_machine->peer()->session() != NULL);
156 : }
157 : }
158 :
159 : Timer *timer_;
160 : };
161 :
162 : struct EvTcpConnected : sc::event<EvTcpConnected> {
163 148 : explicit EvTcpConnected(BgpSession *session) : session(session) {
164 148 : }
165 : static const char *Name() {
166 : return "EvTcpConnected";
167 : }
168 148 : bool validate(StateMachine *state_machine) const {
169 148 : 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 60 : explicit EvTcpPassiveOpen(BgpSession *session) : session(session) {
190 60 : }
191 : static const char *Name() {
192 : return "EvTcpPassiveOpen";
193 : }
194 :
195 : BgpSession *session;
196 : };
197 :
198 : struct EvTcpClose : sc::event<EvTcpClose> {
199 122 : explicit EvTcpClose(BgpSession *session) : session(session) {
200 122 : }
201 : static const char *Name() {
202 : return "EvTcpClose";
203 : }
204 122 : bool validate(StateMachine *state_machine) const {
205 122 : return ((state_machine->peer()->session() == session) ||
206 243 : (state_machine->active_session() == session) ||
207 243 : (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 208 : explicit EvTcpDeleteSession(BgpSession *session) : session(session) {
216 208 : }
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 98 : EvBgpOpen(BgpSession *session, const BgpProto::OpenMessage *msg)
242 98 : : session(session), msg(msg) {
243 98 : BGP_LOG_PEER(Message, session->peer(), SandeshLevel::SYS_INFO,
244 : BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_IN,
245 : "Open " << msg->ToString());
246 98 : }
247 : static const char *Name() {
248 : return "EvBgpOpen";
249 : }
250 98 : bool validate(StateMachine *state_machine) const {
251 98 : return ((state_machine->peer()->session() == session) ||
252 156 : (state_machine->active_session() == session) ||
253 156 : (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 0 : EvBgpOpenError(BgpSession *session, int subcode,
262 : const uint8_t *_data = NULL, size_t data_size = 0)
263 0 : : session(session), subcode(subcode) {
264 0 : 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 0 : } else if (_data) {
270 0 : data = std::string((const char *)_data, data_size);
271 : }
272 0 : }
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 80 : explicit EvBgpKeepalive(BgpSession *session) : session(session) {
284 80 : const StateMachine *state_machine = session->peer()->state_machine();
285 : SandeshLevel::type log_level;
286 80 : if (state_machine->get_state() == StateMachine::ESTABLISHED) {
287 0 : log_level = Sandesh::LoggingUtLevel();
288 : } else {
289 80 : log_level = SandeshLevel::SYS_INFO;
290 : }
291 80 : BGP_LOG_PEER(Message, session->peer(), log_level,
292 : BGP_LOG_FLAG_SYSLOG, BGP_PEER_DIR_IN, "Keepalive");
293 80 : }
294 : static const char *Name() {
295 : return "EvBgpKeepalive";
296 : }
297 80 : bool validate(StateMachine *state_machine) const {
298 80 : return !session->IsClosed();
299 : }
300 :
301 : BgpSession *session;
302 : };
303 :
304 : struct EvBgpNotification : sc::event<EvBgpNotification> {
305 124 : EvBgpNotification(BgpSession *session, const BgpProto::Notification *msg)
306 124 : : session(session), msg(msg) {
307 : string peer_key =
308 124 : session->peer() ? session->peer()->ToUVEKey() : session->ToString();
309 124 : session->LogNotification(msg->error, msg->subcode, BGP_PEER_DIR_IN,
310 : peer_key, *msg);
311 124 : }
312 32 : static const char *Name() {
313 32 : return "EvBgpNotification";
314 : }
315 124 : bool validate(StateMachine *state_machine) const {
316 124 : return ((state_machine->peer()->session() == session) ||
317 128 : (state_machine->active_session() == session) ||
318 128 : (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 9539 : EvBgpUpdate(BgpSession *session, const BgpProto::Update *msg,
327 9539 : size_t msgsize) : session(session), msg(msg), msgsize(msgsize) {
328 9539 : }
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 216 : explicit Idle(my_context ctx) : my_base(ctx) {
408 216 : StateMachine *state_machine = &context<StateMachine>();
409 216 : BgpPeer *peer = state_machine->peer();
410 216 : BgpSession *session = peer->session();
411 216 : peer->clear_session();
412 216 : state_machine->set_active_session(NULL);
413 216 : state_machine->set_passive_session(NULL);
414 216 : state_machine->DeleteSession(session);
415 216 : state_machine->CancelOpenTimer();
416 216 : state_machine->CancelIdleHoldTimer();
417 216 : state_machine->set_state(StateMachine::IDLE);
418 216 : }
419 :
420 432 : ~Idle() {
421 216 : StateMachine *state_machine = &context<StateMachine>();
422 216 : state_machine->CancelIdleHoldTimer();
423 432 : }
424 :
425 : // Start idle hold timer if it's enabled, else go to Active right away.
426 184 : sc::result react(const EvStart &event) {
427 184 : StateMachine *state_machine = &context<StateMachine>();
428 184 : if (state_machine->idle_hold_time()) {
429 136 : state_machine->StartIdleHoldTimer();
430 : } else {
431 48 : return transit<Active>();
432 : }
433 136 : return discard_event();
434 : }
435 :
436 : // The idle hold timer expired, go to Active.
437 132 : sc::result react(const EvIdleHoldTimerExpired &event) {
438 132 : return transit<Active>();
439 : }
440 :
441 : // Delete the session and ignore event.
442 1 : sc::result react(const EvTcpPassiveOpen &event) {
443 1 : StateMachine *state_machine = &context<StateMachine>();
444 1 : BgpSession *session = event.session;
445 1 : state_machine->DeleteSession(session);
446 1 : 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 181 : explicit Active(my_context ctx) : my_base(ctx) {
478 181 : StateMachine *state_machine = &context<StateMachine>();
479 181 : BgpPeer *peer = state_machine->peer();
480 181 : if (!peer->IsPassive() && !state_machine->passive_session())
481 181 : state_machine->StartConnectTimer(state_machine->GetConnectTime());
482 181 : state_machine->set_state(StateMachine::ACTIVE);
483 181 : }
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 362 : ~Active() {
488 181 : StateMachine *state_machine = &context<StateMachine>();
489 181 : state_machine->CancelConnectTimer();
490 362 : }
491 :
492 : // The connect timer expired, go to Connect if the peer is not passive.
493 148 : sc::result react(const EvConnectTimerExpired &event) {
494 148 : StateMachine *state_machine = &context<StateMachine>();
495 148 : BgpPeer *peer = state_machine->peer();
496 148 : if (peer->IsPassive()) {
497 0 : return discard_event();
498 : } else {
499 148 : 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 21 : sc::result react(const EvTcpPassiveOpen &event) {
520 21 : StateMachine *state_machine = &context<StateMachine>();
521 21 : state_machine->set_passive_session(event.session);
522 21 : state_machine->CancelConnectTimer();
523 21 : state_machine->StartOpenTimer(StateMachine::kOpenTime);
524 21 : 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 21 : sc::result react(const EvBgpOpen &event) {
541 21 : StateMachine *state_machine = &context<StateMachine>();
542 21 : BgpPeer *peer = state_machine->peer();
543 21 : 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 21 : 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 21 : if (session != event.session)
556 0 : return discard_event();
557 :
558 : // Send OPEN and go to OpenConfirm.
559 21 : int local_holdtime = state_machine->GetConfiguredHoldTime();
560 21 : state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
561 21 : state_machine->AssignSession(false);
562 21 : peer->SendOpen(session);
563 21 : if (!peer->SetCapabilities(event.msg.get()))
564 0 : return discard_event();
565 21 : 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 148 : explicit Connect(my_context ctx) : my_base(ctx) {
595 148 : StateMachine *state_machine = &context<StateMachine>();
596 148 : state_machine->connect_attempts_inc();
597 148 : state_machine->StartConnectTimer(state_machine->GetConnectTime());
598 148 : state_machine->StartSession();
599 148 : state_machine->set_state(StateMachine::CONNECT);
600 148 : }
601 :
602 296 : ~Connect() {
603 148 : StateMachine *state_machine = &context<StateMachine>();
604 148 : state_machine->CancelConnectTimer();
605 296 : }
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 148 : sc::result react(const EvTcpConnected &event) {
632 148 : StateMachine *state_machine = &context<StateMachine>();
633 148 : BgpPeer *peer = state_machine->peer();
634 148 : BgpSession *session = state_machine->active_session();
635 148 : peer->SendOpen(session);
636 148 : 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 0 : sc::result react(const EvTcpPassiveOpen &event) {
651 0 : StateMachine *state_machine = &context<StateMachine>();
652 0 : state_machine->set_passive_session(event.session);
653 0 : state_machine->StartOpenTimer(StateMachine::kOpenTime);
654 0 : 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 148 : explicit OpenSent(my_context ctx) : my_base(ctx) {
738 148 : StateMachine *state_machine = &context<StateMachine>();
739 148 : state_machine->set_hold_time(StateMachine::kOpenSentHoldTime);
740 148 : state_machine->StartHoldTimer();
741 148 : state_machine->set_state(StateMachine::OPENSENT);
742 148 : }
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 296 : ~OpenSent() {
747 148 : StateMachine *state_machine = &context<StateMachine>();
748 148 : state_machine->CancelHoldTimer();
749 296 : }
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 38 : sc::result react(const EvTcpPassiveOpen &event) {
764 38 : StateMachine *state_machine = &context<StateMachine>();
765 38 : state_machine->set_passive_session(event.session);
766 38 : 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 38 : if (!state_machine->active_session()) {
773 0 : return transit<Active>();
774 : } else {
775 38 : return discard_event();
776 : }
777 : }
778 :
779 : // Either the passive or the active session closed.
780 1 : sc::result react(const EvTcpClose &event) {
781 1 : StateMachine *state_machine = &context<StateMachine>();
782 1 : 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 1 : state_machine->set_active_session(NULL);
787 1 : if (state_machine->passive_session() == NULL ||
788 0 : state_machine->OpenTimerRunning()) {
789 1 : 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 77 : sc::result react(const EvBgpOpen &event) {
805 77 : StateMachine *state_machine = &context<StateMachine>();
806 77 : BgpPeer *peer = state_machine->peer();
807 77 : BgpSession *session = NULL;
808 :
809 115 : if (state_machine->passive_session() &&
810 38 : state_machine->active_session()) {
811 : // Need to resolve connection collision.
812 38 : uint32_t local_bgp_id = peer->server()->bgp_identifier();
813 38 : if (event.msg->identifier > local_bgp_id) {
814 : // Passive connection wins, close the active session.
815 19 : peer->SendNotification(state_machine->active_session(),
816 : BgpProto::Notification::Cease,
817 : BgpProto::Notification::ConnectionCollision,
818 : "Connection collision - closing active session");
819 19 : 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 19 : session = state_machine->passive_session();
824 19 : if (state_machine->OpenTimerRunning()) {
825 19 : state_machine->CancelOpenTimer();
826 19 : 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 19 : if (event.session != session) {
836 0 : return discard_event();
837 : } else {
838 19 : state_machine->AssignSession(false);
839 : }
840 : } else {
841 : // Active connection wins, close the passive session.
842 19 : peer->SendNotification(state_machine->passive_session(),
843 : BgpProto::Notification::Cease,
844 : BgpProto::Notification::ConnectionCollision,
845 : "Connection collision - closing passive session");
846 19 : state_machine->set_passive_session(NULL);
847 19 : 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 19 : session = state_machine->active_session();
856 19 : if (event.session != session) {
857 18 : return discard_event();
858 : } else {
859 1 : state_machine->AssignSession(true);
860 : }
861 : }
862 39 : } 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 39 : } 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 39 : session = state_machine->active_session();
883 39 : if (event.session != session) {
884 0 : return discard_event();
885 : } else {
886 39 : state_machine->AssignSession(true);
887 : }
888 : }
889 :
890 59 : int local_holdtime = state_machine->GetConfiguredHoldTime();
891 59 : state_machine->set_hold_time(min(event.msg->holdtime, local_holdtime));
892 59 : if (!peer->SetCapabilities(event.msg.get()))
893 0 : return discard_event();
894 59 : return transit<OpenConfirm>();
895 : }
896 :
897 : // Notification received on one of the sessions for this state machine.
898 88 : sc::result react(const EvBgpNotification &event) {
899 88 : StateMachine *state_machine = &context<StateMachine>();
900 :
901 : // Ignore if the NOTIFICATION came in on a stale session.
902 88 : 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 88 : 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 88 : } 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 0 : if (state_machine->OpenTimerRunning()) {
921 0 : 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 88 : &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 80 : explicit OpenConfirm(my_context ctx) : my_base(ctx) {
959 80 : StateMachine *state_machine = &context<StateMachine>();
960 80 : BgpPeer *peer = state_machine->peer();
961 80 : peer->SendKeepalive(false);
962 80 : peer->StartKeepaliveTimer();
963 80 : state_machine->CancelOpenTimer();
964 80 : state_machine->StartHoldTimer();
965 80 : state_machine->set_state(StateMachine::OPENCONFIRM);
966 80 : }
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 160 : ~OpenConfirm() {
971 80 : StateMachine *state_machine = &context<StateMachine>();
972 80 : state_machine->CancelHoldTimer();
973 160 : }
974 :
975 : // Send a notification, delete the new session and stay in OpenConfirm.
976 0 : sc::result react(const EvTcpPassiveOpen &event) {
977 0 : StateMachine *state_machine = &context<StateMachine>();
978 0 : BgpPeer *peer = state_machine->peer();
979 0 : BgpSession *session = event.session;
980 0 : peer->SendNotification(session,
981 : BgpProto::Notification::Cease,
982 : BgpProto::Notification::ConnectionRejected,
983 : "Connection rejected - unexpected passive session");
984 0 : state_machine->DeleteSession(session);
985 0 : 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 80 : 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 80 : StateMachine *state_machine = &context<StateMachine>();
1004 80 : if (state_machine->IsPeerCloseInProgress()) {
1005 : return transit<Idle, StateMachine, EvBgpKeepalive>(
1006 0 : &StateMachine::OnIdle<EvBgpKeepalive, 0>, event);
1007 : }
1008 80 : 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 80 : explicit Established(my_context ctx) : my_base(ctx) {
1035 80 : StateMachine *state_machine = &context<StateMachine>();
1036 80 : BgpPeer *peer = state_machine->peer();
1037 80 : state_machine->connect_attempts_clear();
1038 80 : state_machine->StartHoldTimer();
1039 79 : state_machine->set_state(StateMachine::ESTABLISHED);
1040 80 : peer->NotifyEstablished(true);
1041 80 : peer->RegisterAllTables();
1042 80 : }
1043 :
1044 160 : ~Established() {
1045 80 : StateMachine *state_machine = &context<StateMachine>();
1046 80 : BgpPeer *peer = state_machine->peer();
1047 80 : peer->NotifyEstablished(false);
1048 80 : state_machine->CancelHoldTimer();
1049 160 : }
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 0 : sc::result react(const EvTcpPassiveOpen &event) {
1054 0 : StateMachine *state_machine = &context<StateMachine>();
1055 0 : BgpSession *session = event.session;
1056 0 : state_machine->DeleteSession(session);
1057 0 : if (state_machine->IsCloseGraceful() ||
1058 0 : state_machine->IsRouterTypeBGPaaS()) {
1059 0 : state_machine->Shutdown(BgpProto::Notification::Unknown);
1060 : }
1061 0 : 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 9530 : sc::result react(const EvBgpUpdate &event) {
1073 9530 : StateMachine *state_machine = &context<StateMachine>();
1074 9530 : state_machine->StartHoldTimer();
1075 9530 : state_machine->peer()->ProcessUpdate(event.msg.get(), event.msgsize);
1076 9529 : return discard_event();
1077 : }
1078 : };
1079 :
1080 : } // namespace fsm
1081 :
1082 32 : StateMachine::StateMachine(BgpPeer *peer)
1083 32 : : work_queue_(TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1084 : peer->GetTaskInstance(),
1085 : boost::bind(&StateMachine::DequeueEvent, this, _1)),
1086 32 : peer_(peer),
1087 32 : active_session_(NULL),
1088 32 : passive_session_(NULL),
1089 32 : connect_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1090 : "Connect timer",
1091 : TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1092 : peer->GetTaskInstance())),
1093 32 : open_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1094 : "Open timer",
1095 : TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1096 : peer->GetTaskInstance())),
1097 32 : hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1098 : "Hold timer",
1099 : TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1100 : peer->GetTaskInstance())),
1101 32 : idle_hold_timer_(TimerManager::CreateTimer(*peer->server()->ioservice(),
1102 : "Idle hold timer",
1103 : TaskScheduler::GetInstance()->GetTaskId("bgp::StateMachine"),
1104 : peer->GetTaskInstance())),
1105 32 : hold_time_(GetConfiguredHoldTime()),
1106 32 : idle_hold_time_(0),
1107 32 : attempts_(0),
1108 32 : deleted_(false),
1109 32 : state_(IDLE),
1110 64 : last_state_(IDLE) {
1111 32 : seed_ = peer_->bgp_identifier();
1112 32 : initiate();
1113 32 : }
1114 :
1115 32 : void StateMachine::DeleteAllTimers() {
1116 32 : TimerManager::DeleteTimer(connect_timer_);
1117 32 : TimerManager::DeleteTimer(open_timer_);
1118 32 : TimerManager::DeleteTimer(hold_timer_);
1119 32 : TimerManager::DeleteTimer(idle_hold_timer_);
1120 32 : }
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 32 : StateMachine::~StateMachine() {
1127 32 : work_queue_.Shutdown();
1128 32 : terminate();
1129 32 : DeleteAllTimers();
1130 32 : }
1131 :
1132 168 : void StateMachine::Initialize() {
1133 168 : Enqueue(fsm::EvStart());
1134 168 : }
1135 :
1136 96 : bool StateMachine::IsPeerCloseInProgress() const {
1137 96 : return peer_->IsCloseInProgress();
1138 : }
1139 :
1140 48 : void StateMachine::Shutdown(int subcode) {
1141 48 : if (peer_->IsDeleted()) {
1142 32 : work_queue_.SetExitCallback(
1143 : boost::bind(&StateMachine::DequeueEventDone, this, _1));
1144 : }
1145 48 : Enqueue(fsm::EvStop(subcode));
1146 48 : }
1147 :
1148 32 : void StateMachine::SetAdminState(bool down, int subcode) {
1149 32 : if (down) {
1150 16 : Enqueue(fsm::EvStop(subcode));
1151 : } else {
1152 : // Reset all previous state.
1153 16 : reset_idle_hold_time();
1154 16 : reset_last_info();
1155 16 : peer_->reset_flap_count();
1156 16 : if (!IsPeerCloseInProgress())
1157 16 : Enqueue(fsm::EvStart());
1158 : }
1159 32 : }
1160 :
1161 64 : bool StateMachine::IsQueueEmpty() const {
1162 64 : return work_queue_.IsQueueEmpty();
1163 : }
1164 :
1165 184 : void StateMachine::UpdateFlapCount() {
1166 184 : if (get_state() == StateMachine::ESTABLISHED) {
1167 80 : peer_->increment_flap_count();
1168 80 : peer_->peer_stats()->Clear();
1169 : }
1170 184 : }
1171 :
1172 184 : void StateMachine::PeerClose(int code, int subcode) {
1173 184 : UpdateFlapCount();
1174 184 : peer_->Close(peer_->AttemptGRHelperMode(code, subcode));
1175 232 : set_idle_hold_time(idle_hold_time() ? idle_hold_time() :
1176 48 : GetIdleHoldTimeMSecs());
1177 184 : reset_hold_time();
1178 184 : }
1179 :
1180 : template <typename Ev, int code>
1181 88 : void StateMachine::OnIdle(const Ev &event) {
1182 88 : SendNotification(peer_->session(), code);
1183 88 : PeerClose(code, 0);
1184 88 : }
1185 :
1186 : template <typename Ev>
1187 64 : void StateMachine::OnIdleCease(const Ev &event) {
1188 128 : SendNotification(peer_->session(), BgpProto::Notification::Cease,
1189 64 : event.subcode);
1190 64 : PeerClose(BgpProto::Notification::Cease, event.subcode);
1191 64 : }
1192 :
1193 : //
1194 : // The template below must only be called for EvBgpHeaderError, EvBgpOpenError
1195 : // or EvBgpUpdateError.
1196 : //
1197 : template <typename Ev, int code>
1198 0 : void StateMachine::OnIdleError(const Ev &event) {
1199 0 : SendNotification(event.session, code, event.subcode, event.data);
1200 0 : PeerClose(code, event.subcode);
1201 0 : }
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 32 : void StateMachine::OnIdleNotification(const fsm::EvBgpNotification &event) {
1206 32 : PeerClose(event.msg->error, event.msg->subcode);
1207 32 : set_last_notification_in(event.msg->error, event.msg->subcode,
1208 : event.Name());
1209 32 : }
1210 :
1211 329 : int StateMachine::GetConnectTime() const {
1212 329 : int backoff = min(attempts_, 6);
1213 329 : 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 350 : void StateMachine::CancelConnectTimer() {
1229 350 : connect_timer_->Cancel();
1230 350 : }
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 334 : void StateMachine::CancelOpenTimer() {
1244 334 : open_timer_->Cancel();
1245 334 : }
1246 :
1247 19 : bool StateMachine::OpenTimerRunning() {
1248 19 : 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 432 : void StateMachine::CancelIdleHoldTimer() {
1262 432 : idle_hold_timer_->Cancel();
1263 432 : }
1264 :
1265 0 : bool StateMachine::IdleHoldTimerRunning() {
1266 0 : return idle_hold_timer_->running();
1267 : }
1268 :
1269 9838 : void StateMachine::StartHoldTimer() {
1270 9838 : if (hold_time_ <= 0)
1271 0 : return;
1272 :
1273 9838 : hold_timer_->Cancel();
1274 9838 : hold_timer_->Start(hold_time_ * 1000,
1275 : boost::bind(&StateMachine::HoldTimerExpired, this),
1276 : boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
1277 : }
1278 :
1279 308 : void StateMachine::CancelHoldTimer() {
1280 308 : hold_timer_->Cancel();
1281 308 : }
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 148 : void StateMachine::StartSession() {
1311 148 : BgpSession *session = peer_->CreateSession();
1312 148 : if (!session)
1313 0 : return;
1314 148 : set_active_session(session);
1315 148 : session->set_observer(
1316 : boost::bind(&StateMachine::OnSessionEvent, this, _1, _2));
1317 295 : peer_->server()->session_manager()->Connect(session,
1318 147 : 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 983 : void StateMachine::DeleteSession(BgpSession *session) {
1330 983 : if (!session)
1331 775 : return;
1332 208 : session->set_observer(NULL);
1333 208 : session->Close();
1334 208 : 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 80 : void StateMachine::AssignSession(bool active) {
1343 80 : if (active) {
1344 40 : peer_->set_session(active_session_);
1345 40 : active_session_ = NULL;
1346 : } else {
1347 40 : peer_->set_session(passive_session_);
1348 40 : passive_session_ = NULL;
1349 : }
1350 80 : }
1351 :
1352 472 : void StateMachine::set_active_session(BgpSession *session) {
1353 472 : DeleteSession(active_session_);
1354 472 : active_session_ = session;
1355 472 : }
1356 :
1357 294 : void StateMachine::set_passive_session(BgpSession *session) {
1358 294 : DeleteSession(passive_session_);
1359 294 : passive_session_ = session;
1360 294 : }
1361 :
1362 977 : BgpSession *StateMachine::active_session() {
1363 977 : return active_session_;
1364 : }
1365 :
1366 776 : BgpSession *StateMachine::passive_session() {
1367 776 : return passive_session_;
1368 : }
1369 :
1370 152 : 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 152 : if (!session)
1374 104 : session = passive_session_;
1375 152 : if (!session)
1376 104 : session = active_session_;
1377 152 : if (session && code != 0)
1378 48 : peer_->SendNotification(session, code, subcode, data);
1379 152 : }
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 88 : 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 88 : if (session && peer_->session() && peer_->session() != session) {
1394 0 : return false;
1395 : }
1396 :
1397 : // TransitToIdle<EvBgpNotification>::reaction,
1398 88 : if (active_session() == session) {
1399 88 : set_active_session(NULL);
1400 : } else {
1401 0 : set_passive_session(NULL);
1402 : }
1403 :
1404 88 : return true;
1405 : }
1406 :
1407 147 : bool StateMachine::ConnectTimerExpired() {
1408 147 : Enqueue(fsm::EvConnectTimerExpired(connect_timer_));
1409 148 : return false;
1410 : }
1411 :
1412 0 : bool StateMachine::OpenTimerExpired() {
1413 0 : Enqueue(fsm::EvOpenTimerExpired(open_timer_));
1414 0 : return false;
1415 : }
1416 :
1417 0 : bool StateMachine::HoldTimerExpired() {
1418 0 : boost::system::error_code error;
1419 :
1420 : // Reset hold timer if there is data already present in the socket.
1421 0 : if (peer() && peer()->session() && peer()->session()->socket() &&
1422 0 : peer()->session()->socket()->available(error) > 0) {
1423 0 : return true;
1424 : }
1425 0 : Enqueue(fsm::EvHoldTimerExpired(hold_timer_));
1426 0 : peer_->inc_hold_timer_expired();
1427 0 : return false;
1428 : }
1429 :
1430 132 : bool StateMachine::IdleHoldTimerExpired() {
1431 132 : Enqueue(fsm::EvIdleHoldTimerExpired(idle_hold_timer_));
1432 132 : return false;
1433 : }
1434 :
1435 0 : bool StateMachine::IsCloseGraceful() const {
1436 0 : 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 270 : void StateMachine::OnSessionEvent(
1448 : TcpSession *session, TcpSession::Event event) {
1449 270 : BgpSession *bgp_session = static_cast<BgpSession *>(session);
1450 270 : switch (event) {
1451 148 : case TcpSession::CONNECT_COMPLETE:
1452 148 : Enqueue(fsm::EvTcpConnected(bgp_session));
1453 148 : break;
1454 0 : case TcpSession::CONNECT_FAILED:
1455 0 : Enqueue(fsm::EvTcpConnectFail(bgp_session));
1456 0 : peer_->inc_connect_error();
1457 0 : break;
1458 122 : case TcpSession::CLOSE:
1459 122 : Enqueue(fsm::EvTcpClose(bgp_session));
1460 122 : break;
1461 0 : default:
1462 0 : break;
1463 : }
1464 270 : }
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 60 : bool StateMachine::PassiveOpen(BgpSession *session) {
1472 60 : CHECK_CONCURRENCY("bgp::Config");
1473 60 : Enqueue(fsm::EvTcpPassiveOpen(session));
1474 60 : session->set_observer(boost::bind(&StateMachine::OnSessionEvent,
1475 : this, _1, _2));
1476 60 : session->AsyncReadStart();
1477 60 : return true;
1478 : }
1479 :
1480 124 : void StateMachine::OnNotificationMessage(BgpSession *session,
1481 : BgpProto::BgpMessage *msg) {
1482 124 : BgpPeer *peer = session->peer();
1483 124 : if (peer)
1484 124 : peer->inc_rx_notification();
1485 124 : Enqueue(fsm::EvBgpNotification(session,
1486 : static_cast<BgpProto::Notification *>(msg)));
1487 124 : }
1488 :
1489 : //
1490 : // Handle incoming message on the session.
1491 : //
1492 9841 : void StateMachine::OnMessage(BgpSession *session, BgpProto::BgpMessage *msg,
1493 : size_t msgsize) {
1494 9841 : switch (msg->type) {
1495 98 : case BgpProto::OPEN: {
1496 98 : BgpProto::OpenMessage *open_msg =
1497 : static_cast<BgpProto::OpenMessage *>(msg);
1498 98 : BgpPeer *peer = session->peer();
1499 98 : peer->inc_rx_open();
1500 98 : if (int subcode = open_msg->Validate(peer)) {
1501 0 : Enqueue(fsm::EvBgpOpenError(session, subcode));
1502 0 : peer->inc_open_error();
1503 : } else {
1504 98 : Enqueue(fsm::EvBgpOpen(session, open_msg));
1505 98 : msg = NULL;
1506 : }
1507 98 : break;
1508 : }
1509 80 : case BgpProto::KEEPALIVE: {
1510 80 : BgpPeer *peer = session->peer();
1511 80 : Enqueue(fsm::EvBgpKeepalive(session));
1512 80 : if (peer) peer->inc_rx_keepalive();
1513 80 : break;
1514 : }
1515 124 : case BgpProto::NOTIFICATION: {
1516 124 : OnNotificationMessage(session, msg);
1517 124 : msg = NULL;
1518 124 : break;
1519 : }
1520 9539 : case BgpProto::UPDATE: {
1521 9539 : BgpProto::Update *update = static_cast<BgpProto::Update *>(msg);
1522 9539 : BgpPeer *peer = NULL;
1523 9539 : if (session)
1524 9539 : peer = session->peer();
1525 9539 : if (peer)
1526 9539 : peer->inc_rx_update();
1527 :
1528 9539 : std::string data;
1529 : int subcode;
1530 9539 : if (peer && (subcode = update->Validate(peer, &data))) {
1531 0 : Enqueue(fsm::EvBgpUpdateError(session, subcode, data));
1532 0 : peer->inc_update_error();
1533 : } else {
1534 9539 : Enqueue(fsm::EvBgpUpdate(session, update, msgsize));
1535 9539 : msg = NULL;
1536 : }
1537 9539 : break;
1538 9539 : }
1539 0 : default:
1540 0 : SM_LOG_NOTICE("Unknown message type " << msg->type);
1541 0 : break;
1542 : }
1543 :
1544 9841 : delete msg;
1545 9841 : }
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 22643 : const string &StateMachine::StateName() const {
1581 22643 : return state_names[state_];
1582 : }
1583 :
1584 832 : const string &StateMachine::LastStateName() const {
1585 832 : 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 10055 : EvValidate operator()(const Ev *event) {
1613 10055 : return NULL;
1614 : }
1615 : };
1616 :
1617 : template <typename Ev>
1618 : struct ValidateFn<Ev, true> {
1619 851 : EvValidate operator()(const Ev *event) {
1620 851 : return boost::bind(&Ev::validate, event, _1);
1621 : }
1622 : };
1623 :
1624 : template <typename Ev>
1625 10905 : bool StateMachine::Enqueue(const Ev &event) {
1626 10905 : LogEvent(TYPE_NAME(event), "Enqueue");
1627 10907 : EventContainer ec;
1628 10907 : ec.event = event.intrusive_from_this();
1629 10906 : ec.validate = ValidateFn<Ev, HasValidate<Ev>::Has>()(
1630 10906 : static_cast<const Ev *>(ec.event.get()));
1631 10906 : work_queue_.Enqueue(ec);
1632 :
1633 10907 : return true;
1634 10907 : }
1635 :
1636 21814 : void StateMachine::LogEvent(string event_name, string msg,
1637 : SandeshLevel::type log_level) {
1638 : // Reduce log level for keepalive and update messages.
1639 60496 : if (get_state() == ESTABLISHED &&
1640 38682 : (event_name == "fsm::EvBgpKeepalive" ||
1641 19341 : event_name == "fsm::EvBgpUpdate")) {
1642 19069 : log_level = Sandesh::LoggingUtLevel();
1643 : }
1644 21813 : SM_LOG(log_level, msg << " " << event_name << " in state " << StateName());
1645 21814 : }
1646 :
1647 10907 : bool StateMachine::DequeueEvent(StateMachine::EventContainer ec) {
1648 : const fsm::EvTcpDeleteSession *deferred_delete =
1649 10907 : dynamic_cast<const fsm::EvTcpDeleteSession *>(ec.event.get());
1650 10906 : if (deferred_delete != NULL) {
1651 208 : LogEvent(TYPE_NAME(*ec.event), "Dequeue");
1652 208 : peer_->server()->session_manager()->DeleteSession(
1653 208 : deferred_delete->session);
1654 208 : return true;
1655 : }
1656 :
1657 10698 : set_last_event(TYPE_NAME(*ec.event));
1658 10699 : if (ec.validate.empty() || ec.validate(this)) {
1659 10574 : LogEvent(TYPE_NAME(*ec.event), "Dequeue");
1660 10574 : process_event(*ec.event);
1661 : } else {
1662 125 : LogEvent(TYPE_NAME(*ec.event), "Discard", SandeshLevel::SYS_INFO);
1663 : }
1664 10699 : ec.event.reset();
1665 :
1666 10698 : return true;
1667 : }
1668 :
1669 32 : void StateMachine::DequeueEventDone(bool done) {
1670 32 : peer_->RetryDelete();
1671 32 : }
1672 :
1673 1094 : void StateMachine::SetDataCollectionKey(BgpPeerInfo *peer_info) const {
1674 1094 : peer_->SetDataCollectionKey(peer_info);
1675 1094 : }
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 484 : 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 484 : 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 483 : } else if (env_hold_time) {
1707 0 : return env_hold_time;
1708 : }
1709 :
1710 : // Use the configured hold-time from peer if available.
1711 484 : if (peer_ && peer_->hold_time())
1712 0 : return peer_->hold_time();
1713 :
1714 : // Use the configured hold-time from server if available.
1715 484 : if (peer_ && peer_->server()->hold_time())
1716 0 : return peer_->server()->hold_time();
1717 :
1718 : // Use hard coded default.
1719 484 : return kHoldTime;
1720 : }
1721 :
1722 2527 : void StateMachine::BGPPeerInfoSend(const BgpPeerInfoData &peer_info) {
1723 2527 : assert(!peer_info.get_name().empty());
1724 2527 : BGP_UVE_SEND(BGPPeerInfo, peer_info);
1725 2532 : }
1726 :
1727 10697 : void StateMachine::set_last_event(const std::string &event) {
1728 10697 : last_event_ = event;
1729 10696 : last_event_at_ = UTCTimestampUsec();
1730 :
1731 : // Skip keepalive and update events after we've reached established state.
1732 29919 : if (state_ == ESTABLISHED &&
1733 19220 : (event == "fsm::EvBgpKeepalive" || event == "fsm::EvBgpUpdate")) {
1734 9530 : return;
1735 : }
1736 :
1737 2335 : BgpPeerInfoData peer_info;
1738 1166 : peer_info.set_name(peer()->ToUVEKey());
1739 1168 : PeerEventInfo event_info;
1740 1167 : event_info.set_last_event(last_event_);
1741 1165 : event_info.set_last_event_at(last_event_at_);
1742 1165 : peer_info.set_event_info(event_info);
1743 1165 : BGPPeerInfoSend(peer_info);
1744 1169 : }
1745 :
1746 86 : void StateMachine::set_last_notification_out(int code, int subcode,
1747 : const string &reason) {
1748 86 : last_notification_out_ = std::make_pair(code, subcode);
1749 86 : last_notification_out_at_ = UTCTimestampUsec();
1750 86 : last_notification_out_error_ = reason;
1751 :
1752 172 : BgpPeerInfoData peer_info;
1753 86 : peer_info.set_name(peer()->ToUVEKey());
1754 86 : peer_info.set_notification_out_at(last_notification_out_at_);
1755 86 : peer_info.set_notification_out(BgpProto::Notification::toString(
1756 : static_cast<BgpProto::Notification::Code>(code), subcode));
1757 86 : BGPPeerInfoSend(peer_info);
1758 86 : }
1759 :
1760 32 : void StateMachine::set_last_notification_in(int code, int subcode,
1761 : const string &reason) {
1762 32 : last_notification_in_ = std::make_pair(code, subcode);
1763 32 : last_notification_in_at_ = UTCTimestampUsec();
1764 32 : last_notification_in_error_ = reason;
1765 :
1766 64 : BgpPeerInfoData peer_info;
1767 32 : peer_info.set_name(peer()->ToUVEKey());
1768 32 : peer_info.set_notification_in_at(last_notification_in_at_);
1769 32 : peer_info.set_notification_in(BgpProto::Notification::toString(
1770 : static_cast<BgpProto::Notification::Code>(code), subcode));
1771 32 : BGPPeerInfoSend(peer_info);
1772 32 : }
1773 :
1774 852 : void StateMachine::set_state(State state) {
1775 852 : if (state == state_)
1776 36 : return;
1777 816 : last_state_ = state_; state_ = state;
1778 816 : last_state_change_at_ = UTCTimestampUsec();
1779 :
1780 1633 : BgpPeerInfoData peer_info;
1781 816 : peer_info.set_name(peer()->ToUVEKey());
1782 816 : PeerStateInfo state_info;
1783 816 : state_info.set_state(StateName());
1784 816 : state_info.set_last_state(LastStateName());
1785 816 : state_info.set_last_state_at(last_state_change_at_);
1786 816 : peer_info.set_state_info(state_info);
1787 816 : BGPPeerInfoSend(peer_info);
1788 817 : }
1789 :
1790 228 : void StateMachine::set_hold_time(int hold_time) {
1791 228 : hold_time_ = hold_time;
1792 :
1793 456 : BgpPeerInfoData peer_info;
1794 228 : peer_info.set_name(peer()->ToUVEKey());
1795 228 : peer_info.set_hold_time(hold_time_);
1796 228 : BGPPeerInfoSend(peer_info);
1797 228 : }
1798 :
1799 184 : void StateMachine::reset_hold_time() {
1800 184 : hold_time_ = GetConfiguredHoldTime();
1801 :
1802 368 : BgpPeerInfoData peer_info;
1803 184 : peer_info.set_name(peer()->ToUVEKey());
1804 184 : peer_info.set_hold_time(hold_time_);
1805 184 : BGPPeerInfoSend(peer_info);
1806 184 : }
1807 :
1808 16 : void StateMachine::reset_last_info() {
1809 16 : last_notification_in_ = std::make_pair(0, 0);
1810 16 : last_notification_in_at_ = 0;
1811 16 : last_notification_in_error_ = std::string();
1812 16 : last_notification_out_ = std::make_pair(0, 0);
1813 16 : last_notification_out_at_ = 0;
1814 16 : last_notification_out_error_ = std::string();
1815 16 : last_state_ = IDLE;
1816 16 : last_event_ = "";
1817 16 : last_state_change_at_ = 0;
1818 16 : last_event_at_ = 0;
1819 :
1820 32 : BgpPeerInfoData peer_info;
1821 16 : peer_info.set_name(peer()->ToUVEKey());
1822 16 : PeerStateInfo state_info;
1823 16 : state_info.set_state(StateName());
1824 16 : state_info.set_last_state(LastStateName());
1825 16 : state_info.set_last_state_at(last_state_change_at_);
1826 16 : peer_info.set_state_info(state_info);
1827 :
1828 16 : PeerEventInfo event_info;
1829 16 : event_info.set_last_event(last_event_);
1830 16 : event_info.set_last_event_at(last_event_at_);
1831 16 : peer_info.set_event_info(event_info);
1832 16 : BGPPeerInfoSend(peer_info);
1833 16 : }
|