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"
53 #include "bgpd/bgp_io.h"
55 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
56 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
58 /* Definition of display strings corresponding to FSM events. This should be
59 * kept consistent with the events defined in bgpd.h
61 static const char *bgp_event_str
[] = {
65 "TCP_connection_open",
66 "TCP_connection_closed",
67 "TCP_connection_open_failed",
69 "ConnectRetry_timer_expired",
71 "KeepAlive_timer_expired",
72 "Receive_OPEN_message",
73 "Receive_KEEPALIVE_message",
74 "Receive_UPDATE_message",
75 "Receive_NOTIFICATION_message",
79 /* BGP FSM (finite state machine) has three types of functions. Type
80 one is thread functions. Type two is event functions. Type three
81 is FSM functions. Timer functions are set by bgp_timer_set
84 /* BGP event function. */
85 int bgp_event(struct thread
*);
87 /* BGP thread functions. */
88 static int bgp_start_timer(struct thread
*);
89 static int bgp_connect_timer(struct thread
*);
90 static int bgp_holdtime_timer(struct thread
*);
92 /* BGP FSM functions. */
93 static int bgp_start(struct peer
*);
95 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
97 /* Copy stats over. These are only the pre-established state stats */
98 peer_dst
->open_in
+= peer_src
->open_in
;
99 peer_dst
->open_out
+= peer_src
->open_out
;
100 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
101 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
102 peer_dst
->notify_in
+= peer_src
->notify_in
;
103 peer_dst
->notify_out
+= peer_src
->notify_out
;
104 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
105 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
108 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
115 unsigned char last_evt
, last_maj_evt
;
117 assert(from_peer
!= NULL
);
119 peer
= from_peer
->doppelganger
;
121 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
124 if (bgp_debug_neighbor_events(peer
))
125 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
126 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
129 bgp_writes_off(peer
);
131 bgp_writes_off(from_peer
);
132 bgp_reads_off(from_peer
);
134 BGP_TIMER_OFF(peer
->t_routeadv
);
135 BGP_TIMER_OFF(peer
->t_connect
);
136 BGP_TIMER_OFF(peer
->t_connect_check_r
);
137 BGP_TIMER_OFF(peer
->t_connect_check_w
);
138 BGP_TIMER_OFF(from_peer
->t_routeadv
);
139 BGP_TIMER_OFF(from_peer
->t_connect
);
140 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
141 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
144 * At this point in time, it is possible that there are packets pending
145 * on various buffers. Those need to be transferred or dropped,
146 * otherwise we'll get spurious failures during session establishment.
148 pthread_mutex_lock(&peer
->io_mtx
);
149 pthread_mutex_lock(&from_peer
->io_mtx
);
152 peer
->fd
= from_peer
->fd
;
155 stream_fifo_clean(peer
->ibuf
);
156 stream_fifo_clean(peer
->obuf
);
157 stream_reset(peer
->ibuf_work
);
160 * this should never happen, since bgp_process_packet() is the
161 * only task that sets and unsets the current packet and it
162 * runs in our pthread.
166 "[%s] Dropping pending packet on connection transfer:",
168 u_int16_t type
= stream_getc_from(peer
->curr
,
169 BGP_MARKER_SIZE
+ 2);
170 bgp_dump_packet(peer
, type
, peer
->curr
);
171 stream_free(peer
->curr
);
175 // copy each packet from old peer's output queue to new peer
176 while (from_peer
->obuf
->head
)
177 stream_fifo_push(peer
->obuf
,
178 stream_fifo_pop(from_peer
->obuf
));
180 // copy each packet from old peer's input queue to new peer
181 while (from_peer
->ibuf
->head
)
182 stream_fifo_push(peer
->ibuf
,
183 stream_fifo_pop(from_peer
->ibuf
));
185 pthread_mutex_unlock(&from_peer
->io_mtx
);
186 pthread_mutex_unlock(&peer
->io_mtx
);
188 peer
->as
= from_peer
->as
;
189 peer
->v_holdtime
= from_peer
->v_holdtime
;
190 peer
->v_keepalive
= from_peer
->v_keepalive
;
191 peer
->routeadv
= from_peer
->routeadv
;
192 peer
->v_routeadv
= from_peer
->v_routeadv
;
193 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
194 peer
->cap
= from_peer
->cap
;
195 status
= peer
->status
;
196 pstatus
= peer
->ostatus
;
197 last_evt
= peer
->last_event
;
198 last_maj_evt
= peer
->last_major_event
;
199 peer
->status
= from_peer
->status
;
200 peer
->ostatus
= from_peer
->ostatus
;
201 peer
->last_event
= from_peer
->last_event
;
202 peer
->last_major_event
= from_peer
->last_major_event
;
203 from_peer
->status
= status
;
204 from_peer
->ostatus
= pstatus
;
205 from_peer
->last_event
= last_evt
;
206 from_peer
->last_major_event
= last_maj_evt
;
207 peer
->remote_id
= from_peer
->remote_id
;
209 if (from_peer
->hostname
!= NULL
) {
210 if (peer
->hostname
) {
211 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
212 peer
->hostname
= NULL
;
215 peer
->hostname
= from_peer
->hostname
;
216 from_peer
->hostname
= NULL
;
219 if (from_peer
->domainname
!= NULL
) {
220 if (peer
->domainname
) {
221 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
222 peer
->domainname
= NULL
;
225 peer
->domainname
= from_peer
->domainname
;
226 from_peer
->domainname
= NULL
;
229 FOREACH_AFI_SAFI (afi
, safi
) {
230 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
231 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
232 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
233 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
234 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
235 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
236 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
239 if (bgp_getsockname(peer
) < 0) {
241 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
242 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
245 peer
->host
, peer
->fd
, from_peer
->fd
);
250 if (from_peer
->status
> Active
) {
251 if (bgp_getsockname(from_peer
) < 0) {
253 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
254 (CHECK_FLAG(from_peer
->sflags
,
255 PEER_STATUS_ACCEPT_PEER
)
258 from_peer
->host
, from_peer
->fd
, peer
->fd
);
268 peer_xfer_stats(peer
, from_peer
);
273 /* Hook function called after bgp event is occered. And vty's
274 neighbor command invoke this function after making neighbor
276 void bgp_timer_set(struct peer
*peer
)
278 switch (peer
->status
) {
280 /* First entry point of peer's finite state machine. In Idle
281 status start timer is on unless peer is shutdown or peer is
282 inactive. All other timer must be turned off */
283 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)) {
284 BGP_TIMER_OFF(peer
->t_start
);
286 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
289 BGP_TIMER_OFF(peer
->t_connect
);
290 BGP_TIMER_OFF(peer
->t_holdtime
);
291 bgp_keepalives_off(peer
);
292 BGP_TIMER_OFF(peer
->t_routeadv
);
296 /* After start timer is expired, the peer moves to Connect
297 status. Make sure start timer is off and connect timer is
299 BGP_TIMER_OFF(peer
->t_start
);
300 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
302 BGP_TIMER_OFF(peer
->t_holdtime
);
303 bgp_keepalives_off(peer
);
304 BGP_TIMER_OFF(peer
->t_routeadv
);
308 /* Active is waiting connection from remote peer. And if
309 connect timer is expired, change status to Connect. */
310 BGP_TIMER_OFF(peer
->t_start
);
311 /* If peer is passive mode, do not set connect timer. */
312 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
313 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
314 BGP_TIMER_OFF(peer
->t_connect
);
316 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
319 BGP_TIMER_OFF(peer
->t_holdtime
);
320 bgp_keepalives_off(peer
);
321 BGP_TIMER_OFF(peer
->t_routeadv
);
325 /* OpenSent status. */
326 BGP_TIMER_OFF(peer
->t_start
);
327 BGP_TIMER_OFF(peer
->t_connect
);
328 if (peer
->v_holdtime
!= 0) {
329 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
332 BGP_TIMER_OFF(peer
->t_holdtime
);
334 bgp_keepalives_off(peer
);
335 BGP_TIMER_OFF(peer
->t_routeadv
);
339 /* OpenConfirm status. */
340 BGP_TIMER_OFF(peer
->t_start
);
341 BGP_TIMER_OFF(peer
->t_connect
);
343 /* If the negotiated Hold Time value is zero, then the Hold Time
344 timer and KeepAlive timers are not started. */
345 if (peer
->v_holdtime
== 0) {
346 BGP_TIMER_OFF(peer
->t_holdtime
);
347 bgp_keepalives_off(peer
);
349 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
351 bgp_keepalives_on(peer
);
353 BGP_TIMER_OFF(peer
->t_routeadv
);
357 /* In Established status start and connect timer is turned
359 BGP_TIMER_OFF(peer
->t_start
);
360 BGP_TIMER_OFF(peer
->t_connect
);
362 /* Same as OpenConfirm, if holdtime is zero then both holdtime
363 and keepalive must be turned off. */
364 if (peer
->v_holdtime
== 0) {
365 BGP_TIMER_OFF(peer
->t_holdtime
);
366 bgp_keepalives_off(peer
);
368 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
370 bgp_keepalives_on(peer
);
374 BGP_TIMER_OFF(peer
->t_gr_restart
);
375 BGP_TIMER_OFF(peer
->t_gr_stale
);
376 BGP_TIMER_OFF(peer
->t_pmax_restart
);
379 BGP_TIMER_OFF(peer
->t_start
);
380 BGP_TIMER_OFF(peer
->t_connect
);
381 BGP_TIMER_OFF(peer
->t_holdtime
);
382 bgp_keepalives_off(peer
);
383 BGP_TIMER_OFF(peer
->t_routeadv
);
388 /* BGP start timer. This function set BGP_Start event to thread value
389 and process event. */
390 static int bgp_start_timer(struct thread
*thread
)
394 peer
= THREAD_ARG(thread
);
395 peer
->t_start
= NULL
;
397 if (bgp_debug_neighbor_events(peer
))
398 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
400 THREAD_VAL(thread
) = BGP_Start
;
401 bgp_event(thread
); /* bgp_event unlocks peer */
406 /* BGP connect retry timer. */
407 static int bgp_connect_timer(struct thread
*thread
)
412 peer
= THREAD_ARG(thread
);
414 assert(!peer
->t_write
);
415 assert(!peer
->t_read
);
417 peer
->t_connect
= NULL
;
419 if (bgp_debug_neighbor_events(peer
))
420 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
422 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
426 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
427 bgp_event(thread
); /* bgp_event unlocks peer */
434 /* BGP holdtime timer. */
435 static int bgp_holdtime_timer(struct thread
*thread
)
439 peer
= THREAD_ARG(thread
);
440 peer
->t_holdtime
= NULL
;
442 if (bgp_debug_neighbor_events(peer
))
443 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
446 THREAD_VAL(thread
) = Hold_Timer_expired
;
447 bgp_event(thread
); /* bgp_event unlocks peer */
452 int bgp_routeadv_timer(struct thread
*thread
)
456 peer
= THREAD_ARG(thread
);
457 peer
->t_routeadv
= NULL
;
459 if (bgp_debug_neighbor_events(peer
))
460 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
463 peer
->synctime
= bgp_clock();
465 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
466 &peer
->t_generate_updgrp_packets
);
468 /* MRAI timer will be started again when FIFO is built, no need to
474 /* BGP Peer Down Cause */
475 const char *peer_down_str
[] = {"",
479 "Cluster ID changed",
480 "Confederation identifier changed",
481 "Confederation peer changed",
482 "RR client config change",
483 "RS client config change",
484 "Update source change",
485 "Address family activated",
488 "BGP Notification received",
489 "BGP Notification send",
490 "Peer closed the session",
492 "Peer-group add member",
493 "Peer-group delete member",
494 "Capability changed",
495 "Passive config change",
496 "Multihop config change",
497 "NSF peer closed the session",
498 "Intf peering v6only config change",
501 "Neighbor address lost"};
503 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
509 peer
= THREAD_ARG(thread
);
510 peer
->t_gr_restart
= NULL
;
512 /* NSF delete stale route */
513 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
514 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
515 if (peer
->nsf
[afi
][safi
])
516 bgp_clear_stale_route(peer
, afi
, safi
);
518 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
519 BGP_TIMER_OFF(peer
->t_gr_stale
);
521 if (bgp_debug_neighbor_events(peer
)) {
522 zlog_debug("%s graceful restart timer expired", peer
->host
);
523 zlog_debug("%s graceful restart stalepath timer stopped",
532 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
538 peer
= THREAD_ARG(thread
);
539 peer
->t_gr_stale
= NULL
;
541 if (bgp_debug_neighbor_events(peer
))
542 zlog_debug("%s graceful restart stalepath timer expired",
545 /* NSF delete stale route */
546 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
547 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
548 if (peer
->nsf
[afi
][safi
])
549 bgp_clear_stale_route(peer
, afi
, safi
);
554 static int bgp_update_delay_applicable(struct bgp
*bgp
)
556 /* update_delay_over flag should be reset (set to 0) for any new
557 applicability of the update-delay during BGP process lifetime.
558 And it should be set after an occurence of the update-delay is
560 if (!bgp
->update_delay_over
)
566 int bgp_update_delay_active(struct bgp
*bgp
)
568 if (bgp
->t_update_delay
)
574 int bgp_update_delay_configured(struct bgp
*bgp
)
576 if (bgp
->v_update_delay
)
582 /* Do the post-processing needed when bgp comes out of the read-only mode
583 on ending the update delay. */
584 void 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
602 * the route advertisement timer for the peers can be started. Also set
603 * the zebra and peer update hold flags. These flags are used to achieve
604 * three stages in the update-delay post processing:
605 * 1. Finish best-path selection for all the prefixes held on the
607 * (routes in BGP are updated, and peers sync queues are populated
609 * 2. As the eoiu mark is reached in the bgp process routine, ship all
611 * routes to zebra. With that zebra should see updates from BGP
614 * 3. Unblock the peer update writes. With that peer update packing
616 * the prefixes should be at its maximum.
618 bgp_add_eoiu_mark(bgp
);
619 bgp
->main_zebra_update_hold
= 1;
620 bgp
->main_peers_update_hold
= 1;
622 /* Resume the queue processing. This should trigger the event that would
624 care of processing any work that was queued during the read-only
626 work_queue_unplug(bm
->process_main_queue
);
632 void bgp_start_routeadv(struct bgp
*bgp
)
634 struct listnode
*node
, *nnode
;
637 zlog_info("bgp_start_routeadv(), update hold status %d",
638 bgp
->main_peers_update_hold
);
640 if (bgp
->main_peers_update_hold
)
643 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
644 sizeof(bgp
->update_delay_peers_resume_time
));
646 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
647 if (peer
->status
!= Established
)
649 BGP_TIMER_OFF(peer
->t_routeadv
);
650 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
657 void bgp_adjust_routeadv(struct peer
*peer
)
659 time_t nowtime
= bgp_clock();
661 unsigned long remain
;
663 /* Bypass checks for special case of MRAI being 0 */
664 if (peer
->v_routeadv
== 0) {
665 /* Stop existing timer, just in case it is running for a
667 * duration and schedule write thread immediately.
669 if (peer
->t_routeadv
)
670 BGP_TIMER_OFF(peer
->t_routeadv
);
672 peer
->synctime
= bgp_clock();
673 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
,
675 &peer
->t_generate_updgrp_packets
);
682 * If the last update was written more than MRAI back, expire the timer
683 * instantly so that we can send the update out sooner.
685 * <------- MRAI --------->
686 * |-----------------|-----------------------|
687 * <------------- m ------------>
696 diff
= difftime(nowtime
, peer
->last_update
);
697 if (diff
> (double)peer
->v_routeadv
) {
698 BGP_TIMER_OFF(peer
->t_routeadv
);
699 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
705 * - Find when to expire the MRAI timer.
706 * If MRAI timer is not active, assume we can start it now.
708 * <------- MRAI --------->
709 * |------------|-----------------------|
710 * <-------- m ----------><----- r ----->
719 if (peer
->t_routeadv
)
720 remain
= thread_timer_remain_second(peer
->t_routeadv
);
722 remain
= peer
->v_routeadv
;
723 diff
= peer
->v_routeadv
- diff
;
724 if (diff
<= (double)remain
) {
725 BGP_TIMER_OFF(peer
->t_routeadv
);
726 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
730 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
732 if (!bgp
->maxmed_onstartup_over
)
738 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
740 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
746 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
748 if (bgp
->t_maxmed_onstartup
)
754 void bgp_maxmed_update(struct bgp
*bgp
)
756 u_char maxmed_active
;
757 u_int32_t maxmed_value
;
759 if (bgp
->v_maxmed_admin
) {
761 maxmed_value
= bgp
->maxmed_admin_value
;
762 } else if (bgp
->t_maxmed_onstartup
) {
764 maxmed_value
= bgp
->maxmed_onstartup_value
;
767 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
770 if (bgp
->maxmed_active
!= maxmed_active
771 || bgp
->maxmed_value
!= maxmed_value
) {
772 bgp
->maxmed_active
= maxmed_active
;
773 bgp
->maxmed_value
= maxmed_value
;
775 update_group_announce(bgp
);
779 /* The maxmed onstartup timer expiry callback. */
780 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
784 zlog_info("Max med on startup ended - timer expired.");
786 bgp
= THREAD_ARG(thread
);
787 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
788 bgp
->maxmed_onstartup_over
= 1;
790 bgp_maxmed_update(bgp
);
795 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
797 /* Applicable only once in the process lifetime on the startup */
798 if (bgp
->maxmed_onstartup_over
)
801 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
802 bgp
->v_maxmed_onstartup
);
804 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
805 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
807 if (!bgp
->v_maxmed_admin
) {
808 bgp
->maxmed_active
= 1;
809 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
812 /* Route announce to all peers should happen after this in
816 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
818 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
819 bgp_maxmed_onstartup_begin(peer
->bgp
);
823 /* The update delay timer expiry callback. */
824 static int bgp_update_delay_timer(struct thread
*thread
)
828 zlog_info("Update delay ended - timer expired.");
830 bgp
= THREAD_ARG(thread
);
831 THREAD_TIMER_OFF(bgp
->t_update_delay
);
832 bgp_update_delay_end(bgp
);
837 /* The establish wait timer expiry callback. */
838 static int bgp_establish_wait_timer(struct thread
*thread
)
842 zlog_info("Establish wait - timer expired.");
844 bgp
= THREAD_ARG(thread
);
845 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
846 bgp_check_update_delay(bgp
);
851 /* Steps to begin the update delay:
852 - initialize queues if needed
853 - stop the queue processing
855 static void bgp_update_delay_begin(struct bgp
*bgp
)
857 struct listnode
*node
, *nnode
;
860 /* Stop the processing of queued work. Enqueue shall continue */
861 work_queue_plug(bm
->process_main_queue
);
863 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
864 peer
->update_delay_over
= 0;
866 /* Start the update-delay timer */
867 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
868 bgp
->v_update_delay
, &bgp
->t_update_delay
);
870 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
871 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
872 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
874 quagga_timestamp(3, bgp
->update_delay_begin_time
,
875 sizeof(bgp
->update_delay_begin_time
));
878 static void bgp_update_delay_process_status_change(struct peer
*peer
)
880 if (peer
->status
== Established
) {
881 if (!peer
->bgp
->established
++) {
882 bgp_update_delay_begin(peer
->bgp
);
884 "Begin read-only mode - update-delay timer %d seconds",
885 peer
->bgp
->v_update_delay
);
887 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
888 bgp_update_restarted_peers(peer
);
890 if (peer
->ostatus
== Established
891 && bgp_update_delay_active(peer
->bgp
)) {
892 /* Adjust the update-delay state to account for this flap.
893 NOTE: Intentionally skipping adjusting implicit_eors or
895 counters. Extra sanity check in bgp_check_update_delay()
897 be enough to take care of any additive discrepancy in bgp eor
899 peer
->bgp
->established
--;
900 peer
->update_delay_over
= 0;
904 /* Called after event occured, this function change status and reset
905 read/write and timer thread. */
906 void bgp_fsm_change_status(struct peer
*peer
, int status
)
909 bgp_dump_state(peer
, peer
->status
, status
);
911 /* Transition into Clearing or Deleted must /always/ clear all routes..
912 * (and must do so before actually changing into Deleted..
914 if (status
>= Clearing
) {
915 bgp_clear_route_all(peer
);
917 /* If no route was queued for the clear-node processing,
919 * completion event here. This is needed because if there are no
921 * to trigger the background clear-node thread, the event won't
923 * generated and the peer would be stuck in Clearing. Note that
925 * event is for the peer and helps the peer transition out of
927 * state; it should not be generated per (AFI,SAFI). The event
929 * directly posted here without calling clear_node_complete() as
931 * shouldn't do an extra unlock. This event will get processed
933 * the state change that happens below, so peer will be in
937 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
938 BGP_EVENT_ADD(peer
, Clearing_Completed
);
941 /* Preserve old status and change into new status. */
942 peer
->ostatus
= peer
->status
;
943 peer
->status
= status
;
945 /* Save event that caused status change. */
946 peer
->last_major_event
= peer
->cur_event
;
948 if (status
== Established
)
949 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
951 /* If max-med processing is applicable, do the necessary. */
952 if (status
== Established
) {
953 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
954 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
955 bgp_maxmed_onstartup_process_status_change(peer
);
957 peer
->bgp
->maxmed_onstartup_over
= 1;
960 /* If update-delay processing is applicable, do the necessary. */
961 if (bgp_update_delay_configured(peer
->bgp
)
962 && bgp_update_delay_applicable(peer
->bgp
))
963 bgp_update_delay_process_status_change(peer
);
965 if (bgp_debug_neighbor_events(peer
))
966 zlog_debug("%s went from %s to %s", peer
->host
,
967 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
968 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
971 /* Flush the event queue and ensure the peer is shut down */
972 static int bgp_clearing_completed(struct peer
*peer
)
974 int rc
= bgp_stop(peer
);
977 BGP_EVENT_FLUSH(peer
);
982 /* Administrative BGP peer stop event. */
983 /* May be called multiple times for the same peer */
984 int bgp_stop(struct peer
*peer
)
988 char orf_name
[BUFSIZ
];
991 if (peer_dynamic_neighbor(peer
)
992 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
993 if (bgp_debug_neighbor_events(peer
))
994 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
999 /* Can't do this in Clearing; events are used for state transitions */
1000 if (peer
->status
!= Clearing
) {
1001 /* Delete all existing events of the peer */
1002 BGP_EVENT_FLUSH(peer
);
1005 /* Increment Dropped count. */
1006 if (peer
->status
== Established
) {
1009 /* bgp log-neighbor-changes of neighbor Down */
1010 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1011 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1013 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1015 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1016 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1019 peer_down_str
[(int)peer
->last_reset
]);
1022 /* graceful restart */
1023 if (peer
->t_gr_stale
) {
1024 BGP_TIMER_OFF(peer
->t_gr_stale
);
1025 if (bgp_debug_neighbor_events(peer
))
1027 "%s graceful restart stalepath timer stopped",
1030 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1031 if (bgp_debug_neighbor_events(peer
)) {
1033 "%s graceful restart timer started for %d sec",
1034 peer
->host
, peer
->v_gr_restart
);
1036 "%s graceful restart stalepath timer started for %d sec",
1037 peer
->host
, peer
->bgp
->stalepath_time
);
1039 BGP_TIMER_ON(peer
->t_gr_restart
,
1040 bgp_graceful_restart_timer_expire
,
1041 peer
->v_gr_restart
);
1042 BGP_TIMER_ON(peer
->t_gr_stale
,
1043 bgp_graceful_stale_timer_expire
,
1044 peer
->bgp
->stalepath_time
);
1046 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1048 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1049 for (safi
= SAFI_UNICAST
;
1050 safi
<= SAFI_MPLS_VPN
; safi
++)
1051 peer
->nsf
[afi
][safi
] = 0;
1054 /* set last reset time */
1055 peer
->resettime
= peer
->uptime
= bgp_clock();
1057 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1058 zlog_debug("%s remove from all update group",
1060 update_group_remove_peer_afs(peer
);
1062 hook_call(peer_backward_transition
, peer
);
1064 /* Reset peer synctime */
1067 bgp_bfd_deregister_peer(peer
);
1070 /* stop keepalives */
1071 bgp_keepalives_off(peer
);
1073 /* Stop read and write threads. */
1074 bgp_writes_off(peer
);
1075 bgp_reads_off(peer
);
1077 THREAD_OFF(peer
->t_connect_check_r
);
1078 THREAD_OFF(peer
->t_connect_check_w
);
1080 /* Stop all timers. */
1081 BGP_TIMER_OFF(peer
->t_start
);
1082 BGP_TIMER_OFF(peer
->t_connect
);
1083 BGP_TIMER_OFF(peer
->t_holdtime
);
1084 BGP_TIMER_OFF(peer
->t_routeadv
);
1086 /* Clear input and output buffer. */
1087 pthread_mutex_lock(&peer
->io_mtx
);
1090 stream_fifo_clean(peer
->ibuf
);
1092 stream_fifo_clean(peer
->obuf
);
1094 if (peer
->ibuf_work
)
1095 stream_reset(peer
->ibuf_work
);
1096 if (peer
->obuf_work
)
1097 stream_reset(peer
->obuf_work
);
1100 stream_free(peer
->curr
);
1104 pthread_mutex_unlock(&peer
->io_mtx
);
1106 /* Close of file descriptor. */
1107 if (peer
->fd
>= 0) {
1112 FOREACH_AFI_SAFI (afi
, safi
) {
1113 /* Reset all negotiated variables */
1114 peer
->afc_nego
[afi
][safi
] = 0;
1115 peer
->afc_adv
[afi
][safi
] = 0;
1116 peer
->afc_recv
[afi
][safi
] = 0;
1118 /* peer address family capability flags*/
1119 peer
->af_cap
[afi
][safi
] = 0;
1121 /* peer address family status flags*/
1122 peer
->af_sflags
[afi
][safi
] = 0;
1124 /* Received ORF prefix-filter */
1125 peer
->orf_plist
[afi
][safi
] = NULL
;
1127 if ((peer
->status
== OpenConfirm
)
1128 || (peer
->status
== Established
)) {
1129 /* ORF received prefix-filter pnt */
1130 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1131 prefix_bgp_orf_remove_all(afi
, orf_name
);
1135 /* Reset keepalive and holdtime */
1136 if (PEER_OR_GROUP_TIMER_SET(peer
)) {
1137 peer
->v_keepalive
= peer
->keepalive
;
1138 peer
->v_holdtime
= peer
->holdtime
;
1140 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1141 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1144 peer
->update_time
= 0;
1146 /* Until we are sure that there is no problem about prefix count
1147 this should be commented out.*/
1149 /* Reset prefix count */
1150 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1151 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1152 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1153 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1154 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1155 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1156 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1159 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1160 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1164 bgp_peer_conf_if_to_su_update(peer
);
1170 /* BGP peer is stoped by the error. */
1171 static int bgp_stop_with_error(struct peer
*peer
)
1173 /* Double start timer. */
1176 /* Overflow check. */
1177 if (peer
->v_start
>= (60 * 2))
1178 peer
->v_start
= (60 * 2);
1180 if (peer_dynamic_neighbor(peer
)) {
1181 if (bgp_debug_neighbor_events(peer
))
1182 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1187 return (bgp_stop(peer
));
1191 /* something went wrong, send notify and tear down */
1192 static int bgp_stop_with_notify(struct peer
*peer
, u_char code
, u_char sub_code
)
1194 /* Send notify to remote peer */
1195 bgp_notify_send(peer
, code
, sub_code
);
1197 if (peer_dynamic_neighbor(peer
)) {
1198 if (bgp_debug_neighbor_events(peer
))
1199 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1204 /* Clear start timer value to default. */
1205 peer
->v_start
= BGP_INIT_START_TIMER
;
1207 return (bgp_stop(peer
));
1211 * Determines whether a TCP session has successfully established for a peer and
1212 * events as appropriate.
1214 * This function is called when setting up a new session. After connect() is
1215 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1216 * to wait for connection success or failure. When poll() returns, this
1217 * function is called to evaluate the result.
1219 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1220 * the value of .revents in the case of a closed connection - this function is
1221 * scheduled both for a read and a write event. The write event is triggered
1222 * when the connection is established. A read event is triggered when the
1223 * connection is closed. Thus we need to cancel whichever one did not occur.
1225 static int bgp_connect_check(struct thread
*thread
)
1232 peer
= THREAD_ARG(thread
);
1233 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1234 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1235 assert(!peer
->t_read
);
1236 assert(!peer
->t_write
);
1238 THREAD_OFF(peer
->t_connect_check_r
);
1239 THREAD_OFF(peer
->t_connect_check_w
);
1241 /* Check file descriptor. */
1242 slen
= sizeof(status
);
1243 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1246 /* If getsockopt is fail, this is fatal error. */
1248 zlog_info("can't get sockopt for nonblocking connect");
1249 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1253 /* When status is 0 then TCP connection is established. */
1255 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1258 if (bgp_debug_neighbor_events(peer
))
1259 zlog_debug("%s [Event] Connect failed (%s)", peer
->host
,
1260 safe_strerror(errno
));
1261 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1266 /* TCP connection open. Next we send open message to remote peer. And
1267 add read thread for reading open message. */
1268 static int bgp_connect_success(struct peer
*peer
)
1271 zlog_err("bgp_connect_success peer's fd is negative value %d",
1277 if (bgp_getsockname(peer
) < 0) {
1278 zlog_err("%s: bgp_getsockname(): failed for peer %s, fd %d",
1279 __FUNCTION__
, peer
->host
, peer
->fd
);
1280 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1281 0); /* internal error */
1282 bgp_writes_on(peer
);
1288 if (bgp_debug_neighbor_events(peer
)) {
1289 char buf1
[SU_ADDRSTRLEN
];
1291 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1292 zlog_debug("%s open active, local address %s",
1294 sockunion2str(peer
->su_local
, buf1
,
1297 zlog_debug("%s passive open", peer
->host
);
1300 bgp_open_send(peer
);
1305 /* TCP connect fail */
1306 static int bgp_connect_fail(struct peer
*peer
)
1308 if (peer_dynamic_neighbor(peer
)) {
1309 if (bgp_debug_neighbor_events(peer
))
1310 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1315 return (bgp_stop(peer
));
1318 /* This function is the first starting point of all BGP connection. It
1319 try to connect to remote peer with non-blocking IO. */
1320 int bgp_start(struct peer
*peer
)
1325 bgp_peer_conf_if_to_su_update(peer
);
1327 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1328 if (bgp_debug_neighbor_events(peer
))
1330 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1335 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1336 if (bgp_debug_neighbor_events(peer
))
1338 "%s [FSM] Trying to start suppressed peer"
1339 " - this is never supposed to happen!",
1344 /* Scrub some information that might be left over from a previous,
1347 /* Connection information. */
1348 if (peer
->su_local
) {
1349 sockunion_free(peer
->su_local
);
1350 peer
->su_local
= NULL
;
1353 if (peer
->su_remote
) {
1354 sockunion_free(peer
->su_remote
);
1355 peer
->su_remote
= NULL
;
1358 /* Clear remote router-id. */
1359 peer
->remote_id
.s_addr
= 0;
1361 /* Clear peer capability flag. */
1364 /* If the peer is passive mode, force to move to Active mode. */
1365 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1366 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1370 /* Register to be notified on peer up */
1371 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
1372 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1373 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1378 if (!bgp_find_or_add_nexthop(peer
->bgp
,
1379 family2afi(peer
->su
.sa
.sa_family
), NULL
,
1381 #if defined(HAVE_CUMULUS)
1382 if (bgp_debug_neighbor_events(peer
))
1383 zlog_debug("%s [FSM] Waiting for NHT", peer
->host
);
1385 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1390 assert(!peer
->t_write
);
1391 assert(!peer
->t_read
);
1392 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1393 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1394 status
= bgp_connect(peer
);
1398 if (bgp_debug_neighbor_events(peer
))
1399 zlog_debug("%s [FSM] Connect error", peer
->host
);
1400 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1402 case connect_success
:
1403 if (bgp_debug_neighbor_events(peer
))
1405 "%s [FSM] Connect immediately success, fd %d",
1406 peer
->host
, peer
->fd
);
1407 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1409 case connect_in_progress
:
1410 /* To check nonblocking connect, we wait until socket is
1411 readable or writable. */
1412 if (bgp_debug_neighbor_events(peer
))
1414 "%s [FSM] Non blocking connect waiting result, fd %d",
1415 peer
->host
, peer
->fd
);
1417 zlog_err("bgp_start peer's fd is negative value %d",
1422 * - when the socket becomes ready, poll() will signify POLLOUT
1423 * - if it fails to connect, poll() will signify POLLHUP
1424 * - POLLHUP is handled as a 'read' event by thread.c
1426 * therefore, we schedule both a read and a write event with
1427 * bgp_connect_check() as the handler for each and cancel the
1428 * unused event in that function.
1430 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1431 &peer
->t_connect_check_r
);
1432 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1433 &peer
->t_connect_check_w
);
1439 /* Connect retry timer is expired when the peer status is Connect. */
1440 static int bgp_reconnect(struct peer
*peer
)
1442 if (bgp_stop(peer
) < 0)
1449 static int bgp_fsm_open(struct peer
*peer
)
1451 /* Send keepalive and make keepalive timer */
1452 bgp_keepalive_send(peer
);
1454 /* Reset holdtimer value. */
1455 BGP_TIMER_OFF(peer
->t_holdtime
);
1460 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1461 peer and change to Idle status. */
1462 static int bgp_fsm_event_error(struct peer
*peer
)
1464 zlog_err("%s [FSM] unexpected packet received in state %s", peer
->host
,
1465 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1467 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1470 /* Hold timer expire. This is error of BGP connection. So cut the
1471 peer and change to Idle status. */
1472 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1474 if (bgp_debug_neighbor_events(peer
))
1475 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1477 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1481 * Transition to Established state.
1483 * Convert peer from stub to full fledged peer, set some timers, and generate
1486 static int bgp_establish(struct peer
*peer
)
1490 int nsf_af_count
= 0;
1494 other
= peer
->doppelganger
;
1495 peer
= peer_xfer_conn(peer
);
1497 zlog_err("%%Neighbor failed in xfer_conn");
1503 1; /* bgp_establish specific code when xfer_conn
1506 /* Reset capability open status flag. */
1507 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1508 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1510 /* Clear start timer value to default. */
1511 peer
->v_start
= BGP_INIT_START_TIMER
;
1513 /* Increment established count. */
1514 peer
->established
++;
1515 bgp_fsm_change_status(peer
, Established
);
1517 /* bgp log-neighbor-changes of neighbor Up */
1518 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1519 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1520 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1522 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1523 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1527 /* assign update-group/subgroup */
1528 update_group_adjust_peer_afs(peer
);
1530 /* graceful restart */
1531 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1532 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1533 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1534 if (peer
->afc_nego
[afi
][safi
]
1535 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1536 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1537 PEER_CAP_RESTART_AF_RCV
)) {
1538 if (peer
->nsf
[afi
][safi
]
1540 peer
->af_cap
[afi
][safi
],
1541 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1542 bgp_clear_stale_route(peer
, afi
, safi
);
1544 peer
->nsf
[afi
][safi
] = 1;
1547 if (peer
->nsf
[afi
][safi
])
1548 bgp_clear_stale_route(peer
, afi
, safi
);
1549 peer
->nsf
[afi
][safi
] = 0;
1554 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1556 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1557 if (peer
->t_gr_stale
) {
1558 BGP_TIMER_OFF(peer
->t_gr_stale
);
1559 if (bgp_debug_neighbor_events(peer
))
1561 "%s graceful restart stalepath timer stopped",
1566 if (peer
->t_gr_restart
) {
1567 BGP_TIMER_OFF(peer
->t_gr_restart
);
1568 if (bgp_debug_neighbor_events(peer
))
1569 zlog_debug("%s graceful restart timer stopped",
1573 hook_call(peer_established
, peer
);
1575 /* Reset uptime, turn on keepalives, send current table. */
1576 if (!peer
->v_holdtime
)
1577 bgp_keepalives_on(peer
);
1579 peer
->uptime
= bgp_clock();
1581 /* Send route-refresh when ORF is enabled */
1582 FOREACH_AFI_SAFI (afi
, safi
) {
1583 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1584 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1585 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1586 PEER_CAP_ORF_PREFIX_RM_RCV
))
1587 bgp_route_refresh_send(peer
, afi
, safi
,
1589 REFRESH_IMMEDIATE
, 0);
1590 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1591 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1592 bgp_route_refresh_send(peer
, afi
, safi
,
1593 ORF_TYPE_PREFIX_OLD
,
1594 REFRESH_IMMEDIATE
, 0);
1598 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1599 FOREACH_AFI_SAFI (afi
, safi
) {
1600 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1601 PEER_CAP_ORF_PREFIX_RM_ADV
))
1602 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1603 PEER_CAP_ORF_PREFIX_SM_RCV
)
1604 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1605 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1606 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1607 PEER_STATUS_ORF_WAIT_REFRESH
);
1610 bgp_announce_peer(peer
);
1612 /* Start the route advertisement timer to send updates to the peer - if
1614 * is not in read-only mode. If it is, the timer will be started at the
1616 * of read-only mode.
1618 if (!bgp_update_delay_active(peer
->bgp
)) {
1619 BGP_TIMER_OFF(peer
->t_routeadv
);
1620 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1623 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1624 if (bgp_debug_neighbor_events(peer
))
1626 "[Event] Deleting stub connection for peer %s",
1629 if (peer
->doppelganger
->status
> Active
)
1630 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1631 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1633 peer_delete(peer
->doppelganger
);
1636 bgp_bfd_register_peer(peer
);
1640 /* Keepalive packet is received. */
1641 static int bgp_fsm_keepalive(struct peer
*peer
)
1643 BGP_TIMER_OFF(peer
->t_holdtime
);
1647 /* Update packet is received. */
1648 static int bgp_fsm_update(struct peer
*peer
)
1650 BGP_TIMER_OFF(peer
->t_holdtime
);
1654 /* This is empty event. */
1655 static int bgp_ignore(struct peer
*peer
)
1658 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1659 peer
->host
, bgp_event_str
[peer
->cur_event
],
1660 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1661 bgp_event_str
[peer
->last_event
],
1662 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1666 /* This is to handle unexpected events.. */
1667 static int bgp_fsm_exeption(struct peer
*peer
)
1670 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1671 peer
->host
, bgp_event_str
[peer
->cur_event
],
1672 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1673 bgp_event_str
[peer
->last_event
],
1674 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1675 return (bgp_stop(peer
));
1678 void bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1683 switch (peer
->status
) {
1686 BGP_EVENT_ADD(peer
, BGP_Start
);
1690 BGP_TIMER_OFF(peer
->t_connect
);
1691 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1696 BGP_TIMER_OFF(peer
->t_connect
);
1697 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1703 if (!valid
&& (peer
->gtsm_hops
== 1))
1704 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1713 /* Finite State Machine structure */
1714 static const struct {
1715 int (*func
)(struct peer
*);
1717 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1719 /* Idle state: In Idle state, all events other than BGP_Start is
1720 ignored. With BGP_Start event, finite state machine calls
1722 {bgp_start
, Connect
}, /* BGP_Start */
1723 {bgp_stop
, Idle
}, /* BGP_Stop */
1724 {bgp_stop
, Idle
}, /* TCP_connection_open */
1725 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1726 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1727 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1728 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1729 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1730 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1731 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1732 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1733 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1734 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1735 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1739 {bgp_ignore
, Connect
}, /* BGP_Start */
1740 {bgp_stop
, Idle
}, /* BGP_Stop */
1741 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1742 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1743 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1744 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1745 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1746 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1747 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1748 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1749 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1750 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1751 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1752 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1756 {bgp_ignore
, Active
}, /* BGP_Start */
1757 {bgp_stop
, Idle
}, /* BGP_Stop */
1758 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1759 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1760 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1761 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1762 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1763 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1764 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1765 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1766 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1767 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1768 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1769 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1773 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1774 {bgp_stop
, Idle
}, /* BGP_Stop */
1775 {bgp_stop
, Active
}, /* TCP_connection_open */
1776 {bgp_stop
, Active
}, /* TCP_connection_closed */
1777 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1778 {bgp_stop
, Active
}, /* TCP_fatal_error */
1779 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1780 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1781 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1782 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1783 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1784 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1785 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1786 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1790 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1791 {bgp_stop
, Idle
}, /* BGP_Stop */
1792 {bgp_stop
, Idle
}, /* TCP_connection_open */
1793 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1794 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1795 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1796 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1797 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1798 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1799 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1800 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1801 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1802 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1803 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1807 {bgp_ignore
, Established
}, /* BGP_Start */
1808 {bgp_stop
, Clearing
}, /* BGP_Stop */
1809 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1810 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1811 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1812 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1813 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1814 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1815 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1816 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1818 Established
}, /* Receive_KEEPALIVE_message */
1819 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1820 {bgp_stop_with_error
,
1821 Clearing
}, /* Receive_NOTIFICATION_message */
1822 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1826 {bgp_ignore
, Clearing
}, /* BGP_Start */
1827 {bgp_stop
, Clearing
}, /* BGP_Stop */
1828 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1829 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1830 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1831 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1832 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1833 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1834 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1835 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1836 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1837 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1838 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1839 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1843 {bgp_ignore
, Deleted
}, /* BGP_Start */
1844 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1845 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1846 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1847 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1848 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1849 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1850 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1851 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1852 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1853 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1854 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1855 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1856 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1860 /* Execute event process. */
1861 int bgp_event(struct thread
*thread
)
1867 peer
= THREAD_ARG(thread
);
1868 event
= THREAD_VAL(thread
);
1870 ret
= bgp_event_update(peer
, event
);
1875 int bgp_event_update(struct peer
*peer
, int event
)
1880 int passive_conn
= 0;
1883 /* default return code */
1884 ret
= FSM_PEER_NOOP
;
1886 other
= peer
->doppelganger
;
1888 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1889 dyn_nbr
= peer_dynamic_neighbor(peer
);
1891 /* Logging this event. */
1892 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1894 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1895 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1896 bgp_event_str
[event
],
1897 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1898 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1900 peer
->last_event
= peer
->cur_event
;
1901 peer
->cur_event
= event
;
1903 /* Call function. */
1904 if (FSM
[peer
->status
- 1][event
- 1].func
)
1905 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1908 if (ret
== 1 && next
== Established
) {
1909 /* The case when doppelganger swap accurred in
1911 Update the peer pointer accordingly */
1912 ret
= FSM_PEER_TRANSFERRED
;
1916 /* If status is changed. */
1917 if (next
!= peer
->status
) {
1918 bgp_fsm_change_status(peer
, next
);
1921 * If we're going to ESTABLISHED then we executed a
1922 * peer transfer. In this case we can either return
1923 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
1924 * Opting for TRANSFERRED since transfer implies
1925 * session establishment.
1927 if (ret
!= FSM_PEER_TRANSFERRED
)
1928 ret
= FSM_PEER_TRANSITIONED
;
1931 /* Make sure timer is set. */
1932 bgp_timer_set(peer
);
1936 * If we got a return value of -1, that means there was an
1937 * error, restart the FSM. Since bgp_stop() was called on the
1938 * peer. only a few fields are safe to access here. In any case
1939 * we need to indicate that the peer was stopped in the return
1942 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
1944 "%s [FSM] Failure handling event %s in state %s, "
1945 "prior events %s, %s, fd %d",
1946 peer
->host
, bgp_event_str
[peer
->cur_event
],
1947 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1948 bgp_event_str
[peer
->last_event
],
1949 bgp_event_str
[peer
->last_major_event
],
1952 bgp_fsm_change_status(peer
, Idle
);
1953 bgp_timer_set(peer
);
1955 ret
= FSM_PEER_STOPPED
;