blob: 34717e937204b128aeded4e6e7e46d69ecdcd382 [file] [log] [blame]
Neels Hofmeyr538d2c52019-01-28 03:51:35 +01001/* FSM to manage multiple connections of an MGW endpoint
2 *
3 * (C) 2018-2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr <neels@hofmeyr.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26
27#include <osmocom/core/fsm.h>
28#include <osmocom/core/byteswap.h>
29#include <osmocom/core/tdef.h>
30
31#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
32
33#define LOG_CI(ci, level, fmt, args...) do { \
34 if (!ci || !ci->ep) \
35 LOGP(DLGLOBAL, level, "(unknown MGW endpoint) " fmt, ## args); \
36 else \
37 LOG_MGCPC_EP(ci->ep, level, "CI[%d] %s%s%s: " fmt, \
38 (int)(ci - ci->ep->ci), \
39 ci->label ? : "-", \
40 ci->mgcp_ci_str[0] ? " CI=" : "", \
41 ci->mgcp_ci_str[0] ? ci->mgcp_ci_str : "", \
42 ## args); \
43 } while(0)
44
45#define LOG_CI_VERB(ci, level, fmt, args...) do { \
46 if (ci->verb_info.addr[0]) \
47 LOG_CI(ci, level, "%s %s:%u: " fmt, \
48 osmo_mgcp_verb_name(ci->verb), ci->verb_info.addr, ci->verb_info.port, \
49 ## args); \
50 else \
51 LOG_CI(ci, level, "%s: " fmt, \
52 osmo_mgcp_verb_name(ci->verb), \
53 ## args); \
54 } while(0)
55
56enum osmo_mgcpc_ep_fsm_state {
57 OSMO_MGCPC_EP_ST_UNUSED = 0,
58 OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE,
59 OSMO_MGCPC_EP_ST_IN_USE,
60};
61
62enum osmo_mgcpc_ep_fsm_event {
63 _OSMO_MGCPC_EP_EV_LAST = 0,
64 /* and MGW response events are allocated dynamically */
65};
66
67#define FIRST_CI_EVENT (_OSMO_MGCPC_EP_EV_LAST + (_OSMO_MGCPC_EP_EV_LAST & 1)) /* rounded up to even nr */
68#define USABLE_CI ((32 - FIRST_CI_EVENT)/2)
69#define EV_TO_CI_IDX(event) ((event - FIRST_CI_EVENT) / 2)
70
71#define CI_EV_SUCCESS(ci) (FIRST_CI_EVENT + (((ci) - ci->ep->ci) * 2))
72#define CI_EV_FAILURE(ci) (CI_EV_SUCCESS(ci) + 1)
73
74static struct osmo_fsm osmo_mgcpc_ep_fsm;
75
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010076struct fsm_notify {
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +010077 struct llist_head entry;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010078 struct osmo_fsm_inst *fi;
79 uint32_t success;
80 uint32_t failure;
81 void *data;
82};
83
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010084/*! One connection on an endpoint, corresponding to a connection identifier (CI) as returned by the MGW.
85 * An endpoint has a fixed number of slots of these, which may or may not be in use.
86 */
87struct osmo_mgcpc_ep_ci {
88 struct osmo_mgcpc_ep *ep;
89
90 bool occupied;
91 char label[64];
92 struct osmo_fsm_inst *mgcp_client_fi;
93
94 bool pending;
95 bool sent;
96 enum mgcp_verb verb;
97 struct mgcp_conn_peer verb_info;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +010098 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +010099
100 bool got_port_info;
101 struct mgcp_conn_peer rtp_info;
102 char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
103};
104
105/*! An MGW endpoint with N connections, like "rtpbridge/23@mgw". */
106struct osmo_mgcpc_ep {
107 /*! MGCP client connection to the MGW. */
108 struct mgcp_client *mgcp_client;
109 struct osmo_fsm_inst *fi;
110
111 /*! Endpoint string; at first this might be a wildcard, and upon the first CRCX OK response, this will reflect
112 * the endpoint name returned by the MGW. */
113 char endpoint[MGCP_ENDPOINT_MAXLEN];
114
115 /*! Timeout definitions used for this endpoint, see osmo_mgcpc_ep_fsm_timeouts. */
116 const struct osmo_tdef *T_defs;
117
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200118 /*! True as soon as the first CRCX OK is received. The endpoint name may be determined by the first CRCX
119 * response, so to dispatch any other messages, the FSM instance *must* wait for the first CRCX OK to arrive
120 * first. Once the endpoint name is pinpointed, any amount of operations may be dispatched concurrently. */
121 bool first_crcx_complete;
122
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100123 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
124 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
125 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100126
127 /*! Internal use: if a function keeps an fsm_notify for later dispatch while already clearing or re-using the
128 * ci[], the fsm_notify should be kept here to also get canceled by osmo_mgcpc_ep_cancel_notify(). */
129 struct llist_head background_notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100130};
131
132const struct value_string osmo_mgcp_verb_names[] = {
133 { MGCP_VERB_CRCX, "CRCX" },
134 { MGCP_VERB_MDCX, "MDCX" },
135 { MGCP_VERB_DLCX, "DLCX" },
136 { MGCP_VERB_AUEP, "AUEP" },
137 { MGCP_VERB_RSIP, "RSIP" },
138 {}
139};
140
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200141static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
142 int *waiting_for_response);
143
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100144static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
145{
146 if (!ci)
147 return NULL;
148 if (!ci->ep)
149 return NULL;
150 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
151 return NULL;
152 return ci;
153}
154
155static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
156{
157 int idx;
158 if (event < FIRST_CI_EVENT)
159 return NULL;
160 idx = EV_TO_CI_IDX(event);
161 if (idx >= sizeof(ep->ci))
162 return NULL;
163 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
164}
165
166const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
167{
168 if (!ep)
169 return "NULL";
170 if (ep->endpoint[0])
171 return ep->endpoint;
172 return osmo_fsm_inst_name(ep->fi);
173}
174
175const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
176{
177 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
178 * this buffer is too small. */
179 static char buf[1024];
180
181 if (!info)
182 return "NULL";
183
184 if (info->endpoint[0]
185 && info->addr[0])
186 snprintf(buf, sizeof(buf), "%s:%s:%u",
187 info->endpoint, info->addr, info->port);
188 else if (info->endpoint[0])
189 snprintf(buf, sizeof(buf), "%s", info->endpoint);
190 else if (info->addr[0])
191 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
192 else
193 return "empty";
194 return buf;
195}
196
197const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
198{
199 const struct mgcp_conn_peer *rtp_info;
200
201 if (!ci)
202 return "NULL";
203
204 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
205
206 if (rtp_info)
207 return mgcp_conn_peer_name(rtp_info);
208 return osmo_mgcpc_ep_name(ci->ep);
209}
210
211const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
212{
213 if (!ci || !ci->mgcp_ci_str[0])
214 return NULL;
215 return ci->mgcp_ci_str;
216}
217
218static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
219
220static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
221
222static void fill_event_names()
223{
224 int i;
225 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
226 if (i < _OSMO_MGCPC_EP_EV_LAST)
227 continue;
228 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
229 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
230 continue;
231 }
232 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
233 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
234 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
235 }
236}
237
238/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
239 * default MGCP port in osmo-mgw). */
240static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
241{
242 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
243 fill_event_names();
244}
245
246struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
247{
248 OSMO_ASSERT(fi);
249 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
250 OSMO_ASSERT(fi->priv);
251 return fi->priv;
252}
253
254/*! Allocate an osmo_mgcpc_ep FSM.
255 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
256 * osmo_mgcpc_ep_ci_request() is invoked.
257 *
258 * A typical sequence of events would be:
259 *
260 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
261 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
262 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
263 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
264 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
265 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
266 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
267 * ...
268 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
269 * ...
270 * osmo_mgcpc_ep_clear(ep);
271 * ep = NULL;
272 *
273 * \param parent Parent FSM.
274 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
275 * \param mgcp_client Connection to the MGW.
276 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
277 * \param fsm_id FSM instance ID.
278 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
279 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
280 */
281struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
282 struct mgcp_client *mgcp_client,
283 const struct osmo_tdef *T_defs,
284 const char *fsm_id,
285 const char *endpoint_str_fmt, ...)
286{
287 va_list ap;
288 struct osmo_fsm_inst *fi;
289 struct osmo_mgcpc_ep *ep;
290 int rc;
291
292 if (!mgcp_client)
293 return NULL;
294
295 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
296 OSMO_ASSERT(fi);
297
298 osmo_fsm_inst_update_id(fi, fsm_id);
299
300 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
301 OSMO_ASSERT(ep);
302
303 *ep = (struct osmo_mgcpc_ep){
304 .mgcp_client = mgcp_client,
305 .fi = fi,
306 .T_defs = T_defs,
307 };
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100308 INIT_LLIST_HEAD(&ep->background_notify);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100309 fi->priv = ep;
310
311 va_start(ap, endpoint_str_fmt);
312 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
313 va_end(ap);
314
315 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
316 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
317 ep->endpoint);
318 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
319 return NULL;
320 }
321
322 return ep;
323}
324
325/*! Add a connection to an endpoint.
326 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
327 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
328 * \param ep Parent endpoint instance.
329 * \param label_fmt Label for logging.
330 */
331struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
332 const char *label_fmt, ...)
333{
334 va_list ap;
335 int i;
336 struct osmo_mgcpc_ep_ci *ci;
337
338 for (i = 0; i < USABLE_CI; i++) {
339 ci = &ep->ci[i];
340
341 if (ci->occupied || ci->mgcp_client_fi)
342 continue;
343
344 *ci = (struct osmo_mgcpc_ep_ci){
345 .ep = ep,
346 .occupied = true,
347 };
348 if (label_fmt) {
349 va_start(ap, label_fmt);
350 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
351 va_end(ap);
352 }
353 return ci;
354 }
355
356 LOG_MGCPC_EP(ep, LOGL_ERROR,
357 "Cannot allocate another endpoint, all "
358 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
359
360 return NULL;
361}
362
363static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
364
365static void on_failure(struct osmo_mgcpc_ep_ci *ci)
366{
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200367 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100368 struct fsm_notify notify;
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200369 int i;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100370
371 if (!ci->occupied)
372 return;
373
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100374 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn right
375 * away. So we must be ready with a cleared *ci. Store the notify separately and clear before dispatching. */
376 notify = ci->notify;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100377 /* Register the planned notification in ep->background_notify so we also catch any osmo_mgcpc_ep_cancel_notify()
378 * that might be triggered between clearing the ci and actually dispatching the event. */
379 llist_add(&notify.entry, &ep->background_notify);
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100380
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100381 *ci = (struct osmo_mgcpc_ep_ci){
382 .ep = ci->ep,
383 };
384
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200385 /* An MGCP failure typically means the endpoint becomes unusable, cancel all pending request (except DLCX).
386 * Particularly, if two CRCX were scheduled and the first fails, we must no longer dispatch the second CRCX. */
387 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
388 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
389 if (other_ci == ci)
390 continue;
391 if (!other_ci->occupied)
392 continue;
393 if (!other_ci->pending)
394 continue;
395 if (other_ci->sent)
396 continue;
397 if (other_ci->verb == MGCP_VERB_DLCX)
398 continue;
399 /* Just clear the pending request, don't fire more events than below. */
400 other_ci->pending = false;
401 }
402
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100403 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
404 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100405 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ep->fi)) {
406 /* The ep has deallocated, no need to llist_del(&notify.entry) here. */
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100407 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100408 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100409
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100410 if (notify.fi)
411 osmo_fsm_inst_dispatch(notify.fi, notify.failure, notify.data);
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100412
413 llist_del(&notify.entry);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100414}
415
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200416static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
417{
418 struct osmo_mgcpc_ep *ep = ci->ep;
419 int rc;
420 int i;
421
422 if (!strcmp(ep->endpoint, new_endpoint_name)) {
423 /* Same endpoint name, nothing to do. */
424 return 0;
425 }
426
427 /* The endpoint name should only change on the very first CRCX response. */
428 if (ep->first_crcx_complete) {
429 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
430 " This is endpoint %s, instead received %s\n",
431 ep->endpoint, new_endpoint_name);
432 on_failure(ci);
433 return -EINVAL;
434 }
435
436 /* This is the first CRCX response, update endpoint name. */
437 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
438 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
439 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
440 osmo_mgcpc_ep_ci_dlcx(ci);
441 on_failure(ci);
442 return -ENOSPC;
443 }
444
445 /* Make sure already pending requests use this updated endpoint name. */
446 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
447 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
448 if (!other_ci->occupied)
449 continue;
450 if (!other_ci->pending)
451 continue;
452 if (other_ci->sent)
453 continue;
454 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
455 }
456 return 0;
457}
458
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100459static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
460{
461 struct mgcp_conn_peer *rtp_info;
462
463 if (!ci->occupied)
464 return;
465
466 ci->pending = false;
467
468 switch (ci->verb) {
469 case MGCP_VERB_CRCX:
470 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
471 * name here. Also, we receive the MGW's RTP port information. */
472 rtp_info = data;
473 OSMO_ASSERT(rtp_info);
474 ci->got_port_info = true;
475 ci->rtp_info = *rtp_info;
476 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
477 sizeof(ci->mgcp_ci_str));
478 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200479 /* On errors, this instance might already be deallocated. Make sure to not access anything after
480 * error. */
481 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100482 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100483 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200484 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100485 break;
486
487 default:
488 break;
489 }
490
491 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
492 osmo_mgcp_verb_name(ci->verb),
493 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100494 ci->notify.fi ? "" : " (not sending a notification)");
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100495
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100496 if (ci->notify.fi)
497 osmo_fsm_inst_dispatch(ci->notify.fi, ci->notify.success, ci->notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100498
499 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
500}
501
502/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
503const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
504{
505 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
506 if (!ci)
507 return NULL;
508 if (!ci->got_port_info)
509 return NULL;
510 return &ci->rtp_info;
511}
512
513/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
514bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
515{
516 const struct mgcp_conn_peer *rtp_info;
517 struct sockaddr_in *sin;
518
519 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
520 if (!rtp_info)
521 return false;
522
523 sin = (struct sockaddr_in *)dest;
524
525 sin->sin_family = AF_INET;
526 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
527 sin->sin_port = osmo_ntohs(rtp_info->port);
528 return true;
529}
530
Pau Espin Pedrol30907dc2019-05-06 11:54:17 +0200531bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid)
532{
533 const struct mgcp_conn_peer *rtp_info;
534
535 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
536 if (!rtp_info)
537 return false;
538
539 if (!rtp_info->x_osmo_osmux_use)
540 return false;
541
542 *cid = rtp_info->x_osmo_osmux_cid;
543 return true;
544}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100545
546static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
547 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
548};
549
550/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
551 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
552 * Assumes local variable fi exists. */
553#define osmo_mgcpc_ep_fsm_state_chg(state) \
554 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
555 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
556
557/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100558 *
559 * If the 'notify' instance deallocates before it received a notification of event_success or event_failure,
560 * osmo_mgcpc_ep_ci_cancel_notify() or osmo_mgcpc_ep_cancel_notify() must be called. It is not harmful to cancel
561 * notification after an event has been received.
562 *
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100563 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
564 * \param verb MGCP operation to dispatch.
565 * \param verb_info Parameters for the MGCP operation.
566 * \param notify Peer FSM instance to notify of completed/failed operation.
567 * \param event_success Which event to dispatch to 'notify' upon OK response.
568 * \param event_failure Which event to dispatch to 'notify' upon failure response.
569 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
570 */
571void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
572 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
573 struct osmo_fsm_inst *notify,
574 uint32_t event_success, uint32_t event_failure,
575 void *notify_data)
576{
577 struct osmo_mgcpc_ep *ep;
578 struct osmo_fsm_inst *fi;
579 struct osmo_mgcpc_ep_ci cleared_ci;
580 ci = osmo_mgcpc_ep_check_ci(ci);
581
582 if (!ci) {
583 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
584 goto dispatch_error;
585 }
586 if (!verb_info && verb != MGCP_VERB_DLCX) {
587 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
588 osmo_mgcp_verb_name(verb));
589 goto dispatch_error;
590 }
591 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
592 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
593 osmo_mgcp_verb_name(verb));
594 goto dispatch_error;
595 }
596
597 ep = ci->ep;
598 fi = ep->fi;
599
600 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
601 * the char[] directly, dance through cleared_ci and copy back. */
602 cleared_ci = (struct osmo_mgcpc_ep_ci){
603 .ep = ep,
604 .mgcp_client_fi = ci->mgcp_client_fi,
605 .got_port_info = ci->got_port_info,
606 .rtp_info = ci->rtp_info,
607
608 .occupied = true,
609 /* .pending = true follows below */
610 .verb = verb,
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100611 .notify = {
612 .fi = notify,
613 .success = event_success,
614 .failure = event_failure,
615 .data = notify_data,
616 }
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100617 };
618 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
619 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
620 *ci = cleared_ci;
621
Neels Hofmeyr3ff71282019-10-29 17:41:20 +0100622 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify.fi));
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100623
624 if (verb_info)
625 ci->verb_info = *verb_info;
626
627 if (ep->endpoint[0]) {
628 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
629 LOG_CI(ci, LOGL_ERROR,
630 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
631 " Using the proper endpoint instead.\n",
632 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
633 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
634 }
635
636 switch (ci->verb) {
637 case MGCP_VERB_CRCX:
638 if (ci->mgcp_client_fi) {
639 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
640 on_failure(ci);
641 return;
642 }
643 break;
644
645 case MGCP_VERB_MDCX:
646 if (!ci->mgcp_client_fi) {
647 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
648 osmo_mgcp_verb_name(ci->verb));
649 on_failure(ci);
650 return;
651 }
652 break;
653
654 case MGCP_VERB_DLCX:
655 if (!ci->mgcp_client_fi) {
656 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
657 return;
658 }
659 break;
660
661 default:
662 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
663 on_failure(ci);
664 return;
665 }
666
667 ci->pending = true;
668
669 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
670
671 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
672 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
673
674 return;
675dispatch_error:
676 if (notify)
677 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
678}
679
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100680/*! No longer notify for any state changes for any conns of this endpoint.
681 * Useful if the notify instance passed to osmo_mgcpc_ep_ci_request() is about to deallocate.
682 * \param ep The endpoint FSM instance.
683 * \param notify Which target to cancel notification for, if NULL cancel all notifications. */
684void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify)
685{
686 struct fsm_notify *n;
687 int i;
688 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
689 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
690 if (!notify || ci->notify.fi == notify)
691 ci->notify.fi = NULL;
692 }
693 llist_for_each_entry(n, &ep->background_notify, entry) {
694 if (!notify || n->fi == notify)
695 n->fi = NULL;
696 }
697
698}
699
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100700static int send_verb(struct osmo_mgcpc_ep_ci *ci)
701{
702 int rc;
703 struct osmo_mgcpc_ep *ep = ci->ep;
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100704 struct fsm_notify notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100705
706 if (!ci->occupied || !ci->pending || ci->sent)
707 return 0;
708
709 switch (ci->verb) {
710
711 case MGCP_VERB_CRCX:
712 OSMO_ASSERT(!ci->mgcp_client_fi);
713 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
714 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
715 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
716 &ci->verb_info);
717 ci->sent = true;
718 if (!ci->mgcp_client_fi){
719 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
720 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200721 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100722 }
723 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
724 break;
725
726 case MGCP_VERB_MDCX:
727 OSMO_ASSERT(ci->mgcp_client_fi);
728 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
729 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
730 ci->sent = true;
731 if (rc) {
732 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
733 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200734 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100735 }
736 break;
737
738 case MGCP_VERB_DLCX:
739 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
740 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
741 /* The way this is designed, we actually need to forget all about the ci right away. */
742 mgcp_conn_delete(ci->mgcp_client_fi);
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100743 notify = ci->notify;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100744 *ci = (struct osmo_mgcpc_ep_ci){
745 .ep = ep,
746 };
Neels Hofmeyr055ded72019-10-29 17:46:30 +0100747 /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn
748 * right away. So we must be ready with a cleared *ci. */
749 if (notify.fi)
750 osmo_fsm_inst_dispatch(notify.fi, notify.success, notify.data);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100751 break;
752
753 default:
754 OSMO_ASSERT(false);
755 }
756
757 return 1;
758}
759
760/*! DLCX all connections, terminate the endpoint FSM and free. */
761void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
762{
763 if (!ep)
764 return;
Neels Hofmeyrf2bf8dc2019-10-29 17:39:56 +0100765 osmo_mgcpc_ep_cancel_notify(ep, NULL);
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100766 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
767}
768
769static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
770 int *waiting_for_response)
771{
772 int i;
773
774 if (occupied)
775 *occupied = 0;
776
777 if (pending_not_sent)
778 *pending_not_sent = 0;
779
780 if (waiting_for_response)
781 *waiting_for_response = 0;
782
783 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
784 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
785 if (ci->occupied) {
786 if (occupied)
787 (*occupied)++;
788 } else
789 continue;
790
791 if (ci->pending)
792 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
793 ci->sent ? "waiting for response" : "waiting to be sent");
794 else
795 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
796
797 if (ci->pending && ci->sent)
798 if (waiting_for_response)
799 (*waiting_for_response)++;
800 if (ci->pending && !ci->sent)
801 if (pending_not_sent)
802 (*pending_not_sent)++;
803 }
804}
805
806static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
807{
808 int waiting_for_response;
809 int occupied;
810 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
811
812 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
813 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
814
815 if (!occupied) {
816 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
817 * terminating. */
818 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
819 return false;
820 }
821
822 if (!waiting_for_response) {
823 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
824 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
825 }
826
827 return true;
828}
829
830static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
831{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200832 static int re_enter = 0;
833 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100834 int count = 0;
835 int i;
836 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
837
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200838 re_enter++;
839 OSMO_ASSERT(re_enter < 10);
840
841 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
842 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
843 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
844 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
845 * sent (which needs to be a CRCX). */
846
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100847 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200848 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
849
850 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
851 if (!ep->first_crcx_complete) {
852 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
853 continue;
854 }
855
856 rc = send_verb(&ep->ci[i]);
857 /* Need to be careful not to access the instance after failure. Event chains may already have
858 * deallocated this memory. */
859 if (rc < 0)
860 return;
861 if (!rc)
862 continue;
863 count++;
864 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
865 if (!ep->first_crcx_complete)
866 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100867 }
868
869 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200870 if (ep->first_crcx_complete)
871 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
872 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100873}
874
875static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
876{
877 struct osmo_mgcpc_ep_ci *ci;
878 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
879 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
880 if (ci) {
881 if (event == CI_EV_SUCCESS(ci))
882 on_success(ci, data);
883 else
884 on_failure(ci);
885 }
886}
887
888static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
889{
890 int pending_not_sent;
891 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
892
893 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
894 if (pending_not_sent)
895 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
896}
897
898#define S(x) (1 << (x))
899
900static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
901 [OSMO_MGCPC_EP_ST_UNUSED] = {
902 .name = "UNUSED",
903 .in_event_mask = 0,
904 .out_state_mask = 0
905 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
906 ,
907 },
908 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
909 .name = "WAIT_MGW_RESPONSE",
910 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
911 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
912 .in_event_mask = 0xffffffff,
913 .out_state_mask = 0
914 | S(OSMO_MGCPC_EP_ST_IN_USE)
915 ,
916 },
917 [OSMO_MGCPC_EP_ST_IN_USE] = {
918 .name = "IN_USE",
919 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
920 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
921 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
922 .out_state_mask = 0
923 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
924 ,
925 },
926};
927
928static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
929{
930 int i;
931 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
932
933 switch (fi->T) {
934 default:
935 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
936 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
937 if (!ci->occupied)
938 continue;
939 if (!(ci->pending && ci->sent))
940 continue;
941 on_failure(ci);
942 }
943 return 0;
944 }
945
946 return 0;
947}
948
949static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200950 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100951 .states = osmo_mgcpc_ep_fsm_states,
952 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
953 .log_subsys = DLMGCP,
954 .event_names = osmo_mgcpc_ep_fsm_event_names,
955 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
956 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
957};