blob: 29025204f8ca37cf36320b26d2fd1ebb166ffec9 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/*
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
35enum 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
42enum 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
49static struct osmo_fsm rtp_stream_fsm;
50
51static 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
58static __attribute__((constructor)) void rtp_stream_init()
59{
60 OSMO_ASSERT(osmo_fsm_register(&rtp_stream_fsm) == 0);
61}
62
63void 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));
Neels Hofmeyr84ce2062019-10-05 05:15:25 +020077 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010078 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");
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +020085 if (rtps->use_osmux) {
86 if (rtps->remote_osmux_cid < 0)
87 OSMO_STRBUF_PRINTF(sb, ":no-remote-osmux-cid");
88 else if (!rtps->remote_osmux_cid_sent_to_mgw)
89 OSMO_STRBUF_PRINTF(sb, ":remote-osmux-cid-not-sent");
90 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010091 }
Neels Hofmeyr84ce2062019-10-05 05:15:25 +020092 if (osmo_sockaddr_str_is_nonzero(&rtps->local))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010093 OSMO_STRBUF_PRINTF(sb, ":local-%s-%u", rtps->local.ip, rtps->local.port);
Neels Hofmeyr84ce2062019-10-05 05:15:25 +020094 if (osmo_sockaddr_str_is_nonzero(&rtps->remote))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010095 OSMO_STRBUF_PRINTF(sb, ":remote-%s-%u", rtps->remote.ip, rtps->remote.port);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +020096 if (rtps->use_osmux)
97 OSMO_STRBUF_PRINTF(sb, ":osmux-%d-%d", rtps->local_osmux_cid, rtps->remote_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010098
99 /* Replace any dots in the IP address, dots not allowed as FSM instance name */
100 for (p = buf; *p; p++)
101 if (*p == '.')
102 *p = '-';
103
104 osmo_fsm_inst_update_id_f(rtps->fi, "%s", buf);
105}
106
107/* Allocate RTP stream under a call leg. This is one RTP connection from some remote entity with address and port to a
108 * local RTP address and port. call_id is stored for sending in MGCP transactions and as logging context. for_trans is
109 * optional, merely stored for reference by callers, and appears as log context if not NULL. */
110struct rtp_stream *rtp_stream_alloc(struct call_leg *parent_call_leg, enum rtp_direction dir,
111 uint32_t call_id, struct gsm_trans *for_trans)
112{
113 struct osmo_fsm_inst *fi;
114 struct rtp_stream *rtps;
115
116 fi = osmo_fsm_inst_alloc_child(&rtp_stream_fsm, parent_call_leg->fi, CALL_LEG_EV_RTP_STREAM_GONE);
117 OSMO_ASSERT(fi);
118
119 rtps = talloc(fi, struct rtp_stream);
120 OSMO_ASSERT(rtps);
121 fi->priv = rtps;
122 *rtps = (struct rtp_stream){
123 .fi = fi,
124 .parent_call_leg = parent_call_leg,
125 .call_id = call_id,
126 .for_trans = for_trans,
127 .dir = dir,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200128 .local_osmux_cid = -2,
129 .remote_osmux_cid = -2,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100130 };
131
132 rtp_stream_update_id(rtps);
133
134 return rtps;
135}
136
137static void check_established(struct rtp_stream *rtps)
138{
139 if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200140 && osmo_sockaddr_str_is_nonzero(&rtps->local)
141 && osmo_sockaddr_str_is_nonzero(&rtps->remote)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100142 && rtps->remote_sent_to_mgw
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200143 && (!rtps->use_osmux || rtps->remote_osmux_cid_sent_to_mgw)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100144 && rtps->codec_known)
145 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHED);
146}
147
148static void rtp_stream_fsm_establishing_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
149{
150 struct rtp_stream *rtps = fi->priv;
151 const struct mgcp_conn_peer *crcx_info;
152 switch (event) {
153 case RTP_STREAM_EV_CRCX_OK:
154 crcx_info = osmo_mgcpc_ep_ci_get_rtp_info(rtps->ci);
Vadim Yanitskiye0ef6d12019-05-11 04:04:59 +0700155 if (!crcx_info) {
156 LOG_RTPS(rtps, LOGL_ERROR, "osmo_mgcpc_ep_ci_get_rtp_info() has "
157 "failed, ignoring %s\n", osmo_fsm_event_name(fi->fsm, event));
158 return;
159 }
160
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100161 osmo_sockaddr_str_from_str(&rtps->local, crcx_info->addr, crcx_info->port);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200162 if (rtps->use_osmux != crcx_info->x_osmo_osmux_use) {
163 LOG_RTPS(rtps, LOGL_ERROR, "Osmux usage request and response don't match: %d vs %d",
164 rtps->use_osmux, crcx_info->x_osmo_osmux_use);
165 /* TODO: proper failure path */
166 OSMO_ASSERT(rtps->use_osmux != crcx_info->x_osmo_osmux_use);
167 }
168 if (crcx_info->x_osmo_osmux_use)
169 rtps->local_osmux_cid = crcx_info->x_osmo_osmux_cid;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100170 rtp_stream_update_id(rtps);
171 osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, rtps);
172 check_established(rtps);
173
174 if ((!rtps->remote_sent_to_mgw || !rtps->codec_sent_to_mgw)
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200175 && osmo_sockaddr_str_is_nonzero(&rtps->remote)
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200176 && (!rtps->use_osmux || rtps->remote_osmux_cid_sent_to_mgw)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100177 && rtps->codec_known) {
178 LOG_RTPS(rtps, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200179 "local ip:port set;%s%s%s triggering MDCX to send the new settings\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100180 (!rtps->remote_sent_to_mgw)? " remote ip:port not yet sent," : "",
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200181 (!rtps->codec_sent_to_mgw)? " codec not yet sent," : "",
182 (rtps->use_osmux && !rtps->remote_osmux_cid_sent_to_mgw) ? "Osmux CID not yet sent,": "");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100183 rtp_stream_do_mdcx(rtps);
184 }
185 return;
186
187 case RTP_STREAM_EV_MDCX_OK:
188 rtp_stream_update_id(rtps);
189 check_established(rtps);
190 return;
191
192 case RTP_STREAM_EV_CRCX_FAIL:
193 case RTP_STREAM_EV_MDCX_FAIL:
194 rtps->remote_sent_to_mgw = false;
195 rtps->codec_sent_to_mgw = false;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200196 rtps->remote_osmux_cid_sent_to_mgw = false;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100197 rtp_stream_update_id(rtps);
198 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
199 return;
200
201 default:
202 OSMO_ASSERT(false);
203 };
204}
205
206void rtp_stream_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
207{
208 struct rtp_stream *rtps = fi->priv;
209 osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ESTABLISHED, rtps);
210}
211
212static int rtp_stream_fsm_timer_cb(struct osmo_fsm_inst *fi)
213{
214 struct rtp_stream *rtps = fi->priv;
215 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
216 return 0;
217}
218
219static void rtp_stream_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
220{
221 struct rtp_stream *rtps = fi->priv;
222 if (rtps->ci) {
Neels Hofmeyr523b92f2019-10-05 01:14:41 +0200223 osmo_mgcpc_ep_cancel_notify(osmo_mgcpc_ep_ci_ep(rtps->ci), fi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100224 osmo_mgcpc_ep_ci_dlcx(rtps->ci);
225 rtps->ci = NULL;
226 }
227}
228
229void rtp_stream_fsm_discarding_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
230{
231 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
232}
233
234static const struct value_string rtp_stream_fsm_event_names[] = {
235 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_OK),
236 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_FAIL),
237 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_OK),
238 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_FAIL),
239 {}
240};
241
242#define S(x) (1 << (x))
243
244static const struct osmo_fsm_state rtp_stream_fsm_states[] = {
245 [RTP_STREAM_ST_UNINITIALIZED] = {
246 .name = "UNINITIALIZED",
247 .out_state_mask = 0
248 | S(RTP_STREAM_ST_ESTABLISHING)
249 | S(RTP_STREAM_ST_DISCARDING)
250 ,
251 },
252 [RTP_STREAM_ST_ESTABLISHING] = {
253 .name = "ESTABLISHING",
254 .in_event_mask = 0
255 | S(RTP_STREAM_EV_CRCX_OK)
256 | S(RTP_STREAM_EV_CRCX_FAIL)
257 | S(RTP_STREAM_EV_MDCX_OK)
258 | S(RTP_STREAM_EV_MDCX_FAIL)
259 ,
260 .out_state_mask = 0
261 | S(RTP_STREAM_ST_ESTABLISHED)
262 | S(RTP_STREAM_ST_DISCARDING)
263 ,
264 .action = rtp_stream_fsm_establishing_established,
265 },
266 [RTP_STREAM_ST_ESTABLISHED] = {
267 .name = "ESTABLISHED",
268 .out_state_mask = 0
269 | S(RTP_STREAM_ST_ESTABLISHING)
270 | S(RTP_STREAM_ST_DISCARDING)
271 ,
272 .onenter = rtp_stream_fsm_established_onenter,
273 .action = rtp_stream_fsm_establishing_established,
274 },
275 [RTP_STREAM_ST_DISCARDING] = {
276 .name = "DISCARDING",
277 .onenter = rtp_stream_fsm_discarding_onenter,
278 .out_state_mask = 0
279 | S(RTP_STREAM_ST_DISCARDING)
280 ,
281 },
282};
283
284static struct osmo_fsm rtp_stream_fsm = {
285 .name = "rtp_stream",
286 .states = rtp_stream_fsm_states,
287 .num_states = ARRAY_SIZE(rtp_stream_fsm_states),
288 .log_subsys = DCC,
289 .event_names = rtp_stream_fsm_event_names,
290 .timer_cb = rtp_stream_fsm_timer_cb,
291 .cleanup = rtp_stream_fsm_cleanup,
292};
293
294static int rtp_stream_do_mgcp_verb(struct rtp_stream *rtps, enum mgcp_verb verb, uint32_t ok_event, uint32_t fail_event)
295{
296 struct mgcp_conn_peer verb_info;
297
298 if (!rtps->ci) {
299 LOG_RTPS(rtps, LOGL_ERROR, "Cannot send %s, no endpoint CI allocated\n", osmo_mgcp_verb_name(verb));
300 return -EINVAL;
301 }
302
303 verb_info = (struct mgcp_conn_peer){
304 .call_id = rtps->call_id,
305 .ptime = 20,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200306 .x_osmo_osmux_use = rtps->use_osmux,
307 .x_osmo_osmux_cid = rtps->remote_osmux_cid,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308 };
309
310 if (verb == MGCP_VERB_CRCX)
311 verb_info.conn_mode = rtps->crcx_conn_mode;
312
313 if (rtps->codec_known) {
314 verb_info.codecs[0] = rtps->codec;
315 verb_info.codecs_len = 1;
316 rtps->codec_sent_to_mgw = true;
317 }
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200318 if (osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100319 int rc = osmo_strlcpy(verb_info.addr, rtps->remote.ip, sizeof(verb_info.addr));
320 if (rc <= 0 || rc >= sizeof(verb_info.addr)) {
321 LOG_RTPS(rtps, LOGL_ERROR, "Failure to write IP address to MGCP message (rc=%d)\n", rc);
322 return -ENOSPC;
323 }
324 verb_info.port = rtps->remote.port;
325 rtps->remote_sent_to_mgw = true;
326 }
327
328 osmo_mgcpc_ep_ci_request(rtps->ci, verb, &verb_info, rtps->fi, ok_event, fail_event, NULL);
329 return 0;
330}
331
332int rtp_stream_ensure_ci(struct rtp_stream *rtps, struct osmo_mgcpc_ep *at_endpoint)
333{
334 if (rtps->ci)
335 return rtp_stream_commit(rtps);
336
337 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
338
339 rtps->ci = osmo_mgcpc_ep_ci_add(at_endpoint, "%s", rtp_direction_name(rtps->dir));
340 if (!rtps->ci)
341 return -ENODEV;
342
343 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_CRCX, RTP_STREAM_EV_CRCX_OK, RTP_STREAM_EV_CRCX_FAIL);
344}
345
346int rtp_stream_do_mdcx(struct rtp_stream *rtps)
347{
348 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_MDCX, RTP_STREAM_EV_MDCX_OK, RTP_STREAM_EV_MDCX_FAIL);
349}
350
351void rtp_stream_release(struct rtp_stream *rtps)
352{
353 if (!rtps)
354 return;
355
356 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
357}
358
359/* After setting up a remote RTP address or a new codec, call this to trigger an MDCX.
360 * The MDCX will only trigger if all data needed by an endpoint is available (both RTP address and codec) and if at
361 * least one of them has not yet been sent to the MGW in a previous CRCX or MDCX. */
362int rtp_stream_commit(struct rtp_stream *rtps)
363{
364 if (!rtps->ci) {
365 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no MGW endpoint CI set up\n");
366 return -1;
367 }
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200368 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100369 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no remote RTP address known\n");
370 return -1;
371 }
372 if (!rtps->codec_known) {
373 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no codec known\n");
374 return -1;
375 }
376 if (rtps->remote_sent_to_mgw && rtps->codec_sent_to_mgw) {
377 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: both remote RTP address and codec already set up at MGW\n");
378 return 0;
379 }
380
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200381 LOG_RTPS(rtps, LOGL_DEBUG, "Committing: Tx MDCX to update the MGW: updating%s%s%s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100382 rtps->remote_sent_to_mgw ? "" : " remote-RTP-IP-port",
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200383 rtps->codec_sent_to_mgw ? "" : " codec",
384 (!rtps->use_osmux || rtps->remote_osmux_cid_sent_to_mgw) ? "" : " remote-Osmux-CID");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100385 return rtp_stream_do_mdcx(rtps);
386}
387
388void rtp_stream_set_codec(struct rtp_stream *rtps, enum mgcp_codecs codec)
389{
390 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
391 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
392 LOG_RTPS(rtps, LOGL_DEBUG, "setting codec to %s\n", osmo_mgcpc_codec_name(codec));
393 rtps->codec = codec;
394 rtps->codec_known = true;
395 rtps->codec_sent_to_mgw = false;
396 rtp_stream_update_id(rtps);
397}
398
399void rtp_stream_set_remote_addr(struct rtp_stream *rtps, const struct osmo_sockaddr_str *r)
400{
401 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
402 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
403 LOG_RTPS(rtps, LOGL_DEBUG, "setting remote addr to " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(r));
404 rtps->remote = *r;
405 rtps->remote_sent_to_mgw = false;
406 rtp_stream_update_id(rtps);
407}
408
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200409void rtp_stream_set_remote_osmux_cid(struct rtp_stream *rtps, uint8_t osmux_cid)
410{
411 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
412 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
413 LOG_RTPS(rtps, LOGL_DEBUG, "setting remote Osmux CID to %u\n", osmux_cid);
414 rtps->remote_osmux_cid = osmux_cid;
415 rtps->remote_osmux_cid_sent_to_mgw = false;
416 rtp_stream_update_id(rtps);
417}
418
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100419bool rtp_stream_is_established(struct rtp_stream *rtps)
420{
421 if (!rtps)
422 return false;
423 if (!rtps->fi)
424 return false;
425 if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED)
426 return false;
427 if (!rtps->remote_sent_to_mgw
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200428 || !rtps->codec_sent_to_mgw
429 || (rtps->use_osmux && !rtps->remote_osmux_cid_sent_to_mgw))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100430 return false;
431 return true;
432}