blob: a9bab8737a75e9d460e4cd9fa6c3b2309a57f4c8 [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;
360
361 if (!ci->occupied)
362 return;
363
364 *ci = (struct osmo_mgcpc_ep_ci){
365 .ep = ci->ep,
366 };
367
368 /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.
369 * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */
370 if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi))
371 return;
372
373 if (notify)
374 osmo_fsm_inst_dispatch(notify, notify_failure, notify_data);
375}
376
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200377static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)
378{
379 struct osmo_mgcpc_ep *ep = ci->ep;
380 int rc;
381 int i;
382
383 if (!strcmp(ep->endpoint, new_endpoint_name)) {
384 /* Same endpoint name, nothing to do. */
385 return 0;
386 }
387
388 /* The endpoint name should only change on the very first CRCX response. */
389 if (ep->first_crcx_complete) {
390 LOG_CI(ci, LOGL_ERROR, "Reponse returned mismatching endpoint name."
391 " This is endpoint %s, instead received %s\n",
392 ep->endpoint, new_endpoint_name);
393 on_failure(ci);
394 return -EINVAL;
395 }
396
397 /* This is the first CRCX response, update endpoint name. */
398 rc = OSMO_STRLCPY_ARRAY(ep->endpoint, new_endpoint_name);
399 if (rc <= 0 || rc >= sizeof(ep->endpoint)) {
400 LOG_CI(ci, LOGL_ERROR, "Unable to copy endpoint name %s\n", osmo_quote_str(new_endpoint_name, -1));
401 osmo_mgcpc_ep_ci_dlcx(ci);
402 on_failure(ci);
403 return -ENOSPC;
404 }
405
406 /* Make sure already pending requests use this updated endpoint name. */
407 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
408 struct osmo_mgcpc_ep_ci *other_ci = &ep->ci[i];
409 if (!other_ci->occupied)
410 continue;
411 if (!other_ci->pending)
412 continue;
413 if (other_ci->sent)
414 continue;
415 OSMO_STRLCPY_ARRAY(other_ci->verb_info.endpoint, ep->endpoint);
416 }
417 return 0;
418}
419
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100420static void on_success(struct osmo_mgcpc_ep_ci *ci, void *data)
421{
422 struct mgcp_conn_peer *rtp_info;
423
424 if (!ci->occupied)
425 return;
426
427 ci->pending = false;
428
429 switch (ci->verb) {
430 case MGCP_VERB_CRCX:
431 /* If we sent a wildcarded endpoint name on CRCX, we need to store the resulting endpoint
432 * name here. Also, we receive the MGW's RTP port information. */
433 rtp_info = data;
434 OSMO_ASSERT(rtp_info);
435 ci->got_port_info = true;
436 ci->rtp_info = *rtp_info;
437 osmo_strlcpy(ci->mgcp_ci_str, mgcp_conn_get_ci(ci->mgcp_client_fi),
438 sizeof(ci->mgcp_ci_str));
439 if (rtp_info->endpoint[0]) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200440 /* On errors, this instance might already be deallocated. Make sure to not access anything after
441 * error. */
442 if (update_endpoint_name(ci, rtp_info->endpoint))
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100443 return;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100444 }
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200445 ci->ep->first_crcx_complete = true;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100446 break;
447
448 default:
449 break;
450 }
451
452 LOG_CI(ci, LOGL_DEBUG, "received successful response to %s: RTP=%s%s\n",
453 osmo_mgcp_verb_name(ci->verb),
454 mgcp_conn_peer_name(ci->got_port_info? &ci->rtp_info : NULL),
455 ci->notify ? "" : " (not sending a notification)");
456
457 if (ci->notify)
458 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
459
460 osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi);
461}
462
463/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
464const struct mgcp_conn_peer *osmo_mgcpc_ep_ci_get_rtp_info(const struct osmo_mgcpc_ep_ci *ci)
465{
466 ci = osmo_mgcpc_ep_check_ci((struct osmo_mgcpc_ep_ci*)ci);
467 if (!ci)
468 return NULL;
469 if (!ci->got_port_info)
470 return NULL;
471 return &ci->rtp_info;
472}
473
474/*! Return the MGW's RTP port information for this connection, as returned by the last CRCX/MDCX OK message. */
475bool osmo_mgcpc_ep_ci_get_crcx_info_to_sockaddr(const struct osmo_mgcpc_ep_ci *ci, struct sockaddr_storage *dest)
476{
477 const struct mgcp_conn_peer *rtp_info;
478 struct sockaddr_in *sin;
479
480 rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(ci);
481 if (!rtp_info)
482 return false;
483
484 sin = (struct sockaddr_in *)dest;
485
486 sin->sin_family = AF_INET;
487 sin->sin_addr.s_addr = inet_addr(rtp_info->addr);
488 sin->sin_port = osmo_ntohs(rtp_info->port);
489 return true;
490}
491
492
493static const struct osmo_tdef_state_timeout osmo_mgcpc_ep_fsm_timeouts[32] = {
494 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = { .T=2427001 },
495};
496
497/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
498 * The actual timeout value is in turn obtained from osmo_mgcpc_ep.T_defs.
499 * Assumes local variable fi exists. */
500#define osmo_mgcpc_ep_fsm_state_chg(state) \
501 osmo_tdef_fsm_inst_state_chg(fi, state, osmo_mgcpc_ep_fsm_timeouts, \
502 ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)
503
504/*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.
505 * \param ci Connection identifier as obtained from osmo_mgcpc_ep_ci_add().
506 * \param verb MGCP operation to dispatch.
507 * \param verb_info Parameters for the MGCP operation.
508 * \param notify Peer FSM instance to notify of completed/failed operation.
509 * \param event_success Which event to dispatch to 'notify' upon OK response.
510 * \param event_failure Which event to dispatch to 'notify' upon failure response.
511 * \param notify_data Data pointer to pass to the event dispatch for both success and failure.
512 */
513void osmo_mgcpc_ep_ci_request(struct osmo_mgcpc_ep_ci *ci,
514 enum mgcp_verb verb, const struct mgcp_conn_peer *verb_info,
515 struct osmo_fsm_inst *notify,
516 uint32_t event_success, uint32_t event_failure,
517 void *notify_data)
518{
519 struct osmo_mgcpc_ep *ep;
520 struct osmo_fsm_inst *fi;
521 struct osmo_mgcpc_ep_ci cleared_ci;
522 ci = osmo_mgcpc_ep_check_ci(ci);
523
524 if (!ci) {
525 LOGP(DLGLOBAL, LOGL_ERROR, "Invalid MGW endpoint request: no ci\n");
526 goto dispatch_error;
527 }
528 if (!verb_info && verb != MGCP_VERB_DLCX) {
529 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: missing verb details for %s\n",
530 osmo_mgcp_verb_name(verb));
531 goto dispatch_error;
532 }
533 if ((verb < 0) || (verb > MGCP_VERB_RSIP)) {
534 LOG_CI(ci, LOGL_ERROR, "Invalid MGW endpoint request: unknown verb: %s\n",
535 osmo_mgcp_verb_name(verb));
536 goto dispatch_error;
537 }
538
539 ep = ci->ep;
540 fi = ep->fi;
541
542 /* Clear volatile state by explicitly keeping those that should remain. Because we can't assign
543 * the char[] directly, dance through cleared_ci and copy back. */
544 cleared_ci = (struct osmo_mgcpc_ep_ci){
545 .ep = ep,
546 .mgcp_client_fi = ci->mgcp_client_fi,
547 .got_port_info = ci->got_port_info,
548 .rtp_info = ci->rtp_info,
549
550 .occupied = true,
551 /* .pending = true follows below */
552 .verb = verb,
553 .notify = notify,
554 .notify_success = event_success,
555 .notify_failure = event_failure,
556 .notify_data = notify_data,
557 };
558 osmo_strlcpy(cleared_ci.label, ci->label, sizeof(cleared_ci.label));
559 osmo_strlcpy(cleared_ci.mgcp_ci_str, ci->mgcp_ci_str, sizeof(cleared_ci.mgcp_ci_str));
560 *ci = cleared_ci;
561
562 LOG_CI_VERB(ci, LOGL_DEBUG, "notify=%s\n", osmo_fsm_inst_name(ci->notify));
563
564 if (verb_info)
565 ci->verb_info = *verb_info;
566
567 if (ep->endpoint[0]) {
568 if (ci->verb_info.endpoint[0] && strcmp(ci->verb_info.endpoint, ep->endpoint))
569 LOG_CI(ci, LOGL_ERROR,
570 "Warning: Requested %s on endpoint %s, but this CI is on endpoint %s."
571 " Using the proper endpoint instead.\n",
572 osmo_mgcp_verb_name(verb), ci->verb_info.endpoint, ep->endpoint);
573 osmo_strlcpy(ci->verb_info.endpoint, ep->endpoint, sizeof(ci->verb_info.endpoint));
574 }
575
576 switch (ci->verb) {
577 case MGCP_VERB_CRCX:
578 if (ci->mgcp_client_fi) {
579 LOG_CI(ci, LOGL_ERROR, "CRCX can be called only once per MGW endpoint CI\n");
580 on_failure(ci);
581 return;
582 }
583 break;
584
585 case MGCP_VERB_MDCX:
586 if (!ci->mgcp_client_fi) {
587 LOG_CI_VERB(ci, LOGL_ERROR, "The first verb on an unused MGW endpoint CI must be CRCX, not %s\n",
588 osmo_mgcp_verb_name(ci->verb));
589 on_failure(ci);
590 return;
591 }
592 break;
593
594 case MGCP_VERB_DLCX:
595 if (!ci->mgcp_client_fi) {
596 LOG_CI_VERB(ci, LOGL_DEBUG, "Ignoring DLCX on unused MGW endpoint CI\n");
597 return;
598 }
599 break;
600
601 default:
602 LOG_CI(ci, LOGL_ERROR, "This verb is not supported: %s\n", osmo_mgcp_verb_name(ci->verb));
603 on_failure(ci);
604 return;
605 }
606
607 ci->pending = true;
608
609 LOG_CI_VERB(ci, LOGL_DEBUG, "Scheduling\n");
610
611 if (ep->fi->state != OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
612 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
613
614 return;
615dispatch_error:
616 if (notify)
617 osmo_fsm_inst_dispatch(notify, event_failure, notify_data);
618}
619
620static int send_verb(struct osmo_mgcpc_ep_ci *ci)
621{
622 int rc;
623 struct osmo_mgcpc_ep *ep = ci->ep;
624
625 if (!ci->occupied || !ci->pending || ci->sent)
626 return 0;
627
628 switch (ci->verb) {
629
630 case MGCP_VERB_CRCX:
631 OSMO_ASSERT(!ci->mgcp_client_fi);
632 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
633 ci->mgcp_client_fi = mgcp_conn_create(ep->mgcp_client, ep->fi,
634 CI_EV_FAILURE(ci), CI_EV_SUCCESS(ci),
635 &ci->verb_info);
636 ci->sent = true;
637 if (!ci->mgcp_client_fi){
638 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send\n");
639 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200640 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100641 }
642 osmo_fsm_inst_update_id(ci->mgcp_client_fi, ci->label);
643 break;
644
645 case MGCP_VERB_MDCX:
646 OSMO_ASSERT(ci->mgcp_client_fi);
647 LOG_CI_VERB(ci, LOGL_DEBUG, "Sending\n");
648 rc = mgcp_conn_modify(ci->mgcp_client_fi, CI_EV_SUCCESS(ci), &ci->verb_info);
649 ci->sent = true;
650 if (rc) {
651 LOG_CI_VERB(ci, LOGL_ERROR, "Cannot send (rc=%d %s)\n", rc, strerror(-rc));
652 on_failure(ci);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200653 return -EINVAL;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100654 }
655 break;
656
657 case MGCP_VERB_DLCX:
658 LOG_CI(ci, LOGL_DEBUG, "Sending MGCP: %s %s\n",
659 osmo_mgcp_verb_name(ci->verb), ci->mgcp_ci_str);
660 /* The way this is designed, we actually need to forget all about the ci right away. */
661 mgcp_conn_delete(ci->mgcp_client_fi);
662 if (ci->notify)
663 osmo_fsm_inst_dispatch(ci->notify, ci->notify_success, ci->notify_data);
664 *ci = (struct osmo_mgcpc_ep_ci){
665 .ep = ep,
666 };
667 break;
668
669 default:
670 OSMO_ASSERT(false);
671 }
672
673 return 1;
674}
675
676/*! DLCX all connections, terminate the endpoint FSM and free. */
677void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep)
678{
679 if (!ep)
680 return;
681 osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);
682}
683
684static void osmo_mgcpc_ep_count(struct osmo_mgcpc_ep *ep, int *occupied, int *pending_not_sent,
685 int *waiting_for_response)
686{
687 int i;
688
689 if (occupied)
690 *occupied = 0;
691
692 if (pending_not_sent)
693 *pending_not_sent = 0;
694
695 if (waiting_for_response)
696 *waiting_for_response = 0;
697
698 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
699 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
700 if (ci->occupied) {
701 if (occupied)
702 (*occupied)++;
703 } else
704 continue;
705
706 if (ci->pending)
707 LOG_CI_VERB(ci, LOGL_DEBUG, "%s\n",
708 ci->sent ? "waiting for response" : "waiting to be sent");
709 else
710 LOG_CI_VERB(ci, LOGL_DEBUG, "done (%s)\n", mgcp_conn_peer_name(osmo_mgcpc_ep_ci_get_rtp_info(ci)));
711
712 if (ci->pending && ci->sent)
713 if (waiting_for_response)
714 (*waiting_for_response)++;
715 if (ci->pending && !ci->sent)
716 if (pending_not_sent)
717 (*pending_not_sent)++;
718 }
719}
720
721static bool osmo_mgcpc_ep_fsm_check_state_chg_after_response(struct osmo_fsm_inst *fi)
722{
723 int waiting_for_response;
724 int occupied;
725 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
726
727 osmo_mgcpc_ep_count(ep, &occupied, NULL, &waiting_for_response);
728 LOG_MGCPC_EP(ep, LOGL_DEBUG, "CI in use: %d, waiting for response: %d\n", occupied, waiting_for_response);
729
730 if (!occupied) {
731 /* All CI have been released. The endpoint no longer exists. Notify the parent FSM, by
732 * terminating. */
733 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, 0);
734 return false;
735 }
736
737 if (!waiting_for_response) {
738 if (fi->state != OSMO_MGCPC_EP_ST_IN_USE)
739 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_IN_USE);
740 }
741
742 return true;
743}
744
745static void osmo_mgcpc_ep_fsm_wait_mgw_response_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
746{
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200747 static int re_enter = 0;
748 int rc;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100749 int count = 0;
750 int i;
751 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
752
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200753 re_enter++;
754 OSMO_ASSERT(re_enter < 10);
755
756 /* The first CRCX gives us the endpoint name in the CRCX response. So we must wait for the first CRCX endpoint
757 * response to come in before sending any other MGCP requests -- otherwise we might end up creating new
758 * endpoints instead of acting on the same. This FSM always sends out N requests and waits for all of them to
759 * complete before sending out new requests. Hence we're safe when the very first time at most one request is
760 * sent (which needs to be a CRCX). */
761
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100762 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200763 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
764
765 /* Make sure that only CRCX get dispatched if no CRCX were sent yet. */
766 if (!ep->first_crcx_complete) {
767 if (ci->occupied && ci->verb != MGCP_VERB_CRCX)
768 continue;
769 }
770
771 rc = send_verb(&ep->ci[i]);
772 /* Need to be careful not to access the instance after failure. Event chains may already have
773 * deallocated this memory. */
774 if (rc < 0)
775 return;
776 if (!rc)
777 continue;
778 count++;
779 /* Make sure that we wait for the first CRCX response before dispatching more requests. */
780 if (!ep->first_crcx_complete)
781 break;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100782 }
783
784 LOG_MGCPC_EP(ep, LOGL_DEBUG, "Sent messages: %d\n", count);
Neels Hofmeyrc5479fe2019-04-05 01:36:06 +0200785 if (ep->first_crcx_complete)
786 osmo_mgcpc_ep_fsm_check_state_chg_after_response(fi);
787 re_enter--;
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100788}
789
790static void osmo_mgcpc_ep_fsm_handle_ci_events(struct osmo_fsm_inst *fi, uint32_t event, void *data)
791{
792 struct osmo_mgcpc_ep_ci *ci;
793 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
794 ci = osmo_mgcpc_ep_ci_for_event(ep, event);
795 if (ci) {
796 if (event == CI_EV_SUCCESS(ci))
797 on_success(ci, data);
798 else
799 on_failure(ci);
800 }
801}
802
803static void osmo_mgcpc_ep_fsm_in_use_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
804{
805 int pending_not_sent;
806 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
807
808 osmo_mgcpc_ep_count(ep, NULL, &pending_not_sent, NULL);
809 if (pending_not_sent)
810 osmo_mgcpc_ep_fsm_state_chg(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE);
811}
812
813#define S(x) (1 << (x))
814
815static const struct osmo_fsm_state osmo_mgcpc_ep_fsm_states[] = {
816 [OSMO_MGCPC_EP_ST_UNUSED] = {
817 .name = "UNUSED",
818 .in_event_mask = 0,
819 .out_state_mask = 0
820 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
821 ,
822 },
823 [OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE] = {
824 .name = "WAIT_MGW_RESPONSE",
825 .onenter = osmo_mgcpc_ep_fsm_wait_mgw_response_onenter,
826 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
827 .in_event_mask = 0xffffffff,
828 .out_state_mask = 0
829 | S(OSMO_MGCPC_EP_ST_IN_USE)
830 ,
831 },
832 [OSMO_MGCPC_EP_ST_IN_USE] = {
833 .name = "IN_USE",
834 .onenter = osmo_mgcpc_ep_fsm_in_use_onenter,
835 .action = osmo_mgcpc_ep_fsm_handle_ci_events,
836 .in_event_mask = 0xffffffff, /* mgcp_client_fsm may send parent term anytime */
837 .out_state_mask = 0
838 | S(OSMO_MGCPC_EP_ST_WAIT_MGW_RESPONSE)
839 ,
840 },
841};
842
843static int osmo_mgcpc_ep_fsm_timer_cb(struct osmo_fsm_inst *fi)
844{
845 int i;
846 struct osmo_mgcpc_ep *ep = osmo_mgcpc_ep_fi_mgwep(fi);
847
848 switch (fi->T) {
849 default:
850 for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {
851 struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];
852 if (!ci->occupied)
853 continue;
854 if (!(ci->pending && ci->sent))
855 continue;
856 on_failure(ci);
857 }
858 return 0;
859 }
860
861 return 0;
862}
863
864static struct osmo_fsm osmo_mgcpc_ep_fsm = {
Pau Espin Pedrol182ca3b2019-05-08 14:01:20 +0200865 .name = "mgw-endp",
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100866 .states = osmo_mgcpc_ep_fsm_states,
867 .num_states = ARRAY_SIZE(osmo_mgcpc_ep_fsm_states),
868 .log_subsys = DLMGCP,
869 .event_names = osmo_mgcpc_ep_fsm_event_names,
870 .timer_cb = osmo_mgcpc_ep_fsm_timer_cb,
871 /* The FSM termination will automatically trigger any mgcp_client_fsm instances to DLCX. */
872};