Keep sgsn subsystems under struct sgsn_instance lifecycle

Rework initialization and destruction of several sgsn subsystems to be
allocated & released together with the struct sgsn_instance.

This makes it easier to destroy and recreate the entire context and
allows us to start moving global variables scattered around to be under
struct sgsn_instance.

Change-Id: Idf60519b8e475b94d38bbb69e737132a5afaefab
diff --git a/src/sgsn/gprs_sgsn.c b/src/sgsn/gprs_sgsn.c
index 572e90b..5afddb4 100644
--- a/src/sgsn/gprs_sgsn.c
+++ b/src/sgsn/gprs_sgsn.c
@@ -27,6 +27,8 @@
 #include <osmocom/core/rate_ctr.h>
 #include <osmocom/core/stats.h>
 #include <osmocom/core/backtrace.h>
+#include <osmocom/ctrl/control_if.h>
+#include <osmocom/ctrl/ports.h>
 #include <osmocom/gprs/gprs_ns2.h>
 #include <osmocom/gprs/gprs_bssgp.h>
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
@@ -59,6 +61,7 @@
 
 extern struct sgsn_instance *sgsn;
 extern void *tall_sgsn_ctx;
+extern struct osmo_tdef sgsn_T_defs[];
 
 LLIST_HEAD(sgsn_mm_ctxts);
 LLIST_HEAD(sgsn_ggsn_ctxts);
@@ -145,12 +148,6 @@
 	sgsn_ctr_description,
 };
 
-void sgsn_rate_ctr_init(void)
-{
-	sgsn->rate_ctrs = rate_ctr_group_alloc(tall_sgsn_ctx, &sgsn_ctrg_desc, 0);
-	OSMO_ASSERT(sgsn->rate_ctrs);
-}
-
 /* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/
 struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx)
 {
@@ -893,21 +890,67 @@
 	osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
 }
 
+static int sgsn_instance_talloc_destructor(struct sgsn_instance *sgi)
+{
+	sgsn_cdr_release(sgi);
+	osmo_timer_del(&sgi->llme_timer);
+	rate_ctr_group_free(sgi->rate_ctrs);
+	return 0;
+}
+
 struct sgsn_instance *sgsn_instance_alloc(void *talloc_ctx)
 {
 	struct sgsn_instance *inst;
 	inst = talloc_zero(talloc_ctx, struct sgsn_instance);
+
+	talloc_set_destructor(inst, sgsn_instance_talloc_destructor);
+
 	inst->cfg.gtp_statedir = talloc_strdup(inst, "./");
 	inst->cfg.auth_policy = SGSN_AUTH_POLICY_CLOSED;
 	inst->cfg.require_authentication = true; /* only applies if auth_policy is REMOTE */
 	inst->cfg.gsup_server_port = OSMO_GSUP_PORT;
 
+	inst->cfg.T_defs = sgsn_T_defs;
+	osmo_tdefs_reset(inst->cfg.T_defs);
+	inst->cfg.T_defs_gtp = gtp_T_defs;
+	osmo_tdefs_reset(inst->cfg.T_defs_gtp);
+
+	inst->rate_ctrs = rate_ctr_group_alloc(inst, &sgsn_ctrg_desc, 0);
+	OSMO_ASSERT(inst->rate_ctrs);
+
 	INIT_LLIST_HEAD(&inst->mme_list);
+
+	osmo_timer_setup(&inst->llme_timer, sgsn_llme_check_cb, NULL);
+	osmo_timer_schedule(&inst->llme_timer, GPRS_LLME_CHECK_TICK, 0);
+	/* These are mostly setting up stuff not related to VTY cfg, so they can be set up here: */
+	sgsn_auth_init(inst);
+	sgsn_cdr_init(inst);
 	return inst;
 }
 
-void sgsn_inst_init(struct sgsn_instance *sgsn)
+/* To be called after VTY config parsing: */
+int sgsn_inst_init(struct sgsn_instance *sgsn)
 {
-	osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
-	osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
+	int rc;
+
+	/* start control interface after reading config for
+	 * ctrl_vty_get_bind_addr() */
+	sgsn->ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_SGSN, NULL);
+	if (!sgsn->ctrlh) {
+		LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
+		return -EIO;
+	}
+
+	rc = sgsn_ctrl_cmds_install();
+	if (rc != 0) {
+		LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
+		return -EFAULT;
+	}
+
+	rc = gprs_subscr_init(sgsn);
+	if (rc < 0) {
+		LOGP(DGPRS, LOGL_FATAL, "Cannot set up SGSN\n");
+		return rc;
+	}
+	return 0;
 }
diff --git a/src/sgsn/sgsn_cdr.c b/src/sgsn/sgsn_cdr.c
index 24d7524..ac0a6fd 100644
--- a/src/sgsn/sgsn_cdr.c
+++ b/src/sgsn/sgsn_cdr.c
@@ -41,7 +41,6 @@
 
 /* TODO...avoid going through a global */
 extern struct sgsn_instance *sgsn;
-extern struct ctrl_handle *g_ctrlh;
 
 /**
  * The CDR module will generate an entry like:
@@ -65,7 +64,7 @@
 
 static void send_cdr_trap(char *value)
 {
-	if (ctrl_cmd_send_trap(g_ctrlh, "cdr-v1", value) < 0)
+	if (ctrl_cmd_send_trap(sgsn->ctrlh, "cdr-v1", value) < 0)
 		LOGP(DGPRS, LOGL_ERROR, "Failed to create and send TRAP cdr-v1\n");
 }
 
@@ -300,3 +299,8 @@
 
 	return 0;
 }
+
+void sgsn_cdr_release(struct sgsn_instance *sgsn)
+{
+	osmo_signal_unregister_handler(SS_SGSN, handle_sgsn_sig, sgsn);
+}
diff --git a/src/sgsn/sgsn_main.c b/src/sgsn/sgsn_main.c
index 3ad0881..7253348 100644
--- a/src/sgsn/sgsn_main.c
+++ b/src/sgsn/sgsn_main.c
@@ -67,9 +67,6 @@
 #include <osmocom/sgsn/gprs_subscriber.h>
 #include <osmocom/sgsn/gtp.h>
 
-#include <osmocom/ctrl/control_if.h>
-#include <osmocom/ctrl/ports.h>
-
 #include <gtp.h>
 #include <osmocom/sgsn/sgsn_rim.h>
 
@@ -85,7 +82,6 @@
 #include <getopt.h>
 
 void *tall_sgsn_ctx;
-struct ctrl_handle *g_ctrlh;
 
 struct gprs_ns2_inst *sgsn_nsi;
 static int daemonize = 0;
@@ -418,16 +414,11 @@
 	bssgp_set_bssgp_callback(sgsn_bssgp_dispatch_ns_unitdata_req_cb, sgsn_nsi);
 
 	gprs_llc_init("/usr/local/lib/osmocom/crypt/");
-	sgsn_rate_ctr_init();
-	sgsn_inst_init(sgsn);
-
 
 	gprs_ns2_vty_init(sgsn_nsi);
 	bssgp_vty_init();
 	gprs_llc_vty_init();
 	gprs_sndcp_vty_init();
-	sgsn_auth_init(sgsn);
-	sgsn_cdr_init(sgsn);
 
 	handle_options(argc, argv);
 
@@ -458,20 +449,6 @@
 	if (rc < 0)
 		exit(1);
 
-	/* start control interface after reading config for
-	 * ctrl_vty_get_bind_addr() */
-	g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_SGSN, NULL);
-	if (!g_ctrlh) {
-		LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
-		exit(1);
-	}
-
-	if (sgsn_ctrl_cmds_install() != 0) {
-		LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
-		exit(1);
-	}
-
-
 	rc = sgsn_gtp_init(sgsn);
 	if (rc) {
 		LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
@@ -479,9 +456,9 @@
 	} else
 		LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
 
-	rc = gprs_subscr_init(sgsn);
+	rc = sgsn_inst_init(sgsn);
 	if (rc < 0) {
-		LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
+		LOGP(DGPRS, LOGL_FATAL, "Cannot set up SGSN\n");
 		exit(2);
 	}
 
diff --git a/src/sgsn/sgsn_vty.c b/src/sgsn/sgsn_vty.c
index 18951f8..4affa9f 100644
--- a/src/sgsn/sgsn_vty.c
+++ b/src/sgsn/sgsn_vty.c
@@ -99,7 +99,7 @@
 #define NONSPEC_X1001_SECS     5       /* wait for a RANAP Release Complete */
 
 
-static struct osmo_tdef sgsn_T_defs[] = {
+struct osmo_tdef sgsn_T_defs[] = {
 	{ .T=3312, .default_val=GSM0408_T3312_SECS, .desc="Periodic RA Update timer (s)" },
 	{ .T=3313, .default_val=GSM0408_T3313_SECS, .desc="Waiting for paging response timer (s)" },
 	{ .T=3314, .default_val=GSM0408_T3314_SECS, .desc="READY timer. Force to STANDBY on expiry timer (s)" },
@@ -1739,12 +1739,6 @@
 {
 	g_cfg = cfg;
 
-	g_cfg->T_defs = sgsn_T_defs;
-	osmo_tdefs_reset(g_cfg->T_defs);
-
-	g_cfg->T_defs_gtp = gtp_T_defs;
-	osmo_tdefs_reset(g_cfg->T_defs_gtp);
-
 	install_element_ve(&show_sgsn_cmd);
 	//install_element_ve(&show_mmctx_tlli_cmd);
 	install_element_ve(&show_mmctx_imsi_cmd);