From 6adeb607d0a932fcbc07b1daada432e77490e7ce Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 30 Sep 2020 12:52:53 +0200 Subject: Introduce NM BTS FSM Change-Id: I7756a8ce90b6cc8a502b5665889a7987d7f749cb --- src/osmo-bsc/Makefile.am | 1 + src/osmo-bsc/abis_nm.c | 10 +- src/osmo-bsc/bts.c | 7 + src/osmo-bsc/bts_ipaccess_nanobts.c | 40 ++-- src/osmo-bsc/nm_bts_fsm.c | 353 ++++++++++++++++++++++++++++++++++++ src/osmo-bsc/nm_common_fsm.c | 1 + 6 files changed, 400 insertions(+), 12 deletions(-) create mode 100644 src/osmo-bsc/nm_bts_fsm.c (limited to 'src/osmo-bsc') diff --git a/src/osmo-bsc/Makefile.am b/src/osmo-bsc/Makefile.am index ffa39a03e..4584b2c1d 100644 --- a/src/osmo-bsc/Makefile.am +++ b/src/osmo-bsc/Makefile.am @@ -78,6 +78,7 @@ osmo_bsc_SOURCES = \ net_init.c \ nm_common_fsm.c \ nm_bts_sm_fsm.c \ + nm_bts_fsm.c \ gsm_08_08.c \ osmo_bsc_bssap.c \ osmo_bsc_ctrl.c \ diff --git a/src/osmo-bsc/abis_nm.c b/src/osmo-bsc/abis_nm.c index 511c4b3e2..54dfccbbf 100644 --- a/src/osmo-bsc/abis_nm.c +++ b/src/osmo-bsc/abis_nm.c @@ -842,6 +842,14 @@ static int abis_nm_rx_set_radio_attr_ack(struct msgb *mb) return 0; } +static int abis_nm_rx_set_bts_attr_ack(struct msgb *mb) +{ + struct abis_om_fom_hdr *foh = msgb_l3(mb); + DEBUGPFOH(DNM, foh, "Set BTS Attributes ACK\n"); + osmo_signal_dispatch(SS_NM, S_NM_SET_BTS_ATTR_ACK, mb); + return 0; +} + bool all_trx_rsl_connected_unlocked(const struct gsm_bts *bts) { const struct gsm_bts_trx *trx; @@ -978,7 +986,7 @@ static int abis_nm_rcvmsg_fom(struct msgb *mb) osmo_signal_dispatch(SS_NM, S_NM_IPACC_RESTART_NACK, NULL); break; case NM_MT_SET_BTS_ATTR_ACK: - DEBUGPFOH(DNM, foh, "Set BTS Attribute ACK\n"); + abis_nm_rx_set_bts_attr_ack(mb); break; case NM_MT_GET_ATTR_RESP: ret = abis_nm_rx_get_attr_resp(mb, gsm_bts_trx_num(bts, (foh)->obj_inst.trx_nr)); diff --git a/src/osmo-bsc/bts.c b/src/osmo-bsc/bts.c index fe1641230..3dd827e5b 100644 --- a/src/osmo-bsc/bts.c +++ b/src/osmo-bsc/bts.c @@ -153,6 +153,10 @@ static int gsm_bts_talloc_destructor(struct gsm_bts *bts) osmo_fsm_inst_free(bts->site_mgr.mo.fi); bts->site_mgr.mo.fi = NULL; } + if (bts->mo.fi) { + osmo_fsm_inst_free(bts->mo.fi); + bts->mo.fi = NULL; + } return 0; } @@ -183,6 +187,9 @@ struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num) osmo_fsm_inst_update_id_f(bts->site_mgr.mo.fi, "bts_sm"); gsm_mo_init(&bts->site_mgr.mo, bts, NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff); + bts->mo.fi = osmo_fsm_inst_alloc(&nm_bts_fsm, bts, bts, + LOGL_INFO, NULL); + osmo_fsm_inst_update_id_f(bts->mo.fi, "bts%d", bts->nr); gsm_mo_init(&bts->mo, bts, NM_OC_BTS, bts->nr, 0xff, 0xff); for (i = 0; i < ARRAY_SIZE(bts->gprs.nsvc); i++) { diff --git a/src/osmo-bsc/bts_ipaccess_nanobts.c b/src/osmo-bsc/bts_ipaccess_nanobts.c index 457448cdd..bb05236f3 100644 --- a/src/osmo-bsc/bts_ipaccess_nanobts.c +++ b/src/osmo-bsc/bts_ipaccess_nanobts.c @@ -143,7 +143,7 @@ static int nm_statechg_event(int evt, struct nm_statechg_signal_data *nsd) /* S_NM_STATECHG_ADM is called after we call chg_adm_state() and would create * endless loop */ - if (evt != S_NM_STATECHG_OPER) + if (obj_class != NM_OC_BTS && evt != S_NM_STATECHG_OPER) return 0; switch (obj_class) { @@ -153,16 +153,7 @@ static int nm_statechg_event(int evt, struct nm_statechg_signal_data *nsd) break; case NM_OC_BTS: bts = obj; - if (new_state->availability == NM_AVSTATE_DEPENDENCY) { - msgb = nanobts_attr_bts_get(bts); - abis_nm_set_bts_attr(bts, msgb->data, msgb->len); - msgb_free(msgb); - abis_nm_chg_adm_state(bts, obj_class, - bts->bts_nr, 0xff, 0xff, - NM_STATE_UNLOCKED); - abis_nm_opstart(bts, obj_class, - bts->bts_nr, 0xff, 0xff); - } + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_STATE_CHG_REP, nsd); break; case NM_OC_CHANNEL: ts = obj; @@ -265,6 +256,9 @@ static int sw_activ_rep(struct msgb *mb) case NM_OC_SITE_MANAGER: osmo_fsm_inst_dispatch(bts->site_mgr.mo.fi, NM_EV_SW_ACT_REP, NULL); break; + case NM_OC_BTS: + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_SW_ACT_REP, NULL); + break; case NM_OC_BASEB_TRANSC: if (!(trx = gsm_bts_trx_num(bts, foh->obj_inst.trx_nr))) return -EINVAL; @@ -326,6 +320,9 @@ static void nm_rx_opstart_ack(struct msgb *oml_msg) case NM_OC_SITE_MANAGER: osmo_fsm_inst_dispatch(bts->site_mgr.mo.fi, NM_EV_OPSTART_ACK, NULL); break; + case NM_OC_BTS: + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_OPSTART_ACK, NULL); + break; case NM_OC_CHANNEL: nm_rx_opstart_ack_chan(oml_msg); break; @@ -343,11 +340,28 @@ static void nm_rx_opstart_nack(struct msgb *oml_msg) case NM_OC_SITE_MANAGER: osmo_fsm_inst_dispatch(bts->site_mgr.mo.fi, NM_EV_OPSTART_NACK, NULL); break; + case NM_OC_BTS: + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_OPSTART_ACK, NULL); + break; default: break; } } +static void nm_rx_set_bts_attr_ack(struct msgb *oml_msg) +{ + struct abis_om_fom_hdr *foh = msgb_l3(oml_msg); + struct e1inp_sign_link *sign_link = oml_msg->dst; + struct gsm_bts *bts = sign_link->trx->bts; + + if (foh->obj_class != NM_OC_BTS) { + LOG_BTS(bts, DNM, LOGL_ERROR, "Set BTS Attr Ack received on non BTS object!\n"); + return; + } + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_SET_ATTR_ACK, NULL); +} + + static void nm_rx_set_radio_attr_ack(struct msgb *oml_msg) { struct abis_om_fom_hdr *foh = msgb_l3(oml_msg); @@ -382,6 +396,9 @@ static int bts_ipa_nm_sig_cb(unsigned int subsys, unsigned int signal, case S_NM_OPSTART_NACK: nm_rx_opstart_nack(signal_data); return 0; + case S_NM_SET_BTS_ATTR_ACK: + nm_rx_set_bts_attr_ack(signal_data); + return 0; case S_NM_SET_RADIO_ATTR_ACK: nm_rx_set_radio_attr_ack(signal_data); return 0; @@ -467,6 +484,7 @@ void ipaccess_drop_oml(struct gsm_bts *bts, const char *reason) ipaccess_drop_rsl(trx, "OML link drop"); osmo_fsm_inst_dispatch(bts->site_mgr.mo.fi, NM_EV_OML_DOWN, NULL); + osmo_fsm_inst_dispatch(bts->mo.fi, NM_EV_OML_DOWN, NULL); gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL); bts->ip_access.flags = 0; diff --git a/src/osmo-bsc/nm_bts_fsm.c b/src/osmo-bsc/nm_bts_fsm.c new file mode 100644 index 000000000..fa4235166 --- /dev/null +++ b/src/osmo-bsc/nm_bts_fsm.c @@ -0,0 +1,353 @@ +/* NM BTS FSM */ + +/* (C) 2020 by sysmocom - s.m.f.c. GmbH + * Author: Pau Espin Pedrol + * + * 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 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 . + * + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define X(s) (1 << (s)) + +#define nm_bts_fsm_state_chg(fi, NEXT_STATE) \ + osmo_fsm_inst_state_chg(fi, NEXT_STATE, 0, 0) + +////////////////////////// +// FSM STATE ACTIONS +////////////////////////// + +static void st_op_disabled_notinstalled_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + + bts->mo.set_attr_sent = false; + bts->mo.set_attr_ack_received = false; + bts->mo.adm_unlock_sent = false; + bts->mo.opstart_sent = false; +} + +static void st_op_disabled_notinstalled(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct nm_statechg_signal_data *nsd; + struct gsm_nm_state *new_state; + + switch (event) { + case NM_EV_SW_ACT_REP: + break; + case NM_EV_STATE_CHG_REP: + nsd = (struct nm_statechg_signal_data *)data; + new_state = nsd->new_state; + if (new_state->operational == NM_OPSTATE_ENABLED) { + /*should not happen... */ + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_ENABLED); + return; + } + switch (new_state->availability) { /* operational = DISABLED */ + case NM_AVSTATE_DEPENDENCY: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_DEPENDENCY); + return; + case NM_AVSTATE_OFF_LINE: + case NM_AVSTATE_OK: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_OFFLINE); + return; + default: + return; + } + default: + OSMO_ASSERT(0); + } +} + +static void configure_loop(struct gsm_bts *bts, struct gsm_nm_state *state, bool allow_opstart) { + struct msgb *msgb; + + if (!bts->mo.set_attr_sent && !bts->mo.set_attr_ack_received) { + bts->mo.set_attr_sent = true; + msgb = nanobts_attr_bts_get(bts); + abis_nm_set_bts_attr(bts, msgb->data, msgb->len); + msgb_free(msgb); + } + + if (state->administrative != NM_STATE_UNLOCKED && !bts->mo.adm_unlock_sent) { + bts->mo.adm_unlock_sent = true; + abis_nm_chg_adm_state(bts, NM_OC_BTS, + bts->bts_nr, 0xff, 0xff, + NM_STATE_UNLOCKED); + } + + if (allow_opstart && state->administrative == NM_STATE_UNLOCKED && + bts->mo.set_attr_ack_received) { + if (!bts->mo.opstart_sent) { + bts->mo.opstart_sent = true; + abis_nm_opstart(bts, NM_OC_BTS, bts->bts_nr, 0xff, 0xff); + } + } +} + +static void st_op_disabled_dependency_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + + /* nanoBTS is broken, doesn't follow TS 12.21. Opstart MUST be sent + during Dependency, so we simply move to OFFLINE state here to avoid + duplicating code */ + if (bts->type == GSM_BTS_TYPE_NANOBTS) { + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_OFFLINE); + return; + } + configure_loop(bts, &bts->mo.nm_state, false); +} + +static void st_op_disabled_dependency(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + struct nm_statechg_signal_data *nsd; + struct gsm_nm_state *new_state; + + switch (event) { + case NM_EV_SET_ATTR_ACK: + bts->mo.set_attr_ack_received = true; + bts->mo.set_attr_sent = false; + configure_loop(bts, &bts->mo.nm_state, false); + return; + case NM_EV_STATE_CHG_REP: + nsd = (struct nm_statechg_signal_data *)data; + new_state = nsd->new_state; + if (new_state->operational == NM_OPSTATE_ENABLED) { + /* should not happen... */ + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_ENABLED); + return; + } + switch (new_state->availability) { /* operational = DISABLED */ + case NM_AVSTATE_NOT_INSTALLED: + case NM_AVSTATE_POWER_OFF: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_NOTINSTALLED); + return; + case NM_AVSTATE_OFF_LINE: + case NM_AVSTATE_OK: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_OFFLINE); + return; + case NM_AVSTATE_DEPENDENCY: + configure_loop(bts, new_state, false); + return; + default: + return; + } + default: + OSMO_ASSERT(0); + } +} + +static void st_op_disabled_offline_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + + /* Warning: In here we may be acessing an state older than new_state + from prev (syncrhonous) FSM state */ + configure_loop(bts, &bts->mo.nm_state, true); +} + +static void st_op_disabled_offline(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + struct nm_statechg_signal_data *nsd; + struct gsm_nm_state *new_state; + + switch (event) { + case NM_EV_SET_ATTR_ACK: + bts->mo.set_attr_ack_received = true; + bts->mo.set_attr_sent = false; + configure_loop(bts, &bts->mo.nm_state, true); + return; + case NM_EV_STATE_CHG_REP: + nsd = (struct nm_statechg_signal_data *)data; + new_state = nsd->new_state; + if (new_state->operational == NM_OPSTATE_ENABLED) { + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_ENABLED); + return; + } + switch (new_state->availability) { /* operational = DISABLED */ + case NM_AVSTATE_NOT_INSTALLED: + case NM_AVSTATE_POWER_OFF: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_NOTINSTALLED); + return; + case NM_AVSTATE_DEPENDENCY: + /* There's no point in moving back to Dependency in nanoBTS, since it's broken + and it acts actually as if it was in Offline state */ + if (bts->type != GSM_BTS_TYPE_NANOBTS) { + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_DEPENDENCY); + } else { + /* Moreover, in nanoBTS we need to check here for tx + Opstart since we may have gone Unlocked state + in this event, which means Opstart may be txed here. */ + configure_loop(bts, new_state, true); + } + return; + case NM_AVSTATE_OFF_LINE: + case NM_AVSTATE_OK: + configure_loop(bts, new_state, true); + return; + default: + return; + } + default: + OSMO_ASSERT(0); + } +} + +static void st_op_enabled_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + + /* Reset state, we don't need it in this state and it will need to be + reused as soon as we move back to Disabled */ + bts->mo.opstart_sent = false; + bts->mo.adm_unlock_sent = false; + bts->mo.set_attr_sent = false; + bts->mo.set_attr_ack_received = false; +} + +static void st_op_enabled(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct nm_statechg_signal_data *nsd; + struct gsm_nm_state *new_state; + + switch (event) { + case NM_EV_STATE_CHG_REP: + nsd = (struct nm_statechg_signal_data *)data; + new_state = nsd->new_state; + if (new_state->operational == NM_OPSTATE_ENABLED) + return; + switch (new_state->availability) { /* operational = DISABLED */ + case NM_AVSTATE_NOT_INSTALLED: + case NM_AVSTATE_POWER_OFF: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_NOTINSTALLED); + return; + case NM_AVSTATE_DEPENDENCY: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_DEPENDENCY); + return; + case NM_AVSTATE_OFF_LINE: + case NM_AVSTATE_OK: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_OFFLINE); + return; + default: + return; + } + default: + OSMO_ASSERT(0); + } +} + +static void st_op_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct gsm_bts *bts = (struct gsm_bts *)fi->priv; + + switch (event) { + case NM_EV_OPSTART_ACK: + case NM_EV_OPSTART_NACK: + /* TODO: if on state OFFLINE and rx NACK, try again? */ + bts->mo.opstart_sent = false; + break; + case NM_EV_OML_DOWN: + nm_bts_fsm_state_chg(fi, NM_BTS_ST_OP_DISABLED_NOTINSTALLED); + break; + default: + OSMO_ASSERT(0); + } +} + +static struct osmo_fsm_state nm_bts_fsm_states[] = { + [NM_BTS_ST_OP_DISABLED_NOTINSTALLED] = { + .in_event_mask = + X(NM_EV_SW_ACT_REP) | + X(NM_EV_STATE_CHG_REP), + .out_state_mask = + X(NM_BTS_ST_OP_DISABLED_DEPENDENCY) | + X(NM_BTS_ST_OP_DISABLED_OFFLINE) | + X(NM_BTS_ST_OP_ENABLED), + .name = "DISABLED_NOTINSTALLED", + .onenter = st_op_disabled_notinstalled_on_enter, + .action = st_op_disabled_notinstalled, + }, + [NM_BTS_ST_OP_DISABLED_DEPENDENCY] = { + .in_event_mask = + X(NM_EV_STATE_CHG_REP) | + X(NM_EV_SET_ATTR_ACK), + .out_state_mask = + X(NM_BTS_ST_OP_DISABLED_NOTINSTALLED) | + X(NM_BTS_ST_OP_DISABLED_OFFLINE) | + X(NM_BTS_ST_OP_ENABLED), + .name = "DISABLED_DEPENDENCY", + .onenter = st_op_disabled_dependency_on_enter, + .action = st_op_disabled_dependency, + }, + [NM_BTS_ST_OP_DISABLED_OFFLINE] = { + .in_event_mask = + X(NM_EV_STATE_CHG_REP) | + X(NM_EV_SET_ATTR_ACK), + .out_state_mask = + X(NM_BTS_ST_OP_DISABLED_NOTINSTALLED) | + X(NM_BTS_ST_OP_DISABLED_DEPENDENCY) | + X(NM_BTS_ST_OP_ENABLED), + .name = "DISABLED_OFFLINE", + .onenter = st_op_disabled_offline_on_enter, + .action = st_op_disabled_offline, + }, + [NM_BTS_ST_OP_ENABLED] = { + .in_event_mask = + X(NM_EV_STATE_CHG_REP), + .out_state_mask = + X(NM_BTS_ST_OP_DISABLED_NOTINSTALLED) | + X(NM_BTS_ST_OP_DISABLED_DEPENDENCY) | + X(NM_BTS_ST_OP_DISABLED_OFFLINE), + .name = "ENABLED", + .onenter = st_op_enabled_on_enter, + .action = st_op_enabled, + }, +}; + +struct osmo_fsm nm_bts_fsm = { + .name = "NM_BTS_OP", + .states = nm_bts_fsm_states, + .num_states = ARRAY_SIZE(nm_bts_fsm_states), + .allstate_event_mask = + X(NM_EV_OPSTART_ACK) | + X(NM_EV_OPSTART_NACK) | + X(NM_EV_OML_DOWN), + .allstate_action = st_op_allstate, + .event_names = nm_fsm_event_names, + .log_subsys = DNM, +}; + +static __attribute__((constructor)) void nm_bts_fsm_init(void) +{ + OSMO_ASSERT(osmo_fsm_register(&nm_bts_fsm) == 0); +} diff --git a/src/osmo-bsc/nm_common_fsm.c b/src/osmo-bsc/nm_common_fsm.c index 9851f5d95..f8aa779cc 100644 --- a/src/osmo-bsc/nm_common_fsm.c +++ b/src/osmo-bsc/nm_common_fsm.c @@ -25,6 +25,7 @@ const struct value_string nm_fsm_event_names[] = { { NM_EV_SW_ACT_REP, "SW_ACT_REP" }, { NM_EV_STATE_CHG_REP, "STATE_CHG_REP" }, + { NM_EV_SET_ATTR_ACK, "SET_ATTR_ACK" }, { NM_EV_OPSTART_ACK, "OPSTART_ACK" }, { NM_EV_OPSTART_NACK, "OPSTART_NACK" }, { NM_EV_OML_DOWN, "OML_DOWN" }, -- cgit v1.2.3