Port gbproxy to NS2

Since NS2 has a different abstraction we mock up the prim send/recv
functions and don't test NS like the old tests did.

Related: SYS#4998
Change-Id: Iecfd0408a35a11638d254c1db3c1d477b1a11524
diff --git a/src/gbproxy/gb_proxy_main.c b/src/gbproxy/gb_proxy_main.c
index 8c83980..f8c5ade 100644
--- a/src/gbproxy/gb_proxy_main.c
+++ b/src/gbproxy/gb_proxy_main.c
@@ -38,7 +38,7 @@
 #include <osmocom/core/rate_ctr.h>
 #include <osmocom/core/stats.h>
 
-#include <osmocom/gprs/gprs_ns.h>
+#include <osmocom/gprs/gprs_ns2.h>
 #include <osmocom/gprs/gprs_bssgp.h>
 
 #include <osmocom/sgsn/signal.h>
@@ -80,26 +80,6 @@
 /* Pointer to the SGSN peer */
 extern struct gbprox_peer *gbprox_peer_sgsn;
 
-/* call-back function for the NS protocol */
-static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
-		      struct msgb *msg, uint16_t bvci)
-{
-	int rc = 0;
-
-	switch (event) {
-	case GPRS_NS_EVT_UNIT_DATA:
-		rc = gbprox_rcvmsg(gbcfg, msg, nsvc->nsei, bvci, nsvc->nsvci);
-		break;
-	default:
-		LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
-		if (msg)
-			msgb_free(msg);
-		rc = -EIO;
-		break;
-	}
-	return rc;
-}
-
 static void signal_handler(int signal)
 {
 	fprintf(stdout, "signal %u received\n", signal);
@@ -269,6 +249,8 @@
 	return stat(path, &sb) ? false : true;
 }
 
+int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg);
+
 int main(int argc, char **argv)
 {
 	int rc;
@@ -314,23 +296,23 @@
 	rate_ctr_init(tall_sgsn_ctx);
 	osmo_stats_init(tall_sgsn_ctx);
 
-	bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb, tall_sgsn_ctx);
-	if (!bssgp_nsi) {
-		LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
-		exit(1);
-	}
-
 	gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config);
 	if (!gbcfg) {
 		LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
 		exit(1);
 	}
 	gbproxy_init_config(gbcfg);
-	gbcfg->nsi = bssgp_nsi;
-	gprs_ns_vty_init(bssgp_nsi);
-	gprs_ns_set_log_ss(DNS);
+	gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg);
+	if (!gbcfg->nsi) {
+		LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
+		exit(1);
+	}
+
+	gprs_ns2_vty_init(gbcfg->nsi, NULL);
 	bssgp_set_log_ss(DBSSGP);
-	osmo_signal_register_handler(SS_L_NS, &gbprox_signal, gbcfg);
+	gprs_ns2_dynamic_create_nse(gbcfg->nsi, true);
+
+	bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg);
 
 	rc = gbproxy_parse_config(config_file, gbcfg);
 	if (rc < 0) {
@@ -338,6 +320,8 @@
 		exit(2);
 	}
 
+	gprs_ns2_vty_create();
+
 	/* start telnet after reading config for vty_get_bind_addr() */
 	rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
 			       vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
@@ -357,26 +341,13 @@
 		exit(1);
 	}
 
-	if (!gprs_nsvc_by_nsei(gbcfg->nsi, gbcfg->nsip_sgsn_nsei)) {
+	if (!gprs_ns2_nse_by_nsei(gbcfg->nsi, gbcfg->nsip_sgsn_nsei)) {
 		LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u "
 			"without creating that NSEI before\n",
 			gbcfg->nsip_sgsn_nsei);
 		exit(2);
 	}
 
-	rc = gprs_ns_nsip_listen(bssgp_nsi);
-	if (rc < 0) {
-		LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
-		exit(2);
-	}
-
-	rc = gprs_ns_frgre_listen(bssgp_nsi);
-	if (rc < 0) {
-		LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
-			"socket. Do you have CAP_NET_RAW?\n");
-		exit(2);
-	}
-
 	if (daemonize) {
 		rc = osmo_daemonize();
 		if (rc < 0) {
@@ -385,9 +356,6 @@
 		}
 	}
 
-	/* Reset all the persistent NS-VCs that we've read from the config */
-	gbprox_reset_persistent_nsvcs(bssgp_nsi);
-
 	while (1) {
 		rc = osmo_select_main(0);
 		if (rc < 0)