ms: Integrate the MS storage

Use the MS storage to find a MS object for a given TLLI instead of
searching the TBF lists. The TBFs are then taken from the MS object,
if one has been found. If all TBF might be temporarily detached from
the MS object, a GprsMs::Guard is added to prevent the deletion of
the object, in case another TBF gets attached later on in the scope.

Ticket: #1674
Sponsored-by: On-Waves ehf
diff --git a/src/tbf.cpp b/src/tbf.cpp
index 89072e4..799699a 100644
--- a/src/tbf.cpp
+++ b/src/tbf.cpp
@@ -94,8 +94,7 @@
 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));
+		set_ms(bts->ms_store().get_or_create_ms(tlli));
 	else
 		ms()->set_tlli(tlli);
 }
@@ -236,7 +235,14 @@
 	if (direction != GPRS_RLCMAC_DL_TBF)
 		return -EINVAL;
 
-	ul_tbf = bts->ul_tbf_by_tlli(m_tlli);
+	if (ms()) {
+		ul_tbf = ms()->ul_tbf();
+	} else if (is_tlli_valid()) {
+		LOGP(DRLCMAC, LOGL_NOTICE,
+			"Using ul_tbf_by_tlli() since there is no MS object for "
+			"TLLI 0x%08x\n", m_tlli);
+		ul_tbf = bts->ul_tbf_by_tlli(m_tlli);
+	}
 
 	tbf_unlink_pdch(this);
 	rc = bts_data->alloc_algorithm(bts_data, ul_tbf, this, bts_data->alloc_algorithm_curst, 0);
@@ -840,10 +846,12 @@
 
 int gprs_rlcmac_tbf::extract_tlli(const uint8_t *data, const size_t len)
 {
-	struct gprs_rlcmac_tbf *dl_tbf, *ul_tbf;
+	struct gprs_rlcmac_tbf *dl_tbf = NULL;
+	struct gprs_rlcmac_tbf *ul_tbf = NULL;
 	struct rlc_ul_header *rh = (struct rlc_ul_header *)data;
 	uint32_t new_tlli;
 	int rc;
+	GprsMs *old_ms;
 
 	/* no TLLI yet */
 	if (!rh->ti) {
@@ -858,11 +866,21 @@
 		"of UL DATA TFI=%d.\n", rh->tfi);
 		return 0;
 	}
+
+	old_ms = bts->ms_by_tlli(new_tlli);
+	if (old_ms) {
+		/* Get them before calling set_ms() */
+		dl_tbf = old_ms->dl_tbf();
+		ul_tbf = old_ms->ul_tbf();
+
+		set_ms(old_ms);
+	}
+
 	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()))) {
+	if (dl_tbf) {
 		LOGP(DRLCMACUL, LOGL_NOTICE, "Got RACH from "
 			"TLLI=0x%08x while %s still exists. "
 			"Killing pending DL TBF\n", tlli(),
@@ -870,9 +888,7 @@
 		tbf_free(dl_tbf);
 		dl_tbf = NULL;
 	}
-	/* ul_tbf_by_tlli will not find your TLLI, because it is not
-	 * yet marked valid */
-	if ((ul_tbf = bts->ul_tbf_by_tlli(tlli()))) {
+	if (ul_tbf) {
 		LOGP(DRLCMACUL, LOGL_NOTICE, "Got RACH from "
 			"TLLI=0x%08x while %s still exists. "
 			"Killing pending UL TBF\n", tlli(),