Review at https://gerrit.osmocom.org/4334
mgcp: use osmo-mgw to switch RTP streams osmo-bsc currently negotiates the RTP stream directly with the BTS and reports back the RTP IP/Port on the BTS. This works fine for a single BTS, but for Handover the port/ip pointing to the MSC side must not change, so an entity in between the BTSs and the MSC is required. Integrate the mgcp-client and use osmo-mgw to switch the RTP streams. TODO: Handover will not work yet, because there is no functionality to update the connection with the port/ip of the new BTS. Change-Id: Ia2882b7ca31a3219c676986e85045fa08a425d7a mgcp: Integrate osmo-mgw / mgcp --- M configure.ac M include/osmocom/bsc/Makefile.am M include/osmocom/bsc/gsm_data.h M include/osmocom/bsc/osmo_bsc.h A include/osmocom/bsc/osmo_bsc_mgcp.h M src/Makefile.am M src/osmo-bsc/Makefile.am M src/osmo-bsc/osmo_bsc_audio.c M src/osmo-bsc/osmo_bsc_bssap.c M src/osmo-bsc/osmo_bsc_main.c A src/osmo-bsc/osmo_bsc_mgcp.c M src/osmo-bsc/osmo_bsc_sigtran.c M src/osmo-bsc/osmo_bsc_vty.c 13 files changed, 1,035 insertions(+), 67 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/34/4334/1 diff --git a/configure.ac b/configure.ac index 4edbb83..f73f33c 100644 --- a/configure.ac +++ b/configure.ac @@ -47,8 +47,9 @@ PKG_CHECK_MODULES(LIBOSMONETIF, libosmo-netif >= 0.0.1) PKG_CHECK_MODULES(LIBOSMOSIGTRAN, libosmo-sigtran) # TODO version? PKG_CHECK_MODULES(LIBCRYPTO, libcrypto >= 0.9.5) -PKG_CHECK_MODULES(LIBOSMOLEGACYMGCP, libosmo-legacy-mgcp >= 0.0.1) PKG_CHECK_MODULES(LIBOSMOSCCP, libosmo-sccp >= 0.0.2) +PKG_CHECK_MODULES(LIBOSMOMGCPCLIENT, libosmo-mgcp-client >= 1.0.0) +PKG_CHECK_MODULES(LIBOSMOLEGACYMGCP, libosmo-legacy-mgcp >= 1.0.0) dnl checks for header files AC_HEADER_STDC @@ -135,7 +136,6 @@ src/libfilter/Makefile src/libcommon-cs/Makefile src/osmo-bsc/Makefile - src/osmo-bsc_nat/Makefile src/ipaccess/Makefile src/utils/Makefile tests/Makefile diff --git a/include/osmocom/bsc/Makefile.am b/include/osmocom/bsc/Makefile.am index 8ad2b5d..1f7cd39 100644 --- a/include/osmocom/bsc/Makefile.am +++ b/include/osmocom/bsc/Makefile.am @@ -41,6 +41,7 @@ openbscdefines.h \ osmo_bsc.h \ osmo_bsc_grace.h \ + osmo_bsc_mgcp.h \ osmo_bsc_rf.h \ osmo_bsc_sigtran.h \ bsc_msc_data.h \ diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h index 51b2c98..5cb7efd 100644 --- a/include/osmocom/bsc/gsm_data.h +++ b/include/osmocom/bsc/gsm_data.h @@ -479,6 +479,11 @@ uint8_t t3212; struct { + struct mgcp_client_conf *conf; + struct mgcp_client *client; + } mgw; + + struct { /* CS7 instance id number (set via VTY) */ uint32_t cs7_instance; /* A list with the context information about diff --git a/include/osmocom/bsc/osmo_bsc.h b/include/osmocom/bsc/osmo_bsc.h index 5ebea50..addad46 100644 --- a/include/osmocom/bsc/osmo_bsc.h +++ b/include/osmocom/bsc/osmo_bsc.h @@ -28,6 +28,21 @@ uint16_t cic; uint32_t rtp_ip; int rtp_port; + uint16_t rtp_endpoint; + + /* 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; + + /* storage to keep states of the MGCP connection handler, the + * handler is created when an assignment request is received + * and is terminated when the assignment complete message is + * sent */ + struct mgcp_ctx *mgcp_ctx; /* for advanced ping/pong */ int send_ping; @@ -72,4 +87,6 @@ struct llist_head *bsc_access_lists(void); +int bssmap_send_aoip_ass_compl(struct gsm_lchan *lchan); + #endif diff --git a/include/osmocom/bsc/osmo_bsc_mgcp.h b/include/osmocom/bsc/osmo_bsc_mgcp.h new file mode 100644 index 0000000..da40fdb --- /dev/null +++ b/include/osmocom/bsc/osmo_bsc_mgcp.h @@ -0,0 +1,44 @@ +/* (C) 2017 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Philipp Maier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#pragma once + +/* MGCP state handler context (fsm etc..) */ +struct mgcp_ctx { + /* FSM instance, which handles the connection switching procedure */ + struct osmo_fsm_inst *fsm; + + /* A human readable name to display in the logs */ + char name[256]; + + /* Copy of the pointer and the data with context information + * needed to process the AoIP and MGCP requests (system data) */ + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + int chan_mode; + int full_rate; + struct gsm_lchan *lchan; + struct msgb *resp; +}; + +struct mgcp_ctx *mgcp_assignm_req(void *ctx, struct gsm_network *network, + struct osmo_bsc_sccp_con *conn, int chan_mode, int full_rate); +void mgcp_clear_complete(struct mgcp_ctx *mgcp_ctx, struct msgb *resp); +void mgcp_ass_complete(struct mgcp_ctx *mgcp_ctx, struct gsm_lchan *lchan); diff --git a/src/Makefile.am b/src/Makefile.am index d04f025..dd1ad3d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -33,5 +33,4 @@ utils \ ipaccess \ osmo-bsc \ - osmo-bsc_nat \ $(NULL) diff --git a/src/osmo-bsc/Makefile.am b/src/osmo-bsc/Makefile.am index dfc4def..7db698c 100644 --- a/src/osmo-bsc/Makefile.am +++ b/src/osmo-bsc/Makefile.am @@ -15,6 +15,7 @@ $(COVERAGE_CFLAGS) \ $(LIBOSMOABIS_CFLAGS) \ $(LIBOSMOSIGTRAN_CFLAGS) \ + $(LIBOSMOMGCPCLIENT_CFLAGS) \ $(NULL) AM_LDFLAGS = \ @@ -30,6 +31,7 @@ osmo_bsc_vty.c \ osmo_bsc_api.c \ osmo_bsc_grace.c \ + osmo_bsc_mgcp.c \ osmo_bsc_msc.c \ osmo_bsc_sigtran.c \ osmo_bsc_filter.c \ @@ -53,4 +55,5 @@ $(COVERAGE_LDFLAGS) \ $(LIBOSMOABIS_LIBS) \ $(LIBOSMOSIGTRAN_LIBS) \ + $(LIBOSMOMGCPCLIENT_LIBS) \ $(NULL) diff --git a/src/osmo-bsc/osmo_bsc_audio.c b/src/osmo-bsc/osmo_bsc_audio.c index 94aa350..326703d 100644 --- a/src/osmo-bsc/osmo_bsc_audio.c +++ b/src/osmo-bsc/osmo_bsc_audio.c @@ -29,46 +29,9 @@ #include <osmocom/gsm/gsm0808.h> #include <osmocom/gsm/gsm0808_utils.h> #include <osmocom/bsc/osmo_bsc_sigtran.h> +#include <osmocom/bsc/osmo_bsc_mgcp.h> #include <arpa/inet.h> - -/* Generate and send assignment complete message */ -static int send_aoip_ass_compl(struct gsm_subscriber_connection *conn, struct gsm_lchan *lchan) -{ - struct msgb *resp; - struct sockaddr_storage rtp_addr; - struct sockaddr_in rtp_addr_in; - struct gsm0808_speech_codec sc; - - OSMO_ASSERT(lchan->abis_ip.ass_compl.valid == true); - - /* Package RTP-Address data */ - memset(&rtp_addr_in, 0, sizeof(rtp_addr_in)); - rtp_addr_in.sin_family = AF_INET; - rtp_addr_in.sin_port = htons(lchan->abis_ip.bound_port); - rtp_addr_in.sin_addr.s_addr = htonl(lchan->abis_ip.bound_ip); - memset(&rtp_addr, 0, sizeof(rtp_addr)); - memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in)); - - /* Extrapolate speech codec from speech mode */ - gsm0808_speech_codec_from_chan_type(&sc, lchan->abis_ip.ass_compl.speech_mode); - - /* Generate message */ - resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause, - lchan->abis_ip.ass_compl.chosen_channel, - lchan->abis_ip.ass_compl.encr_alg_id, - lchan->abis_ip.ass_compl.speech_mode, - &rtp_addr, - &sc, - NULL); - - if (!resp) { - LOGP(DMSC, LOGL_ERROR, "Failed to generate assignment completed message!\n"); \ - return -EINVAL; - } - - return osmo_bsc_sigtran_send(conn->sccp_con, resp); -} static int handle_abisip_signal(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data) @@ -124,11 +87,9 @@ * IPA based base stations. See also osmo_bsc_api.c, * function bsc_assign_compl() */ LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL (POSTPONED)\n"); - if (send_aoip_ass_compl(con, lchan) != 0) - return -EINVAL; + mgcp_ass_complete(con->sccp_con->mgcp_ctx, lchan); } break; - break; } return 0; diff --git a/src/osmo-bsc/osmo_bsc_bssap.c b/src/osmo-bsc/osmo_bsc_bssap.c index 4311250..320c0ef 100644 --- a/src/osmo-bsc/osmo_bsc_bssap.c +++ b/src/osmo-bsc/osmo_bsc_bssap.c @@ -24,7 +24,8 @@ #include <osmocom/bsc/bsc_msc_data.h> #include <osmocom/bsc/debug.h> #include <osmocom/bsc/bsc_subscriber.h> -#include <osmocom/legacy_mgcp/mgcp.h> +#include <osmocom/mgcp_client/mgcp_client.h> +#include <osmocom/bsc/osmo_bsc_mgcp.h> #include <osmocom/bsc/paging.h> #include <osmocom/bsc/gsm_04_08_utils.h> @@ -321,14 +322,29 @@ conn->conn = NULL; } - /* send the clear complete message */ + /* generate the clear complete message */ resp = gsm0808_create_clear_complete(); if (!resp) { LOGP(DMSC, LOGL_ERROR, "Sending clear complete failed.\n"); return -1; } - osmo_bsc_sigtran_send(conn, resp); + if (conn->mgcp_ctx) { + /* NOTE: This is the AoIP case, osmo-bsc has to negotiate with + * the MGCP-GW. For this an mgcp_ctx should be created that + * contains the FSM and some system data. When the connection + * is removed from the MGCP-GW, then osmo_bsc_sigtran_send() + * calls osmo_bsc_sigtran_send(). */ + mgcp_clear_complete(conn->mgcp_ctx, resp); + conn->mgcp_ctx = NULL; + } else { + /* NOTE: This is the SCCP-Lite case, since we do not handle + * the MGCP-GW switching ourselves, we may skip everything + * that is MGCP-GW related and sent the clear complete message + * directly */ + osmo_bsc_sigtran_send(conn, resp); + } + return 0; } @@ -426,7 +442,6 @@ int port, full_rate = -1; bool aoip = false; struct sockaddr_storage rtp_addr; - struct sockaddr_in *rtp_addr_in; struct gsm0808_channel_type ct; struct gsm0808_speech_codec_list scl; struct gsm0808_speech_codec_list *scl_ptr = NULL; @@ -531,29 +546,35 @@ 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)); - if (aoip == false) { - /* map it to a MGCP Endpoint and a RTP port */ + /* Forward the assingment request to lower layers */ + 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->aoip_rtp_addr_remote, &rtp_addr, sizeof(rtp_addr)); + + /* Create an assignment request using the MGCP fsm. This FSM + * is directly started when its created (now) and will also + * take care about the further processing (creating RTP + * endpoints, calling gsm0808_assign_req(), rsponding to + * the assignment request etc... */ + conn->mgcp_ctx = mgcp_assignm_req(conn, msc->network, conn, chan_mode, full_rate); + if (!conn->mgcp_ctx) + 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. */ port = mgcp_timeslot_to_endpoint(multiplex, timeslot); conn->rtp_port = rtp_calculate_port(port, msc->rtp_base); conn->rtp_ip = 0; - } else { - /* use address / port supplied with the AoIP - * transport address element */ - if (rtp_addr.ss_family == AF_INET) { - rtp_addr_in = (struct sockaddr_in *)&rtp_addr; - conn->rtp_port = osmo_ntohs(rtp_addr_in->sin_port); - memcpy(&conn->rtp_ip, &rtp_addr_in->sin_addr.s_addr, - IP_V4_ADDR_LEN); - conn->rtp_ip = osmo_ntohl(conn->rtp_ip); - } else { - LOGP(DMSC, LOGL_ERROR, - "Unsopported addressing scheme. (supports only IPV4)\n"); - goto reject; - } + return gsm0808_assign_req(conn->conn, chan_mode, full_rate); } - - return gsm0808_assign_req(conn->conn, chan_mode, full_rate); - + reject: resp = gsm0808_create_assignment_failure @@ -729,3 +750,38 @@ return -1; } + +/* Generate and send assignment complete message */ +int bssmap_send_aoip_ass_compl(struct gsm_lchan *lchan) +{ + struct msgb *resp; + struct gsm0808_speech_codec sc; + struct gsm_subscriber_connection *conn; + + conn = lchan->conn; + + OSMO_ASSERT(lchan->abis_ip.ass_compl.valid == true); + OSMO_ASSERT(conn); + OSMO_ASSERT(conn->sccp_con); + + LOGP(DMSC, LOGL_NOTICE, "Sending assignment complete message...\n"); + + /* Extrapolate speech codec from speech mode */ + gsm0808_speech_codec_from_chan_type(&sc, lchan->abis_ip.ass_compl.speech_mode); + + /* Generate message */ + resp = gsm0808_create_ass_compl(lchan->abis_ip.ass_compl.rr_cause, + lchan->abis_ip.ass_compl.chosen_channel, + lchan->abis_ip.ass_compl.encr_alg_id, + lchan->abis_ip.ass_compl.speech_mode, + &conn->sccp_con->aoip_rtp_addr_local, + &sc, + NULL); + + if (!resp) { + LOGP(DMSC, LOGL_ERROR, "Failed to generate assignment completed message!\n"); \ + return -EINVAL; + } + + return osmo_bsc_sigtran_send(conn->sccp_con, resp); +} diff --git a/src/osmo-bsc/osmo_bsc_main.c b/src/osmo-bsc/osmo_bsc_main.c index 5e8f45e..5386bfe 100644 --- a/src/osmo-bsc/osmo_bsc_main.c +++ b/src/osmo-bsc/osmo_bsc_main.c @@ -44,6 +44,7 @@ #include <osmocom/abis/abis.h> #include <osmocom/sccp/sccp.h> +#include <osmocom/mgcp_client/mgcp_client.h> #define _GNU_SOURCE #include <getopt.h> @@ -203,6 +204,9 @@ exit(1); } + bsc_gsmnet->mgw.conf = talloc_zero(bsc_gsmnet, struct mgcp_client_conf); + mgcp_client_conf_init(bsc_gsmnet->mgw.conf); + osmo_init_logging(&log_info); osmo_stats_init(tall_bsc_ctx); @@ -274,6 +278,13 @@ } } + bsc_gsmnet->mgw.client = mgcp_client_init(bsc_gsmnet, bsc_gsmnet->mgw.conf); + + if (mgcp_client_connect(bsc_gsmnet->mgw.client)) { + printf("MGCPGW connect failed\n"); + exit(1); + } + if (osmo_bsc_sigtran_init(&bsc_gsmnet->bsc_data->mscs) != 0) { LOGP(DNM, LOGL_ERROR, "Failed to initalize sigtran backhaul.\n"); exit(1); diff --git a/src/osmo-bsc/osmo_bsc_mgcp.c b/src/osmo-bsc/osmo_bsc_mgcp.c new file mode 100644 index 0000000..d5c3cf2 --- /dev/null +++ b/src/osmo-bsc/osmo_bsc_mgcp.c @@ -0,0 +1,864 @@ +/* (C) 2017 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Philipp Maier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <osmocom/mgcp_client/mgcp_client.h> +#include <osmocom/bsc/gsm_data.h> +#include <osmocom/bsc/osmo_bsc_mgcp.h> +#include <osmocom/bsc/debug.h> +#include <osmocom/bsc/osmo_bsc.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/utils.h> +#include <osmocom/core/timer.h> +#include <osmocom/core/fsm.h> +#include <osmocom/bsc/osmo_bsc_sigtran.h> +#include <osmocom/core/byteswap.h> +#include <arpa/inet.h> + +#define CONN_ID_BTS 1 +#define CONN_ID_NET 2 + +#define MGCP_MGW_TIMEOUT 4 /* sek */ +#define MGCP_MGW_TIMEOUT_TIMER_NO 7411 +#define MGCP_BSS_TIMEOUT 4 /* sek */ +#define MGCP_BSS_TIMEOUT_TIMER_NO 7412 + +#define MGCP_ENDPOINT_FORMAT "%i@mgw" + +extern struct gsm_network *bsc_gsmnet; + +enum fsm_states { + /* Initalization state to start the FSM */ + ST_CRCX_BTS, + + /* Send CRCX for BTS and wait for response */ + ST_ASSIGN_PROC, + + /* Wait for the BSS to setup the connection */ + ST_MDCX_BTS, + + /* Send MDCX for BTS and wait for response */ + ST_CRCX_NET, + + /* Send CRCX (single phase) for NET and wait for response */ + ST_ASSIGN_COMPL, + + /* Call is now active, wait for call end */ + ST_DLCX, + + /* Send DLCX for BTS/NET and wait for response */ + ST_HALT, +}; + +static const struct value_string fsm_state_names[] = { + {ST_CRCX_BTS, "ST_CRCX_BTS (initalize FSM)"}, + {ST_ASSIGN_PROC, "ST_ASSIGN_PROC (create BTS connection)"}, + {ST_MDCX_BTS, "ST_MDCX_BTS (wait for BSS)"}, + {ST_CRCX_NET, "ST_CRCX_NET (update BTS connection)"}, + {ST_ASSIGN_COMPL, "ST_ASSIGN_COMPL (create NET connection)"}, + {ST_ASSIGN_COMPL, "ST_HALT (delete BTS/ connection)"}, + {ST_DLCX, "ST_DLCX (wait for call end)"}, + {0, NULL}, +}; + +enum fsm_evt { + /* Initial event: start off the state machine */ + EV_INIT, + + /* External event: Assignment complete, event is issued shortly before + * the assignment complete message is sent via the A-Interface */ + EV_ASS_COMPLETE, + + /* External event: Teardown event, this event is used to notify the end + * of a. It is also issued in case of errors to teardown a half open + * connection. */ + EV_TEARDOWN, + + /* Internal event: The mgcp_gw has sent its CRCX response for + * the BTS side */ + EV_CRCX_BTS_RESP, + + /* Internal event: The mgcp_gw has sent its MDCX response for + * the BTS side */ + EV_MDCX_BTS_RESP, + + /* Internal event: The mgcp_gw has sent its CRCX response for + * the NET side */ + EV_CRCX_NET_RESP, + + /* Internal event: The mgcp_gw has sent its DLCX response for + * the NET and BTS side */ + EV_DLCX_ALL_RESP, +}; + +static const struct value_string fsm_evt_names[] = { + {EV_INIT, "EV_INIT (start state machine (send CRCX for BTS)"}, + {EV_ASS_COMPLETE, "EV_ASS_COMPLETE (assignment complete)"}, + {EV_TEARDOWN, "EV_TEARDOWN (teardown all connections)"}, + {EV_CRCX_BTS_RESP, "EV_CRCX_BTS_RESP (got CRCX reponse for BTS)"}, + {EV_MDCX_BTS_RESP, "EV_MDCX_BTS_RESP (got MDCX reponse for BTS)"}, + {EV_CRCX_NET_RESP, "EV_CRCX_NET_RESP (got CRCX reponse for NET)"}, + {EV_DLCX_ALL_RESP, "EV_DLCX_ALL_RESP (got DLCX reponse for BTS/NET)"}, + {0, NULL}, +}; + +/* On error, go directly to the DLCX phase. */ +static void on_error_goto_dlcx(struct mgcp_ctx *mgcp_ctx) +{ + /* This function forces the FSM into the DLCX phase. The FSM will just + * behave like the call were ended normally. */ + + struct osmo_fsm_inst *fi; + struct osmo_bsc_sccp_con *conn; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + fi = mgcp_ctx->fsm; + OSMO_ASSERT(fi); + + LOGP(DMSC, LOGL_NOTICE, "MGCPGW: (%s) fsm-state: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state)); + + LOGP(DMGCP, LOGL_ERROR, "(%s) MGCPGW error proceeding request, graceful shutdown...\n", mgcp_ctx->name); + + /* Set the VM into the state where it waits for the call end */ + osmo_fsm_inst_state_chg(fi, ST_DLCX, 0, 0); + + /* Simulate the call end by sending a teardown event, so that + * the FSM proceeds directly with the DLCX */ + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_TEARDOWN, mgcp_ctx); +} + +/* Forward declaration to keep the function in logical order */ +static void crcx_for_bts_resp_cb(struct mgcp_response *r, void *priv); + +/* Callback for ST_CRCX_BTS: startup state machine send out CRCX for BTS side */ +static void fsm_crcx_bts_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct msgb *msg; + struct mgcp_msg mgcp_msg; + struct mgcp_client *mgcp; + uint16_t rtp_endpoint; + unsigned int call_id; + + OSMO_ASSERT(mgcp_ctx); + network = mgcp_ctx->network; + OSMO_ASSERT(network); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + rtp_endpoint = mgcp_client_next_endpoint(mgcp); + conn->rtp_endpoint = rtp_endpoint; + call_id = conn->conn_id; + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) creating connection for the BTS side on " + "MGCPGW endpoint:%x...\n", mgcp_ctx->name, rtp_endpoint); + + /* Generate MGCP message string */ + mgcp_msg.verb = MGCP_VERB_CRCX; + mgcp_msg.presence = + (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | + MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); + snprintf(mgcp_msg.endpoint, MGCP_ENDPOINT_MAXLEN, MGCP_ENDPOINT_FORMAT, rtp_endpoint); + mgcp_msg.call_id = call_id; + mgcp_msg.conn_id = CONN_ID_BTS; + mgcp_msg.conn_mode = MGCP_CONN_LOOPBACK; + msg = mgcp_msg_gen(mgcp, &mgcp_msg); + OSMO_ASSERT(msg); + + /* Note: if transmitting fails, receiving will also fail which eventually + * will cause the error be handled by the timeout callback */ + mgcp_client_tx(mgcp, msg, crcx_for_bts_resp_cb, mgcp_ctx); + + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_ASSIGN_PROC, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NO); +} + +/* Callback for MGCP-Client: handle response for BTS associated CRCX */ +static void crcx_for_bts_resp_cb(struct mgcp_response *r, void *priv) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)priv; + int rc; + struct osmo_bsc_sccp_con *conn; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + if (r->head.response_code != 200) { + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) CRCX response yields error: %d %s\n", + mgcp_ctx->name, r->head.response_code, r->head.comment); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + rc = mgcp_response_parse_params(r); + if (rc) { + LOGP(DMGCP, LOGL_ERROR, "MGCPGW: (%s) Cannot parse CRCX response\n", mgcp_ctx->name); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + LOGP(DMGCP, LOGL_DEBUG, + "MGCPGW: (%s) CRCX responded with address %s:%u\n", mgcp_ctx->name, r->audio_ip, r->audio_port); + + /* Set the connection details in the conn struct. The code that + * controls the BTS via RSL will take these values and signal them + * to the BTS via RSL/IPACC */ + conn->rtp_port = r->audio_port; + conn->rtp_ip = osmo_ntohl(inet_addr(r->audio_ip)); + + /* Notify the FSM that we got the response. */ + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_CRCX_BTS_RESP, mgcp_ctx); +} + +/* Callback for ST_ASSIGN_PROC: An mgcp response has been received, proceed + * with the assignment request */ +static void fsm_proc_assignmnent_req_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct osmo_bsc_sccp_con *conn; + int chan_mode; + int full_rate; + int rc; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + /* Bail on teardown */ + if (event == EV_TEARDOWN) { + on_error_goto_dlcx(mgcp_ctx); + return; + } + + OSMO_ASSERT(conn->conn); + chan_mode = mgcp_ctx->chan_mode; + full_rate = mgcp_ctx->full_rate; + + LOGP(DMGCP, LOGL_NOTICE, "MGCPGW: (%s) MGCPGW proceeding assignment request...\n", mgcp_ctx->name); + rc = gsm0808_assign_req(conn->conn, chan_mode, full_rate); + + if (rc < 0) { + on_error_goto_dlcx(mgcp_ctx); + return; + } + + osmo_fsm_inst_state_chg(fi, ST_MDCX_BTS, MGCP_BSS_TIMEOUT, MGCP_BSS_TIMEOUT_TIMER_NO); +} + +/* Forward declaration to keep the function in logical order */ +static void mdcx_for_bts_resp_cb(struct mgcp_response *r, void *priv); + +/* Callback for ST_MDCX_BTS: When the BSS has completed the assignment, + * proceed with updating the connection for the BTS side */ +static void fsm_mdcx_bts_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct gsm_lchan *lchan; + struct msgb *msg; + struct mgcp_msg mgcp_msg; + struct mgcp_client *mgcp; + uint16_t rtp_endpoint; + unsigned int call_id; + struct in_addr addr; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + /* Bail on teardown */ + if (event == EV_TEARDOWN) { + on_error_goto_dlcx(mgcp_ctx); + return; + } + + network = mgcp_ctx->network; + OSMO_ASSERT(network); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + lchan = mgcp_ctx->lchan; + OSMO_ASSERT(lchan); + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) MGCPGW BSS has completed the assignment, now prceed with MDCX towards BTS...\n", + mgcp_ctx->name); + + rtp_endpoint = conn->rtp_endpoint; + call_id = conn->conn_id; + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) completing connection for the BTS side on " + "MGCPGW endpoint:%x...\n", mgcp_ctx->name, rtp_endpoint); + + addr.s_addr = osmo_ntohl(lchan->abis_ip.bound_ip); + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) BTS expects RTP input on address %s:%u\n", + mgcp_ctx->name, inet_ntoa(addr), lchan->abis_ip.bound_port); + + /* Generate MGCP message string */ + mgcp_msg.verb = MGCP_VERB_MDCX; + mgcp_msg.presence = + (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | + MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | + MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); + snprintf(mgcp_msg.endpoint, sizeof(mgcp_msg.endpoint), MGCP_ENDPOINT_FORMAT, rtp_endpoint); + mgcp_msg.call_id = call_id; + mgcp_msg.conn_id = CONN_ID_BTS; + mgcp_msg.conn_mode = MGCP_CONN_RECV_SEND; + mgcp_msg.audio_ip = inet_ntoa(addr); + mgcp_msg.audio_port = lchan->abis_ip.bound_port; + msg = mgcp_msg_gen(mgcp, &mgcp_msg); + OSMO_ASSERT(msg); + + /* See note in fsm_init_cb() */ + mgcp_client_tx(mgcp, msg, mdcx_for_bts_resp_cb, mgcp_ctx); + + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_CRCX_NET, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NO); +} + +/* Callback for MGCP-Client: handle response for BTS associated MDCX */ +static void mdcx_for_bts_resp_cb(struct mgcp_response *r, void *priv) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)priv; + int rc; + struct in_addr addr; + struct osmo_bsc_sccp_con *conn; + struct gsm_lchan *lchan; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + lchan = mgcp_ctx->lchan; + OSMO_ASSERT(lchan); + + if (r->head.response_code != 200) { + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) MDCX response yields error: %d %s\n", + mgcp_ctx->name, r->head.response_code, r->head.comment); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + rc = mgcp_response_parse_params(r); + if (rc) { + LOGP(DMGCP, LOGL_ERROR, "MGCPGW: (%s) Cannot parse MDCX response\n", mgcp_ctx->name); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + LOGP(DMGCP, LOGL_DEBUG, + "MGCPGW: (%s) MDCX responded with address %s:%u\n", mgcp_ctx->name, r->audio_ip, r->audio_port); + + addr.s_addr = lchan->abis_ip.bound_ip; + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) MDCX corresponding lchan has been bound to address %s:%u\n", + mgcp_ctx->name, inet_ntoa(addr), lchan->abis_ip.bound_port); + + /* Notify the FSM that we got the response. */ + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_MDCX_BTS_RESP, mgcp_ctx); +} + +/* Forward declaration to keep the function in logical order */ +static void crcx_for_net_resp_cb(struct mgcp_response *r, void *priv); + +/* Callback for ST_CRCX_NET: An mgcp response has been received, proceed... */ +static void fsm_crcx_net_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct msgb *msg; + struct mgcp_msg mgcp_msg; + struct mgcp_client *mgcp; + uint16_t rtp_endpoint; + unsigned int call_id; + struct sockaddr_in *sin; + char *addr; + uint16_t port; + + OSMO_ASSERT(mgcp_ctx); + network = mgcp_ctx->network; + OSMO_ASSERT(network); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + rtp_endpoint = conn->rtp_endpoint; + call_id = conn->conn_id; + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) creating connection for the NET side on " + "MGCPGW endpoint:%x...\n", mgcp_ctx->name, rtp_endpoint); + + /* Currently we only have support for IPv4 in our MGCP software, the + * AoIP part is ready to support IPv6 in theory, because the IE + * parser/generator uses sockaddr_storage for the AoIP transport + * identifier. However, the MGCP-GW does not support IPv6 yet. This is + * why we stop here in case some MSC tries to signal IPv6 AoIP + * transport identifiers */ + if (conn->aoip_rtp_addr_remote.ss_family != AF_INET) { + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW (%s) endpoint:%x MSC uses unsupported address format in AoIP transport identifier -- aborting...\n", + mgcp_ctx->name, rtp_endpoint); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + sin = (struct sockaddr_in *)&conn->aoip_rtp_addr_remote; + addr = inet_ntoa(sin->sin_addr); + port = osmo_ntohs(sin->sin_port); + LOGP(DMGCP, LOGL_NOTICE, "MGCPGW: (%s) MSC expects RTP input on address %s:%u\n", mgcp_ctx->name, addr, port); + + /* Generate MGCP message string */ + mgcp_msg.verb = MGCP_VERB_CRCX; + mgcp_msg.presence = + (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | + MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | + MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); + snprintf(mgcp_msg.endpoint, sizeof(mgcp_msg.endpoint), MGCP_ENDPOINT_FORMAT, rtp_endpoint); + mgcp_msg.call_id = call_id; + mgcp_msg.conn_id = CONN_ID_NET; + mgcp_msg.conn_mode = MGCP_CONN_RECV_SEND; + mgcp_msg.audio_ip = addr; + mgcp_msg.audio_port = port; + msg = mgcp_msg_gen(mgcp, &mgcp_msg); + OSMO_ASSERT(msg); + + /* See note in fsm_init_cb() */ + mgcp_client_tx(mgcp, msg, crcx_for_net_resp_cb, mgcp_ctx); + + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_ASSIGN_COMPL, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NO); +} + +/* Callback for MGCP-Client: handle response for NET associated CRCX */ +static void crcx_for_net_resp_cb(struct mgcp_response *r, void *priv) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)priv; + int rc; + struct osmo_bsc_sccp_con *conn; + struct gsm_lchan *lchan; + struct sockaddr_in *sin; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + lchan = mgcp_ctx->lchan; + OSMO_ASSERT(lchan); + + if (r->head.response_code != 200) { + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) CRCX response yields error: %d %s\n", + mgcp_ctx->name, r->head.response_code, r->head.comment); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + rc = mgcp_response_parse_params(r); + if (rc) { + LOGP(DMGCP, LOGL_ERROR, "MGCPGW: (%s) Cannot parse CRCX response\n", mgcp_ctx->name); + on_error_goto_dlcx(mgcp_ctx); + return; + } + + LOGP(DMGCP, LOGL_DEBUG, + "MGCPGW: (%s) CRCX responded with address %s:%u\n", mgcp_ctx->name, r->audio_ip, r->audio_port); + + /* Store address */ + sin = (struct sockaddr_in *)&conn->aoip_rtp_addr_local; + sin->sin_family = AF_INET; + sin->sin_addr.s_addr = inet_addr(r->audio_ip); + sin->sin_port = osmo_ntohs(r->audio_port); + + /* Notify the FSM that we got the response. */ + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_CRCX_NET_RESP, mgcp_ctx); +} + +/* Callback for ST_ASSIGN_COMPL: Send back assignment complete and wait until the call ends */ +static void fsm_send_assignment_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct gsm_lchan *lchan; + struct osmo_bsc_sccp_con *conn; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + /* Bail on teardown */ + if (event == EV_TEARDOWN) { + on_error_goto_dlcx(mgcp_ctx); + return; + } + + lchan = mgcp_ctx->lchan; + OSMO_ASSERT(lchan); + + /* Send assignment completion message via AoIP, this will complete + * the circuit. The message will also contain the port and IP-Address + * where the MGCPGW expects the RTP input from the MSC side */ + bssmap_send_aoip_ass_compl(lchan); + + LOGP(DMGCP, LOGL_NOTICE, "MGCPGW: (%s) call in progress, waiting for call end...\n", mgcp_ctx->name); + + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_DLCX, 0, 0); +} + +/* Forward declaration to keep the function in logical order */ +static void dlcx_for_all_resp_cb(struct mgcp_response *r, void *priv); + +/* Callback for ST_DLCX: Remove connection for the BTS and NET side. */ +static void fsm_dlcx_all_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct msgb *msg; + struct mgcp_msg mgcp_msg; + struct mgcp_client *mgcp; + uint16_t rtp_endpoint; + unsigned int call_id; + + OSMO_ASSERT(mgcp_ctx); + network = mgcp_ctx->network; + OSMO_ASSERT(network); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + rtp_endpoint = conn->rtp_endpoint; + call_id = conn->conn_id; + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) removing connection for the BTS and NET side on " + "MGCPGW endpoint:%x...\n", mgcp_ctx->name, rtp_endpoint); + + /* We now relase the endpoint back to the pool in order to allow + * other connections to use this endpoint */ + mgcp_client_release_endpoint(rtp_endpoint, mgcp); + + /* Generate MGCP message string */ + mgcp_msg.verb = MGCP_VERB_DLCX; + mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID); + snprintf(mgcp_msg.endpoint, sizeof(mgcp_msg.endpoint), MGCP_ENDPOINT_FORMAT, rtp_endpoint); + mgcp_msg.call_id = call_id; + msg = mgcp_msg_gen(mgcp, &mgcp_msg); + OSMO_ASSERT(msg); + + /* See note in fsm_init_cb() */ + mgcp_client_tx(mgcp, msg, dlcx_for_all_resp_cb, mgcp_ctx); + + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_HALT, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NO); +} + +/* Callback for MGCP-Client: handle response for NET associated CRCX */ +static void dlcx_for_all_resp_cb(struct mgcp_response *r, void *priv) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)priv; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct mgcp_client *mgcp; + + OSMO_ASSERT(mgcp_ctx); + network = mgcp_ctx->network; + OSMO_ASSERT(network); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + + /* Note: We check the return code, but in case of an error there is + * not much that can be done to recover. However, at least we tryed + * to remove the connection (if there was even any) */ + if (r->head.response_code != 200) { + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) DLCX response yields error: %d %s\n", + mgcp_ctx->name, r->head.response_code, r->head.comment); + } + + /* Notify the FSM that we got the response. */ + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_DLCX_ALL_RESP, mgcp_ctx); +} + +/* Callback for ST_HALT: Terminate the state machine */ +static void fsm_halt_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)data; + struct osmo_bsc_sccp_con *conn; + + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + + LOGP(DMSC, LOGL_NOTICE, + "MGCPGW: (%s) fsm-state: %s, fsm-event: %s\n", + mgcp_ctx->name, get_value_string(fsm_state_names, fi->state), get_value_string(fsm_evt_names, event)); + + LOGP(DMGCP, LOGL_NOTICE, + "MGCPGW: (%s) timeout (T%i) in state %s, state machine halted\n", + mgcp_ctx->name, fi->T, get_value_string(fsm_state_names, fi->state)); + + /* Send pending sigtran message */ + if (mgcp_ctx->resp) { + osmo_bsc_sigtran_send(conn, mgcp_ctx->resp); + mgcp_ctx->resp = NULL; + } + + /* Destroy the state machine and all context information */ + osmo_fsm_inst_free(mgcp_ctx->fsm); + memset(mgcp_ctx, 0, sizeof(*mgcp_ctx)); + talloc_free(mgcp_ctx); +} + +/* Timer callback to shut down in case of connectivity problems */ +static int fsm_timeout_cb(struct osmo_fsm_inst *fi) +{ + struct mgcp_ctx *mgcp_ctx = (struct mgcp_ctx *)fi->priv; + struct gsm_network *network; + struct osmo_bsc_sccp_con *conn; + struct mgcp_client *mgcp; + + OSMO_ASSERT(mgcp_ctx); + network = mgcp_ctx->network; + OSMO_ASSERT(mgcp_ctx); + conn = mgcp_ctx->conn; + OSMO_ASSERT(conn); + mgcp = network->mgw.client; + OSMO_ASSERT(mgcp); + + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) timeout (T%i) in state %s, attempting graceful teardown...\n", + mgcp_ctx->name, fi->T, get_value_string(fsm_state_names, fi->state)); + + if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NO) { + /* Note: We were unable to communicate with the MGCP-GW, + * unfortunately there is no meaningful action we can take + * now other than giving up. */ + LOGP(DMGCP, LOGL_ERROR, + "MGCPGW: (%s) graceful teardown not possible, terminating...\n", mgcp_ctx->name); + + /* At least release the occupied endpoint ID */ + mgcp_client_release_endpoint(conn->rtp_endpoint, mgcp); + + /* Initiate self destruction of the FSM */ + osmo_fsm_inst_state_chg(fi, ST_HALT, 0, 0); + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_TEARDOWN, mgcp_ctx); + } else if (fi->T == MGCP_BSS_TIMEOUT_TIMER_NO) + /* Note: If the logic that controls the BSS is unable to + * negotiate a connection, we presumably still have a + * working connection to the MGCP-GW, we will try to + * shut down gracefully. */ + on_error_goto_dlcx(mgcp_ctx); + else { + /* Note: Ther must not be any unsolicited timers + * in this FSM. If so, we have serious problem. */ + OSMO_ASSERT(false); + } + + return 0; +} + +static struct osmo_fsm_state fsm_states[] = { + + /* Startup state machine, send CRCX to BTS. */ + [ST_CRCX_BTS] = { + .in_event_mask = (1 << EV_INIT), + .out_state_mask = (1 << ST_ASSIGN_PROC), + .name = "ST_CRCX_BTS", + .action = fsm_crcx_bts_cb, + }, + + /* When the CRCX response for the BTS side is received, then + * proceed the assignment on the BSS side. */ + [ST_ASSIGN_PROC] = { + .in_event_mask = (1 << EV_TEARDOWN) | (1 << EV_CRCX_BTS_RESP), + .out_state_mask = (1 << ST_DLCX) | (1 << ST_MDCX_BTS), + .name = "ST_ASSIGN_PROC", + .action = fsm_proc_assignmnent_req_cb, + }, + + /* When the BSS has processed the assignment request, + * then send the MDCX command for the BTS side in order to + * update the connections with the actual PORT/IP where the + * BTS expects the RTP input. */ + [ST_MDCX_BTS] = { + .in_event_mask = (1 << EV_TEARDOWN) | (1 << EV_ASS_COMPLETE), + .out_state_mask = (1 << ST_DLCX) | (1 << ST_CRCX_NET), + .name = "ST_MDCX_BTS", + .action = fsm_mdcx_bts_cb, + }, + + /* When the MDCX response for the BTS siede is received, then + * directly proceed with sending the CRCX command to connect the + * network side. This is done in one phase (no MDCX needed). */ + [ST_CRCX_NET] = { + .in_event_mask = (1 << EV_TEARDOWN) | (1 << EV_MDCX_BTS_RESP), + .out_state_mask = (1 << ST_DLCX) | (1 << ST_ASSIGN_COMPL), + .name = "ST_CRCX_NET", + .action = fsm_crcx_net_cb, + }, + + /* When the CRCX response for the NET side is received. Then + * send the assignment complete message via the A-Interface and + * enter wait state in order to wait for the end of the call. */ + [ST_ASSIGN_COMPL] = { + .in_event_mask = (1 << EV_TEARDOWN) | (1 << EV_CRCX_NET_RESP), + .out_state_mask = (1 << ST_DLCX) | (1 << ST_DLCX), + .name = "ST_ASSIGN_COMPL", + .action = fsm_send_assignment_complete, + }, + + /* When the call ends, remove all RTP connections from the + * MGCP-GW by sending a wildcarded DLCX. */ + [ST_DLCX] = { + .in_event_mask = (1 << EV_TEARDOWN), + .out_state_mask = (1 << ST_HALT), + .name = "ST_DLCX", + .action = fsm_dlcx_all_cb, + }, + + /* When the MGCP_GW confirms that the connections are terminated, + * then halt the state machine. */ + [ST_HALT] = { + .in_event_mask = (1 << EV_TEARDOWN) | (1 << EV_DLCX_ALL_RESP), + .out_state_mask = 0, + .name = "ST_HALT", + .action = fsm_halt_cb, + }, +}; + +/* State machine definition */ +static struct osmo_fsm fsm = { + .name = "FSM MGCP", + .states = fsm_states, + .num_states = ARRAY_SIZE(fsm_states), + .log_subsys = DMGCP, + .timer_cb = fsm_timeout_cb, +}; + +/* Notify that the a new call begins. This will create a connection for the + * BTS on the MGCP-GW and set up the port numbers in struct osmo_bsc_sccp_con. + * After that gsm0808_assign_req() to proceed. + * Parameter: + * ctx: talloc context + * network: associated gsm network + * conn: associated sccp connection + * chan_mode: channel mode (system data, passed through) + * full_rate: full rate flag (system data, passed through) + * Returns an mgcp_context that contains system data and the OSMO-FSM */ +struct mgcp_ctx *mgcp_assignm_req(void *ctx, struct gsm_network *network, + struct osmo_bsc_sccp_con *conn, int chan_mode, int full_rate) +{ + struct mgcp_ctx *mgcp_ctx; + + OSMO_ASSERT(network); + OSMO_ASSERT(conn); + + /* Register the fsm description (if not already done) */ + if (osmo_fsm_find_by_name(fsm.name) != &fsm) + osmo_fsm_register(&fsm); + + /* Allocate and configure a new fsm instance */ + mgcp_ctx = talloc_zero(ctx, struct mgcp_ctx); + OSMO_ASSERT(mgcp_ctx); + + snprintf(mgcp_ctx->name, sizeof(mgcp_ctx->name), "MGCP FSM, id=%i", conn->conn_id); + mgcp_ctx->fsm = osmo_fsm_inst_alloc(&fsm, NULL, ctx, LOGL_DEBUG, "FSM MGCP INST"); + OSMO_ASSERT(mgcp_ctx->fsm); + mgcp_ctx->fsm->priv = mgcp_ctx; + LOGP(DMGCP, LOGL_NOTICE, "MGCPGW: (%s) MGCPGW handler fsm created\n", mgcp_ctx->name); + mgcp_ctx->network = network; + mgcp_ctx->conn = conn; + mgcp_ctx->chan_mode = chan_mode; + mgcp_ctx->full_rate = full_rate; + + /* start state machine */ + osmo_fsm_inst_state_chg(mgcp_ctx->fsm, ST_CRCX_BTS, 0, 0); + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_INIT, mgcp_ctx); + + return mgcp_ctx; +} + +/* Notify that the call has ended, remove all connections from the MGCP-GW, + * then send the clear complete message and destroy the FSM instance + * Parameter: + * mgcp_ctx: context information (FSM, and pointer to external system data) + * respmgcp_ctx: pending clear complete message to send via A-Interface */ +void mgcp_clear_complete(struct mgcp_ctx *mgcp_ctx, struct msgb *resp) +{ + OSMO_ASSERT(mgcp_ctx); + OSMO_ASSERT(resp); + + mgcp_ctx->resp = resp; + + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_TEARDOWN, mgcp_ctx); +} + +/* Notify that the BSS ready, send the assingnment complete message when the + * mgcp connection is completed + * Parameter: + * mgcp_ctx: context information (FSM, and pointer to external system data) + * lchan: needed for sending the assignment complete message via A-Interface */ +void mgcp_ass_complete(struct mgcp_ctx *mgcp_ctx, struct gsm_lchan *lchan) +{ + OSMO_ASSERT(mgcp_ctx); + OSMO_ASSERT(lchan); + + mgcp_ctx->lchan = lchan; + + osmo_fsm_inst_dispatch(mgcp_ctx->fsm, EV_ASS_COMPLETE, mgcp_ctx); + + return; +} diff --git a/src/osmo-bsc/osmo_bsc_sigtran.c b/src/osmo-bsc/osmo_bsc_sigtran.c index 48796c1..cc1f314 100644 --- a/src/osmo-bsc/osmo_bsc_sigtran.c +++ b/src/osmo-bsc/osmo_bsc_sigtran.c @@ -33,6 +33,7 @@ #include <osmocom/bsc/osmo_bsc_sigtran.h> #include <osmocom/bsc/a_reset.h> #include <osmocom/bsc/gsm_04_80.h> +#include <osmocom/bsc/osmo_bsc_mgcp.h> /* A pointer to a list with all involved MSCs * (a copy of the pointer location submitted with osmo_bsc_sigtran_init() */ diff --git a/src/osmo-bsc/osmo_bsc_vty.c b/src/osmo-bsc/osmo_bsc_vty.c index 0e7003f..3ee7534 100644 --- a/src/osmo-bsc/osmo_bsc_vty.c +++ b/src/osmo-bsc/osmo_bsc_vty.c @@ -29,6 +29,8 @@ #include <osmocom/core/talloc.h> #include <osmocom/vty/logging.h> #include <osmocom/sccp/sccp_types.h> +#include <osmocom/mgcp_client/mgcp_client.h> + #include <time.h> @@ -971,6 +973,8 @@ int bsc_vty_init_extra(void) { + struct gsm_network *net = bsc_gsmnet; + install_element(CONFIG_NODE, &cfg_net_msc_cmd); install_element(CONFIG_NODE, &cfg_net_bsc_cmd); @@ -1035,5 +1039,7 @@ install_element(CFG_LOG_NODE, &logging_fltr_imsi_cmd); + mgcp_client_vty_init(net, MSC_NODE, net->mgw.conf); + return 0; } -- To view, visit https://gerrit.osmocom.org/4334 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia2882b7ca31a3219c676986e85045fa08a425d7a Gerrit-PatchSet: 1 Gerrit-Project: osmo-bsc Gerrit-Branch: master Gerrit-Owner: dexter <pma...@sysmocom.de>