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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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"
52 #include "bgpd/bgp_keepalives.h"
54 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
55 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
57 /* Definition of display strings corresponding to FSM events. This should be
58 * kept consistent with the events defined in bgpd.h
60 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
*);
91 /* BGP FSM functions. */
92 static int bgp_start(struct peer
*);
94 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
96 /* Copy stats over. These are only the pre-established state stats */
97 peer_dst
->open_in
+= peer_src
->open_in
;
98 peer_dst
->open_out
+= peer_src
->open_out
;
99 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
100 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
101 peer_dst
->notify_in
+= peer_src
->notify_in
;
102 peer_dst
->notify_out
+= peer_src
->notify_out
;
103 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
104 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
107 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
114 unsigned char last_evt
, last_maj_evt
;
116 assert(from_peer
!= NULL
);
118 peer
= from_peer
->doppelganger
;
120 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
123 if (bgp_debug_neighbor_events(peer
))
124 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
125 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
128 peer_writes_off(peer
);
129 BGP_READ_OFF(peer
->t_read
);
130 peer_writes_off(from_peer
);
131 BGP_READ_OFF(from_peer
->t_read
);
133 BGP_TIMER_OFF(peer
->t_routeadv
);
134 BGP_TIMER_OFF(from_peer
->t_routeadv
);
137 peer
->fd
= from_peer
->fd
;
139 stream_reset(peer
->ibuf
);
141 pthread_mutex_lock(&peer
->obuf_mtx
);
143 stream_fifo_clean(peer
->obuf
);
145 pthread_mutex_unlock(&peer
->obuf_mtx
);
147 pthread_mutex_lock(&from_peer
->obuf_mtx
);
149 stream_fifo_clean(from_peer
->obuf
);
151 pthread_mutex_unlock(&from_peer
->obuf_mtx
);
153 peer
->as
= from_peer
->as
;
154 peer
->v_holdtime
= from_peer
->v_holdtime
;
155 peer
->v_keepalive
= from_peer
->v_keepalive
;
156 peer
->routeadv
= from_peer
->routeadv
;
157 peer
->v_routeadv
= from_peer
->v_routeadv
;
158 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
159 peer
->cap
= from_peer
->cap
;
160 status
= peer
->status
;
161 pstatus
= peer
->ostatus
;
162 last_evt
= peer
->last_event
;
163 last_maj_evt
= peer
->last_major_event
;
164 peer
->status
= from_peer
->status
;
165 peer
->ostatus
= from_peer
->ostatus
;
166 peer
->last_event
= from_peer
->last_event
;
167 peer
->last_major_event
= from_peer
->last_major_event
;
168 from_peer
->status
= status
;
169 from_peer
->ostatus
= pstatus
;
170 from_peer
->last_event
= last_evt
;
171 from_peer
->last_major_event
= last_maj_evt
;
172 peer
->remote_id
= from_peer
->remote_id
;
174 if (from_peer
->hostname
!= NULL
) {
175 if (peer
->hostname
) {
176 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
177 peer
->hostname
= NULL
;
180 peer
->hostname
= from_peer
->hostname
;
181 from_peer
->hostname
= NULL
;
184 if (from_peer
->domainname
!= NULL
) {
185 if (peer
->domainname
) {
186 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
187 peer
->domainname
= NULL
;
190 peer
->domainname
= from_peer
->domainname
;
191 from_peer
->domainname
= NULL
;
194 FOREACH_AFI_SAFI (afi
, safi
) {
195 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
196 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
197 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
198 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
199 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
200 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
201 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
204 if (bgp_getsockname(peer
) < 0) {
206 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
207 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
210 peer
->host
, peer
->fd
, from_peer
->fd
);
215 if (from_peer
->status
> Active
) {
216 if (bgp_getsockname(from_peer
) < 0) {
218 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
219 (CHECK_FLAG(from_peer
->sflags
,
220 PEER_STATUS_ACCEPT_PEER
)
223 from_peer
->host
, from_peer
->fd
, peer
->fd
);
229 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
230 peer_writes_on(peer
);
233 peer_xfer_stats(peer
, from_peer
);
238 /* Hook function called after bgp event is occered. And vty's
239 neighbor command invoke this function after making neighbor
241 void bgp_timer_set(struct peer
*peer
)
243 switch (peer
->status
) {
245 /* First entry point of peer's finite state machine. In Idle
246 status start timer is on unless peer is shutdown or peer is
247 inactive. All other timer must be turned off */
248 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)) {
249 BGP_TIMER_OFF(peer
->t_start
);
251 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
254 BGP_TIMER_OFF(peer
->t_connect
);
255 BGP_TIMER_OFF(peer
->t_holdtime
);
256 peer_keepalives_off(peer
);
257 BGP_TIMER_OFF(peer
->t_routeadv
);
261 /* After start timer is expired, the peer moves to Connect
262 status. Make sure start timer is off and connect timer is
264 BGP_TIMER_OFF(peer
->t_start
);
265 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
267 BGP_TIMER_OFF(peer
->t_holdtime
);
268 peer_keepalives_off(peer
);
269 BGP_TIMER_OFF(peer
->t_routeadv
);
273 /* Active is waiting connection from remote peer. And if
274 connect timer is expired, change status to Connect. */
275 BGP_TIMER_OFF(peer
->t_start
);
276 /* If peer is passive mode, do not set connect timer. */
277 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
278 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
279 BGP_TIMER_OFF(peer
->t_connect
);
281 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
284 BGP_TIMER_OFF(peer
->t_holdtime
);
285 peer_keepalives_off(peer
);
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) {
294 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
297 BGP_TIMER_OFF(peer
->t_holdtime
);
299 peer_keepalives_off(peer
);
300 BGP_TIMER_OFF(peer
->t_routeadv
);
304 /* OpenConfirm status. */
305 BGP_TIMER_OFF(peer
->t_start
);
306 BGP_TIMER_OFF(peer
->t_connect
);
308 /* If the negotiated Hold Time value is zero, then the Hold Time
309 timer and KeepAlive timers are not started. */
310 if (peer
->v_holdtime
== 0) {
311 BGP_TIMER_OFF(peer
->t_holdtime
);
312 peer_keepalives_off(peer
);
314 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
316 peer_keepalives_on(peer
);
318 BGP_TIMER_OFF(peer
->t_routeadv
);
322 /* In Established status start and connect timer is turned
324 BGP_TIMER_OFF(peer
->t_start
);
325 BGP_TIMER_OFF(peer
->t_connect
);
327 /* Same as OpenConfirm, if holdtime is zero then both holdtime
328 and keepalive must be turned off. */
329 if (peer
->v_holdtime
== 0) {
330 BGP_TIMER_OFF(peer
->t_holdtime
);
331 peer_keepalives_off(peer
);
333 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
335 peer_keepalives_on(peer
);
339 BGP_TIMER_OFF(peer
->t_gr_restart
);
340 BGP_TIMER_OFF(peer
->t_gr_stale
);
341 BGP_TIMER_OFF(peer
->t_pmax_restart
);
344 BGP_TIMER_OFF(peer
->t_start
);
345 BGP_TIMER_OFF(peer
->t_connect
);
346 BGP_TIMER_OFF(peer
->t_holdtime
);
347 peer_keepalives_off(peer
);
348 BGP_TIMER_OFF(peer
->t_routeadv
);
353 /* BGP start timer. This function set BGP_Start event to thread value
354 and process event. */
355 static int bgp_start_timer(struct thread
*thread
)
359 peer
= THREAD_ARG(thread
);
360 peer
->t_start
= NULL
;
362 if (bgp_debug_neighbor_events(peer
))
363 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
365 THREAD_VAL(thread
) = BGP_Start
;
366 bgp_event(thread
); /* bgp_event unlocks peer */
371 /* BGP connect retry timer. */
372 static int bgp_connect_timer(struct thread
*thread
)
377 peer
= THREAD_ARG(thread
);
378 peer
->t_connect
= NULL
;
380 if (bgp_debug_neighbor_events(peer
))
381 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
383 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
387 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
388 bgp_event(thread
); /* bgp_event unlocks peer */
395 /* BGP holdtime timer. */
396 static int bgp_holdtime_timer(struct thread
*thread
)
400 peer
= THREAD_ARG(thread
);
401 peer
->t_holdtime
= NULL
;
403 if (bgp_debug_neighbor_events(peer
))
404 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
407 THREAD_VAL(thread
) = Hold_Timer_expired
;
408 bgp_event(thread
); /* bgp_event unlocks peer */
413 int bgp_routeadv_timer(struct thread
*thread
)
417 peer
= THREAD_ARG(thread
);
418 peer
->t_routeadv
= NULL
;
420 if (bgp_debug_neighbor_events(peer
))
421 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
424 peer
->synctime
= bgp_clock();
426 /* MRAI timer will be started again when FIFO is built, no need to
432 /* BGP Peer Down Cause */
433 const char *peer_down_str
[] = {"",
437 "Cluster ID changed",
438 "Confederation identifier changed",
439 "Confederation peer changed",
440 "RR client config change",
441 "RS client config change",
442 "Update source change",
443 "Address family activated",
446 "BGP Notification received",
447 "BGP Notification send",
448 "Peer closed the session",
450 "Peer-group add member",
451 "Peer-group delete member",
452 "Capability changed",
453 "Passive config change",
454 "Multihop config change",
455 "NSF peer closed the session",
456 "Intf peering v6only config change",
459 "Neighbor address lost"};
461 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
467 peer
= THREAD_ARG(thread
);
468 peer
->t_gr_restart
= NULL
;
470 /* NSF delete stale route */
471 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
472 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
473 if (peer
->nsf
[afi
][safi
])
474 bgp_clear_stale_route(peer
, afi
, safi
);
476 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
477 BGP_TIMER_OFF(peer
->t_gr_stale
);
479 if (bgp_debug_neighbor_events(peer
)) {
480 zlog_debug("%s graceful restart timer expired", peer
->host
);
481 zlog_debug("%s graceful restart stalepath timer stopped",
490 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
496 peer
= THREAD_ARG(thread
);
497 peer
->t_gr_stale
= NULL
;
499 if (bgp_debug_neighbor_events(peer
))
500 zlog_debug("%s graceful restart stalepath timer expired",
503 /* NSF delete stale route */
504 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
505 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
506 if (peer
->nsf
[afi
][safi
])
507 bgp_clear_stale_route(peer
, afi
, safi
);
512 static int bgp_update_delay_applicable(struct bgp
*bgp
)
514 /* update_delay_over flag should be reset (set to 0) for any new
515 applicability of the update-delay during BGP process lifetime.
516 And it should be set after an occurence of the update-delay is
518 if (!bgp
->update_delay_over
)
524 int bgp_update_delay_active(struct bgp
*bgp
)
526 if (bgp
->t_update_delay
)
532 int bgp_update_delay_configured(struct bgp
*bgp
)
534 if (bgp
->v_update_delay
)
540 /* Do the post-processing needed when bgp comes out of the read-only mode
541 on ending the update delay. */
542 void bgp_update_delay_end(struct bgp
*bgp
)
544 THREAD_TIMER_OFF(bgp
->t_update_delay
);
545 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
547 /* Reset update-delay related state */
548 bgp
->update_delay_over
= 1;
549 bgp
->established
= 0;
550 bgp
->restarted_peers
= 0;
551 bgp
->implicit_eors
= 0;
552 bgp
->explicit_eors
= 0;
554 quagga_timestamp(3, bgp
->update_delay_end_time
,
555 sizeof(bgp
->update_delay_end_time
));
558 * Add an end-of-initial-update marker to the main process queues so
560 * the route advertisement timer for the peers can be started. Also set
561 * the zebra and peer update hold flags. These flags are used to achieve
562 * three stages in the update-delay post processing:
563 * 1. Finish best-path selection for all the prefixes held on the
565 * (routes in BGP are updated, and peers sync queues are populated
567 * 2. As the eoiu mark is reached in the bgp process routine, ship all
569 * routes to zebra. With that zebra should see updates from BGP
572 * 3. Unblock the peer update writes. With that peer update packing
574 * the prefixes should be at its maximum.
576 bgp_add_eoiu_mark(bgp
);
577 bgp
->main_zebra_update_hold
= 1;
578 bgp
->main_peers_update_hold
= 1;
580 /* Resume the queue processing. This should trigger the event that would
582 care of processing any work that was queued during the read-only
584 work_queue_unplug(bm
->process_main_queue
);
590 void bgp_start_routeadv(struct bgp
*bgp
)
592 struct listnode
*node
, *nnode
;
595 zlog_info("bgp_start_routeadv(), update hold status %d",
596 bgp
->main_peers_update_hold
);
598 if (bgp
->main_peers_update_hold
)
601 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
602 sizeof(bgp
->update_delay_peers_resume_time
));
604 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
605 if (peer
->status
!= Established
)
607 BGP_TIMER_OFF(peer
->t_routeadv
);
608 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
615 void bgp_adjust_routeadv(struct peer
*peer
)
617 time_t nowtime
= bgp_clock();
619 unsigned long remain
;
621 /* Bypass checks for special case of MRAI being 0 */
622 if (peer
->v_routeadv
== 0) {
623 /* Stop existing timer, just in case it is running for a
625 * duration and schedule write thread immediately.
627 if (peer
->t_routeadv
)
628 BGP_TIMER_OFF(peer
->t_routeadv
);
630 peer
->synctime
= bgp_clock();
637 * If the last update was written more than MRAI back, expire the timer
638 * instantly so that we can send the update out sooner.
640 * <------- MRAI --------->
641 * |-----------------|-----------------------|
642 * <------------- m ------------>
651 diff
= difftime(nowtime
, peer
->last_update
);
652 if (diff
> (double)peer
->v_routeadv
) {
653 BGP_TIMER_OFF(peer
->t_routeadv
);
654 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
660 * - Find when to expire the MRAI timer.
661 * If MRAI timer is not active, assume we can start it now.
663 * <------- MRAI --------->
664 * |------------|-----------------------|
665 * <-------- m ----------><----- r ----->
674 if (peer
->t_routeadv
)
675 remain
= thread_timer_remain_second(peer
->t_routeadv
);
677 remain
= peer
->v_routeadv
;
678 diff
= peer
->v_routeadv
- diff
;
679 if (diff
<= (double)remain
) {
680 BGP_TIMER_OFF(peer
->t_routeadv
);
681 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
685 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
687 if (!bgp
->maxmed_onstartup_over
)
693 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
695 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
701 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
703 if (bgp
->t_maxmed_onstartup
)
709 void bgp_maxmed_update(struct bgp
*bgp
)
711 u_char maxmed_active
;
712 u_int32_t maxmed_value
;
714 if (bgp
->v_maxmed_admin
) {
716 maxmed_value
= bgp
->maxmed_admin_value
;
717 } else if (bgp
->t_maxmed_onstartup
) {
719 maxmed_value
= bgp
->maxmed_onstartup_value
;
722 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
725 if (bgp
->maxmed_active
!= maxmed_active
726 || bgp
->maxmed_value
!= maxmed_value
) {
727 bgp
->maxmed_active
= maxmed_active
;
728 bgp
->maxmed_value
= maxmed_value
;
730 update_group_announce(bgp
);
734 /* The maxmed onstartup timer expiry callback. */
735 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
739 zlog_info("Max med on startup ended - timer expired.");
741 bgp
= THREAD_ARG(thread
);
742 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
743 bgp
->maxmed_onstartup_over
= 1;
745 bgp_maxmed_update(bgp
);
750 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
752 /* Applicable only once in the process lifetime on the startup */
753 if (bgp
->maxmed_onstartup_over
)
756 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
757 bgp
->v_maxmed_onstartup
);
759 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
760 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
762 if (!bgp
->v_maxmed_admin
) {
763 bgp
->maxmed_active
= 1;
764 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
767 /* Route announce to all peers should happen after this in
771 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
773 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
774 bgp_maxmed_onstartup_begin(peer
->bgp
);
778 /* The update delay timer expiry callback. */
779 static int bgp_update_delay_timer(struct thread
*thread
)
783 zlog_info("Update delay ended - timer expired.");
785 bgp
= THREAD_ARG(thread
);
786 THREAD_TIMER_OFF(bgp
->t_update_delay
);
787 bgp_update_delay_end(bgp
);
792 /* The establish wait timer expiry callback. */
793 static int bgp_establish_wait_timer(struct thread
*thread
)
797 zlog_info("Establish wait - timer expired.");
799 bgp
= THREAD_ARG(thread
);
800 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
801 bgp_check_update_delay(bgp
);
806 /* Steps to begin the update delay:
807 - initialize queues if needed
808 - stop the queue processing
810 static void bgp_update_delay_begin(struct bgp
*bgp
)
812 struct listnode
*node
, *nnode
;
815 /* Stop the processing of queued work. Enqueue shall continue */
816 work_queue_plug(bm
->process_main_queue
);
818 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
819 peer
->update_delay_over
= 0;
821 /* Start the update-delay timer */
822 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
823 bgp
->v_update_delay
, &bgp
->t_update_delay
);
825 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
826 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
827 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
829 quagga_timestamp(3, bgp
->update_delay_begin_time
,
830 sizeof(bgp
->update_delay_begin_time
));
833 static void bgp_update_delay_process_status_change(struct peer
*peer
)
835 if (peer
->status
== Established
) {
836 if (!peer
->bgp
->established
++) {
837 bgp_update_delay_begin(peer
->bgp
);
839 "Begin read-only mode - update-delay timer %d seconds",
840 peer
->bgp
->v_update_delay
);
842 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
843 bgp_update_restarted_peers(peer
);
845 if (peer
->ostatus
== Established
846 && bgp_update_delay_active(peer
->bgp
)) {
847 /* Adjust the update-delay state to account for this flap.
848 NOTE: Intentionally skipping adjusting implicit_eors or
850 counters. Extra sanity check in bgp_check_update_delay()
852 be enough to take care of any additive discrepancy in bgp eor
854 peer
->bgp
->established
--;
855 peer
->update_delay_over
= 0;
859 /* Called after event occured, this function change status and reset
860 read/write and timer thread. */
861 void bgp_fsm_change_status(struct peer
*peer
, int status
)
864 bgp_dump_state(peer
, peer
->status
, status
);
866 /* Transition into Clearing or Deleted must /always/ clear all routes..
867 * (and must do so before actually changing into Deleted..
869 if (status
>= Clearing
) {
870 bgp_clear_route_all(peer
);
872 /* If no route was queued for the clear-node processing,
874 * completion event here. This is needed because if there are no
876 * to trigger the background clear-node thread, the event won't
878 * generated and the peer would be stuck in Clearing. Note that
880 * event is for the peer and helps the peer transition out of
882 * state; it should not be generated per (AFI,SAFI). The event
884 * directly posted here without calling clear_node_complete() as
886 * shouldn't do an extra unlock. This event will get processed
888 * the state change that happens below, so peer will be in
892 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
893 BGP_EVENT_ADD(peer
, Clearing_Completed
);
896 /* Preserve old status and change into new status. */
897 peer
->ostatus
= peer
->status
;
898 peer
->status
= status
;
900 /* Save event that caused status change. */
901 peer
->last_major_event
= peer
->cur_event
;
903 if (status
== Established
)
904 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
906 /* If max-med processing is applicable, do the necessary. */
907 if (status
== Established
) {
908 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
909 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
910 bgp_maxmed_onstartup_process_status_change(peer
);
912 peer
->bgp
->maxmed_onstartup_over
= 1;
915 /* If update-delay processing is applicable, do the necessary. */
916 if (bgp_update_delay_configured(peer
->bgp
)
917 && bgp_update_delay_applicable(peer
->bgp
))
918 bgp_update_delay_process_status_change(peer
);
920 if (bgp_debug_neighbor_events(peer
))
921 zlog_debug("%s went from %s to %s", peer
->host
,
922 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
923 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
926 /* Flush the event queue and ensure the peer is shut down */
927 static int bgp_clearing_completed(struct peer
*peer
)
929 int rc
= bgp_stop(peer
);
932 BGP_EVENT_FLUSH(peer
);
937 /* Administrative BGP peer stop event. */
938 /* May be called multiple times for the same peer */
939 int bgp_stop(struct peer
*peer
)
943 char orf_name
[BUFSIZ
];
946 if (peer_dynamic_neighbor(peer
)
947 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
948 if (bgp_debug_neighbor_events(peer
))
949 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
954 /* Can't do this in Clearing; events are used for state transitions */
955 if (peer
->status
!= Clearing
) {
956 /* Delete all existing events of the peer */
957 BGP_EVENT_FLUSH(peer
);
960 /* Increment Dropped count. */
961 if (peer
->status
== Established
) {
964 /* bgp log-neighbor-changes of neighbor Down */
965 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
966 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
968 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
970 (peer
->hostname
) ? peer
->hostname
: "Unknown",
971 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
974 peer_down_str
[(int)peer
->last_reset
]);
977 /* graceful restart */
978 if (peer
->t_gr_stale
) {
979 BGP_TIMER_OFF(peer
->t_gr_stale
);
980 if (bgp_debug_neighbor_events(peer
))
982 "%s graceful restart stalepath timer stopped",
985 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
986 if (bgp_debug_neighbor_events(peer
)) {
988 "%s graceful restart timer started for %d sec",
989 peer
->host
, peer
->v_gr_restart
);
991 "%s graceful restart stalepath timer started for %d sec",
992 peer
->host
, peer
->bgp
->stalepath_time
);
994 BGP_TIMER_ON(peer
->t_gr_restart
,
995 bgp_graceful_restart_timer_expire
,
997 BGP_TIMER_ON(peer
->t_gr_stale
,
998 bgp_graceful_stale_timer_expire
,
999 peer
->bgp
->stalepath_time
);
1001 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1003 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1004 for (safi
= SAFI_UNICAST
;
1005 safi
<= SAFI_MPLS_VPN
; safi
++)
1006 peer
->nsf
[afi
][safi
] = 0;
1009 /* set last reset time */
1010 peer
->resettime
= peer
->uptime
= bgp_clock();
1012 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1013 zlog_debug("%s remove from all update group",
1015 update_group_remove_peer_afs(peer
);
1017 hook_call(peer_backward_transition
, peer
);
1019 /* Reset peer synctime */
1022 bgp_bfd_deregister_peer(peer
);
1025 /* Stop read and write threads when exists. */
1026 BGP_READ_OFF(peer
->t_read
);
1027 peer_writes_off(peer
);
1029 /* Stop all timers. */
1030 BGP_TIMER_OFF(peer
->t_start
);
1031 BGP_TIMER_OFF(peer
->t_connect
);
1032 BGP_TIMER_OFF(peer
->t_holdtime
);
1033 peer_keepalives_off(peer
);
1034 BGP_TIMER_OFF(peer
->t_routeadv
);
1037 peer
->packet_size
= 0;
1039 /* Clear input and output buffer. */
1041 stream_reset(peer
->ibuf
);
1043 stream_reset(peer
->work
);
1045 pthread_mutex_lock(&peer
->obuf_mtx
);
1048 stream_fifo_clean(peer
->obuf
);
1050 pthread_mutex_unlock(&peer
->obuf_mtx
);
1052 /* Close of file descriptor. */
1053 if (peer
->fd
>= 0) {
1058 FOREACH_AFI_SAFI (afi
, safi
) {
1059 /* Reset all negotiated variables */
1060 peer
->afc_nego
[afi
][safi
] = 0;
1061 peer
->afc_adv
[afi
][safi
] = 0;
1062 peer
->afc_recv
[afi
][safi
] = 0;
1064 /* peer address family capability flags*/
1065 peer
->af_cap
[afi
][safi
] = 0;
1067 /* peer address family status flags*/
1068 peer
->af_sflags
[afi
][safi
] = 0;
1070 /* Received ORF prefix-filter */
1071 peer
->orf_plist
[afi
][safi
] = NULL
;
1073 if ((peer
->status
== OpenConfirm
)
1074 || (peer
->status
== Established
)) {
1075 /* ORF received prefix-filter pnt */
1076 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1077 prefix_bgp_orf_remove_all(afi
, orf_name
);
1081 /* Reset keepalive and holdtime */
1082 if (PEER_OR_GROUP_TIMER_SET(peer
)) {
1083 peer
->v_keepalive
= peer
->keepalive
;
1084 peer
->v_holdtime
= peer
->holdtime
;
1086 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1087 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1090 peer
->update_time
= 0;
1092 /* Until we are sure that there is no problem about prefix count
1093 this should be commented out.*/
1095 /* Reset prefix count */
1096 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1097 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1098 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1099 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1100 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1101 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1102 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1105 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1106 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1110 bgp_peer_conf_if_to_su_update(peer
);
1116 /* BGP peer is stoped by the error. */
1117 static int bgp_stop_with_error(struct peer
*peer
)
1119 /* Double start timer. */
1122 /* Overflow check. */
1123 if (peer
->v_start
>= (60 * 2))
1124 peer
->v_start
= (60 * 2);
1126 if (peer_dynamic_neighbor(peer
)) {
1127 if (bgp_debug_neighbor_events(peer
))
1128 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1133 return (bgp_stop(peer
));
1137 /* something went wrong, send notify and tear down */
1138 static int bgp_stop_with_notify(struct peer
*peer
, u_char code
, u_char sub_code
)
1140 /* Send notify to remote peer */
1141 bgp_notify_send(peer
, code
, sub_code
);
1143 if (peer_dynamic_neighbor(peer
)) {
1144 if (bgp_debug_neighbor_events(peer
))
1145 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1150 /* Clear start timer value to default. */
1151 peer
->v_start
= BGP_INIT_START_TIMER
;
1153 return (bgp_stop(peer
));
1157 * Determines whether a TCP session has successfully established for a peer and
1158 * events as appropriate.
1160 * This function is called when setting up a new session. After connect() is
1161 * called on the peer's socket (in bgp_start()), the fd is passed to select()
1162 * to wait for connection success or failure. When select() returns, this
1163 * function is called to evaluate the result.
1165 static int bgp_connect_check(struct thread
*thread
)
1172 peer
= THREAD_ARG(thread
);
1174 /* Check file descriptor. */
1175 slen
= sizeof(status
);
1176 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1179 /* If getsockopt is fail, this is fatal error. */
1181 zlog_info("can't get sockopt for nonblocking connect");
1182 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1186 /* When status is 0 then TCP connection is established. */
1188 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1191 if (bgp_debug_neighbor_events(peer
))
1192 zlog_debug("%s [Event] Connect failed (%s)", peer
->host
,
1193 safe_strerror(errno
));
1194 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1199 /* TCP connection open. Next we send open message to remote peer. And
1200 add read thread for reading open message. */
1201 static int bgp_connect_success(struct peer
*peer
)
1204 zlog_err("bgp_connect_success peer's fd is negative value %d",
1210 peer_writes_on(peer
);
1212 if (bgp_getsockname(peer
) < 0) {
1213 zlog_err("%s: bgp_getsockname(): failed for peer %s, fd %d",
1214 __FUNCTION__
, peer
->host
, peer
->fd
);
1215 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1216 0); /* internal error */
1220 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
1222 if (bgp_debug_neighbor_events(peer
)) {
1223 char buf1
[SU_ADDRSTRLEN
];
1225 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1226 zlog_debug("%s open active, local address %s",
1228 sockunion2str(peer
->su_local
, buf1
,
1231 zlog_debug("%s passive open", peer
->host
);
1234 bgp_open_send(peer
);
1239 /* TCP connect fail */
1240 static int bgp_connect_fail(struct peer
*peer
)
1242 if (peer_dynamic_neighbor(peer
)) {
1243 if (bgp_debug_neighbor_events(peer
))
1244 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1249 return (bgp_stop(peer
));
1252 /* This function is the first starting point of all BGP connection. It
1253 try to connect to remote peer with non-blocking IO. */
1254 int bgp_start(struct peer
*peer
)
1259 bgp_peer_conf_if_to_su_update(peer
);
1261 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1262 if (bgp_debug_neighbor_events(peer
))
1264 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1269 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1270 if (bgp_debug_neighbor_events(peer
))
1272 "%s [FSM] Trying to start suppressed peer"
1273 " - this is never supposed to happen!",
1278 /* Scrub some information that might be left over from a previous,
1281 /* Connection information. */
1282 if (peer
->su_local
) {
1283 sockunion_free(peer
->su_local
);
1284 peer
->su_local
= NULL
;
1287 if (peer
->su_remote
) {
1288 sockunion_free(peer
->su_remote
);
1289 peer
->su_remote
= NULL
;
1292 /* Clear remote router-id. */
1293 peer
->remote_id
.s_addr
= 0;
1295 /* Clear peer capability flag. */
1298 /* If the peer is passive mode, force to move to Active mode. */
1299 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1300 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1304 /* Register to be notified on peer up */
1305 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
1306 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1307 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1312 if (!bgp_find_or_add_nexthop(peer
->bgp
,
1313 family2afi(peer
->su
.sa
.sa_family
), NULL
,
1315 #if defined(HAVE_CUMULUS)
1316 if (bgp_debug_neighbor_events(peer
))
1317 zlog_debug("%s [FSM] Waiting for NHT", peer
->host
);
1319 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1324 status
= bgp_connect(peer
);
1328 if (bgp_debug_neighbor_events(peer
))
1329 zlog_debug("%s [FSM] Connect error", peer
->host
);
1330 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1332 case connect_success
:
1333 if (bgp_debug_neighbor_events(peer
))
1335 "%s [FSM] Connect immediately success, fd %d",
1336 peer
->host
, peer
->fd
);
1337 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1339 case connect_in_progress
:
1340 /* To check nonblocking connect, we wait until socket is
1341 readable or writable. */
1342 if (bgp_debug_neighbor_events(peer
))
1344 "%s [FSM] Non blocking connect waiting result, fd %d",
1345 peer
->host
, peer
->fd
);
1347 zlog_err("bgp_start peer's fd is negative value %d",
1351 // when the socket becomes ready (or fails to connect),
1352 // bgp_connect_check
1354 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
);
1360 /* Connect retry timer is expired when the peer status is Connect. */
1361 static int bgp_reconnect(struct peer
*peer
)
1363 if (bgp_stop(peer
) < 0)
1370 static int bgp_fsm_open(struct peer
*peer
)
1372 /* Send keepalive and make keepalive timer */
1373 bgp_keepalive_send(peer
);
1375 /* Reset holdtimer value. */
1376 BGP_TIMER_OFF(peer
->t_holdtime
);
1381 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1382 peer and change to Idle status. */
1383 static int bgp_fsm_event_error(struct peer
*peer
)
1385 zlog_err("%s [FSM] unexpected packet received in state %s", peer
->host
,
1386 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1388 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1391 /* Hold timer expire. This is error of BGP connection. So cut the
1392 peer and change to Idle status. */
1393 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1395 if (bgp_debug_neighbor_events(peer
))
1396 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1398 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1401 /* Status goes to Established. Send keepalive packet then make first
1402 update information. */
1403 static int bgp_establish(struct peer
*peer
)
1407 int nsf_af_count
= 0;
1411 other
= peer
->doppelganger
;
1412 peer
= peer_xfer_conn(peer
);
1414 zlog_err("%%Neighbor failed in xfer_conn");
1420 1; /* bgp_establish specific code when xfer_conn
1423 /* Reset capability open status flag. */
1424 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1425 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1427 /* Clear start timer value to default. */
1428 peer
->v_start
= BGP_INIT_START_TIMER
;
1430 /* Increment established count. */
1431 peer
->established
++;
1432 bgp_fsm_change_status(peer
, Established
);
1434 /* bgp log-neighbor-changes of neighbor Up */
1435 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1436 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1437 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1439 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1440 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1444 /* assign update-group/subgroup */
1445 update_group_adjust_peer_afs(peer
);
1447 /* graceful restart */
1448 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1449 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1450 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1451 if (peer
->afc_nego
[afi
][safi
]
1452 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1453 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1454 PEER_CAP_RESTART_AF_RCV
)) {
1455 if (peer
->nsf
[afi
][safi
]
1457 peer
->af_cap
[afi
][safi
],
1458 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1459 bgp_clear_stale_route(peer
, afi
, safi
);
1461 peer
->nsf
[afi
][safi
] = 1;
1464 if (peer
->nsf
[afi
][safi
])
1465 bgp_clear_stale_route(peer
, afi
, safi
);
1466 peer
->nsf
[afi
][safi
] = 0;
1471 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1473 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1474 if (peer
->t_gr_stale
) {
1475 BGP_TIMER_OFF(peer
->t_gr_stale
);
1476 if (bgp_debug_neighbor_events(peer
))
1478 "%s graceful restart stalepath timer stopped",
1483 if (peer
->t_gr_restart
) {
1484 BGP_TIMER_OFF(peer
->t_gr_restart
);
1485 if (bgp_debug_neighbor_events(peer
))
1486 zlog_debug("%s graceful restart timer stopped",
1490 hook_call(peer_established
, peer
);
1492 /* Reset uptime, send keepalive, send current table. */
1493 peer
->uptime
= bgp_clock();
1495 /* Send route-refresh when ORF is enabled */
1496 FOREACH_AFI_SAFI (afi
, safi
) {
1497 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1498 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1499 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1500 PEER_CAP_ORF_PREFIX_RM_RCV
))
1501 bgp_route_refresh_send(peer
, afi
, safi
,
1503 REFRESH_IMMEDIATE
, 0);
1504 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1505 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1506 bgp_route_refresh_send(peer
, afi
, safi
,
1507 ORF_TYPE_PREFIX_OLD
,
1508 REFRESH_IMMEDIATE
, 0);
1512 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1513 FOREACH_AFI_SAFI (afi
, safi
) {
1514 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1515 PEER_CAP_ORF_PREFIX_RM_ADV
))
1516 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1517 PEER_CAP_ORF_PREFIX_SM_RCV
)
1518 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1519 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1520 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1521 PEER_STATUS_ORF_WAIT_REFRESH
);
1524 bgp_announce_peer(peer
);
1526 /* Start the route advertisement timer to send updates to the peer - if
1528 * is not in read-only mode. If it is, the timer will be started at the
1530 * of read-only mode.
1532 if (!bgp_update_delay_active(peer
->bgp
)) {
1533 BGP_TIMER_OFF(peer
->t_routeadv
);
1534 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1537 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1538 if (bgp_debug_neighbor_events(peer
))
1540 "[Event] Deleting stub connection for peer %s",
1543 if (peer
->doppelganger
->status
> Active
)
1544 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1545 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1547 peer_delete(peer
->doppelganger
);
1550 bgp_bfd_register_peer(peer
);
1554 /* Keepalive packet is received. */
1555 static int bgp_fsm_keepalive(struct peer
*peer
)
1557 bgp_update_implicit_eors(peer
);
1559 /* peer count update */
1560 peer
->keepalive_in
++;
1562 BGP_TIMER_OFF(peer
->t_holdtime
);
1566 /* Update packet is received. */
1567 static int bgp_fsm_update(struct peer
*peer
)
1569 BGP_TIMER_OFF(peer
->t_holdtime
);
1573 /* This is empty event. */
1574 static int bgp_ignore(struct peer
*peer
)
1577 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1578 peer
->host
, bgp_event_str
[peer
->cur_event
],
1579 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1580 bgp_event_str
[peer
->last_event
],
1581 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1585 /* This is to handle unexpected events.. */
1586 static int bgp_fsm_exeption(struct peer
*peer
)
1589 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1590 peer
->host
, bgp_event_str
[peer
->cur_event
],
1591 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1592 bgp_event_str
[peer
->last_event
],
1593 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1594 return (bgp_stop(peer
));
1597 void bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1602 switch (peer
->status
) {
1605 BGP_EVENT_ADD(peer
, BGP_Start
);
1609 BGP_TIMER_OFF(peer
->t_connect
);
1610 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1615 BGP_TIMER_OFF(peer
->t_connect
);
1616 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1622 if (!valid
&& (peer
->gtsm_hops
== 1))
1623 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1632 /* Finite State Machine structure */
1633 static const struct {
1634 int (*func
)(struct peer
*);
1636 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1638 /* Idle state: In Idle state, all events other than BGP_Start is
1639 ignored. With BGP_Start event, finite state machine calls
1641 {bgp_start
, Connect
}, /* BGP_Start */
1642 {bgp_stop
, Idle
}, /* BGP_Stop */
1643 {bgp_stop
, Idle
}, /* TCP_connection_open */
1644 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1645 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1646 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1647 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1648 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1649 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1650 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1651 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1652 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1653 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1654 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1658 {bgp_ignore
, Connect
}, /* BGP_Start */
1659 {bgp_stop
, Idle
}, /* BGP_Stop */
1660 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1661 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1662 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1663 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1664 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1665 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1666 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1667 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1668 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1669 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1670 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1671 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1675 {bgp_ignore
, Active
}, /* 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_ignore
, Active
}, /* TCP_connection_open_failed */
1680 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1681 {bgp_start
, 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_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1688 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1692 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1693 {bgp_stop
, Idle
}, /* BGP_Stop */
1694 {bgp_stop
, Active
}, /* TCP_connection_open */
1695 {bgp_stop
, Active
}, /* TCP_connection_closed */
1696 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1697 {bgp_stop
, Active
}, /* TCP_fatal_error */
1698 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1699 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1700 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1701 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1702 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1703 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1704 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1705 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1709 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1710 {bgp_stop
, Idle
}, /* BGP_Stop */
1711 {bgp_stop
, Idle
}, /* TCP_connection_open */
1712 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1713 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1714 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1715 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1716 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1717 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1718 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1719 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1720 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1721 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1722 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1726 {bgp_ignore
, Established
}, /* BGP_Start */
1727 {bgp_stop
, Clearing
}, /* BGP_Stop */
1728 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1729 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1730 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1731 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1732 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1733 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1734 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1735 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1737 Established
}, /* Receive_KEEPALIVE_message */
1738 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1739 {bgp_stop_with_error
,
1740 Clearing
}, /* Receive_NOTIFICATION_message */
1741 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1745 {bgp_ignore
, Clearing
}, /* BGP_Start */
1746 {bgp_stop
, Clearing
}, /* BGP_Stop */
1747 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1748 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1749 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1750 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1751 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1752 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1753 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1754 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1755 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1756 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1757 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1758 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1762 {bgp_ignore
, Deleted
}, /* BGP_Start */
1763 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1764 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1765 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1766 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1767 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1768 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1769 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1770 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1771 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1772 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1773 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1774 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1775 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1779 /* Execute event process. */
1780 int bgp_event(struct thread
*thread
)
1786 peer
= THREAD_ARG(thread
);
1787 event
= THREAD_VAL(thread
);
1789 ret
= bgp_event_update(peer
, event
);
1794 int bgp_event_update(struct peer
*peer
, int event
)
1799 int passive_conn
= 0;
1802 other
= peer
->doppelganger
;
1804 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1805 dyn_nbr
= peer_dynamic_neighbor(peer
);
1807 /* Logging this event. */
1808 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1810 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1811 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1812 bgp_event_str
[event
],
1813 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1814 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1816 peer
->last_event
= peer
->cur_event
;
1817 peer
->cur_event
= event
;
1819 /* Call function. */
1820 if (FSM
[peer
->status
- 1][event
- 1].func
)
1821 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1823 /* When function do not want proceed next job return -1. */
1825 if (ret
== 1 && next
== Established
) {
1826 /* The case when doppelganger swap accurred in
1828 Update the peer pointer accordingly */
1832 /* If status is changed. */
1833 if (next
!= peer
->status
)
1834 bgp_fsm_change_status(peer
, next
);
1836 /* Make sure timer is set. */
1837 bgp_timer_set(peer
);
1839 } else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
1840 /* If we got a return value of -1, that means there was an
1842 * the FSM. If the peer structure was deleted
1845 "%s [FSM] Failure handling event %s in state %s, "
1846 "prior events %s, %s, fd %d",
1847 peer
->host
, bgp_event_str
[peer
->cur_event
],
1848 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1849 bgp_event_str
[peer
->last_event
],
1850 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1852 bgp_fsm_change_status(peer
, Idle
);
1853 bgp_timer_set(peer
);