blob: c3880bf7b35e0458bcddd0a6a3f9bb28a5ba87e1 [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));
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");
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 }
92 if (osmo_sockaddr_str_is_set(&rtps->local))
93 OSMO_STRBUF_PRINTF(sb, ":local-%s-%u", rtps->local.ip, rtps->local.port);
94 if (osmo_sockaddr_str_is_set(&rtps->remote))
95 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
140 && osmo_sockaddr_str_is_set(&rtps->local)
141 && osmo_sockaddr_str_is_set(&rtps->remote)
142 && 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)
175 && osmo_sockaddr_str_is_set(&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) {
223 osmo_mgcpc_ep_ci_dlcx(rtps->ci);
224 rtps->ci = NULL;
225 }
226}
227
228void rtp_stream_fsm_discarding_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
229{
230 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
231}
232
233static const struct value_string rtp_stream_fsm_event_names[] = {
234 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_OK),
235 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_FAIL),
236 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_OK),
237 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_FAIL),
238 {}
239};
240
241#define S(x) (1 << (x))
242
243static const struct osmo_fsm_state rtp_stream_fsm_states[] = {
244 [RTP_STREAM_ST_UNINITIALIZED] = {
245 .name = "UNINITIALIZED",
246 .out_state_mask = 0
247 | S(RTP_STREAM_ST_ESTABLISHING)
248 | S(RTP_STREAM_ST_DISCARDING)
249 ,
250 },
251 [RTP_STREAM_ST_ESTABLISHING] = {
252 .name = "ESTABLISHING",
253 .in_event_mask = 0
254 | S(RTP_STREAM_EV_CRCX_OK)
255 | S(RTP_STREAM_EV_CRCX_FAIL)
256 | S(RTP_STREAM_EV_MDCX_OK)
257 | S(RTP_STREAM_EV_MDCX_FAIL)
258 ,
259 .out_state_mask = 0
260 | S(RTP_STREAM_ST_ESTABLISHED)
261 | S(RTP_STREAM_ST_DISCARDING)
262 ,
263 .action = rtp_stream_fsm_establishing_established,
264 },
265 [RTP_STREAM_ST_ESTABLISHED] = {
266 .name = "ESTABLISHED",
267 .out_state_mask = 0
268 | S(RTP_STREAM_ST_ESTABLISHING)
269 | S(RTP_STREAM_ST_DISCARDING)
270 ,
271 .onenter = rtp_stream_fsm_established_onenter,
272 .action = rtp_stream_fsm_establishing_established,
273 },
274 [RTP_STREAM_ST_DISCARDING] = {
275 .name = "DISCARDING",
276 .onenter = rtp_stream_fsm_discarding_onenter,
277 .out_state_mask = 0
278 | S(RTP_STREAM_ST_DISCARDING)
279 ,
280 },
281};
282
283static struct osmo_fsm rtp_stream_fsm = {
284 .name = "rtp_stream",
285 .states = rtp_stream_fsm_states,
286 .num_states = ARRAY_SIZE(rtp_stream_fsm_states),
287 .log_subsys = DCC,
288 .event_names = rtp_stream_fsm_event_names,
289 .timer_cb = rtp_stream_fsm_timer_cb,
290 .cleanup = rtp_stream_fsm_cleanup,
291};
292
293static int rtp_stream_do_mgcp_verb(struct rtp_stream *rtps, enum mgcp_verb verb, uint32_t ok_event, uint32_t fail_event)
294{
295 struct mgcp_conn_peer verb_info;
296
297 if (!rtps->ci) {
298 LOG_RTPS(rtps, LOGL_ERROR, "Cannot send %s, no endpoint CI allocated\n", osmo_mgcp_verb_name(verb));
299 return -EINVAL;
300 }
301
302 verb_info = (struct mgcp_conn_peer){
303 .call_id = rtps->call_id,
304 .ptime = 20,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200305 .x_osmo_osmux_use = rtps->use_osmux,
306 .x_osmo_osmux_cid = rtps->remote_osmux_cid,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100307 };
308
309 if (verb == MGCP_VERB_CRCX)
310 verb_info.conn_mode = rtps->crcx_conn_mode;
311
312 if (rtps->codec_known) {
313 verb_info.codecs[0] = rtps->codec;
314 verb_info.codecs_len = 1;
315 rtps->codec_sent_to_mgw = true;
316 }
317 if (osmo_sockaddr_str_is_set(&rtps->remote)) {
318 int rc = osmo_strlcpy(verb_info.addr, rtps->remote.ip, sizeof(verb_info.addr));
319 if (rc <= 0 || rc >= sizeof(verb_info.addr)) {
320 LOG_RTPS(rtps, LOGL_ERROR, "Failure to write IP address to MGCP message (rc=%d)\n", rc);
321 return -ENOSPC;
322 }
323 verb_info.port = rtps->remote.port;
324 rtps->remote_sent_to_mgw = true;
325 }
326
327 osmo_mgcpc_ep_ci_request(rtps->ci, verb, &verb_info, rtps->fi, ok_event, fail_event, NULL);
328 return 0;
329}
330
331int rtp_stream_ensure_ci(struct rtp_stream *rtps, struct osmo_mgcpc_ep *at_endpoint)
332{
333 if (rtps->ci)
334 return rtp_stream_commit(rtps);
335
336 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
337
338 rtps->ci = osmo_mgcpc_ep_ci_add(at_endpoint, "%s", rtp_direction_name(rtps->dir));
339 if (!rtps->ci)
340 return -ENODEV;
341
342 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_CRCX, RTP_STREAM_EV_CRCX_OK, RTP_STREAM_EV_CRCX_FAIL);
343}
344
345int rtp_stream_do_mdcx(struct rtp_stream *rtps)
346{
347 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_MDCX, RTP_STREAM_EV_MDCX_OK, RTP_STREAM_EV_MDCX_FAIL);
348}
349
350void rtp_stream_release(struct rtp_stream *rtps)
351{
352 if (!rtps)
353 return;
354
355 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
356}
357
358/* After setting up a remote RTP address or a new codec, call this to trigger an MDCX.
359 * The MDCX will only trigger if all data needed by an endpoint is available (both RTP address and codec) and if at
360 * least one of them has not yet been sent to the MGW in a previous CRCX or MDCX. */
361int rtp_stream_commit(struct rtp_stream *rtps)
362{
363 if (!rtps->ci) {
364 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no MGW endpoint CI set up\n");
365 return -1;
366 }
367 if (!osmo_sockaddr_str_is_set(&rtps->remote)) {
368 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no remote RTP address known\n");
369 return -1;
370 }
371 if (!rtps->codec_known) {
372 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no codec known\n");
373 return -1;
374 }
375 if (rtps->remote_sent_to_mgw && rtps->codec_sent_to_mgw) {
376 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: both remote RTP address and codec already set up at MGW\n");
377 return 0;
378 }
379
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200380 LOG_RTPS(rtps, LOGL_DEBUG, "Committing: Tx MDCX to update the MGW: updating%s%s%s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100381 rtps->remote_sent_to_mgw ? "" : " remote-RTP-IP-port",
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200382 rtps->codec_sent_to_mgw ? "" : " codec",
383 (!rtps->use_osmux || rtps->remote_osmux_cid_sent_to_mgw) ? "" : " remote-Osmux-CID");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100384 return rtp_stream_do_mdcx(rtps);
385}
386
387void rtp_stream_set_codec(struct rtp_stream *rtps, enum mgcp_codecs codec)
388{
389 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
390 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
391 LOG_RTPS(rtps, LOGL_DEBUG, "setting codec to %s\n", osmo_mgcpc_codec_name(codec));
392 rtps->codec = codec;
393 rtps->codec_known = true;
394 rtps->codec_sent_to_mgw = false;
395 rtp_stream_update_id(rtps);
396}
397
398void rtp_stream_set_remote_addr(struct rtp_stream *rtps, const struct osmo_sockaddr_str *r)
399{
400 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
401 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
402 LOG_RTPS(rtps, LOGL_DEBUG, "setting remote addr to " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(r));
403 rtps->remote = *r;
404 rtps->remote_sent_to_mgw = false;
405 rtp_stream_update_id(rtps);
406}
407
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200408void rtp_stream_set_remote_osmux_cid(struct rtp_stream *rtps, uint8_t osmux_cid)
409{
410 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
411 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
412 LOG_RTPS(rtps, LOGL_DEBUG, "setting remote Osmux CID to %u\n", osmux_cid);
413 rtps->remote_osmux_cid = osmux_cid;
414 rtps->remote_osmux_cid_sent_to_mgw = false;
415 rtp_stream_update_id(rtps);
416}
417
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100418bool rtp_stream_is_established(struct rtp_stream *rtps)
419{
420 if (!rtps)
421 return false;
422 if (!rtps->fi)
423 return false;
424 if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED)
425 return false;
426 if (!rtps->remote_sent_to_mgw
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200427 || !rtps->codec_sent_to_mgw
428 || (rtps->use_osmux && !rtps->remote_osmux_cid_sent_to_mgw))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100429 return false;
430 return true;
431}