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"
58 #include "bgpd/bgp_vty.h"
60 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
));
61 DEFINE_HOOK(peer_status_changed
, (struct peer
* peer
), (peer
));
63 /* Definition of display strings corresponding to FSM events. This should be
64 * kept consistent with the events defined in bgpd.h
66 static const char *const bgp_event_str
[] = {
70 "TCP_connection_open",
71 "TCP_connection_open_w_delay",
72 "TCP_connection_closed",
73 "TCP_connection_open_failed",
75 "ConnectRetry_timer_expired",
77 "KeepAlive_timer_expired",
78 "DelayOpen_timer_expired",
79 "Receive_OPEN_message",
80 "Receive_KEEPALIVE_message",
81 "Receive_UPDATE_message",
82 "Receive_NOTIFICATION_message",
86 /* BGP FSM (finite state machine) has three types of functions. Type
87 one is thread functions. Type two is event functions. Type three
88 is FSM functions. Timer functions are set by bgp_timer_set
91 /* BGP event function. */
92 int bgp_event(struct thread
*);
94 /* BGP thread functions. */
95 static int bgp_start_timer(struct thread
*);
96 static int bgp_connect_timer(struct thread
*);
97 static int bgp_holdtime_timer(struct thread
*);
98 static int bgp_delayopen_timer(struct thread
*);
100 /* BGP FSM functions. */
101 static int bgp_start(struct peer
*);
103 /* Register peer with NHT */
104 static int bgp_peer_reg_with_nht(struct peer
*peer
)
108 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== BGP_DEFAULT_TTL
109 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
110 && !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
113 return bgp_find_or_add_nexthop(peer
->bgp
, peer
->bgp
,
114 family2afi(peer
->su
.sa
.sa_family
),
115 SAFI_UNICAST
, NULL
, peer
, connected
);
118 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
120 /* Copy stats over. These are only the pre-established state stats */
121 peer_dst
->open_in
+= peer_src
->open_in
;
122 peer_dst
->open_out
+= peer_src
->open_out
;
123 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
124 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
125 peer_dst
->notify_in
+= peer_src
->notify_in
;
126 peer_dst
->notify_out
+= peer_src
->notify_out
;
127 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
128 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
131 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
137 enum bgp_fsm_status status
, pstatus
;
138 enum bgp_fsm_events last_evt
, last_maj_evt
;
140 assert(from_peer
!= NULL
);
142 peer
= from_peer
->doppelganger
;
144 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
148 * Let's check that we are not going to loose known configuration
149 * state based upon doppelganger rules.
151 FOREACH_AFI_SAFI (afi
, safi
) {
152 if (from_peer
->afc
[afi
][safi
] != peer
->afc
[afi
][safi
]) {
154 EC_BGP_DOPPELGANGER_CONFIG
,
155 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
161 if (bgp_debug_neighbor_events(peer
))
162 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
163 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
166 bgp_writes_off(peer
);
168 bgp_writes_off(from_peer
);
169 bgp_reads_off(from_peer
);
172 * Before exchanging FD remove doppelganger from
173 * keepalive peer hash. It could be possible conf peer
174 * fd is set to -1. If blocked on lock then keepalive
175 * thread can access peer pointer with fd -1.
177 bgp_keepalives_off(from_peer
);
179 BGP_TIMER_OFF(peer
->t_routeadv
);
180 BGP_TIMER_OFF(peer
->t_connect
);
181 BGP_TIMER_OFF(peer
->t_delayopen
);
182 BGP_TIMER_OFF(peer
->t_connect_check_r
);
183 BGP_TIMER_OFF(peer
->t_connect_check_w
);
184 BGP_TIMER_OFF(from_peer
->t_routeadv
);
185 BGP_TIMER_OFF(from_peer
->t_connect
);
186 BGP_TIMER_OFF(from_peer
->t_delayopen
);
187 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
188 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
189 BGP_TIMER_OFF(from_peer
->t_process_packet
);
192 * At this point in time, it is possible that there are packets pending
193 * on various buffers. Those need to be transferred or dropped,
194 * otherwise we'll get spurious failures during session establishment.
196 frr_with_mutex(&peer
->io_mtx
, &from_peer
->io_mtx
) {
198 peer
->fd
= from_peer
->fd
;
201 stream_fifo_clean(peer
->ibuf
);
202 stream_fifo_clean(peer
->obuf
);
205 * this should never happen, since bgp_process_packet() is the
206 * only task that sets and unsets the current packet and it
207 * runs in our pthread.
212 "[%s] Dropping pending packet on connection transfer:",
214 /* there used to be a bgp_packet_dump call here, but
215 * that's extremely confusing since there's no way to
216 * identify the packet in MRT dumps or BMP as dropped
217 * due to connection transfer.
219 stream_free(peer
->curr
);
223 // copy each packet from old peer's output queue to new peer
224 while (from_peer
->obuf
->head
)
225 stream_fifo_push(peer
->obuf
,
226 stream_fifo_pop(from_peer
->obuf
));
228 // copy each packet from old peer's input queue to new peer
229 while (from_peer
->ibuf
->head
)
230 stream_fifo_push(peer
->ibuf
,
231 stream_fifo_pop(from_peer
->ibuf
));
233 ringbuf_wipe(peer
->ibuf_work
);
234 ringbuf_copy(peer
->ibuf_work
, from_peer
->ibuf_work
,
235 ringbuf_remain(from_peer
->ibuf_work
));
238 peer
->as
= from_peer
->as
;
239 peer
->v_holdtime
= from_peer
->v_holdtime
;
240 peer
->v_keepalive
= from_peer
->v_keepalive
;
241 peer
->v_routeadv
= from_peer
->v_routeadv
;
242 peer
->v_delayopen
= from_peer
->v_delayopen
;
243 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
244 peer
->cap
= from_peer
->cap
;
245 status
= peer
->status
;
246 pstatus
= peer
->ostatus
;
247 last_evt
= peer
->last_event
;
248 last_maj_evt
= peer
->last_major_event
;
249 peer
->status
= from_peer
->status
;
250 peer
->ostatus
= from_peer
->ostatus
;
251 peer
->last_event
= from_peer
->last_event
;
252 peer
->last_major_event
= from_peer
->last_major_event
;
253 from_peer
->status
= status
;
254 from_peer
->ostatus
= pstatus
;
255 from_peer
->last_event
= last_evt
;
256 from_peer
->last_major_event
= last_maj_evt
;
257 peer
->remote_id
= from_peer
->remote_id
;
258 peer
->last_reset
= from_peer
->last_reset
;
260 peer
->peer_gr_present_state
= from_peer
->peer_gr_present_state
;
261 peer
->peer_gr_new_status_flag
= from_peer
->peer_gr_new_status_flag
;
262 bgp_peer_gr_flags_update(peer
);
264 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
267 if (bgp_peer_gr_mode_get(peer
) == PEER_DISABLE
) {
269 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
271 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
276 if (from_peer
->hostname
!= NULL
) {
277 if (peer
->hostname
) {
278 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
279 peer
->hostname
= NULL
;
282 peer
->hostname
= from_peer
->hostname
;
283 from_peer
->hostname
= NULL
;
286 if (from_peer
->domainname
!= NULL
) {
287 if (peer
->domainname
) {
288 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
289 peer
->domainname
= NULL
;
292 peer
->domainname
= from_peer
->domainname
;
293 from_peer
->domainname
= NULL
;
296 FOREACH_AFI_SAFI (afi
, safi
) {
297 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
298 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
299 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
300 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
301 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
302 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
303 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
306 if (bgp_getsockname(peer
) < 0) {
309 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
310 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
313 peer
->host
, peer
->fd
, from_peer
->fd
);
314 BGP_EVENT_ADD(peer
, BGP_Stop
);
315 BGP_EVENT_ADD(from_peer
, BGP_Stop
);
318 if (from_peer
->status
> Active
) {
319 if (bgp_getsockname(from_peer
) < 0) {
322 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
324 (CHECK_FLAG(from_peer
->sflags
,
325 PEER_STATUS_ACCEPT_PEER
)
328 from_peer
->host
, from_peer
->fd
, peer
->fd
);
335 // Note: peer_xfer_stats() must be called with I/O turned OFF
337 peer_xfer_stats(peer
, from_peer
);
339 /* Register peer for NHT. This is to allow RAs to be enabled when
340 * needed, even on a passive connection.
342 bgp_peer_reg_with_nht(peer
);
346 thread_add_timer_msec(bm
->master
, bgp_process_packet
, peer
, 0,
347 &peer
->t_process_packet
);
352 /* Hook function called after bgp event is occered. And vty's
353 neighbor command invoke this function after making neighbor
355 void bgp_timer_set(struct peer
*peer
)
357 switch (peer
->status
) {
359 /* First entry point of peer's finite state machine. In Idle
360 status start timer is on unless peer is shutdown or peer is
361 inactive. All other timer must be turned off */
362 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)
363 || (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
364 peer
->bgp
->vrf_id
== VRF_UNKNOWN
)) {
365 BGP_TIMER_OFF(peer
->t_start
);
367 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
370 BGP_TIMER_OFF(peer
->t_connect
);
371 BGP_TIMER_OFF(peer
->t_holdtime
);
372 bgp_keepalives_off(peer
);
373 BGP_TIMER_OFF(peer
->t_routeadv
);
374 BGP_TIMER_OFF(peer
->t_delayopen
);
378 /* After start timer is expired, the peer moves to Connect
379 status. Make sure start timer is off and connect timer is
381 BGP_TIMER_OFF(peer
->t_start
);
382 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
383 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
384 (peer
->v_delayopen
+ peer
->v_connect
));
386 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
389 BGP_TIMER_OFF(peer
->t_holdtime
);
390 bgp_keepalives_off(peer
);
391 BGP_TIMER_OFF(peer
->t_routeadv
);
395 /* Active is waiting connection from remote peer. And if
396 connect timer is expired, change status to Connect. */
397 BGP_TIMER_OFF(peer
->t_start
);
398 /* If peer is passive mode, do not set connect timer. */
399 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
400 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
401 BGP_TIMER_OFF(peer
->t_connect
);
403 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
405 peer
->t_connect
, bgp_connect_timer
,
406 (peer
->v_delayopen
+ peer
->v_connect
));
408 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
411 BGP_TIMER_OFF(peer
->t_holdtime
);
412 bgp_keepalives_off(peer
);
413 BGP_TIMER_OFF(peer
->t_routeadv
);
417 /* OpenSent status. */
418 BGP_TIMER_OFF(peer
->t_start
);
419 BGP_TIMER_OFF(peer
->t_connect
);
420 if (peer
->v_holdtime
!= 0) {
421 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
424 BGP_TIMER_OFF(peer
->t_holdtime
);
426 bgp_keepalives_off(peer
);
427 BGP_TIMER_OFF(peer
->t_routeadv
);
428 BGP_TIMER_OFF(peer
->t_delayopen
);
432 /* OpenConfirm status. */
433 BGP_TIMER_OFF(peer
->t_start
);
434 BGP_TIMER_OFF(peer
->t_connect
);
436 /* If the negotiated Hold Time value is zero, then the Hold Time
437 timer and KeepAlive timers are not started. */
438 if (peer
->v_holdtime
== 0) {
439 BGP_TIMER_OFF(peer
->t_holdtime
);
440 bgp_keepalives_off(peer
);
442 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
444 bgp_keepalives_on(peer
);
446 BGP_TIMER_OFF(peer
->t_routeadv
);
447 BGP_TIMER_OFF(peer
->t_delayopen
);
451 /* In Established status start and connect timer is turned
453 BGP_TIMER_OFF(peer
->t_start
);
454 BGP_TIMER_OFF(peer
->t_connect
);
455 BGP_TIMER_OFF(peer
->t_delayopen
);
457 /* Same as OpenConfirm, if holdtime is zero then both holdtime
458 and keepalive must be turned off. */
459 if (peer
->v_holdtime
== 0) {
460 BGP_TIMER_OFF(peer
->t_holdtime
);
461 bgp_keepalives_off(peer
);
463 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
465 bgp_keepalives_on(peer
);
469 BGP_TIMER_OFF(peer
->t_gr_restart
);
470 BGP_TIMER_OFF(peer
->t_gr_stale
);
471 BGP_TIMER_OFF(peer
->t_pmax_restart
);
472 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
475 BGP_TIMER_OFF(peer
->t_start
);
476 BGP_TIMER_OFF(peer
->t_connect
);
477 BGP_TIMER_OFF(peer
->t_holdtime
);
478 bgp_keepalives_off(peer
);
479 BGP_TIMER_OFF(peer
->t_routeadv
);
480 BGP_TIMER_OFF(peer
->t_delayopen
);
483 flog_err(EC_LIB_DEVELOPMENT
,
484 "BGP_STATUS_MAX while a legal state is not valid state for the FSM");
489 /* BGP start timer. This function set BGP_Start event to thread value
490 and process event. */
491 static int bgp_start_timer(struct thread
*thread
)
495 peer
= THREAD_ARG(thread
);
497 if (bgp_debug_neighbor_events(peer
))
498 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
500 THREAD_VAL(thread
) = BGP_Start
;
501 bgp_event(thread
); /* bgp_event unlocks peer */
506 /* BGP connect retry timer. */
507 static int bgp_connect_timer(struct thread
*thread
)
512 peer
= THREAD_ARG(thread
);
514 /* stop the DelayOpenTimer if it is running */
515 if (peer
->t_delayopen
)
516 BGP_TIMER_OFF(peer
->t_delayopen
);
518 assert(!peer
->t_write
);
519 assert(!peer
->t_read
);
521 if (bgp_debug_neighbor_events(peer
))
522 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
524 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
528 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
529 bgp_event(thread
); /* bgp_event unlocks peer */
536 /* BGP holdtime timer. */
537 static int bgp_holdtime_timer(struct thread
*thread
)
539 atomic_size_t inq_count
;
542 peer
= THREAD_ARG(thread
);
544 if (bgp_debug_neighbor_events(peer
))
545 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
549 * Given that we do not have any expectation of ordering
550 * for handling packets from a peer -vs- handling
551 * the hold timer for a peer as that they are both
552 * events on the peer. If we have incoming
553 * data on the peers inq, let's give the system a chance
554 * to handle that data. This can be especially true
555 * for systems where we are heavily loaded for one
558 inq_count
= atomic_load_explicit(&peer
->ibuf
->count
,
559 memory_order_relaxed
);
561 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
567 THREAD_VAL(thread
) = Hold_Timer_expired
;
568 bgp_event(thread
); /* bgp_event unlocks peer */
573 int bgp_routeadv_timer(struct thread
*thread
)
577 peer
= THREAD_ARG(thread
);
579 if (bgp_debug_neighbor_events(peer
))
580 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
583 peer
->synctime
= bgp_clock();
585 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
586 &peer
->t_generate_updgrp_packets
);
588 /* MRAI timer will be started again when FIFO is built, no need to
594 /* RFC 4271 DelayOpenTimer */
595 int bgp_delayopen_timer(struct thread
*thread
)
599 peer
= THREAD_ARG(thread
);
601 if (bgp_debug_neighbor_events(peer
))
602 zlog_debug("%s [FSM] Timer (DelayOpentimer expire)",
605 THREAD_VAL(thread
) = DelayOpen_timer_expired
;
606 bgp_event(thread
); /* bgp_event unlocks peer */
611 /* BGP Peer Down Cause */
612 const char *const peer_down_str
[] = {"",
616 "Cluster ID changed",
617 "Confederation identifier changed",
618 "Confederation peer changed",
619 "RR client config change",
620 "RS client config change",
621 "Update source change",
622 "Address family activated",
625 "BGP Notification received",
626 "BGP Notification send",
627 "Peer closed the session",
629 "Peer-group add member",
630 "Peer-group delete member",
631 "Capability changed",
632 "Passive config change",
633 "Multihop config change",
634 "NSF peer closed the session",
635 "Intf peering v6only config change",
638 "Neighbor address lost",
640 "Waiting for Peer IPv6 LLA",
641 "Waiting for VRF to be initialized",
642 "No AFI/SAFI activated for peer",
643 "AS Set config change",
644 "Waiting for peer OPEN",
645 "Reached received prefix count"};
647 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
653 peer
= THREAD_ARG(thread
);
655 /* NSF delete stale route */
656 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
657 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
658 if (peer
->nsf
[afi
][safi
])
659 bgp_clear_stale_route(peer
, afi
, safi
);
661 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
662 BGP_TIMER_OFF(peer
->t_gr_stale
);
664 if (bgp_debug_neighbor_events(peer
)) {
665 zlog_debug("%s graceful restart timer expired", peer
->host
);
666 zlog_debug("%s graceful restart stalepath timer stopped",
675 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
681 peer
= THREAD_ARG(thread
);
683 if (bgp_debug_neighbor_events(peer
))
684 zlog_debug("%s graceful restart stalepath timer expired",
687 /* NSF delete stale route */
688 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
689 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
690 if (peer
->nsf
[afi
][safi
])
691 bgp_clear_stale_route(peer
, afi
, safi
);
696 /* Selection deferral timer processing function */
697 static int bgp_graceful_deferral_timer_expire(struct thread
*thread
)
699 struct afi_safi_info
*info
;
704 info
= THREAD_ARG(thread
);
709 if (BGP_DEBUG(update
, UPDATE_OUT
))
711 "afi %d, safi %d : graceful restart deferral timer expired",
714 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
715 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
716 XFREE(MTYPE_TMP
, info
);
718 /* Best path selection */
719 return bgp_best_path_select_defer(bgp
, afi
, safi
);
722 static bool bgp_update_delay_applicable(struct bgp
*bgp
)
724 /* update_delay_over flag should be reset (set to 0) for any new
725 applicability of the update-delay during BGP process lifetime.
726 And it should be set after an occurence of the update-delay is
728 if (!bgp
->update_delay_over
)
733 bool bgp_update_delay_active(struct bgp
*bgp
)
735 if (bgp
->t_update_delay
)
740 bool bgp_update_delay_configured(struct bgp
*bgp
)
742 if (bgp
->v_update_delay
)
747 /* Do the post-processing needed when bgp comes out of the read-only mode
748 on ending the update delay. */
749 void bgp_update_delay_end(struct bgp
*bgp
)
751 THREAD_OFF(bgp
->t_update_delay
);
752 THREAD_OFF(bgp
->t_establish_wait
);
754 /* Reset update-delay related state */
755 bgp
->update_delay_over
= 1;
756 bgp
->established
= 0;
757 bgp
->restarted_peers
= 0;
758 bgp
->implicit_eors
= 0;
759 bgp
->explicit_eors
= 0;
761 quagga_timestamp(3, bgp
->update_delay_end_time
,
762 sizeof(bgp
->update_delay_end_time
));
765 * Add an end-of-initial-update marker to the main process queues so
767 * the route advertisement timer for the peers can be started. Also set
768 * the zebra and peer update hold flags. These flags are used to achieve
769 * three stages in the update-delay post processing:
770 * 1. Finish best-path selection for all the prefixes held on the
772 * (routes in BGP are updated, and peers sync queues are populated
774 * 2. As the eoiu mark is reached in the bgp process routine, ship all
776 * routes to zebra. With that zebra should see updates from BGP
779 * 3. Unblock the peer update writes. With that peer update packing
781 * the prefixes should be at its maximum.
783 bgp_add_eoiu_mark(bgp
);
784 bgp
->main_zebra_update_hold
= 1;
785 bgp
->main_peers_update_hold
= 1;
788 * Resume the queue processing. This should trigger the event that would
789 * take care of processing any work that was queued during the read-only
792 work_queue_unplug(bgp
->process_queue
);
798 void bgp_start_routeadv(struct bgp
*bgp
)
800 struct listnode
*node
, *nnode
;
803 zlog_info("bgp_start_routeadv(), update hold status %d",
804 bgp
->main_peers_update_hold
);
806 if (bgp
->main_peers_update_hold
)
809 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
810 sizeof(bgp
->update_delay_peers_resume_time
));
812 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
813 if (peer
->status
!= Established
)
815 BGP_TIMER_OFF(peer
->t_routeadv
);
816 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
823 void bgp_adjust_routeadv(struct peer
*peer
)
825 time_t nowtime
= bgp_clock();
827 unsigned long remain
;
829 /* Bypass checks for special case of MRAI being 0 */
830 if (peer
->v_routeadv
== 0) {
831 /* Stop existing timer, just in case it is running for a
833 * duration and schedule write thread immediately.
835 if (peer
->t_routeadv
)
836 BGP_TIMER_OFF(peer
->t_routeadv
);
838 peer
->synctime
= bgp_clock();
839 /* If suppress fib pending is enabled, route is advertised to
840 * peers when the status is received from the FIB. The delay
841 * is added to update group packet generate which will allow
842 * more routes to be sent in the update message
844 BGP_UPDATE_GROUP_TIMER_ON(&peer
->t_generate_updgrp_packets
,
845 bgp_generate_updgrp_packets
);
852 * If the last update was written more than MRAI back, expire the timer
853 * instantly so that we can send the update out sooner.
855 * <------- MRAI --------->
856 * |-----------------|-----------------------|
857 * <------------- m ------------>
866 diff
= difftime(nowtime
, peer
->last_update
);
867 if (diff
> (double)peer
->v_routeadv
) {
868 BGP_TIMER_OFF(peer
->t_routeadv
);
869 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
875 * - Find when to expire the MRAI timer.
876 * If MRAI timer is not active, assume we can start it now.
878 * <------- MRAI --------->
879 * |------------|-----------------------|
880 * <-------- m ----------><----- r ----->
889 if (peer
->t_routeadv
)
890 remain
= thread_timer_remain_second(peer
->t_routeadv
);
892 remain
= peer
->v_routeadv
;
893 diff
= peer
->v_routeadv
- diff
;
894 if (diff
<= (double)remain
) {
895 BGP_TIMER_OFF(peer
->t_routeadv
);
896 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
900 static bool bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
902 if (!bgp
->maxmed_onstartup_over
)
907 bool bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
909 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
914 bool bgp_maxmed_onstartup_active(struct bgp
*bgp
)
916 if (bgp
->t_maxmed_onstartup
)
921 void bgp_maxmed_update(struct bgp
*bgp
)
923 uint8_t maxmed_active
;
924 uint32_t maxmed_value
;
926 if (bgp
->v_maxmed_admin
) {
928 maxmed_value
= bgp
->maxmed_admin_value
;
929 } else if (bgp
->t_maxmed_onstartup
) {
931 maxmed_value
= bgp
->maxmed_onstartup_value
;
934 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
937 if (bgp
->maxmed_active
!= maxmed_active
938 || bgp
->maxmed_value
!= maxmed_value
) {
939 bgp
->maxmed_active
= maxmed_active
;
940 bgp
->maxmed_value
= maxmed_value
;
942 update_group_announce(bgp
);
946 int bgp_fsm_error_subcode(int status
)
948 int fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_UNSPECIFIC
;
952 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_OPENSENT
;
955 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_OPENCONFIRM
;
958 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_ESTABLISHED
;
964 return fsm_err_subcode
;
967 /* The maxmed onstartup timer expiry callback. */
968 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
972 zlog_info("Max med on startup ended - timer expired.");
974 bgp
= THREAD_ARG(thread
);
975 THREAD_OFF(bgp
->t_maxmed_onstartup
);
976 bgp
->maxmed_onstartup_over
= 1;
978 bgp_maxmed_update(bgp
);
983 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
985 /* Applicable only once in the process lifetime on the startup */
986 if (bgp
->maxmed_onstartup_over
)
989 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
990 bgp
->v_maxmed_onstartup
);
992 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
993 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
995 if (!bgp
->v_maxmed_admin
) {
996 bgp
->maxmed_active
= 1;
997 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
1000 /* Route announce to all peers should happen after this in
1001 * bgp_establish() */
1004 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
1006 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
1007 bgp_maxmed_onstartup_begin(peer
->bgp
);
1011 /* The update delay timer expiry callback. */
1012 static int bgp_update_delay_timer(struct thread
*thread
)
1016 zlog_info("Update delay ended - timer expired.");
1018 bgp
= THREAD_ARG(thread
);
1019 THREAD_OFF(bgp
->t_update_delay
);
1020 bgp_update_delay_end(bgp
);
1025 /* The establish wait timer expiry callback. */
1026 static int bgp_establish_wait_timer(struct thread
*thread
)
1030 zlog_info("Establish wait - timer expired.");
1032 bgp
= THREAD_ARG(thread
);
1033 THREAD_OFF(bgp
->t_establish_wait
);
1034 bgp_check_update_delay(bgp
);
1039 /* Steps to begin the update delay:
1040 - initialize queues if needed
1041 - stop the queue processing
1042 - start the timer */
1043 static void bgp_update_delay_begin(struct bgp
*bgp
)
1045 struct listnode
*node
, *nnode
;
1048 /* Stop the processing of queued work. Enqueue shall continue */
1049 work_queue_plug(bgp
->process_queue
);
1051 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
1052 peer
->update_delay_over
= 0;
1054 /* Start the update-delay timer */
1055 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
1056 bgp
->v_update_delay
, &bgp
->t_update_delay
);
1058 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
1059 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
1060 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
1062 quagga_timestamp(3, bgp
->update_delay_begin_time
,
1063 sizeof(bgp
->update_delay_begin_time
));
1066 static void bgp_update_delay_process_status_change(struct peer
*peer
)
1068 if (peer
->status
== Established
) {
1069 if (!peer
->bgp
->established
++) {
1070 bgp_update_delay_begin(peer
->bgp
);
1072 "Begin read-only mode - update-delay timer %d seconds",
1073 peer
->bgp
->v_update_delay
);
1075 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
1076 bgp_update_restarted_peers(peer
);
1078 if (peer
->ostatus
== Established
1079 && bgp_update_delay_active(peer
->bgp
)) {
1080 /* Adjust the update-delay state to account for this flap.
1081 NOTE: Intentionally skipping adjusting implicit_eors or
1083 counters. Extra sanity check in bgp_check_update_delay()
1085 be enough to take care of any additive discrepancy in bgp eor
1087 peer
->bgp
->established
--;
1088 peer
->update_delay_over
= 0;
1092 /* Called after event occurred, this function change status and reset
1093 read/write and timer thread. */
1094 void bgp_fsm_change_status(struct peer
*peer
, int status
)
1097 uint32_t peer_count
;
1100 peer_count
= bgp
->established_peers
;
1102 if (status
== Established
)
1103 bgp
->established_peers
++;
1104 else if ((peer
->status
== Established
) && (status
!= Established
))
1105 bgp
->established_peers
--;
1107 if (bgp_debug_neighbor_events(peer
)) {
1108 struct vrf
*vrf
= vrf_lookup_by_id(bgp
->vrf_id
);
1110 zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__
,
1111 vrf
? vrf
->name
: "Unknown", bgp
->vrf_id
,
1112 lookup_msg(bgp_status_msg
, status
, NULL
),
1113 bgp
->established_peers
);
1116 /* Set to router ID to the value provided by RIB if there are no peers
1117 * in the established state and peer count did not change
1119 if ((peer_count
!= bgp
->established_peers
) &&
1120 (bgp
->established_peers
== 0))
1121 bgp_router_id_zebra_bump(bgp
->vrf_id
, NULL
);
1123 /* Transition into Clearing or Deleted must /always/ clear all routes..
1124 * (and must do so before actually changing into Deleted..
1126 if (status
>= Clearing
) {
1127 bgp_clear_route_all(peer
);
1129 /* If no route was queued for the clear-node processing,
1131 * completion event here. This is needed because if there are no
1133 * to trigger the background clear-node thread, the event won't
1135 * generated and the peer would be stuck in Clearing. Note that
1137 * event is for the peer and helps the peer transition out of
1139 * state; it should not be generated per (AFI,SAFI). The event
1141 * directly posted here without calling clear_node_complete() as
1143 * shouldn't do an extra unlock. This event will get processed
1145 * the state change that happens below, so peer will be in
1149 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
1150 BGP_EVENT_ADD(peer
, Clearing_Completed
);
1153 /* Preserve old status and change into new status. */
1154 peer
->ostatus
= peer
->status
;
1155 peer
->status
= status
;
1157 /* Reset received keepalives counter on every FSM change */
1158 peer
->rtt_keepalive_rcv
= 0;
1160 /* Fire backward transition hook if that's the case */
1161 if (peer
->ostatus
> peer
->status
)
1162 hook_call(peer_backward_transition
, peer
);
1164 /* Save event that caused status change. */
1165 peer
->last_major_event
= peer
->cur_event
;
1167 /* Operations after status change */
1168 hook_call(peer_status_changed
, peer
);
1170 if (status
== Established
)
1171 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1173 /* If max-med processing is applicable, do the necessary. */
1174 if (status
== Established
) {
1175 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
1176 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
1177 bgp_maxmed_onstartup_process_status_change(peer
);
1179 peer
->bgp
->maxmed_onstartup_over
= 1;
1182 /* If update-delay processing is applicable, do the necessary. */
1183 if (bgp_update_delay_configured(peer
->bgp
)
1184 && bgp_update_delay_applicable(peer
->bgp
))
1185 bgp_update_delay_process_status_change(peer
);
1187 if (bgp_debug_neighbor_events(peer
))
1188 zlog_debug("%s went from %s to %s", peer
->host
,
1189 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
1190 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1193 /* Flush the event queue and ensure the peer is shut down */
1194 static int bgp_clearing_completed(struct peer
*peer
)
1196 int rc
= bgp_stop(peer
);
1199 BGP_EVENT_FLUSH(peer
);
1204 /* Administrative BGP peer stop event. */
1205 /* May be called multiple times for the same peer */
1206 int bgp_stop(struct peer
*peer
)
1210 char orf_name
[BUFSIZ
];
1212 struct bgp
*bgp
= peer
->bgp
;
1213 struct graceful_restart_info
*gr_info
= NULL
;
1215 peer
->nsf_af_count
= 0;
1217 /* deregister peer */
1218 if (peer
->last_reset
== PEER_DOWN_UPDATE_SOURCE_CHANGE
)
1219 bgp_bfd_deregister_peer(peer
);
1221 if (peer_dynamic_neighbor(peer
)
1222 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1223 if (bgp_debug_neighbor_events(peer
))
1224 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1229 /* Can't do this in Clearing; events are used for state transitions */
1230 if (peer
->status
!= Clearing
) {
1231 /* Delete all existing events of the peer */
1232 BGP_EVENT_FLUSH(peer
);
1235 /* Increment Dropped count. */
1236 if (peer
->status
== Established
) {
1239 /* bgp log-neighbor-changes of neighbor Down */
1240 if (CHECK_FLAG(peer
->bgp
->flags
,
1241 BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1242 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1245 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1247 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1248 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1252 peer_down_str
[(int)peer
->last_reset
]);
1255 /* graceful restart */
1256 if (peer
->t_gr_stale
) {
1257 BGP_TIMER_OFF(peer
->t_gr_stale
);
1258 if (bgp_debug_neighbor_events(peer
))
1260 "%s graceful restart stalepath timer stopped",
1263 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1264 if (bgp_debug_neighbor_events(peer
)) {
1266 "%s graceful restart timer started for %d sec",
1267 peer
->host
, peer
->v_gr_restart
);
1269 "%s graceful restart stalepath timer started for %d sec",
1270 peer
->host
, peer
->bgp
->stalepath_time
);
1272 BGP_TIMER_ON(peer
->t_gr_restart
,
1273 bgp_graceful_restart_timer_expire
,
1274 peer
->v_gr_restart
);
1275 BGP_TIMER_ON(peer
->t_gr_stale
,
1276 bgp_graceful_stale_timer_expire
,
1277 peer
->bgp
->stalepath_time
);
1279 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1281 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1282 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
;
1284 peer
->nsf
[afi
][safi
] = 0;
1287 /* Stop route-refresh stalepath timer */
1288 if (peer
->t_refresh_stalepath
) {
1289 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
1291 if (bgp_debug_neighbor_events(peer
))
1293 "%s: route-refresh restart stalepath timer stopped",
1297 /* If peer reset before receiving EOR, decrement EOR count and
1298 * cancel the selection deferral timer if there are no
1299 * pending EOR messages to be received
1301 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
1302 FOREACH_AFI_SAFI (afi
, safi
) {
1303 if (!peer
->afc_nego
[afi
][safi
]
1304 || CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1305 PEER_STATUS_EOR_RECEIVED
))
1308 gr_info
= &bgp
->gr_info
[afi
][safi
];
1312 if (gr_info
->eor_required
)
1313 gr_info
->eor_required
--;
1315 if (BGP_DEBUG(update
, UPDATE_OUT
))
1316 zlog_debug("peer %s, EOR_required %d",
1318 gr_info
->eor_required
);
1320 /* There is no pending EOR message */
1321 if (gr_info
->eor_required
== 0) {
1323 gr_info
->t_select_deferral
);
1324 gr_info
->eor_received
= 0;
1329 /* set last reset time */
1330 peer
->resettime
= peer
->uptime
= bgp_clock();
1332 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1333 zlog_debug("%s remove from all update group",
1335 update_group_remove_peer_afs(peer
);
1337 /* Reset peer synctime */
1341 /* stop keepalives */
1342 bgp_keepalives_off(peer
);
1344 /* Stop read and write threads. */
1345 bgp_writes_off(peer
);
1346 bgp_reads_off(peer
);
1348 THREAD_OFF(peer
->t_connect_check_r
);
1349 THREAD_OFF(peer
->t_connect_check_w
);
1351 /* Stop all timers. */
1352 BGP_TIMER_OFF(peer
->t_start
);
1353 BGP_TIMER_OFF(peer
->t_connect
);
1354 BGP_TIMER_OFF(peer
->t_holdtime
);
1355 BGP_TIMER_OFF(peer
->t_routeadv
);
1356 BGP_TIMER_OFF(peer
->t_delayopen
);
1358 /* Clear input and output buffer. */
1359 frr_with_mutex(&peer
->io_mtx
) {
1361 stream_fifo_clean(peer
->ibuf
);
1363 stream_fifo_clean(peer
->obuf
);
1365 if (peer
->ibuf_work
)
1366 ringbuf_wipe(peer
->ibuf_work
);
1367 if (peer
->obuf_work
)
1368 stream_reset(peer
->obuf_work
);
1371 stream_free(peer
->curr
);
1376 /* Close of file descriptor. */
1377 if (peer
->fd
>= 0) {
1382 FOREACH_AFI_SAFI (afi
, safi
) {
1383 /* Reset all negotiated variables */
1384 peer
->afc_nego
[afi
][safi
] = 0;
1385 peer
->afc_adv
[afi
][safi
] = 0;
1386 peer
->afc_recv
[afi
][safi
] = 0;
1388 /* peer address family capability flags*/
1389 peer
->af_cap
[afi
][safi
] = 0;
1391 /* peer address family status flags*/
1392 peer
->af_sflags
[afi
][safi
] = 0;
1394 /* Received ORF prefix-filter */
1395 peer
->orf_plist
[afi
][safi
] = NULL
;
1397 if ((peer
->status
== OpenConfirm
)
1398 || (peer
->status
== Established
)) {
1399 /* ORF received prefix-filter pnt */
1400 snprintf(orf_name
, sizeof(orf_name
), "%s.%d.%d",
1401 peer
->host
, afi
, safi
);
1402 prefix_bgp_orf_remove_all(afi
, orf_name
);
1406 /* Reset keepalive and holdtime */
1407 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1408 peer
->v_keepalive
= peer
->keepalive
;
1409 peer
->v_holdtime
= peer
->holdtime
;
1411 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1412 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1415 /* Reset DelayOpenTime */
1416 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
1417 peer
->v_delayopen
= peer
->delayopen
;
1419 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
1421 peer
->update_time
= 0;
1423 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1424 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1428 bgp_peer_conf_if_to_su_update(peer
);
1433 /* BGP peer is stoped by the error. */
1434 static int bgp_stop_with_error(struct peer
*peer
)
1436 /* Double start timer. */
1439 /* Overflow check. */
1440 if (peer
->v_start
>= (60 * 2))
1441 peer
->v_start
= (60 * 2);
1443 if (peer_dynamic_neighbor(peer
)) {
1444 if (bgp_debug_neighbor_events(peer
))
1445 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1450 return (bgp_stop(peer
));
1454 /* something went wrong, send notify and tear down */
1455 static int bgp_stop_with_notify(struct peer
*peer
, uint8_t code
,
1458 /* Send notify to remote peer */
1459 bgp_notify_send(peer
, code
, sub_code
);
1461 if (peer_dynamic_neighbor(peer
)) {
1462 if (bgp_debug_neighbor_events(peer
))
1463 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1468 /* Clear start timer value to default. */
1469 peer
->v_start
= BGP_INIT_START_TIMER
;
1471 return (bgp_stop(peer
));
1475 * Determines whether a TCP session has successfully established for a peer and
1476 * events as appropriate.
1478 * This function is called when setting up a new session. After connect() is
1479 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1480 * to wait for connection success or failure. When poll() returns, this
1481 * function is called to evaluate the result.
1483 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1484 * the value of .revents in the case of a closed connection - this function is
1485 * scheduled both for a read and a write event. The write event is triggered
1486 * when the connection is established. A read event is triggered when the
1487 * connection is closed. Thus we need to cancel whichever one did not occur.
1489 static int bgp_connect_check(struct thread
*thread
)
1496 peer
= THREAD_ARG(thread
);
1497 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1498 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1499 assert(!peer
->t_read
);
1500 assert(!peer
->t_write
);
1502 THREAD_OFF(peer
->t_connect_check_r
);
1503 THREAD_OFF(peer
->t_connect_check_w
);
1505 /* Check file descriptor. */
1506 slen
= sizeof(status
);
1507 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1510 /* If getsockopt is fail, this is fatal error. */
1512 zlog_err("can't get sockopt for nonblocking connect: %d(%s)",
1513 errno
, safe_strerror(errno
));
1514 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1518 /* When status is 0 then TCP connection is established. */
1520 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
1521 BGP_EVENT_ADD(peer
, TCP_connection_open_w_delay
);
1523 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1526 if (bgp_debug_neighbor_events(peer
))
1527 zlog_debug("%s [Event] Connect failed %d(%s)",
1528 peer
->host
, status
, safe_strerror(status
));
1529 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1534 /* TCP connection open. Next we send open message to remote peer. And
1535 add read thread for reading open message. */
1536 static int bgp_connect_success(struct peer
*peer
)
1539 flog_err(EC_BGP_CONNECT
,
1540 "bgp_connect_success peer's fd is negative value %d",
1546 if (bgp_getsockname(peer
) < 0) {
1547 flog_err_sys(EC_LIB_SOCKET
,
1548 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1549 __func__
, peer
->host
, peer
->fd
);
1550 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1551 bgp_fsm_error_subcode(peer
->status
));
1552 bgp_writes_on(peer
);
1558 if (bgp_debug_neighbor_events(peer
)) {
1559 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1560 zlog_debug("%s open active, local address %pSU",
1561 peer
->host
, peer
->su_local
);
1563 zlog_debug("%s passive open", peer
->host
);
1566 /* Send an open message */
1567 bgp_open_send(peer
);
1572 /* TCP connection open with RFC 4271 optional session attribute DelayOpen flag
1575 static int bgp_connect_success_w_delayopen(struct peer
*peer
)
1578 flog_err(EC_BGP_CONNECT
, "%s: peer's fd is negative value %d",
1579 __func__
, peer
->fd
);
1584 if (bgp_getsockname(peer
) < 0) {
1585 flog_err_sys(EC_LIB_SOCKET
,
1586 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1587 __func__
, peer
->host
, peer
->fd
);
1588 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1589 bgp_fsm_error_subcode(peer
->status
));
1590 bgp_writes_on(peer
);
1596 if (bgp_debug_neighbor_events(peer
)) {
1597 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1598 zlog_debug("%s open active, local address %pSU",
1599 peer
->host
, peer
->su_local
);
1601 zlog_debug("%s passive open", peer
->host
);
1604 /* set the DelayOpenTime to the inital value */
1605 peer
->v_delayopen
= peer
->delayopen
;
1607 /* Start the DelayOpenTimer if it is not already running */
1608 if (!peer
->t_delayopen
)
1609 BGP_TIMER_ON(peer
->t_delayopen
, bgp_delayopen_timer
,
1612 if (bgp_debug_neighbor_events(peer
))
1613 zlog_debug("%s [FSM] BGP OPEN message delayed for %d seconds",
1614 peer
->host
, peer
->delayopen
);
1619 /* TCP connect fail */
1620 static int bgp_connect_fail(struct peer
*peer
)
1622 if (peer_dynamic_neighbor(peer
)) {
1623 if (bgp_debug_neighbor_events(peer
))
1624 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1630 * If we are doing nht for a peer that ls v6 LL based
1631 * massage the event system to make things happy
1633 bgp_nht_interface_events(peer
);
1635 return (bgp_stop(peer
));
1638 /* This function is the first starting point of all BGP connection. It
1639 * try to connect to remote peer with non-blocking IO.
1641 int bgp_start(struct peer
*peer
)
1645 bgp_peer_conf_if_to_su_update(peer
);
1647 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1648 if (bgp_debug_neighbor_events(peer
))
1650 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1652 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1656 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1657 if (bgp_debug_neighbor_events(peer
))
1658 flog_err(EC_BGP_FSM
,
1659 "%s [FSM] Trying to start suppressed peer - this is never supposed to happen!",
1661 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
1662 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
1663 else if (CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
))
1664 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
1665 else if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
1666 peer
->last_reset
= PEER_DOWN_PFX_COUNT
;
1670 /* Scrub some information that might be left over from a previous,
1673 /* Connection information. */
1674 if (peer
->su_local
) {
1675 sockunion_free(peer
->su_local
);
1676 peer
->su_local
= NULL
;
1679 if (peer
->su_remote
) {
1680 sockunion_free(peer
->su_remote
);
1681 peer
->su_remote
= NULL
;
1684 /* Clear remote router-id. */
1685 peer
->remote_id
.s_addr
= INADDR_ANY
;
1687 /* Clear peer capability flag. */
1690 /* If the peer is passive mode, force to move to Active mode. */
1691 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1692 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1696 if (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
1697 peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
1698 if (bgp_debug_neighbor_events(peer
))
1701 "%s [FSM] In a VRF that is not initialised yet",
1703 peer
->last_reset
= PEER_DOWN_VRF_UNINIT
;
1707 /* Register peer for NHT. If next hop is already resolved, proceed
1708 * with connection setup, else wait.
1710 if (!bgp_peer_reg_with_nht(peer
)) {
1711 if (bgp_zebra_num_connects()) {
1712 if (bgp_debug_neighbor_events(peer
))
1713 zlog_debug("%s [FSM] Waiting for NHT",
1715 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1716 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1721 assert(!peer
->t_write
);
1722 assert(!peer
->t_read
);
1723 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1724 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1725 status
= bgp_connect(peer
);
1729 if (bgp_debug_neighbor_events(peer
))
1730 zlog_debug("%s [FSM] Connect error", peer
->host
);
1731 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1733 case connect_success
:
1734 if (bgp_debug_neighbor_events(peer
))
1736 "%s [FSM] Connect immediately success, fd %d",
1737 peer
->host
, peer
->fd
);
1739 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1741 case connect_in_progress
:
1742 /* To check nonblocking connect, we wait until socket is
1743 readable or writable. */
1744 if (bgp_debug_neighbor_events(peer
))
1746 "%s [FSM] Non blocking connect waiting result, fd %d",
1747 peer
->host
, peer
->fd
);
1749 flog_err(EC_BGP_FSM
,
1750 "bgp_start peer's fd is negative value %d",
1755 * - when the socket becomes ready, poll() will signify POLLOUT
1756 * - if it fails to connect, poll() will signify POLLHUP
1757 * - POLLHUP is handled as a 'read' event by thread.c
1759 * therefore, we schedule both a read and a write event with
1760 * bgp_connect_check() as the handler for each and cancel the
1761 * unused event in that function.
1763 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1764 &peer
->t_connect_check_r
);
1765 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1766 &peer
->t_connect_check_w
);
1772 /* Connect retry timer is expired when the peer status is Connect. */
1773 static int bgp_reconnect(struct peer
*peer
)
1775 if (bgp_stop(peer
) < 0)
1778 /* Send graceful restart capabilty */
1779 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
1786 static int bgp_fsm_open(struct peer
*peer
)
1788 /* If DelayOpen is active, we may still need to send an open message */
1789 if ((peer
->status
== Connect
) || (peer
->status
== Active
))
1790 bgp_open_send(peer
);
1792 /* Send keepalive and make keepalive timer */
1793 bgp_keepalive_send(peer
);
1798 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1799 peer and change to Idle status. */
1800 static int bgp_fsm_event_error(struct peer
*peer
)
1802 flog_err(EC_BGP_FSM
, "%s [FSM] unexpected packet received in state %s",
1803 peer
->host
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1805 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
,
1806 bgp_fsm_error_subcode(peer
->status
));
1809 /* Hold timer expire. This is error of BGP connection. So cut the
1810 peer and change to Idle status. */
1811 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1813 if (bgp_debug_neighbor_events(peer
))
1814 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1816 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1819 /* RFC 4271 DelayOpenTimer_Expires event */
1820 static int bgp_fsm_delayopen_timer_expire(struct peer
*peer
)
1822 /* Stop the DelayOpenTimer */
1823 BGP_TIMER_OFF(peer
->t_delayopen
);
1825 /* Send open message to peer */
1826 bgp_open_send(peer
);
1828 /* Set the HoldTimer to a large value (4 minutes) */
1829 peer
->v_holdtime
= 245;
1834 /* Start the selection deferral timer thread for the specified AFI, SAFI */
1835 static int bgp_start_deferral_timer(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1836 struct graceful_restart_info
*gr_info
)
1838 struct afi_safi_info
*thread_info
;
1840 /* If the deferral timer is active, then increment eor count */
1841 if (gr_info
->t_select_deferral
) {
1842 gr_info
->eor_required
++;
1846 /* Start the deferral timer when the first peer enabled for the graceful
1847 * restart is established
1849 if (gr_info
->eor_required
== 0) {
1850 thread_info
= XMALLOC(MTYPE_TMP
, sizeof(struct afi_safi_info
));
1852 thread_info
->afi
= afi
;
1853 thread_info
->safi
= safi
;
1854 thread_info
->bgp
= bgp
;
1856 thread_add_timer(bm
->master
, bgp_graceful_deferral_timer_expire
,
1857 thread_info
, bgp
->select_defer_time
,
1858 &gr_info
->t_select_deferral
);
1860 gr_info
->eor_required
++;
1861 /* Send message to RIB indicating route update pending */
1862 if (gr_info
->af_enabled
[afi
][safi
] == false) {
1863 gr_info
->af_enabled
[afi
][safi
] = true;
1864 /* Send message to RIB */
1865 bgp_zebra_update(afi
, safi
, bgp
->vrf_id
,
1866 ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
);
1868 if (BGP_DEBUG(update
, UPDATE_OUT
))
1869 zlog_debug("Started the deferral timer for %s eor_required %d",
1870 get_afi_safi_str(afi
, safi
, false),
1871 gr_info
->eor_required
);
1875 /* Update the graceful restart information for the specified AFI, SAFI */
1876 static int bgp_update_gr_info(struct peer
*peer
, afi_t afi
, safi_t safi
)
1878 struct graceful_restart_info
*gr_info
;
1879 struct bgp
*bgp
= peer
->bgp
;
1882 if ((afi
< AFI_IP
) || (afi
>= AFI_MAX
)) {
1883 if (BGP_DEBUG(update
, UPDATE_OUT
))
1884 zlog_debug("%s : invalid afi %d", __func__
, afi
);
1888 if ((safi
< SAFI_UNICAST
) || (safi
> SAFI_MPLS_VPN
)) {
1889 if (BGP_DEBUG(update
, UPDATE_OUT
))
1890 zlog_debug("%s : invalid safi %d", __func__
, safi
);
1894 /* Restarting router */
1895 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
1896 && BGP_PEER_RESTARTING_MODE(peer
)) {
1897 /* Check if the forwarding state is preserved */
1898 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_GR_PRESERVE_FWD
)) {
1899 gr_info
= &(bgp
->gr_info
[afi
][safi
]);
1900 ret
= bgp_start_deferral_timer(bgp
, afi
, safi
, gr_info
);
1907 * Transition to Established state.
1909 * Convert peer from stub to full fledged peer, set some timers, and generate
1912 static int bgp_establish(struct peer
*peer
)
1916 int nsf_af_count
= 0;
1921 other
= peer
->doppelganger
;
1922 peer
= peer_xfer_conn(peer
);
1924 flog_err(EC_BGP_CONNECT
, "%%Neighbor failed in xfer_conn");
1929 ret
= 1; /* bgp_establish specific code when xfer_conn
1932 /* Reset capability open status flag. */
1933 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1934 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1936 /* Clear start timer value to default. */
1937 peer
->v_start
= BGP_INIT_START_TIMER
;
1939 /* Increment established count. */
1940 peer
->established
++;
1941 bgp_fsm_change_status(peer
, Established
);
1943 /* bgp log-neighbor-changes of neighbor Up */
1944 if (CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1945 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1947 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Up", peer
->host
,
1948 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1949 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1953 /* assign update-group/subgroup */
1954 update_group_adjust_peer_afs(peer
);
1956 /* graceful restart */
1957 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1958 if (bgp_debug_neighbor_events(peer
)) {
1959 if (BGP_PEER_RESTARTING_MODE(peer
))
1960 zlog_debug("peer %s BGP_RESTARTING_MODE", peer
->host
);
1961 else if (BGP_PEER_HELPER_MODE(peer
))
1962 zlog_debug("peer %s BGP_HELPER_MODE", peer
->host
);
1964 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1965 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1966 if (peer
->afc_nego
[afi
][safi
]
1967 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1968 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1969 PEER_CAP_RESTART_AF_RCV
)) {
1970 if (peer
->nsf
[afi
][safi
]
1972 peer
->af_cap
[afi
][safi
],
1973 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1974 bgp_clear_stale_route(peer
, afi
, safi
);
1976 peer
->nsf
[afi
][safi
] = 1;
1979 if (peer
->nsf
[afi
][safi
])
1980 bgp_clear_stale_route(peer
, afi
, safi
);
1981 peer
->nsf
[afi
][safi
] = 0;
1983 /* Update the graceful restart information */
1984 if (peer
->afc_nego
[afi
][safi
]) {
1985 if (!BGP_SELECT_DEFER_DISABLE(peer
->bgp
)) {
1986 status
= bgp_update_gr_info(peer
, afi
,
1990 "Error in updating graceful restart for %s",
1995 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(
1997 && BGP_PEER_RESTARTING_MODE(peer
)
2000 BGP_FLAG_GR_PRESERVE_FWD
))
2001 peer
->bgp
->gr_info
[afi
][safi
]
2007 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
2008 if ((bgp_peer_gr_mode_get(peer
) == PEER_GR
)
2009 || ((bgp_peer_gr_mode_get(peer
) == PEER_GLOBAL_INHERIT
)
2010 && (bgp_global_gr_mode_get(peer
->bgp
) == GLOBAL_GR
))) {
2011 FOREACH_AFI_SAFI (afi
, safi
)
2012 /* Send route processing complete
2015 afi
, safi
, peer
->bgp
->vrf_id
,
2016 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
);
2019 /* Peer sends R-bit. In this case, we need to send
2020 * ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE to Zebra. */
2021 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
)) {
2022 FOREACH_AFI_SAFI (afi
, safi
)
2023 /* Send route processing complete
2026 afi
, safi
, peer
->bgp
->vrf_id
,
2027 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
);
2031 peer
->nsf_af_count
= nsf_af_count
;
2034 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2036 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2037 if (peer
->t_gr_stale
) {
2038 BGP_TIMER_OFF(peer
->t_gr_stale
);
2039 if (bgp_debug_neighbor_events(peer
))
2041 "%s graceful restart stalepath timer stopped",
2046 if (peer
->t_gr_restart
) {
2047 BGP_TIMER_OFF(peer
->t_gr_restart
);
2048 if (bgp_debug_neighbor_events(peer
))
2049 zlog_debug("%s graceful restart timer stopped",
2053 /* Reset uptime, turn on keepalives, send current table. */
2054 if (!peer
->v_holdtime
)
2055 bgp_keepalives_on(peer
);
2057 peer
->uptime
= bgp_clock();
2059 /* Send route-refresh when ORF is enabled */
2060 FOREACH_AFI_SAFI (afi
, safi
) {
2061 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2062 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
2063 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2064 PEER_CAP_ORF_PREFIX_RM_RCV
))
2065 bgp_route_refresh_send(
2066 peer
, afi
, safi
, ORF_TYPE_PREFIX
,
2067 REFRESH_IMMEDIATE
, 0,
2068 BGP_ROUTE_REFRESH_NORMAL
);
2069 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2070 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
2071 bgp_route_refresh_send(
2072 peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
2073 REFRESH_IMMEDIATE
, 0,
2074 BGP_ROUTE_REFRESH_NORMAL
);
2078 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2079 FOREACH_AFI_SAFI (afi
, safi
) {
2080 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2081 PEER_CAP_ORF_PREFIX_RM_ADV
))
2082 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2083 PEER_CAP_ORF_PREFIX_SM_RCV
)
2084 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2085 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
2086 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2087 PEER_STATUS_ORF_WAIT_REFRESH
);
2090 bgp_announce_peer(peer
);
2092 /* Start the route advertisement timer to send updates to the peer - if
2094 * is not in read-only mode. If it is, the timer will be started at the
2096 * of read-only mode.
2098 if (!bgp_update_delay_active(peer
->bgp
)) {
2099 BGP_TIMER_OFF(peer
->t_routeadv
);
2100 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
2103 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
2104 if (bgp_debug_neighbor_events(peer
))
2106 "[Event] Deleting stub connection for peer %s",
2109 if (peer
->doppelganger
->status
> Active
)
2110 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
2111 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
2113 peer_delete(peer
->doppelganger
);
2117 * If we are replacing the old peer for a doppelganger
2118 * then switch it around in the bgp->peerhash
2119 * the doppelgangers su and this peer's su are the same
2120 * so the hash_release is the same for either.
2122 hash_release(peer
->bgp
->peerhash
, peer
);
2123 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
2125 bgp_bfd_reset_peer(peer
);
2129 /* Keepalive packet is received. */
2130 static int bgp_fsm_keepalive(struct peer
*peer
)
2132 BGP_TIMER_OFF(peer
->t_holdtime
);
2136 /* Update packet is received. */
2137 static int bgp_fsm_update(struct peer
*peer
)
2139 BGP_TIMER_OFF(peer
->t_holdtime
);
2143 /* This is empty event. */
2144 static int bgp_ignore(struct peer
*peer
)
2148 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
2149 peer
->host
, bgp_event_str
[peer
->cur_event
],
2150 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2151 bgp_event_str
[peer
->last_event
],
2152 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
2156 /* This is to handle unexpected events.. */
2157 static int bgp_fsm_exeption(struct peer
*peer
)
2161 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
2162 peer
->host
, bgp_event_str
[peer
->cur_event
],
2163 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2164 bgp_event_str
[peer
->last_event
],
2165 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
2166 return (bgp_stop(peer
));
2169 void bgp_fsm_nht_update(struct peer
*peer
, bool has_valid_nexthops
)
2174 switch (peer
->status
) {
2176 if (has_valid_nexthops
)
2177 BGP_EVENT_ADD(peer
, BGP_Start
);
2180 if (!has_valid_nexthops
) {
2181 BGP_TIMER_OFF(peer
->t_connect
);
2182 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
2186 if (has_valid_nexthops
) {
2187 BGP_TIMER_OFF(peer
->t_connect
);
2188 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
2194 if (!has_valid_nexthops
2195 && (peer
->gtsm_hops
== BGP_GTSM_HOPS_CONNECTED
))
2196 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
2204 /* Finite State Machine structure */
2205 static const struct {
2206 int (*func
)(struct peer
*);
2207 enum bgp_fsm_status next_state
;
2208 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
2210 /* Idle state: In Idle state, all events other than BGP_Start is
2211 ignored. With BGP_Start event, finite state machine calls
2213 {bgp_start
, Connect
}, /* BGP_Start */
2214 {bgp_stop
, Idle
}, /* BGP_Stop */
2215 {bgp_stop
, Idle
}, /* TCP_connection_open */
2216 {bgp_stop
, Idle
}, /* TCP_connection_open_w_delay */
2217 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2218 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
2219 {bgp_stop
, Idle
}, /* TCP_fatal_error */
2220 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
2221 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
2222 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
2223 {bgp_ignore
, Idle
}, /* DelayOpen_timer_expired */
2224 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
2225 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
2226 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
2227 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
2228 {bgp_ignore
, Idle
}, /* Clearing_Completed */
2232 {bgp_ignore
, Connect
}, /* BGP_Start */
2233 {bgp_stop
, Idle
}, /* BGP_Stop */
2234 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
2235 {bgp_connect_success_w_delayopen
,
2236 Connect
}, /* TCP_connection_open_w_delay */
2237 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2238 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
2239 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
2240 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
2241 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
2242 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2243 {bgp_fsm_delayopen_timer_expire
,
2244 OpenSent
}, /* DelayOpen_timer_expired */
2245 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2246 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
2247 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2248 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
2249 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2253 {bgp_ignore
, Active
}, /* BGP_Start */
2254 {bgp_stop
, Idle
}, /* BGP_Stop */
2255 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
2256 {bgp_connect_success_w_delayopen
,
2257 Active
}, /* TCP_connection_open_w_delay */
2258 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2259 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
2260 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
2261 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
2262 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
2263 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2264 {bgp_fsm_delayopen_timer_expire
,
2265 OpenSent
}, /* DelayOpen_timer_expired */
2266 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2267 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
2268 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2269 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
2270 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2274 {bgp_ignore
, OpenSent
}, /* BGP_Start */
2275 {bgp_stop
, Idle
}, /* BGP_Stop */
2276 {bgp_stop
, Active
}, /* TCP_connection_open */
2277 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2278 {bgp_stop
, Active
}, /* TCP_connection_closed */
2279 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
2280 {bgp_stop
, Active
}, /* TCP_fatal_error */
2281 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
2282 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
2283 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2284 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2285 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2286 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
2287 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
2288 {bgp_fsm_event_error
, Idle
}, /* Receive_NOTIFICATION_message */
2289 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2293 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
2294 {bgp_stop
, Idle
}, /* BGP_Stop */
2295 {bgp_stop
, Idle
}, /* TCP_connection_open */
2296 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2297 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2298 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
2299 {bgp_stop
, Idle
}, /* TCP_fatal_error */
2300 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
2301 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
2302 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
2303 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2304 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
2305 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
2306 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2307 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
2308 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2312 {bgp_ignore
, Established
}, /* BGP_Start */
2313 {bgp_stop
, Clearing
}, /* BGP_Stop */
2314 {bgp_stop
, Clearing
}, /* TCP_connection_open */
2315 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2316 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
2317 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
2318 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
2319 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
2320 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
2321 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
2322 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2323 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
2325 Established
}, /* Receive_KEEPALIVE_message */
2326 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
2327 {bgp_stop_with_error
,
2328 Clearing
}, /* Receive_NOTIFICATION_message */
2329 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2333 {bgp_ignore
, Clearing
}, /* BGP_Start */
2334 {bgp_stop
, Clearing
}, /* BGP_Stop */
2335 {bgp_stop
, Clearing
}, /* TCP_connection_open */
2336 {bgp_stop
, Clearing
}, /* TCP_connection_open_w_delay */
2337 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
2338 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
2339 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
2340 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
2341 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
2342 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
2343 {bgp_stop
, Clearing
}, /* DelayOpen_timer_expired */
2344 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
2345 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
2346 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
2347 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
2348 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
2352 {bgp_ignore
, Deleted
}, /* BGP_Start */
2353 {bgp_ignore
, Deleted
}, /* BGP_Stop */
2354 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
2355 {bgp_ignore
, Deleted
}, /* TCP_connection_open_w_delay */
2356 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
2357 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
2358 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
2359 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
2360 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
2361 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
2362 {bgp_ignore
, Deleted
}, /* DelayOpen_timer_expired */
2363 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
2364 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
2365 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
2366 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
2367 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
2371 /* Execute event process. */
2372 int bgp_event(struct thread
*thread
)
2374 enum bgp_fsm_events event
;
2378 peer
= THREAD_ARG(thread
);
2379 event
= THREAD_VAL(thread
);
2381 ret
= bgp_event_update(peer
, event
);
2386 int bgp_event_update(struct peer
*peer
, enum bgp_fsm_events event
)
2388 enum bgp_fsm_status next
;
2391 int passive_conn
= 0;
2394 /* default return code */
2395 ret
= FSM_PEER_NOOP
;
2397 other
= peer
->doppelganger
;
2399 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
2400 dyn_nbr
= peer_dynamic_neighbor(peer
);
2402 /* Logging this event. */
2403 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
2405 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
2406 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
2407 bgp_event_str
[event
],
2408 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2409 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
2411 peer
->last_event
= peer
->cur_event
;
2412 peer
->cur_event
= event
;
2414 /* Call function. */
2415 if (FSM
[peer
->status
- 1][event
- 1].func
)
2416 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
2419 if (ret
== 1 && next
== Established
) {
2420 /* The case when doppelganger swap accurred in
2422 Update the peer pointer accordingly */
2423 ret
= FSM_PEER_TRANSFERRED
;
2427 /* If status is changed. */
2428 if (next
!= peer
->status
) {
2429 bgp_fsm_change_status(peer
, next
);
2432 * If we're going to ESTABLISHED then we executed a
2433 * peer transfer. In this case we can either return
2434 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
2435 * Opting for TRANSFERRED since transfer implies
2436 * session establishment.
2438 if (ret
!= FSM_PEER_TRANSFERRED
)
2439 ret
= FSM_PEER_TRANSITIONED
;
2442 /* Make sure timer is set. */
2443 bgp_timer_set(peer
);
2447 * If we got a return value of -1, that means there was an
2448 * error, restart the FSM. Since bgp_stop() was called on the
2449 * peer. only a few fields are safe to access here. In any case
2450 * we need to indicate that the peer was stopped in the return
2453 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
2456 "%s [FSM] Failure handling event %s in state %s, prior events %s, %s, fd %d",
2457 peer
->host
, bgp_event_str
[peer
->cur_event
],
2458 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2459 bgp_event_str
[peer
->last_event
],
2460 bgp_event_str
[peer
->last_major_event
],
2463 bgp_fsm_change_status(peer
, Idle
);
2464 bgp_timer_set(peer
);
2466 ret
= FSM_PEER_STOPPED
;
2473 int bgp_gr_lookup_n_update_all_peer(struct bgp
*bgp
,
2474 enum global_mode global_new_state
,
2475 enum global_mode global_old_state
)
2477 struct peer
*peer
= {0};
2478 struct listnode
*node
= {0};
2479 struct listnode
*nnode
= {0};
2480 enum peer_mode peer_old_state
= PEER_INVALID
;
2482 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
2484 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2485 zlog_debug("%s [BGP_GR] Peer: (%s) :", __func__
,
2488 peer_old_state
= bgp_peer_gr_mode_get(peer
);
2490 if (peer_old_state
== PEER_GLOBAL_INHERIT
) {
2493 *Reset only these peers and send a
2494 *new open message with the change capabilities.
2495 *Considering the mode to be "global_new_state" and
2496 *do all operation accordingly
2499 switch (global_new_state
) {
2501 BGP_PEER_GR_HELPER_ENABLE(peer
);
2504 BGP_PEER_GR_ENABLE(peer
);
2506 case GLOBAL_DISABLE
:
2507 BGP_PEER_GR_DISABLE(peer
);
2509 case GLOBAL_INVALID
:
2510 zlog_debug("%s [BGP_GR] GLOBAL_INVALID",
2512 return BGP_ERR_GR_OPERATION_FAILED
;
2517 bgp
->global_gr_present_state
= global_new_state
;
2519 return BGP_GR_SUCCESS
;
2522 int bgp_gr_update_all(struct bgp
*bgp
, int global_gr_cmd
)
2524 enum global_mode global_new_state
= GLOBAL_INVALID
;
2525 enum global_mode global_old_state
= GLOBAL_INVALID
;
2527 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2528 zlog_debug("%s [BGP_GR]START: global_gr_cmd :%s:", __func__
,
2529 print_global_gr_cmd(global_gr_cmd
));
2531 global_old_state
= bgp_global_gr_mode_get(bgp
);
2533 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2534 zlog_debug("[BGP_GR] global_old_gr_state :%s:",
2535 print_global_gr_mode(global_old_state
));
2537 if (global_old_state
!= GLOBAL_INVALID
) {
2539 bgp
->GLOBAL_GR_FSM
[global_old_state
][global_gr_cmd
];
2541 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2542 zlog_debug("[BGP_GR] global_new_gr_state :%s:",
2543 print_global_gr_mode(global_new_state
));
2545 zlog_err("%s [BGP_GR] global_old_state == GLOBAL_INVALID",
2547 return BGP_ERR_GR_OPERATION_FAILED
;
2550 if (global_new_state
== GLOBAL_INVALID
) {
2551 zlog_err("%s [BGP_GR] global_new_state == GLOBAL_INVALID",
2553 return BGP_ERR_GR_INVALID_CMD
;
2555 if (global_new_state
== global_old_state
) {
2557 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2559 "%s [BGP_GR] global_new_state == global_old_state :%s",
2561 print_global_gr_mode(global_new_state
));
2562 return BGP_GR_NO_OPERATION
;
2565 return bgp_gr_lookup_n_update_all_peer(bgp
, global_new_state
,
2569 const char *print_peer_gr_mode(enum peer_mode pr_mode
)
2571 const char *peer_gr_mode
= NULL
;
2575 peer_gr_mode
= "PEER_HELPER";
2578 peer_gr_mode
= "PEER_GR";
2581 peer_gr_mode
= "PEER_DISABLE";
2584 peer_gr_mode
= "PEER_INVALID";
2586 case PEER_GLOBAL_INHERIT
:
2587 peer_gr_mode
= "PEER_GLOBAL_INHERIT";
2591 return peer_gr_mode
;
2594 const char *print_peer_gr_cmd(enum peer_gr_command pr_gr_cmd
)
2596 const char *peer_gr_cmd
= NULL
;
2598 switch (pr_gr_cmd
) {
2600 peer_gr_cmd
= "PEER_GR_CMD";
2602 case NO_PEER_GR_CMD
:
2603 peer_gr_cmd
= "NO_PEER_GR_CMD";
2605 case PEER_DISABLE_CMD
:
2606 peer_gr_cmd
= "PEER_GR_CMD";
2608 case NO_PEER_DISABLE_CMD
:
2609 peer_gr_cmd
= "NO_PEER_GR_CMD";
2611 case PEER_HELPER_CMD
:
2612 peer_gr_cmd
= "PEER_HELPER_CMD";
2614 case NO_PEER_HELPER_CMD
:
2615 peer_gr_cmd
= "NO_PEER_HELPER_CMD";
2622 const char *print_global_gr_mode(enum global_mode gl_mode
)
2624 const char *global_gr_mode
= NULL
;
2628 global_gr_mode
= "GLOBAL_HELPER";
2631 global_gr_mode
= "GLOBAL_GR";
2633 case GLOBAL_DISABLE
:
2634 global_gr_mode
= "GLOBAL_DISABLE";
2636 case GLOBAL_INVALID
:
2637 global_gr_mode
= "GLOBAL_INVALID";
2641 return global_gr_mode
;
2644 const char *print_global_gr_cmd(enum global_gr_command gl_gr_cmd
)
2646 const char *global_gr_cmd
= NULL
;
2648 switch (gl_gr_cmd
) {
2650 global_gr_cmd
= "GLOBAL_GR_CMD";
2652 case NO_GLOBAL_GR_CMD
:
2653 global_gr_cmd
= "NO_GLOBAL_GR_CMD";
2655 case GLOBAL_DISABLE_CMD
:
2656 global_gr_cmd
= "GLOBAL_DISABLE_CMD";
2658 case NO_GLOBAL_DISABLE_CMD
:
2659 global_gr_cmd
= "NO_GLOBAL_DISABLE_CMD";
2663 return global_gr_cmd
;
2666 enum global_mode
bgp_global_gr_mode_get(struct bgp
*bgp
)
2668 return bgp
->global_gr_present_state
;
2671 enum peer_mode
bgp_peer_gr_mode_get(struct peer
*peer
)
2673 return peer
->peer_gr_present_state
;
2676 int bgp_neighbor_graceful_restart(struct peer
*peer
, int peer_gr_cmd
)
2678 enum peer_mode peer_new_state
= PEER_INVALID
;
2679 enum peer_mode peer_old_state
= PEER_INVALID
;
2680 struct bgp_peer_gr peer_state
;
2681 int result
= BGP_GR_FAILURE
;
2684 * fetch peer_old_state from peer structure also
2685 * fetch global_old_state from bgp structure,
2686 * peer had a back pointer to bgpo struct ;
2689 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2690 zlog_debug("%s [BGP_GR] START:Peer: (%s) : peer_gr_cmd :%s:",
2691 __func__
, peer
->host
,
2692 print_peer_gr_cmd(peer_gr_cmd
));
2694 peer_old_state
= bgp_peer_gr_mode_get(peer
);
2696 if (peer_old_state
== PEER_INVALID
) {
2697 zlog_debug("[BGP_GR] peer_old_state == Invalid state !");
2698 return BGP_ERR_GR_OPERATION_FAILED
;
2701 peer_state
= peer
->PEER_GR_FSM
[peer_old_state
][peer_gr_cmd
];
2702 peer_new_state
= peer_state
.next_state
;
2704 if (peer_new_state
== PEER_INVALID
) {
2706 "[BGP_GR] Invalid bgp graceful restart command used !");
2707 return BGP_ERR_GR_INVALID_CMD
;
2710 if (peer_new_state
!= peer_old_state
) {
2711 result
= peer_state
.action_fun(peer
, peer_old_state
,
2714 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2716 "[BGP_GR] peer_old_state == peer_new_state !");
2717 return BGP_GR_NO_OPERATION
;
2720 if (result
== BGP_GR_SUCCESS
) {
2722 /* Update the mode i.e peer_new_state into the peer structure */
2723 peer
->peer_gr_present_state
= peer_new_state
;
2724 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2726 "[BGP_GR] Successfully change the state of the peer to : %s : !",
2727 print_peer_gr_mode(peer_new_state
));
2729 return BGP_GR_SUCCESS
;
2735 unsigned int bgp_peer_gr_action(struct peer
*peer
, int old_peer_state
,
2738 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2740 "%s [BGP_GR] Move peer from old_peer_state :%s: to new_peer_state :%s: !!!!",
2741 __func__
, print_peer_gr_mode(old_peer_state
),
2742 print_peer_gr_mode(new_peer_state
));
2744 int bgp_gr_global_mode
= GLOBAL_INVALID
;
2745 unsigned int ret
= BGP_GR_FAILURE
;
2747 if (old_peer_state
== new_peer_state
) {
2748 /* Nothing to do over here as the present and old state is the
2750 return BGP_GR_NO_OPERATION
;
2752 if ((old_peer_state
== PEER_INVALID
)
2753 || (new_peer_state
== PEER_INVALID
)) {
2754 /* something bad happend , print error message */
2755 return BGP_ERR_GR_INVALID_CMD
;
2758 bgp_gr_global_mode
= bgp_global_gr_mode_get(peer
->bgp
);
2760 if ((old_peer_state
== PEER_GLOBAL_INHERIT
)
2761 && (new_peer_state
!= PEER_GLOBAL_INHERIT
)) {
2763 /* fetch the Mode running in the Global state machine
2764 *from the bgp structure into a variable called
2768 /* Here we are checking if the
2769 *1. peer_new_state == global_mode == helper_mode
2770 *2. peer_new_state == global_mode == GR_mode
2771 *3. peer_new_state == global_mode == disabled_mode
2774 BGP_PEER_GR_GLOBAL_INHERIT_UNSET(peer
);
2776 if (new_peer_state
== bgp_gr_global_mode
) {
2777 /*This is incremental updates i.e no tear down
2778 *of the existing session
2779 *as the peer is already working in the same mode.
2781 ret
= BGP_GR_SUCCESS
;
2783 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2785 "[BGP_GR] Peer state changed from :%s ",
2786 print_peer_gr_mode(old_peer_state
));
2788 bgp_peer_move_to_gr_mode(peer
, new_peer_state
);
2790 ret
= BGP_GR_SUCCESS
;
2793 /* In the case below peer is going into Global inherit mode i.e.
2794 * the peer would work as the mode configured at the global level
2796 else if ((new_peer_state
== PEER_GLOBAL_INHERIT
)
2797 && (old_peer_state
!= PEER_GLOBAL_INHERIT
)) {
2798 /* Here in this case it would be destructive
2799 * in all the cases except one case when,
2800 * Global GR is configured Disabled
2801 * and present_peer_state is not disable
2804 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer
);
2806 if (old_peer_state
== bgp_gr_global_mode
) {
2808 /* This is incremental updates
2809 *i.e no tear down of the existing session
2810 *as the peer is already working in the same mode.
2812 ret
= BGP_GR_SUCCESS
;
2814 /* Destructive always */
2815 /* Tear down the old session
2816 * and send the new capability
2817 * as per the bgp_gr_global_mode
2820 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2822 "[BGP_GR] Peer state changed from :%s",
2823 print_peer_gr_mode(old_peer_state
));
2825 bgp_peer_move_to_gr_mode(peer
, bgp_gr_global_mode
);
2827 ret
= BGP_GR_SUCCESS
;
2831 *This else case, it include all the cases except -->
2832 *(new_peer_state != Peer_Global) &&
2833 *( old_peer_state != Peer_Global )
2835 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2836 zlog_debug("[BGP_GR] Peer state changed from :%s",
2837 print_peer_gr_mode(old_peer_state
));
2839 bgp_peer_move_to_gr_mode(peer
, new_peer_state
);
2841 ret
= BGP_GR_SUCCESS
;
2847 inline void bgp_peer_move_to_gr_mode(struct peer
*peer
, int new_state
)
2850 int bgp_global_gr_mode
= bgp_global_gr_mode_get(peer
->bgp
);
2852 switch (new_state
) {
2854 BGP_PEER_GR_HELPER_ENABLE(peer
);
2857 BGP_PEER_GR_ENABLE(peer
);
2860 BGP_PEER_GR_DISABLE(peer
);
2862 case PEER_GLOBAL_INHERIT
:
2863 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer
);
2865 if (bgp_global_gr_mode
== GLOBAL_HELPER
) {
2866 BGP_PEER_GR_HELPER_ENABLE(peer
);
2867 } else if (bgp_global_gr_mode
== GLOBAL_GR
) {
2868 BGP_PEER_GR_ENABLE(peer
);
2869 } else if (bgp_global_gr_mode
== GLOBAL_DISABLE
) {
2870 BGP_PEER_GR_DISABLE(peer
);
2873 "[BGP_GR] Default switch inherit mode ::: SOMETHING IS WRONG !!!");
2878 "[BGP_GR] Default switch mode ::: SOMETHING IS WRONG !!!");
2881 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2882 zlog_debug("[BGP_GR] Peer state changed --to--> : %d : !",
2886 void bgp_peer_gr_flags_update(struct peer
*peer
)
2888 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2889 zlog_debug("%s [BGP_GR] called !", __func__
);
2890 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
2891 PEER_GRACEFUL_RESTART_NEW_STATE_HELPER
))
2892 SET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
);
2894 UNSET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
);
2895 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2897 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_HELPER : %s : !",
2899 (CHECK_FLAG(peer
->flags
,
2900 PEER_FLAG_GRACEFUL_RESTART_HELPER
)
2903 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
2904 PEER_GRACEFUL_RESTART_NEW_STATE_RESTART
))
2905 SET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
);
2907 UNSET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
);
2908 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2910 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART : %s : !",
2912 (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
2915 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
2916 PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT
))
2917 SET_FLAG(peer
->flags
,
2918 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
);
2920 UNSET_FLAG(peer
->flags
,
2921 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
);
2922 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2924 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT : %s : !",
2926 (CHECK_FLAG(peer
->flags
,
2927 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
)
2931 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
2932 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
)) {
2933 zlog_debug("[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_MODE!",
2936 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2938 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
2940 peer_nsf_stop(peer
);
2942 "[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_WAIT!",