refactor subscr_conn and subscr_conn_fsm de-/alloc

Refactor:

1. Glue the gsm_subscriber_connection alloc to the subscr_conn_fsm.
2. Add separate AUTH_CIPH state to the FSM.
3. Use conn->use_count to trigger conn release.
4. Add separate RELEASING state to the FSM.
5. Add rate counters for each of the three Complete Layer 3 types.

Details:

1. Glue the gsm_subscriber_connection alloc to the subscr_conn_fsm.

Historically, a gsm_subscriber_connection was allocated in libbsc land, and
only upon Complete Layer 3 did libmsc add the fsm instance. After splitting
openbsc.git into a separate osmo-msc, this is no longer necessary, hence:

Closely tie gsm_subscriber_connection allocation to the subscr_conn_fsm
instance: talloc the conn as a child of the FSM instance, and discard the conn
as soon as the FSM terminates.

2. Add separate AUTH_CIPH state to the FSM.

Decoding the Complete Layer 3 message is distinctly separate from waiting for
the VLR FSMs to conclude. Use the NEW state as "we don't know if this is a
valid message yet", and the AUTH_CIPH state as "evaluating, don't release".

A profound effect of this: should we for any odd reason fail to leave the FSM's
NEW state, the conn will be released right at the end of msc_compl_l3(),
without needing to trigger release in each code path.

3. Use conn->use_count to trigger conn release.

Before, the FSM itself would hold a use count on the conn, and hence we would
need to ask it whether it is ready to release the conn yet by dispatching
events, to achieve a use_count decrement.

Instead, unite the FSM instance and conn, and do not hold a use count by the
FSM. Hence, trigger an FSM "UNUSED" event only when the use_count reaches zero.
As long as use counts are done correctly, the FSM will terminate correctly.

These exceptions:

- The new AUTH_CIPH state explicitly ignores UNUSED events, since we expect the
  use count to reach zero while evaluating Authentication and Ciphering. (I
  experimented with holding a use count by AUTH_CIPH onenter() and releasing by
  onleave(), but the use count and thus the conn are released before the next
  state can initiate transactions that would increment the use count again.
  Same thing for the VLR FSMs holding a use count, they should be done before
  we advance to the next state. The easiest is to simply expect zero use count
  during the AUTH_CIPH state.)

- A CM Service Request means that even though the MSC would be through with all
  it wants to do, we shall still wait for a request to follow from the MS.
  Hence the FSM holds a use count on itself while a CM Service is pending.

- While waiting for a Release/Clear Complete, the FSM holds a use count on
  itself.

4. Add separate RELEASING state to the FSM.

If we decide to release for other reasons than a use count reaching zero, we
still need to be able to wait for the msc_dtap() use count on the conn to
release.

(An upcoming patch will further use the RELEASING state to properly wait for
Clear Complete / Release Complete messages.)

5. Add rate counters for each of the three Complete Layer 3 types.

Besides LU, also count CM Service Request and Paging Response
acceptance/rejections. Without these counters, only very few of the auth+ciph
outcomes actually show in the counters.

Related: OS#3122
Change-Id: I55feb379e176a96a831e105b86202b17a0ffe889
diff --git a/include/osmocom/msc/osmo_msc.h b/include/osmocom/msc/osmo_msc.h
index 1334138..f7d89a2 100644
--- a/include/osmocom/msc/osmo_msc.h
+++ b/include/osmocom/msc/osmo_msc.h
@@ -14,8 +14,8 @@
 enum subscr_conn_fsm_event {
 	/* Mark 0 as invalid to catch uninitialized vars */
 	SUBSCR_CONN_E_INVALID = 0,
-	/* Timeout on connection establishment starts */
-	SUBSCR_CONN_E_START,
+	/* Accepted the initial Complete Layer 3 (starting to evaluate Authentication and Ciphering) */
+	SUBSCR_CONN_E_COMPLETE_LAYER_3,
 	/* LU or Process Access FSM has determined that this conn is good */
 	SUBSCR_CONN_E_ACCEPTED,
 	/* received first reply from MS in "real" CC, SMS, USSD communication */
@@ -26,13 +26,16 @@
 	SUBSCR_CONN_E_MO_CLOSE,
 	/* MSC originated close request, e.g. failed authentication */
 	SUBSCR_CONN_E_CN_CLOSE,
+	/* The usage count for the conn has reached zero */
+	SUBSCR_CONN_E_UNUSED,
 };
 
 enum subscr_conn_fsm_state {
-	SUBSCR_CONN_S_INIT,
 	SUBSCR_CONN_S_NEW,
+	SUBSCR_CONN_S_AUTH_CIPH,
 	SUBSCR_CONN_S_ACCEPTED,
 	SUBSCR_CONN_S_COMMUNICATING,
+	SUBSCR_CONN_S_RELEASING,
 	SUBSCR_CONN_S_RELEASED,
 };
 
@@ -47,7 +50,8 @@
 void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn,
 			       enum complete_layer3_type from, const char *id);
 char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn);
-int msc_create_conn_fsm(struct gsm_subscriber_connection *conn, const char *id);
+
+void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn);
 
 int msc_vlr_alloc(struct gsm_network *net);
 int msc_vlr_start(struct gsm_network *net);
@@ -69,19 +73,23 @@
 
 void msc_subscr_conn_init(void);
 bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn);
+bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn);
 void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn);
 void msc_subscr_conn_close(struct gsm_subscriber_connection *conn,
 			   uint32_t cause);
+bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn);
 
 enum msc_subscr_conn_use {
 	MSC_CONN_USE_UNTRACKED = -1,
 	MSC_CONN_USE_COMPL_L3,
 	MSC_CONN_USE_DTAP,
-	MSC_CONN_USE_FSM,
+	MSC_CONN_USE_AUTH_CIPH,
+	MSC_CONN_USE_CM_SERVICE,
 	MSC_CONN_USE_TRANS_CC,
 	MSC_CONN_USE_TRANS_SMS,
 	MSC_CONN_USE_TRANS_USSD,
 	MSC_CONN_USE_SILENT_CALL,
+	MSC_CONN_USE_RELEASE,
 };
 
 extern const struct value_string msc_subscr_conn_use_names[];
@@ -102,6 +110,4 @@
 
 void msc_stop_paging(struct vlr_subscr *vsub);
 
-void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn);
-
 #endif