1 /* BGP-4 Finite State Machine
2 * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "sockunion.h"
32 #include "workqueue.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_packet.h"
43 #include "bgpd/bgp_network.h"
44 #include "bgpd/bgp_route.h"
45 #include "bgpd/bgp_dump.h"
46 #include "bgpd/bgp_open.h"
47 #include "bgpd/bgp_advertise.h"
48 #include "bgpd/bgp_updgrp.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_memory.h"
52 #include "bgpd/bgp_keepalives.h"
53 #include "bgpd/bgp_io.h"
55 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
56 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
58 /* Definition of display strings corresponding to FSM events. This should be
59 * kept consistent with the events defined in bgpd.h
61 static const char *bgp_event_str
[] = {
65 "TCP_connection_open",
66 "TCP_connection_closed",
67 "TCP_connection_open_failed",
69 "ConnectRetry_timer_expired",
71 "KeepAlive_timer_expired",
72 "Receive_OPEN_message",
73 "Receive_KEEPALIVE_message",
74 "Receive_UPDATE_message",
75 "Receive_NOTIFICATION_message",
79 /* BGP FSM (finite state machine) has three types of functions. Type
80 one is thread functions. Type two is event functions. Type three
81 is FSM functions. Timer functions are set by bgp_timer_set
84 /* BGP event function. */
85 int bgp_event(struct thread
*);
87 /* BGP thread functions. */
88 static int bgp_start_timer(struct thread
*);
89 static int bgp_connect_timer(struct thread
*);
90 static int bgp_holdtime_timer(struct thread
*);
92 /* BGP FSM functions. */
93 static int bgp_start(struct peer
*);
95 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
97 /* Copy stats over. These are only the pre-established state stats */
98 peer_dst
->open_in
+= peer_src
->open_in
;
99 peer_dst
->open_out
+= peer_src
->open_out
;
100 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
101 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
102 peer_dst
->notify_in
+= peer_src
->notify_in
;
103 peer_dst
->notify_out
+= peer_src
->notify_out
;
104 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
105 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
108 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
115 unsigned char last_evt
, last_maj_evt
;
117 assert(from_peer
!= NULL
);
119 peer
= from_peer
->doppelganger
;
121 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
124 if (bgp_debug_neighbor_events(peer
))
125 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
126 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
129 bgp_writes_off(peer
);
131 bgp_writes_off(from_peer
);
132 bgp_reads_off(from_peer
);
134 BGP_TIMER_OFF(peer
->t_routeadv
);
135 BGP_TIMER_OFF(peer
->t_connect
);
136 BGP_TIMER_OFF(peer
->t_connect_check
);
137 BGP_TIMER_OFF(from_peer
->t_routeadv
);
138 BGP_TIMER_OFF(from_peer
->t_connect
);
139 BGP_TIMER_OFF(from_peer
->t_connect_check
);
141 // At this point in time, it is possible that there are packets pending
143 // various buffers. Those need to be transferred or dropped, otherwise
145 // get spurious failures during session establishment.
146 pthread_mutex_lock(&peer
->io_mtx
);
147 pthread_mutex_lock(&from_peer
->io_mtx
);
150 peer
->fd
= from_peer
->fd
;
153 stream_fifo_clean(peer
->ibuf
);
154 stream_fifo_clean(peer
->obuf
);
155 stream_reset(peer
->ibuf_work
);
157 // this should never happen, since bgp_process_packet() is the
159 // that sets and unsets the current packet and it runs in our
163 "[%s] Dropping pending packet on connection transfer:",
165 u_int16_t type
= stream_getc_from(peer
->curr
,
166 BGP_MARKER_SIZE
+ 2);
167 bgp_dump_packet(peer
, type
, peer
->curr
);
168 stream_free(peer
->curr
);
172 // copy each packet from old peer's output queue to new peer
173 while (from_peer
->obuf
->head
)
174 stream_fifo_push(peer
->obuf
,
175 stream_fifo_pop(from_peer
->obuf
));
177 // copy each packet from old peer's input queue to new peer
178 while (from_peer
->ibuf
->head
)
179 stream_fifo_push(peer
->ibuf
,
180 stream_fifo_pop(from_peer
->ibuf
));
182 pthread_mutex_unlock(&from_peer
->io_mtx
);
183 pthread_mutex_unlock(&peer
->io_mtx
);
185 peer
->as
= from_peer
->as
;
186 peer
->v_holdtime
= from_peer
->v_holdtime
;
187 peer
->v_keepalive
= from_peer
->v_keepalive
;
188 peer
->routeadv
= from_peer
->routeadv
;
189 peer
->v_routeadv
= from_peer
->v_routeadv
;
190 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
191 peer
->cap
= from_peer
->cap
;
192 status
= peer
->status
;
193 pstatus
= peer
->ostatus
;
194 last_evt
= peer
->last_event
;
195 last_maj_evt
= peer
->last_major_event
;
196 peer
->status
= from_peer
->status
;
197 peer
->ostatus
= from_peer
->ostatus
;
198 peer
->last_event
= from_peer
->last_event
;
199 peer
->last_major_event
= from_peer
->last_major_event
;
200 from_peer
->status
= status
;
201 from_peer
->ostatus
= pstatus
;
202 from_peer
->last_event
= last_evt
;
203 from_peer
->last_major_event
= last_maj_evt
;
204 peer
->remote_id
= from_peer
->remote_id
;
206 if (from_peer
->hostname
!= NULL
) {
207 if (peer
->hostname
) {
208 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
209 peer
->hostname
= NULL
;
212 peer
->hostname
= from_peer
->hostname
;
213 from_peer
->hostname
= NULL
;
216 if (from_peer
->domainname
!= NULL
) {
217 if (peer
->domainname
) {
218 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
219 peer
->domainname
= NULL
;
222 peer
->domainname
= from_peer
->domainname
;
223 from_peer
->domainname
= NULL
;
226 FOREACH_AFI_SAFI (afi
, safi
) {
227 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
228 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
229 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
230 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
231 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
232 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
233 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
236 if (bgp_getsockname(peer
) < 0) {
238 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
239 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
242 peer
->host
, peer
->fd
, from_peer
->fd
);
247 if (from_peer
->status
> Active
) {
248 if (bgp_getsockname(from_peer
) < 0) {
250 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
251 (CHECK_FLAG(from_peer
->sflags
,
252 PEER_STATUS_ACCEPT_PEER
)
255 from_peer
->host
, from_peer
->fd
, peer
->fd
);
265 peer_xfer_stats(peer
, from_peer
);
270 /* Hook function called after bgp event is occered. And vty's
271 neighbor command invoke this function after making neighbor
273 void bgp_timer_set(struct peer
*peer
)
275 switch (peer
->status
) {
277 /* First entry point of peer's finite state machine. In Idle
278 status start timer is on unless peer is shutdown or peer is
279 inactive. All other timer must be turned off */
280 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)) {
281 BGP_TIMER_OFF(peer
->t_start
);
283 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
286 BGP_TIMER_OFF(peer
->t_connect
);
287 BGP_TIMER_OFF(peer
->t_holdtime
);
288 bgp_keepalives_off(peer
);
289 BGP_TIMER_OFF(peer
->t_routeadv
);
293 /* After start timer is expired, the peer moves to Connect
294 status. Make sure start timer is off and connect timer is
296 BGP_TIMER_OFF(peer
->t_start
);
297 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
299 BGP_TIMER_OFF(peer
->t_holdtime
);
300 bgp_keepalives_off(peer
);
301 BGP_TIMER_OFF(peer
->t_routeadv
);
305 /* Active is waiting connection from remote peer. And if
306 connect timer is expired, change status to Connect. */
307 BGP_TIMER_OFF(peer
->t_start
);
308 /* If peer is passive mode, do not set connect timer. */
309 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
310 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
311 BGP_TIMER_OFF(peer
->t_connect
);
313 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
316 BGP_TIMER_OFF(peer
->t_holdtime
);
317 bgp_keepalives_off(peer
);
318 BGP_TIMER_OFF(peer
->t_routeadv
);
322 /* OpenSent status. */
323 BGP_TIMER_OFF(peer
->t_start
);
324 BGP_TIMER_OFF(peer
->t_connect
);
325 if (peer
->v_holdtime
!= 0) {
326 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
329 BGP_TIMER_OFF(peer
->t_holdtime
);
331 bgp_keepalives_off(peer
);
332 BGP_TIMER_OFF(peer
->t_routeadv
);
336 /* OpenConfirm status. */
337 BGP_TIMER_OFF(peer
->t_start
);
338 BGP_TIMER_OFF(peer
->t_connect
);
340 /* If the negotiated Hold Time value is zero, then the Hold Time
341 timer and KeepAlive timers are not started. */
342 if (peer
->v_holdtime
== 0) {
343 BGP_TIMER_OFF(peer
->t_holdtime
);
344 bgp_keepalives_off(peer
);
346 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
348 bgp_keepalives_on(peer
);
350 BGP_TIMER_OFF(peer
->t_routeadv
);
354 /* In Established status start and connect timer is turned
356 BGP_TIMER_OFF(peer
->t_start
);
357 BGP_TIMER_OFF(peer
->t_connect
);
359 /* Same as OpenConfirm, if holdtime is zero then both holdtime
360 and keepalive must be turned off. */
361 if (peer
->v_holdtime
== 0) {
362 BGP_TIMER_OFF(peer
->t_holdtime
);
364 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
369 BGP_TIMER_OFF(peer
->t_gr_restart
);
370 BGP_TIMER_OFF(peer
->t_gr_stale
);
371 BGP_TIMER_OFF(peer
->t_pmax_restart
);
374 BGP_TIMER_OFF(peer
->t_start
);
375 BGP_TIMER_OFF(peer
->t_connect
);
376 BGP_TIMER_OFF(peer
->t_holdtime
);
377 bgp_keepalives_off(peer
);
378 BGP_TIMER_OFF(peer
->t_routeadv
);
383 /* BGP start timer. This function set BGP_Start event to thread value
384 and process event. */
385 static int bgp_start_timer(struct thread
*thread
)
389 peer
= THREAD_ARG(thread
);
390 peer
->t_start
= NULL
;
392 if (bgp_debug_neighbor_events(peer
))
393 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
395 THREAD_VAL(thread
) = BGP_Start
;
396 bgp_event(thread
); /* bgp_event unlocks peer */
401 /* BGP connect retry timer. */
402 static int bgp_connect_timer(struct thread
*thread
)
407 peer
= THREAD_ARG(thread
);
409 assert(!peer
->t_write
);
410 assert(!peer
->t_read
);
412 peer
->t_connect
= NULL
;
414 if (bgp_debug_neighbor_events(peer
))
415 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
417 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
421 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
422 bgp_event(thread
); /* bgp_event unlocks peer */
429 /* BGP holdtime timer. */
430 static int bgp_holdtime_timer(struct thread
*thread
)
434 peer
= THREAD_ARG(thread
);
435 peer
->t_holdtime
= NULL
;
437 if (bgp_debug_neighbor_events(peer
))
438 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
441 THREAD_VAL(thread
) = Hold_Timer_expired
;
442 bgp_event(thread
); /* bgp_event unlocks peer */
447 int bgp_routeadv_timer(struct thread
*thread
)
451 peer
= THREAD_ARG(thread
);
452 peer
->t_routeadv
= NULL
;
454 if (bgp_debug_neighbor_events(peer
))
455 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
458 peer
->synctime
= bgp_clock();
460 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
461 &peer
->t_generate_updgrp_packets
);
463 /* MRAI timer will be started again when FIFO is built, no need to
469 /* BGP Peer Down Cause */
470 const char *peer_down_str
[] = {"",
474 "Cluster ID changed",
475 "Confederation identifier changed",
476 "Confederation peer changed",
477 "RR client config change",
478 "RS client config change",
479 "Update source change",
480 "Address family activated",
483 "BGP Notification received",
484 "BGP Notification send",
485 "Peer closed the session",
487 "Peer-group add member",
488 "Peer-group delete member",
489 "Capability changed",
490 "Passive config change",
491 "Multihop config change",
492 "NSF peer closed the session",
493 "Intf peering v6only config change",
496 "Neighbor address lost"};
498 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
504 peer
= THREAD_ARG(thread
);
505 peer
->t_gr_restart
= NULL
;
507 /* NSF delete stale route */
508 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
509 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
510 if (peer
->nsf
[afi
][safi
])
511 bgp_clear_stale_route(peer
, afi
, safi
);
513 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
514 BGP_TIMER_OFF(peer
->t_gr_stale
);
516 if (bgp_debug_neighbor_events(peer
)) {
517 zlog_debug("%s graceful restart timer expired", peer
->host
);
518 zlog_debug("%s graceful restart stalepath timer stopped",
527 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
533 peer
= THREAD_ARG(thread
);
534 peer
->t_gr_stale
= NULL
;
536 if (bgp_debug_neighbor_events(peer
))
537 zlog_debug("%s graceful restart stalepath timer expired",
540 /* NSF delete stale route */
541 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
542 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
543 if (peer
->nsf
[afi
][safi
])
544 bgp_clear_stale_route(peer
, afi
, safi
);
549 static int bgp_update_delay_applicable(struct bgp
*bgp
)
551 /* update_delay_over flag should be reset (set to 0) for any new
552 applicability of the update-delay during BGP process lifetime.
553 And it should be set after an occurence of the update-delay is
555 if (!bgp
->update_delay_over
)
561 int bgp_update_delay_active(struct bgp
*bgp
)
563 if (bgp
->t_update_delay
)
569 int bgp_update_delay_configured(struct bgp
*bgp
)
571 if (bgp
->v_update_delay
)
577 /* Do the post-processing needed when bgp comes out of the read-only mode
578 on ending the update delay. */
579 void bgp_update_delay_end(struct bgp
*bgp
)
581 THREAD_TIMER_OFF(bgp
->t_update_delay
);
582 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
584 /* Reset update-delay related state */
585 bgp
->update_delay_over
= 1;
586 bgp
->established
= 0;
587 bgp
->restarted_peers
= 0;
588 bgp
->implicit_eors
= 0;
589 bgp
->explicit_eors
= 0;
591 quagga_timestamp(3, bgp
->update_delay_end_time
,
592 sizeof(bgp
->update_delay_end_time
));
595 * Add an end-of-initial-update marker to the main process queues so
597 * the route advertisement timer for the peers can be started. Also set
598 * the zebra and peer update hold flags. These flags are used to achieve
599 * three stages in the update-delay post processing:
600 * 1. Finish best-path selection for all the prefixes held on the
602 * (routes in BGP are updated, and peers sync queues are populated
604 * 2. As the eoiu mark is reached in the bgp process routine, ship all
606 * routes to zebra. With that zebra should see updates from BGP
609 * 3. Unblock the peer update writes. With that peer update packing
611 * the prefixes should be at its maximum.
613 bgp_add_eoiu_mark(bgp
);
614 bgp
->main_zebra_update_hold
= 1;
615 bgp
->main_peers_update_hold
= 1;
617 /* Resume the queue processing. This should trigger the event that would
619 care of processing any work that was queued during the read-only
621 work_queue_unplug(bm
->process_main_queue
);
627 void bgp_start_routeadv(struct bgp
*bgp
)
629 struct listnode
*node
, *nnode
;
632 zlog_info("bgp_start_routeadv(), update hold status %d",
633 bgp
->main_peers_update_hold
);
635 if (bgp
->main_peers_update_hold
)
638 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
639 sizeof(bgp
->update_delay_peers_resume_time
));
641 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
642 if (peer
->status
!= Established
)
644 BGP_TIMER_OFF(peer
->t_routeadv
);
645 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
652 void bgp_adjust_routeadv(struct peer
*peer
)
654 time_t nowtime
= bgp_clock();
656 unsigned long remain
;
658 /* Bypass checks for special case of MRAI being 0 */
659 if (peer
->v_routeadv
== 0) {
660 /* Stop existing timer, just in case it is running for a
662 * duration and schedule write thread immediately.
664 if (peer
->t_routeadv
)
665 BGP_TIMER_OFF(peer
->t_routeadv
);
667 peer
->synctime
= bgp_clock();
668 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
,
670 &peer
->t_generate_updgrp_packets
);
677 * If the last update was written more than MRAI back, expire the timer
678 * instantly so that we can send the update out sooner.
680 * <------- MRAI --------->
681 * |-----------------|-----------------------|
682 * <------------- m ------------>
691 diff
= difftime(nowtime
, peer
->last_update
);
692 if (diff
> (double)peer
->v_routeadv
) {
693 BGP_TIMER_OFF(peer
->t_routeadv
);
694 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
700 * - Find when to expire the MRAI timer.
701 * If MRAI timer is not active, assume we can start it now.
703 * <------- MRAI --------->
704 * |------------|-----------------------|
705 * <-------- m ----------><----- r ----->
714 if (peer
->t_routeadv
)
715 remain
= thread_timer_remain_second(peer
->t_routeadv
);
717 remain
= peer
->v_routeadv
;
718 diff
= peer
->v_routeadv
- diff
;
719 if (diff
<= (double)remain
) {
720 BGP_TIMER_OFF(peer
->t_routeadv
);
721 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
725 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
727 if (!bgp
->maxmed_onstartup_over
)
733 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
735 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
741 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
743 if (bgp
->t_maxmed_onstartup
)
749 void bgp_maxmed_update(struct bgp
*bgp
)
751 u_char maxmed_active
;
752 u_int32_t maxmed_value
;
754 if (bgp
->v_maxmed_admin
) {
756 maxmed_value
= bgp
->maxmed_admin_value
;
757 } else if (bgp
->t_maxmed_onstartup
) {
759 maxmed_value
= bgp
->maxmed_onstartup_value
;
762 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
765 if (bgp
->maxmed_active
!= maxmed_active
766 || bgp
->maxmed_value
!= maxmed_value
) {
767 bgp
->maxmed_active
= maxmed_active
;
768 bgp
->maxmed_value
= maxmed_value
;
770 update_group_announce(bgp
);
774 /* The maxmed onstartup timer expiry callback. */
775 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
779 zlog_info("Max med on startup ended - timer expired.");
781 bgp
= THREAD_ARG(thread
);
782 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
783 bgp
->maxmed_onstartup_over
= 1;
785 bgp_maxmed_update(bgp
);
790 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
792 /* Applicable only once in the process lifetime on the startup */
793 if (bgp
->maxmed_onstartup_over
)
796 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
797 bgp
->v_maxmed_onstartup
);
799 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
800 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
802 if (!bgp
->v_maxmed_admin
) {
803 bgp
->maxmed_active
= 1;
804 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
807 /* Route announce to all peers should happen after this in
811 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
813 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
814 bgp_maxmed_onstartup_begin(peer
->bgp
);
818 /* The update delay timer expiry callback. */
819 static int bgp_update_delay_timer(struct thread
*thread
)
823 zlog_info("Update delay ended - timer expired.");
825 bgp
= THREAD_ARG(thread
);
826 THREAD_TIMER_OFF(bgp
->t_update_delay
);
827 bgp_update_delay_end(bgp
);
832 /* The establish wait timer expiry callback. */
833 static int bgp_establish_wait_timer(struct thread
*thread
)
837 zlog_info("Establish wait - timer expired.");
839 bgp
= THREAD_ARG(thread
);
840 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
841 bgp_check_update_delay(bgp
);
846 /* Steps to begin the update delay:
847 - initialize queues if needed
848 - stop the queue processing
850 static void bgp_update_delay_begin(struct bgp
*bgp
)
852 struct listnode
*node
, *nnode
;
855 /* Stop the processing of queued work. Enqueue shall continue */
856 work_queue_plug(bm
->process_main_queue
);
858 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
859 peer
->update_delay_over
= 0;
861 /* Start the update-delay timer */
862 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
863 bgp
->v_update_delay
, &bgp
->t_update_delay
);
865 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
866 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
867 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
869 quagga_timestamp(3, bgp
->update_delay_begin_time
,
870 sizeof(bgp
->update_delay_begin_time
));
873 static void bgp_update_delay_process_status_change(struct peer
*peer
)
875 if (peer
->status
== Established
) {
876 if (!peer
->bgp
->established
++) {
877 bgp_update_delay_begin(peer
->bgp
);
879 "Begin read-only mode - update-delay timer %d seconds",
880 peer
->bgp
->v_update_delay
);
882 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
883 bgp_update_restarted_peers(peer
);
885 if (peer
->ostatus
== Established
886 && bgp_update_delay_active(peer
->bgp
)) {
887 /* Adjust the update-delay state to account for this flap.
888 NOTE: Intentionally skipping adjusting implicit_eors or
890 counters. Extra sanity check in bgp_check_update_delay()
892 be enough to take care of any additive discrepancy in bgp eor
894 peer
->bgp
->established
--;
895 peer
->update_delay_over
= 0;
899 /* Called after event occured, this function change status and reset
900 read/write and timer thread. */
901 void bgp_fsm_change_status(struct peer
*peer
, int status
)
904 bgp_dump_state(peer
, peer
->status
, status
);
906 /* Transition into Clearing or Deleted must /always/ clear all routes..
907 * (and must do so before actually changing into Deleted..
909 if (status
>= Clearing
) {
910 bgp_clear_route_all(peer
);
912 /* If no route was queued for the clear-node processing,
914 * completion event here. This is needed because if there are no
916 * to trigger the background clear-node thread, the event won't
918 * generated and the peer would be stuck in Clearing. Note that
920 * event is for the peer and helps the peer transition out of
922 * state; it should not be generated per (AFI,SAFI). The event
924 * directly posted here without calling clear_node_complete() as
926 * shouldn't do an extra unlock. This event will get processed
928 * the state change that happens below, so peer will be in
932 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
933 BGP_EVENT_ADD(peer
, Clearing_Completed
);
936 /* Preserve old status and change into new status. */
937 peer
->ostatus
= peer
->status
;
938 peer
->status
= status
;
940 /* Save event that caused status change. */
941 peer
->last_major_event
= peer
->cur_event
;
943 if (status
== Established
)
944 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
946 /* If max-med processing is applicable, do the necessary. */
947 if (status
== Established
) {
948 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
949 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
950 bgp_maxmed_onstartup_process_status_change(peer
);
952 peer
->bgp
->maxmed_onstartup_over
= 1;
955 /* If update-delay processing is applicable, do the necessary. */
956 if (bgp_update_delay_configured(peer
->bgp
)
957 && bgp_update_delay_applicable(peer
->bgp
))
958 bgp_update_delay_process_status_change(peer
);
960 if (bgp_debug_neighbor_events(peer
))
961 zlog_debug("%s went from %s to %s", peer
->host
,
962 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
963 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
966 /* Flush the event queue and ensure the peer is shut down */
967 static int bgp_clearing_completed(struct peer
*peer
)
969 int rc
= bgp_stop(peer
);
972 BGP_EVENT_FLUSH(peer
);
977 /* Administrative BGP peer stop event. */
978 /* May be called multiple times for the same peer */
979 int bgp_stop(struct peer
*peer
)
983 char orf_name
[BUFSIZ
];
986 if (peer_dynamic_neighbor(peer
)
987 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
988 if (bgp_debug_neighbor_events(peer
))
989 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
994 /* Can't do this in Clearing; events are used for state transitions */
995 if (peer
->status
!= Clearing
) {
996 /* Delete all existing events of the peer */
997 BGP_EVENT_FLUSH(peer
);
1000 /* Increment Dropped count. */
1001 if (peer
->status
== Established
) {
1004 /* bgp log-neighbor-changes of neighbor Down */
1005 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1006 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1008 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1010 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1011 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1014 peer_down_str
[(int)peer
->last_reset
]);
1017 /* graceful restart */
1018 if (peer
->t_gr_stale
) {
1019 BGP_TIMER_OFF(peer
->t_gr_stale
);
1020 if (bgp_debug_neighbor_events(peer
))
1022 "%s graceful restart stalepath timer stopped",
1025 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1026 if (bgp_debug_neighbor_events(peer
)) {
1028 "%s graceful restart timer started for %d sec",
1029 peer
->host
, peer
->v_gr_restart
);
1031 "%s graceful restart stalepath timer started for %d sec",
1032 peer
->host
, peer
->bgp
->stalepath_time
);
1034 BGP_TIMER_ON(peer
->t_gr_restart
,
1035 bgp_graceful_restart_timer_expire
,
1036 peer
->v_gr_restart
);
1037 BGP_TIMER_ON(peer
->t_gr_stale
,
1038 bgp_graceful_stale_timer_expire
,
1039 peer
->bgp
->stalepath_time
);
1041 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1043 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1044 for (safi
= SAFI_UNICAST
;
1045 safi
<= SAFI_MPLS_VPN
; safi
++)
1046 peer
->nsf
[afi
][safi
] = 0;
1049 /* set last reset time */
1050 peer
->resettime
= peer
->uptime
= bgp_clock();
1052 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1053 zlog_debug("%s remove from all update group",
1055 update_group_remove_peer_afs(peer
);
1057 hook_call(peer_backward_transition
, peer
);
1059 /* Reset peer synctime */
1062 bgp_bfd_deregister_peer(peer
);
1065 /* stop keepalives */
1066 bgp_keepalives_off(peer
);
1068 /* Stop read and write threads. */
1069 bgp_writes_off(peer
);
1070 bgp_reads_off(peer
);
1072 THREAD_OFF(peer
->t_connect_check
);
1074 /* Stop all timers. */
1075 BGP_TIMER_OFF(peer
->t_start
);
1076 BGP_TIMER_OFF(peer
->t_connect
);
1077 BGP_TIMER_OFF(peer
->t_holdtime
);
1078 BGP_TIMER_OFF(peer
->t_routeadv
);
1080 /* Clear input and output buffer. */
1081 pthread_mutex_lock(&peer
->io_mtx
);
1084 stream_fifo_clean(peer
->ibuf
);
1086 stream_fifo_clean(peer
->obuf
);
1088 if (peer
->ibuf_work
)
1089 stream_reset(peer
->ibuf_work
);
1090 if (peer
->obuf_work
)
1091 stream_reset(peer
->obuf_work
);
1094 stream_free(peer
->curr
);
1098 pthread_mutex_unlock(&peer
->io_mtx
);
1100 /* Close of file descriptor. */
1101 if (peer
->fd
>= 0) {
1106 FOREACH_AFI_SAFI (afi
, safi
) {
1107 /* Reset all negotiated variables */
1108 peer
->afc_nego
[afi
][safi
] = 0;
1109 peer
->afc_adv
[afi
][safi
] = 0;
1110 peer
->afc_recv
[afi
][safi
] = 0;
1112 /* peer address family capability flags*/
1113 peer
->af_cap
[afi
][safi
] = 0;
1115 /* peer address family status flags*/
1116 peer
->af_sflags
[afi
][safi
] = 0;
1118 /* Received ORF prefix-filter */
1119 peer
->orf_plist
[afi
][safi
] = NULL
;
1121 if ((peer
->status
== OpenConfirm
)
1122 || (peer
->status
== Established
)) {
1123 /* ORF received prefix-filter pnt */
1124 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1125 prefix_bgp_orf_remove_all(afi
, orf_name
);
1129 /* Reset keepalive and holdtime */
1130 if (PEER_OR_GROUP_TIMER_SET(peer
)) {
1131 peer
->v_keepalive
= peer
->keepalive
;
1132 peer
->v_holdtime
= peer
->holdtime
;
1134 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1135 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1138 peer
->update_time
= 0;
1140 /* Until we are sure that there is no problem about prefix count
1141 this should be commented out.*/
1143 /* Reset prefix count */
1144 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1145 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1146 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1147 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1148 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1149 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1150 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1153 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1154 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1158 bgp_peer_conf_if_to_su_update(peer
);
1164 /* BGP peer is stoped by the error. */
1165 static int bgp_stop_with_error(struct peer
*peer
)
1167 /* Double start timer. */
1170 /* Overflow check. */
1171 if (peer
->v_start
>= (60 * 2))
1172 peer
->v_start
= (60 * 2);
1174 if (peer_dynamic_neighbor(peer
)) {
1175 if (bgp_debug_neighbor_events(peer
))
1176 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1181 return (bgp_stop(peer
));
1185 /* something went wrong, send notify and tear down */
1186 static int bgp_stop_with_notify(struct peer
*peer
, u_char code
, u_char sub_code
)
1188 /* Send notify to remote peer */
1189 bgp_notify_send(peer
, code
, sub_code
);
1191 if (peer_dynamic_neighbor(peer
)) {
1192 if (bgp_debug_neighbor_events(peer
))
1193 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1198 /* Clear start timer value to default. */
1199 peer
->v_start
= BGP_INIT_START_TIMER
;
1201 return (bgp_stop(peer
));
1205 * Determines whether a TCP session has successfully established for a peer and
1206 * events as appropriate.
1208 * This function is called when setting up a new session. After connect() is
1209 * called on the peer's socket (in bgp_start()), the fd is passed to select()
1210 * to wait for connection success or failure. When select() returns, this
1211 * function is called to evaluate the result.
1213 static int bgp_connect_check(struct thread
*thread
)
1220 peer
= THREAD_ARG(thread
);
1221 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1222 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1223 assert(!peer
->t_read
);
1224 assert(!peer
->t_write
);
1226 peer
->t_connect_check
= NULL
;
1228 /* Check file descriptor. */
1229 slen
= sizeof(status
);
1230 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1233 /* If getsockopt is fail, this is fatal error. */
1235 zlog_info("can't get sockopt for nonblocking connect");
1236 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1240 /* When status is 0 then TCP connection is established. */
1242 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1245 if (bgp_debug_neighbor_events(peer
))
1246 zlog_debug("%s [Event] Connect failed (%s)", peer
->host
,
1247 safe_strerror(errno
));
1248 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1253 /* TCP connection open. Next we send open message to remote peer. And
1254 add read thread for reading open message. */
1255 static int bgp_connect_success(struct peer
*peer
)
1258 zlog_err("bgp_connect_success peer's fd is negative value %d",
1264 if (bgp_getsockname(peer
) < 0) {
1265 zlog_err("%s: bgp_getsockname(): failed for peer %s, fd %d",
1266 __FUNCTION__
, peer
->host
, peer
->fd
);
1267 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1268 0); /* internal error */
1269 bgp_writes_on(peer
);
1275 if (bgp_debug_neighbor_events(peer
)) {
1276 char buf1
[SU_ADDRSTRLEN
];
1278 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1279 zlog_debug("%s open active, local address %s",
1281 sockunion2str(peer
->su_local
, buf1
,
1284 zlog_debug("%s passive open", peer
->host
);
1287 bgp_open_send(peer
);
1292 /* TCP connect fail */
1293 static int bgp_connect_fail(struct peer
*peer
)
1295 if (peer_dynamic_neighbor(peer
)) {
1296 if (bgp_debug_neighbor_events(peer
))
1297 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1302 return (bgp_stop(peer
));
1305 /* This function is the first starting point of all BGP connection. It
1306 try to connect to remote peer with non-blocking IO. */
1307 int bgp_start(struct peer
*peer
)
1312 bgp_peer_conf_if_to_su_update(peer
);
1314 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1315 if (bgp_debug_neighbor_events(peer
))
1317 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1322 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1323 if (bgp_debug_neighbor_events(peer
))
1325 "%s [FSM] Trying to start suppressed peer"
1326 " - this is never supposed to happen!",
1331 /* Scrub some information that might be left over from a previous,
1334 /* Connection information. */
1335 if (peer
->su_local
) {
1336 sockunion_free(peer
->su_local
);
1337 peer
->su_local
= NULL
;
1340 if (peer
->su_remote
) {
1341 sockunion_free(peer
->su_remote
);
1342 peer
->su_remote
= NULL
;
1345 /* Clear remote router-id. */
1346 peer
->remote_id
.s_addr
= 0;
1348 /* Clear peer capability flag. */
1351 /* If the peer is passive mode, force to move to Active mode. */
1352 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1353 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1357 /* Register to be notified on peer up */
1358 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
1359 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1360 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1365 if (!bgp_find_or_add_nexthop(peer
->bgp
,
1366 family2afi(peer
->su
.sa
.sa_family
), NULL
,
1368 #if defined(HAVE_CUMULUS)
1369 if (bgp_debug_neighbor_events(peer
))
1370 zlog_debug("%s [FSM] Waiting for NHT", peer
->host
);
1372 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1377 assert(!peer
->t_write
);
1378 assert(!peer
->t_read
);
1379 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1380 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1381 status
= bgp_connect(peer
);
1385 if (bgp_debug_neighbor_events(peer
))
1386 zlog_debug("%s [FSM] Connect error", peer
->host
);
1387 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1389 case connect_success
:
1390 if (bgp_debug_neighbor_events(peer
))
1392 "%s [FSM] Connect immediately success, fd %d",
1393 peer
->host
, peer
->fd
);
1394 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1396 case connect_in_progress
:
1397 /* To check nonblocking connect, we wait until socket is
1398 readable or writable. */
1399 if (bgp_debug_neighbor_events(peer
))
1401 "%s [FSM] Non blocking connect waiting result, fd %d",
1402 peer
->host
, peer
->fd
);
1404 zlog_err("bgp_start peer's fd is negative value %d",
1408 // when the socket becomes ready (or fails to connect),
1409 // bgp_connect_check
1411 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1412 &peer
->t_connect_check
);
1418 /* Connect retry timer is expired when the peer status is Connect. */
1419 static int bgp_reconnect(struct peer
*peer
)
1421 if (bgp_stop(peer
) < 0)
1428 static int bgp_fsm_open(struct peer
*peer
)
1430 /* Send keepalive and make keepalive timer */
1431 bgp_keepalive_send(peer
);
1433 /* Reset holdtimer value. */
1434 BGP_TIMER_OFF(peer
->t_holdtime
);
1439 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1440 peer and change to Idle status. */
1441 static int bgp_fsm_event_error(struct peer
*peer
)
1443 zlog_err("%s [FSM] unexpected packet received in state %s", peer
->host
,
1444 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1446 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1449 /* Hold timer expire. This is error of BGP connection. So cut the
1450 peer and change to Idle status. */
1451 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1453 if (bgp_debug_neighbor_events(peer
))
1454 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1456 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1460 * Transition to Established state.
1462 * Convert peer from stub to full fledged peer, set some timers, and generate
1465 static int bgp_establish(struct peer
*peer
)
1469 int nsf_af_count
= 0;
1473 other
= peer
->doppelganger
;
1474 peer
= peer_xfer_conn(peer
);
1476 zlog_err("%%Neighbor failed in xfer_conn");
1482 1; /* bgp_establish specific code when xfer_conn
1485 /* Reset capability open status flag. */
1486 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1487 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1489 /* Clear start timer value to default. */
1490 peer
->v_start
= BGP_INIT_START_TIMER
;
1492 /* Increment established count. */
1493 peer
->established
++;
1494 bgp_fsm_change_status(peer
, Established
);
1496 /* bgp log-neighbor-changes of neighbor Up */
1497 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1498 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1499 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1501 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1502 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1506 /* assign update-group/subgroup */
1507 update_group_adjust_peer_afs(peer
);
1509 /* graceful restart */
1510 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1511 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1512 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1513 if (peer
->afc_nego
[afi
][safi
]
1514 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1515 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1516 PEER_CAP_RESTART_AF_RCV
)) {
1517 if (peer
->nsf
[afi
][safi
]
1519 peer
->af_cap
[afi
][safi
],
1520 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1521 bgp_clear_stale_route(peer
, afi
, safi
);
1523 peer
->nsf
[afi
][safi
] = 1;
1526 if (peer
->nsf
[afi
][safi
])
1527 bgp_clear_stale_route(peer
, afi
, safi
);
1528 peer
->nsf
[afi
][safi
] = 0;
1533 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1535 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1536 if (peer
->t_gr_stale
) {
1537 BGP_TIMER_OFF(peer
->t_gr_stale
);
1538 if (bgp_debug_neighbor_events(peer
))
1540 "%s graceful restart stalepath timer stopped",
1545 if (peer
->t_gr_restart
) {
1546 BGP_TIMER_OFF(peer
->t_gr_restart
);
1547 if (bgp_debug_neighbor_events(peer
))
1548 zlog_debug("%s graceful restart timer stopped",
1552 hook_call(peer_established
, peer
);
1554 /* Reset uptime, turn on keepalives, send current table. */
1555 if (!peer
->v_holdtime
)
1556 bgp_keepalives_on(peer
);
1558 peer
->uptime
= bgp_clock();
1560 /* Send route-refresh when ORF is enabled */
1561 FOREACH_AFI_SAFI (afi
, safi
) {
1562 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1563 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1564 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1565 PEER_CAP_ORF_PREFIX_RM_RCV
))
1566 bgp_route_refresh_send(peer
, afi
, safi
,
1568 REFRESH_IMMEDIATE
, 0);
1569 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1570 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1571 bgp_route_refresh_send(peer
, afi
, safi
,
1572 ORF_TYPE_PREFIX_OLD
,
1573 REFRESH_IMMEDIATE
, 0);
1577 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1578 FOREACH_AFI_SAFI (afi
, safi
) {
1579 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1580 PEER_CAP_ORF_PREFIX_RM_ADV
))
1581 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1582 PEER_CAP_ORF_PREFIX_SM_RCV
)
1583 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1584 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1585 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1586 PEER_STATUS_ORF_WAIT_REFRESH
);
1589 bgp_announce_peer(peer
);
1591 /* Start the route advertisement timer to send updates to the peer - if
1593 * is not in read-only mode. If it is, the timer will be started at the
1595 * of read-only mode.
1597 if (!bgp_update_delay_active(peer
->bgp
)) {
1598 BGP_TIMER_OFF(peer
->t_routeadv
);
1599 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1602 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1603 if (bgp_debug_neighbor_events(peer
))
1605 "[Event] Deleting stub connection for peer %s",
1608 if (peer
->doppelganger
->status
> Active
)
1609 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1610 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1612 peer_delete(peer
->doppelganger
);
1615 bgp_bfd_register_peer(peer
);
1619 /* Keepalive packet is received. */
1620 static int bgp_fsm_keepalive(struct peer
*peer
)
1622 BGP_TIMER_OFF(peer
->t_holdtime
);
1626 /* Update packet is received. */
1627 static int bgp_fsm_update(struct peer
*peer
)
1629 BGP_TIMER_OFF(peer
->t_holdtime
);
1633 /* This is empty event. */
1634 static int bgp_ignore(struct peer
*peer
)
1637 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1638 peer
->host
, bgp_event_str
[peer
->cur_event
],
1639 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1640 bgp_event_str
[peer
->last_event
],
1641 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1645 /* This is to handle unexpected events.. */
1646 static int bgp_fsm_exeption(struct peer
*peer
)
1648 zlog_err("%s [FSM] cur_event: %d", peer
->host
, peer
->cur_event
);
1650 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1651 peer
->host
, bgp_event_str
[peer
->cur_event
],
1652 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1653 bgp_event_str
[peer
->last_event
],
1654 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1655 return (bgp_stop(peer
));
1658 void bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1663 switch (peer
->status
) {
1666 BGP_EVENT_ADD(peer
, BGP_Start
);
1670 BGP_TIMER_OFF(peer
->t_connect
);
1671 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1676 BGP_TIMER_OFF(peer
->t_connect
);
1677 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1683 if (!valid
&& (peer
->gtsm_hops
== 1))
1684 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1693 /* Finite State Machine structure */
1694 static const struct {
1695 int (*func
)(struct peer
*);
1697 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1699 /* Idle state: In Idle state, all events other than BGP_Start is
1700 ignored. With BGP_Start event, finite state machine calls
1702 {bgp_start
, Connect
}, /* BGP_Start */
1703 {bgp_stop
, Idle
}, /* BGP_Stop */
1704 {bgp_stop
, Idle
}, /* TCP_connection_open */
1705 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1706 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1707 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1708 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1709 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1710 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1711 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1712 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1713 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1714 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1715 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1719 {bgp_ignore
, Connect
}, /* BGP_Start */
1720 {bgp_stop
, Idle
}, /* BGP_Stop */
1721 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1722 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1723 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1724 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1725 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1726 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1727 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1728 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1729 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1730 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1731 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1732 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1736 {bgp_ignore
, Active
}, /* BGP_Start */
1737 {bgp_stop
, Idle
}, /* BGP_Stop */
1738 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1739 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1740 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1741 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1742 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1743 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1744 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1745 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1746 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1747 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1748 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1749 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1753 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1754 {bgp_stop
, Idle
}, /* BGP_Stop */
1755 {bgp_stop
, Active
}, /* TCP_connection_open */
1756 {bgp_stop
, Active
}, /* TCP_connection_closed */
1757 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1758 {bgp_stop
, Active
}, /* TCP_fatal_error */
1759 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1760 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1761 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1762 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1763 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1764 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1765 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1766 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1770 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1771 {bgp_stop
, Idle
}, /* BGP_Stop */
1772 {bgp_stop
, Idle
}, /* TCP_connection_open */
1773 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1774 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1775 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1776 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1777 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1778 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1779 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1780 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1781 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1782 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1783 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1787 {bgp_ignore
, Established
}, /* BGP_Start */
1788 {bgp_stop
, Clearing
}, /* BGP_Stop */
1789 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1790 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1791 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1792 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1793 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1794 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1795 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1796 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1798 Established
}, /* Receive_KEEPALIVE_message */
1799 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1800 {bgp_stop_with_error
,
1801 Clearing
}, /* Receive_NOTIFICATION_message */
1802 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1806 {bgp_ignore
, Clearing
}, /* BGP_Start */
1807 {bgp_stop
, Clearing
}, /* BGP_Stop */
1808 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1809 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1810 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1811 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1812 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1813 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1814 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1815 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1816 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1817 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1818 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1819 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1823 {bgp_ignore
, Deleted
}, /* BGP_Start */
1824 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1825 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1826 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1827 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1828 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1829 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1830 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1831 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1832 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1833 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1834 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1835 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1836 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1840 /* Execute event process. */
1841 int bgp_event(struct thread
*thread
)
1847 peer
= THREAD_ARG(thread
);
1848 event
= THREAD_VAL(thread
);
1850 ret
= bgp_event_update(peer
, event
);
1855 int bgp_event_update(struct peer
*peer
, int event
)
1860 int passive_conn
= 0;
1863 /* default return code */
1864 ret
= FSM_PEER_NOOP
;
1866 other
= peer
->doppelganger
;
1868 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1869 dyn_nbr
= peer_dynamic_neighbor(peer
);
1871 /* Logging this event. */
1872 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1874 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1875 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1876 bgp_event_str
[event
],
1877 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1878 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1880 peer
->last_event
= peer
->cur_event
;
1881 peer
->cur_event
= event
;
1883 /* Call function. */
1884 if (FSM
[peer
->status
- 1][event
- 1].func
)
1885 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1888 if (ret
== 1 && next
== Established
) {
1889 /* The case when doppelganger swap accurred in
1891 Update the peer pointer accordingly */
1892 ret
= FSM_PEER_TRANSFERRED
;
1896 /* If status is changed. */
1897 if (next
!= peer
->status
) {
1898 bgp_fsm_change_status(peer
, next
);
1900 /* If we're going to ESTABLISHED then we executed a peer
1902 * this case we can either return FSM_PEER_TRANSITIONED
1904 * FSM_PEER_TRANSFERRED. Opting for TRANSFERRED since
1906 * session establishment. */
1907 if (ret
!= FSM_PEER_TRANSFERRED
)
1908 ret
= FSM_PEER_TRANSITIONED
;
1911 /* Make sure timer is set. */
1912 bgp_timer_set(peer
);
1914 } else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
1915 /* If we got a return value of -1, that means there was an
1917 * the FSM. If the peer structure was deleted
1920 "%s [FSM] Failure handling event %s in state %s, "
1921 "prior events %s, %s, fd %d",
1922 peer
->host
, bgp_event_str
[peer
->cur_event
],
1923 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1924 bgp_event_str
[peer
->last_event
],
1925 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1927 bgp_fsm_change_status(peer
, Idle
);
1928 bgp_timer_set(peer
);