From 1c352e523167361495312721ec628b7921bb0101 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 14 May 2018 18:14:15 +0200 Subject: large refactoring: use FSMs for lchans; add inter-BSC HO Add FSMs: - timeslot_fsm: handle dynamic timeslots and OML+RSL availability. - lchan_fsm: handle an individual lchan activation, RTP stream and release, signal the appropriate calling FSMs on success, failure, release. - mgw_endpoint_fsm: handle one entire endpoint with several CI. - assignment_fsm: BSSMAP Assignment Request. - handover_fsm: all of intra, inter-MO and inter-MT handover. Above FSMs absorb large parts of the gscon FSM. The gscon FSM was surpassing the maximum amount events (32), and it is more logical to treat assignment, handover and MGW procedures in separate FSMs. - Add logging macros for each FSM type: - LOG_TS() - LOG_LCHAN() - LOG_MGWEP(), LOG_CI() - LOG_ASSIGNMENT() - LOG_HO() These log with the osmo_fsm_inst where present. New style decision: logging without a final newline char is awkward, especially for gsmtap logging and when other logs interleave LOGPC() calls; we have various cases where the final \n goes missing, and also this invokes the log category checking N times instead of once. So I decided to make these macros *always* append a newline, but only if there is no final newline yet. I hope that the compiler optimizes the strlen() of the constant format strings away. Thus I can log with or without typing "\n" and always get an \n termination anyway. General: - replace osmo_timers, state enums and program-wide osmo_signal_dispatch() with dedicated FSM timeouts, states and events. - introduce a common way to handle Tnnn timers: gsm_timers.h/.c: struct T_def. These can be used (with some macro magic) to define a state's timeout once, and not make mistakes for each osmo_fsm_inst_state_chg(). Details: bsc_subscr_conn_fsm.c: - move most states of this FSM to lchan_fsm, assignment_fsm, handover_fsm and mgw_endpoint_fsm. - There is exactly one state for an ongoing Assignment, with all details handled in conn->assignment.fi. The state relies on the assignment_fsm's timeout. - There is one state for an ongoing Handover; except for an incoming Handover from a remote BSS, the gscon remains in ST_INIT until the new lchan and conn are both established. - move bssmap_add_lcls_status() to osmo_bsc_lcls.c abis_rsl.c: - move all dynamic timeslot logic away into timeslot_fsm. Only keep plain send/receive functions in abis_rsl.c - reduce some rsl functions to merely send a message, rename to "_tx_". - rsl_ipacc_mdcx(): add '_tx_' in the name; move parts that change the lchan state out into the lchan_fsm, the lchan->abis_ip.* are now set there prior to invoking this function. - move all timers and error/release handling away into various FSMs. - tweak ipa_smod_s_for_lchan() and ipa_rtp_pt_for_lchan() to not require an lchan passed, but just mode,type that they require. Rename to ipacc_speech_mode*() and ipacc_payload_type(). - add rsl_forward_layer3_info, used for inter-BSC HO MO, to just send the RR message received during BSSMAP Handover Command. - move various logging to LOG_LCHAN() in order to log with the lchan FSM instance. One drawback is that the lchan FSM is limited to one logging category, i.e. this moves some logging from DRR to DRSL. It might actually make sense to combine those categories. - lose LOGP...LOGPC logging cascades: they are bad for gsmtap logging and for performance. - handle_classmark_chg(): change logging, move cm2 len check out of the cm3 condition (I hope that's correct). - gsm48_send_ho_cmd(): split off gsm48_make_ho_cmd() which doesn't send right away, so that during inter-bsc HO we can make an RR Handover Command to send via the MSC to the remote BSS. assignment_fsm.c: - the Chan Mode Modify in case of re-using the same lchan is not implemented yet, because this was also missing in the previous implementation. Depends: I03ee7ce840ecfa0b6a33358e7385528aabd4873f Change-Id: I82e3f918295daa83274a4cf803f046979f284366 --- include/osmocom/bsc/Makefile.am | 6 + include/osmocom/bsc/abis_rsl.h | 31 +- include/osmocom/bsc/assignment_fsm.h | 42 + include/osmocom/bsc/bsc_api.h | 6 +- include/osmocom/bsc/bsc_msc_data.h | 5 - include/osmocom/bsc/bsc_subscr_conn_fsm.h | 67 +- include/osmocom/bsc/bsc_subscriber.h | 1 + include/osmocom/bsc/chan_alloc.h | 9 - include/osmocom/bsc/debug.h | 2 + include/osmocom/bsc/gsm_data.h | 364 ++++-- include/osmocom/bsc/handover.h | 80 +- include/osmocom/bsc/handover_fsm.h | 81 ++ include/osmocom/bsc/lchan_fsm.h | 87 ++ include/osmocom/bsc/lchan_select.h | 6 + include/osmocom/bsc/mgw_endpoint_fsm.h | 60 + include/osmocom/bsc/neighbor_ident.h | 5 +- include/osmocom/bsc/osmo_bsc.h | 14 +- include/osmocom/bsc/osmo_bsc_lcls.h | 1 + include/osmocom/bsc/timeslot_fsm.h | 50 + src/ipaccess/Makefile.am | 2 +- src/ipaccess/stubs.c | 3 + src/osmo-bsc/Makefile.am | 8 +- src/osmo-bsc/abis_om2000.c | 16 +- src/osmo-bsc/abis_rsl.c | 1741 ++++++-------------------- src/osmo-bsc/assignment_fsm.c | 638 ++++++++++ src/osmo-bsc/bsc_api.c | 249 +--- src/osmo-bsc/bsc_dyn_ts.c | 60 - src/osmo-bsc/bsc_subscr_conn_fsm.c | 1141 ++++++----------- src/osmo-bsc/bsc_subscriber.c | 14 + src/osmo-bsc/bsc_vty.c | 291 +++-- src/osmo-bsc/bts_ericsson_rbs2000.c | 3 +- src/osmo-bsc/bts_ipaccess_nanobts.c | 11 +- src/osmo-bsc/bts_nokia_site.c | 35 +- src/osmo-bsc/bts_siemens_bs11.c | 5 +- src/osmo-bsc/chan_alloc.c | 579 +-------- src/osmo-bsc/gsm_04_08_utils.c | 35 - src/osmo-bsc/gsm_data.c | 667 +++++++--- src/osmo-bsc/handover_decision.c | 75 +- src/osmo-bsc/handover_decision_2.c | 189 ++- src/osmo-bsc/handover_fsm.c | 1124 +++++++++++++++++ src/osmo-bsc/handover_logic.c | 455 +------ src/osmo-bsc/lchan_fsm.c | 1467 ++++++++++++++++++++++ src/osmo-bsc/lchan_select.c | 260 ++++ src/osmo-bsc/meas_feed.c | 2 +- src/osmo-bsc/mgw_endpoint_fsm.c | 748 +++++++++++ src/osmo-bsc/neighbor_ident_vty.c | 20 +- src/osmo-bsc/osmo_bsc_api.c | 271 +++- src/osmo-bsc/osmo_bsc_audio.c | 98 -- src/osmo-bsc/osmo_bsc_bssap.c | 438 ++++--- src/osmo-bsc/osmo_bsc_filter.c | 205 --- src/osmo-bsc/osmo_bsc_grace.c | 3 +- src/osmo-bsc/osmo_bsc_lcls.c | 80 +- src/osmo-bsc/osmo_bsc_main.c | 26 +- src/osmo-bsc/osmo_bsc_sigtran.c | 51 +- src/osmo-bsc/paging.c | 2 + src/osmo-bsc/pcu_sock.c | 31 +- src/osmo-bsc/system_information.c | 12 +- src/osmo-bsc/timeslot_fsm.c | 859 +++++++++++++ src/utils/Makefile.am | 2 - src/utils/bs11_config.c | 3 + src/utils/meas_json.c | 3 + src/utils/stubs.c | 36 - tests/abis/abis_test.c | 1 + tests/bsc/Makefile.am | 12 +- tests/bsc/bsc_test.c | 2 + tests/gsm0408/gsm0408_test.c | 2 + tests/handover/Makefile.am | 14 +- tests/handover/handover_test.c | 153 ++- tests/handover/neighbor_ident_test.c | 8 +- tests/nanobts_omlattr/nanobts_omlattr_test.c | 2 + 70 files changed, 8251 insertions(+), 4818 deletions(-) create mode 100644 include/osmocom/bsc/assignment_fsm.h create mode 100644 include/osmocom/bsc/handover_fsm.h create mode 100644 include/osmocom/bsc/lchan_fsm.h create mode 100644 include/osmocom/bsc/lchan_select.h create mode 100644 include/osmocom/bsc/mgw_endpoint_fsm.h create mode 100644 include/osmocom/bsc/timeslot_fsm.h create mode 100644 src/osmo-bsc/assignment_fsm.c delete mode 100644 src/osmo-bsc/bsc_dyn_ts.c create mode 100644 src/osmo-bsc/handover_fsm.c create mode 100644 src/osmo-bsc/lchan_fsm.c create mode 100644 src/osmo-bsc/lchan_select.c create mode 100644 src/osmo-bsc/mgw_endpoint_fsm.c delete mode 100644 src/osmo-bsc/osmo_bsc_audio.c create mode 100644 src/osmo-bsc/timeslot_fsm.c delete mode 100644 src/utils/stubs.c diff --git a/include/osmocom/bsc/Makefile.am b/include/osmocom/bsc/Makefile.am index 82a2e26cd..49d35e602 100644 --- a/include/osmocom/bsc/Makefile.am +++ b/include/osmocom/bsc/Makefile.am @@ -5,6 +5,7 @@ noinst_HEADERS = \ abis_rsl.h \ acc_ramp.h \ arfcn_range_encode.h \ + assignment_fsm.h \ bsc_msg_filter.h \ bsc_rll.h \ bsc_subscriber.h \ @@ -23,11 +24,15 @@ noinst_HEADERS = \ handover_cfg.h \ handover_decision.h \ handover_decision_2.h \ + handover_fsm.h \ handover_vty.h \ ipaccess.h \ + lchan_fsm.h \ + lchan_select.h \ meas_feed.h \ meas_rep.h \ misdn.h \ + mgw_endpoint_fsm.h \ neighbor_ident.h \ network_listen.h \ openbscdefines.h \ @@ -44,6 +49,7 @@ noinst_HEADERS = \ rs232.h \ signal.h \ system_information.h \ + timeslot_fsm.h \ ussd.h \ vty.h \ bsc_api.h \ diff --git a/include/osmocom/bsc/abis_rsl.h b/include/osmocom/bsc/abis_rsl.h index c04c61a9f..1177b373c 100644 --- a/include/osmocom/bsc/abis_rsl.h +++ b/include/osmocom/bsc/abis_rsl.h @@ -35,19 +35,20 @@ struct gsm_bts_trx_ts; #define GSM48_LEN2PLEN(a) (((a) << 2) | 1) -#define rsl_lchan_set_state(lch_, st_) \ - rsl_lchan_set_state_with_log(lch_, st_, __BASE_FILE__, __LINE__) +int gsm48_sendmsg(struct msgb *msg); int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len); int rsl_sacch_filling(struct gsm_bts_trx *trx, uint8_t type, const uint8_t *data, int len); -int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, - uint8_t ho_ref); +int rsl_tx_chan_activ(struct gsm_lchan *lchan, uint8_t act_type, uint8_t ho_ref); int rsl_chan_mode_modify_req(struct gsm_lchan *ts); int rsl_encryption_cmd(struct msgb *msg); int rsl_paging_cmd(struct gsm_bts *bts, uint8_t paging_group, uint8_t len, uint8_t *ms_ident, uint8_t chan_needed, bool is_gprs); int rsl_imm_assign_cmd(struct gsm_bts *bts, uint8_t len, uint8_t *val); +int rsl_tx_imm_assignment(struct gsm_lchan *lchan); +int rsl_tx_imm_ass_rej(struct gsm_bts *bts, struct gsm48_req_ref *rqd_ref); +int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, uint8_t power_command); int rsl_data_request(struct msgb *msg, uint8_t link_id); int rsl_establish_request(struct gsm_lchan *lchan, uint8_t link_id); @@ -60,9 +61,8 @@ int rsl_ericsson_imm_assign_cmd(struct gsm_bts *bts, uint32_t tlli, uint8_t len, int rsl_siemens_mrpci(struct gsm_lchan *lchan, struct rsl_mrpci *mrpci); /* ip.access specfic RSL extensions */ -int rsl_ipacc_crcx(struct gsm_lchan *lchan); -int rsl_ipacc_mdcx(struct gsm_lchan *lchan, uint32_t ip, - uint16_t port, uint8_t rtp_payload2); +int rsl_tx_ipacc_crcx(struct gsm_lchan *lchan); +int rsl_tx_ipacc_mdcx(struct gsm_lchan *lchan); int rsl_ipacc_mdcx_to_rtpsock(struct gsm_lchan *lchan); int rsl_ipacc_pdch_activate(struct gsm_bts_trx_ts *ts, int act); @@ -71,7 +71,6 @@ int abis_rsl_rcvmsg(struct msgb *msg); int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id, enum rsl_rel_mode release_mode); -int rsl_lchan_set_state_with_log(struct gsm_lchan *lchan, enum gsm_lchan_state state, const char *file, unsigned line); int rsl_lchan_mark_broken(struct gsm_lchan *lchan, const char *broken); /* to be provided by external code */ @@ -106,9 +105,15 @@ int rsl_start_t3109(struct gsm_lchan *lchan); int rsl_direct_rf_release(struct gsm_lchan *lchan); -void dyn_ts_init(struct gsm_bts_trx_ts *ts); -int dyn_ts_switchover_start(struct gsm_bts_trx_ts *ts, - enum gsm_phys_chan_config to_pchan); +int rsl_tx_dyn_ts_pdch_act_deact(struct gsm_bts_trx_ts *ts, bool activate); + +int rsl_forward_layer3_info(struct gsm_lchan *lchan, const uint8_t *l3_info, uint8_t l3_info_len); + +int ipacc_speech_mode(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type); +void ipacc_speech_mode_set_direction(uint8_t *speech_mode, bool send); +int ipacc_payload_type(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type); + +int rsl_tx_rf_chan_release(struct gsm_lchan *lchan); struct msgb *gsm48_create_rr_status(uint8_t cause); int gsm48_tx_rr_status(struct gsm_subscriber_connection *conn, uint8_t cause); @@ -120,9 +125,11 @@ int send_siemens_mrpci(struct gsm_lchan *lchan, int gsm48_rx_rr_modif_ack(struct msgb *msg); int gsm48_send_rr_release(struct gsm_lchan *lchan); int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv); + +struct msgb *gsm48_make_ho_cmd(struct gsm_lchan *new_lchan, uint8_t power_command, uint8_t ho_ref); int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan, uint8_t power_command, uint8_t ho_ref); -int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, uint8_t power_command); +int gsm48_rx_rr_modif_ack(struct msgb *msg); int gsm48_lchan_modify(struct gsm_lchan *lchan, uint8_t mode); #endif /* RSL_MT_H */ diff --git a/include/osmocom/bsc/assignment_fsm.h b/include/osmocom/bsc/assignment_fsm.h new file mode 100644 index 000000000..ffb698320 --- /dev/null +++ b/include/osmocom/bsc/assignment_fsm.h @@ -0,0 +1,42 @@ +/* osmo-bsc API to manage BSSMAP Assignment Command */ +#pragma once + +#include + +#include + +/* This macro automatically includes a final \n, if omitted. */ +#define LOG_ASSIGNMENT(conn, level, fmt, args...) do { \ + if (conn->assignment.fi) \ + LOGPFSML(conn->assignment.fi, level, "%s%s" fmt "%s", \ + conn->assignment.new_lchan ? gsm_lchan_name(conn->assignment.new_lchan) : "", \ + conn->assignment.new_lchan ? " " : "", \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + else \ + LOGP(DMSC, level, "Assignment%s%s: " fmt "%s", \ + conn->assignment.new_lchan ? " of " : "", \ + conn->assignment.new_lchan ? gsm_lchan_name(conn->assignment.new_lchan) : "", \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + } while(0) + +enum assignment_fsm_state { + ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE, + ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE, + ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED, + ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC, +}; + +enum assignment_fsm_event { + ASSIGNMENT_EV_LCHAN_ACTIVE, + ASSIGNMENT_EV_LCHAN_ESTABLISHED, + ASSIGNMENT_EV_LCHAN_ERROR, + ASSIGNMENT_EV_MSC_MGW_OK, + ASSIGNMENT_EV_MSC_MGW_FAIL, + ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE, + ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL, + ASSIGNMENT_EV_CONN_RELEASING, +}; + +void assignment_fsm_start(struct gsm_subscriber_connection *conn, struct gsm_bts *bts, + struct assignment_request *req); +void assignment_reset(struct gsm_subscriber_connection *conn); diff --git a/include/osmocom/bsc/bsc_api.h b/include/osmocom/bsc/bsc_api.h index 9e2b44e2c..0d62506ab 100644 --- a/include/osmocom/bsc/bsc_api.h +++ b/include/osmocom/bsc/bsc_api.h @@ -12,15 +12,14 @@ void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci); void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t chosen_encr); int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint16_t chosen_channel); void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg); -void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause); -void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause); int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause); void bsc_cm_update(struct gsm_subscriber_connection *conn, const uint8_t *cm2, uint8_t cm2_len, const uint8_t *cm3, uint8_t cm3_len); void bsc_mr_config(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan, int full_rate); -int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id, int allow_sacch); +int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t link_id, + bool allow_sacch); int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate); int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher, const uint8_t *key, int len, int include_imeisv); @@ -28,4 +27,5 @@ int gsm0808_page(struct gsm_bts *bts, unsigned int page_group, unsigned int mi_len, uint8_t *mi, int chan_type); int gsm0808_clear(struct gsm_subscriber_connection *conn); +bool msc_connected(struct gsm_subscriber_connection *conn); #endif diff --git a/include/osmocom/bsc/bsc_msc_data.h b/include/osmocom/bsc/bsc_msc_data.h index 7235fbacc..79d2eca48 100644 --- a/include/osmocom/bsc/bsc_msc_data.h +++ b/include/osmocom/bsc/bsc_msc_data.h @@ -48,11 +48,6 @@ struct osmo_bsc_rf; struct gsm_network; -struct gsm_audio_support { - uint8_t hr : 1, - ver : 7; -}; - enum { MSC_CON_TYPE_NORMAL, MSC_CON_TYPE_LOCAL, diff --git a/include/osmocom/bsc/bsc_subscr_conn_fsm.h b/include/osmocom/bsc/bsc_subscr_conn_fsm.h index 0d373eb49..f689d9f71 100644 --- a/include/osmocom/bsc/bsc_subscr_conn_fsm.h +++ b/include/osmocom/bsc/bsc_subscr_conn_fsm.h @@ -8,28 +8,20 @@ enum gscon_fsm_event { GSCON_EV_A_CONN_REQ, /* MSC confirms the SCCP connection */ GSCON_EV_A_CONN_CFM, - /* MSC requests assignment */ - GSCON_EV_A_ASSIGNMENT_CMD, /* MSC has sent BSSMAP CLEAR CMD */ GSCON_EV_A_CLEAR_CMD, /* MSC SCCP disconnect indication */ GSCON_EV_A_DISC_IND, - /* MSC sends Handover Request (in CR) */ - GSCON_EV_A_HO_REQ, - /* RR ASSIGNMENT COMPLETE received */ - GSCON_EV_RR_ASS_COMPL, - /* RR ASSIGNMENT FAIL received */ - GSCON_EV_RR_ASS_FAIL, + GSCON_EV_ASSIGNMENT_START, + GSCON_EV_ASSIGNMENT_END, + + GSCON_EV_HANDOVER_START, + GSCON_EV_HANDOVER_END, - /* RSL RLL Release Indication */ - GSCON_EV_RLL_REL_IND, /* RSL CONNection FAILure Indication */ GSCON_EV_RSL_CONN_FAIL, - /* RSL/lchan tells us clearing is complete */ - GSCON_EV_RSL_CLEAR_COMPL, - /* Mobile-originated DTAP (from MS) */ GSCON_EV_MO_DTAP, /* Mobile-terminated DTAP (from MSC) */ @@ -38,39 +30,48 @@ enum gscon_fsm_event { /* Transmit custom SCCP message */ GSCON_EV_TX_SCCP, - /* MGW is indicating failure (BTS) */ - GSCON_EV_MGW_FAIL_BTS, - /* MGW is indicating failure (MSC) */ - GSCON_EV_MGW_FAIL_MSC, - /* CRCX response received (BTS) */ - GSCON_EV_MGW_CRCX_RESP_BTS, - /* MDCX response received (BTS) */ - GSCON_EV_MGW_MDCX_RESP_BTS, - /* CRCX response received (MSC) */ - GSCON_EV_MGW_CRCX_RESP_MSC, /* MDCX response received (MSC) - triggered by LCLS */ GSCON_EV_MGW_MDCX_RESP_MSC, - /* Internal handover request (intra-BSC handover) */ - GSCON_EV_HO_START, - /* Handover timed out (T3103 in handover_logic.c) */ - GSCON_EV_HO_TIMEOUT, - /* Handover failed (handover_logic.c) */ - GSCON_EV_HO_FAIL, - /* Handover completed successfully (handover_logic.c) */ - GSCON_EV_HO_COMPL, - /* LCLS child FSM has terminated due to hard failure */ GSCON_EV_LCLS_FAIL, + + GSCON_EV_FORGET_LCHAN, + GSCON_EV_FORGET_MGW_ENDPOINT, }; struct gsm_subscriber_connection; struct gsm_network; struct msgb; +struct mgcp_conn_peer; +struct mgwep_ci; +struct assignment_request; +struct gsm_lchan; /* Allocate a subscriber connection and its associated FSM */ struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net); +void gscon_update_id(struct gsm_subscriber_connection *conn); void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id, int allow_sacch); -void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send); +int gscon_sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg); + +struct mgw_endpoint *gscon_ensure_mgw_endpoint(struct gsm_subscriber_connection *conn); +bool gscon_connect_mgw_to_msc(struct gsm_subscriber_connection *conn, + struct gsm_lchan *for_lchan, + const char *addr, uint16_t port, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data, + struct mgwep_ci **created_ci); + +void gscon_start_assignment(struct gsm_subscriber_connection *conn, + struct assignment_request *req); + +void gscon_change_primary_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *new_lchan); +void gscon_release_lchans(struct gsm_subscriber_connection *conn, bool do_sacch_deact); + +void gscon_lchan_releasing(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan); +void gscon_forget_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan); + +void gscon_forget_mgw_endpoint_ci(struct gsm_subscriber_connection *conn, struct mgwep_ci *ci); diff --git a/include/osmocom/bsc/bsc_subscriber.h b/include/osmocom/bsc/bsc_subscriber.h index 324734f9a..a7b3e53fc 100644 --- a/include/osmocom/bsc/bsc_subscriber.h +++ b/include/osmocom/bsc/bsc_subscriber.h @@ -19,6 +19,7 @@ struct bsc_subscr { }; const char *bsc_subscr_name(struct bsc_subscr *bsub); +const char *bsc_subscr_id(struct bsc_subscr *bsub); struct bsc_subscr *bsc_subscr_find_or_create_by_imsi(struct llist_head *list, const char *imsi); diff --git a/include/osmocom/bsc/chan_alloc.h b/include/osmocom/bsc/chan_alloc.h index f3aec9dd5..97f6cb2db 100644 --- a/include/osmocom/bsc/chan_alloc.h +++ b/include/osmocom/bsc/chan_alloc.h @@ -24,18 +24,11 @@ struct gsm_subscriber_connection; -/* Count number of free TS of given pchan type */ -int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan); - /* Allocate a logical channel (SDCCH, TCH, ...) */ struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type, int allow_bigger); /* Free a logical channel (SDCCH, TCH, ...) */ void lchan_free(struct gsm_lchan *lchan); -void lchan_reset(struct gsm_lchan *lchan); - -/* Release the given lchan */ -int lchan_release(struct gsm_lchan *lchan, int sacch_deact, enum rsl_rel_mode release_mode); struct pchan_load { struct load_counter pchan[_GSM_PCHAN_MAX]; @@ -45,6 +38,4 @@ void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts); void network_chan_load(struct pchan_load *pl, struct gsm_network *net); void bts_update_t3122_chan_load(struct gsm_bts *bts); -bool ts_is_usable(const struct gsm_bts_trx_ts *ts); - #endif /* _CHAN_ALLOC_H */ diff --git a/include/osmocom/bsc/debug.h b/include/osmocom/bsc/debug.h index 1133bf6f6..ed6cbc3cc 100644 --- a/include/osmocom/bsc/debug.h +++ b/include/osmocom/bsc/debug.h @@ -6,6 +6,8 @@ #define DEBUG #include +#define LOG_ADD_NEWLINE(fmt) ((!fmt || !*fmt || fmt[strlen(fmt)-1] != '\n') ? "\n" : "") + /* Debug Areas of the code */ enum { DRLL, diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h index ce7a8fee6..5d28dd94c 100644 --- a/include/osmocom/bsc/gsm_data.h +++ b/include/osmocom/bsc/gsm_data.h @@ -14,13 +14,13 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -31,6 +31,10 @@ #include #include #include +#include +#include + +#define GSM_T3122_DEFAULT 10 #define GSM_T3122_DEFAULT 10 @@ -38,6 +42,7 @@ struct mgcp_client_conf; struct mgcp_client; struct mgcp_ctx; struct gsm0808_cell_id; +struct mgw_endpoint; /** annotations for msgb ownership */ #define __uses @@ -46,6 +51,7 @@ struct gsm0808_cell_id; struct bsc_subscr; struct gprs_ra_id; +struct handover; #define OBSC_LINKID_CB(__msgb) (__msgb)->cb[3] @@ -95,6 +101,95 @@ enum subscr_sccp_state { SUBSCR_SCCP_ST_CONNECTED }; +struct assignment_request { + bool aoip; + + char msc_rtp_addr[INET_ADDRSTRLEN]; + uint16_t msc_rtp_port; + + enum gsm48_chan_mode chan_mode; + bool full_rate; +}; + +struct assignment_fsm_data { + struct assignment_request req; + bool requires_voice_stream; + + struct osmo_fsm_inst *fi; + struct gsm_lchan *new_lchan; + + /* Whether this assignment triggered creation of the MGW endpoint: if the assignment + * fails, we will release that again as soon as possible. (If false, the endpoint already + * existed before or isn't needed at all.)*/ + struct mgwep_ci *created_ci_for_msc; + + enum gsm0808_cause failure_cause; + enum gsm48_rr_cause rr_cause; + + bool result_rate_ctr_done; +}; + +enum hodec_id { + HODEC_NONE, + HODEC1 = 1, + HODEC2 = 2, + HODEC_USER, + HODEC_REMOTE, +}; + +/* For example, to count specific kinds of ongoing handovers, it is useful to be able to OR-combine + * scopes. */ +enum handover_scope { + HO_NO_HANDOVER = 0, + HO_INTRA_CELL = 0x1, + HO_INTRA_BSC = 0x2, + HO_INTER_BSC_MO = 0x4, + HO_INTER_BSC_MT = 0x8, + HO_SCOPE_ALL = 0xffff, +}; + +extern const struct value_string handover_scope_names[]; +inline static const char *handover_scope_name(enum handover_scope val) +{ return get_value_string(handover_scope_names, val); } + +struct handover_mo_req { + enum hodec_id from_hodec_id; + struct gsm_lchan *old_lchan; + struct neighbor_ident_key target_nik; + enum gsm_chan_t new_lchan_type; /*< leave GSM_LCHAN_NONE to use same as old_lchan */ +}; + +struct handover_mt_req { + struct gsm0808_channel_type ct; + struct gsm0808_speech_codec_list scl; + struct gsm0808_encrypt_info ei; + struct gsm_classmark classmark; + struct gsm0808_cell_id cell_id_serving; + char cell_id_serving_name[64]; + struct gsm0808_cell_id cell_id_target; + char cell_id_target_name[64]; + char msc_rtp_addr[INET_ADDRSTRLEN]; + uint16_t msc_rtp_port; +}; + +struct handover { + struct osmo_fsm_inst *fi; + + enum hodec_id from_hodec_id; + enum handover_scope scope; + enum gsm_chan_t new_lchan_type; + struct neighbor_ident_key target_cell; + + struct { + uint8_t ho_ref; + struct gsm_bts *new_bts; + struct gsm_lchan *new_lchan; + bool async; + struct handover_mt_req inter_bsc; + struct mgwep_ci *created_ci_for_msc; + } mt; +}; + /* active radio connection of a mobile subscriber */ struct gsm_subscriber_connection { /* global linked list of subscriber_connections */ @@ -112,11 +207,10 @@ struct gsm_subscriber_connection { /* the primary / currently active lchan to the BTS/subscriber */ struct gsm_lchan *lchan; - /* handover information, if a handover is pending for this conn. */ - struct bsc_handover *ho; + struct assignment_fsm_data assignment; - /* the future allocated but not yet used lchan during ASSIGNMENT */ - struct gsm_lchan *secondary_lchan; + /* handover information, if a handover is pending for this conn. */ + struct handover ho; /* buffer/cache for classmark of the ME of the subscriber */ struct gsm_classmark classmark; @@ -137,7 +231,6 @@ struct gsm_subscriber_connection { * i.e. by heeding the "Codec list (MSC Preferred)", we inherently heed the MS bearer * capabilities, which the MSC is required to translate into the codec list. */ struct gsm0808_speech_codec_list codec_list; - bool codec_list_present; /* flag to prevent multiple simultaneous ciphering commands */ int ciphering_handled; @@ -166,21 +259,12 @@ struct gsm_subscriber_connection { uint16_t cic; uint32_t rtp_ip; int rtp_port; - /* RTP address of the remote end (assigned by MSC through assignment request) */ - struct sockaddr_storage aoip_rtp_addr_remote; - - /* Local RTP address (reported back to the MSC by us with the - * assignment complete message) */ - struct sockaddr_storage aoip_rtp_addr_local; - - /* FSM instance to control the BTS sided RTP connection */ - struct osmo_fsm_inst *fi_bts; - /* FSM instance to control the MSC sided RTP connection */ - struct osmo_fsm_inst *fi_msc; + char msc_assigned_rtp_addr[INET_ADDRSTRLEN]; + uint16_t msc_assigned_rtp_port; - /* Endpoint identifier of the MGCP endpoint the connection uses */ - char *mgw_endpoint; + struct mgw_endpoint *mgw_endpoint; + struct mgwep_ci *mgw_endpoint_ci_msc; /* Channel rate flag, FR=1, HR=0, Invalid=-1 */ int full_rate; @@ -298,18 +382,6 @@ struct om2k_mo { #define LCHAN_SAPI_UNUSED 0 #define LCHAN_SAPI_MS 1 #define LCHAN_SAPI_NET 2 -#define LCHAN_SAPI_REL 3 - -/* state of a logical channel */ -enum gsm_lchan_state { - LCHAN_S_NONE, /* channel is not active */ - LCHAN_S_ACT_REQ, /* channel activation requested */ - LCHAN_S_ACTIVE, /* channel is active and operational */ - LCHAN_S_REL_REQ, /* channel release has been requested */ - LCHAN_S_REL_ERR, /* channel is in an error state */ - LCHAN_S_BROKEN, /* channel is somehow unusable */ - LCHAN_S_INACTIVE, /* channel is set inactive */ -}; /* BTS ONLY */ #define MAX_NUM_UL_MEAS 104 @@ -386,11 +458,65 @@ struct gsm_encr { bsc_subscr_name(lchan && lchan->conn ? lchan->conn->bsub : NULL), \ ## args) +/* usage: + * struct gsm_lchan *lchan; + * struct gsm_bts_trx_ts *ts = get_some_timeslot(); + * ts_for_each_lchan(lchan, ts) { + * LOGPLCHAN(DMAIN, LOGL_DEBUG, "hello world\n"); + * } + * Iterate only those lchans that have an FSM allocated. */ +#define ts_for_each_lchan(lchan, ts) ts_as_pchan_for_each_lchan(lchan, ts, (ts)->pchan_is) + +/* Same as ts_for_each_lchan() but with an explicit pchan kind (GSM_PCHAN_* constant). + * Iterate only those lchans that have an FSM allocated. */ +#define ts_as_pchan_for_each_lchan(lchan, ts, as_pchan) \ + for (lchan = (ts)->lchan; \ + ((lchan - (ts)->lchan) < ARRAY_SIZE((ts)->lchan)) \ + && lchan->fi \ + && lchan->nr < pchan_subslots(as_pchan); \ + lchan++) + +enum lchan_activate_mode { + FOR_NONE, + FOR_MS_CHANNEL_REQUEST, + FOR_ASSIGNMENT, + FOR_HANDOVER, + FOR_VTY, +}; + +extern const struct value_string lchan_activate_mode_names[]; +static inline const char *lchan_activate_mode_name(enum lchan_activate_mode activ_for) +{ return get_value_string(lchan_activate_mode_names, activ_for); } + struct gsm_lchan { /* The TS that we're part of */ struct gsm_bts_trx_ts *ts; /* The logical subslot number in the TS */ uint8_t nr; + char *name; + + struct osmo_fsm_inst *fi; + struct mgwep_ci *mgw_endpoint_ci_bts; + + struct { + enum lchan_activate_mode activ_for; + bool concluded; /*< true as soon as LCHAN_ST_ESTABLISHED is reached */ + bool requires_voice_stream; + bool mgw_endpoint_available; + enum gsm0808_cause gsm0808_error_cause; + } activate; + + /* If an event to release the lchan comes in while still waiting for responses, just mark this + * flag, so that the lchan will gracefully release at the next sensible junction. */ + bool release_requested; + bool deact_sacch; + + char *last_error; + + /* There is an RSL error cause of value 0, so we need a separate flag. */ + bool release_in_error; + uint8_t error_cause; + /* The logical channel type */ enum gsm_chan_t type; /* RSL channel mode */ @@ -398,9 +524,6 @@ struct gsm_lchan { /* If TCH, traffic channel mode */ enum gsm48_chan_mode tch_mode; enum lchan_csd_mode csd_mode; - /* State */ - enum gsm_lchan_state state; - const char *broken_reason; /* Power levels for MS and BTS */ uint8_t bs_power; uint8_t ms_power; @@ -415,15 +538,14 @@ struct gsm_lchan { uint8_t sapis[8]; struct { - uint32_t bound_ip; - uint32_t connect_ip; + uint32_t bound_ip; /*< where the BTS receives RTP */ uint16_t bound_port; + uint32_t connect_ip; /*< where the BTS sends RTP to (MGW) */ uint16_t connect_port; uint16_t conn_id; uint8_t rtp_payload; uint8_t rtp_payload2; uint8_t speech_mode; - struct rtp_socket *rtp_socket; /* info we need to postpone the AoIP * assignment completed message */ @@ -435,16 +557,6 @@ struct gsm_lchan { uint8_t rqd_ta; - char *name; - - struct osmo_timer_list T3101; - struct osmo_timer_list T3109; - struct osmo_timer_list T3111; - struct osmo_timer_list error_timer; - struct osmo_timer_list act_timer; - struct osmo_timer_list rel_work; - uint8_t error_cause; - /* table of neighbor cell measurements */ struct neigh_meas_proc neigh_meas[MAX_NEIGH_MEAS]; @@ -455,43 +567,38 @@ struct gsm_lchan { uint8_t meas_rep_last_seen_nr; /* GSM Random Access data */ + /* TODO: don't allocate this, rather keep an "is_present" flag */ struct gsm48_req_ref *rqd_ref; struct gsm_subscriber_connection *conn; - - struct { - /* channel activation type and handover ref */ - uint8_t act_type; - uint8_t ho_ref; - struct gsm48_req_ref *rqd_ref; - uint8_t rqd_ta; - } dyn; }; -enum { - TS_F_PDCH_ACTIVE = 0x1000, - TS_F_PDCH_ACT_PENDING = 0x2000, - TS_F_PDCH_DEACT_PENDING = 0x4000, - TS_F_PDCH_PENDING_MASK = (TS_F_PDCH_ACT_PENDING | TS_F_PDCH_DEACT_PENDING), -} gsm_bts_trx_ts_flags; - /* One Timeslot in a TRX */ struct gsm_bts_trx_ts { struct gsm_bts_trx *trx; - bool initialized; - /* number of this timeslot at the TRX */ uint8_t nr; - enum gsm_phys_chan_config pchan; - - struct { - enum gsm_phys_chan_config pchan_is; - enum gsm_phys_chan_config pchan_want; - struct msgb *pending_chan_activ; - } dyn; + struct osmo_fsm_inst *fi; + char *last_errmsg; + + /* vty phys_chan_config setting, not necessarily in effect in case it was changed in the telnet + * vty after OML activation. Gets written on vty 'write file'. */ + enum gsm_phys_chan_config pchan_from_config; + /* When the timeslot OML is established, pchan_from_config is copied here. This is the pchan + * currently in effect; for dynamic ts, this is the dyn kind (GSM_PCHAN_TCH_F_TCH_H_PDCH or + * GSM_PCHAN_TCH_F_PDCH) and does not show the pchan type currently active. */ + enum gsm_phys_chan_config pchan_on_init; + /* This is the *actual* pchan type currently active. For dynamic timeslots, this reflects either + * GSM_PCHAN_NONE or one of the standard GSM_PCHAN_TCH_F, GSM_PCHAN_TCH_H, GSM_PCHAN_PDCH. + * Callers can use this transparently without being aware of dyn ts. */ + enum gsm_phys_chan_config pchan_is; + + /* After a PDCH ACT NACK, we shall not infinitely loop to try and ACT again. + * Also marks a timeslot where PDCH was deactivated by VTY. This is cleared whenever a timeslot + * enters IN_USE state, i.e. after each TCH use we try to PDCH ACT once again. */ + bool pdch_act_allowed; - unsigned int flags; struct gsm_abis_mo mo; struct tlv_parsed nm_attr; uint8_t nm_chan_comb; @@ -1011,9 +1118,12 @@ struct gsm_bts { struct gsm_network *gsm_network_init(void *ctx); struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num); -struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num); -bool gsm_bts_matches_cell_id(struct gsm_bts *bts, const struct gsm0808_cell_id *ci); -struct gsm_bts *gsm_bts_by_cell_id(struct gsm_network *net, const struct gsm0808_cell_id *ci); +struct gsm_bts *gsm_bts_num(const struct gsm_network *net, int num); +bool gsm_bts_matches_cell_id(const struct gsm_bts *bts, const struct gsm0808_cell_id *cell_id); +bool gsm_bts_matches_lai(const struct gsm_bts *bts, const struct osmo_location_area_id *lai); +struct gsm_bts *gsm_bts_by_cell_id(const struct gsm_network *net, + const struct gsm0808_cell_id *cell_id, + int match_idx); int gsm_bts_local_neighbor_add(struct gsm_bts *bts, struct gsm_bts *neighbor); int gsm_bts_local_neighbor_del(struct gsm_bts *bts, const struct gsm_bts *neighbor); @@ -1030,9 +1140,12 @@ enum gsm_bts_type_variant str2btsvariant(const char *arg); const char *btsvariant2str(enum gsm_bts_type_variant v); extern const struct value_string gsm_chreq_descs[]; -const struct value_string gsm_pchant_names[13]; -const struct value_string gsm_pchant_descs[13]; +extern const struct value_string gsm_pchant_names[]; +extern const struct value_string gsm_pchant_descs[]; +extern const struct value_string gsm_pchan_ids[]; const char *gsm_pchan_name(enum gsm_phys_chan_config c); +static inline const char *gsm_pchan_id(enum gsm_phys_chan_config c) +{ return get_value_string(gsm_pchan_ids, c); } enum gsm_phys_chan_config gsm_pchan_parse(const char *name); const char *gsm_lchant_name(enum gsm_chan_t c); const char *gsm_chreq_name(enum gsm_chreq_reason_t c); @@ -1040,7 +1153,6 @@ char *gsm_trx_name(const struct gsm_bts_trx *trx); char *gsm_ts_name(const struct gsm_bts_trx_ts *ts); char *gsm_ts_and_pchan_name(const struct gsm_bts_trx_ts *ts); char *gsm_lchan_name_compute(const struct gsm_lchan *lchan); -const char *gsm_lchans_name(enum gsm_lchan_state s); static inline char *gsm_lchan_name(const struct gsm_lchan *lchan) { @@ -1086,7 +1198,7 @@ struct gsm_lchan *rsl_lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr, int *rc); enum gsm_phys_chan_config ts_pchan(struct gsm_bts_trx_ts *ts); -uint8_t ts_subslots(struct gsm_bts_trx_ts *ts); +uint8_t pchan_subslots(enum gsm_phys_chan_config pchan); bool ts_is_tch(struct gsm_bts_trx_ts *ts); @@ -1156,11 +1268,32 @@ enum { }; enum { + BSC_CTR_ASSIGNMENT_ATTEMPTED, + BSC_CTR_ASSIGNMENT_COMPLETED, + BSC_CTR_ASSIGNMENT_STOPPED, + BSC_CTR_ASSIGNMENT_NO_CHANNEL, + BSC_CTR_ASSIGNMENT_TIMEOUT, + BSC_CTR_ASSIGNMENT_FAILED, + BSC_CTR_ASSIGNMENT_ERROR, BSC_CTR_HANDOVER_ATTEMPTED, + BSC_CTR_HANDOVER_COMPLETED, + BSC_CTR_HANDOVER_STOPPED, BSC_CTR_HANDOVER_NO_CHANNEL, BSC_CTR_HANDOVER_TIMEOUT, - BSC_CTR_HANDOVER_COMPLETED, BSC_CTR_HANDOVER_FAILED, + BSC_CTR_HANDOVER_ERROR, + BSC_CTR_INTER_BSC_HO_MO_ATTEMPTED, + BSC_CTR_INTER_BSC_HO_MO_COMPLETED, + BSC_CTR_INTER_BSC_HO_MO_STOPPED, + BSC_CTR_INTER_BSC_HO_MO_TIMEOUT, + BSC_CTR_INTER_BSC_HO_MO_ERROR, + BSC_CTR_INTER_BSC_HO_MT_ATTEMPTED, + BSC_CTR_INTER_BSC_HO_MT_COMPLETED, + BSC_CTR_INTER_BSC_HO_MT_STOPPED, + BSC_CTR_INTER_BSC_HO_MT_NO_CHANNEL, + BSC_CTR_INTER_BSC_HO_MT_FAILED, + BSC_CTR_INTER_BSC_HO_MT_TIMEOUT, + BSC_CTR_INTER_BSC_HO_MT_ERROR, BSC_CTR_PAGING_ATTEMPTED, BSC_CTR_PAGING_DETACHED, BSC_CTR_PAGING_RESPONDED, @@ -1168,11 +1301,42 @@ enum { }; static const struct rate_ctr_desc bsc_ctr_description[] = { - [BSC_CTR_HANDOVER_ATTEMPTED] = {"handover:attempted", "Received handover attempts."}, - [BSC_CTR_HANDOVER_NO_CHANNEL] = {"handover:no_channel", "Sent no channel available responses."}, - [BSC_CTR_HANDOVER_TIMEOUT] = {"handover:timeout", "Timeouts of timer T3103."}, - [BSC_CTR_HANDOVER_COMPLETED] = {"handover:completed", "Received handover completed."}, - [BSC_CTR_HANDOVER_FAILED] = {"handover:failed", "Received HO FAIL messages."}, + [BSC_CTR_ASSIGNMENT_ATTEMPTED] = {"assignment:attempted", "Intra-cell re-assignment attempts."}, + [BSC_CTR_ASSIGNMENT_COMPLETED] = {"assignment:completed", "Intra-cell re-assignment completed."}, + [BSC_CTR_ASSIGNMENT_STOPPED] = {"assignment:stopped", "Connection ended during re-assignment."}, + [BSC_CTR_ASSIGNMENT_NO_CHANNEL] = {"assignment:no_channel", "Failure to allocate lchan for re-assignment."}, + [BSC_CTR_ASSIGNMENT_TIMEOUT] = {"assignment:timeout", "Re-assignment timed out."}, + [BSC_CTR_ASSIGNMENT_FAILED] = {"assignment:failed", "Received FAIL message."}, + [BSC_CTR_ASSIGNMENT_ERROR] = {"assignment:error", "Re-assigment failed for other reason."}, + + [BSC_CTR_HANDOVER_ATTEMPTED] = {"handover:attempted", "Intra-BSC handover attempts."}, + [BSC_CTR_HANDOVER_COMPLETED] = {"handover:completed", "Intra-BSC handover completed."}, + [BSC_CTR_HANDOVER_STOPPED] = {"handover:stopped", "Connection ended during HO."}, + [BSC_CTR_HANDOVER_NO_CHANNEL] = {"handover:no_channel", "Failure to allocate lchan for HO."}, + [BSC_CTR_HANDOVER_TIMEOUT] = {"handover:timeout", "Handover timed out."}, + [BSC_CTR_HANDOVER_FAILED] = {"handover:failed", "Received Handover Fail messages."}, + [BSC_CTR_HANDOVER_ERROR] = {"handover:error", "Re-assigment failed for other reason."}, + + [BSC_CTR_INTER_BSC_HO_MO_ATTEMPTED] = {"interbsc_ho_mo:attempted", + "Attempts to handover to remote BSS."}, + [BSC_CTR_INTER_BSC_HO_MO_COMPLETED] = {"interbsc_ho_mo:completed", + "Handover to remote BSS completed."}, + [BSC_CTR_INTER_BSC_HO_MO_STOPPED] = {"interbsc_ho_mo:stopped", "Connection ended during HO."}, + [BSC_CTR_INTER_BSC_HO_MO_TIMEOUT] = {"interbsc_ho_mo:timeout", "Handover timed out."}, + [BSC_CTR_INTER_BSC_HO_MO_ERROR] = {"interbsc_ho_mo:error", + "Handover to remote BSS failed for other reason."}, + + [BSC_CTR_INTER_BSC_HO_MT_ATTEMPTED] = {"interbsc_ho_mt:attempted", + "Attempts to handover from remote BSS."}, + [BSC_CTR_INTER_BSC_HO_MT_COMPLETED] = {"interbsc_ho_mt:completed", + "Handover from remote BSS completed."}, + [BSC_CTR_INTER_BSC_HO_MT_STOPPED] = {"interbsc_ho_mt:stopped", "Connection ended during HO."}, + [BSC_CTR_INTER_BSC_HO_MT_NO_CHANNEL] = {"interbsc_ho_mt:no_channel", + "Failure to allocate lchan for HO."}, + [BSC_CTR_INTER_BSC_HO_MT_TIMEOUT] = {"interbsc_ho_mt:timeout", "Handover from remote BSS timed out."}, + [BSC_CTR_INTER_BSC_HO_MT_FAILED] = {"interbsc_ho_mt:failed", "Received Handover Fail message."}, + [BSC_CTR_INTER_BSC_HO_MT_ERROR] = {"interbsc_ho_mt:error", + "Handover from remote BSS failed for other reason."}, [BSC_CTR_PAGING_ATTEMPTED] = {"paging:attempted", "Paging attempts for a subscriber."}, [BSC_CTR_PAGING_DETACHED] = {"paging:detached", "Paging request send failures because no responsible BTS was found."}, @@ -1269,6 +1433,11 @@ struct gsm_network { struct neighbor_ident_list *neighbor_bss_cells; }; +struct gsm_audio_support { + uint8_t hr : 1, + ver : 7; +}; + static inline const struct osmo_location_area_id *bts_lai(struct gsm_bts *bts) { static struct osmo_location_area_id lai; @@ -1404,12 +1573,27 @@ void gsm_bts_set_radio_link_timeout(struct gsm_bts *bts, int value); bool classmark_is_r99(struct gsm_classmark *cm); -void gsm_ts_check_init(struct gsm_bts_trx_ts *ts); -void gsm_trx_mark_all_ts_uninitialized(struct gsm_bts_trx *trx); -void gsm_bts_mark_all_ts_uninitialized(struct gsm_bts *bts); - bool trx_is_usable(const struct gsm_bts_trx *trx); +bool ts_is_usable(const struct gsm_bts_trx_ts *ts); + +int gsm_lchan_type_by_pchan(enum gsm_phys_chan_config pchan); +enum gsm_phys_chan_config gsm_pchan_by_lchan_type(enum gsm_chan_t type); + +void gsm_bts_all_ts_dispatch(struct gsm_bts *bts, uint32_t ts_ev, void *data); +void gsm_trx_all_ts_dispatch(struct gsm_bts_trx *trx, uint32_t ts_ev, void *data); + +int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan); + +static inline const char *gsm48_chan_mode_name(enum gsm48_chan_mode val) +{ return get_value_string(gsm48_chan_mode_names, val); } -bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts); +int bsc_match_codec_pref(enum gsm48_chan_mode *chan_mode, + bool *full_rate, + const struct gsm0808_channel_type *ct, + const struct gsm0808_speech_codec_list *scl, + struct gsm_audio_support * const *audio_support, + int audio_length); +bool sockaddr_to_str_and_uint(char *rtp_addr, size_t rtp_addr_len, uint16_t *rtp_port, + const struct sockaddr_storage *sa); #endif /* _GSM_DATA_H */ diff --git a/include/osmocom/bsc/handover.h b/include/osmocom/bsc/handover.h index 847d985fb..a9b813372 100644 --- a/include/osmocom/bsc/handover.h +++ b/include/osmocom/bsc/handover.h @@ -5,71 +5,38 @@ #include #include #include +#include #include +#include +struct gsm_network; struct gsm_lchan; struct gsm_bts; struct gsm_subscriber_connection; struct gsm_meas_rep mr; -#define LOGPHOLCHANTOLCHAN(old_lchan, new_lchan, level, fmt, args...) \ - LOGP(DHODEC, level, "(BTS %u trx %u arfcn %u ts %u lchan %u %s)->(BTS %u trx %u arfcn %u ts %u lchan %u %s) (subscr %s) " fmt, \ - old_lchan->ts->trx->bts->nr, \ - old_lchan->ts->trx->nr, \ - old_lchan->ts->trx->arfcn, \ - old_lchan->ts->nr, \ - old_lchan->nr, \ - gsm_pchan_name(old_lchan->ts->pchan), \ - new_lchan->ts->trx->bts->nr, \ - new_lchan->ts->trx->nr, \ - new_lchan->ts->trx->arfcn, \ - new_lchan->ts->nr, \ - new_lchan->nr, \ - gsm_pchan_name(new_lchan->ts->pchan), \ - bsc_subscr_name(old_lchan->conn? old_lchan->conn->bsub : NULL), \ - ## args) - -#define LOGPHO(struct_bsc_handover, level, fmt, args ...) \ - LOGPHOLCHANTOLCHAN(struct_bsc_handover->old_lchan, struct_bsc_handover->new_lchan, level, fmt, ## args) - -enum hodec_id { - HODEC_NONE, - HODEC1 = 1, - HODEC2 = 2, +enum handover_result { + HO_RESULT_OK, + HO_RESULT_FAIL_NO_CHANNEL, + HO_RESULT_FAIL_RR_HO_FAIL, + HO_RESULT_FAIL_TIMEOUT, + HO_RESULT_CONN_RELEASE, + HO_RESULT_ERROR, }; -struct bsc_handover { - struct llist_head list; - - /* Initial details of what is requested */ - struct gsm_lchan *old_lchan; - struct gsm_bts *new_bts; - enum gsm_chan_t new_lchan_type; - bool async; - - /* Derived and resulting state */ - bool inter_cell; - uint8_t ho_ref; - enum hodec_id from_hodec_id; - struct gsm_lchan *new_lchan; - struct osmo_timer_list T3103; -}; +extern const struct value_string handover_result_names[]; +inline static const char *handover_result_name(enum handover_result val) +{ return get_value_string(handover_result_names, val); } -int bsc_handover_start(enum hodec_id from_hodec_id, struct gsm_lchan *old_lchan, struct gsm_bts *new_bts, - enum gsm_chan_t new_lchan_type); -int bsc_handover_start_gscon(struct gsm_subscriber_connection *conn); -void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan); -struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan); - -int bsc_ho_count(struct gsm_bts *bts, bool inter_cell); +int handover_count(struct gsm_bts *bts, int ho_scopes); /* Handover decision algorithms' actions to take on incoming handover-relevant events. * * All events that are interesting for handover decision are actually communicated by S_LCHAN_* signals, * so theoretically, each handover algorithm could evaluate those. However, handover_logic.c cleans up * handover operation state upon receiving some of these signals. To allow a handover decision algorithm - * to take advantage of e.g. the struct bsc_handover before it is discarded, the handover decision event + * to take advantage of e.g. the struct handover before it is discarded, the handover decision event * handler needs to be invoked before handover_logic.c discards the state. For example, if the handover * decision wants to place a penalty timer upon a handover failure, it still needs to know which target * cell the handover failed for; handover_logic.c erases that knowledge on handover failure, since it @@ -89,13 +56,26 @@ struct handover_decision_callbacks { int hodec_id; void (*on_measurement_report)(struct gsm_meas_rep *mr); - void (*on_ho_chan_activ_nack)(struct bsc_handover *ho); - void (*on_ho_failure)(struct bsc_handover *ho); + void (*on_handover_end)(struct gsm_subscriber_connection *conn, enum handover_result result); }; void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc); struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id); +int bsc_tx_bssmap_ho_required(struct gsm_lchan *lchan, const struct gsm0808_cell_id_list2 *target_cells); +int bsc_tx_bssmap_ho_request_ack(struct gsm_subscriber_connection *conn, + struct msgb *rr_ho_command); +int bsc_tx_bssmap_ho_detect(struct gsm_subscriber_connection *conn); +enum handover_result bsc_tx_bssmap_ho_complete(struct gsm_subscriber_connection *conn, + struct gsm_lchan *lchan); +void bsc_tx_bssmap_ho_failure(struct gsm_subscriber_connection *conn); + struct gsm_bts *bts_by_neighbor_ident(const struct gsm_network *net, const struct neighbor_ident_key *search_for); struct neighbor_ident_key *bts_ident_key(const struct gsm_bts *bts); + +void handover_parse_inter_bsc_mt(struct gsm_subscriber_connection *conn, + struct msgb *ho_request_msg); + +void handover_mt_allocate_lchan(struct handover *ho); +int handover_mt_send_rr_ho_command(struct handover *ho); diff --git a/include/osmocom/bsc/handover_fsm.h b/include/osmocom/bsc/handover_fsm.h new file mode 100644 index 000000000..4b8b6ec00 --- /dev/null +++ b/include/osmocom/bsc/handover_fsm.h @@ -0,0 +1,81 @@ +/* Handover FSM API for intra-BSC and inter-BSC MT (to this BSC) Handover. + * (For inter-BSC MO, from this BSC, see handover_inter_mo_fsm.c) */ +#pragma once + +#include +#include + +const char *handover_status(struct gsm_subscriber_connection *conn); + +/* This macro automatically includes a final \n, if omitted. */ +#define LOG_HO(conn, level, fmt, args...) do { \ + if (conn->ho.fi) \ + LOGPFSML(conn->ho.fi, level, "%s: " fmt "%s", \ + handover_status(conn), \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + else \ + LOGP(DHODEC, level, "%s: " fmt "%s", \ + handover_status(conn), \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + } while(0) + +/* Terminology: + * Intra-Cell: stays within one BTS, this should actually be an Assignment. + * Intra-BSC: stays within one BSC, but moves between BTSes. + * Inter-BSC: moves between BSCs. + * Inter-BSC MO: Mobile Originated: move away from this BSC to another one. + * Inter-BSC MT: Mobile Terminated: move from another BSC to this one. + */ + +enum handover_fsm_state { + HO_ST_NOT_STARTED, + + HO_ST_WAIT_LCHAN_ACTIVE, + HO_ST_WAIT_RR_HO_DETECT, + HO_ST_WAIT_RR_HO_COMPLETE, + HO_ST_WAIT_LCHAN_ESTABLISHED, + HO_ST_WAIT_MGW_ENDPOINT_TO_MSC, + + /* The inter-BSC MO Handover FSM has completely separate states, but since it makes sense for it + * to also live in conn->ho.fi, it should share the same event enum. From there it is merely + * cosmetic to just include the separate fairly trivial states in the same FSM definition. + * An inter-MO FSM is almost unnecessary. The sole reason is to wait whether the MSC indeed + * clears the conn, and if not to log and count a failed handover attempt. */ + MOHO_ST_WAIT_HO_COMMAND, + MOHO_ST_WAIT_CLEAR, +}; + +enum handover_fsm_event { + HO_EV_LCHAN_ACTIVE, + HO_EV_LCHAN_ESTABLISHED, + HO_EV_LCHAN_ERROR, + HO_EV_RR_HO_DETECT, + HO_EV_RR_HO_COMPLETE, + HO_EV_RR_HO_FAIL, + HO_EV_MSC_MGW_OK, + HO_EV_MSC_MGW_FAIL, + HO_EV_CONN_RELEASING, + + MOHO_EV_BSSMAP_HO_COMMAND, +}; + +struct moho_rx_bssmap_ho_command { + const uint8_t *l3_info; + const uint8_t l3_info_len; +}; + +/* To be sent along with the HO_EV_RR_HO_DETECT */ +struct handover_rr_detect_data { + struct msgb *msg; + const uint8_t *access_delay; +}; + +void handover_request(struct handover_mo_req *req); +void handover_start(struct handover_mo_req *req); +void handover_start_inter_bsc_mt(struct gsm_subscriber_connection *conn, + struct msgb *ho_request_msg); +void handover_end(struct gsm_subscriber_connection *conn, enum handover_result result); + +const char *handover_status(struct gsm_subscriber_connection *conn); +bool handover_is_sane(struct gsm_subscriber_connection *conn, struct gsm_lchan *old_lchan, + struct gsm_lchan *new_lchan); diff --git a/include/osmocom/bsc/lchan_fsm.h b/include/osmocom/bsc/lchan_fsm.h new file mode 100644 index 000000000..d5dd6d377 --- /dev/null +++ b/include/osmocom/bsc/lchan_fsm.h @@ -0,0 +1,87 @@ +/* osmo-bsc API to manage lchans, logical channels in GSM cells. */ +#pragma once + +#include +#include + +/* This macro automatically includes a final \n, if omitted. */ +#define LOG_LCHAN(lchan, level, fmt, args...) do { \ + if (lchan->fi) \ + LOGPFSML(lchan->fi, level, "(type=%s) " fmt "%s", gsm_lchant_name(lchan->type), \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + else \ + LOGP(DRSL, level, "%s (not initialized) " fmt "%s", gsm_lchan_name(lchan), \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + } while(0) + +enum lchan_fsm_state { + LCHAN_ST_UNUSED, + LCHAN_ST_WAIT_TS_READY, + LCHAN_ST_WAIT_ACTIV_ACK, /*< After RSL Chan Act Ack, lchan is active but RTP not configured. */ + LCHAN_ST_WAIT_RLL_ESTABLISH, + LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE, + LCHAN_ST_WAIT_IPACC_CRCX_ACK, + LCHAN_ST_WAIT_IPACC_MDCX_ACK, + LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED, + LCHAN_ST_ESTABLISHED, /*< Active and RTP is fully configured. */ + LCHAN_ST_WAIT_SAPIS_RELEASED, + LCHAN_ST_WAIT_BEFORE_RF_RELEASE, + LCHAN_ST_WAIT_RF_RELEASE_ACK, + LCHAN_ST_WAIT_AFTER_ERROR, + LCHAN_ST_BORKEN, +}; + +enum lchan_fsm_event { + LCHAN_EV_ACTIVATE, + LCHAN_EV_TS_READY, + LCHAN_EV_TS_ERROR, + LCHAN_EV_RSL_CHAN_ACTIV_ACK, + LCHAN_EV_RSL_CHAN_ACTIV_NACK, + LCHAN_EV_RLL_ESTABLISH_IND, + LCHAN_EV_MGW_ENDPOINT_AVAILABLE, + LCHAN_EV_MGW_ENDPOINT_CONFIGURED, + LCHAN_EV_MGW_ENDPOINT_ERROR, + LCHAN_EV_IPACC_CRCX_ACK, + LCHAN_EV_IPACC_CRCX_NACK, + LCHAN_EV_IPACC_MDCX_ACK, + LCHAN_EV_IPACC_MDCX_NACK, + LCHAN_EV_RLL_REL_IND, + LCHAN_EV_RLL_REL_CONF, + LCHAN_EV_RSL_RF_CHAN_REL_ACK, + LCHAN_EV_RLL_ERR_IND, + + /* FIXME: not yet implemented: Chan Mode Modify */ + LCHAN_EV_CHAN_MODE_MODIF_ACK, + LCHAN_EV_CHAN_MODE_MODIF_ERROR, +}; + +void lchan_fsm_alloc(struct gsm_lchan *lchan); +void lchan_release(struct gsm_lchan *lchan, bool sacch_deact, + bool err, enum gsm48_rr_cause cause_rr); + +struct lchan_activate_info { + enum lchan_activate_mode activ_for; + struct gsm_subscriber_connection *for_conn; + /* This always is for a specific lchan, so its lchan->type indicates full or half rate. + * When a dyn TS was selected, the lchan->type has been set to the desired rate. */ + enum gsm48_chan_mode chan_mode; + bool requires_voice_stream; + struct gsm_lchan *old_lchan; +}; + +void lchan_activate(struct gsm_lchan *lchan, struct lchan_activate_info *info); + +static inline const char *lchan_state_name(struct gsm_lchan *lchan) +{ + return lchan->fi ? osmo_fsm_inst_state_name(lchan->fi) : "NULL"; +} + +static inline bool lchan_state_is(struct gsm_lchan *lchan, uint32_t state) +{ + return lchan->fi && lchan->fi->state == state; +} + +bool lchan_may_receive_data(struct gsm_lchan *lchan); + +void lchan_forget_conn(struct gsm_lchan *lchan); +void lchan_forget_mgw_endpoint(struct gsm_lchan *lchan); diff --git a/include/osmocom/bsc/lchan_select.h b/include/osmocom/bsc/lchan_select.h new file mode 100644 index 000000000..4aecdf676 --- /dev/null +++ b/include/osmocom/bsc/lchan_select.h @@ -0,0 +1,6 @@ +/* Select a suitable lchan from a given cell. */ +#pragma once + +struct gsm_lchan *lchan_select_by_type(struct gsm_bts *bts, enum gsm_chan_t type); +struct gsm_lchan *lchan_select_by_chan_mode(struct gsm_bts *bts, + enum gsm48_chan_mode chan_mode, bool full_rate); diff --git a/include/osmocom/bsc/mgw_endpoint_fsm.h b/include/osmocom/bsc/mgw_endpoint_fsm.h new file mode 100644 index 000000000..e68468b5c --- /dev/null +++ b/include/osmocom/bsc/mgw_endpoint_fsm.h @@ -0,0 +1,60 @@ +/* osmo-bsc API to manage all sides of an MGW endpoint */ +#pragma once + +#include + +#include + +/* This macro automatically includes a final \n, if omitted. */ +#define LOG_MGWEP(mgwep, level, fmt, args...) do { \ + LOGPFSML(mgwep->fi, level, "(%s) " fmt "%s", \ + mgw_endpoint_name(mgwep), \ + ## args, LOG_ADD_NEWLINE(fmt)); \ + } while(0) + +enum mgwep_fsm_state { + MGWEP_ST_UNUSED, + MGWEP_ST_WAIT_MGW_RESPONSE, + MGWEP_ST_IN_USE, +}; + +enum mgwep_fsm_event { + _MGWEP_EV_LAST, + /* and MGW response events are allocated dynamically */ +}; + +struct mgw_endpoint; +struct mgwep_ci; +struct T_def; + +void mgw_endpoint_fsm_init(struct T_def *T_defs); + +struct mgw_endpoint *mgw_endpoint_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event, + struct mgcp_client *mgcp_client, + const char *fsm_id, + const char *endpoint_str_fmt, ...); + +struct mgwep_ci *mgw_endpoint_ci_add(struct mgw_endpoint *mgwep, + const char *label_fmt, ...); +const struct mgcp_conn_peer *mgwep_ci_get_rtp_info(const struct mgwep_ci *ci); +bool mgwep_ci_get_crcx_info_to_sockaddr(const struct mgwep_ci *ci, struct sockaddr_storage *dest); + +void mgw_endpoint_ci_request(struct mgwep_ci *ci, + enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data); + +static inline void mgw_endpoint_ci_dlcx(struct mgwep_ci *ci) +{ + mgw_endpoint_ci_request(ci, MGCP_VERB_DLCX, NULL, NULL, 0, 0, NULL); +} + +void mgw_endpoint_clear(struct mgw_endpoint *mgwep); + +const char *mgw_endpoint_name(const struct mgw_endpoint *mgwep); +const char *mgwep_ci_name(const struct mgwep_ci *ci); +const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info); + +enum mgcp_codecs chan_mode_to_mgcp_codec(enum gsm48_chan_mode chan_mode, bool full_rate); +void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan); diff --git a/include/osmocom/bsc/neighbor_ident.h b/include/osmocom/bsc/neighbor_ident.h index 86e062a67..bba18c3e3 100644 --- a/include/osmocom/bsc/neighbor_ident.h +++ b/include/osmocom/bsc/neighbor_ident.h @@ -57,4 +57,7 @@ void neighbor_ident_vty_write(struct vty *vty, const char *indent, struct gsm_bt "ARFCN of neighbor cell\n" "ARFCN value\n" \ "BSIC of neighbor cell\n" "9-bit BSIC of neighbor cell\n" "BSIC value\n" \ "for all BSICs / use any BSIC in this ARFCN\n" -bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv, struct neighbor_ident_key *key); +bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv, + struct neighbor_ident_key *key); +bool neighbor_ident_bts_parse_key_params(struct vty *vty, struct gsm_bts *bts, const char **argv, + struct neighbor_ident_key *key); diff --git a/include/osmocom/bsc/osmo_bsc.h b/include/osmocom/bsc/osmo_bsc.h index ed5698db4..0e19b0bbc 100644 --- a/include/osmocom/bsc/osmo_bsc.h +++ b/include/osmocom/bsc/osmo_bsc.h @@ -1,10 +1,9 @@ -/* OpenBSC BSC code */ +#pragma once -#ifndef OSMO_BSC_H -#define OSMO_BSC_H +#include -#include "bsc_api.h" -#include "bsc_msg_filter.h" +#include +#include #define BSS_SEND_USSD 1 @@ -16,6 +15,9 @@ enum bsc_con { }; struct bsc_msc_data; +struct gsm0808_channel_type; +struct gsm0808_speech_codec_list; +struct gsm_audio_support; struct bsc_api *osmo_bsc_api(); @@ -38,5 +40,3 @@ int bsc_ctrl_cmds_install(); void bsc_gen_location_state_trap(struct gsm_bts *bts); struct llist_head *bsc_access_lists(void); - -#endif diff --git a/include/osmocom/bsc/osmo_bsc_lcls.h b/include/osmocom/bsc/osmo_bsc_lcls.h index 2e6023404..3eaa5891a 100644 --- a/include/osmocom/bsc/osmo_bsc_lcls.h +++ b/include/osmocom/bsc/osmo_bsc_lcls.h @@ -38,3 +38,4 @@ void lcls_apply_config(struct gsm_subscriber_connection *conn); extern struct osmo_fsm lcls_fsm; +void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection *conn, struct msgb *msg); diff --git a/include/osmocom/bsc/timeslot_fsm.h b/include/osmocom/bsc/timeslot_fsm.h new file mode 100644 index 000000000..7ca9009ae --- /dev/null +++ b/include/osmocom/bsc/timeslot_fsm.h @@ -0,0 +1,50 @@ +/* osmo-bsc API to manage timeslot status: init and switch of dynamic PDCH. */ +#pragma once + +#include + +/* This macro automatically includes a final \n, if omitted. */ +#define LOG_TS(ts, level, fmt, args...) do { \ + if (ts->fi) \ + LOGPFSML(ts->fi, level, "%s%s%s" fmt "%s", \ + ts->pchan_is != ts->pchan_from_config ? "(pchan_is=" : "", \ + ts->pchan_is != ts->pchan_from_config ? gsm_pchan_name(ts->pchan_is) : "", \ + ts->pchan_is != ts->pchan_from_config ? ") " : "", \ +## args, \ + (!fmt || !*fmt || fmt[strlen(fmt)-1] != '\n') ? "\n" : ""); \ + else \ + LOGP(DRSL, level, "%s" fmt "%s", \ + gsm_ts_name(ts), \ + ## args, \ + (!fmt || !*fmt || fmt[strlen(fmt)-1] != '\n') ? "\n" : ""); \ + } while(0) + +enum ts_fsm_state { + TS_ST_NOT_INITIALIZED, + TS_ST_UNUSED, + TS_ST_WAIT_PDCH_ACT, + TS_ST_PDCH, + TS_ST_WAIT_PDCH_DEACT, + TS_ST_IN_USE, + TS_ST_BORKEN, +}; + +enum ts_fsm_event { + TS_EV_OML_READY, + TS_EV_OML_DOWN, + TS_EV_RSL_READY, + TS_EV_RSL_DOWN, + TS_EV_LCHAN_REQUESTED, + TS_EV_LCHAN_UNUSED, + TS_EV_PDCH_ACT_ACK, + TS_EV_PDCH_ACT_NACK, + TS_EV_PDCH_DEACT_ACK, + TS_EV_PDCH_DEACT_NACK, +}; + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts); + +bool ts_is_capable_of_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config pchan); +bool ts_is_lchan_waiting_for_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan); +bool ts_is_pchan_switching(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan); +bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config as_pchan); diff --git a/src/ipaccess/Makefile.am b/src/ipaccess/Makefile.am index ec3e02705..d73aa4d77 100644 --- a/src/ipaccess/Makefile.am +++ b/src/ipaccess/Makefile.am @@ -54,8 +54,8 @@ ipaccess_config_LDADD = \ $(top_builddir)/src/osmo-bsc/bts_ipaccess_nanobts.o \ $(top_builddir)/src/osmo-bsc/bts_ipaccess_nanobts_omlattr.o \ $(top_builddir)/src/osmo-bsc/gsm_data.o \ - $(top_builddir)/src/osmo-bsc/net_init.o \ $(top_builddir)/src/osmo-bsc/gsm_timers.o \ + $(top_builddir)/src/osmo-bsc/net_init.o \ $(OSMO_LIBS) \ $(NULL) diff --git a/src/ipaccess/stubs.c b/src/ipaccess/stubs.c index c52d52b77..cb561051a 100644 --- a/src/ipaccess/stubs.c +++ b/src/ipaccess/stubs.c @@ -41,3 +41,6 @@ void paging_flush_bts(struct gsm_bts *bts, struct bsc_msc_data *msc) { /* No paging flushing */ } + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) +{} diff --git a/src/osmo-bsc/Makefile.am b/src/osmo-bsc/Makefile.am index 86d943f4a..0fe83ba65 100644 --- a/src/osmo-bsc/Makefile.am +++ b/src/osmo-bsc/Makefile.am @@ -34,10 +34,10 @@ osmo_bsc_SOURCES = \ abis_rsl.c \ acc_ramp.c \ arfcn_range_encode.c \ + assignment_fsm.c \ bsc_api.c \ bsc_ctrl_commands.c \ bsc_ctrl_lookup.c \ - bsc_dyn_ts.c \ bsc_init.c \ bsc_rf_ctrl.c \ bsc_rll.c \ @@ -62,15 +62,18 @@ osmo_bsc_SOURCES = \ handover_cfg.c \ handover_decision.c \ handover_decision_2.c \ + handover_fsm.c \ handover_logic.c \ handover_vty.c \ + lchan_fsm.c \ + lchan_select.c \ meas_feed.c \ meas_rep.c \ + mgw_endpoint_fsm.c \ neighbor_ident.c \ neighbor_ident_vty.c \ net_init.c \ osmo_bsc_api.c \ - osmo_bsc_audio.c \ osmo_bsc_bssap.c \ osmo_bsc_ctrl.c \ osmo_bsc_filter.c \ @@ -85,6 +88,7 @@ osmo_bsc_SOURCES = \ penalty_timers.c \ rest_octets.c \ system_information.c \ + timeslot_fsm.c \ $(NULL) osmo_bsc_LDADD = \ diff --git a/src/osmo-bsc/abis_om2000.c b/src/osmo-bsc/abis_om2000.c index 293459087..9715dfc17 100644 --- a/src/osmo-bsc/abis_om2000.c +++ b/src/osmo-bsc/abis_om2000.c @@ -43,6 +43,7 @@ #include #include #include +#include #include /* FIXME: move to libosmocore */ @@ -1296,8 +1297,7 @@ static uint8_t pchan2comb(enum gsm_phys_chan_config pchan) static uint8_t ts2comb(struct gsm_bts_trx_ts *ts) { - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_PDCH: + if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) { LOGP(DNM, LOGL_ERROR, "%s pchan %s not intended for use" " with OM2000, use %s instead\n", gsm_ts_and_pchan_name(ts), @@ -1307,11 +1307,8 @@ static uint8_t ts2comb(struct gsm_bts_trx_ts *ts) * when we try to send the ip.access specific RSL PDCH Act * message for it. Rather fail completely right now: */ return 0; - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - return pchan2comb(GSM_PCHAN_TCH_F); - default: - return pchan2comb(ts->pchan); } + return pchan2comb(ts->pchan_is); } static int put_freq_list(uint8_t *buf, uint16_t arfcn) @@ -1374,7 +1371,7 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts) msgb_tv_put(msg, OM2K_DEI_EXT_RANGE, 0); /* Off */ /* Optional: Interference Rejection Combining */ msgb_tv_put(msg, OM2K_DEI_INTERF_REJ_COMB, 0x00); - switch (ts->pchan) { + switch (ts->pchan_is) { case GSM_PCHAN_CCCH: msgb_tv_put(msg, OM2K_DEI_BA_PA_MFRMS, 0x06); msgb_tv_put(msg, OM2K_DEI_BS_AG_BKS_RES, 0x01); @@ -1418,7 +1415,7 @@ int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts) msgb_tv_fixed_put(msg, OM2K_DEI_ICM_BOUND_PARAMS, sizeof(icm_bound_params), icm_bound_params); msgb_tv_put(msg, OM2K_DEI_TTA, 10); /* Timer for Time Alignment */ - if (ts->pchan == GSM_PCHAN_TCH_H) + if (ts->pchan_is == GSM_PCHAN_TCH_H) msgb_tv_put(msg, OM2K_DEI_ICM_CHAN_RATE, 1); /* TCH/H */ else msgb_tv_put(msg, OM2K_DEI_ICM_CHAN_RATE, 0); /* TCH/F */ @@ -2744,7 +2741,8 @@ void abis_om2k_trx_init(struct gsm_bts_trx *trx) struct gsm_bts_trx_ts *ts = &trx->ts[i]; om2k_mo_init(&ts->rbs2000.om2k_mo, OM2K_MO_CLS_TS, bts->nr, trx->nr, i); - gsm_ts_check_init(ts); + OSMO_ASSERT(ts->fi); + osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_READY, NULL); } } diff --git a/src/osmo-bsc/abis_rsl.c b/src/osmo-bsc/abis_rsl.c index 756a6894f..860350ccc 100644 --- a/src/osmo-bsc/abis_rsl.c +++ b/src/osmo-bsc/abis_rsl.c @@ -28,10 +28,13 @@ #include #include +#include +#include +#include #include #include #include -#include +#include #include #include #include @@ -47,6 +50,8 @@ #include #include #include +#include +#include #define RSL_ALLOC_SIZE 1024 #define RSL_ALLOC_HEADROOM 128 @@ -61,12 +66,6 @@ enum sacch_deact { SACCH_DEACTIVATE, }; -static int rsl_send_imm_assignment(struct gsm_lchan *lchan); -static void error_timeout_cb(void *data); -static int dyn_ts_switchover_continue(struct gsm_bts_trx_ts *ts); -static int dyn_ts_switchover_failed(struct gsm_bts_trx_ts *ts, int rc); -static void dyn_ts_switchover_complete(struct gsm_lchan *lchan); - static void send_lchan_signal(int sig_no, struct gsm_lchan *lchan, struct gsm_meas_rep *resp) { @@ -76,21 +75,6 @@ static void send_lchan_signal(int sig_no, struct gsm_lchan *lchan, osmo_signal_dispatch(SS_LCHAN, sig_no, &sig); } -static void do_lchan_free(struct gsm_lchan *lchan) -{ - /* We start the error timer to make the channel available again */ - if (lchan->state == LCHAN_S_REL_ERR) { - osmo_timer_setup(&lchan->error_timer, error_timeout_cb, lchan); - osmo_timer_schedule(&lchan->error_timer, - T_def_get(lchan->ts->trx->bts->network->T_defs, - 993111, T_S, -1), - 0); - } else { - rsl_lchan_set_state(lchan, LCHAN_S_NONE); - } - lchan_free(lchan); -} - static void count_codecs(struct gsm_bts *bts, struct gsm_lchan *lchan) { OSMO_ASSERT(bts); @@ -193,33 +177,20 @@ static int build_encr_info(uint8_t *out, struct gsm_lchan *lchan) return lchan->encr.key_len + 1; } -static void print_rsl_cause(int lvl, const uint8_t *cause_v, uint8_t cause_len) -{ - int i; - - LOGPC(DRSL, lvl, "CAUSE=0x%02x(%s) ", - cause_v[0], rsl_err_name(cause_v[0])); - for (i = 1; i < cause_len-1; i++) - LOGPC(DRSL, lvl, "%02x ", cause_v[i]); -} - -static void lchan_act_tmr_cb(void *data) -{ - struct gsm_lchan *lchan = data; - - rsl_lchan_mark_broken(lchan, "activation timeout"); - lchan_free(lchan); -} - -static void lchan_deact_tmr_cb(void *data) +static const char *rsl_cause_name(struct tlv_parsed *tp) { - struct gsm_lchan *lchan = data; + static char buf[128]; - rsl_lchan_mark_broken(lchan, "de-activation timeout"); - lchan_free(lchan); + if (TLVP_PRESENT(tp, RSL_IE_CAUSE)) { + const uint8_t *cause = TLVP_VAL(tp, RSL_IE_CAUSE); + snprintf(buf, sizeof(buf), " (cause=%s [ %s])", + rsl_err_name(*cause), + osmo_hexdump(cause, TLVP_LEN(tp, RSL_IE_CAUSE))); + return buf; + } else + return ""; } - /* Send a BCCH_INFO message as per Chapter 8.5.1 */ int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len) { @@ -474,59 +445,8 @@ static void mr_config_for_bts(struct gsm_lchan *lchan, struct msgb *msg) lchan->mr_bts_lv + 1); } -static enum gsm_phys_chan_config pchan_for_lchant(enum gsm_chan_t type) -{ - switch (type) { - case GSM_LCHAN_TCH_F: - return GSM_PCHAN_TCH_F; - case GSM_LCHAN_TCH_H: - return GSM_PCHAN_TCH_H; - case GSM_LCHAN_NONE: - case GSM_LCHAN_PDTCH: - /* TODO: so far lchan->type is NONE in PDCH mode. PDTCH is only - * used in osmo-bts. Maybe set PDTCH and drop the NONE case - * here. */ - return GSM_PCHAN_PDCH; - default: - return GSM_PCHAN_UNKNOWN; - } -} - -/*! Tx simplified channel activation message for non-standard PDCH type. */ -static int rsl_chan_activate_lchan_as_pdch(struct gsm_lchan *lchan) -{ - struct msgb *msg; - struct abis_rsl_dchan_hdr *dh; - - /* This might be called after release of the second lchan of a TCH/H, - * but PDCH activation must always happen on the first lchan. Make sure - * the calling code passes the correct lchan. */ - OSMO_ASSERT(lchan == lchan->ts->lchan); - - rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ); - - msg = rsl_msgb_alloc(); - dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); - init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV); - dh->chan_nr = gsm_lchan_as_pchan2chan_nr(lchan, GSM_PCHAN_PDCH); - - msgb_tv_put(msg, RSL_IE_ACT_TYPE, RSL_ACT_OSMO_PDCH); - - if (lchan->ts->trx->bts->type == GSM_BTS_TYPE_RBS2000 && - lchan->ts->trx->bts->rbs2000.use_superchannel) { - const uint8_t eric_pgsl_tmr[] = { 30, 1 }; - msgb_tv_fixed_put(msg, RSL_IE_ERIC_PGSL_TIMERS, - sizeof(eric_pgsl_tmr), eric_pgsl_tmr); - } - - msg->dst = lchan->ts->trx->rsl_link; - - return abis_rsl_sendmsg(msg); -} - /* Chapter 8.4.1 */ -int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, - uint8_t ho_ref) +int rsl_tx_chan_activ(struct gsm_lchan *lchan, uint8_t act_type, uint8_t ho_ref) { struct abis_rsl_dchan_hdr *dh; struct msgb *msg; @@ -537,75 +457,12 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, struct rsl_ie_chan_mode cm; struct gsm48_chan_desc cd; - /* If a TCH_F/PDCH TS is in PDCH mode, deactivate PDCH first. */ - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH - && (lchan->ts->flags & TS_F_PDCH_ACTIVE)) { - /* store activation type and handover reference */ - lchan->dyn.act_type = act_type; - lchan->dyn.ho_ref = ho_ref; - return rsl_ipacc_pdch_activate(lchan->ts, 0); - } - - /* - * If necessary, release PDCH on dynamic TS. Note that sending a - * release here is only necessary when in PDCH mode; for TCH types, an - * RSL RF Chan Release is initiated by the BTS when a voice call ends, - * so when we reach this, it will already be released. If a dyn TS is - * in PDCH mode, it is still active and we need to initiate a release - * from the BSC side here. - * - * If pchan_is != pchan_want, the PDCH has already been taken down and - * the switchover now needs to enable the TCH lchan. - * - * To switch a dyn TS between TCH/H and TCH/F, it is sufficient to send - * a chan activ with the new lchan type, because it will already be - * released. - */ - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && lchan->ts->dyn.pchan_is == lchan->ts->dyn.pchan_want) { - enum gsm_phys_chan_config pchan_want; - pchan_want = pchan_for_lchant(lchan->type); - if (lchan->ts->dyn.pchan_is != pchan_want) { - /* - * Make sure to record on lchan[0] so that we'll find - * it after the PDCH release. - */ - struct gsm_lchan *lchan0 = lchan->ts->lchan; - lchan0->dyn.act_type = act_type, - lchan0->dyn.ho_ref = ho_ref; - lchan0->dyn.rqd_ref = lchan->rqd_ref; - lchan0->dyn.rqd_ta = lchan->rqd_ta; - lchan->rqd_ref = NULL; - lchan->rqd_ta = 0; - DEBUGP(DRSL, "%s saved rqd_ref=%p ta=%u\n", - gsm_lchan_name(lchan0), lchan0->rqd_ref, - lchan0->rqd_ta); - return dyn_ts_switchover_start(lchan->ts, pchan_want); - } - } - DEBUGP(DRSL, "%s Tx RSL Channel Activate with act_type=%s\n", gsm_ts_and_pchan_name(lchan->ts), rsl_act_type_name(act_type)); - if (act_type == RSL_ACT_OSMO_PDCH) { - if (lchan->ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH) { - LOGP(DRSL, LOGL_ERROR, - "%s PDCH channel activation only allowed on %s\n", - gsm_ts_and_pchan_name(lchan->ts), - gsm_pchan_name(GSM_PCHAN_TCH_F_TCH_H_PDCH)); - return -EINVAL; - } - return rsl_chan_activate_lchan_as_pdch(lchan); - } - - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && lchan->ts->dyn.pchan_want == GSM_PCHAN_PDCH) { - LOGP(DRSL, LOGL_ERROR, - "%s Expected PDCH activation kind\n", - gsm_ts_and_pchan_name(lchan->ts)); - return -EINVAL; - } + /* PDCH activation is a job for rsl_tx_dyn_ts_pdch_act_deact(); */ + OSMO_ASSERT(act_type != RSL_ACT_OSMO_PDCH); rc = channel_mode_from_lchan(&cm, lchan); if (rc < 0) { @@ -615,8 +472,6 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, return rc; } - rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ); - ta = lchan->rqd_ta; /* BS11 requires TA shifted by 2 bits */ @@ -630,11 +485,7 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); init_dchan_hdr(dh, RSL_MT_CHAN_ACTIV); - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) - dh->chan_nr = gsm_lchan_as_pchan2chan_nr( - lchan, lchan->ts->dyn.pchan_want); - else - dh->chan_nr = gsm_lchan2chan_nr(lchan); + dh->chan_nr = gsm_lchan2chan_nr(lchan); msgb_tv_put(msg, RSL_IE_ACT_TYPE, act_type); msgb_tlv_put(msg, RSL_IE_CHAN_MODE, sizeof(cm), @@ -686,10 +537,7 @@ int rsl_chan_activate_lchan(struct gsm_lchan *lchan, uint8_t act_type, rate_ctr_inc(&lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_TOTAL]); - rc = abis_rsl_sendmsg(msg); - if (!rc) - rsl_lchan_set_state(lchan, LCHAN_S_ACT_REQ); - return rc; + return abis_rsl_sendmsg(msg); } /* Chapter 8.4.9: Modify channel mode on BTS side */ @@ -778,80 +626,11 @@ int rsl_deact_sacch(struct gsm_lchan *lchan) return abis_rsl_sendmsg(msg); } -static bool dyn_ts_should_switch_to_pdch(struct gsm_bts_trx_ts *ts) -{ - int ss; - - if (ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH) - return false; - - if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE) - return false; - - /* Already in PDCH mode? */ - if (ts->dyn.pchan_is == GSM_PCHAN_PDCH) - return false; - - /* See if all lchans are released. */ - for (ss = 0; ss < ts_subslots(ts); ss++) { - struct gsm_lchan *lc = &ts->lchan[ss]; - if (lc->state != LCHAN_S_NONE) { - DEBUGP(DRSL, "%s lchan %u still in use" - " (type=%s,state=%s)\n", - gsm_ts_and_pchan_name(ts), lc->nr, - gsm_lchant_name(lc->type), - gsm_lchans_name(lc->state)); - /* An lchan is still used. */ - return false; - } - } - - /* All channels are released, go to PDCH mode. */ - DEBUGP(DRSL, "%s back to PDCH\n", - gsm_ts_and_pchan_name(ts)); - return true; -} - -static void error_timeout_cb(void *data) -{ - struct gsm_lchan *lchan = data; - if (lchan->state != LCHAN_S_REL_ERR) { - LOGP(DRSL, LOGL_ERROR, "%s error timeout but not in error state: %d\n", - gsm_lchan_name(lchan), lchan->state); - return; - } - - /* go back to the none state */ - LOGP(DRSL, LOGL_INFO, "%s is back in operation.\n", gsm_lchan_name(lchan)); - rsl_lchan_set_state(lchan, LCHAN_S_NONE); - - /* Put PDCH channel back into PDCH mode, if GPRS is enabled */ - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_PDCH - && lchan->ts->trx->bts->gprs.mode != BTS_GPRS_NONE) - rsl_ipacc_pdch_activate(lchan->ts, 1); - - if (dyn_ts_should_switch_to_pdch(lchan->ts)) - dyn_ts_switchover_start(lchan->ts, GSM_PCHAN_PDCH); -} - -static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan); - /* Chapter 8.4.14 / 4.7: Tell BTS to release the radio channel */ -static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error, - enum sacch_deact deact_sacch) +int rsl_tx_rf_chan_release(struct gsm_lchan *lchan) { struct abis_rsl_dchan_hdr *dh; struct msgb *msg; - int rc; - - /* Stop timers that should lead to a channel release */ - osmo_timer_del(&lchan->T3109); - - if (lchan->state == LCHAN_S_REL_ERR) { - LOGP(DRSL, LOGL_NOTICE, "%s is in error state, not sending release.\n", - gsm_lchan_name(lchan)); - return -1; - } msg = rsl_msgb_alloc(); dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); @@ -861,157 +640,7 @@ static int rsl_rf_chan_release(struct gsm_lchan *lchan, int error, msg->lchan = lchan; msg->dst = lchan->ts->trx->rsl_link; - if (error) - DEBUGP(DRSL, "%s RF Channel Release due to error: %d\n", - gsm_lchan_name(lchan), error); - else - DEBUGP(DRSL, "%s RF Channel Release\n", gsm_lchan_name(lchan)); - - if (error) { - /* - * FIXME: GSM 04.08 gives us two options for the abnormal - * chanel release. This can be either like in the non-existent - * sub-lcuase 3.5.1 or for the main signalling link deactivate - * the SACCH, start timer T3109 and consider the channel as - * released. - * - * This code is doing the later for all raido links and not - * only the main link. Right now all SAPIs are released on the - * local end, the SACCH will be de-activated and right now the - * T3111 will be started. First T3109 should be started and then - * the T3111. - * - * TODO: Move this out of the function. - */ - - /* - * sacch de-activate and "local end release" - */ - if (deact_sacch == SACCH_DEACTIVATE) - rsl_deact_sacch(lchan); - rsl_release_sapis_from(lchan, 0, RSL_REL_LOCAL_END); - - /* - * TODO: start T3109 now. - */ - rsl_lchan_set_state(lchan, LCHAN_S_REL_ERR); - } - - /* Start another timer or assume the BTS sends a ACK/NACK? */ - osmo_timer_setup(&lchan->act_timer, lchan_deact_tmr_cb, lchan); - osmo_timer_schedule(&lchan->act_timer, 4, 0); - - rc = abis_rsl_sendmsg(msg); - - /* BTS will respond by RF CHAN REL ACK */ - return rc; -} - -/* - * Special handling for channel releases in the error case. - */ -static int rsl_rf_chan_release_err(struct gsm_lchan *lchan) -{ - enum sacch_deact sacch_deact; - if (lchan->state != LCHAN_S_ACTIVE) - return 0; - switch (ts_pchan(lchan->ts)) { - case GSM_PCHAN_TCH_F: - case GSM_PCHAN_TCH_H: - case GSM_PCHAN_CCCH_SDCCH4: - case GSM_PCHAN_CCCH_SDCCH4_CBCH: - case GSM_PCHAN_SDCCH8_SACCH8C: - case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: - sacch_deact = SACCH_DEACTIVATE; - break; - default: - sacch_deact = SACCH_NONE; - break; - } - return rsl_rf_chan_release(lchan, 1, sacch_deact); -} - -static int rsl_rx_rf_chan_rel_ack(struct gsm_lchan *lchan) -{ - struct gsm_bts_trx_ts *ts = lchan->ts; - - DEBUGP(DRSL, "%s RF CHANNEL RELEASE ACK\n", gsm_lchan_name(lchan)); - - /* Stop all pending timers */ - osmo_timer_del(&lchan->act_timer); - osmo_timer_del(&lchan->T3111); - - /* - * The BTS didn't respond within the timeout to our channel - * release request and we have marked the channel as broken. - * Now we do receive an ACK and let's be conservative. If it - * is a sysmoBTS we know that only one RF Channel Release ACK - * will be sent. So let's "repair" the channel. - */ - if (lchan->state == LCHAN_S_BROKEN) { - int do_free = is_sysmobts_v2(ts->trx->bts); - LOGP(DRSL, LOGL_NOTICE, - "%s CHAN REL ACK for broken channel. %s.\n", - gsm_lchan_name(lchan), - do_free ? "Releasing it" : "Keeping it broken"); - if (do_free) - do_lchan_free(lchan); - if (dyn_ts_should_switch_to_pdch(lchan->ts)) - dyn_ts_switchover_start(lchan->ts, GSM_PCHAN_PDCH); - return 0; - } - - if (lchan->state != LCHAN_S_REL_REQ && lchan->state != LCHAN_S_REL_ERR) - LOGP(DRSL, LOGL_NOTICE, "%s CHAN REL ACK but state %s\n", - gsm_lchan_name(lchan), - gsm_lchans_name(lchan->state)); - - do_lchan_free(lchan); - - /* - * Check Osmocom RSL CHAN ACT style dynamic TCH/F_TCH/H_PDCH TS for pending - * transitions in these cases: - * - * a) after PDCH was released due to switchover request, activate TCH. - * BSC initiated this switchover, so dyn.pchan_is != pchan_want and - * lchan->type has been set to the desired GSM_LCHAN_*. - * - * b) Voice call ended and a TCH is released. If the TS is now unused, - * switch to PDCH. Here still dyn.pchan_is == dyn.pchan_want because - * we're only just notified and may decide to switch to PDCH now. - */ - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { - DEBUGP(DRSL, "%s Rx RSL Channel Release ack for lchan %u\n", - gsm_ts_and_pchan_name(ts), lchan->nr); - - /* (a) */ - if (ts->dyn.pchan_is != ts->dyn.pchan_want) - return dyn_ts_switchover_continue(ts); - - /* (b) */ - if (dyn_ts_should_switch_to_pdch(ts)) - return dyn_ts_switchover_start(ts, GSM_PCHAN_PDCH); - } - - /* - * Put a dynamic TCH/F_PDCH channel back to PDCH mode iff it was - * released successfully. If in error, the PDCH ACT will follow after - * T3111 in error_timeout_cb(). - * - * Any state other than LCHAN_S_REL_ERR became LCHAN_S_NONE after above - * do_lchan_free(). Assert this, because that's what ensures a PDCH ACT - * on a TCH/F_PDCH TS in all cases. - * - * If GPRS is disabled, always skip the PDCH ACT. - */ - OSMO_ASSERT(lchan->state == LCHAN_S_NONE - || lchan->state == LCHAN_S_REL_ERR); - if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE) - return 0; - if (ts->pchan == GSM_PCHAN_TCH_F_PDCH - && lchan->state == LCHAN_S_NONE) - return rsl_ipacc_pdch_activate(ts, 1); - return 0; + return abis_rsl_sendmsg(msg); } int rsl_paging_cmd(struct gsm_bts *bts, uint8_t paging_group, uint8_t len, @@ -1038,6 +667,22 @@ int rsl_paging_cmd(struct gsm_bts *bts, uint8_t paging_group, uint8_t len, return abis_rsl_sendmsg(msg); } +int rsl_forward_layer3_info(struct gsm_lchan *lchan, const uint8_t *l3_info, uint8_t l3_info_len) +{ + struct msgb *msg; + uint8_t *dst; + + if (!l3_info || !l3_info_len) + return -EINVAL; + + msg = rsl_msgb_alloc(); + dst = msgb_put(msg, l3_info_len); + memcpy(dst, l3_info, l3_info_len); + + msg->lchan = lchan; + return rsl_data_request(msg, 0); +} + int imsi_str2bcd(uint8_t *bcd_out, const char *str_in) { int i, len = strlen(str_in); @@ -1160,22 +805,6 @@ int rsl_establish_request(struct gsm_lchan *lchan, uint8_t link_id) return abis_rsl_sendmsg(msg); } -static void rsl_handle_release(struct gsm_lchan *lchan); - -/* Special work handler to handle missing RSL_MT_REL_CONF message from - * Nokia InSite BTS */ -static void lchan_rel_work_cb(void *data) -{ - struct gsm_lchan *lchan = data; - int sapi; - - for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) { - if (lchan->sapis[sapi] == LCHAN_SAPI_REL) - lchan->sapis[sapi] = LCHAN_SAPI_UNUSED; - } - rsl_handle_release(lchan); -} - /* Chapter 8.3.7 Request the release of multiframe mode of RLL connection. This is what higher layers should call. The BTS then responds with RELEASE CONFIRM, which we in turn use to trigger RSL CHANNEL RELEASE, @@ -1192,8 +821,6 @@ int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id, /* 0 is normal release, 1 is local end */ msgb_tv_put(msg, RSL_IE_RELEASE_MODE, release_mode); - /* FIXME: start some timer in case we don't receive a REL ACK ? */ - msg->dst = lchan->ts->trx->rsl_link; DEBUGP(DRLL, "%s RSL RLL RELEASE REQ (link_id=0x%02x, reason=%u)\n", @@ -1201,107 +828,18 @@ int rsl_release_request(struct gsm_lchan *lchan, uint8_t link_id, abis_rsl_sendmsg(msg); - /* Do not wait for Nokia BTS to send the confirm. */ - if (is_nokia_bts(lchan->ts->trx->bts) - && lchan->ts->trx->bts->nokia.no_loc_rel_cnf - && release_mode == RSL_REL_LOCAL_END) { - DEBUGP(DRLL, "Scheduling release, becasuse Nokia InSite BTS does not send a RELease CONFirm.\n"); - lchan->sapis[link_id & 0x7] = LCHAN_SAPI_REL; - osmo_timer_setup(&lchan->rel_work, lchan_rel_work_cb, lchan); - osmo_timer_schedule(&lchan->rel_work, 0, 0); - } - - return 0; -} - -int rsl_lchan_mark_broken(struct gsm_lchan *lchan, const char *reason) -{ - LOGP(DRSL, LOGL_ERROR, "%s %s lchan broken: %s\n", - gsm_lchan_name(lchan), gsm_lchant_name(lchan->type), reason); - rsl_lchan_set_state(lchan, LCHAN_S_BROKEN); - lchan->broken_reason = reason; - return 0; -} - -int rsl_lchan_set_state_with_log(struct gsm_lchan *lchan, enum gsm_lchan_state state, const char *file, unsigned line) -{ - if (lchan->state != state) - LOGPSRC(DRSL, LOGL_DEBUG, file, line, "%s state %s -> %s\n", - gsm_lchan_name(lchan), gsm_lchans_name(lchan->state), gsm_lchans_name(state)); - - lchan->state = state; return 0; } -/* Chapter 8.4.2: Channel Activate Acknowledge */ -static int rsl_rx_chan_act_ack(struct msgb *msg) +static bool msg_for_osmocom_dyn_ts(struct msgb *msg) { struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg); - struct gsm_lchan *lchan = msg->lchan; - struct gsm_bts_trx_ts *ts = lchan->ts; - - /* BTS has confirmed channel activation, we now need - * to assign the activated channel to the MS */ - if (rslh->ie_chan != RSL_IE_CHAN_NR) - return -EINVAL; - - osmo_timer_del(&lchan->act_timer); - - if (lchan->state == LCHAN_S_BROKEN) { - int do_release = is_sysmobts_v2(ts->trx->bts); - LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK for broken channel. %s\n", - gsm_lchan_name(lchan), - do_release ? "Releasing it" : "Keeping it broken"); - if (do_release) { - talloc_free(lchan->rqd_ref); - lchan->rqd_ref = NULL; - lchan->rqd_ta = 0; - rsl_lchan_set_state(msg->lchan, LCHAN_S_ACTIVE); - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { - /* - * lchan_act_tmr_cb() already called - * lchan_free() and cleared the lchan->type, so - * calling dyn_ts_switchover_complete() here - * would not have the desired effect of - * mimicking an activated lchan that we can - * release. Instead hack the dyn ts state to - * make sure that rsl_rx_rf_chan_rel_ack() will - * switch back to PDCH, i.e. have pchan_is == - * pchan_want, both != GSM_PCHAN_PDCH: - */ - ts->dyn.pchan_is = GSM_PCHAN_NONE; - ts->dyn.pchan_want = GSM_PCHAN_NONE; - } - rsl_rf_chan_release(msg->lchan, 0, SACCH_NONE); - } - return 0; - } - - if (lchan->state != LCHAN_S_ACT_REQ) - LOGP(DRSL, LOGL_NOTICE, "%s CHAN ACT ACK, but state %s\n", - gsm_lchan_name(lchan), - gsm_lchans_name(lchan->state)); - rsl_lchan_set_state(lchan, LCHAN_S_ACTIVE); - - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) - dyn_ts_switchover_complete(lchan); - - if (lchan->rqd_ref) { - rsl_send_imm_assignment(lchan); - talloc_free(lchan->rqd_ref); - lchan->rqd_ref = NULL; - lchan->rqd_ta = 0; - } - - send_lchan_signal(S_LCHAN_ACTIVATE_ACK, lchan, NULL); - - /* Update bts attributes inside the PCU */ - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH || - ts->pchan == GSM_PCHAN_TCH_F_PDCH || - ts->pchan == GSM_PCHAN_PDCH) - pcu_info_update(ts->trx->bts); - - return 0; + if (msg->lchan->ts->pchan_on_init != GSM_PCHAN_TCH_F_TCH_H_PDCH) + return false; + /* dyn TS messages always come in on the first lchan of a timeslot */ + if (msg->lchan->nr != 0) + return false; + return (rslh->chan_nr & RSL_CHAN_OSMO_PDCH) == RSL_CHAN_OSMO_PDCH; } /* Chapter 8.4.3: Channel Activate NACK */ @@ -1309,46 +847,24 @@ static int rsl_rx_chan_act_nack(struct msgb *msg) { struct abis_rsl_dchan_hdr *dh = msgb_l2(msg); struct tlv_parsed tp; - - osmo_timer_del(&msg->lchan->act_timer); + struct gsm_lchan *lchan = msg->lchan; + const uint8_t *cause = NULL; rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_NACK]); - if (msg->lchan->state == LCHAN_S_BROKEN) { - LOGP(DRSL, LOGL_ERROR, - "%s CHANNEL ACTIVATE NACK for broken channel.\n", - gsm_lchan_name(msg->lchan)); - return -1; - } - - /* BTS has rejected channel activation ?!? */ if (dh->ie_chan != RSL_IE_CHAN_NR) { - LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK, and chan nr mismatches\n", - gsm_lchan_name(msg->lchan)); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "Invalid IE: expected CHAN_NR IE (0x%x), got 0x%x", + RSL_IE_CHAN_NR, dh->ie_chan); return -EINVAL; } rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh)); - if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) { - const uint8_t *cause = TLVP_VAL(&tp, RSL_IE_CAUSE); - LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK: ", - gsm_lchan_name(msg->lchan)); - print_rsl_cause(LOGL_ERROR, cause, - TLVP_LEN(&tp, RSL_IE_CAUSE)); - LOGPC(DRSL, LOGL_ERROR, "\n"); - msg->lchan->error_cause = *cause; - if (*cause != RSL_ERR_RCH_ALR_ACTV_ALLOC) { - rsl_lchan_mark_broken(msg->lchan, "NACK on activation"); - } else - rsl_rf_chan_release(msg->lchan, 1, SACCH_DEACTIVATE); - - } else { - LOGP(DRSL, LOGL_ERROR, "%s CHANNEL ACTIVATE NACK, no cause IE\n", - gsm_lchan_name(msg->lchan)); - rsl_lchan_mark_broken(msg->lchan, "NACK on activation no IE"); - } + LOG_LCHAN(lchan, LOGL_ERROR, "CHANNEL ACTIVATE NACK%s", rsl_cause_name(&tp)); - send_lchan_signal(S_LCHAN_ACTIVATE_NACK, msg->lchan, NULL); + if (msg_for_osmocom_dyn_ts(msg)) + osmo_fsm_inst_dispatch(lchan->ts->fi, TS_EV_PDCH_ACT_NACK, (void*)cause); + else + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_CHAN_ACTIV_NACK, (void*)cause); return 0; } @@ -1360,22 +876,15 @@ static int rsl_rx_conn_fail(struct msgb *msg) struct tlv_parsed tp; uint8_t cause = 0; - LOGP(DRSL, LOGL_NOTICE, "%s CONNECTION FAIL in state %s ", - gsm_lchan_name(msg->lchan), - gsm_lchans_name(msg->lchan->state)); - rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh)); - if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) { - print_rsl_cause(LOGL_NOTICE, TLVP_VAL(&tp, RSL_IE_CAUSE), - TLVP_LEN(&tp, RSL_IE_CAUSE)); - cause = *TLVP_VAL(&tp, RSL_IE_CAUSE); - } + LOG_LCHAN(lchan, LOGL_ERROR, "CONNECTION FAIL%s", rsl_cause_name(&tp)); - LOGPC(DRSL, LOGL_NOTICE, "\n"); rate_ctr_inc(&lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RF_FAIL]); - if (lchan->conn) + if (!lchan->conn) { + lchan_release(lchan, false, true, cause); + } else osmo_fsm_inst_dispatch(lchan->conn->fi, GSCON_EV_RSL_CONN_FAIL, &cause); return 0; @@ -1462,11 +971,8 @@ static int rsl_rx_meas_res(struct msgb *msg) const uint8_t *val; int rc; - /* check if this channel is actually active */ - /* FIXME: maybe this check should be way more generic/centralized */ - if (msg->lchan->state != LCHAN_S_ACTIVE) { - LOGP(DRSL, LOGL_DEBUG, "%s: MEAS RES for inactive channel\n", - gsm_lchan_name(msg->lchan)); + if (!lchan_may_receive_data(msg->lchan)) { + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "MEAS RES for inactive channel"); return 0; } @@ -1546,76 +1052,37 @@ static int rsl_rx_hando_det(struct msgb *msg) { struct abis_rsl_dchan_hdr *dh = msgb_l2(msg); struct tlv_parsed tp; - - DEBUGP(DRSL, "%s HANDOVER DETECT ", gsm_lchan_name(msg->lchan)); + struct handover_rr_detect_data d = { + .msg = msg, + }; rsl_tlv_parse(&tp, dh->data, msgb_l2len(msg)-sizeof(*dh)); if (TLVP_PRESENT(&tp, RSL_IE_ACCESS_DELAY)) - DEBUGPC(DRSL, "access delay = %u\n", - *TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY)); - else - DEBUGPC(DRSL, "\n"); - - send_lchan_signal(S_LCHAN_HANDOVER_DETECT, msg->lchan, NULL); - - return 0; -} - -static bool lchan_may_change_pdch(struct gsm_lchan *lchan, bool pdch_act) -{ - struct gsm_bts_trx_ts *ts; - - OSMO_ASSERT(lchan); - - ts = lchan->ts; - OSMO_ASSERT(ts); - OSMO_ASSERT(ts->trx); - OSMO_ASSERT(ts->trx->bts); - - if (lchan->ts->pchan != GSM_PCHAN_TCH_F_PDCH) { - LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK" - " for channel that is no TCH/F_PDCH\n", - gsm_lchan_name(lchan), - gsm_pchan_name(ts->pchan), - pdch_act? "ACT" : "DEACT"); - return false; - } + d.access_delay = TLVP_VAL(&tp, RSL_IE_ACCESS_DELAY); - if (lchan->state != LCHAN_S_NONE) { - LOGP(DRSL, LOGL_ERROR, "%s pchan=%s Rx PDCH %s ACK" - " in unexpected state: %s\n", - gsm_lchan_name(lchan), - gsm_pchan_name(ts->pchan), - pdch_act? "ACT" : "DEACT", - gsm_lchans_name(lchan->state)); - return false; + if (!msg->lchan->conn || !msg->lchan->conn->ho.fi) { + LOGP(DRSL, LOGL_ERROR, "%s HANDOVER DETECT but no handover is ongoing\n", + gsm_lchan_name(msg->lchan)); + return 0; } - return true; -} - -static int rsl_rx_pdch_act_ack(struct msgb *msg) -{ - if (!lchan_may_change_pdch(msg->lchan, true)) - return -EINVAL; - msg->lchan->ts->flags |= TS_F_PDCH_ACTIVE; - msg->lchan->ts->flags &= ~TS_F_PDCH_ACT_PENDING; + osmo_fsm_inst_dispatch(msg->lchan->conn->ho.fi, HO_EV_RR_HO_DETECT, &d); return 0; } -static int rsl_rx_pdch_deact_ack(struct msgb *msg) +static int rsl_rx_ipacc_pdch(struct msgb *msg, char *name, uint32_t ts_ev) { - if (!lchan_may_change_pdch(msg->lchan, false)) - return -EINVAL; + struct gsm_bts_trx_ts *ts = msg->lchan->ts; - msg->lchan->ts->flags &= ~TS_F_PDCH_ACTIVE; - msg->lchan->ts->flags &= ~TS_F_PDCH_DEACT_PENDING; - - rsl_chan_activate_lchan(msg->lchan, msg->lchan->dyn.act_type, - msg->lchan->dyn.ho_ref); + if (ts->pchan_on_init != GSM_PCHAN_TCH_F_PDCH) { + LOG_TS(ts, LOGL_ERROR, "Rx RSL ip.access PDCH %s acceptable only for %s", + name, gsm_pchan_name(GSM_PCHAN_TCH_F_PDCH)); + return -EINVAL; + } + osmo_fsm_inst_dispatch(ts->fi, ts_ev, NULL); return 0; } @@ -1623,20 +1090,39 @@ static int abis_rsl_rx_dchan(struct msgb *msg) { struct abis_rsl_dchan_hdr *rslh = msgb_l2(msg); int rc = 0; - char *ts_name; struct e1inp_sign_link *sign_link = msg->dst; + if (rslh->ie_chan != RSL_IE_CHAN_NR) { + LOGP(DRSL, LOGL_ERROR, + "Rx RSL DCHAN: invalid RSL header, expecting Channel Number IE tag, got 0x%x\n", + rslh->ie_chan); + return -EINVAL; + } + msg->lchan = lchan_lookup(sign_link->trx, rslh->chan_nr, "Abis RSL rx DCHAN: "); - if (!msg->lchan) - return -1; - ts_name = gsm_lchan_name(msg->lchan); + if (!msg->lchan) { + LOGP(DRSL, LOGL_ERROR, + "Rx RSL DCHAN: unable to match RSL message to an lchan: chan_nr=0x%x\n", + rslh->chan_nr); + return -EINVAL; + } + + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "Rx %s", rsl_or_ipac_msg_name(rslh->c.msg_type)); + + if (!msg->lchan->fi) { + LOG_LCHAN(msg->lchan, LOGL_ERROR, "Rx RSL DCHAN: RSL message for unconfigured lchan"); + return -EINVAL; + } switch (rslh->c.msg_type) { case RSL_MT_CHAN_ACTIV_ACK: - DEBUGP(DRSL, "%s CHANNEL ACTIVATE ACK\n", ts_name); - rc = rsl_rx_chan_act_ack(msg); - count_codecs(sign_link->trx->bts, msg->lchan); + if (msg_for_osmocom_dyn_ts(msg)) + osmo_fsm_inst_dispatch(msg->lchan->ts->fi, TS_EV_PDCH_ACT_ACK, NULL); + else { + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RSL_CHAN_ACTIV_ACK, NULL); + count_codecs(sign_link->trx->bts, msg->lchan); + } break; case RSL_MT_CHAN_ACTIV_NACK: rc = rsl_rx_chan_act_nack(msg); @@ -1651,31 +1137,30 @@ static int abis_rsl_rx_dchan(struct msgb *msg) rc = rsl_rx_hando_det(msg); break; case RSL_MT_RF_CHAN_REL_ACK: - rc = rsl_rx_rf_chan_rel_ack(msg->lchan); + if (msg_for_osmocom_dyn_ts(msg)) + osmo_fsm_inst_dispatch(msg->lchan->ts->fi, TS_EV_PDCH_DEACT_ACK, NULL); + else + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RSL_RF_CHAN_REL_ACK, NULL); break; case RSL_MT_MODE_MODIFY_ACK: + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "CHANNEL MODE MODIFY ACK"); count_codecs(sign_link->trx->bts, msg->lchan); - DEBUGP(DRSL, "%s CHANNEL MODE MODIFY ACK\n", ts_name); break; case RSL_MT_MODE_MODIFY_NACK: - LOGP(DRSL, LOGL_ERROR, "%s CHANNEL MODE MODIFY NACK\n", ts_name); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "CHANNEL MODE MODIFY NACK"); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_MODE_MODIFY_NACK]); break; case RSL_MT_IPAC_PDCH_ACT_ACK: - DEBUGP(DRSL, "%s IPAC PDCH ACT ACK\n", ts_name); - rc = rsl_rx_pdch_act_ack(msg); + rc = rsl_rx_ipacc_pdch(msg, "ACT ACK", TS_EV_PDCH_ACT_ACK); break; case RSL_MT_IPAC_PDCH_ACT_NACK: - LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH ACT NACK\n", ts_name); - rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + rc = rsl_rx_ipacc_pdch(msg, "ACT NACK", TS_EV_PDCH_ACT_NACK); break; case RSL_MT_IPAC_PDCH_DEACT_ACK: - DEBUGP(DRSL, "%s IPAC PDCH DEACT ACK\n", ts_name); - rc = rsl_rx_pdch_deact_ack(msg); + rc = rsl_rx_ipacc_pdch(msg, "DEACT ACK", TS_EV_PDCH_DEACT_ACK); break; case RSL_MT_IPAC_PDCH_DEACT_NACK: - LOGP(DRSL, LOGL_ERROR, "%s IPAC PDCH DEACT NACK\n", ts_name); - rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + rc = rsl_rx_ipacc_pdch(msg, "DEACT NACK", TS_EV_PDCH_DEACT_NACK); break; case RSL_MT_PHY_CONTEXT_CONF: case RSL_MT_PREPROC_MEAS_RES: @@ -1685,13 +1170,13 @@ static int abis_rsl_rx_dchan(struct msgb *msg) case RSL_MT_MR_CODEC_MOD_ACK: case RSL_MT_MR_CODEC_MOD_NACK: case RSL_MT_MR_CODEC_MOD_PER: - LOGP(DRSL, LOGL_NOTICE, "%s Unimplemented Abis RSL DChan " - "msg 0x%02x\n", ts_name, rslh->c.msg_type); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Unimplemented Abis RSL DChan msg 0x%02x\n", + rslh->c.msg_type); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]); break; default: - LOGP(DRSL, LOGL_NOTICE, "%s unknown Abis RSL DChan msg 0x%02x\n", - ts_name, rslh->c.msg_type); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Unknown Abis RSL DChan msg 0x%02x\n", + rslh->c.msg_type); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]); return -EINVAL; } @@ -1705,15 +1190,10 @@ static int rsl_rx_error_rep(struct msgb *msg) struct tlv_parsed tp; struct e1inp_sign_link *sign_link = msg->dst; - LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT ", gsm_trx_name(sign_link->trx)); - rsl_tlv_parse(&tp, rslh->data, msgb_l2len(msg)-sizeof(*rslh)); - if (TLVP_PRESENT(&tp, RSL_IE_CAUSE)) - print_rsl_cause(LOGL_ERROR, TLVP_VAL(&tp, RSL_IE_CAUSE), - TLVP_LEN(&tp, RSL_IE_CAUSE)); - - LOGPC(DRSL, LOGL_ERROR, "\n"); + LOGP(DRSL, LOGL_ERROR, "%s ERROR REPORT%s\n", + gsm_trx_name(sign_link->trx), rsl_cause_name(&tp)); return 0; } @@ -1752,36 +1232,6 @@ static int abis_rsl_rx_trx(struct msgb *msg) return rc; } -/* If T3101 expires, we never received a response to IMMEDIATE ASSIGN */ -static void t3101_expired(void *data) -{ - struct gsm_lchan *lchan = data; - LOGP(DRSL, LOGL_NOTICE, - "%s T3101 expired: no response to IMMEDIATE ASSIGN\n", - gsm_lchan_name(lchan)); - rsl_rf_chan_release(lchan, 1, SACCH_DEACTIVATE); -} - -/* If T3111 expires, we will send the RF Channel Request */ -static void t3111_expired(void *data) -{ - struct gsm_lchan *lchan = data; - LOGP(DRSL, LOGL_NOTICE, - "%s T3111 expired: releasing RF Channel\n", - gsm_lchan_name(lchan)); - rsl_rf_chan_release(lchan, 0, SACCH_NONE); -} - -/* If T3109 expires the MS has not send a UA/UM do the error release */ -static void t3109_expired(void *data) -{ - struct gsm_lchan *lchan = data; - - LOGP(DRSL, LOGL_ERROR, - "%s SACCH deactivation timeout.\n", gsm_lchan_name(lchan)); - rsl_rf_chan_release(lchan, 1, SACCH_NONE); -} - /* Format an IMM ASS REJ according to 04.08 Chapter 9.1.20 */ static int rsl_send_imm_ass_rej(struct gsm_bts *bts, struct gsm48_req_ref *rqd_ref, @@ -1817,6 +1267,18 @@ static int rsl_send_imm_ass_rej(struct gsm_bts *bts, return rsl_imm_assign_cmd(bts, sizeof(*iar), (uint8_t *) iar); } +int rsl_tx_imm_ass_rej(struct gsm_bts *bts, struct gsm48_req_ref *rqd_ref) +{ + uint8_t wait_ind; + wait_ind = bts->T3122; + if (!wait_ind) + wait_ind = T_def_get(bts->network->T_defs, 3122, T_S, -1); + if (!wait_ind) + wait_ind = GSM_T3122_DEFAULT; + /* The BTS will gather multiple CHAN RQD and reject up to 4 MS at the same time. */ + return rsl_send_imm_ass_rej(bts, rqd_ref, wait_ind); +} + /* Handle packet channel rach requests */ static int rsl_rx_pchan_rqd(struct msgb *msg, struct gsm_bts *bts) { @@ -1862,9 +1324,6 @@ static int rsl_rx_chan_rqd(struct msgb *msg) struct gsm_lchan *lchan; uint8_t rqd_ta; - uint16_t arfcn; - uint8_t subch; - /* parse request reference to be used in immediate assign */ if (rqd_hdr->data[0] != RSL_IE_REQ_REFERENCE) return -EINVAL; @@ -1904,78 +1363,44 @@ static int rsl_rx_chan_rqd(struct msgb *msg) * Note: If the MS requests not TCH/H, we don't know if the phone * supports TCH/H, so we must assign TCH/F or SDCCH. */ - lchan = lchan_alloc(bts, GSM_LCHAN_SDCCH, 0); + lchan = lchan_select_by_type(bts, GSM_LCHAN_SDCCH); if (!lchan && lctype != GSM_LCHAN_SDCCH) { LOGP(DRSL, LOGL_NOTICE, "(bts=%d) CHAN RQD: no resources for %s " "0x%x, retrying with %s\n", msg->lchan->ts->trx->bts->nr, gsm_lchant_name(GSM_LCHAN_SDCCH), rqd_ref->ra, gsm_lchant_name(lctype)); - lchan = lchan_alloc(bts, lctype, 0); + lchan = lchan_select_by_type(bts, lctype); } if (!lchan) { - uint8_t wait_ind; LOGP(DRSL, LOGL_NOTICE, "(bts=%d) CHAN RQD: no resources for %s 0x%x\n", msg->lchan->ts->trx->bts->nr, gsm_lchant_name(lctype), rqd_ref->ra); rate_ctr_inc(&bts->bts_ctrs->ctr[BTS_CTR_CHREQ_NO_CHANNEL]); - wait_ind = bts->T3122; - if (!wait_ind) - wait_ind = T_def_get(bts->network->T_defs, 3122, T_S, -1); - if (!wait_ind) - wait_ind = GSM_T3122_DEFAULT; - /* The BTS will gather multiple CHAN RQD and reject up to 4 MS at the same time. */ - rsl_send_imm_ass_rej(bts, rqd_ref, wait_ind); + rsl_tx_imm_ass_rej(bts, rqd_ref); return 0; } - /* - * Expecting lchan state to be NONE, except for dyn TS in PDCH mode. - * Those are expected to be ACTIVE: the PDCH release will be sent from - * rsl_chan_activate_lchan() below. - */ - if (lchan->state != LCHAN_S_NONE - && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && lchan->ts->dyn.pchan_is == GSM_PCHAN_PDCH - && lchan->state == LCHAN_S_ACTIVE)) - LOGP(DRSL, LOGL_NOTICE, "%s lchan_alloc() returned channel " - "in state %s\n", gsm_lchan_name(lchan), - gsm_lchans_name(lchan->state)); - /* save the RACH data as we need it after the CHAN ACT ACK */ lchan->rqd_ref = talloc_zero(bts, struct gsm48_req_ref); - if (!lchan->rqd_ref) { - LOGP(DRSL, LOGL_ERROR, "Failed to allocate gsm48_req_ref.\n"); - lchan_free(lchan); - return -ENOMEM; - } + OSMO_ASSERT(lchan->rqd_ref); - memcpy(lchan->rqd_ref, rqd_ref, sizeof(*rqd_ref)); + *(lchan->rqd_ref) = *rqd_ref; lchan->rqd_ta = rqd_ta; - arfcn = lchan->ts->trx->arfcn; - subch = lchan->nr; - - lchan->encr.alg_id = RSL_ENC_ALG_A5(0); /* no encryption */ - lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power); - lchan->bs_power = 0; /* 0dB reduction, output power = Pn */ - lchan->rsl_cmode = RSL_CMOD_SPD_SIGN; - lchan->tch_mode = GSM48_CMODE_SIGN; - - /* Start another timer or assume the BTS sends a ACK/NACK? */ - osmo_timer_setup(&lchan->act_timer, lchan_act_tmr_cb, lchan); - osmo_timer_schedule(&lchan->act_timer, 4, 0); - - DEBUGP(DRSL, "%s Activating ARFCN(%u) SS(%u) lctype %s " - "r=%s ra=0x%02x ta=%d\n", gsm_lchan_name(lchan), arfcn, subch, - gsm_lchant_name(lchan->type), gsm_chreq_name(chreq_reason), - rqd_ref->ra, rqd_ta); - - rsl_chan_activate_lchan(lchan, RSL_ACT_INTRA_IMM_ASS, 0); + LOG_LCHAN(lchan, LOGL_DEBUG, "MS: Channel Request: reason=%s ra=0x%02x ta=%d\n", + gsm_chreq_name(chreq_reason), rqd_ref->ra, rqd_ta); + { + struct lchan_activate_info info = { + .activ_for = FOR_MS_CHANNEL_REQUEST, + .chan_mode = GSM48_CMODE_SIGN, + }; + lchan_activate(lchan, &info); + } return 0; } -static int rsl_send_imm_assignment(struct gsm_lchan *lchan) +int rsl_tx_imm_assignment(struct gsm_lchan *lchan) { struct gsm_bts *bts = lchan->ts->trx->bts; uint8_t buf[GSM_MACBLOCK_LEN]; @@ -2001,10 +1426,6 @@ static int rsl_send_imm_assignment(struct gsm_lchan *lchan) /* we need to subtract 1 byte from sizeof(*ia) since ia includes the l2_plen field */ ia->l2_plen = GSM48_LEN2PLEN((sizeof(*ia)-1) + ia->mob_alloc_len); - /* Start timer T3101 to wait for GSM48_MT_RR_PAG_RESP */ - osmo_timer_setup(&lchan->T3101, t3101_expired, lchan); - osmo_timer_schedule(&lchan->T3101, T_def_get(bts->network->T_defs, 3101, T_S, -1), 0); - /* send IMMEDIATE ASSIGN CMD on RSL to BTS (to send on CCCH to MS) */ return rsl_imm_assign_cmd(bts, sizeof(*ia)+ia->mob_alloc_len, (uint8_t *) ia); } @@ -2115,55 +1536,20 @@ static int rsl_rx_rll_err_ind(struct msgb *msg) rsl_tlv_parse(&tp, rllh->data, msgb_l2len(msg) - sizeof(*rllh)); if (!TLVP_PRESENT(&tp, RSL_IE_RLM_CAUSE)) { - LOGP(DRLL, LOGL_ERROR, - "%s ERROR INDICATION without mandantory cause.\n", - gsm_lchan_name(msg->lchan)); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "ERROR INDICATION without mandantory cause."); return -1; } rlm_cause = *TLVP_VAL(&tp, RSL_IE_RLM_CAUSE); - LOGP(DRLL, LOGL_ERROR, "%s ERROR INDICATION cause=%s in state=%s\n", - gsm_lchan_name(msg->lchan), - rsl_rlm_cause_name(rlm_cause), - gsm_lchans_name(msg->lchan->state)); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "ERROR INDICATION cause=%s\n", rsl_rlm_cause_name(rlm_cause)); rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_ERR_IND); - if (rlm_cause == RLL_CAUSE_T200_EXPIRED) { - rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RLL_ERR]); - return rsl_rf_chan_release_err(msg->lchan); - } - - return 0; -} + rate_ctr_inc(&msg->lchan->ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_RLL_ERR]); -static void rsl_handle_release(struct gsm_lchan *lchan) -{ - int sapi; - struct gsm_bts *bts; - - /* - * Maybe only one link/SAPI was releasd or the error handling - * was activated. Just return now and let the other code handle - * it. - */ - if (lchan->state != LCHAN_S_REL_REQ) - return; - - for (sapi = 0; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) { - if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) - continue; - LOGP(DRSL, LOGL_DEBUG, "%s waiting for SAPI=%d to be released.\n", - gsm_lchan_name(lchan), sapi); - return; - } + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_ERR_IND, &rlm_cause); - - /* Stop T3109 and wait for T3111 before re-using the channel */ - osmo_timer_del(&lchan->T3109); - osmo_timer_setup(&lchan->T3111, t3111_expired, lchan); - bts = lchan->ts->trx->bts; - osmo_timer_schedule(&lchan->T3111, T_def_get(bts->network->T_defs, 3111, T_S, -1), 0); + return 0; } /* ESTABLISH INDICATION, LOCATION AREA UPDATE REQUEST @@ -2177,17 +1563,13 @@ static int abis_rsl_rx_rll(struct msgb *msg) struct e1inp_sign_link *sign_link = msg->dst; struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); int rc = 0; - char *ts_name; - uint8_t sapi = rllh->link_id & 7; + uint8_t sapi = rllh->link_id & 0x7; - msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr, - "Abis RSL rx RLL: "); - ts_name = gsm_lchan_name(msg->lchan); - DEBUGP(DRLL, "%s SAPI=%u ", ts_name, sapi); + msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr, "Abis RSL rx RLL: "); switch (rllh->c.msg_type) { case RSL_MT_DATA_IND: - DEBUGPC(DRLL, "DATA INDICATION\n"); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "SAPI=%u DATA INDICATION", sapi); if (msgb_l2len(msg) > sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) && rllh->data[0] == RSL_IE_L3_INFO) { @@ -2196,16 +1578,16 @@ static int abis_rsl_rx_rll(struct msgb *msg) } break; case RSL_MT_EST_IND: - DEBUGPC(DRLL, "ESTABLISH INDICATION\n"); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "SAPI=%u ESTABLISH INDICATION", sapi); /* lchan is established, stop T3101 */ /* Note: By definition the first Establish Indication must * happen first on SAPI 0, once the connection on SAPI 0 is * made, parallel connections on other SAPIs are permitted */ if (sapi != 0 && msg->lchan->sapis[0] != LCHAN_SAPI_MS) { - LOGP(DRLL, LOGL_NOTICE, "MS attempted to establish DCCH on SAPI=%d (expected SAPI=0)\n", - rllh->link_id & 0x7); - + LOG_LCHAN(msg->lchan, LOGL_NOTICE, + "MS attempted to establish DCCH on SAPI=%d (expected SAPI=0)\n", + sapi); /* Note: We do not need to close the channel, * since we might still get a proper Establish Ind. * If not, T3101 will close the channel on timeout. */ @@ -2219,7 +1601,8 @@ static int abis_rsl_rx_rll(struct msgb *msg) * (see also 3GPP TS 44.005, figure 5) So we have to drop such * Establish Indications */ if (sapi == 0 && (rllh->link_id >> 6 & 0x03) == 1) { - LOGP(DRLL, LOGL_NOTICE, "MS attempted to establish an SACCH in MF mode on SAPI=0 (not permitted)\n"); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, + "MS attempted to establish an SACCH in MF mode on SAPI=0 (not permitted)\n"); /* Note: We do not need to close the channel, * since we might still get a proper Establish Ind. @@ -2227,8 +1610,9 @@ static int abis_rsl_rx_rll(struct msgb *msg) break; } - msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_MS; - osmo_timer_del(&msg->lchan->T3101); + msg->lchan->sapis[sapi] = LCHAN_SAPI_MS; + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, msg); + if (msgb_l2len(msg) > sizeof(struct abis_rsl_common_hdr) + sizeof(*rllh) && rllh->data[0] == RSL_IE_L3_INFO) { @@ -2237,52 +1621,42 @@ static int abis_rsl_rx_rll(struct msgb *msg) } break; case RSL_MT_EST_CONF: - DEBUGPC(DRLL, "ESTABLISH CONFIRM\n"); - msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_NET; + LOG_LCHAN(msg->lchan, LOGL_ERROR, "SAPI=%u ESTABLISH CONFIRM\n", sapi); + msg->lchan->sapis[sapi] = LCHAN_SAPI_NET; rll_indication(msg->lchan, rllh->link_id, BSC_RLLR_IND_EST_CONF); break; case RSL_MT_REL_IND: /* BTS informs us of having received DISC from MS */ - DEBUGPC(DRLL, "RELEASE INDICATION\n"); - msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED; - rll_indication(msg->lchan, rllh->link_id, - BSC_RLLR_IND_REL_IND); - rsl_handle_release(msg->lchan); - /* if it was the main signalling link, let the subscr_conn_fsm know */ - if (msg->lchan->conn && sapi == 0 && (rllh->link_id >> 6) == 0) - osmo_fsm_inst_dispatch(msg->lchan->conn->fi, GSCON_EV_RLL_REL_IND, msg); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_REL_IND, &rllh->link_id); break; case RSL_MT_REL_CONF: /* BTS informs us of having received UA from MS, * in response to DISC that we've sent earlier */ - DEBUGPC(DRLL, "RELEASE CONFIRMATION\n"); - msg->lchan->sapis[rllh->link_id & 0x7] = LCHAN_SAPI_UNUSED; - rsl_handle_release(msg->lchan); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_RLL_REL_CONF, &rllh->link_id); break; case RSL_MT_ERROR_IND: - DEBUGPC(DRLL, "ERROR INDICATION\n"); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "SAPI=%u ERROR INDICATION\n", sapi); rc = rsl_rx_rll_err_ind(msg); break; case RSL_MT_UNIT_DATA_IND: - DEBUGPC(DRLL, "UNIT DATA INDICATION\n"); - LOGP(DRLL, LOGL_NOTICE, "unimplemented Abis RLL message " - "type 0x%02x\n", rllh->c.msg_type); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "SAPI=%u UNIT DATA INDICATION:" + " unimplemented Abis RLL message type 0x%02x\n", sapi, rllh->c.msg_type); break; default: - DEBUGPC(DRLL, "UNKNOWN\n"); - LOGP(DRLL, LOGL_NOTICE, "unknown Abis RLL message " - "type 0x%02x\n", rllh->c.msg_type); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "SAPI=%u Unknown Abis RLL message type 0x%02x\n", + sapi, rllh->c.msg_type); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]); } return rc; } -static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan) +/* Return an ip.access BTS speech mode value (uint8_t) or negative on error. */ +int ipacc_speech_mode(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type) { - switch (lchan->tch_mode) { + switch (tch_mode) { case GSM48_CMODE_SPEECH_V1: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: return 0x00; case GSM_LCHAN_TCH_H: @@ -2292,7 +1666,7 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan) } break; case GSM48_CMODE_SPEECH_EFR: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: return 0x01; /* there's no half-rate EFR */ @@ -2301,7 +1675,7 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan) } break; case GSM48_CMODE_SPEECH_AMR: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: return 0x02; case GSM_LCHAN_TCH_H: @@ -2313,16 +1687,24 @@ static uint8_t ipa_smod_s_for_lchan(struct gsm_lchan *lchan) default: break; } - LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access speech mode for " - "tch_mode == 0x%02x\n", lchan->tch_mode); - return 0; + return -EINVAL; } -static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan) +void ipacc_speech_mode_set_direction(uint8_t *speech_mode, bool send) { - switch (lchan->tch_mode) { + const uint8_t recv_only_flag = 0x10; + if (send) + *speech_mode = *speech_mode & ~recv_only_flag; + else + *speech_mode = *speech_mode | recv_only_flag; +} + +/* Return an ip.access BTS payload type value (uint8_t) or negative on error. */ +int ipacc_payload_type(enum gsm48_chan_mode tch_mode, enum gsm_chan_t type) +{ + switch (tch_mode) { case GSM48_CMODE_SPEECH_V1: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: return RTP_PT_GSM_FULL; case GSM_LCHAN_TCH_H: @@ -2332,7 +1714,7 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan) } break; case GSM48_CMODE_SPEECH_EFR: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: return RTP_PT_GSM_EFR; /* there's no half-rate EFR */ @@ -2341,7 +1723,7 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan) } break; case GSM48_CMODE_SPEECH_AMR: - switch (lchan->type) { + switch (type) { case GSM_LCHAN_TCH_F: case GSM_LCHAN_TCH_H: return RTP_PT_AMR; @@ -2352,72 +1734,70 @@ static uint8_t ipa_rtp_pt_for_lchan(struct gsm_lchan *lchan) default: break; } - LOGP(DRSL, LOGL_ERROR, "Cannot determine ip.access rtp payload type for " - "tch_mode == 0x%02x\n & lchan_type == %d", - lchan->tch_mode, lchan->type); - return 0; + return -EINVAL; +} + +static const char *ip_to_a(uint32_t ip) +{ + struct in_addr ia; + ia.s_addr = htonl(ip); + return inet_ntoa(ia); } /* ip.access specific RSL extensions */ -static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv) +static void ipac_parse_rtp(struct gsm_lchan *lchan, struct tlv_parsed *tv, const char *label) { struct in_addr ip; uint16_t port, conn_id; if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_IP)) { ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_LOCAL_IP); - DEBUGPC(DRSL, "LOCAL_IP=%s ", inet_ntoa(ip)); lchan->abis_ip.bound_ip = ntohl(ip.s_addr); } if (TLVP_PRESENT(tv, RSL_IE_IPAC_LOCAL_PORT)) { port = tlvp_val16_unal(tv, RSL_IE_IPAC_LOCAL_PORT); port = ntohs(port); - DEBUGPC(DRSL, "LOCAL_PORT=%u ", port); lchan->abis_ip.bound_port = port; } if (TLVP_PRESENT(tv, RSL_IE_IPAC_CONN_ID)) { conn_id = tlvp_val16_unal(tv, RSL_IE_IPAC_CONN_ID); conn_id = ntohs(conn_id); - DEBUGPC(DRSL, "CON_ID=%u ", conn_id); lchan->abis_ip.conn_id = conn_id; } if (TLVP_PRESENT(tv, RSL_IE_IPAC_RTP_PAYLOAD2)) { lchan->abis_ip.rtp_payload2 = *TLVP_VAL(tv, RSL_IE_IPAC_RTP_PAYLOAD2); - DEBUGPC(DRSL, "RTP_PAYLOAD2=0x%02x ", - lchan->abis_ip.rtp_payload2); } if (TLVP_PRESENT(tv, RSL_IE_IPAC_SPEECH_MODE)) { lchan->abis_ip.speech_mode = *TLVP_VAL(tv, RSL_IE_IPAC_SPEECH_MODE); - DEBUGPC(DRSL, "speech_mode=0x%02x ", - lchan->abis_ip.speech_mode); } + /* Why would we receive the MGW IP and port back from the BTS, and why would we care?? */ if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_IP)) { ip.s_addr = tlvp_val32_unal(tv, RSL_IE_IPAC_REMOTE_IP); - DEBUGPC(DRSL, "REMOTE_IP=%s ", inet_ntoa(ip)); lchan->abis_ip.connect_ip = ntohl(ip.s_addr); } - if (TLVP_PRESENT(tv, RSL_IE_IPAC_REMOTE_PORT)) { port = tlvp_val16_unal(tv, RSL_IE_IPAC_REMOTE_PORT); port = ntohs(port); - DEBUGPC(DRSL, "REMOTE_PORT=%u ", port); lchan->abis_ip.connect_port = port; } - DEBUGPC(DRSL, "\n"); + LOG_LCHAN(lchan, LOGL_DEBUG, "Rx IPACC %s ACK:" + " BTS=%s:%u conn_id=%u rtp_payload2=0x%02x speech_mode=0x%02x", + label, ip_to_a(lchan->abis_ip.bound_ip), lchan->abis_ip.bound_port, + lchan->abis_ip.conn_id, lchan->abis_ip.rtp_payload2, lchan->abis_ip.speech_mode); } -/*! \brief Issue IPA RSL CRCX to configure RTP on BTS side - * \param[in] lchan Logical Channel for which we issue CRCX +/*! Send Issue IPA RSL CRCX to configure the RTP port of the BTS. + * \param[in] lchan Logical Channel for which we issue CRCX */ -int rsl_ipacc_crcx(struct gsm_lchan *lchan) +int rsl_tx_ipacc_crcx(struct gsm_lchan *lchan) { struct msgb *msg = rsl_msgb_alloc(); struct abis_rsl_dchan_hdr *dh; @@ -2428,116 +1808,52 @@ int rsl_ipacc_crcx(struct gsm_lchan *lchan) dh->chan_nr = gsm_lchan2chan_nr(lchan); /* 0x1- == receive-only, 0x-1 == EFR codec */ - lchan->abis_ip.speech_mode = 0x10 | ipa_smod_s_for_lchan(lchan); - lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan); msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode); msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload); - DEBUGP(DRSL, "%s IPAC_BIND speech_mode=0x%02x RTP_PAYLOAD=%d\n", - gsm_lchan_name(lchan), lchan->abis_ip.speech_mode, - lchan->abis_ip.rtp_payload); + LOG_LCHAN(lchan, LOGL_DEBUG, "Sending IPACC CRCX to BTS: speech_mode=0x%02x RTP_PAYLOAD=%d\n", + lchan->abis_ip.speech_mode, lchan->abis_ip.rtp_payload); msg->dst = lchan->ts->trx->rsl_link; return abis_rsl_sendmsg(msg); } -/*! \brief Issue IPA RSL MDCX to configure MGW-side of RTP - * \param[in] lchan Logical Channel for which we issue MDCX - * \param[in] ip Remote (MGW) IP address for RTP - * \param[in] port Remote (MGW) UDP port number for RTP - * \param[in] rtp_payload2 Contents of RTP PAYLOAD 2 IE +/*! Send IPA RSL MDCX to configure the RTP port the BTS sends to (MGW). + * \param[in] lchan Logical Channel for which we issue MDCX + * Remote (MGW) IP address, port and payload types for RTP are determined from lchan->abis_ip. */ -int rsl_ipacc_mdcx(struct gsm_lchan *lchan, uint32_t ip, uint16_t port, - uint8_t rtp_payload2) +int rsl_tx_ipacc_mdcx(struct gsm_lchan *lchan) { struct msgb *msg = rsl_msgb_alloc(); struct abis_rsl_dchan_hdr *dh; uint32_t *att_ip; - struct in_addr ia; dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); init_dchan_hdr(dh, RSL_MT_IPAC_MDCX); dh->c.msg_discr = ABIS_RSL_MDISC_IPACCESS; dh->chan_nr = gsm_lchan2chan_nr(lchan); - /* we need to store these now as MDCX_ACK does not return them :( */ - lchan->abis_ip.rtp_payload2 = rtp_payload2; - lchan->abis_ip.connect_port = port; - lchan->abis_ip.connect_ip = ip; - - /* 0x0- == both directions, 0x-1 == EFR codec */ - lchan->abis_ip.speech_mode = 0x00 | ipa_smod_s_for_lchan(lchan); - lchan->abis_ip.rtp_payload = ipa_rtp_pt_for_lchan(lchan); - - ia.s_addr = htonl(ip); - DEBUGP(DRSL, "%s IPAC_MDCX IP=%s PORT=%d RTP_PAYLOAD=%d RTP_PAYLOAD2=%d " - "CONN_ID=%d speech_mode=0x%02x\n", gsm_lchan_name(lchan), - inet_ntoa(ia), port, lchan->abis_ip.rtp_payload, rtp_payload2, - lchan->abis_ip.conn_id, lchan->abis_ip.speech_mode); - msgb_tv16_put(msg, RSL_IE_IPAC_CONN_ID, lchan->abis_ip.conn_id); msgb_v_put(msg, RSL_IE_IPAC_REMOTE_IP); - att_ip = (uint32_t *) msgb_put(msg, sizeof(ip)); - *att_ip = ia.s_addr; - msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, port); + att_ip = (uint32_t *)msgb_put(msg, sizeof(uint32_t)); + *att_ip = htonl(lchan->abis_ip.connect_ip); + msgb_tv16_put(msg, RSL_IE_IPAC_REMOTE_PORT, lchan->abis_ip.connect_port); msgb_tv_put(msg, RSL_IE_IPAC_SPEECH_MODE, lchan->abis_ip.speech_mode); msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD, lchan->abis_ip.rtp_payload); - if (rtp_payload2) - msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, rtp_payload2); + if (lchan->abis_ip.rtp_payload2) + msgb_tv_put(msg, RSL_IE_IPAC_RTP_PAYLOAD2, lchan->abis_ip.rtp_payload2); msg->dst = lchan->ts->trx->rsl_link; - return abis_rsl_sendmsg(msg); -} - -static bool check_gprs_enabled(struct gsm_bts_trx_ts *ts) -{ - if (ts->trx->bts->gprs.mode == BTS_GPRS_NONE) { - LOGP(DRSL, LOGL_NOTICE, "%s: GPRS mode is 'none': not activating PDCH.\n", - gsm_ts_and_pchan_name(ts)); - return false; - } - return true; -} - -int rsl_ipacc_pdch_activate(struct gsm_bts_trx_ts *ts, int act) -{ - struct msgb *msg = rsl_msgb_alloc(); - struct abis_rsl_dchan_hdr *dh; - uint8_t msg_type; - - if (ts->flags & TS_F_PDCH_PENDING_MASK) { - LOGP(DRSL, LOGL_ERROR, - "%s PDCH %s requested, but a PDCH%s%s is still pending\n", - gsm_ts_name(ts), - act ? "ACT" : "DEACT", - ts->flags & TS_F_PDCH_ACT_PENDING? " ACT" : "", - ts->flags & TS_F_PDCH_DEACT_PENDING? " DEACT" : ""); - return -EINVAL; - } - - if (act){ - if (!check_gprs_enabled(ts)) - return -ENOTSUP; - - msg_type = RSL_MT_IPAC_PDCH_ACT; - ts->flags |= TS_F_PDCH_ACT_PENDING; - } else { - msg_type = RSL_MT_IPAC_PDCH_DEACT; - ts->flags |= TS_F_PDCH_DEACT_PENDING; - } - /* TODO add timeout to cancel PDCH DE/ACT */ - - dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); - init_dchan_hdr(dh, msg_type); - dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN; - dh->chan_nr = gsm_pchan2chan_nr(GSM_PCHAN_TCH_F, ts->nr, 0); - - DEBUGP(DRSL, "%s IPAC PDCH %sACT\n", gsm_ts_name(ts), - act ? "" : "DE"); - - msg->dst = ts->trx->rsl_link; + LOG_LCHAN(lchan, LOGL_DEBUG, "Sending IPACC MDCX to BTS:" + " %s:%u rtp_payload=%u rtp_payload2=%u conn_id=%u speech_mode=0x%02x", + ip_to_a(lchan->abis_ip.connect_ip), + lchan->abis_ip.connect_port, + lchan->abis_ip.rtp_payload, + lchan->abis_ip.rtp_payload2, + lchan->abis_ip.conn_id, + lchan->abis_ip.speech_mode); return abis_rsl_sendmsg(msg); } @@ -2560,9 +1876,9 @@ static int abis_rsl_rx_ipacc_crcx_ack(struct msgb *msg) return -EINVAL; } - ipac_parse_rtp(lchan, &tv); + ipac_parse_rtp(lchan, &tv, "CRCX"); - osmo_signal_dispatch(SS_ABISIP, S_ABISIP_CRCX_ACK, msg->lchan); + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_IPACC_CRCX_ACK, 0); return 0; } @@ -2578,8 +1894,9 @@ static int abis_rsl_rx_ipacc_mdcx_ack(struct msgb *msg) * connected the given logical channel */ rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh)); - ipac_parse_rtp(lchan, &tv); - osmo_signal_dispatch(SS_ABISIP, S_ABISIP_MDCX_ACK, msg->lchan); + ipac_parse_rtp(lchan, &tv, "MDCX"); + + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_IPACC_MDCX_ACK, 0); return 0; } @@ -2590,12 +1907,8 @@ static int abis_rsl_rx_ipacc_dlcx_ind(struct msgb *msg) struct tlv_parsed tv; rsl_tlv_parse(&tv, dh->data, msgb_l2len(msg)-sizeof(*dh)); - - if (TLVP_PRESENT(&tv, RSL_IE_CAUSE)) - print_rsl_cause(LOGL_DEBUG, TLVP_VAL(&tv, RSL_IE_CAUSE), - TLVP_LEN(&tv, RSL_IE_CAUSE)); - - osmo_signal_dispatch(SS_ABISIP, S_ABISIP_DLCX_IND, msg->lchan); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Rx IPACC DLCX IND%s", + rsl_cause_name(&tv)); return 0; } @@ -2604,42 +1917,51 @@ static int abis_rsl_rx_ipacc(struct msgb *msg) { struct e1inp_sign_link *sign_link = msg->dst; struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); - char *ts_name; int rc = 0; msg->lchan = lchan_lookup(sign_link->trx, rllh->chan_nr, "Abis RSL rx IPACC: "); - ts_name = gsm_lchan_name(msg->lchan); + + if (!msg->lchan) { + LOGP(DRSL, LOGL_ERROR, + "Rx RSL IPACC: unable to match RSL message to an lchan: chan_nr=0x%x\n", + rllh->chan_nr); + return -EINVAL; + } + + if (!msg->lchan->fi) { + LOG_LCHAN(msg->lchan, LOGL_ERROR, "Rx RSL IPACC: RSL message for unconfigured lchan"); + return -EINVAL; + } + + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "Rx %s", rsl_or_ipac_msg_name(rllh->c.msg_type)); switch (rllh->c.msg_type) { case RSL_MT_IPAC_CRCX_ACK: - DEBUGP(DRSL, "%s IPAC_CRCX_ACK ", ts_name); rc = abis_rsl_rx_ipacc_crcx_ack(msg); break; case RSL_MT_IPAC_CRCX_NACK: /* somehow the BTS was unable to bind the lchan to its local * port?!? */ - LOGP(DRSL, LOGL_ERROR, "%s IPAC_CRCX_NACK\n", ts_name); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_IPACC_CRCX_NACK, 0); break; case RSL_MT_IPAC_MDCX_ACK: /* the BTS tells us that a connect operation was successful */ - DEBUGP(DRSL, "%s IPAC_MDCX_ACK ", ts_name); rc = abis_rsl_rx_ipacc_mdcx_ack(msg); break; case RSL_MT_IPAC_MDCX_NACK: /* somehow the BTS was unable to connect the lchan to a remote * port */ - LOGP(DRSL, LOGL_ERROR, "%s IPAC_MDCX_NACK\n", ts_name); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_IPACC_MDCX_NACK, 0); break; case RSL_MT_IPAC_DLCX_IND: - DEBUGP(DRSL, "%s IPAC_DLCX_IND ", ts_name); rc = abis_rsl_rx_ipacc_dlcx_ind(msg); break; default: - LOGP(DRSL, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n", - rllh->c.msg_type); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Unknown ip.access msg_type 0x%02x\n", + rllh->c.msg_type); rate_ctr_inc(&sign_link->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_UNKNOWN]); break; } @@ -2647,207 +1969,84 @@ static int abis_rsl_rx_ipacc(struct msgb *msg) return rc; } -int dyn_ts_switchover_start(struct gsm_bts_trx_ts *ts, - enum gsm_phys_chan_config to_pchan) +/*! Tx simplified channel (de-)activation message for non-standard Osmocom dyn TS PDCH type. */ +static int send_osmocom_style_pdch_chan_act(struct gsm_bts_trx_ts *ts, bool activate) { - int ss; - int rc = -EIO; + struct msgb *msg; + struct abis_rsl_dchan_hdr *dh; - OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH); + msg = rsl_msgb_alloc(); + dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); + init_dchan_hdr(dh, activate ? RSL_MT_CHAN_ACTIV : RSL_MT_RF_CHAN_REL); - if (ts->dyn.pchan_is != ts->dyn.pchan_want) { - LOGP(DRSL, LOGL_ERROR, - "%s: Attempt to switch dynamic channel to %s," - " but is already in switchover.\n", - gsm_ts_and_pchan_name(ts), - gsm_pchan_name(to_pchan)); - return ts->dyn.pchan_want == to_pchan? 0 : -EAGAIN; - } - - if (ts->dyn.pchan_is == to_pchan) { - LOGP(DRSL, LOGL_INFO, - "%s %s Already is in %s mode, cannot switchover.\n", - gsm_ts_name(ts), gsm_pchan_name(ts->pchan), - gsm_pchan_name(to_pchan)); - return -EINVAL; - } + dh->chan_nr = RSL_CHAN_OSMO_PDCH | (ts->nr & ~RSL_CHAN_NR_MASK); - /* Paranoia: let's make sure all is indeed released. */ - for (ss = 0; ss < ts_subslots(ts); ss++) { - struct gsm_lchan *lc = &ts->lchan[ss]; - if (lc->state != LCHAN_S_NONE) { - LOGP(DRSL, LOGL_ERROR, - "%s Attempt to switch dynamic channel to %s," - " but is not fully released.\n", - gsm_ts_and_pchan_name(ts), - gsm_pchan_name(to_pchan)); - return -EAGAIN; + if (activate) { + msgb_tv_put(msg, RSL_IE_ACT_TYPE, RSL_ACT_OSMO_PDCH); + + if (ts->trx->bts->type == GSM_BTS_TYPE_RBS2000 + && ts->trx->bts->rbs2000.use_superchannel) { + const uint8_t eric_pgsl_tmr[] = { 30, 1 }; + msgb_tv_fixed_put(msg, RSL_IE_ERIC_PGSL_TIMERS, + sizeof(eric_pgsl_tmr), eric_pgsl_tmr); } } - if (to_pchan == GSM_PCHAN_PDCH && !check_gprs_enabled(ts)) - return -ENOTSUP; - - DEBUGP(DRSL, "%s starting switchover to %s\n", - gsm_ts_and_pchan_name(ts), gsm_pchan_name(to_pchan)); + msg->dst = ts->trx->rsl_link; + return abis_rsl_sendmsg(msg); +} - /* Record that we're busy switching. */ - ts->dyn.pchan_want = to_pchan; +/*! Tx simplified channel (de-)activation message for non-standard ip.access dyn TS PDCH type. */ +static int send_ipacc_style_pdch_act(struct gsm_bts_trx_ts *ts, bool activate) +{ + struct msgb *msg = rsl_msgb_alloc(); + struct abis_rsl_dchan_hdr *dh; - /* - * To switch from PDCH, we need to initiate the release from the BSC - * side. dyn_ts_switchover_continue() will be called from - * rsl_rx_rf_chan_rel_ack(). PDCH is always on lchan[0]. - */ - if (ts->dyn.pchan_is == GSM_PCHAN_PDCH) { - rsl_lchan_set_state(ts->lchan, LCHAN_S_REL_REQ); - rc = rsl_rf_chan_release(ts->lchan, 0, SACCH_NONE); - if (rc) { - LOGP(DRSL, LOGL_ERROR, - "%s RSL RF Chan Release failed\n", - gsm_ts_and_pchan_name(ts)); - return dyn_ts_switchover_failed(ts, rc); - } - return 0; - } + dh = (struct abis_rsl_dchan_hdr *) msgb_put(msg, sizeof(*dh)); + init_dchan_hdr(dh, activate ? RSL_MT_IPAC_PDCH_ACT : RSL_MT_IPAC_PDCH_DEACT); + dh->c.msg_discr = ABIS_RSL_MDISC_DED_CHAN; + dh->chan_nr = gsm_pchan2chan_nr(GSM_PCHAN_TCH_F, ts->nr, 0); - /* - * To switch from TCH/F and TCH/H pchans, this has been called from - * rsl_rx_rf_chan_rel_ack(), i.e. release is complete. Go ahead and - * activate as new type. This will always be PDCH. - */ - return dyn_ts_switchover_continue(ts); + msg->dst = ts->trx->rsl_link; + return abis_rsl_sendmsg(msg); } -static int dyn_ts_switchover_continue(struct gsm_bts_trx_ts *ts) +int rsl_tx_dyn_ts_pdch_act_deact(struct gsm_bts_trx_ts *ts, bool activate) { int rc; - uint8_t act_type; - uint8_t ho_ref; - int ss; - struct gsm_lchan *lchan; - - OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH); - DEBUGP(DRSL, "%s switchover: release complete," - " activating new pchan type\n", - gsm_ts_and_pchan_name(ts)); + const char *what; + const char *act; - if (ts->dyn.pchan_is == ts->dyn.pchan_want) { - LOGP(DRSL, LOGL_ERROR, - "%s Requested to switchover dynamic channel to the" - " same type it is already in.\n", - gsm_ts_and_pchan_name(ts)); - return 0; - } + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + what = "Osmocom dyn TS"; + act = activate? "PDCH Chan Activ" : "PDCH Chan RF Release"; - for (ss = 0; ss < ts_subslots(ts); ss++) { - lchan = &ts->lchan[ss]; - if (lchan->rqd_ref) { - LOGP(DRSL, LOGL_ERROR, - "%s During dyn TS switchover, expecting no" - " Request Reference to be pending. Discarding!\n", - gsm_lchan_name(lchan)); - talloc_free(lchan->rqd_ref); - lchan->rqd_ref = NULL; - } - } + rc = send_osmocom_style_pdch_chan_act(ts, activate); + break; - /* - * When switching pchan modes, all lchans are unused. So always - * activate whatever wants to be activated on the first lchan. (We - * wouldn't remember to use lchan[1] across e.g. a PDCH deact anyway) - */ - lchan = ts->lchan; + case GSM_PCHAN_TCH_F_PDCH: + what = "ip.access dyn TS"; + act = activate? "PDCH ACT" : "PDCH DEACT"; - /* - * For TCH/x, the lchan->type has been set in lchan_alloc(), but it may - * have been lost during channel release due to dynamic switchover. - * - * For PDCH, the lchan->type will actually remain NONE. - * TODO: set GSM_LCHAN_PDTCH? - */ - switch (ts->dyn.pchan_want) { - case GSM_PCHAN_TCH_F: - lchan->type = GSM_LCHAN_TCH_F; - break; - case GSM_PCHAN_TCH_H: - lchan->type = GSM_LCHAN_TCH_H; - break; - case GSM_PCHAN_PDCH: - lchan->type = GSM_LCHAN_NONE; + rc = send_ipacc_style_pdch_act(ts, activate); break; + default: - LOGP(DRSL, LOGL_ERROR, - "%s Invalid target pchan for dynamic TS\n", - gsm_ts_and_pchan_name(ts)); - } - - act_type = (ts->dyn.pchan_want == GSM_PCHAN_PDCH) - ? RSL_ACT_OSMO_PDCH - : lchan->dyn.act_type; - ho_ref = (ts->dyn.pchan_want == GSM_PCHAN_PDCH) - ? 0 - : lchan->dyn.ho_ref; - - /* Fetch the rqd_ref back from before switchover started. */ - lchan->rqd_ref = lchan->dyn.rqd_ref; - lchan->rqd_ta = lchan->dyn.rqd_ta; - lchan->dyn.rqd_ref = NULL; - lchan->dyn.rqd_ta = 0; - - /* During switchover, we have received a release ack, which means that - * the act_timer has been stopped. Start the timer again so we mark - * this channel broken if the activation ack comes too late. */ - osmo_timer_setup(&lchan->act_timer, lchan_act_tmr_cb, lchan); - osmo_timer_schedule(&lchan->act_timer, 4, 0); - - rc = rsl_chan_activate_lchan(lchan, act_type, ho_ref); - if (rc) { - LOGP(DRSL, LOGL_ERROR, - "%s RSL Chan Activate failed\n", - gsm_ts_and_pchan_name(ts)); - return dyn_ts_switchover_failed(ts, rc); + what = "static timeslot"; + act = activate? "dynamic PDCH activation" : "dynamic PDCH deactivation"; + rc = -EINVAL; + break; } - return 0; -} -static int dyn_ts_switchover_failed(struct gsm_bts_trx_ts *ts, int rc) -{ - ts->dyn.pchan_want = ts->dyn.pchan_is; - LOGP(DRSL, LOGL_ERROR, "%s Error %d during dynamic channel switchover." - " Going back to previous pchan.\n", gsm_ts_and_pchan_name(ts), - rc); + if (rc) + LOG_TS(ts, LOGL_ERROR, "Tx FAILED: %s: %s: %d (%s)", + what, act, rc, strerror(-rc)); + else + LOG_TS(ts, LOGL_DEBUG, "Tx: %s: %s", what, act); return rc; } -static void dyn_ts_switchover_complete(struct gsm_lchan *lchan) -{ - enum gsm_phys_chan_config pchan_act; - enum gsm_phys_chan_config pchan_was; - struct gsm_bts_trx_ts *ts = lchan->ts; - - OSMO_ASSERT(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH); - - pchan_act = pchan_for_lchant(lchan->type); - /* - * Paranoia: do the types match? - * In case of errors: we've received an act ack already, so what to do - * about it? Logging the error should suffice for now. - */ - if (pchan_act != ts->dyn.pchan_want) - LOGP(DRSL, LOGL_ERROR, - "%s Requested transition does not match lchan type %s\n", - gsm_ts_and_pchan_name(ts), - gsm_lchant_name(lchan->type)); - - pchan_was = ts->dyn.pchan_is; - ts->dyn.pchan_is = ts->dyn.pchan_want = pchan_act; - - if (pchan_was != ts->dyn.pchan_is) - LOGP(DRSL, LOGL_INFO, "%s switchover from %s complete.\n", - gsm_ts_and_pchan_name(ts), gsm_pchan_name(pchan_was)); -} - /* Entry-point where L2 RSL from BTS enters */ int abis_rsl_rcvmsg(struct msgb *msg) { @@ -2970,71 +2169,6 @@ int rsl_bs_power_control(struct gsm_bts_trx *trx, uint8_t channel, uint8_t reduc return abis_rsl_sendmsg(msg); } -/** - * Release all allocated SAPIs starting from @param start and - * release them with the given release mode. Once the release - * confirmation arrives it will be attempted to release the - * the RF channel. - */ -int rsl_release_sapis_from(struct gsm_lchan *lchan, int start, - enum rsl_rel_mode release_mode) -{ - int no_sapi = 1; - int sapi; - - for (sapi = start; sapi < ARRAY_SIZE(lchan->sapis); ++sapi) { - uint8_t link_id; - if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) - continue; - - link_id = sapi; - if (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H) - link_id |= 0x40; - rsl_release_request(lchan, link_id, release_mode); - no_sapi = 0; - } - - return no_sapi; -} - -int rsl_start_t3109(struct gsm_lchan *lchan) -{ - struct gsm_bts *bts = lchan->ts->trx->bts; - - osmo_timer_setup(&lchan->T3109, t3109_expired, lchan); - osmo_timer_schedule(&lchan->T3109, T_def_get(bts->network->T_defs, 3109, T_S, -1), 0); - return 0; -} - -/** - * \brief directly RF Channel Release the lchan - * - * When no SAPI was allocated, directly release the logical channel. This - * should only be called from chan_alloc.c on channel release handling. In - * case no SAPI was established the RF Channel can be directly released, - */ -int rsl_direct_rf_release(struct gsm_lchan *lchan) -{ - int i; - for (i = 0; i < ARRAY_SIZE(lchan->sapis); ++i) { - if (lchan->sapis[i] != LCHAN_SAPI_UNUSED) { - LOGP(DRSL, LOGL_ERROR, "%s SAPI(%d) still allocated.\n", - gsm_lchan_name(lchan), i); - return -1; - } - } - - /* Now release it */ - return rsl_rf_chan_release(lchan, 0, SACCH_NONE); -} - -/* Initial timeslot actions when a timeslot first comes into operation. */ -bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts) -{ - dyn_ts_init(ts); - return true; -} - static void handle_classmark_chg(struct gsm_subscriber_connection *conn, struct msgb *msg) { @@ -3043,175 +2177,35 @@ static void handle_classmark_chg(struct gsm_subscriber_connection *conn, uint8_t cm2_len, cm3_len = 0; uint8_t *cm2, *cm3 = NULL; - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "CLASSMARK CHANGE "); /* classmark 2 */ cm2_len = gh->data[0]; cm2 = &gh->data[1]; - DEBUGPC(DRR, "CM2(len=%u) ", cm2_len); + + if (cm2_len > 3) { + LOG_LCHAN(msg->lchan, LOGL_ERROR, "CLASSMARK CHANGE: CM2 too long: %u", cm2_len); + return; + } if (payload_len > cm2_len + 1) { /* we must have a classmark3 */ if (gh->data[cm2_len+1] != 0x20) { - DEBUGPC(DRR, "ERR CM3 TAG\n"); - return; - } - if (cm2_len > 3) { - DEBUGPC(DRR, "CM2 too long!\n"); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "CLASSMARK CHANGE: invalid CM3 TAG\n"); return; } cm3_len = gh->data[cm2_len+2]; cm3 = &gh->data[cm2_len+3]; if (cm3_len > 14) { - DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "CLASSMARK CHANGE: CM3 too long: %u", + cm3_len); return; } - DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len); } - bsc_cm_update(conn, cm2, cm2_len, cm3, cm3_len); -} - -/* Chapter 9.1.16 Handover complete */ -static void handle_rr_ho_compl(struct msgb *msg) -{ - struct lchan_signal_data sig; - struct gsm48_hdr *gh = msgb_l3(msg); - - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, - "HANDOVER COMPLETE cause = %s\n", rr_cause_name(gh->data[0])); - - sig.lchan = msg->lchan; - sig.mr = NULL; - osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig); - /* FIXME: release old channel */ - - /* send pending messages, if any */ - gscon_dtap_cache_flush(msg->lchan->conn, 1); -} - -/* Chapter 9.1.17 Handover Failure */ -static void handle_rr_ho_fail(struct msgb *msg) -{ - struct lchan_signal_data sig; - struct gsm48_hdr *gh = msgb_l3(msg); - - /* Log on both RR and HO categories: it is an RR message, but is still quite important when - * filtering on HO. */ - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, - "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0])); - LOGPLCHAN(msg->lchan, DHO, LOGL_DEBUG, - "HANDOVER FAILED cause = %s\n", rr_cause_name(gh->data[0])); - sig.lchan = msg->lchan; - sig.mr = NULL; - osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig); - /* FIXME: release allocated new channel */ - - /* send pending messages, if any */ - gscon_dtap_cache_flush(msg->lchan->conn, 1); -} - -static void handle_ass_compl(struct gsm_subscriber_connection *conn, - struct msgb *msg) -{ - struct gsm48_hdr *gh = msgb_l3(msg); - enum gsm48_rr_cause cause; - - /* Expecting gsm48_hdr + cause value */ - if (msgb_l3len(msg) != sizeof(*gh) + 1) { - LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR, - "RR Assignment Complete: length invalid: %u, expected %zu\n", - msgb_l3len(msg), sizeof(*gh) + 1); - return; - } - - cause = gh->data[0]; - - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT COMPLETE cause = %s\n", - rr_cause_name(cause)); - - if (conn->ho) { - struct lchan_signal_data sig = { - .lchan = msg->lchan, - }; - osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_COMPL, &sig); - /* FIXME: release old channel */ - - /* send pending messages, if any */ - gscon_dtap_cache_flush(conn, 1); - - return; - } - - if (conn->secondary_lchan != msg->lchan) { - LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR, - "RR Assignment Complete does not match conn's secondary lchan.\n"); - return; - } - - lchan_release(conn->lchan, 0, RSL_REL_LOCAL_END); - conn->lchan = conn->secondary_lchan; - conn->secondary_lchan = NULL; - - /* send pending messages, if any */ - gscon_dtap_cache_flush(conn, 1); - - if (is_ipaccess_bts(conn_get_bts(conn)) && conn->lchan->tch_mode != GSM48_CMODE_SIGN) - rsl_ipacc_crcx(conn->lchan); - - bsc_assign_compl(conn, cause); -} - -static void handle_ass_fail(struct gsm_subscriber_connection *conn, - struct msgb *msg) -{ - uint8_t *rr_failure; - struct gsm48_hdr *gh; - - if (conn->ho) { - struct lchan_signal_data sig; - struct gsm48_hdr *gh = msgb_l3(msg); - - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, "ASSIGNMENT FAILED cause = %s\n", - rr_cause_name(gh->data[0])); - - sig.lchan = msg->lchan; - sig.mr = NULL; - osmo_signal_dispatch(SS_LCHAN, S_LCHAN_ASSIGNMENT_FAIL, &sig); - /* FIXME: release allocated new channel */ - - /* send pending messages, if any */ - gscon_dtap_cache_flush(conn, 1); - - return; - } - - if (conn->lchan != msg->lchan) { - LOGPLCHAN(msg->lchan, DMSC, LOGL_ERROR, - "Assignment failure should occur on primary lchan.\n"); - return; - } - - /* stop the timer and release it */ - if (conn->secondary_lchan) { - lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END); - conn->secondary_lchan = NULL; - } - - /* send pending messages, if any */ - gscon_dtap_cache_flush(conn, 1); - - gh = msgb_l3(msg); - if (msgb_l3len(msg) - sizeof(*gh) != 1) { - LOGPLCHAN(conn->lchan, DMSC, LOGL_ERROR, "assignment failure unhandled: %zu\n", - msgb_l3len(msg) - sizeof(*gh)); - rr_failure = NULL; - } else { - rr_failure = &gh->data[0]; - } - - bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE, rr_failure); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "CLASSMARK CHANGE CM2(len=%u) CM3(len=%u)", + cm2_len, cm3_len); + bsc_cm_update(conn, cm2, cm2_len, cm3, cm3_len); } static void dispatch_dtap(struct gsm_subscriber_connection *conn, @@ -3223,8 +2217,8 @@ static void dispatch_dtap(struct gsm_subscriber_connection *conn, int rc; if (msgb_l3len(msg) < sizeof(*gh)) { - LOGP(DMSC, LOGL_ERROR, "(%s) Message too short for a GSM48 header.\n", - bsc_subscr_name(conn->bsub)); + LOG_LCHAN(msg->lchan, LOGL_ERROR, + "Message too short for a GSM48 header (%u)", msgb_l3len(msg)); return; } @@ -3240,45 +2234,63 @@ static void dispatch_dtap(struct gsm_subscriber_connection *conn, * will call api->compl_l3() for it */ switch (pdisc) { case GSM48_PDISC_RR: + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "Rx %s", gsm48_rr_msg_name(msg_type)); switch (msg_type) { case GSM48_MT_RR_GPRS_SUSP_REQ: - LOGPLCHAN(msg->lchan, DRR, LOGL_DEBUG, - "%s\n", gsm48_rr_msg_name(GSM48_MT_RR_GPRS_SUSP_REQ)); + /* do something? */ break; case GSM48_MT_RR_STATUS: - LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE, - "%s (cause: %s)\n", gsm48_rr_msg_name(GSM48_MT_RR_STATUS), - rr_cause_name(gh->data[0])); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "RR Status: %s", rr_cause_name(gh->data[0])); + /* do something? */ break; case GSM48_MT_RR_MEAS_REP: /* This shouldn't actually end up here, as RSL treats * L3 Info of 08.58 MEASUREMENT REPORT different by calling * directly into gsm48_parse_meas_rep */ - LOGPLCHAN(msg->lchan, DMEAS, LOGL_ERROR, - "DIRECT GSM48 MEASUREMENT REPORT ?!?\n"); + LOG_LCHAN(msg->lchan, LOGL_ERROR, "DIRECT GSM48 MEASUREMENT REPORT ?!?\n"); gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT); break; case GSM48_MT_RR_HANDO_COMPL: - handle_rr_ho_compl(msg); + /* Chapter 9.1.16 Handover complete */ + if (!conn->ho.fi) + LOG_LCHAN(msg->lchan, LOGL_ERROR, + "Rx RR Handover Complete, but no handover is ongoing\n"); + else + osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_RR_HO_COMPLETE, msg); break; case GSM48_MT_RR_HANDO_FAIL: - handle_rr_ho_fail(msg); + /* Chapter 9.1.17 Handover Failure */ + if (!conn->ho.fi) + LOG_LCHAN(msg->lchan, LOGL_ERROR, + "Rx RR Handover Fail, but no handover is ongoing\n"); + else + osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_RR_HO_FAIL, msg); break; case GSM48_MT_RR_CIPH_M_COMPL: bsc_cipher_mode_compl(conn, msg, conn->lchan->encr.alg_id); break; case GSM48_MT_RR_ASS_COMPL: - handle_ass_compl(conn, msg); + if (conn->assignment.fi) + osmo_fsm_inst_dispatch(conn->assignment.fi, + ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE, msg); + else + LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR, + "Rx RR Assignment Complete, but no assignment is ongoing\n"); break; case GSM48_MT_RR_ASS_FAIL: - handle_ass_fail(conn, msg); + if (conn->assignment.fi) + osmo_fsm_inst_dispatch(conn->assignment.fi, + ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL, msg); + else + LOGPLCHAN(msg->lchan, DRR, LOGL_ERROR, + "Rx RR Assignment Failure, but no assignment is ongoing\n"); break; case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: rc = gsm48_rx_rr_modif_ack(msg); if (rc < 0) - bsc_assign_fail(conn, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_CHAN_MODE_MODIF_ERROR, &rc); else - bsc_assign_compl(conn, 0); + osmo_fsm_inst_dispatch(msg->lchan->fi, LCHAN_EV_CHAN_MODE_MODIF_ACK, msg); break; case GSM48_MT_RR_CLSM_CHG: handle_classmark_chg(conn, msg); @@ -3290,8 +2302,8 @@ static void dispatch_dtap(struct gsm_subscriber_connection *conn, break; default: /* Drop unknown RR message */ - LOGPLCHAN(msg->lchan, DRR, LOGL_NOTICE, - "Dropping %s 04.08 RR message\n", gsm48_rr_msg_name(msg_type)); + LOG_LCHAN(msg->lchan, LOGL_NOTICE, "Unknown RR message: %s", + gsm48_rr_msg_name(msg_type)); gsm48_tx_rr_status(conn, GSM48_RR_CAUSE_MSG_TYPE_N); break; } @@ -3305,36 +2317,30 @@ static void dispatch_dtap(struct gsm_subscriber_connection *conn, /*! \brief RSL has received a DATA INDICATION with L3 from MS */ int gsm0408_rcvmsg(struct msgb *msg, uint8_t link_id) { - int rc; struct gsm_lchan *lchan; lchan = msg->lchan; - if (lchan->state != LCHAN_S_ACTIVE) { - LOGPLCHAN(msg->lchan, DRSL, LOGL_INFO, "Got data in non active state, discarding.\n"); + if (!lchan_may_receive_data(lchan)) { + LOG_LCHAN(msg->lchan, LOGL_INFO, "Got data in non active state, discarding.\n"); return -1; } - if (lchan->conn) { /* if we already have a connection, forward via DTAP to * MSC */ dispatch_dtap(lchan->conn, link_id, msg); } else { /* allocate a new connection */ - rc = BSC_API_CONN_POL_REJECT; lchan->conn = bsc_subscr_con_allocate(msg->lchan->ts->trx->bts->network); if (!lchan->conn) { - lchan_release(lchan, 1, RSL_REL_NORMAL); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); return -1; } lchan->conn->lchan = lchan; /* fwd via bsc_api to send COMPLETE L3 INFO to MSC */ - rc = bsc_compl_l3(lchan->conn, msg, 0); - - if (rc != BSC_API_CONN_POL_ACCEPT) { - //osmo_fsm_inst_dispatch(lchan->conn->fi, FIXME, NULL); - } + bsc_compl_l3(lchan->conn, msg, 0); + /* conn shall release lchan on teardown, also if this Layer 3 Complete is rejected. */ } return 0; @@ -3391,11 +2397,14 @@ int gsm48_rx_rr_modif_ack(struct msgb *msg) struct gsm48_chan_mode_modify *mod = (struct gsm48_chan_mode_modify *) gh->data; - DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n"); + LOG_LCHAN(msg->lchan, LOGL_DEBUG, "CHANNEL MODE MODIFY ACK for %s", + gsm48_chan_mode_name(mod->mode)); if (mod->mode != msg->lchan->tch_mode) { - LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n", - msg->lchan->tch_mode, mod->mode); + LOG_LCHAN(msg->lchan, LOGL_ERROR, + "CHANNEL MODE MODIFY ACK has wrong mode: Wanted: %s Got: %s\n", + gsm48_chan_mode_name(msg->lchan->tch_mode), + gsm48_chan_mode_name(mod->mode)); return -1; } @@ -3424,11 +2433,11 @@ int gsm48_rx_rr_modif_ack(struct msgb *msg) /* FIXME: we not only need to do this after mode modify, but * also after channel activation */ if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN) - rsl_ipacc_crcx(msg->lchan); + rsl_tx_ipacc_crcx(msg->lchan); return rc; } -static int gsm48_sendmsg(struct msgb *msg) +int gsm48_sendmsg(struct msgb *msg) { if (msg->lchan) msg->dst = msg->lchan->ts->trx->rsl_link; @@ -3494,15 +2503,13 @@ static void gsm48_cell_desc(struct gsm48_cell_desc *cd, #define GSM48_HOCMD_CCHDESC_LEN 16 /* Chapter 9.1.15: Handover Command */ -int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan, - uint8_t power_command, uint8_t ho_ref) +struct msgb *gsm48_make_ho_cmd(struct gsm_lchan *new_lchan, uint8_t power_command, uint8_t ho_ref) { struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 HO CMD"); struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); struct gsm48_ho_cmd *ho = (struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho)); - msg->lchan = old_lchan; gh->proto_discr = GSM48_PDISC_RR; gh->msg_type = GSM48_MT_RR_HANDO_CMD; @@ -3537,6 +2544,16 @@ int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan, msgb_tlv_put(msg, GSM48_IE_MUL_RATE_CFG, new_lchan->mr_ms_lv[0], new_lchan->mr_ms_lv + 1); + return msg; +} + +int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan, + uint8_t power_command, uint8_t ho_ref) +{ + struct msgb *msg = gsm48_make_ho_cmd(new_lchan, power_command, ho_ref); + if (!msg) + return -EINVAL; + msg->lchan = old_lchan; return gsm48_sendmsg(msg); } diff --git a/src/osmo-bsc/assignment_fsm.c b/src/osmo-bsc/assignment_fsm.c new file mode 100644 index 000000000..c12f27b5a --- /dev/null +++ b/src/osmo-bsc/assignment_fsm.c @@ -0,0 +1,638 @@ +/* osmo-bsc BSSMAP Assignment procedure implementation. + * + * (C) 2018 by sysmocom - s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 . + * + */ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static struct osmo_fsm assignment_fsm; + +/* From local var fi->priv, define local var conn. */ +#define GET_CONN() \ + struct gsm_subscriber_connection *conn = fi->priv; \ + OSMO_ASSERT((fi)->fsm == &assignment_fsm && (fi)->priv) + +struct state_timeout assignment_fsm_timeouts[32] = { + [ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE] = { .T=10 }, + [ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE] = { .keep_timer=true }, + [ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED] = { .keep_timer=true }, + [ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { .T=23042 }, +}; + +/* Transition to a state, using the T timer defined in assignment_fsm_timeouts. + * The actual timeout value is in turn obtained from network->T_defs. + * Assumes local variable fi exists. */ +#define assignment_fsm_state_chg(state) \ + fsm_inst_state_chg_T(fi, state, \ + assignment_fsm_timeouts, \ + ((struct gsm_subscriber_connection*)(fi->priv))->network->T_defs, \ + 5) + +/* Log failure and transition to ASSIGNMENT_ST_FAILURE, which triggers the appropriate actions. */ +#define assignment_fail(cause, fmt, args...) do { \ + struct gsm_subscriber_connection *_conn = fi->priv; \ + _conn->assignment.failure_cause = cause; \ + LOG_ASSIGNMENT(_conn, LOGL_ERROR, "Assignment failed in state %s, cause %s: " fmt, \ + osmo_fsm_inst_state_name(fi), gsm0808_cause_name(cause), ## args); \ + assignment_count_result(BSC_CTR_ASSIGNMENT_ERROR); \ + on_assignment_failure(_conn); \ + } while(0) + +/* Assume presence of local var 'conn' as struct gsm_subscriber_connection */ +#define assignment_count(counter) do { \ + LOG_ASSIGNMENT(conn, LOGL_DEBUG, "incrementing rate counter: %s %s", \ + bsc_ctr_description[counter].name, \ + bsc_ctr_description[counter].description); \ + rate_ctr_inc(&conn->network->bsc_ctrs->ctr[counter]); \ + } while(0) + +#define assignment_count_result(counter) do { \ + if (!conn->assignment.result_rate_ctr_done) { \ + assignment_count(counter); \ + conn->assignment.result_rate_ctr_done = true; \ + } else \ + LOG_ASSIGNMENT(conn, LOGL_DEBUG, \ + "result rate counter already recorded, NOT counting as: %s %s", \ + bsc_ctr_description[counter].name, \ + bsc_ctr_description[counter].description); \ + } while(0) + +void assignment_reset(struct gsm_subscriber_connection *conn) +{ + if (conn->assignment.new_lchan) { + struct gsm_lchan *lchan = conn->assignment.new_lchan; + conn->assignment.new_lchan = NULL; + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + } + + if (conn->assignment.created_ci_for_msc) { + gscon_forget_mgw_endpoint_ci(conn, conn->assignment.created_ci_for_msc); + /* If this is the last endpoint released, the mgw_endpoint_fsm will terminate and tell + * the gscon about it. */ + mgw_endpoint_ci_dlcx(conn->assignment.created_ci_for_msc); + } + + conn->assignment = (struct assignment_fsm_data){ + .fi = conn->assignment.fi, /* The FSM shall clear itself when it's done. */ + }; +} + +static void on_assignment_failure(struct gsm_subscriber_connection *conn) +{ + struct msgb *resp = gsm0808_create_assignment_failure(conn->assignment.failure_cause, NULL); + + if (!resp) + LOG_ASSIGNMENT(conn, LOGL_ERROR, "Unable to compose BSSMAP Assignment Failure message"); + else + gscon_sigtran_send(conn, resp); + + /* If assignment failed as early as in assignment_fsm_start(), there may not be an fi yet. */ + if (conn->assignment.fi) { + LOG_ASSIGNMENT(conn, LOGL_ERROR, "Assignment failed"); + osmo_fsm_inst_term(conn->assignment.fi, OSMO_FSM_TERM_ERROR, 0); + } +} + +static void send_assignment_complete(struct gsm_subscriber_connection *conn, struct gsm_lchan *new_lchan) +{ + int rc; + struct gsm0808_speech_codec sc; + struct gsm0808_speech_codec *sc_ptr = NULL; + struct sockaddr_storage addr_local; + struct sockaddr_storage *addr_local_p = NULL; + int perm_spch = 0; + uint8_t chosen_channel; + struct msgb *resp; + struct gsm_lchan *lchan = new_lchan; + struct osmo_fsm_inst *fi = conn->fi; + + chosen_channel = gsm0808_chosen_channel(lchan->type, lchan->tch_mode); + if (!chosen_channel) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to compose Chosen Channel for mode=%s type=%s", + get_value_string(gsm48_chan_mode_names, lchan->tch_mode), + gsm_lchant_name(lchan->type)); + return; + } + + /* Generate voice related fields */ + if (conn->assignment.requires_voice_stream) { + perm_spch = gsm0808_permitted_speech(lchan->type, lchan->tch_mode); + + switch (conn->sccp.msc->a.asp_proto) { + case OSMO_SS7_ASP_PROT_IPA: + /* don't add any AoIP specific fields. CIC allocated by MSC */ + break; + default: + if (!mgwep_ci_get_crcx_info_to_sockaddr(conn->user_plane.mgw_endpoint_ci_msc, + &addr_local)) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to compose RTP address of MGW -> MSC"); + return; + } + addr_local_p = &addr_local; + + /* Extrapolate speech codec from speech mode */ + gsm0808_speech_codec_from_chan_type(&sc, perm_spch); + sc_ptr = ≻ + break; + } + /* FIXME: AMR codec configuration must be derived from lchan1! */ + } + + gsm0808_speech_codec_from_chan_type(&sc, perm_spch); + + resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause, + chosen_channel, + lchan->encr.alg_id, perm_spch, + addr_local_p, sc_ptr, NULL); + + if (!resp) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to compose Assignment Complete message"); + return; + } + + /* Add LCLS BSS-Status IE in case there is any LCLS status for this connection */ + bssmap_add_lcls_status_if_needed(conn, resp); + + rc = gscon_sigtran_send(conn, resp); + if (rc) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable send Assignment Complete message: rc=%d %s", + rc, strerror(-rc)); + return; + } +} + +static void assignment_success(struct gsm_subscriber_connection *conn) +{ + /* apply LCLS configuration (if any) */ + lcls_apply_config(conn); + + send_assignment_complete(conn, conn->assignment.new_lchan); + /* If something went wrong during send_assignment_complete(), the fi will be gone from + * error handling in there. Almost a success, but then again the whole thing failed. */ + if (!conn->assignment.fi) + return; + + /* Take on the new lchan */ + gscon_change_primary_lchan(conn, conn->assignment.new_lchan); + conn->assignment.new_lchan = NULL; + + /* Rembered this only for error handling: should assignment fail, assignment_reset() will release + * the MGW endpoint right away. If successful, the conn continues to use the endpoint. */ + conn->assignment.created_ci_for_msc = NULL; + + /* New RTP information is now accepted */ + osmo_strlcpy(conn->user_plane.msc_assigned_rtp_addr, conn->assignment.req.msc_rtp_addr, + sizeof(conn->user_plane.msc_assigned_rtp_addr)); + conn->user_plane.msc_assigned_rtp_port = conn->assignment.req.msc_rtp_port; + + LOG_ASSIGNMENT(conn, LOGL_DEBUG, "Assignment successful"); + osmo_fsm_inst_term(conn->assignment.fi, OSMO_FSM_TERM_REGULAR, 0); + + assignment_count_result(BSC_CTR_ASSIGNMENT_COMPLETED); +} + +static void assignment_fsm_update_id(struct gsm_subscriber_connection *conn) +{ + struct gsm_lchan *new_lchan = conn->assignment.new_lchan; + if (!new_lchan) { + osmo_fsm_inst_update_id(conn->assignment.fi, conn->fi->id); + return; + } + + osmo_fsm_inst_update_id_f(conn->assignment.fi, "%s_%u-%u-%u-%s%s%s-%u", + conn->fi->id, + new_lchan->ts->trx->bts->nr, new_lchan->ts->trx->nr, new_lchan->ts->nr, + gsm_pchan_id(new_lchan->ts->pchan_on_init), + (new_lchan->ts->pchan_on_init == new_lchan->ts->pchan_is)? "" : "as", + (new_lchan->ts->pchan_on_init == new_lchan->ts->pchan_is)? "" + : gsm_pchan_id(new_lchan->ts->pchan_is), + new_lchan->nr); +} + +static bool lchan_type_compat_with_mode(enum gsm_chan_t type, + enum gsm48_chan_mode chan_mode, int full_rate) +{ + switch (chan_mode) { + case GSM48_CMODE_SIGN: + switch (type) { + case GSM_LCHAN_TCH_F: + case GSM_LCHAN_TCH_H: + case GSM_LCHAN_SDCCH: + return true; + default: + return false; + } + + case GSM48_CMODE_SPEECH_V1: + case GSM48_CMODE_SPEECH_AMR: + case GSM48_CMODE_DATA_3k6: + case GSM48_CMODE_DATA_6k0: + /* these services can all run on TCH/H, but we may have + * an explicit override by the 'full_rate' argument */ + switch (type) { + case GSM_LCHAN_TCH_F: + return full_rate; + case GSM_LCHAN_TCH_H: + return !full_rate; + default: + return false; + } + + case GSM48_CMODE_DATA_12k0: + case GSM48_CMODE_DATA_14k5: + case GSM48_CMODE_SPEECH_EFR: + /* these services all explicitly require a TCH/F */ + return type == GSM_LCHAN_TCH_F; + + default: + return false; + } +} + +void assignment_fsm_start(struct gsm_subscriber_connection *conn, struct gsm_bts *bts, + struct assignment_request *req) +{ + struct osmo_fsm_inst *fi; + + static bool g_initialized = false; + if (!g_initialized) { + OSMO_ASSERT(osmo_fsm_register(&assignment_fsm) == 0); + g_initialized = true; + } + OSMO_ASSERT(conn); + OSMO_ASSERT(conn->fi); + OSMO_ASSERT(!conn->assignment.fi); + OSMO_ASSERT(!conn->assignment.new_lchan); + + assignment_count(BSC_CTR_ASSIGNMENT_ATTEMPTED); + + fi = osmo_fsm_inst_alloc_child(&assignment_fsm, conn->fi, GSCON_EV_ASSIGNMENT_END); + OSMO_ASSERT(fi); + conn->assignment.fi = fi; + fi->priv = conn; + + conn->assignment.req = *req; + + switch (req->chan_mode) { + + case GSM48_CMODE_SPEECH_V1: + case GSM48_CMODE_SPEECH_EFR: + case GSM48_CMODE_SPEECH_AMR: + conn->assignment.requires_voice_stream = true; + /* Select an lchan below. */ + break; + + case GSM48_CMODE_SIGN: + conn->assignment.requires_voice_stream = false; + /* Select an lchan below. */ + break; + + default: + assignment_fail(GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_NOT_SUPP, + "Channel mode not supported: %s", + gsm48_chan_mode_name(req->chan_mode)); + return; + } + + if (conn->lchan + && lchan_type_compat_with_mode(conn->lchan->type, req->chan_mode, req->full_rate)) { + + if (conn->lchan->tch_mode == req->chan_mode) { + /* current lchan suffices and already is in the right mode. We're done. */ + LOG_ASSIGNMENT(conn, LOGL_DEBUG, + "Current lchan is compatible with requested chan_mode," + " sending BSSMAP Assignment Complete directly." + " requested chan_mode=%s; current lchan is %s", + gsm48_chan_mode_name(req->chan_mode), + gsm_lchan_name(conn->lchan)); + send_assignment_complete(conn, conn->lchan); + return; + } + + /* FIXME */ + LOG_ASSIGNMENT(conn, LOGL_ERROR, + "NOT IMPLEMENTED:" + " Current lchan would be compatible, we should send Channel Mode Modify"); +#if 0 + case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: + rc = gsm48_rx_rr_modif_ack(msg); + if (rc < 0) + bsc_assign_fail(conn, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL); + else + bsc_assign_compl(conn, 0); + break; +#endif + } + + conn->assignment.new_lchan = lchan_select_by_chan_mode(bts, req->chan_mode, req->full_rate); + + if (!conn->assignment.new_lchan) { + assignment_count_result(BSC_CTR_ASSIGNMENT_NO_CHANNEL); + assignment_fail(GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, + "BSSMAP Assignment Command:" + " No lchan available for: chan_mode=%s, full_rate=%i\n", + get_value_string(gsm48_chan_mode_names, req->chan_mode), req->full_rate); + return; + } + + assignment_fsm_update_id(conn); + LOG_ASSIGNMENT(conn, LOGL_INFO, "Starting Assignment: chan_mode=%s, full_rate=%d," + " aoip=%s MSC-rtp=%s:%u", + gsm48_chan_mode_name(req->chan_mode), req->full_rate, + req->aoip ? "yes" : "no", req->msc_rtp_addr, req->msc_rtp_port); + + assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE); + { + struct lchan_activate_info info = { + .activ_for = FOR_ASSIGNMENT, + .for_conn = conn, + .chan_mode = req->chan_mode, + .requires_voice_stream = conn->assignment.requires_voice_stream, + .old_lchan = conn->lchan, + }; + lchan_activate(conn->assignment.new_lchan, &info); + } +} + +static void assignment_fsm_wait_lchan(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case ASSIGNMENT_EV_LCHAN_ACTIVE: + if (data != conn->assignment.new_lchan) + return; + + assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void assignment_fsm_wait_rr_ass_complete_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_CONN(); + + rc = gsm48_send_rr_ass_cmd(conn->lchan, conn->assignment.new_lchan, + conn->lchan->ms_power); + + if (rc) + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, "Unable to send RR Assignment Command"); +} + +static uint8_t get_cause(void *data) +{ + if (data) + return *(uint8_t*)data; + return GSM0808_CAUSE_EQUIPMENT_FAILURE; +} + +static void assignment_fsm_wait_rr_ass_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE: + assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED); + return; + + case ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL: + assignment_count_result(BSC_CTR_ASSIGNMENT_FAILED); + assignment_fail(get_cause(data), "Rx RR Assignment Failure"); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void assignment_fsm_wait_lchan_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_CONN(); + /* The RR Assignment Complete counts as RLL Establish event */ + osmo_fsm_inst_dispatch(conn->assignment.new_lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, 0); +} + +static void assignment_fsm_wait_lchan_established(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case ASSIGNMENT_EV_LCHAN_ESTABLISHED: + if (conn->assignment.requires_voice_stream) + assignment_fsm_state_chg(ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC); + else + assignment_success(conn); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void assignment_fsm_wait_mgw_endpoint_to_msc_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_CONN(); + + OSMO_ASSERT(conn->assignment.requires_voice_stream); + + LOG_ASSIGNMENT(conn, LOGL_DEBUG, + "Connecting MGW endpoint to the MSC's RTP port: %s:%u", + conn->assignment.req.msc_rtp_addr, + conn->assignment.req.msc_rtp_port); + + if (!gscon_connect_mgw_to_msc(conn, + conn->assignment.new_lchan, + conn->assignment.req.msc_rtp_addr, + conn->assignment.req.msc_rtp_port, + fi, + ASSIGNMENT_EV_MSC_MGW_OK, + ASSIGNMENT_EV_MSC_MGW_FAIL, + NULL, + &conn->assignment.created_ci_for_msc)) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to connect MGW endpoint to the MSC side"); + return; + } +} + +static void assignment_fsm_wait_mgw_endpoint_to_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + const struct mgcp_conn_peer *mgw_info; + GET_CONN(); + switch (event) { + + case ASSIGNMENT_EV_MSC_MGW_OK: + mgw_info = mgwep_ci_get_rtp_info(conn->user_plane.mgw_endpoint_ci_msc); + if (!mgw_info) { + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to retrieve RTP port info allocated by MGW for the MSC side."); + return; + } + LOG_ASSIGNMENT(conn, LOGL_DEBUG, "MGW's MSC side CI: %s:%u", + mgw_info->addr, mgw_info->port); + assignment_success(conn); + return; + + case ASSIGNMENT_EV_MSC_MGW_FAIL: + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, + "Unable to connect MGW endpoint to the MSC side"); + return; + + default: + OSMO_ASSERT(false); + } +} + +#define S(x) (1 << (x)) + +static const struct osmo_fsm_state assignment_fsm_states[] = { + [ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE] = { + .name = "WAIT_LCHAN_ACTIVE", + .action = assignment_fsm_wait_lchan, + .in_event_mask = 0 + | S(ASSIGNMENT_EV_LCHAN_ACTIVE) + , + .out_state_mask = 0 + | S(ASSIGNMENT_ST_WAIT_LCHAN_ACTIVE) + | S(ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE) + , + }, + [ASSIGNMENT_ST_WAIT_RR_ASS_COMPLETE] = { + .name = "WAIT_RR_ASS_COMPLETE", + .onenter = assignment_fsm_wait_rr_ass_complete_onenter, + .action = assignment_fsm_wait_rr_ass_complete, + .in_event_mask = 0 + | S(ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE) + | S(ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL) + , + .out_state_mask = 0 + | S(ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED) + , + }, + [ASSIGNMENT_ST_WAIT_LCHAN_ESTABLISHED] = { + .name = "WAIT_LCHAN_ESTABLISHED", + .onenter = assignment_fsm_wait_lchan_established_onenter, + .action = assignment_fsm_wait_lchan_established, + .in_event_mask = 0 + | S(ASSIGNMENT_EV_LCHAN_ESTABLISHED) + , + .out_state_mask = 0 + | S(ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC) + , + }, + [ASSIGNMENT_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { + .name = "WAIT_MGW_ENDPOINT_TO_MSC", + .onenter = assignment_fsm_wait_mgw_endpoint_to_msc_onenter, + .action = assignment_fsm_wait_mgw_endpoint_to_msc, + .in_event_mask = 0 + | S(ASSIGNMENT_EV_MSC_MGW_OK) + | S(ASSIGNMENT_EV_MSC_MGW_FAIL) + , + }, +}; + +static const struct value_string assignment_fsm_event_names[] = { + OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ACTIVE), + OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ESTABLISHED), + OSMO_VALUE_STRING(ASSIGNMENT_EV_LCHAN_ERROR), + OSMO_VALUE_STRING(ASSIGNMENT_EV_MSC_MGW_OK), + OSMO_VALUE_STRING(ASSIGNMENT_EV_MSC_MGW_FAIL), + OSMO_VALUE_STRING(ASSIGNMENT_EV_RR_ASSIGNMENT_COMPLETE), + OSMO_VALUE_STRING(ASSIGNMENT_EV_RR_ASSIGNMENT_FAIL), + OSMO_VALUE_STRING(ASSIGNMENT_EV_CONN_RELEASING), + {} +}; + +void assignment_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case ASSIGNMENT_EV_CONN_RELEASING: + assignment_count_result(BSC_CTR_ASSIGNMENT_STOPPED); + osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, 0); + return; + + case ASSIGNMENT_EV_LCHAN_ERROR: + if (data != conn->assignment.new_lchan) + return; + assignment_fail(conn->assignment.new_lchan->activate.gsm0808_error_cause, + "Failed to activate lchan %s", + gsm_lchan_name(conn->assignment.new_lchan)); + return; + + default: + return; + } +} + +int assignment_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + GET_CONN(); + assignment_count_result(BSC_CTR_ASSIGNMENT_TIMEOUT); + assignment_fail(GSM0808_CAUSE_EQUIPMENT_FAILURE, "Timeout"); + return 0; +} + +void assignment_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) +{ + GET_CONN(); + assignment_reset(conn); + conn->assignment.fi = NULL; +} + +static struct osmo_fsm assignment_fsm = { + .name = "assignment", + .states = assignment_fsm_states, + .num_states = ARRAY_SIZE(assignment_fsm_states), + .log_subsys = DRSL, + .event_names = assignment_fsm_event_names, + .allstate_action = assignment_fsm_allstate_action, + .allstate_event_mask = 0 + | S(ASSIGNMENT_EV_CONN_RELEASING) + | S(ASSIGNMENT_EV_LCHAN_ERROR) + , + .timer_cb = assignment_fsm_timer_cb, + .cleanup = assignment_fsm_cleanup, +}; diff --git a/src/osmo-bsc/bsc_api.c b/src/osmo-bsc/bsc_api.c index eed475b34..a6ecbc9a4 100644 --- a/src/osmo-bsc/bsc_api.c +++ b/src/osmo-bsc/bsc_api.c @@ -22,7 +22,6 @@ */ #include -#include #include #include #include @@ -34,161 +33,13 @@ #include #include #include +#include #include #include #include -#define GSM0808_T10_VALUE 6, 0 - -static void handle_release(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan); -static void handle_chan_ack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan); -static void handle_chan_nack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan); - -/* - * Start a new assignment and make sure that it is completed within T10 either - * positively, negatively or by the timeout. - * - * 1.) allocate a new lchan - * 2.) copy the encryption key and other data from the - * old to the new channel. - * 3.) RSL Channel Activate this channel and wait - * - * -> Signal handler for the LCHAN - * 4.) Send GSM 04.08 assignment command to the MS - * - * -> Assignment Complete/Assignment Failure - * 5.) Release the SDCCH, continue signalling on the new link - */ -static int handle_new_assignment(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate) -{ - struct gsm_lchan *new_lchan; - enum gsm_chan_t chan_type; - - chan_type = full_rate ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H; - - new_lchan = lchan_alloc(conn_get_bts(conn), chan_type, 0); - - if (!new_lchan) { - LOGP(DMSC, LOGL_NOTICE, "%s No free channel for %s\n", - bsc_subscr_name(conn->bsub), gsm_lchant_name(chan_type)); - return -1; - } - - /* check if we are on TCH/F and requested TCH/H, but got TCH/F */ - if (conn->lchan->type == new_lchan->type - && chan_type != new_lchan->type) { - LOGPLCHAN(conn->lchan, DHO, LOGL_NOTICE, - "-> %s Will not re-assign to identical channel type, %s was requested\n", - gsm_lchan_name(new_lchan), gsm_lchant_name(chan_type)); - lchan_free(new_lchan); - return -1; - } - - /* copy old data to the new channel */ - memcpy(&new_lchan->encr, &conn->lchan->encr, sizeof(new_lchan->encr)); - new_lchan->ms_power = conn->lchan->ms_power; - new_lchan->bs_power = conn->lchan->bs_power; - new_lchan->rqd_ta = conn->lchan->rqd_ta; - - /* copy new data to it */ - new_lchan->tch_mode = chan_mode; - new_lchan->rsl_cmode = (chan_mode == GSM48_CMODE_SIGN) ? - RSL_CMOD_SPD_SIGN : RSL_CMOD_SPD_SPEECH; - - /* handle AMR correctly */ - if (chan_mode == GSM48_CMODE_SPEECH_AMR) - bsc_mr_config(conn, new_lchan, full_rate); - - if (rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTRA_NORM_ASS, 0) < 0) { - LOGPLCHAN(new_lchan, DHO, LOGL_ERROR, "could not activate channel\n"); - lchan_free(new_lchan); - return -1; - } - - /* remember that we have the channel */ - conn->secondary_lchan = new_lchan; - new_lchan->conn = conn; - return 0; -} - -/* - * \brief Check if the given channel is compatible with the mode/fullrate - */ -static int chan_compat_with_mode(struct gsm_lchan *lchan, int chan_mode, int full_rate) -{ - switch (chan_mode) { - case GSM48_CMODE_SIGN: - switch (lchan->type) { - case GSM_LCHAN_TCH_F: - case GSM_LCHAN_TCH_H: - case GSM_LCHAN_SDCCH: - return 1; - default: - return 0; - } - case GSM48_CMODE_SPEECH_V1: - case GSM48_CMODE_SPEECH_AMR: - case GSM48_CMODE_DATA_3k6: - case GSM48_CMODE_DATA_6k0: - /* these services can all run on TCH/H, but we may have - * an explicit override by the 'full_rate' argument */ - switch (lchan->type) { - case GSM_LCHAN_TCH_F: - return full_rate ? 1 : 0; - case GSM_LCHAN_TCH_H: - return full_rate ? 0 : 1; - default: - return 0; - } - case GSM48_CMODE_DATA_12k0: - case GSM48_CMODE_DATA_14k5: - case GSM48_CMODE_SPEECH_EFR: - /* these services all explicitly require a TCH/F */ - return (lchan->type == GSM_LCHAN_TCH_F) ? 1 : 0; - default: - return 0; - } -} - -/*! Send a GSM08.08 Assignment Request. Right now this does not contain the - * audio codec type or the allowed rates for the config. In case the current - * channel does not allow the selected mode a new one will be allocated. - * \param[out] conn related subscriber connection - * \param[in] chan_mode mode of the channel (see enum gsm48_chan_mode) - * \param[in] full_rate select full rate or half rate channel - * \returns 0 on success, 1 when no operation is neccessary, -1 on failure */ -int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_mode, int full_rate) -{ - /* TODO: Add multirate configuration, make it work for more than audio. */ - - if (!chan_compat_with_mode(conn->lchan, chan_mode, full_rate)) { - if (handle_new_assignment(conn, chan_mode, full_rate) != 0) - goto error; - } else { - /* Check if the channel is already in the requested mode, if - * yes, we skip unnecessary channel mode modify operations. */ - if (conn->lchan->tch_mode == chan_mode) - return 1; - - if (chan_mode == GSM48_CMODE_SPEECH_AMR) - bsc_mr_config(conn, conn->lchan, full_rate); - - LOGPLCHAN(conn->lchan, DMSC, LOGL_NOTICE, - "Sending ChanModify for speech: %s\n", - get_value_string(gsm48_chan_mode_names, chan_mode)); - gsm48_lchan_modify(conn->lchan, chan_mode); - } - - /* we expect the caller will manage T10 */ - return 0; - -error: - bsc_assign_fail(conn, 0, NULL); - return -1; -} - int gsm0808_page(struct gsm_bts *bts, unsigned int page_group, unsigned int mi_len, uint8_t *mi, int chan_type) { @@ -222,101 +73,3 @@ int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher, return gsm48_send_rr_ciph_mode(conn->lchan, include_imeisv); } - -/* - * Release all occupied RF Channels but stay around for more. - */ -int gsm0808_clear(struct gsm_subscriber_connection *conn) -{ - if (conn->ho) - bsc_clear_handover(conn, 1); - - if (conn->secondary_lchan) - lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END); - - if (conn->lchan) - lchan_release(conn->lchan, 1, RSL_REL_NORMAL); - - conn->lchan = NULL; - conn->secondary_lchan = NULL; - - return 0; -} - -static int bsc_handle_lchan_signal(unsigned int subsys, unsigned int signal, - void *handler_data, void *signal_data) -{ - struct gsm_lchan *lchan; - struct lchan_signal_data *lchan_data; - - if (subsys != SS_LCHAN) - return 0; - - - lchan_data = signal_data; - if (!lchan_data->lchan || !lchan_data->lchan->conn) - return 0; - - lchan = lchan_data->lchan; - - switch (signal) { - case S_LCHAN_UNEXPECTED_RELEASE: - LOGPLCHAN(lchan, DMSC, LOGL_NOTICE, "S_LCHAN_UNEXPECTED_RELEASE\n"); - handle_release(lchan->conn, lchan); - break; - case S_LCHAN_ACTIVATE_ACK: - handle_chan_ack(lchan->conn, lchan); - break; - case S_LCHAN_ACTIVATE_NACK: - handle_chan_nack(lchan->conn, lchan); - break; - } - - return 0; -} - -static void handle_release(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) -{ - if (conn->secondary_lchan == lchan) { - LOGPLCHAN(lchan, DMSC, LOGL_NOTICE, - "lchan release on new lchan, Assignment failed\n"); - conn->secondary_lchan = NULL; - - bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE, NULL); - } - - /* clear the connection now */ - bsc_clear_request(conn, 0); - - /* now give up all channels */ - if (conn->lchan == lchan) - conn->lchan = NULL; - if (conn->ho && conn->ho->new_lchan == lchan) - bsc_clear_handover(conn, 0); - lchan->conn = NULL; -} - -static void handle_chan_ack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) -{ - if (conn->secondary_lchan != lchan) - return; - - LOGPLCHAN(lchan, DMSC, LOGL_NOTICE, "Sending RR Assignment\n"); - gsm48_send_rr_ass_cmd(conn->lchan, lchan, lchan->ms_power); -} - -static void handle_chan_nack(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) -{ - if (conn->secondary_lchan != lchan) - return; - - LOGPLCHAN(lchan, DMSC, LOGL_ERROR, "Channel activation failed.\n"); - conn->secondary_lchan->conn = NULL; - conn->secondary_lchan = NULL; - bsc_assign_fail(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE, NULL); -} - -static __attribute__((constructor)) void on_dso_load_bsc(void) -{ - osmo_signal_register_handler(SS_LCHAN, bsc_handle_lchan_signal, NULL); -} diff --git a/src/osmo-bsc/bsc_dyn_ts.c b/src/osmo-bsc/bsc_dyn_ts.c deleted file mode 100644 index ed7caed7f..000000000 --- a/src/osmo-bsc/bsc_dyn_ts.c +++ /dev/null @@ -1,60 +0,0 @@ -/* Dynamic PDCH initialisation implementation shared across NM and RSL */ - -/* (C) 2016 by sysmocom s.f.m.c. GmbH - * 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 . - * - */ - -#include -#include -#include -#include - -static void tchf_pdch_ts_init(struct gsm_bts_trx_ts *ts) -{ - int rc; - - rc = rsl_ipacc_pdch_activate(ts, 1); - if (rc != 0 && rc != -ENOTSUP) - LOGP(DRSL, LOGL_ERROR, "%s %s: PDCH ACT failed\n", - gsm_ts_name(ts), gsm_pchan_name(ts->pchan)); -} - -static void tchf_tchh_pdch_ts_init(struct gsm_bts_trx_ts *ts) -{ - dyn_ts_switchover_start(ts, GSM_PCHAN_PDCH); -} - -void dyn_ts_init(struct gsm_bts_trx_ts *ts) -{ - /* Clear all TCH/F_PDCH flags */ - ts->flags &= ~(TS_F_PDCH_PENDING_MASK | TS_F_PDCH_ACTIVE); - - /* Clear TCH/F_TCH/H_PDCH state */ - ts->dyn.pchan_is = ts->dyn.pchan_want = GSM_PCHAN_NONE; - ts->dyn.pending_chan_activ = NULL; - - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_PDCH: - tchf_pdch_ts_init(ts); - break; - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - tchf_tchh_pdch_ts_init(ts); - break; - default: - break; - } -} diff --git a/src/osmo-bsc/bsc_subscr_conn_fsm.c b/src/osmo-bsc/bsc_subscr_conn_fsm.c index 2f7afdc3a..b9239bd92 100644 --- a/src/osmo-bsc/bsc_subscr_conn_fsm.c +++ b/src/osmo-bsc/bsc_subscr_conn_fsm.c @@ -26,8 +26,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -37,6 +37,9 @@ #include #include #include +#include +#include +#include #include #include @@ -48,113 +51,40 @@ #define MGCP_MGW_HO_TIMEOUT 4 /* in seconds */ #define MGCP_MGW_HO_TIMEOUT_TIMER_NR 2 -#define ENDPOINT_ID "rtpbridge/*@mgw" - enum gscon_fsm_states { ST_INIT, /* waiting for CC from MSC */ ST_WAIT_CC, /* active connection */ ST_ACTIVE, - /* during assignment; waiting for ASS_CMPL */ - ST_WAIT_ASS_CMPL, + ST_ASSIGNMENT, + ST_HANDOVER, /* BSSMAP CLEAR has been received */ ST_CLEARING, - -/* MGW handling */ - /* during assignment; waiting for MGW response to CRCX for BTS */ - ST_WAIT_CRCX_BTS, - /* during assignment; waiting for MGW response to MDCX for BTS */ - ST_WAIT_MDCX_BTS, - /* during assignment; waiting for MGW response to CRCX for MSC */ - ST_WAIT_CRCX_MSC, - -/* MT (inbound) handover */ - /* Wait for Handover Access from MS/BTS */ - ST_WAIT_MT_HO_ACC, - /* Wait for RR Handover Complete from MS/BTS */ - ST_WAIT_MT_HO_COMPL, - -/* MO (outbound) handover */ - /* Wait for Handover Command / Handover Required Reject from MSC */ - ST_WAIT_MO_HO_CMD, - /* Wait for Clear Command from MSC */ - ST_MO_HO_PROCEEDING, - -/* Internal HO handling */ - /* Wait for the handover logic to complete the handover */ - ST_WAIT_HO_COMPL, - /* during handover; waiting for MGW response to MDCX for BTS */ - ST_WAIT_MDCX_BTS_HO, }; static const struct value_string gscon_fsm_event_names[] = { {GSCON_EV_A_CONN_IND, "MT-CONNECT.ind"}, {GSCON_EV_A_CONN_REQ, "MO-CONNECT.req"}, {GSCON_EV_A_CONN_CFM, "MO-CONNECT.cfm"}, - {GSCON_EV_A_ASSIGNMENT_CMD, "ASSIGNMENT_CMD"}, {GSCON_EV_A_CLEAR_CMD, "CLEAR_CMD"}, {GSCON_EV_A_DISC_IND, "DISCONNET.ind"}, - {GSCON_EV_A_HO_REQ, "HANDOVER_REQUEST"}, - - {GSCON_EV_RR_ASS_COMPL, "RR_ASSIGN_COMPL"}, - {GSCON_EV_RR_ASS_FAIL, "RR_ASSIGN_FAIL"}, - {GSCON_EV_RLL_REL_IND, "RLL_RELEASE.ind"}, - {GSCON_EV_RSL_CONN_FAIL, "RSL_CONN_FAIL.ind"}, - {GSCON_EV_RSL_CLEAR_COMPL, "RSL_CLEAR_COMPLETE"}, - - {GSCON_EV_MO_DTAP, "MO-DTAP"}, - {GSCON_EV_MT_DTAP, "MT-DTAP"}, + {GSCON_EV_ASSIGNMENT_START, "ASSIGNMENT_START"}, + {GSCON_EV_ASSIGNMENT_END, "ASSIGNMENT_END"}, + {GSCON_EV_HANDOVER_START, "HANDOVER_START"}, + {GSCON_EV_HANDOVER_END, "HANDOVER_END"}, + {GSCON_EV_RSL_CONN_FAIL, "RSL_CONN_FAIL"}, + {GSCON_EV_MO_DTAP, "MO_DTAP"}, + {GSCON_EV_MT_DTAP, "MT_DTAP"}, {GSCON_EV_TX_SCCP, "TX_SCCP"}, - - {GSCON_EV_MGW_FAIL_BTS, "MGW_FAILURE_BTS"}, - {GSCON_EV_MGW_FAIL_MSC, "MGW_FAILURE_MSC"}, - {GSCON_EV_MGW_CRCX_RESP_BTS, "MGW_CRCX_RESPONSE_BTS"}, - {GSCON_EV_MGW_MDCX_RESP_BTS, "MGW_MDCX_RESPONSE_BTS"}, - {GSCON_EV_MGW_CRCX_RESP_MSC, "MGW_CRCX_RESPONSE_MSC"}, - {GSCON_EV_MGW_MDCX_RESP_MSC, "MGW_MDCX_RESPONSE_MSC"}, - - {GSCON_EV_HO_START, "HO_START"}, - {GSCON_EV_HO_TIMEOUT, "HO_TIMEOUT"}, - {GSCON_EV_HO_FAIL, "HO_FAIL"}, - {GSCON_EV_HO_COMPL, "HO_COMPL"}, + {GSCON_EV_MGW_MDCX_RESP_MSC, "MGW_MDCX_RESP_MSC"}, {GSCON_EV_LCLS_FAIL, "LCLS_FAIL"}, - - {0, NULL} + {GSCON_EV_FORGET_LCHAN, "FORGET_LCHAN"}, + {GSCON_EV_FORGET_MGW_ENDPOINT, "FORGET_MGW_ENDPOINT"}, + {} }; -/* Depending on the channel mode and rate, set the codec type that is signalled - * towards the MGW. */ -static void mgcp_pick_codec(struct gsm_subscriber_connection *conn, - struct mgcp_conn_peer *conn_peer) -{ - switch (conn->user_plane.chan_mode) { - case GSM48_CMODE_SPEECH_V1: - if (conn->user_plane.full_rate) - conn_peer->codecs[0] = CODEC_GSM_8000_1; - else - conn_peer->codecs[0] = CODEC_GSMHR_8000_1; - conn_peer->codecs_len = 1; - break; - case GSM48_CMODE_SPEECH_EFR: - conn_peer->codecs[0] = CODEC_GSMEFR_8000_1; - conn_peer->codecs_len = 1; - break; - case GSM48_CMODE_SPEECH_AMR: - conn_peer->codecs[0] = CODEC_AMR_8000_1; - conn_peer->codecs_len = 1; - break; - default: - conn_peer->codecs_len = 0; - } -} - struct state_timeout conn_fsm_timeouts[32] = { - [ST_WAIT_ASS_CMPL] = { .T = 10 }, - [ST_WAIT_CRCX_BTS] = { .T = 992427 }, - [ST_WAIT_MDCX_BTS] = { .T = 992427 }, - [ST_WAIT_CRCX_MSC] = { .T = 992427 }, - [ST_WAIT_MDCX_BTS_HO] = { .T = 992427 }, [ST_WAIT_CC] = { .T = 993210 }, [ST_CLEARING] = { .T = 999 }, }; @@ -169,158 +99,131 @@ struct state_timeout conn_fsm_timeouts[32] = { -1) /* forward MT DTAP from BSSAP side to RSL side */ -static inline void submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, - struct osmo_fsm_inst *fi) +static inline void submit_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg) { - OSMO_ASSERT(fi); OSMO_ASSERT(msg); OSMO_ASSERT(conn); gscon_submit_rsl_dtap(conn, msg, OBSC_LINKID_CB(msg), 1); } -/* Send data SCCP message through SCCP connection. All sigtran messages - * that are send from this FSM must use this function. Never use - * osmo_bsc_sigtran_send() directly since this would defeat the checks - * provided by this function. */ -static void sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_fsm_inst *fi) +static void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send); + +int gscon_sigtran_send(struct gsm_subscriber_connection *conn, struct msgb *msg) { int rc; + if (!msg) + return -ENOMEM; + /* Make sure that we only attempt to send SCCP messages if we have * a life SCCP connection. Otherwise drop the message. */ - if (fi->state == ST_INIT || fi->state == ST_WAIT_CC) { - LOGPFSML(fi, LOGL_ERROR, "No active SCCP connection, dropping message!\n"); + if (conn->fi->state == ST_INIT || conn->fi->state == ST_WAIT_CC) { + LOGPFSML(conn->fi, LOGL_ERROR, "No active SCCP connection, dropping message\n"); msgb_free(msg); - return; + return -ENODEV; } rc = osmo_bsc_sigtran_send(conn, msg); if (rc < 0) - LOGPFSML(fi, LOGL_ERROR, "Unable to deliver SCCP message!\n"); + LOGPFSML(conn->fi, LOGL_ERROR, "Unable to deliver SCCP message\n"); + return rc; } -/* Add the LCLS BSS Status IE to a BSSMAP message. We assume this is - * called on a msgb that was returned by gsm0808_create_ass_compl() */ -static void bssmap_add_lcls_status(struct msgb *msg, enum gsm0808_lcls_status status) +static void gscon_bssmap_clear(struct gsm_subscriber_connection *conn, + enum gsm0808_cause cause) { - OSMO_ASSERT(msg->l3h[0] == BSSAP_MSG_BSS_MANAGEMENT); - OSMO_ASSERT(msg->l3h[2] == BSS_MAP_MSG_ASSIGMENT_COMPLETE || - msg->l3h[2] == BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE || - msg->l3h[2] == BSS_MAP_MSG_HANDOVER_COMPLETE || - msg->l3h[2] == BSS_MAP_MSG_HANDOVER_PERFORMED); - OSMO_ASSERT(msgb_tailroom(msg) >= 2); - - /* append IE to end of message */ - msgb_tv_put(msg, GSM0808_IE_LCLS_BSS_STATUS, status); - /* increment the "length" byte in the BSSAP header */ - msg->l3h[1] += 2; + struct msgb *resp = gsm0808_create_clear_rqst(cause); + gscon_sigtran_send(conn, resp); } -/* Add (append) the LCLS BSS Status IE to a BSSMAP message, if there is any LCLS - * active on the given \a conn */ -static void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection *conn, - struct msgb *msg) +/* forward MO DTAP from RSL side to BSSAP side */ +static void forward_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_fsm_inst *fi) { - enum gsm0808_lcls_status status = lcls_get_status(conn); - if (status != 0xff) { - LOGPFSM(conn->fi, "Adding LCLS BSS-Status (%s) to %s\n", - gsm0808_lcls_status_name(status), - gsm0808_bssmap_name(msg->l3h[2])); - bssmap_add_lcls_status(msg, status); - } + struct msgb *resp = NULL; + + OSMO_ASSERT(msg); + OSMO_ASSERT(conn); + + resp = gsm0808_create_dtap(msg, OBSC_LINKID_CB(msg)); + gscon_sigtran_send(conn, resp); } -/* Generate and send assignment complete message */ -static void send_ass_compl(struct gsm_lchan *lchan, struct osmo_fsm_inst *fi, bool voice) +void gscon_release_lchans(struct gsm_subscriber_connection *conn, bool do_sacch_deact) { - struct msgb *resp; - struct gsm0808_speech_codec sc; - struct gsm0808_speech_codec *sc_ptr = NULL; - struct gsm_subscriber_connection *conn; - struct sockaddr_storage *addr_local = NULL; - int perm_spch = 0; - uint8_t chosen_channel; - - conn = lchan->conn; - OSMO_ASSERT(conn); + if (conn->ho.fi) + handover_end(conn, HO_RESULT_CONN_RELEASE); - /* apply LCLS configuration (if any) */ - lcls_apply_config(conn); - - LOGPFSML(fi, LOGL_DEBUG, "Sending assignment complete message... (id=%i)\n", conn->sccp.conn_id); - - /* Generate voice related fields */ - if (voice) { - perm_spch = gsm0808_permitted_speech(lchan->type, lchan->tch_mode); - switch (conn->sccp.msc->a.asp_proto) { - case OSMO_SS7_ASP_PROT_IPA: - /* don't add any AoIP specific fields. CIC allocated by MSC */ - break; - default: - OSMO_ASSERT(lchan->abis_ip.ass_compl.valid); - addr_local = &conn->user_plane.aoip_rtp_addr_local; - - /* Extrapolate speech codec from speech mode */ - gsm0808_speech_codec_from_chan_type(&sc, perm_spch); - sc_ptr = ≻ - break; - } - /* FIXME: AMR codec configuration must be derived from lchan1! */ - } + assignment_reset(conn); - chosen_channel = gsm0808_chosen_channel(lchan->tch_mode, lchan->type); - if (!chosen_channel) - LOGP(DMSC, LOGL_ERROR, "Unknown lchan type or TCH mode: %s\n", gsm_lchan_name(lchan)); + lchan_release(conn->lchan, do_sacch_deact, false, 0); + lchan_forget_conn(conn->lchan); + conn->lchan = NULL; +} - /* Generate message */ - resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause, - chosen_channel, - lchan->encr.alg_id, perm_spch, - addr_local, sc_ptr, NULL); +static void handle_bssap_n_connect(struct osmo_fsm_inst *fi, struct osmo_scu_prim *scu_prim) +{ + struct gsm_subscriber_connection *conn = fi->priv; + struct msgb *msg = scu_prim->oph.msg; + struct bssmap_header *bs; + uint8_t bssmap_type; - if (!resp) { - LOGPFSML(fi, LOGL_ERROR, "Failed to generate assignment completed message! (id=%i)\n", - conn->sccp.conn_id); + msg->l3h = msgb_l2(msg); + if (!msgb_l3(msg)) { + LOGPFSML(fi, LOGL_ERROR, "internal error: no l3 in msg\n"); + goto refuse; } - /* Add LCLS BSS-Status IE in case there is any LCLS status for this connection */ - bssmap_add_lcls_status_if_needed(conn, resp); + if (msgb_l3len(msg) < sizeof(*bs)) { + LOGPFSML(fi, LOGL_NOTICE, "message too short for BSSMAP header (%u < %zu)\n", + msgb_l3len(msg), sizeof(*bs)); + goto refuse; + } - sigtran_send(conn, resp, fi); -} + bs = (struct bssmap_header*)msgb_l3(msg); + if (msgb_l3len(msg) < (bs->length + sizeof(*bs))) { + LOGPFSML(fi, LOGL_NOTICE, + "message too short for length indicated in BSSMAP header (%u < %u)\n", + msgb_l3len(msg), bs->length); + goto refuse; + } -/* forward MO DTAP from RSL side to BSSAP side */ -static void forward_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_fsm_inst *fi) -{ - struct msgb *resp = NULL; + switch (bs->type) { + case BSSAP_MSG_BSS_MANAGEMENT: + break; + default: + LOGPFSML(fi, LOGL_NOTICE, + "message type not allowed for N-CONNECT: %s\n", gsm0808_bssap_name(bs->type)); + goto refuse; + } - OSMO_ASSERT(msg); - OSMO_ASSERT(conn); + msg->l4h = &msg->l3h[sizeof(*bs)]; + bssmap_type = msg->l4h[0]; - resp = gsm0808_create_dtap(msg, OBSC_LINKID_CB(msg)); - sigtran_send(conn, resp, fi); -} + LOGPFSML(fi, LOGL_DEBUG, "Rx N-CONNECT: %s: %s\n", gsm0808_bssap_name(bs->type), + gsm0808_bssmap_name(bssmap_type)); -/* In case there are open MGCP connections, toss - * those connections */ -static void toss_mgcp_conn(struct gsm_subscriber_connection *conn, struct osmo_fsm_inst *fi) -{ - LOGPFSML(fi, LOGL_ERROR, "tossing all MGCP connections...\n"); + switch (bssmap_type) { + case BSS_MAP_MSG_HANDOVER_RQST: + /* First off, accept the new conn. */ + osmo_sccp_tx_conn_resp(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id, + &scu_prim->u.connect.called_addr, NULL, 0); - if (conn->user_plane.fi_bts) { - mgcp_conn_delete(conn->user_plane.fi_bts); - conn->user_plane.fi_bts = NULL; - } + /* Make sure the conn FSM will osmo_sccp_tx_disconn() on term */ + conn->sccp.state = SUBSCR_SCCP_ST_CONNECTED; - if (conn->user_plane.fi_msc) { - mgcp_conn_delete(conn->user_plane.fi_msc); - conn->user_plane.fi_msc = NULL; + /* Inter-BSC MT Handover Request, another BSS is handovering to us. */ + handover_start_inter_bsc_mt(conn, msg); + return; + default: + break; } - if (conn->user_plane.mgw_endpoint) { - talloc_free(conn->user_plane.mgw_endpoint); - conn->user_plane.mgw_endpoint = NULL; - } + LOGPFSML(fi, LOGL_NOTICE, "No support for N-CONNECT: %s: %s\n", + gsm0808_bssap_name(bs->type), gsm0808_bssmap_name(bssmap_type)); +refuse: + osmo_sccp_tx_disconn(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id, + &scu_prim->u.connect.called_addr, 0); + osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); } static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) @@ -329,6 +232,7 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) struct osmo_scu_prim *scu_prim = NULL; struct msgb *msg = NULL; int rc; + enum handover_result ho_result; switch (event) { case GSCON_EV_A_CONN_REQ: @@ -347,8 +251,10 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) * using T3210 (20s), T3220 (5s) or T3230 (10s) */ conn_fsm_state_chg(ST_WAIT_CC); } + gscon_update_id(conn); break; case GSCON_EV_A_CONN_IND: + gscon_update_id(conn); scu_prim = data; if (!conn->sccp.msc) { LOGPFSML(fi, LOGL_NOTICE, "N-CONNECT.ind from unknown MSC %s\n", @@ -356,18 +262,37 @@ static void gscon_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) osmo_sccp_tx_disconn(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, 0); osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); + return; } /* FIXME: Extract optional IMSI and update FSM using osmo_fsm_inst_set_id() * related: OS2969 (same as above) */ - LOGPFSML(fi, LOGL_NOTICE, "No support for MSC-originated SCCP Connections yet\n"); - osmo_sccp_tx_disconn(conn->sccp.msc->a.sccp_user, scu_prim->u.connect.conn_id, - &scu_prim->u.connect.called_addr, 0); - osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); - break; + handle_bssap_n_connect(fi, scu_prim); + break; + case GSCON_EV_HANDOVER_END: + ho_result = HO_RESULT_ERROR; + if (data) + ho_result = *(enum handover_result*)data; + LOGPFSML(fi, LOGL_DEBUG, "Handover result: %s\n", handover_result_name(ho_result)); + if (ho_result == HO_RESULT_OK) { + /* In this case the ho struct should still be populated. */ + if (conn->ho.scope & HO_INTER_BSC_MT) { + /* Done with establishing a conn where we accept another BSC's MS via + * inter-BSC handover */ + + osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0); + gscon_dtap_cache_flush(conn, 1); + return; + } + LOG_HO(conn, LOGL_ERROR, + "Conn is in state %s, the only accepted handover kind is inter-BSC MT", + osmo_fsm_inst_state_name(conn->fi)); + } + gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE); + osmo_fsm_inst_state_chg(fi, ST_CLEARING, 60, 999); + return; default: OSMO_ASSERT(false); - break; } } @@ -385,481 +310,232 @@ static void gscon_fsm_wait_cc(struct osmo_fsm_inst *fi, uint32_t event, void *da break; default: OSMO_ASSERT(false); - break; } } -static const char *get_mgw_ep_name(struct gsm_subscriber_connection *conn) -{ - static char ep_name[256]; - struct bsc_msc_data *msc = conn->sccp.msc; - - switch (conn->sccp.msc->a.asp_proto) { - case OSMO_SS7_ASP_PROT_IPA: - /* derive endpoint name from CIC on A interface side */ - snprintf(ep_name, sizeof(ep_name), "%x@mgw", - mgcp_port_to_cic(conn->user_plane.rtp_port, msc->rtp_base)); - break; - default: - /* use dynamic RTPBRIDGE endpoint allocation in MGW */ - osmo_strlcpy(ep_name, ENDPOINT_ID, sizeof(ep_name)); - break; - } - return ep_name; -} - -#define assignment_failed(fi, cause) \ - _assignment_failed(fi, cause, __FILE__, __LINE__) -static void _assignment_failed(struct osmo_fsm_inst *fi, enum gsm0808_cause cause, - const char *file, int line) +static void gscon_fsm_active_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) { struct gsm_subscriber_connection *conn = fi->priv; - struct msgb *resp = NULL; - - LOGPFSMLSRC(fi, LOGL_ERROR, file, line, "Assignment failed: %s\n", gsm0808_cause_name(cause)); - - resp = gsm0808_create_assignment_failure(cause, NULL); - sigtran_send(conn, resp, fi); - if (fi->state != ST_ACTIVE) - conn_fsm_state_chg(ST_ACTIVE); + if (!conn->lchan) + gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE); } /* We're on an active subscriber connection, passing DTAP back and forth */ static void gscon_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data) { struct gsm_subscriber_connection *conn = fi->priv; - struct msgb *resp = NULL; - struct mgcp_conn_peer conn_peer; - int rc; + struct gsm_bts *bts; switch (event) { - case GSCON_EV_A_ASSIGNMENT_CMD: - /* MSC requests us to perform assignment, this code section is - * triggered via signal GSCON_EV_A_ASSIGNMENT_CMD from - * bssmap_handle_assignm_req() in osmo_bsc_bssap.c, which does - * the parsing of incoming assignment requests. */ - - LOGPFSML(fi, LOGL_NOTICE, "Channel assignment: chan_mode=%s, full_rate=%i\n", - get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode), - conn->user_plane.full_rate); - - /* FIXME: We need to check if current channel is sufficient. If - * yes, do MODIFY. If not, do assignment (see commented lines below) */ - - switch (conn->user_plane.chan_mode) { - case GSM48_CMODE_SPEECH_V1: - case GSM48_CMODE_SPEECH_EFR: - case GSM48_CMODE_SPEECH_AMR: - /* A voice channel is requested, so we run down the - * mgcp-ass-mgcp state-chain (see FIXME above) */ - memset(&conn_peer, 0, sizeof(conn_peer)); - mgcp_pick_codec(conn, &conn_peer); - conn_peer.call_id = conn->sccp.conn_id; - conn_peer.ptime = 20; - osmo_strlcpy(conn_peer.endpoint, get_mgw_ep_name(conn), sizeof(conn_peer.endpoint)); - - /* (Pre)Change state and create the connection */ - conn_fsm_state_chg(ST_WAIT_CRCX_BTS); - conn->user_plane.fi_bts = - mgcp_conn_create(conn->network->mgw.client, fi, GSCON_EV_MGW_FAIL_BTS, - GSCON_EV_MGW_CRCX_RESP_BTS, &conn_peer); - if (!conn->user_plane.fi_bts) { - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - return; - } - break; - case GSM48_CMODE_SIGN: - /* A signalling channel is requested, so we perform the - * channel assignment directly without performing any - * MGCP actions. ST_WAIT_ASS_CMPL will see by the - * conn->user_plane.chan_mode parameter that this - * assignment is for a signalling channel and will then - * change back to ST_ACTIVE (here) immediately. */ - rc = gsm0808_assign_req(conn, conn->user_plane.chan_mode, - conn->user_plane.full_rate); - - if (rc == 1) { - send_ass_compl(conn->lchan, fi, false); - return; - } else if (rc != 0) { - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - return; - } - conn_fsm_state_chg(ST_WAIT_ASS_CMPL); - break; - default: - /* An unsupported channel is requested, so we have to - * reject this request by sending an assignment failure - * message immediately */ - LOGPFSML(fi, LOGL_ERROR, "Requested channel mode is not supported! chan_mode=%s full_rate=%d\n", - get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode), - conn->user_plane.full_rate); - - /* The requested channel mode is not supported */ - assignment_failed(fi, GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_NOT_SUPP); - break; - } - break; - case GSCON_EV_HO_START: - rc = bsc_handover_start_gscon(conn); - if (rc) { - resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE); - sigtran_send(conn, resp, fi); - conn_fsm_state_chg(ST_CLEARING); - return; - } + case GSCON_EV_ASSIGNMENT_START: + bts = conn->lchan? conn->lchan->ts->trx->bts : NULL; - /* Note: No timeout is set here, T3103 in handover_logic.c - * will generate a GSCON_EV_HO_TIMEOUT event should the - * handover time out, so we do not need another timeout - * here (maybe its worth to think about giving GSCON - * more power over the actual handover process). */ - conn_fsm_state_chg(ST_WAIT_HO_COMPL); - break; - case GSCON_EV_A_HO_REQ: - /* FIXME: reject any handover requests with HO FAIL until implemented */ - break; - case GSCON_EV_MO_DTAP: - forward_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); - break; - default: - OSMO_ASSERT(false); - break; - } -} - -/* Before we may start the channel assignment we need to get an IP/Port for the - * RTP connection from the MGW */ -static void gscon_fsm_wait_crcx_bts(struct osmo_fsm_inst *fi, uint32_t event, void *data) -{ - struct gsm_subscriber_connection *conn = fi->priv; - struct mgcp_conn_peer *conn_peer = NULL; - int rc; - - switch (event) { - case GSCON_EV_MGW_CRCX_RESP_BTS: - conn_peer = data; - - /* Check if the MGW has assigned an enpoint to us, otherwise we - * can not proceed. */ - if (strlen(conn_peer->endpoint) <= 0) { - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); + if (!bts) { + LOGPFSML(fi, LOGL_ERROR, "Cannot do assignment, no active BTS\n"); return; } - /* Memorize the endpoint name we got assigned from the MGW. - * When the BTS sided connection is done, we need to create - * a second connection on that same endpoint, so we need - * to know its ID */ - if (!conn->user_plane.mgw_endpoint) - conn->user_plane.mgw_endpoint = talloc_zero_size(conn, MGCP_ENDPOINT_MAXLEN); - OSMO_ASSERT(conn->user_plane.mgw_endpoint); - osmo_strlcpy(conn->user_plane.mgw_endpoint, conn_peer->endpoint, MGCP_ENDPOINT_MAXLEN); - - /* Store the IP-Address and the port the MGW assigned to us, - * then start the channel assignment. */ - conn->user_plane.rtp_port = conn_peer->port; - conn->user_plane.rtp_ip = osmo_ntohl(inet_addr(conn_peer->addr)); - rc = gsm0808_assign_req(conn, conn->user_plane.chan_mode, conn->user_plane.full_rate); - if (rc != 0) { - assignment_failed(fi, GSM0808_CAUSE_RQSTED_SPEECH_VERSION_UNAVAILABLE); - return; - } + /* Rely on assignment_fsm timeout */ + osmo_fsm_inst_state_chg(fi, ST_ASSIGNMENT, 0, 0); + assignment_fsm_start(conn, bts, data); + return; - conn_fsm_state_chg(ST_WAIT_ASS_CMPL); + case GSCON_EV_HANDOVER_START: + rate_ctr_inc(&conn->network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]); + /* Rely on handover_fsm timeout */ + if (osmo_fsm_inst_state_chg(fi, ST_HANDOVER, 0, 0)) + LOGPFSML(fi, LOGL_ERROR, "Cannot transition to HANDOVER state, discarding\n"); + else + handover_start(data); break; + case GSCON_EV_MO_DTAP: forward_dtap(conn, (struct msgb *)data, fi); break; case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); + submit_dtap(conn, (struct msgb *)data); break; case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); + gscon_sigtran_send(conn, (struct msgb *)data); break; default: OSMO_ASSERT(false); - break; } } -/* We're waiting for an ASSIGNMENT COMPLETE from MS */ -static void gscon_fsm_wait_ass_cmpl(struct osmo_fsm_inst *fi, uint32_t event, void *data) +static void gscon_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data) { struct gsm_subscriber_connection *conn = fi->priv; - struct gsm_lchan *lchan = conn->lchan; - struct mgcp_conn_peer conn_peer; - struct in_addr addr; - int rc; switch (event) { - case GSCON_EV_RR_ASS_COMPL: - switch (conn->user_plane.chan_mode) { - case GSM48_CMODE_SPEECH_V1: - case GSM48_CMODE_SPEECH_EFR: - case GSM48_CMODE_SPEECH_AMR: - /* FIXME: What if we are using SCCP-Lite? */ - - /* We are dealing with a voice channel, so we can not - * confirm the assignment directly. We must first do - * some final steps on the MGCP side. */ - - /* Prepare parameters with the information we got during the assignment */ - memset(&conn_peer, 0, sizeof(conn_peer)); - mgcp_pick_codec(conn, &conn_peer); - addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip); - osmo_strlcpy(conn_peer.addr, inet_ntoa(addr), sizeof(conn_peer.addr)); - conn_peer.port = lchan->abis_ip.bound_port; - conn_peer.ptime = 20; - - /* (Pre)Change state and modify the connection */ - conn_fsm_state_chg(ST_WAIT_MDCX_BTS); - rc = mgcp_conn_modify(conn->user_plane.fi_bts, GSCON_EV_MGW_MDCX_RESP_BTS, &conn_peer); - if (rc != 0) { - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - return; - } - break; - case GSM48_CMODE_SIGN: - /* Confirm the successful assignment on BSSMAP and - * change back into active state */ - send_ass_compl(lchan, fi, false); - conn_fsm_state_chg(ST_ACTIVE); - break; - default: - /* Unsupported modes should have been already filtered - * by gscon_fsm_active(). If we reach the default - * section here anyway than some unsupported mode must - * have made it into the FSM, this would be a bug, so - * we fire an assertion here */ - OSMO_ASSERT(false); - break; - } + case GSCON_EV_ASSIGNMENT_END: + osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0); + gscon_dtap_cache_flush(conn, 1); + return; - break; - case GSCON_EV_RR_ASS_FAIL: - { - enum gsm0808_cause cause = GSM0808_CAUSE_RQSTED_TERRESTRIAL_RESOURCE_UNAVAILABLE; - if (data) - cause = *((enum gsm0808_cause*)data); - assignment_failed(fi, cause); - } - break; case GSCON_EV_MO_DTAP: forward_dtap(conn, (struct msgb *)data, fi); break; case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); + submit_dtap(conn, (struct msgb *)data); break; case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); + gscon_sigtran_send(conn, (struct msgb *)data); break; default: OSMO_ASSERT(false); - break; } } -/* We are waiting for the MGW response to the MDCX */ -static void gscon_fsm_wait_mdcx_bts(struct osmo_fsm_inst *fi, uint32_t event, void *data) +static void gscon_fsm_handover(struct osmo_fsm_inst *fi, uint32_t event, void *data) { struct gsm_subscriber_connection *conn = fi->priv; - struct mgcp_conn_peer conn_peer; - struct sockaddr_in *sin = NULL; switch (event) { - case GSCON_EV_MGW_MDCX_RESP_BTS: - - /* Prepare parameters with the connection information we got - * with the assignment command */ - memset(&conn_peer, 0, sizeof(conn_peer)); - mgcp_pick_codec(conn, &conn_peer); - conn_peer.call_id = conn->sccp.conn_id; - sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_remote; - conn_peer.port = osmo_ntohs(sin->sin_port); - osmo_strlcpy(conn_peer.addr, inet_ntoa(sin->sin_addr), sizeof(conn_peer.addr)); - conn_peer.ptime = 20; - - /* Make sure we use the same endpoint where we created the - * BTS connection. */ - osmo_strlcpy(conn_peer.endpoint, conn->user_plane.mgw_endpoint, sizeof(conn_peer.endpoint)); - - switch (conn->sccp.msc->a.asp_proto) { - case OSMO_SS7_ASP_PROT_IPA: - /* Send assignment complete message to the MSC */ - send_ass_compl(conn->lchan, fi, true); - conn_fsm_state_chg(ST_ACTIVE); - break; - default: - /* (Pre)Change state and create the connection */ - conn_fsm_state_chg(ST_WAIT_CRCX_MSC); - conn->user_plane.fi_msc = mgcp_conn_create(conn->network->mgw.client, fi, - GSCON_EV_MGW_FAIL_MSC, - GSCON_EV_MGW_CRCX_RESP_MSC, &conn_peer); - if (!conn->user_plane.fi_msc) { - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - return; - } - break; - } + case GSCON_EV_HANDOVER_END: + osmo_fsm_inst_state_chg(fi, ST_ACTIVE, 0, 0); + gscon_dtap_cache_flush(conn, 1); + return; - break; case GSCON_EV_MO_DTAP: forward_dtap(conn, (struct msgb *)data, fi); break; case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); + /* cache until handover is done */ + submit_dtap(conn, (struct msgb *)data); break; case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); + gscon_sigtran_send(conn, (struct msgb *)data); break; default: OSMO_ASSERT(false); - break; } } -static void gscon_fsm_wait_crcx_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data) +static bool same_mgw_info(const struct mgcp_conn_peer *a, const struct mgcp_conn_peer *b) { - struct gsm_subscriber_connection *conn = fi->priv; - struct mgcp_conn_peer *conn_peer = NULL; - struct gsm_lchan *lchan = conn->lchan; - struct sockaddr_in *sin = NULL; - - switch (event) { - case GSCON_EV_MGW_CRCX_RESP_MSC: - conn_peer = data; + if (!a || !b) + return false; + if (a == b) + return true; + if (strcmp(a->addr, b->addr)) + return false; + if (a->port != b->port) + return false; + if (a->call_id != b->call_id) + return false; + return true; +} - /* Store address information we got in response from the CRCX command. */ - sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_local; - sin->sin_family = AF_INET; - sin->sin_addr.s_addr = inet_addr(conn_peer->addr); - sin->sin_port = osmo_ntohs(conn_peer->port); +/* Make sure a conn->user_plane.mgw_endpoint is allocated with the proper mgw endpoint name. */ +struct mgw_endpoint *gscon_ensure_mgw_endpoint(struct gsm_subscriber_connection *conn) +{ + struct bsc_msc_data *msc; + if (!conn || !conn->sccp.msc) + return NULL; + if (conn->user_plane.mgw_endpoint) + return conn->user_plane.mgw_endpoint; - /* Send assignment complete message to the MSC */ - send_ass_compl(lchan, fi, true); + msc = conn->sccp.msc; - conn_fsm_state_chg(ST_ACTIVE); + switch (msc->a.asp_proto) { + case OSMO_SS7_ASP_PROT_IPA: + /* derive endpoint name from CIC on A interface side */ + conn->user_plane.mgw_endpoint = + mgw_endpoint_alloc(conn->fi, GSCON_EV_FORGET_MGW_ENDPOINT, + conn->network->mgw.client, conn->fi->id, + "%x@mgw", + mgcp_port_to_cic(conn->user_plane.rtp_port, msc->rtp_base)); - break; - case GSCON_EV_MO_DTAP: - forward_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); break; default: - OSMO_ASSERT(false); + /* use dynamic RTPBRIDGE endpoint allocation in MGW */ + conn->user_plane.mgw_endpoint = + mgw_endpoint_alloc(conn->fi, GSCON_EV_FORGET_MGW_ENDPOINT, + conn->network->mgw.client, conn->fi->id, + "rtpbridge/*@mgw"); break; } + + return conn->user_plane.mgw_endpoint; } -static void gscon_fsm_clearing(struct osmo_fsm_inst *fi, uint32_t event, void *data) +bool gscon_connect_mgw_to_msc(struct gsm_subscriber_connection *conn, + struct gsm_lchan *for_lchan, + const char *addr, uint16_t port, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data, + struct mgwep_ci **created_ci) { - struct gsm_subscriber_connection *conn = fi->priv; - struct msgb *resp; + int rc; + struct mgwep_ci *ci; + struct mgcp_conn_peer mgw_info = { + .port = port, + .call_id = conn->sccp.conn_id, + .ptime = 20, + }; + enum mgcp_verb verb; - switch (event) { - case GSCON_EV_RSL_CLEAR_COMPL: - resp = gsm0808_create_clear_complete(); - sigtran_send(conn, resp, fi); - /* we cannot terminate the FSM here, as that would send N-DISCCONNET.req - * and 3GPP TS 48.006 Section 9.2 clearly states that SCCP connections must - * always be released from the MSC side*/ - break; - default: - OSMO_ASSERT(false); - break; + mgcp_pick_codec(&mgw_info, for_lchan); + + if (created_ci) + *created_ci = NULL; + + rc = osmo_strlcpy(mgw_info.addr, addr, sizeof(mgw_info.addr)); + if (rc <= 0 || rc >= sizeof(mgw_info.addr)) { + LOGPFSML(conn->fi, LOGL_ERROR, "Failed to compose MGW endpoint address for MGW -> MSC\n"); + return false; } -} -/* Wait for the handover logic to tell us whether the handover completed, - * failed or has timed out */ -static void gscon_fsm_wait_ho_compl(struct osmo_fsm_inst *fi, uint32_t event, void *data) -{ - struct gsm_subscriber_connection *conn = fi->priv; - struct mgcp_conn_peer conn_peer; - struct gsm_lchan *lchan = conn->lchan; - struct in_addr addr; - struct msgb *resp; - int rc; + ci = conn->user_plane.mgw_endpoint_ci_msc; + if (ci) { + const struct mgcp_conn_peer *prev_crcx_info = mgwep_ci_get_rtp_info(ci); - switch (event) { - case GSCON_EV_HO_COMPL: - /* The handover logic informs us that the handover has been - * completet. Now we have to tell the MGW the IP/Port on the - * new BTS so that the uplink RTP traffic can be redirected - * there. */ - - /* Prepare parameters with the information we got during the - * handover procedure (via IPACC) */ - memset(&conn_peer, 0, sizeof(conn_peer)); - mgcp_pick_codec(conn, &conn_peer); - addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip); - osmo_strlcpy(conn_peer.addr, inet_ntoa(addr), sizeof(conn_peer.addr)); - conn_peer.port = lchan->abis_ip.bound_port; - conn_peer.ptime = 20; - - /* (Pre)Change state and modify the connection */ - conn_fsm_state_chg(ST_WAIT_MDCX_BTS_HO); - rc = mgcp_conn_modify(conn->user_plane.fi_bts, GSCON_EV_MGW_MDCX_RESP_BTS, &conn_peer); - if (rc != 0) { - resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE); - sigtran_send(conn, resp, fi); - conn_fsm_state_chg(ST_CLEARING); - return; + if (!conn->user_plane.mgw_endpoint) { + LOGPFSML(conn->fi, LOGL_ERROR, "Internal error: conn has a CI but no endoint\n"); + return false; } - break; - case GSCON_EV_HO_TIMEOUT: - case GSCON_EV_HO_FAIL: - /* The handover logic informs us that the handover failed for - * some reason. This means the phone stays on the TS/BTS on - * which it currently is. We will change back to the active - * state again as there are no further operations needed */ - conn_fsm_state_chg(ST_ACTIVE); - break; - default: - OSMO_ASSERT(false); - break; - } -} -/* Wait for the MGW to confirm handover related modification of the connection - * parameters */ -static void gscon_fsm_wait_mdcx_bts_ho(struct osmo_fsm_inst *fi, uint32_t event, void *data) -{ - struct gsm_subscriber_connection *conn = fi->priv; + if (!prev_crcx_info) { + LOGPFSML(conn->fi, LOGL_ERROR, "There already is an MGW connection for the MSC side," + " but it seems to be broken. Will not CRCX another one (%s)\n", + mgwep_ci_name(ci)); + return false; + } - switch (event) { - case GSCON_EV_MGW_MDCX_RESP_BTS: - /* The MGW has confirmed the handover MDCX, and the handover - * is now also done on the RTP side. We may now change back - * to the active state. */ - conn_fsm_state_chg(ST_ACTIVE); - break; - case GSCON_EV_MO_DTAP: - forward_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_MT_DTAP: - submit_dtap(conn, (struct msgb *)data, fi); - break; - case GSCON_EV_TX_SCCP: - sigtran_send(conn, (struct msgb *)data, fi); - break; - default: - OSMO_ASSERT(false); - break; + if (same_mgw_info(&mgw_info, prev_crcx_info)) { + LOGPFSML(conn->fi, LOGL_DEBUG, + "MSC side MGW endpoint ci is already configured to %s", + mgwep_ci_name(ci)); + return true; + } + + verb = MGCP_VERB_MDCX; + } else + verb = MGCP_VERB_CRCX; + + gscon_ensure_mgw_endpoint(conn); + + if (!conn->user_plane.mgw_endpoint) { + LOGPFSML(conn->fi, LOGL_ERROR, "Unable to allocate endpoint info\n"); + return false; } + + if (!ci) { + ci = mgw_endpoint_ci_add(conn->user_plane.mgw_endpoint, "to-MSC"); + if (created_ci) + *created_ci = ci; + conn->user_plane.mgw_endpoint_ci_msc = ci; + } + if (!ci) { + LOGPFSML(conn->fi, LOGL_ERROR, "Unable to allocate endpoint CI info\n"); + return false; + } + + mgw_endpoint_ci_request(ci, verb, &mgw_info, notify, event_success, event_failure, notify_data); + return true; } #define EV_TRANSPARENT_SCCP S(GSCON_EV_TX_SCCP) | S(GSCON_EV_MO_DTAP) | S(GSCON_EV_MT_DTAP) @@ -867,8 +543,9 @@ static void gscon_fsm_wait_mdcx_bts_ho(struct osmo_fsm_inst *fi, uint32_t event, static const struct osmo_fsm_state gscon_fsm_states[] = { [ST_INIT] = { .name = "INIT", - .in_event_mask = S(GSCON_EV_A_CONN_REQ) | S(GSCON_EV_A_CONN_IND), - .out_state_mask = S(ST_WAIT_CC), + .in_event_mask = S(GSCON_EV_A_CONN_REQ) | S(GSCON_EV_A_CONN_IND) + | S(GSCON_EV_HANDOVER_END), + .out_state_mask = S(ST_WAIT_CC) | S(ST_ACTIVE) | S(ST_CLEARING), .action = gscon_fsm_init, }, [ST_WAIT_CC] = { @@ -879,133 +556,135 @@ static const struct osmo_fsm_state gscon_fsm_states[] = { }, [ST_ACTIVE] = { .name = "ACTIVE", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_A_ASSIGNMENT_CMD) | - S(GSCON_EV_A_HO_REQ) | S(GSCON_EV_HO_START), - .out_state_mask = S(ST_CLEARING) | S(ST_WAIT_CRCX_BTS) | S(ST_WAIT_ASS_CMPL) | - S(ST_WAIT_MO_HO_CMD) | S(ST_WAIT_HO_COMPL), + .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_ASSIGNMENT_START) | + S(GSCON_EV_HANDOVER_START), + .out_state_mask = S(ST_CLEARING) | S(ST_ASSIGNMENT) | + S(ST_HANDOVER), + .onenter = gscon_fsm_active_onenter, .action = gscon_fsm_active, }, - [ST_WAIT_CRCX_BTS] = { - .name = "WAIT_CRCX_BTS", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_CRCX_RESP_BTS), - .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_ASS_CMPL), - .action = gscon_fsm_wait_crcx_bts, + [ST_ASSIGNMENT] = { + .name = "ASSIGNMENT", + .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_ASSIGNMENT_END), + .out_state_mask = S(ST_ACTIVE) | S(ST_CLEARING), + .action = gscon_fsm_assignment, }, - [ST_WAIT_ASS_CMPL] = { - .name = "WAIT_ASS_CMPL", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_RR_ASS_COMPL) | S(GSCON_EV_RR_ASS_FAIL), - .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_MDCX_BTS), - .action = gscon_fsm_wait_ass_cmpl, - }, - [ST_WAIT_MDCX_BTS] = { - .name = "WAIT_MDCX_BTS", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_MDCX_RESP_BTS), - .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_CRCX_MSC), - .action = gscon_fsm_wait_mdcx_bts, - }, - [ST_WAIT_CRCX_MSC] = { - .name = "WAIT_CRCX_MSC", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_CRCX_RESP_MSC), - .out_state_mask = S(ST_ACTIVE), - .action = gscon_fsm_wait_crcx_msc, + [ST_HANDOVER] = { + .name = "HANDOVER", + .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_HANDOVER_END), + .out_state_mask = S(ST_ACTIVE) | S(ST_CLEARING), + .action = gscon_fsm_handover, }, [ST_CLEARING] = { .name = "CLEARING", - .in_event_mask = S(GSCON_EV_RSL_CLEAR_COMPL), - .action = gscon_fsm_clearing, + /* dead end state */ }, +}; - /* TODO: external handover, probably it makes sense to break up the - * program flow in handover_logic.c a bit and handle some of the logic - * here? */ - [ST_WAIT_MT_HO_ACC] = { - .name = "WAIT_MT_HO_ACC", - }, - [ST_WAIT_MT_HO_COMPL] = { - .name = "WAIT_MT_HO_COMPL", - }, - [ST_WAIT_MO_HO_CMD] = { - .name = "WAIT_MO_HO_CMD", - }, - [ST_MO_HO_PROCEEDING] = { - .name = "MO_HO_PROCEEDING", - }, +void gscon_change_primary_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *new_lchan) +{ + /* On release, do not receive release events that look like the primary lchan is gone. */ + struct gsm_lchan *old_lchan = conn->lchan; - /* Internal handover */ - [ST_WAIT_HO_COMPL] = { - .name = "WAIT_HO_COMPL", - .in_event_mask = S(GSCON_EV_HO_COMPL) | S(GSCON_EV_HO_FAIL) | S(GSCON_EV_HO_TIMEOUT), - .out_state_mask = S(ST_ACTIVE) | S(ST_WAIT_MDCX_BTS_HO) | S(ST_CLEARING), - .action = gscon_fsm_wait_ho_compl, - }, - [ST_WAIT_MDCX_BTS_HO] = { - .name = "WAIT_MDCX_BTS_HO", - .in_event_mask = EV_TRANSPARENT_SCCP | S(GSCON_EV_MGW_MDCX_RESP_BTS), - .action = gscon_fsm_wait_mdcx_bts_ho, - .out_state_mask = S(ST_ACTIVE), - }, -}; + conn->lchan = new_lchan; + conn->lchan->conn = conn; + + if (old_lchan) { + lchan_forget_conn(old_lchan); + lchan_release(old_lchan, false, false, 0); + } +} + +void gscon_lchan_releasing(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) +{ + if (!lchan) + return; + if (conn->assignment.new_lchan == lchan) { + if (conn->assignment.fi) + osmo_fsm_inst_dispatch(conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ERROR, lchan); + lchan_forget_conn(conn->assignment.new_lchan); + conn->assignment.new_lchan = NULL; + } + if (conn->ho.mt.new_lchan == lchan) { + if (conn->ho.fi) + osmo_fsm_inst_dispatch(conn->ho.fi, HO_EV_LCHAN_ERROR, lchan); + } + if (conn->lchan == lchan) { + lchan_forget_conn(conn->lchan); + conn->lchan = NULL; + } + if (!conn->lchan) { + osmo_fsm_inst_state_chg(conn->fi, ST_CLEARING, 60, 999); + gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE); + } +} + +/* An lchan was deallocated. */ +void gscon_forget_lchan(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) +{ + if (!lchan) + return; + if (conn->assignment.new_lchan == lchan) + conn->assignment.new_lchan = NULL; + if (conn->ho.mt.new_lchan == lchan) + conn->ho.mt.new_lchan = NULL; + if (conn->lchan == lchan) + conn->lchan = NULL; + if (!conn->lchan) + gscon_bssmap_clear(conn, GSM0808_CAUSE_EQUIPMENT_FAILURE); +} + +static void gscon_forget_mgw_endpoint(struct gsm_subscriber_connection *conn) +{ + conn->user_plane.mgw_endpoint = NULL; + conn->user_plane.mgw_endpoint_ci_msc = NULL; + lchan_forget_mgw_endpoint(conn->lchan); + lchan_forget_mgw_endpoint(conn->assignment.new_lchan); + lchan_forget_mgw_endpoint(conn->ho.mt.new_lchan); +} + +void gscon_forget_mgw_endpoint_ci(struct gsm_subscriber_connection *conn, struct mgwep_ci *ci) +{ + if (ci != conn->user_plane.mgw_endpoint_ci_msc) + return; + conn->user_plane.mgw_endpoint_ci_msc = NULL; +} static void gscon_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data) { struct gsm_subscriber_connection *conn = fi->priv; - struct msgb *resp = NULL; - - /* When a connection on the MGW fails, make sure that the reference - * in our book-keeping is erased. */ - switch (event) { - case GSCON_EV_MGW_FAIL_BTS: - conn->user_plane.fi_bts = NULL; - break; - case GSCON_EV_MGW_FAIL_MSC: - conn->user_plane.fi_msc = NULL; - break; - } /* Regular allstate event processing */ switch (event) { - case GSCON_EV_MGW_FAIL_BTS: - case GSCON_EV_MGW_FAIL_MSC: - /* Note: An MGW connection die per definition at any time. - * However, if it dies during the assignment we must return - * with an assignment failure */ - OSMO_ASSERT(fi->state != ST_INIT && fi->state != ST_WAIT_CC); - if (fi->state == ST_WAIT_CRCX_BTS || fi->state == ST_WAIT_ASS_CMPL || fi->state == ST_WAIT_MDCX_BTS - || fi->state == ST_WAIT_CRCX_MSC) - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - break; case GSCON_EV_A_CLEAR_CMD: /* MSC tells us to cleanly shut down */ - conn_fsm_state_chg(ST_CLEARING); - gsm0808_clear(conn); + osmo_fsm_inst_state_chg(fi, ST_CLEARING, 60, 999); + LOGPFSML(fi, LOGL_DEBUG, "Releasing all lchans (if any) after BSSMAP Clear Command\n"); + gscon_release_lchans(conn, true); /* FIXME: Release all terestrial resources in ST_CLEARING */ /* According to 3GPP 48.008 3.1.9.1. "The BSS need not wait for the radio channel * release to be completed or for the guard timer to expire before returning the * CLEAR COMPLETE message" */ /* Close MGCP connections */ - toss_mgcp_conn(conn, fi); + mgw_endpoint_clear(conn->user_plane.mgw_endpoint); - /* FIXME: Question: Is this a hack to force a clear complete from internel? - * nobody seems to send the event from outside? */ - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RSL_CLEAR_COMPL, NULL); + gscon_sigtran_send(conn, gsm0808_create_clear_complete()); break; case GSCON_EV_A_DISC_IND: /* MSC or SIGTRAN network has hard-released SCCP connection, * terminate the FSM now. */ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, data); break; - case GSCON_EV_RLL_REL_IND: - /* BTS reports that one of the LAPDm data links was released */ - /* send proper clear request to MSC */ - LOGPFSML(fi, LOGL_DEBUG, "Tx BSSMAP CLEAR REQUEST to MSC\n"); - resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_MESSAGE_FAILURE); - sigtran_send(conn, resp, fi); + case GSCON_EV_FORGET_MGW_ENDPOINT: + gscon_forget_mgw_endpoint(conn); break; case GSCON_EV_RSL_CONN_FAIL: - LOGPFSML(fi, LOGL_DEBUG, "Tx BSSMAP CLEAR REQUEST to MSC\n"); - resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); - sigtran_send(conn, resp, fi); + if (conn->lchan) { + conn->lchan->release_in_error = true; + conn->lchan->error_cause = data ? *(uint8_t*)data : RSL_ERR_IE_ERROR; + } + gscon_bssmap_clear(conn, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); break; case GSCON_EV_MGW_MDCX_RESP_MSC: LOGPFSML(fi, LOGL_DEBUG, "Rx MDCX of MSC side (LCLS?)\n"); @@ -1022,22 +701,9 @@ static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cau { struct gsm_subscriber_connection *conn = fi->priv; - if (conn->ho) { - LOGPFSML(fi, LOGL_DEBUG, "Releasing handover state\n"); - bsc_clear_handover(conn, 1); - conn->ho = NULL; - } - - if (conn->secondary_lchan) { - LOGPFSML(fi, LOGL_DEBUG, "Releasing secondary_lchan\n"); - lchan_release(conn->secondary_lchan, 0, RSL_REL_LOCAL_END); - conn->secondary_lchan = NULL; - } - if (conn->lchan) { - LOGPFSML(fi, LOGL_DEBUG, "Releasing lchan\n"); - lchan_release(conn->lchan, 0, RSL_REL_LOCAL_END); - conn->lchan = NULL; - } + lchan_forget_conn(conn->lchan); + lchan_forget_conn(conn->assignment.new_lchan); + lchan_forget_conn(conn->ho.mt.new_lchan); if (conn->sccp.state != SUBSCR_SCCP_ST_NONE) { LOGPFSML(fi, LOGL_DEBUG, "Disconnecting SCCP\n"); @@ -1047,11 +713,6 @@ static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cau conn->sccp.state = SUBSCR_SCCP_ST_NONE; } - /* drop pending messages */ - gscon_dtap_cache_flush(conn, 0); - - penalty_timers_free(&conn->hodec2.penalty_timers); - if (conn->bsub) { LOGPFSML(fi, LOGL_DEBUG, "Putting bsc_subscr\n"); bsc_subscr_put(conn->bsub); @@ -1060,21 +721,27 @@ static void gscon_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cau llist_del(&conn->entry); talloc_free(conn); - fi->priv = NULL; } static void gscon_pre_term(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) { struct gsm_subscriber_connection *conn = fi->priv; - /* Make sure all possibly still open MGCP connections get closed */ - toss_mgcp_conn(conn, fi); + mgw_endpoint_clear(conn->user_plane.mgw_endpoint); if (conn->lcls.fi) { /* request termination of LCLS FSM */ osmo_fsm_inst_term(conn->lcls.fi, cause, NULL); conn->lcls.fi = NULL; } + + LOGPFSML(fi, LOGL_DEBUG, "Releasing all lchans (if any) because this conn is terminating\n"); + gscon_release_lchans(conn, false); + + /* drop pending messages */ + gscon_dtap_cache_flush(conn, 0); + + penalty_timers_free(&conn->hodec2.penalty_timers); } static int gscon_timer_cb(struct osmo_fsm_inst *fi) @@ -1083,6 +750,8 @@ static int gscon_timer_cb(struct osmo_fsm_inst *fi) switch (fi->T) { case 993210: + lchan_release(conn->lchan, false, true, RSL_ERR_INTERWORKING); + /* MSC has not responded/confirmed connection with CC, this * could indicate a bad SCCP connection. We now inform the the * FSM that controls the BSSMAP reset about the event. Maybe @@ -1094,16 +763,16 @@ static int gscon_timer_cb(struct osmo_fsm_inst *fi) * gscon_cleanup() above) */ osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); break; - case 10: /* Assignment Failed */ - assignment_failed(fi, GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); - break; - case MGCP_MGW_TIMEOUT_TIMER_NR: /* Assignment failed (no response from MGW) */ - assignment_failed(fi, GSM0808_CAUSE_EQUIPMENT_FAILURE); - break; - case MGCP_MGW_HO_TIMEOUT_TIMER_NR: /* Handover failed (no response from MGW) */ - conn_fsm_state_chg(ST_ACTIVE); + case 999: + /* The MSC has sent a BSSMAP Clear Command, we acknowledged that, but the conn was never + * disconnected. */ + LOGPFSML(fi, LOGL_ERROR, "Long after a BSSMAP Clear Command, the conn is still not" + " released. For sanity, discarding this conn now.\n"); + a_reset_conn_fail(conn->sccp.msc->a.reset_fsm); + osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); break; default: + LOGPFSML(fi, LOGL_ERROR, "Unknown timer %d expired\n", fi->T); OSMO_ASSERT(false); } return 0; @@ -1114,8 +783,9 @@ static struct osmo_fsm gscon_fsm = { .states = gscon_fsm_states, .num_states = ARRAY_SIZE(gscon_fsm_states), .allstate_event_mask = S(GSCON_EV_A_DISC_IND) | S(GSCON_EV_A_CLEAR_CMD) | S(GSCON_EV_RSL_CONN_FAIL) | - S(GSCON_EV_RLL_REL_IND) | S(GSCON_EV_MGW_FAIL_BTS) | S(GSCON_EV_MGW_FAIL_MSC) | - S(GSCON_EV_MGW_MDCX_RESP_MSC) | S(GSCON_EV_LCLS_FAIL), + S(GSCON_EV_LCLS_FAIL) | + S(GSCON_EV_FORGET_LCHAN) | + S(GSCON_EV_FORGET_MGW_ENDPOINT), .allstate_action = gscon_fsm_allstate, .cleanup = gscon_cleanup, .pre_term = gscon_pre_term, @@ -1191,17 +861,11 @@ static void gscon_dtap_cache_add(struct gsm_subscriber_connection *conn, struct msgb_enqueue(&conn->dtap_cache, msg); } -void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send) +static void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send) { struct msgb *msg; unsigned int flushed_count = 0; - if (conn->secondary_lchan || conn->ho) { - LOGP(DHO, LOGL_ERROR, "%s: Cannot send cached DTAP messages, handover/assignment is still ongoing\n", - bsc_subscr_name(conn->bsub)); - send = 0; - } - while ((msg = msgb_dequeue(&conn->dtap_cache))) { conn->dtap_cache_len --; flushed_count ++; @@ -1291,7 +955,7 @@ static void gsm0808_send_rsl_dtap(struct gsm_subscriber_connection *conn, failed_to_send: LOGPFSML(conn->fi, LOGL_ERROR, "Tx BSSMAP CLEAR REQUEST to MSC\n"); resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_EQUIPMENT_FAILURE); - sigtran_send(conn, resp, conn->fi); + gscon_sigtran_send(conn, resp); osmo_fsm_inst_state_chg(conn->fi, ST_ACTIVE, 0, 0); } @@ -1299,10 +963,19 @@ void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id, int allow_sacch) { /* buffer message during assignment / handover */ - if (conn->secondary_lchan || conn->ho) { + if (conn->fi->state != ST_ACTIVE) { gscon_dtap_cache_add(conn, msg, link_id, !! allow_sacch); return; } gsm0808_send_rsl_dtap(conn, msg, link_id, allow_sacch); } + +/* Compose an FSM ID, if possible from the current subscriber information */ +void gscon_update_id(struct gsm_subscriber_connection *conn) +{ + osmo_fsm_inst_update_id_f(conn->fi, "conn%u%s%s", + conn->sccp.conn_id, + conn->bsub? "_" : "", + conn->bsub? bsc_subscr_id(conn->bsub) : ""); +} diff --git a/src/osmo-bsc/bsc_subscriber.c b/src/osmo-bsc/bsc_subscriber.c index 65a660b40..2541883d5 100644 --- a/src/osmo-bsc/bsc_subscriber.c +++ b/src/osmo-bsc/bsc_subscriber.c @@ -118,6 +118,20 @@ const char *bsc_subscr_name(struct bsc_subscr *bsub) return buf; } +/* Like bsc_subscr_name() but returns only characters approved by osmo_identifier_valid(), useful for + * osmo_fsm_inst IDs. */ +const char *bsc_subscr_id(struct bsc_subscr *bsub) +{ + static char buf[32]; + if (!bsub) + return "unknown"; + if (bsub->imsi[0]) + snprintf(buf, sizeof(buf), "IMSI%s", bsub->imsi); + else + snprintf(buf, sizeof(buf), "TMSI%08x", bsub->tmsi); + return buf; +} + static void bsc_subscr_free(struct bsc_subscr *bsub) { llist_del(&bsub->entry); diff --git a/src/osmo-bsc/bsc_vty.c b/src/osmo-bsc/bsc_vty.c index ababb3eaf..274428f92 100644 --- a/src/osmo-bsc/bsc_vty.c +++ b/src/osmo-bsc/bsc_vty.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -56,7 +57,7 @@ #include #include #include -#include +#include #include #include #include @@ -65,6 +66,10 @@ #include #include #include +#include +#include +#include +#include #include @@ -552,9 +557,9 @@ static void config_write_ts_single(struct vty *vty, struct gsm_bts_trx_ts *ts) vty_out(vty, " timeslot %u%s", ts->nr, VTY_NEWLINE); if (ts->tsc != -1) vty_out(vty, " training_sequence_code %u%s", ts->tsc, VTY_NEWLINE); - if (ts->pchan != GSM_PCHAN_NONE) + if (ts->pchan_from_config != GSM_PCHAN_NONE) vty_out(vty, " phys_chan_config %s%s", - gsm_pchan_name(ts->pchan), VTY_NEWLINE); + gsm_pchan_name(ts->pchan_from_config), VTY_NEWLINE); vty_out(vty, " hopping enabled %u%s", ts->hopping.enabled, VTY_NEWLINE); if (ts->hopping.enabled) { @@ -1100,17 +1105,12 @@ DEFUN(show_trx, static void ts_dump_vty(struct vty *vty, struct gsm_bts_trx_ts *ts) { - vty_out(vty, "BTS %u, TRX %u, Timeslot %u, phys cfg %s, TSC %u", + vty_out(vty, "BTS %u, TRX %u, Timeslot %u, phys cfg %s", ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan), gsm_ts_tsc(ts)); - if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) { - vty_out(vty, " (%s mode)", - ts->flags & TS_F_PDCH_ACTIVE ? "PDCH" : "TCH/F"); - } else if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { - vty_out(vty, " (%s mode)", gsm_pchan_name(ts->dyn.pchan_is)); - } - vty_out(vty, "%s", VTY_NEWLINE); - vty_out(vty, " NM State: "); + gsm_pchan_name(ts->pchan_on_init)); + if (ts->pchan_is != ts->pchan_on_init) + vty_out(vty, " (%s mode)", gsm_pchan_name(ts->pchan_is)); + vty_out(vty, ", TSC %u%s NM State: ", gsm_ts_tsc(ts), VTY_NEWLINE); net_dump_nmstate(vty, &ts->mo.nm_state); if (!is_ipaccess_bts(ts->trx->bts)) vty_out(vty, " E1 Line %u, Timeslot %u, Subslot %u%s", @@ -1237,48 +1237,17 @@ static void meas_rep_dump_vty(struct vty *vty, struct gsm_meas_rep *mr, meas_rep_dump_uni_vty(vty, &mr->ul, prefix, "ul"); } -/* FIXME: move this to libosmogsm */ -static const struct value_string gsm48_cmode_names[] = { - { GSM48_CMODE_SIGN, "signalling" }, - { GSM48_CMODE_SPEECH_V1, "FR or HR" }, - { GSM48_CMODE_SPEECH_EFR, "EFR" }, - { GSM48_CMODE_SPEECH_AMR, "AMR" }, - { GSM48_CMODE_DATA_14k5, "CSD(14k5)" }, - { GSM48_CMODE_DATA_12k0, "CSD(12k0)" }, - { GSM48_CMODE_DATA_6k0, "CSD(6k0)" }, - { GSM48_CMODE_DATA_3k6, "CSD(3k6)" }, - { 0, NULL } -}; /* call vty_out() to print a string like " as TCH/H" for dynamic timeslots. * Don't do anything if the ts is not dynamic. */ static void vty_out_dyn_ts_status(struct vty *vty, struct gsm_bts_trx_ts *ts) { - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - if (ts->dyn.pchan_is == ts->dyn.pchan_want) - vty_out(vty, " as %s", - gsm_pchan_name(ts->dyn.pchan_is)); - else - vty_out(vty, " switching %s -> %s", - gsm_pchan_name(ts->dyn.pchan_is), - gsm_pchan_name(ts->dyn.pchan_want)); - break; - case GSM_PCHAN_TCH_F_PDCH: - if ((ts->flags & TS_F_PDCH_PENDING_MASK) == 0) - vty_out(vty, " as %s", - (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH" - : "TCH/F"); - else - vty_out(vty, " switching %s -> %s", - (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH" - : "TCH/F", - (ts->flags & TS_F_PDCH_ACT_PENDING)? "PDCH" - : "TCH/F"); - break; - default: - /* no dyn ts */ - break; + enum gsm_phys_chan_config target; + if (ts_is_pchan_switching(ts, &target)) { + vty_out(vty, " switching %s -> %s", gsm_pchan_name(ts->pchan_is), + gsm_pchan_name(target)); + } else if (ts->pchan_is != ts->pchan_on_init) { + vty_out(vty, " as %s", gsm_pchan_name(ts->pchan_is)); } } @@ -1290,7 +1259,7 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan) lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, lchan->nr, gsm_lchant_name(lchan->type), VTY_NEWLINE); /* show dyn TS details, if applicable */ - switch (lchan->ts->pchan) { + switch (lchan->ts->pchan_on_init) { case GSM_PCHAN_TCH_F_TCH_H_PDCH: vty_out(vty, " Osmocom Dyn TS:"); vty_out_dyn_ts_status(vty, lchan->ts); @@ -1306,10 +1275,9 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan) break; } vty_out(vty, " Connection: %u, State: %s%s%s%s", - lchan->conn ? 1: 0, - gsm_lchans_name(lchan->state), - lchan->state == LCHAN_S_BROKEN ? " Error reason: " : "", - lchan->state == LCHAN_S_BROKEN ? lchan->broken_reason : "", + lchan->conn ? 1: 0, lchan_state_name(lchan), + lchan->fi && lchan->fi->state == LCHAN_ST_BORKEN ? " Error reason: " : "", + lchan->fi && lchan->fi->state == LCHAN_ST_BORKEN ? lchan->last_error : "", VTY_NEWLINE); vty_out(vty, " BS Power: %u dBm, MS Power: %u dBm%s", lchan->ts->trx->nominal_power - lchan->ts->trx->max_power_red @@ -1317,7 +1285,7 @@ static void lchan_dump_full_vty(struct vty *vty, struct gsm_lchan *lchan) ms_pwr_dbm(lchan->ts->trx->bts->band, lchan->ms_power), VTY_NEWLINE); vty_out(vty, " Channel Mode / Codec: %s%s", - get_value_string(gsm48_cmode_names, lchan->tch_mode), + gsm48_chan_mode_name(lchan->tch_mode), VTY_NEWLINE); if (lchan->conn && lchan->conn->bsub) { vty_out(vty, " Subscriber:%s", VTY_NEWLINE); @@ -1361,12 +1329,12 @@ static void lchan_dump_short_vty(struct vty *vty, struct gsm_lchan *lchan) vty_out(vty, "BTS %u, TRX %u, Timeslot %u %s", lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, - gsm_pchan_name(lchan->ts->pchan)); + gsm_pchan_name(lchan->ts->pchan_on_init)); vty_out_dyn_ts_status(vty, lchan->ts); vty_out(vty, ", Lchan %u, Type %s, State %s - " "L1 MS Power: %u dBm RXL-FULL-dl: %4d dBm RXL-FULL-ul: %4d dBm%s", lchan->nr, - gsm_lchant_name(lchan->type), gsm_lchans_name(lchan->state), + gsm_lchant_name(lchan->type), lchan_state_name(lchan), mr->ms_l1.pwr, rxlev2dbm(mr->dl.full.rx_lev), rxlev2dbm(mr->ul.full.rx_lev), @@ -1380,7 +1348,7 @@ static int dump_lchan_trx_ts(struct gsm_bts_trx_ts *ts, struct vty *vty, int lchan_nr; for (lchan_nr = 0; lchan_nr < TS_MAX_LCHAN; lchan_nr++) { struct gsm_lchan *lchan = &ts->lchan[lchan_nr]; - if ((lchan->type == GSM_LCHAN_NONE) && (lchan->state == LCHAN_S_NONE)) + if ((lchan->type == GSM_LCHAN_NONE) && lchan_state_is(lchan, LCHAN_ST_UNUSED)) continue; dump_cb(vty, lchan); } @@ -1507,7 +1475,7 @@ static void dump_one_subscr_conn(struct vty *vty, const struct gsm_subscriber_co vty_out(vty, "conn ID=%u, MSC=%u, hodec2_fail=%d, mode=%s, mgw_ep=%s%s", conn->sccp.conn_id, conn->sccp.msc->nr, conn->hodec2.failures, get_value_string(gsm48_chan_mode_names, conn->user_plane.chan_mode), - conn->user_plane.mgw_endpoint, VTY_NEWLINE); + mgw_endpoint_name(conn->user_plane.mgw_endpoint), VTY_NEWLINE); if (conn->lcls.global_call_ref_len) { vty_out(vty, " LCLS GCR: %s%s", osmo_hexdump_nospc(conn->lcls.global_call_ref, conn->lcls.global_call_ref_len), @@ -1518,8 +1486,8 @@ static void dump_one_subscr_conn(struct vty *vty, const struct gsm_subscriber_co } if (conn->lchan) lchan_dump_full_vty(vty, conn->lchan); - if (conn->secondary_lchan) - lchan_dump_full_vty(vty, conn->secondary_lchan); + if (conn->assignment.new_lchan) + lchan_dump_full_vty(vty, conn->assignment.new_lchan); } DEFUN(show_subscr_conn, @@ -1548,8 +1516,6 @@ DEFUN(show_subscr_conn, static int trigger_ho_or_as(struct vty *vty, struct gsm_lchan *from_lchan, struct gsm_bts *to_bts) { - int rc; - if (!to_bts || from_lchan->ts->trx->bts == to_bts) { LOGP(DHO, LOGL_NOTICE, "%s Manually triggering Assignment from VTY\n", gsm_lchan_name(from_lchan)); @@ -1557,11 +1523,13 @@ static int trigger_ho_or_as(struct vty *vty, struct gsm_lchan *from_lchan, struc } else LOGP(DHO, LOGL_NOTICE, "%s (ARFCN %u) --> BTS %u Manually triggering Handover from VTY\n", gsm_lchan_name(from_lchan), from_lchan->ts->trx->arfcn, to_bts->nr); - rc = bsc_handover_start(HODEC_NONE, from_lchan, to_bts, from_lchan->type); - if (rc) { - vty_out(vty, "bsc_handover_start() returned %d=%s%s", rc, - strerror(-rc), VTY_NEWLINE); - return CMD_WARNING; + { + struct handover_mo_req req = { + .from_hodec_id = HODEC_USER, + .old_lchan = from_lchan, + .target_nik = *bts_ident_key(to_bts), + }; + handover_request(&req); } return CMD_SUCCESS; } @@ -1650,18 +1618,13 @@ static struct gsm_lchan *find_used_voice_lchan(struct vty *vty) int i; for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { struct gsm_bts_trx_ts *ts = &trx->ts[i]; - int j; - int subslots; + struct gsm_lchan *lchan; - /* skip administratively deactivated timeslots */ - if (!nm_is_running(&ts->mo.nm_state)) + if (ts->fi->state != TS_ST_IN_USE) continue; - subslots = ts_subslots(ts); - for (j = 0; j < subslots; j++) { - struct gsm_lchan *lchan = &ts->lchan[j]; - - if (lchan->state == LCHAN_S_ACTIVE + ts_for_each_lchan(lchan, ts) { + if (lchan_state_is(lchan, LCHAN_ST_ESTABLISHED) && (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H)) { @@ -1693,30 +1656,27 @@ static struct gsm_bts *find_other_bts_with_free_slots(struct vty *vty, struct gs llist_for_each_entry(trx, &bts->trx_list, list) { int i; + /* FIXME: use lchan_select_by_type() instead */ for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { struct gsm_bts_trx_ts *ts = &trx->ts[i]; - int j; - int subslots; + struct gsm_lchan *lchan; /* skip administratively deactivated timeslots */ if (!nm_is_running(&ts->mo.nm_state)) continue; - if (ts->pchan != free_type) + if (ts->pchan_is != free_type) continue; - subslots = ts_subslots(ts); - for (j = 0; j < subslots; j++) { - struct gsm_lchan *lchan = &ts->lchan[j]; - - if (lchan->state == LCHAN_S_NONE) { - vty_out(vty, "Found unused %s slot: %s%s", - gsm_pchan_name(free_type), - gsm_lchan_name(lchan), - VTY_NEWLINE); - lchan_dump_full_vty(vty, lchan); - return bts; - } + ts_for_each_lchan(lchan, ts) { + if (lchan->fi->state != LCHAN_ST_UNUSED) + continue; + vty_out(vty, "Found unused %s slot: %s%s", + gsm_pchan_name(free_type), + gsm_lchan_name(lchan), + VTY_NEWLINE); + lchan_dump_full_vty(vty, lchan); + return bts; } } } @@ -1740,7 +1700,7 @@ DEFUN(handover_any, handover_any_cmd, return CMD_WARNING; to_bts = find_other_bts_with_free_slots(vty, from_lchan->ts->trx->bts, - ts_pchan(from_lchan->ts)); + from_lchan->ts->pchan_is); if (!to_bts) return CMD_WARNING; @@ -1762,6 +1722,61 @@ DEFUN(assignment_any, assignment_any_cmd, return trigger_ho_or_as(vty, from_lchan, NULL); } +#if 0 + +DEFUN(handover_ext_cgi, + handover_ext_cgi_cmd, + "handover external (cgi|lac+ci|ci|lai|lac|all|none) [VAL1] [VAL2] [VAL3] [VAL4]", + MANUAL_HANDOVER_EXTERNAL_STR + "Identify handover target cell by Cell Global Identifier; supply MCC MNC LAC CI arguments\n" + "Identify handover target cell by Location Area Code and Cell Identify\n" + "Identify handover target cell by Cell Identify\n" + "Identify handover target cell by Location Area Identifcation\n" + "Identify handover target cell by Location Area Code\n" + "Identify handover target as all and any available cell\n" + "Do not associate a cell with the Handover Required message\n" + "Value to specify target cell, format depends\n" + "Value to specify target cell, format depends\n" + "Value to specify target cell, format depends\n" + "Value to specify target cell, format depends\n") +{ + return ho_or_as(vty, argv, argc); +} +#endif + +DEFUN(handover_any_to_arfcn_bsic, handover_any_to_arfcn_bsic_cmd, + "handover any to " NEIGHBOR_IDENT_VTY_KEY_PARAMS, + MANUAL_HANDOVER_STR + "Pick any actively used TCH/F or TCH/H lchan to handover to another cell." + " This is likely to fail outside of a lab setup where you are certain that" + " all MS are able to see the target cell.\n" + "'to'\n" + NEIGHBOR_IDENT_VTY_KEY_DOC) +{ + struct gsm_lchan *from_lchan; + + from_lchan = find_used_voice_lchan(vty); + if (!from_lchan) + return CMD_WARNING; + + { + struct handover_mo_req req = { + .from_hodec_id = HODEC_USER, + .old_lchan = from_lchan, + }; + + if (!neighbor_ident_bts_parse_key_params(vty, from_lchan->ts->trx->bts, + argv, &req.target_nik)) { + vty_out(vty, "%% BTS %u does not know about this neighbor%s", + from_lchan->ts->trx->bts->nr, VTY_NEWLINE); + return CMD_WARNING; + } + + handover_request(&req); + } + return CMD_SUCCESS; +} + static void paging_dump_vty(struct vty *vty, struct gsm_paging_request *pag) { vty_out(vty, "Paging on BTS %u%s", pag->bts->nr, VTY_NEWLINE); @@ -3956,7 +3971,7 @@ DEFUN(cfg_ts_pchan, if (pchanc < 0) return CMD_WARNING; - ts->pchan = pchanc; + ts->pchan_from_config = pchanc; return CMD_SUCCESS; } @@ -3975,7 +3990,7 @@ DEFUN_HIDDEN(cfg_ts_pchan_compat, if (pchanc < 0) return CMD_WARNING; - ts->pchan = pchanc; + ts->pchan_from_config = pchanc; return CMD_SUCCESS; } @@ -4323,8 +4338,10 @@ DEFUN(pdch_act, pdch_act_cmd, int activate; ts = vty_get_ts(vty, argv[0], argv[1], argv[2]); - if (!ts) + if (!ts || !ts->fi || ts->fi->state == TS_ST_NOT_INITIALIZED || ts->fi->state == TS_ST_BORKEN) { + vty_out(vty, "%% Timeslot is not usable%s", VTY_NEWLINE); return CMD_WARNING; + } if (!is_ipaccess_bts(ts->trx->bts)) { vty_out(vty, "%% This command only works for ipaccess BTS%s", @@ -4332,9 +4349,10 @@ DEFUN(pdch_act, pdch_act_cmd, return CMD_WARNING; } - if (ts->pchan != GSM_PCHAN_TCH_F_PDCH) { - vty_out(vty, "%% Timeslot %u is not in dynamic TCH_F/PDCH " - "mode%s", ts->nr, VTY_NEWLINE); + if (ts->pchan_on_init != GSM_PCHAN_TCH_F_TCH_H_PDCH + && ts->pchan_on_init != GSM_PCHAN_TCH_F_PDCH) { + vty_out(vty, "%% Timeslot %u is not dynamic TCH/F_TCH/H_PDCH or TCH/F_PDCH%s", + ts->nr, VTY_NEWLINE); return CMD_WARNING; } @@ -4343,30 +4361,24 @@ DEFUN(pdch_act, pdch_act_cmd, else activate = 0; - rsl_ipacc_pdch_activate(ts, activate); + if (activate && ts->fi->state != TS_ST_UNUSED) { + vty_out(vty, "%% Timeslot %u is still in use%s", + ts->nr, VTY_NEWLINE); + return CMD_WARNING; + } else if (!activate && ts->fi->state != TS_ST_PDCH) { + vty_out(vty, "%% Timeslot %u is not in PDCH mode%s", + ts->nr, VTY_NEWLINE); + return CMD_WARNING; + } + + LOG_TS(ts, LOGL_NOTICE, "telnet VTY user asks to %s", activate ? "PDCH ACT" : "PDCH DEACT"); + ts->pdch_act_allowed = activate; + osmo_fsm_inst_state_chg(ts->fi, activate ? TS_ST_WAIT_PDCH_ACT : TS_ST_WAIT_PDCH_DEACT, 4, 0); return CMD_SUCCESS; } -/* determine the logical channel type based on the physical channel type */ -static int lchan_type_by_pchan(enum gsm_phys_chan_config pchan) -{ - switch (pchan) { - case GSM_PCHAN_TCH_F: - return GSM_LCHAN_TCH_F; - case GSM_PCHAN_TCH_H: - return GSM_LCHAN_TCH_H; - case GSM_PCHAN_SDCCH8_SACCH8C: - case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: - case GSM_PCHAN_CCCH_SDCCH4: - case GSM_PCHAN_CCCH_SDCCH4_CBCH: - return GSM_LCHAN_SDCCH; - default: - return -1; - } -} - /* configure the lchan for a single AMR mode (as specified) */ static int lchan_set_single_amr_mode(struct gsm_lchan *lchan, uint8_t amr_mode) { @@ -4423,19 +4435,21 @@ DEFUN(lchan_act, lchan_act_cmd, else activate = 0; - if (ss_nr >= ts_subslots(ts)) { - vty_out(vty, "%% subslot %d >= permitted %d for physical channel %s%s", - ss_nr, ts_subslots(ts), gsm_pchan_name(ts->pchan), VTY_NEWLINE); + /* FIXME: allow dynamic channels with switchover, lchan_activate(lchan, FOR_VTY) */ + if (ss_nr >= pchan_subslots(ts->pchan_is)) { + vty_out(vty, "%% subslot index %d too large for physical channel %s (%u slots)%s", + ss_nr, gsm_pchan_name(ts->pchan_is), pchan_subslots(ts->pchan_is), + VTY_NEWLINE); return CMD_WARNING; } if (activate) { int lchan_t; - if (lchan->state != LCHAN_S_NONE) { + if (lchan->fi->state != LCHAN_ST_UNUSED) { vty_out(vty, "%% Cannot activate: Channel busy!%s", VTY_NEWLINE); return CMD_WARNING; } - lchan_t = lchan_type_by_pchan(ts->pchan); + lchan_t = gsm_lchan_type_by_pchan(ts->pchan_is); if (lchan_t < 0) return CMD_WARNING; /* configure the lchan */ @@ -4456,10 +4470,16 @@ DEFUN(lchan_act, lchan_act_cmd, lchan_set_single_amr_mode(lchan, amr_mode); } vty_out(vty, "%% activating lchan %s%s", gsm_lchan_name(lchan), VTY_NEWLINE); - rsl_chan_activate_lchan(lchan, RSL_ACT_TYPE_INITIAL, 0); - rsl_ipacc_crcx(lchan); + rsl_tx_chan_activ(lchan, RSL_ACT_TYPE_INITIAL, 0); + rsl_tx_ipacc_crcx(lchan); } else { - rsl_direct_rf_release(lchan); + if (!lchan->fi) { + vty_out(vty, "%% Cannot release: Channel not initialized%s", VTY_NEWLINE); + return CMD_WARNING; + } + vty_out(vty, "%% Asking for release of %s in state %s%s", gsm_lchan_name(lchan), + osmo_fsm_inst_state_name(lchan->fi), VTY_NEWLINE); + lchan_release(lchan, false, false, 0); } return CMD_SUCCESS; @@ -4483,15 +4503,18 @@ DEFUN(lchan_mdcx, lchan_mdcx_cmd, lchan = &ts->lchan[ss_nr]; - if (ss_nr >= ts_subslots(ts)) { - vty_out(vty, "%% subslot %d >= permitted %d for physical channel %s%s", - ss_nr, ts_subslots(ts), gsm_pchan_name(ts->pchan), VTY_NEWLINE); + if (ss_nr >= pchan_subslots(ts->pchan_is)) { + vty_out(vty, "%% subslot index %d too large for physical channel %s (%u slots)%s", + ss_nr, gsm_pchan_name(ts->pchan_is), pchan_subslots(ts->pchan_is), + VTY_NEWLINE); return CMD_WARNING; } vty_out(vty, "%% connecting RTP of %s to %s:%u%s", gsm_lchan_name(lchan), inet_ntoa(ia), port, VTY_NEWLINE); - rsl_ipacc_mdcx(lchan, ntohl(ia.s_addr), port, 0); + lchan->abis_ip.connect_ip = ia.s_addr; + lchan->abis_ip.connect_port = port; + rsl_tx_ipacc_mdcx(lchan); return CMD_SUCCESS; } @@ -4773,12 +4796,14 @@ int bsc_vty_init(struct gsm_network *network) install_element_ve(&show_lchan_summary_cmd); install_element_ve(&show_subscr_conn_cmd); - install_element_ve(&handover_any_cmd); - install_element_ve(&assignment_any_cmd); install_element_ve(&show_paging_cmd); install_element_ve(&show_paging_group_cmd); + install_element(ENABLE_NODE, &handover_any_cmd); + install_element(ENABLE_NODE, &assignment_any_cmd); + install_element(ENABLE_NODE, &handover_any_to_arfcn_bsic_cmd); + logging_vty_add_cmds(NULL); osmo_talloc_vty_add_cmds(); diff --git a/src/osmo-bsc/bts_ericsson_rbs2000.c b/src/osmo-bsc/bts_ericsson_rbs2000.c index 9c8b90ee2..c2975f4c6 100644 --- a/src/osmo-bsc/bts_ericsson_rbs2000.c +++ b/src/osmo-bsc/bts_ericsson_rbs2000.c @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -52,7 +53,7 @@ static void bootstrap_om_trx(struct gsm_bts_trx *trx) static int shutdown_om(struct gsm_bts *bts) { - gsm_bts_mark_all_ts_uninitialized(bts); + gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL); /* FIXME */ return 0; diff --git a/src/osmo-bsc/bts_ipaccess_nanobts.c b/src/osmo-bsc/bts_ipaccess_nanobts.c index 0e23955c8..412bba877 100644 --- a/src/osmo-bsc/bts_ipaccess_nanobts.c +++ b/src/osmo-bsc/bts_ipaccess_nanobts.c @@ -42,6 +42,7 @@ #include #include #include +#include static int bts_model_nanobts_start(struct gsm_network *net); static void bts_model_nanobts_e1line_bind_ops(struct e1inp_line *line); @@ -169,7 +170,7 @@ static int nm_statechg_event(int evt, struct nm_statechg_signal_data *nsd) if (new_state->operational == NM_OPSTATE_DISABLED && new_state->availability == NM_AVSTATE_DEPENDENCY) { enum abis_nm_chan_comb ccomb = - abis_nm_chcomb4pchan(ts->pchan); + abis_nm_chcomb4pchan(ts->pchan_from_config); if (abis_nm_set_channel_attr(ts, ccomb) == -EINVAL) { ipaccess_drop_oml(trx->bts); return -1; @@ -305,8 +306,12 @@ static void nm_rx_opstart_ack_chan(struct msgb *oml_msg) if (!ts) /* error already logged in abis_nm_get_ts() */ return; + if (!ts->fi) { + LOG_TS(ts, LOGL_ERROR, "Channel OPSTART ACK for uninitialized TS"); + return; + } - gsm_ts_check_init(ts); + osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_READY, NULL); } static void nm_rx_opstart_ack(struct msgb *oml_msg) @@ -413,7 +418,7 @@ void ipaccess_drop_oml(struct gsm_bts *bts) llist_for_each_entry(trx, &bts->trx_list, list) ipaccess_drop_rsl(trx); - gsm_bts_mark_all_ts_uninitialized(bts); + gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL); bts->ip_access.flags = 0; diff --git a/src/osmo-bsc/bts_nokia_site.c b/src/osmo-bsc/bts_nokia_site.c index 4a24c3931..052741ed1 100644 --- a/src/osmo-bsc/bts_nokia_site.c +++ b/src/osmo-bsc/bts_nokia_site.c @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -55,13 +56,13 @@ static void bootstrap_om_bts(struct gsm_bts *bts) { LOGP(DNM, LOGL_NOTICE, "bootstrapping OML for BTS %u\n", bts->nr); - gsm_bts_mark_all_ts_uninitialized(bts); - if (!bts->nokia.skip_reset) { if (!bts->nokia.did_reset) abis_nm_reset(bts, 1); } else bts->nokia.did_reset = 1; + + gsm_bts_all_ts_dispatch(bts, TS_EV_OML_READY, NULL); } static void bootstrap_om_trx(struct gsm_bts_trx *trx) @@ -69,7 +70,7 @@ static void bootstrap_om_trx(struct gsm_bts_trx *trx) LOGP(DNM, LOGL_NOTICE, "bootstrapping OML for TRX %u/%u\n", trx->bts->nr, trx->nr); - gsm_trx_mark_all_ts_uninitialized(trx); + gsm_trx_all_ts_dispatch(trx, TS_EV_OML_READY, NULL); } static int shutdown_om(struct gsm_bts *bts) @@ -779,24 +780,32 @@ static int make_fu_config(struct gsm_bts_trx *trx, uint8_t id, 0xFF = spare TS */ - if (ts->pchan == GSM_PCHAN_NONE) + switch (ts->pchan_from_config) { + case GSM_PCHAN_NONE: chan_config = 0xFF; - else if (ts->pchan == GSM_PCHAN_CCCH) + break; + case GSM_PCHAN_CCCH: chan_config = 0; - else if (ts->pchan == GSM_PCHAN_CCCH_SDCCH4) + break; + case GSM_PCHAN_CCCH_SDCCH4: chan_config = 1; - else if (ts->pchan == GSM_PCHAN_TCH_F) + break; + case GSM_PCHAN_TCH_F: chan_config = 6; /* 9 should work too */ - else if (ts->pchan == GSM_PCHAN_TCH_H) + break; + case GSM_PCHAN_TCH_H: chan_config = 9; - else if (ts->pchan == GSM_PCHAN_SDCCH8_SACCH8C) + break; + case GSM_PCHAN_SDCCH8_SACCH8C: chan_config = 4; - else if (ts->pchan == GSM_PCHAN_PDCH) + break; + case GSM_PCHAN_PDCH: chan_config = 11; - else { + break; + default: fprintf(stderr, - "unsupported channel config %d for timeslot %d\n", - ts->pchan, i); + "unsupported channel config %s for timeslot %d\n", + gsm_pchan_name(ts->pchan_from_config), i); return 0; } diff --git a/src/osmo-bsc/bts_siemens_bs11.c b/src/osmo-bsc/bts_siemens_bs11.c index 1da189fb9..78a87e5c6 100644 --- a/src/osmo-bsc/bts_siemens_bs11.c +++ b/src/osmo-bsc/bts_siemens_bs11.c @@ -28,6 +28,7 @@ #include #include #include +#include static int bts_model_bs11_start(struct gsm_network *net); @@ -393,7 +394,7 @@ static void patch_nm_tables(struct gsm_bts *bts) static void nm_reconfig_ts(struct gsm_bts_trx_ts *ts) { - enum abis_nm_chan_comb ccomb = abis_nm_chcomb4pchan(ts->pchan); + enum abis_nm_chan_comb ccomb = abis_nm_chcomb4pchan(ts->pchan_from_config); struct gsm_e1_subslot *e1l = &ts->e1_link; abis_nm_set_channel_attr(ts, ccomb); @@ -537,7 +538,7 @@ static int shutdown_om(struct gsm_bts *bts) /* Reset BTS Site manager resource */ abis_nm_bs11_reset_resource(bts); - gsm_bts_mark_all_ts_uninitialized(bts); + gsm_bts_all_ts_dispatch(bts, TS_EV_OML_DOWN, NULL); return 0; } diff --git a/src/osmo-bsc/chan_alloc.c b/src/osmo-bsc/chan_alloc.c index a24fbea94..de8f63183 100644 --- a/src/osmo-bsc/chan_alloc.c +++ b/src/osmo-bsc/chan_alloc.c @@ -31,573 +31,12 @@ #include #include #include +#include +#include #include #include -bool ts_is_usable(const struct gsm_bts_trx_ts *ts) -{ - if (!trx_is_usable(ts->trx)) { - LOGP(DRLL, LOGL_DEBUG, "%s not usable\n", gsm_trx_name(ts->trx)); - return false; - } - - /* If a TCH/F_PDCH TS is busy changing, it is already taken or not - * yet available. */ - if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) { - if (ts->flags & TS_F_PDCH_PENDING_MASK) { - LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n", - gsm_ts_and_pchan_name(ts)); - return false; - } - } - - /* If a dynamic channel is busy changing, it is already taken or not - * yet available. */ - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { - if (ts->dyn.pchan_is != ts->dyn.pchan_want) { - LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n", - gsm_ts_and_pchan_name(ts)); - return false; - } - } - - return true; -} - -static int trx_count_free_ts(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan) -{ - struct gsm_bts_trx_ts *ts; - int j, ss; - int count = 0; - - if (!trx_is_usable(trx)) - return 0; - - for (j = 0; j < ARRAY_SIZE(trx->ts); j++) { - enum gsm_phys_chan_config ts_pchan_is; - ts = &trx->ts[j]; - if (!ts_is_usable(ts)) - continue; - - ts_pchan_is = ts_pchan(ts); - - if (ts_pchan_is == GSM_PCHAN_PDCH) { - /* Dynamic timeslots in PDCH mode will become TCH if needed. */ - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_PDCH: - if (pchan == GSM_PCHAN_TCH_F) - count++; - continue; - - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - if (pchan == GSM_PCHAN_TCH_F) - count++; - else if (pchan == GSM_PCHAN_TCH_H) - count += 2; - continue; - - default: - /* Not dynamic, not applicable. */ - continue; - } - } - - if (ts_pchan_is != pchan) - continue; - /* check if all sub-slots are allocated yet */ - for (ss = 0; ss < ts_subslots(ts); ss++) { - struct gsm_lchan *lc = &ts->lchan[ss]; - if (lc->type == GSM_LCHAN_NONE && - lc->state == LCHAN_S_NONE) - count++; - } - } - - return count; -} - -/* Count number of free TS of given pchan type */ -int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) -{ - struct gsm_bts_trx *trx; - int count = 0; - - llist_for_each_entry(trx, &bts->trx_list, list) - count += trx_count_free_ts(trx, pchan); - - return count; -} - -static bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts, - enum gsm_phys_chan_config as_pchan) -{ - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_PDCH: - if (ts->flags & TS_F_PDCH_PENDING_MASK) { - /* currently being switched over. Not usable. */ - return false; - } - switch (as_pchan) { - case GSM_PCHAN_TCH_F: - case GSM_PCHAN_PDCH: - /* continue to check below. */ - break; - default: - return false; - } - break; - - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - if (ts->dyn.pchan_is != ts->dyn.pchan_want) { - /* currently being switched over. Not usable. */ - return false; - } - switch (as_pchan) { - case GSM_PCHAN_TCH_F: - case GSM_PCHAN_TCH_H: - case GSM_PCHAN_PDCH: - /* continue to check below. */ - break; - default: - return false; - } - break; - - default: - /* static timeslots never switch. */ - return ts->pchan == as_pchan; - } - - /* Dynamic timeslots -- Checks depending on the current actual pchan mode: */ - switch (ts_pchan(ts)) { - case GSM_PCHAN_NONE: - /* Not initialized, possibly because GPRS was disabled. We may switch. */ - return true; - - case GSM_PCHAN_PDCH: - /* This slot is in PDCH mode and available to switch pchan mode. But check for - * error states: */ - if (ts->lchan->state != LCHAN_S_NONE && ts->lchan->state != LCHAN_S_ACTIVE) - return false; - return true; - - case GSM_PCHAN_TCH_F: - case GSM_PCHAN_TCH_H: - /* No need to switch at all? */ - if (ts_pchan(ts) == as_pchan) - return true; - - /* If any lchan is in use, we can't change the pchan kind */ - { - int ss; - int subslots = ts_subslots(ts); - for (ss = 0; ss < subslots; ss++) { - struct gsm_lchan *lc = &ts->lchan[ss]; - if (lc->type != GSM_LCHAN_NONE || lc->state != LCHAN_S_NONE) - return false; - } - } - return true; - - default: - /* Not implemented. */ - return false; - } -} - -static struct gsm_lchan * -_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan, - enum gsm_phys_chan_config as_pchan) -{ - struct gsm_bts_trx_ts *ts; - int j, start, stop, dir, ss; - int check_subslots; - -#define LOGPLCHANALLOC(fmt, args...) \ - LOGP(DRLL, LOGL_DEBUG, "looking for lchan %s as %s: " fmt, \ - gsm_pchan_name(pchan), gsm_pchan_name(as_pchan), ## args) - - if (!trx_is_usable(trx)) { - LOGPLCHANALLOC("%s trx not usable\n", gsm_trx_name(trx)); - return NULL; - } - - if (trx->bts->chan_alloc_reverse) { - /* check TS 7..0 */ - start = 7; - stop = -1; - dir = -1; - } else { - /* check TS 0..7 */ - start = 0; - stop = 8; - dir = 1; - } - - for (j = start; j != stop; j += dir) { - ts = &trx->ts[j]; - if (!ts_is_usable(ts)) - continue; - /* The caller first selects what kind of TS to search in, e.g. looking for exact - * GSM_PCHAN_TCH_F, or maybe among dynamic GSM_PCHAN_TCH_F_TCH_H_PDCH... */ - if (ts->pchan != pchan) { - LOGPLCHANALLOC("%s is != %s\n", gsm_ts_and_pchan_name(ts), - gsm_pchan_name(pchan)); - continue; - } - /* Next, is this timeslot in or can it be switched to the pchan we want to use it for? */ - if (!ts_usable_as_pchan(ts, as_pchan)) { - LOGPLCHANALLOC("%s is not usable as %s\n", gsm_ts_and_pchan_name(ts), - gsm_pchan_name(as_pchan)); - continue; - } - /* If we need to switch it, after above check we are also allowed to switch it, and we - * will always use the first lchan after the switch. Return that lchan and rely on the - * caller to perform the pchan switchover. */ - if (ts_pchan(ts) != as_pchan) { - LOGPLCHANALLOC("%s is a match, will switch to %s\n", gsm_ts_and_pchan_name(ts), - gsm_pchan_name(as_pchan)); - return ts->lchan; - } - - /* TS is in desired pchan mode. Go ahead and check for an available lchan. */ - check_subslots = ts_subslots(ts); - for (ss = 0; ss < check_subslots; ss++) { - struct gsm_lchan *lc = &ts->lchan[ss]; - if (lc->type == GSM_LCHAN_NONE && - lc->state == LCHAN_S_NONE) { - LOGPLCHANALLOC("%s ss=%d is available\n", gsm_ts_and_pchan_name(ts), - lc->nr); - return lc; - } - LOGPLCHANALLOC("%s ss=%d in type=%s,state=%s not suitable\n", - gsm_ts_and_pchan_name(ts), lc->nr, gsm_lchant_name(lc->type), - gsm_lchans_name(lc->state)); - } - } - - return NULL; -#undef LOGPLCHANALLOC -} - -static struct gsm_lchan * -_lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan, - enum gsm_phys_chan_config dyn_as_pchan) -{ - struct gsm_bts_trx *trx; - struct gsm_lchan *lc; - - if (bts->chan_alloc_reverse) { - llist_for_each_entry_reverse(trx, &bts->trx_list, list) { - lc = _lc_find_trx(trx, pchan, dyn_as_pchan); - if (lc) - return lc; - } - } else { - llist_for_each_entry(trx, &bts->trx_list, list) { - lc = _lc_find_trx(trx, pchan, dyn_as_pchan); - if (lc) - return lc; - } - } - - return NULL; -} - -static struct gsm_lchan * -_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) -{ - return _lc_dyn_find_bts(bts, pchan, pchan); -} - -/* Allocate a logical channel. - * - * Dynamic channel types: we always prefer a dedicated TS, and only pick + - * switch a dynamic TS if no pure TS of the requested PCHAN is available. - * - * TCH_F/PDCH: if we pick a PDCH ACT style dynamic TS as TCH/F channel, PDCH - * will be disabled in rsl_chan_activate_lchan(); there is no need to check - * whether PDCH mode is currently active, here. - */ -struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type, - int allow_bigger) -{ - struct gsm_lchan *lchan = NULL; - enum gsm_phys_chan_config first, first_cbch, second, second_cbch; - - LOGP(DRLL, LOGL_DEBUG, "(bts=%d) lchan_alloc(%s)\n", bts->nr, gsm_lchant_name(type)); - - switch (type) { - case GSM_LCHAN_SDCCH: - if (bts->chan_alloc_reverse) { - first = GSM_PCHAN_SDCCH8_SACCH8C; - first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; - second = GSM_PCHAN_CCCH_SDCCH4; - second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; - } else { - first = GSM_PCHAN_CCCH_SDCCH4; - first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; - second = GSM_PCHAN_SDCCH8_SACCH8C; - second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; - } - - lchan = _lc_find_bts(bts, first); - if (lchan == NULL) - lchan = _lc_find_bts(bts, first_cbch); - if (lchan == NULL) - lchan = _lc_find_bts(bts, second); - if (lchan == NULL) - lchan = _lc_find_bts(bts, second_cbch); - - /* allow to assign bigger channels */ - if (allow_bigger) { - if (lchan == NULL) { - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); - if (lchan) - type = GSM_LCHAN_TCH_H; - } - - if (lchan == NULL) { - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); - if (lchan) - type = GSM_LCHAN_TCH_F; - } - - /* try dynamic TCH/F_PDCH */ - if (lchan == NULL) { - lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, - GSM_PCHAN_TCH_F); - /* TCH/F_PDCH will be used as TCH/F */ - if (lchan) - type = GSM_LCHAN_TCH_F; - } - - /* try fully dynamic TCH/F_TCH/H_PDCH */ - if (lchan == NULL) { - lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_TCH_H_PDCH, - GSM_PCHAN_TCH_H); - if (lchan) - type = GSM_LCHAN_TCH_H; - } - /* - * No need to check fully dynamic channels for TCH/F: - * if no TCH/H was available, neither will be TCH/F. - */ - } - break; - case GSM_LCHAN_TCH_F: - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); - /* If we don't have TCH/F available, fall-back to TCH/H */ - if (!lchan) { - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); - if (lchan) - type = GSM_LCHAN_TCH_H; - } - /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */ - if (!lchan) { - lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, - GSM_PCHAN_TCH_F); - /* TCH/F_PDCH used as TCH/F -- here, type is already - * set to GSM_LCHAN_TCH_F, but for clarity's sake... */ - if (lchan) - type = GSM_LCHAN_TCH_F; - } - - /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */ - if (!lchan && bts->network->dyn_ts_allow_tch_f) { - lchan = _lc_dyn_find_bts(bts, - GSM_PCHAN_TCH_F_TCH_H_PDCH, - GSM_PCHAN_TCH_F); - if (lchan) - type = GSM_LCHAN_TCH_F; - } - /* ...and as TCH/H. */ - if (!lchan) { - lchan = _lc_dyn_find_bts(bts, - GSM_PCHAN_TCH_F_TCH_H_PDCH, - GSM_PCHAN_TCH_H); - if (lchan) - type = GSM_LCHAN_TCH_H; - } - break; - case GSM_LCHAN_TCH_H: - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); - /* If we don't have TCH/H available, fall-back to TCH/F */ - if (!lchan) { - lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); - if (lchan) - type = GSM_LCHAN_TCH_F; - } - /* No dedicated TCH/x available -- try fully dynamic - * TCH/F_TCH/H_PDCH */ - if (!lchan) { - lchan = _lc_dyn_find_bts(bts, - GSM_PCHAN_TCH_F_TCH_H_PDCH, - GSM_PCHAN_TCH_H); - if (lchan) - type = GSM_LCHAN_TCH_H; - } - /* - * No need to check TCH/F_TCH/H_PDCH channels for TCH/F: - * if no TCH/H was available, neither will be TCH/F. - */ - /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */ - if (!lchan) { - lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, - GSM_PCHAN_TCH_F); - if (lchan) - type = GSM_LCHAN_TCH_F; - } - break; - default: - LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type); - } - - if (lchan) { - lchan->type = type; - - LOGP(DRLL, LOGL_INFO, "%s Allocating lchan=%u as %s\n", - gsm_ts_and_pchan_name(lchan->ts), - lchan->nr, gsm_lchant_name(lchan->type)); - - /* reset measurement report counter and index */ - lchan->meas_rep_count = 0; - lchan->meas_rep_idx = 0; - lchan->meas_rep_last_seen_nr = 255; - - /* clear sapis */ - memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis)); - - /* clear multi rate config */ - memset(&lchan->mr_ms_lv, 0, sizeof(lchan->mr_ms_lv)); - memset(&lchan->mr_bts_lv, 0, sizeof(lchan->mr_bts_lv)); - lchan->broken_reason = ""; - } else { - struct challoc_signal_data sig; - - LOGP(DRLL, LOGL_ERROR, "(bts=%d) Failed to allocate %s channel\n", - bts->nr, gsm_lchant_name(type)); - - sig.bts = bts; - sig.type = type; - osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig); - } - - return lchan; -} - -/* Free a logical channel */ -void lchan_free(struct gsm_lchan *lchan) -{ - struct challoc_signal_data sig; - int i; - - sig.type = lchan->type; - lchan->type = GSM_LCHAN_NONE; - - - if (lchan->conn - && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) { - struct lchan_signal_data sig; - - /* We might kill an active channel... */ - sig.lchan = lchan; - sig.mr = NULL; - osmo_signal_dispatch(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, &sig); - } - - /* stop the timer */ - osmo_timer_del(&lchan->T3101); - - /* clear cached measuement reports */ - lchan->meas_rep_idx = 0; - for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) { - lchan->meas_rep[i].flags = 0; - lchan->meas_rep[i].nr = 0; - } - for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++) - lchan->neigh_meas[i].arfcn = 0; - - if (lchan->rqd_ref) { - talloc_free(lchan->rqd_ref); - lchan->rqd_ref = NULL; - lchan->rqd_ta = 0; - } - - sig.lchan = lchan; - sig.bts = lchan->ts->trx->bts; - osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_FREED, &sig); - - if (lchan->conn - && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) { - LOGP(DRLL, LOGL_ERROR, "the subscriber connection should be gone.\n"); - lchan->conn = NULL; - } - - /* FIXME: ts_free() the timeslot, if we're the last logical - * channel using it */ -} - -/* - * There was an error with the TRX and we need to forget - * any state so that a lchan can be allocated again after - * the trx is fully usable. - * - * This should be called after lchan_free to force a channel - * be available for allocation again. This means that this - * method will stop the "delay after error"-timer and set the - * state to LCHAN_S_NONE. - */ -void lchan_reset(struct gsm_lchan *lchan) -{ - osmo_timer_del(&lchan->T3101); - osmo_timer_del(&lchan->T3109); - osmo_timer_del(&lchan->T3111); - osmo_timer_del(&lchan->error_timer); - - lchan->type = GSM_LCHAN_NONE; - rsl_lchan_set_state(lchan, LCHAN_S_NONE); -} - -/* Drive the release process of the lchan */ -static void _lchan_handle_release(struct gsm_lchan *lchan, - int sacch_deact, int mode) -{ - /* Release all SAPIs on the local end and continue */ - rsl_release_sapis_from(lchan, 1, RSL_REL_LOCAL_END); - - /* - * Shall we send a RR Release, start T3109 and wait for the - * release indication from the BTS or just take it down (e.g. - * on assignment requests) - */ - if (sacch_deact) { - gsm48_send_rr_release(lchan); - - /* Deactivate the SACCH on the BTS side */ - rsl_deact_sacch(lchan); - rsl_start_t3109(lchan); - } else if (lchan->sapis[0] == LCHAN_SAPI_UNUSED) { - rsl_direct_rf_release(lchan); - } else { - rsl_release_request(lchan, 0, mode); - } -} - -/* Consider releasing the channel now */ -int lchan_release(struct gsm_lchan *lchan, int sacch_deact, enum rsl_rel_mode mode) -{ - DEBUGP(DRLL, "%s starting release sequence\n", gsm_lchan_name(lchan)); - rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ); - - lchan->conn = NULL; - _lchan_handle_release(lchan, sacch_deact, mode); - return 1; -} - void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) { struct gsm_bts_trx *trx; @@ -612,22 +51,18 @@ void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { struct gsm_bts_trx_ts *ts = &trx->ts[i]; - struct load_counter *pl = &cl->pchan[ts->pchan]; - int j; - int subslots; + struct load_counter *pl = &cl->pchan[ts->pchan_on_init]; + struct gsm_lchan *lchan; /* skip administratively deactivated timeslots */ if (!nm_is_running(&ts->mo.nm_state)) continue; - subslots = ts_subslots(ts); - for (j = 0; j < subslots; j++) { - struct gsm_lchan *lchan = &ts->lchan[j]; - + ts_for_each_lchan(lchan, ts) { pl->total++; - switch (lchan->state) { - case LCHAN_S_NONE: + switch (lchan->fi->state) { + case LCHAN_ST_UNUSED: break; default: pl->used++; diff --git a/src/osmo-bsc/gsm_04_08_utils.c b/src/osmo-bsc/gsm_04_08_utils.c index c52239f28..18ab95635 100644 --- a/src/osmo-bsc/gsm_04_08_utils.c +++ b/src/osmo-bsc/gsm_04_08_utils.c @@ -341,41 +341,6 @@ int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg) return 0; } -/* 9.2.5 CM service accept */ -int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn) -{ - struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACK"); - struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); - - msg->lchan = conn->lchan; - - gh->proto_discr = GSM48_PDISC_MM; - gh->msg_type = GSM48_MT_MM_CM_SERV_ACC; - - DEBUGP(DMM, "-> CM SERVICE ACK\n"); - - gscon_submit_rsl_dtap(conn, msg, 0, 0); - return 0; -} - -/* 9.2.6 CM service reject */ -int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn, - enum gsm48_reject_value value) -{ - struct msgb *msg; - - msg = gsm48_create_mm_serv_rej(value); - if (!msg) { - LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n"); - return -1; - } - - DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value); - - gscon_submit_rsl_dtap(conn, msg, 0, 0); - return 0; -} - struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value) { struct msgb *msg; diff --git a/src/osmo-bsc/gsm_data.c b/src/osmo-bsc/gsm_data.c index 0ff17f2c6..fee54b78b 100644 --- a/src/osmo-bsc/gsm_data.c +++ b/src/osmo-bsc/gsm_data.c @@ -25,9 +25,10 @@ #include #include #include +#include #include -#include +#include #include #include #include @@ -40,6 +41,9 @@ #include #include #include +#include +#include +#include void *tall_bsc_ctx = NULL; @@ -471,7 +475,7 @@ const struct value_string gsm_chreq_descs[] = { { 0, NULL } }; -const struct value_string gsm_pchant_names[13] = { +const struct value_string gsm_pchant_names[] = { { GSM_PCHAN_NONE, "NONE" }, { GSM_PCHAN_CCCH, "CCCH" }, { GSM_PCHAN_CCCH_SDCCH4,"CCCH+SDCCH4" }, @@ -487,6 +491,22 @@ const struct value_string gsm_pchant_names[13] = { { 0, NULL } }; +const struct value_string gsm_pchan_ids[] = { + { GSM_PCHAN_NONE, "NONE" }, + { GSM_PCHAN_CCCH, "CCCH" }, + { GSM_PCHAN_CCCH_SDCCH4,"CCCH_SDCCH4" }, + { GSM_PCHAN_TCH_F, "TCH_F" }, + { GSM_PCHAN_TCH_H, "TCH_H" }, + { GSM_PCHAN_SDCCH8_SACCH8C, "SDCCH8" }, + { GSM_PCHAN_PDCH, "PDCH" }, + { GSM_PCHAN_TCH_F_PDCH, "TCH_F_PDCH" }, + { GSM_PCHAN_UNKNOWN, "UNKNOWN" }, + { GSM_PCHAN_CCCH_SDCCH4_CBCH, "CCCH_SDCCH4_CBCH" }, + { GSM_PCHAN_SDCCH8_SACCH8C_CBCH, "SDCCH8_CBCH" }, + { GSM_PCHAN_TCH_F_TCH_H_PDCH, "TCH_F_TCH_H_PDCH" }, + { 0, NULL } +}; + const struct value_string gsm_pchant_descs[13] = { { GSM_PCHAN_NONE, "Physical Channel not configured" }, { GSM_PCHAN_CCCH, "FCCH + SCH + BCCH + CCCH (Comb. IV)" }, @@ -520,22 +540,6 @@ const char *gsm_lchant_name(enum gsm_chan_t c) return get_value_string(gsm_chan_t_names, c); } -static const struct value_string lchan_s_names[] = { - { LCHAN_S_NONE, "NONE" }, - { LCHAN_S_ACT_REQ, "ACTIVATION REQUESTED" }, - { LCHAN_S_ACTIVE, "ACTIVE" }, - { LCHAN_S_INACTIVE, "INACTIVE" }, - { LCHAN_S_REL_REQ, "RELEASE REQUESTED" }, - { LCHAN_S_REL_ERR, "RELEASE DUE ERROR" }, - { LCHAN_S_BROKEN, "BROKEN UNUSABLE" }, - { 0, NULL } -}; - -const char *gsm_lchans_name(enum gsm_lchan_state s) -{ - return get_value_string(lchan_s_names, s); -} - static const struct value_string chreq_names[] = { { GSM_CHREQ_REASON_EMERG, "EMERGENCY" }, { GSM_CHREQ_REASON_PAG, "PAGING" }, @@ -550,7 +554,7 @@ const char *gsm_chreq_name(enum gsm_chreq_reason_t c) return get_value_string(chreq_names, c); } -struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num) +struct gsm_bts *gsm_bts_num(const struct gsm_network *net, int num) { struct gsm_bts *bts; @@ -565,61 +569,63 @@ struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num) return NULL; } -bool gsm_bts_matches_cell_id(struct gsm_bts *bts, const struct gsm0808_cell_id *ci) +bool gsm_bts_matches_lai(const struct gsm_bts *bts, + const struct osmo_location_area_id *lai) { - if (!bts || !ci) + return osmo_plmn_cmp(&lai->plmn, &bts->network->plmn) == 0 + && lai->lac == bts->location_area_code; +} + +bool gsm_bts_matches_cell_id(const struct gsm_bts *bts, const struct gsm0808_cell_id *cell_id) +{ + const union gsm0808_cell_id_u *id = &cell_id->id; + if (!bts || !cell_id) return false; - switch (ci->id_discr) { + + switch (cell_id->id_discr) { case CELL_IDENT_WHOLE_GLOBAL: - if (osmo_plmn_cmp(&bts->network->plmn, &ci->id.global.lai.plmn)) - return false; - if (bts->location_area_code != ci->id.global.lai.lac) - return false; - if (bts->cell_identity != ci->id.global.cell_identity) - return false; - return true; + return gsm_bts_matches_lai(bts, &id->global.lai) + && id->global.cell_identity == bts->cell_identity; case CELL_IDENT_LAC_AND_CI: - if (bts->location_area_code != ci->id.lac_and_ci.lac) - return false; - if (bts->cell_identity != ci->id.lac_and_ci.ci) - return false; - return true; + return id->lac_and_ci.lac == bts->location_area_code + && id->lac_and_ci.ci == bts->cell_identity; case CELL_IDENT_CI: - if (bts->cell_identity != ci->id.ci) - return false; - return true; + return id->ci == bts->cell_identity; case CELL_IDENT_NO_CELL: return false; case CELL_IDENT_LAI_AND_LAC: - if (osmo_plmn_cmp(&bts->network->plmn, &ci->id.lai_and_lac.plmn)) - return false; - if (bts->location_area_code != ci->id.lai_and_lac.lac) - return false; - return true; + return gsm_bts_matches_lai(bts, &id->lai_and_lac); case CELL_IDENT_LAC: - if (bts->location_area_code != ci->id.lac) - return false; - return true; + return id->lac == bts->location_area_code; case CELL_IDENT_BSS: return true; case CELL_IDENT_UTRAN_PLMN_LAC_RNC: case CELL_IDENT_UTRAN_RNC: case CELL_IDENT_UTRAN_LAC_RNC: - /* Not implemented */ - default: return false; + default: + OSMO_ASSERT(false); } } -struct gsm_bts *gsm_bts_by_cell_id(struct gsm_network *net, const struct gsm0808_cell_id *ci) +/* From a list of local BTSes that match the cell_id, return the Nth one, or NULL if there is no such + * match. */ +struct gsm_bts *gsm_bts_by_cell_id(const struct gsm_network *net, + const struct gsm0808_cell_id *cell_id, + int match_idx) { struct gsm_bts *bts; - + int i = 0; llist_for_each_entry(bts, &net->bts_list, list) { - if (gsm_bts_matches_cell_id(bts, ci)) - return bts; + if (!gsm_bts_matches_cell_id(bts, cell_id)) + continue; + if (i < match_idx) { + /* this is only the i'th match, we're looking for a later one... */ + i++; + continue; + } + return bts; } - return NULL; } @@ -697,13 +703,14 @@ struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts) struct gsm_bts_trx_ts *ts = &trx->ts[k]; int l; + ts->trx = trx; ts->nr = k; - ts->pchan = GSM_PCHAN_NONE; - ts->dyn.pchan_is = GSM_PCHAN_NONE; - ts->dyn.pchan_want = GSM_PCHAN_NONE; + ts->pchan_from_config = ts->pchan_on_init = ts->pchan_is = GSM_PCHAN_NONE; ts->tsc = -1; + ts_fsm_alloc(ts); + gsm_mo_init(&ts->mo, bts, NM_OC_CHANNEL, bts->nr, trx->nr, ts->nr); @@ -818,7 +825,7 @@ struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, uint8_t bts_num) talloc_free(bts); return NULL; } - bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4; + bts->c0->ts[0].pchan_from_config = GSM_PCHAN_CCCH_SDCCH4; /* TODO: really?? */ bts->rach_b_thresh = -1; bts->rach_ldavg_slots = -1; @@ -938,49 +945,30 @@ char *gsm_ts_name(const struct gsm_bts_trx_ts *ts) /*! Log timeslot number with full pchan information */ char *gsm_ts_and_pchan_name(const struct gsm_bts_trx_ts *ts) { - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - if (ts->dyn.pchan_is == ts->dyn.pchan_want) - snprintf(ts2str, sizeof(ts2str), - "(bts=%d,trx=%d,ts=%d,pchan=%s as %s)", - ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan), - gsm_pchan_name(ts->dyn.pchan_is)); - else - snprintf(ts2str, sizeof(ts2str), - "(bts=%d,trx=%d,ts=%d,pchan=%s" - " switching %s -> %s)", - ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan), - gsm_pchan_name(ts->dyn.pchan_is), - gsm_pchan_name(ts->dyn.pchan_want)); - break; - case GSM_PCHAN_TCH_F_PDCH: - if ((ts->flags & TS_F_PDCH_PENDING_MASK) == 0) - snprintf(ts2str, sizeof(ts2str), - "(bts=%d,trx=%d,ts=%d,pchan=%s as %s)", - ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan), - (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH" - : "TCH/F"); - else - snprintf(ts2str, sizeof(ts2str), - "(bts=%d,trx=%d,ts=%d,pchan=%s" - " switching %s -> %s)", - ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan), - (ts->flags & TS_F_PDCH_ACTIVE)? "PDCH" - : "TCH/F", - (ts->flags & TS_F_PDCH_ACT_PENDING)? "PDCH" - : "TCH/F"); - break; - default: - snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d,pchan=%s)", + if (!ts->fi) + snprintf(ts2str, sizeof(ts2str), + "(bts=%d,trx=%d,ts=%d,pchan_from_config=%s, not allocated)", ts->trx->bts->nr, ts->trx->nr, ts->nr, - gsm_pchan_name(ts->pchan)); - break; - } - + gsm_pchan_name(ts->pchan_from_config)); + else if (ts->fi->state == TS_ST_NOT_INITIALIZED) + snprintf(ts2str, sizeof(ts2str), + "(bts=%d,trx=%d,ts=%d,pchan_from_config=%s,state=%s)", + ts->trx->bts->nr, ts->trx->nr, ts->nr, + gsm_pchan_name(ts->pchan_from_config), + osmo_fsm_inst_state_name(ts->fi)); + else if (ts->pchan_is == ts->pchan_on_init) + snprintf(ts2str, sizeof(ts2str), + "(bts=%d,trx=%d,ts=%d,pchan=%s,state=%s)", + ts->trx->bts->nr, ts->trx->nr, ts->nr, + gsm_pchan_name(ts->pchan_is), + osmo_fsm_inst_state_name(ts->fi)); + else + snprintf(ts2str, sizeof(ts2str), + "(bts=%d,trx=%d,ts=%d,pchan_on_init=%s,pchan=%s,state=%s)", + ts->trx->bts->nr, ts->trx->nr, ts->nr, + gsm_pchan_name(ts->pchan_on_init), + gsm_pchan_name(ts->pchan_is), + osmo_fsm_inst_state_name(ts->fi)); return ts2str; } @@ -1198,20 +1186,9 @@ uint8_t gsm_pchan2chan_nr(enum gsm_phys_chan_config pchan, uint8_t gsm_lchan2chan_nr(const struct gsm_lchan *lchan) { - enum gsm_phys_chan_config pchan = lchan->ts->pchan; - if (pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) - return gsm_lchan_as_pchan2chan_nr(lchan, - lchan->ts->dyn.pchan_is); - return gsm_pchan2chan_nr(lchan->ts->pchan, lchan->ts->nr, lchan->nr); -} - -uint8_t gsm_lchan_as_pchan2chan_nr(const struct gsm_lchan *lchan, - enum gsm_phys_chan_config as_pchan) -{ - if (lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && as_pchan == GSM_PCHAN_PDCH) - return RSL_CHAN_OSMO_PDCH | (lchan->ts->nr & ~RSL_CHAN_NR_MASK); - return gsm_pchan2chan_nr(as_pchan, lchan->ts->nr, lchan->nr); + /* Note: non-standard Osmocom style dyn TS PDCH mode chan_nr is only used within + * rsl_tx_dyn_ts_pdch_act_deact(). */ + return gsm_pchan2chan_nr(lchan->ts->pchan_is, lchan->ts->nr, lchan->nr); } /* return the gsm_lchan for the CBCH (if it exists at all) */ @@ -1220,12 +1197,12 @@ struct gsm_lchan *gsm_bts_get_cbch(struct gsm_bts *bts) struct gsm_lchan *lchan = NULL; struct gsm_bts_trx *trx = bts->c0; - if (trx->ts[0].pchan == GSM_PCHAN_CCCH_SDCCH4_CBCH) + if (trx->ts[0].pchan_is == GSM_PCHAN_CCCH_SDCCH4_CBCH) lchan = &trx->ts[0].lchan[2]; else { int i; for (i = 0; i < 8; i++) { - if (trx->ts[i].pchan == GSM_PCHAN_SDCCH8_SACCH8C_CBCH) { + if (trx->ts[i].pchan_is == GSM_PCHAN_SDCCH8_SACCH8C_CBCH) { lchan = &trx->ts[i].lchan[2]; break; } @@ -1243,48 +1220,31 @@ struct gsm_lchan *rsl_lchan_lookup(struct gsm_bts_trx *trx, uint8_t chan_nr, uint8_t cbits = chan_nr >> 3; uint8_t lch_idx; struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr]; - bool ok = true; + bool ok; if (rc) *rc = -EINVAL; if (cbits == 0x01) { - lch_idx = 0; /* TCH/F */ - if (ts->pchan != GSM_PCHAN_TCH_F && - ts->pchan != GSM_PCHAN_PDCH && - ts->pchan != GSM_PCHAN_TCH_F_PDCH - && !(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && (ts->dyn.pchan_is == GSM_PCHAN_TCH_F - || ts->dyn.pchan_want == GSM_PCHAN_TCH_F))) - ok = false; + lch_idx = 0; /* TCH/F */ + ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_TCH_F) + || ts->pchan_on_init == GSM_PCHAN_PDCH; /* PDCH? really? */ } else if ((cbits & 0x1e) == 0x02) { lch_idx = cbits & 0x1; /* TCH/H */ - if (ts->pchan != GSM_PCHAN_TCH_H - && !(ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH - && (ts->dyn.pchan_is == GSM_PCHAN_TCH_H - || ts->dyn.pchan_want == GSM_PCHAN_TCH_H))) - ok = false; + ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_TCH_H); } else if ((cbits & 0x1c) == 0x04) { lch_idx = cbits & 0x3; /* SDCCH/4 */ - if (ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) - ok = false; + ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_CCCH_SDCCH4); } else if ((cbits & 0x18) == 0x08) { lch_idx = cbits & 0x7; /* SDCCH/8 */ - if (ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C && - ts->pchan != GSM_PCHAN_SDCCH8_SACCH8C_CBCH) - ok = false; + ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_SDCCH8_SACCH8C); } else if (cbits == 0x10 || cbits == 0x11 || cbits == 0x12) { - lch_idx = 0; - if (ts->pchan != GSM_PCHAN_CCCH && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4 && - ts->pchan != GSM_PCHAN_CCCH_SDCCH4_CBCH) - ok = false; + lch_idx = 0; /* CCCH? */ + ok = ts_is_capable_of_pchan(ts, GSM_PCHAN_CCCH); /* FIXME: we should not return first sdcch4 !!! */ } else if ((chan_nr & RSL_CHAN_NR_MASK) == RSL_CHAN_OSMO_PDCH) { lch_idx = 0; - if (ts->pchan != GSM_PCHAN_TCH_F_TCH_H_PDCH) - ok = false; + ok = (ts->pchan_on_init == GSM_PCHAN_TCH_F_TCH_H_PDCH); } else return NULL; @@ -1304,33 +1264,18 @@ static const uint8_t subslots_per_pchan[] = { [GSM_PCHAN_SDCCH8_SACCH8C] = 8, [GSM_PCHAN_CCCH_SDCCH4_CBCH] = 4, [GSM_PCHAN_SDCCH8_SACCH8C_CBCH] = 8, - /* - * GSM_PCHAN_TCH_F_PDCH and GSM_PCHAN_TCH_F_TCH_H_PDCH should not be - * part of this, those TS are handled according to their dynamic state. - */ + /* Dyn TS: maximum allowed subslots */ + [GSM_PCHAN_TCH_F_TCH_H_PDCH] = 2, + [GSM_PCHAN_TCH_F_PDCH] = 1, }; -/*! Return the actual pchan type, also heeding dynamic TS. */ -enum gsm_phys_chan_config ts_pchan(struct gsm_bts_trx_ts *ts) -{ - switch (ts->pchan) { - case GSM_PCHAN_TCH_F_TCH_H_PDCH: - return ts->dyn.pchan_is; - case GSM_PCHAN_TCH_F_PDCH: - if (ts->flags & TS_F_PDCH_ACTIVE) - return GSM_PCHAN_PDCH; - else - return GSM_PCHAN_TCH_F; - default: - return ts->pchan; - } -} - /*! According to ts->pchan and possibly ts->dyn_pchan, return the number of * logical channels available in the timeslot. */ -uint8_t ts_subslots(struct gsm_bts_trx_ts *ts) +uint8_t pchan_subslots(enum gsm_phys_chan_config pchan) { - return subslots_per_pchan[ts_pchan(ts)]; + if (pchan < 0 || pchan >= ARRAY_SIZE(subslots_per_pchan)) + return 0; + return subslots_per_pchan[pchan]; } static bool pchan_is_tch(enum gsm_phys_chan_config pchan) @@ -1346,7 +1291,7 @@ static bool pchan_is_tch(enum gsm_phys_chan_config pchan) bool ts_is_tch(struct gsm_bts_trx_ts *ts) { - return pchan_is_tch(ts_pchan(ts)); + return pchan_is_tch(ts->pchan_is); } bool trx_is_usable(const struct gsm_bts_trx *trx) @@ -1361,34 +1306,20 @@ bool trx_is_usable(const struct gsm_bts_trx *trx) return true; } -void gsm_trx_mark_all_ts_uninitialized(struct gsm_bts_trx *trx) +void gsm_trx_all_ts_dispatch(struct gsm_bts_trx *trx, uint32_t ts_ev, void *data) { int i; for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { struct gsm_bts_trx_ts *ts = &trx->ts[i]; - ts->initialized = false; + osmo_fsm_inst_dispatch(ts->fi, ts_ev, data); } } -void gsm_bts_mark_all_ts_uninitialized(struct gsm_bts *bts) +void gsm_bts_all_ts_dispatch(struct gsm_bts *bts, uint32_t ts_ev, void *data) { struct gsm_bts_trx *trx; llist_for_each_entry(trx, &bts->trx_list, list) - gsm_trx_mark_all_ts_uninitialized(trx); -} - -/* Trigger initial timeslot actions iff both OML and RSL are setup. */ -void gsm_ts_check_init(struct gsm_bts_trx_ts *ts) -{ - struct gsm_bts *bts = ts->trx->bts; - if (bts->model->oml_is_ts_ready - && !bts->model->oml_is_ts_ready(ts)) - return; - if (!ts->trx->rsl_link) - return; - if (ts->initialized) - return; - ts->initialized = on_gsm_ts_init(ts); + gsm_trx_all_ts_dispatch(trx, ts_ev, data); } void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd, @@ -1417,3 +1348,375 @@ bool nm_is_running(const struct gsm_nm_state *s) { (s->availability == 0xff) ); } + +/* determine the logical channel type based on the physical channel type */ +int gsm_lchan_type_by_pchan(enum gsm_phys_chan_config pchan) +{ + switch (pchan) { + case GSM_PCHAN_TCH_F: + return GSM_LCHAN_TCH_F; + case GSM_PCHAN_TCH_H: + return GSM_LCHAN_TCH_H; + case GSM_PCHAN_SDCCH8_SACCH8C: + case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: + case GSM_PCHAN_CCCH_SDCCH4: + case GSM_PCHAN_CCCH_SDCCH4_CBCH: + return GSM_LCHAN_SDCCH; + default: + return -1; + } +} + +enum gsm_phys_chan_config gsm_pchan_by_lchan_type(enum gsm_chan_t type) +{ + switch (type) { + case GSM_LCHAN_TCH_F: + return GSM_PCHAN_TCH_F; + case GSM_LCHAN_TCH_H: + return GSM_PCHAN_TCH_H; + case GSM_LCHAN_NONE: + case GSM_LCHAN_PDTCH: + /* TODO: so far lchan->type is NONE in PDCH mode. PDTCH is only + * used in osmo-bts. Maybe set PDTCH and drop the NONE case + * here. */ + return GSM_PCHAN_PDCH; + default: + return GSM_PCHAN_UNKNOWN; + } +} + +/* Can the timeslot in principle be used as this PCHAN kind? */ +bool ts_is_capable_of_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config pchan) +{ + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_PDCH: + switch (pchan) { + case GSM_PCHAN_TCH_F: + case GSM_PCHAN_PDCH: + return true; + default: + return false; + } + + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + switch (pchan) { + case GSM_PCHAN_TCH_F: + case GSM_PCHAN_TCH_H: + case GSM_PCHAN_PDCH: + return true; + default: + return false; + } + + case GSM_PCHAN_CCCH_SDCCH4_CBCH: + switch (pchan) { + case GSM_PCHAN_CCCH_SDCCH4_CBCH: + case GSM_PCHAN_CCCH_SDCCH4: + case GSM_PCHAN_CCCH: + return true; + default: + return false; + } + + case GSM_PCHAN_CCCH_SDCCH4: + switch (pchan) { + case GSM_PCHAN_CCCH_SDCCH4: + case GSM_PCHAN_CCCH: + return true; + default: + return false; + } + + case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: + switch (pchan) { + case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: + case GSM_PCHAN_SDCCH8_SACCH8C: + return true; + default: + return false; + } + + default: + return ts->pchan_on_init == pchan; + } +} + +static int trx_count_free_ts(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan) +{ + struct gsm_bts_trx_ts *ts; + struct gsm_lchan *lchan; + int j; + int count = 0; + + if (!trx_is_usable(trx)) + return 0; + + for (j = 0; j < ARRAY_SIZE(trx->ts); j++) { + ts = &trx->ts[j]; + if (!ts_is_usable(ts)) + continue; + + if (ts->pchan_is == GSM_PCHAN_PDCH) { + /* Dynamic timeslots in PDCH mode will become TCH if needed. */ + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_PDCH: + if (pchan == GSM_PCHAN_TCH_F) + count++; + continue; + + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + if (pchan == GSM_PCHAN_TCH_F) + count++; + else if (pchan == GSM_PCHAN_TCH_H) + count += 2; + continue; + + default: + /* Not dynamic, not applicable. */ + continue; + } + } + + if (ts->pchan_is != pchan) + continue; + + ts_for_each_lchan(lchan, ts) { + if (lchan_state_is(lchan, LCHAN_ST_UNUSED)) + count++; + } + } + + return count; +} + +/* Count number of free TS of given pchan type */ +int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) +{ + struct gsm_bts_trx *trx; + int count = 0; + + llist_for_each_entry(trx, &bts->trx_list, list) + count += trx_count_free_ts(trx, pchan); + + return count; +} + +bool ts_is_usable(const struct gsm_bts_trx_ts *ts) +{ + if (!trx_is_usable(ts->trx)) { + LOGP(DRLL, LOGL_DEBUG, "%s not usable\n", gsm_trx_name(ts->trx)); + return false; + } + + if (!ts->fi) + return false; + + switch (ts->fi->state) { + case TS_ST_NOT_INITIALIZED: + case TS_ST_BORKEN: + return false; + default: + break; + } + + return true; +} + +const struct value_string lchan_activate_mode_names[] = { + OSMO_VALUE_STRING(FOR_NONE), + OSMO_VALUE_STRING(FOR_MS_CHANNEL_REQUEST), + OSMO_VALUE_STRING(FOR_ASSIGNMENT), + OSMO_VALUE_STRING(FOR_HANDOVER), + OSMO_VALUE_STRING(FOR_VTY), + {} +}; + +/* Helper function for bsc_match_codec_pref(), looks up a matching permitted speech + * value for a given msc audio codec pref */ +static enum gsm0808_permitted_speech audio_support_to_gsm88(const struct gsm_audio_support *audio) +{ + if (audio->hr) { + switch (audio->ver) { + case 1: + return GSM0808_PERM_HR1; + break; + case 2: + return GSM0808_PERM_HR2; + break; + case 3: + return GSM0808_PERM_HR3; + break; + default: + LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: hr%d, using hr1 instead\n", + audio->ver); + return GSM0808_PERM_HR1; + } + } else { + switch (audio->ver) { + case 1: + return GSM0808_PERM_FR1; + break; + case 2: + return GSM0808_PERM_FR2; + break; + case 3: + return GSM0808_PERM_FR3; + break; + default: + LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: fr%d, using fr1 instead\n", + audio->ver); + return GSM0808_PERM_FR1; + } + } +} + +/* Helper function for bsc_match_codec_pref(), looks up a matching chan mode for + * a given permitted speech value */ +static enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech) +{ + switch (speech) { + case GSM0808_PERM_HR1: + case GSM0808_PERM_FR1: + return GSM48_CMODE_SPEECH_V1; + break; + case GSM0808_PERM_HR2: + case GSM0808_PERM_FR2: + return GSM48_CMODE_SPEECH_EFR; + break; + case GSM0808_PERM_HR3: + case GSM0808_PERM_FR3: + return GSM48_CMODE_SPEECH_AMR; + break; + default: + LOGP(DMSC, LOGL_FATAL, + "Unsupported permitted speech selected, assuming AMR as channel mode...\n"); + return GSM48_CMODE_SPEECH_AMR; + } +} + +/* Helper function for bsc_match_codec_pref(), tests if a given audio support + * matches one of the permitted speech settings of the channel type element. + * The matched permitted speech value is then also compared against the + * speech codec list. (optional, only relevant for AoIP) */ +static bool test_codec_pref(const struct gsm0808_channel_type *ct, + const struct gsm0808_speech_codec_list *scl, + uint8_t perm_spch) +{ + unsigned int i; + bool match = false; + struct gsm0808_speech_codec sc; + int rc; + + /* Try to finde the given permitted speech value in the + * codec list of the channel type element */ + for (i = 0; i < ct->perm_spch_len; i++) { + if (ct->perm_spch[i] == perm_spch) { + match = true; + break; + } + } + + /* If we do not have a speech codec list to test against, + * we just exit early (will be always the case in non-AoIP networks) */ + if (!scl || !scl->len) + return match; + + /* If we failed to match until here, there is no + * point in testing further */ + if (match == false) + return false; + + /* Extrapolate speech codec data */ + rc = gsm0808_speech_codec_from_chan_type(&sc, perm_spch); + if (rc < 0) + return false; + + /* Try to find extrapolated speech codec data in + * the speech codec list */ + for (i = 0; i < scl->len; i++) { + if (sc.type == scl->codec[i].type) + return true; + } + + return false; +} + +/*! Match the codec preferences from local config with a received codec preferences IEs received from the + * MSC. + * \param[out] chan_mode GSM 04.08 channel mode. + * \param[out] full_rate true iff full-rate. + * \param[in] ct GSM 08.08 channel type received from MSC. + * \param[in] scl GSM 08.08 speech codec list received from MSC (optional). + * \param[in] audio_support List of allowed codecs as from local config. + * \param[in] audio_length Number of items in audio_support. + * \returns 0 on success, -1 in case no match was found */ +int bsc_match_codec_pref(enum gsm48_chan_mode *chan_mode, + bool *full_rate, + const struct gsm0808_channel_type *ct, + const struct gsm0808_speech_codec_list *scl, + struct gsm_audio_support * const *audio_support, + int audio_length) +{ + unsigned int i; + uint8_t perm_spch; + bool match = false; + + for (i = 0; i < audio_length; i++) { + perm_spch = audio_support_to_gsm88(audio_support[i]); + if (test_codec_pref(ct, scl, perm_spch)) { + match = true; + break; + } + } + + /* Exit without result, in case no match can be deteched */ + if (!match) { + *full_rate = false; + *chan_mode = GSM48_CMODE_SIGN; + return -1; + } + + /* Check if the result is a half or full rate codec */ + switch (perm_spch) { + case GSM0808_PERM_HR1: + case GSM0808_PERM_HR2: + case GSM0808_PERM_HR3: + case GSM0808_PERM_HR4: + case GSM0808_PERM_HR6: + *full_rate = false; + break; + + case GSM0808_PERM_FR1: + case GSM0808_PERM_FR2: + case GSM0808_PERM_FR3: + case GSM0808_PERM_FR4: + case GSM0808_PERM_FR5: + *full_rate = true; + break; + + default: + LOGP(DMSC, LOGL_ERROR, "Invalid permitted-speech value: %u\n", perm_spch); + return -EINVAL; + } + + /* Lookup a channel mode for the selected codec */ + *chan_mode = gsm88_to_chan_mode(perm_spch); + + return 0; +} + +bool sockaddr_to_str_and_uint(char *rtp_addr, size_t rtp_addr_len, uint16_t *rtp_port, + const struct sockaddr_storage *sa) +{ + int rc; + const struct sockaddr_in *sin; + + sin = (const struct sockaddr_in *)sa; + *rtp_port = osmo_ntohs(sin->sin_port); + + rc = osmo_strlcpy(rtp_addr, inet_ntoa(sin->sin_addr), rtp_addr_len); + if (rc <= 0 || rc >= rtp_addr_len) + return false; + + return true; +} diff --git a/src/osmo-bsc/handover_decision.c b/src/osmo-bsc/handover_decision.c index 887c2993f..25c297f71 100644 --- a/src/osmo-bsc/handover_decision.c +++ b/src/osmo-bsc/handover_decision.c @@ -32,47 +32,9 @@ #include #include -#include +#include #include -/* Find BTS by ARFCN and BSIC */ -struct gsm_bts *bts_by_arfcn_bsic(const struct gsm_network *net, - uint16_t arfcn, uint8_t bsic) -{ - struct gsm_bts *bts; - - llist_for_each_entry(bts, &net->bts_list, list) { - if (bts->c0->arfcn == arfcn && - bts->bsic == bsic) - return bts; - } - - return NULL; -} - - -/* issue handover to a cell identified by ARFCN and BSIC */ -static int handover_to_arfcn_bsic(struct gsm_lchan *lchan, - uint16_t arfcn, uint8_t bsic) -{ - struct gsm_bts *new_bts; - - /* resolve the gsm_bts structure for the best neighbor */ - /* FIXME: use some better heuristics here to determine which cell - * using this ARFCN really is closest to the target cell. For - * now we simply assume that each ARFCN will only be used by one - * cell */ - new_bts = bts_by_arfcn_bsic(lchan->ts->trx->bts->network, arfcn, bsic); - if (!new_bts) { - LOGP(DHODEC, LOGL_NOTICE, "unable to determine neighbor BTS " - "for ARFCN %u BSIC %u ?!?\n", arfcn, bsic); - return -EINVAL; - } - - /* and actually try to handover to that cell */ - return bsc_handover_start(HODEC1, lchan, new_bts, lchan->type); -} - /* did we get a RXLEV for a given cell in the given report? */ static int rxlev_for_cell_in_rep(struct gsm_meas_rep *mr, uint16_t arfcn, uint8_t bsic) @@ -196,12 +158,12 @@ static void process_meas_neigh(struct gsm_meas_rep *mr) } /* attempt to do a handover */ -static int attempt_handover(struct gsm_meas_rep *mr) +static void attempt_handover(struct gsm_meas_rep *mr) { struct gsm_bts *bts = mr->lchan->ts->trx->bts; struct neigh_meas_proc *best_cell = NULL; unsigned int best_better_db = 0; - int i, rc; + int i; /* find the best cell in this report that is at least RXLEV_HYST * better than the current serving cell */ @@ -229,30 +191,29 @@ static int attempt_handover(struct gsm_meas_rep *mr) } if (!best_cell) - return 0; + return; LOGP(DHODEC, LOGL_INFO, "%s: Cell on ARFCN %u is better: ", gsm_ts_name(mr->lchan->ts), best_cell->arfcn); if (!ho_get_ho_active(bts->ho)) { LOGPC(DHODEC, LOGL_INFO, "Skipping, Handover disabled\n"); - return 0; + return; } - rc = handover_to_arfcn_bsic(mr->lchan, best_cell->arfcn, best_cell->bsic); - switch (rc) { - case 0: - LOGPC(DHODEC, LOGL_INFO, "Starting handover: meas report number %d \n", mr->nr); - break; - case -ENOSPC: - LOGPC(DHODEC, LOGL_INFO, "No channel available\n"); - break; - case -EBUSY: - LOGPC(DHODEC, LOGL_INFO, "Handover already active\n"); - break; - default: - LOGPC(DHODEC, LOGL_ERROR, "Unknown error\n"); + { + struct handover_mo_req req = { + .from_hodec_id = HODEC1, + .old_lchan = mr->lchan, + .target_nik = { + .from_bts = bts->nr, + .arfcn = best_cell->arfcn, + .bsic_kind = BSIC_6BIT, + .bsic = best_cell->bsic, + }, + }; + + handover_request(&req); } - return rc; } /* process an already parsed measurement report and decide if we want to diff --git a/src/osmo-bsc/handover_decision_2.c b/src/osmo-bsc/handover_decision_2.c index 7ac54df95..447615167 100644 --- a/src/osmo-bsc/handover_decision_2.c +++ b/src/osmo-bsc/handover_decision_2.c @@ -27,35 +27,41 @@ #include #include -#include +#include #include #include #include #include -#include +#include #include #include +#include +#include #define LOGPHOBTS(bts, level, fmt, args...) \ LOGP(DHODEC, level, "(BTS %u) " fmt, bts->nr, ## args) #define LOGPHOLCHAN(lchan, level, fmt, args...) \ - LOGP(DHODEC, level, "(lchan %u.%u%u%u %s) (subscr %s) " fmt, \ + LOGP(DHODEC, level, "(lchan %u.%u%u%u %s%s%s) (subscr %s) " fmt, \ lchan->ts->trx->bts->nr, \ lchan->ts->trx->nr, \ lchan->ts->nr, \ lchan->nr, \ - gsm_pchan_name(lchan->ts->pchan), \ + gsm_pchan_name(lchan->ts->pchan_on_init), \ + lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : " as ", \ + lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : gsm_pchan_name(lchan->ts->pchan_is), \ bsc_subscr_name(lchan->conn? lchan->conn->bsub : NULL), \ ## args) #define LOGPHOLCHANTOBTS(lchan, new_bts, level, fmt, args...) \ - LOGP(DHODEC, level, "(lchan %u.%u%u%u %s)->(BTS %u) (subscr %s) " fmt, \ + LOGP(DHODEC, level, "(lchan %u.%u%u%u %s%s%s)->(BTS %u) (subscr %s) " fmt, \ lchan->ts->trx->bts->nr, \ lchan->ts->trx->nr, \ lchan->ts->nr, \ lchan->nr, \ - gsm_pchan_name(lchan->ts->pchan), \ + gsm_pchan_name(lchan->ts->pchan_on_init), \ + lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : " as ", \ + lchan->ts->pchan_on_init == lchan->ts->pchan_is? "" : gsm_pchan_name(lchan->ts->pchan_is), \ new_bts->nr, \ bsc_subscr_name(lchan->conn? lchan->conn->bsub : NULL), \ ## args) @@ -74,7 +80,8 @@ struct ho_candidate { struct gsm_lchan *lchan; /* candidate for whom */ - struct gsm_bts *bts; /* target BTS */ + struct gsm_bts *bts; /* target BTS in local BSS */ + struct gsm0808_cell_id_list2 *cil; /* target cells in remote BSS */ uint8_t requirements; /* what is fulfilled */ int avg; /* average RX level */ }; @@ -147,22 +154,47 @@ void hodec2_on_change_congestion_check_interval(struct gsm_network *net, unsigne reinit_congestion_timer(net); } -static void conn_penalty_time_add(struct gsm_subscriber_connection *conn, struct gsm_bts *bts, +static void _conn_penalty_time_add(struct gsm_subscriber_connection *conn, + const void *for_object, int penalty_time) { + if (!for_object) { + LOGP(DHODEC, LOGL_ERROR, "%s Unable to set Handover-2 penalty timer:" + " no target cell pointer\n", + bsc_subscr_name(conn->bsub)); + return; + } + if (!conn->hodec2.penalty_timers) { conn->hodec2.penalty_timers = penalty_timers_init(conn); OSMO_ASSERT(conn->hodec2.penalty_timers); } - penalty_timers_add(conn->hodec2.penalty_timers, bts, penalty_time); + + penalty_timers_add(conn->hodec2.penalty_timers, for_object, penalty_time); +} + +static void nik_penalty_time_add(struct gsm_subscriber_connection *conn, + struct neighbor_ident_key *nik, + int penalty_time) +{ + _conn_penalty_time_add(conn, + neighbor_ident_get(conn->network->neighbor_bss_cells, nik), + penalty_time); +} + +static void bts_penalty_time_add(struct gsm_subscriber_connection *conn, + struct gsm_bts *bts, + int penalty_time) +{ + _conn_penalty_time_add(conn, bts, penalty_time); } static unsigned int conn_penalty_time_remaining(struct gsm_subscriber_connection *conn, - struct gsm_bts *bts) + const void *for_object) { if (!conn->hodec2.penalty_timers) return 0; - return penalty_timers_remaining(conn->hodec2.penalty_timers, bts); + return penalty_timers_remaining(conn->hodec2.penalty_timers, for_object); } /* did we get a RXLEV for a given cell in the given report? Mark matches as MRC_F_PROCESSED. */ @@ -300,7 +332,7 @@ static bool codec_type_is_supported(struct gsm_subscriber_connection *conn, int i; struct gsm0808_speech_codec_list *clist = &conn->codec_list; - if (!conn->codec_list_present) { + if (!conn->codec_list.len) { /* We don't have a list of supported codecs. This should never happen. */ LOGPHOLCHAN(conn->lchan, LOGL_ERROR, "No Speech Codec List present, accepting all codecs\n"); @@ -546,7 +578,7 @@ static uint8_t check_requirements(struct gsm_lchan *lchan, struct gsm_bts *bts, /* the maximum number of unsynchonized handovers must no be exceeded */ if (current_bts != bts - && bsc_ho_count(bts, true) >= ho_get_hodec2_ho_max(bts->ho)) { + && handover_count(bts, HO_SCOPE_ALL) >= ho_get_hodec2_ho_max(bts->ho)) { LOGPHOLCHANTOBTS(lchan, bts, LOGL_DEBUG, "not a candidate, number of allowed handovers (%d) would be exceeded\n", ho_get_hodec2_ho_max(bts->ho)); @@ -687,8 +719,16 @@ static int trigger_handover_or_assignment(struct gsm_lchan *lchan, struct gsm_bt full_rate ? "TCH/F" : "TCH/H", ho_reason_name(global_ho_reason)); - return bsc_handover_start(HODEC2, lchan, current_bts == new_bts? NULL : new_bts, - full_rate? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H); + { + struct handover_mo_req req = { + .from_hodec_id = HODEC2, + .old_lchan = lchan, + .target_nik = *bts_ident_key(new_bts), + .new_lchan_type = full_rate? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H, + }; + handover_request(&req); + } + return 0; } /* debug collected candidates */ @@ -788,6 +828,13 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea struct gsm_bts *bts = lchan->ts->trx->bts; int tchf_count, tchh_count; struct gsm_bts *neighbor_bts; + const struct gsm0808_cell_id_list2 *neighbor_cil; + struct neighbor_ident_key ni = { + .from_bts = bts->nr, + .arfcn = nmp->arfcn, + .bsic_kind = BSIC_6BIT, + .bsic = nmp->bsic, + }; int avg; struct ho_candidate *c; int min_rxlev; @@ -801,16 +848,24 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea /* skip if measurement report is old */ if (nmp->last_seen_nr != lchan->meas_rep_last_seen_nr) { - LOGPHOLCHAN(lchan, LOGL_DEBUG, "neighbor ARFCN %u measurement report is old" + LOGPHOLCHAN(lchan, LOGL_DEBUG, "neighbor ARFCN %u BSIC %u measurement report is old" " (nmp->last_seen_nr=%u lchan->meas_rep_last_seen_nr=%u)\n", - nmp->arfcn, nmp->last_seen_nr, lchan->meas_rep_last_seen_nr); + nmp->arfcn, nmp->bsic, nmp->last_seen_nr, lchan->meas_rep_last_seen_nr); return; } - neighbor_bts = bts_by_arfcn_bsic(bts->network, nmp->arfcn, nmp->bsic); + neighbor_bts = bts_by_neighbor_ident(bts->network, &ni); if (!neighbor_bts) { - LOGPHOBTS(bts, LOGL_DEBUG, "neighbor ARFCN %u does not belong to this network\n", - nmp->arfcn); + neighbor_cil = neighbor_ident_get(bts->network->neighbor_bss_cells, &ni); + if (neighbor_cil) { + LOGPHOBTS(bts, LOGL_ERROR, "would inter-BSC handover to ARFCN %u BSIC %u," + " but inter-BSC handover not implemented for ho decision 2\n", + nmp->arfcn, nmp->bsic); + return; + } + + LOGPHOBTS(bts, LOGL_DEBUG, "no neighbor ARFCN %u BSIC %u configured for this cell\n", + nmp->arfcn, nmp->bsic); return; } @@ -820,7 +875,7 @@ static void collect_handover_candidate(struct gsm_lchan *lchan, struct neigh_mea return; } - /* caculate average rxlev for this cell over the window */ + /* calculate average rxlev for this cell over the window */ avg = neigh_meas_avg(nmp, ho_get_hodec2_rxlev_neigh_avg_win(bts->ho)); /* Heed rxlev hysteresis only if the RXLEV/RXQUAL/TA levels of the MS aren't critically bad and @@ -1146,12 +1201,12 @@ static void on_measurement_report(struct gsm_meas_rep *mr) LOGPHOLCHAN(lchan, LOGL_ERROR, "Skipping, No subscriber connection???\n"); return; } - if (lchan->conn->secondary_lchan) { + if (lchan->conn->assignment.new_lchan) { LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Initial Assignment is still ongoing\n"); return; } - if (lchan->conn->ho) { - LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Handover already triggered\n"); + if (lchan->conn->ho.fi) { + LOGPHOLCHAN(lchan, LOGL_INFO, "Skipping, Handover still ongoing\n"); return; } @@ -1227,11 +1282,11 @@ static void on_measurement_report(struct gsm_meas_rep *mr) && lchan->rqd_ta > ho_get_hodec2_max_distance(bts->ho)) { global_ho_reason = HO_REASON_MAX_DISTANCE; LOGPHOLCHAN(lchan, LOGL_INFO, "Attempting handover due to high TA\n"); - /* start penalty timer to prevent comming back too + /* start penalty timer to prevent coming back too * early. it must be started before selecting a better cell, * so there is no assignment selected, due to running * penalty timer. */ - conn_penalty_time_add(lchan->conn, bts, ho_get_hodec2_penalty_max_dist(bts->ho)); + bts_penalty_time_add(lchan->conn, bts, ho_get_hodec2_penalty_max_dist(bts->ho)); find_alternative_lchan(lchan, true); return; } @@ -1340,16 +1395,16 @@ static int bts_resolve_congestion(struct gsm_bts *bts, int tchf_congestion, int continue; /* (Do not consider dynamic TS that are in PDCH mode) */ - switch (ts_pchan(ts)) { + switch (ts->pchan_is) { case GSM_PCHAN_TCH_F: lc = &ts->lchan[0]; /* omit if channel not active */ if (lc->type != GSM_LCHAN_TCH_F - || lc->state != LCHAN_S_ACTIVE) + || !lchan_state_is(lc, LCHAN_ST_ESTABLISHED)) break; /* omit if there is an ongoing ho/as */ - if (!lc->conn || lc->conn->secondary_lchan - || lc->conn->ho) + if (!lc->conn || lc->conn->assignment.new_lchan + || lc->conn->ho.fi) break; /* We desperately want to resolve congestion, ignore rxlev when * collecting candidates by passing include_weaker_rxlev=true. */ @@ -1360,12 +1415,12 @@ static int bts_resolve_congestion(struct gsm_bts *bts, int tchf_congestion, int lc = &ts->lchan[j]; /* omit if channel not active */ if (lc->type != GSM_LCHAN_TCH_H - || lc->state != LCHAN_S_ACTIVE) + || !lchan_state_is(lc, LCHAN_ST_ESTABLISHED)) continue; /* omit of there is an ongoing ho/as */ if (!lc->conn - || lc->conn->secondary_lchan - || lc->conn->ho) + || lc->conn->assignment.new_lchan + || lc->conn->ho.fi) continue; /* We desperately want to resolve congestion, ignore rxlev when * collecting candidates by passing include_weaker_rxlev=true. */ @@ -1776,50 +1831,58 @@ static void congestion_check_cb(void *arg) reinit_congestion_timer(net); } -void on_ho_chan_activ_nack(struct bsc_handover *ho) +static void on_handover_end(struct gsm_subscriber_connection *conn, enum handover_result result) { - struct gsm_bts *new_bts = ho->new_lchan->ts->trx->bts; + struct gsm_bts *old_bts = NULL; + struct gsm_bts *new_bts = NULL; + int penalty; + struct handover *ho = &conn->ho; - LOGPHO(ho, LOGL_ERROR, "Channel Activate Nack for %s, starting penalty timer\n", ho->inter_cell? "Handover" : "Assignment"); - - /* if channel failed, wait 10 seconds before allowing to retry handover */ - conn_penalty_time_add(ho->old_lchan->conn, new_bts, 10); /* FIXME configurable */ -} + /* If all went fine, then there are no penalty timers to set. */ + if (result == HO_RESULT_OK) + return; -void on_ho_failure(struct bsc_handover *ho) -{ - struct gsm_bts *old_bts = ho->old_lchan->ts->trx->bts; - struct gsm_bts *new_bts = ho->new_lchan->ts->trx->bts; - struct gsm_subscriber_connection *conn = ho->old_lchan->conn; + if (conn->lchan) + old_bts = conn->lchan->ts->trx->bts; + if (ho->mt.new_lchan) + new_bts = ho->mt.new_lchan->ts->trx->bts; - if (!conn) { - LOGPHO(ho, LOGL_ERROR, "HO failure, but no conn"); + /* Only interested in handovers within this BSS or going out into another BSS. Incoming handovers + * from another BSS are accounted for in the other BSS. */ + if (!old_bts) return; - } - if (conn->hodec2.failures >= ho_get_hodec2_retries(old_bts->ho)) { - int penalty = ho->inter_cell - ? ho_get_hodec2_penalty_failed_ho(old_bts->ho) - : ho_get_hodec2_penalty_failed_as(old_bts->ho); - LOGPHO(ho, LOGL_NOTICE, "%s failed, starting penalty timer (%d s)\n", - ho->inter_cell ? "Handover" : "Assignment", - penalty); - conn->hodec2.failures = 0; - conn_penalty_time_add(conn, new_bts, penalty); - } else { + if (conn->hodec2.failures < ho_get_hodec2_retries(old_bts->ho)) { conn->hodec2.failures++; - LOGPHO(ho, LOGL_NOTICE, "%s failed, allowing handover decision to try again" + LOG_HO(conn, LOGL_NOTICE, "Failed, allowing handover decision to try again" " (%d/%d attempts)\n", - ho->inter_cell ? "Handover" : "Assignment", conn->hodec2.failures, ho_get_hodec2_retries(old_bts->ho)); + return; } + + switch (ho->scope) { + case HO_INTRA_CELL: + penalty = ho_get_hodec2_penalty_failed_as(old_bts->ho); + break; + default: + /* TODO: separate penalty for inter-BSC HO? */ + penalty = ho_get_hodec2_penalty_failed_ho(old_bts->ho); + break; + } + + LOG_HO(conn, LOGL_NOTICE, "Failed, starting penalty timer (%d s)\n", penalty); + conn->hodec2.failures = 0; + + if (new_bts) + bts_penalty_time_add(conn, new_bts, penalty); + else + nik_penalty_time_add(conn, &ho->target_cell, penalty); } -struct handover_decision_callbacks hodec2_callbacks = { +static struct handover_decision_callbacks hodec2_callbacks = { .hodec_id = 2, .on_measurement_report = on_measurement_report, - .on_ho_chan_activ_nack = on_ho_chan_activ_nack, - .on_ho_failure = on_ho_failure, + .on_handover_end = on_handover_end, }; void hodec2_init(struct gsm_network *net) diff --git a/src/osmo-bsc/handover_fsm.c b/src/osmo-bsc/handover_fsm.c new file mode 100644 index 000000000..3ec761eb6 --- /dev/null +++ b/src/osmo-bsc/handover_fsm.c @@ -0,0 +1,1124 @@ +/* Handover FSM implementation for intra-BSC and inter-BSC MT (to this BSC) Handover. + * (For inter-BSC MO, from this BSC, see handover_inter_mo_fsm.c) + * + * (C) 2018 by sysmocom - s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 . + * + */ + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOG_FMT_BTS "bts %u lac-ci %u-%u arfcn-bsic %d-%d" +#define LOG_ARGS_BTS(bts) \ + (bts) ? (bts)->nr : 0, \ + (bts) ? (bts)->location_area_code : 0, \ + (bts) ? (bts)->cell_identity : 0, \ + (bts) ? (bts)->c0->arfcn : 0, \ + (bts) ? (bts)->bsic : 0 + +#define LOG_FMT_LCHAN "%u-%u-%u-%s%s%s-%u" +#define LOG_ARGS_LCHAN(lchan) \ + lchan ? lchan->ts->trx->bts->nr : 0, \ + lchan ? lchan->ts->trx->nr : 0, \ + lchan ? lchan->ts->nr : 0, \ + lchan ? gsm_pchan_name(lchan->ts->pchan_on_init) : "-", \ + (!lchan || lchan->ts->pchan_on_init == lchan->ts->pchan_is)? "" : ":", \ + (!lchan || lchan->ts->pchan_on_init == lchan->ts->pchan_is)? "" \ + : gsm_pchan_name(lchan->ts->pchan_is), \ + lchan ? lchan->nr : 0 \ + +#define LOG_FMT_HO_SCOPE "(subscr %s) %s" +#define LOG_ARGS_HO_SCOPE(conn) \ + bsc_subscr_name(conn->bsub), \ + handover_scope_name(conn->ho.scope) + +/* Assume presence of local var 'conn' as struct gsm_subscriber_connection */ +#define ho_count(counter) do { \ + LOG_HO(conn, LOGL_DEBUG, "incrementing rate counter: %s %s", \ + bsc_ctr_description[counter].name, \ + bsc_ctr_description[counter].description); \ + rate_ctr_inc(&conn->network->bsc_ctrs->ctr[counter]); \ + } while(0) + +static uint8_t g_next_ho_ref = 1; + +const char *handover_status(struct gsm_subscriber_connection *conn) +{ + static char buf[256]; + struct handover *ho = &conn->ho; + + if (!conn) + return ""; + + if (ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC)) { + if (ho->mt.new_lchan) + snprintf(buf, sizeof(buf), + "("LOG_FMT_LCHAN") --HO-> (" LOG_FMT_LCHAN ") " LOG_FMT_HO_SCOPE, + LOG_ARGS_LCHAN(conn->lchan), + LOG_ARGS_LCHAN(ho->mt.new_lchan), + LOG_ARGS_HO_SCOPE(conn)); + else if (ho->mt.new_bts) + snprintf(buf, sizeof(buf), + "("LOG_FMT_LCHAN") --HO-> ("LOG_FMT_BTS",%s) " LOG_FMT_HO_SCOPE, + LOG_ARGS_LCHAN(conn->lchan), + LOG_ARGS_BTS(ho->mt.new_bts), + gsm_lchant_name(ho->new_lchan_type), + LOG_ARGS_HO_SCOPE(conn)); + else + snprintf(buf, sizeof(buf), + "("LOG_FMT_LCHAN") --HO->(?) " LOG_FMT_HO_SCOPE, + LOG_ARGS_LCHAN(conn->lchan), + LOG_ARGS_HO_SCOPE(conn)); + + } else if (ho->scope & HO_INTER_BSC_MO) + snprintf(buf, sizeof(buf), + "("LOG_FMT_LCHAN") --HO-> (%s) " LOG_FMT_HO_SCOPE, + LOG_ARGS_LCHAN(conn->lchan), + neighbor_ident_key_name(&ho->target_cell), + LOG_ARGS_HO_SCOPE(conn)); + + else if (ho->scope & HO_INTER_BSC_MT) { + if (ho->mt.new_lchan) + snprintf(buf, sizeof(buf), + "(remote:%s) --HO-> (local:%s|"LOG_FMT_LCHAN") " LOG_FMT_HO_SCOPE, + ho->mt.inter_bsc.cell_id_serving_name, + ho->mt.inter_bsc.cell_id_target_name, + LOG_ARGS_LCHAN(ho->mt.new_lchan), + LOG_ARGS_HO_SCOPE(conn)); + else if (ho->mt.new_bts) + snprintf(buf, sizeof(buf), + "(remote:%s) --HO-> (local:%s|"LOG_FMT_BTS",%s) " LOG_FMT_HO_SCOPE, + ho->mt.inter_bsc.cell_id_serving_name, + ho->mt.inter_bsc.cell_id_target_name, + LOG_ARGS_BTS(ho->mt.new_bts), + gsm_lchant_name(ho->new_lchan_type), + LOG_ARGS_HO_SCOPE(conn)); + else + snprintf(buf, sizeof(buf), + "(remote:%s) --HO-> (local:%s,%s) " LOG_FMT_HO_SCOPE, + ho->mt.inter_bsc.cell_id_serving_name, + ho->mt.inter_bsc.cell_id_target_name, + gsm_lchant_name(ho->new_lchan_type), + LOG_ARGS_HO_SCOPE(conn)); + } else + snprintf(buf, sizeof(buf), LOG_FMT_HO_SCOPE, LOG_ARGS_HO_SCOPE(conn)); + return buf; +} + +static struct osmo_fsm ho_fsm; + +/* From local var fi->priv, define local var conn. */ +#define GET_CONN() \ + struct gsm_subscriber_connection *conn = fi->priv; \ + OSMO_ASSERT((fi)->fsm == &ho_fsm && (fi)->priv) \ + +#define GET_HO() \ + GET_CONN(); \ + struct handover *ho = &conn->ho + +struct state_timeout ho_fsm_timeouts[32] = { + [HO_ST_WAIT_LCHAN_ACTIVE] = { .T = 23042 }, + [HO_ST_WAIT_RR_HO_DETECT] = { .T = 23042 }, + [HO_ST_WAIT_RR_HO_COMPLETE] = { .T = 23042 }, + [HO_ST_WAIT_LCHAN_ESTABLISHED] = { .T = 23042 }, + [HO_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { .T = 23042 }, + [MOHO_ST_WAIT_HO_COMMAND] = { .T = 7 }, + [MOHO_ST_WAIT_CLEAR] = { .T = 8 }, +}; + +/* Transition to a state, using the T timer defined in ho_fsm_timeouts. + * The actual timeout value is in turn obtained from network->T_defs. + * Assumes local variable fi exists. */ +#define ho_fsm_state_chg(state) \ + fsm_inst_state_chg_T(fi, state, \ + ho_fsm_timeouts, \ + ((struct gsm_subscriber_connection*)(fi->priv))->network->T_defs, \ + 5) + +/* Log failure and transition to HO_ST_FAILURE, which triggers the appropriate actions. */ +#define ho_fail(result, fmt, args...) do { \ + LOG_HO(conn, LOGL_ERROR, "Handover failed in state %s, %s: " fmt, \ + osmo_fsm_inst_state_name(conn->fi), handover_result_name(result), ## args); \ + handover_end(conn, result); \ + } while(0) + +#define ho_success() do { \ + LOG_HO(conn, LOGL_DEBUG, "Handover succeeded"); \ + handover_end(conn, HO_RESULT_OK); \ + } while(0) + +/* issue handover to a cell identified by ARFCN and BSIC */ +void handover_request(struct handover_mo_req *req) +{ + struct gsm_subscriber_connection *conn; + OSMO_ASSERT(req->old_lchan); + + conn = req->old_lchan->conn; + OSMO_ASSERT(conn && conn->fi); + + /* To make sure we're allowed to start a handover, go through a gscon event dispatch. */ + osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HANDOVER_START, req); +} + +/* Check that ho has old_lchan and/or new_lchan and conn pointers match. + * If old_lchan and/or new_lchan are NULL, omit those checks. + * On error, return false, log an error and call handover_end() with HO_RESULT_ERROR. */ +bool handover_is_sane(struct gsm_subscriber_connection *conn, struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan) +{ + if (!conn->ho.fi) { + LOG_HO(conn, LOGL_ERROR, "No handover ongoing"); + return false; + } + + if (old_lchan + && (conn != old_lchan->conn || conn->lchan != old_lchan)) + goto insane; + if (new_lchan + && (conn != new_lchan->conn || conn->ho.mt.new_lchan != new_lchan)) + goto insane; + if (conn->lchan && conn->lchan == conn->ho.mt.new_lchan) + goto insane; + + return true; +insane: + LOG_HO(conn, LOGL_ERROR, "Handover state is corrupted"); + handover_end(conn, HO_RESULT_ERROR); + return false; +} + +static void ho_fsm_update_id(struct osmo_fsm_inst *fi, const char *label) +{ + GET_CONN(); + if (conn->fi->id) + osmo_fsm_inst_update_id_f(fi, "%s_%s", label, conn->fi->id); + else + osmo_fsm_inst_update_id_f(fi, "%s_conn%u", label, conn->sccp.conn_id); +} + +static void handover_reset(struct gsm_subscriber_connection *conn) +{ + struct mgwep_ci *ci; + if (conn->ho.mt.new_lchan) + /* New lchan was activated but never passed to a conn */ + lchan_release(conn->ho.mt.new_lchan, true, true, RSL_ERR_EQUIPMENT_FAIL); + + ci = conn->ho.mt.created_ci_for_msc; + if (ci) { + gscon_forget_mgw_endpoint_ci(conn, ci); + /* If this is the last endpoint released, the mgw_endpoint_fsm will terminate and tell + * the gscon about it. */ + mgw_endpoint_ci_dlcx(ci); + } + + conn->ho = (struct handover){ + .fi = conn->ho.fi, + }; +} + +void handover_fsm_alloc(struct gsm_subscriber_connection *conn) { + static bool g_initialized = false; + if (!g_initialized) { + OSMO_ASSERT(osmo_fsm_register(&ho_fsm) == 0); + g_initialized = true; + } + + OSMO_ASSERT(conn->fi); + OSMO_ASSERT(!conn->ho.fi); + + conn->ho.fi = osmo_fsm_inst_alloc_child(&ho_fsm, conn->fi, GSCON_EV_HANDOVER_END); + OSMO_ASSERT(conn->ho.fi); + conn->ho.fi->priv = conn; +} + +static void handover_start_intra_bsc(struct gsm_subscriber_connection *conn); +static void handover_start_inter_bsc_mo(struct gsm_subscriber_connection *conn, + const struct gsm0808_cell_id_list2 *target_cells); + +/* Invoked by gscon if a handover was accepted to start now. */ +void handover_start(struct handover_mo_req *req) +{ + + OSMO_ASSERT(req && req->old_lchan && req->old_lchan->conn); + struct gsm_subscriber_connection *conn = req->old_lchan->conn; + struct handover *ho = &conn->ho; + struct gsm_bts *bts; + const struct gsm0808_cell_id_list2 *cil; + + if (conn->ho.fi) { + LOG_HO(conn, LOGL_ERROR, "Handover requested while another handover is ongoing; Ignore"); + return; + } + handover_reset(conn); + + handover_fsm_alloc(conn); + + ho->from_hodec_id = req->from_hodec_id; + ho->new_lchan_type = req->new_lchan_type == GSM_LCHAN_NONE ? + req->old_lchan->type : req->new_lchan_type; + ho->target_cell = req->target_nik; + + bts = bts_by_neighbor_ident(conn->network, &req->target_nik); + if (bts) { + ho->mt.new_bts = bts; + handover_start_intra_bsc(conn); + return; + } + + cil = neighbor_ident_get(conn->network->neighbor_bss_cells, &req->target_nik); + if (cil) { + handover_start_inter_bsc_mo(conn, cil); + return; + } + + LOG_HO(conn, LOGL_ERROR, "Cannot handover %s: neighbor unknown\n", + neighbor_ident_key_name(&req->target_nik)); + handover_end(conn, HO_RESULT_FAIL_NO_CHANNEL); +} + +/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type. + * This is the main entry point for the actual handover algorithm, after the decision whether to initiate + * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */ +static void handover_start_intra_bsc(struct gsm_subscriber_connection *conn) +{ + struct handover *ho = &conn->ho; + struct osmo_fsm_inst *fi = conn->ho.fi; + + OSMO_ASSERT(ho->mt.new_bts); + OSMO_ASSERT(ho->new_lchan_type != GSM_LCHAN_NONE); + OSMO_ASSERT(!ho->mt.new_lchan); + + ho->scope = (ho->mt.new_bts == conn->lchan->ts->trx->bts) ? HO_INTRA_CELL : HO_INTRA_BSC; + ho->mt.ho_ref = g_next_ho_ref++; + ho->mt.async = true; + + ho->mt.new_lchan = lchan_select_by_type(ho->mt.new_bts, ho->new_lchan_type); + + if (ho->scope & HO_INTRA_CELL) + ho_fsm_update_id(fi, "intraCell"); + else + ho_fsm_update_id(fi, "intraBSC"); + + ho_count(BSC_CTR_HANDOVER_ATTEMPTED); + + if (!ho->mt.new_lchan) { + ho_fail(HO_RESULT_FAIL_NO_CHANNEL, + "No %s lchan available on BTS %u", + gsm_lchant_name(ho->new_lchan_type), ho->mt.new_bts->nr); + return; + } + LOG_HO(conn, LOGL_DEBUG, "Selected lchan %s", gsm_lchan_name(ho->mt.new_lchan)); + + ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ACTIVE); + + { + struct lchan_activate_info info = { + .activ_for = FOR_HANDOVER, + .for_conn = conn, + .chan_mode = conn->lchan->tch_mode, + .requires_voice_stream = conn->lchan->mgw_endpoint_ci_bts ? true : false, + .old_lchan = conn->lchan, + }; + + lchan_activate(ho->mt.new_lchan, &info); + } +} + +/* 3GPP TS 48.008 § 3.2.1.8 Handover Request */ +static bool parse_ho_request(struct gsm_subscriber_connection *conn, const struct msgb *msg, + struct handover_mt_req *req) +{ + struct tlv_parsed tp_arr[2]; + struct tlv_parsed *tp = &tp_arr[0]; + struct tlv_parsed *tp2 = &tp_arr[1]; + struct tlv_p_entry *e; + bool aoip = (conn->sccp.msc->a.asp_proto != OSMO_SS7_ASP_PROT_IPA); + + int payload_length = msg->tail - msg->l4h; + if (tlv_parse2(tp_arr, 2, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0) <= 0) { + LOG_HO(conn, LOGL_ERROR, "Failed to parse IEs\n"); + return false; + } + + if (!(e = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE))) { + LOG_HO(conn, LOGL_ERROR, "Missing Channel Type IE\n"); + return false; + } + if (gsm0808_dec_channel_type(&req->ct, e->val, e->len) <= 0) { + LOG_HO(conn, LOGL_ERROR, "Failed to parse Channel Type IE\n"); + return false; + } + + if (!(e = TLVP_GET(tp, GSM0808_IE_ENCRYPTION_INFORMATION))) { + LOG_HO(conn, LOGL_ERROR, "Missing Encryption Information IE\n"); + return false; + } + if (gsm0808_dec_encrypt_info(&req->ei, e->val, e->len) <= 0) { + LOG_HO(conn, LOGL_ERROR, "Failed to parse Encryption Information IE\n"); + return false; + } + + if ((e = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_TYPE_1))) { + if (e->len != sizeof(req->classmark.classmark1)) { + LOG_HO(conn, LOGL_ERROR, "Classmark Information 1 has wrong size\n"); + return false; + } + req->classmark.classmark1 = *(struct gsm48_classmark1*)e->val; + req->classmark.classmark1_set = true; + } else if ((e = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2))) { + uint8_t len = OSMO_MIN(sizeof(req->classmark.classmark2), + e->len); + if (!len) { + LOG_HO(conn, LOGL_ERROR, "Classmark Information 2 has zero size\n"); + return false; + } + memcpy(&req->classmark.classmark2, e->val, len); + req->classmark.classmark2_len = len; + } else { + LOG_HO(conn, LOGL_ERROR, "Missing IE: either Classmark Information 1 or 2 required\n"); + return false; + } + + if (TLVP_PRESENT(tp, GSM0808_IE_AOIP_TRASP_ADDR)) { + int rc; + struct sockaddr_storage msc_rtp_sa; + + if (!aoip) { + LOG_HO(conn, LOGL_ERROR, + "BSSMAP Handover Request contains AoIP Transport Address," + " but this is not an AoIP connection"); + return false; + } + rc = gsm0808_dec_aoip_trasp_addr(&msc_rtp_sa, + TLVP_VAL(tp, GSM0808_IE_AOIP_TRASP_ADDR), + TLVP_LEN(tp, GSM0808_IE_AOIP_TRASP_ADDR)); + if (rc < 0) { + LOG_HO(conn, LOGL_ERROR, "Unable to decode AoIP Transport Address."); + return false; + } + + if (!sockaddr_to_str_and_uint(req->msc_rtp_addr, sizeof(req->msc_rtp_addr), + &req->msc_rtp_port, &msc_rtp_sa)) { + LOG_HO(conn, LOGL_ERROR, "Invalid MSC's RTP address (too long?)"); + return false; + } + } else if (aoip) { + LOG_HO(conn, LOGL_ERROR, + "BSSMAP Handover Request lacks AoIP Transport Address on an AoIP connection"); + return false; + } + + /* The Cell Identifier (Serving) and Cell Identifier (Target) are both 3.2.2.17 and are + * identified by the same tag. So get one from tp and the other from tp2. */ + if (!(e = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER))) { + LOG_HO(conn, LOGL_ERROR, "Missing IE: Cell Identifier (Serving)\n"); + return false; + } + if (gsm0808_dec_cell_id(&req->cell_id_serving, e->val, e->len) < 0) { + LOG_HO(conn, LOGL_ERROR, "Invalid IE: Cell Identifier (Serving)\n"); + return false; + } + /* To be able to use gsm0808_cell_id_name() in logging without interfering with these cell id + * printouts, store the result. */ + snprintf(req->cell_id_serving_name, sizeof(req->cell_id_serving_name), + gsm0808_cell_id_name(&req->cell_id_serving)); + + if (!(e = TLVP_GET(tp2, GSM0808_IE_CELL_IDENTIFIER))) { + LOG_HO(conn, LOGL_ERROR, "Missing IE: Cell Identifier (Target)\n"); + return false; + } + if (gsm0808_dec_cell_id(&req->cell_id_target, e->val, e->len) < 0) { + LOG_HO(conn, LOGL_ERROR, "Invalid IE: Cell Identifier (Target)\n"); + return false; + } + snprintf(req->cell_id_target_name, sizeof(req->cell_id_target_name), + gsm0808_cell_id_name(&req->cell_id_target)); + + /* A lot of IEs remain ignored... */ + + return true; +} + +static bool chan_mode_is_tch(enum gsm48_chan_mode mode) +{ + switch (mode) { + case GSM48_CMODE_SPEECH_V1: + case GSM48_CMODE_SPEECH_EFR: + case GSM48_CMODE_SPEECH_AMR: + return true; + default: + return false; + } +} + +void handover_start_inter_bsc_mt(struct gsm_subscriber_connection *conn, + struct msgb *ho_request_msg) +{ + struct handover *ho = &conn->ho; + struct bsc_msc_data *msc = conn->sccp.msc; + struct handover_mt_req *req = &ho->mt.inter_bsc; + int match_idx; + enum gsm48_chan_mode mode; + bool full_rate; + struct osmo_fsm_inst *fi; + + handover_fsm_alloc(conn); + + *ho = (struct handover){ + .fi = ho->fi, + .from_hodec_id = HODEC_REMOTE, + .scope = HO_INTER_BSC_MT, + .mt = { + .ho_ref = g_next_ho_ref++, + .async = true, + }, + }; + + fi = ho->fi; + ho_fsm_update_id(fi, "interMT"); + + if (!parse_ho_request(conn, ho_request_msg, req)) { + ho_fail(HO_RESULT_ERROR, "Invalid Handover Request message from MSC\n"); + return; + } + + ho_count(BSC_CTR_INTER_BSC_HO_MT_ATTEMPTED); + + /* Figure out channel type */ + if (bsc_match_codec_pref(&mode, + &full_rate, + &req->ct, + &req->scl, + msc->audio_support, msc->audio_length)) { + ho_fail(HO_RESULT_FAIL_NO_CHANNEL, + "Could not find an allowed channel codec (%s, speech codec list len = %u)", + gsm0808_channel_type_name(&req->ct), + req->scl.len); + return; + } + + LOG_HO(conn, LOGL_DEBUG, "Found matching audio type: %s %s for channel_type =" + " { ch_indctr=0x%x, ch_rate_type=0x%x, perm_spch=[ %s] }\n", + gsm48_chan_mode_name(mode), full_rate? "full-rate" : "half-rate", + req->ct.ch_indctr, req->ct.ch_rate_type, + osmo_hexdump(req->ct.perm_spch, req->ct.perm_spch_len)); + + /* Figure out which cell to handover to. */ + for (match_idx = 0; ; match_idx++) { + struct gsm_bts *bts; + + bts = gsm_bts_by_cell_id(conn->network, &req->cell_id_target, + match_idx); + + /* Did we iterate all matches? */ + if (!bts) + break; + + ho->mt.new_bts = bts; + LOG_HO(conn, LOGL_DEBUG, "BTS %u matches cell id %s", + bts->nr, req->cell_id_target_name); + + ho->mt.new_lchan = lchan_select_by_chan_mode(bts, mode, full_rate); + if (!ho->mt.new_lchan) { + LOG_HO(conn, LOGL_DEBUG, "BTS %u has no matching free channels", + bts->nr); + ho->mt.new_bts = NULL; + continue; + } + + /* Found a match. */ + break; + } + + if (!ho->mt.new_bts) { + ho_fail(HO_RESULT_ERROR, "No local cell matches the target %s", + req->cell_id_target_name); + return; + } + + if (!ho->mt.new_lchan) { + ho_fail(HO_RESULT_ERROR, "No free/matching lchan found for %s %s %s\n", + req->cell_id_target_name, + gsm48_chan_mode_name(mode), full_rate ? "full-rate" : "half-rate"); + return; + } + + /* Just for completeness' sake, maybe some logging uses it? */ + ho->new_lchan_type = ho->mt.new_lchan->type; + + ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ACTIVE); + + { + struct lchan_activate_info info = { + .activ_for = FOR_HANDOVER, + .for_conn = conn, + .chan_mode = mode, + .requires_voice_stream = chan_mode_is_tch(mode), + }; + + lchan_activate(ho->mt.new_lchan, &info); + } +} + +#define FUNC_RESULT_COUNTER(name) \ +static int result_counter_##name(enum handover_result result) \ +{ \ + switch (result) { \ + case HO_RESULT_OK: \ + return BSC_CTR_##name##_COMPLETED; \ + case HO_RESULT_FAIL_NO_CHANNEL: \ + return BSC_CTR_##name##_NO_CHANNEL; \ + case HO_RESULT_FAIL_RR_HO_FAIL: \ + return BSC_CTR_##name##_FAILED; \ + case HO_RESULT_FAIL_TIMEOUT: \ + return BSC_CTR_##name##_TIMEOUT; \ + case HO_RESULT_CONN_RELEASE: \ + return BSC_CTR_##name##_STOPPED; \ + default: \ + case HO_RESULT_ERROR: \ + return BSC_CTR_##name##_ERROR; \ + } \ +} + +FUNC_RESULT_COUNTER(ASSIGNMENT) +FUNC_RESULT_COUNTER(HANDOVER) +FUNC_RESULT_COUNTER(INTER_BSC_HO_MT) + +static int result_counter_INTER_BSC_HO_MO(enum handover_result result) { + switch (result) { + case HO_RESULT_OK: + return BSC_CTR_INTER_BSC_HO_MT_COMPLETED; + case HO_RESULT_FAIL_TIMEOUT: + return BSC_CTR_INTER_BSC_HO_MT_TIMEOUT; + case HO_RESULT_CONN_RELEASE: + return BSC_CTR_INTER_BSC_HO_MT_STOPPED; + default: + case HO_RESULT_ERROR: + return BSC_CTR_INTER_BSC_HO_MT_ERROR; + } +} + +static int result_counter(enum handover_scope scope, enum handover_result result) +{ + switch (scope) { + case HO_INTRA_CELL: + return result_counter_ASSIGNMENT(result); + default: + LOGP(DHO, LOGL_ERROR, "invalid enum handover_scope value: %s\n", + handover_scope_name(scope)); + /* use "normal" HO_INTRA_BSC counter... */ + case HO_INTRA_BSC: + return result_counter_HANDOVER(result); + case HO_INTER_BSC_MO: + return result_counter_INTER_BSC_HO_MO(result); + case HO_INTER_BSC_MT: + return result_counter_INTER_BSC_HO_MT(result); + return result_counter_HANDOVER(result); + } +} + +/* Notify the handover decision algorithm of failure and clear out any handover state. */ +void handover_end(struct gsm_subscriber_connection *conn, enum handover_result result) +{ + struct handover_decision_callbacks *hdc; + struct handover *ho = &conn->ho; + + /* Sanity -- an error result ensures beyond doubt that we don't handover_mt_use_new_lchan() below + * when the handover isn't actually allowed to change this conn. */ + if (result == HO_RESULT_OK && ho->mt.new_lchan) { + if (!(ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC | HO_INTER_BSC_MT))) { + LOG_HO(conn, LOGL_ERROR, "Got new lchan, but this is not an MT HO\n"); + result = HO_RESULT_ERROR; + } + if (ho->mt.new_lchan->conn != conn) { + LOG_HO(conn, LOGL_ERROR, "Got new lchan, but it is for another conn\n"); + result = HO_RESULT_ERROR; + } + } + + if (ho->scope & HO_INTER_BSC_MT) { + if (result == HO_RESULT_OK) { + if (!ho->mt.new_lchan) { + LOG_HO(conn, LOGL_ERROR, "Inter-BSC HO MT ends in success," + " but there is no lchan"); + result = HO_RESULT_ERROR; + } else + result = bsc_tx_bssmap_ho_complete(conn, ho->mt.new_lchan); + } + + if (result != HO_RESULT_OK) + bsc_tx_bssmap_ho_failure(conn); + } + + /* Rembered this only for error handling: should handover fail, handover_reset() will release the + * MGW endpoint right away. If successful, the conn continues to use the endpoint. */ + if (result == HO_RESULT_OK) + conn->ho.mt.created_ci_for_msc = NULL; + + hdc = handover_decision_callbacks_get(ho->from_hodec_id); + if (hdc && hdc->on_handover_end) + hdc->on_handover_end(conn, result); + + ho_count(result_counter(ho->scope, result)); + + LOG_HO(conn, LOGL_INFO, "Result: %s\n", handover_result_name(result)); + + osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HANDOVER_END, &result); + + /* Detach the new_lchan last, so we can still see it in above logging */ + if (ho->mt.new_lchan) { + if (result == HO_RESULT_OK) + gscon_change_primary_lchan(conn, conn->ho.mt.new_lchan); + else + /* Release new lchan, it didn't work out */ + lchan_release(ho->mt.new_lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + ho->mt.new_lchan = NULL; + } + + handover_reset(conn); + + /* We've dispatched the handover result above, let's disconnect to not fire the same event again. + * The parent term event is a safety measure for unplanned termination. */ + osmo_fsm_inst_unlink_parent(conn->ho.fi, conn); + osmo_fsm_inst_term(conn->ho.fi, OSMO_FSM_TERM_REGULAR, 0); +} + +static void ho_fsm_wait_lchan_active(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case HO_EV_LCHAN_ACTIVE: + ho_fsm_state_chg(HO_ST_WAIT_RR_HO_DETECT); + return; + + case HO_EV_LCHAN_ERROR: + ho_fail(HO_RESULT_ERROR, "error while activating lchan %s", + gsm_lchan_name(conn->ho.mt.new_lchan)); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void ho_fsm_wait_rr_ho_detect_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_CONN(); + struct handover *ho = &conn->ho; + + struct msgb *rr_ho_cmd = gsm48_make_ho_cmd(ho->mt.new_lchan, + ho->mt.new_lchan->ms_power, + ho->mt.ho_ref); + if (!rr_ho_cmd) { + ho_fail(HO_RESULT_ERROR, "Unable to compose RR Handover Command"); + return; + } + + + if (ho->scope & (HO_INTRA_CELL | HO_INTRA_BSC)) { + /* conn->lchan is the old lchan being handovered from */ + rr_ho_cmd->lchan = conn->lchan; + rc = gsm48_sendmsg(rr_ho_cmd); + if (rc) + ho_fail(HO_RESULT_ERROR, "Unable to Tx RR Handover Command (rc=%d %s)", + rc, strerror(-rc)); + return; + } + + if (ho->scope & HO_INTER_BSC_MT) { + rc = bsc_tx_bssmap_ho_request_ack(conn, rr_ho_cmd); + if (rc) + ho_fail(HO_RESULT_ERROR, "Unable to Tx BSSMAP Handover Request Ack (rc=%d %s)", + rc, strerror(-rc)); + return; + } + + ho_fail(HO_RESULT_ERROR, "Invalid situation, no target for RR Handover Command"); +} + +static void ho_fsm_wait_rr_ho_detect(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_HO(); + switch (event) { + + case HO_EV_RR_HO_DETECT: + { + OSMO_ASSERT(data); + struct handover_rr_detect_data *d = data; + if (d->access_delay) { + LOG_HO(conn, LOGL_DEBUG, "RR Handover Detect (Access Delay=%u)", + *(d->access_delay)); + } else + LOG_HO(conn, LOGL_DEBUG, "RR Handover Detect (no Access Delay IE)"); + } + + if (ho->scope & HO_INTER_BSC_MT) { + int rc = bsc_tx_bssmap_ho_detect(conn); + if (rc) { + ho_fail(HO_RESULT_ERROR, + "Unable to send BSSMAP Handover Detect"); + return; + } + } + + ho_fsm_state_chg(HO_ST_WAIT_RR_HO_COMPLETE); + /* The lchan FSM will already start to redirect the RTP stream */ + return; + + case HO_EV_RR_HO_COMPLETE: + LOG_HO(conn, LOGL_ERROR, + "Received RR Handover Complete, but haven't even seen a Handover Detect yet;" + " Accepting handover anyway"); + ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ESTABLISHED); + return; + + case HO_EV_RR_HO_FAIL: + ho_fail(HO_RESULT_FAIL_RR_HO_FAIL, "Received RR Handover Fail message"); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void ho_fsm_wait_rr_ho_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + + switch (event) { + + case HO_EV_RR_HO_COMPLETE: + ho_fsm_state_chg(HO_ST_WAIT_LCHAN_ESTABLISHED); + return; + + case HO_EV_RR_HO_FAIL: + ho_fail(HO_RESULT_FAIL_RR_HO_FAIL, "Received RR Handover Fail message"); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void ho_fsm_post_lchan_established(struct osmo_fsm_inst *fi); + +static void ho_fsm_wait_lchan_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_HO(); + + /* The RR Assignment Complete counts as RLL Establish event */ + osmo_fsm_inst_dispatch(ho->mt.new_lchan->fi, LCHAN_EV_RLL_ESTABLISH_IND, 0); + + if (conn->ho.fi + && lchan_state_is(ho->mt.new_lchan, LCHAN_ST_ESTABLISHED)) + ho_fsm_post_lchan_established(fi); +} + +static void ho_fsm_wait_lchan_established(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + + case HO_EV_LCHAN_ESTABLISHED: + ho_fsm_post_lchan_established(fi); + break; + + default: + OSMO_ASSERT(false); + } +} + +static void ho_fsm_post_lchan_established(struct osmo_fsm_inst *fi) +{ + GET_HO(); + + if (ho->mt.new_lchan->activate.requires_voice_stream + && (ho->scope & HO_INTER_BSC_MT)) + ho_fsm_state_chg(HO_ST_WAIT_MGW_ENDPOINT_TO_MSC); + else + ho_success(); +} + +static void ho_fsm_wait_mgw_endpoint_to_msc_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_HO(); + + if (!gscon_connect_mgw_to_msc(conn, + ho->mt.new_lchan, + ho->mt.inter_bsc.msc_rtp_addr, + ho->mt.inter_bsc.msc_rtp_port, + fi, + HO_EV_MSC_MGW_OK, + HO_EV_MSC_MGW_FAIL, + NULL, + &ho->mt.created_ci_for_msc)) { + ho_fail(HO_RESULT_ERROR, + "Unable to connect MGW endpoint to the MSC side"); + } +} + +static void ho_fsm_wait_mgw_endpoint_to_msc(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case HO_EV_MSC_MGW_OK: + ho_success(); + return; + + case HO_EV_MSC_MGW_FAIL: + ho_fail(HO_RESULT_ERROR, + "Unable to connect MGW endpoint to the MSC side"); + return; + + default: + OSMO_ASSERT(false); + } +} + +/* Inter-BSC MO */ + +static void handover_start_inter_bsc_mo(struct gsm_subscriber_connection *conn, + const struct gsm0808_cell_id_list2 *target_cells) +{ + int rc; + struct handover *ho = &conn->ho; + struct osmo_fsm_inst *fi = conn->ho.fi; + + ho->scope = HO_INTER_BSC_MO; + ho_fsm_update_id(fi, "interMO"); + ho_count(BSC_CTR_INTER_BSC_HO_MO_ATTEMPTED); + + rc = bsc_tx_bssmap_ho_required(conn->lchan, target_cells); + if (rc) { + ho_fail(HO_RESULT_ERROR, "Unable to send BSSMAP Handover Required message"); + return; + } + + ho_fsm_state_chg(MOHO_ST_WAIT_HO_COMMAND); +} + +static void moho_fsm_wait_ho_command(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + int rc; + struct moho_rx_bssmap_ho_command *rx; + GET_CONN(); + switch (event) { + + case MOHO_EV_BSSMAP_HO_COMMAND: + rx = data; + if (!rx) { + ho_fail(HO_RESULT_ERROR, + "Rx BSSMAP Handover Command: no L3 info passed with event"); + return; + } + + LOG_HO(conn, LOGL_DEBUG, "Rx BSSMAP Handover Command: forwarding Layer 3 Info: %s", + osmo_hexdump(rx->l3_info, rx->l3_info_len)); + + rc = rsl_forward_layer3_info(conn->lchan, rx->l3_info, rx->l3_info_len); + if (rc) { + ho_fail(HO_RESULT_ERROR, + "Rx BSSMAP Handover Command: Failed to forward Layer 3 Info (rc=%d %s)", + rc, strerror(-rc)); + return; + } + + ho_fsm_state_chg(MOHO_ST_WAIT_CLEAR); + return; + + default: + OSMO_ASSERT(false); + } +} + + +#define S(x) (1 << (x)) + +static const struct osmo_fsm_state ho_fsm_states[] = { + [HO_ST_NOT_STARTED] = { + .name = "NOT_STARTED", + .out_state_mask = 0 + | S(HO_ST_WAIT_LCHAN_ACTIVE) + | S(MOHO_ST_WAIT_HO_COMMAND) + , + }, + [HO_ST_WAIT_LCHAN_ACTIVE] = { + .name = "WAIT_LCHAN_ACTIVE", + .action = ho_fsm_wait_lchan_active, + .in_event_mask = 0 + | S(HO_EV_LCHAN_ACTIVE) + | S(HO_EV_LCHAN_ERROR) + , + .out_state_mask = 0 + | S(HO_ST_WAIT_LCHAN_ACTIVE) + | S(HO_ST_WAIT_RR_HO_DETECT) + , + }, + [HO_ST_WAIT_RR_HO_DETECT] = { + .name = "WAIT_RR_HO_DETECT", + .onenter = ho_fsm_wait_rr_ho_detect_onenter, + .action = ho_fsm_wait_rr_ho_detect, + .in_event_mask = 0 + | S(HO_EV_RR_HO_DETECT) + | S(HO_EV_RR_HO_COMPLETE) /* actually as error */ + | S(HO_EV_RR_HO_FAIL) + , + .out_state_mask = 0 + | S(HO_ST_WAIT_RR_HO_COMPLETE) + | S(HO_ST_WAIT_LCHAN_ESTABLISHED) + , + }, + [HO_ST_WAIT_RR_HO_COMPLETE] = { + .name = "WAIT_RR_HO_COMPLETE", + .action = ho_fsm_wait_rr_ho_complete, + .in_event_mask = 0 + | S(HO_EV_RR_HO_COMPLETE) + | S(HO_EV_RR_HO_FAIL) + , + .out_state_mask = 0 + | S(HO_ST_WAIT_LCHAN_ESTABLISHED) + , + }, + [HO_ST_WAIT_LCHAN_ESTABLISHED] = { + .name = "WAIT_LCHAN_ESTABLISHED", + .onenter = ho_fsm_wait_lchan_established_onenter, + .action = ho_fsm_wait_lchan_established, + .in_event_mask = 0 + | S(HO_EV_LCHAN_ESTABLISHED) + , + .out_state_mask = 0 + | S(HO_ST_WAIT_MGW_ENDPOINT_TO_MSC) + , + }, + [HO_ST_WAIT_MGW_ENDPOINT_TO_MSC] = { + .name = "WAIT_MGW_ENDPOINT_TO_MSC", + .onenter = ho_fsm_wait_mgw_endpoint_to_msc_onenter, + .action = ho_fsm_wait_mgw_endpoint_to_msc, + .in_event_mask = 0 + | S(HO_EV_MSC_MGW_OK) + | S(HO_EV_MSC_MGW_FAIL) + , + }, + + [MOHO_ST_WAIT_HO_COMMAND] = { + .name = "inter-BSC-MO:WAIT_HO_COMMAND", + .action = moho_fsm_wait_ho_command, + .in_event_mask = 0 + | S(MOHO_EV_BSSMAP_HO_COMMAND) + , + .out_state_mask = 0 + | S(MOHO_ST_WAIT_CLEAR) + , + }, + [MOHO_ST_WAIT_CLEAR] = { + .name = "inter-BSC-MO:WAIT_CLEAR", + }, +}; + +static const struct value_string ho_fsm_event_names[] = { + OSMO_VALUE_STRING(HO_EV_LCHAN_ACTIVE), + OSMO_VALUE_STRING(HO_EV_LCHAN_ESTABLISHED), + OSMO_VALUE_STRING(HO_EV_LCHAN_ERROR), + OSMO_VALUE_STRING(HO_EV_RR_HO_DETECT), + OSMO_VALUE_STRING(HO_EV_RR_HO_COMPLETE), + OSMO_VALUE_STRING(HO_EV_RR_HO_FAIL), + OSMO_VALUE_STRING(HO_EV_MSC_MGW_OK), + OSMO_VALUE_STRING(HO_EV_MSC_MGW_FAIL), + OSMO_VALUE_STRING(HO_EV_CONN_RELEASING), + OSMO_VALUE_STRING(MOHO_EV_BSSMAP_HO_COMMAND), + {} +}; + +void ho_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_CONN(); + switch (event) { + + case HO_EV_CONN_RELEASING: + switch (fi->state) { + case MOHO_ST_WAIT_CLEAR: + ho_success(); + return; + default: + ho_fail(HO_RESULT_CONN_RELEASE, + "Connection releasing in the middle of handover"); + return; + } + + case HO_EV_LCHAN_ERROR: + switch (fi->state) { + case MOHO_ST_WAIT_HO_COMMAND: + case MOHO_ST_WAIT_CLEAR: + LOG_HO(conn, LOGL_ERROR, "Event not permitted: %s", + osmo_fsm_event_name(fi->fsm, event)); + return; + + default: + ho_fail(HO_RESULT_ERROR, "Error while establishing lchan %s", + gsm_lchan_name(data)); + return; + } + + default: + OSMO_ASSERT(false); + } +} + +int ho_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + GET_CONN(); + ho_fail(HO_RESULT_FAIL_TIMEOUT, "Timeout"); + return 0; +} + +void ho_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) +{ + GET_CONN(); + conn->ho.fi = NULL; +} + +static struct osmo_fsm ho_fsm = { + .name = "handover", + .states = ho_fsm_states, + .num_states = ARRAY_SIZE(ho_fsm_states), + .log_subsys = DRSL, + .event_names = ho_fsm_event_names, + .allstate_action = ho_fsm_allstate_action, + .allstate_event_mask = 0 + | S(HO_EV_CONN_RELEASING) + | S(HO_EV_LCHAN_ERROR) + , + .timer_cb = ho_fsm_timer_cb, + .cleanup = ho_fsm_cleanup, +}; diff --git a/src/osmo-bsc/handover_logic.c b/src/osmo-bsc/handover_logic.c index 55af0ed50..0c6f29252 100644 --- a/src/osmo-bsc/handover_logic.c +++ b/src/osmo-bsc/handover_logic.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include #include @@ -41,355 +41,88 @@ #include #include #include +#include +#include +#include +#include + +const struct value_string handover_scope_names[] = { + { HO_NO_HANDOVER, "HO-none" }, + { HO_INTRA_CELL, "AS" }, + { HO_INTRA_BSC, "HO-intraBSC" }, + { HO_INTER_BSC_MO, "HO-interBSC-MO" }, + { HO_INTER_BSC_MT, "HO-interBSC-MT" }, + { HO_SCOPE_ALL, "HO-any" }, + {} +}; + +const struct value_string handover_result_names[] = { + { HO_RESULT_OK, "Complete" }, + { HO_RESULT_FAIL_NO_CHANNEL, "Failure (no channel could be allocated)" }, + { HO_RESULT_FAIL_RR_HO_FAIL, "Failure (MS sent RR Handover Failure)" }, + { HO_RESULT_FAIL_TIMEOUT, "Failure (timeout)" }, + { HO_RESULT_CONN_RELEASE, "Connection released" }, + { HO_RESULT_ERROR, "Failure" }, + {} +}; -static LLIST_HEAD(bsc_handovers); static LLIST_HEAD(handover_decision_callbacks); -static void handover_free(struct bsc_handover *ho) -{ - osmo_timer_del(&ho->T3103); - llist_del(&ho->list); - talloc_free(ho); -} - -static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan) -{ - struct bsc_handover *ho; - - llist_for_each_entry(ho, &bsc_handovers, list) { - if (ho->new_lchan == new_lchan) - return ho; - } - - return NULL; -} - -static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan) -{ - struct bsc_handover *ho; - - llist_for_each_entry(ho, &bsc_handovers, list) { - if (ho->old_lchan == old_lchan) - return ho; - } - - return NULL; -} - -/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type. - * This is the main entry point for the actual handover algorithm, after the decision whether to initiate - * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */ -int bsc_handover_start(enum hodec_id from_hodec_id, struct gsm_lchan *old_lchan, struct gsm_bts *new_bts, - enum gsm_chan_t new_lchan_type) -{ - struct gsm_subscriber_connection *conn; - struct bsc_handover *ho; - static uint8_t ho_ref = 0; - bool do_assignment; - - OSMO_ASSERT(old_lchan); - - /* don't attempt multiple handovers for the same lchan at - * the same time */ - if (bsc_ho_by_old_lchan(old_lchan)) - return -EBUSY; - - conn = old_lchan->conn; - if (!conn) { - LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n"); - return -ENOSPC; - } - - if (!new_bts) - new_bts = old_lchan->ts->trx->bts; - OSMO_ASSERT(new_bts); - - do_assignment = (new_bts == old_lchan->ts->trx->bts); - - ho = talloc_zero(conn, struct bsc_handover); - if (!ho) { - LOGP(DHO, LOGL_FATAL, "Out of Memory\n"); - return -ENOMEM; - } - ho->from_hodec_id = from_hodec_id; - ho->old_lchan = old_lchan; - ho->new_bts = new_bts; - ho->new_lchan_type = new_lchan_type; - ho->ho_ref = ho_ref++; - ho->inter_cell = !do_assignment; - ho->async = true; - llist_add(&ho->list, &bsc_handovers); - - conn->ho = ho; - - DEBUGP(DHO, "(BTS %u trx %u ts %u lchan %u %s)->(BTS %u lchan %s) Initiating %s...\n", - old_lchan->ts->trx->bts->nr, - old_lchan->ts->trx->nr, - old_lchan->ts->nr, - old_lchan->nr, - gsm_pchan_name(old_lchan->ts->pchan), - new_bts->nr, - gsm_lchant_name(new_lchan_type), - do_assignment ? "Assignment" : "Handover"); - - return osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_HO_START, NULL); -} - -/*! Start actual handover. Call bsc_handover_start() instead; The only legal caller is the GSCON FSM in - * bsc_subscr_conn_fsm.c. */ -int bsc_handover_start_gscon(struct gsm_subscriber_connection *conn) -{ - int rc; - struct gsm_network *network = conn->network; - struct bsc_handover *ho = conn->ho; - struct gsm_lchan *old_lchan; - struct gsm_lchan *new_lchan; - - if (!ho) { - LOGP(DHO, LOGL_ERROR, "%s: Requested to start handover, but conn->ho is NULL\n", - bsc_subscr_name(conn->bsub)); - return -EINVAL; - } - - OSMO_ASSERT(ho->old_lchan && ho->new_bts); - - if (ho->old_lchan->conn != conn) { - LOGP(DHO, LOGL_ERROR, - "%s: Requested to start handover, but the lchan does not belong to this conn\n", - bsc_subscr_name(conn->bsub)); - return -EINVAL; - } - - rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]); - - ho->new_lchan = lchan_alloc(ho->new_bts, ho->new_lchan_type, 0); - if (!ho->new_lchan) { - LOGP(DHO, LOGL_NOTICE, "No free channel for %s\n", gsm_lchant_name(ho->new_lchan_type)); - rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_NO_CHANNEL]); - return -ENOSPC; - } - - LOGPHO(ho, LOGL_INFO, "Triggering %s\n", ho->inter_cell? "Handover" : "Assignment"); - - /* copy some parameters from old lchan */ - old_lchan = ho->old_lchan; - new_lchan = ho->new_lchan; - memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr)); - if (!ho->inter_cell) { - new_lchan->ms_power = old_lchan->ms_power; - new_lchan->rqd_ta = old_lchan->rqd_ta; - } else { - new_lchan->ms_power = - ms_pwr_ctl_lvl(ho->new_bts->band, ho->new_bts->ms_max_power); - /* FIXME: do we have a better idea of the timing advance? */ - //new_lchan->rqd_ta = old_lchan->rqd_ta; - } - new_lchan->bs_power = old_lchan->bs_power; - new_lchan->rsl_cmode = old_lchan->rsl_cmode; - new_lchan->tch_mode = old_lchan->tch_mode; - memcpy(&new_lchan->mr_ms_lv, &old_lchan->mr_ms_lv, sizeof(new_lchan->mr_ms_lv)); - memcpy(&new_lchan->mr_bts_lv, &old_lchan->mr_bts_lv, sizeof(new_lchan->mr_bts_lv)); - - new_lchan->conn = conn; - - rc = rsl_chan_activate_lchan(new_lchan, - ho->async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC, - ho->ho_ref); - if (rc < 0) { - LOGPHO(ho, LOGL_INFO, "%s Failure: activate lchan rc = %d\n", - ho->inter_cell? "Handover" : "Assignment", rc); - lchan_free(new_lchan); - ho->new_lchan = NULL; - bsc_clear_handover(conn, 0); - return rc; - } - - rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ); - /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */ - - return 0; -} - -/* clear any operation for this connection */ -void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan) -{ - struct bsc_handover *ho = conn->ho; - - if (!ho) - return; - - if (ho->new_lchan) { - ho->new_lchan->conn = NULL; - if (free_lchan) - lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END); - ho->new_lchan = NULL; - } - - handover_free(ho); - conn->ho = NULL; -} - -/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */ -static void ho_T3103_cb(void *_ho) -{ - struct bsc_handover *ho = _ho; - struct gsm_network *net = ho->new_lchan->ts->trx->bts->network; - - DEBUGP(DHO, "HO T3103 expired\n"); - rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_TIMEOUT]); - - /* Inform the GSCON FSM about the timed out handover */ - osmo_fsm_inst_dispatch(ho->old_lchan->conn->fi, GSCON_EV_HO_TIMEOUT, NULL); - - bsc_clear_handover(ho->old_lchan->conn, 1); -} - -/* RSL has acknowledged activation of the new lchan */ -static int ho_chan_activ_ack(struct gsm_lchan *new_lchan) +void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc) { - struct bsc_handover *ho; - - /* we need to check if this channel activation is related to - * a handover at all (and if, which particular handover) */ - ho = bsc_ho_by_new_lchan(new_lchan); - if (!ho) - return -ENODEV; - - LOGPHO(ho, LOGL_INFO, "Channel Activate Ack, send %s COMMAND\n", ho->inter_cell? "HANDOVER" : "ASSIGNMENT"); - - /* we can now send the 04.08 HANDOVER COMMAND to the MS - * using the old lchan */ - - gsm48_send_ho_cmd(ho->old_lchan, new_lchan, new_lchan->ms_power, ho->ho_ref); - - /* start T3103. We can continue either with T3103 expiration, - * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */ - osmo_timer_setup(&ho->T3103, ho_T3103_cb, ho); - osmo_timer_schedule(&ho->T3103, 10, 0); - - /* create a RTP connection */ - if (is_ipaccess_bts(new_lchan->ts->trx->bts)) - rsl_ipacc_crcx(new_lchan); - - return 0; + llist_add_tail(&hdc->entry, &handover_decision_callbacks); } -/* RSL has not acknowledged activation of the new lchan */ -static int ho_chan_activ_nack(struct gsm_lchan *new_lchan) +struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id) { - struct bsc_handover *ho; struct handover_decision_callbacks *hdc; - - ho = bsc_ho_by_new_lchan(new_lchan); - if (!ho) { - /* This lchan is not involved in a handover. */ - return 0; - } - - hdc = handover_decision_callbacks_get(ho->from_hodec_id); - if (hdc && hdc->on_ho_chan_activ_nack) - hdc->on_ho_chan_activ_nack(ho); - - bsc_clear_handover(new_lchan->conn, 0); - return 0; -} - -/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */ -static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan) -{ - struct gsm_network *net; - struct bsc_handover *ho; - - ho = bsc_ho_by_new_lchan(new_lchan); - if (!ho) { - LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); - return -ENODEV; + llist_for_each_entry(hdc, &handover_decision_callbacks, entry) { + if (hdc->hodec_id == hodec_id) + return hdc; } - - net = new_lchan->ts->trx->bts->network; - - LOGPHO(ho, LOGL_INFO, "%s Complete\n", ho->inter_cell ? "Handover" : "Assignment"); - - rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_COMPLETED]); - - osmo_timer_del(&ho->T3103); - - /* Replace the ho lchan with the primary one */ - if (ho->old_lchan != new_lchan->conn->lchan) - LOGPHO(ho, LOGL_ERROR, "Primary lchan changed during handover.\n"); - - if (new_lchan->conn->ho != ho) - LOGPHO(ho, LOGL_ERROR, "Handover channel changed during this handover.\n"); - - new_lchan->conn->lchan = new_lchan; - ho->old_lchan->conn = NULL; - - lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END); - - handover_free(ho); - new_lchan->conn->ho = NULL; - - /* Inform the GSCON FSM that the handover is complete */ - osmo_fsm_inst_dispatch(new_lchan->conn->fi, GSCON_EV_HO_COMPL, NULL); - return 0; + return NULL; } -/* GSM 04.08 HANDOVER FAIL has been received */ -static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan) +static void ho_meas_rep(struct gsm_meas_rep *mr) { - struct gsm_network *net = old_lchan->ts->trx->bts->network; - struct bsc_handover *ho; struct handover_decision_callbacks *hdc; + enum hodec_id hodec_id = ho_get_algorithm(mr->lchan->ts->trx->bts->ho); - ho = bsc_ho_by_old_lchan(old_lchan); - if (!ho) { - LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); - return -ENODEV; - } - - hdc = handover_decision_callbacks_get(ho->from_hodec_id); - if (hdc && hdc->on_ho_failure) - hdc->on_ho_failure(ho); - - rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_FAILED]); - - bsc_clear_handover(ho->new_lchan->conn, 1); - - /* Inform the GSCON FSM that the handover failed */ - osmo_fsm_inst_dispatch(old_lchan->conn->fi, GSCON_EV_HO_FAIL, NULL); - return 0; + hdc = handover_decision_callbacks_get(hodec_id); + if (!hdc || !hdc->on_measurement_report) + return; + hdc->on_measurement_report(mr); } -/* GSM 08.58 HANDOVER DETECT has been received */ -static int ho_rsl_detect(struct gsm_lchan *new_lchan) +/* Count ongoing handovers within the given BTS. + * ho_scopes is an OR'd combination of enum handover_scope values to include in the count. */ +int handover_count(struct gsm_bts *bts, int ho_scopes) { - struct bsc_handover *ho; + struct gsm_bts_trx *trx; + int count = 0; - ho = bsc_ho_by_new_lchan(new_lchan); - if (!ho) { - LOGP(DHO, LOGL_ERROR, "unable to find HO record\n"); - return -ENODEV; + llist_for_each_entry(trx, &bts->trx_list, list) { + int i; + for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { + struct gsm_bts_trx_ts *ts = &trx->ts[i]; + struct gsm_lchan *lchan; + + /* skip administratively deactivated timeslots */ + if (!nm_is_running(&ts->mo.nm_state)) + continue; + + ts_for_each_lchan(lchan, ts) { + if (!lchan->conn) + continue; + if (!lchan->conn->ho.fi) + continue; + if (lchan->conn->ho.scope & ho_scopes) + count++; + } + } } - LOGPHO(ho, LOGL_DEBUG, "Handover RACH detected\n"); - - /* This is just for logging on the DHO category. The actual MGCP switchover happens in - * osmo_bsc_mgcp.c by receiving the same S_LCHAN_HANDOVER_DETECT signal. - * (Calling mgcp_handover() directly currently breaks linking in utils/...) */ - - return 0; -} - -static int ho_meas_rep(struct gsm_meas_rep *mr) -{ - struct handover_decision_callbacks *hdc; - enum hodec_id hodec_id = ho_get_algorithm(mr->lchan->ts->trx->bts->ho); - - hdc = handover_decision_callbacks_get(hodec_id); - if (!hdc || !hdc->on_measurement_report) - return 0; - hdc->on_measurement_report(mr); - return 0; + return count; } struct gsm_bts *bts_by_neighbor_ident(const struct gsm_network *net, @@ -445,73 +178,23 @@ static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal, lchan_data = signal_data; switch (subsys) { case SS_LCHAN: + OSMO_ASSERT(lchan_data); lchan = lchan_data->lchan; + OSMO_ASSERT(lchan); + switch (signal) { - case S_LCHAN_ACTIVATE_ACK: - return ho_chan_activ_ack(lchan); - case S_LCHAN_ACTIVATE_NACK: - return ho_chan_activ_nack(lchan); - case S_LCHAN_HANDOVER_DETECT: - return ho_rsl_detect(lchan); - case S_LCHAN_HANDOVER_COMPL: - return ho_gsm48_ho_compl(lchan); - case S_LCHAN_HANDOVER_FAIL: - return ho_gsm48_ho_fail(lchan); case S_LCHAN_MEAS_REP: - return ho_meas_rep(lchan_data->mr); + ho_meas_rep(lchan_data->mr); + break; } - break; + default: break; } - return 0; } -/* Return the old lchan or NULL. This is meant for audio handling */ -struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan) -{ - struct bsc_handover *ho; - ho = bsc_ho_by_new_lchan(new_lchan); - if (!ho) - return NULL; - return ho->old_lchan; -} - static __attribute__((constructor)) void on_dso_load_ho_logic(void) { osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL); } - -/* Count number of currently ongoing handovers - * inter_cell: if true, count only handovers between two cells. If false, count only handovers within one - * cell. */ -int bsc_ho_count(struct gsm_bts *bts, bool inter_cell) -{ - struct bsc_handover *ho; - int count = 0; - - llist_for_each_entry(ho, &bsc_handovers, list) { - if (ho->inter_cell != inter_cell) - continue; - if (ho->new_lchan->ts->trx->bts == bts) - count++; - } - - return count; -} - -void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc) -{ - llist_add_tail(&hdc->entry, &handover_decision_callbacks); -} - -struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id) -{ - struct handover_decision_callbacks *hdc; - llist_for_each_entry(hdc, &handover_decision_callbacks, entry) { - if (hdc->hodec_id == hodec_id) - return hdc; - } - return NULL; -} diff --git a/src/osmo-bsc/lchan_fsm.c b/src/osmo-bsc/lchan_fsm.c new file mode 100644 index 000000000..83f5b7e37 --- /dev/null +++ b/src/osmo-bsc/lchan_fsm.c @@ -0,0 +1,1467 @@ +/* osmo-bsc API to allocate an lchan, complete with dyn TS switchover and MGCP communication to allocate + * RTP endpoints. + * + * (C) 2018 by sysmocom - s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 . + * + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct osmo_fsm lchan_fsm; + +#define GET_LCHAN() \ + struct gsm_lchan *lchan = fi->priv; \ + OSMO_ASSERT((fi)->fsm == &lchan_fsm); \ + OSMO_ASSERT((fi)->priv); \ + OSMO_ASSERT(lchan->fi == (fi)) + +bool lchan_may_receive_data(struct gsm_lchan *lchan) +{ + if (!lchan || !lchan->fi) + return false; + + switch (lchan->fi->state) { + case LCHAN_ST_WAIT_RLL_ESTABLISH: + case LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE: + case LCHAN_ST_WAIT_IPACC_CRCX_ACK: + case LCHAN_ST_WAIT_IPACC_MDCX_ACK: + case LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED: + case LCHAN_ST_ESTABLISHED: + return true; + default: + return false; + } +} + +static void lchan_set_last_error(struct gsm_lchan *lchan, const char *fmt, ...) +{ + va_list ap; + /* This dance allows using an existing error reason in above fmt */ + char *last_error_was = lchan->last_error; + lchan->last_error = NULL; + + if (fmt) { + va_start(ap, fmt); + lchan->last_error = talloc_vasprintf(lchan->ts->trx, fmt, ap); + va_end(ap); + + LOG_LCHAN(lchan, LOGL_ERROR, "%s", lchan->last_error); + } + + if (last_error_was) + talloc_free(last_error_was); +} + +/* The idea here is that we must not require to change any lchan state in order to deny a request. */ +#define lchan_on_activation_failure(lchan, for_conn, activ_for) \ + _lchan_on_activation_failure(lchan, for_conn, activ_for, \ + __FILE__, __LINE__) +static void _lchan_on_activation_failure(struct gsm_lchan *lchan, enum lchan_activate_mode activ_for, + struct gsm_subscriber_connection *for_conn, + const char *file, int line) +{ + switch (activ_for) { + + case FOR_MS_CHANNEL_REQUEST: + LOG_LCHAN(lchan, LOGL_NOTICE, "Tx Immediate Assignment Reject (%s)", + lchan->last_error ? : "unknown error"); + rsl_tx_imm_ass_rej(lchan->ts->trx->bts, lchan->rqd_ref); + break; + + case FOR_ASSIGNMENT: + LOG_LCHAN(lchan, LOGL_NOTICE, "Signalling Assignment FSM of error (%s)", + lchan->last_error ? : "unknown error"); + _osmo_fsm_inst_dispatch(for_conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ERROR, lchan, + file, line); + return; + + case FOR_HANDOVER: + LOG_LCHAN(lchan, LOGL_NOTICE, "Signalling Handover FSM of error (%s)", + lchan->last_error ? : "unknown error"); + if (!for_conn) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for Handover failed, but activation request has" + " no conn"); + break; + } + if (!for_conn->ho.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for Handover failed, but conn has no ongoing" + " handover procedure"); + break; + } + _osmo_fsm_inst_dispatch(for_conn->ho.fi, HO_EV_LCHAN_ERROR, lchan, file, line); + break; + + case FOR_VTY: + LOG_LCHAN(lchan, LOGL_ERROR, "VTY user invoked lchan activation failed (%s)", + lchan->last_error ? : "unknown error"); + break; + + default: + LOG_LCHAN(lchan, LOGL_ERROR, "lchan activation failed (%s)", + lchan->last_error ? : "unknown error"); + break; + } +} + +static void lchan_on_activation_success(struct gsm_lchan *lchan) +{ + switch (lchan->activate.activ_for) { + case FOR_MS_CHANNEL_REQUEST: + /* Nothing to do here, MS is free to use the channel. */ + break; + + case FOR_ASSIGNMENT: + if (!lchan->conn) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for assignment succeeded, but lchan has no conn:" + " cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + if (!lchan->conn->assignment.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for assignment succeeded, but lchan has no" + " assignment ongoing: cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + osmo_fsm_inst_dispatch(lchan->conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ESTABLISHED, + lchan); + break; + + case FOR_HANDOVER: + if (!lchan->conn) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for handover succeeded, but lchan has no conn"); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + if (!lchan->conn->ho.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for handover succeeded, but lchan has no" + " handover ongoing"); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + osmo_fsm_inst_dispatch(lchan->conn->ho.fi, HO_EV_LCHAN_ESTABLISHED, lchan); + break; + + default: + LOG_LCHAN(lchan, LOGL_NOTICE, "lchan %s fully established", + lchan_activate_mode_name(lchan->activate.activ_for)); + break; + } +} + +struct state_timeout lchan_fsm_timeouts[32] = { + [LCHAN_ST_WAIT_TS_READY] = { .T=23001 }, + [LCHAN_ST_WAIT_ACTIV_ACK] = { .T=23002 }, + [LCHAN_ST_WAIT_RLL_ESTABLISH] = { .T=3101 }, + [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = { .T=23004 }, + [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = { .T=23005 }, + [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = { .T=23006 }, + [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = { .T=23004 }, + [LCHAN_ST_WAIT_SAPIS_RELEASED] = { .T=3109 }, + [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = { .T=3111 }, + [LCHAN_ST_WAIT_RF_RELEASE_ACK] = { .T=3111 }, + [LCHAN_ST_WAIT_AFTER_ERROR] = { .T=993111 }, +}; + +/* Transition to a state, using the T timer defined in lchan_fsm_timeouts. + * The actual timeout value is in turn obtained from network->T_defs. + * Assumes local variable fi exists. */ +#define lchan_fsm_state_chg(state) \ + fsm_inst_state_chg_T(fi, state, \ + lchan_fsm_timeouts, \ + ((struct gsm_lchan*)(fi->priv))->ts->trx->bts->network->T_defs, \ + 5) + +/* Set a failure message, trigger the common actions to take on failure, transition to a state to + * continue with (using state timeouts from lchan_fsm_timeouts[]). Assumes local variable fi exists. */ +#define lchan_fail_to(state_chg, fmt, args...) do { \ + struct gsm_lchan *_lchan = fi->priv; \ + uint32_t state_was = fi->state; \ + lchan_set_last_error(fi->priv, "lchan %s in state %s: " fmt, \ + _lchan->activate.concluded ? "failure" : "allocation failed", \ + osmo_fsm_state_name(fi->fsm, state_was), ## args); \ + if (!_lchan->activate.concluded) \ + lchan_on_activation_failure(_lchan, _lchan->activate.activ_for, _lchan->conn); \ + _lchan->activate.concluded = true; \ + lchan_fsm_state_chg(state_chg); \ + } while(0) + +/* Which state to transition to when lchan_fail() is called in a given state. */ +uint32_t lchan_fsm_on_error[32] = { + [LCHAN_ST_UNUSED] = LCHAN_ST_UNUSED, + [LCHAN_ST_WAIT_TS_READY] = LCHAN_ST_UNUSED, + [LCHAN_ST_WAIT_ACTIV_ACK] = LCHAN_ST_BORKEN, + [LCHAN_ST_WAIT_RLL_ESTABLISH] = LCHAN_ST_WAIT_RF_RELEASE_ACK, + [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = LCHAN_ST_WAIT_SAPIS_RELEASED, + [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = LCHAN_ST_WAIT_SAPIS_RELEASED, + [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = LCHAN_ST_WAIT_SAPIS_RELEASED, + [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = LCHAN_ST_WAIT_SAPIS_RELEASED, + [LCHAN_ST_ESTABLISHED] = LCHAN_ST_WAIT_SAPIS_RELEASED, + [LCHAN_ST_WAIT_SAPIS_RELEASED] = LCHAN_ST_WAIT_RF_RELEASE_ACK, + [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = LCHAN_ST_WAIT_RF_RELEASE_ACK, + [LCHAN_ST_WAIT_RF_RELEASE_ACK] = LCHAN_ST_BORKEN, + [LCHAN_ST_WAIT_AFTER_ERROR] = LCHAN_ST_UNUSED, + [LCHAN_ST_BORKEN] = LCHAN_ST_BORKEN, +}; + +#define lchan_fail(fmt, args...) lchan_fail_to(lchan_fsm_on_error[fi->state], fmt, ## args) + +void lchan_activate(struct gsm_lchan *lchan, struct lchan_activate_info *info) +{ + int rc; + + OSMO_ASSERT(lchan && info); + + if (!lchan_state_is(lchan, LCHAN_ST_UNUSED)) + goto abort; + + /* ensure some basic sanity up first, before we enter the machine. */ + OSMO_ASSERT(lchan->ts && lchan->ts->fi && lchan->fi); + + switch (info->activ_for) { + + case FOR_ASSIGNMENT: + if (!info->for_conn + || !info->for_conn->fi) { + LOG_LCHAN(lchan, LOGL_ERROR, "Activation requested, but no conn"); + goto abort; + } + if (info->for_conn->assignment.new_lchan != lchan) { + LOG_LCHAN(lchan, LOGL_ERROR, + "Activation for Assignment requested, but conn's state does" + " not reflect this lchan to be activated (instead: %s)", + info->for_conn->assignment.new_lchan? + gsm_lchan_name(info->for_conn->assignment.new_lchan) + : "NULL"); + goto abort; + } + break; + + case FOR_HANDOVER: + if (!info->for_conn + || !info->for_conn->fi) { + LOG_LCHAN(lchan, LOGL_ERROR, "Activation requested, but no conn"); + goto abort; + } + if (!info->for_conn->ho.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "Activation for Handover requested, but conn has no HO pending."); + goto abort; + } + if (info->for_conn->ho.mt.new_lchan != lchan) { + LOG_LCHAN(lchan, LOGL_ERROR, + "Activation for Handover requested, but conn's HO state does" + " not reflect this lchan to be activated (instead: %s)", + info->for_conn->ho.mt.new_lchan? + gsm_lchan_name(info->for_conn->ho.mt.new_lchan) + : "NULL"); + goto abort; + } + break; + + default: + break; + } + + /* To make sure that the lchan is actually allowed to initiate an activation, feed through an FSM + * event. */ + rc = osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_ACTIVATE, info); + + if (rc) { + LOG_LCHAN(lchan, LOGL_ERROR, + "Activation requested, but cannot dispatch LCHAN_EV_ACTIVATE event"); + goto abort; + } + return; + +abort: + lchan_on_activation_failure(lchan, info->activ_for, info->for_conn); + /* Remain in state UNUSED */ +} + +static void lchan_fsm_update_id(struct gsm_lchan *lchan) +{ + osmo_fsm_inst_update_id_f(lchan->fi, "%u-%u-%u-%s-%u", + lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, + gsm_pchan_id(lchan->ts->pchan_on_init), lchan->nr); +} + +void lchan_fsm_alloc(struct gsm_lchan *lchan) +{ + static bool g_initialized = false; + if (!g_initialized) { + OSMO_ASSERT(osmo_fsm_register(&lchan_fsm) == 0); + g_initialized = true; + } + OSMO_ASSERT(lchan->ts); + OSMO_ASSERT(lchan->ts->fi); + OSMO_ASSERT(!lchan->fi); + + lchan->fi = osmo_fsm_inst_alloc_child(&lchan_fsm, lchan->ts->fi, TS_EV_LCHAN_UNUSED); + OSMO_ASSERT(lchan->fi); + lchan->fi->priv = lchan; + lchan_fsm_update_id(lchan); + LOGPFSML(lchan->fi, LOGL_DEBUG, "new lchan\n"); +} + +/* Clear volatile state of the lchan. Clear all except + * - the ts backpointer, + * - the nr, + * - name, + * - the FSM instance including its current state, + * - last_error string. + */ +static void lchan_reset(struct gsm_lchan *lchan) +{ + if (lchan->rqd_ref) { + talloc_free(lchan->rqd_ref); + lchan->rqd_ref = NULL; + } + if (lchan->mgw_endpoint_ci_bts) { + mgw_endpoint_ci_dlcx(lchan->mgw_endpoint_ci_bts); + lchan->mgw_endpoint_ci_bts = NULL; + } + + /* NUL all volatile state */ + *lchan = (struct gsm_lchan){ + .ts = lchan->ts, + .nr = lchan->nr, + .fi = lchan->fi, + .name = lchan->name, + + .meas_rep_last_seen_nr = 255, + + .last_error = lchan->last_error, + }; +} + +static void lchan_fsm_unused_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_LCHAN(); + lchan_reset(lchan); + osmo_fsm_inst_dispatch(lchan->ts->fi, TS_EV_LCHAN_UNUSED, lchan); +} + +static void lchan_fsm_unused(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct lchan_activate_info *info = data; + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_ACTIVATE: + OSMO_ASSERT(info); + OSMO_ASSERT(!lchan->conn); + OSMO_ASSERT(!lchan->mgw_endpoint_ci_bts); + lchan_set_last_error(lchan, NULL); + lchan->release_requested = false; + + lchan->conn = info->for_conn; + lchan->activate.activ_for = info->activ_for; + lchan->activate.requires_voice_stream = info->requires_voice_stream; + lchan->activate.concluded = false; + + if (info->old_lchan) { + lchan->mgw_endpoint_ci_bts = info->old_lchan->mgw_endpoint_ci_bts; + /* TODO: rather take info->for_conn->encr? */ + lchan->encr = info->old_lchan->encr; + lchan->ms_power = info->old_lchan->ms_power; + lchan->bs_power = info->old_lchan->bs_power; + lchan->rqd_ta = info->old_lchan->rqd_ta; + } else { + struct gsm_bts *bts = lchan->ts->trx->bts; + /* TODO: rather take info->for_conn->encr? */ + lchan->encr = (struct gsm_encr){ + .alg_id = RSL_ENC_ALG_A5(0), /* no encryption */ + }; + lchan->ms_power = ms_pwr_ctl_lvl(bts->band, bts->ms_max_power); + lchan->bs_power = 0; /* 0dB reduction, output power = Pn */ + } + + switch (info->chan_mode) { + + case GSM48_CMODE_SIGN: + lchan->rsl_cmode = RSL_CMOD_SPD_SIGN; + lchan->tch_mode = GSM48_CMODE_SIGN; + break; + + case GSM48_CMODE_SPEECH_V1: + case GSM48_CMODE_SPEECH_EFR: + case GSM48_CMODE_SPEECH_AMR: + lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH; + lchan->tch_mode = info->chan_mode; + break; + + default: + lchan_fail("Not implemented: cannot activate for chan mode %s", + gsm48_chan_mode_name(info->chan_mode)); + return; + } + + lchan_fsm_state_chg(LCHAN_ST_WAIT_TS_READY); + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_wait_ts_ready_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct mgw_endpoint *mgwep; + struct mgcp_conn_peer crcx_info = {}; + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + LOG_LCHAN(lchan, LOGL_INFO, + "Activation requested: %s voice=%s MGW-ci=%s type=%s tch-mode=%s", + lchan_activate_mode_name(lchan->activate.activ_for), + lchan->activate.requires_voice_stream ? "yes" : "no", + lchan->activate.requires_voice_stream ? + (lchan->mgw_endpoint_ci_bts ? + mgwep_ci_name(lchan->mgw_endpoint_ci_bts) : "new") + : "none", + gsm_lchant_name(lchan->type), + gsm48_chan_mode_name(lchan->tch_mode)); + + /* Ask for the timeslot to make ready for this lchan->type. + * We'll receive LCHAN_EV_TS_READY or LCHAN_EV_TS_ERROR in response. */ + osmo_fsm_inst_dispatch(lchan->ts->fi, TS_EV_LCHAN_REQUESTED, lchan); + + /* Prepare an MGW endpoint CI if appropriate. */ + if (!lchan->activate.requires_voice_stream) + return; + + if (lchan->mgw_endpoint_ci_bts) { + lchan->activate.mgw_endpoint_available = true; + return; + } + + mgwep = gscon_ensure_mgw_endpoint(lchan->conn); + if (!mgwep) { + lchan_fail("Internal error: cannot obtain MGW endpoint handle for conn"); + return; + } + + lchan->mgw_endpoint_ci_bts = mgw_endpoint_ci_add(mgwep, "to-BTS"); + + if (lchan->conn) + crcx_info.call_id = lchan->conn->sccp.conn_id; + crcx_info.ptime = 20; + mgcp_pick_codec(&crcx_info, lchan); + + mgw_endpoint_ci_request(lchan->mgw_endpoint_ci_bts, + MGCP_VERB_CRCX, &crcx_info, + lchan->fi, + LCHAN_EV_MGW_ENDPOINT_AVAILABLE, + LCHAN_EV_MGW_ENDPOINT_ERROR, 0); +} + +static void lchan_fsm_wait_ts_ready(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_TS_READY: + /* timeslot agrees that we may Chan Activ now. Sending it in onenter. */ + lchan_fsm_state_chg(LCHAN_ST_WAIT_ACTIV_ACK); + break; + + case LCHAN_EV_MGW_ENDPOINT_AVAILABLE: + /* conn FSM is already done preparing an MGW endpoint. Remember that. */ + lchan->activate.mgw_endpoint_available = true; + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_wait_activ_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + uint8_t act_type; + uint8_t ho_ref = 0; + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail_to(LCHAN_ST_UNUSED, "Release requested while activating"); + return; + } + + switch (lchan->activate.activ_for) { + case FOR_MS_CHANNEL_REQUEST: + act_type = RSL_ACT_INTRA_IMM_ASS; + break; + case FOR_HANDOVER: + act_type = lchan->conn->ho.mt.async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC; + ho_ref = lchan->conn->ho.mt.ho_ref; + break; + default: + case FOR_ASSIGNMENT: + act_type = RSL_ACT_INTRA_NORM_ASS; + break; + } + + rc = rsl_tx_chan_activ(lchan, act_type, ho_ref); + if (rc) + lchan_fail_to(LCHAN_ST_UNUSED, "Tx Chan Activ failed: %s (%d)", strerror(-rc), rc); +} + +static void lchan_fsm_wait_activ_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_MGW_ENDPOINT_AVAILABLE: + lchan->activate.mgw_endpoint_available = true; + break; + + case LCHAN_EV_RSL_CHAN_ACTIV_ACK: + /* Chan Activ was ack'd, but we need an RLL Establish to be sure it's working out. */ + lchan_fsm_state_chg(LCHAN_ST_WAIT_RLL_ESTABLISH); + break; + + case LCHAN_EV_RSL_CHAN_ACTIV_NACK: + if (data) { + uint32_t next_state; + lchan->error_cause = *(uint8_t*)data; + lchan->release_in_error = true; + if (lchan->error_cause != RSL_ERR_RCH_ALR_ACTV_ALLOC) + next_state = LCHAN_ST_BORKEN; + else + /* Taking this over from legacy code: send an RF Chan Release even though + * the Activ was NACKed. Is this really correct? */ + next_state = LCHAN_ST_WAIT_RF_RELEASE_ACK; + + lchan_fail_to(next_state, "Chan Activ NACK: %s (0x%x)", + rsl_err_name(lchan->error_cause), lchan->error_cause); + } else { + lchan->error_cause = RSL_ERR_IE_NONEXIST; + lchan->release_in_error = true; + lchan_fail_to(LCHAN_ST_BORKEN, "Chan Activ NACK without cause IE"); + } + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_wait_rll_establish_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_LCHAN(); + if (lchan->release_requested) { + lchan_fail_to(LCHAN_ST_WAIT_RF_RELEASE_ACK, "Release requested while activating"); + return; + } + + switch (lchan->activate.activ_for) { + + case FOR_MS_CHANNEL_REQUEST: + rc = rsl_tx_imm_assignment(lchan); + if (rc) { + lchan_fail("Failed to Tx RR Immediate Assignment message (rc=%d %s)", + rc, strerror(-rc)); + return; + } + LOG_LCHAN(lchan, LOGL_DEBUG, "Tx RR Immediate Assignment"); + break; + + case FOR_ASSIGNMENT: + if (!lchan->conn) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for assignment succeeded, but lchan has no conn:" + " cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + if (!lchan->conn->assignment.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for assignment succeeded, but lchan has no" + " assignment ongoing: cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + /* After the Chan Activ Ack, the MS expects to receive an RR Assignment Command. + * Let the assignment_fsm handle that. */ + osmo_fsm_inst_dispatch(lchan->conn->assignment.fi, ASSIGNMENT_EV_LCHAN_ACTIVE, lchan); + break; + + case FOR_HANDOVER: + if (!lchan->conn) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for handover succeeded, but lchan has no conn:" + " cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + if (!lchan->conn->ho.fi) { + LOG_LCHAN(lchan, LOGL_ERROR, + "lchan activation for handover succeeded, but lchan has no" + " handover ongoing: cannot trigger appropriate actions. Release."); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + break; + } + /* After the Chan Activ Ack of the new lchan, send the MS an RR Handover Command on the + * old channel. The handover_fsm handles that. */ + osmo_fsm_inst_dispatch(lchan->conn->ho.fi, HO_EV_LCHAN_ACTIVE, lchan); + break; + + default: + LOG_LCHAN(lchan, LOGL_NOTICE, "lchan %s is now active", + lchan_activate_mode_name(lchan->activate.activ_for)); + break; + } +} + +static void lchan_fsm_wait_rll_establish(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_MGW_ENDPOINT_AVAILABLE: + lchan->activate.mgw_endpoint_available = true; + break; + + case LCHAN_EV_RLL_ESTABLISH_IND: + lchan->sapis[0] = LCHAN_SAPI_MS; + if (lchan->activate.requires_voice_stream) { + /* For Abis/IP, we would technically only need the MGW endpoint one step later, + * on IPACC MDCX. But usually the MGW endpoint is anyway done by now, so keep one + * common endpoint wait state for all BTS types. */ + lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE); + } else + lchan_fsm_state_chg(LCHAN_ST_ESTABLISHED); + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_tch_post_endpoint_available(struct osmo_fsm_inst *fi); + +static void lchan_fsm_wait_mgw_endpoint_available_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + if (lchan->activate.mgw_endpoint_available) { + LOG_LCHAN(lchan, LOGL_DEBUG, "MGW endpoint already available"); + lchan_fsm_tch_post_endpoint_available(fi); + } +} + +static void lchan_fsm_wait_mgw_endpoint_available(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_MGW_ENDPOINT_AVAILABLE: + lchan->activate.mgw_endpoint_available = true; + lchan_fsm_tch_post_endpoint_available(fi); + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_tch_post_endpoint_available(struct osmo_fsm_inst *fi) +{ + GET_LCHAN(); + + LOG_LCHAN(lchan, LOGL_DEBUG, "MGW endpoint: %s", + mgcp_conn_peer_name(mgwep_ci_get_rtp_info(lchan->mgw_endpoint_ci_bts))); + + if (is_ipaccess_bts(lchan->ts->trx->bts)) + lchan_fsm_state_chg(LCHAN_ST_WAIT_IPACC_CRCX_ACK); + else + lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED); +} + +static void lchan_fsm_wait_ipacc_crcx_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + int val; + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + val = ipacc_speech_mode(lchan->tch_mode, lchan->type); + if (val < 0) { + lchan_fail("Cannot determine Abis/IP speech mode for tch_mode=%s type=%s\n", + get_value_string(gsm48_chan_mode_names, lchan->tch_mode), + gsm_lchant_name(lchan->type)); + return; + } + lchan->abis_ip.speech_mode = val; + + val = ipacc_payload_type(lchan->tch_mode, lchan->type); + if (val < 0) { + lchan_fail("Cannot determine Abis/IP payload type for tch_mode=%s type=%s\n", + get_value_string(gsm48_chan_mode_names, lchan->tch_mode), + gsm_lchant_name(lchan->type)); + return; + } + lchan->abis_ip.rtp_payload = val; + + /* recv-only */ + ipacc_speech_mode_set_direction(&lchan->abis_ip.speech_mode, false); + + rc = rsl_tx_ipacc_crcx(lchan); + if (rc) + lchan_fail("Failure to transmit IPACC CRCX to BTS (rc=%d, %s)", + rc, strerror(-rc)); +} + +static void lchan_fsm_wait_ipacc_crcx_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + + case LCHAN_EV_IPACC_CRCX_ACK: + /* the CRCX ACK parsing has already noted the RTP port information at + * lchan->abis_ip.bound_*, see ipac_parse_rtp(). We'll use that in + * lchan_fsm_wait_mgw_endpoint_configured_onenter(). */ + lchan_fsm_state_chg(LCHAN_ST_WAIT_IPACC_MDCX_ACK); + return; + + case LCHAN_EV_IPACC_CRCX_NACK: + lchan_fail("Received NACK on IPACC CRCX"); + return; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_wait_ipacc_mdcx_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_LCHAN(); + + uint32_t ip = lchan->abis_ip.bound_ip; + int port = lchan->abis_ip.bound_port; + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + if (!ip) { + lchan_fail("Cannot send IPACC MDCX to BTS:" + " there is no RTP IP set that the BTS should send RTP to."); + return; + } + + if (port <= 0 || port > 65535) { + lchan_fail("Cannot send IPACC MDCX to BTS:" + " invalid port number that the BTS should send RTP to: %d", port); + return; + } + + /* Other RTP settings were already setup in lchan_fsm_wait_ipacc_crcx_ack_onenter() */ + lchan->abis_ip.connect_ip = ip; + lchan->abis_ip.connect_port = port; + + /* send-recv */ + ipacc_speech_mode_set_direction(&lchan->abis_ip.speech_mode, true); + + rc = rsl_tx_ipacc_mdcx(lchan); + if (rc) + lchan_fail("Failure to transmit IPACC MDCX to BTS (rc=%d, %s)", + rc, strerror(-rc)); + +} + +static void lchan_fsm_wait_ipacc_mdcx_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + + case LCHAN_EV_IPACC_MDCX_ACK: + /* Finally, the lchan and its RTP are established. */ + lchan_fsm_state_chg(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED); + return; + + case LCHAN_EV_IPACC_MDCX_NACK: + lchan_fail("Received NACK on IPACC MDCX"); + return; + + default: + OSMO_ASSERT(false); + } +} + +/* Tell the MGW endpoint about the RTP port allocated on BTS side. */ +static void lchan_fsm_wait_mgw_endpoint_configured_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + struct mgcp_conn_peer mdcx_info; + struct in_addr addr; + const char *addr_str; + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + mdcx_info = (struct mgcp_conn_peer){ + .port = lchan->abis_ip.bound_port, + .ptime = 20, + }; + mgcp_pick_codec(&mdcx_info, lchan); + + addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip); + addr_str = inet_ntoa(addr); + rc = osmo_strlcpy(mdcx_info.addr, addr_str, sizeof(mdcx_info.addr)); + if (rc <= 0 || rc >= sizeof(mdcx_info.addr)) { + lchan_fail("Cannot compose BTS side RTP IP address to send to MGW: '%s'", + addr_str); + return; + } + + if (!lchan->mgw_endpoint_ci_bts) { + lchan_fail("No MGW endpoint ci configured"); + return; + } + + LOG_LCHAN(lchan, LOGL_DEBUG, "Sending BTS side RTP port info %s:%u to MGW %s", + mdcx_info.addr, mdcx_info.port, mgwep_ci_name(lchan->mgw_endpoint_ci_bts)); + mgw_endpoint_ci_request(lchan->mgw_endpoint_ci_bts, MGCP_VERB_MDCX, + &mdcx_info, fi, LCHAN_EV_MGW_ENDPOINT_CONFIGURED, + LCHAN_EV_MGW_ENDPOINT_ERROR, 0); +} + +static void lchan_fsm_wait_mgw_endpoint_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + + case LCHAN_EV_MGW_ENDPOINT_CONFIGURED: + lchan_fsm_state_chg(LCHAN_ST_ESTABLISHED); + return; + + case LCHAN_EV_MGW_ENDPOINT_ERROR: + lchan_fail("Error while redirecting the MGW to the BTS' RTP port"); + return; + + default: + OSMO_ASSERT(false); + } +} + + +static void lchan_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_LCHAN(); + + if (lchan->release_requested) { + lchan_fail("Release requested while activating"); + return; + } + + /* This flag ensures that when an lchan activation has succeeded, and we have already sent ACKs + * like Immediate Assignment or BSSMAP Assignment Complete, and if then, way later, some other + * error occurs, e.g. during release, that we don't send a NACK out of context. */ + lchan->activate.concluded = true; + lchan_on_activation_success(lchan); +} + +#define for_each_sapi(sapi, start, lchan) \ + for (sapi = start; sapi < ARRAY_SIZE(lchan->sapis); sapi++) + +static int next_active_sapi(struct gsm_lchan *lchan, int from_sapi) +{ + int sapi; + for_each_sapi(sapi, from_sapi, lchan) { + if (lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) + continue; + return sapi; + } + return sapi; +} + +#define for_each_active_sapi(sapi, start, lchan) \ + for (sapi = next_active_sapi(lchan, start); \ + sapi < ARRAY_SIZE(lchan->sapis); sapi=next_active_sapi(lchan, sapi+1)) + +static int lchan_active_sapis(struct gsm_lchan *lchan, int start) +{ + int sapis = 0; + int sapi; + for_each_active_sapi(sapi, start, lchan) { + LOG_LCHAN(lchan, LOGL_DEBUG, + "Still active: SAPI[%d] (%d)", sapi, lchan->sapis[sapi]); + sapis ++; + } + LOG_LCHAN(lchan, LOGL_DEBUG, "Still active SAPIs: %d", sapis); + return sapis; +} + +static void handle_rll_rel_ind_or_conf(struct osmo_fsm_inst *fi, uint32_t event, void *data, + bool wait_for_sapi0_rel) +{ + uint8_t link_id; + uint8_t sapi; + GET_LCHAN(); + + OSMO_ASSERT(data); + link_id = *(uint8_t*)data; + sapi = link_id & 7; + + LOG_LCHAN(lchan, LOGL_DEBUG, "Rx RLL Release %s: SAPI=%u link_id=0x%x", + event == LCHAN_EV_RLL_REL_CONF ? "CONF" : "IND", sapi, link_id); + + /* TODO this reflects the code state before the lchan FSM. However, it would make more sense to + * me that a Release IND is indeed a cue for us to send a Release Request, and not count it as an + * equal to Release CONF. */ + + lchan->sapis[sapi] = LCHAN_SAPI_UNUSED; + rll_indication(lchan, link_id, BSC_RLLR_IND_REL_IND); + + /* Releasing SAPI 0 means the conn becomes invalid; but not if the link_id contains a TCH flag. + * (TODO: is this the correct interpretation?) */ + if (lchan->conn && sapi == 0 && !(link_id & 0xc0)) { + LOG_LCHAN(lchan, LOGL_DEBUG, "lchan is releasing"); + gscon_lchan_releasing(lchan->conn, lchan); + } + + if (!lchan_active_sapis(lchan, wait_for_sapi0_rel? 0 : 1)) + lchan_fsm_state_chg(LCHAN_ST_WAIT_BEFORE_RF_RELEASE); +} + +static void lchan_fsm_established(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + handle_rll_rel_ind_or_conf(fi, event, data, true); +} + +static bool should_sacch_deact(struct gsm_lchan *lchan) +{ + switch (lchan->ts->pchan_is) { + case GSM_PCHAN_TCH_F: + case GSM_PCHAN_TCH_H: + case GSM_PCHAN_CCCH_SDCCH4: + case GSM_PCHAN_CCCH_SDCCH4_CBCH: + case GSM_PCHAN_SDCCH8_SACCH8C: + case GSM_PCHAN_SDCCH8_SACCH8C_CBCH: + return true; + default: + return false; + } +} + +static void lchan_fsm_wait_sapis_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int sapis; + int sapi; + GET_LCHAN(); + + for (sapi=0; sapi < ARRAY_SIZE(lchan->sapis); sapi++) + if (lchan->sapis[sapi]) + LOG_LCHAN(lchan, LOGL_DEBUG, "SAPI[%d] = %d", sapi, lchan->sapis[sapi]); + + if (lchan->conn) + gsm48_send_rr_release(lchan); + + if (lchan->deact_sacch && should_sacch_deact(lchan)) + rsl_deact_sacch(lchan); + + sapis = 0; + for_each_active_sapi(sapi, 1, lchan) { + uint8_t link_id = sapi; + + if (lchan->type == GSM_LCHAN_TCH_F || lchan->type == GSM_LCHAN_TCH_H) + link_id |= 0x40; + LOG_LCHAN(lchan, LOGL_DEBUG, "Tx: Release SAPI %u link_id 0x%x", sapi, link_id); + rsl_release_request(lchan, link_id, RSL_REL_LOCAL_END); + sapis ++; + } + + /* Do not wait for Nokia BTS to send the confirm. */ + if (is_nokia_bts(lchan->ts->trx->bts) + && lchan->ts->trx->bts->nokia.no_loc_rel_cnf) { + + LOG_LCHAN(lchan, LOGL_DEBUG, "Nokia InSite BTS: not waiting for RELease CONFirm"); + + for_each_active_sapi(sapi, 1, lchan) + lchan->sapis[sapi] = LCHAN_SAPI_UNUSED; + sapis = 0; + } + + if (!sapis) + lchan_fsm_state_chg(LCHAN_ST_WAIT_BEFORE_RF_RELEASE); +} + +static void lchan_fsm_wait_sapis_released(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + /* When we're telling the MS to release, we're fine to carry on with RF Channel Release when SAPI + * 0 release is not confirmed yet. + * TODO: that's how the code was before lchan FSM, is this correct/useful? */ + handle_rll_rel_ind_or_conf(fi, event, data, false); +} + +static void lchan_fsm_wait_rf_release_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_LCHAN(); + rc = rsl_tx_rf_chan_release(lchan); + if (rc) + LOG_LCHAN(lchan, LOGL_ERROR, "Failed to Tx RSL RF Channel Release: rc=%d %s\n", + rc, strerror(-rc)); +} + +static void lchan_fsm_wait_rf_release_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_RSL_RF_CHAN_REL_ACK: + if (lchan->error_cause) + lchan_fsm_state_chg(LCHAN_ST_WAIT_AFTER_ERROR); + else + lchan_fsm_state_chg(LCHAN_ST_UNUSED); + break; + + default: + OSMO_ASSERT(false); + } +} + +static void lchan_fsm_borken_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_LCHAN(); + lchan_reset(lchan); +} + +static void lchan_fsm_borken(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + switch (event) { + + case LCHAN_EV_RSL_CHAN_ACTIV_ACK: + /* A late Chan Activ ACK? Release. */ + lchan->release_in_error = true; + lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK); + return; + + case LCHAN_EV_RSL_CHAN_ACTIV_NACK: + /* A late Chan Activ NACK? Ok then, unused. */ + lchan_fsm_state_chg(LCHAN_ST_UNUSED); + return; + + case LCHAN_ST_WAIT_RF_RELEASE_ACK: + /* A late Release ACK? */ + lchan->release_in_error = true; + lchan_fsm_state_chg(LCHAN_ST_WAIT_AFTER_ERROR); + /* TODO: we used to do this only for sysmobts: + int do_free = is_sysmobts_v2(ts->trx->bts); + LOGP(DRSL, LOGL_NOTICE, + "%s CHAN REL ACK for broken channel. %s.\n", + gsm_lchan_name(lchan), + do_free ? "Releasing it" : "Keeping it broken"); + if (do_free) + do_lchan_free(lchan); + * Clarify the reason. If a BTS sends a RF Chan Rel ACK, we can consider it released, + * independently from the BTS model, right?? */ + return; + + default: + OSMO_ASSERT(false); + } +} + +#define S(x) (1 << (x)) + +static const struct osmo_fsm_state lchan_fsm_states[] = { + [LCHAN_ST_UNUSED] = { + .name = "UNUSED", + .onenter = lchan_fsm_unused_onenter, + .action = lchan_fsm_unused, + .in_event_mask = 0 + | S(LCHAN_EV_ACTIVATE) + , + .out_state_mask = 0 + | S(LCHAN_ST_WAIT_TS_READY) + , + }, + [LCHAN_ST_WAIT_TS_READY] = { + .name = "WAIT_TS_READY", + .onenter = lchan_fsm_wait_ts_ready_onenter, + .action = lchan_fsm_wait_ts_ready, + .in_event_mask = 0 + | S(LCHAN_EV_TS_READY) + | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_ACTIV_ACK) + , + }, + [LCHAN_ST_WAIT_ACTIV_ACK] = { + .name = "WAIT_ACTIV_ACK", + .onenter = lchan_fsm_wait_activ_ack_onenter, + .action = lchan_fsm_wait_activ_ack, + .in_event_mask = 0 + | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE) + | S(LCHAN_EV_RSL_CHAN_ACTIV_ACK) + | S(LCHAN_EV_RSL_CHAN_ACTIV_NACK) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_RLL_ESTABLISH) + | S(LCHAN_ST_BORKEN) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_RLL_ESTABLISH] = { + .name = "WAIT_RLL_ESTABLISH", + .onenter = lchan_fsm_wait_rll_establish_onenter, + .action = lchan_fsm_wait_rll_establish, + .in_event_mask = 0 + | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE) + | S(LCHAN_EV_RLL_ESTABLISH_IND) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE) + | S(LCHAN_ST_ESTABLISHED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + , + }, + [LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE] = { + .name = "WAIT_MGW_ENDPOINT_AVAILABLE", + .onenter = lchan_fsm_wait_mgw_endpoint_available_onenter, + .action = lchan_fsm_wait_mgw_endpoint_available, + .in_event_mask = 0 + | S(LCHAN_EV_MGW_ENDPOINT_AVAILABLE) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_IPACC_CRCX_ACK) + | S(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_IPACC_CRCX_ACK] = { + .name = "WAIT_IPACC_CRCX_ACK", + .onenter = lchan_fsm_wait_ipacc_crcx_ack_onenter, + .action = lchan_fsm_wait_ipacc_crcx_ack, + .in_event_mask = 0 + | S(LCHAN_EV_IPACC_CRCX_ACK) + | S(LCHAN_EV_IPACC_CRCX_NACK) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_IPACC_MDCX_ACK) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_IPACC_MDCX_ACK] = { + .name = "WAIT_IPACC_MDCX_ACK", + .onenter = lchan_fsm_wait_ipacc_mdcx_ack_onenter, + .action = lchan_fsm_wait_ipacc_mdcx_ack, + .in_event_mask = 0 + | S(LCHAN_EV_IPACC_MDCX_ACK) + | S(LCHAN_EV_IPACC_MDCX_NACK) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_MGW_ENDPOINT_CONFIGURED] = { + .name = "WAIT_MGW_ENDPOINT_CONFIGURED", + .onenter = lchan_fsm_wait_mgw_endpoint_configured_onenter, + .action = lchan_fsm_wait_mgw_endpoint_configured, + .in_event_mask = 0 + | S(LCHAN_EV_MGW_ENDPOINT_CONFIGURED) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_ESTABLISHED) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_ESTABLISHED] = { + .name = "ESTABLISHED", + .onenter = lchan_fsm_established_onenter, + .action = lchan_fsm_established, + .in_event_mask = 0 + | S(LCHAN_EV_RLL_REL_IND) + | S(LCHAN_EV_RLL_REL_CONF) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_SAPIS_RELEASED) + | S(LCHAN_ST_WAIT_BEFORE_RF_RELEASE) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_SAPIS_RELEASED] = { + .name = "WAIT_SAPIS_RELEASED", + .onenter = lchan_fsm_wait_sapis_released_onenter, + .action = lchan_fsm_wait_sapis_released, + .in_event_mask = 0 + | S(LCHAN_EV_RLL_REL_IND) + | S(LCHAN_EV_RLL_REL_CONF) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_BEFORE_RF_RELEASE) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_BEFORE_RF_RELEASE] = { + .name = "WAIT_BEFORE_RF_RELEASE", + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_RF_RELEASE_ACK) + , + }, + [LCHAN_ST_WAIT_RF_RELEASE_ACK] = { + .name = "WAIT_RF_RELEASE_ACK", + .onenter = lchan_fsm_wait_rf_release_ack_onenter, + .action = lchan_fsm_wait_rf_release_ack, + .in_event_mask = 0 + | S(LCHAN_EV_RSL_RF_CHAN_REL_ACK) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + | S(LCHAN_ST_WAIT_AFTER_ERROR) + | S(LCHAN_ST_BORKEN) + , + }, + [LCHAN_ST_WAIT_AFTER_ERROR] = { + .name = "WAIT_AFTER_ERROR", + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + , + }, + [LCHAN_ST_BORKEN] = { + .name = "BORKEN", + .onenter = lchan_fsm_borken_onenter, + .action = lchan_fsm_borken, + .in_event_mask = 0 + | S(LCHAN_EV_RSL_CHAN_ACTIV_ACK) + | S(LCHAN_EV_RSL_CHAN_ACTIV_NACK) + | S(LCHAN_EV_RSL_RF_CHAN_REL_ACK) + , + .out_state_mask = 0 + | S(LCHAN_ST_UNUSED) + , + }, +}; + +static const struct value_string lchan_fsm_event_names[] = { + OSMO_VALUE_STRING(LCHAN_EV_ACTIVATE), + OSMO_VALUE_STRING(LCHAN_EV_TS_READY), + OSMO_VALUE_STRING(LCHAN_EV_TS_ERROR), + OSMO_VALUE_STRING(LCHAN_EV_RSL_CHAN_ACTIV_ACK), + OSMO_VALUE_STRING(LCHAN_EV_RSL_CHAN_ACTIV_NACK), + OSMO_VALUE_STRING(LCHAN_EV_RLL_ESTABLISH_IND), + OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_AVAILABLE), + OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_CONFIGURED), + OSMO_VALUE_STRING(LCHAN_EV_MGW_ENDPOINT_ERROR), + OSMO_VALUE_STRING(LCHAN_EV_IPACC_CRCX_ACK), + OSMO_VALUE_STRING(LCHAN_EV_IPACC_CRCX_NACK), + OSMO_VALUE_STRING(LCHAN_EV_IPACC_MDCX_ACK), + OSMO_VALUE_STRING(LCHAN_EV_IPACC_MDCX_NACK), + OSMO_VALUE_STRING(LCHAN_EV_RLL_REL_IND), + OSMO_VALUE_STRING(LCHAN_EV_RLL_REL_CONF), + OSMO_VALUE_STRING(LCHAN_EV_RSL_RF_CHAN_REL_ACK), + OSMO_VALUE_STRING(LCHAN_EV_RLL_ERR_IND), + OSMO_VALUE_STRING(LCHAN_EV_CHAN_MODE_MODIF_ACK), + OSMO_VALUE_STRING(LCHAN_EV_CHAN_MODE_MODIF_ERROR), + {} +}; + +void lchan_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_LCHAN(); + + switch (event) { + + case LCHAN_EV_TS_ERROR: + lchan_fail_to(LCHAN_ST_UNUSED, "LCHAN_EV_TS_ERROR"); + return; + + case LCHAN_EV_MGW_ENDPOINT_ERROR: + /* This event during activation means that it was not possible to establish an endpoint. + * After activation was successful, it could also come in at any point to signal that the + * MGW side has become unavailable, which should lead to graceful release. */ + if (fi->state == LCHAN_ST_WAIT_MGW_ENDPOINT_AVAILABLE) { + /* This state is actually waiting for availability. Fail it immediately. */ + lchan_fail("LCHAN_EV_MGW_ENDPOINT_ERROR"); + return; + } + LOG_LCHAN(lchan, LOGL_ERROR, "Releasing due to MGW endpoint error"); + lchan_release(lchan, false, true, RSL_ERR_EQUIPMENT_FAIL); + return; + + default: + return; + } +} + +int lchan_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + GET_LCHAN(); + switch (fi->state) { + + case LCHAN_ST_WAIT_BEFORE_RF_RELEASE: + lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK); + return 0; + + case LCHAN_ST_WAIT_AFTER_ERROR: + lchan_fsm_state_chg(LCHAN_ST_UNUSED); + return 0; + + default: + lchan->release_in_error = true; + lchan_fail("Timeout"); + return 0; + } +} + +void lchan_release(struct gsm_lchan *lchan, bool sacch_deact, + bool err, enum gsm48_rr_cause cause_rr) +{ + if (!lchan || !lchan->fi) + return; + struct osmo_fsm_inst *fi = lchan->fi; + lchan->release_in_error = err; + lchan->error_cause = cause_rr; + lchan->deact_sacch = sacch_deact; + + /* This would also happen later, but better to do this a sooner. */ + if (lchan->mgw_endpoint_ci_bts) { + mgw_endpoint_ci_dlcx(lchan->mgw_endpoint_ci_bts); + lchan->mgw_endpoint_ci_bts = NULL; + } + + /* States waiting for events will notice the desire to release when done waiting, so it is enough + * to mark for release. */ + lchan->release_requested = true; + + /* But when in error, shortcut that. */ + if (lchan->release_in_error) { + switch (lchan->fi->state) { + default: + /* Normally we deact SACCH in lchan_fsm_wait_sapis_released_onenter(). When + * skipping that, but asked to SACCH deact, do it now. */ + if (lchan->deact_sacch) + rsl_deact_sacch(lchan); + lchan_fsm_state_chg(LCHAN_ST_WAIT_RF_RELEASE_ACK); + return; + case LCHAN_ST_WAIT_TS_READY: + lchan_fsm_state_chg(LCHAN_ST_UNUSED); + return; + case LCHAN_ST_WAIT_RF_RELEASE_ACK: + case LCHAN_ST_BORKEN: + return; + } + } + + /* The only non-broken state that would stay stuck without noticing the release_requested flag + * is: */ + if (fi->state == LCHAN_ST_ESTABLISHED) + lchan_fsm_state_chg(LCHAN_ST_WAIT_SAPIS_RELEASED); +} + +void lchan_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) +{ + GET_LCHAN(); + if (lchan->conn) + gscon_forget_lchan(lchan->conn, lchan); + lchan_reset(lchan); + if (lchan->last_error) { + talloc_free(lchan->last_error); + lchan->last_error = NULL; + } + lchan->fi = NULL; +} + +/* The mgw_endpoint was invalidated, just and simply forget the pointer without cleanup. */ +void lchan_forget_mgw_endpoint(struct gsm_lchan *lchan) +{ + if (!lchan) + return; + lchan->mgw_endpoint_ci_bts = NULL; +} + +/* The conn is deallocating, just forget all about it */ +void lchan_forget_conn(struct gsm_lchan *lchan) +{ + if (!lchan) + return; + lchan_forget_mgw_endpoint(lchan); + lchan->conn = NULL; +} + +static struct osmo_fsm lchan_fsm = { + .name = "lchan", + .states = lchan_fsm_states, + .num_states = ARRAY_SIZE(lchan_fsm_states), + .log_subsys = DRSL, + .event_names = lchan_fsm_event_names, + .allstate_action = lchan_fsm_allstate_action, + .allstate_event_mask = 0 + | S(LCHAN_EV_TS_ERROR) + | S(LCHAN_EV_MGW_ENDPOINT_ERROR) + , + .timer_cb = lchan_fsm_timer_cb, + .cleanup = lchan_fsm_cleanup, +}; diff --git a/src/osmo-bsc/lchan_select.c b/src/osmo-bsc/lchan_select.c new file mode 100644 index 000000000..29a6a2fc7 --- /dev/null +++ b/src/osmo-bsc/lchan_select.c @@ -0,0 +1,260 @@ +/* Select a suitable lchan from a given cell. + * + * (C) 2008 by Harald Welte + * (C) 2008, 2009 by Holger Hans Peter Freyther + * (C) 2018 by sysmocom - s.f.m.c. GmbH + * + * 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 . + * + */ + +#include + +#include +#include +#include + +#include + +static struct gsm_lchan * +_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan, + enum gsm_phys_chan_config as_pchan) +{ + struct gsm_lchan *lchan; + struct gsm_bts_trx_ts *ts; + int j, start, stop, dir; + +#define LOGPLCHANALLOC(fmt, args...) \ + LOGP(DRLL, LOGL_DEBUG, "looking for lchan %s%s%s: " fmt, \ + gsm_pchan_name(pchan), \ + pchan == as_pchan ? "" : " as ", \ + pchan == as_pchan ? "" : gsm_pchan_name(as_pchan), ## args) + + if (!trx_is_usable(trx)) { + LOGPLCHANALLOC("%s trx not usable\n", gsm_trx_name(trx)); + return NULL; + } + + if (trx->bts->chan_alloc_reverse) { + /* check TS 7..0 */ + start = 7; + stop = -1; + dir = -1; + } else { + /* check TS 0..7 */ + start = 0; + stop = 8; + dir = 1; + } + + for (j = start; j != stop; j += dir) { + ts = &trx->ts[j]; + if (!ts_is_usable(ts)) + continue; + /* The caller first selects what kind of TS to search in, e.g. looking for exact + * GSM_PCHAN_TCH_F, or maybe among dynamic GSM_PCHAN_TCH_F_TCH_H_PDCH... */ + if (ts->pchan_on_init != pchan) { + LOGPLCHANALLOC("%s is != %s\n", gsm_ts_and_pchan_name(ts), + gsm_pchan_name(pchan)); + continue; + } + /* Next, is this timeslot in or can it be switched to the pchan we want to use it for? */ + if (!ts_usable_as_pchan(ts, as_pchan)) { + LOGPLCHANALLOC("%s is not usable as %s\n", gsm_ts_and_pchan_name(ts), + gsm_pchan_name(as_pchan)); + continue; + } + + /* TS is (going to be) in desired pchan mode. Go ahead and check for an available lchan. */ + ts_as_pchan_for_each_lchan(lchan, ts, as_pchan) { + if (lchan->fi->state == LCHAN_ST_UNUSED) { + LOGPLCHANALLOC("%s ss=%d is available%s\n", + gsm_ts_and_pchan_name(ts), lchan->nr, + ts->pchan_is != as_pchan ? " after dyn PCHAN change" : ""); + return lchan; + } + LOGPLCHANALLOC("%s ss=%d in type=%s,state=%s not suitable\n", + gsm_ts_and_pchan_name(ts), lchan->nr, + gsm_lchant_name(lchan->type), + osmo_fsm_inst_state_name(lchan->fi)); + } + } + + return NULL; +#undef LOGPLCHANALLOC +} + +static struct gsm_lchan * +_lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan, + enum gsm_phys_chan_config dyn_as_pchan) +{ + struct gsm_bts_trx *trx; + struct gsm_lchan *lc; + + if (bts->chan_alloc_reverse) { + llist_for_each_entry_reverse(trx, &bts->trx_list, list) { + lc = _lc_find_trx(trx, pchan, dyn_as_pchan); + if (lc) + return lc; + } + } else { + llist_for_each_entry(trx, &bts->trx_list, list) { + lc = _lc_find_trx(trx, pchan, dyn_as_pchan); + if (lc) + return lc; + } + } + + return NULL; +} + +static struct gsm_lchan * +_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) +{ + return _lc_dyn_find_bts(bts, pchan, pchan); +} + +struct gsm_lchan *lchan_select_by_chan_mode(struct gsm_bts *bts, + enum gsm48_chan_mode chan_mode, bool full_rate) +{ + enum gsm_chan_t type; + + switch (chan_mode) { + case GSM48_CMODE_SIGN: + type = GSM_LCHAN_SDCCH; + break; + case GSM48_CMODE_SPEECH_V1: + case GSM48_CMODE_SPEECH_EFR: + case GSM48_CMODE_SPEECH_AMR: + type = full_rate ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H; + break; + default: + return NULL; + } + + return lchan_select_by_type(bts, type); +} + +/* Return a matching lchan from a specific BTS that is currently available. The next logical step is + * lchan_activate() on it, which would possibly cause dynamic timeslot pchan switching, taken care of by + * the lchan and timeslot FSMs. */ +struct gsm_lchan *lchan_select_by_type(struct gsm_bts *bts, enum gsm_chan_t type) +{ + struct gsm_lchan *lchan = NULL; + enum gsm_phys_chan_config first, first_cbch, second, second_cbch; + + LOGP(DRLL, LOGL_DEBUG, "(bts=%d) lchan_select_by_type(%s)\n", bts->nr, gsm_lchant_name(type)); + + switch (type) { + case GSM_LCHAN_SDCCH: + if (bts->chan_alloc_reverse) { + first = GSM_PCHAN_SDCCH8_SACCH8C; + first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; + second = GSM_PCHAN_CCCH_SDCCH4; + second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; + } else { + first = GSM_PCHAN_CCCH_SDCCH4; + first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; + second = GSM_PCHAN_SDCCH8_SACCH8C; + second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; + } + + lchan = _lc_find_bts(bts, first); + if (lchan == NULL) + lchan = _lc_find_bts(bts, first_cbch); + if (lchan == NULL) + lchan = _lc_find_bts(bts, second); + if (lchan == NULL) + lchan = _lc_find_bts(bts, second_cbch); + break; + case GSM_LCHAN_TCH_F: + lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); + /* If we don't have TCH/F available, fall-back to TCH/H */ + if (!lchan) { + lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); + if (lchan) + type = GSM_LCHAN_TCH_H; + } + /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */ + if (!lchan) { + lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, + GSM_PCHAN_TCH_F); + /* TCH/F_PDCH used as TCH/F -- here, type is already + * set to GSM_LCHAN_TCH_F, but for clarity's sake... */ + if (lchan) + type = GSM_LCHAN_TCH_F; + } + + /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */ + if (!lchan && bts->network->dyn_ts_allow_tch_f) { + lchan = _lc_dyn_find_bts(bts, + GSM_PCHAN_TCH_F_TCH_H_PDCH, + GSM_PCHAN_TCH_F); + if (lchan) + type = GSM_LCHAN_TCH_F; + } + /* ...and as TCH/H. */ + if (!lchan) { + lchan = _lc_dyn_find_bts(bts, + GSM_PCHAN_TCH_F_TCH_H_PDCH, + GSM_PCHAN_TCH_H); + if (lchan) + type = GSM_LCHAN_TCH_H; + } + break; + case GSM_LCHAN_TCH_H: + lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); + /* If we don't have TCH/H available, fall-back to TCH/F */ + if (!lchan) { + lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); + if (lchan) + type = GSM_LCHAN_TCH_F; + } + /* No dedicated TCH/x available -- try fully dynamic + * TCH/F_TCH/H_PDCH */ + if (!lchan) { + lchan = _lc_dyn_find_bts(bts, + GSM_PCHAN_TCH_F_TCH_H_PDCH, + GSM_PCHAN_TCH_H); + if (lchan) + type = GSM_LCHAN_TCH_H; + } + /* + * No need to check TCH/F_TCH/H_PDCH channels for TCH/F: + * if no TCH/H was available, neither will be TCH/F. + */ + /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */ + if (!lchan) { + lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, + GSM_PCHAN_TCH_F); + if (lchan) + type = GSM_LCHAN_TCH_F; + } + break; + default: + LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type); + } + + if (lchan) { + lchan->type = type; + LOG_LCHAN(lchan, LOGL_INFO, "Selected"); + } else + LOGP(DRLL, LOGL_ERROR, "(bts=%d) Failed to select %s channel\n", + bts->nr, gsm_lchant_name(type)); + + return lchan; +} + diff --git a/src/osmo-bsc/meas_feed.c b/src/osmo-bsc/meas_feed.c index 2e80754d4..8450f69ca 100644 --- a/src/osmo-bsc/meas_feed.c +++ b/src/osmo-bsc/meas_feed.c @@ -75,7 +75,7 @@ static int process_meas_rep(struct gsm_meas_rep *mr) /* copy channel information */ /* we assume that the measurement report always belong to some timeslot */ mfm->lchan_type = (uint8_t)mr->lchan->type; - mfm->pchan_type = (uint8_t)mr->lchan->ts->pchan; + mfm->pchan_type = (uint8_t)mr->lchan->ts->pchan_is; mfm->bts_nr = mr->lchan->ts->trx->bts->nr; mfm->trx_nr = mr->lchan->ts->trx->nr; mfm->ts_nr = mr->lchan->ts->nr; diff --git a/src/osmo-bsc/mgw_endpoint_fsm.c b/src/osmo-bsc/mgw_endpoint_fsm.c new file mode 100644 index 000000000..14d0b03b0 --- /dev/null +++ b/src/osmo-bsc/mgw_endpoint_fsm.c @@ -0,0 +1,748 @@ +/* osmo-bsc API to manage all sides of an MGW endpoint + * + * (C) 2018 by sysmocom - s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 . + * + */ + +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include + +#define LOG_CI(ci, level, fmt, args...) do { \ + if (!ci || !ci->mgwep) \ + LOGP(DLGLOBAL, level, "(unknown MGW endpoint) " fmt, ## args); \ + else \ + LOG_MGWEP(ci->mgwep, level, "CI[%d] %s%s%s: " fmt, \ + (int)(ci - ci->mgwep->ci), \ + ci->label ? : "-", \ + ci->mgcp_ci_str[0] ? " CI=" : "", \ + ci->mgcp_ci_str[0] ? ci->mgcp_ci_str : "", \ + ## args); \ + } while(0) + +#define FIRST_CI_EVENT (_MGWEP_EV_LAST + (_MGWEP_EV_LAST & 1)) /* rounded up to even nr */ +#define USABLE_CI ((32 - FIRST_CI_EVENT)/2) +#define EV_TO_CI_IDX(event) ((event - FIRST_CI_EVENT) / 2) + +#define CI_EV_SUCCESS(ci) (FIRST_CI_EVENT + (((ci) - ci->mgwep->ci) * 2)) +#define CI_EV_FAILURE(ci) (CI_EV_SUCCESS(ci) + 1) + +static struct osmo_fsm mgwep_fsm; + +struct mgwep_ci { + struct mgw_endpoint *mgwep; + + bool occupied; + char label[64]; + struct osmo_fsm_inst *mgcp_client_fi; + + bool pending; + bool sent; + enum mgcp_verb verb; + struct mgcp_conn_peer verb_info; + struct osmo_fsm_inst *notify; + uint32_t notify_success; + uint32_t notify_failure; + void *notify_data; + + bool got_port_info; + struct mgcp_conn_peer rtp_info; + char mgcp_ci_str[MGCP_CONN_ID_LENGTH]; +}; + +struct mgw_endpoint { + struct mgcp_client *mgcp_client; + struct osmo_fsm_inst *fi; + char endpoint[MGCP_ENDPOINT_MAXLEN]; + + struct mgwep_ci ci[USABLE_CI]; +}; + +static const struct value_string mgcp_verb_names[] = { + { MGCP_VERB_CRCX, "CRCX" }, + { MGCP_VERB_MDCX, "MDCX" }, + { MGCP_VERB_DLCX, "DLCX" }, + { MGCP_VERB_AUEP, "AUEP" }, + { MGCP_VERB_RSIP, "RSIP" }, + {} +}; + +static inline const char *mgcp_verb_name(enum mgcp_verb val) +{ return get_value_string(mgcp_verb_names, val); } + +static struct mgwep_ci *mgwep_check_ci(struct mgwep_ci *ci) +{ + if (!ci) + return NULL; + if (!ci->mgwep) + return NULL; + if (ci < ci->mgwep->ci || ci >= &ci->mgwep->ci[USABLE_CI]) + return NULL; + return ci; +} + +static struct mgwep_ci *mgwep_ci_for_event(struct mgw_endpoint *mgwep, uint32_t event) +{ + int idx; + if (event < FIRST_CI_EVENT) + return NULL; + idx = EV_TO_CI_IDX(event); + if (idx >= sizeof(mgwep->ci)) + return NULL; + return mgwep_check_ci(&mgwep->ci[idx]); +} + +const char *mgw_endpoint_name(const struct mgw_endpoint *mgwep) +{ + if (!mgwep) + return "NULL"; + if (mgwep->endpoint[0]) + return mgwep->endpoint; + return osmo_fsm_inst_name(mgwep->fi); +} + +const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info) +{ + static char buf[128]; + + if (!info) + return "NULL"; + + if (info->endpoint[0] + && info->addr[0]) + snprintf(buf, sizeof(buf), "%s:%s:%u", + info->endpoint, info->addr, info->port); + else if (info->endpoint[0]) + snprintf(buf, sizeof(buf), "%s", info->endpoint); + else if (info->addr[0]) + snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port); + else + return "empty"; + return buf; +} + +const char *mgwep_ci_name(const struct mgwep_ci *ci) +{ + const struct mgcp_conn_peer *rtp_info; + + if (!ci) + return "NULL"; + + rtp_info = mgwep_ci_get_rtp_info(ci); + + if (rtp_info) + return mgcp_conn_peer_name(rtp_info); + return mgw_endpoint_name(ci->mgwep); +} + +static struct value_string mgwep_fsm_event_names[33] = {}; + +static char mgwep_fsm_event_name_bufs[32][32] = {}; + +static void fill_event_names() +{ + int i; + for (i = 0; i < (ARRAY_SIZE(mgwep_fsm_event_names) - 1); i++) { + if (i < _MGWEP_EV_LAST) + continue; + if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) { + mgwep_fsm_event_names[i] = (struct value_string){i, "Unused"}; + continue; + } + snprintf(mgwep_fsm_event_name_bufs[i], sizeof(mgwep_fsm_event_name_bufs[i]), + "MGW Response for CI #%d", EV_TO_CI_IDX(i)); + mgwep_fsm_event_names[i] = (struct value_string){i, mgwep_fsm_event_name_bufs[i]}; + } +} + +static struct T_def *g_T_defs = NULL; + +void mgw_endpoint_fsm_init(struct T_def *T_defs) +{ + static bool g_initialized = false; + g_T_defs = T_defs; + if (!g_initialized) { + OSMO_ASSERT(osmo_fsm_register(&mgwep_fsm) == 0); + fill_event_names(); + g_initialized = true; + } +} + +/* From local var fi->priv, define local var mgewp. */ +#define GET_MGWEP() \ + struct mgw_endpoint *mgwep = fi->priv; \ + OSMO_ASSERT((fi)->fsm == &mgwep_fsm && (fi)->priv) + +struct mgw_endpoint *mgw_endpoint_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event, + struct mgcp_client *mgcp_client, + const char *fsm_id, + const char *endpoint_str_fmt, ...) +{ + va_list ap; + struct osmo_fsm_inst *fi; + struct mgw_endpoint *mgwep; + int rc; + + if (!mgcp_client) + return NULL; + + /* use mgcp_client as talloc ctx, so that the conn, lchan, ts can deallocate while MGCP DLCX are + * still going on. */ + fi = osmo_fsm_inst_alloc_child(&mgwep_fsm, parent, parent_term_event); + OSMO_ASSERT(fi); + + osmo_fsm_inst_update_id(fi, fsm_id); + + mgwep = talloc_zero(fi, struct mgw_endpoint); + OSMO_ASSERT(mgwep); + + va_start(ap, endpoint_str_fmt); + rc = snprintf(mgwep->endpoint, sizeof(mgwep->endpoint), endpoint_str_fmt, ap); + va_end(ap); + + if (rc <= 0 || rc >= sizeof(mgwep->endpoint)) { + LOG_MGWEP(mgwep, LOGL_ERROR, "Endpoint name too long, truncated: %s", + mgwep->endpoint); + osmo_fsm_inst_term(mgwep->fi, OSMO_FSM_TERM_ERROR, 0); + return NULL; + } + + mgwep->mgcp_client = mgcp_client; + mgwep->fi = fi; + mgwep->fi->priv = mgwep; + return mgwep; +} + +struct mgwep_ci *mgw_endpoint_ci_add(struct mgw_endpoint *mgwep, + const char *label_fmt, ...) +{ + va_list ap; + int i; + struct mgwep_ci *ci; + + for (i = 0; i < USABLE_CI; i++) { + ci = &mgwep->ci[i]; + + if (ci->occupied || ci->mgcp_client_fi) + continue; + + *ci = (struct mgwep_ci){ + .mgwep = mgwep, + .occupied = true, + }; + if (label_fmt) { + va_start(ap, label_fmt); + vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap); + va_end(ap); + } + return ci; + } + + LOG_MGWEP(mgwep, LOGL_ERROR, + "Cannot allocate another endpoint, all " + OSMO_STRINGIFY_VAL(USABLE_CI) " are in use"); + + return NULL; +} + +static void mgwep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi); + +static void on_failure(struct mgwep_ci *ci) +{ + if (!ci->occupied) + return; + + if (ci->notify) + osmo_fsm_inst_dispatch(ci->notify, ci->notify_failure, ci->notify_data); + + *ci = (struct mgwep_ci){ + .mgwep = ci->mgwep, + }; + + + mgwep_fsm_check_state_chg_after_response(ci->mgwep->fi); +} + +static void on_success(struct mgwep_ci *ci, void *data) +{ + struct mgcp_conn_peer *rtp_info; + + if (!ci->occupied) + return; + + ci->pending = false; + + switch (ci->verb) { + case MGCP_VERB_CRCX: + /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint + * name here. Also, we receive the MGW's RTP port information. */ + rtp_info = data; + OSMO_ASSERT(rtp_info); + ci->got_port_info = true; + ci->rtp_info = *rtp_info; + osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi), + sizeof(ci->mgcp_ci_str)); + if (rtp_info->endpoint[0]) { + int rc; + rc = osmo_strlcpy(ci->mgwep->endpoint, rtp_info->endpoint, + sizeof(ci->mgwep->endpoint)); + if (rc <= 0 || rc >= sizeof(ci->mgwep->endpoint)) { + LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name '%s'", + rtp_info->endpoint); + mgw_endpoint_ci_dlcx(ci); + on_failure(ci); + return; + } + } + break; + + case MGCP_VERB_MDCX: + /* If an MDCX succeeded, remember the new port information */ + ci->got_port_info = true; + ci->rtp_info = ci->verb_info; + break; + + default: + break; + } + + LOG_CI(ci, LOGL_DEBUG, "received successful response to %s RTP=%s%s", + mgcp_verb_name(ci->verb), + mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL), + ci->notify ? "" : " (not sending a notification)"); + + if (ci->notify) + osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data); + + mgwep_fsm_check_state_chg_after_response(ci->mgwep->fi); +} + +const struct mgcp_conn_peer *mgwep_ci_get_rtp_info(const struct mgwep_ci *ci) +{ + ci = mgwep_check_ci((struct mgwep_ci*)ci); + if (!ci) + return NULL; + if (!ci->got_port_info) + return NULL; + return &ci->rtp_info; +} + +bool mgwep_ci_get_crcx_info_to_sockaddr(const struct mgwep_ci *ci, struct sockaddr_storage *dest) +{ + const struct mgcp_conn_peer *rtp_info; + struct sockaddr_in *sin; + + rtp_info = mgwep_ci_get_rtp_info(ci); + if (!rtp_info) + return false; + + sin = (struct sockaddr_in *)dest; + + sin->sin_family = AF_INET; + sin->sin_addr.s_addr = inet_addr(rtp_info->addr); + sin->sin_port = osmo_ntohs(rtp_info->port); + return true; +} + + +struct state_timeout mgwep_fsm_timeouts[32] = { + [MGWEP_ST_WAIT_MGW_RESPONSE] = { .T=23042 }, +}; + +/* Transition to a state, using the T timer defined in assignment_fsm_timeouts. + * The actual timeout value is in turn obtained from g_T_defs. + * Assumes local variable fi exists. */ +#define mgwep_fsm_state_chg(state) \ + fsm_inst_state_chg_T(fi, state, mgwep_fsm_timeouts, g_T_defs, 5) + +void mgw_endpoint_ci_request(struct mgwep_ci *ci, + enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data) +{ + struct mgw_endpoint *mgwep; + struct osmo_fsm_inst *fi; + struct mgwep_ci cleared_ci; + ci = mgwep_check_ci(ci); + + if (!ci) { + LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n"); + goto dispatch_error; + } + if (!verb_info && verb != MGCP_VERB_DLCX) { + LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s", + mgcp_verb_name(verb)); + goto dispatch_error; + } + if ((verb < 0) || (verb > MGCP_VERB_RSIP)) { + LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s", + mgcp_verb_name(verb)); + goto dispatch_error; + } + + mgwep = ci->mgwep; + fi = mgwep->fi; + + /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign + * the char[] directly, dance through cleared_ci and copy back. */ + cleared_ci = (struct mgwep_ci){ + .mgwep = mgwep, + .mgcp_client_fi = ci->mgcp_client_fi, + .got_port_info = ci->got_port_info, + .rtp_info = ci->rtp_info, + + .occupied = true, + /* .pending = true follows below */ + .verb = verb, + .notify = notify, + .notify_success = event_success, + .notify_failure = event_failure, + .notify_data = notify_data, + }; + osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label)); + osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str)); + *ci = cleared_ci; + + LOG_CI(ci, LOGL_ERROR, "%s, notify=%p %s", mgcp_verb_name(ci->verb), ci->notify, ci->notify ? + ci->notify->id : ""); + + if (verb_info) + ci->verb_info = *verb_info; + + if (mgwep->endpoint[0]) { + if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, mgwep->endpoint)) + LOG_CI(ci, LOGL_ERROR, + "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s." + " Using the proper endpoint instead.", + mgcp_verb_name(verb), ci->verb_info.endpoint, mgwep->endpoint); + osmo_strlcpy(ci->verb_info.endpoint, mgwep->endpoint, sizeof(ci->verb_info.endpoint)); + } + + switch (ci->verb) { + case MGCP_VERB_CRCX: + if (ci->mgcp_client_fi) { + LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI"); + on_failure(ci); + return; + } + break; + + case MGCP_VERB_MDCX: + case MGCP_VERB_DLCX: + if (!ci->mgcp_client_fi) { + LOG_CI(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s", + mgcp_verb_name(ci->verb)); + on_failure(ci); + return; + } + break; + + default: + LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s", mgcp_verb_name(ci->verb)); + on_failure(ci); + return; + } + + ci->pending = true; + + LOG_CI(ci, LOGL_DEBUG, "Scheduling MGCP: %s (%s:%u)", + mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port); + + if (mgwep->fi->state != MGWEP_ST_WAIT_MGW_RESPONSE) + mgwep_fsm_state_chg(MGWEP_ST_WAIT_MGW_RESPONSE); + + return; +dispatch_error: + if (notify) + osmo_fsm_inst_dispatch(notify, event_failure, notify_data); +} + +static int send_verb(struct mgwep_ci *ci) +{ + int rc; + struct mgw_endpoint *mgwep = ci->mgwep; + + if (!ci->occupied || !ci->pending || ci->sent) + return 0; + + switch (ci->verb) { + + case MGCP_VERB_CRCX: + OSMO_ASSERT(!ci->mgcp_client_fi); + LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s:%u", + mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port); + ci->mgcp_client_fi = mgcp_conn_create(mgwep->mgcp_client, mgwep->fi, + CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci), + &ci->verb_info); + ci->sent = true; + if (!ci->mgcp_client_fi){ + LOG_CI(ci, LOGL_ERROR, "Error sending MGCP: %s %s:%u", + mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port); + on_failure(ci); + } + osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label); + break; + + case MGCP_VERB_MDCX: + OSMO_ASSERT(ci->mgcp_client_fi); + LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s %s:%u", + mgcp_verb_name(ci->verb), ci->mgcp_ci_str, ci->verb_info.addr, ci->verb_info.port); + rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info); + ci->sent = true; + if (rc) { + LOG_CI(ci, LOGL_ERROR, "Error sending MGCP: %s %s:%u (rc=%d %s)", + mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port, + rc, strerror(-rc)); + on_failure(ci); + } + break; + + case MGCP_VERB_DLCX: + LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s", + mgcp_verb_name(ci->verb), ci->mgcp_ci_str); + /* The way this is designed, we actually need to forget all about the ci right away. */ + mgcp_conn_delete(ci->mgcp_client_fi); + if (ci->notify) + osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data); + *ci = (struct mgwep_ci){ + .mgwep = mgwep, + }; + break; + + default: + OSMO_ASSERT(false); + } + + return 1; +} + +void mgw_endpoint_clear(struct mgw_endpoint *mgwep) +{ + if (!mgwep) + return; + osmo_fsm_inst_term(mgwep->fi, OSMO_FSM_TERM_REGULAR, 0); +} + +static void mgwep_count(struct mgw_endpoint *mgwep, int *occupied, int *pending_not_sent, + int *waiting_for_response) +{ + int i; + + if (occupied) + *occupied = 0; + + if (pending_not_sent) + *pending_not_sent = 0; + + if (waiting_for_response) + *waiting_for_response = 0; + + for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) { + struct mgwep_ci *ci = &mgwep->ci[i]; + if (ci->occupied) { + if (occupied) + (*occupied)++; + } else + continue; + + if (ci->pending) + LOG_CI(ci, LOGL_DEBUG, "verb=%s, %s", + mgcp_verb_name(ci->verb), + ci->sent ? "waiting for response" : "waiting to be sent"); + else + LOG_CI(ci, LOGL_DEBUG, "%s", + mgcp_conn_peer_name(mgwep_ci_get_rtp_info(ci))); + + if (ci->pending && ci->sent) + if (waiting_for_response) + (*waiting_for_response)++; + if (ci->pending && !ci->sent) + if (pending_not_sent) + (*pending_not_sent)++; + } +} + +static void mgwep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi) +{ + int waiting_for_response; + int occupied; + GET_MGWEP(); + + mgwep_count(mgwep, &occupied, NULL, &waiting_for_response); + LOG_MGWEP(mgwep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d", occupied, waiting_for_response); + + if (!occupied) { + /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by + * terminating. */ + osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0); + return; + } + + if (!waiting_for_response) { + if (fi->state != MGWEP_ST_IN_USE) + mgwep_fsm_state_chg(MGWEP_ST_IN_USE); + return; + } + +} + +static void mgwep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int count = 0; + int i; + GET_MGWEP(); + + for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) { + count += send_verb(&mgwep->ci[i]); + } + + LOG_MGWEP(mgwep, LOGL_DEBUG, "Sent messages: %d", count); + mgwep_fsm_check_state_chg_after_response(fi); + +} + +static void mgwep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgwep_ci *ci; + GET_MGWEP(); + ci = mgwep_ci_for_event(mgwep, event); + if (ci) { + if (event == CI_EV_SUCCESS(ci)) + on_success(ci, data); + else + on_failure(ci); + } +} + +static void mgwep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int pending_not_sent; + GET_MGWEP(); + + mgwep_count(mgwep, NULL, &pending_not_sent, NULL); + if (pending_not_sent) + mgwep_fsm_state_chg(MGWEP_ST_WAIT_MGW_RESPONSE); +} + +#define S(x) (1 << (x)) + +static const struct osmo_fsm_state mgwep_fsm_states[] = { + [MGWEP_ST_UNUSED] = { + .name = "UNUSED", + .in_event_mask = 0, + .out_state_mask = 0 + | S(MGWEP_ST_WAIT_MGW_RESPONSE) + , + }, + [MGWEP_ST_WAIT_MGW_RESPONSE] = { + .name = "WAIT_MGW_RESPONSE", + .onenter = mgwep_fsm_wait_mgw_response_onenter, + .action = mgwep_fsm_handle_ci_events, + .in_event_mask = 0xffffffff, + .out_state_mask = 0 + | S(MGWEP_ST_IN_USE) + , + }, + [MGWEP_ST_IN_USE] = { + .name = "IN_USE", + .onenter = mgwep_fsm_in_use_onenter, + .action = mgwep_fsm_handle_ci_events, + .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */ + .out_state_mask = 0 + | S(MGWEP_ST_WAIT_MGW_RESPONSE) + , + }, +}; + +int mgwep_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + int i; + GET_MGWEP(); + + switch (fi->T) { + default: + for (i = 0; i < ARRAY_SIZE(mgwep->ci); i++) { + struct mgwep_ci *ci = &mgwep->ci[i]; + if (!ci->occupied) + continue; + if (!(ci->pending && ci->sent)) + continue; + on_failure(ci); + } + return 0; + } + + return 0; +} + +static struct osmo_fsm mgwep_fsm = { + .name = "mgw-endpoint", + .states = mgwep_fsm_states, + .num_states = ARRAY_SIZE(mgwep_fsm_states), + .log_subsys = DRSL, + .event_names = mgwep_fsm_event_names, + .timer_cb = mgwep_fsm_timer_cb, + /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */ +}; + +/* Depending on the channel mode and rate, return the codec type that is signalled towards the MGW. */ +enum mgcp_codecs chan_mode_to_mgcp_codec(enum gsm48_chan_mode chan_mode, bool full_rate) +{ + switch (chan_mode) { + case GSM48_CMODE_SPEECH_V1: + if (full_rate) + return CODEC_GSM_8000_1; + return CODEC_GSMHR_8000_1; + + case GSM48_CMODE_SPEECH_EFR: + return CODEC_GSMEFR_8000_1; + + case GSM48_CMODE_SPEECH_AMR: + return CODEC_AMR_8000_1; + + default: + return -1; + } +} + +void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan) +{ + enum mgcp_codecs codec = chan_mode_to_mgcp_codec(lchan->tch_mode, + lchan->type == GSM_LCHAN_TCH_H? false : true); + if (codec < 0) { + LOG_LCHAN(lchan, LOGL_ERROR, + "Unable to determine MGCP codec type for %s in chan-mode %s\n", + gsm_lchant_name(lchan->type), gsm48_chan_mode_name(lchan->tch_mode)); + verb_info->codecs_len = 0; + } + + verb_info->codecs[0] = codec; + verb_info->codecs_len = 1; +} diff --git a/src/osmo-bsc/neighbor_ident_vty.c b/src/osmo-bsc/neighbor_ident_vty.c index a606ed130..39712c105 100644 --- a/src/osmo-bsc/neighbor_ident_vty.c +++ b/src/osmo-bsc/neighbor_ident_vty.c @@ -36,14 +36,26 @@ static struct neighbor_ident_list *g_neighbor_cells = NULL; /* Parse VTY parameters matching NEIGHBOR_IDENT_VTY_KEY_PARAMS. Pass a pointer so that argv[0] is the * ARFCN value followed by the BSIC keyword and value. vty *must* reference a BTS_NODE. */ -bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv, struct neighbor_ident_key *key) +bool neighbor_ident_vty_parse_key_params(struct vty *vty, const char **argv, + struct neighbor_ident_key *key) { struct gsm_bts *bts = vty->index; + + OSMO_ASSERT(vty->node == BTS_NODE); + OSMO_ASSERT(bts); + + return neighbor_ident_bts_parse_key_params(vty, bts, argv, key); +} + +/* same as neighbor_ident_vty_parse_key_params() but pass an explicit bts, so it works on any node. */ +bool neighbor_ident_bts_parse_key_params(struct vty *vty, struct gsm_bts *bts, const char **argv, + struct neighbor_ident_key *key) +{ const char *arfcn_str = argv[0]; const char *bsic_kind = argv[1]; const char *bsic_str = argv[2]; - OSMO_ASSERT(vty->node == BTS_NODE && bts); + OSMO_ASSERT(bts); *key = (struct neighbor_ident_key){ .from_bts = bts->nr, @@ -81,7 +93,7 @@ static struct gsm_bts *neighbor_ident_vty_parse_bts_nr(struct vty *vty, const ch static struct gsm_bts *bts_by_cell_id(struct vty *vty, struct gsm0808_cell_id *cell_id) { - struct gsm_bts *bts = gsm_bts_by_cell_id(g_net, cell_id); + struct gsm_bts *bts = gsm_bts_by_cell_id(g_net, cell_id, 0); if (!bts) vty_out(vty, "%% No such BTS: %s%s\n", gsm0808_cell_id_name(cell_id), VTY_NEWLINE); return bts; @@ -250,7 +262,7 @@ static int add_remote_or_local_bts(struct vty *vty, const struct gsm0808_cell_id } /* Is there a local BTS that matches the cell_id? */ - local_neigh = gsm_bts_by_cell_id(g_net, cell_id); + local_neigh = gsm_bts_by_cell_id(g_net, cell_id, 0); if (local_neigh) { /* But do the advertised ARFCN and BSIC match as intended? * The user may omit ARFCN and BSIC for local cells, but if they are provided, diff --git a/src/osmo-bsc/osmo_bsc_api.c b/src/osmo-bsc/osmo_bsc_api.c index d409c9350..ea55f1017 100644 --- a/src/osmo-bsc/osmo_bsc_api.c +++ b/src/osmo-bsc/osmo_bsc_api.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,7 @@ #include /* Check if we have a proper connection to the MSC */ -static bool msc_connected(struct gsm_subscriber_connection *conn) +bool msc_connected(struct gsm_subscriber_connection *conn) { /* No subscriber conn at all */ if (!conn) @@ -167,6 +168,43 @@ void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, struct msgb * msgb_free(resp); } +/* 9.2.5 CM service accept */ +int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn) +{ + struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACK"); + struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); + + msg->lchan = conn->lchan; + + gh->proto_discr = GSM48_PDISC_MM; + gh->msg_type = GSM48_MT_MM_CM_SERV_ACC; + + DEBUGP(DMM, "-> CM SERVICE ACK\n"); + + gscon_submit_rsl_dtap(conn, msg, 0, 0); + return 0; +} + +#if 0 +no-one is using this +/* 9.2.6 CM service reject */ +int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn, + enum gsm48_reject_value value) +{ + struct msgb *msg; + + msg = gsm48_create_mm_serv_rej(value); + if (!msg) { + LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n"); + return -1; + } + + DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value); + + return gsm0808_submit_dtap(conn, msg, 0, 0); +} +#endif + static void bsc_send_ussd_no_srv(struct gsm_subscriber_connection *conn, struct msgb *msg, const char *text) { @@ -211,6 +249,141 @@ static void bsc_send_ussd_no_srv(struct gsm_subscriber_connection *conn, bsc_send_ussd_release_complete(conn); } +static int is_cm_service_for_emerg(struct msgb *msg) +{ + struct gsm48_service_request *cm; + struct gsm48_hdr *gh = msgb_l3(msg); + + if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*cm)) { + LOGP(DMSC, LOGL_ERROR, "CM ServiceRequest does not fit.\n"); + return 0; + } + + cm = (struct gsm48_service_request *) &gh->data[0]; + return cm->cm_service_type == GSM48_CMSERV_EMERGENCY; +} + +/* extract a subscriber from the paging response */ +static struct bsc_subscr *extract_sub(struct gsm_subscriber_connection *conn, + struct msgb *msg) +{ + uint8_t mi_type; + char mi_string[GSM48_MI_SIZE]; + struct gsm48_hdr *gh; + struct gsm48_pag_resp *resp; + struct bsc_subscr *subscr; + + if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*resp)) { + LOGP(DMSC, LOGL_ERROR, "PagingResponse too small: %u\n", msgb_l3len(msg)); + return NULL; + } + + gh = msgb_l3(msg); + resp = (struct gsm48_pag_resp *) &gh->data[0]; + + gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), + mi_string, &mi_type); + DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", + gsm48_mi_type_name(mi_type), mi_string); + + switch (mi_type) { + case GSM_MI_TYPE_TMSI: + subscr = bsc_subscr_find_by_tmsi(conn->network->bsc_subscribers, + tmsi_from_string(mi_string)); + break; + case GSM_MI_TYPE_IMSI: + subscr = bsc_subscr_find_by_imsi(conn->network->bsc_subscribers, + mi_string); + break; + default: + subscr = NULL; + break; + } + + return subscr; +} + +struct bsc_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn, + struct msgb *msg) +{ + struct gsm48_hdr *gh; + int8_t pdisc; + uint8_t mtype; + struct osmo_bsc_data *bsc; + struct bsc_msc_data *msc, *pag_msc; + struct bsc_subscr *subscr; + int is_emerg = 0; + + bsc = conn->network->bsc_data; + + if (msgb_l3len(msg) < sizeof(*gh)) { + LOGP(DMSC, LOGL_ERROR, "There is no GSM48 header here.\n"); + return NULL; + } + + gh = msgb_l3(msg); + pdisc = gsm48_hdr_pdisc(gh); + mtype = gsm48_hdr_msg_type(gh); + + /* + * We are asked to select a MSC here but they are not equal. We + * want to respond to a paging request on the MSC where we got the + * request from. This is where we need to decide where this connection + * will go. + */ + if (pdisc == GSM48_PDISC_RR && mtype == GSM48_MT_RR_PAG_RESP) + goto paging; + else if (pdisc == GSM48_PDISC_MM && mtype == GSM48_MT_MM_CM_SERV_REQ) { + is_emerg = is_cm_service_for_emerg(msg); + goto round_robin; + } else + goto round_robin; + +round_robin: + llist_for_each_entry(msc, &bsc->mscs, entry) { + if (!msc->is_authenticated) + continue; + if (!is_emerg && msc->type != MSC_CON_TYPE_NORMAL) + continue; + if (is_emerg && !msc->allow_emerg) + continue; + + /* force round robin by moving it to the end */ + llist_move_tail(&msc->entry, &bsc->mscs); + return msc; + } + + return NULL; + +paging: + subscr = extract_sub(conn, msg); + + if (!subscr) { + LOGP(DMSC, LOGL_ERROR, "Got paged but no subscriber found.\n"); + return NULL; + } + + pag_msc = paging_get_msc(conn_get_bts(conn), subscr); + bsc_subscr_put(subscr); + + llist_for_each_entry(msc, &bsc->mscs, entry) { + if (msc != pag_msc) + continue; + + /* + * We don't check if the MSC is connected. In case it + * is not the connection will be dropped. + */ + + /* force round robin by moving it to the end */ + llist_move_tail(&msc->entry, &bsc->mscs); + return msc; + } + + LOGP(DMSC, LOGL_ERROR, "Got paged but no request found.\n"); + return NULL; +} + /*! MS->MSC: New MM context with L3 payload. */ int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint16_t chosen_channel) { @@ -230,6 +403,61 @@ int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint1 return complete_layer3(conn, msg, msc); } +static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg) +{ + struct bsc_subscr *subscr = extract_sub(conn, msg); + + if (!subscr) { + LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n"); + return -1; + } + + paging_request_stop(&conn->network->bts_list, conn_get_bts(conn), subscr, conn, + msg); + bsc_subscr_put(subscr); + return 0; +} + +static void handle_lu_request(struct gsm_subscriber_connection *conn, + struct msgb *msg) +{ + struct gsm48_hdr *gh; + struct gsm48_loc_upd_req *lu; + struct gsm48_loc_area_id lai; + + if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*lu)) { + LOGP(DMSC, LOGL_ERROR, "LU too small to look at: %u\n", msgb_l3len(msg)); + return; + } + + gh = msgb_l3(msg); + lu = (struct gsm48_loc_upd_req *) gh->data; + + gsm48_generate_lai2(&lai, bts_lai(conn_get_bts(conn))); + + if (memcmp(&lai, &lu->lai, sizeof(lai)) != 0) { + LOGP(DMSC, LOGL_DEBUG, "Marking con for welcome USSD.\n"); + conn->new_subscriber = 1; + } +} + +int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg) +{ + struct gsm48_hdr *gh = msgb_l3(msg); + uint8_t pdisc = gsm48_hdr_pdisc(gh); + uint8_t mtype = gsm48_hdr_msg_type(gh); + + if (pdisc == GSM48_PDISC_MM) { + if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST) + handle_lu_request(conn, msg); + } else if (pdisc == GSM48_PDISC_RR) { + if (mtype == GSM48_MT_RR_PAG_RESP) + handle_page_resp(conn, msg); + } + + return 0; +} + static int complete_layer3(struct gsm_subscriber_connection *conn, struct msgb *msg, struct bsc_msc_data *msc) { @@ -274,6 +502,7 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, } else conn->bsub = bsc_subscr_find_or_create_by_imsi(msc->network->bsc_subscribers, imsi); + gscon_update_id(conn); } conn->filter_state.con_type = con_type; @@ -311,8 +540,10 @@ static int move_to_msc(struct gsm_subscriber_connection *_conn, * properly. */ if (complete_layer3(_conn, msg, msc) != BSC_API_CONN_POL_ACCEPT) { - gsm0808_clear(_conn); - //bsc_subscr_con_free(_conn); + /* FIXME: I have not the slightest idea what move_to_msc() intends to do; during lchan + * FSM introduction, I changed this and hope it is the appropriate action. I actually + * assume this is unused legacy code for osmo-bsc_nat?? */ + gscon_release_lchans(_conn, false); return 1; } @@ -411,40 +642,6 @@ void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct ms osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_MO_DTAP, msg); } -/*! BSC->MSC: Assignment of lchan successful. */ -void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause) -{ - if (!msc_connected(conn)) - return; - - conn->lchan->abis_ip.ass_compl.rr_cause = rr_cause; - - if (is_ipaccess_bts(conn_get_bts(conn)) && conn->user_plane.rtp_ip) { - /* NOTE: In a network that makes use of an IPA base station - * and AoIP, we have to wait until the BTS reports its RTP - * IP/Port combination back to BSC via RSL. Unfortunately, the - * IPA protocol sends its Abis assignment complete message - * before it sends its RTP IP/Port via IPACC. So we will now - * postpone the AoIP assignment completed message until we - * know the RTP IP/Port combination. */ - LOGP(DMSC, LOGL_INFO, "POSTPONE MSC ASSIGN COMPL\n"); - conn->lchan->abis_ip.ass_compl.valid = true; - - } else { - /* NOTE: Send the A assignment complete message immediately. */ - LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n"); - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RR_ASS_COMPL, NULL); - } -} - -/*! BSC->MSC: Assignment of lchan failed. */ -void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause) -{ - LOGPFSML(conn->fi, LOGL_ERROR, "Assignment failure: BSSMAP: '%s' from RR: '%s'\n", - gsm0808_cause_name(cause), rr_cause ? rr_cause_name(*rr_cause) : "(none)"); - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_RR_ASS_FAIL, &cause); -} - /*! BSC->MSC: RR conn has been cleared. */ int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause) { diff --git a/src/osmo-bsc/osmo_bsc_audio.c b/src/osmo-bsc/osmo_bsc_audio.c deleted file mode 100644 index 8eef8d2da..000000000 --- a/src/osmo-bsc/osmo_bsc_audio.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * ipaccess audio handling - * - * (C) 2009-2010 by Holger Hans Peter Freyther - * (C) 2009-2010 by On-Waves - * 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 . - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -static int handle_abisip_signal(unsigned int subsys, unsigned int signal, - void *handler_data, void *signal_data) -{ - struct gsm_subscriber_connection *con; - struct gsm_lchan *lchan = signal_data; - int rc; - uint32_t rtp_ip; - - if (subsys != SS_ABISIP) - return 0; - - con = lchan->conn; - if (!con) - return 0; - - switch (signal) { - case S_ABISIP_CRCX_ACK: - /* we can ask it to connect now */ - LOGP(DMSC, LOGL_DEBUG, "Connecting BTS to port: %d conn: %d\n", - con->user_plane.rtp_port, lchan->abis_ip.conn_id); - - /* If AoIP is in use, the rtp_ip, which has been communicated - * via the A interface as connect_ip */ - if(con->user_plane.rtp_ip) - rtp_ip = con->user_plane.rtp_ip; - else - rtp_ip = ntohl(INADDR_ANY); - - rc = rsl_ipacc_mdcx(lchan, rtp_ip, - con->user_plane.rtp_port, - lchan->abis_ip.rtp_payload2); - if (rc < 0) { - LOGP(DMSC, LOGL_ERROR, "Failed to send MDCX: %d\n", rc); - return rc; - } - break; - - case S_ABISIP_MDCX_ACK: - if (con->ho) { - LOGPHO(con->ho, LOGL_DEBUG, "BTS sent MDCX ACK\n"); - /* No need to do anything for handover here. As soon as a HANDOVER DETECT - * happens, handover_logic.c and bsc_subscr_conn_fsm.c will trigger the - * MGCP MDCX towards MGW by receiving an S_LCHAN_HANDOVER_DETECT signal. */ - } else if (is_ipaccess_bts(conn_get_bts(con)) && con->user_plane.rtp_ip) { - /* NOTE: This is only relevant on AoIP networks with - * IPA based base stations. See also osmo_bsc_api.c, - * function bsc_assign_compl() */ - LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL (POSTPONED)\n"); - osmo_fsm_inst_dispatch(con->fi, GSCON_EV_RR_ASS_COMPL, NULL); - - } - break; - } - - return 0; -} - -int osmo_bsc_audio_init(struct gsm_network *net) -{ - osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, net); - return 0; -} diff --git a/src/osmo-bsc/osmo_bsc_bssap.c b/src/osmo-bsc/osmo_bsc_bssap.c index c2d51b5cb..1a271e78b 100644 --- a/src/osmo-bsc/osmo_bsc_bssap.c +++ b/src/osmo-bsc/osmo_bsc_bssap.c @@ -19,6 +19,8 @@ * */ +#include + #include #include #include @@ -27,7 +29,10 @@ #include #include #include +#include #include +#include +#include #include #include @@ -36,7 +41,7 @@ #include #include #include -#include +#include #include #define IP_V4_ADDR_LEN 4 @@ -45,161 +50,6 @@ * helpers for the assignment command */ -/* Helper function for match_codec_pref(), looks up a matching permitted speech - * value for a given msc audio codec pref */ -enum gsm0808_permitted_speech audio_support_to_gsm88(struct gsm_audio_support *audio) -{ - if (audio->hr) { - switch (audio->ver) { - case 1: - return GSM0808_PERM_HR1; - break; - case 2: - return GSM0808_PERM_HR2; - break; - case 3: - return GSM0808_PERM_HR3; - break; - default: - LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: hr%d, using hr1 instead\n", - audio->ver); - return GSM0808_PERM_HR1; - } - } else { - switch (audio->ver) { - case 1: - return GSM0808_PERM_FR1; - break; - case 2: - return GSM0808_PERM_FR2; - break; - case 3: - return GSM0808_PERM_FR3; - break; - default: - LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: fr%d, using fr1 instead\n", - audio->ver); - return GSM0808_PERM_FR1; - } - } -} - -/* Helper function for match_codec_pref(), looks up a matching chan mode for - * a given permitted speech value */ -enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech) -{ - switch (speech) { - case GSM0808_PERM_HR1: - case GSM0808_PERM_FR1: - return GSM48_CMODE_SPEECH_V1; - break; - case GSM0808_PERM_HR2: - case GSM0808_PERM_FR2: - return GSM48_CMODE_SPEECH_EFR; - break; - case GSM0808_PERM_HR3: - case GSM0808_PERM_FR3: - return GSM48_CMODE_SPEECH_AMR; - break; - default: - LOGP(DMSC, LOGL_FATAL, - "Unsupported permitted speech selected, assuming AMR as channel mode...\n"); - return GSM48_CMODE_SPEECH_AMR; - } -} - -/* Helper function for match_codec_pref(), tests if a given audio support - * matches one of the permitted speech settings of the channel type element. - * The matched permitted speech value is then also compared against the - * speech codec list. (optional, only relevant for AoIP) */ -static bool test_codec_pref(const struct gsm0808_channel_type *ct, - const struct gsm0808_speech_codec_list *scl, - uint8_t perm_spch) -{ - unsigned int i; - bool match = false; - struct gsm0808_speech_codec sc; - int rc; - - /* Try to finde the given permitted speech value in the - * codec list of the channel type element */ - for (i = 0; i < ct->perm_spch_len; i++) { - if (ct->perm_spch[i] == perm_spch) { - match = true; - break; - } - } - - /* If we do not have a speech codec list to test against, - * we just exit early (will be always the case in non-AoIP networks) */ - if (!scl) - return match; - - /* If we failed to match until here, there is no - * point in testing further */ - if (match == false) - return false; - - /* Extrapolate speech codec data */ - rc = gsm0808_speech_codec_from_chan_type(&sc, perm_spch); - if (rc < 0) - return false; - - /* Try to find extrapolated speech codec data in - * the speech codec list */ - for (i = 0; i < scl->len; i++) { - if (sc.type == scl->codec[i].type) - return true; - } - - return false; -} - -/*! Helper function for bssmap_handle_assignm_req(), matches the codec - * preferences from the MSC with the codec preferences - * \param[out] full_rate '1' if full-rate, '0' if half-rate, '-1' if no match - * \param[out] chan_mode GSM 04.08 channel mode - * \param[in] ct GSM 08.08 channel type - * \param[in] scl GSM 08.08 speech codec list - * \param[in] msc MSC data [for configuration] - * \returns 0 on success, -1 in case no match was found */ -static int match_codec_pref(int *full_rate, enum gsm48_chan_mode *chan_mode, - const struct gsm0808_channel_type *ct, - const struct gsm0808_speech_codec_list *scl, - const struct bsc_msc_data *msc) -{ - unsigned int i; - uint8_t perm_spch; - bool match = false; - - for (i = 0; i < msc->audio_length; i++) { - perm_spch = audio_support_to_gsm88(msc->audio_support[i]); - if (test_codec_pref(ct, scl, perm_spch)) { - match = true; - break; - } - } - - /* Exit without result, in case no match can be deteched */ - if (!match) { - *full_rate = -1; - *chan_mode = GSM48_CMODE_SIGN; - return -1; - } - - /* Check if the result is a half or full rate codec */ - if (perm_spch == GSM0808_PERM_HR1 || perm_spch == GSM0808_PERM_HR2 - || perm_spch == GSM0808_PERM_HR3 || perm_spch == GSM0808_PERM_HR4 - || perm_spch == GSM0808_PERM_HR6) - *full_rate = 0; - else - *full_rate = 1; - - /* Lookup a channel mode for the selected codec */ - *chan_mode = gsm88_to_chan_mode(perm_spch); - - return 0; -} static int bssmap_handle_reset_ack(struct bsc_msc_data *msc, struct msgb *msg, unsigned int length) @@ -712,7 +562,6 @@ static int bssmap_handle_lcls_connect_ctrl(struct gsm_subscriber_connection *con return 0; } - /* * Handle the assignment request message. * @@ -727,17 +576,17 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn, uint8_t timeslot = 0; uint8_t multiplex = 0; enum gsm48_chan_mode chan_mode = GSM48_CMODE_SIGN; - int full_rate = -1; + bool full_rate = false; bool aoip = false; struct sockaddr_storage rtp_addr; struct gsm0808_channel_type ct; - struct gsm0808_speech_codec_list *scl_ptr = NULL; uint8_t cause; int rc; + struct assignment_request req = {}; if (!conn) { LOGP(DMSC, LOGL_ERROR, - "No lchan/msc_data in cipher mode command.\n"); + "No lchan/msc_data in Assignment Request\n"); return -1; } @@ -803,27 +652,18 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn, cause = GSM0808_CAUSE_INCORRECT_VALUE; goto reject; } - } else { - if (aoip) { - /* no AoIP transport level address but AoIP transport: illegal */ - LOGP(DMSC, LOGL_ERROR, "AoIP transport address missing in ASSIGN REQ, " - "audio would not work; rejecting\n"); - cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; - goto reject; - } + } else if (aoip) { + /* no AoIP transport level address but AoIP transport: illegal */ + LOGP(DMSC, LOGL_ERROR, "AoIP transport address missing in ASSIGN REQ, " + "audio would not work; rejecting\n"); + cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; + goto reject; } - /* Decode speech codec list (AoIP) */ - conn->codec_list_present = false; - if (aoip) { - - /* Check for speech codec list element */ - if (!TLVP_PRESENT(&tp, GSM0808_IE_SPEECH_CODEC_LIST)) { - LOGP(DMSC, LOGL_ERROR, "Mandatory speech codec list not present.\n"); - cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; - goto reject; - } - + /* Decode speech codec list. First set len = 0. */ + conn->codec_list = (struct gsm0808_speech_codec_list){}; + /* Check for speech codec list element */ + if (TLVP_PRESENT(&tp, GSM0808_IE_SPEECH_CODEC_LIST)) { /* Decode Speech Codec list */ rc = gsm0808_dec_speech_codec_list(&conn->codec_list, TLVP_VAL(&tp, GSM0808_IE_SPEECH_CODEC_LIST), @@ -833,13 +673,19 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn, cause = GSM0808_CAUSE_INCORRECT_VALUE; goto reject; } - conn->codec_list_present = true; - scl_ptr = &conn->codec_list; + } + + if (aoip && !conn->codec_list.len) { + LOGP(DMSC, LOGL_ERROR, "%s: AoIP speech mode Assignment Request:" + " Missing or empty Speech Codec List IE\n", bsc_subscr_name(conn->bsub)); + cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; + goto reject; } /* Match codec information from the assignment command against the * local preferences of the BSC */ - rc = match_codec_pref(&full_rate, &chan_mode, &ct, scl_ptr, msc); + rc = bsc_match_codec_pref(&chan_mode, &full_rate, &ct, &conn->codec_list, + msc->audio_support, msc->audio_length); if (rc < 0) { LOGP(DMSC, LOGL_ERROR, "No supported audio type found for channel_type =" " { ch_indctr=0x%x, ch_rate_type=0x%x, perm_spch=[ %s] }\n", @@ -856,37 +702,37 @@ static int bssmap_handle_assignm_req(struct gsm_subscriber_connection *conn, get_value_string(gsm48_chan_mode_names, chan_mode), ct.ch_indctr, ct.ch_rate_type, osmo_hexdump(ct.perm_spch, ct.perm_spch_len)); - /* Forward the assignment request to lower layers */ + req = (struct assignment_request){ + .aoip = aoip, + .chan_mode = chan_mode, + .full_rate = full_rate, + }; if (aoip) { - /* Store network side RTP connection information, we will - * process this address later after we have established an RTP - * connection to the BTS. This is just for organizational - * reasons, functional wise it would not matter when exactly - * the network side RTP connection is made, as long it is made - * before we return with the assignment complete message. */ - memcpy(&conn->user_plane.aoip_rtp_addr_remote, &rtp_addr, sizeof(rtp_addr)); + if (!sockaddr_to_str_and_uint(req.msc_rtp_addr, sizeof(req.msc_rtp_addr), + &req.msc_rtp_port, &rtp_addr)) { + LOGP(DMSC, LOGL_ERROR, "Assignment request: Invalid RTP address (too long?)\n"); + cause = GSM0808_CAUSE_REQ_CODEC_TYPE_OR_CONFIG_UNAVAIL; + goto reject; + } } else { /* Note: In the sccp-lite case we to not perform any mgcp operation, - * (the MSC does that for us). We set conn->rtp_ip to 0 and check - * on this later. By this we know that we have to behave accordingly - * to sccp-lite. */ - conn->user_plane.rtp_port = mgcp_timeslot_to_port(multiplex, timeslot, msc->rtp_base); - conn->user_plane.rtp_ip = 0; + * (the MSC does that for us). */ + req.msc_rtp_port = mgcp_timeslot_to_port(multiplex, timeslot, msc->rtp_base); } - conn->user_plane.chan_mode = chan_mode; - conn->user_plane.full_rate = full_rate; - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_ASSIGNMENT_CMD, NULL); break; case GSM0808_CHAN_SIGN: - conn->user_plane.chan_mode = GSM48_CMODE_SIGN; - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_ASSIGNMENT_CMD, NULL); + req = (struct assignment_request){ + .aoip = aoip, + .chan_mode = chan_mode, + }; break; default: cause = GSM0808_CAUSE_INVALID_MESSAGE_CONTENTS; goto reject; } - return 0; + return osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_ASSIGNMENT_START, &req); + reject: resp = gsm0808_create_assignment_failure(cause, NULL); OSMO_ASSERT(resp); @@ -895,6 +741,50 @@ reject: return -1; } +/* Handle Handover Command message, part of inter-BSC handover: + * This BSS sent a Handover Required message. + * The MSC contacts the remote BSS and receives from it an RR Handover Command; this BSSMAP Handover + * Command passes the RR Handover Command over to us and it's our job to forward to the MS. + * + * See 3GPP TS 48.008 §3.2.1.11 + */ +static int bssmap_handle_handover_cmd(struct gsm_subscriber_connection *conn, + struct msgb *msg, unsigned int length) +{ + struct tlv_parsed tp; + + if (!conn->ho.fi) { + LOGP(DMSC, LOGL_ERROR, "Received Handover Command, but no handover was requested"); + /* Should we actually allow the MSC to make us handover without us having requested it + * first? Doesn't make any practical sense AFAICT. */ + return -EINVAL; + } + + tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, length - 1, 0, 0); + + /* Check for channel type element, if its missing, immediately reject */ + if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { + LOGP(DMSC, LOGL_ERROR, "Mandatory IE not present: Layer 3 Information\n"); + goto reject; + } + + + { + struct moho_rx_bssmap_ho_command rx = { + .l3_info = TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION), + .l3_info_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION), + }; + + osmo_fsm_inst_dispatch(conn->ho.fi, MOHO_EV_BSSMAP_HO_COMMAND, &rx); + } + return 0; +reject: + /* No "Handover Command Reject" message or similar is specified, so we cannot reply in case of + * failure. Or is there?? */ + handover_end(conn, HO_RESULT_ERROR); + return -EINVAL; +} + static int bssmap_rcvmsg_udt(struct bsc_msc_data *msc, struct msgb *msg, unsigned int length) { @@ -953,6 +843,9 @@ static int bssmap_rcvmsg_dt1(struct gsm_subscriber_connection *conn, case BSS_MAP_MSG_LCLS_CONNECT_CTRL: ret = bssmap_handle_lcls_connect_ctrl(conn, msg, length); break; + case BSS_MAP_MSG_HANDOVER_CMD: + ret = bssmap_handle_handover_cmd(conn, msg, length); + break; default: LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg->l4h[0])); @@ -962,6 +855,14 @@ static int bssmap_rcvmsg_dt1(struct gsm_subscriber_connection *conn, return ret; } +int bsc_send_welcome_ussd(struct gsm_subscriber_connection *conn) +{ + bsc_send_ussd_notify(conn, 1, conn->sccp.msc->ussd_welcome_txt); + bsc_send_ussd_release_complete(conn); + + return 0; +} + static int dtap_rcvmsg(struct gsm_subscriber_connection *conn, struct msgb *msg, unsigned int length) { @@ -1066,3 +967,138 @@ int bsc_handle_dt(struct gsm_subscriber_connection *conn, return -1; } + +int bsc_tx_bssmap_ho_required(struct gsm_lchan *lchan, const struct gsm0808_cell_id_list2 *target_cells) +{ + int rc; + struct msgb *msg; + struct gsm0808_handover_required params = { + .cause = GSM0808_CAUSE_BETTER_CELL, + .cil = *target_cells, + .current_channel_type_1_present = true, + .current_channel_type_1 = gsm0808_current_channel_type_1(lchan->type), + }; + + switch (lchan->type) { + case GSM_LCHAN_TCH_F: + case GSM_LCHAN_TCH_H: + params.speech_version_used_present = true; + params.speech_version_used = gsm0808_permitted_speech(lchan->type, + lchan->tch_mode); + if (!params.speech_version_used) { + LOG_HO(lchan->conn, LOGL_ERROR, "Cannot encode Speech Version (Used)" + " for BSSMAP Handover Required message"); + return -EINVAL; + } + break; + default: + break; + } + + msg = gsm0808_create_handover_required(¶ms); + if (!msg) { + LOG_HO(lchan->conn, LOGL_ERROR, "Cannot compose BSSMAP Handover Required message"); + return -EINVAL; + } + + rc = gscon_sigtran_send(lchan->conn, msg); + if (rc) { + LOG_HO(lchan->conn, LOGL_ERROR, "Cannot send BSSMAP Handover Required message"); + return rc; + } + + return 0; +} + +/* Inter-BSC MT HO, new BSS has allocated a channel and sends the RR Handover Command via MSC to the old + * BSS, encapsulated in a BSSMAP Handover Request Acknowledge. */ +int bsc_tx_bssmap_ho_request_ack(struct gsm_subscriber_connection *conn, struct msgb *rr_ho_command) +{ + struct msgb *msg; + struct gsm_lchan *new_lchan = conn->ho.mt.new_lchan; + + msg = gsm0808_create_handover_request_ack(rr_ho_command->data, rr_ho_command->len, + gsm0808_chosen_channel(new_lchan->type, + new_lchan->tch_mode), + new_lchan->encr.alg_id, + gsm0808_permitted_speech(new_lchan->type, + new_lchan->tch_mode)); + msgb_free(rr_ho_command); + if (!msg) + return -ENOMEM; + return osmo_bsc_sigtran_send(conn, msg); +} + +int bsc_tx_bssmap_ho_detect(struct gsm_subscriber_connection *conn) +{ + struct msgb *msg; + msg = gsm0808_create_handover_detect(); + if (!msg) + return -ENOMEM; + + return osmo_bsc_sigtran_send(conn, msg); +} + +enum handover_result bsc_tx_bssmap_ho_complete(struct gsm_subscriber_connection *conn, + struct gsm_lchan *lchan) +{ + int rc; + struct msgb *msg; + struct handover *ho = &conn->ho; + enum gsm0808_lcls_status lcls_status = lcls_get_status(conn); + + struct gsm0808_handover_complete params = { + .chosen_encr_alg_present = true, + .chosen_encr_alg = lchan->encr.alg_id, + + .chosen_channel_present = true, + .chosen_channel = gsm0808_chosen_channel(lchan->type, lchan->tch_mode), + + .lcls_bss_status_present = (lcls_status != 0xff), + .lcls_bss_status = lcls_status, + }; + + /* speech_codec_chosen */ + if (ho->mt.new_lchan->activate.requires_voice_stream + && conn->sccp.msc->a.asp_proto != OSMO_SS7_ASP_PROT_IPA) { + int perm_spch = gsm0808_permitted_speech(lchan->type, lchan->tch_mode); + params.speech_codec_chosen_present = true; + rc = gsm0808_speech_codec_from_chan_type(¶ms.speech_codec_chosen, perm_spch); + if (rc) { + LOG_HO(conn, LOGL_ERROR, "Unable to compose Speech Codec (Chosen)"); + return HO_RESULT_ERROR; + } + } + + msg = gsm0808_create_handover_complete(¶ms); + if (!msg) { + LOG_HO(conn, LOGL_ERROR, "Unable to compose BSSMAP Handover Complete message"); + return HO_RESULT_ERROR; + } + + rc = gscon_sigtran_send(conn, msg); + if (rc) { + LOG_HO(conn, LOGL_ERROR, "Cannot send BSSMAP Handover Complete message"); + return HO_RESULT_ERROR; + } + + return HO_RESULT_OK; +} + +void bsc_tx_bssmap_ho_failure(struct gsm_subscriber_connection *conn) +{ + int rc; + struct msgb *msg; + struct gsm0808_handover_failure params = {}; + + msg = gsm0808_create_handover_failure(¶ms); + if (!msg) { + LOG_HO(conn, LOGL_ERROR, "Unable to compose BSSMAP Handover Failure message"); + return; + } + + rc = gscon_sigtran_send(conn, msg); + if (rc) + LOG_HO(conn, LOGL_ERROR, "Cannot send BSSMAP Handover Failure message (rc=%d %s)", + rc, strerror(-rc)); +} diff --git a/src/osmo-bsc/osmo_bsc_filter.c b/src/osmo-bsc/osmo_bsc_filter.c index 0d0fc29c6..e4d2be1cc 100644 --- a/src/osmo-bsc/osmo_bsc_filter.c +++ b/src/osmo-bsc/osmo_bsc_filter.c @@ -30,203 +30,6 @@ #include -static void handle_lu_request(struct gsm_subscriber_connection *conn, - struct msgb *msg) -{ - struct gsm48_hdr *gh; - struct gsm48_loc_upd_req *lu; - struct gsm48_loc_area_id lai; - - if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*lu)) { - LOGP(DMSC, LOGL_ERROR, "LU too small to look at: %u\n", msgb_l3len(msg)); - return; - } - - gh = msgb_l3(msg); - lu = (struct gsm48_loc_upd_req *) gh->data; - - gsm48_generate_lai2(&lai, bts_lai(conn_get_bts(conn))); - - if (memcmp(&lai, &lu->lai, sizeof(lai)) != 0) { - LOGP(DMSC, LOGL_DEBUG, "Marking con for welcome USSD.\n"); - conn->new_subscriber = 1; - } -} - -/* extract a subscriber from the paging response */ -static struct bsc_subscr *extract_sub(struct gsm_subscriber_connection *conn, - struct msgb *msg) -{ - uint8_t mi_type; - char mi_string[GSM48_MI_SIZE]; - struct gsm48_hdr *gh; - struct gsm48_pag_resp *resp; - struct bsc_subscr *subscr; - - if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*resp)) { - LOGP(DMSC, LOGL_ERROR, "PagingResponse too small: %u\n", msgb_l3len(msg)); - return NULL; - } - - gh = msgb_l3(msg); - resp = (struct gsm48_pag_resp *) &gh->data[0]; - - gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), - mi_string, &mi_type); - DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", - gsm48_mi_type_name(mi_type), mi_string); - - switch (mi_type) { - case GSM_MI_TYPE_TMSI: - subscr = bsc_subscr_find_by_tmsi(conn->network->bsc_subscribers, - tmsi_from_string(mi_string)); - break; - case GSM_MI_TYPE_IMSI: - subscr = bsc_subscr_find_by_imsi(conn->network->bsc_subscribers, - mi_string); - break; - default: - subscr = NULL; - break; - } - - return subscr; -} - -/* we will need to stop the paging request */ -static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg) -{ - struct bsc_subscr *subscr = extract_sub(conn, msg); - - if (!subscr) { - LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n"); - return -1; - } - - paging_request_stop(&conn->network->bts_list, conn_get_bts(conn), subscr, conn, - msg); - bsc_subscr_put(subscr); - return 0; -} - -static int is_cm_service_for_emerg(struct msgb *msg) -{ - struct gsm48_service_request *cm; - struct gsm48_hdr *gh = msgb_l3(msg); - - if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*cm)) { - LOGP(DMSC, LOGL_ERROR, "CM ServiceRequest does not fit.\n"); - return 0; - } - - cm = (struct gsm48_service_request *) &gh->data[0]; - return cm->cm_service_type == GSM48_CMSERV_EMERGENCY; -} - -struct bsc_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn, - struct msgb *msg) -{ - struct gsm48_hdr *gh; - int8_t pdisc; - uint8_t mtype; - struct osmo_bsc_data *bsc; - struct bsc_msc_data *msc, *pag_msc; - struct bsc_subscr *subscr; - int is_emerg = 0; - - bsc = conn->network->bsc_data; - - if (msgb_l3len(msg) < sizeof(*gh)) { - LOGP(DMSC, LOGL_ERROR, "There is no GSM48 header here.\n"); - return NULL; - } - - gh = msgb_l3(msg); - pdisc = gsm48_hdr_pdisc(gh); - mtype = gsm48_hdr_msg_type(gh); - - /* - * We are asked to select a MSC here but they are not equal. We - * want to respond to a paging request on the MSC where we got the - * request from. This is where we need to decide where this connection - * will go. - */ - if (pdisc == GSM48_PDISC_RR && mtype == GSM48_MT_RR_PAG_RESP) - goto paging; - else if (pdisc == GSM48_PDISC_MM && mtype == GSM48_MT_MM_CM_SERV_REQ) { - is_emerg = is_cm_service_for_emerg(msg); - goto round_robin; - } else - goto round_robin; - -round_robin: - llist_for_each_entry(msc, &bsc->mscs, entry) { - if (!msc->is_authenticated) - continue; - if (!is_emerg && msc->type != MSC_CON_TYPE_NORMAL) - continue; - if (is_emerg && !msc->allow_emerg) - continue; - - /* force round robin by moving it to the end */ - llist_move_tail(&msc->entry, &bsc->mscs); - return msc; - } - - return NULL; - -paging: - subscr = extract_sub(conn, msg); - - if (!subscr) { - LOGP(DMSC, LOGL_ERROR, "Got paged but no subscriber found.\n"); - return NULL; - } - - pag_msc = paging_get_msc(conn_get_bts(conn), subscr); - bsc_subscr_put(subscr); - - llist_for_each_entry(msc, &bsc->mscs, entry) { - if (msc != pag_msc) - continue; - - /* - * We don't check if the MSC is connected. In case it - * is not the connection will be dropped. - */ - - /* force round robin by moving it to the end */ - llist_move_tail(&msc->entry, &bsc->mscs); - return msc; - } - - LOGP(DMSC, LOGL_ERROR, "Got paged but no request found.\n"); - return NULL; -} - - -/** - * This is used to scan a message for extra functionality of the BSC. This - * includes scanning for location updating requests/acceptd and then send - * a welcome USSD message to the subscriber. - */ -int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg) -{ - struct gsm48_hdr *gh = msgb_l3(msg); - uint8_t pdisc = gsm48_hdr_pdisc(gh); - uint8_t mtype = gsm48_hdr_msg_type(gh); - - if (pdisc == GSM48_PDISC_MM) { - if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST) - handle_lu_request(conn, msg); - } else if (pdisc == GSM48_PDISC_RR) { - if (mtype == GSM48_MT_RR_PAG_RESP) - handle_page_resp(conn, msg); - } - - return 0; -} - static int send_welcome_ussd(struct gsm_subscriber_connection *conn) { if (!conn->sccp.msc->ussd_welcome_txt) { @@ -237,14 +40,6 @@ static int send_welcome_ussd(struct gsm_subscriber_connection *conn) return BSS_SEND_USSD; } -int bsc_send_welcome_ussd(struct gsm_subscriber_connection *conn) -{ - bsc_send_ussd_notify(conn, 1, conn->sccp.msc->ussd_welcome_txt); - bsc_send_ussd_release_complete(conn); - - return 0; -} - static int bsc_patch_mm_info(struct gsm_subscriber_connection *conn, uint8_t *data, unsigned int length) { diff --git a/src/osmo-bsc/osmo_bsc_grace.c b/src/osmo-bsc/osmo_bsc_grace.c index 705933cb4..2cc3d1a4e 100644 --- a/src/osmo-bsc/osmo_bsc_grace.c +++ b/src/osmo-bsc/osmo_bsc_grace.c @@ -25,6 +25,7 @@ #include #include #include +#include int bsc_grace_allow_new_connection(struct gsm_network *network, struct gsm_bts *bts) { @@ -89,7 +90,7 @@ static int handle_sub(struct gsm_lchan *lchan, const char *text) return -1; /* only when active */ - if (lchan->state != LCHAN_S_ACTIVE) + if (lchan->fi->state != LCHAN_ST_ESTABLISHED) return -1; bsc_send_ussd_notify(conn, 0, text); diff --git a/src/osmo-bsc/osmo_bsc_lcls.c b/src/osmo-bsc/osmo_bsc_lcls.c index c2b076090..14a043769 100644 --- a/src/osmo-bsc/osmo_bsc_lcls.c +++ b/src/osmo-bsc/osmo_bsc_lcls.c @@ -28,6 +28,7 @@ #include #include #include +#include #include struct value_string lcls_event_names[] = { @@ -228,23 +229,23 @@ void lcls_apply_config(struct gsm_subscriber_connection *conn) static void lcls_break_local_switching(struct gsm_subscriber_connection *conn) { - struct mgcp_conn_peer peer; - struct sockaddr_in *sin; + struct mgcp_conn_peer mdcx_info; LOGPFSM(conn->lcls.fi, "=== HERE IS WHERE WE DISABLE LCLS\n"); - if (!conn->user_plane.fi_msc) { + if (!conn->user_plane.mgw_endpoint_ci_msc) { /* the MGCP FSM has died, e.g. due to some MGCP/SDP parsing error */ LOGPFSML(conn->lcls.fi, LOGL_NOTICE, "Cannot disable LCLS without MSC-side MGCP FSM\n"); return; } - sin = (struct sockaddr_in *)&conn->user_plane.aoip_rtp_addr_remote; - OSMO_ASSERT(sin->sin_family == AF_INET); + mdcx_info = (struct mgcp_conn_peer){ + .port = conn->user_plane.msc_assigned_rtp_port, + }; + osmo_strlcpy(mdcx_info.addr, conn->user_plane.msc_assigned_rtp_addr, sizeof(mdcx_info.addr)); - memset(&peer, 0, sizeof(peer)); - peer.port = htons(sin->sin_port); - osmo_strlcpy(peer.addr, inet_ntoa(sin->sin_addr), sizeof(peer.addr)); - mgcp_conn_modify(conn->user_plane.fi_msc, 0, &peer); + mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc, + MGCP_VERB_MDCX, &mdcx_info, + NULL, 0, 0, NULL); } static bool lcls_enable_possible(struct gsm_subscriber_connection *conn) @@ -546,25 +547,34 @@ static void lcls_locally_switched_onenter(struct osmo_fsm_inst *fi, uint32_t pre { struct gsm_subscriber_connection *conn = fi->priv; struct gsm_subscriber_connection *conn_other = conn->lcls.other; - struct mgcp_conn_peer peer; - struct sockaddr_in *sin; + const struct mgcp_conn_peer *other_mgw_info; + struct mgcp_conn_peer mdcx_info; OSMO_ASSERT(conn_other); LOGPFSM(fi, "=== HERE IS WHERE WE ENABLE LCLS\n"); - if (!conn->user_plane.fi_msc) { + if (!conn->user_plane.mgw_endpoint_ci_msc) { + LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without MSC-side MGCP FSM. FIXME\n"); + return; + } + if (!conn_other->user_plane.mgw_endpoint_ci_msc) { LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without MSC-side MGCP FSM. FIXME\n"); return; } - sin = (struct sockaddr_in *)&conn_other->user_plane.aoip_rtp_addr_local; - OSMO_ASSERT(sin->sin_family == AF_INET); - - memset(&peer, 0, sizeof(peer)); - peer.port = htons(sin->sin_port); - osmo_strlcpy(peer.addr, inet_ntoa(sin->sin_addr), sizeof(peer.addr)); - mgcp_conn_modify(conn->user_plane.fi_msc, 0, &peer); + other_mgw_info = mgwep_ci_get_rtp_info(conn_other->user_plane.mgw_endpoint_ci_msc); + if (!other_mgw_info) { + LOGPFSML(fi, LOGL_ERROR, "Cannot enable LCLS without RTP port info of MSC-side" + " -- missing CRCX?\n"); + return; + } + mdcx_info = *other_mgw_info; + /* Make sure the request doesn't want to use the other side's endpoint string. */ + mdcx_info.endpoint[0] = 0; + mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc, + MGCP_VERB_MDCX, &mdcx_info, + NULL, 0, 0, NULL); } static void lcls_locally_switched_wait_break_fn(struct osmo_fsm_inst *fi, uint32_t event, void *data) @@ -764,3 +774,35 @@ struct osmo_fsm lcls_fsm = { .log_subsys = DLCLS, .event_names = lcls_event_names, }; + +/* Add the LCLS BSS Status IE to a BSSMAP message. We assume this is + * called on a msgb that was returned by gsm0808_create_ass_compl() */ +static void bssmap_add_lcls_status(struct msgb *msg, enum gsm0808_lcls_status status) +{ + OSMO_ASSERT(msg->l3h[0] == BSSAP_MSG_BSS_MANAGEMENT); + OSMO_ASSERT(msg->l3h[2] == BSS_MAP_MSG_ASSIGMENT_COMPLETE || + msg->l3h[2] == BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE || + msg->l3h[2] == BSS_MAP_MSG_HANDOVER_COMPLETE || + msg->l3h[2] == BSS_MAP_MSG_HANDOVER_PERFORMED); + OSMO_ASSERT(msgb_tailroom(msg) >= 2); + + /* append IE to end of message */ + msgb_tv_put(msg, GSM0808_IE_LCLS_BSS_STATUS, status); + /* increment the "length" byte in the BSSAP header */ + msg->l3h[1] += 2; +} + +/* Add (append) the LCLS BSS Status IE to a BSSMAP message, if there is any LCLS + * active on the given \a conn */ +void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection *conn, struct msgb *msg) +{ + enum gsm0808_lcls_status status = lcls_get_status(conn); + if (status != 0xff) { + LOGPFSM(conn->fi, "Adding LCLS BSS-Status (%s) to %s\n", + gsm0808_lcls_status_name(status), + gsm0808_bssmap_name(msg->l3h[2])); + bssmap_add_lcls_status(msg, status); + } +} + + diff --git a/src/osmo-bsc/osmo_bsc_main.c b/src/osmo-bsc/osmo_bsc_main.c index 69db32e55..abb869886 100644 --- a/src/osmo-bsc/osmo_bsc_main.c +++ b/src/osmo-bsc/osmo_bsc_main.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include @@ -296,7 +298,8 @@ static void bootstrap_rsl(struct gsm_bts_trx *trx) for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { struct gsm_bts_trx_ts *ts = &trx->ts[i]; generate_ma_for_ts(ts); - gsm_ts_check_init(ts); + OSMO_ASSERT(ts->fi); + osmo_fsm_inst_dispatch(ts->fi, TS_EV_RSL_READY, NULL); } } @@ -306,7 +309,7 @@ static int inp_sig_cb(unsigned int subsys, unsigned int signal, { struct input_signal_data *isd = signal_data; struct gsm_bts_trx *trx = isd->trx; - int ts_no, lchan_no; + int ts_no; /* N. B: we rely on attribute order when parsing response in abis_nm_rx_get_attr_resp() */ const uint8_t bts_attr[] = { NM_ATT_MANUF_ID, NM_ATT_SW_CONFIG, }; const uint8_t trx_attr[] = { NM_ATT_MANUF_STATE, NM_ATT_SW_CONFIG, }; @@ -365,12 +368,8 @@ static int inp_sig_cb(unsigned int subsys, unsigned int signal, */ for (ts_no = 0; ts_no < ARRAY_SIZE(trx->ts); ++ts_no) { struct gsm_bts_trx_ts *ts = &trx->ts[ts_no]; - - for (lchan_no = 0; lchan_no < ARRAY_SIZE(ts->lchan); ++lchan_no) { - if (ts->lchan[lchan_no].state != LCHAN_S_NONE) - lchan_free(&ts->lchan[lchan_no]); - lchan_reset(&ts->lchan[lchan_no]); - } + if (ts->fi) + osmo_fsm_inst_dispatch(ts->fi, TS_EV_OML_DOWN, 0); } gsm_bts_mo_reset(trx->bts); @@ -434,11 +433,9 @@ static int bootstrap_bts(struct gsm_bts *bts) /* Control Channel Description is set from vty/config */ - /* T3212 is set from vty/config */ - /* Set ccch config by looking at ts config */ for (n=0, i=0; i<8; i++) - n += bts->c0->ts[i].pchan == GSM_PCHAN_CCCH ? 1 : 0; + n += bts->c0->ts[i].pchan_is == GSM_PCHAN_CCCH ? 1 : 0; /* Indicate R99 MSC in SI3 */ bts->si_common.chan_desc.mscr = 1; @@ -820,6 +817,8 @@ int main(int argc, char **argv) /* seed the PRNG */ srand(time(NULL)); + mgw_endpoint_fsm_init(bsc_gsmnet->T_defs); + /* Read the config */ rc = bsc_network_configure(config_file); if (rc < 0) { @@ -874,11 +873,6 @@ int main(int argc, char **argv) exit(1); } - if (osmo_bsc_audio_init(bsc_gsmnet) != 0) { - LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n"); - exit(1); - } - handover_decision_1_init(); hodec2_init(bsc_gsmnet); diff --git a/src/osmo-bsc/osmo_bsc_sigtran.c b/src/osmo-bsc/osmo_bsc_sigtran.c index 72ee50f4c..449f1df03 100644 --- a/src/osmo-bsc/osmo_bsc_sigtran.c +++ b/src/osmo-bsc/osmo_bsc_sigtran.c @@ -153,6 +153,47 @@ static int handle_unitdata_from_msc(const struct osmo_sccp_addr *msc_addr, struc return rc; } +static int handle_n_connect_from_msc(struct osmo_sccp_user *scu, struct osmo_scu_prim *scu_prim) +{ + struct bsc_msc_data *msc = get_msc_by_addr(&scu_prim->u.connect.calling_addr); + struct gsm_subscriber_connection *conn; + int rc = 0; + + conn = get_bsc_conn_by_conn_id(scu_prim->u.connect.conn_id); + if (conn) { + LOGP(DMSC, LOGL_NOTICE, + "(calling_addr=%s conn_id=%u) N-CONNECT.ind with already used conn_id, ignoring\n", + osmo_sccp_addr_dump(&scu_prim->u.connect.calling_addr), + scu_prim->u.connect.conn_id); + /* The situation is illogical. A conn was already established with this conn id, if we + * would like to reply with a disconn onto this conn id, we would close the existing + * conn. So just ignore this impossible N-CONNECT completely (including the BSSMAP PDU). */ + return -EINVAL; + } + + if (!msc) { + LOGP(DMSC, LOGL_NOTICE, "(calling_addr=%s conn_id=%u) N-CONNECT.ind from unknown MSC\n", + osmo_sccp_addr_dump(&scu_prim->u.connect.calling_addr), + scu_prim->u.connect.conn_id); + rc = -ENOENT; + goto refuse; + } + + conn = bsc_subscr_con_allocate(bsc_gsmnet); + if (!conn) + return -ENOMEM; + conn->sccp.msc = msc; + conn->sccp.conn_id = scu_prim->u.connect.conn_id; + + /* Take actions asked for by the enclosed PDU */ + osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_CONN_IND, scu_prim); + + return 0; +refuse: + osmo_sccp_tx_disconn(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, 0); + return rc; +} + /* Callback function, called by the SSCP stack when data arrives */ static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) { @@ -171,13 +212,7 @@ static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): /* Handle inbound connections */ DEBUGP(DMSC, "N-CONNECT.ind(X->%u)\n", scu_prim->u.connect.conn_id); - conn = bsc_subscr_con_allocate(bsc_gsmnet); - if (conn) { - conn->sccp.msc = get_msc_by_addr(&scu_prim->u.connect.calling_addr); - /* MSC may be NULL, let the FSM deal with it */ - osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_A_CONN_IND, scu_prim); - } else - LOGP(DMSC, LOGL_ERROR, "Unable to alloc subscr_conn for inbound N-CONNECT.ind\n"); + rc = handle_n_connect_from_msc(scu, scu_prim); break; case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): @@ -191,7 +226,7 @@ static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) if (msgb_l2len(oph->msg) > 0) handle_data_from_msc(conn, oph->msg); } else { - LOGP(DMSC, LOGL_ERROR, "N-CONNET.cfm(%u, %s) for unknown conn?!?\n", + LOGP(DMSC, LOGL_ERROR, "N-CONNECT.cfm(%u, %s) for unknown conn?!?\n", scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); } diff --git a/src/osmo-bsc/paging.c b/src/osmo-bsc/paging.c index b75d7316b..3b09c391e 100644 --- a/src/osmo-bsc/paging.c +++ b/src/osmo-bsc/paging.c @@ -52,6 +52,7 @@ #include #include #include +#include void *tall_paging_ctx = NULL; @@ -394,6 +395,7 @@ void paging_request_stop(struct llist_head *bts_list, log_set_context(LOG_CTX_BSC_SUBSCR, bsub); conn->bsub = bsc_subscr_get(bsub); + gscon_update_id(conn); /* Stop this first and dispatch the request */ if (_bts) { diff --git a/src/osmo-bsc/pcu_sock.c b/src/osmo-bsc/pcu_sock.c index 8a3c331a7..7617b333d 100644 --- a/src/osmo-bsc/pcu_sock.c +++ b/src/osmo-bsc/pcu_sock.c @@ -94,33 +94,6 @@ struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr) return msg; } -/* Helper function exclusivly used by pcu_if_signal_cb() */ -static bool ts_should_be_pdch(struct gsm_bts_trx_ts *ts) { - if (ts->pchan == GSM_PCHAN_PDCH) - return true; - if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) { - /* When we're busy deactivating the PDCH, we first set - * DEACT_PENDING, tell the PCU about it and wait for a - * response. So DEACT_PENDING means "no PDCH" to the PCU. - * Similarly, when we're activating PDCH, we set the - * ACT_PENDING and wait for an activation response from the - * PCU, so ACT_PENDING means "is PDCH". */ - if (ts->flags & TS_F_PDCH_ACTIVE) - return !(ts->flags & TS_F_PDCH_DEACT_PENDING); - else - return (ts->flags & TS_F_PDCH_ACT_PENDING); - } - if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { - /* - * When we're busy de-/activating the PDCH, we first set - * ts->dyn.pchan_want, tell the PCU about it and wait for a - * response. So only care about dyn.pchan_want here. - */ - return ts->dyn.pchan_want == GSM_PCHAN_PDCH; - } - return false; -} - /* Send BTS properties to the PCU */ static int pcu_tx_info_ind(struct gsm_bts *bts) { @@ -232,7 +205,7 @@ static int pcu_tx_info_ind(struct gsm_bts *bts) for (j = 0; j < ARRAY_SIZE(trx->ts); j++) { ts = &trx->ts[j]; if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED - && ts_should_be_pdch(ts)) { + && ts->pchan_is == GSM_PCHAN_PDCH) { info_ind->trx[i].pdch_mask |= (1 << j); info_ind->trx[i].tsc[j] = (ts->tsc >= 0) ? ts->tsc : bts->bsic & 7; @@ -521,7 +494,7 @@ static void pcu_sock_close(struct pcu_sock_state *state) for (j = 0; j < 8; j++) { ts = &trx->ts[j]; if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED - && ts->pchan == GSM_PCHAN_PDCH) { + && ts->pchan_is == GSM_PCHAN_PDCH) { printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n"); } } diff --git a/src/osmo-bsc/system_information.c b/src/osmo-bsc/system_information.c index 071babab6..0852ef571 100644 --- a/src/osmo-bsc/system_information.c +++ b/src/osmo-bsc/system_information.c @@ -1224,6 +1224,7 @@ static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = { int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type) { + int rc; gen_si_fn_t gen_si; switch (bts->gprs.mode) { @@ -1244,8 +1245,15 @@ int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type) sizeof(struct gsm48_si_selection_params)); gen_si = gen_si_fn[si_type]; - if (!gen_si) + if (!gen_si) { + LOGP(DRR, LOGL_ERROR, "bts %u: no gen_si_fn() for SI%s\n", + bts->nr, get_value_string(osmo_sitype_strs, si_type)); return -EINVAL; + } - return gen_si(si_type, bts); + rc = gen_si(si_type, bts); + if (rc < 0) + LOGP(DRR, LOGL_ERROR, "bts %u: Error while generating SI%s: %s (%d)\n", + bts->nr, get_value_string(osmo_sitype_strs, si_type), strerror(-rc), rc); + return rc; } diff --git a/src/osmo-bsc/timeslot_fsm.c b/src/osmo-bsc/timeslot_fsm.c new file mode 100644 index 000000000..65f6889e6 --- /dev/null +++ b/src/osmo-bsc/timeslot_fsm.c @@ -0,0 +1,859 @@ +/* osmo-bsc API to manage timeslot status: init and switch of dynamic PDCH. + * + * (C) 2017 by sysmocom - s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 . + * + */ + +#include + +#include + +#include +#include +#include +#include + +static struct osmo_fsm ts_fsm; + +#define CHAN_ACT_DEACT_TIMEOUT 4 /* TODO: proper T number? */ + +enum ts_fsm_T { + T_CHAN_ACT_DEACT=23001, +}; + +#define GET_TS(fi, TS) \ + struct gsm_bts_trx_ts *TS = fi->priv; \ + OSMO_ASSERT((fi)->fsm == &ts_fsm && (fi)->priv) + +#define GET_BTS_TS(fi, BTS, TS) \ + GET_TS(fi, TS); \ + struct gsm_bts *BTS = TS->trx->bts + +static void ts_fsm_update_id(struct gsm_bts_trx_ts *ts) +{ + osmo_fsm_inst_update_id_f(ts->fi, "%u-%u-%u-%s", ts->trx->bts->nr, ts->trx->nr, ts->nr, + gsm_pchan_id(ts->pchan_on_init)); +} + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) +{ + static bool g_initialized = false; + if (!g_initialized) { + OSMO_ASSERT(osmo_fsm_register(&ts_fsm) == 0); + g_initialized = true; + } + OSMO_ASSERT(!ts->fi); + OSMO_ASSERT(ts->trx); + ts->fi = osmo_fsm_inst_alloc(&ts_fsm, ts->trx, ts, LOGL_DEBUG, NULL); + OSMO_ASSERT(ts->fi); +} + +enum lchan_sanity { + LCHAN_IS_INSANE = -1, + LCHAN_IS_READY_TO_GO, + LCHAN_NEEDS_PCHAN_CHANGE, +}; + +static enum lchan_sanity is_lchan_sane(struct gsm_bts_trx_ts *ts, struct gsm_lchan *lchan) +{ + OSMO_ASSERT(ts); + OSMO_ASSERT(lchan); + if (lchan->ts != ts) + return LCHAN_IS_INSANE; + if (!lchan->fi) + return LCHAN_IS_INSANE; + + if (lchan->type == gsm_lchan_type_by_pchan(ts->pchan_is)) + return LCHAN_IS_READY_TO_GO; + + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + if (lchan->type == GSM_LCHAN_TCH_H) + return LCHAN_NEEDS_PCHAN_CHANGE; + /* fall thru */ + case GSM_PCHAN_TCH_F_PDCH: + if (lchan->type == GSM_LCHAN_TCH_F) + return LCHAN_NEEDS_PCHAN_CHANGE; + /* fall thru */ + default: + return LCHAN_IS_INSANE; + } + +} + +static void lchan_dispatch(struct gsm_lchan *lchan, uint32_t lchan_ev) +{ + if (!lchan->fi) + return; + osmo_fsm_inst_dispatch(lchan->fi, lchan_ev, NULL); + OSMO_ASSERT(lchan->fi->state != LCHAN_ST_WAIT_TS_READY); +} + +static int ts_count_active_lchans(struct gsm_bts_trx_ts *ts) +{ + struct gsm_lchan *lchan; + int count = 0; + + ts_for_each_lchan(lchan, ts) { + if (lchan->fi->state == LCHAN_ST_UNUSED) + continue; + count++; + } + + return count; +} + +static void ts_lchans_dispatch(struct gsm_bts_trx_ts *ts, int lchan_state, uint32_t lchan_ev) +{ + struct gsm_lchan *lchan; + + ts_for_each_lchan(lchan, ts) { + if (lchan_state >= 0 + && !lchan_state_is(lchan, lchan_state)) + continue; + lchan_dispatch(lchan, lchan_ev); + } +} + +static void ts_terminate_lchan_fsms(struct gsm_bts_trx_ts *ts) +{ + struct gsm_lchan *lchan; + + ts_for_each_lchan(lchan, ts) { + osmo_fsm_inst_term(lchan->fi, OSMO_FSM_TERM_REQUEST, NULL); + } +} + +static int ts_lchans_waiting(struct gsm_bts_trx_ts *ts) +{ + struct gsm_lchan *lchan; + int count = 0; + ts_for_each_lchan(lchan, ts) + if (lchan->fi->state == LCHAN_ST_WAIT_TS_READY) + count++; + return count; +} + +static void ts_fsm_error(struct osmo_fsm_inst *fi, uint32_t state_chg, const char *fmt, ...) +{ + GET_TS(fi, ts); + + char *errmsg = NULL; + + if (fmt) { + va_list ap; + + va_start(ap, fmt); + errmsg = talloc_vasprintf(ts->trx, fmt, ap); + va_end(ap); + } + + if (ts->last_errmsg) + talloc_free(ts->last_errmsg); + ts->last_errmsg = errmsg; + + if (errmsg) + LOG_TS(ts, LOGL_ERROR, "%s", errmsg); + + ts_lchans_dispatch(ts, LCHAN_ST_WAIT_TS_READY, LCHAN_EV_TS_ERROR); + if (fi->state != state_chg) + osmo_fsm_inst_state_chg(fi, state_chg, 0, 0); +} + +static void ts_fsm_err_ready_to_go_in_pdch(struct osmo_fsm_inst *fi, struct gsm_lchan *lchan) +{ + /* This shouldn't ever happen, so aggressively mark it. */ + ts_fsm_error(fi, TS_ST_BORKEN, + "Internal error: lchan marked as 'ready to go', but activating" + " any lchan should need PCHAN switchover in state %s (lchan: %s)", + osmo_fsm_inst_state_name(fi), gsm_lchan_name(lchan)); +} + +static void ts_setup_lchans(struct gsm_bts_trx_ts *ts) +{ + int i, max_lchans; + + ts->pchan_on_init = ts->pchan_from_config; + ts_fsm_update_id(ts); + + max_lchans = pchan_subslots(ts->pchan_on_init); + LOG_TS(ts, LOGL_DEBUG, "max lchans: %d", max_lchans); + + for (i = 0; i < max_lchans; i++) { + /* We sometimes receive more than one Channel OPSTART ACK. Not quite sure why, just + * seeing it happen with bts_ipaccess_nanobts. Don't fail on the second init. */ + if (ts->lchan[i].fi) + continue; + lchan_fsm_alloc(&ts->lchan[i]); + } + + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + ts->pchan_is = GSM_PCHAN_NONE; + break; + case GSM_PCHAN_TCH_F_PDCH: + ts->pchan_is = GSM_PCHAN_TCH_F; + break; + default: + ts->pchan_is = ts->pchan_on_init; + break; + } + + LOG_TS(ts, LOGL_DEBUG, "lchans initialized: %d", max_lchans); +} + +static void ts_fsm_not_initialized(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_BTS_TS(fi, bts, ts); + switch (event) { + + case TS_EV_OML_READY: + ts->pdch_act_allowed = true; + ts_setup_lchans(ts); + if (!ts->trx->rsl_link) { + LOG_TS(ts, LOGL_DEBUG, "No RSL link yet"); + return; + } + /* -> UNUSED below */ + break; + + case TS_EV_RSL_READY: + ts->pdch_act_allowed = true; + if (bts->model->oml_is_ts_ready + && !bts->model->oml_is_ts_ready(ts)) { + LOG_TS(ts, LOGL_DEBUG, "OML not ready yet"); + return; + } + /* -> UNUSED below */ + break; + + case TS_EV_LCHAN_REQUESTED: + { + /* TS is not initialized, no lchan can be requested. */ + struct gsm_lchan *lchan = data; + if (lchan && lchan->fi) + osmo_fsm_inst_dispatch(fi, LCHAN_EV_TS_ERROR, NULL); + } + return; + + case TS_EV_LCHAN_UNUSED: + /* ignored. */ + return; + + default: + OSMO_ASSERT(false); + } + + osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0); +} + +static void ts_fsm_unused_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + GET_BTS_TS(fi, bts, ts); + + /* We are entering the unused state. There must by definition not be any lchans waiting to be + * activated. */ + if (ts_lchans_waiting(ts)) { + ts_fsm_error(fi, TS_ST_BORKEN, + "Internal error: entering UNUSED state, but there are lchans waiting to be" + " activated. Not activating them to prevent infinite loops."); + return; + } + + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + case GSM_PCHAN_TCH_F_PDCH: + if (bts->gprs.mode == BTS_GPRS_NONE) { + LOG_TS(ts, LOGL_DEBUG, "GPRS mode is 'none': not activating PDCH."); + return; + } + if (!ts->pdch_act_allowed) { + LOG_TS(ts, LOGL_DEBUG, "PDCH is disabled for this timeslot," + " either due to a PDCH ACT NACK, or from manual VTY command:" + " not activating PDCH. (last error: %s)", + ts->last_errmsg ? : "-"); + return; + } + osmo_fsm_inst_state_chg(fi, TS_ST_WAIT_PDCH_ACT, CHAN_ACT_DEACT_TIMEOUT, + T_CHAN_ACT_DEACT); + break; + + default: + /* nothing to do */ + break; + } +} + +static void ts_fsm_unused(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + + switch (event) { + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + switch (is_lchan_sane(ts, lchan)) { + case LCHAN_NEEDS_PCHAN_CHANGE: + /* Osmocom style dyn TS: in UNUSED state, PDCH is already switched off, + * we merely need to RSL Chan Activ the new lchan. For ip.access style + * dyn TS this is already TCH/F, and we should never hit this. */ + case LCHAN_IS_READY_TO_GO: + osmo_fsm_inst_state_chg(fi, TS_ST_IN_USE, 0, 0); + return; + default: + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_TS_ERROR, NULL); + return; + } + } + + case TS_EV_LCHAN_UNUSED: + /* ignored. */ + return; + + default: + OSMO_ASSERT(false); + } +} + +static inline void ts_fsm_pdch_deact(struct osmo_fsm_inst *fi) +{ + osmo_fsm_inst_state_chg(fi, TS_ST_WAIT_PDCH_DEACT, CHAN_ACT_DEACT_TIMEOUT, T_CHAN_ACT_DEACT); +} + +static void ts_fsm_wait_pdch_act_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_TS(fi, ts); + + rc = rsl_tx_dyn_ts_pdch_act_deact(ts, true); + + /* On error, we couldn't send the activation message and remain unused. */ + if (rc) + ts_fsm_error(fi, TS_ST_UNUSED, "Unable to send PDCH activation"); +} + +static void ts_fsm_wait_pdch_act(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + switch (event) { + + case TS_EV_PDCH_ACT_ACK: + osmo_fsm_inst_state_chg(fi, TS_ST_PDCH, 0, 0); + return; + + case TS_EV_PDCH_ACT_NACK: + if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) + rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + else + rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_CHAN_ACT_NACK]); + ts->pdch_act_allowed = false; + ts_fsm_error(fi, TS_ST_UNUSED, "Received PDCH activation NACK"); + return; + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + switch (is_lchan_sane(ts, lchan)) { + case LCHAN_IS_READY_TO_GO: + /* PDCH activation has not been acked, the previous pchan kind may still + * linger in ts->pchan and make it look like the ts is usable right away. + * But we've started the switchover and must finish that first. */ + case LCHAN_NEEDS_PCHAN_CHANGE: + /* PDCH onenter will see that the lchan is waiting and continue to switch + * off PDCH right away. */ + return; + + default: + lchan_dispatch(lchan, LCHAN_EV_TS_ERROR); + return; + } + } + + case TS_EV_LCHAN_UNUSED: + /* ignored. */ + return; + + default: + OSMO_ASSERT(false); + } +} + +static void ts_fsm_pdch_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int count; + GET_TS(fi, ts); + + /* Set pchan = PDCH status, but double check. */ + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + case GSM_PCHAN_TCH_F_PDCH: + case GSM_PCHAN_PDCH: + ts->pchan_is = GSM_PCHAN_PDCH; + break; + default: + ts_fsm_error(fi, TS_ST_BORKEN, "pchan %s is incapable of activating PDCH", + gsm_pchan_name(ts->pchan_on_init)); + return; + } + + /* PDCH use has changed, tell the PCU about it. */ + pcu_info_update(ts->trx->bts); + + /* If we received TS_EV_LCHAN_REQUESTED in the meantime, go right out of PDCH again. */ + if ((count = ts_lchans_waiting(ts))) { + LOG_TS(ts, LOGL_DEBUG, "%d lchan(s) waiting for usable timeslot", count); + ts_fsm_pdch_deact(fi); + } +} + +static void ts_fsm_pdch(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + switch (event) { + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + switch (is_lchan_sane(ts, lchan)) { + case LCHAN_NEEDS_PCHAN_CHANGE: + ts_fsm_pdch_deact(fi); + return; + + case LCHAN_IS_READY_TO_GO: + ts_fsm_err_ready_to_go_in_pdch(fi, lchan); + return; + + default: + /* Reject just this lchan. */ + lchan_dispatch(lchan, LCHAN_EV_TS_ERROR); + return; + } + } + + case TS_EV_LCHAN_UNUSED: + /* ignored */ + return; + + default: + OSMO_ASSERT(false); + } +} + +static void ts_fsm_wait_pdch_deact_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + int rc; + GET_TS(fi, ts); + + rc = rsl_tx_dyn_ts_pdch_act_deact(ts, false); + + /* On error, we couldn't send the deactivation message and remain in PDCH. */ + if (rc) + ts_fsm_error(fi, TS_ST_PDCH, "Unable to send PDCH deactivation"); +} + +static void ts_fsm_wait_pdch_deact(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + switch (event) { + + case TS_EV_PDCH_DEACT_ACK: + /* Remove pchan = PDCH status, but double check. */ + switch (ts->pchan_on_init) { + case GSM_PCHAN_TCH_F_TCH_H_PDCH: + ts->pchan_is = GSM_PCHAN_NONE; + break; + case GSM_PCHAN_TCH_F_PDCH: + ts->pchan_is = GSM_PCHAN_TCH_F; + break; + default: + ts_fsm_error(fi, TS_ST_BORKEN, "pchan %s is incapable of deactivating PDCH", + gsm_pchan_name(ts->pchan_on_init)); + return; + } + osmo_fsm_inst_state_chg(fi, TS_ST_IN_USE, 0, 0); + /* IN_USE onenter will signal all waiting lchans. */ + + /* PDCH use has changed, tell the PCU about it. */ + pcu_info_update(ts->trx->bts); + return; + + case TS_EV_PDCH_DEACT_NACK: + if (ts->pchan_on_init == GSM_PCHAN_TCH_F_PDCH) + rate_ctr_inc(&ts->trx->bts->bts_ctrs->ctr[BTS_CTR_RSL_IPA_NACK]); + /* For Osmocom style dyn TS, there actually is no NACK, since there is no RF Channel + * Release NACK message in RSL. */ + ts_fsm_error(fi, TS_ST_BORKEN, "Received PDCH deactivation NACK"); + return; + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + switch (is_lchan_sane(ts, lchan)) { + case LCHAN_NEEDS_PCHAN_CHANGE: + /* IN_USE onenter will see that the lchan is waiting and signal it. */ + return; + + case LCHAN_IS_READY_TO_GO: + ts_fsm_err_ready_to_go_in_pdch(fi, lchan); + return; + + default: + /* Reject just this lchan. */ + lchan_dispatch(lchan, LCHAN_EV_TS_ERROR); + return; + } + } + + default: + OSMO_ASSERT(false); + } +} + +static void ts_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_event) +{ + int in_use; + struct gsm_lchan *lchan; + GET_TS(fi, ts); + + /* After being in use, allow PDCH act again, if appropriate. */ + ts->pdch_act_allowed = true; + + ts_lchans_dispatch(ts, LCHAN_ST_WAIT_TS_READY, LCHAN_EV_TS_READY); + + in_use = 0; + ts_for_each_lchan(lchan, ts) { + if (lchan_state_is(lchan, LCHAN_ST_UNUSED)) + continue; + in_use ++; + } + if (!in_use) { + LOG_TS(ts, LOGL_DEBUG, "Entered IN_USE state but no lchans are actually in use now."); + osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0); + } +} + +static void ts_fsm_in_use(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + switch (event) { + case TS_EV_LCHAN_UNUSED: + if (!ts_count_active_lchans(ts)) + osmo_fsm_inst_state_chg(fi, TS_ST_UNUSED, 0, 0); + return; + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + switch (is_lchan_sane(ts, lchan)) { + case LCHAN_IS_READY_TO_GO: + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_TS_READY, NULL); + return; + + case LCHAN_NEEDS_PCHAN_CHANGE: + LOG_TS(ts, LOGL_ERROR, + "cannot activate lchan of mismatching pchan type" + " when the TS is already in use: %s", + gsm_lchan_name(lchan)); + /* fall thru */ + default: + /* Reject just this lchan. */ + lchan_dispatch(lchan, LCHAN_EV_TS_ERROR); + return; + } + } + + default: + OSMO_ASSERT(false); + } +} + +static void ts_fsm_borken(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + case TS_EV_LCHAN_UNUSED: + /* ignored */ + return; + + case TS_EV_LCHAN_REQUESTED: + { + struct gsm_lchan *lchan = data; + lchan_dispatch(lchan, LCHAN_EV_TS_ERROR); + return; + } + + default: + OSMO_ASSERT(false); + } +} + +static int ts_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + switch (fi->state) { + case TS_ST_WAIT_PDCH_ACT: + ts_fsm_error(fi, TS_ST_BORKEN, "PDCH activation timeout"); + return 0; + + case TS_ST_WAIT_PDCH_DEACT: + ts_fsm_error(fi, TS_ST_BORKEN, "PDCH deactivation timeout"); + return 0; + + default: + ts_fsm_error(fi, TS_ST_BORKEN, "Unknown timeout in state %s", + osmo_fsm_inst_state_name(fi)); + return 0; + } +} + +static void ts_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + GET_TS(fi, ts); + switch (event) { + case TS_EV_OML_DOWN: + if (fi->state != TS_ST_NOT_INITIALIZED) + osmo_fsm_inst_state_chg(fi, TS_ST_NOT_INITIALIZED, 0, 0); + OSMO_ASSERT(fi->state == TS_ST_NOT_INITIALIZED); + ts_terminate_lchan_fsms(ts); + ts->pchan_is = ts->pchan_on_init = GSM_PCHAN_NONE; + ts_fsm_update_id(ts); + break; + + case TS_EV_RSL_DOWN: + if (fi->state != TS_ST_NOT_INITIALIZED) + osmo_fsm_inst_state_chg(fi, TS_ST_NOT_INITIALIZED, 0, 0); + OSMO_ASSERT(fi->state == TS_ST_NOT_INITIALIZED); + ts->pchan_is = GSM_PCHAN_NONE; + ts_lchans_dispatch(ts, -1, LCHAN_EV_TS_ERROR); + break; + + default: + OSMO_ASSERT(false); + } +} + +#define S(x) (1 << (x)) + +static const struct osmo_fsm_state ts_fsm_states[] = { + [TS_ST_NOT_INITIALIZED] = { + .name = "NOT_INITIALIZED", + .action = ts_fsm_not_initialized, + .in_event_mask = 0 + | S(TS_EV_OML_READY) + | S(TS_EV_RSL_READY) + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_UNUSED) + , + }, + [TS_ST_UNUSED] = { + .name = "UNUSED", + .onenter = ts_fsm_unused_onenter, + .action = ts_fsm_unused, + .in_event_mask = 0 + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_WAIT_PDCH_ACT) + | S(TS_ST_IN_USE) + | S(TS_ST_NOT_INITIALIZED) + , + }, + [TS_ST_WAIT_PDCH_ACT] = { + .name = "WAIT_PDCH_ACT", + .onenter = ts_fsm_wait_pdch_act_onenter, + .action = ts_fsm_wait_pdch_act, + .in_event_mask = 0 + | S(TS_EV_PDCH_ACT_ACK) + | S(TS_EV_PDCH_ACT_NACK) + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_PDCH) + | S(TS_ST_UNUSED) + | S(TS_ST_BORKEN) + | S(TS_ST_NOT_INITIALIZED) + , + }, + [TS_ST_PDCH] = { + .name = "PDCH", + .onenter = ts_fsm_pdch_onenter, + .action = ts_fsm_pdch, + .in_event_mask = 0 + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_WAIT_PDCH_DEACT) + | S(TS_ST_NOT_INITIALIZED) + , + }, + [TS_ST_WAIT_PDCH_DEACT] = { + .name = "WAIT_PDCH_ACT", + .onenter = ts_fsm_wait_pdch_deact_onenter, + .action = ts_fsm_wait_pdch_deact, + .in_event_mask = 0 + | S(TS_EV_PDCH_DEACT_ACK) + | S(TS_EV_PDCH_DEACT_NACK) + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_IN_USE) + | S(TS_ST_UNUSED) + | S(TS_ST_BORKEN) + | S(TS_ST_NOT_INITIALIZED) + , + }, + [TS_ST_IN_USE] = { + .name = "IN_USE", + .onenter = ts_fsm_in_use_onenter, + .action = ts_fsm_in_use, + .in_event_mask = 0 + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_UNUSED) + | S(TS_ST_NOT_INITIALIZED) + , + }, + [TS_ST_BORKEN] = { + .name = "BORKEN", + .action = ts_fsm_borken, + .in_event_mask = 0 + | S(TS_EV_LCHAN_REQUESTED) + | S(TS_EV_LCHAN_UNUSED) + , + .out_state_mask = 0 + | S(TS_ST_NOT_INITIALIZED) + , + }, + +}; + +static const struct value_string ts_fsm_event_names[] = { + OSMO_VALUE_STRING(TS_EV_OML_READY), + OSMO_VALUE_STRING(TS_EV_OML_DOWN), + OSMO_VALUE_STRING(TS_EV_RSL_READY), + OSMO_VALUE_STRING(TS_EV_RSL_DOWN), + OSMO_VALUE_STRING(TS_EV_LCHAN_REQUESTED), + OSMO_VALUE_STRING(TS_EV_LCHAN_UNUSED), + OSMO_VALUE_STRING(TS_EV_PDCH_ACT_ACK), + OSMO_VALUE_STRING(TS_EV_PDCH_ACT_NACK), + OSMO_VALUE_STRING(TS_EV_PDCH_DEACT_ACK), + OSMO_VALUE_STRING(TS_EV_PDCH_DEACT_NACK), + {} +}; + +static struct osmo_fsm ts_fsm = { + .name = "timeslot", + .states = ts_fsm_states, + .num_states = ARRAY_SIZE(ts_fsm_states), + .timer_cb = ts_fsm_timer_cb, + .log_subsys = DRSL, + .event_names = ts_fsm_event_names, + .allstate_event_mask = 0 + | S(TS_EV_OML_DOWN) + | S(TS_EV_RSL_DOWN) + , + .allstate_action = ts_fsm_allstate, +}; + +/* Return true if any lchans are waiting for this timeslot to become a specific PCHAN. If target_pchan is + * not NULL, also return the PCHAN being waited for. */ +bool ts_is_lchan_waiting_for_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan) +{ + struct gsm_lchan *lchan; + ts_for_each_lchan(lchan, ts) { + if (lchan->fi->state == LCHAN_ST_WAIT_TS_READY) { + if (target_pchan) + *target_pchan = gsm_pchan_by_lchan_type(lchan->type); + return true; + } + } + + if (target_pchan) + *target_pchan = ts->pchan_is; + return false; +} + +/* Return true if we are busy changing the PCHAN kind. If target_pchan is not NULL, also return the PCHAN + * (ultimately) being switched to. */ +bool ts_is_pchan_switching(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config *target_pchan) +{ + switch (ts->fi->state) { + case TS_ST_NOT_INITIALIZED: + case TS_ST_BORKEN: + return false; + default: + break; + } + + /* If an lchan is waiting, return the final pchan after all switching is done. */ + if (ts_is_lchan_waiting_for_pchan(ts, target_pchan)) + return true; + + /* No lchans waiting. Return any ongoing switching. */ + + switch (ts->fi->state) { + case TS_ST_WAIT_PDCH_ACT: + if (target_pchan) + *target_pchan = GSM_PCHAN_PDCH; + return true; + + case TS_ST_WAIT_PDCH_DEACT: + /* If we were switching to a specific pchan kind, an lchan would be waiting. So this must + * be NONE then. */ + if (target_pchan) + *target_pchan = GSM_PCHAN_NONE; + return true; + + default: + return false; + } +} + +/* Does the timeslot's *current* state allow use as this PCHAN kind? If the ts is in switchover, return + * true if the switchover's target PCHAN matches, i.e. an lchan for this pchan kind could be requested + * and will be served after the switch. (Do not check whether any lchans are actually available.) */ +bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts, enum gsm_phys_chan_config as_pchan) +{ + if (!ts_is_usable(ts)) + return false; + + switch (ts->fi->state) { + case TS_ST_IN_USE: + return ts->pchan_is == as_pchan; + + default: + break; + } + + { + enum gsm_phys_chan_config target_pchan; + if (ts_is_lchan_waiting_for_pchan(ts, &target_pchan)) + return target_pchan == as_pchan; + } + + return ts_is_capable_of_pchan(ts, as_pchan); +} diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 42118a05c..914a6042d 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -46,7 +46,6 @@ endif bs11_config_SOURCES = \ bs11_config.c \ - stubs.c \ $(NULL) bs11_config_LDADD = \ @@ -118,7 +117,6 @@ osmo_meas_udp2db_CFLAGS = \ meas_json_SOURCES = \ meas_json.c \ - stubs.c \ $(NULL) meas_json_LDADD = \ diff --git a/src/utils/bs11_config.c b/src/utils/bs11_config.c index ae307c796..7fac3e39a 100644 --- a/src/utils/bs11_config.c +++ b/src/utils/bs11_config.c @@ -980,3 +980,6 @@ int osmo_bsc_sigtran_open_conn(struct gsm_subscriber_connection *conn, struct ms { return 0; } + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} +int abis_rsl_rcvmsg(struct msgb *msg) { return 0; } diff --git a/src/utils/meas_json.c b/src/utils/meas_json.c index b91e78b88..365b450f4 100644 --- a/src/utils/meas_json.c +++ b/src/utils/meas_json.c @@ -187,3 +187,6 @@ int main(int argc, char **argv) exit(0); } + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} +int abis_rsl_rcvmsg(struct msgb *msg) { return 0; } diff --git a/src/utils/stubs.c b/src/utils/stubs.c deleted file mode 100644 index 624797fde..000000000 --- a/src/utils/stubs.c +++ /dev/null @@ -1,36 +0,0 @@ -/* Stubs required for linking */ - -/* (C) 2018 by sysmocom s.f.m.c. GmbH - * - * 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 . - * - */ - -#include -struct gsm_bts_trx_ts; -struct msgb; - -bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts) -{ - /* No TS init required here. */ - return true; -} - -int abis_rsl_rcvmsg(struct msgb *msg) -{ - /* No RSL handling here */ - return 0; -} diff --git a/tests/abis/abis_test.c b/tests/abis/abis_test.c index c6f29f577..5102aea6b 100644 --- a/tests/abis/abis_test.c +++ b/tests/abis/abis_test.c @@ -188,3 +188,4 @@ struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *ne } bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts) { return true; } +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} diff --git a/tests/bsc/Makefile.am b/tests/bsc/Makefile.am index 57e883753..aa416ddfe 100644 --- a/tests/bsc/Makefile.am +++ b/tests/bsc/Makefile.am @@ -11,6 +11,8 @@ AM_CFLAGS = \ $(LIBOSMOABIS_CFLAGS) \ $(LIBOSMOLEGACYMGCP_CFLAGS) \ $(LIBOSMOSIGTRAN_CFLAGS) \ + $(LIBOSMOMGCPCLIENT_CFLAGS) \ + $(LIBOSMOSIGTRAN_CFLAGS) \ $(COVERAGE_CFLAGS) \ $(NULL) @@ -32,26 +34,16 @@ bsc_test_SOURCES = \ bsc_test_LDADD = \ $(top_builddir)/src/osmo-bsc/abis_nm.o \ - $(top_builddir)/src/osmo-bsc/abis_rsl.o \ $(top_builddir)/src/osmo-bsc/arfcn_range_encode.o \ - $(top_builddir)/src/osmo-bsc/bsc_api.o \ - $(top_builddir)/src/osmo-bsc/bsc_dyn_ts.o \ $(top_builddir)/src/osmo-bsc/osmo_bsc_filter.o \ - $(top_builddir)/src/osmo-bsc/bsc_rll.o \ $(top_builddir)/src/osmo-bsc/bsc_subscriber.o \ - $(top_builddir)/src/osmo-bsc/chan_alloc.o \ $(top_builddir)/src/osmo-bsc/gsm_04_08_utils.o \ - $(top_builddir)/src/osmo-bsc/gsm_04_80_utils.o \ $(top_builddir)/src/osmo-bsc/gsm_data.o \ $(top_builddir)/src/osmo-bsc/gsm_timers.o \ $(top_builddir)/src/osmo-bsc/handover_cfg.o \ $(top_builddir)/src/osmo-bsc/handover_logic.o \ $(top_builddir)/src/osmo-bsc/neighbor_ident.o \ $(top_builddir)/src/osmo-bsc/net_init.o \ - $(top_builddir)/src/osmo-bsc/paging.o \ - $(top_builddir)/src/osmo-bsc/pcu_sock.o \ - $(top_builddir)/src/osmo-bsc/rest_octets.o \ - $(top_builddir)/src/osmo-bsc/system_information.o \ $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) \ $(LIBOSMOVTY_LIBS) \ diff --git a/tests/bsc/bsc_test.c b/tests/bsc/bsc_test.c index b05ca547d..f71445f9b 100644 --- a/tests/bsc/bsc_test.c +++ b/tests/bsc/bsc_test.c @@ -252,3 +252,5 @@ void bsc_mr_config(struct gsm_subscriber_connection *conn, struct gsm_lchan *lch void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn, struct msgb *msg, int link_id, int allow_sacch) {} void gscon_dtap_cache_flush(struct gsm_subscriber_connection *conn, int send) {} +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} +void lchan_activate(struct gsm_lchan *lchan, void *info) {} diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c index 9552fb11d..bc2777d3c 100644 --- a/tests/gsm0408/gsm0408_test.c +++ b/tests/gsm0408/gsm0408_test.c @@ -852,3 +852,5 @@ bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts) { return true; } + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} diff --git a/tests/handover/Makefile.am b/tests/handover/Makefile.am index b12b5cb74..c755c91ff 100644 --- a/tests/handover/Makefile.am +++ b/tests/handover/Makefile.am @@ -32,8 +32,9 @@ handover_test_SOURCES = \ handover_test.c \ $(NULL) -handover_test_LDFLAGS =\ - -Wl,--wrap=abis_rsl_sendmsg,--wrap=mgcp_conn_modify,--wrap=mgcp_conn_delete\ +handover_test_LDFLAGS = \ + -Wl,--wrap=abis_rsl_sendmsg \ + -Wl,--wrap=mgw_endpoint_ci_request \ $(NULL) handover_test_LDADD = \ @@ -41,8 +42,8 @@ handover_test_LDADD = \ $(top_builddir)/src/osmo-bsc/abis_nm.o \ $(top_builddir)/src/osmo-bsc/abis_rsl.o \ $(top_builddir)/src/osmo-bsc/arfcn_range_encode.o \ + $(top_builddir)/src/osmo-bsc/assignment_fsm.o \ $(top_builddir)/src/osmo-bsc/bsc_api.o \ - $(top_builddir)/src/osmo-bsc/bsc_dyn_ts.o \ $(top_builddir)/src/osmo-bsc/bsc_init.o \ $(top_builddir)/src/osmo-bsc/bsc_rll.o \ $(top_builddir)/src/osmo-bsc/bsc_subscr_conn_fsm.o \ @@ -58,16 +59,21 @@ handover_test_LDADD = \ $(top_builddir)/src/osmo-bsc/handover_cfg.o \ $(top_builddir)/src/osmo-bsc/handover_decision.o \ $(top_builddir)/src/osmo-bsc/handover_decision_2.o \ + $(top_builddir)/src/osmo-bsc/handover_fsm.o \ $(top_builddir)/src/osmo-bsc/handover_logic.o \ + $(top_builddir)/src/osmo-bsc/lchan_fsm.o \ + $(top_builddir)/src/osmo-bsc/lchan_select.o \ $(top_builddir)/src/osmo-bsc/meas_rep.o \ + $(top_builddir)/src/osmo-bsc/mgw_endpoint_fsm.o \ $(top_builddir)/src/osmo-bsc/neighbor_ident.o \ - $(top_builddir)/src/osmo-bsc/osmo_bsc_lcls.o \ $(top_builddir)/src/osmo-bsc/net_init.o \ + $(top_builddir)/src/osmo-bsc/osmo_bsc_lcls.o \ $(top_builddir)/src/osmo-bsc/paging.o \ $(top_builddir)/src/osmo-bsc/pcu_sock.o \ $(top_builddir)/src/osmo-bsc/penalty_timers.o \ $(top_builddir)/src/osmo-bsc/rest_octets.o \ $(top_builddir)/src/osmo-bsc/system_information.o \ + $(top_builddir)/src/osmo-bsc/timeslot_fsm.o \ $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) \ $(LIBOSMOABIS_LIBS) \ diff --git a/tests/handover/handover_test.c b/tests/handover/handover_test.c index 769fd069e..b753165a6 100644 --- a/tests/handover/handover_test.c +++ b/tests/handover/handover_test.c @@ -32,67 +32,42 @@ #include #include #include -#include +#include +#include #include #include +#include #include #include #include #include #include #include +#include +#include void *ctx; struct gsm_network *bsc_gsmnet; -/* override, requires '-Wl,--wrap=mgcp_conn_modify'. +/* override, requires '-Wl,--wrap=mgw_endpoint_ci_request'. * Catch modification of an MGCP connection. */ -int __real_mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer); -int __wrap_mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer) +void __real_mgw_endpoint_ci_request(struct mgwep_ci *ci, + enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data); +void __wrap_mgw_endpoint_ci_request(struct mgwep_ci *ci, + enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info, + struct osmo_fsm_inst *notify, + uint32_t event_success, uint32_t event_failure, + void *notify_data) { - /* CAUTION HACK: - * - * The pointer fi is misused to pass a reference to GSCON FSM ! - * - * This function is called from gscon_fsm_wait_ho_compl() from - * bsc_subscr_conn_fsm.c when GSCON_EV_HO_COMPL is dispatched to the - * GSCON FSM. By then, the GSCON FSM has already changed to the state - * ST_WAIT_MDCX_BTS_HO (see gscon_fsm_wait_mdcx_bts_ho()) and waits for - * GSCON_EV_MGW_MDCX_RESP_BTS. The signal GSCON_EV_MGW_MDCX_RESP_BTS - * is sent to this function using the parameter parent_evt. So we - * implicitly know the event that is needed to simulate a successful - * MGW negotiation to the GSCON FSM. All we need to do is to dispatch - * parent_evt back to the GSCON FSM in order to make it think that the - * MGW negotiation is done. - * - * Unfortunately, there is a problem with this test implementation. - * in order to simplfy the test we do not allocate any MGCP Client - * FSM but the GSCON FSM will call this function with the fi pointer - * pointing to the MGCP Client FSM. This means we get a nullpointer - * here and there is no way to distinguish which GSCON FSM called - * the function at all (normally we would know through the parent - * pointer). - * - * To get around this problem we populate the fi pointer with the - * reference to the GSCON FSM itsself, so we can know who called the - * function. This is a misuse of the pointer since it normally would - * hold an MGCP Client FSM instead of a GSCON FSM. - * - * See also note in function create_conn() */ - - osmo_fsm_inst_dispatch(fi, parent_evt, NULL); - return 0; -} - -/* override, requires '-Wl,--wrap=mgcp_conn_delete'. - * Catch deletion of an MGCP connection. */ -int __real_mgcp_conn_delete(struct osmo_fsm_inst *fi); -int __wrap_mgcp_conn_delete(struct osmo_fsm_inst *fi) -{ - /* Just do nothing and pretend that everything went well. - * We never have allocatec any MGCP connections. */ - return 0; + struct mgcp_conn_peer fake_data = {}; + /* All MGCP shall be successful */ + if (!notify) + return; + osmo_fsm_inst_dispatch(notify, event_success, &fake_data); } /* measurement report */ @@ -225,14 +200,14 @@ static struct gsm_bts *create_bts(int arfcn) /* 4 full rate and 4 half rate channels */ for (i = 1; i <= 6; i++) { - bts->c0->ts[i].pchan = - (i < 5) ? GSM_PCHAN_TCH_F : GSM_PCHAN_TCH_H; + bts->c0->ts[i].pchan_from_config = (i < 5) ? GSM_PCHAN_TCH_F : GSM_PCHAN_TCH_H; bts->c0->ts[i].mo.nm_state.operational = NM_OPSTATE_ENABLED; bts->c0->ts[i].mo.nm_state.availability = NM_AVSTATE_OK; - bts->c0->ts[i].lchan[0].type = GSM_LCHAN_NONE; - bts->c0->ts[i].lchan[0].state = LCHAN_S_NONE; - bts->c0->ts[i].lchan[1].type = GSM_LCHAN_NONE; - bts->c0->ts[i].lchan[1].state = LCHAN_S_NONE; + } + + for (i = 0; i < ARRAY_SIZE(bts->c0->ts); i++) { + /* make sure ts->lchans[] get initialized */ + osmo_fsm_inst_dispatch(bts->c0->ts[i].fi, TS_EV_OML_READY, 0); } return bts; } @@ -243,18 +218,14 @@ void create_conn(struct gsm_lchan *lchan) char imsi[sizeof(lchan->conn->bsub->imsi)]; struct gsm_network *net = lchan->ts->trx->bts->network; struct gsm_subscriber_connection *conn; + struct mgcp_client *fake_mgcp_client = (void*)talloc_zero(net, int); conn = bsc_subscr_con_allocate(net); - /* CAUTION HACK: When __real_mgcp_conn_modify() is called by the GSCON - * FSM, then we need to know the reference to caller FSM (GSCON FSM). - * Unfortunately the function __real_mgcp_conn_modify() is called with - * fi_bts, which is unpopulated in this setup. The real function would - * perform the communication with the MGW and then dispatch a signal - * back to the parent FSM. Since we do not have all that in this setup - * we populate the fi_bts pointer with a reference to the GSCON FSM in - * order to have it available later in __real_mgcp_conn_modify(). */ - conn->user_plane.fi_bts = conn->fi; + conn->user_plane.mgw_endpoint = mgw_endpoint_alloc(conn->fi, + GSCON_EV_FORGET_MGW_ENDPOINT, + fake_mgcp_client, "test", + "fake endpoint"); lchan->conn = conn; conn->lchan = lchan; @@ -274,13 +245,17 @@ struct gsm_lchan *create_lchan(struct gsm_bts *bts, int full_rate, char *codec) { struct gsm_lchan *lchan; - lchan = lchan_alloc(bts, - (full_rate) ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H, 0); + lchan = lchan_select_by_type(bts, (full_rate) ? GSM_LCHAN_TCH_F : GSM_LCHAN_TCH_H); if (!lchan) { printf("No resource for lchan\n"); exit(EXIT_FAILURE); } - lchan->state = LCHAN_S_ACTIVE; + + /* serious hack into osmo_fsm */ + lchan->fi->state = LCHAN_ST_ESTABLISHED; + lchan->ts->fi->state = TS_ST_IN_USE; + LOG_LCHAN(lchan, LOGL_DEBUG, "activated by handover_test.c"); + create_conn(lchan); if (!strcasecmp(codec, "FR") && full_rate) lchan->tch_mode = GSM48_CMODE_SPEECH_V1; @@ -305,7 +280,6 @@ struct gsm_lchan *create_lchan(struct gsm_bts *bts, int full_rate, char *codec) }, .len = 5, }; - lchan->conn->codec_list_present = true; return lchan; } @@ -1336,6 +1310,18 @@ static const struct log_info_cat log_categories[] = { .color = "\033[1;35m", .enabled = 1, .loglevel = LOGL_DEBUG, }, + [DRR] = { + .name = "DRR", + .description = "RR", + .color = "\033[1;35m", + .enabled = 1, .loglevel = LOGL_DEBUG, + }, + [DRLL] = { + .name = "DRLL", + .description = "RLL", + .color = "\033[1;35m", + .enabled = 1, .loglevel = LOGL_DEBUG, + }, [DMSC] = { .name = "DMSC", .description = "Mobile Switching Center", @@ -1380,11 +1366,14 @@ int main(int argc, char **argv) log_set_print_category(osmo_stderr_target, 1); log_set_print_category_hex(osmo_stderr_target, 0); log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME); + osmo_fsm_log_addr(false); bsc_network_alloc(); if (!bsc_gsmnet) exit(1); + mgw_endpoint_fsm_init(bsc_gsmnet->T_defs); + ho_set_algorithm(bsc_gsmnet->ho, 2); ho_set_ho_active(bsc_gsmnet->ho, true); ho_set_hodec2_as_active(bsc_gsmnet->ho, true); @@ -1429,7 +1418,7 @@ int main(int argc, char **argv) for (i = 0; i < n; i++) bts[bts_num + i] = create_bts(arfcn++); for (i = 0; i < n; i++) { - if (gsm_generate_si(bts[bts_num + i], SYSINFO_TYPE_2)) + if (gsm_generate_si(bts[bts_num + i], SYSINFO_TYPE_2) <= 0) fprintf(stderr, "Error generating SI2\n"); } bts_num += n; @@ -1670,6 +1659,27 @@ int main(int argc, char **argv) *test_case); return EXIT_FAILURE; } + + { + /* Help the lchan out of releasing states */ + struct gsm_bts *bts; + llist_for_each_entry(bts, &bsc_gsmnet->bts_list, list) { + struct gsm_bts_trx *trx; + llist_for_each_entry(trx, &bts->trx_list, list) { + int ts_nr; + for (ts_nr = 0; ts_nr < TRX_NR_TS; ts_nr++) { + struct gsm_lchan *lchan; + ts_for_each_lchan(lchan, &trx->ts[ts_nr]) { + + if (lchan->fi && lchan->fi->state == LCHAN_ST_WAIT_BEFORE_RF_RELEASE) { + osmo_fsm_inst_state_chg(lchan->fi, LCHAN_ST_WAIT_RF_RELEASE_ACK, 0, 0); + osmo_fsm_inst_dispatch(lchan->fi, LCHAN_EV_RSL_RF_CHAN_REL_ACK, 0); + } + } + } + } + } + } } for (i = 0; i < lchan_num; i++) { @@ -1677,7 +1687,6 @@ int main(int argc, char **argv) lchan[i]->conn = NULL; conn->lchan = NULL; osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_REGULAR, NULL); - lchan_free(lchan[i]); } fprintf(stderr, "--------------------\n"); @@ -1708,10 +1717,18 @@ int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, uint1 { return 0; } void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) {} void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause) {} -void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause) {} int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause) { return 0; } void bsc_cm_update(struct gsm_subscriber_connection *conn, const uint8_t *cm2, uint8_t cm2_len, const uint8_t *cm3, uint8_t cm3_len) {} void bsc_mr_config(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan, int full_rate) {} +struct gsm0808_handover_required; +int bsc_tx_bssmap_ho_required(struct gsm_lchan *lchan, const struct gsm0808_cell_id_list2 *target_cells) +{ return 0; } +int bsc_tx_bssmap_ho_request_ack(struct gsm_subscriber_connection *conn, struct msgb *rr_ho_command) +{ return 0; } +int bsc_tx_bssmap_ho_detect(struct gsm_subscriber_connection *conn) { return 0; } +enum handover_result bsc_tx_bssmap_ho_complete(struct gsm_subscriber_connection *conn, + struct gsm_lchan *lchan) { return HO_RESULT_OK; } +void bsc_tx_bssmap_ho_failure(struct gsm_subscriber_connection *conn) {} diff --git a/tests/handover/neighbor_ident_test.c b/tests/handover/neighbor_ident_test.c index b67219cdf..25df3e643 100644 --- a/tests/handover/neighbor_ident_test.c +++ b/tests/handover/neighbor_ident_test.c @@ -28,7 +28,7 @@ #include -struct neighbor_ident_list *nil; +static struct neighbor_ident_list *nil; static const struct neighbor_ident_key *k(int from_bts, uint16_t arfcn, enum bsic_kind kind, uint16_t bsic) { @@ -106,7 +106,7 @@ static const struct gsm0808_cell_id_list2 lac2 = { }, }; -void print_cil(const struct gsm0808_cell_id_list2 *cil) +static void print_cil(const struct gsm0808_cell_id_list2 *cil) { unsigned int i; if (!cil) { @@ -133,7 +133,7 @@ void print_cil(const struct gsm0808_cell_id_list2 *cil) static int print_nil_i; -bool nil_cb(const struct neighbor_ident_key *key, const struct gsm0808_cell_id_list2 *val, +static bool nil_cb(const struct neighbor_ident_key *key, const struct gsm0808_cell_id_list2 *val, void *cb_data) { printf(" %2d: %s\n", print_nil_i++, neighbor_ident_key_name(key)); @@ -141,7 +141,7 @@ bool nil_cb(const struct neighbor_ident_key *key, const struct gsm0808_cell_id_l return true; } -void print_nil() +static void print_nil() { print_nil_i = 0; neighbor_ident_iter(nil, nil_cb, NULL); diff --git a/tests/nanobts_omlattr/nanobts_omlattr_test.c b/tests/nanobts_omlattr/nanobts_omlattr_test.c index 7a3a80e2d..38729ac35 100644 --- a/tests/nanobts_omlattr/nanobts_omlattr_test.c +++ b/tests/nanobts_omlattr/nanobts_omlattr_test.c @@ -317,3 +317,5 @@ struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *ne bool on_gsm_ts_init(struct gsm_bts_trx_ts *ts) { return true; } + +void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} -- cgit v1.2.3