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"
33 #include "workqueue.h"
37 #include "lib_errors.h"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_attr.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_errors.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_packet.h"
46 #include "bgpd/bgp_network.h"
47 #include "bgpd/bgp_route.h"
48 #include "bgpd/bgp_dump.h"
49 #include "bgpd/bgp_open.h"
50 #include "bgpd/bgp_advertise.h"
51 #include "bgpd/bgp_updgrp.h"
52 #include "bgpd/bgp_nht.h"
53 #include "bgpd/bgp_bfd.h"
54 #include "bgpd/bgp_memory.h"
55 #include "bgpd/bgp_keepalives.h"
56 #include "bgpd/bgp_io.h"
57 #include "bgpd/bgp_zebra.h"
59 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
60 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
62 /* Definition of display strings corresponding to FSM events. This should be
63 * kept consistent with the events defined in bgpd.h
65 static const char *bgp_event_str
[] = {
69 "TCP_connection_open",
70 "TCP_connection_closed",
71 "TCP_connection_open_failed",
73 "ConnectRetry_timer_expired",
75 "KeepAlive_timer_expired",
76 "Receive_OPEN_message",
77 "Receive_KEEPALIVE_message",
78 "Receive_UPDATE_message",
79 "Receive_NOTIFICATION_message",
83 /* BGP FSM (finite state machine) has three types of functions. Type
84 one is thread functions. Type two is event functions. Type three
85 is FSM functions. Timer functions are set by bgp_timer_set
88 /* BGP event function. */
89 int bgp_event(struct thread
*);
91 /* BGP thread functions. */
92 static int bgp_start_timer(struct thread
*);
93 static int bgp_connect_timer(struct thread
*);
94 static int bgp_holdtime_timer(struct thread
*);
96 /* BGP FSM functions. */
97 static int bgp_start(struct peer
*);
99 /* Register peer with NHT */
100 static int bgp_peer_reg_with_nht(struct peer
*peer
)
104 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
105 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
106 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
109 return bgp_find_or_add_nexthop(
110 peer
->bgp
, peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
111 NULL
, peer
, connected
);
114 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
116 /* Copy stats over. These are only the pre-established state stats */
117 peer_dst
->open_in
+= peer_src
->open_in
;
118 peer_dst
->open_out
+= peer_src
->open_out
;
119 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
120 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
121 peer_dst
->notify_in
+= peer_src
->notify_in
;
122 peer_dst
->notify_out
+= peer_src
->notify_out
;
123 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
124 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
127 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
134 unsigned char last_evt
, last_maj_evt
;
136 assert(from_peer
!= NULL
);
138 peer
= from_peer
->doppelganger
;
140 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
144 * Let's check that we are not going to loose known configuration
145 * state based upon doppelganger rules.
147 FOREACH_AFI_SAFI (afi
, safi
) {
148 if (from_peer
->afc
[afi
][safi
] != peer
->afc
[afi
][safi
]) {
150 EC_BGP_DOPPELGANGER_CONFIG
,
151 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
157 if (bgp_debug_neighbor_events(peer
))
158 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
159 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
162 bgp_writes_off(peer
);
164 bgp_writes_off(from_peer
);
165 bgp_reads_off(from_peer
);
167 BGP_TIMER_OFF(peer
->t_routeadv
);
168 BGP_TIMER_OFF(peer
->t_connect
);
169 BGP_TIMER_OFF(peer
->t_connect_check_r
);
170 BGP_TIMER_OFF(peer
->t_connect_check_w
);
171 BGP_TIMER_OFF(from_peer
->t_routeadv
);
172 BGP_TIMER_OFF(from_peer
->t_connect
);
173 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
174 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
175 BGP_TIMER_OFF(from_peer
->t_process_packet
);
178 * At this point in time, it is possible that there are packets pending
179 * on various buffers. Those need to be transferred or dropped,
180 * otherwise we'll get spurious failures during session establishment.
182 frr_with_mutex(&peer
->io_mtx
, &from_peer
->io_mtx
) {
184 peer
->fd
= from_peer
->fd
;
187 stream_fifo_clean(peer
->ibuf
);
188 stream_fifo_clean(peer
->obuf
);
191 * this should never happen, since bgp_process_packet() is the
192 * only task that sets and unsets the current packet and it
193 * runs in our pthread.
198 "[%s] Dropping pending packet on connection transfer:",
200 /* there used to be a bgp_packet_dump call here, but
201 * that's extremely confusing since there's no way to
202 * identify the packet in MRT dumps or BMP as dropped
203 * due to connection transfer.
205 stream_free(peer
->curr
);
209 // copy each packet from old peer's output queue to new peer
210 while (from_peer
->obuf
->head
)
211 stream_fifo_push(peer
->obuf
,
212 stream_fifo_pop(from_peer
->obuf
));
214 // copy each packet from old peer's input queue to new peer
215 while (from_peer
->ibuf
->head
)
216 stream_fifo_push(peer
->ibuf
,
217 stream_fifo_pop(from_peer
->ibuf
));
219 ringbuf_wipe(peer
->ibuf_work
);
220 ringbuf_copy(peer
->ibuf_work
, from_peer
->ibuf_work
,
221 ringbuf_remain(from_peer
->ibuf_work
));
224 peer
->as
= from_peer
->as
;
225 peer
->v_holdtime
= from_peer
->v_holdtime
;
226 peer
->v_keepalive
= from_peer
->v_keepalive
;
227 peer
->v_routeadv
= from_peer
->v_routeadv
;
228 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
229 peer
->cap
= from_peer
->cap
;
230 status
= peer
->status
;
231 pstatus
= peer
->ostatus
;
232 last_evt
= peer
->last_event
;
233 last_maj_evt
= peer
->last_major_event
;
234 peer
->status
= from_peer
->status
;
235 peer
->ostatus
= from_peer
->ostatus
;
236 peer
->last_event
= from_peer
->last_event
;
237 peer
->last_major_event
= from_peer
->last_major_event
;
238 from_peer
->status
= status
;
239 from_peer
->ostatus
= pstatus
;
240 from_peer
->last_event
= last_evt
;
241 from_peer
->last_major_event
= last_maj_evt
;
242 peer
->remote_id
= from_peer
->remote_id
;
243 peer
->last_reset
= from_peer
->last_reset
;
245 if (from_peer
->hostname
!= NULL
) {
246 if (peer
->hostname
) {
247 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
248 peer
->hostname
= NULL
;
251 peer
->hostname
= from_peer
->hostname
;
252 from_peer
->hostname
= NULL
;
255 if (from_peer
->domainname
!= NULL
) {
256 if (peer
->domainname
) {
257 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
258 peer
->domainname
= NULL
;
261 peer
->domainname
= from_peer
->domainname
;
262 from_peer
->domainname
= NULL
;
265 FOREACH_AFI_SAFI (afi
, safi
) {
266 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
267 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
268 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
269 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
270 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
271 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
272 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
275 if (bgp_getsockname(peer
) < 0) {
278 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
279 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
282 peer
->host
, peer
->fd
, from_peer
->fd
);
287 if (from_peer
->status
> Active
) {
288 if (bgp_getsockname(from_peer
) < 0) {
291 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
293 (CHECK_FLAG(from_peer
->sflags
,
294 PEER_STATUS_ACCEPT_PEER
)
297 from_peer
->host
, from_peer
->fd
, peer
->fd
);
304 // Note: peer_xfer_stats() must be called with I/O turned OFF
306 peer_xfer_stats(peer
, from_peer
);
308 /* Register peer for NHT. This is to allow RAs to be enabled when
309 * needed, even on a passive connection.
311 bgp_peer_reg_with_nht(peer
);
315 thread_add_timer_msec(bm
->master
, bgp_process_packet
, peer
, 0,
316 &peer
->t_process_packet
);
321 /* Hook function called after bgp event is occered. And vty's
322 neighbor command invoke this function after making neighbor
324 void bgp_timer_set(struct peer
*peer
)
326 switch (peer
->status
) {
328 /* First entry point of peer's finite state machine. In Idle
329 status start timer is on unless peer is shutdown or peer is
330 inactive. All other timer must be turned off */
331 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)
332 || (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
333 peer
->bgp
->vrf_id
== VRF_UNKNOWN
)) {
334 BGP_TIMER_OFF(peer
->t_start
);
336 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
339 BGP_TIMER_OFF(peer
->t_connect
);
340 BGP_TIMER_OFF(peer
->t_holdtime
);
341 bgp_keepalives_off(peer
);
342 BGP_TIMER_OFF(peer
->t_routeadv
);
346 /* After start timer is expired, the peer moves to Connect
347 status. Make sure start timer is off and connect timer is
349 BGP_TIMER_OFF(peer
->t_start
);
350 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
352 BGP_TIMER_OFF(peer
->t_holdtime
);
353 bgp_keepalives_off(peer
);
354 BGP_TIMER_OFF(peer
->t_routeadv
);
358 /* Active is waiting connection from remote peer. And if
359 connect timer is expired, change status to Connect. */
360 BGP_TIMER_OFF(peer
->t_start
);
361 /* If peer is passive mode, do not set connect timer. */
362 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
363 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
364 BGP_TIMER_OFF(peer
->t_connect
);
366 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
369 BGP_TIMER_OFF(peer
->t_holdtime
);
370 bgp_keepalives_off(peer
);
371 BGP_TIMER_OFF(peer
->t_routeadv
);
375 /* OpenSent status. */
376 BGP_TIMER_OFF(peer
->t_start
);
377 BGP_TIMER_OFF(peer
->t_connect
);
378 if (peer
->v_holdtime
!= 0) {
379 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
382 BGP_TIMER_OFF(peer
->t_holdtime
);
384 bgp_keepalives_off(peer
);
385 BGP_TIMER_OFF(peer
->t_routeadv
);
389 /* OpenConfirm status. */
390 BGP_TIMER_OFF(peer
->t_start
);
391 BGP_TIMER_OFF(peer
->t_connect
);
393 /* If the negotiated Hold Time value is zero, then the Hold Time
394 timer and KeepAlive timers are not started. */
395 if (peer
->v_holdtime
== 0) {
396 BGP_TIMER_OFF(peer
->t_holdtime
);
397 bgp_keepalives_off(peer
);
399 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
401 bgp_keepalives_on(peer
);
403 BGP_TIMER_OFF(peer
->t_routeadv
);
407 /* In Established status start and connect timer is turned
409 BGP_TIMER_OFF(peer
->t_start
);
410 BGP_TIMER_OFF(peer
->t_connect
);
412 /* Same as OpenConfirm, if holdtime is zero then both holdtime
413 and keepalive must be turned off. */
414 if (peer
->v_holdtime
== 0) {
415 BGP_TIMER_OFF(peer
->t_holdtime
);
416 bgp_keepalives_off(peer
);
418 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
420 bgp_keepalives_on(peer
);
424 BGP_TIMER_OFF(peer
->t_gr_restart
);
425 BGP_TIMER_OFF(peer
->t_gr_stale
);
426 BGP_TIMER_OFF(peer
->t_pmax_restart
);
429 BGP_TIMER_OFF(peer
->t_start
);
430 BGP_TIMER_OFF(peer
->t_connect
);
431 BGP_TIMER_OFF(peer
->t_holdtime
);
432 bgp_keepalives_off(peer
);
433 BGP_TIMER_OFF(peer
->t_routeadv
);
438 /* BGP start timer. This function set BGP_Start event to thread value
439 and process event. */
440 static int bgp_start_timer(struct thread
*thread
)
444 peer
= THREAD_ARG(thread
);
445 peer
->t_start
= NULL
;
447 if (bgp_debug_neighbor_events(peer
))
448 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
450 THREAD_VAL(thread
) = BGP_Start
;
451 bgp_event(thread
); /* bgp_event unlocks peer */
456 /* BGP connect retry timer. */
457 static int bgp_connect_timer(struct thread
*thread
)
462 peer
= THREAD_ARG(thread
);
464 assert(!peer
->t_write
);
465 assert(!peer
->t_read
);
467 peer
->t_connect
= NULL
;
469 if (bgp_debug_neighbor_events(peer
))
470 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
472 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
476 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
477 bgp_event(thread
); /* bgp_event unlocks peer */
484 /* BGP holdtime timer. */
485 static int bgp_holdtime_timer(struct thread
*thread
)
489 peer
= THREAD_ARG(thread
);
490 peer
->t_holdtime
= NULL
;
492 if (bgp_debug_neighbor_events(peer
))
493 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
496 THREAD_VAL(thread
) = Hold_Timer_expired
;
497 bgp_event(thread
); /* bgp_event unlocks peer */
502 int bgp_routeadv_timer(struct thread
*thread
)
506 peer
= THREAD_ARG(thread
);
507 peer
->t_routeadv
= NULL
;
509 if (bgp_debug_neighbor_events(peer
))
510 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
513 peer
->synctime
= bgp_clock();
515 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
516 &peer
->t_generate_updgrp_packets
);
518 /* MRAI timer will be started again when FIFO is built, no need to
524 /* BGP Peer Down Cause */
525 const char *peer_down_str
[] = {"",
529 "Cluster ID changed",
530 "Confederation identifier changed",
531 "Confederation peer changed",
532 "RR client config change",
533 "RS client config change",
534 "Update source change",
535 "Address family activated",
538 "BGP Notification received",
539 "BGP Notification send",
540 "Peer closed the session",
542 "Peer-group add member",
543 "Peer-group delete member",
544 "Capability changed",
545 "Passive config change",
546 "Multihop config change",
547 "NSF peer closed the session",
548 "Intf peering v6only config change",
551 "Neighbor address lost",
553 "Waiting for Peer IPv6 LLA",
554 "Waiting for VRF to be initialized",
555 "No AFI/SAFI activated for peer"};
557 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
563 peer
= THREAD_ARG(thread
);
564 peer
->t_gr_restart
= NULL
;
566 /* NSF delete stale route */
567 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
568 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
569 if (peer
->nsf
[afi
][safi
])
570 bgp_clear_stale_route(peer
, afi
, safi
);
572 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
573 BGP_TIMER_OFF(peer
->t_gr_stale
);
575 if (bgp_debug_neighbor_events(peer
)) {
576 zlog_debug("%s graceful restart timer expired", peer
->host
);
577 zlog_debug("%s graceful restart stalepath timer stopped",
586 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
592 peer
= THREAD_ARG(thread
);
593 peer
->t_gr_stale
= NULL
;
595 if (bgp_debug_neighbor_events(peer
))
596 zlog_debug("%s graceful restart stalepath timer expired",
599 /* NSF delete stale route */
600 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
601 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
602 if (peer
->nsf
[afi
][safi
])
603 bgp_clear_stale_route(peer
, afi
, safi
);
608 static int bgp_update_delay_applicable(struct bgp
*bgp
)
610 /* update_delay_over flag should be reset (set to 0) for any new
611 applicability of the update-delay during BGP process lifetime.
612 And it should be set after an occurence of the update-delay is
614 if (!bgp
->update_delay_over
)
620 int bgp_update_delay_active(struct bgp
*bgp
)
622 if (bgp
->t_update_delay
)
628 int bgp_update_delay_configured(struct bgp
*bgp
)
630 if (bgp
->v_update_delay
)
636 /* Do the post-processing needed when bgp comes out of the read-only mode
637 on ending the update delay. */
638 void bgp_update_delay_end(struct bgp
*bgp
)
640 THREAD_TIMER_OFF(bgp
->t_update_delay
);
641 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
643 /* Reset update-delay related state */
644 bgp
->update_delay_over
= 1;
645 bgp
->established
= 0;
646 bgp
->restarted_peers
= 0;
647 bgp
->implicit_eors
= 0;
648 bgp
->explicit_eors
= 0;
650 quagga_timestamp(3, bgp
->update_delay_end_time
,
651 sizeof(bgp
->update_delay_end_time
));
654 * Add an end-of-initial-update marker to the main process queues so
656 * the route advertisement timer for the peers can be started. Also set
657 * the zebra and peer update hold flags. These flags are used to achieve
658 * three stages in the update-delay post processing:
659 * 1. Finish best-path selection for all the prefixes held on the
661 * (routes in BGP are updated, and peers sync queues are populated
663 * 2. As the eoiu mark is reached in the bgp process routine, ship all
665 * routes to zebra. With that zebra should see updates from BGP
668 * 3. Unblock the peer update writes. With that peer update packing
670 * the prefixes should be at its maximum.
672 bgp_add_eoiu_mark(bgp
);
673 bgp
->main_zebra_update_hold
= 1;
674 bgp
->main_peers_update_hold
= 1;
676 /* Resume the queue processing. This should trigger the event that would
678 care of processing any work that was queued during the read-only
680 work_queue_unplug(bm
->process_main_queue
);
686 void bgp_start_routeadv(struct bgp
*bgp
)
688 struct listnode
*node
, *nnode
;
691 zlog_info("bgp_start_routeadv(), update hold status %d",
692 bgp
->main_peers_update_hold
);
694 if (bgp
->main_peers_update_hold
)
697 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
698 sizeof(bgp
->update_delay_peers_resume_time
));
700 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
701 if (peer
->status
!= Established
)
703 BGP_TIMER_OFF(peer
->t_routeadv
);
704 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
711 void bgp_adjust_routeadv(struct peer
*peer
)
713 time_t nowtime
= bgp_clock();
715 unsigned long remain
;
717 /* Bypass checks for special case of MRAI being 0 */
718 if (peer
->v_routeadv
== 0) {
719 /* Stop existing timer, just in case it is running for a
721 * duration and schedule write thread immediately.
723 if (peer
->t_routeadv
)
724 BGP_TIMER_OFF(peer
->t_routeadv
);
726 peer
->synctime
= bgp_clock();
727 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
,
729 &peer
->t_generate_updgrp_packets
);
736 * If the last update was written more than MRAI back, expire the timer
737 * instantly so that we can send the update out sooner.
739 * <------- MRAI --------->
740 * |-----------------|-----------------------|
741 * <------------- m ------------>
750 diff
= difftime(nowtime
, peer
->last_update
);
751 if (diff
> (double)peer
->v_routeadv
) {
752 BGP_TIMER_OFF(peer
->t_routeadv
);
753 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
759 * - Find when to expire the MRAI timer.
760 * If MRAI timer is not active, assume we can start it now.
762 * <------- MRAI --------->
763 * |------------|-----------------------|
764 * <-------- m ----------><----- r ----->
773 if (peer
->t_routeadv
)
774 remain
= thread_timer_remain_second(peer
->t_routeadv
);
776 remain
= peer
->v_routeadv
;
777 diff
= peer
->v_routeadv
- diff
;
778 if (diff
<= (double)remain
) {
779 BGP_TIMER_OFF(peer
->t_routeadv
);
780 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
784 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
786 if (!bgp
->maxmed_onstartup_over
)
792 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
794 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
800 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
802 if (bgp
->t_maxmed_onstartup
)
808 void bgp_maxmed_update(struct bgp
*bgp
)
810 uint8_t maxmed_active
;
811 uint32_t maxmed_value
;
813 if (bgp
->v_maxmed_admin
) {
815 maxmed_value
= bgp
->maxmed_admin_value
;
816 } else if (bgp
->t_maxmed_onstartup
) {
818 maxmed_value
= bgp
->maxmed_onstartup_value
;
821 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
824 if (bgp
->maxmed_active
!= maxmed_active
825 || bgp
->maxmed_value
!= maxmed_value
) {
826 bgp
->maxmed_active
= maxmed_active
;
827 bgp
->maxmed_value
= maxmed_value
;
829 update_group_announce(bgp
);
833 /* The maxmed onstartup timer expiry callback. */
834 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
838 zlog_info("Max med on startup ended - timer expired.");
840 bgp
= THREAD_ARG(thread
);
841 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
842 bgp
->maxmed_onstartup_over
= 1;
844 bgp_maxmed_update(bgp
);
849 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
851 /* Applicable only once in the process lifetime on the startup */
852 if (bgp
->maxmed_onstartup_over
)
855 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
856 bgp
->v_maxmed_onstartup
);
858 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
859 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
861 if (!bgp
->v_maxmed_admin
) {
862 bgp
->maxmed_active
= 1;
863 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
866 /* Route announce to all peers should happen after this in
870 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
872 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
873 bgp_maxmed_onstartup_begin(peer
->bgp
);
877 /* The update delay timer expiry callback. */
878 static int bgp_update_delay_timer(struct thread
*thread
)
882 zlog_info("Update delay ended - timer expired.");
884 bgp
= THREAD_ARG(thread
);
885 THREAD_TIMER_OFF(bgp
->t_update_delay
);
886 bgp_update_delay_end(bgp
);
891 /* The establish wait timer expiry callback. */
892 static int bgp_establish_wait_timer(struct thread
*thread
)
896 zlog_info("Establish wait - timer expired.");
898 bgp
= THREAD_ARG(thread
);
899 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
900 bgp_check_update_delay(bgp
);
905 /* Steps to begin the update delay:
906 - initialize queues if needed
907 - stop the queue processing
909 static void bgp_update_delay_begin(struct bgp
*bgp
)
911 struct listnode
*node
, *nnode
;
914 /* Stop the processing of queued work. Enqueue shall continue */
915 work_queue_plug(bm
->process_main_queue
);
917 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
918 peer
->update_delay_over
= 0;
920 /* Start the update-delay timer */
921 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
922 bgp
->v_update_delay
, &bgp
->t_update_delay
);
924 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
925 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
926 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
928 quagga_timestamp(3, bgp
->update_delay_begin_time
,
929 sizeof(bgp
->update_delay_begin_time
));
932 static void bgp_update_delay_process_status_change(struct peer
*peer
)
934 if (peer
->status
== Established
) {
935 if (!peer
->bgp
->established
++) {
936 bgp_update_delay_begin(peer
->bgp
);
938 "Begin read-only mode - update-delay timer %d seconds",
939 peer
->bgp
->v_update_delay
);
941 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
942 bgp_update_restarted_peers(peer
);
944 if (peer
->ostatus
== Established
945 && bgp_update_delay_active(peer
->bgp
)) {
946 /* Adjust the update-delay state to account for this flap.
947 NOTE: Intentionally skipping adjusting implicit_eors or
949 counters. Extra sanity check in bgp_check_update_delay()
951 be enough to take care of any additive discrepancy in bgp eor
953 peer
->bgp
->established
--;
954 peer
->update_delay_over
= 0;
958 /* Called after event occurred, this function change status and reset
959 read/write and timer thread. */
960 void bgp_fsm_change_status(struct peer
*peer
, int status
)
965 bgp_dump_state(peer
, peer
->status
, status
);
968 peer_count
= bgp
->established_peers
;
970 if (status
== Established
)
971 bgp
->established_peers
++;
972 else if ((peer
->status
== Established
) && (status
!= Established
))
973 bgp
->established_peers
--;
975 if (bgp_debug_neighbor_events(peer
)) {
976 struct vrf
*vrf
= vrf_lookup_by_id(bgp
->vrf_id
);
978 zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__
,
979 vrf
? vrf
->name
: "Unknown", bgp
->vrf_id
,
980 lookup_msg(bgp_status_msg
, status
, NULL
),
981 bgp
->established_peers
);
984 /* Set to router ID to the value provided by RIB if there are no peers
985 * in the established state and peer count did not change
987 if ((peer_count
!= bgp
->established_peers
) &&
988 (bgp
->established_peers
== 0))
989 bgp_router_id_zebra_bump(bgp
->vrf_id
, NULL
);
991 /* Transition into Clearing or Deleted must /always/ clear all routes..
992 * (and must do so before actually changing into Deleted..
994 if (status
>= Clearing
) {
995 bgp_clear_route_all(peer
);
997 /* If no route was queued for the clear-node processing,
999 * completion event here. This is needed because if there are no
1001 * to trigger the background clear-node thread, the event won't
1003 * generated and the peer would be stuck in Clearing. Note that
1005 * event is for the peer and helps the peer transition out of
1007 * state; it should not be generated per (AFI,SAFI). The event
1009 * directly posted here without calling clear_node_complete() as
1011 * shouldn't do an extra unlock. This event will get processed
1013 * the state change that happens below, so peer will be in
1017 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
1018 BGP_EVENT_ADD(peer
, Clearing_Completed
);
1021 /* Preserve old status and change into new status. */
1022 peer
->ostatus
= peer
->status
;
1023 peer
->status
= status
;
1025 /* Save event that caused status change. */
1026 peer
->last_major_event
= peer
->cur_event
;
1028 if (status
== Established
)
1029 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1031 /* If max-med processing is applicable, do the necessary. */
1032 if (status
== Established
) {
1033 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
1034 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
1035 bgp_maxmed_onstartup_process_status_change(peer
);
1037 peer
->bgp
->maxmed_onstartup_over
= 1;
1040 /* If update-delay processing is applicable, do the necessary. */
1041 if (bgp_update_delay_configured(peer
->bgp
)
1042 && bgp_update_delay_applicable(peer
->bgp
))
1043 bgp_update_delay_process_status_change(peer
);
1045 if (bgp_debug_neighbor_events(peer
))
1046 zlog_debug("%s went from %s to %s", peer
->host
,
1047 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
1048 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1051 /* Flush the event queue and ensure the peer is shut down */
1052 static int bgp_clearing_completed(struct peer
*peer
)
1054 int rc
= bgp_stop(peer
);
1057 BGP_EVENT_FLUSH(peer
);
1062 /* Administrative BGP peer stop event. */
1063 /* May be called multiple times for the same peer */
1064 int bgp_stop(struct peer
*peer
)
1068 char orf_name
[BUFSIZ
];
1071 if (peer_dynamic_neighbor(peer
)
1072 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1073 if (bgp_debug_neighbor_events(peer
))
1074 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1079 /* Can't do this in Clearing; events are used for state transitions */
1080 if (peer
->status
!= Clearing
) {
1081 /* Delete all existing events of the peer */
1082 BGP_EVENT_FLUSH(peer
);
1085 /* Increment Dropped count. */
1086 if (peer
->status
== Established
) {
1089 /* bgp log-neighbor-changes of neighbor Down */
1090 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1091 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1093 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1095 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1096 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1100 peer_down_str
[(int)peer
->last_reset
]);
1103 /* graceful restart */
1104 if (peer
->t_gr_stale
) {
1105 BGP_TIMER_OFF(peer
->t_gr_stale
);
1106 if (bgp_debug_neighbor_events(peer
))
1108 "%s graceful restart stalepath timer stopped",
1111 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1112 if (bgp_debug_neighbor_events(peer
)) {
1114 "%s graceful restart timer started for %d sec",
1115 peer
->host
, peer
->v_gr_restart
);
1117 "%s graceful restart stalepath timer started for %d sec",
1118 peer
->host
, peer
->bgp
->stalepath_time
);
1120 BGP_TIMER_ON(peer
->t_gr_restart
,
1121 bgp_graceful_restart_timer_expire
,
1122 peer
->v_gr_restart
);
1123 BGP_TIMER_ON(peer
->t_gr_stale
,
1124 bgp_graceful_stale_timer_expire
,
1125 peer
->bgp
->stalepath_time
);
1127 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1129 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1130 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
;
1132 peer
->nsf
[afi
][safi
] = 0;
1135 /* set last reset time */
1136 peer
->resettime
= peer
->uptime
= bgp_clock();
1138 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1139 zlog_debug("%s remove from all update group",
1141 update_group_remove_peer_afs(peer
);
1143 hook_call(peer_backward_transition
, peer
);
1145 /* Reset peer synctime */
1149 /* stop keepalives */
1150 bgp_keepalives_off(peer
);
1152 /* Stop read and write threads. */
1153 bgp_writes_off(peer
);
1154 bgp_reads_off(peer
);
1156 THREAD_OFF(peer
->t_connect_check_r
);
1157 THREAD_OFF(peer
->t_connect_check_w
);
1159 /* Stop all timers. */
1160 BGP_TIMER_OFF(peer
->t_start
);
1161 BGP_TIMER_OFF(peer
->t_connect
);
1162 BGP_TIMER_OFF(peer
->t_holdtime
);
1163 BGP_TIMER_OFF(peer
->t_routeadv
);
1165 /* Clear input and output buffer. */
1166 frr_with_mutex(&peer
->io_mtx
) {
1168 stream_fifo_clean(peer
->ibuf
);
1170 stream_fifo_clean(peer
->obuf
);
1172 if (peer
->ibuf_work
)
1173 ringbuf_wipe(peer
->ibuf_work
);
1174 if (peer
->obuf_work
)
1175 stream_reset(peer
->obuf_work
);
1178 stream_free(peer
->curr
);
1183 /* Close of file descriptor. */
1184 if (peer
->fd
>= 0) {
1189 FOREACH_AFI_SAFI (afi
, safi
) {
1190 /* Reset all negotiated variables */
1191 peer
->afc_nego
[afi
][safi
] = 0;
1192 peer
->afc_adv
[afi
][safi
] = 0;
1193 peer
->afc_recv
[afi
][safi
] = 0;
1195 /* peer address family capability flags*/
1196 peer
->af_cap
[afi
][safi
] = 0;
1198 /* peer address family status flags*/
1199 peer
->af_sflags
[afi
][safi
] = 0;
1201 /* Received ORF prefix-filter */
1202 peer
->orf_plist
[afi
][safi
] = NULL
;
1204 if ((peer
->status
== OpenConfirm
)
1205 || (peer
->status
== Established
)) {
1206 /* ORF received prefix-filter pnt */
1207 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1208 prefix_bgp_orf_remove_all(afi
, orf_name
);
1212 /* Reset keepalive and holdtime */
1213 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1214 peer
->v_keepalive
= peer
->keepalive
;
1215 peer
->v_holdtime
= peer
->holdtime
;
1217 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1218 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1221 peer
->update_time
= 0;
1223 /* Until we are sure that there is no problem about prefix count
1224 this should be commented out.*/
1226 /* Reset prefix count */
1227 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1228 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1229 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1230 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1231 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1232 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1233 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1236 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1237 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1241 bgp_peer_conf_if_to_su_update(peer
);
1247 /* BGP peer is stoped by the error. */
1248 static int bgp_stop_with_error(struct peer
*peer
)
1250 /* Double start timer. */
1253 /* Overflow check. */
1254 if (peer
->v_start
>= (60 * 2))
1255 peer
->v_start
= (60 * 2);
1257 if (peer_dynamic_neighbor(peer
)) {
1258 if (bgp_debug_neighbor_events(peer
))
1259 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1264 return (bgp_stop(peer
));
1268 /* something went wrong, send notify and tear down */
1269 static int bgp_stop_with_notify(struct peer
*peer
, uint8_t code
,
1272 /* Send notify to remote peer */
1273 bgp_notify_send(peer
, code
, sub_code
);
1275 if (peer_dynamic_neighbor(peer
)) {
1276 if (bgp_debug_neighbor_events(peer
))
1277 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1282 /* Clear start timer value to default. */
1283 peer
->v_start
= BGP_INIT_START_TIMER
;
1285 return (bgp_stop(peer
));
1289 * Determines whether a TCP session has successfully established for a peer and
1290 * events as appropriate.
1292 * This function is called when setting up a new session. After connect() is
1293 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1294 * to wait for connection success or failure. When poll() returns, this
1295 * function is called to evaluate the result.
1297 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1298 * the value of .revents in the case of a closed connection - this function is
1299 * scheduled both for a read and a write event. The write event is triggered
1300 * when the connection is established. A read event is triggered when the
1301 * connection is closed. Thus we need to cancel whichever one did not occur.
1303 static int bgp_connect_check(struct thread
*thread
)
1310 peer
= THREAD_ARG(thread
);
1311 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1312 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1313 assert(!peer
->t_read
);
1314 assert(!peer
->t_write
);
1316 THREAD_OFF(peer
->t_connect_check_r
);
1317 THREAD_OFF(peer
->t_connect_check_w
);
1319 /* Check file descriptor. */
1320 slen
= sizeof(status
);
1321 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1324 /* If getsockopt is fail, this is fatal error. */
1326 zlog_info("can't get sockopt for nonblocking connect: %d(%s)",
1327 errno
, safe_strerror(errno
));
1328 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1332 /* When status is 0 then TCP connection is established. */
1334 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1337 if (bgp_debug_neighbor_events(peer
))
1338 zlog_debug("%s [Event] Connect failed %d(%s)",
1339 peer
->host
, status
, safe_strerror(status
));
1340 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1345 /* TCP connection open. Next we send open message to remote peer. And
1346 add read thread for reading open message. */
1347 static int bgp_connect_success(struct peer
*peer
)
1350 flog_err(EC_BGP_CONNECT
,
1351 "bgp_connect_success peer's fd is negative value %d",
1357 if (bgp_getsockname(peer
) < 0) {
1358 flog_err_sys(EC_LIB_SOCKET
,
1359 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1360 __FUNCTION__
, peer
->host
, peer
->fd
);
1361 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1362 0); /* internal error */
1363 bgp_writes_on(peer
);
1369 if (bgp_debug_neighbor_events(peer
)) {
1370 char buf1
[SU_ADDRSTRLEN
];
1372 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1373 zlog_debug("%s open active, local address %s",
1375 sockunion2str(peer
->su_local
, buf1
,
1378 zlog_debug("%s passive open", peer
->host
);
1381 bgp_open_send(peer
);
1386 /* TCP connect fail */
1387 static int bgp_connect_fail(struct peer
*peer
)
1389 if (peer_dynamic_neighbor(peer
)) {
1390 if (bgp_debug_neighbor_events(peer
))
1391 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1396 return (bgp_stop(peer
));
1399 /* This function is the first starting point of all BGP connection. It
1400 try to connect to remote peer with non-blocking IO. */
1401 int bgp_start(struct peer
*peer
)
1405 bgp_peer_conf_if_to_su_update(peer
);
1407 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1408 if (bgp_debug_neighbor_events(peer
))
1410 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1412 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1416 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1417 if (bgp_debug_neighbor_events(peer
))
1418 flog_err(EC_BGP_FSM
,
1419 "%s [FSM] Trying to start suppressed peer"
1420 " - this is never supposed to happen!",
1425 /* Scrub some information that might be left over from a previous,
1428 /* Connection information. */
1429 if (peer
->su_local
) {
1430 sockunion_free(peer
->su_local
);
1431 peer
->su_local
= NULL
;
1434 if (peer
->su_remote
) {
1435 sockunion_free(peer
->su_remote
);
1436 peer
->su_remote
= NULL
;
1439 /* Clear remote router-id. */
1440 peer
->remote_id
.s_addr
= 0;
1442 /* Clear peer capability flag. */
1445 /* If the peer is passive mode, force to move to Active mode. */
1446 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1447 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1451 if (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
1452 peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
1453 if (bgp_debug_neighbor_events(peer
))
1456 "%s [FSM] In a VRF that is not initialised yet",
1458 peer
->last_reset
= PEER_DOWN_VRF_UNINIT
;
1462 /* Register peer for NHT. If next hop is already resolved, proceed
1463 * with connection setup, else wait.
1465 if (!bgp_peer_reg_with_nht(peer
)) {
1466 if (bgp_zebra_num_connects()) {
1467 if (bgp_debug_neighbor_events(peer
))
1468 zlog_debug("%s [FSM] Waiting for NHT",
1470 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1471 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1476 assert(!peer
->t_write
);
1477 assert(!peer
->t_read
);
1478 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1479 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1480 status
= bgp_connect(peer
);
1484 if (bgp_debug_neighbor_events(peer
))
1485 zlog_debug("%s [FSM] Connect error", peer
->host
);
1486 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1488 case connect_success
:
1489 if (bgp_debug_neighbor_events(peer
))
1491 "%s [FSM] Connect immediately success, fd %d",
1492 peer
->host
, peer
->fd
);
1493 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1495 case connect_in_progress
:
1496 /* To check nonblocking connect, we wait until socket is
1497 readable or writable. */
1498 if (bgp_debug_neighbor_events(peer
))
1500 "%s [FSM] Non blocking connect waiting result, fd %d",
1501 peer
->host
, peer
->fd
);
1503 flog_err(EC_BGP_FSM
,
1504 "bgp_start peer's fd is negative value %d",
1509 * - when the socket becomes ready, poll() will signify POLLOUT
1510 * - if it fails to connect, poll() will signify POLLHUP
1511 * - POLLHUP is handled as a 'read' event by thread.c
1513 * therefore, we schedule both a read and a write event with
1514 * bgp_connect_check() as the handler for each and cancel the
1515 * unused event in that function.
1517 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1518 &peer
->t_connect_check_r
);
1519 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1520 &peer
->t_connect_check_w
);
1526 /* Connect retry timer is expired when the peer status is Connect. */
1527 static int bgp_reconnect(struct peer
*peer
)
1529 if (bgp_stop(peer
) < 0)
1536 static int bgp_fsm_open(struct peer
*peer
)
1538 /* Send keepalive and make keepalive timer */
1539 bgp_keepalive_send(peer
);
1541 /* Reset holdtimer value. */
1542 BGP_TIMER_OFF(peer
->t_holdtime
);
1547 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1548 peer and change to Idle status. */
1549 static int bgp_fsm_event_error(struct peer
*peer
)
1551 flog_err(EC_BGP_FSM
, "%s [FSM] unexpected packet received in state %s",
1552 peer
->host
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1554 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1557 /* Hold timer expire. This is error of BGP connection. So cut the
1558 peer and change to Idle status. */
1559 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1561 if (bgp_debug_neighbor_events(peer
))
1562 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1564 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1568 * Transition to Established state.
1570 * Convert peer from stub to full fledged peer, set some timers, and generate
1573 static int bgp_establish(struct peer
*peer
)
1577 int nsf_af_count
= 0;
1581 other
= peer
->doppelganger
;
1582 peer
= peer_xfer_conn(peer
);
1584 flog_err(EC_BGP_CONNECT
, "%%Neighbor failed in xfer_conn");
1589 ret
= 1; /* bgp_establish specific code when xfer_conn
1592 /* Reset capability open status flag. */
1593 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1594 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1596 /* Clear start timer value to default. */
1597 peer
->v_start
= BGP_INIT_START_TIMER
;
1599 /* Increment established count. */
1600 peer
->established
++;
1601 bgp_fsm_change_status(peer
, Established
);
1603 /* bgp log-neighbor-changes of neighbor Up */
1604 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1605 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1606 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1608 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1609 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1614 /* assign update-group/subgroup */
1615 update_group_adjust_peer_afs(peer
);
1617 /* graceful restart */
1618 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1619 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1620 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1621 if (peer
->afc_nego
[afi
][safi
]
1622 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1623 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1624 PEER_CAP_RESTART_AF_RCV
)) {
1625 if (peer
->nsf
[afi
][safi
]
1627 peer
->af_cap
[afi
][safi
],
1628 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1629 bgp_clear_stale_route(peer
, afi
, safi
);
1631 peer
->nsf
[afi
][safi
] = 1;
1634 if (peer
->nsf
[afi
][safi
])
1635 bgp_clear_stale_route(peer
, afi
, safi
);
1636 peer
->nsf
[afi
][safi
] = 0;
1641 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1643 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1644 if (peer
->t_gr_stale
) {
1645 BGP_TIMER_OFF(peer
->t_gr_stale
);
1646 if (bgp_debug_neighbor_events(peer
))
1648 "%s graceful restart stalepath timer stopped",
1653 if (peer
->t_gr_restart
) {
1654 BGP_TIMER_OFF(peer
->t_gr_restart
);
1655 if (bgp_debug_neighbor_events(peer
))
1656 zlog_debug("%s graceful restart timer stopped",
1660 hook_call(peer_established
, peer
);
1662 /* Reset uptime, turn on keepalives, send current table. */
1663 if (!peer
->v_holdtime
)
1664 bgp_keepalives_on(peer
);
1666 peer
->uptime
= bgp_clock();
1668 /* Send route-refresh when ORF is enabled */
1669 FOREACH_AFI_SAFI (afi
, safi
) {
1670 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1671 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1672 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1673 PEER_CAP_ORF_PREFIX_RM_RCV
))
1674 bgp_route_refresh_send(peer
, afi
, safi
,
1676 REFRESH_IMMEDIATE
, 0);
1677 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1678 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1679 bgp_route_refresh_send(peer
, afi
, safi
,
1680 ORF_TYPE_PREFIX_OLD
,
1681 REFRESH_IMMEDIATE
, 0);
1685 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1686 FOREACH_AFI_SAFI (afi
, safi
) {
1687 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1688 PEER_CAP_ORF_PREFIX_RM_ADV
))
1689 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1690 PEER_CAP_ORF_PREFIX_SM_RCV
)
1691 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1692 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1693 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1694 PEER_STATUS_ORF_WAIT_REFRESH
);
1697 bgp_announce_peer(peer
);
1699 /* Start the route advertisement timer to send updates to the peer - if
1701 * is not in read-only mode. If it is, the timer will be started at the
1703 * of read-only mode.
1705 if (!bgp_update_delay_active(peer
->bgp
)) {
1706 BGP_TIMER_OFF(peer
->t_routeadv
);
1707 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1710 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1711 if (bgp_debug_neighbor_events(peer
))
1713 "[Event] Deleting stub connection for peer %s",
1716 if (peer
->doppelganger
->status
> Active
)
1717 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1718 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1720 peer_delete(peer
->doppelganger
);
1724 * If we are replacing the old peer for a doppelganger
1725 * then switch it around in the bgp->peerhash
1726 * the doppelgangers su and this peer's su are the same
1727 * so the hash_release is the same for either.
1729 hash_release(peer
->bgp
->peerhash
, peer
);
1730 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1732 bgp_bfd_register_peer(peer
);
1736 /* Keepalive packet is received. */
1737 static int bgp_fsm_keepalive(struct peer
*peer
)
1739 BGP_TIMER_OFF(peer
->t_holdtime
);
1743 /* Update packet is received. */
1744 static int bgp_fsm_update(struct peer
*peer
)
1746 BGP_TIMER_OFF(peer
->t_holdtime
);
1750 /* This is empty event. */
1751 static int bgp_ignore(struct peer
*peer
)
1755 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1756 peer
->host
, bgp_event_str
[peer
->cur_event
],
1757 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1758 bgp_event_str
[peer
->last_event
],
1759 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1763 /* This is to handle unexpected events.. */
1764 static int bgp_fsm_exeption(struct peer
*peer
)
1768 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1769 peer
->host
, bgp_event_str
[peer
->cur_event
],
1770 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1771 bgp_event_str
[peer
->last_event
],
1772 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1773 return (bgp_stop(peer
));
1776 void bgp_fsm_event_update(struct peer
*peer
, int valid
)
1781 switch (peer
->status
) {
1784 BGP_EVENT_ADD(peer
, BGP_Start
);
1788 BGP_TIMER_OFF(peer
->t_connect
);
1789 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1794 BGP_TIMER_OFF(peer
->t_connect
);
1795 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1801 if (!valid
&& (peer
->gtsm_hops
== 1))
1802 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1810 /* Finite State Machine structure */
1811 static const struct {
1812 int (*func
)(struct peer
*);
1814 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1816 /* Idle state: In Idle state, all events other than BGP_Start is
1817 ignored. With BGP_Start event, finite state machine calls
1819 {bgp_start
, Connect
}, /* BGP_Start */
1820 {bgp_stop
, Idle
}, /* BGP_Stop */
1821 {bgp_stop
, Idle
}, /* TCP_connection_open */
1822 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1823 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1824 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1825 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1826 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1827 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1828 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1829 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1830 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1831 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1832 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1836 {bgp_ignore
, Connect
}, /* BGP_Start */
1837 {bgp_stop
, Idle
}, /* BGP_Stop */
1838 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1839 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1840 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1841 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1842 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1843 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1844 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1845 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1846 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1847 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1848 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1849 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1853 {bgp_ignore
, Active
}, /* BGP_Start */
1854 {bgp_stop
, Idle
}, /* BGP_Stop */
1855 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1856 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1857 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1858 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1859 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1860 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1861 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1862 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1863 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1864 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1865 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1866 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1870 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1871 {bgp_stop
, Idle
}, /* BGP_Stop */
1872 {bgp_stop
, Active
}, /* TCP_connection_open */
1873 {bgp_stop
, Active
}, /* TCP_connection_closed */
1874 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1875 {bgp_stop
, Active
}, /* TCP_fatal_error */
1876 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1877 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1878 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1879 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1880 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1881 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1882 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1883 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1887 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1888 {bgp_stop
, Idle
}, /* BGP_Stop */
1889 {bgp_stop
, Idle
}, /* TCP_connection_open */
1890 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1891 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1892 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1893 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1894 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1895 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1896 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1897 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1898 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1899 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1900 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1904 {bgp_ignore
, Established
}, /* BGP_Start */
1905 {bgp_stop
, Clearing
}, /* BGP_Stop */
1906 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1907 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1908 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1909 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1910 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1911 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1912 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1913 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1915 Established
}, /* Receive_KEEPALIVE_message */
1916 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1917 {bgp_stop_with_error
,
1918 Clearing
}, /* Receive_NOTIFICATION_message */
1919 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1923 {bgp_ignore
, Clearing
}, /* BGP_Start */
1924 {bgp_stop
, Clearing
}, /* BGP_Stop */
1925 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1926 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1927 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1928 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1929 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1930 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1931 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1932 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1933 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1934 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1935 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1936 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1940 {bgp_ignore
, Deleted
}, /* BGP_Start */
1941 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1942 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1943 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1944 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1945 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1946 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1947 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1948 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1949 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1950 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1951 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1952 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1953 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1957 /* Execute event process. */
1958 int bgp_event(struct thread
*thread
)
1964 peer
= THREAD_ARG(thread
);
1965 event
= THREAD_VAL(thread
);
1967 ret
= bgp_event_update(peer
, event
);
1972 int bgp_event_update(struct peer
*peer
, int event
)
1977 int passive_conn
= 0;
1980 /* default return code */
1981 ret
= FSM_PEER_NOOP
;
1983 other
= peer
->doppelganger
;
1985 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1986 dyn_nbr
= peer_dynamic_neighbor(peer
);
1988 /* Logging this event. */
1989 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1991 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1992 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1993 bgp_event_str
[event
],
1994 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1995 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1997 peer
->last_event
= peer
->cur_event
;
1998 peer
->cur_event
= event
;
2000 /* Call function. */
2001 if (FSM
[peer
->status
- 1][event
- 1].func
)
2002 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
2005 if (ret
== 1 && next
== Established
) {
2006 /* The case when doppelganger swap accurred in
2008 Update the peer pointer accordingly */
2009 ret
= FSM_PEER_TRANSFERRED
;
2013 /* If status is changed. */
2014 if (next
!= peer
->status
) {
2015 bgp_fsm_change_status(peer
, next
);
2018 * If we're going to ESTABLISHED then we executed a
2019 * peer transfer. In this case we can either return
2020 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
2021 * Opting for TRANSFERRED since transfer implies
2022 * session establishment.
2024 if (ret
!= FSM_PEER_TRANSFERRED
)
2025 ret
= FSM_PEER_TRANSITIONED
;
2028 /* Make sure timer is set. */
2029 bgp_timer_set(peer
);
2033 * If we got a return value of -1, that means there was an
2034 * error, restart the FSM. Since bgp_stop() was called on the
2035 * peer. only a few fields are safe to access here. In any case
2036 * we need to indicate that the peer was stopped in the return
2039 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
2042 "%s [FSM] Failure handling event %s in state %s, "
2043 "prior events %s, %s, fd %d",
2044 peer
->host
, bgp_event_str
[peer
->cur_event
],
2045 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2046 bgp_event_str
[peer
->last_event
],
2047 bgp_event_str
[peer
->last_major_event
],
2050 bgp_fsm_change_status(peer
, Idle
);
2051 bgp_timer_set(peer
);
2053 ret
= FSM_PEER_STOPPED
;