Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: AGPL-3.0+ |
| 6 | * |
| 7 | * Author: Neels Hofmeyr |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include <osmocom/core/fsm.h> |
| 24 | |
| 25 | #include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h> |
| 26 | |
| 27 | #include <osmocom/msc/debug.h> |
| 28 | #include <osmocom/msc/transaction.h> |
| 29 | #include <osmocom/msc/call_leg.h> |
| 30 | #include <osmocom/msc/rtp_stream.h> |
| 31 | |
| 32 | #define LOG_RTPS(rtps, level, fmt, args...) \ |
| 33 | LOGPFSML(rtps->fi, level, fmt, ##args) |
| 34 | |
| 35 | enum rtp_stream_event { |
| 36 | RTP_STREAM_EV_CRCX_OK, |
| 37 | RTP_STREAM_EV_CRCX_FAIL, |
| 38 | RTP_STREAM_EV_MDCX_OK, |
| 39 | RTP_STREAM_EV_MDCX_FAIL, |
| 40 | }; |
| 41 | |
| 42 | enum rtp_stream_state { |
| 43 | RTP_STREAM_ST_UNINITIALIZED, |
| 44 | RTP_STREAM_ST_ESTABLISHING, |
| 45 | RTP_STREAM_ST_ESTABLISHED, |
| 46 | RTP_STREAM_ST_DISCARDING, |
| 47 | }; |
| 48 | |
| 49 | static struct osmo_fsm rtp_stream_fsm; |
| 50 | |
| 51 | static struct osmo_tdef_state_timeout rtp_stream_fsm_timeouts[32] = { |
| 52 | [RTP_STREAM_ST_ESTABLISHING] = { .T = -2 }, |
| 53 | }; |
| 54 | |
| 55 | #define rtp_stream_state_chg(rtps, state) \ |
| 56 | osmo_tdef_fsm_inst_state_chg((rtps)->fi, state, rtp_stream_fsm_timeouts, g_mgw_tdefs, 5) |
| 57 | |
| 58 | static __attribute__((constructor)) void rtp_stream_init() |
| 59 | { |
| 60 | OSMO_ASSERT(osmo_fsm_register(&rtp_stream_fsm) == 0); |
| 61 | } |
| 62 | |
| 63 | void rtp_stream_update_id(struct rtp_stream *rtps) |
| 64 | { |
| 65 | char buf[256]; |
| 66 | char *p; |
| 67 | struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) }; |
| 68 | OSMO_STRBUF_PRINTF(sb, "%s", rtps->fi->proc.parent->id); |
| 69 | if (rtps->for_trans) |
| 70 | OSMO_STRBUF_PRINTF(sb, ":trans-%u", rtps->for_trans->transaction_id); |
| 71 | OSMO_STRBUF_PRINTF(sb, ":call-%u", rtps->call_id); |
| 72 | OSMO_STRBUF_PRINTF(sb, ":%s", rtp_direction_name(rtps->dir)); |
| 73 | if (!osmo_mgcpc_ep_ci_id(rtps->ci)) { |
| 74 | OSMO_STRBUF_PRINTF(sb, ":no-CI"); |
| 75 | } else { |
| 76 | OSMO_STRBUF_PRINTF(sb, ":CI-%s", osmo_mgcpc_ep_ci_id(rtps->ci)); |
| 77 | if (!osmo_sockaddr_str_is_set(&rtps->remote)) |
| 78 | OSMO_STRBUF_PRINTF(sb, ":no-remote-port"); |
| 79 | else if (!rtps->remote_sent_to_mgw) |
| 80 | OSMO_STRBUF_PRINTF(sb, ":remote-port-not-sent"); |
| 81 | if (!rtps->codec_known) |
| 82 | OSMO_STRBUF_PRINTF(sb, ":no-codec"); |
| 83 | else if (!rtps->codec_sent_to_mgw) |
| 84 | OSMO_STRBUF_PRINTF(sb, ":codec-not-sent"); |
| 85 | } |
| 86 | if (osmo_sockaddr_str_is_set(&rtps->local)) |
| 87 | OSMO_STRBUF_PRINTF(sb, ":local-%s-%u", rtps->local.ip, rtps->local.port); |
| 88 | if (osmo_sockaddr_str_is_set(&rtps->remote)) |
| 89 | OSMO_STRBUF_PRINTF(sb, ":remote-%s-%u", rtps->remote.ip, rtps->remote.port); |
| 90 | |
| 91 | /* Replace any dots in the IP address, dots not allowed as FSM instance name */ |
| 92 | for (p = buf; *p; p++) |
| 93 | if (*p == '.') |
| 94 | *p = '-'; |
| 95 | |
| 96 | osmo_fsm_inst_update_id_f(rtps->fi, "%s", buf); |
| 97 | } |
| 98 | |
| 99 | /* Allocate RTP stream under a call leg. This is one RTP connection from some remote entity with address and port to a |
| 100 | * local RTP address and port. call_id is stored for sending in MGCP transactions and as logging context. for_trans is |
| 101 | * optional, merely stored for reference by callers, and appears as log context if not NULL. */ |
| 102 | struct rtp_stream *rtp_stream_alloc(struct call_leg *parent_call_leg, enum rtp_direction dir, |
| 103 | uint32_t call_id, struct gsm_trans *for_trans) |
| 104 | { |
| 105 | struct osmo_fsm_inst *fi; |
| 106 | struct rtp_stream *rtps; |
| 107 | |
| 108 | fi = osmo_fsm_inst_alloc_child(&rtp_stream_fsm, parent_call_leg->fi, CALL_LEG_EV_RTP_STREAM_GONE); |
| 109 | OSMO_ASSERT(fi); |
| 110 | |
| 111 | rtps = talloc(fi, struct rtp_stream); |
| 112 | OSMO_ASSERT(rtps); |
| 113 | fi->priv = rtps; |
| 114 | *rtps = (struct rtp_stream){ |
| 115 | .fi = fi, |
| 116 | .parent_call_leg = parent_call_leg, |
| 117 | .call_id = call_id, |
| 118 | .for_trans = for_trans, |
| 119 | .dir = dir, |
| 120 | }; |
| 121 | |
| 122 | rtp_stream_update_id(rtps); |
| 123 | |
| 124 | return rtps; |
| 125 | } |
| 126 | |
| 127 | static void check_established(struct rtp_stream *rtps) |
| 128 | { |
| 129 | if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED |
| 130 | && osmo_sockaddr_str_is_set(&rtps->local) |
| 131 | && osmo_sockaddr_str_is_set(&rtps->remote) |
| 132 | && rtps->remote_sent_to_mgw |
| 133 | && rtps->codec_known) |
| 134 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHED); |
| 135 | } |
| 136 | |
| 137 | static void rtp_stream_fsm_establishing_established(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 138 | { |
| 139 | struct rtp_stream *rtps = fi->priv; |
| 140 | const struct mgcp_conn_peer *crcx_info; |
| 141 | switch (event) { |
| 142 | case RTP_STREAM_EV_CRCX_OK: |
| 143 | crcx_info = osmo_mgcpc_ep_ci_get_rtp_info(rtps->ci); |
| 144 | osmo_sockaddr_str_from_str(&rtps->local, crcx_info->addr, crcx_info->port); |
| 145 | rtp_stream_update_id(rtps); |
| 146 | osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, rtps); |
| 147 | check_established(rtps); |
| 148 | |
| 149 | if ((!rtps->remote_sent_to_mgw || !rtps->codec_sent_to_mgw) |
| 150 | && osmo_sockaddr_str_is_set(&rtps->remote) |
| 151 | && rtps->codec_known) { |
| 152 | LOG_RTPS(rtps, LOGL_DEBUG, |
| 153 | "local ip:port set;%s%s triggering MDCX to send the new settings\n", |
| 154 | (!rtps->remote_sent_to_mgw)? " remote ip:port not yet sent," : "", |
| 155 | (!rtps->codec_sent_to_mgw)? " codec not yet sent," : ""); |
| 156 | rtp_stream_do_mdcx(rtps); |
| 157 | } |
| 158 | return; |
| 159 | |
| 160 | case RTP_STREAM_EV_MDCX_OK: |
| 161 | rtp_stream_update_id(rtps); |
| 162 | check_established(rtps); |
| 163 | return; |
| 164 | |
| 165 | case RTP_STREAM_EV_CRCX_FAIL: |
| 166 | case RTP_STREAM_EV_MDCX_FAIL: |
| 167 | rtps->remote_sent_to_mgw = false; |
| 168 | rtps->codec_sent_to_mgw = false; |
| 169 | rtp_stream_update_id(rtps); |
| 170 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING); |
| 171 | return; |
| 172 | |
| 173 | default: |
| 174 | OSMO_ASSERT(false); |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | void rtp_stream_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 179 | { |
| 180 | struct rtp_stream *rtps = fi->priv; |
| 181 | osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ESTABLISHED, rtps); |
| 182 | } |
| 183 | |
| 184 | static int rtp_stream_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 185 | { |
| 186 | struct rtp_stream *rtps = fi->priv; |
| 187 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void rtp_stream_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 192 | { |
| 193 | struct rtp_stream *rtps = fi->priv; |
| 194 | if (rtps->ci) { |
| 195 | osmo_mgcpc_ep_ci_dlcx(rtps->ci); |
| 196 | rtps->ci = NULL; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void rtp_stream_fsm_discarding_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 201 | { |
| 202 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 203 | } |
| 204 | |
| 205 | static const struct value_string rtp_stream_fsm_event_names[] = { |
| 206 | OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_OK), |
| 207 | OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_FAIL), |
| 208 | OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_OK), |
| 209 | OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_FAIL), |
| 210 | {} |
| 211 | }; |
| 212 | |
| 213 | #define S(x) (1 << (x)) |
| 214 | |
| 215 | static const struct osmo_fsm_state rtp_stream_fsm_states[] = { |
| 216 | [RTP_STREAM_ST_UNINITIALIZED] = { |
| 217 | .name = "UNINITIALIZED", |
| 218 | .out_state_mask = 0 |
| 219 | | S(RTP_STREAM_ST_ESTABLISHING) |
| 220 | | S(RTP_STREAM_ST_DISCARDING) |
| 221 | , |
| 222 | }, |
| 223 | [RTP_STREAM_ST_ESTABLISHING] = { |
| 224 | .name = "ESTABLISHING", |
| 225 | .in_event_mask = 0 |
| 226 | | S(RTP_STREAM_EV_CRCX_OK) |
| 227 | | S(RTP_STREAM_EV_CRCX_FAIL) |
| 228 | | S(RTP_STREAM_EV_MDCX_OK) |
| 229 | | S(RTP_STREAM_EV_MDCX_FAIL) |
| 230 | , |
| 231 | .out_state_mask = 0 |
| 232 | | S(RTP_STREAM_ST_ESTABLISHED) |
| 233 | | S(RTP_STREAM_ST_DISCARDING) |
| 234 | , |
| 235 | .action = rtp_stream_fsm_establishing_established, |
| 236 | }, |
| 237 | [RTP_STREAM_ST_ESTABLISHED] = { |
| 238 | .name = "ESTABLISHED", |
| 239 | .out_state_mask = 0 |
| 240 | | S(RTP_STREAM_ST_ESTABLISHING) |
| 241 | | S(RTP_STREAM_ST_DISCARDING) |
| 242 | , |
| 243 | .onenter = rtp_stream_fsm_established_onenter, |
| 244 | .action = rtp_stream_fsm_establishing_established, |
| 245 | }, |
| 246 | [RTP_STREAM_ST_DISCARDING] = { |
| 247 | .name = "DISCARDING", |
| 248 | .onenter = rtp_stream_fsm_discarding_onenter, |
| 249 | .out_state_mask = 0 |
| 250 | | S(RTP_STREAM_ST_DISCARDING) |
| 251 | , |
| 252 | }, |
| 253 | }; |
| 254 | |
| 255 | static struct osmo_fsm rtp_stream_fsm = { |
| 256 | .name = "rtp_stream", |
| 257 | .states = rtp_stream_fsm_states, |
| 258 | .num_states = ARRAY_SIZE(rtp_stream_fsm_states), |
| 259 | .log_subsys = DCC, |
| 260 | .event_names = rtp_stream_fsm_event_names, |
| 261 | .timer_cb = rtp_stream_fsm_timer_cb, |
| 262 | .cleanup = rtp_stream_fsm_cleanup, |
| 263 | }; |
| 264 | |
| 265 | static int rtp_stream_do_mgcp_verb(struct rtp_stream *rtps, enum mgcp_verb verb, uint32_t ok_event, uint32_t fail_event) |
| 266 | { |
| 267 | struct mgcp_conn_peer verb_info; |
| 268 | |
| 269 | if (!rtps->ci) { |
| 270 | LOG_RTPS(rtps, LOGL_ERROR, "Cannot send %s, no endpoint CI allocated\n", osmo_mgcp_verb_name(verb)); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
| 274 | verb_info = (struct mgcp_conn_peer){ |
| 275 | .call_id = rtps->call_id, |
| 276 | .ptime = 20, |
| 277 | }; |
| 278 | |
| 279 | if (verb == MGCP_VERB_CRCX) |
| 280 | verb_info.conn_mode = rtps->crcx_conn_mode; |
| 281 | |
| 282 | if (rtps->codec_known) { |
| 283 | verb_info.codecs[0] = rtps->codec; |
| 284 | verb_info.codecs_len = 1; |
| 285 | rtps->codec_sent_to_mgw = true; |
| 286 | } |
| 287 | if (osmo_sockaddr_str_is_set(&rtps->remote)) { |
| 288 | int rc = osmo_strlcpy(verb_info.addr, rtps->remote.ip, sizeof(verb_info.addr)); |
| 289 | if (rc <= 0 || rc >= sizeof(verb_info.addr)) { |
| 290 | LOG_RTPS(rtps, LOGL_ERROR, "Failure to write IP address to MGCP message (rc=%d)\n", rc); |
| 291 | return -ENOSPC; |
| 292 | } |
| 293 | verb_info.port = rtps->remote.port; |
| 294 | rtps->remote_sent_to_mgw = true; |
| 295 | } |
| 296 | |
| 297 | osmo_mgcpc_ep_ci_request(rtps->ci, verb, &verb_info, rtps->fi, ok_event, fail_event, NULL); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | int rtp_stream_ensure_ci(struct rtp_stream *rtps, struct osmo_mgcpc_ep *at_endpoint) |
| 302 | { |
| 303 | if (rtps->ci) |
| 304 | return rtp_stream_commit(rtps); |
| 305 | |
| 306 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING); |
| 307 | |
| 308 | rtps->ci = osmo_mgcpc_ep_ci_add(at_endpoint, "%s", rtp_direction_name(rtps->dir)); |
| 309 | if (!rtps->ci) |
| 310 | return -ENODEV; |
| 311 | |
| 312 | return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_CRCX, RTP_STREAM_EV_CRCX_OK, RTP_STREAM_EV_CRCX_FAIL); |
| 313 | } |
| 314 | |
| 315 | int rtp_stream_do_mdcx(struct rtp_stream *rtps) |
| 316 | { |
| 317 | return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_MDCX, RTP_STREAM_EV_MDCX_OK, RTP_STREAM_EV_MDCX_FAIL); |
| 318 | } |
| 319 | |
| 320 | void rtp_stream_release(struct rtp_stream *rtps) |
| 321 | { |
| 322 | if (!rtps) |
| 323 | return; |
| 324 | |
| 325 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING); |
| 326 | } |
| 327 | |
| 328 | /* After setting up a remote RTP address or a new codec, call this to trigger an MDCX. |
| 329 | * The MDCX will only trigger if all data needed by an endpoint is available (both RTP address and codec) and if at |
| 330 | * least one of them has not yet been sent to the MGW in a previous CRCX or MDCX. */ |
| 331 | int rtp_stream_commit(struct rtp_stream *rtps) |
| 332 | { |
| 333 | if (!rtps->ci) { |
| 334 | LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no MGW endpoint CI set up\n"); |
| 335 | return -1; |
| 336 | } |
| 337 | if (!osmo_sockaddr_str_is_set(&rtps->remote)) { |
| 338 | LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no remote RTP address known\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | if (!rtps->codec_known) { |
| 342 | LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no codec known\n"); |
| 343 | return -1; |
| 344 | } |
| 345 | if (rtps->remote_sent_to_mgw && rtps->codec_sent_to_mgw) { |
| 346 | LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: both remote RTP address and codec already set up at MGW\n"); |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | LOG_RTPS(rtps, LOGL_DEBUG, "Committing: Tx MDCX to update the MGW: updating%s%s\n", |
| 351 | rtps->remote_sent_to_mgw ? "" : " remote-RTP-IP-port", |
| 352 | rtps->codec_sent_to_mgw ? "" : " codec"); |
| 353 | return rtp_stream_do_mdcx(rtps); |
| 354 | } |
| 355 | |
| 356 | void rtp_stream_set_codec(struct rtp_stream *rtps, enum mgcp_codecs codec) |
| 357 | { |
| 358 | if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED) |
| 359 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING); |
| 360 | LOG_RTPS(rtps, LOGL_DEBUG, "setting codec to %s\n", osmo_mgcpc_codec_name(codec)); |
| 361 | rtps->codec = codec; |
| 362 | rtps->codec_known = true; |
| 363 | rtps->codec_sent_to_mgw = false; |
| 364 | rtp_stream_update_id(rtps); |
| 365 | } |
| 366 | |
| 367 | void rtp_stream_set_remote_addr(struct rtp_stream *rtps, const struct osmo_sockaddr_str *r) |
| 368 | { |
| 369 | if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED) |
| 370 | rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING); |
| 371 | LOG_RTPS(rtps, LOGL_DEBUG, "setting remote addr to " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(r)); |
| 372 | rtps->remote = *r; |
| 373 | rtps->remote_sent_to_mgw = false; |
| 374 | rtp_stream_update_id(rtps); |
| 375 | } |
| 376 | |
| 377 | bool rtp_stream_is_established(struct rtp_stream *rtps) |
| 378 | { |
| 379 | if (!rtps) |
| 380 | return false; |
| 381 | if (!rtps->fi) |
| 382 | return false; |
| 383 | if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED) |
| 384 | return false; |
| 385 | if (!rtps->remote_sent_to_mgw |
| 386 | || !rtps->codec_sent_to_mgw) |
| 387 | return false; |
| 388 | return true; |
| 389 | } |