db v6: determine 3G AUC IND from VLR name

Each VLR requesting auth tuples should use a distinct IND pool for 3G
auth.  So far we tied the IND to the GSUP peer connection; MSC and SGSN
were always distinct GSUP peers, they ended up using distinct INDs.

However, we have implemented a GSUP proxy, so that, in a distributed
setup, a remotely roaming subscriber has only one direct GSUP peer
proxying for both remote MSC and SGSN. That means as soon as a
subscriber roams to a different site, we would use the GSUP proxy name
to determine the IND instead of the separate MSC and SGSN. The site's
MSC and SGSN appear as the same client, get the same IND bucket, waste
SQNs rapidly and cause auth tuple generation load.

So instead of using the local client as IND, persistently keep a list of
VLR names and assign a different IND to each. Use the
gsup_req->source_name as indicator, which reflects the actual remote
VLR's name (remote MSC or SGSN).

Persist the site <-> IND assignments in the database.

Add an IND test to db_test.c

There was an earlier patch version that separated the IND pools by
cn_domain, but it turned out to add complex semantics, while only
solving one aspect of the "adjacent VLR" problem. We need a solution not
only for CS vs PS, but also for 2,3G vs 4G, and for sites that are
physically adjacent to each other. This patch version does not offer any
automatic solution for that -- as soon as more than 2^IND_bitlen
(usually 32) VLRs show up, it is the responsibility of the admin to
ensure the 'ind' table in the hlr.db does not have unfortunate IND
assignments. So far no VTY commands exist for that, they may be added in
the future.

Related: OS#4319
Change-Id: I6f0a6bbef3a27507605c3b4a0e1a89bdfd468374
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9015494..9270efa 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,5 @@
 SUBDIRS = \
 	auc \
-	gsup_server \
 	db \
 	gsup \
 	db_upgrade \
diff --git a/tests/db/db_test.c b/tests/db/db_test.c
index 0c68238..00434cf 100644
--- a/tests/db/db_test.c
+++ b/tests/db/db_test.c
@@ -918,6 +918,50 @@
 	comment_end();
 }
 
+static void test_ind()
+{
+	comment_start();
+
+#define ASSERT_IND(VLR, IND) do { \
+		unsigned int ind; \
+		struct osmo_cni_peer_id vlr; \
+		OSMO_ASSERT(!osmo_cni_peer_id_set_str(&vlr, OSMO_CNI_PEER_ID_IPA_NAME, VLR)); \
+		ASSERT_RC(db_ind(dbc, &vlr, &ind), 0); \
+		fprintf(stderr, "%s ind = %u\n\n", osmo_quote_str((char*)vlr.ipa_name.val, vlr.ipa_name.len), ind); \
+		if (ind != (IND)) \
+			fprintf(stderr, "  ERROR: expected " #IND "\n"); \
+	} while (0)
+#define IND_DEL(VLR) do { \
+		struct osmo_cni_peer_id vlr; \
+		OSMO_ASSERT(!osmo_cni_peer_id_set_str(&vlr, OSMO_CNI_PEER_ID_IPA_NAME, VLR)); \
+		ASSERT_RC(db_ind_del(dbc, &vlr), 0); \
+		fprintf(stderr, "%s ind deleted\n\n", osmo_quote_str((char*)vlr.ipa_name.val, vlr.ipa_name.len)); \
+	} while (0)
+
+	ASSERT_IND("msc-23", 1);
+	ASSERT_IND("sgsn-11", 2);
+	ASSERT_IND("msc-42", 3);
+	ASSERT_IND("sgsn-22", 4);
+	ASSERT_IND("msc-0x17", 5);
+	ASSERT_IND("sgsn-0xaa", 6);
+	ASSERT_IND("msc-42", 3);
+	ASSERT_IND("sgsn-22", 4);
+	ASSERT_IND("msc-0x17", 5);
+	ASSERT_IND("sgsn-0xaa", 6);
+	ASSERT_IND("sgsn-0xbb", 7);
+	ASSERT_IND("msc-0x2a", 8);
+	ASSERT_IND("msc-42", 3);
+	ASSERT_IND("sgsn-22", 4);
+	ASSERT_IND("msc-23", 1);
+	ASSERT_IND("sgsn-11", 2);
+
+	IND_DEL("msc-0x17"); /* dropped IND == 5 */
+	ASSERT_IND("msc-0x2a", 8); /* known CS remains where it is */
+	ASSERT_IND("any-unknown", 9); /* new VLR takes a new IND from the end */
+
+	comment_end();
+}
+
 static struct {
 	bool verbose;
 } cmdline_opts = {
@@ -1002,6 +1046,7 @@
 	test_subscr_aud();
 	test_subscr_aud_invalid_len();
 	test_subscr_sqn();
+	test_ind();
 
 	printf("Done\n");
 	db_close(dbc);
diff --git a/tests/db/db_test.err b/tests/db/db_test.err
index 871e722..ddf6d00 100644
--- a/tests/db/db_test.err
+++ b/tests/db/db_test.err
@@ -1613,3 +1613,83 @@
 
 ===== test_subscr_sqn: SUCCESS
 
+
+===== test_ind
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-23\0" ind = 1
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-11\0" ind = 2
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-42\0" ind = 3
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-22\0" ind = 4
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-0x17\0" ind = 5
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-0xaa\0" ind = 6
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-42\0" ind = 3
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-22\0" ind = 4
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-0x17\0" ind = 5
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-0xaa\0" ind = 6
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-0xbb\0" ind = 7
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-0x2a\0" ind = 8
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-42\0" ind = 3
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-22\0" ind = 4
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-23\0" ind = 1
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"sgsn-11\0" ind = 2
+
+db_ind_del(dbc, &vlr) --> 0
+
+"msc-0x17\0" ind deleted
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"msc-0x2a\0" ind = 8
+
+db_ind(dbc, &vlr, &ind) --> 0
+
+"any-unknown\0" ind = 9
+
+===== test_ind: SUCCESS
+
diff --git a/tests/db_upgrade/db_upgrade_test.ok b/tests/db_upgrade/db_upgrade_test.ok
index 2bc6a39..0a45f7c 100644
--- a/tests/db_upgrade/db_upgrade_test.ok
+++ b/tests/db_upgrade/db_upgrade_test.ok
@@ -85,6 +85,7 @@
 DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 3
 DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 4
 DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 5
+DDB Database <PATH>test.db' has been upgraded to HLR DB schema version 6
 DMAIN Cmdline option --db-check: Database was opened successfully, quitting.
 
 Resulting db:
@@ -117,6 +118,13 @@
 5|5|44444444444444444444444444444444|44444444444444444444444444444444||0|5
 5|5|55555555555555555555555555555555||55555555555555555555555555555555|0|6
 
+Table: ind
+name|type|notnull|dflt_value|pk
+ind|INTEGER|0||1
+vlr|TEXT|1||0
+
+Table ind contents:
+
 Table: subscriber
 name|type|notnull|dflt_value|pk
 ggsn_number|VARCHAR(15)|0||0
@@ -171,5 +179,5 @@
 rc = 0
 DMAIN hlr starting
 DDB using database: <PATH>test.db
-DDB Database <PATH>test.db' has HLR DB schema version 5
+DDB Database <PATH>test.db' has HLR DB schema version 6
 DMAIN Cmdline option --db-check: Database was opened successfully, quitting.
diff --git a/tests/gsup_server/Makefile.am b/tests/gsup_server/Makefile.am
deleted file mode 100644
index 34acd30..0000000
--- a/tests/gsup_server/Makefile.am
+++ /dev/null
@@ -1,44 +0,0 @@
-AM_CPPFLAGS = \
-	$(all_includes) \
-	$(NULL)
-
-AM_CFLAGS = \
-	-Wall \
-	-ggdb3 \
-	-I$(top_srcdir)/include \
-	$(LIBOSMOCORE_CFLAGS) \
-	$(LIBOSMOGSM_CFLAGS) \
-	$(LIBOSMOABIS_CFLAGS) \
-	$(NULL)
-
-AM_LDFLAGS = \
-	-no-install \
-	$(NULL)
-
-EXTRA_DIST = \
-	gsup_server_test.ok \
-	gsup_server_test.err \
-	$(NULL)
-
-noinst_PROGRAMS = \
-	gsup_server_test \
-	$(NULL)
-
-gsup_server_test_SOURCES = \
-	gsup_server_test.c \
-	$(NULL)
-
-gsup_server_test_LDADD = \
-	$(top_srcdir)/src/gsup_server.c \
-	$(top_srcdir)/src/gsup_router.c \
-	$(top_srcdir)/src/gsup_send.c \
-	$(top_srcdir)/src/gsupclient/cni_peer_id.c \
-	$(top_srcdir)/src/gsupclient/gsup_req.c \
-	$(LIBOSMOCORE_LIBS) \
-	$(LIBOSMOGSM_LIBS) \
-	$(LIBOSMOABIS_LIBS) \
-	$(NULL)
-
-.PHONY: update_exp
-update_exp:
-	$(builddir)/gsup_server_test >"$(srcdir)/gsup_server_test.ok" 2>"$(srcdir)/gsup_server_test.err"
diff --git a/tests/gsup_server/gsup_server_test.c b/tests/gsup_server/gsup_server_test.c
deleted file mode 100644
index 4aec69b..0000000
--- a/tests/gsup_server/gsup_server_test.c
+++ /dev/null
@@ -1,145 +0,0 @@
-/* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
- * All Rights Reserved
- *
- * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <stdio.h>
-#include <osmocom/core/utils.h>
-#include <osmocom/hlr/gsup_server.h>
-
-#define comment_start() printf("\n===== %s\n", __func__)
-#define comment_end() printf("===== %s: SUCCESS\n\n", __func__)
-#define btw(fmt, args...) printf("\n" fmt "\n", ## args)
-
-#define VERBOSE_ASSERT(val, expect_op, fmt) \
-	do { \
-		printf(#val " == " fmt "\n", (val)); \
-		OSMO_ASSERT((val) expect_op); \
-	} while (0)
-
-void osmo_gsup_server_add_conn(struct llist_head *clients,
-			       struct osmo_gsup_conn *conn);
-
-static void test_add_conn(void)
-{
-	struct llist_head _list;
-	struct llist_head *clients = &_list;
-	struct osmo_gsup_conn conn_inst[23] = {};
-	struct osmo_gsup_conn *conn;
-	unsigned int i;
-
-	comment_start();
-
-	INIT_LLIST_HEAD(clients);
-
-	btw("Add 10 items");
-	for (i = 0; i < 10; i++) {
-		osmo_gsup_server_add_conn(clients, &conn_inst[i]);
-		printf("conn_inst[%u].auc_3g_ind == %u\n", i, conn_inst[i].auc_3g_ind);
-		OSMO_ASSERT(clients->next == &conn_inst[0].list);
-	}
-
-	btw("Expecting a list of 0..9");
-	i = 0;
-	llist_for_each_entry(conn, clients, list) {
-		printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind);
-		OSMO_ASSERT(conn->auc_3g_ind == i);
-		OSMO_ASSERT(conn == &conn_inst[i]);
-		i++;
-	}
-
-	btw("Punch two holes in the sequence in arbitrary order,"
-	    " a larger one from 2..4 and a single one at 7.");
-	llist_del(&conn_inst[4].list);
-	llist_del(&conn_inst[2].list);
-	llist_del(&conn_inst[3].list);
-	llist_del(&conn_inst[7].list);
-
-	btw("Expecting a list of 0,1, 5,6, 8,9");
-	i = 0;
-	llist_for_each_entry(conn, clients, list) {
-		printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind);
-		i++;
-	}
-
-	btw("Add conns, expecting them to take the open slots");
-	osmo_gsup_server_add_conn(clients, &conn_inst[12]);
-	VERBOSE_ASSERT(conn_inst[12].auc_3g_ind, == 2, "%u");
-
-	osmo_gsup_server_add_conn(clients, &conn_inst[13]);
-	VERBOSE_ASSERT(conn_inst[13].auc_3g_ind, == 3, "%u");
-
-	osmo_gsup_server_add_conn(clients, &conn_inst[14]);
-	VERBOSE_ASSERT(conn_inst[14].auc_3g_ind, == 4, "%u");
-
-	osmo_gsup_server_add_conn(clients, &conn_inst[17]);
-	VERBOSE_ASSERT(conn_inst[17].auc_3g_ind, == 7, "%u");
-
-	osmo_gsup_server_add_conn(clients, &conn_inst[18]);
-	VERBOSE_ASSERT(conn_inst[18].auc_3g_ind, == 10, "%u");
-
-	btw("Expecting a list of 0..10");
-	i = 0;
-	llist_for_each_entry(conn, clients, list) {
-		printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind);
-		OSMO_ASSERT(conn->auc_3g_ind == i);
-		i++;
-	}
-
-	btw("Does it also work for the first item?");
-	llist_del(&conn_inst[0].list);
-
-	btw("Expecting a list of 1..10");
-	i = 0;
-	llist_for_each_entry(conn, clients, list) {
-		printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind);
-		OSMO_ASSERT(conn->auc_3g_ind == i + 1);
-		i++;
-	}
-
-	btw("Add another conn, should take auc_3g_ind == 0");
-	osmo_gsup_server_add_conn(clients, &conn_inst[20]);
-	VERBOSE_ASSERT(conn_inst[20].auc_3g_ind, == 0, "%u");
-
-	btw("Expecting a list of 0..10");
-	i = 0;
-	llist_for_each_entry(conn, clients, list) {
-		printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind);
-		OSMO_ASSERT(conn->auc_3g_ind == i);
-		i++;
-	}
-
-	btw("If a client reconnects, it will (likely) get the same auc_3g_ind");
-	VERBOSE_ASSERT(conn_inst[5].auc_3g_ind, == 5, "%u");
-	llist_del(&conn_inst[5].list);
-	conn_inst[5].auc_3g_ind = 423;
-	osmo_gsup_server_add_conn(clients, &conn_inst[5]);
-	VERBOSE_ASSERT(conn_inst[5].auc_3g_ind, == 5, "%u");
-
-	comment_end();
-}
-
-int main(int argc, char **argv)
-{
-	printf("test_gsup_server.c\n");
-
-	test_add_conn();
-
-	printf("Done\n");
-	return 0;
-}
diff --git a/tests/gsup_server/gsup_server_test.err b/tests/gsup_server/gsup_server_test.err
deleted file mode 100644
index e69de29..0000000
--- a/tests/gsup_server/gsup_server_test.err
+++ /dev/null
diff --git a/tests/gsup_server/gsup_server_test.ok b/tests/gsup_server/gsup_server_test.ok
deleted file mode 100644
index 80d944c..0000000
--- a/tests/gsup_server/gsup_server_test.ok
+++ /dev/null
@@ -1,94 +0,0 @@
-test_gsup_server.c
-
-===== test_add_conn
-
-Add 10 items
-conn_inst[0].auc_3g_ind == 0
-conn_inst[1].auc_3g_ind == 1
-conn_inst[2].auc_3g_ind == 2
-conn_inst[3].auc_3g_ind == 3
-conn_inst[4].auc_3g_ind == 4
-conn_inst[5].auc_3g_ind == 5
-conn_inst[6].auc_3g_ind == 6
-conn_inst[7].auc_3g_ind == 7
-conn_inst[8].auc_3g_ind == 8
-conn_inst[9].auc_3g_ind == 9
-
-Expecting a list of 0..9
-conn[0].auc_3g_ind == 0
-conn[1].auc_3g_ind == 1
-conn[2].auc_3g_ind == 2
-conn[3].auc_3g_ind == 3
-conn[4].auc_3g_ind == 4
-conn[5].auc_3g_ind == 5
-conn[6].auc_3g_ind == 6
-conn[7].auc_3g_ind == 7
-conn[8].auc_3g_ind == 8
-conn[9].auc_3g_ind == 9
-
-Punch two holes in the sequence in arbitrary order, a larger one from 2..4 and a single one at 7.
-
-Expecting a list of 0,1, 5,6, 8,9
-conn[0].auc_3g_ind == 0
-conn[1].auc_3g_ind == 1
-conn[2].auc_3g_ind == 5
-conn[3].auc_3g_ind == 6
-conn[4].auc_3g_ind == 8
-conn[5].auc_3g_ind == 9
-
-Add conns, expecting them to take the open slots
-conn_inst[12].auc_3g_ind == 2
-conn_inst[13].auc_3g_ind == 3
-conn_inst[14].auc_3g_ind == 4
-conn_inst[17].auc_3g_ind == 7
-conn_inst[18].auc_3g_ind == 10
-
-Expecting a list of 0..10
-conn[0].auc_3g_ind == 0
-conn[1].auc_3g_ind == 1
-conn[2].auc_3g_ind == 2
-conn[3].auc_3g_ind == 3
-conn[4].auc_3g_ind == 4
-conn[5].auc_3g_ind == 5
-conn[6].auc_3g_ind == 6
-conn[7].auc_3g_ind == 7
-conn[8].auc_3g_ind == 8
-conn[9].auc_3g_ind == 9
-conn[10].auc_3g_ind == 10
-
-Does it also work for the first item?
-
-Expecting a list of 1..10
-conn[0].auc_3g_ind == 1
-conn[1].auc_3g_ind == 2
-conn[2].auc_3g_ind == 3
-conn[3].auc_3g_ind == 4
-conn[4].auc_3g_ind == 5
-conn[5].auc_3g_ind == 6
-conn[6].auc_3g_ind == 7
-conn[7].auc_3g_ind == 8
-conn[8].auc_3g_ind == 9
-conn[9].auc_3g_ind == 10
-
-Add another conn, should take auc_3g_ind == 0
-conn_inst[20].auc_3g_ind == 0
-
-Expecting a list of 0..10
-conn[0].auc_3g_ind == 0
-conn[1].auc_3g_ind == 1
-conn[2].auc_3g_ind == 2
-conn[3].auc_3g_ind == 3
-conn[4].auc_3g_ind == 4
-conn[5].auc_3g_ind == 5
-conn[6].auc_3g_ind == 6
-conn[7].auc_3g_ind == 7
-conn[8].auc_3g_ind == 8
-conn[9].auc_3g_ind == 9
-conn[10].auc_3g_ind == 10
-
-If a client reconnects, it will (likely) get the same auc_3g_ind
-conn_inst[5].auc_3g_ind == 5
-conn_inst[5].auc_3g_ind == 5
-===== test_add_conn: SUCCESS
-
-Done
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 956ef87..65f90b4 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -22,13 +22,6 @@
 AT_CHECK([$abs_top_builddir/tests/gsup/gsup_test], [], [expout], [experr])
 AT_CLEANUP
 
-AT_SETUP([gsup_server])
-AT_KEYWORDS([gsup_server])
-cat $abs_srcdir/gsup_server/gsup_server_test.ok > expout
-cat $abs_srcdir/gsup_server/gsup_server_test.err > experr
-AT_CHECK([$abs_top_builddir/tests/gsup_server/gsup_server_test], [], [expout], [experr])
-AT_CLEANUP
-
 AT_SETUP([db])
 AT_KEYWORDS([db])
 cat $abs_srcdir/db/db_test.ok > expout