osmith has submitted this change. ( 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: SYS#5560 Depends: libosmocore I81c64a7ae440304522c2179c212023a566ddced8 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, 325 insertions(+), 0 deletions(-)
Approvals: Jenkins Builder: Verified pespin: Looks good to me, but someone else must approve fixeria: Looks good to me, but someone else must approve laforge: Looks good to me, approved
diff --git a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg index e69de29..c3c5825 100644 --- a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg +++ b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg @@ -0,0 +1,15 @@ +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..2031527 100644 --- a/include/osmocom/bsc_nat/bsc_nat.h +++ b/include/osmocom/bsc_nat/bsc_nat.h @@ -18,7 +18,21 @@ */ #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 { + 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..944de71 --- /dev/null +++ b/include/osmocom/bsc_nat/bsc_nat_fsm.h @@ -0,0 +1,23 @@ +/* (C) 2021 by sysmocom - s.f.m.c. GmbH info@sysmocom.de + * Author: Oliver Smith osmith@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 + +void bsc_nat_fsm_alloc(struct bsc_nat *bsc_nat); +void bsc_nat_fsm_start(struct bsc_nat *bsc_nat); +void bsc_nat_fsm_stop(struct bsc_nat *bsc_nat); 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 f3a6748..adb32c4 100644 --- a/src/osmo-bsc-nat/bsc_nat.c +++ b/src/osmo-bsc-nat/bsc_nat.c @@ -22,6 +22,7 @@ #include <osmocom/core/talloc.h> #include <osmocom/core/utils.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) { @@ -30,10 +31,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_fsm_alloc(bsc_nat); + 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..7706276 --- /dev/null +++ b/src/osmo-bsc-nat/bsc_nat_fsm.c @@ -0,0 +1,214 @@ +/* (C) 2021 by sysmocom - s.f.m.c. GmbH info@sysmocom.de + * Author: Oliver Smith osmith@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 DEFAULT_PC_RAN "0.23.1" /* same as default for OsmoMSC */ +#define DEFAULT_PC_CN "0.23.3" /* same as default for OsmoBSC */ + +#define X(s) (1 << (s)) + +enum bsc_nat_fsm_states { + BSC_NAT_FSM_ST_STOPPED, + BSC_NAT_FSM_ST_STARTING, + BSC_NAT_FSM_ST_STARTED, +}; + +enum bsc_nat_fsm_events { + BSC_NAT_FSM_EV_START, + BSC_NAT_FSM_EV_STOP, +}; + +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); + + talloc_free(inst); +} + +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_STOPPED, 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_STOPPED, 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_STOPPED, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +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; + + ss7_inst_free(bsc_nat->cn); + bsc_nat->cn = NULL; + + ss7_inst_free(bsc_nat->ran); + bsc_nat->ran = NULL; +} + +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; + default: + OSMO_ASSERT(false); + } +} + +static struct osmo_fsm_state bsc_nat_fsm_states[] = { + [BSC_NAT_FSM_ST_STOPPED] = { + .name = "STOPPED", + .in_event_mask = 0 + | X(BSC_NAT_FSM_EV_START) + , + .out_state_mask = 0 + | X(BSC_NAT_FSM_ST_STARTING) + , + .action = st_stopped, + .onenter = st_stopped_on_enter, + }, + [BSC_NAT_FSM_ST_STARTING] = { + .name = "STARTING", + .out_state_mask = 0 + | X(BSC_NAT_FSM_ST_STARTED) + | X(BSC_NAT_FSM_ST_STOPPED) + , + .onenter = st_starting_on_enter, + }, + [BSC_NAT_FSM_ST_STARTED] = { + .name = "STARTED", + .in_event_mask = 0 + | X(BSC_NAT_FSM_EV_STOP) + , + .out_state_mask = 0 + | X(BSC_NAT_FSM_ST_STOPPED) + , + .action = st_started, + }, +}; + +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 } +}; + +struct osmo_fsm bsc_nat_fsm = { + .name = "BSC_NAT", + .states = bsc_nat_fsm_states, + .num_states = ARRAY_SIZE(bsc_nat_fsm_states), + .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_fsm_alloc(struct bsc_nat *bsc_nat) +{ + bsc_nat->fi = osmo_fsm_inst_alloc(&bsc_nat_fsm, bsc_nat, bsc_nat, LOGL_INFO, NULL); + OSMO_ASSERT(bsc_nat->fi); +} + +void bsc_nat_fsm_start(struct bsc_nat *bsc_nat) +{ + osmo_fsm_inst_dispatch(bsc_nat->fi, BSC_NAT_FSM_EV_START, NULL); +} + +void bsc_nat_fsm_stop(struct bsc_nat *bsc_nat) +{ + 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..715ade0 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);
@@ -185,14 +191,25 @@ 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_fsm_start(g_bsc_nat); + while (!osmo_select_shutdown_done()) osmo_select_main_ctx(0);
+ bsc_nat_fsm_stop(g_bsc_nat); + + 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); }