tbf: Add MS object management to TBF code

This commit adds MS object creation and cleanup to the TBF related
code. MS objects are created when a TBF that has been "anonymous" so
far gets associated with a TLLI. When a TBF is replaced by another,
the old TBF is detached and the new one is attached to the MS. When
all TBFs have been detached, the MS object gets deleted.

The TBF related code should not call attach_tbf/detach_tbf directly
but use set_ms instead to make sure, that the references are updated
properly. GprsMs::detach_tbf also calls set_ms(NULL) on the detached
TBF object.

The MS object is not really used yet, the focus is still on object
creation, TBF association, and cleanup.

Ticket: #1674
Sponsored-by: On-Waves ehf
diff --git a/src/tbf.cpp b/src/tbf.cpp
index b1377f0..89072e4 100644
--- a/src/tbf.cpp
+++ b/src/tbf.cpp
@@ -26,6 +26,7 @@
 #include <gprs_rlcmac.h>
 #include <gprs_debug.h>
 #include <gprs_bssgp_pcu.h>
+#include <gprs_ms.h>
 #include <decoding.h>
 
 extern "C" {
@@ -60,16 +61,43 @@
 				tbf_name(this), tbf_name(tbf));
 			return;
 		}
-		if (m_new_tbf != this)
+		if (m_new_tbf != this) {
 			LOGP(DRLCMAC, LOGL_NOTICE,
 				"%s m_new_tbf is already assigned to %s, "
 				"overwriting the old value with %s\n",
 				tbf_name(this), tbf_name(m_new_tbf), tbf_name(tbf));
+		}
 		/* Detach from other TBF */
 		m_new_tbf->m_old_tbf = NULL;
 	}
 	m_new_tbf = tbf;
 	tbf->m_old_tbf = this;
+
+	if (!tbf->ms())
+		tbf->set_ms(ms());
+}
+
+void gprs_rlcmac_tbf::set_ms(GprsMs *ms)
+{
+	if (m_ms == ms)
+		return;
+
+	if (m_ms)
+		m_ms->detach_tbf(this);
+
+	m_ms = ms;
+
+	if (m_ms)
+		m_ms->attach_tbf(this);
+}
+
+void gprs_rlcmac_tbf::update_ms(uint32_t tlli)
+{
+	if (!ms())
+		/* TODO: access the container instead when that is implemented */
+		set_ms(new GprsMs(tlli));
+	else
+		ms()->set_tlli(tlli);
 }
 
 gprs_rlcmac_ul_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts,
@@ -102,6 +130,7 @@
 	tbf->set_state(GPRS_RLCMAC_ASSIGN);
 	tbf->state_flags |= (1 << GPRS_RLCMAC_FLAG_PACCH);
 	tbf_timer_start(tbf, 3169, bts->t3169, 0);
+	tbf->update_ms(tlli);
 
 	return tbf;
 }
@@ -168,6 +197,8 @@
 				"tbf->m_old_tbf->m_new_tbf != tbf\n",
 				tbf_name(tbf));
 		}
+
+		tbf->m_old_tbf = NULL;
 	}
 
 	if (tbf->m_new_tbf) {
@@ -183,8 +214,13 @@
 				"tbf->m_new_tbf->m_old_tbf != tbf\n",
 				tbf_name(tbf));
 		}
+
+		tbf->m_new_tbf = NULL;
 	}
 
+	if (tbf->ms())
+		tbf->set_ms(NULL);
+
 	LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF ends here **********\n");
 	talloc_free(tbf);
 }
@@ -438,6 +474,12 @@
 	llist_add(&tbf->list.list, &bts->ul_tbfs);
 	tbf->bts->tbf_ul_created();
 
+	if (old_tbf && old_tbf->ms())
+		tbf->set_ms(old_tbf->ms());
+
+	if (tbf->ms())
+		tbf->ms()->attach_ul_tbf(tbf);
+
 	return tbf;
 }
 
@@ -477,6 +519,12 @@
 	gettimeofday(&tbf->m_bw.dl_bw_tv, NULL);
 	gettimeofday(&tbf->m_bw.dl_loss_tv, NULL);
 
+	if (old_tbf && old_tbf->ms())
+		tbf->set_ms(old_tbf->ms());
+
+	if (tbf->ms())
+		tbf->ms()->attach_dl_tbf(tbf);
+
 	return tbf;
 }
 
@@ -811,6 +859,7 @@
 		return 0;
 	}
 	update_tlli(new_tlli);
+	update_ms(new_tlli);
 	LOGP(DRLCMACUL, LOGL_INFO, "Decoded premier TLLI=0x%08x of "
 		"UL DATA TFI=%d.\n", tlli(), rh->tfi);
 	if ((dl_tbf = bts->dl_tbf_by_tlli(tlli()))) {