blob: afe24ad513b6783aaf77e03de5895bfebd31b57a [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");
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. */
102struct 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
127static 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
137static 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);
Vadim Yanitskiye0ef6d12019-05-11 04:04:59 +0700144 if (!crcx_info) {
145 LOG_RTPS(rtps, LOGL_ERROR, "osmo_mgcpc_ep_ci_get_rtp_info() has "
146 "failed, ignoring %s\n", osmo_fsm_event_name(fi->fsm, event));
147 return;
148 }
149
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100150 osmo_sockaddr_str_from_str(&rtps->local, crcx_info->addr, crcx_info->port);
151 rtp_stream_update_id(rtps);
152 osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, rtps);
153 check_established(rtps);
154
155 if ((!rtps->remote_sent_to_mgw || !rtps->codec_sent_to_mgw)
156 && osmo_sockaddr_str_is_set(&rtps->remote)
157 && rtps->codec_known) {
158 LOG_RTPS(rtps, LOGL_DEBUG,
159 "local ip:port set;%s%s triggering MDCX to send the new settings\n",
160 (!rtps->remote_sent_to_mgw)? " remote ip:port not yet sent," : "",
161 (!rtps->codec_sent_to_mgw)? " codec not yet sent," : "");
162 rtp_stream_do_mdcx(rtps);
163 }
164 return;
165
166 case RTP_STREAM_EV_MDCX_OK:
167 rtp_stream_update_id(rtps);
168 check_established(rtps);
169 return;
170
171 case RTP_STREAM_EV_CRCX_FAIL:
172 case RTP_STREAM_EV_MDCX_FAIL:
173 rtps->remote_sent_to_mgw = false;
174 rtps->codec_sent_to_mgw = false;
175 rtp_stream_update_id(rtps);
176 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
177 return;
178
179 default:
180 OSMO_ASSERT(false);
181 };
182}
183
184void rtp_stream_fsm_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
185{
186 struct rtp_stream *rtps = fi->priv;
187 osmo_fsm_inst_dispatch(fi->proc.parent, CALL_LEG_EV_RTP_STREAM_ESTABLISHED, rtps);
188}
189
190static int rtp_stream_fsm_timer_cb(struct osmo_fsm_inst *fi)
191{
192 struct rtp_stream *rtps = fi->priv;
193 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
194 return 0;
195}
196
197static void rtp_stream_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
198{
199 struct rtp_stream *rtps = fi->priv;
200 if (rtps->ci) {
201 osmo_mgcpc_ep_ci_dlcx(rtps->ci);
202 rtps->ci = NULL;
203 }
204}
205
206void rtp_stream_fsm_discarding_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
207{
208 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
209}
210
211static const struct value_string rtp_stream_fsm_event_names[] = {
212 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_OK),
213 OSMO_VALUE_STRING(RTP_STREAM_EV_CRCX_FAIL),
214 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_OK),
215 OSMO_VALUE_STRING(RTP_STREAM_EV_MDCX_FAIL),
216 {}
217};
218
219#define S(x) (1 << (x))
220
221static const struct osmo_fsm_state rtp_stream_fsm_states[] = {
222 [RTP_STREAM_ST_UNINITIALIZED] = {
223 .name = "UNINITIALIZED",
224 .out_state_mask = 0
225 | S(RTP_STREAM_ST_ESTABLISHING)
226 | S(RTP_STREAM_ST_DISCARDING)
227 ,
228 },
229 [RTP_STREAM_ST_ESTABLISHING] = {
230 .name = "ESTABLISHING",
231 .in_event_mask = 0
232 | S(RTP_STREAM_EV_CRCX_OK)
233 | S(RTP_STREAM_EV_CRCX_FAIL)
234 | S(RTP_STREAM_EV_MDCX_OK)
235 | S(RTP_STREAM_EV_MDCX_FAIL)
236 ,
237 .out_state_mask = 0
238 | S(RTP_STREAM_ST_ESTABLISHED)
239 | S(RTP_STREAM_ST_DISCARDING)
240 ,
241 .action = rtp_stream_fsm_establishing_established,
242 },
243 [RTP_STREAM_ST_ESTABLISHED] = {
244 .name = "ESTABLISHED",
245 .out_state_mask = 0
246 | S(RTP_STREAM_ST_ESTABLISHING)
247 | S(RTP_STREAM_ST_DISCARDING)
248 ,
249 .onenter = rtp_stream_fsm_established_onenter,
250 .action = rtp_stream_fsm_establishing_established,
251 },
252 [RTP_STREAM_ST_DISCARDING] = {
253 .name = "DISCARDING",
254 .onenter = rtp_stream_fsm_discarding_onenter,
255 .out_state_mask = 0
256 | S(RTP_STREAM_ST_DISCARDING)
257 ,
258 },
259};
260
261static struct osmo_fsm rtp_stream_fsm = {
262 .name = "rtp_stream",
263 .states = rtp_stream_fsm_states,
264 .num_states = ARRAY_SIZE(rtp_stream_fsm_states),
265 .log_subsys = DCC,
266 .event_names = rtp_stream_fsm_event_names,
267 .timer_cb = rtp_stream_fsm_timer_cb,
268 .cleanup = rtp_stream_fsm_cleanup,
269};
270
271static int rtp_stream_do_mgcp_verb(struct rtp_stream *rtps, enum mgcp_verb verb, uint32_t ok_event, uint32_t fail_event)
272{
273 struct mgcp_conn_peer verb_info;
274
275 if (!rtps->ci) {
276 LOG_RTPS(rtps, LOGL_ERROR, "Cannot send %s, no endpoint CI allocated\n", osmo_mgcp_verb_name(verb));
277 return -EINVAL;
278 }
279
280 verb_info = (struct mgcp_conn_peer){
281 .call_id = rtps->call_id,
282 .ptime = 20,
283 };
284
285 if (verb == MGCP_VERB_CRCX)
286 verb_info.conn_mode = rtps->crcx_conn_mode;
287
288 if (rtps->codec_known) {
289 verb_info.codecs[0] = rtps->codec;
290 verb_info.codecs_len = 1;
291 rtps->codec_sent_to_mgw = true;
292 }
293 if (osmo_sockaddr_str_is_set(&rtps->remote)) {
294 int rc = osmo_strlcpy(verb_info.addr, rtps->remote.ip, sizeof(verb_info.addr));
295 if (rc <= 0 || rc >= sizeof(verb_info.addr)) {
296 LOG_RTPS(rtps, LOGL_ERROR, "Failure to write IP address to MGCP message (rc=%d)\n", rc);
297 return -ENOSPC;
298 }
299 verb_info.port = rtps->remote.port;
300 rtps->remote_sent_to_mgw = true;
301 }
302
303 osmo_mgcpc_ep_ci_request(rtps->ci, verb, &verb_info, rtps->fi, ok_event, fail_event, NULL);
304 return 0;
305}
306
307int rtp_stream_ensure_ci(struct rtp_stream *rtps, struct osmo_mgcpc_ep *at_endpoint)
308{
309 if (rtps->ci)
310 return rtp_stream_commit(rtps);
311
312 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
313
314 rtps->ci = osmo_mgcpc_ep_ci_add(at_endpoint, "%s", rtp_direction_name(rtps->dir));
315 if (!rtps->ci)
316 return -ENODEV;
317
318 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_CRCX, RTP_STREAM_EV_CRCX_OK, RTP_STREAM_EV_CRCX_FAIL);
319}
320
321int rtp_stream_do_mdcx(struct rtp_stream *rtps)
322{
323 return rtp_stream_do_mgcp_verb(rtps, MGCP_VERB_MDCX, RTP_STREAM_EV_MDCX_OK, RTP_STREAM_EV_MDCX_FAIL);
324}
325
326void rtp_stream_release(struct rtp_stream *rtps)
327{
328 if (!rtps)
329 return;
330
331 rtp_stream_state_chg(rtps, RTP_STREAM_ST_DISCARDING);
332}
333
334/* After setting up a remote RTP address or a new codec, call this to trigger an MDCX.
335 * The MDCX will only trigger if all data needed by an endpoint is available (both RTP address and codec) and if at
336 * least one of them has not yet been sent to the MGW in a previous CRCX or MDCX. */
337int rtp_stream_commit(struct rtp_stream *rtps)
338{
339 if (!rtps->ci) {
340 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no MGW endpoint CI set up\n");
341 return -1;
342 }
343 if (!osmo_sockaddr_str_is_set(&rtps->remote)) {
344 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no remote RTP address known\n");
345 return -1;
346 }
347 if (!rtps->codec_known) {
348 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: no codec known\n");
349 return -1;
350 }
351 if (rtps->remote_sent_to_mgw && rtps->codec_sent_to_mgw) {
352 LOG_RTPS(rtps, LOGL_DEBUG, "Not committing: both remote RTP address and codec already set up at MGW\n");
353 return 0;
354 }
355
356 LOG_RTPS(rtps, LOGL_DEBUG, "Committing: Tx MDCX to update the MGW: updating%s%s\n",
357 rtps->remote_sent_to_mgw ? "" : " remote-RTP-IP-port",
358 rtps->codec_sent_to_mgw ? "" : " codec");
359 return rtp_stream_do_mdcx(rtps);
360}
361
362void rtp_stream_set_codec(struct rtp_stream *rtps, enum mgcp_codecs codec)
363{
364 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
365 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
366 LOG_RTPS(rtps, LOGL_DEBUG, "setting codec to %s\n", osmo_mgcpc_codec_name(codec));
367 rtps->codec = codec;
368 rtps->codec_known = true;
369 rtps->codec_sent_to_mgw = false;
370 rtp_stream_update_id(rtps);
371}
372
373void rtp_stream_set_remote_addr(struct rtp_stream *rtps, const struct osmo_sockaddr_str *r)
374{
375 if (rtps->fi->state == RTP_STREAM_ST_ESTABLISHED)
376 rtp_stream_state_chg(rtps, RTP_STREAM_ST_ESTABLISHING);
377 LOG_RTPS(rtps, LOGL_DEBUG, "setting remote addr to " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(r));
378 rtps->remote = *r;
379 rtps->remote_sent_to_mgw = false;
380 rtp_stream_update_id(rtps);
381}
382
383bool rtp_stream_is_established(struct rtp_stream *rtps)
384{
385 if (!rtps)
386 return false;
387 if (!rtps->fi)
388 return false;
389 if (rtps->fi->state != RTP_STREAM_ST_ESTABLISHED)
390 return false;
391 if (!rtps->remote_sent_to_mgw
392 || !rtps->codec_sent_to_mgw)
393 return false;
394 return true;
395}