blob: 3ea1492623256136f050ff132448bc2a1610460c [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
76/*! One connection on an endpoint, corresponding to a connection identifier (CI) as returned by the MGW.
77 * An endpoint has a fixed number of slots of these, which may or may not be in use.
78 */
79struct osmo_mgcpc_ep_ci {
80 struct osmo_mgcpc_ep *ep;
81
82 bool occupied;
83 char label[64];
84 struct osmo_fsm_inst *mgcp_client_fi;
85
86 bool pending;
87 bool sent;
88 enum mgcp_verb verb;
89 struct mgcp_conn_peer verb_info;
90 struct osmo_fsm_inst *notify;
91 uint32_t notify_success;
92 uint32_t notify_failure;
93 void *notify_data;
94
95 bool got_port_info;
96 struct mgcp_conn_peer rtp_info;
97 char mgcp_ci_str[MGCP_CONN_ID_LENGTH];
98};
99
100/*! An MGW endpoint with N connections, like "rtpbridge/23@mgw". */
101struct osmo_mgcpc_ep {
102 /*! MGCP client connection to the MGW. */
103 struct mgcp_client *mgcp_client;
104 struct osmo_fsm_inst *fi;
105
106 /*! Endpoint string; at first this might be a wildcard, and upon the first CRCX OK response, this will reflect
107 * the endpoint name returned by the MGW. */
108 char endpoint[MGCP_ENDPOINT_MAXLEN];
109
110 /*! Timeout definitions used for this endpoint, see osmo_mgcpc_ep_fsm_timeouts. */
111 const struct osmo_tdef *T_defs;
112
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200113 /*! True as soon as the first CRCX OK is received. The endpoint name may be determined by the first CRCX
114 * response, so to dispatch any other messages, the FSM instance *must* wait for the first CRCX OK to arrive
115 * first. Once the endpoint name is pinpointed, any amount of operations may be dispatched concurrently. */
116 bool first_crcx_complete;
117
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100118 /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success
119 * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */
120 struct osmo_mgcpc_ep_ci ci[USABLE_CI];
121};
122
123const struct value_string osmo_mgcp_verb_names[] = {
124 { MGCP_VERB_CRCX, "CRCX" },
125 { MGCP_VERB_MDCX, "MDCX" },
126 { MGCP_VERB_DLCX, "DLCX" },
127 { MGCP_VERB_AUEP, "AUEP" },
128 { MGCP_VERB_RSIP, "RSIP" },
129 {}
130};
131
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200132static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
133 int *waiting_for_response);
134
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100135static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_check_ci(struct osmo_mgcpc_ep_ci *ci)
136{
137 if (!ci)
138 return NULL;
139 if (!ci->ep)
140 return NULL;
141 if (ci < ci->ep->ci || ci >= &ci->ep->ci[USABLE_CI])
142 return NULL;
143 return ci;
144}
145
146static struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_for_event(struct osmo_mgcpc_ep *ep, uint32_t event)
147{
148 int idx;
149 if (event < FIRST_CI_EVENT)
150 return NULL;
151 idx = EV_TO_CI_IDX(event);
152 if (idx >= sizeof(ep->ci))
153 return NULL;
154 return osmo_mgcpc_ep_check_ci(&ep->ci[idx]);
155}
156
157const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep)
158{
159 if (!ep)
160 return "NULL";
161 if (ep->endpoint[0])
162 return ep->endpoint;
163 return osmo_fsm_inst_name(ep->fi);
164}
165
166const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
167{
168 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
169 * this buffer is too small. */
170 static char buf[1024];
171
172 if (!info)
173 return "NULL";
174
175 if (info->endpoint[0]
176 && info->addr[0])
177 snprintf(buf, sizeof(buf), "%s:%s:%u",
178 info->endpoint, info->addr, info->port);
179 else if (info->endpoint[0])
180 snprintf(buf, sizeof(buf), "%s", info->endpoint);
181 else if (info->addr[0])
182 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
183 else
184 return "empty";
185 return buf;
186}
187
188const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci)
189{
190 const struct mgcp_conn_peer *rtp_info;
191
192 if (!ci)
193 return "NULL";
194
195 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
196
197 if (rtp_info)
198 return mgcp_conn_peer_name(rtp_info);
199 return osmo_mgcpc_ep_name(ci->ep);
200}
201
202const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci)
203{
204 if (!ci || !ci->mgcp_ci_str[0])
205 return NULL;
206 return ci->mgcp_ci_str;
207}
208
209static struct value_string osmo_mgcpc_ep_fsm_event_names[33] = {};
210
211static char osmo_mgcpc_ep_fsm_event_name_bufs[32][32] = {};
212
213static void fill_event_names()
214{
215 int i;
216 for (i = 0; i < (ARRAY_SIZE(osmo_mgcpc_ep_fsm_event_names) - 1); i++) {
217 if (i < _OSMO_MGCPC_EP_EV_LAST)
218 continue;
219 if (i < FIRST_CI_EVENT || EV_TO_CI_IDX(i) > USABLE_CI) {
220 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, "Unused"};
221 continue;
222 }
223 snprintf(osmo_mgcpc_ep_fsm_event_name_bufs[i], sizeof(osmo_mgcpc_ep_fsm_event_name_bufs[i]),
224 "MGW Response for CI #%d", EV_TO_CI_IDX(i));
225 osmo_mgcpc_ep_fsm_event_names[i] = (struct value_string){i, osmo_mgcpc_ep_fsm_event_name_bufs[i]};
226 }
227}
228
229/* T_defs is used to obtain an (Osmocom specific) T2427001: timeout for an MGCP response (note, 2427 corresponds to the
230 * default MGCP port in osmo-mgw). */
231static __attribute__((constructor)) void osmo_mgcpc_ep_fsm_init()
232{
233 OSMO_ASSERT(osmo_fsm_register(&osmo_mgcpc_ep_fsm) == 0);
234 fill_event_names();
235}
236
237struct osmo_mgcpc_ep *osmo_mgcpc_ep_fi_mgwep(struct osmo_fsm_inst *fi)
238{
239 OSMO_ASSERT(fi);
240 OSMO_ASSERT(fi->fsm == &osmo_mgcpc_ep_fsm);
241 OSMO_ASSERT(fi->priv);
242 return fi->priv;
243}
244
245/*! Allocate an osmo_mgcpc_ep FSM.
246 * MGCP messages to set up the endpoint will be sent on the given mgcp_client, as soon as the first
247 * osmo_mgcpc_ep_ci_request() is invoked.
248 *
249 * A typical sequence of events would be:
250 *
251 * ep = osmo_mgcpc_ep_alloc(..., mgcp_client_rtpbridge_wildcard(client));
252 * ci_to_ran = osmo_mgcpc_ep_ci_add(ep);
253 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_CRCX, verb_info,
254 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
255 * ci_to_cn = osmo_mgcpc_ep_ci_add(ep);
256 * osmo_mgcpc_ep_ci_request(ci_to_cn, MGCP_VERB_CRCX, verb_info,
257 * my_call_fsm, MY_EVENT_MGCP_OK, MY_EVENT_MGCP_FAIL);
258 * ...
259 * osmo_mgcpc_ep_ci_request(ci_to_ran, MGCP_VERB_MDCX, ...);
260 * ...
261 * osmo_mgcpc_ep_clear(ep);
262 * ep = NULL;
263 *
264 * \param parent Parent FSM.
265 * \param parent_term_event Event to dispatch to the parent on termination of this FSM instance.
266 * \param mgcp_client Connection to the MGW.
267 * \param T_defs Timeout definitions to be used for FSM states, see osmo_mgcpc_ep_fsm_timeouts.
268 * \param fsm_id FSM instance ID.
269 * \param endpoint_str_fmt The endpoint string format to send to the MGW upon the first CRCX.
270 * See mgcp_client_rtpbridge_wildcard() for "rtpbridge" endpoints.
271 */
272struct osmo_mgcpc_ep *osmo_mgcpc_ep_alloc(struct osmo_fsm_inst *parent, uint32_t parent_term_event,
273 struct mgcp_client *mgcp_client,
274 const struct osmo_tdef *T_defs,
275 const char *fsm_id,
276 const char *endpoint_str_fmt, ...)
277{
278 va_list ap;
279 struct osmo_fsm_inst *fi;
280 struct osmo_mgcpc_ep *ep;
281 int rc;
282
283 if (!mgcp_client)
284 return NULL;
285
286 fi = osmo_fsm_inst_alloc_child(&osmo_mgcpc_ep_fsm, parent, parent_term_event);
287 OSMO_ASSERT(fi);
288
289 osmo_fsm_inst_update_id(fi, fsm_id);
290
291 ep = talloc_zero(fi, struct osmo_mgcpc_ep);
292 OSMO_ASSERT(ep);
293
294 *ep = (struct osmo_mgcpc_ep){
295 .mgcp_client = mgcp_client,
296 .fi = fi,
297 .T_defs = T_defs,
298 };
299 fi->priv = ep;
300
301 va_start(ap, endpoint_str_fmt);
302 rc = vsnprintf(ep->endpoint, sizeof(ep->endpoint), endpoint_str_fmt ? : "", ap);
303 va_end(ap);
304
305 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
306 LOG_MGCPC_EP(ep, LOGL_ERROR, "Endpoint name too long or too short: %s\n",
307 ep->endpoint);
308 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_ERROR, 0);
309 return NULL;
310 }
311
312 return ep;
313}
314
315/*! Add a connection to an endpoint.
316 * Allocate a connection identifier slot in the osmo_mgcpc_ep instance, do not yet dispatch a CRCX.
317 * The CRCX is dispatched only upon the first osmo_mgcpc_ep_ci_request().
318 * \param ep Parent endpoint instance.
319 * \param label_fmt Label for logging.
320 */
321struct osmo_mgcpc_ep_ci *osmo_mgcpc_ep_ci_add(struct osmo_mgcpc_ep *ep,
322 const char *label_fmt, ...)
323{
324 va_list ap;
325 int i;
326 struct osmo_mgcpc_ep_ci *ci;
327
328 for (i = 0; i < USABLE_CI; i++) {
329 ci = &ep->ci[i];
330
331 if (ci->occupied || ci->mgcp_client_fi)
332 continue;
333
334 *ci = (struct osmo_mgcpc_ep_ci){
335 .ep = ep,
336 .occupied = true,
337 };
338 if (label_fmt) {
339 va_start(ap, label_fmt);
340 vsnprintf(ci->label, sizeof(ci->label), label_fmt, ap);
341 va_end(ap);
342 }
343 return ci;
344 }
345
346 LOG_MGCPC_EP(ep, LOGL_ERROR,
347 "Cannot allocate another endpoint, all "
348 OSMO_STRINGIFY_VAL(USABLE_CI) " are in use\n");
349
350 return NULL;
351}
352
353static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi);
354
355static void on_failure(struct osmo_mgcpc_ep_ci *ci)
356{
357 struct osmo_fsm_inst *notify = ci->notify;
358 uint32_t notify_failure = ci->notify_failure;
359 void *notify_data = ci->notify_data;
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200360 struct osmo_mgcpc_ep *ep = ci->ep;
361 int i;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100362
363 if (!ci->occupied)
364 return;
365
366 *ci = (struct osmo_mgcpc_ep_ci){
367 .ep = ci->ep,
368 };
369
Neels Hofmeyrcc0b97e2019-10-01 19:44:10 +0200370 /* An MGCP failure typically means the endpoint becomes unusable, cancel all pending request (except DLCX).
371 * Particularly, if two CRCX were scheduled and the first fails, we must no longer dispatch the second CRCX. */
372 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
373 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
374 if (other_ci == ci)
375 continue;
376 if (!other_ci->occupied)
377 continue;
378 if (!other_ci->pending)
379 continue;
380 if (other_ci->sent)
381 continue;
382 if (other_ci->verb == MGCP_VERB_DLCX)
383 continue;
384 /* Just clear the pending request, don't fire more events than below. */
385 other_ci->pending = false;
386 }
387
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100388 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
389 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
390 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi))
391 return;
392
393 if (notify)
394 osmo_fsm_inst_dispatch(notify, notify_failure, notify_data);
395}
396
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200397static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
398{
399 struct osmo_mgcpc_ep *ep = ci->ep;
400 int rc;
401 int i;
402
403 if (!strcmp(ep->endpoint, new_endpoint_name)) {
404 /* Same endpoint name, nothing to do. */
405 return 0;
406 }
407
408 /* The endpoint name should only change on the very first CRCX response. */
409 if (ep->first_crcx_complete) {
410 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
411 " This is endpoint %s, instead received %s\n",
412 ep->endpoint, new_endpoint_name);
413 on_failure(ci);
414 return -EINVAL;
415 }
416
417 /* This is the first CRCX response, update endpoint name. */
418 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
419 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
420 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
421 osmo_mgcpc_ep_ci_dlcx(ci);
422 on_failure(ci);
423 return -ENOSPC;
424 }
425
426 /* Make sure already pending requests use this updated endpoint name. */
427 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
428 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
429 if (!other_ci->occupied)
430 continue;
431 if (!other_ci->pending)
432 continue;
433 if (other_ci->sent)
434 continue;
435 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
436 }
437 return 0;
438}
439
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100440static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
441{
442 struct mgcp_conn_peer *rtp_info;
443
444 if (!ci->occupied)
445 return;
446
447 ci->pending = false;
448
449 switch (ci->verb) {
450 case MGCP_VERB_CRCX:
451 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
452 * name here. Also, we receive the MGW's RTP port information. */
453 rtp_info = data;
454 OSMO_ASSERT(rtp_info);
455 ci->got_port_info = true;
456 ci->rtp_info = *rtp_info;
457 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
458 sizeof(ci->mgcp_ci_str));
459 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200460 /* On errors, this instance might already be deallocated. Make sure to not access anything after
461 * error. */
462 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100463 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100464 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200465 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100466 break;
467
468 default:
469 break;
470 }
471
472 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
473 osmo_mgcp_verb_name(ci->verb),
474 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
475 ci->notify ? "" : " (not sending a notification)");
476
477 if (ci->notify)
478 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
479
480 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
481}
482
483/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
484const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
485{
486 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
487 if (!ci)
488 return NULL;
489 if (!ci->got_port_info)
490 return NULL;
491 return &ci->rtp_info;
492}
493
494/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
495bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
496{
497 const struct mgcp_conn_peer *rtp_info;
498 struct sockaddr_in *sin;
499
500 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
501 if (!rtp_info)
502 return false;
503
504 sin = (struct sockaddr_in *)dest;
505
506 sin->sin_family = AF_INET;
507 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
508 sin->sin_port = osmo_ntohs(rtp_info->port);
509 return true;
510}
511
Pau Espin Pedrol30907dc2019-05-06 11:54:17 +0200512bool osmo_mgcpc_ep_ci_get_crcx_info_to_osmux_cid(const struct osmo_mgcpc_ep_ci *ci, uint8_t* cid)
513{
514 const struct mgcp_conn_peer *rtp_info;
515
516 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
517 if (!rtp_info)
518 return false;
519
520 if (!rtp_info->x_osmo_osmux_use)
521 return false;
522
523 *cid = rtp_info->x_osmo_osmux_cid;
524 return true;
525}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100526
527static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
528 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
529};
530
531/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
532 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
533 * Assumes local variable fi exists. */
534#define osmo_mgcpc_ep_fsm_state_chg(state) \
535 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
536 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
537
538/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
539 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
540 * \param verb MGCP operation to dispatch.
541 * \param verb_info Parameters for the MGCP operation.
542 * \param notify Peer FSM instance to notify of completed/failed operation.
543 * \param event_success Which event to dispatch to 'notify' upon OK response.
544 * \param event_failure Which event to dispatch to 'notify' upon failure response.
545 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
546 */
547void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
548 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
549 struct osmo_fsm_inst *notify,
550 uint32_t event_success, uint32_t event_failure,
551 void *notify_data)
552{
553 struct osmo_mgcpc_ep *ep;
554 struct osmo_fsm_inst *fi;
555 struct osmo_mgcpc_ep_ci cleared_ci;
556 ci = osmo_mgcpc_ep_check_ci(ci);
557
558 if (!ci) {
559 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
560 goto dispatch_error;
561 }
562 if (!verb_info && verb != MGCP_VERB_DLCX) {
563 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
564 osmo_mgcp_verb_name(verb));
565 goto dispatch_error;
566 }
567 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
568 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
569 osmo_mgcp_verb_name(verb));
570 goto dispatch_error;
571 }
572
573 ep = ci->ep;
574 fi = ep->fi;
575
576 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
577 * the char[] directly, dance through cleared_ci and copy back. */
578 cleared_ci = (struct osmo_mgcpc_ep_ci){
579 .ep = ep,
580 .mgcp_client_fi = ci->mgcp_client_fi,
581 .got_port_info = ci->got_port_info,
582 .rtp_info = ci->rtp_info,
583
584 .occupied = true,
585 /* .pending = true follows below */
586 .verb = verb,
587 .notify = notify,
588 .notify_success = event_success,
589 .notify_failure = event_failure,
590 .notify_data = notify_data,
591 };
592 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
593 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
594 *ci = cleared_ci;
595
596 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify));
597
598 if (verb_info)
599 ci->verb_info = *verb_info;
600
601 if (ep->endpoint[0]) {
602 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
603 LOG_CI(ci, LOGL_ERROR,
604 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
605 " Using the proper endpoint instead.\n",
606 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
607 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
608 }
609
610 switch (ci->verb) {
611 case MGCP_VERB_CRCX:
612 if (ci->mgcp_client_fi) {
613 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
614 on_failure(ci);
615 return;
616 }
617 break;
618
619 case MGCP_VERB_MDCX:
620 if (!ci->mgcp_client_fi) {
621 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
622 osmo_mgcp_verb_name(ci->verb));
623 on_failure(ci);
624 return;
625 }
626 break;
627
628 case MGCP_VERB_DLCX:
629 if (!ci->mgcp_client_fi) {
630 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
631 return;
632 }
633 break;
634
635 default:
636 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
637 on_failure(ci);
638 return;
639 }
640
641 ci->pending = true;
642
643 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
644
645 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
646 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
647
648 return;
649dispatch_error:
650 if (notify)
651 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
652}
653
654static int send_verb(struct osmo_mgcpc_ep_ci *ci)
655{
656 int rc;
657 struct osmo_mgcpc_ep *ep = ci->ep;
658
659 if (!ci->occupied || !ci->pending || ci->sent)
660 return 0;
661
662 switch (ci->verb) {
663
664 case MGCP_VERB_CRCX:
665 OSMO_ASSERT(!ci->mgcp_client_fi);
666 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
667 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
668 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
669 &ci->verb_info);
670 ci->sent = true;
671 if (!ci->mgcp_client_fi){
672 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
673 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200674 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100675 }
676 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
677 break;
678
679 case MGCP_VERB_MDCX:
680 OSMO_ASSERT(ci->mgcp_client_fi);
681 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
682 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
683 ci->sent = true;
684 if (rc) {
685 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
686 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200687 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100688 }
689 break;
690
691 case MGCP_VERB_DLCX:
692 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
693 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
694 /* The way this is designed, we actually need to forget all about the ci right away. */
695 mgcp_conn_delete(ci->mgcp_client_fi);
696 if (ci->notify)
697 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
698 *ci = (struct osmo_mgcpc_ep_ci){
699 .ep = ep,
700 };
701 break;
702
703 default:
704 OSMO_ASSERT(false);
705 }
706
707 return 1;
708}
709
710/*! DLCX all connections, terminate the endpoint FSM and free. */
711void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
712{
713 if (!ep)
714 return;
715 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
716}
717
718static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
719 int *waiting_for_response)
720{
721 int i;
722
723 if (occupied)
724 *occupied = 0;
725
726 if (pending_not_sent)
727 *pending_not_sent = 0;
728
729 if (waiting_for_response)
730 *waiting_for_response = 0;
731
732 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
733 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
734 if (ci->occupied) {
735 if (occupied)
736 (*occupied)++;
737 } else
738 continue;
739
740 if (ci->pending)
741 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
742 ci->sent ? "waiting for response" : "waiting to be sent");
743 else
744 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
745
746 if (ci->pending && ci->sent)
747 if (waiting_for_response)
748 (*waiting_for_response)++;
749 if (ci->pending && !ci->sent)
750 if (pending_not_sent)
751 (*pending_not_sent)++;
752 }
753}
754
755static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
756{
757 int waiting_for_response;
758 int occupied;
759 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
760
761 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
762 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
763
764 if (!occupied) {
765 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
766 * terminating. */
767 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
768 return false;
769 }
770
771 if (!waiting_for_response) {
772 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
773 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
774 }
775
776 return true;
777}
778
779static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
780{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200781 static int re_enter = 0;
782 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100783 int count = 0;
784 int i;
785 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
786
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200787 re_enter++;
788 OSMO_ASSERT(re_enter < 10);
789
790 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
791 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
792 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
793 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
794 * sent (which needs to be a CRCX). */
795
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100796 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200797 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
798
799 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
800 if (!ep->first_crcx_complete) {
801 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
802 continue;
803 }
804
805 rc = send_verb(&ep->ci[i]);
806 /* Need to be careful not to access the instance after failure. Event chains may already have
807 * deallocated this memory. */
808 if (rc < 0)
809 return;
810 if (!rc)
811 continue;
812 count++;
813 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
814 if (!ep->first_crcx_complete)
815 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100816 }
817
818 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200819 if (ep->first_crcx_complete)
820 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
821 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100822}
823
824static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
825{
826 struct osmo_mgcpc_ep_ci *ci;
827 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
828 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
829 if (ci) {
830 if (event == CI_EV_SUCCESS(ci))
831 on_success(ci, data);
832 else
833 on_failure(ci);
834 }
835}
836
837static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
838{
839 int pending_not_sent;
840 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
841
842 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
843 if (pending_not_sent)
844 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
845}
846
847#define S(x) (1 << (x))
848
849static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
850 [OSMO_MGCPC_EP_ST_UNUSED] = {
851 .name = "UNUSED",
852 .in_event_mask = 0,
853 .out_state_mask = 0
854 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
855 ,
856 },
857 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
858 .name = "WAIT_MGW_RESPONSE",
859 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
860 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
861 .in_event_mask = 0xffffffff,
862 .out_state_mask = 0
863 | S(OSMO_MGCPC_EP_ST_IN_USE)
864 ,
865 },
866 [OSMO_MGCPC_EP_ST_IN_USE] = {
867 .name = "IN_USE",
868 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
869 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
870 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
871 .out_state_mask = 0
872 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
873 ,
874 },
875};
876
877static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
878{
879 int i;
880 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
881
882 switch (fi->T) {
883 default:
884 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
885 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
886 if (!ci->occupied)
887 continue;
888 if (!(ci->pending && ci->sent))
889 continue;
890 on_failure(ci);
891 }
892 return 0;
893 }
894
895 return 0;
896}
897
898static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200899 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100900 .states = osmo_mgcpc_ep_fsm_states,
901 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
902 .log_subsys = DLMGCP,
903 .event_names = osmo_mgcpc_ep_fsm_event_names,
904 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
905 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
906};