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_community.h"
52 #include "bgpd/bgp_updgrp.h"
53 #include "bgpd/bgp_nht.h"
54 #include "bgpd/bgp_bfd.h"
55 #include "bgpd/bgp_memory.h"
56 #include "bgpd/bgp_keepalives.h"
57 #include "bgpd/bgp_io.h"
58 #include "bgpd/bgp_zebra.h"
59 #include "bgpd/bgp_vty.h"
61 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
));
62 DEFINE_HOOK(peer_status_changed
, (struct peer
* peer
), (peer
));
64 /* Definition of display strings corresponding to FSM events. This should be
65 * kept consistent with the events defined in bgpd.h
67 static const char *const bgp_event_str
[] = {
71 "TCP_connection_open",
72 "TCP_connection_open_w_delay",
73 "TCP_connection_closed",
74 "TCP_connection_open_failed",
76 "ConnectRetry_timer_expired",
78 "KeepAlive_timer_expired",
79 "DelayOpen_timer_expired",
80 "Receive_OPEN_message",
81 "Receive_KEEPALIVE_message",
82 "Receive_UPDATE_message",
83 "Receive_NOTIFICATION_message",
87 /* BGP FSM (finite state machine) has three types of functions. Type
88 one is thread functions. Type two is event functions. Type three
89 is FSM functions. Timer functions are set by bgp_timer_set
92 /* BGP event function. */
93 int bgp_event(struct thread
*);
95 /* BGP thread functions. */
96 static int bgp_start_timer(struct thread
*);
97 static int bgp_connect_timer(struct thread
*);
98 static int bgp_holdtime_timer(struct thread
*);
99 static int bgp_delayopen_timer(struct thread
*);
101 /* BGP FSM functions. */
102 static int bgp_start(struct peer
*);
104 /* Register peer with NHT */
105 int bgp_peer_reg_with_nht(struct peer
*peer
)
109 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== BGP_DEFAULT_TTL
110 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
111 && !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
114 return bgp_find_or_add_nexthop(
115 peer
->bgp
, peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
116 SAFI_UNICAST
, NULL
, peer
, connected
, NULL
);
119 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
121 /* Copy stats over. These are only the pre-established state stats */
122 peer_dst
->open_in
+= peer_src
->open_in
;
123 peer_dst
->open_out
+= peer_src
->open_out
;
124 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
125 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
126 peer_dst
->notify_in
+= peer_src
->notify_in
;
127 peer_dst
->notify_out
+= peer_src
->notify_out
;
128 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
129 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
132 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
138 enum bgp_fsm_status status
, pstatus
;
139 enum bgp_fsm_events last_evt
, last_maj_evt
;
141 assert(from_peer
!= NULL
);
143 peer
= from_peer
->doppelganger
;
145 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
149 * Let's check that we are not going to loose known configuration
150 * state based upon doppelganger rules.
152 FOREACH_AFI_SAFI (afi
, safi
) {
153 if (from_peer
->afc
[afi
][safi
] != peer
->afc
[afi
][safi
]) {
155 EC_BGP_DOPPELGANGER_CONFIG
,
156 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
162 if (bgp_debug_neighbor_events(peer
))
163 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
164 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
167 bgp_writes_off(peer
);
169 bgp_writes_off(from_peer
);
170 bgp_reads_off(from_peer
);
173 * Before exchanging FD remove doppelganger from
174 * keepalive peer hash. It could be possible conf peer
175 * fd is set to -1. If blocked on lock then keepalive
176 * thread can access peer pointer with fd -1.
178 bgp_keepalives_off(from_peer
);
180 BGP_TIMER_OFF(peer
->t_routeadv
);
181 BGP_TIMER_OFF(peer
->t_connect
);
182 BGP_TIMER_OFF(peer
->t_delayopen
);
183 BGP_TIMER_OFF(peer
->t_connect_check_r
);
184 BGP_TIMER_OFF(peer
->t_connect_check_w
);
185 BGP_TIMER_OFF(from_peer
->t_routeadv
);
186 BGP_TIMER_OFF(from_peer
->t_connect
);
187 BGP_TIMER_OFF(from_peer
->t_delayopen
);
188 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
189 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
190 BGP_TIMER_OFF(from_peer
->t_process_packet
);
193 * At this point in time, it is possible that there are packets pending
194 * on various buffers. Those need to be transferred or dropped,
195 * otherwise we'll get spurious failures during session establishment.
197 frr_with_mutex(&peer
->io_mtx
, &from_peer
->io_mtx
) {
199 peer
->fd
= from_peer
->fd
;
202 stream_fifo_clean(peer
->ibuf
);
203 stream_fifo_clean(peer
->obuf
);
206 * this should never happen, since bgp_process_packet() is the
207 * only task that sets and unsets the current packet and it
208 * runs in our pthread.
213 "[%s] Dropping pending packet on connection transfer:",
215 /* there used to be a bgp_packet_dump call here, but
216 * that's extremely confusing since there's no way to
217 * identify the packet in MRT dumps or BMP as dropped
218 * due to connection transfer.
220 stream_free(peer
->curr
);
224 // copy each packet from old peer's output queue to new peer
225 while (from_peer
->obuf
->head
)
226 stream_fifo_push(peer
->obuf
,
227 stream_fifo_pop(from_peer
->obuf
));
229 // copy each packet from old peer's input queue to new peer
230 while (from_peer
->ibuf
->head
)
231 stream_fifo_push(peer
->ibuf
,
232 stream_fifo_pop(from_peer
->ibuf
));
234 ringbuf_wipe(peer
->ibuf_work
);
235 ringbuf_copy(peer
->ibuf_work
, from_peer
->ibuf_work
,
236 ringbuf_remain(from_peer
->ibuf_work
));
239 peer
->as
= from_peer
->as
;
240 peer
->v_holdtime
= from_peer
->v_holdtime
;
241 peer
->v_keepalive
= from_peer
->v_keepalive
;
242 peer
->v_routeadv
= from_peer
->v_routeadv
;
243 peer
->v_delayopen
= from_peer
->v_delayopen
;
244 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
245 peer
->cap
= from_peer
->cap
;
246 status
= peer
->status
;
247 pstatus
= peer
->ostatus
;
248 last_evt
= peer
->last_event
;
249 last_maj_evt
= peer
->last_major_event
;
250 peer
->status
= from_peer
->status
;
251 peer
->ostatus
= from_peer
->ostatus
;
252 peer
->last_event
= from_peer
->last_event
;
253 peer
->last_major_event
= from_peer
->last_major_event
;
254 from_peer
->status
= status
;
255 from_peer
->ostatus
= pstatus
;
256 from_peer
->last_event
= last_evt
;
257 from_peer
->last_major_event
= last_maj_evt
;
258 peer
->remote_id
= from_peer
->remote_id
;
259 peer
->last_reset
= from_peer
->last_reset
;
260 peer
->max_packet_size
= from_peer
->max_packet_size
;
262 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
265 if (bgp_peer_gr_mode_get(peer
) == PEER_DISABLE
) {
267 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
269 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
274 if (from_peer
->hostname
!= NULL
) {
275 if (peer
->hostname
) {
276 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
277 peer
->hostname
= NULL
;
280 peer
->hostname
= from_peer
->hostname
;
281 from_peer
->hostname
= NULL
;
284 if (from_peer
->domainname
!= NULL
) {
285 if (peer
->domainname
) {
286 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
287 peer
->domainname
= NULL
;
290 peer
->domainname
= from_peer
->domainname
;
291 from_peer
->domainname
= NULL
;
294 FOREACH_AFI_SAFI (afi
, safi
) {
295 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
296 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
297 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
298 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
299 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
300 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
301 peer
->llgr
[afi
][safi
] = from_peer
->llgr
[afi
][safi
];
304 if (bgp_getsockname(peer
) < 0) {
307 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
308 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
311 peer
->host
, peer
->fd
, from_peer
->fd
);
312 BGP_EVENT_ADD(peer
, BGP_Stop
);
313 BGP_EVENT_ADD(from_peer
, BGP_Stop
);
316 if (from_peer
->status
> Active
) {
317 if (bgp_getsockname(from_peer
) < 0) {
320 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
322 (CHECK_FLAG(from_peer
->sflags
,
323 PEER_STATUS_ACCEPT_PEER
)
326 from_peer
->host
, from_peer
->fd
, peer
->fd
);
333 // Note: peer_xfer_stats() must be called with I/O turned OFF
335 peer_xfer_stats(peer
, from_peer
);
337 /* Register peer for NHT. This is to allow RAs to be enabled when
338 * needed, even on a passive connection.
340 bgp_peer_reg_with_nht(peer
);
342 bgp_replace_nexthop_by_peer(from_peer
, peer
);
346 thread_add_event(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
)
360 switch (peer
->status
) {
362 /* First entry point of peer's finite state machine. In Idle
363 status start timer is on unless peer is shutdown or peer is
364 inactive. All other timer must be turned off */
365 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)
366 || peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
367 BGP_TIMER_OFF(peer
->t_start
);
369 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
372 BGP_TIMER_OFF(peer
->t_connect
);
373 BGP_TIMER_OFF(peer
->t_holdtime
);
374 bgp_keepalives_off(peer
);
375 BGP_TIMER_OFF(peer
->t_routeadv
);
376 BGP_TIMER_OFF(peer
->t_delayopen
);
380 /* After start timer is expired, the peer moves to Connect
381 status. Make sure start timer is off and connect timer is
383 BGP_TIMER_OFF(peer
->t_start
);
384 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
385 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
386 (peer
->v_delayopen
+ peer
->v_connect
));
388 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
391 BGP_TIMER_OFF(peer
->t_holdtime
);
392 bgp_keepalives_off(peer
);
393 BGP_TIMER_OFF(peer
->t_routeadv
);
397 /* Active is waiting connection from remote peer. And if
398 connect timer is expired, change status to Connect. */
399 BGP_TIMER_OFF(peer
->t_start
);
400 /* If peer is passive mode, do not set connect timer. */
401 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
402 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
403 BGP_TIMER_OFF(peer
->t_connect
);
405 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
407 peer
->t_connect
, bgp_connect_timer
,
408 (peer
->v_delayopen
+ peer
->v_connect
));
410 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
413 BGP_TIMER_OFF(peer
->t_holdtime
);
414 bgp_keepalives_off(peer
);
415 BGP_TIMER_OFF(peer
->t_routeadv
);
419 /* OpenSent status. */
420 BGP_TIMER_OFF(peer
->t_start
);
421 BGP_TIMER_OFF(peer
->t_connect
);
422 if (peer
->v_holdtime
!= 0) {
423 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
426 BGP_TIMER_OFF(peer
->t_holdtime
);
428 bgp_keepalives_off(peer
);
429 BGP_TIMER_OFF(peer
->t_routeadv
);
430 BGP_TIMER_OFF(peer
->t_delayopen
);
434 /* OpenConfirm status. */
435 BGP_TIMER_OFF(peer
->t_start
);
436 BGP_TIMER_OFF(peer
->t_connect
);
438 /* If the negotiated Hold Time value is zero, then the Hold Time
439 timer and KeepAlive timers are not started. */
440 if (peer
->v_holdtime
== 0) {
441 BGP_TIMER_OFF(peer
->t_holdtime
);
442 bgp_keepalives_off(peer
);
444 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
446 bgp_keepalives_on(peer
);
448 BGP_TIMER_OFF(peer
->t_routeadv
);
449 BGP_TIMER_OFF(peer
->t_delayopen
);
453 /* In Established status start and connect timer is turned
455 BGP_TIMER_OFF(peer
->t_start
);
456 BGP_TIMER_OFF(peer
->t_connect
);
457 BGP_TIMER_OFF(peer
->t_delayopen
);
459 /* Same as OpenConfirm, if holdtime is zero then both holdtime
460 and keepalive must be turned off. */
461 if (peer
->v_holdtime
== 0) {
462 BGP_TIMER_OFF(peer
->t_holdtime
);
463 bgp_keepalives_off(peer
);
465 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
467 bgp_keepalives_on(peer
);
471 BGP_TIMER_OFF(peer
->t_gr_restart
);
472 BGP_TIMER_OFF(peer
->t_gr_stale
);
474 FOREACH_AFI_SAFI (afi
, safi
)
475 BGP_TIMER_OFF(peer
->t_llgr_stale
[afi
][safi
]);
477 BGP_TIMER_OFF(peer
->t_pmax_restart
);
478 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
481 BGP_TIMER_OFF(peer
->t_start
);
482 BGP_TIMER_OFF(peer
->t_connect
);
483 BGP_TIMER_OFF(peer
->t_holdtime
);
484 bgp_keepalives_off(peer
);
485 BGP_TIMER_OFF(peer
->t_routeadv
);
486 BGP_TIMER_OFF(peer
->t_delayopen
);
489 flog_err(EC_LIB_DEVELOPMENT
,
490 "BGP_STATUS_MAX while a legal state is not valid state for the FSM");
495 /* BGP start timer. This function set BGP_Start event to thread value
496 and process event. */
497 static int bgp_start_timer(struct thread
*thread
)
501 peer
= THREAD_ARG(thread
);
503 if (bgp_debug_neighbor_events(peer
))
504 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
506 THREAD_VAL(thread
) = BGP_Start
;
507 bgp_event(thread
); /* bgp_event unlocks peer */
512 /* BGP connect retry timer. */
513 static int bgp_connect_timer(struct thread
*thread
)
518 peer
= THREAD_ARG(thread
);
520 /* stop the DelayOpenTimer if it is running */
521 BGP_TIMER_OFF(peer
->t_delayopen
);
523 assert(!peer
->t_write
);
524 assert(!peer
->t_read
);
526 if (bgp_debug_neighbor_events(peer
))
527 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
529 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
533 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
534 bgp_event(thread
); /* bgp_event unlocks peer */
541 /* BGP holdtime timer. */
542 static int bgp_holdtime_timer(struct thread
*thread
)
544 atomic_size_t inq_count
;
547 peer
= THREAD_ARG(thread
);
549 if (bgp_debug_neighbor_events(peer
))
550 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
554 * Given that we do not have any expectation of ordering
555 * for handling packets from a peer -vs- handling
556 * the hold timer for a peer as that they are both
557 * events on the peer. If we have incoming
558 * data on the peers inq, let's give the system a chance
559 * to handle that data. This can be especially true
560 * for systems where we are heavily loaded for one
563 inq_count
= atomic_load_explicit(&peer
->ibuf
->count
,
564 memory_order_relaxed
);
566 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
572 THREAD_VAL(thread
) = Hold_Timer_expired
;
573 bgp_event(thread
); /* bgp_event unlocks peer */
578 int bgp_routeadv_timer(struct thread
*thread
)
582 peer
= THREAD_ARG(thread
);
584 if (bgp_debug_neighbor_events(peer
))
585 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
588 peer
->synctime
= bgp_clock();
590 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
591 &peer
->t_generate_updgrp_packets
);
593 /* MRAI timer will be started again when FIFO is built, no need to
599 /* RFC 4271 DelayOpenTimer */
600 int bgp_delayopen_timer(struct thread
*thread
)
604 peer
= THREAD_ARG(thread
);
606 if (bgp_debug_neighbor_events(peer
))
607 zlog_debug("%s [FSM] Timer (DelayOpentimer expire)",
610 THREAD_VAL(thread
) = DelayOpen_timer_expired
;
611 bgp_event(thread
); /* bgp_event unlocks peer */
616 /* BGP Peer Down Cause */
617 const char *const peer_down_str
[] = {"",
621 "Cluster ID changed",
622 "Confederation identifier changed",
623 "Confederation peer changed",
624 "RR client config change",
625 "RS client config change",
626 "Update source change",
627 "Address family activated",
630 "BGP Notification received",
631 "BGP Notification send",
632 "Peer closed the session",
634 "Peer-group add member",
635 "Peer-group delete member",
636 "Capability changed",
637 "Passive config change",
638 "Multihop config change",
639 "NSF peer closed the session",
640 "Intf peering v6only config change",
643 "Neighbor address lost",
645 "Waiting for Peer IPv6 LLA",
646 "Waiting for VRF to be initialized",
647 "No AFI/SAFI activated for peer",
648 "AS Set config change",
649 "Waiting for peer OPEN",
650 "Reached received prefix count",
653 static void bgp_graceful_restart_timer_off(struct peer
*peer
)
658 FOREACH_AFI_SAFI (afi
, safi
)
659 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
660 PEER_STATUS_LLGR_WAIT
))
663 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
664 BGP_TIMER_OFF(peer
->t_gr_stale
);
666 if (peer_dynamic_neighbor(peer
) &&
667 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
668 if (bgp_debug_neighbor_events(peer
))
669 zlog_debug("%s (dynamic neighbor) deleted (%s)",
670 peer
->host
, __func__
);
677 static int bgp_llgr_stale_timer_expire(struct thread
*thread
)
684 paf
= THREAD_ARG(thread
);
690 /* If the timer for the "Long-lived Stale Time" expires before the
691 * session is re-established, the helper MUST delete all the
692 * stale routes from the neighbor that it is retaining.
694 if (bgp_debug_neighbor_events(peer
))
695 zlog_debug("%s Long-lived stale timer (%s) expired", peer
->host
,
696 get_afi_safi_str(afi
, safi
, false));
698 UNSET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_LLGR_WAIT
);
700 bgp_clear_stale_route(peer
, afi
, safi
);
702 bgp_graceful_restart_timer_off(peer
);
707 static void bgp_set_llgr_stale(struct peer
*peer
, afi_t afi
, safi_t safi
)
709 struct bgp_dest
*dest
;
710 struct bgp_path_info
*pi
;
711 struct bgp_table
*table
;
714 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
|| safi
== SAFI_EVPN
) {
715 for (dest
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); dest
;
716 dest
= bgp_route_next(dest
)) {
719 table
= bgp_dest_get_bgp_table_info(dest
);
723 for (rm
= bgp_table_top(table
); rm
;
724 rm
= bgp_route_next(rm
))
725 for (pi
= bgp_dest_get_bgp_path_info(rm
); pi
;
727 if (pi
->peer
!= peer
)
730 if (pi
->attr
->community
&&
736 if (bgp_debug_neighbor_events(peer
))
738 "%s Long-lived set stale community (LLGR_STALE) for: %pFX",
739 peer
->host
, &dest
->p
);
742 bgp_attr_add_llgr_community(&attr
);
743 pi
->attr
= bgp_attr_intern(&attr
);
744 bgp_recalculate_afi_safi_bestpaths(
745 peer
->bgp
, afi
, safi
);
751 for (dest
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); dest
;
752 dest
= bgp_route_next(dest
))
753 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
755 if (pi
->peer
!= peer
)
758 if (pi
->attr
->community
&&
759 community_include(pi
->attr
->community
,
763 if (bgp_debug_neighbor_events(peer
))
765 "%s Long-lived set stale community (LLGR_STALE) for: %pFX",
766 peer
->host
, &dest
->p
);
769 bgp_attr_add_llgr_community(&attr
);
770 pi
->attr
= bgp_attr_intern(&attr
);
771 bgp_recalculate_afi_safi_bestpaths(peer
->bgp
,
779 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
781 struct peer
*peer
, *tmp_peer
;
782 struct listnode
*node
, *nnode
;
787 peer
= THREAD_ARG(thread
);
789 if (bgp_debug_neighbor_events(peer
)) {
790 zlog_debug("%s graceful restart timer expired", peer
->host
);
791 zlog_debug("%s graceful restart stalepath timer stopped",
795 FOREACH_AFI_SAFI (afi
, safi
) {
796 if (!peer
->nsf
[afi
][safi
])
799 /* Once the "Restart Time" period ends, the LLGR period is
800 * said to have begun and the following procedures MUST be
803 * The helper router MUST start a timer for the
804 * "Long-lived Stale Time".
806 * The helper router MUST attach the LLGR_STALE community
807 * for the stale routes being retained. Note that this
808 * requirement implies that the routes would need to be
809 * readvertised, to disseminate the modified community.
811 if (peer
->llgr
[afi
][safi
].stale_time
) {
812 paf
= peer_af_find(peer
, afi
, safi
);
816 if (bgp_debug_neighbor_events(peer
))
818 "%s Long-lived stale timer (%s) started for %d sec",
820 get_afi_safi_str(afi
, safi
, false),
821 peer
->llgr
[afi
][safi
].stale_time
);
823 SET_FLAG(peer
->af_sflags
[afi
][safi
],
824 PEER_STATUS_LLGR_WAIT
);
826 bgp_set_llgr_stale(peer
, afi
, safi
);
827 bgp_clear_stale_route(peer
, afi
, safi
);
829 thread_add_timer(bm
->master
,
830 bgp_llgr_stale_timer_expire
, paf
,
831 peer
->llgr
[afi
][safi
].stale_time
,
832 &peer
->t_llgr_stale
[afi
][safi
]);
834 for (ALL_LIST_ELEMENTS(peer
->bgp
->peer
, node
, nnode
,
836 bgp_announce_route(tmp_peer
, afi
, safi
, false);
838 bgp_clear_stale_route(peer
, afi
, safi
);
842 bgp_graceful_restart_timer_off(peer
);
847 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
853 peer
= THREAD_ARG(thread
);
855 if (bgp_debug_neighbor_events(peer
))
856 zlog_debug("%s graceful restart stalepath timer expired",
859 /* NSF delete stale route */
860 FOREACH_AFI_SAFI_NSF (afi
, safi
)
861 if (peer
->nsf
[afi
][safi
])
862 bgp_clear_stale_route(peer
, afi
, safi
);
867 /* Selection deferral timer processing function */
868 static int bgp_graceful_deferral_timer_expire(struct thread
*thread
)
870 struct afi_safi_info
*info
;
875 info
= THREAD_ARG(thread
);
880 if (BGP_DEBUG(update
, UPDATE_OUT
))
882 "afi %d, safi %d : graceful restart deferral timer expired",
885 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
886 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
887 XFREE(MTYPE_TMP
, info
);
889 /* Best path selection */
890 return bgp_best_path_select_defer(bgp
, afi
, safi
);
893 static bool bgp_update_delay_applicable(struct bgp
*bgp
)
895 /* update_delay_over flag should be reset (set to 0) for any new
896 applicability of the update-delay during BGP process lifetime.
897 And it should be set after an occurence of the update-delay is
899 if (!bgp
->update_delay_over
)
904 bool bgp_update_delay_active(struct bgp
*bgp
)
906 if (bgp
->t_update_delay
)
911 bool bgp_update_delay_configured(struct bgp
*bgp
)
913 if (bgp
->v_update_delay
)
918 /* Do the post-processing needed when bgp comes out of the read-only mode
919 on ending the update delay. */
920 void bgp_update_delay_end(struct bgp
*bgp
)
922 THREAD_OFF(bgp
->t_update_delay
);
923 THREAD_OFF(bgp
->t_establish_wait
);
925 /* Reset update-delay related state */
926 bgp
->update_delay_over
= 1;
927 bgp
->established
= 0;
928 bgp
->restarted_peers
= 0;
929 bgp
->implicit_eors
= 0;
930 bgp
->explicit_eors
= 0;
932 frr_timestamp(3, bgp
->update_delay_end_time
,
933 sizeof(bgp
->update_delay_end_time
));
936 * Add an end-of-initial-update marker to the main process queues so
938 * the route advertisement timer for the peers can be started. Also set
939 * the zebra and peer update hold flags. These flags are used to achieve
940 * three stages in the update-delay post processing:
941 * 1. Finish best-path selection for all the prefixes held on the
943 * (routes in BGP are updated, and peers sync queues are populated
945 * 2. As the eoiu mark is reached in the bgp process routine, ship all
947 * routes to zebra. With that zebra should see updates from BGP
950 * 3. Unblock the peer update writes. With that peer update packing
952 * the prefixes should be at its maximum.
954 bgp_add_eoiu_mark(bgp
);
955 bgp
->main_zebra_update_hold
= 1;
956 bgp
->main_peers_update_hold
= 1;
959 * Resume the queue processing. This should trigger the event that would
960 * take care of processing any work that was queued during the read-only
963 work_queue_unplug(bgp
->process_queue
);
969 void bgp_start_routeadv(struct bgp
*bgp
)
971 struct listnode
*node
, *nnode
;
974 zlog_info("bgp_start_routeadv(), update hold status %d",
975 bgp
->main_peers_update_hold
);
977 if (bgp
->main_peers_update_hold
)
980 frr_timestamp(3, bgp
->update_delay_peers_resume_time
,
981 sizeof(bgp
->update_delay_peers_resume_time
));
983 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
984 if (!peer_established(peer
))
986 BGP_TIMER_OFF(peer
->t_routeadv
);
987 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
994 void bgp_adjust_routeadv(struct peer
*peer
)
996 time_t nowtime
= bgp_clock();
998 unsigned long remain
;
1000 /* Bypass checks for special case of MRAI being 0 */
1001 if (peer
->v_routeadv
== 0) {
1002 /* Stop existing timer, just in case it is running for a
1004 * duration and schedule write thread immediately.
1006 BGP_TIMER_OFF(peer
->t_routeadv
);
1008 peer
->synctime
= bgp_clock();
1009 /* If suppress fib pending is enabled, route is advertised to
1010 * peers when the status is received from the FIB. The delay
1011 * is added to update group packet generate which will allow
1012 * more routes to be sent in the update message
1014 BGP_UPDATE_GROUP_TIMER_ON(&peer
->t_generate_updgrp_packets
,
1015 bgp_generate_updgrp_packets
);
1022 * If the last update was written more than MRAI back, expire the timer
1023 * instantly so that we can send the update out sooner.
1025 * <------- MRAI --------->
1026 * |-----------------|-----------------------|
1027 * <------------- m ------------>
1036 diff
= difftime(nowtime
, peer
->last_update
);
1037 if (diff
> (double)peer
->v_routeadv
) {
1038 BGP_TIMER_OFF(peer
->t_routeadv
);
1039 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1045 * - Find when to expire the MRAI timer.
1046 * If MRAI timer is not active, assume we can start it now.
1048 * <------- MRAI --------->
1049 * |------------|-----------------------|
1050 * <-------- m ----------><----- r ----->
1059 if (peer
->t_routeadv
)
1060 remain
= thread_timer_remain_second(peer
->t_routeadv
);
1062 remain
= peer
->v_routeadv
;
1063 diff
= peer
->v_routeadv
- diff
;
1064 if (diff
<= (double)remain
) {
1065 BGP_TIMER_OFF(peer
->t_routeadv
);
1066 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
1070 static bool bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
1072 if (!bgp
->maxmed_onstartup_over
)
1077 bool bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
1079 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
1084 bool bgp_maxmed_onstartup_active(struct bgp
*bgp
)
1086 if (bgp
->t_maxmed_onstartup
)
1091 void bgp_maxmed_update(struct bgp
*bgp
)
1093 uint8_t maxmed_active
;
1094 uint32_t maxmed_value
;
1096 if (bgp
->v_maxmed_admin
) {
1098 maxmed_value
= bgp
->maxmed_admin_value
;
1099 } else if (bgp
->t_maxmed_onstartup
) {
1101 maxmed_value
= bgp
->maxmed_onstartup_value
;
1104 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
1107 if (bgp
->maxmed_active
!= maxmed_active
1108 || bgp
->maxmed_value
!= maxmed_value
) {
1109 bgp
->maxmed_active
= maxmed_active
;
1110 bgp
->maxmed_value
= maxmed_value
;
1112 update_group_announce(bgp
);
1116 int bgp_fsm_error_subcode(int status
)
1118 int fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_UNSPECIFIC
;
1122 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_OPENSENT
;
1125 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_OPENCONFIRM
;
1128 fsm_err_subcode
= BGP_NOTIFY_FSM_ERR_SUBCODE_ESTABLISHED
;
1134 return fsm_err_subcode
;
1137 /* The maxmed onstartup timer expiry callback. */
1138 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
1142 zlog_info("Max med on startup ended - timer expired.");
1144 bgp
= THREAD_ARG(thread
);
1145 THREAD_OFF(bgp
->t_maxmed_onstartup
);
1146 bgp
->maxmed_onstartup_over
= 1;
1148 bgp_maxmed_update(bgp
);
1153 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
1155 /* Applicable only once in the process lifetime on the startup */
1156 if (bgp
->maxmed_onstartup_over
)
1159 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
1160 bgp
->v_maxmed_onstartup
);
1162 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
1163 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
1165 if (!bgp
->v_maxmed_admin
) {
1166 bgp
->maxmed_active
= 1;
1167 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
1170 /* Route announce to all peers should happen after this in
1171 * bgp_establish() */
1174 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
1176 if (peer_established(peer
) && !peer
->bgp
->established
) {
1177 bgp_maxmed_onstartup_begin(peer
->bgp
);
1181 /* The update delay timer expiry callback. */
1182 static int bgp_update_delay_timer(struct thread
*thread
)
1186 zlog_info("Update delay ended - timer expired.");
1188 bgp
= THREAD_ARG(thread
);
1189 THREAD_OFF(bgp
->t_update_delay
);
1190 bgp_update_delay_end(bgp
);
1195 /* The establish wait timer expiry callback. */
1196 static int bgp_establish_wait_timer(struct thread
*thread
)
1200 zlog_info("Establish wait - timer expired.");
1202 bgp
= THREAD_ARG(thread
);
1203 THREAD_OFF(bgp
->t_establish_wait
);
1204 bgp_check_update_delay(bgp
);
1209 /* Steps to begin the update delay:
1210 - initialize queues if needed
1211 - stop the queue processing
1212 - start the timer */
1213 static void bgp_update_delay_begin(struct bgp
*bgp
)
1215 struct listnode
*node
, *nnode
;
1218 /* Stop the processing of queued work. Enqueue shall continue */
1219 work_queue_plug(bgp
->process_queue
);
1221 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
1222 peer
->update_delay_over
= 0;
1224 /* Start the update-delay timer */
1225 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
1226 bgp
->v_update_delay
, &bgp
->t_update_delay
);
1228 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
1229 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
1230 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
1232 frr_timestamp(3, bgp
->update_delay_begin_time
,
1233 sizeof(bgp
->update_delay_begin_time
));
1236 static void bgp_update_delay_process_status_change(struct peer
*peer
)
1238 if (peer_established(peer
)) {
1239 if (!peer
->bgp
->established
++) {
1240 bgp_update_delay_begin(peer
->bgp
);
1242 "Begin read-only mode - update-delay timer %d seconds",
1243 peer
->bgp
->v_update_delay
);
1245 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
1246 bgp_update_restarted_peers(peer
);
1248 if (peer
->ostatus
== Established
1249 && bgp_update_delay_active(peer
->bgp
)) {
1250 /* Adjust the update-delay state to account for this flap.
1251 NOTE: Intentionally skipping adjusting implicit_eors or
1253 counters. Extra sanity check in bgp_check_update_delay()
1255 be enough to take care of any additive discrepancy in bgp eor
1257 peer
->bgp
->established
--;
1258 peer
->update_delay_over
= 0;
1262 /* Called after event occurred, this function change status and reset
1263 read/write and timer thread. */
1264 void bgp_fsm_change_status(struct peer
*peer
, int status
)
1267 uint32_t peer_count
;
1270 peer_count
= bgp
->established_peers
;
1272 if (status
== Established
)
1273 bgp
->established_peers
++;
1274 else if ((peer_established(peer
)) && (status
!= Established
))
1275 bgp
->established_peers
--;
1277 if (bgp_debug_neighbor_events(peer
)) {
1278 struct vrf
*vrf
= vrf_lookup_by_id(bgp
->vrf_id
);
1280 zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__
,
1281 vrf
? vrf
->name
: "Unknown", bgp
->vrf_id
,
1282 lookup_msg(bgp_status_msg
, status
, NULL
),
1283 bgp
->established_peers
);
1286 /* Set to router ID to the value provided by RIB if there are no peers
1287 * in the established state and peer count did not change
1289 if ((peer_count
!= bgp
->established_peers
) &&
1290 (bgp
->established_peers
== 0))
1291 bgp_router_id_zebra_bump(bgp
->vrf_id
, NULL
);
1293 /* Transition into Clearing or Deleted must /always/ clear all routes..
1294 * (and must do so before actually changing into Deleted..
1296 if (status
>= Clearing
) {
1297 bgp_clear_route_all(peer
);
1299 /* If no route was queued for the clear-node processing,
1301 * completion event here. This is needed because if there are no
1303 * to trigger the background clear-node thread, the event won't
1305 * generated and the peer would be stuck in Clearing. Note that
1307 * event is for the peer and helps the peer transition out of
1309 * state; it should not be generated per (AFI,SAFI). The event
1311 * directly posted here without calling clear_node_complete() as
1313 * shouldn't do an extra unlock. This event will get processed
1315 * the state change that happens below, so peer will be in
1319 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
1320 BGP_EVENT_ADD(peer
, Clearing_Completed
);
1323 /* Preserve old status and change into new status. */
1324 peer
->ostatus
= peer
->status
;
1325 peer
->status
= status
;
1327 /* Reset received keepalives counter on every FSM change */
1328 peer
->rtt_keepalive_rcv
= 0;
1330 /* Fire backward transition hook if that's the case */
1331 if (peer
->ostatus
> peer
->status
)
1332 hook_call(peer_backward_transition
, peer
);
1334 /* Save event that caused status change. */
1335 peer
->last_major_event
= peer
->cur_event
;
1337 /* Operations after status change */
1338 hook_call(peer_status_changed
, peer
);
1340 if (status
== Established
)
1341 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1343 /* If max-med processing is applicable, do the necessary. */
1344 if (status
== Established
) {
1345 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
1346 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
1347 bgp_maxmed_onstartup_process_status_change(peer
);
1349 peer
->bgp
->maxmed_onstartup_over
= 1;
1352 /* If update-delay processing is applicable, do the necessary. */
1353 if (bgp_update_delay_configured(peer
->bgp
)
1354 && bgp_update_delay_applicable(peer
->bgp
))
1355 bgp_update_delay_process_status_change(peer
);
1357 if (bgp_debug_neighbor_events(peer
))
1358 zlog_debug("%s went from %s to %s", peer
->host
,
1359 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
1360 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1363 /* Flush the event queue and ensure the peer is shut down */
1364 static int bgp_clearing_completed(struct peer
*peer
)
1366 int rc
= bgp_stop(peer
);
1369 BGP_EVENT_FLUSH(peer
);
1374 /* Administrative BGP peer stop event. */
1375 /* May be called multiple times for the same peer */
1376 int bgp_stop(struct peer
*peer
)
1380 char orf_name
[BUFSIZ
];
1382 struct bgp
*bgp
= peer
->bgp
;
1383 struct graceful_restart_info
*gr_info
= NULL
;
1385 peer
->nsf_af_count
= 0;
1387 /* deregister peer */
1388 if (peer
->bfd_config
1389 && peer
->last_reset
== PEER_DOWN_UPDATE_SOURCE_CHANGE
)
1390 bfd_sess_uninstall(peer
->bfd_config
->session
);
1392 if (peer_dynamic_neighbor_no_nsf(peer
) &&
1393 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1394 if (bgp_debug_neighbor_events(peer
))
1395 zlog_debug("%s (dynamic neighbor) deleted (%s)",
1396 peer
->host
, __func__
);
1401 /* Can't do this in Clearing; events are used for state transitions */
1402 if (peer
->status
!= Clearing
) {
1403 /* Delete all existing events of the peer */
1404 BGP_EVENT_FLUSH(peer
);
1407 /* Increment Dropped count. */
1408 if (peer_established(peer
)) {
1411 /* bgp log-neighbor-changes of neighbor Down */
1412 if (CHECK_FLAG(peer
->bgp
->flags
,
1413 BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1414 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1417 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1419 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1420 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1424 peer_down_str
[(int)peer
->last_reset
]);
1427 /* graceful restart */
1428 if (peer
->t_gr_stale
) {
1429 BGP_TIMER_OFF(peer
->t_gr_stale
);
1430 if (bgp_debug_neighbor_events(peer
))
1432 "%s graceful restart stalepath timer stopped",
1435 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1436 if (bgp_debug_neighbor_events(peer
)) {
1438 "%s graceful restart timer started for %d sec",
1439 peer
->host
, peer
->v_gr_restart
);
1441 "%s graceful restart stalepath timer started for %d sec",
1442 peer
->host
, peer
->bgp
->stalepath_time
);
1444 BGP_TIMER_ON(peer
->t_gr_restart
,
1445 bgp_graceful_restart_timer_expire
,
1446 peer
->v_gr_restart
);
1447 BGP_TIMER_ON(peer
->t_gr_stale
,
1448 bgp_graceful_stale_timer_expire
,
1449 peer
->bgp
->stalepath_time
);
1451 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1453 FOREACH_AFI_SAFI_NSF (afi
, safi
)
1454 peer
->nsf
[afi
][safi
] = 0;
1457 /* Stop route-refresh stalepath timer */
1458 if (peer
->t_refresh_stalepath
) {
1459 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
1461 if (bgp_debug_neighbor_events(peer
))
1463 "%s: route-refresh restart stalepath timer stopped",
1467 /* If peer reset before receiving EOR, decrement EOR count and
1468 * cancel the selection deferral timer if there are no
1469 * pending EOR messages to be received
1471 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
1472 FOREACH_AFI_SAFI (afi
, safi
) {
1473 if (!peer
->afc_nego
[afi
][safi
]
1474 || CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1475 PEER_STATUS_EOR_RECEIVED
))
1478 gr_info
= &bgp
->gr_info
[afi
][safi
];
1482 if (gr_info
->eor_required
)
1483 gr_info
->eor_required
--;
1485 if (BGP_DEBUG(update
, UPDATE_OUT
))
1486 zlog_debug("peer %s, EOR_required %d",
1488 gr_info
->eor_required
);
1490 /* There is no pending EOR message */
1491 if (gr_info
->eor_required
== 0) {
1493 gr_info
->t_select_deferral
);
1494 gr_info
->eor_received
= 0;
1499 /* set last reset time */
1500 peer
->resettime
= peer
->uptime
= bgp_clock();
1502 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1503 zlog_debug("%s remove from all update group",
1505 update_group_remove_peer_afs(peer
);
1507 /* Reset peer synctime */
1511 /* stop keepalives */
1512 bgp_keepalives_off(peer
);
1514 /* Stop read and write threads. */
1515 bgp_writes_off(peer
);
1516 bgp_reads_off(peer
);
1518 THREAD_OFF(peer
->t_connect_check_r
);
1519 THREAD_OFF(peer
->t_connect_check_w
);
1521 /* Stop all timers. */
1522 BGP_TIMER_OFF(peer
->t_start
);
1523 BGP_TIMER_OFF(peer
->t_connect
);
1524 BGP_TIMER_OFF(peer
->t_holdtime
);
1525 BGP_TIMER_OFF(peer
->t_routeadv
);
1526 BGP_TIMER_OFF(peer
->t_delayopen
);
1528 /* Clear input and output buffer. */
1529 frr_with_mutex(&peer
->io_mtx
) {
1531 stream_fifo_clean(peer
->ibuf
);
1533 stream_fifo_clean(peer
->obuf
);
1535 if (peer
->ibuf_work
)
1536 ringbuf_wipe(peer
->ibuf_work
);
1537 if (peer
->obuf_work
)
1538 stream_reset(peer
->obuf_work
);
1541 stream_free(peer
->curr
);
1546 /* Close of file descriptor. */
1547 if (peer
->fd
>= 0) {
1552 /* Reset capabilities. */
1555 FOREACH_AFI_SAFI (afi
, safi
) {
1556 /* Reset all negotiated variables */
1557 peer
->afc_nego
[afi
][safi
] = 0;
1558 peer
->afc_adv
[afi
][safi
] = 0;
1559 peer
->afc_recv
[afi
][safi
] = 0;
1561 /* peer address family capability flags*/
1562 peer
->af_cap
[afi
][safi
] = 0;
1564 /* peer address family status flags*/
1565 peer
->af_sflags
[afi
][safi
] = 0;
1567 /* Received ORF prefix-filter */
1568 peer
->orf_plist
[afi
][safi
] = NULL
;
1570 if ((peer
->status
== OpenConfirm
) || (peer_established(peer
))) {
1571 /* ORF received prefix-filter pnt */
1572 snprintf(orf_name
, sizeof(orf_name
), "%s.%d.%d",
1573 peer
->host
, afi
, safi
);
1574 prefix_bgp_orf_remove_all(afi
, orf_name
);
1578 /* Reset keepalive and holdtime */
1579 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1580 peer
->v_keepalive
= peer
->keepalive
;
1581 peer
->v_holdtime
= peer
->holdtime
;
1583 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1584 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1587 /* Reset DelayOpenTime */
1588 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
1589 peer
->v_delayopen
= peer
->delayopen
;
1591 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
1593 peer
->update_time
= 0;
1595 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1596 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1600 bgp_peer_conf_if_to_su_update(peer
);
1605 /* BGP peer is stoped by the error. */
1606 static int bgp_stop_with_error(struct peer
*peer
)
1608 /* Double start timer. */
1611 /* Overflow check. */
1612 if (peer
->v_start
>= (60 * 2))
1613 peer
->v_start
= (60 * 2);
1615 if (peer_dynamic_neighbor_no_nsf(peer
)) {
1616 if (bgp_debug_neighbor_events(peer
))
1617 zlog_debug("%s (dynamic neighbor) deleted (%s)",
1618 peer
->host
, __func__
);
1623 return (bgp_stop(peer
));
1627 /* something went wrong, send notify and tear down */
1628 static int bgp_stop_with_notify(struct peer
*peer
, uint8_t code
,
1631 /* Send notify to remote peer */
1632 bgp_notify_send(peer
, code
, sub_code
);
1634 if (peer_dynamic_neighbor_no_nsf(peer
)) {
1635 if (bgp_debug_neighbor_events(peer
))
1636 zlog_debug("%s (dynamic neighbor) deleted (%s)",
1637 peer
->host
, __func__
);
1642 /* Clear start timer value to default. */
1643 peer
->v_start
= BGP_INIT_START_TIMER
;
1645 return (bgp_stop(peer
));
1649 * Determines whether a TCP session has successfully established for a peer and
1650 * events as appropriate.
1652 * This function is called when setting up a new session. After connect() is
1653 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1654 * to wait for connection success or failure. When poll() returns, this
1655 * function is called to evaluate the result.
1657 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1658 * the value of .revents in the case of a closed connection - this function is
1659 * scheduled both for a read and a write event. The write event is triggered
1660 * when the connection is established. A read event is triggered when the
1661 * connection is closed. Thus we need to cancel whichever one did not occur.
1663 static int bgp_connect_check(struct thread
*thread
)
1670 peer
= THREAD_ARG(thread
);
1671 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1672 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1673 assert(!peer
->t_read
);
1674 assert(!peer
->t_write
);
1676 THREAD_OFF(peer
->t_connect_check_r
);
1677 THREAD_OFF(peer
->t_connect_check_w
);
1679 /* Check file descriptor. */
1680 slen
= sizeof(status
);
1681 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1684 /* If getsockopt is fail, this is fatal error. */
1686 zlog_err("can't get sockopt for nonblocking connect: %d(%s)",
1687 errno
, safe_strerror(errno
));
1688 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1692 /* When status is 0 then TCP connection is established. */
1694 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
1695 BGP_EVENT_ADD(peer
, TCP_connection_open_w_delay
);
1697 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1700 if (bgp_debug_neighbor_events(peer
))
1701 zlog_debug("%s [Event] Connect failed %d(%s)",
1702 peer
->host
, status
, safe_strerror(status
));
1703 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1708 /* TCP connection open. Next we send open message to remote peer. And
1709 add read thread for reading open message. */
1710 static int bgp_connect_success(struct peer
*peer
)
1713 flog_err(EC_BGP_CONNECT
,
1714 "bgp_connect_success peer's fd is negative value %d",
1720 if (bgp_getsockname(peer
) < 0) {
1721 flog_err_sys(EC_LIB_SOCKET
,
1722 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1723 __func__
, peer
->host
, peer
->fd
);
1724 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1725 bgp_fsm_error_subcode(peer
->status
));
1726 bgp_writes_on(peer
);
1732 if (bgp_debug_neighbor_events(peer
)) {
1733 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1734 zlog_debug("%s open active, local address %pSU",
1735 peer
->host
, peer
->su_local
);
1737 zlog_debug("%s passive open", peer
->host
);
1740 /* Send an open message */
1741 bgp_open_send(peer
);
1746 /* TCP connection open with RFC 4271 optional session attribute DelayOpen flag
1749 static int bgp_connect_success_w_delayopen(struct peer
*peer
)
1752 flog_err(EC_BGP_CONNECT
, "%s: peer's fd is negative value %d",
1753 __func__
, peer
->fd
);
1758 if (bgp_getsockname(peer
) < 0) {
1759 flog_err_sys(EC_LIB_SOCKET
,
1760 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1761 __func__
, peer
->host
, peer
->fd
);
1762 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1763 bgp_fsm_error_subcode(peer
->status
));
1764 bgp_writes_on(peer
);
1770 if (bgp_debug_neighbor_events(peer
)) {
1771 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1772 zlog_debug("%s open active, local address %pSU",
1773 peer
->host
, peer
->su_local
);
1775 zlog_debug("%s passive open", peer
->host
);
1778 /* set the DelayOpenTime to the inital value */
1779 peer
->v_delayopen
= peer
->delayopen
;
1781 /* Start the DelayOpenTimer if it is not already running */
1782 if (!peer
->t_delayopen
)
1783 BGP_TIMER_ON(peer
->t_delayopen
, bgp_delayopen_timer
,
1786 if (bgp_debug_neighbor_events(peer
))
1787 zlog_debug("%s [FSM] BGP OPEN message delayed for %d seconds",
1788 peer
->host
, peer
->delayopen
);
1793 /* TCP connect fail */
1794 static int bgp_connect_fail(struct peer
*peer
)
1796 if (peer_dynamic_neighbor_no_nsf(peer
)) {
1797 if (bgp_debug_neighbor_events(peer
))
1798 zlog_debug("%s (dynamic neighbor) deleted (%s)",
1799 peer
->host
, __func__
);
1805 * If we are doing nht for a peer that ls v6 LL based
1806 * massage the event system to make things happy
1808 bgp_nht_interface_events(peer
);
1810 return (bgp_stop(peer
));
1813 /* This function is the first starting point of all BGP connection. It
1814 * try to connect to remote peer with non-blocking IO.
1816 int bgp_start(struct peer
*peer
)
1820 bgp_peer_conf_if_to_su_update(peer
);
1822 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1823 if (bgp_debug_neighbor_events(peer
))
1825 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1827 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1831 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1832 if (bgp_debug_neighbor_events(peer
))
1833 flog_err(EC_BGP_FSM
,
1834 "%s [FSM] Trying to start suppressed peer - this is never supposed to happen!",
1836 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
1837 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
1838 else if (CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
))
1839 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
1840 else if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
1841 peer
->last_reset
= PEER_DOWN_PFX_COUNT
;
1845 /* Scrub some information that might be left over from a previous,
1848 /* Connection information. */
1849 if (peer
->su_local
) {
1850 sockunion_free(peer
->su_local
);
1851 peer
->su_local
= NULL
;
1854 if (peer
->su_remote
) {
1855 sockunion_free(peer
->su_remote
);
1856 peer
->su_remote
= NULL
;
1859 /* Clear remote router-id. */
1860 peer
->remote_id
.s_addr
= INADDR_ANY
;
1862 /* Clear peer capability flag. */
1865 /* If the peer is passive mode, force to move to Active mode. */
1866 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1867 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1871 if (peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
1872 if (bgp_debug_neighbor_events(peer
))
1875 "%s [FSM] In a VRF that is not initialised yet",
1877 peer
->last_reset
= PEER_DOWN_VRF_UNINIT
;
1881 /* Register peer for NHT. If next hop is already resolved, proceed
1882 * with connection setup, else wait.
1884 if (!bgp_peer_reg_with_nht(peer
)) {
1885 if (bgp_zebra_num_connects()) {
1886 if (bgp_debug_neighbor_events(peer
))
1887 zlog_debug("%s [FSM] Waiting for NHT",
1889 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1890 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1895 assert(!peer
->t_write
);
1896 assert(!peer
->t_read
);
1897 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1898 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1899 status
= bgp_connect(peer
);
1903 if (bgp_debug_neighbor_events(peer
))
1904 zlog_debug("%s [FSM] Connect error", peer
->host
);
1905 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1907 case connect_success
:
1908 if (bgp_debug_neighbor_events(peer
))
1910 "%s [FSM] Connect immediately success, fd %d",
1911 peer
->host
, peer
->fd
);
1913 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1915 case connect_in_progress
:
1916 /* To check nonblocking connect, we wait until socket is
1917 readable or writable. */
1918 if (bgp_debug_neighbor_events(peer
))
1920 "%s [FSM] Non blocking connect waiting result, fd %d",
1921 peer
->host
, peer
->fd
);
1923 flog_err(EC_BGP_FSM
,
1924 "bgp_start peer's fd is negative value %d",
1929 * - when the socket becomes ready, poll() will signify POLLOUT
1930 * - if it fails to connect, poll() will signify POLLHUP
1931 * - POLLHUP is handled as a 'read' event by thread.c
1933 * therefore, we schedule both a read and a write event with
1934 * bgp_connect_check() as the handler for each and cancel the
1935 * unused event in that function.
1937 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1938 &peer
->t_connect_check_r
);
1939 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1940 &peer
->t_connect_check_w
);
1946 /* Connect retry timer is expired when the peer status is Connect. */
1947 static int bgp_reconnect(struct peer
*peer
)
1949 if (bgp_stop(peer
) < 0)
1952 /* Send graceful restart capabilty */
1953 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
1960 static int bgp_fsm_open(struct peer
*peer
)
1962 /* If DelayOpen is active, we may still need to send an open message */
1963 if ((peer
->status
== Connect
) || (peer
->status
== Active
))
1964 bgp_open_send(peer
);
1966 /* Send keepalive and make keepalive timer */
1967 bgp_keepalive_send(peer
);
1972 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1973 peer and change to Idle status. */
1974 static int bgp_fsm_event_error(struct peer
*peer
)
1976 flog_err(EC_BGP_FSM
, "%s [FSM] unexpected packet received in state %s",
1977 peer
->host
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1979 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
,
1980 bgp_fsm_error_subcode(peer
->status
));
1983 /* Hold timer expire. This is error of BGP connection. So cut the
1984 peer and change to Idle status. */
1985 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1987 if (bgp_debug_neighbor_events(peer
))
1988 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1990 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1993 /* RFC 4271 DelayOpenTimer_Expires event */
1994 static int bgp_fsm_delayopen_timer_expire(struct peer
*peer
)
1996 /* Stop the DelayOpenTimer */
1997 BGP_TIMER_OFF(peer
->t_delayopen
);
1999 /* Send open message to peer */
2000 bgp_open_send(peer
);
2002 /* Set the HoldTimer to a large value (4 minutes) */
2003 peer
->v_holdtime
= 245;
2008 /* Start the selection deferral timer thread for the specified AFI, SAFI */
2009 static int bgp_start_deferral_timer(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2010 struct graceful_restart_info
*gr_info
)
2012 struct afi_safi_info
*thread_info
;
2014 /* If the deferral timer is active, then increment eor count */
2015 if (gr_info
->t_select_deferral
) {
2016 gr_info
->eor_required
++;
2020 /* Start the deferral timer when the first peer enabled for the graceful
2021 * restart is established
2023 if (gr_info
->eor_required
== 0) {
2024 thread_info
= XMALLOC(MTYPE_TMP
, sizeof(struct afi_safi_info
));
2026 thread_info
->afi
= afi
;
2027 thread_info
->safi
= safi
;
2028 thread_info
->bgp
= bgp
;
2030 thread_add_timer(bm
->master
, bgp_graceful_deferral_timer_expire
,
2031 thread_info
, bgp
->select_defer_time
,
2032 &gr_info
->t_select_deferral
);
2034 gr_info
->eor_required
++;
2035 /* Send message to RIB indicating route update pending */
2036 if (gr_info
->af_enabled
[afi
][safi
] == false) {
2037 gr_info
->af_enabled
[afi
][safi
] = true;
2038 /* Send message to RIB */
2039 bgp_zebra_update(afi
, safi
, bgp
->vrf_id
,
2040 ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
);
2042 if (BGP_DEBUG(update
, UPDATE_OUT
))
2043 zlog_debug("Started the deferral timer for %s eor_required %d",
2044 get_afi_safi_str(afi
, safi
, false),
2045 gr_info
->eor_required
);
2049 /* Update the graceful restart information for the specified AFI, SAFI */
2050 static int bgp_update_gr_info(struct peer
*peer
, afi_t afi
, safi_t safi
)
2052 struct graceful_restart_info
*gr_info
;
2053 struct bgp
*bgp
= peer
->bgp
;
2056 if ((afi
< AFI_IP
) || (afi
>= AFI_MAX
)) {
2057 if (BGP_DEBUG(update
, UPDATE_OUT
))
2058 zlog_debug("%s : invalid afi %d", __func__
, afi
);
2062 if ((safi
< SAFI_UNICAST
) || (safi
> SAFI_MPLS_VPN
)) {
2063 if (BGP_DEBUG(update
, UPDATE_OUT
))
2064 zlog_debug("%s : invalid safi %d", __func__
, safi
);
2068 /* Restarting router */
2069 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
2070 && BGP_PEER_RESTARTING_MODE(peer
)) {
2071 /* Check if the forwarding state is preserved */
2072 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_GR_PRESERVE_FWD
)) {
2073 gr_info
= &(bgp
->gr_info
[afi
][safi
]);
2074 ret
= bgp_start_deferral_timer(bgp
, afi
, safi
, gr_info
);
2081 * Transition to Established state.
2083 * Convert peer from stub to full fledged peer, set some timers, and generate
2086 static int bgp_establish(struct peer
*peer
)
2090 int nsf_af_count
= 0;
2095 other
= peer
->doppelganger
;
2096 peer
= peer_xfer_conn(peer
);
2098 flog_err(EC_BGP_CONNECT
, "%%Neighbor failed in xfer_conn");
2103 ret
= 1; /* bgp_establish specific code when xfer_conn
2106 /* Reset capability open status flag. */
2107 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
2108 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
2110 /* Clear start timer value to default. */
2111 peer
->v_start
= BGP_INIT_START_TIMER
;
2113 /* Increment established count. */
2114 peer
->established
++;
2115 bgp_fsm_change_status(peer
, Established
);
2117 /* bgp log-neighbor-changes of neighbor Up */
2118 if (CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
2119 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
2121 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Up", peer
->host
,
2122 (peer
->hostname
) ? peer
->hostname
: "Unknown",
2123 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
2127 /* assign update-group/subgroup */
2128 update_group_adjust_peer_afs(peer
);
2130 /* graceful restart */
2131 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2132 if (bgp_debug_neighbor_events(peer
)) {
2133 if (BGP_PEER_RESTARTING_MODE(peer
))
2134 zlog_debug("peer %s BGP_RESTARTING_MODE", peer
->host
);
2135 else if (BGP_PEER_HELPER_MODE(peer
))
2136 zlog_debug("peer %s BGP_HELPER_MODE", peer
->host
);
2139 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2140 if (peer
->afc_nego
[afi
][safi
] &&
2141 CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
) &&
2142 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2143 PEER_CAP_RESTART_AF_RCV
)) {
2144 if (peer
->nsf
[afi
][safi
] &&
2145 !CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2146 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
2147 bgp_clear_stale_route(peer
, afi
, safi
);
2149 peer
->nsf
[afi
][safi
] = 1;
2152 if (peer
->nsf
[afi
][safi
])
2153 bgp_clear_stale_route(peer
, afi
, safi
);
2154 peer
->nsf
[afi
][safi
] = 0;
2156 /* Update the graceful restart information */
2157 if (peer
->afc_nego
[afi
][safi
]) {
2158 if (!BGP_SELECT_DEFER_DISABLE(peer
->bgp
)) {
2159 status
= bgp_update_gr_info(peer
, afi
, safi
);
2162 "Error in updating graceful restart for %s",
2163 get_afi_safi_str(afi
, safi
,
2166 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
) &&
2167 BGP_PEER_RESTARTING_MODE(peer
) &&
2168 CHECK_FLAG(peer
->bgp
->flags
,
2169 BGP_FLAG_GR_PRESERVE_FWD
))
2170 peer
->bgp
->gr_info
[afi
][safi
]
2176 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
2177 if ((bgp_peer_gr_mode_get(peer
) == PEER_GR
)
2178 || ((bgp_peer_gr_mode_get(peer
) == PEER_GLOBAL_INHERIT
)
2179 && (bgp_global_gr_mode_get(peer
->bgp
) == GLOBAL_GR
))) {
2180 FOREACH_AFI_SAFI (afi
, safi
)
2181 /* Send route processing complete
2184 afi
, safi
, peer
->bgp
->vrf_id
,
2185 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
);
2188 /* Peer sends R-bit. In this case, we need to send
2189 * ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE to Zebra. */
2190 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
)) {
2191 FOREACH_AFI_SAFI (afi
, safi
)
2192 /* Send route processing complete
2195 afi
, safi
, peer
->bgp
->vrf_id
,
2196 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
);
2200 peer
->nsf_af_count
= nsf_af_count
;
2203 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2205 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2206 if (peer
->t_gr_stale
) {
2207 BGP_TIMER_OFF(peer
->t_gr_stale
);
2208 if (bgp_debug_neighbor_events(peer
))
2210 "%s graceful restart stalepath timer stopped",
2215 if (peer
->t_gr_restart
) {
2216 BGP_TIMER_OFF(peer
->t_gr_restart
);
2217 if (bgp_debug_neighbor_events(peer
))
2218 zlog_debug("%s graceful restart timer stopped",
2222 /* Reset uptime, turn on keepalives, send current table. */
2223 if (!peer
->v_holdtime
)
2224 bgp_keepalives_on(peer
);
2226 peer
->uptime
= bgp_clock();
2228 /* Send route-refresh when ORF is enabled */
2229 FOREACH_AFI_SAFI (afi
, safi
) {
2230 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2231 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
2232 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2233 PEER_CAP_ORF_PREFIX_RM_RCV
))
2234 bgp_route_refresh_send(
2235 peer
, afi
, safi
, ORF_TYPE_PREFIX
,
2236 REFRESH_IMMEDIATE
, 0,
2237 BGP_ROUTE_REFRESH_NORMAL
);
2238 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2239 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
2240 bgp_route_refresh_send(
2241 peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
2242 REFRESH_IMMEDIATE
, 0,
2243 BGP_ROUTE_REFRESH_NORMAL
);
2247 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2248 FOREACH_AFI_SAFI (afi
, safi
) {
2249 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2250 PEER_CAP_ORF_PREFIX_RM_ADV
))
2251 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2252 PEER_CAP_ORF_PREFIX_SM_RCV
)
2253 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2254 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
2255 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2256 PEER_STATUS_ORF_WAIT_REFRESH
);
2259 bgp_announce_peer(peer
);
2261 /* Start the route advertisement timer to send updates to the peer - if
2263 * is not in read-only mode. If it is, the timer will be started at the
2265 * of read-only mode.
2267 if (!bgp_update_delay_active(peer
->bgp
)) {
2268 BGP_TIMER_OFF(peer
->t_routeadv
);
2269 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
2272 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
2273 if (bgp_debug_neighbor_events(peer
))
2275 "[Event] Deleting stub connection for peer %s",
2278 if (peer
->doppelganger
->status
> Active
)
2279 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
2280 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
2282 peer_delete(peer
->doppelganger
);
2286 * If we are replacing the old peer for a doppelganger
2287 * then switch it around in the bgp->peerhash
2288 * the doppelgangers su and this peer's su are the same
2289 * so the hash_release is the same for either.
2291 hash_release(peer
->bgp
->peerhash
, peer
);
2292 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
2294 /* Start BFD peer if not already running. */
2295 if (peer
->bfd_config
)
2296 bgp_peer_bfd_update_source(peer
);
2301 /* Keepalive packet is received. */
2302 static int bgp_fsm_keepalive(struct peer
*peer
)
2304 BGP_TIMER_OFF(peer
->t_holdtime
);
2308 /* Update packet is received. */
2309 static int bgp_fsm_update(struct peer
*peer
)
2311 BGP_TIMER_OFF(peer
->t_holdtime
);
2315 /* This is empty event. */
2316 static int bgp_ignore(struct peer
*peer
)
2320 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
2321 peer
->host
, bgp_event_str
[peer
->cur_event
],
2322 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2323 bgp_event_str
[peer
->last_event
],
2324 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
2328 /* This is to handle unexpected events.. */
2329 static int bgp_fsm_exeption(struct peer
*peer
)
2333 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
2334 peer
->host
, bgp_event_str
[peer
->cur_event
],
2335 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2336 bgp_event_str
[peer
->last_event
],
2337 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
2338 return (bgp_stop(peer
));
2341 void bgp_fsm_nht_update(struct peer
*peer
, bool has_valid_nexthops
)
2346 switch (peer
->status
) {
2348 if (has_valid_nexthops
)
2349 BGP_EVENT_ADD(peer
, BGP_Start
);
2352 if (!has_valid_nexthops
) {
2353 BGP_TIMER_OFF(peer
->t_connect
);
2354 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
2358 if (has_valid_nexthops
) {
2359 BGP_TIMER_OFF(peer
->t_connect
);
2360 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
2366 if (!has_valid_nexthops
2367 && (peer
->gtsm_hops
== BGP_GTSM_HOPS_CONNECTED
2368 || peer
->bgp
->fast_convergence
))
2369 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
2377 /* Finite State Machine structure */
2378 static const struct {
2379 int (*func
)(struct peer
*);
2380 enum bgp_fsm_status next_state
;
2381 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
2383 /* Idle state: In Idle state, all events other than BGP_Start is
2384 ignored. With BGP_Start event, finite state machine calls
2386 {bgp_start
, Connect
}, /* BGP_Start */
2387 {bgp_stop
, Idle
}, /* BGP_Stop */
2388 {bgp_stop
, Idle
}, /* TCP_connection_open */
2389 {bgp_stop
, Idle
}, /* TCP_connection_open_w_delay */
2390 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2391 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
2392 {bgp_stop
, Idle
}, /* TCP_fatal_error */
2393 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
2394 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
2395 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
2396 {bgp_ignore
, Idle
}, /* DelayOpen_timer_expired */
2397 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
2398 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
2399 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
2400 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
2401 {bgp_ignore
, Idle
}, /* Clearing_Completed */
2405 {bgp_ignore
, Connect
}, /* BGP_Start */
2406 {bgp_stop
, Idle
}, /* BGP_Stop */
2407 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
2408 {bgp_connect_success_w_delayopen
,
2409 Connect
}, /* TCP_connection_open_w_delay */
2410 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2411 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
2412 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
2413 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
2414 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
2415 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2416 {bgp_fsm_delayopen_timer_expire
,
2417 OpenSent
}, /* DelayOpen_timer_expired */
2418 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2419 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
2420 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2421 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
2422 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2426 {bgp_ignore
, Active
}, /* BGP_Start */
2427 {bgp_stop
, Idle
}, /* BGP_Stop */
2428 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
2429 {bgp_connect_success_w_delayopen
,
2430 Active
}, /* TCP_connection_open_w_delay */
2431 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2432 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
2433 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
2434 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
2435 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
2436 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2437 {bgp_fsm_delayopen_timer_expire
,
2438 OpenSent
}, /* DelayOpen_timer_expired */
2439 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2440 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
2441 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2442 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
2443 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2447 {bgp_ignore
, OpenSent
}, /* BGP_Start */
2448 {bgp_stop
, Idle
}, /* BGP_Stop */
2449 {bgp_stop
, Active
}, /* TCP_connection_open */
2450 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2451 {bgp_stop
, Active
}, /* TCP_connection_closed */
2452 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
2453 {bgp_stop
, Active
}, /* TCP_fatal_error */
2454 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
2455 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
2456 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
2457 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2458 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
2459 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
2460 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
2461 {bgp_fsm_event_error
, Idle
}, /* Receive_NOTIFICATION_message */
2462 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2466 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
2467 {bgp_stop
, Idle
}, /* BGP_Stop */
2468 {bgp_stop
, Idle
}, /* TCP_connection_open */
2469 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2470 {bgp_stop
, Idle
}, /* TCP_connection_closed */
2471 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
2472 {bgp_stop
, Idle
}, /* TCP_fatal_error */
2473 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
2474 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
2475 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
2476 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2477 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
2478 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
2479 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
2480 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
2481 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2485 {bgp_ignore
, Established
}, /* BGP_Start */
2486 {bgp_stop
, Clearing
}, /* BGP_Stop */
2487 {bgp_stop
, Clearing
}, /* TCP_connection_open */
2488 {bgp_fsm_exeption
, Idle
}, /* TCP_connection_open_w_delay */
2489 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
2490 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
2491 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
2492 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
2493 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
2494 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
2495 {bgp_fsm_exeption
, Idle
}, /* DelayOpen_timer_expired */
2496 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
2498 Established
}, /* Receive_KEEPALIVE_message */
2499 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
2500 {bgp_stop_with_error
,
2501 Clearing
}, /* Receive_NOTIFICATION_message */
2502 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
2506 {bgp_ignore
, Clearing
}, /* BGP_Start */
2507 {bgp_stop
, Clearing
}, /* BGP_Stop */
2508 {bgp_stop
, Clearing
}, /* TCP_connection_open */
2509 {bgp_stop
, Clearing
}, /* TCP_connection_open_w_delay */
2510 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
2511 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
2512 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
2513 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
2514 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
2515 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
2516 {bgp_stop
, Clearing
}, /* DelayOpen_timer_expired */
2517 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
2518 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
2519 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
2520 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
2521 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
2525 {bgp_ignore
, Deleted
}, /* BGP_Start */
2526 {bgp_ignore
, Deleted
}, /* BGP_Stop */
2527 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
2528 {bgp_ignore
, Deleted
}, /* TCP_connection_open_w_delay */
2529 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
2530 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
2531 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
2532 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
2533 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
2534 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
2535 {bgp_ignore
, Deleted
}, /* DelayOpen_timer_expired */
2536 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
2537 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
2538 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
2539 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
2540 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
2544 /* Execute event process. */
2545 int bgp_event(struct thread
*thread
)
2547 enum bgp_fsm_events event
;
2551 peer
= THREAD_ARG(thread
);
2552 event
= THREAD_VAL(thread
);
2554 ret
= bgp_event_update(peer
, event
);
2559 int bgp_event_update(struct peer
*peer
, enum bgp_fsm_events event
)
2561 enum bgp_fsm_status next
;
2564 int passive_conn
= 0;
2567 /* default return code */
2568 ret
= FSM_PEER_NOOP
;
2570 other
= peer
->doppelganger
;
2572 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
2573 dyn_nbr
= peer_dynamic_neighbor(peer
);
2575 /* Logging this event. */
2576 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
2578 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
2579 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
2580 bgp_event_str
[event
],
2581 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2582 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
2584 peer
->last_event
= peer
->cur_event
;
2585 peer
->cur_event
= event
;
2587 /* Call function. */
2588 if (FSM
[peer
->status
- 1][event
- 1].func
)
2589 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
2592 if (ret
== 1 && next
== Established
) {
2593 /* The case when doppelganger swap accurred in
2595 Update the peer pointer accordingly */
2596 ret
= FSM_PEER_TRANSFERRED
;
2600 /* If status is changed. */
2601 if (next
!= peer
->status
) {
2602 bgp_fsm_change_status(peer
, next
);
2605 * If we're going to ESTABLISHED then we executed a
2606 * peer transfer. In this case we can either return
2607 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
2608 * Opting for TRANSFERRED since transfer implies
2609 * session establishment.
2611 if (ret
!= FSM_PEER_TRANSFERRED
)
2612 ret
= FSM_PEER_TRANSITIONED
;
2615 /* Make sure timer is set. */
2616 bgp_timer_set(peer
);
2620 * If we got a return value of -1, that means there was an
2621 * error, restart the FSM. Since bgp_stop() was called on the
2622 * peer. only a few fields are safe to access here. In any case
2623 * we need to indicate that the peer was stopped in the return
2626 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
2629 "%s [FSM] Failure handling event %s in state %s, prior events %s, %s, fd %d",
2630 peer
->host
, bgp_event_str
[peer
->cur_event
],
2631 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2632 bgp_event_str
[peer
->last_event
],
2633 bgp_event_str
[peer
->last_major_event
],
2636 bgp_fsm_change_status(peer
, Idle
);
2637 bgp_timer_set(peer
);
2639 ret
= FSM_PEER_STOPPED
;
2646 int bgp_gr_lookup_n_update_all_peer(struct bgp
*bgp
,
2647 enum global_mode global_new_state
,
2648 enum global_mode global_old_state
)
2650 struct peer
*peer
= {0};
2651 struct listnode
*node
= {0};
2652 struct listnode
*nnode
= {0};
2653 enum peer_mode peer_old_state
= PEER_INVALID
;
2655 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
2657 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2658 zlog_debug("%s [BGP_GR] Peer: (%s) :", __func__
,
2661 peer_old_state
= bgp_peer_gr_mode_get(peer
);
2663 if (peer_old_state
== PEER_GLOBAL_INHERIT
) {
2666 *Reset only these peers and send a
2667 *new open message with the change capabilities.
2668 *Considering the mode to be "global_new_state" and
2669 *do all operation accordingly
2672 switch (global_new_state
) {
2674 BGP_PEER_GR_HELPER_ENABLE(peer
);
2677 BGP_PEER_GR_ENABLE(peer
);
2679 case GLOBAL_DISABLE
:
2680 BGP_PEER_GR_DISABLE(peer
);
2682 case GLOBAL_INVALID
:
2683 zlog_debug("%s [BGP_GR] GLOBAL_INVALID",
2685 return BGP_ERR_GR_OPERATION_FAILED
;
2690 bgp
->global_gr_present_state
= global_new_state
;
2692 return BGP_GR_SUCCESS
;
2695 int bgp_gr_update_all(struct bgp
*bgp
, int global_gr_cmd
)
2697 enum global_mode global_new_state
= GLOBAL_INVALID
;
2698 enum global_mode global_old_state
= GLOBAL_INVALID
;
2700 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2701 zlog_debug("%s [BGP_GR]START: global_gr_cmd :%s:", __func__
,
2702 print_global_gr_cmd(global_gr_cmd
));
2704 global_old_state
= bgp_global_gr_mode_get(bgp
);
2706 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2707 zlog_debug("[BGP_GR] global_old_gr_state :%s:",
2708 print_global_gr_mode(global_old_state
));
2710 if (global_old_state
!= GLOBAL_INVALID
) {
2712 bgp
->GLOBAL_GR_FSM
[global_old_state
][global_gr_cmd
];
2714 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2715 zlog_debug("[BGP_GR] global_new_gr_state :%s:",
2716 print_global_gr_mode(global_new_state
));
2718 zlog_err("%s [BGP_GR] global_old_state == GLOBAL_INVALID",
2720 return BGP_ERR_GR_OPERATION_FAILED
;
2723 if (global_new_state
== GLOBAL_INVALID
) {
2724 zlog_err("%s [BGP_GR] global_new_state == GLOBAL_INVALID",
2726 return BGP_ERR_GR_INVALID_CMD
;
2728 if (global_new_state
== global_old_state
) {
2730 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2732 "%s [BGP_GR] global_new_state == global_old_state :%s",
2734 print_global_gr_mode(global_new_state
));
2735 return BGP_GR_NO_OPERATION
;
2738 return bgp_gr_lookup_n_update_all_peer(bgp
, global_new_state
,
2742 const char *print_peer_gr_mode(enum peer_mode pr_mode
)
2744 const char *peer_gr_mode
= NULL
;
2748 peer_gr_mode
= "PEER_HELPER";
2751 peer_gr_mode
= "PEER_GR";
2754 peer_gr_mode
= "PEER_DISABLE";
2757 peer_gr_mode
= "PEER_INVALID";
2759 case PEER_GLOBAL_INHERIT
:
2760 peer_gr_mode
= "PEER_GLOBAL_INHERIT";
2764 return peer_gr_mode
;
2767 const char *print_peer_gr_cmd(enum peer_gr_command pr_gr_cmd
)
2769 const char *peer_gr_cmd
= NULL
;
2771 switch (pr_gr_cmd
) {
2773 peer_gr_cmd
= "PEER_GR_CMD";
2775 case NO_PEER_GR_CMD
:
2776 peer_gr_cmd
= "NO_PEER_GR_CMD";
2778 case PEER_DISABLE_CMD
:
2779 peer_gr_cmd
= "PEER_GR_CMD";
2781 case NO_PEER_DISABLE_CMD
:
2782 peer_gr_cmd
= "NO_PEER_GR_CMD";
2784 case PEER_HELPER_CMD
:
2785 peer_gr_cmd
= "PEER_HELPER_CMD";
2787 case NO_PEER_HELPER_CMD
:
2788 peer_gr_cmd
= "NO_PEER_HELPER_CMD";
2795 const char *print_global_gr_mode(enum global_mode gl_mode
)
2797 const char *global_gr_mode
= NULL
;
2801 global_gr_mode
= "GLOBAL_HELPER";
2804 global_gr_mode
= "GLOBAL_GR";
2806 case GLOBAL_DISABLE
:
2807 global_gr_mode
= "GLOBAL_DISABLE";
2809 case GLOBAL_INVALID
:
2810 global_gr_mode
= "GLOBAL_INVALID";
2814 return global_gr_mode
;
2817 const char *print_global_gr_cmd(enum global_gr_command gl_gr_cmd
)
2819 const char *global_gr_cmd
= NULL
;
2821 switch (gl_gr_cmd
) {
2823 global_gr_cmd
= "GLOBAL_GR_CMD";
2825 case NO_GLOBAL_GR_CMD
:
2826 global_gr_cmd
= "NO_GLOBAL_GR_CMD";
2828 case GLOBAL_DISABLE_CMD
:
2829 global_gr_cmd
= "GLOBAL_DISABLE_CMD";
2831 case NO_GLOBAL_DISABLE_CMD
:
2832 global_gr_cmd
= "NO_GLOBAL_DISABLE_CMD";
2836 return global_gr_cmd
;
2839 enum global_mode
bgp_global_gr_mode_get(struct bgp
*bgp
)
2841 return bgp
->global_gr_present_state
;
2844 enum peer_mode
bgp_peer_gr_mode_get(struct peer
*peer
)
2846 return peer
->peer_gr_present_state
;
2849 int bgp_neighbor_graceful_restart(struct peer
*peer
, int peer_gr_cmd
)
2851 enum peer_mode peer_new_state
= PEER_INVALID
;
2852 enum peer_mode peer_old_state
= PEER_INVALID
;
2853 struct bgp_peer_gr peer_state
;
2854 int result
= BGP_GR_FAILURE
;
2857 * fetch peer_old_state from peer structure also
2858 * fetch global_old_state from bgp structure,
2859 * peer had a back pointer to bgpo struct ;
2862 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2863 zlog_debug("%s [BGP_GR] START:Peer: (%s) : peer_gr_cmd :%s:",
2864 __func__
, peer
->host
,
2865 print_peer_gr_cmd(peer_gr_cmd
));
2867 peer_old_state
= bgp_peer_gr_mode_get(peer
);
2869 if (peer_old_state
== PEER_INVALID
) {
2870 zlog_debug("[BGP_GR] peer_old_state == Invalid state !");
2871 return BGP_ERR_GR_OPERATION_FAILED
;
2874 peer_state
= peer
->PEER_GR_FSM
[peer_old_state
][peer_gr_cmd
];
2875 peer_new_state
= peer_state
.next_state
;
2877 if (peer_new_state
== PEER_INVALID
) {
2879 "[BGP_GR] Invalid bgp graceful restart command used !");
2880 return BGP_ERR_GR_INVALID_CMD
;
2883 if (peer_new_state
!= peer_old_state
) {
2884 result
= peer_state
.action_fun(peer
, peer_old_state
,
2887 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2889 "[BGP_GR] peer_old_state == peer_new_state !");
2890 return BGP_GR_NO_OPERATION
;
2893 if (result
== BGP_GR_SUCCESS
) {
2895 /* Update the mode i.e peer_new_state into the peer structure */
2896 peer
->peer_gr_present_state
= peer_new_state
;
2897 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2899 "[BGP_GR] Successfully change the state of the peer to : %s : !",
2900 print_peer_gr_mode(peer_new_state
));
2902 return BGP_GR_SUCCESS
;
2908 unsigned int bgp_peer_gr_action(struct peer
*peer
, int old_peer_state
,
2911 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2913 "%s [BGP_GR] Move peer from old_peer_state :%s: to new_peer_state :%s: !!!!",
2914 __func__
, print_peer_gr_mode(old_peer_state
),
2915 print_peer_gr_mode(new_peer_state
));
2917 int bgp_gr_global_mode
= GLOBAL_INVALID
;
2918 unsigned int ret
= BGP_GR_FAILURE
;
2920 if (old_peer_state
== new_peer_state
) {
2921 /* Nothing to do over here as the present and old state is the
2923 return BGP_GR_NO_OPERATION
;
2925 if ((old_peer_state
== PEER_INVALID
)
2926 || (new_peer_state
== PEER_INVALID
)) {
2927 /* something bad happend , print error message */
2928 return BGP_ERR_GR_INVALID_CMD
;
2931 bgp_gr_global_mode
= bgp_global_gr_mode_get(peer
->bgp
);
2933 if ((old_peer_state
== PEER_GLOBAL_INHERIT
)
2934 && (new_peer_state
!= PEER_GLOBAL_INHERIT
)) {
2936 /* fetch the Mode running in the Global state machine
2937 *from the bgp structure into a variable called
2941 /* Here we are checking if the
2942 *1. peer_new_state == global_mode == helper_mode
2943 *2. peer_new_state == global_mode == GR_mode
2944 *3. peer_new_state == global_mode == disabled_mode
2947 BGP_PEER_GR_GLOBAL_INHERIT_UNSET(peer
);
2949 if (new_peer_state
== bgp_gr_global_mode
) {
2950 /*This is incremental updates i.e no tear down
2951 *of the existing session
2952 *as the peer is already working in the same mode.
2954 ret
= BGP_GR_SUCCESS
;
2956 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2958 "[BGP_GR] Peer state changed from :%s ",
2959 print_peer_gr_mode(old_peer_state
));
2961 bgp_peer_move_to_gr_mode(peer
, new_peer_state
);
2963 ret
= BGP_GR_SUCCESS
;
2966 /* In the case below peer is going into Global inherit mode i.e.
2967 * the peer would work as the mode configured at the global level
2969 else if ((new_peer_state
== PEER_GLOBAL_INHERIT
)
2970 && (old_peer_state
!= PEER_GLOBAL_INHERIT
)) {
2971 /* Here in this case it would be destructive
2972 * in all the cases except one case when,
2973 * Global GR is configured Disabled
2974 * and present_peer_state is not disable
2977 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer
);
2979 if (old_peer_state
== bgp_gr_global_mode
) {
2981 /* This is incremental updates
2982 *i.e no tear down of the existing session
2983 *as the peer is already working in the same mode.
2985 ret
= BGP_GR_SUCCESS
;
2987 /* Destructive always */
2988 /* Tear down the old session
2989 * and send the new capability
2990 * as per the bgp_gr_global_mode
2993 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
2995 "[BGP_GR] Peer state changed from :%s",
2996 print_peer_gr_mode(old_peer_state
));
2998 bgp_peer_move_to_gr_mode(peer
, bgp_gr_global_mode
);
3000 ret
= BGP_GR_SUCCESS
;
3004 *This else case, it include all the cases except -->
3005 *(new_peer_state != Peer_Global) &&
3006 *( old_peer_state != Peer_Global )
3008 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3009 zlog_debug("[BGP_GR] Peer state changed from :%s",
3010 print_peer_gr_mode(old_peer_state
));
3012 bgp_peer_move_to_gr_mode(peer
, new_peer_state
);
3014 ret
= BGP_GR_SUCCESS
;
3020 inline void bgp_peer_move_to_gr_mode(struct peer
*peer
, int new_state
)
3023 int bgp_global_gr_mode
= bgp_global_gr_mode_get(peer
->bgp
);
3025 switch (new_state
) {
3027 BGP_PEER_GR_HELPER_ENABLE(peer
);
3030 BGP_PEER_GR_ENABLE(peer
);
3033 BGP_PEER_GR_DISABLE(peer
);
3035 case PEER_GLOBAL_INHERIT
:
3036 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer
);
3038 if (bgp_global_gr_mode
== GLOBAL_HELPER
) {
3039 BGP_PEER_GR_HELPER_ENABLE(peer
);
3040 } else if (bgp_global_gr_mode
== GLOBAL_GR
) {
3041 BGP_PEER_GR_ENABLE(peer
);
3042 } else if (bgp_global_gr_mode
== GLOBAL_DISABLE
) {
3043 BGP_PEER_GR_DISABLE(peer
);
3046 "[BGP_GR] Default switch inherit mode ::: SOMETHING IS WRONG !!!");
3051 "[BGP_GR] Default switch mode ::: SOMETHING IS WRONG !!!");
3054 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3055 zlog_debug("[BGP_GR] Peer state changed --to--> : %d : !",
3059 void bgp_peer_gr_flags_update(struct peer
*peer
)
3061 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3062 zlog_debug("%s [BGP_GR] called !", __func__
);
3063 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
3064 PEER_GRACEFUL_RESTART_NEW_STATE_HELPER
))
3065 SET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
);
3067 UNSET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
);
3068 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3070 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_HELPER : %s : !",
3072 (CHECK_FLAG(peer
->flags
,
3073 PEER_FLAG_GRACEFUL_RESTART_HELPER
)
3076 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
3077 PEER_GRACEFUL_RESTART_NEW_STATE_RESTART
))
3078 SET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
);
3080 UNSET_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
);
3081 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3083 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART : %s : !",
3085 (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
3088 if (CHECK_FLAG(peer
->peer_gr_new_status_flag
,
3089 PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT
))
3090 SET_FLAG(peer
->flags
,
3091 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
);
3093 UNSET_FLAG(peer
->flags
,
3094 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
);
3095 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
3097 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT : %s : !",
3099 (CHECK_FLAG(peer
->flags
,
3100 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT
)
3104 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
3105 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART_HELPER
)) {
3106 zlog_debug("[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_MODE!",
3109 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
3111 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
3113 peer_nsf_stop(peer
);
3115 "[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_WAIT!",