Tomato 1.28
[tomato.git] / release / src / router / zebra / bgpd / bgp_fsm.c
blob847c79e1dfd3f69929d105f399310b376b810fbc
1 /*
2 * BGP-4 Finite State Machine
3 * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
4 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
24 #include <zebra.h>
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "vty.h"
29 #include "sockunion.h"
30 #include "thread.h"
31 #include "log.h"
32 #include "stream.h"
33 #include "memory.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_attr.h"
37 #include "bgpd/bgp_debug.h"
38 #include "bgpd/bgp_fsm.h"
39 #include "bgpd/bgp_packet.h"
40 #include "bgpd/bgp_network.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_dump.h"
44 /* BGP FSM (finite state machine) has three types of functions. Type
45 one is thread functions. Type two is event functions. Type three
46 is FSM functions. Timer functions are set by bgp_timer_set
47 function. */
49 /* BGP event function. */
50 int bgp_event (struct thread *);
52 /* BGP thread functions. */
53 static int bgp_start_timer (struct thread *);
54 static int bgp_connect_timer (struct thread *);
55 static int bgp_holdtime_timer (struct thread *);
56 static int bgp_keepalive_timer (struct thread *);
58 /* BGP FSM functions. */
59 static int bgp_start (struct peer *);
61 /* BGP start timer jitter. */
62 int
63 bgp_start_jitter (int time)
65 return ((rand () % (time + 1)) - (time / 2));
68 /* Hook function called after bgp event is occered. And vty's
69 neighbor command invoke this function after making neighbor
70 structure. */
71 void
72 bgp_timer_set (struct peer *peer)
74 int jitter = 0;
76 switch (peer->status)
78 case Idle:
79 /* First entry point of peer's finite state machine. In Idle
80 status start timer is on unless peer is shutdown or peer is
81 inactive. All other timer must be turned off */
82 if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)
83 || CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW)
84 || ! peer_active (peer))
86 BGP_TIMER_OFF (peer->t_start);
88 else
90 jitter = bgp_start_jitter (peer->v_start);
91 BGP_TIMER_ON (peer->t_start, bgp_start_timer,
92 peer->v_start + jitter);
94 BGP_TIMER_OFF (peer->t_connect);
95 BGP_TIMER_OFF (peer->t_holdtime);
96 BGP_TIMER_OFF (peer->t_keepalive);
97 BGP_TIMER_OFF (peer->t_asorig);
98 BGP_TIMER_OFF (peer->t_routeadv);
99 break;
101 case Connect:
102 /* After start timer is expired, the peer moves to Connnect
103 status. Make sure start timer is off and connect timer is
104 on. */
105 BGP_TIMER_OFF (peer->t_start);
106 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
107 BGP_TIMER_OFF (peer->t_holdtime);
108 BGP_TIMER_OFF (peer->t_keepalive);
109 BGP_TIMER_OFF (peer->t_asorig);
110 BGP_TIMER_OFF (peer->t_routeadv);
111 break;
113 case Active:
114 /* Active is waiting connection from remote peer. And if
115 connect timer is expired, change status to Connect. */
116 BGP_TIMER_OFF (peer->t_start);
117 /* If peer is passive mode, do not set connect timer. */
118 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
120 BGP_TIMER_OFF (peer->t_connect);
122 else
124 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
126 BGP_TIMER_OFF (peer->t_holdtime);
127 BGP_TIMER_OFF (peer->t_keepalive);
128 BGP_TIMER_OFF (peer->t_asorig);
129 BGP_TIMER_OFF (peer->t_routeadv);
130 break;
132 case OpenSent:
133 /* OpenSent status. */
134 BGP_TIMER_OFF (peer->t_start);
135 BGP_TIMER_OFF (peer->t_connect);
136 if (peer->v_holdtime != 0)
138 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
139 peer->v_holdtime);
141 else
143 BGP_TIMER_OFF (peer->t_holdtime);
145 BGP_TIMER_OFF (peer->t_keepalive);
146 BGP_TIMER_OFF (peer->t_asorig);
147 BGP_TIMER_OFF (peer->t_routeadv);
148 break;
150 case OpenConfirm:
151 /* OpenConfirm status. */
152 BGP_TIMER_OFF (peer->t_start);
153 BGP_TIMER_OFF (peer->t_connect);
155 /* If the negotiated Hold Time value is zero, then the Hold Time
156 timer and KeepAlive timers are not started. */
157 if (peer->v_holdtime == 0)
159 BGP_TIMER_OFF (peer->t_holdtime);
160 BGP_TIMER_OFF (peer->t_keepalive);
162 else
164 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
165 peer->v_holdtime);
166 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
167 peer->v_keepalive);
169 BGP_TIMER_OFF (peer->t_asorig);
170 BGP_TIMER_OFF (peer->t_routeadv);
171 break;
173 case Established:
174 /* In Established status start and connect timer is turned
175 off. */
176 BGP_TIMER_OFF (peer->t_start);
177 BGP_TIMER_OFF (peer->t_connect);
179 /* Same as OpenConfirm, if holdtime is zero then both holdtime
180 and keepalive must be turned off. */
181 if (peer->v_holdtime == 0)
183 BGP_TIMER_OFF (peer->t_holdtime);
184 BGP_TIMER_OFF (peer->t_keepalive);
186 else
188 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
189 peer->v_holdtime);
190 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
191 peer->v_keepalive);
193 BGP_TIMER_OFF (peer->t_asorig);
194 BGP_TIMER_OFF (peer->t_routeadv);
195 break;
199 /* BGP start timer. This function set BGP_Start event to thread value
200 and process event. */
201 static int
202 bgp_start_timer (struct thread *thread)
204 struct peer *peer;
206 peer = THREAD_ARG (thread);
207 peer->t_start = NULL;
209 if (BGP_DEBUG (fsm, FSM))
210 zlog (peer->log, LOG_DEBUG,
211 "%s [FSM] Timer (start timer expire).", peer->host);
213 THREAD_VAL (thread) = BGP_Start;
214 bgp_event (thread);
216 return 0;
219 /* BGP connect retry timer. */
220 static int
221 bgp_connect_timer (struct thread *thread)
223 struct peer *peer;
225 peer = THREAD_ARG (thread);
226 peer->t_connect = NULL;
228 if (BGP_DEBUG (fsm, FSM))
229 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
230 peer->host);
232 THREAD_VAL (thread) = ConnectRetry_timer_expired;
233 bgp_event (thread);
235 return 0;
238 /* BGP holdtime timer. */
239 static int
240 bgp_holdtime_timer (struct thread *thread)
242 struct peer *peer;
244 peer = THREAD_ARG (thread);
245 peer->t_holdtime = NULL;
247 if (BGP_DEBUG (fsm, FSM))
248 zlog (peer->log, LOG_DEBUG,
249 "%s [FSM] Timer (holdtime timer expire)",
250 peer->host);
252 THREAD_VAL (thread) = Hold_Timer_expired;
253 bgp_event (thread);
255 return 0;
258 /* BGP keepalive fire ! */
259 static int
260 bgp_keepalive_timer (struct thread *thread)
262 struct peer *peer;
264 peer = THREAD_ARG (thread);
265 peer->t_keepalive = NULL;
267 if (BGP_DEBUG (fsm, FSM))
268 zlog (peer->log, LOG_DEBUG,
269 "%s [FSM] Timer (keepalive timer expire)",
270 peer->host);
272 THREAD_VAL (thread) = KeepAlive_timer_expired;
273 bgp_event (thread);
275 return 0;
278 /* Reset bgp update timer */
279 static void
280 bgp_uptime_reset (struct peer *peer)
282 peer->uptime = time (NULL);
285 /* Administrative BGP peer stop event. */
287 bgp_stop (struct peer *peer)
289 /* Need of clear of peer. */
290 bgp_route_clear (peer);
291 bgp_uptime_reset (peer);
293 /* Stop read and write threads when exists. */
294 BGP_READ_OFF (peer->t_read);
295 BGP_WRITE_OFF (peer->t_write);
297 /* Stream reset. */
298 peer->packet_size = 0;
299 if (peer->ibuf)
300 stream_reset (peer->ibuf);
302 /* Stop all timers. */
303 BGP_TIMER_OFF (peer->t_start);
304 BGP_TIMER_OFF (peer->t_connect);
305 BGP_TIMER_OFF (peer->t_holdtime);
306 BGP_TIMER_OFF (peer->t_keepalive);
307 BGP_TIMER_OFF (peer->t_asorig);
308 BGP_TIMER_OFF (peer->t_routeadv);
310 /* Delete all existing events of the peer. */
311 BGP_EVENT_DELETE (peer);
313 /* Clear output buffer. */
314 stream_fifo_free (peer->obuf);
316 /* Close of file descriptor. */
317 if (peer->fd >= 0)
319 close (peer->fd);
320 peer->fd = -1;
323 /* Connection information. */
324 if (peer->su_local)
326 XFREE (MTYPE_TMP, peer->su_local);
327 peer->su_local = NULL;
330 if (peer->su_remote)
332 XFREE (MTYPE_TMP, peer->su_remote);
333 peer->su_remote = NULL;
336 /* Clear remote router-id. */
337 peer->remote_id.s_addr = 0;
339 /* Reset all negotiated variables */
340 peer->afc_nego[AFI_IP][SAFI_UNICAST] = 0;
341 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = 0;
342 peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] = 0;
343 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = 0;
344 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = 0;
345 peer->afc_adv[AFI_IP][SAFI_UNICAST] = 0;
346 peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 0;
347 peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 0;
348 peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 0;
349 peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 0;
350 peer->afc_recv[AFI_IP][SAFI_UNICAST] = 0;
351 peer->afc_recv[AFI_IP][SAFI_MULTICAST] = 0;
352 peer->afc_recv[AFI_IP][SAFI_MPLS_VPN] = 0;
353 peer->afc_recv[AFI_IP6][SAFI_UNICAST] = 0;
354 peer->afc_recv[AFI_IP6][SAFI_MULTICAST] = 0;
356 /* Reset route refresh flag. */
357 peer->refresh_adv = 0;
358 peer->refresh_nego_old = 0;
359 peer->refresh_nego_new = 0;
361 /* Reset keepalive and holdtime */
362 if (peer->config & PEER_CONFIG_TIMER)
364 peer->v_keepalive = peer->keepalive;
365 peer->v_holdtime = peer->holdtime;
367 else
369 peer->v_keepalive = peer->global_keepalive;
370 peer->v_holdtime = peer->global_holdtime;
373 /* Increment Dropped count. */
374 if (peer->status == Established)
376 peer->dropped++;
377 fsm_change_status (peer, Idle);
378 #ifdef HAVE_SNMP
379 bgpTrapBackwardTransition (peer);
380 #endif /* HAVE_SNMP */
383 peer->update_time = 0;
385 return 0;
388 /* BGP peer is stoped by the error. */
390 bgp_stop_with_error (struct peer *peer)
392 /* Double start timer. */
393 peer->v_start *= 2;
395 /* Overflow check. */
396 if (peer->v_start >= (60 * 2))
397 peer->v_start = (60 * 2);
399 bgp_stop (peer);
401 return 0;
404 /* TCP connection open. Next we send open message to remote peer. And
405 add read thread for reading open message. */
407 bgp_connect_success (struct peer *peer)
409 if (peer->fd < 0)
411 zlog_err ("bgp_connect_success peer's fd is negative value %d",
412 peer->fd);
413 return -1;
415 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
417 /* bgp_getsockname (peer); */
419 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
420 bgp_open_send (peer);
422 return 0;
425 /* TCP connect fail */
427 bgp_connect_fail (struct peer *peer)
429 bgp_stop (peer);
430 return 0;
433 /* This function is the first starting point of all BGP connection. It
434 try to connect to remote peer with non-blocking IO. */
436 bgp_start (struct peer *peer)
438 int status;
440 /* If the peer is passive mode, force to move to Active mode. */
441 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
443 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
444 return 0;
447 status = bgp_connect (peer);
449 switch (status)
451 case connect_error:
452 if (BGP_DEBUG (fsm, FSM))
453 plog_info (peer->log, "%s [FSM] Connect error", peer->host);
454 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
455 break;
456 case connect_success:
457 if (BGP_DEBUG (fsm, FSM))
458 plog_info (peer->log, "%s [FSM] Connect immediately success",
459 peer->host);
460 BGP_EVENT_ADD (peer, TCP_connection_open);
461 break;
462 case connect_in_progress:
463 /* To check nonblocking connect, we wait until socket is
464 readable or writable. */
465 if (BGP_DEBUG (fsm, FSM))
466 plog_info (peer->log, "%s [FSM] Non blocking connect waiting result",
467 peer->host);
468 if (peer->fd < 0)
470 zlog_err ("bgp_start peer's fd is negative value %d",
471 peer->fd);
472 return -1;
474 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
475 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
476 break;
478 return 0;
481 /* Connect retry timer is expired when the peer status is Connect. */
483 bgp_reconnect (struct peer *peer)
485 bgp_stop (peer);
486 bgp_start (peer);
487 return 0;
491 fsm_open (struct peer *peer)
493 /* Send keepalive and make keepalive timer */
494 bgp_keepalive_send (peer);
496 /* Reset holdtimer value. */
497 BGP_TIMER_OFF (peer->t_holdtime);
499 return 0;
502 /* Called after event occured, this function change status and reset
503 read/write and timer thread. */
504 void
505 fsm_change_status (struct peer *peer, int status)
507 bgp_dump_state (peer, peer->status, status);
509 /* Preserve old status and change into new status. */
510 peer->ostatus = peer->status;
511 peer->status = status;
514 /* Keepalive send to peer. */
516 fsm_keepalive_expire (struct peer *peer)
518 bgp_keepalive_send (peer);
519 return 0;
522 /* Hold timer expire. This is error of BGP connection. So cut the
523 peer and change to Idle status. */
525 fsm_holdtime_expire (struct peer *peer)
527 if (BGP_DEBUG (fsm, FSM))
528 zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
530 /* Send notify to remote peer. */
531 bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
533 /* Sweep if it is temporary peer. */
534 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
536 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
537 peer_delete (peer);
538 return -1;
541 return 0;
544 /* Status goes to Established. Send keepalive packet then make first
545 update information. */
547 bgp_establish (struct peer *peer)
549 struct bgp_notify *notify;
551 /* Reset capability open status flag. */
552 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
553 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
555 /* Clear last notification data. */
556 notify = &peer->notify;
557 if (notify->data)
558 XFREE (MTYPE_TMP, notify->data);
559 memset (notify, 0, sizeof (struct bgp_notify));
561 /* Clear start timer value to default. */
562 peer->v_start = BGP_INIT_START_TIMER;
564 /* Increment established count. */
565 peer->established++;
566 fsm_change_status (peer, Established);
567 #ifdef HAVE_SNMP
568 bgpTrapEstablished (peer);
569 #endif /* HAVE_SNMP */
571 /* Reset uptime, send keepalive, send current table. */
572 bgp_uptime_reset (peer);
574 if (peer->v_keepalive)
575 bgp_keepalive_send (peer);
577 bgp_announce_table (peer);
579 return 0;
582 /* Keepalive packet is received. */
584 fsm_keepalive (struct peer *peer)
586 /* peer count update */
587 peer->keepalive_in++;
589 BGP_TIMER_OFF (peer->t_holdtime);
590 return 0;
593 /* Update packet is received. */
595 fsm_update (struct peer *peer)
597 BGP_TIMER_OFF (peer->t_holdtime);
598 return 0;
601 /* This is empty event. */
603 bgp_ignore (struct peer *peer)
605 if (BGP_DEBUG (fsm, FSM))
606 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
607 return 0;
610 /* Finite State Machine structure */
611 struct {
612 int (*func) ();
613 int next_state;
614 } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
617 /* Idle state: In Idle state, all events other than BGP_Start is
618 ignored. With BGP_Start event, finite state machine calls
619 bgp_start(). */
620 {bgp_start, Connect}, /* BGP_Start */
621 {bgp_stop, Idle}, /* BGP_Stop */
622 {bgp_stop, Idle}, /* TCP_connection_open */
623 {bgp_stop, Idle}, /* TCP_connection_closed */
624 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
625 {bgp_stop, Idle}, /* TCP_fatal_error */
626 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
627 {bgp_ignore, Idle}, /* Hold_Timer_expired */
628 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
629 {bgp_ignore, Idle}, /* Receive_OPEN_message */
630 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
631 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
632 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
635 /* Connect */
636 {bgp_ignore, Connect}, /* BGP_Start */
637 {bgp_stop, Idle}, /* BGP_Stop */
638 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
639 {bgp_stop, Idle}, /* TCP_connection_closed */
640 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
641 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
642 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
643 {bgp_ignore, Idle}, /* Hold_Timer_expired */
644 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
645 {bgp_ignore, Idle}, /* Receive_OPEN_message */
646 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
647 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
648 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
651 /* Active, */
652 {bgp_ignore, Active}, /* BGP_Start */
653 {bgp_stop, Idle}, /* BGP_Stop */
654 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
655 {bgp_stop, Idle}, /* TCP_connection_closed */
656 {bgp_ignore, Active}, /* TCP_connection_open_failed */
657 {bgp_ignore, Idle}, /* TCP_fatal_error */
658 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
659 {bgp_ignore, Idle}, /* Hold_Timer_expired */
660 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
661 {bgp_ignore, Idle}, /* Receive_OPEN_message */
662 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
663 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
664 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
667 /* OpenSent, */
668 {bgp_ignore, OpenSent}, /* BGP_Start */
669 {bgp_stop, Idle}, /* BGP_Stop */
670 {bgp_stop, Idle}, /* TCP_connection_open */
671 {bgp_stop, Active}, /* TCP_connection_closed */
672 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
673 {bgp_stop, Idle}, /* TCP_fatal_error */
674 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
675 {fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
676 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
677 {fsm_open, OpenConfirm}, /* Receive_OPEN_message */
678 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
679 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
680 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
683 /* OpenConfirm, */
684 {bgp_ignore, OpenConfirm}, /* BGP_Start */
685 {bgp_stop, Idle}, /* BGP_Stop */
686 {bgp_stop, Idle}, /* TCP_connection_open */
687 {bgp_stop, Idle}, /* TCP_connection_closed */
688 {bgp_stop, Idle}, /* TCP_connection_open_failed */
689 {bgp_stop, Idle}, /* TCP_fatal_error */
690 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
691 {fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
692 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
693 {bgp_ignore, Idle}, /* Receive_OPEN_message */
694 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
695 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
696 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
699 /* Established, */
700 {bgp_ignore, Established}, /* BGP_Start */
701 {bgp_stop, Idle}, /* BGP_Stop */
702 {bgp_stop, Idle}, /* TCP_connection_open */
703 {bgp_stop, Idle}, /* TCP_connection_closed */
704 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
705 {bgp_stop, Idle}, /* TCP_fatal_error */
706 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
707 {fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
708 {fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
709 {bgp_stop, Idle}, /* Receive_OPEN_message */
710 {fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
711 {fsm_update, Established}, /* Receive_UPDATE_message */
712 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
716 static char *bgp_event_str[] =
718 NULL,
719 "BGP_Start",
720 "BGP_Stop",
721 "TCP_connection_open",
722 "TCP_connection_closed",
723 "TCP_connection_open_failed",
724 "TCP_fatal_error",
725 "ConnectRetry_timer_expired",
726 "Hold_Timer_expired",
727 "KeepAlive_timer_expired",
728 "Receive_OPEN_message",
729 "Receive_KEEPALIVE_message",
730 "Receive_UPDATE_message",
731 "Receive_NOTIFICATION_message"
734 /* Execute event process. */
736 bgp_event (struct thread *thread)
738 int ret;
739 int event;
740 int next;
741 struct peer *peer;
743 peer = THREAD_ARG (thread);
744 event = THREAD_VAL (thread);
746 /* Logging this event. */
747 next = FSM [peer->status -1][event - 1].next_state;
749 if (BGP_DEBUG (fsm, FSM))
750 plog_info (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
751 bgp_event_str[event],
752 LOOKUP (bgp_status_msg, peer->status),
753 LOOKUP (bgp_status_msg, next));
754 if (BGP_DEBUG (normal, NORMAL)
755 && strcmp (LOOKUP (bgp_status_msg, peer->status), LOOKUP (bgp_status_msg, next)))
756 zlog_info ("%s went from %s to %s",
757 peer->host,
758 LOOKUP (bgp_status_msg, peer->status),
759 LOOKUP (bgp_status_msg, next));
761 /* Call function. */
762 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
764 /* When function do not want proceed next job return -1. */
765 if (ret < 0)
766 return ret;
768 /* If status is changed. */
769 if (next != peer->status)
770 fsm_change_status (peer, next);
772 /* Make sure timer is set. */
773 bgp_timer_set (peer);
775 return 0;