Change in osmo-bsc-nat[master]: Add bsc_nat fsm

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

osmith gerrit-no-reply at lists.osmocom.org
Tue Dec 21 19:46:16 UTC 2021


osmith has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-bsc-nat/+/26662 )


Change subject: Add bsc_nat fsm
......................................................................

Add bsc_nat fsm

Create the initial FSM that starts and stops two SS7 instances (one for
CN, one for RAN), and binds an sccp user to each.

Related: OS#2545
Depends: libosmocore 0066f1bdcb328e7ffc846b4746e3e1d02afe4be8
Change-Id: I7d52fa649c397582b18a1a7dcc40bb407f3b2c97
---
M doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg
M include/osmocom/bsc_nat/Makefile.am
M include/osmocom/bsc_nat/bsc_nat.h
A include/osmocom/bsc_nat/bsc_nat_fsm.h
M src/osmo-bsc-nat/Makefile.am
M src/osmo-bsc-nat/bsc_nat.c
A src/osmo-bsc-nat/bsc_nat_fsm.c
M src/osmo-bsc-nat/main.c
M src/osmo-bsc-nat/vty.c
9 files changed, 339 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc-nat refs/changes/62/26662/1

diff --git a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg
index e69de29..65ce821 100644
--- a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg
+++ b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg
@@ -0,0 +1,16 @@
+cs7 instance 0
+ point-code 0.23.3
+ asp asp-clnt-OsmoBSCNAT-CN 2905 0 m3ua
+  remote-ip 127.0.0.1
+  local-ip 127.0.0.3
+
+cs7 instance 1
+ point-code 0.23.1
+ asp asp-clnt-OsmoBSCNAT-RAN 2905 0 m3ua
+  remote-ip 127.0.0.2
+  local-ip 127.0.0.4
+
+bsc-nat
+ cs7-instance-cn 0
+ cs7-instance-ran 1
+
diff --git a/include/osmocom/bsc_nat/Makefile.am b/include/osmocom/bsc_nat/Makefile.am
index f573dd9..90f44a7 100644
--- a/include/osmocom/bsc_nat/Makefile.am
+++ b/include/osmocom/bsc_nat/Makefile.am
@@ -1,5 +1,6 @@
 noinst_HEADERS = \
 	bsc_nat.h \
+	bsc_nat_fsm.h \
 	logging.h \
 	vty.h \
 	$(NULL)
diff --git a/include/osmocom/bsc_nat/bsc_nat.h b/include/osmocom/bsc_nat/bsc_nat.h
index 511d5a4..d1a046e 100644
--- a/include/osmocom/bsc_nat/bsc_nat.h
+++ b/include/osmocom/bsc_nat/bsc_nat.h
@@ -18,7 +18,22 @@
  */
 #pragma once
 
+#include <osmocom/core/fsm.h>
+#include <osmocom/sigtran/sccp_sap.h>
+
+
+struct bsc_nat_ss7_inst {
+	uint32_t ss7_id;
+	struct osmo_sccp_addr local_sccp_addr;
+	struct osmo_sccp_user *scu;
+};
+
 struct bsc_nat {
+	bool shutdown;
+	struct osmo_fsm_inst *fi;
+
+	struct bsc_nat_ss7_inst *cn;
+	struct bsc_nat_ss7_inst *ran;
 };
 
 struct bsc_nat *bsc_nat_alloc(void *tall_ctx);
diff --git a/include/osmocom/bsc_nat/bsc_nat_fsm.h b/include/osmocom/bsc_nat/bsc_nat_fsm.h
new file mode 100644
index 0000000..252fd67
--- /dev/null
+++ b/include/osmocom/bsc_nat/bsc_nat_fsm.h
@@ -0,0 +1,36 @@
+/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ * Author: Oliver Smith <osmith at sysmocom.de>
+ * All Rights Reserved
+ *
+ * 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 Affero 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/lienses/>.
+ *
+ */
+#pragma once
+
+enum bsc_nat_fsm_states {
+	BSC_NAT_FSM_ST_STOPPED,
+	BSC_NAT_FSM_ST_STARTING,
+	BSC_NAT_FSM_ST_STARTED,
+	BSC_NAT_FSM_ST_STOPPING,
+};
+
+enum bsc_nat_fsm_events {
+	BSC_NAT_FSM_EV_START,
+	BSC_NAT_FSM_EV_STOP,
+};
+
+extern struct osmo_fsm bsc_nat_fsm;
+
+void bsc_nat_start(struct bsc_nat *bsc_nat);
+void bsc_nat_stop(struct bsc_nat *bsc_nat, bool exit_proc);
diff --git a/src/osmo-bsc-nat/Makefile.am b/src/osmo-bsc-nat/Makefile.am
index 9aa9524..6a759cb 100644
--- a/src/osmo-bsc-nat/Makefile.am
+++ b/src/osmo-bsc-nat/Makefile.am
@@ -26,6 +26,7 @@
 
 osmo_bsc_nat_SOURCES = \
 	bsc_nat.c \
+	bsc_nat_fsm.c \
 	logging.c \
 	main.c \
 	vty.c \
diff --git a/src/osmo-bsc-nat/bsc_nat.c b/src/osmo-bsc-nat/bsc_nat.c
index 149e7cb..e911c0f 100644
--- a/src/osmo-bsc-nat/bsc_nat.c
+++ b/src/osmo-bsc-nat/bsc_nat.c
@@ -21,6 +21,7 @@
 
 #include <osmocom/core/talloc.h>
 #include <osmocom/bsc_nat/bsc_nat.h>
+#include <osmocom/bsc_nat/bsc_nat_fsm.h>
 
 struct bsc_nat *bsc_nat_alloc(void *tall_ctx)
 {
@@ -29,10 +30,25 @@
 	bsc_nat = talloc_zero(tall_ctx, struct bsc_nat);
 	OSMO_ASSERT(bsc_nat);
 
+	bsc_nat->cn = talloc_zero(bsc_nat, struct bsc_nat_ss7_inst);
+	OSMO_ASSERT(bsc_nat->cn);
+	talloc_set_name_const(bsc_nat->cn, "struct bsc_nat_ss7_inst (CN)");
+
+	bsc_nat->ran = talloc_zero(bsc_nat, struct bsc_nat_ss7_inst);
+	OSMO_ASSERT(bsc_nat->ran);
+	talloc_set_name_const(bsc_nat->ran, "struct bsc_nat_ss7_inst (RAN)");
+
+	bsc_nat->fi = osmo_fsm_inst_alloc(&bsc_nat_fsm, bsc_nat, bsc_nat, LOGL_INFO, NULL);
+
 	return bsc_nat;
 }
 
 void bsc_nat_free(struct bsc_nat *bsc_nat)
 {
+	if (bsc_nat->fi) {
+		osmo_fsm_inst_free(bsc_nat->fi);
+		bsc_nat->fi = NULL;
+	}
+
 	talloc_free(bsc_nat);
 }
diff --git a/src/osmo-bsc-nat/bsc_nat_fsm.c b/src/osmo-bsc-nat/bsc_nat_fsm.c
new file mode 100644
index 0000000..bfaa7f7
--- /dev/null
+++ b/src/osmo-bsc-nat/bsc_nat_fsm.c
@@ -0,0 +1,213 @@
+/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ * Author: Oliver Smith <osmith at sysmocom.de>
+ * All Rights Reserved
+ *
+ * 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 Affero 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/lienses/>.
+ *
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdint.h>
+
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/select.h>
+
+#include <osmocom/sigtran/osmo_ss7.h>
+
+#include <osmocom/bsc_nat/bsc_nat.h>
+#include <osmocom/bsc_nat/bsc_nat_fsm.h>
+#include <osmocom/bsc_nat/logging.h>
+
+#define X(s) (1 << (s))
+
+#define DEFAULT_PC_RAN "0.23.1" /* same as default for OsmoMSC */
+#define DEFAULT_PC_CN "0.23.3" /* same as default for OsmoBSC */
+
+static void st_stopped(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case BSC_NAT_FSM_EV_START:
+		osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STARTING, 0, 0);
+		break;
+	}
+}
+
+static void st_stopped_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+	struct bsc_nat *bsc_nat = (struct bsc_nat *)fi->priv;
+
+	if (bsc_nat->shutdown) {
+		LOGPFSML(fi, LOGL_NOTICE, "Shutting down\n");
+		osmo_select_shutdown_request();
+	}
+}
+
+static int sccp_sap_up(struct osmo_prim_hdr *oph, void *priv)
+{
+	LOGP(DMAIN, LOGL_NOTICE, "STUB: sccp_sap_up() called\n");
+
+	return 0;
+}
+
+static int ss7_inst_init(struct bsc_nat_ss7_inst *inst, const char *name, const char *default_pc_str,
+			 enum osmo_sccp_ssn ssn)
+{
+	int default_pc;
+	struct osmo_sccp_instance *sccp;
+
+	default_pc = osmo_ss7_pointcode_parse(NULL, default_pc_str);
+	OSMO_ASSERT(default_pc >= 0);
+
+	sccp = osmo_sccp_simple_client_on_ss7_id(inst, inst->ss7_id, name, default_pc, OSMO_SS7_ASP_PROT_M3UA, 0, NULL,
+						 0, NULL);
+	if (!sccp) {
+		LOGP(DMAIN, LOGL_ERROR, "%s: failed to request sccp client instance for sccp user\n", name);
+		return -1;
+	}
+
+	osmo_sccp_local_addr_by_instance(&inst->local_sccp_addr, sccp, ssn);
+
+	inst->scu = osmo_sccp_user_bind(sccp, name, sccp_sap_up, ssn);
+	if (!inst->scu) {
+		LOGP(DMAIN, LOGL_ERROR, "%s: failed to bind sccp user\n", name);
+		return -2;
+	}
+
+	osmo_sccp_user_set_priv(inst->scu, inst);
+	return 0;
+}
+
+static void ss7_inst_free(struct bsc_nat_ss7_inst *inst)
+{
+	if (inst->scu) {
+		osmo_sccp_user_unbind(inst->scu);
+		inst->scu = NULL;
+	}
+
+	struct osmo_ss7_instance *ss7 = osmo_ss7_instance_find(inst->ss7_id);
+	if (ss7)
+		osmo_ss7_instance_destroy(ss7);
+}
+
+static void st_starting_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+	struct bsc_nat *bsc_nat = (struct bsc_nat *)fi->priv;
+
+	if (ss7_inst_init(bsc_nat->cn, "OsmoBSCNAT-CN", DEFAULT_PC_CN, OSMO_SCCP_SSN_RANAP) < 0) {
+		osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPING, 0, 0);
+		return;
+	}
+
+	if (ss7_inst_init(bsc_nat->ran, "OsmoBSCNAT-RAN", DEFAULT_PC_RAN, OSMO_SCCP_SSN_MSC) < 0) {
+		osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPING, 0, 0);
+		return;
+	}
+
+	osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STARTED, 0, 0);
+}
+
+static void st_started(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case BSC_NAT_FSM_EV_STOP:
+		osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPING, 0, 0);
+		break;
+	}
+}
+
+static void st_stopping_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
+{
+	struct bsc_nat *bsc_nat = (struct bsc_nat *)fi->priv;
+
+	ss7_inst_free(bsc_nat->cn);
+	ss7_inst_free(bsc_nat->ran);
+
+	osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPED, 0, 0);
+}
+
+static struct osmo_fsm_state bsc_nat_fsm_states[] = {
+	[BSC_NAT_FSM_ST_STOPPED] = {
+		.name = "STOPPED",
+		.in_event_mask =
+			X(BSC_NAT_FSM_EV_START),
+		.out_state_mask =
+			X(BSC_NAT_FSM_ST_STARTING),
+		.action = st_stopped,
+		.onenter = st_stopped_on_enter,
+	},
+	[BSC_NAT_FSM_ST_STARTING] = {
+		.name = "STARTING",
+		.out_state_mask =
+			X(BSC_NAT_FSM_ST_STARTED) |
+			X(BSC_NAT_FSM_ST_STOPPING),
+		.onenter = st_starting_on_enter,
+	},
+	[BSC_NAT_FSM_ST_STARTED] = {
+		.name = "STARTED",
+		.in_event_mask =
+			X(BSC_NAT_FSM_EV_STOP),
+		.out_state_mask =
+			X(BSC_NAT_FSM_ST_STOPPING),
+		.action = st_started,
+	},
+	[BSC_NAT_FSM_ST_STOPPING] = {
+		.name = "STOPPING",
+		.out_state_mask =
+			X(BSC_NAT_FSM_ST_STOPPED),
+		.onenter = st_stopping_on_enter,
+	},
+};
+
+const struct value_string bsc_nat_fsm_event_names[] = {
+	OSMO_VALUE_STRING(BSC_NAT_FSM_EV_START),
+	OSMO_VALUE_STRING(BSC_NAT_FSM_EV_STOP),
+	{ 0, NULL }
+};
+
+int bsc_nat_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+	switch (fi->state) {
+	default:
+		OSMO_ASSERT(false);
+	}
+	return 0;
+}
+
+struct osmo_fsm bsc_nat_fsm = {
+	.name = "BSC_NAT",
+	.states = bsc_nat_fsm_states,
+	.num_states = ARRAY_SIZE(bsc_nat_fsm_states),
+	.timer_cb = bsc_nat_fsm_timer_cb,
+	.log_subsys = DMAIN,
+	.event_names = bsc_nat_fsm_event_names,
+};
+
+static __attribute__((constructor)) void bsc_nat_fsm_init(void)
+{
+	OSMO_ASSERT(osmo_fsm_register(&bsc_nat_fsm) == 0);
+}
+
+void bsc_nat_start(struct bsc_nat *bsc_nat)
+{
+	osmo_fsm_inst_dispatch(bsc_nat->fi, BSC_NAT_FSM_EV_START, NULL);
+}
+
+void bsc_nat_stop(struct bsc_nat *bsc_nat, bool exit_proc)
+{
+	if (exit_proc)
+		bsc_nat->shutdown = true;
+	osmo_fsm_inst_dispatch(bsc_nat->fi, BSC_NAT_FSM_EV_STOP, NULL);
+}
diff --git a/src/osmo-bsc-nat/main.c b/src/osmo-bsc-nat/main.c
index a37d3cb..d1e32a9 100644
--- a/src/osmo-bsc-nat/main.c
+++ b/src/osmo-bsc-nat/main.c
@@ -22,6 +22,8 @@
 #include <getopt.h>
 
 #include <osmocom/core/application.h>
+#include <osmocom/sigtran/osmo_ss7.h>
+#include <osmocom/sigtran/sccp_sap.h>
 #include <osmocom/vty/cpu_sched_vty.h>
 #include <osmocom/vty/logging.h>
 #include <osmocom/vty/misc.h>
@@ -29,6 +31,7 @@
 #include <osmocom/vty/telnet_interface.h>
 
 #include <osmocom/bsc_nat/bsc_nat.h>
+#include <osmocom/bsc_nat/bsc_nat_fsm.h>
 #include <osmocom/bsc_nat/logging.h>
 #include <osmocom/bsc_nat/vty.h>
 
@@ -113,7 +116,10 @@
 
 	logging_vty_add_cmds();
 	osmo_talloc_vty_add_cmds();
+	osmo_fsm_vty_add_cmds();
 	osmo_cpu_sched_vty_init(tall_bsc_nat_ctx);
+	osmo_ss7_vty_init_asp(NULL);
+	osmo_sccp_vty_init();
 
 	handle_options(argc, argv);
 
@@ -139,9 +145,9 @@
 	case SIGINT:
 	case SIGTERM:
 		/* If SIGTERM was already sent before, just terminate immediately. */
-		if (osmo_select_shutdown_requested())
+		if (g_bsc_nat->shutdown)
 			exit(-1);
-		osmo_select_shutdown_request();
+		bsc_nat_stop(g_bsc_nat, true);
 		break;
 	case SIGABRT:
 		/* in case of abort, we want to obtain a talloc report and
@@ -185,14 +191,23 @@
 	if (rc < 0)
 		exit(1);
 
+	rc = osmo_ss7_init();
+	if (rc < 0)
+		exit(1);
+
 	g_bsc_nat = bsc_nat_alloc(tall_bsc_nat_ctx);
 
 	main_vty_init(argc, argv);
 	signal_handler_init();
 
+	bsc_nat_start(g_bsc_nat);
+
 	while (!osmo_select_shutdown_done())
 		osmo_select_main_ctx(0);
 
+	talloc_report_full(g_bsc_nat, stderr);
+	bsc_nat_free(g_bsc_nat);
+	log_fini();
 	talloc_report_full(tall_bsc_nat_ctx, stderr);
 	talloc_free(tall_bsc_nat_ctx);
 	talloc_free(tall_vty_ctx);
diff --git a/src/osmo-bsc-nat/vty.c b/src/osmo-bsc-nat/vty.c
index 089d27b..128940e 100644
--- a/src/osmo-bsc-nat/vty.c
+++ b/src/osmo-bsc-nat/vty.c
@@ -68,12 +68,36 @@
 static int config_write_bsc_nat(struct vty *vty)
 {
 	vty_out(vty, "bsc-nat%s", VTY_NEWLINE);
+	vty_out(vty, " cs7-instance-cn %u%s", g_bsc_nat->cn->ss7_id, VTY_NEWLINE);
+	vty_out(vty, " cs7-instance-ran %u%s", g_bsc_nat->ran->ss7_id, VTY_NEWLINE);
+
 	return CMD_SUCCESS;
 }
 
+#define SS7_REF_STR "SS7 instance reference number\n"
+
+DEFUN(cfg_cs7_instance_cn,
+      cfg_cs7_instance_cn_cmd,
+      "cs7-instance-cn <0-15>",
+      "Set SS7 to be used to connect to CN-side\n" SS7_REF_STR)
+{
+	g_bsc_nat->cn->ss7_id = atoi(argv[0]);
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_cs7_instance_ran,
+      cfg_cs7_instance_ran_cmd,
+      "cs7-instance-ran <0-15>",
+      "Set SS7 to be used to connect to RAN-side\n" SS7_REF_STR)
+{
+	g_bsc_nat->ran->ss7_id = atoi(argv[0]);
+	return CMD_SUCCESS;
+}
 
 void bsc_nat_vty_init(void)
 {
 	install_element(CONFIG_NODE, &cfg_bsc_nat_cmd);
 	install_node(&bsc_nat_node, config_write_bsc_nat);
+	install_element(BSC_NAT_NODE, &cfg_cs7_instance_cn_cmd);
+	install_element(BSC_NAT_NODE, &cfg_cs7_instance_ran_cmd);
 }

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-bsc-nat/+/26662
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-bsc-nat
Gerrit-Branch: master
Gerrit-Change-Id: I7d52fa649c397582b18a1a7dcc40bb407f3b2c97
Gerrit-Change-Number: 26662
Gerrit-PatchSet: 1
Gerrit-Owner: osmith <osmith at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211221/25a4cd04/attachment.htm>


More information about the gerrit-log mailing list