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"
39 #include "bgpd/bgpd.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_fsm.h"
43 #include "bgpd/bgp_packet.h"
44 #include "bgpd/bgp_network.h"
45 #include "bgpd/bgp_route.h"
46 #include "bgpd/bgp_dump.h"
47 #include "bgpd/bgp_open.h"
48 #include "bgpd/bgp_advertise.h"
50 #include "bgpd/bgp_snmp.h"
51 #endif /* HAVE_SNMP */
52 #include "bgpd/bgp_updgrp.h"
53 #include "bgpd/bgp_nht.h"
54 #include "bgpd/bgp_bfd.h"
55 #include "bgpd/bgp_memory.h"
57 /* Definition of display strings corresponding to FSM events. This should be
58 * kept consistent with the events defined in bgpd.h
60 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
*);
91 static int bgp_keepalive_timer (struct thread
*);
93 /* BGP FSM functions. */
94 static int bgp_start (struct peer
*);
97 peer_xfer_stats (struct peer
*peer_dst
, struct peer
*peer_src
)
99 /* Copy stats over. These are only the pre-established state stats */
100 peer_dst
->open_in
+= peer_src
->open_in
;
101 peer_dst
->open_out
+= peer_src
->open_out
;
102 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
103 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
104 peer_dst
->notify_in
+= peer_src
->notify_in
;
105 peer_dst
->notify_out
+= peer_src
->notify_out
;
106 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
107 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
111 peer_xfer_conn(struct peer
*from_peer
)
118 unsigned char last_evt
, last_maj_evt
;
120 assert(from_peer
!= NULL
);
122 peer
= from_peer
->doppelganger
;
124 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
127 if (bgp_debug_neighbor_events(peer
))
128 zlog_debug ("%s: peer transfer %p fd %d -> %p fd %d)", from_peer
->host
,
129 from_peer
, from_peer
->fd
, peer
, peer
->fd
);
131 BGP_WRITE_OFF(peer
->t_write
);
132 BGP_READ_OFF(peer
->t_read
);
133 BGP_WRITE_OFF(from_peer
->t_write
);
134 BGP_READ_OFF(from_peer
->t_read
);
136 BGP_TIMER_OFF(peer
->t_routeadv
);
137 BGP_TIMER_OFF(from_peer
->t_routeadv
);
140 peer
->fd
= from_peer
->fd
;
142 stream_reset(peer
->ibuf
);
143 stream_fifo_clean(peer
->obuf
);
144 stream_fifo_clean(from_peer
->obuf
);
146 peer
->as
= from_peer
->as
;
147 peer
->v_holdtime
= from_peer
->v_holdtime
;
148 peer
->v_keepalive
= from_peer
->v_keepalive
;
149 peer
->routeadv
= from_peer
->routeadv
;
150 peer
->v_routeadv
= from_peer
->v_routeadv
;
151 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
152 peer
->cap
= from_peer
->cap
;
153 status
= peer
->status
;
154 pstatus
= peer
->ostatus
;
155 last_evt
= peer
->last_event
;
156 last_maj_evt
= peer
->last_major_event
;
157 peer
->status
= from_peer
->status
;
158 peer
->ostatus
= from_peer
->ostatus
;
159 peer
->last_event
= from_peer
->last_event
;
160 peer
->last_major_event
= from_peer
->last_major_event
;
161 from_peer
->status
= status
;
162 from_peer
->ostatus
= pstatus
;
163 from_peer
->last_event
= last_evt
;
164 from_peer
->last_major_event
= last_maj_evt
;
165 peer
->remote_id
= from_peer
->remote_id
;
167 if (from_peer
->hostname
!= NULL
)
171 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
172 peer
->hostname
= NULL
;
175 peer
->hostname
= 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
= from_peer
->domainname
;
188 from_peer
->domainname
= NULL
;
191 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
192 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
194 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
195 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
196 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
197 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
198 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
199 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
200 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
203 if (bgp_getsockname(peer
) < 0)
205 zlog_err ("%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
206 (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
207 peer
->host
, peer
->fd
, from_peer
->fd
);
212 if (from_peer
->status
> Active
)
214 if (bgp_getsockname(from_peer
) < 0)
216 zlog_err ("%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
217 (CHECK_FLAG (from_peer
->sflags
, PEER_STATUS_ACCEPT_PEER
) ? "accept" : ""),
218 from_peer
->host
, from_peer
->fd
, peer
->fd
);
224 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
225 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
228 peer_xfer_stats(peer
, from_peer
);
233 /* Hook function called after bgp event is occered. And vty's
234 neighbor command invoke this function after making neighbor
237 bgp_timer_set (struct peer
*peer
)
239 switch (peer
->status
)
242 /* First entry point of peer's finite state machine. In Idle
243 status start timer is on unless peer is shutdown or peer is
244 inactive. All other timer must be turned off */
245 if (BGP_PEER_START_SUPPRESSED (peer
) || ! peer_active (peer
))
247 BGP_TIMER_OFF (peer
->t_start
);
251 BGP_TIMER_ON (peer
->t_start
, bgp_start_timer
,
254 BGP_TIMER_OFF (peer
->t_connect
);
255 BGP_TIMER_OFF (peer
->t_holdtime
);
256 BGP_TIMER_OFF (peer
->t_keepalive
);
257 BGP_TIMER_OFF (peer
->t_routeadv
);
261 /* After start timer is expired, the peer moves to Connect
262 status. Make sure start timer is off and connect timer is
264 BGP_TIMER_OFF (peer
->t_start
);
265 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
266 BGP_TIMER_OFF (peer
->t_holdtime
);
267 BGP_TIMER_OFF (peer
->t_keepalive
);
268 BGP_TIMER_OFF (peer
->t_routeadv
);
272 /* Active is waiting connection from remote peer. And if
273 connect timer is expired, change status to Connect. */
274 BGP_TIMER_OFF (peer
->t_start
);
275 /* If peer is passive mode, do not set connect timer. */
276 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
)
277 || CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
279 BGP_TIMER_OFF (peer
->t_connect
);
283 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
285 BGP_TIMER_OFF (peer
->t_holdtime
);
286 BGP_TIMER_OFF (peer
->t_keepalive
);
287 BGP_TIMER_OFF (peer
->t_routeadv
);
291 /* OpenSent status. */
292 BGP_TIMER_OFF (peer
->t_start
);
293 BGP_TIMER_OFF (peer
->t_connect
);
294 if (peer
->v_holdtime
!= 0)
296 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
301 BGP_TIMER_OFF (peer
->t_holdtime
);
303 BGP_TIMER_OFF (peer
->t_keepalive
);
304 BGP_TIMER_OFF (peer
->t_routeadv
);
308 /* OpenConfirm status. */
309 BGP_TIMER_OFF (peer
->t_start
);
310 BGP_TIMER_OFF (peer
->t_connect
);
312 /* If the negotiated Hold Time value is zero, then the Hold Time
313 timer and KeepAlive timers are not started. */
314 if (peer
->v_holdtime
== 0)
316 BGP_TIMER_OFF (peer
->t_holdtime
);
317 BGP_TIMER_OFF (peer
->t_keepalive
);
321 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
323 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
326 BGP_TIMER_OFF (peer
->t_routeadv
);
330 /* In Established status start and connect timer is turned
332 BGP_TIMER_OFF (peer
->t_start
);
333 BGP_TIMER_OFF (peer
->t_connect
);
335 /* Same as OpenConfirm, if holdtime is zero then both holdtime
336 and keepalive must be turned off. */
337 if (peer
->v_holdtime
== 0)
339 BGP_TIMER_OFF (peer
->t_holdtime
);
340 BGP_TIMER_OFF (peer
->t_keepalive
);
344 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
346 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
351 BGP_TIMER_OFF (peer
->t_gr_restart
);
352 BGP_TIMER_OFF (peer
->t_gr_stale
);
353 BGP_TIMER_OFF (peer
->t_pmax_restart
);
355 BGP_TIMER_OFF (peer
->t_start
);
356 BGP_TIMER_OFF (peer
->t_connect
);
357 BGP_TIMER_OFF (peer
->t_holdtime
);
358 BGP_TIMER_OFF (peer
->t_keepalive
);
359 BGP_TIMER_OFF (peer
->t_routeadv
);
364 /* BGP start timer. This function set BGP_Start event to thread value
365 and process event. */
367 bgp_start_timer (struct thread
*thread
)
371 peer
= THREAD_ARG (thread
);
372 peer
->t_start
= NULL
;
374 if (bgp_debug_neighbor_events(peer
))
375 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
377 THREAD_VAL (thread
) = BGP_Start
;
378 bgp_event (thread
); /* bgp_event unlocks peer */
383 /* BGP connect retry timer. */
385 bgp_connect_timer (struct thread
*thread
)
390 peer
= THREAD_ARG (thread
);
391 peer
->t_connect
= NULL
;
393 if (bgp_debug_neighbor_events(peer
))
394 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
396 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
403 THREAD_VAL (thread
) = ConnectRetry_timer_expired
;
404 bgp_event (thread
); /* bgp_event unlocks peer */
411 /* BGP holdtime timer. */
413 bgp_holdtime_timer (struct thread
*thread
)
417 peer
= THREAD_ARG (thread
);
418 peer
->t_holdtime
= NULL
;
420 if (bgp_debug_neighbor_events(peer
))
421 zlog_debug ("%s [FSM] Timer (holdtime timer expire)", peer
->host
);
423 THREAD_VAL (thread
) = Hold_Timer_expired
;
424 bgp_event (thread
); /* bgp_event unlocks peer */
429 /* BGP keepalive fire ! */
431 bgp_keepalive_timer (struct thread
*thread
)
435 peer
= THREAD_ARG (thread
);
436 peer
->t_keepalive
= NULL
;
438 if (bgp_debug_neighbor_events(peer
))
439 zlog_debug ("%s [FSM] Timer (keepalive timer expire)", peer
->host
);
441 THREAD_VAL (thread
) = KeepAlive_timer_expired
;
442 bgp_event (thread
); /* bgp_event unlocks peer */
448 bgp_routeadv_timer (struct thread
*thread
)
452 peer
= THREAD_ARG (thread
);
453 peer
->t_routeadv
= NULL
;
455 if (bgp_debug_neighbor_events(peer
))
456 zlog_debug ("%s [FSM] Timer (routeadv timer expire)", peer
->host
);
458 peer
->synctime
= bgp_clock ();
460 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
462 /* MRAI timer will be started again when FIFO is built, no need to
468 /* BGP Peer Down Cause */
469 const char *peer_down_str
[] =
475 "Cluster ID changed",
476 "Confederation identifier changed",
477 "Confederation peer changed",
478 "RR client config change",
479 "RS client config change",
480 "Update source change",
481 "Address family activated",
484 "BGP Notification received",
485 "BGP Notification send",
486 "Peer closed the session",
488 "Peer-group add member",
489 "Peer-group delete member",
490 "Capability changed",
491 "Passive config change",
492 "Multihop config change",
493 "NSF peer closed the session",
494 "Intf peering v6only config change",
497 "Neighbor address lost"
501 bgp_graceful_restart_timer_expire (struct thread
*thread
)
507 peer
= THREAD_ARG (thread
);
508 peer
->t_gr_restart
= NULL
;
510 /* NSF delete stale route */
511 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
512 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
513 if (peer
->nsf
[afi
][safi
])
514 bgp_clear_stale_route (peer
, afi
, safi
);
516 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
517 BGP_TIMER_OFF (peer
->t_gr_stale
);
519 if (bgp_debug_neighbor_events(peer
))
521 zlog_debug ("%s graceful restart timer expired", peer
->host
);
522 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
525 bgp_timer_set (peer
);
531 bgp_graceful_stale_timer_expire (struct thread
*thread
)
537 peer
= THREAD_ARG (thread
);
538 peer
->t_gr_stale
= NULL
;
540 if (bgp_debug_neighbor_events(peer
))
541 zlog_debug ("%s graceful restart stalepath timer expired", peer
->host
);
543 /* NSF delete stale route */
544 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
545 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
546 if (peer
->nsf
[afi
][safi
])
547 bgp_clear_stale_route (peer
, afi
, safi
);
553 bgp_update_delay_applicable (struct bgp
*bgp
)
555 /* update_delay_over flag should be reset (set to 0) for any new
556 applicability of the update-delay during BGP process lifetime.
557 And it should be set after an occurence of the update-delay is over)*/
558 if (!bgp
->update_delay_over
)
565 bgp_update_delay_active (struct bgp
*bgp
)
567 if (bgp
->t_update_delay
)
574 bgp_update_delay_configured (struct bgp
*bgp
)
576 if (bgp
->v_update_delay
)
582 /* Do the post-processing needed when bgp comes out of the read-only mode
583 on ending the update delay. */
585 bgp_update_delay_end (struct bgp
*bgp
)
587 THREAD_TIMER_OFF (bgp
->t_update_delay
);
588 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
590 /* Reset update-delay related state */
591 bgp
->update_delay_over
= 1;
592 bgp
->established
= 0;
593 bgp
->restarted_peers
= 0;
594 bgp
->implicit_eors
= 0;
595 bgp
->explicit_eors
= 0;
597 quagga_timestamp(3, bgp
->update_delay_end_time
,
598 sizeof(bgp
->update_delay_end_time
));
601 * Add an end-of-initial-update marker to the main process queues so that
602 * the route advertisement timer for the peers can be started. Also set
603 * the zebra and peer update hold flags. These flags are used to achieve
604 * three stages in the update-delay post processing:
605 * 1. Finish best-path selection for all the prefixes held on the queues.
606 * (routes in BGP are updated, and peers sync queues are populated too)
607 * 2. As the eoiu mark is reached in the bgp process routine, ship all the
608 * routes to zebra. With that zebra should see updates from BGP close
610 * 3. Unblock the peer update writes. With that peer update packing with
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 take
618 care of processing any work that was queued during the read-only mode. */
619 work_queue_unplug(bm
->process_main_queue
);
626 bgp_start_routeadv (struct bgp
*bgp
)
628 struct listnode
*node
, *nnode
;
631 zlog_info("bgp_start_routeadv(), update hold status %d",
632 bgp
->main_peers_update_hold
);
634 if (bgp
->main_peers_update_hold
)
637 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
638 sizeof(bgp
->update_delay_peers_resume_time
));
640 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);
653 bgp_adjust_routeadv (struct peer
*peer
)
655 time_t nowtime
= bgp_clock();
657 unsigned long remain
;
659 /* Bypass checks for special case of MRAI being 0 */
660 if (peer
->v_routeadv
== 0)
662 /* Stop existing timer, just in case it is running for a different
663 * duration and schedule write thread immediately.
665 if (peer
->t_routeadv
)
666 BGP_TIMER_OFF(peer
->t_routeadv
);
668 peer
->synctime
= bgp_clock ();
669 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
676 * If the last update was written more than MRAI back, expire the timer
677 * instantly so that we can send the update out sooner.
679 * <------- MRAI --------->
680 * |-----------------|-----------------------|
681 * <------------- m ------------>
690 diff
= difftime(nowtime
, peer
->last_update
);
691 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
)
721 BGP_TIMER_OFF(peer
->t_routeadv
);
722 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
727 bgp_maxmed_onstartup_applicable (struct bgp
*bgp
)
729 if (!bgp
->maxmed_onstartup_over
)
736 bgp_maxmed_onstartup_configured (struct bgp
*bgp
)
738 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
745 bgp_maxmed_onstartup_active (struct bgp
*bgp
)
747 if (bgp
->t_maxmed_onstartup
)
754 bgp_maxmed_update (struct bgp
*bgp
)
756 u_char maxmed_active
;
757 u_int32_t maxmed_value
;
759 if (bgp
->v_maxmed_admin
)
762 maxmed_value
= bgp
->maxmed_admin_value
;
764 else if (bgp
->t_maxmed_onstartup
)
767 maxmed_value
= bgp
->maxmed_onstartup_value
;
772 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
775 if (bgp
->maxmed_active
!= maxmed_active
||
776 bgp
->maxmed_value
!= maxmed_value
)
778 bgp
->maxmed_active
= maxmed_active
;
779 bgp
->maxmed_value
= maxmed_value
;
781 update_group_announce(bgp
);
785 /* The maxmed onstartup timer expiry callback. */
787 bgp_maxmed_onstartup_timer (struct thread
*thread
)
791 zlog_info ("Max med on startup ended - timer expired.");
793 bgp
= THREAD_ARG (thread
);
794 THREAD_TIMER_OFF (bgp
->t_maxmed_onstartup
);
795 bgp
->maxmed_onstartup_over
= 1;
797 bgp_maxmed_update(bgp
);
803 bgp_maxmed_onstartup_begin (struct bgp
*bgp
)
805 /* Applicable only once in the process lifetime on the startup */
806 if (bgp
->maxmed_onstartup_over
)
809 zlog_info ("Begin maxmed onstartup mode - timer %d seconds",
810 bgp
->v_maxmed_onstartup
);
812 THREAD_TIMER_ON (bm
->master
, bgp
->t_maxmed_onstartup
,
813 bgp_maxmed_onstartup_timer
,
814 bgp
, bgp
->v_maxmed_onstartup
);
816 if (!bgp
->v_maxmed_admin
)
818 bgp
->maxmed_active
= 1;
819 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
822 /* Route announce to all peers should happen after this in bgp_establish() */
826 bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
828 if (peer
->status
== Established
&& !peer
->bgp
->established
)
830 bgp_maxmed_onstartup_begin(peer
->bgp
);
834 /* The update delay timer expiry callback. */
836 bgp_update_delay_timer (struct thread
*thread
)
840 zlog_info ("Update delay ended - timer expired.");
842 bgp
= THREAD_ARG (thread
);
843 THREAD_TIMER_OFF (bgp
->t_update_delay
);
844 bgp_update_delay_end(bgp
);
849 /* The establish wait timer expiry callback. */
851 bgp_establish_wait_timer (struct thread
*thread
)
855 zlog_info ("Establish wait - timer expired.");
857 bgp
= THREAD_ARG (thread
);
858 THREAD_TIMER_OFF (bgp
->t_establish_wait
);
859 bgp_check_update_delay(bgp
);
864 /* Steps to begin the update delay:
865 - initialize queues if needed
866 - stop the queue processing
869 bgp_update_delay_begin (struct bgp
*bgp
)
871 struct listnode
*node
, *nnode
;
874 /* Stop the processing of queued work. Enqueue shall continue */
875 work_queue_plug(bm
->process_main_queue
);
877 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
878 peer
->update_delay_over
= 0;
880 /* Start the update-delay timer */
881 THREAD_TIMER_ON (bm
->master
, bgp
->t_update_delay
, bgp_update_delay_timer
,
882 bgp
, bgp
->v_update_delay
);
884 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
885 THREAD_TIMER_ON (bm
->master
, bgp
->t_establish_wait
, bgp_establish_wait_timer
,
886 bgp
, bgp
->v_establish_wait
);
888 quagga_timestamp(3, bgp
->update_delay_begin_time
,
889 sizeof(bgp
->update_delay_begin_time
));
893 bgp_update_delay_process_status_change(struct peer
*peer
)
895 if (peer
->status
== Established
)
897 if (!peer
->bgp
->established
++)
899 bgp_update_delay_begin(peer
->bgp
);
900 zlog_info ("Begin read-only mode - update-delay timer %d seconds",
901 peer
->bgp
->v_update_delay
);
903 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
904 bgp_update_restarted_peers(peer
);
906 if (peer
->ostatus
== Established
&& bgp_update_delay_active(peer
->bgp
))
908 /* Adjust the update-delay state to account for this flap.
909 NOTE: Intentionally skipping adjusting implicit_eors or explicit_eors
910 counters. Extra sanity check in bgp_check_update_delay() should
911 be enough to take care of any additive discrepancy in bgp eor
913 peer
->bgp
->established
--;
914 peer
->update_delay_over
= 0;
918 /* Called after event occured, this function change status and reset
919 read/write and timer thread. */
921 bgp_fsm_change_status (struct peer
*peer
, int status
)
924 bgp_dump_state (peer
, peer
->status
, status
);
926 /* Transition into Clearing or Deleted must /always/ clear all routes..
927 * (and must do so before actually changing into Deleted..
929 if (status
>= Clearing
)
931 bgp_clear_route_all (peer
);
933 /* If no route was queued for the clear-node processing, generate the
934 * completion event here. This is needed because if there are no routes
935 * to trigger the background clear-node thread, the event won't get
936 * generated and the peer would be stuck in Clearing. Note that this
937 * event is for the peer and helps the peer transition out of Clearing
938 * state; it should not be generated per (AFI,SAFI). The event is
939 * directly posted here without calling clear_node_complete() as we
940 * shouldn't do an extra unlock. This event will get processed after
941 * the state change that happens below, so peer will be in Clearing
944 if (!work_queue_is_scheduled (peer
->clear_node_queue
))
945 BGP_EVENT_ADD (peer
, Clearing_Completed
);
948 /* Preserve old status and change into new status. */
949 peer
->ostatus
= peer
->status
;
950 peer
->status
= status
;
952 /* Save event that caused status change. */
953 peer
->last_major_event
= peer
->cur_event
;
955 if (status
== Established
)
956 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
958 /* If max-med processing is applicable, do the necessary. */
959 if (status
== Established
)
961 if (bgp_maxmed_onstartup_configured(peer
->bgp
) &&
962 bgp_maxmed_onstartup_applicable(peer
->bgp
))
963 bgp_maxmed_onstartup_process_status_change(peer
);
965 peer
->bgp
->maxmed_onstartup_over
= 1;
968 /* If update-delay processing is applicable, do the necessary. */
969 if (bgp_update_delay_configured(peer
->bgp
) &&
970 bgp_update_delay_applicable(peer
->bgp
))
971 bgp_update_delay_process_status_change(peer
);
973 if (bgp_debug_neighbor_events(peer
))
974 zlog_debug ("%s went from %s to %s",
976 LOOKUP (bgp_status_msg
, peer
->ostatus
),
977 LOOKUP (bgp_status_msg
, peer
->status
));
980 /* Flush the event queue and ensure the peer is shut down */
982 bgp_clearing_completed (struct peer
*peer
)
984 int rc
= bgp_stop(peer
);
987 BGP_EVENT_FLUSH (peer
);
992 /* Administrative BGP peer stop event. */
993 /* May be called multiple times for the same peer */
995 bgp_stop (struct peer
*peer
)
999 char orf_name
[BUFSIZ
];
1002 if (peer_dynamic_neighbor(peer
) &&
1003 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1005 if (bgp_debug_neighbor_events(peer
))
1006 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1011 /* Can't do this in Clearing; events are used for state transitions */
1012 if (peer
->status
!= Clearing
)
1014 /* Delete all existing events of the peer */
1015 BGP_EVENT_FLUSH (peer
);
1018 /* Increment Dropped count. */
1019 if (peer
->status
== Established
)
1023 /* bgp log-neighbor-changes of neighbor Down */
1024 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1025 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer
->host
,
1026 peer_down_str
[(int) peer
->last_reset
]);
1028 /* graceful restart */
1029 if (peer
->t_gr_stale
)
1031 BGP_TIMER_OFF (peer
->t_gr_stale
);
1032 if (bgp_debug_neighbor_events(peer
))
1033 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1035 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
1037 if (bgp_debug_neighbor_events(peer
))
1039 zlog_debug ("%s graceful restart timer started for %d sec",
1040 peer
->host
, peer
->v_gr_restart
);
1041 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
1042 peer
->host
, peer
->bgp
->stalepath_time
);
1044 BGP_TIMER_ON (peer
->t_gr_restart
, bgp_graceful_restart_timer_expire
,
1045 peer
->v_gr_restart
);
1046 BGP_TIMER_ON (peer
->t_gr_stale
, bgp_graceful_stale_timer_expire
,
1047 peer
->bgp
->stalepath_time
);
1051 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1053 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1054 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
1055 peer
->nsf
[afi
][safi
] = 0;
1058 /* set last reset time */
1059 peer
->resettime
= peer
->uptime
= bgp_clock ();
1061 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1062 zlog_debug ("%s remove from all update group", peer
->host
);
1063 update_group_remove_peer_afs(peer
);
1066 bgpTrapBackwardTransition (peer
);
1067 #endif /* HAVE_SNMP */
1069 /* Reset peer synctime */
1072 bgp_bfd_deregister_peer(peer
);
1075 /* Stop read and write threads when exists. */
1076 BGP_READ_OFF (peer
->t_read
);
1077 BGP_WRITE_OFF (peer
->t_write
);
1079 /* Stop all timers. */
1080 BGP_TIMER_OFF (peer
->t_start
);
1081 BGP_TIMER_OFF (peer
->t_connect
);
1082 BGP_TIMER_OFF (peer
->t_holdtime
);
1083 BGP_TIMER_OFF (peer
->t_keepalive
);
1084 BGP_TIMER_OFF (peer
->t_routeadv
);
1087 peer
->packet_size
= 0;
1089 /* Clear input and output buffer. */
1091 stream_reset (peer
->ibuf
);
1093 stream_reset (peer
->work
);
1095 stream_fifo_clean (peer
->obuf
);
1097 /* Close of file descriptor. */
1104 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1105 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; 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
) || (peer
->status
== Established
)) {
1122 /* ORF received prefix-filter pnt */
1123 sprintf (orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1124 prefix_bgp_orf_remove_all (afi
, orf_name
);
1128 /* Reset keepalive and holdtime */
1129 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1131 peer
->v_keepalive
= peer
->keepalive
;
1132 peer
->v_holdtime
= peer
->holdtime
;
1136 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1137 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1140 peer
->update_time
= 0;
1142 /* Until we are sure that there is no problem about prefix count
1143 this should be commented out.*/
1145 /* Reset prefix count */
1146 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1147 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1148 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1149 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1150 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1153 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
) &&
1154 !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
)))
1161 bgp_peer_conf_if_to_su_update(peer
);
1167 /* BGP peer is stoped by the error. */
1169 bgp_stop_with_error (struct peer
*peer
)
1171 /* Double start timer. */
1174 /* Overflow check. */
1175 if (peer
->v_start
>= (60 * 2))
1176 peer
->v_start
= (60 * 2);
1178 if (peer_dynamic_neighbor(peer
))
1180 if (bgp_debug_neighbor_events(peer
))
1181 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1186 return(bgp_stop (peer
));
1190 /* something went wrong, send notify and tear down */
1192 bgp_stop_with_notify (struct peer
*peer
, u_char code
, u_char sub_code
)
1194 /* Send notify to remote peer */
1195 bgp_notify_send (peer
, code
, sub_code
);
1197 if (peer_dynamic_neighbor(peer
))
1199 if (bgp_debug_neighbor_events(peer
))
1200 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1205 /* Clear start timer value to default. */
1206 peer
->v_start
= BGP_INIT_START_TIMER
;
1208 return(bgp_stop(peer
));
1212 /* TCP connection open. Next we send open message to remote peer. And
1213 add read thread for reading open message. */
1215 bgp_connect_success (struct peer
*peer
)
1219 zlog_err ("bgp_connect_success peer's fd is negative value %d",
1225 if (bgp_getsockname (peer
) < 0)
1227 zlog_err ("%s: bgp_getsockname(): failed for peer %s, fd %d", __FUNCTION__
,
1228 peer
->host
, peer
->fd
);
1229 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0); /* internal error */
1233 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1235 if (bgp_debug_neighbor_events(peer
))
1237 char buf1
[SU_ADDRSTRLEN
];
1239 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1240 zlog_debug ("%s open active, local address %s", peer
->host
,
1241 sockunion2str (peer
->su_local
, buf1
, SU_ADDRSTRLEN
));
1243 zlog_debug ("%s passive open", peer
->host
);
1246 bgp_open_send (peer
);
1251 /* TCP connect fail */
1253 bgp_connect_fail (struct peer
*peer
)
1255 if (peer_dynamic_neighbor(peer
))
1257 if (bgp_debug_neighbor_events(peer
))
1258 zlog_debug ("%s (dynamic neighbor) deleted", peer
->host
);
1263 return (bgp_stop (peer
));
1266 /* This function is the first starting point of all BGP connection. It
1267 try to connect to remote peer with non-blocking IO. */
1269 bgp_start (struct peer
*peer
)
1274 bgp_peer_conf_if_to_su_update(peer
);
1276 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1278 if (bgp_debug_neighbor_events(peer
))
1279 zlog_debug ("%s [FSM] Unable to get neighbor's IP address, waiting...",
1284 if (BGP_PEER_START_SUPPRESSED (peer
))
1286 if (bgp_debug_neighbor_events(peer
))
1287 zlog_err ("%s [FSM] Trying to start suppressed peer"
1288 " - this is never supposed to happen!", peer
->host
);
1292 /* Scrub some information that might be left over from a previous,
1295 /* Connection information. */
1298 sockunion_free (peer
->su_local
);
1299 peer
->su_local
= NULL
;
1302 if (peer
->su_remote
)
1304 sockunion_free (peer
->su_remote
);
1305 peer
->su_remote
= NULL
;
1308 /* Clear remote router-id. */
1309 peer
->remote_id
.s_addr
= 0;
1311 /* Clear peer capability flag. */
1314 /* If the peer is passive mode, force to move to Active mode. */
1315 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
))
1317 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1321 /* Register to be notified on peer up */
1322 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1 &&
1323 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1324 && ! bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1329 if (!bgp_find_or_add_nexthop(peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
1330 NULL
, peer
, connected
))
1332 #if defined (HAVE_CUMULUS)
1333 if (bgp_debug_neighbor_events(peer
))
1334 zlog_debug ("%s [FSM] Waiting for NHT", peer
->host
);
1336 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1341 status
= bgp_connect (peer
);
1346 if (bgp_debug_neighbor_events(peer
))
1347 zlog_debug ("%s [FSM] Connect error", peer
->host
);
1348 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
1350 case connect_success
:
1351 if (bgp_debug_neighbor_events(peer
))
1352 zlog_debug ("%s [FSM] Connect immediately success, fd %d",
1353 peer
->host
, peer
->fd
);
1354 BGP_EVENT_ADD (peer
, TCP_connection_open
);
1356 case connect_in_progress
:
1357 /* To check nonblocking connect, we wait until socket is
1358 readable or writable. */
1359 if (bgp_debug_neighbor_events(peer
))
1360 zlog_debug ("%s [FSM] Non blocking connect waiting result, fd %d",
1361 peer
->host
, peer
->fd
);
1364 zlog_err ("bgp_start peer's fd is negative value %d",
1368 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1369 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1375 /* Connect retry timer is expired when the peer status is Connect. */
1377 bgp_reconnect (struct peer
*peer
)
1379 if (bgp_stop (peer
) < 0)
1387 bgp_fsm_open (struct peer
*peer
)
1389 /* Send keepalive and make keepalive timer */
1390 bgp_keepalive_send (peer
);
1392 /* Reset holdtimer value. */
1393 BGP_TIMER_OFF (peer
->t_holdtime
);
1398 /* Keepalive send to peer. */
1400 bgp_fsm_keepalive_expire (struct peer
*peer
)
1402 bgp_keepalive_send (peer
);
1406 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1407 peer and change to Idle status. */
1409 bgp_fsm_event_error (struct peer
*peer
)
1411 zlog_err ("%s [FSM] unexpected packet received in state %s",
1412 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1414 return bgp_stop_with_notify (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1417 /* Hold timer expire. This is error of BGP connection. So cut the
1418 peer and change to Idle status. */
1420 bgp_fsm_holdtime_expire (struct peer
*peer
)
1422 if (bgp_debug_neighbor_events(peer
))
1423 zlog_debug ("%s [FSM] Hold timer expire", peer
->host
);
1425 return bgp_stop_with_notify (peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1428 /* Status goes to Established. Send keepalive packet then make first
1429 update information. */
1431 bgp_establish (struct peer
*peer
)
1435 int nsf_af_count
= 0;
1439 other
= peer
->doppelganger
;
1440 peer
= peer_xfer_conn(peer
);
1443 zlog_err ("%%Neighbor failed in xfer_conn");
1448 ret
= 1; /* bgp_establish specific code when xfer_conn happens. */
1450 /* Reset capability open status flag. */
1451 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1452 SET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1454 /* Clear start timer value to default. */
1455 peer
->v_start
= BGP_INIT_START_TIMER
;
1457 /* Increment established count. */
1458 peer
->established
++;
1459 bgp_fsm_change_status (peer
, Established
);
1461 /* bgp log-neighbor-changes of neighbor Up */
1462 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
1463 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer
->host
);
1465 /* assign update-group/subgroup */
1466 update_group_adjust_peer_afs(peer
);
1468 /* graceful restart */
1469 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1470 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1471 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_3
; safi
++)
1473 if (peer
->afc_nego
[afi
][safi
]
1474 && CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_ADV
)
1475 && CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_RCV
))
1477 if (peer
->nsf
[afi
][safi
]
1478 && ! CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1479 bgp_clear_stale_route (peer
, afi
, safi
);
1481 peer
->nsf
[afi
][safi
] = 1;
1486 if (peer
->nsf
[afi
][safi
])
1487 bgp_clear_stale_route (peer
, afi
, safi
);
1488 peer
->nsf
[afi
][safi
] = 0;
1493 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1496 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
1497 if (peer
->t_gr_stale
)
1499 BGP_TIMER_OFF (peer
->t_gr_stale
);
1500 if (bgp_debug_neighbor_events(peer
))
1501 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
1505 if (peer
->t_gr_restart
)
1507 BGP_TIMER_OFF (peer
->t_gr_restart
);
1508 if (bgp_debug_neighbor_events(peer
))
1509 zlog_debug ("%s graceful restart timer stopped", peer
->host
);
1513 bgpTrapEstablished (peer
);
1514 #endif /* HAVE_SNMP */
1516 /* Reset uptime, send keepalive, send current table. */
1517 peer
->uptime
= bgp_clock ();
1519 /* Send route-refresh when ORF is enabled */
1520 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1521 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1522 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
))
1524 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_RCV
))
1525 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX
,
1526 REFRESH_IMMEDIATE
, 0);
1527 else if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1528 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
1529 REFRESH_IMMEDIATE
, 0);
1532 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1533 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1534 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1535 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
))
1536 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
1537 || CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1538 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1540 bgp_announce_peer (peer
);
1542 /* Start the route advertisement timer to send updates to the peer - if BGP
1543 * is not in read-only mode. If it is, the timer will be started at the end
1544 * of read-only mode.
1546 if (!bgp_update_delay_active(peer
->bgp
))
1548 BGP_TIMER_OFF(peer
->t_routeadv
);
1549 BGP_TIMER_ON (peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1552 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
))
1554 if (bgp_debug_neighbor_events(peer
))
1555 zlog_debug("[Event] Deleting stub connection for peer %s", peer
->host
);
1557 if (peer
->doppelganger
->status
> Active
)
1558 bgp_notify_send (peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1559 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1561 peer_delete(peer
->doppelganger
);
1564 bgp_bfd_register_peer(peer
);
1568 /* Keepalive packet is received. */
1570 bgp_fsm_keepalive (struct peer
*peer
)
1572 bgp_update_implicit_eors(peer
);
1574 /* peer count update */
1575 peer
->keepalive_in
++;
1577 BGP_TIMER_OFF (peer
->t_holdtime
);
1581 /* Update packet is received. */
1583 bgp_fsm_update (struct peer
*peer
)
1585 BGP_TIMER_OFF (peer
->t_holdtime
);
1589 /* This is empty event. */
1591 bgp_ignore (struct peer
*peer
)
1593 zlog_err ("%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1594 peer
->host
, bgp_event_str
[peer
->cur_event
],
1595 LOOKUP (bgp_status_msg
, peer
->status
),
1596 bgp_event_str
[peer
->last_event
],
1597 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1601 /* This is to handle unexpected events.. */
1603 bgp_fsm_exeption (struct peer
*peer
)
1605 zlog_err ("%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1606 peer
->host
, bgp_event_str
[peer
->cur_event
],
1607 LOOKUP (bgp_status_msg
, peer
->status
),
1608 bgp_event_str
[peer
->last_event
],
1609 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1610 return(bgp_stop (peer
));
1614 bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1619 switch (peer
->status
)
1623 BGP_EVENT_ADD(peer
, BGP_Start
);
1628 BGP_TIMER_OFF(peer
->t_connect
);
1629 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1635 BGP_TIMER_OFF(peer
->t_connect
);
1636 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1642 if (!valid
&& (peer
->gtsm_hops
== 1))
1643 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1652 /* Finite State Machine structure */
1653 static const struct {
1654 int (*func
) (struct peer
*);
1656 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] =
1659 /* Idle state: In Idle state, all events other than BGP_Start is
1660 ignored. With BGP_Start event, finite state machine calls
1662 {bgp_start
, Connect
}, /* BGP_Start */
1663 {bgp_stop
, Idle
}, /* BGP_Stop */
1664 {bgp_stop
, Idle
}, /* TCP_connection_open */
1665 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1666 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1667 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1668 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1669 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1670 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1671 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1672 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1673 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1674 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1675 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1679 {bgp_ignore
, Connect
}, /* BGP_Start */
1680 {bgp_stop
, Idle
}, /* BGP_Stop */
1681 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1682 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1683 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1684 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1685 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1686 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1687 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1688 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1689 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1690 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1691 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1692 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1696 {bgp_ignore
, Active
}, /* BGP_Start */
1697 {bgp_stop
, Idle
}, /* BGP_Stop */
1698 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1699 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1700 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1701 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1702 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1703 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1704 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1705 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1706 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1707 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1708 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1709 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1713 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1714 {bgp_stop
, Idle
}, /* BGP_Stop */
1715 {bgp_stop
, Active
}, /* TCP_connection_open */
1716 {bgp_stop
, Active
}, /* TCP_connection_closed */
1717 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1718 {bgp_stop
, Active
}, /* TCP_fatal_error */
1719 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1720 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1721 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1722 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1723 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1724 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1725 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1726 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1730 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1731 {bgp_stop
, Idle
}, /* BGP_Stop */
1732 {bgp_stop
, Idle
}, /* TCP_connection_open */
1733 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1734 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1735 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1736 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1737 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1738 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1739 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1740 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1741 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1742 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1743 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1747 {bgp_ignore
, Established
}, /* BGP_Start */
1748 {bgp_stop
, Clearing
}, /* BGP_Stop */
1749 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1750 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1751 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1752 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1753 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1754 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1755 {bgp_fsm_keepalive_expire
, Established
}, /* KeepAlive_timer_expired */
1756 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1757 {bgp_fsm_keepalive
, Established
}, /* Receive_KEEPALIVE_message */
1758 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1759 {bgp_stop_with_error
, Clearing
}, /* Receive_NOTIFICATION_message */
1760 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1764 {bgp_ignore
, Clearing
}, /* BGP_Start */
1765 {bgp_stop
, Clearing
}, /* BGP_Stop */
1766 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1767 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1768 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1769 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1770 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1771 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1772 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1773 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1774 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1775 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1776 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1777 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1781 {bgp_ignore
, Deleted
}, /* BGP_Start */
1782 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1783 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1784 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1785 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1786 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1787 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1788 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1789 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1790 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1791 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1792 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1793 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1794 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1798 /* Execute event process. */
1800 bgp_event (struct thread
*thread
)
1806 peer
= THREAD_ARG (thread
);
1807 event
= THREAD_VAL (thread
);
1809 ret
= bgp_event_update(peer
, event
);
1815 bgp_event_update (struct peer
*peer
, int event
)
1820 int passive_conn
= 0;
1823 other
= peer
->doppelganger
;
1824 passive_conn
= (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1825 dyn_nbr
= peer_dynamic_neighbor(peer
);
1827 /* Logging this event. */
1828 next
= FSM
[peer
->status
-1][event
- 1].next_state
;
1830 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1831 zlog_debug ("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1832 bgp_event_str
[event
],
1833 LOOKUP (bgp_status_msg
, peer
->status
),
1834 LOOKUP (bgp_status_msg
, next
), peer
->fd
);
1836 peer
->last_event
= peer
->cur_event
;
1837 peer
->cur_event
= event
;
1839 /* Call function. */
1840 if (FSM
[peer
->status
-1][event
- 1].func
)
1841 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1843 /* When function do not want proceed next job return -1. */
1846 if (ret
== 1 && next
== Established
)
1848 /* The case when doppelganger swap accurred in bgp_establish.
1849 Update the peer pointer accordingly */
1853 /* If status is changed. */
1854 if (next
!= peer
->status
)
1855 bgp_fsm_change_status (peer
, next
);
1857 /* Make sure timer is set. */
1858 bgp_timer_set (peer
);
1861 else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
)
1863 /* If we got a return value of -1, that means there was an error, restart
1864 * the FSM. If the peer structure was deleted
1866 zlog_err ("%s [FSM] Failure handling event %s in state %s, "
1867 "prior events %s, %s, fd %d",
1868 peer
->host
, bgp_event_str
[peer
->cur_event
],
1869 LOOKUP (bgp_status_msg
, peer
->status
),
1870 bgp_event_str
[peer
->last_event
],
1871 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1873 bgp_fsm_change_status(peer
, Idle
);
1874 bgp_timer_set(peer
);