[bgpd] Fix 0.99 shutdown regression, introduce Clearing and Deleted states

2006-09-14 Paul Jakma <paul.jakma@sun.com>

	* (general) Fix some niggly issues around 'shutdown' and clearing
	  by adding a Clearing FSM wait-state and a hidden 'Deleted'
	  FSM state, to allow deleted peers to 'cool off' and hit 0
	  references. This introduces a slow memory leak of struct peer,
	  however that's more a testament to the fragility of the
	  reference counting than a bug in this patch, cleanup of
	  reference counting to fix this is to follow.
	* bgpd.h: Add Clearing, Deleted states and Clearing_Completed
	  and event.
	* bgp_debug.c: (bgp_status_msg[]) Add strings for Clearing and
	  Deleted.
	* bgp_fsm.h: Don't allow timer/event threads to set anything
	  for Deleted peers.
	* bgp_fsm.c: (bgp_timer_set) Add Clearing and Deleted. Deleted
	  needs to stop everything.
	  (bgp_stop) Remove explicit fsm_change_status call, the
	  general framework handles the transition.
	  (bgp_start) Log a warning if a start is attempted on a peer
	  that should stay down, trying to start a peer.
	  (struct .. FSM) Add Clearing_Completed
	  events, has little influence except when in state
	  Clearing to signal wait-state can end.
	  Add Clearing and Deleted states, former is a wait-state,
	  latter is a placeholder state to allow peers to disappear
	  quietly once refcounts settle.
	  (bgp_event) Try reduce verbosity of FSM state-change debug,
	  changes to same state are not interesting (Established->Established)
	  Allow NULL action functions in FSM.
	* bgp_packet.c: (bgp_write) Use FSM events, rather than trying
	  to twiddle directly with FSM state behind the back of FSM.
	  (bgp_write_notify) ditto.
	  (bgp_read) Remove the vague ACCEPT_PEER peer_unlock, or else
	  this patch crashes, now it leaks instead.
	* bgp_route.c: (bgp_clear_node_complete) Clearing_Completed
	  event, to end clearing.
	  (bgp_clear_route) See extensive comments.
	* bgpd.c: (peer_free) should only be called while in Deleted,
	  peer refcounting controls when peer_free is called.
	  bgp_sync_delete should be here, not in peer_delete.
	  (peer_delete) Initiate delete.
	  Transition to Deleted state manually.
	  When removing peer from indices that provide visibility of it,
	  take great care to be idempotent wrt the reference counting
	  of struct peer through those indices.
	  Use bgp_timer_set, rather than replicating.
	  Call to bgp_sync_delete isn't appropriate here, sync can be
	  referenced while shutting down and finishing deletion.
	  (peer_group_bind) Take care to be idempotent wrt list references
	  indexing peers.
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index b8ae30a..8b180a4 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -386,7 +386,6 @@
 #define PEER_STATUS_GROUP             (1 << 4) /* peer-group conf */
 #define PEER_STATUS_NSF_MODE          (1 << 5) /* NSF aware peer */
 #define PEER_STATUS_NSF_WAIT          (1 << 6) /* wait comeback peer */
-#define PEER_STATUS_CLEARING          (1 << 7) /* peers table being cleared */
 
   /* Peer status af flags (reset in bgp_stop) */
   u_int16_t af_sflags[AFI_MAX][SAFI_MAX];
@@ -662,7 +661,9 @@
 #define OpenSent                                 4
 #define OpenConfirm                              5
 #define Established                              6
-#define BGP_STATUS_MAX                           7
+#define Clearing                                 7
+#define Deleted                                  8
+#define BGP_STATUS_MAX                           9
 
 /* BGP finite state machine events.  */
 #define BGP_Start                                1
@@ -678,7 +679,8 @@
 #define Receive_KEEPALIVE_message               11
 #define Receive_UPDATE_message                  12
 #define Receive_NOTIFICATION_message            13
-#define BGP_EVENTS_MAX                          14
+#define Clearing_Completed                      14
+#define BGP_EVENTS_MAX                          15
 
 /* BGP timers default value.  */
 #define BGP_INIT_START_TIMER                     5