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
18 along with GNU Zebra; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "sockunion.h"
33 #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"
49 #include "bgpd/bgp_snmp.h"
50 #endif /* HAVE_SNMP */
51 #include "bgpd/bgp_updgrp.h"
52 #include "bgpd/bgp_nht.h"
53 #include "bgpd/bgp_bfd.h"
54 #include "bgpd/bgp_memory.h"
56 /* Definition of display strings corresponding to FSM events. This should be
57 * kept consistent with the events defined in bgpd.h
59 static const char *bgp_event_str
[] =
64 "TCP_connection_open",
65 "TCP_connection_closed",
66 "TCP_connection_open_failed",
68 "ConnectRetry_timer_expired",
70 "KeepAlive_timer_expired",
71 "Receive_OPEN_message",
72 "Receive_KEEPALIVE_message",
73 "Receive_UPDATE_message",
74 "Receive_NOTIFICATION_message",
78 /* BGP FSM (finite state machine) has three types of functions. Type
79 one is thread functions. Type two is event functions. Type three
80 is FSM functions. Timer functions are set by bgp_timer_set
83 /* BGP event function. */
84 int bgp_event (struct thread
*);
86 /* BGP thread functions. */
87 static int bgp_start_timer (struct thread
*);
88 static int bgp_connect_timer (struct thread
*);
89 static int bgp_holdtime_timer (struct thread
*);
90 static int bgp_keepalive_timer (struct thread
*);
92 /* BGP FSM functions. */
93 static int bgp_start (struct peer
*);
96 peer_xfer_stats (struct peer
*peer_dst
, struct peer
*peer_src
)
98 /* Copy stats over. These are only the pre-established state stats */
99 peer_dst
->open_in
+= peer_src
->open_in
;
100 peer_dst
->open_out
+= peer_src
->open_out
;
101 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
102 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
103 peer_dst
->notify_in
+= peer_src
->notify_in
;
104 peer_dst
->notify_out
+= peer_src
->notify_out
;
105 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
106 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
110 peer_xfer_conn(struct peer
*from_peer
)
117 unsigned char last_evt
, last_maj_evt
;
119 assert(from_peer
!= NULL
);
121 peer
= from_peer
->doppelganger
;
123 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
126 if (bgp_debug_neighbor_events(peer
))
127 zlog_debug ("%s: peer transfer %p fd %d -> %p fd %d)", from_peer
->host
,
128 from_peer
, from_peer
->fd
, peer
, peer
->fd
);
130 BGP_WRITE_OFF(peer
->t_write
);
131 BGP_READ_OFF(peer
->t_read
);
132 BGP_WRITE_OFF(from_peer
->t_write
);
133 BGP_READ_OFF(from_peer
->t_read
);
135 BGP_TIMER_OFF(peer
->t_routeadv
);
136 BGP_TIMER_OFF(from_peer
->t_routeadv
);
139 peer
->fd
= from_peer
->fd
;
141 stream_reset(peer
->ibuf
);
142 stream_fifo_clean(peer
->obuf
);
143 stream_fifo_clean(from_peer
->obuf
);
145 peer
->as
= from_peer
->as
;
146 peer
->v_holdtime
= from_peer
->v_holdtime
;
147 peer
->v_keepalive
= from_peer
->v_keepalive
;
148 peer
->routeadv
= from_peer
->routeadv
;
149 peer
->v_routeadv
= from_peer
->v_routeadv
;
150 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
151 peer
->cap
= from_peer
->cap
;
152 status
= peer
->status
;
153 pstatus
= peer
->ostatus
;
154 last_evt
= peer
->last_event
;
155 last_maj_evt
= peer
->last_major_event
;
156 peer
->status
= from_peer
->status
;
157 peer
->ostatus
= from_peer
->ostatus
;
158 peer
->last_event
= from_peer
->last_event
;
159 peer
->last_major_event
= from_peer
->last_major_event
;
160 from_peer
->status
= status
;
161 from_peer
->ostatus
= pstatus
;
162 from_peer
->last_event
= last_evt
;
163 from_peer
->last_major_event
= last_maj_evt
;
164 peer
->remote_id
= from_peer
->remote_id
;
166 if (from_peer
->hostname
!= NULL
)
170 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
171 peer
->hostname
= NULL
;
174 peer
->hostname
= XSTRDUP(MTYPE_BGP_PEER_HOST
, from_peer
->hostname
);
175 XFREE(MTYPE_BGP_PEER_HOST
, from_peer
->hostname
);
176 from_peer
->hostname
= NULL
;
179 if (from_peer
->domainname
!= NULL
)
181 if (peer
->domainname
)
183 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
184 peer
->domainname
= NULL
;
187 peer
->domainname
= XSTRDUP(MTYPE_BGP_PEER_HOST
, from_peer
->domainname
);
188 XFREE(MTYPE_BGP_PEER_HOST
, from_peer
->domainname
);
189 from_peer
->domainname
= NULL
;
192 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
193 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
195 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
196 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
197 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
198 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
199 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
200 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
201 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
204 if (bgp_getsockname(peer
) < 0)
206 zlog_err ("%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
207 (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
208 peer
->host
, peer
->fd
, from_peer
->fd
);
213 if (from_peer
->status
> Active
)
215 if (bgp_getsockname(from_peer
) < 0)
217 zlog_err ("%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
218 (CHECK_FLAG (from_peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
219 from_peer
->host
, from_peer
->fd
, peer
->fd
);
225 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
226 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
229 peer_xfer_stats(peer
, from_peer
);
234 /* Hook function called after bgp event is occered. And vty's
235 neighbor command invoke this function after making neighbor
238 bgp_timer_set (struct peer
*peer
)
240 switch (peer
->status
)
243 /* First entry point of peer's finite state machine. In Idle
244 status start timer is on unless peer is shutdown or peer is
245 inactive. All other timer must be turned off */
246 if (BGP_PEER_START_SUPPRESSED (peer
) || ! peer_active (peer
))
248 BGP_TIMER_OFF (peer
->t_start
);
252 BGP_TIMER_ON (peer
->t_start
, bgp_start_timer
,
255 BGP_TIMER_OFF (peer
->t_connect
);
256 BGP_TIMER_OFF (peer
->t_holdtime
);
257 BGP_TIMER_OFF (peer
->t_keepalive
);
258 BGP_TIMER_OFF (peer
->t_routeadv
);
262 /* After start timer is expired, the peer moves to Connect
263 status. Make sure start timer is off and connect timer is
265 BGP_TIMER_OFF (peer
->t_start
);
266 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
267 BGP_TIMER_OFF (peer
->t_holdtime
);
268 BGP_TIMER_OFF (peer
->t_keepalive
);
269 BGP_TIMER_OFF (peer
->t_routeadv
);
273 /* Active is waiting connection from remote peer. And if
274 connect timer is expired, change status to Connect. */
275 BGP_TIMER_OFF (peer
->t_start
);
276 /* If peer is passive mode, do not set connect timer. */
277 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
)
278 || CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
280 BGP_TIMER_OFF (peer
->t_connect
);
284 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
286 BGP_TIMER_OFF (peer
->t_holdtime
);
287 BGP_TIMER_OFF (peer
->t_keepalive
);
288 BGP_TIMER_OFF (peer
->t_routeadv
);
292 /* OpenSent status. */
293 BGP_TIMER_OFF (peer
->t_start
);
294 BGP_TIMER_OFF (peer
->t_connect
);
295 if (peer
->v_holdtime
!= 0)
297 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
302 BGP_TIMER_OFF (peer
->t_holdtime
);
304 BGP_TIMER_OFF (peer
->t_keepalive
);
305 BGP_TIMER_OFF (peer
->t_routeadv
);
309 /* OpenConfirm status. */
310 BGP_TIMER_OFF (peer
->t_start
);
311 BGP_TIMER_OFF (peer
->t_connect
);
313 /* If the negotiated Hold Time value is zero, then the Hold Time
314 timer and KeepAlive timers are not started. */
315 if (peer
->v_holdtime
== 0)
317 BGP_TIMER_OFF (peer
->t_holdtime
);
318 BGP_TIMER_OFF (peer
->t_keepalive
);
322 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
324 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
327 BGP_TIMER_OFF (peer
->t_routeadv
);
331 /* In Established status start and connect timer is turned
333 BGP_TIMER_OFF (peer
->t_start
);
334 BGP_TIMER_OFF (peer
->t_connect
);
336 /* Same as OpenConfirm, if holdtime is zero then both holdtime
337 and keepalive must be turned off. */
338 if (peer
->v_holdtime
== 0)
340 BGP_TIMER_OFF (peer
->t_holdtime
);
341 BGP_TIMER_OFF (peer
->t_keepalive
);
345 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
347 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
352 BGP_TIMER_OFF (peer
->t_gr_restart
);
353 BGP_TIMER_OFF (peer
->t_gr_stale
);
354 BGP_TIMER_OFF (peer
->t_pmax_restart
);
356 BGP_TIMER_OFF (peer
->t_start
);
357 BGP_TIMER_OFF (peer
->t_connect
);
358 BGP_TIMER_OFF (peer
->t_holdtime
);
359 BGP_TIMER_OFF (peer
->t_keepalive
);
360 BGP_TIMER_OFF (peer
->t_routeadv
);
365 /* BGP start timer. This function set BGP_Start event to thread value
366 and process event. */
368 bgp_start_timer (struct thread
*thread
)
372 peer
= THREAD_ARG (thread
);
373 peer
->t_start
= NULL
;
375 if (bgp_debug_neighbor_events(peer
))
376 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
378 THREAD_VAL (thread
) = BGP_Start
;
379 bgp_event (thread
); /* bgp_event unlocks peer */
384 /* BGP connect retry timer. */
386 bgp_connect_timer (struct thread
*thread
)
391 peer
= THREAD_ARG (thread
);
392 peer
->t_connect
= NULL
;
394 if (bgp_debug_neighbor_events(peer
))
395 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
397 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
404 THREAD_VAL (thread
) = ConnectRetry_timer_expired
;
405 bgp_event (thread
); /* bgp_event unlocks peer */
412 /* BGP holdtime timer. */
414 bgp_holdtime_timer (struct thread
*thread
)
418 peer
= THREAD_ARG (thread
);
419 peer
->t_holdtime
= NULL
;
421 if (bgp_debug_neighbor_events(peer
))
422 zlog_debug ("%s [FSM] Timer (holdtime timer expire)", peer
->host
);
424 THREAD_VAL (thread
) = Hold_Timer_expired
;
425 bgp_event (thread
); /* bgp_event unlocks peer */
430 /* BGP keepalive fire ! */
432 bgp_keepalive_timer (struct thread
*thread
)
436 peer
= THREAD_ARG (thread
);
437 peer
->t_keepalive
= NULL
;
439 if (bgp_debug_neighbor_events(peer
))
440 zlog_debug ("%s [FSM] Timer (keepalive timer expire)", peer
->host
);
442 THREAD_VAL (thread
) = KeepAlive_timer_expired
;
443 bgp_event (thread
); /* bgp_event unlocks peer */
449 bgp_routeadv_timer (struct thread
*thread
)
453 peer
= THREAD_ARG (thread
);
454 peer
->t_routeadv
= NULL
;
456 if (bgp_debug_neighbor_events(peer
))
457 zlog_debug ("%s [FSM] Timer (routeadv timer expire)", peer
->host
);
459 peer
->synctime
= bgp_clock ();
461 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
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
[] =
476 "Cluster ID changed",
477 "Confederation identifier changed",
478 "Confederation peer changed",
479 "RR client config change",
480 "RS client config change",
481 "Update source change",
482 "Address family activated",
485 "BGP Notification received",
486 "BGP Notification send",
487 "Peer closed the session",
489 "Peer-group add member",
490 "Peer-group delete member",
491 "Capability changed",
492 "Passive config change",
493 "Multihop config change",
494 "NSF peer closed the session",
495 "Intf peering v6only config change",
498 "Neighbor address lost"
502 bgp_graceful_restart_timer_expire (struct thread
*thread
)
508 peer
= THREAD_ARG (thread
);
509 peer
->t_gr_restart
= NULL
;
511 /* NSF delete stale route */
512 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
513 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
514 if (peer
->nsf
[afi
][safi
])
515 bgp_clear_stale_route (peer
, afi
, safi
);
517 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
518 BGP_TIMER_OFF (peer
->t_gr_stale
);
520 if (bgp_debug_neighbor_events(peer
))
522 zlog_debug ("%s graceful restart timer expired", peer
->host
);
523 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
526 bgp_timer_set (peer
);
532 bgp_graceful_stale_timer_expire (struct thread
*thread
)
538 peer
= THREAD_ARG (thread
);
539 peer
->t_gr_stale
= NULL
;
541 if (bgp_debug_neighbor_events(peer
))
542 zlog_debug ("%s graceful restart stalepath timer expired", peer
->host
);
544 /* NSF delete stale route */
545 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
546 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
547 if (peer
->nsf
[afi
][safi
])
548 bgp_clear_stale_route (peer
, afi
, safi
);
554 bgp_update_delay_applicable (struct bgp
*bgp
)
556 /* update_delay_over flag should be reset (set to 0) for any new
557 applicability of the update-delay during BGP process lifetime.
558 And it should be set after an occurence of the update-delay is over)*/
559 if (!bgp
->update_delay_over
)
566 bgp_update_delay_active (struct bgp
*bgp
)
568 if (bgp
->t_update_delay
)
575 bgp_update_delay_configured (struct bgp
*bgp
)
577 if (bgp
->v_update_delay
)
583 /* Do the post-processing needed when bgp comes out of the read-only mode
584 on ending the update delay. */
586 bgp_update_delay_end (struct bgp
*bgp
)
588 THREAD_TIMER_OFF (bgp
->t_update_delay
);
589 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
591 /* Reset update-delay related state */
592 bgp
->update_delay_over
= 1;
593 bgp
->established
= 0;
594 bgp
->restarted_peers
= 0;
595 bgp
->implicit_eors
= 0;
596 bgp
->explicit_eors
= 0;
598 quagga_timestamp(3, bgp
->update_delay_end_time
,
599 sizeof(bgp
->update_delay_end_time
));
602 * Add an end-of-initial-update marker to the main process queues so that
603 * the route advertisement timer for the peers can be started. Also set
604 * the zebra and peer update hold flags. These flags are used to achieve
605 * three stages in the update-delay post processing:
606 * 1. Finish best-path selection for all the prefixes held on the queues.
607 * (routes in BGP are updated, and peers sync queues are populated too)
608 * 2. As the eoiu mark is reached in the bgp process routine, ship all the
609 * routes to zebra. With that zebra should see updates from BGP close
611 * 3. Unblock the peer update writes. With that peer update packing with
612 * the prefixes should be at its maximum.
614 bgp_add_eoiu_mark(bgp
);
615 bgp
->main_zebra_update_hold
= 1;
616 bgp
->main_peers_update_hold
= 1;
618 /* Resume the queue processing. This should trigger the event that would take
619 care of processing any work that was queued during the read-only mode. */
620 work_queue_unplug(bm
->process_main_queue
);
627 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
))
643 if (peer
->status
!= Established
)
645 BGP_TIMER_OFF(peer
->t_routeadv
);
646 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
654 bgp_adjust_routeadv (struct peer
*peer
)
656 time_t nowtime
= bgp_clock();
658 unsigned long remain
;
660 /* Bypass checks for special case of MRAI being 0 */
661 if (peer
->v_routeadv
== 0)
663 /* Stop existing timer, just in case it is running for a different
664 * duration and schedule write thread immediately.
666 if (peer
->t_routeadv
)
667 BGP_TIMER_OFF(peer
->t_routeadv
);
669 peer
->synctime
= bgp_clock ();
670 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
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_write
);
692 if (diff
> (double) peer
->v_routeadv
)
694 BGP_TIMER_OFF(peer
->t_routeadv
);
695 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
701 * - Find when to expire the MRAI timer.
702 * If MRAI timer is not active, assume we can start it now.
704 * <------- MRAI --------->
705 * |------------|-----------------------|
706 * <-------- m ----------><----- r ----->
715 if (peer
->t_routeadv
)
716 remain
= thread_timer_remain_second(peer
->t_routeadv
);
718 remain
= peer
->v_routeadv
;
719 diff
= peer
->v_routeadv
- diff
;
720 if (diff
<= (double) remain
)
722 BGP_TIMER_OFF(peer
->t_routeadv
);
723 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
728 bgp_maxmed_onstartup_applicable (struct bgp
*bgp
)
730 if (!bgp
->maxmed_onstartup_over
)
737 bgp_maxmed_onstartup_configured (struct bgp
*bgp
)
739 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
746 bgp_maxmed_onstartup_active (struct bgp
*bgp
)
748 if (bgp
->t_maxmed_onstartup
)
755 bgp_maxmed_update (struct bgp
*bgp
)
757 u_char maxmed_active
;
758 u_int32_t maxmed_value
;
760 if (bgp
->v_maxmed_admin
)
763 maxmed_value
= bgp
->maxmed_admin_value
;
765 else if (bgp
->t_maxmed_onstartup
)
768 maxmed_value
= bgp
->maxmed_onstartup_value
;
773 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
776 if (bgp
->maxmed_active
!= maxmed_active
||
777 bgp
->maxmed_value
!= maxmed_value
)
779 bgp
->maxmed_active
= maxmed_active
;
780 bgp
->maxmed_value
= maxmed_value
;
782 update_group_announce(bgp
);
786 /* The maxmed onstartup timer expiry callback. */
788 bgp_maxmed_onstartup_timer (struct thread
*thread
)
792 zlog_info ("Max med on startup ended - timer expired.");
794 bgp
= THREAD_ARG (thread
);
795 THREAD_TIMER_OFF (bgp
->t_maxmed_onstartup
);
796 bgp
->maxmed_onstartup_over
= 1;
798 bgp_maxmed_update(bgp
);
804 bgp_maxmed_onstartup_begin (struct bgp
*bgp
)
806 /* Applicable only once in the process lifetime on the startup */
807 if (bgp
->maxmed_onstartup_over
)
810 zlog_info ("Begin maxmed onstartup mode - timer %d seconds",
811 bgp
->v_maxmed_onstartup
);
813 THREAD_TIMER_ON (bm
->master
, bgp
->t_maxmed_onstartup
,
814 bgp_maxmed_onstartup_timer
,
815 bgp
, bgp
->v_maxmed_onstartup
);
817 if (!bgp
->v_maxmed_admin
)
819 bgp
->maxmed_active
= 1;
820 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
823 /* Route announce to all peers should happen after this in bgp_establish() */
827 bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
829 if (peer
->status
== Established
&& !peer
->bgp
->established
)
831 bgp_maxmed_onstartup_begin(peer
->bgp
);
835 /* The update delay timer expiry callback. */
837 bgp_update_delay_timer (struct thread
*thread
)
841 zlog_info ("Update delay ended - timer expired.");
843 bgp
= THREAD_ARG (thread
);
844 THREAD_TIMER_OFF (bgp
->t_update_delay
);
845 bgp_update_delay_end(bgp
);
850 /* The establish wait timer expiry callback. */
852 bgp_establish_wait_timer (struct thread
*thread
)
856 zlog_info ("Establish wait - timer expired.");
858 bgp
= THREAD_ARG (thread
);
859 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
860 bgp_check_update_delay(bgp
);
865 /* Steps to begin the update delay:
866 - initialize queues if needed
867 - stop the queue processing
870 bgp_update_delay_begin (struct bgp
*bgp
)
872 struct listnode
*node
, *nnode
;
875 /* Stop the processing of queued work. Enqueue shall continue */
876 work_queue_plug(bm
->process_main_queue
);
878 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
879 peer
->update_delay_over
= 0;
881 /* Start the update-delay timer */
882 THREAD_TIMER_ON (bm
->master
, bgp
->t_update_delay
, bgp_update_delay_timer
,
883 bgp
, bgp
->v_update_delay
);
885 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
886 THREAD_TIMER_ON (bm
->master
, bgp
->t_establish_wait
, bgp_establish_wait_timer
,
887 bgp
, bgp
->v_establish_wait
);
889 quagga_timestamp(3, bgp
->update_delay_begin_time
,
890 sizeof(bgp
->update_delay_begin_time
));
894 bgp_update_delay_process_status_change(struct peer
*peer
)
896 if (peer
->status
== Established
)
898 if (!peer
->bgp
->established
++)
900 bgp_update_delay_begin(peer
->bgp
);
901 zlog_info ("Begin read-only mode - update-delay timer %d seconds",
902 peer
->bgp
->v_update_delay
);
904 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
905 bgp_update_restarted_peers(peer
);
907 if (peer
->ostatus
== Established
&& bgp_update_delay_active(peer
->bgp
))
909 /* Adjust the update-delay state to account for this flap.
910 NOTE: Intentionally skipping adjusting implicit_eors or explicit_eors
911 counters. Extra sanity check in bgp_check_update_delay() should
912 be enough to take care of any additive discrepancy in bgp eor
914 peer
->bgp
->established
--;
915 peer
->update_delay_over
= 0;
919 /* Called after event occured, this function change status and reset
920 read/write and timer thread. */
922 bgp_fsm_change_status (struct peer
*peer
, int status
)
925 bgp_dump_state (peer
, peer
->status
, status
);
927 /* Transition into Clearing or Deleted must /always/ clear all routes..
928 * (and must do so before actually changing into Deleted..
930 if (status
>= Clearing
)
932 bgp_clear_route_all (peer
);
934 /* If no route was queued for the clear-node processing, generate the
935 * completion event here. This is needed because if there are no routes
936 * to trigger the background clear-node thread, the event won't get
937 * generated and the peer would be stuck in Clearing. Note that this
938 * event is for the peer and helps the peer transition out of Clearing
939 * state; it should not be generated per (AFI,SAFI). The event is
940 * directly posted here without calling clear_node_complete() as we
941 * shouldn't do an extra unlock. This event will get processed after
942 * the state change that happens below, so peer will be in Clearing
945 if (!work_queue_is_scheduled (peer
->clear_node_queue
))
946 BGP_EVENT_ADD (peer
, Clearing_Completed
);
949 /* Preserve old status and change into new status. */
950 peer
->ostatus
= peer
->status
;
951 peer
->status
= status
;
953 /* Save event that caused status change. */
954 peer
->last_major_event
= peer
->cur_event
;
956 if (status
== Established
)
957 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
959 /* If max-med processing is applicable, do the necessary. */
960 if (status
== Established
)
962 if (bgp_maxmed_onstartup_configured(peer
->bgp
) &&
963 bgp_maxmed_onstartup_applicable(peer
->bgp
))
964 bgp_maxmed_onstartup_process_status_change(peer
);
966 peer
->bgp
->maxmed_onstartup_over
= 1;
969 /* If update-delay processing is applicable, do the necessary. */
970 if (bgp_update_delay_configured(peer
->bgp
) &&
971 bgp_update_delay_applicable(peer
->bgp
))
972 bgp_update_delay_process_status_change(peer
);
974 if (bgp_debug_neighbor_events(peer
))
975 zlog_debug ("%s went from %s to %s",
977 LOOKUP (bgp_status_msg
, peer
->ostatus
),
978 LOOKUP (bgp_status_msg
, peer
->status
));
981 /* Flush the event queue and ensure the peer is shut down */
983 bgp_clearing_completed (struct peer
*peer
)
985 int rc
= bgp_stop(peer
);
988 BGP_EVENT_FLUSH (peer
);
993 /* Administrative BGP peer stop event. */
994 /* May be called multiple times for the same peer */
996 bgp_stop (struct peer
*peer
)
1000 char orf_name
[BUFSIZ
];
1003 if (peer_dynamic_neighbor(peer
) &&
1004 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1006 if (bgp_debug_neighbor_events(peer
))
1007 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1012 /* Can't do this in Clearing; events are used for state transitions */
1013 if (peer
->status
!= Clearing
)
1015 /* Delete all existing events of the peer */
1016 BGP_EVENT_FLUSH (peer
);
1019 /* Increment Dropped count. */
1020 if (peer
->status
== Established
)
1024 /* bgp log-neighbor-changes of neighbor Down */
1025 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1026 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer
->host
,
1027 peer_down_str
[(int) peer
->last_reset
]);
1029 /* graceful restart */
1030 if (peer
->t_gr_stale
)
1032 BGP_TIMER_OFF (peer
->t_gr_stale
);
1033 if (bgp_debug_neighbor_events(peer
))
1034 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1036 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
1038 if (bgp_debug_neighbor_events(peer
))
1040 zlog_debug ("%s graceful restart timer started for %d sec",
1041 peer
->host
, peer
->v_gr_restart
);
1042 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
1043 peer
->host
, peer
->bgp
->stalepath_time
);
1045 BGP_TIMER_ON (peer
->t_gr_restart
, bgp_graceful_restart_timer_expire
,
1046 peer
->v_gr_restart
);
1047 BGP_TIMER_ON (peer
->t_gr_stale
, bgp_graceful_stale_timer_expire
,
1048 peer
->bgp
->stalepath_time
);
1052 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1054 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1055 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
1056 peer
->nsf
[afi
][safi
] = 0;
1059 /* set last reset time */
1060 peer
->resettime
= peer
->uptime
= bgp_clock ();
1062 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1063 zlog_debug ("%s remove from all update group", peer
->host
);
1064 update_group_remove_peer_afs(peer
);
1067 bgpTrapBackwardTransition (peer
);
1068 #endif /* HAVE_SNMP */
1070 /* Reset peer synctime */
1073 bgp_bfd_deregister_peer(peer
);
1076 /* Stop read and write threads when exists. */
1077 BGP_READ_OFF (peer
->t_read
);
1078 BGP_WRITE_OFF (peer
->t_write
);
1080 /* Stop all timers. */
1081 BGP_TIMER_OFF (peer
->t_start
);
1082 BGP_TIMER_OFF (peer
->t_connect
);
1083 BGP_TIMER_OFF (peer
->t_holdtime
);
1084 BGP_TIMER_OFF (peer
->t_keepalive
);
1085 BGP_TIMER_OFF (peer
->t_routeadv
);
1088 peer
->packet_size
= 0;
1090 /* Clear input and output buffer. */
1092 stream_reset (peer
->ibuf
);
1094 stream_reset (peer
->work
);
1096 stream_fifo_clean (peer
->obuf
);
1098 /* Close of file descriptor. */
1105 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1106 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1108 /* Reset all negotiated variables */
1109 peer
->afc_nego
[afi
][safi
] = 0;
1110 peer
->afc_adv
[afi
][safi
] = 0;
1111 peer
->afc_recv
[afi
][safi
] = 0;
1113 /* peer address family capability flags*/
1114 peer
->af_cap
[afi
][safi
] = 0;
1116 /* peer address family status flags*/
1117 peer
->af_sflags
[afi
][safi
] = 0;
1119 /* Received ORF prefix-filter */
1120 peer
->orf_plist
[afi
][safi
] = NULL
;
1122 if ((peer
->status
== OpenConfirm
) || (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 (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1132 peer
->v_keepalive
= peer
->keepalive
;
1133 peer
->v_holdtime
= peer
->holdtime
;
1137 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1138 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1141 peer
->update_time
= 0;
1143 /* Until we are sure that there is no problem about prefix count
1144 this should be commented out.*/
1146 /* Reset prefix count */
1147 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1148 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1149 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1150 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1151 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1154 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
) &&
1155 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1162 bgp_peer_conf_if_to_su_update(peer
);
1168 /* BGP peer is stoped by the error. */
1170 bgp_stop_with_error (struct peer
*peer
)
1172 /* Double start timer. */
1175 /* Overflow check. */
1176 if (peer
->v_start
>= (60 * 2))
1177 peer
->v_start
= (60 * 2);
1179 if (peer_dynamic_neighbor(peer
))
1181 if (bgp_debug_neighbor_events(peer
))
1182 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1187 return(bgp_stop (peer
));
1191 /* something went wrong, send notify and tear down */
1193 bgp_stop_with_notify (struct peer
*peer
, u_char code
, u_char sub_code
)
1195 /* Send notify to remote peer */
1196 bgp_notify_send (peer
, code
, sub_code
);
1198 if (peer_dynamic_neighbor(peer
))
1200 if (bgp_debug_neighbor_events(peer
))
1201 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1206 /* Clear start timer value to default. */
1207 peer
->v_start
= BGP_INIT_START_TIMER
;
1209 return(bgp_stop(peer
));
1213 /* TCP connection open. Next we send open message to remote peer. And
1214 add read thread for reading open message. */
1216 bgp_connect_success (struct peer
*peer
)
1220 zlog_err ("bgp_connect_success peer's fd is negative value %d",
1226 if (bgp_getsockname (peer
) < 0)
1228 zlog_err ("%s: bgp_getsockname(): failed for peer %s, fd %d", __FUNCTION__
,
1229 peer
->host
, peer
->fd
);
1230 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0); /* internal error */
1234 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1236 if (bgp_debug_neighbor_events(peer
))
1238 char buf1
[SU_ADDRSTRLEN
];
1240 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1241 zlog_debug ("%s open active, local address %s", peer
->host
,
1242 sockunion2str (peer
->su_local
, buf1
, SU_ADDRSTRLEN
));
1244 zlog_debug ("%s passive open", peer
->host
);
1247 bgp_open_send (peer
);
1252 /* TCP connect fail */
1254 bgp_connect_fail (struct peer
*peer
)
1256 if (peer_dynamic_neighbor(peer
))
1258 if (bgp_debug_neighbor_events(peer
))
1259 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1264 return (bgp_stop (peer
));
1267 /* This function is the first starting point of all BGP connection. It
1268 try to connect to remote peer with non-blocking IO. */
1270 bgp_start (struct peer
*peer
)
1275 bgp_peer_conf_if_to_su_update(peer
);
1277 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1279 if (bgp_debug_neighbor_events(peer
))
1280 zlog_debug ("%s [FSM] Unable to get neighbor's IP address, waiting...",
1285 if (BGP_PEER_START_SUPPRESSED (peer
))
1287 if (bgp_debug_neighbor_events(peer
))
1288 zlog_err ("%s [FSM] Trying to start suppressed peer"
1289 " - this is never supposed to happen!", peer
->host
);
1293 /* Scrub some information that might be left over from a previous,
1296 /* Connection information. */
1299 sockunion_free (peer
->su_local
);
1300 peer
->su_local
= NULL
;
1303 if (peer
->su_remote
)
1305 sockunion_free (peer
->su_remote
);
1306 peer
->su_remote
= NULL
;
1309 /* Clear remote router-id. */
1310 peer
->remote_id
.s_addr
= 0;
1312 /* Clear peer capability flag. */
1315 /* If the peer is passive mode, force to move to Active mode. */
1316 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
))
1318 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1322 /* Register to be notified on peer up */
1323 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1 &&
1324 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1325 && ! bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1330 if (!bgp_find_or_add_nexthop(peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
1331 NULL
, peer
, connected
))
1333 if (bgp_debug_neighbor_events(peer
))
1334 zlog_debug ("%s [FSM] Waiting for NHT", peer
->host
);
1336 #if !defined (HAVE_BGP_STANDALONE)
1337 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1342 status
= bgp_connect (peer
);
1347 if (bgp_debug_neighbor_events(peer
))
1348 zlog_debug ("%s [FSM] Connect error", peer
->host
);
1349 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1351 case connect_success
:
1352 if (bgp_debug_neighbor_events(peer
))
1353 zlog_debug ("%s [FSM] Connect immediately success, fd %d",
1354 peer
->host
, peer
->fd
);
1355 BGP_EVENT_ADD (peer
, TCP_connection_open
);
1357 case connect_in_progress
:
1358 /* To check nonblocking connect, we wait until socket is
1359 readable or writable. */
1360 if (bgp_debug_neighbor_events(peer
))
1361 zlog_debug ("%s [FSM] Non blocking connect waiting result, fd %d",
1362 peer
->host
, peer
->fd
);
1365 zlog_err ("bgp_start peer's fd is negative value %d",
1369 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1370 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1376 /* Connect retry timer is expired when the peer status is Connect. */
1378 bgp_reconnect (struct peer
*peer
)
1380 if (bgp_stop (peer
) < 0)
1388 bgp_fsm_open (struct peer
*peer
)
1390 /* Send keepalive and make keepalive timer */
1391 bgp_keepalive_send (peer
);
1393 /* Reset holdtimer value. */
1394 BGP_TIMER_OFF (peer
->t_holdtime
);
1399 /* Keepalive send to peer. */
1401 bgp_fsm_keepalive_expire (struct peer
*peer
)
1403 bgp_keepalive_send (peer
);
1407 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1408 peer and change to Idle status. */
1410 bgp_fsm_event_error (struct peer
*peer
)
1412 zlog_err ("%s [FSM] unexpected packet received in state %s",
1413 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1415 return bgp_stop_with_notify (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1418 /* Hold timer expire. This is error of BGP connection. So cut the
1419 peer and change to Idle status. */
1421 bgp_fsm_holdtime_expire (struct peer
*peer
)
1423 if (bgp_debug_neighbor_events(peer
))
1424 zlog_debug ("%s [FSM] Hold timer expire", peer
->host
);
1426 return bgp_stop_with_notify (peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1429 /* Status goes to Established. Send keepalive packet then make first
1430 update information. */
1432 bgp_establish (struct peer
*peer
)
1436 int nsf_af_count
= 0;
1440 other
= peer
->doppelganger
;
1441 peer
= peer_xfer_conn(peer
);
1444 zlog_err ("%%Neighbor failed in xfer_conn");
1449 ret
= 1; /* bgp_establish specific code when xfer_conn happens. */
1451 /* Reset capability open status flag. */
1452 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1453 SET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1455 /* Clear start timer value to default. */
1456 peer
->v_start
= BGP_INIT_START_TIMER
;
1458 /* Increment established count. */
1459 peer
->established
++;
1460 bgp_fsm_change_status (peer
, Established
);
1462 /* bgp log-neighbor-changes of neighbor Up */
1463 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1464 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer
->host
);
1466 /* assign update-group/subgroup */
1467 update_group_adjust_peer_afs(peer
);
1469 /* graceful restart */
1470 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1471 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1472 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
1474 if (peer
->afc_nego
[afi
][safi
]
1475 && CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_ADV
)
1476 && CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_RCV
))
1478 if (peer
->nsf
[afi
][safi
]
1479 && ! CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1480 bgp_clear_stale_route (peer
, afi
, safi
);
1482 peer
->nsf
[afi
][safi
] = 1;
1487 if (peer
->nsf
[afi
][safi
])
1488 bgp_clear_stale_route (peer
, afi
, safi
);
1489 peer
->nsf
[afi
][safi
] = 0;
1494 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1497 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1498 if (peer
->t_gr_stale
)
1500 BGP_TIMER_OFF (peer
->t_gr_stale
);
1501 if (bgp_debug_neighbor_events(peer
))
1502 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1506 if (peer
->t_gr_restart
)
1508 BGP_TIMER_OFF (peer
->t_gr_restart
);
1509 if (bgp_debug_neighbor_events(peer
))
1510 zlog_debug ("%s graceful restart timer stopped", peer
->host
);
1514 bgpTrapEstablished (peer
);
1515 #endif /* HAVE_SNMP */
1517 /* Reset uptime, send keepalive, send current table. */
1518 peer
->uptime
= bgp_clock ();
1520 /* Send route-refresh when ORF is enabled */
1521 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1522 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1523 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
))
1525 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_RCV
))
1526 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX
,
1527 REFRESH_IMMEDIATE
, 0);
1528 else if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1529 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
1530 REFRESH_IMMEDIATE
, 0);
1533 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1534 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1535 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1536 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
))
1537 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
1538 || CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1539 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1541 bgp_announce_peer (peer
);
1543 /* Start the route advertisement timer to send updates to the peer - if BGP
1544 * is not in read-only mode. If it is, the timer will be started at the end
1545 * of read-only mode.
1547 if (!bgp_update_delay_active(peer
->bgp
))
1549 BGP_TIMER_OFF(peer
->t_routeadv
);
1550 BGP_TIMER_ON (peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1553 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
))
1555 if (bgp_debug_neighbor_events(peer
))
1556 zlog_debug("[Event] Deleting stub connection for peer %s", peer
->host
);
1558 if (peer
->doppelganger
->status
> Active
)
1559 bgp_notify_send (peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1560 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1562 peer_delete(peer
->doppelganger
);
1565 bgp_bfd_register_peer(peer
);
1569 /* Keepalive packet is received. */
1571 bgp_fsm_keepalive (struct peer
*peer
)
1573 bgp_update_implicit_eors(peer
);
1575 /* peer count update */
1576 peer
->keepalive_in
++;
1578 BGP_TIMER_OFF (peer
->t_holdtime
);
1582 /* Update packet is received. */
1584 bgp_fsm_update (struct peer
*peer
)
1586 BGP_TIMER_OFF (peer
->t_holdtime
);
1590 /* This is empty event. */
1592 bgp_ignore (struct peer
*peer
)
1594 zlog_err ("%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1595 peer
->host
, bgp_event_str
[peer
->cur_event
],
1596 LOOKUP (bgp_status_msg
, peer
->status
),
1597 bgp_event_str
[peer
->last_event
],
1598 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1602 /* This is to handle unexpected events.. */
1604 bgp_fsm_exeption (struct peer
*peer
)
1606 zlog_err ("%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1607 peer
->host
, bgp_event_str
[peer
->cur_event
],
1608 LOOKUP (bgp_status_msg
, peer
->status
),
1609 bgp_event_str
[peer
->last_event
],
1610 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1611 return(bgp_stop (peer
));
1615 bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1620 switch (peer
->status
)
1624 BGP_EVENT_ADD(peer
, BGP_Start
);
1629 BGP_TIMER_OFF(peer
->t_connect
);
1630 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1636 BGP_TIMER_OFF(peer
->t_connect
);
1637 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1643 if (!valid
&& (peer
->gtsm_hops
== 1))
1644 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1653 /* Finite State Machine structure */
1654 static const struct {
1655 int (*func
) (struct peer
*);
1657 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] =
1660 /* Idle state: In Idle state, all events other than BGP_Start is
1661 ignored. With BGP_Start event, finite state machine calls
1663 {bgp_start
, Connect
}, /* BGP_Start */
1664 {bgp_stop
, Idle
}, /* BGP_Stop */
1665 {bgp_stop
, Idle
}, /* TCP_connection_open */
1666 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1667 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1668 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1669 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1670 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1671 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1672 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1673 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1674 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1675 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1676 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1680 {bgp_ignore
, Connect
}, /* BGP_Start */
1681 {bgp_stop
, Idle
}, /* BGP_Stop */
1682 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1683 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1684 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1685 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1686 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1687 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1688 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1689 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1690 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1691 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1692 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1693 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1697 {bgp_ignore
, Active
}, /* BGP_Start */
1698 {bgp_stop
, Idle
}, /* BGP_Stop */
1699 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1700 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1701 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1702 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1703 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1704 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1705 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1706 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1707 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1708 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1709 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1710 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1714 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1715 {bgp_stop
, Idle
}, /* BGP_Stop */
1716 {bgp_stop
, Active
}, /* TCP_connection_open */
1717 {bgp_stop
, Active
}, /* TCP_connection_closed */
1718 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1719 {bgp_stop
, Active
}, /* TCP_fatal_error */
1720 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1721 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1722 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1723 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1724 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1725 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1726 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1727 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1731 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1732 {bgp_stop
, Idle
}, /* BGP_Stop */
1733 {bgp_stop
, Idle
}, /* TCP_connection_open */
1734 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1735 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1736 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1737 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1738 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1739 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1740 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1741 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1742 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1743 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1744 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1748 {bgp_ignore
, Established
}, /* BGP_Start */
1749 {bgp_stop
, Clearing
}, /* BGP_Stop */
1750 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1751 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1752 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1753 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1754 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1755 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1756 {bgp_fsm_keepalive_expire
, Established
}, /* KeepAlive_timer_expired */
1757 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1758 {bgp_fsm_keepalive
, Established
}, /* Receive_KEEPALIVE_message */
1759 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1760 {bgp_stop_with_error
, Clearing
}, /* Receive_NOTIFICATION_message */
1761 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1765 {bgp_ignore
, Clearing
}, /* BGP_Start */
1766 {bgp_stop
, Clearing
}, /* BGP_Stop */
1767 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1768 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1769 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1770 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1771 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1772 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1773 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1774 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1775 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1776 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1777 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1778 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1782 {bgp_ignore
, Deleted
}, /* BGP_Start */
1783 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1784 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1785 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1786 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1787 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1788 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1789 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1790 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1791 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1792 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1793 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1794 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1795 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1799 /* Execute event process. */
1801 bgp_event (struct thread
*thread
)
1807 peer
= THREAD_ARG (thread
);
1808 event
= THREAD_VAL (thread
);
1810 ret
= bgp_event_update(peer
, event
);
1816 bgp_event_update (struct peer
*peer
, int event
)
1821 int passive_conn
= 0;
1824 other
= peer
->doppelganger
;
1825 passive_conn
= (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1826 dyn_nbr
= peer_dynamic_neighbor(peer
);
1828 /* Logging this event. */
1829 next
= FSM
[peer
->status
-1][event
- 1].next_state
;
1831 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1832 zlog_debug ("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1833 bgp_event_str
[event
],
1834 LOOKUP (bgp_status_msg
, peer
->status
),
1835 LOOKUP (bgp_status_msg
, next
), peer
->fd
);
1837 peer
->last_event
= peer
->cur_event
;
1838 peer
->cur_event
= event
;
1840 /* Call function. */
1841 if (FSM
[peer
->status
-1][event
- 1].func
)
1842 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1844 /* When function do not want proceed next job return -1. */
1847 if (ret
== 1 && next
== Established
)
1849 /* The case when doppelganger swap accurred in bgp_establish.
1850 Update the peer pointer accordingly */
1854 /* If status is changed. */
1855 if (next
!= peer
->status
)
1856 bgp_fsm_change_status (peer
, next
);
1858 /* Make sure timer is set. */
1859 bgp_timer_set (peer
);
1862 else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
)
1864 /* If we got a return value of -1, that means there was an error, restart
1865 * the FSM. If the peer structure was deleted
1867 zlog_err ("%s [FSM] Failure handling event %s in state %s, "
1868 "prior events %s, %s, fd %d",
1869 peer
->host
, bgp_event_str
[peer
->cur_event
],
1870 LOOKUP (bgp_status_msg
, peer
->status
),
1871 bgp_event_str
[peer
->last_event
],
1872 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1874 bgp_fsm_change_status(peer
, Idle
);
1875 bgp_timer_set(peer
);