Optimize PAGING-CS PDCH set selection when target MS is known

Before this patch, when a PAGING-GS was received in PCU from SGSN, it
would always forward the paging request to all PDCHs in all TRXs of all
BTS (well, it did some heuristics to avoid sending it in some PDCHs
where onyl repeated TBFs would be listening).

The previous behavior, didn't make much sense in the case where the PCU
is asked to page an MS which it knows (ie in which PDCHs is listening
to). Hence, in that case it makes sense to simply send the paging
request on 1 PDCH where the MS is listening, instead of sending it in a
big set of different PDCHs.

This commit also splits the old get_paging_mi() helper which was
erroneously created to parseboth CS/PS-PAGING requesst, since they
actually use a different set of target subscriber information (for
instance, CS-PAGING provides optionally a TLLI, and one provides P-TMSI
while the other provides TMSI).

In this patch, the handling of CS paging request is split into 2 parts:
1- A new helper "struct paging_req_cs" is introduced, where incoming
CS-PAGING requests (from both SGSN over BSSGP and BTS/BSC over PCUIF)
are parsed and information stored. Then, from available information, it
tries to find a target MS if avaialable
2- bts_add_paging() is called from both BSSGP and PCUIF paths with the
helper struct and the target MS (NULL if not found). If MS exists,
paging is forwarding only on 1 PDCH that MS is attached to. If no MS
exists, then the old heursitics are used to forward the request to all
MS.

Change-Id: Iea46d5321a29d800813b1aa2bf4ce175ce45e2cf
diff --git a/src/gprs_bssgp_pcu.c b/src/gprs_bssgp_pcu.c
index f6114de..5d0a489 100644
--- a/src/gprs_bssgp_pcu.c
+++ b/src/gprs_bssgp_pcu.c
@@ -171,8 +171,78 @@
 			ms_class, egprs_ms_class, delay_csec, data, len);
 }
 
+/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
+static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
+{
+	int rc;
+
+	req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
+
+	if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
+		LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
+		return BSSGP_CAUSE_MISSING_MAND_IE;
+	}
+
+	rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
+					 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
+	if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
+		LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
+		return BSSGP_CAUSE_INV_MAND_INF;
+	}
+	req->mi_imsi_present = true;
+
+	/* TIMSI is optional */
+	req->mi_tmsi_present = false;
+	if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
+		/* Be safe against an evil SGSN - check the length */
+		if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
+			LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
+			return BSSGP_CAUSE_COND_IE_ERR;
+		}
+
+		/* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
+		req->mi_tmsi = (struct osmo_mobile_identity){
+			.type = GSM_MI_TYPE_TMSI,
+		};
+		req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
+		req->mi_tmsi_present = true;
+	}
+
+	if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
+		req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
+	else
+		req->tlli = GSM_RESERVED_TMSI;
+
+	return 0;
+}
+
+static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
+{
+	struct paging_req_cs req;
+	struct gprs_rlcmac_bts *bts;
+	struct GprsMs *ms;
+	int rc;
+
+	if ((rc = get_paging_cs_mi(&req, tp)) > 0)
+		return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
+
+	/* We need to page all BTSs since even if a BTS has a matching MS, it
+	 * may have already moved to a newer BTS. On Each BTS, if the MS is
+	 * known, then bts_add_paging() can optimize and page only on PDCHs the
+	 * target MS is using. */
+	llist_for_each_entry(bts, &the_pcu->bts_list, list) {
+		/* TODO: Match by TMSI before IMSI if present?! */
+		ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
+		if (!ms && req.mi_imsi_present)
+			ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
+		bts_add_paging(bts, &req, ms);
+	}
+
+	return 0;
+}
+
 /* Returns 0 on success, suggested BSSGP cause otherwise */
-static unsigned int get_paging_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
+static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
 {
 	/* Use TMSI (if present) or IMSI */
 	if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
@@ -202,22 +272,6 @@
 	return 0;
 }
 
-static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
-{
-	struct osmo_mobile_identity mi;
-	struct gprs_rlcmac_bts *bts;
-	int rc;
-
-	if ((rc = get_paging_mi(&mi, tp)) > 0)
-		return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
-
-	/* FIXME: look if MS is attached a specific BTS and then only page on that one? */
-	llist_for_each_entry(bts, &the_pcu->bts_list, list) {
-		bts_add_paging(bts, tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0), &mi);
-	}
-	return 0;
-}
-
 static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
 {
 	struct osmo_mobile_identity mi_imsi;
@@ -242,7 +296,7 @@
 		return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
 	}
 
-	if ((rc = get_paging_mi(&paging_mi, tp)) > 0)
+	if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
 		return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
 
 	/* FIXME: look if MS is attached a specific BTS and then only page on that one? */