1 /* BGP-4 Finite State Machine
2 From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
5 This file is part of GNU Zebra.
7 GNU Zebra is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 GNU Zebra is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Zebra; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "sockunion.h"
32 #include "workqueue.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_packet.h"
43 #include "bgpd/bgp_network.h"
44 #include "bgpd/bgp_route.h"
45 #include "bgpd/bgp_dump.h"
46 #include "bgpd/bgp_open.h"
47 #include "bgpd/bgp_advertise.h"
48 #include "bgpd/bgp_updgrp.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_memory.h"
53 DEFINE_HOOK(peer_backward_transition
, (struct peer
*peer
), (peer
))
54 DEFINE_HOOK(peer_established
, (struct peer
*peer
), (peer
))
56 /* Definition of display strings corresponding to FSM events. This should be
57 * kept consistent with the events defined in bgpd.h
59 static const char *bgp_event_str
[] =
64 "TCP_connection_open",
65 "TCP_connection_closed",
66 "TCP_connection_open_failed",
68 "ConnectRetry_timer_expired",
70 "KeepAlive_timer_expired",
71 "Receive_OPEN_message",
72 "Receive_KEEPALIVE_message",
73 "Receive_UPDATE_message",
74 "Receive_NOTIFICATION_message",
78 /* BGP FSM (finite state machine) has three types of functions. Type
79 one is thread functions. Type two is event functions. Type three
80 is FSM functions. Timer functions are set by bgp_timer_set
83 /* BGP event function. */
84 int bgp_event (struct thread
*);
86 /* BGP thread functions. */
87 static int bgp_start_timer (struct thread
*);
88 static int bgp_connect_timer (struct thread
*);
89 static int bgp_holdtime_timer (struct thread
*);
90 static int bgp_keepalive_timer (struct thread
*);
92 /* BGP FSM functions. */
93 static int bgp_start (struct peer
*);
96 peer_xfer_stats (struct peer
*peer_dst
, struct peer
*peer_src
)
98 /* Copy stats over. These are only the pre-established state stats */
99 peer_dst
->open_in
+= peer_src
->open_in
;
100 peer_dst
->open_out
+= peer_src
->open_out
;
101 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
102 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
103 peer_dst
->notify_in
+= peer_src
->notify_in
;
104 peer_dst
->notify_out
+= peer_src
->notify_out
;
105 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
106 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
110 peer_xfer_conn(struct peer
*from_peer
)
117 unsigned char last_evt
, last_maj_evt
;
119 assert(from_peer
!= NULL
);
121 peer
= from_peer
->doppelganger
;
123 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
126 if (bgp_debug_neighbor_events(peer
))
127 zlog_debug ("%s: peer transfer %p fd %d -> %p fd %d)", from_peer
->host
,
128 from_peer
, from_peer
->fd
, peer
, peer
->fd
);
130 BGP_WRITE_OFF(peer
->t_write
);
131 BGP_READ_OFF(peer
->t_read
);
132 BGP_WRITE_OFF(from_peer
->t_write
);
133 BGP_READ_OFF(from_peer
->t_read
);
135 BGP_TIMER_OFF(peer
->t_routeadv
);
136 BGP_TIMER_OFF(from_peer
->t_routeadv
);
139 peer
->fd
= from_peer
->fd
;
141 stream_reset(peer
->ibuf
);
142 stream_fifo_clean(peer
->obuf
);
143 stream_fifo_clean(from_peer
->obuf
);
145 peer
->as
= from_peer
->as
;
146 peer
->v_holdtime
= from_peer
->v_holdtime
;
147 peer
->v_keepalive
= from_peer
->v_keepalive
;
148 peer
->routeadv
= from_peer
->routeadv
;
149 peer
->v_routeadv
= from_peer
->v_routeadv
;
150 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
151 peer
->cap
= from_peer
->cap
;
152 status
= peer
->status
;
153 pstatus
= peer
->ostatus
;
154 last_evt
= peer
->last_event
;
155 last_maj_evt
= peer
->last_major_event
;
156 peer
->status
= from_peer
->status
;
157 peer
->ostatus
= from_peer
->ostatus
;
158 peer
->last_event
= from_peer
->last_event
;
159 peer
->last_major_event
= from_peer
->last_major_event
;
160 from_peer
->status
= status
;
161 from_peer
->ostatus
= pstatus
;
162 from_peer
->last_event
= last_evt
;
163 from_peer
->last_major_event
= last_maj_evt
;
164 peer
->remote_id
= from_peer
->remote_id
;
166 if (from_peer
->hostname
!= NULL
)
170 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
171 peer
->hostname
= NULL
;
174 peer
->hostname
= from_peer
->hostname
;
175 from_peer
->hostname
= NULL
;
178 if (from_peer
->domainname
!= NULL
)
180 if (peer
->domainname
)
182 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
183 peer
->domainname
= NULL
;
186 peer
->domainname
= from_peer
->domainname
;
187 from_peer
->domainname
= NULL
;
190 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
191 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
193 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
194 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
195 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
196 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
197 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
198 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
199 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
202 if (bgp_getsockname(peer
) < 0)
204 zlog_err ("%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
205 (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
206 peer
->host
, peer
->fd
, from_peer
->fd
);
211 if (from_peer
->status
> Active
)
213 if (bgp_getsockname(from_peer
) < 0)
215 zlog_err ("%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
216 (CHECK_FLAG (from_peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
217 from_peer
->host
, from_peer
->fd
, peer
->fd
);
223 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
224 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
227 peer_xfer_stats(peer
, from_peer
);
232 /* Hook function called after bgp event is occered. And vty's
233 neighbor command invoke this function after making neighbor
236 bgp_timer_set (struct peer
*peer
)
238 switch (peer
->status
)
241 /* First entry point of peer's finite state machine. In Idle
242 status start timer is on unless peer is shutdown or peer is
243 inactive. All other timer must be turned off */
244 if (BGP_PEER_START_SUPPRESSED (peer
) || ! peer_active (peer
))
246 BGP_TIMER_OFF (peer
->t_start
);
250 BGP_TIMER_ON (peer
->t_start
, bgp_start_timer
,
253 BGP_TIMER_OFF (peer
->t_connect
);
254 BGP_TIMER_OFF (peer
->t_holdtime
);
255 BGP_TIMER_OFF (peer
->t_keepalive
);
256 BGP_TIMER_OFF (peer
->t_routeadv
);
260 /* After start timer is expired, the peer moves to Connect
261 status. Make sure start timer is off and connect timer is
263 BGP_TIMER_OFF (peer
->t_start
);
264 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
265 BGP_TIMER_OFF (peer
->t_holdtime
);
266 BGP_TIMER_OFF (peer
->t_keepalive
);
267 BGP_TIMER_OFF (peer
->t_routeadv
);
271 /* Active is waiting connection from remote peer. And if
272 connect timer is expired, change status to Connect. */
273 BGP_TIMER_OFF (peer
->t_start
);
274 /* If peer is passive mode, do not set connect timer. */
275 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
)
276 || CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
278 BGP_TIMER_OFF (peer
->t_connect
);
282 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
284 BGP_TIMER_OFF (peer
->t_holdtime
);
285 BGP_TIMER_OFF (peer
->t_keepalive
);
286 BGP_TIMER_OFF (peer
->t_routeadv
);
290 /* OpenSent status. */
291 BGP_TIMER_OFF (peer
->t_start
);
292 BGP_TIMER_OFF (peer
->t_connect
);
293 if (peer
->v_holdtime
!= 0)
295 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
300 BGP_TIMER_OFF (peer
->t_holdtime
);
302 BGP_TIMER_OFF (peer
->t_keepalive
);
303 BGP_TIMER_OFF (peer
->t_routeadv
);
307 /* OpenConfirm status. */
308 BGP_TIMER_OFF (peer
->t_start
);
309 BGP_TIMER_OFF (peer
->t_connect
);
311 /* If the negotiated Hold Time value is zero, then the Hold Time
312 timer and KeepAlive timers are not started. */
313 if (peer
->v_holdtime
== 0)
315 BGP_TIMER_OFF (peer
->t_holdtime
);
316 BGP_TIMER_OFF (peer
->t_keepalive
);
320 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
322 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
325 BGP_TIMER_OFF (peer
->t_routeadv
);
329 /* In Established status start and connect timer is turned
331 BGP_TIMER_OFF (peer
->t_start
);
332 BGP_TIMER_OFF (peer
->t_connect
);
334 /* Same as OpenConfirm, if holdtime is zero then both holdtime
335 and keepalive must be turned off. */
336 if (peer
->v_holdtime
== 0)
338 BGP_TIMER_OFF (peer
->t_holdtime
);
339 BGP_TIMER_OFF (peer
->t_keepalive
);
343 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
345 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
350 BGP_TIMER_OFF (peer
->t_gr_restart
);
351 BGP_TIMER_OFF (peer
->t_gr_stale
);
352 BGP_TIMER_OFF (peer
->t_pmax_restart
);
354 BGP_TIMER_OFF (peer
->t_start
);
355 BGP_TIMER_OFF (peer
->t_connect
);
356 BGP_TIMER_OFF (peer
->t_holdtime
);
357 BGP_TIMER_OFF (peer
->t_keepalive
);
358 BGP_TIMER_OFF (peer
->t_routeadv
);
363 /* BGP start timer. This function set BGP_Start event to thread value
364 and process event. */
366 bgp_start_timer (struct thread
*thread
)
370 peer
= THREAD_ARG (thread
);
371 peer
->t_start
= NULL
;
373 if (bgp_debug_neighbor_events(peer
))
374 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
376 THREAD_VAL (thread
) = BGP_Start
;
377 bgp_event (thread
); /* bgp_event unlocks peer */
382 /* BGP connect retry timer. */
384 bgp_connect_timer (struct thread
*thread
)
389 peer
= THREAD_ARG (thread
);
390 peer
->t_connect
= NULL
;
392 if (bgp_debug_neighbor_events(peer
))
393 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
395 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
402 THREAD_VAL (thread
) = ConnectRetry_timer_expired
;
403 bgp_event (thread
); /* bgp_event unlocks peer */
410 /* BGP holdtime timer. */
412 bgp_holdtime_timer (struct thread
*thread
)
416 peer
= THREAD_ARG (thread
);
417 peer
->t_holdtime
= NULL
;
419 if (bgp_debug_neighbor_events(peer
))
420 zlog_debug ("%s [FSM] Timer (holdtime timer expire)", peer
->host
);
422 THREAD_VAL (thread
) = Hold_Timer_expired
;
423 bgp_event (thread
); /* bgp_event unlocks peer */
428 /* BGP keepalive fire ! */
430 bgp_keepalive_timer (struct thread
*thread
)
434 peer
= THREAD_ARG (thread
);
435 peer
->t_keepalive
= NULL
;
437 if (bgp_debug_neighbor_events(peer
))
438 zlog_debug ("%s [FSM] Timer (keepalive timer expire)", peer
->host
);
440 THREAD_VAL (thread
) = KeepAlive_timer_expired
;
441 bgp_event (thread
); /* bgp_event unlocks peer */
447 bgp_routeadv_timer (struct thread
*thread
)
451 peer
= THREAD_ARG (thread
);
452 peer
->t_routeadv
= NULL
;
454 if (bgp_debug_neighbor_events(peer
))
455 zlog_debug ("%s [FSM] Timer (routeadv timer expire)", peer
->host
);
457 peer
->synctime
= bgp_clock ();
459 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
461 /* MRAI timer will be started again when FIFO is built, no need to
467 /* BGP Peer Down Cause */
468 const char *peer_down_str
[] =
474 "Cluster ID changed",
475 "Confederation identifier changed",
476 "Confederation peer changed",
477 "RR client config change",
478 "RS client config change",
479 "Update source change",
480 "Address family activated",
483 "BGP Notification received",
484 "BGP Notification send",
485 "Peer closed the session",
487 "Peer-group add member",
488 "Peer-group delete member",
489 "Capability changed",
490 "Passive config change",
491 "Multihop config change",
492 "NSF peer closed the session",
493 "Intf peering v6only config change",
496 "Neighbor address lost"
500 bgp_graceful_restart_timer_expire (struct thread
*thread
)
506 peer
= THREAD_ARG (thread
);
507 peer
->t_gr_restart
= NULL
;
509 /* NSF delete stale route */
510 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
511 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
512 if (peer
->nsf
[afi
][safi
])
513 bgp_clear_stale_route (peer
, afi
, safi
);
515 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
516 BGP_TIMER_OFF (peer
->t_gr_stale
);
518 if (bgp_debug_neighbor_events(peer
))
520 zlog_debug ("%s graceful restart timer expired", peer
->host
);
521 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
524 bgp_timer_set (peer
);
530 bgp_graceful_stale_timer_expire (struct thread
*thread
)
536 peer
= THREAD_ARG (thread
);
537 peer
->t_gr_stale
= NULL
;
539 if (bgp_debug_neighbor_events(peer
))
540 zlog_debug ("%s graceful restart stalepath timer expired", peer
->host
);
542 /* NSF delete stale route */
543 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
544 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
545 if (peer
->nsf
[afi
][safi
])
546 bgp_clear_stale_route (peer
, afi
, safi
);
552 bgp_update_delay_applicable (struct bgp
*bgp
)
554 /* update_delay_over flag should be reset (set to 0) for any new
555 applicability of the update-delay during BGP process lifetime.
556 And it should be set after an occurence of the update-delay is over)*/
557 if (!bgp
->update_delay_over
)
564 bgp_update_delay_active (struct bgp
*bgp
)
566 if (bgp
->t_update_delay
)
573 bgp_update_delay_configured (struct bgp
*bgp
)
575 if (bgp
->v_update_delay
)
581 /* Do the post-processing needed when bgp comes out of the read-only mode
582 on ending the update delay. */
584 bgp_update_delay_end (struct bgp
*bgp
)
586 THREAD_TIMER_OFF (bgp
->t_update_delay
);
587 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
589 /* Reset update-delay related state */
590 bgp
->update_delay_over
= 1;
591 bgp
->established
= 0;
592 bgp
->restarted_peers
= 0;
593 bgp
->implicit_eors
= 0;
594 bgp
->explicit_eors
= 0;
596 quagga_timestamp(3, bgp
->update_delay_end_time
,
597 sizeof(bgp
->update_delay_end_time
));
600 * Add an end-of-initial-update marker to the main process queues so that
601 * the route advertisement timer for the peers can be started. Also set
602 * the zebra and peer update hold flags. These flags are used to achieve
603 * three stages in the update-delay post processing:
604 * 1. Finish best-path selection for all the prefixes held on the queues.
605 * (routes in BGP are updated, and peers sync queues are populated too)
606 * 2. As the eoiu mark is reached in the bgp process routine, ship all the
607 * routes to zebra. With that zebra should see updates from BGP close
609 * 3. Unblock the peer update writes. With that peer update packing with
610 * the prefixes should be at its maximum.
612 bgp_add_eoiu_mark(bgp
);
613 bgp
->main_zebra_update_hold
= 1;
614 bgp
->main_peers_update_hold
= 1;
616 /* Resume the queue processing. This should trigger the event that would take
617 care of processing any work that was queued during the read-only mode. */
618 work_queue_unplug(bm
->process_main_queue
);
625 bgp_start_routeadv (struct bgp
*bgp
)
627 struct listnode
*node
, *nnode
;
630 zlog_info("bgp_start_routeadv(), update hold status %d",
631 bgp
->main_peers_update_hold
);
633 if (bgp
->main_peers_update_hold
)
636 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
637 sizeof(bgp
->update_delay_peers_resume_time
));
639 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
641 if (peer
->status
!= Established
)
643 BGP_TIMER_OFF(peer
->t_routeadv
);
644 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
652 bgp_adjust_routeadv (struct peer
*peer
)
654 time_t nowtime
= bgp_clock();
656 unsigned long remain
;
658 /* Bypass checks for special case of MRAI being 0 */
659 if (peer
->v_routeadv
== 0)
661 /* Stop existing timer, just in case it is running for a different
662 * duration and schedule write thread immediately.
664 if (peer
->t_routeadv
)
665 BGP_TIMER_OFF(peer
->t_routeadv
);
667 peer
->synctime
= bgp_clock ();
668 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
675 * If the last update was written more than MRAI back, expire the timer
676 * instantly so that we can send the update out sooner.
678 * <------- MRAI --------->
679 * |-----------------|-----------------------|
680 * <------------- m ------------>
689 diff
= difftime(nowtime
, peer
->last_update
);
690 if (diff
> (double) peer
->v_routeadv
)
692 BGP_TIMER_OFF(peer
->t_routeadv
);
693 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
699 * - Find when to expire the MRAI timer.
700 * If MRAI timer is not active, assume we can start it now.
702 * <------- MRAI --------->
703 * |------------|-----------------------|
704 * <-------- m ----------><----- r ----->
713 if (peer
->t_routeadv
)
714 remain
= thread_timer_remain_second(peer
->t_routeadv
);
716 remain
= peer
->v_routeadv
;
717 diff
= peer
->v_routeadv
- diff
;
718 if (diff
<= (double) remain
)
720 BGP_TIMER_OFF(peer
->t_routeadv
);
721 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
726 bgp_maxmed_onstartup_applicable (struct bgp
*bgp
)
728 if (!bgp
->maxmed_onstartup_over
)
735 bgp_maxmed_onstartup_configured (struct bgp
*bgp
)
737 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
744 bgp_maxmed_onstartup_active (struct bgp
*bgp
)
746 if (bgp
->t_maxmed_onstartup
)
753 bgp_maxmed_update (struct bgp
*bgp
)
755 u_char maxmed_active
;
756 u_int32_t maxmed_value
;
758 if (bgp
->v_maxmed_admin
)
761 maxmed_value
= bgp
->maxmed_admin_value
;
763 else if (bgp
->t_maxmed_onstartup
)
766 maxmed_value
= bgp
->maxmed_onstartup_value
;
771 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
774 if (bgp
->maxmed_active
!= maxmed_active
||
775 bgp
->maxmed_value
!= maxmed_value
)
777 bgp
->maxmed_active
= maxmed_active
;
778 bgp
->maxmed_value
= maxmed_value
;
780 update_group_announce(bgp
);
784 /* The maxmed onstartup timer expiry callback. */
786 bgp_maxmed_onstartup_timer (struct thread
*thread
)
790 zlog_info ("Max med on startup ended - timer expired.");
792 bgp
= THREAD_ARG (thread
);
793 THREAD_TIMER_OFF (bgp
->t_maxmed_onstartup
);
794 bgp
->maxmed_onstartup_over
= 1;
796 bgp_maxmed_update(bgp
);
802 bgp_maxmed_onstartup_begin (struct bgp
*bgp
)
804 /* Applicable only once in the process lifetime on the startup */
805 if (bgp
->maxmed_onstartup_over
)
808 zlog_info ("Begin maxmed onstartup mode - timer %d seconds",
809 bgp
->v_maxmed_onstartup
);
811 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
812 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
814 if (!bgp
->v_maxmed_admin
)
816 bgp
->maxmed_active
= 1;
817 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
820 /* Route announce to all peers should happen after this in bgp_establish() */
824 bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
826 if (peer
->status
== Established
&& !peer
->bgp
->established
)
828 bgp_maxmed_onstartup_begin(peer
->bgp
);
832 /* The update delay timer expiry callback. */
834 bgp_update_delay_timer (struct thread
*thread
)
838 zlog_info ("Update delay ended - timer expired.");
840 bgp
= THREAD_ARG (thread
);
841 THREAD_TIMER_OFF (bgp
->t_update_delay
);
842 bgp_update_delay_end(bgp
);
847 /* The establish wait timer expiry callback. */
849 bgp_establish_wait_timer (struct thread
*thread
)
853 zlog_info ("Establish wait - timer expired.");
855 bgp
= THREAD_ARG (thread
);
856 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
857 bgp_check_update_delay(bgp
);
862 /* Steps to begin the update delay:
863 - initialize queues if needed
864 - stop the queue processing
867 bgp_update_delay_begin (struct bgp
*bgp
)
869 struct listnode
*node
, *nnode
;
872 /* Stop the processing of queued work. Enqueue shall continue */
873 work_queue_plug(bm
->process_main_queue
);
875 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
876 peer
->update_delay_over
= 0;
878 /* Start the update-delay timer */
879 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
880 bgp
->v_update_delay
, &bgp
->t_update_delay
);
882 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
883 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
884 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
886 quagga_timestamp(3, bgp
->update_delay_begin_time
,
887 sizeof(bgp
->update_delay_begin_time
));
891 bgp_update_delay_process_status_change(struct peer
*peer
)
893 if (peer
->status
== Established
)
895 if (!peer
->bgp
->established
++)
897 bgp_update_delay_begin(peer
->bgp
);
898 zlog_info ("Begin read-only mode - update-delay timer %d seconds",
899 peer
->bgp
->v_update_delay
);
901 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
902 bgp_update_restarted_peers(peer
);
904 if (peer
->ostatus
== Established
&& bgp_update_delay_active(peer
->bgp
))
906 /* Adjust the update-delay state to account for this flap.
907 NOTE: Intentionally skipping adjusting implicit_eors or explicit_eors
908 counters. Extra sanity check in bgp_check_update_delay() should
909 be enough to take care of any additive discrepancy in bgp eor
911 peer
->bgp
->established
--;
912 peer
->update_delay_over
= 0;
916 /* Called after event occured, this function change status and reset
917 read/write and timer thread. */
919 bgp_fsm_change_status (struct peer
*peer
, int status
)
922 bgp_dump_state (peer
, peer
->status
, status
);
924 /* Transition into Clearing or Deleted must /always/ clear all routes..
925 * (and must do so before actually changing into Deleted..
927 if (status
>= Clearing
)
929 bgp_clear_route_all (peer
);
931 /* If no route was queued for the clear-node processing, generate the
932 * completion event here. This is needed because if there are no routes
933 * to trigger the background clear-node thread, the event won't get
934 * generated and the peer would be stuck in Clearing. Note that this
935 * event is for the peer and helps the peer transition out of Clearing
936 * state; it should not be generated per (AFI,SAFI). The event is
937 * directly posted here without calling clear_node_complete() as we
938 * shouldn't do an extra unlock. This event will get processed after
939 * the state change that happens below, so peer will be in Clearing
942 if (!work_queue_is_scheduled (peer
->clear_node_queue
))
943 BGP_EVENT_ADD (peer
, Clearing_Completed
);
946 /* Preserve old status and change into new status. */
947 peer
->ostatus
= peer
->status
;
948 peer
->status
= status
;
950 /* Save event that caused status change. */
951 peer
->last_major_event
= peer
->cur_event
;
953 if (status
== Established
)
954 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
956 /* If max-med processing is applicable, do the necessary. */
957 if (status
== Established
)
959 if (bgp_maxmed_onstartup_configured(peer
->bgp
) &&
960 bgp_maxmed_onstartup_applicable(peer
->bgp
))
961 bgp_maxmed_onstartup_process_status_change(peer
);
963 peer
->bgp
->maxmed_onstartup_over
= 1;
966 /* If update-delay processing is applicable, do the necessary. */
967 if (bgp_update_delay_configured(peer
->bgp
) &&
968 bgp_update_delay_applicable(peer
->bgp
))
969 bgp_update_delay_process_status_change(peer
);
971 if (bgp_debug_neighbor_events(peer
))
972 zlog_debug ("%s went from %s to %s",
974 LOOKUP (bgp_status_msg
, peer
->ostatus
),
975 LOOKUP (bgp_status_msg
, peer
->status
));
978 /* Flush the event queue and ensure the peer is shut down */
980 bgp_clearing_completed (struct peer
*peer
)
982 int rc
= bgp_stop(peer
);
985 BGP_EVENT_FLUSH (peer
);
990 /* Administrative BGP peer stop event. */
991 /* May be called multiple times for the same peer */
993 bgp_stop (struct peer
*peer
)
997 char orf_name
[BUFSIZ
];
1000 if (peer_dynamic_neighbor(peer
) &&
1001 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1003 if (bgp_debug_neighbor_events(peer
))
1004 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1009 /* Can't do this in Clearing; events are used for state transitions */
1010 if (peer
->status
!= Clearing
)
1012 /* Delete all existing events of the peer */
1013 BGP_EVENT_FLUSH (peer
);
1016 /* Increment Dropped count. */
1017 if (peer
->status
== Established
)
1021 /* bgp log-neighbor-changes of neighbor Down */
1022 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1023 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer
->host
,
1024 peer_down_str
[(int) peer
->last_reset
]);
1026 /* graceful restart */
1027 if (peer
->t_gr_stale
)
1029 BGP_TIMER_OFF (peer
->t_gr_stale
);
1030 if (bgp_debug_neighbor_events(peer
))
1031 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1033 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
1035 if (bgp_debug_neighbor_events(peer
))
1037 zlog_debug ("%s graceful restart timer started for %d sec",
1038 peer
->host
, peer
->v_gr_restart
);
1039 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
1040 peer
->host
, peer
->bgp
->stalepath_time
);
1042 BGP_TIMER_ON (peer
->t_gr_restart
, bgp_graceful_restart_timer_expire
,
1043 peer
->v_gr_restart
);
1044 BGP_TIMER_ON (peer
->t_gr_stale
, bgp_graceful_stale_timer_expire
,
1045 peer
->bgp
->stalepath_time
);
1049 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1051 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1052 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
1053 peer
->nsf
[afi
][safi
] = 0;
1056 /* set last reset time */
1057 peer
->resettime
= peer
->uptime
= bgp_clock ();
1059 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1060 zlog_debug ("%s remove from all update group", peer
->host
);
1061 update_group_remove_peer_afs(peer
);
1063 hook_call(peer_backward_transition
, peer
);
1065 /* Reset peer synctime */
1068 bgp_bfd_deregister_peer(peer
);
1071 /* Stop read and write threads when exists. */
1072 BGP_READ_OFF (peer
->t_read
);
1073 BGP_WRITE_OFF (peer
->t_write
);
1075 /* Stop all timers. */
1076 BGP_TIMER_OFF (peer
->t_start
);
1077 BGP_TIMER_OFF (peer
->t_connect
);
1078 BGP_TIMER_OFF (peer
->t_holdtime
);
1079 BGP_TIMER_OFF (peer
->t_keepalive
);
1080 BGP_TIMER_OFF (peer
->t_routeadv
);
1083 peer
->packet_size
= 0;
1085 /* Clear input and output buffer. */
1087 stream_reset (peer
->ibuf
);
1089 stream_reset (peer
->work
);
1091 stream_fifo_clean (peer
->obuf
);
1093 /* Close of file descriptor. */
1100 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1101 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1103 /* Reset all negotiated variables */
1104 peer
->afc_nego
[afi
][safi
] = 0;
1105 peer
->afc_adv
[afi
][safi
] = 0;
1106 peer
->afc_recv
[afi
][safi
] = 0;
1108 /* peer address family capability flags*/
1109 peer
->af_cap
[afi
][safi
] = 0;
1111 /* peer address family status flags*/
1112 peer
->af_sflags
[afi
][safi
] = 0;
1114 /* Received ORF prefix-filter */
1115 peer
->orf_plist
[afi
][safi
] = NULL
;
1117 if ((peer
->status
== OpenConfirm
) || (peer
->status
== Established
)) {
1118 /* ORF received prefix-filter pnt */
1119 sprintf (orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1120 prefix_bgp_orf_remove_all (afi
, orf_name
);
1124 /* Reset keepalive and holdtime */
1125 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1127 peer
->v_keepalive
= peer
->keepalive
;
1128 peer
->v_holdtime
= peer
->holdtime
;
1132 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1133 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1136 peer
->update_time
= 0;
1138 /* Until we are sure that there is no problem about prefix count
1139 this should be commented out.*/
1141 /* Reset prefix count */
1142 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1143 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1144 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1145 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1146 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1147 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1148 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1151 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
) &&
1152 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1159 bgp_peer_conf_if_to_su_update(peer
);
1165 /* BGP peer is stoped by the error. */
1167 bgp_stop_with_error (struct peer
*peer
)
1169 /* Double start timer. */
1172 /* Overflow check. */
1173 if (peer
->v_start
>= (60 * 2))
1174 peer
->v_start
= (60 * 2);
1176 if (peer_dynamic_neighbor(peer
))
1178 if (bgp_debug_neighbor_events(peer
))
1179 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1184 return(bgp_stop (peer
));
1188 /* something went wrong, send notify and tear down */
1190 bgp_stop_with_notify (struct peer
*peer
, u_char code
, u_char sub_code
)
1192 /* Send notify to remote peer */
1193 bgp_notify_send (peer
, code
, sub_code
);
1195 if (peer_dynamic_neighbor(peer
))
1197 if (bgp_debug_neighbor_events(peer
))
1198 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1203 /* Clear start timer value to default. */
1204 peer
->v_start
= BGP_INIT_START_TIMER
;
1206 return(bgp_stop(peer
));
1210 /* TCP connection open. Next we send open message to remote peer. And
1211 add read thread for reading open message. */
1213 bgp_connect_success (struct peer
*peer
)
1217 zlog_err ("bgp_connect_success peer's fd is negative value %d",
1223 if (bgp_getsockname (peer
) < 0)
1225 zlog_err ("%s: bgp_getsockname(): failed for peer %s, fd %d", __FUNCTION__
,
1226 peer
->host
, peer
->fd
);
1227 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0); /* internal error */
1231 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1233 if (bgp_debug_neighbor_events(peer
))
1235 char buf1
[SU_ADDRSTRLEN
];
1237 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1238 zlog_debug ("%s open active, local address %s", peer
->host
,
1239 sockunion2str (peer
->su_local
, buf1
, SU_ADDRSTRLEN
));
1241 zlog_debug ("%s passive open", peer
->host
);
1244 bgp_open_send (peer
);
1249 /* TCP connect fail */
1251 bgp_connect_fail (struct peer
*peer
)
1253 if (peer_dynamic_neighbor(peer
))
1255 if (bgp_debug_neighbor_events(peer
))
1256 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1261 return (bgp_stop (peer
));
1264 /* This function is the first starting point of all BGP connection. It
1265 try to connect to remote peer with non-blocking IO. */
1267 bgp_start (struct peer
*peer
)
1272 bgp_peer_conf_if_to_su_update(peer
);
1274 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1276 if (bgp_debug_neighbor_events(peer
))
1277 zlog_debug ("%s [FSM] Unable to get neighbor's IP address, waiting...",
1282 if (BGP_PEER_START_SUPPRESSED (peer
))
1284 if (bgp_debug_neighbor_events(peer
))
1285 zlog_err ("%s [FSM] Trying to start suppressed peer"
1286 " - this is never supposed to happen!", peer
->host
);
1290 /* Scrub some information that might be left over from a previous,
1293 /* Connection information. */
1296 sockunion_free (peer
->su_local
);
1297 peer
->su_local
= NULL
;
1300 if (peer
->su_remote
)
1302 sockunion_free (peer
->su_remote
);
1303 peer
->su_remote
= NULL
;
1306 /* Clear remote router-id. */
1307 peer
->remote_id
.s_addr
= 0;
1309 /* Clear peer capability flag. */
1312 /* If the peer is passive mode, force to move to Active mode. */
1313 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
))
1315 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1319 /* Register to be notified on peer up */
1320 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1 &&
1321 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1322 && ! bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1327 if (!bgp_find_or_add_nexthop(peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
1328 NULL
, peer
, connected
))
1330 #if defined (HAVE_CUMULUS)
1331 if (bgp_debug_neighbor_events(peer
))
1332 zlog_debug ("%s [FSM] Waiting for NHT", peer
->host
);
1334 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1339 status
= bgp_connect (peer
);
1344 if (bgp_debug_neighbor_events(peer
))
1345 zlog_debug ("%s [FSM] Connect error", peer
->host
);
1346 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1348 case connect_success
:
1349 if (bgp_debug_neighbor_events(peer
))
1350 zlog_debug ("%s [FSM] Connect immediately success, fd %d",
1351 peer
->host
, peer
->fd
);
1352 BGP_EVENT_ADD (peer
, TCP_connection_open
);
1354 case connect_in_progress
:
1355 /* To check nonblocking connect, we wait until socket is
1356 readable or writable. */
1357 if (bgp_debug_neighbor_events(peer
))
1358 zlog_debug ("%s [FSM] Non blocking connect waiting result, fd %d",
1359 peer
->host
, peer
->fd
);
1362 zlog_err ("bgp_start peer's fd is negative value %d",
1366 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1367 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1373 /* Connect retry timer is expired when the peer status is Connect. */
1375 bgp_reconnect (struct peer
*peer
)
1377 if (bgp_stop (peer
) < 0)
1385 bgp_fsm_open (struct peer
*peer
)
1387 /* Send keepalive and make keepalive timer */
1388 bgp_keepalive_send (peer
);
1390 /* Reset holdtimer value. */
1391 BGP_TIMER_OFF (peer
->t_holdtime
);
1396 /* Keepalive send to peer. */
1398 bgp_fsm_keepalive_expire (struct peer
*peer
)
1400 bgp_keepalive_send (peer
);
1404 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1405 peer and change to Idle status. */
1407 bgp_fsm_event_error (struct peer
*peer
)
1409 zlog_err ("%s [FSM] unexpected packet received in state %s",
1410 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1412 return bgp_stop_with_notify (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1415 /* Hold timer expire. This is error of BGP connection. So cut the
1416 peer and change to Idle status. */
1418 bgp_fsm_holdtime_expire (struct peer
*peer
)
1420 if (bgp_debug_neighbor_events(peer
))
1421 zlog_debug ("%s [FSM] Hold timer expire", peer
->host
);
1423 return bgp_stop_with_notify (peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1426 /* Status goes to Established. Send keepalive packet then make first
1427 update information. */
1429 bgp_establish (struct peer
*peer
)
1433 int nsf_af_count
= 0;
1437 other
= peer
->doppelganger
;
1438 peer
= peer_xfer_conn(peer
);
1441 zlog_err ("%%Neighbor failed in xfer_conn");
1446 ret
= 1; /* bgp_establish specific code when xfer_conn happens. */
1448 /* Reset capability open status flag. */
1449 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1450 SET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1452 /* Clear start timer value to default. */
1453 peer
->v_start
= BGP_INIT_START_TIMER
;
1455 /* Increment established count. */
1456 peer
->established
++;
1457 bgp_fsm_change_status (peer
, Established
);
1459 /* bgp log-neighbor-changes of neighbor Up */
1460 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1461 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer
->host
);
1463 /* assign update-group/subgroup */
1464 update_group_adjust_peer_afs(peer
);
1466 /* graceful restart */
1467 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1468 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1469 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
1471 if (peer
->afc_nego
[afi
][safi
]
1472 && CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_ADV
)
1473 && CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_RCV
))
1475 if (peer
->nsf
[afi
][safi
]
1476 && ! CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1477 bgp_clear_stale_route (peer
, afi
, safi
);
1479 peer
->nsf
[afi
][safi
] = 1;
1484 if (peer
->nsf
[afi
][safi
])
1485 bgp_clear_stale_route (peer
, afi
, safi
);
1486 peer
->nsf
[afi
][safi
] = 0;
1491 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1494 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1495 if (peer
->t_gr_stale
)
1497 BGP_TIMER_OFF (peer
->t_gr_stale
);
1498 if (bgp_debug_neighbor_events(peer
))
1499 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1503 if (peer
->t_gr_restart
)
1505 BGP_TIMER_OFF (peer
->t_gr_restart
);
1506 if (bgp_debug_neighbor_events(peer
))
1507 zlog_debug ("%s graceful restart timer stopped", peer
->host
);
1510 hook_call(peer_established
, peer
);
1512 /* Reset uptime, send keepalive, send current table. */
1513 peer
->uptime
= bgp_clock ();
1515 /* Send route-refresh when ORF is enabled */
1516 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1517 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1518 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
))
1520 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_RCV
))
1521 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX
,
1522 REFRESH_IMMEDIATE
, 0);
1523 else if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1524 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
1525 REFRESH_IMMEDIATE
, 0);
1528 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1529 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1530 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1531 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
))
1532 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
1533 || CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1534 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1536 bgp_announce_peer (peer
);
1538 /* Start the route advertisement timer to send updates to the peer - if BGP
1539 * is not in read-only mode. If it is, the timer will be started at the end
1540 * of read-only mode.
1542 if (!bgp_update_delay_active(peer
->bgp
))
1544 BGP_TIMER_OFF(peer
->t_routeadv
);
1545 BGP_TIMER_ON (peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1548 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
))
1550 if (bgp_debug_neighbor_events(peer
))
1551 zlog_debug("[Event] Deleting stub connection for peer %s", peer
->host
);
1553 if (peer
->doppelganger
->status
> Active
)
1554 bgp_notify_send (peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1555 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1557 peer_delete(peer
->doppelganger
);
1560 bgp_bfd_register_peer(peer
);
1564 /* Keepalive packet is received. */
1566 bgp_fsm_keepalive (struct peer
*peer
)
1568 bgp_update_implicit_eors(peer
);
1570 /* peer count update */
1571 peer
->keepalive_in
++;
1573 BGP_TIMER_OFF (peer
->t_holdtime
);
1577 /* Update packet is received. */
1579 bgp_fsm_update (struct peer
*peer
)
1581 BGP_TIMER_OFF (peer
->t_holdtime
);
1585 /* This is empty event. */
1587 bgp_ignore (struct peer
*peer
)
1589 zlog_err ("%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1590 peer
->host
, bgp_event_str
[peer
->cur_event
],
1591 LOOKUP (bgp_status_msg
, peer
->status
),
1592 bgp_event_str
[peer
->last_event
],
1593 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1597 /* This is to handle unexpected events.. */
1599 bgp_fsm_exeption (struct peer
*peer
)
1601 zlog_err ("%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1602 peer
->host
, bgp_event_str
[peer
->cur_event
],
1603 LOOKUP (bgp_status_msg
, peer
->status
),
1604 bgp_event_str
[peer
->last_event
],
1605 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1606 return(bgp_stop (peer
));
1610 bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1615 switch (peer
->status
)
1619 BGP_EVENT_ADD(peer
, BGP_Start
);
1624 BGP_TIMER_OFF(peer
->t_connect
);
1625 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1631 BGP_TIMER_OFF(peer
->t_connect
);
1632 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1638 if (!valid
&& (peer
->gtsm_hops
== 1))
1639 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1648 /* Finite State Machine structure */
1649 static const struct {
1650 int (*func
) (struct peer
*);
1652 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] =
1655 /* Idle state: In Idle state, all events other than BGP_Start is
1656 ignored. With BGP_Start event, finite state machine calls
1658 {bgp_start
, Connect
}, /* BGP_Start */
1659 {bgp_stop
, Idle
}, /* BGP_Stop */
1660 {bgp_stop
, Idle
}, /* TCP_connection_open */
1661 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1662 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1663 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1664 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1665 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1666 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1667 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1668 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1669 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1670 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1671 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1675 {bgp_ignore
, Connect
}, /* BGP_Start */
1676 {bgp_stop
, Idle
}, /* BGP_Stop */
1677 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1678 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1679 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1680 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1681 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1682 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1683 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1684 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1685 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1686 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1687 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1688 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1692 {bgp_ignore
, Active
}, /* BGP_Start */
1693 {bgp_stop
, Idle
}, /* BGP_Stop */
1694 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1695 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1696 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1697 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1698 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1699 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1700 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1701 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1702 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1703 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1704 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1705 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1709 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1710 {bgp_stop
, Idle
}, /* BGP_Stop */
1711 {bgp_stop
, Active
}, /* TCP_connection_open */
1712 {bgp_stop
, Active
}, /* TCP_connection_closed */
1713 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1714 {bgp_stop
, Active
}, /* TCP_fatal_error */
1715 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1716 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1717 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1718 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1719 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1720 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1721 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1722 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1726 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1727 {bgp_stop
, Idle
}, /* BGP_Stop */
1728 {bgp_stop
, Idle
}, /* TCP_connection_open */
1729 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1730 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1731 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1732 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1733 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1734 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1735 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1736 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1737 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1738 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1739 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1743 {bgp_ignore
, Established
}, /* BGP_Start */
1744 {bgp_stop
, Clearing
}, /* BGP_Stop */
1745 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1746 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1747 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1748 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1749 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1750 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1751 {bgp_fsm_keepalive_expire
, Established
}, /* KeepAlive_timer_expired */
1752 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1753 {bgp_fsm_keepalive
, Established
}, /* Receive_KEEPALIVE_message */
1754 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1755 {bgp_stop_with_error
, Clearing
}, /* Receive_NOTIFICATION_message */
1756 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1760 {bgp_ignore
, Clearing
}, /* BGP_Start */
1761 {bgp_stop
, Clearing
}, /* BGP_Stop */
1762 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1763 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1764 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1765 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1766 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1767 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1768 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1769 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1770 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1771 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1772 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1773 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1777 {bgp_ignore
, Deleted
}, /* BGP_Start */
1778 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1779 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1780 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1781 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1782 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1783 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1784 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1785 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1786 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1787 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1788 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1789 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1790 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1794 /* Execute event process. */
1796 bgp_event (struct thread
*thread
)
1802 peer
= THREAD_ARG (thread
);
1803 event
= THREAD_VAL (thread
);
1805 ret
= bgp_event_update(peer
, event
);
1811 bgp_event_update (struct peer
*peer
, int event
)
1816 int passive_conn
= 0;
1819 other
= peer
->doppelganger
;
1820 passive_conn
= (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1821 dyn_nbr
= peer_dynamic_neighbor(peer
);
1823 /* Logging this event. */
1824 next
= FSM
[peer
->status
-1][event
- 1].next_state
;
1826 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1827 zlog_debug ("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1828 bgp_event_str
[event
],
1829 LOOKUP (bgp_status_msg
, peer
->status
),
1830 LOOKUP (bgp_status_msg
, next
), peer
->fd
);
1832 peer
->last_event
= peer
->cur_event
;
1833 peer
->cur_event
= event
;
1835 /* Call function. */
1836 if (FSM
[peer
->status
-1][event
- 1].func
)
1837 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1839 /* When function do not want proceed next job return -1. */
1842 if (ret
== 1 && next
== Established
)
1844 /* The case when doppelganger swap accurred in bgp_establish.
1845 Update the peer pointer accordingly */
1849 /* If status is changed. */
1850 if (next
!= peer
->status
)
1851 bgp_fsm_change_status (peer
, next
);
1853 /* Make sure timer is set. */
1854 bgp_timer_set (peer
);
1857 else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
)
1859 /* If we got a return value of -1, that means there was an error, restart
1860 * the FSM. If the peer structure was deleted
1862 zlog_err ("%s [FSM] Failure handling event %s in state %s, "
1863 "prior events %s, %s, fd %d",
1864 peer
->host
, bgp_event_str
[peer
->cur_event
],
1865 LOOKUP (bgp_status_msg
, peer
->status
),
1866 bgp_event_str
[peer
->last_event
],
1867 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1869 bgp_fsm_change_status(peer
, Idle
);
1870 bgp_timer_set(peer
);