mgcp_client: refactor function init_socket

The function init_socket has an arbitrary retry count when opening the
socket. After each retry the local port is incremented by one. The
intention behind this is to find a useable local port in case the
configured port is used by another process.

The maximum number of retrys is hardcoded. The upcomming MGW pooling
patch requires to set the maximum retry count.

Change-Id: Ifd65511daa92fbe610f52da1c4c3b6a7c761d890
Related: SYS#5091
This commit is contained in:
Philipp Maier 2021-08-04 14:08:37 +02:00
parent 276a414aa3
commit 3d2b76fd95
1 changed files with 23 additions and 17 deletions

View File

@ -796,40 +796,46 @@ struct mgcp_client *mgcp_client_init(void *ctx,
return mgcp; return mgcp;
} }
static int init_socket(struct mgcp_client *mgcp) static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)
{ {
int rc; int rc;
struct osmo_wqueue *wq; struct osmo_wqueue *wq;
int i; unsigned int i;
wq = &mgcp->wq; wq = &mgcp->wq;
for (i = 0; i < 100; i++) { for (i = 0; i < retry_n_ports + 1; i++) {
/* Initalize socket with the currently configured port /* Initialize socket with the currently configured port number */
* number */
rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr, rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port, mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
if (rc > 0) if (rc > 0)
return rc; return rc;
/* If there is a different port than the default port /* If there is a different port than the default port configured then we assume that the user has
* configured then we assume that the user has choosen * chosen that port conciously and we will not try to resolve this by silently choosing a different
* that port conciously and we will not try to resolve * port. */
* this by silently choosing a different port. */
if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0) if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
return -EINVAL; return -EINVAL;
/* Choose a new port number to try next */ if (i == retry_n_ports) {
LOGP(DLMGCP, LOGL_NOTICE, /* Last try failed */
"MGCPGW failed to bind to %s:%u, retrying with port %u\n", LOGP(DLMGCP, LOGL_NOTICE, "MGCPGW failed to bind to %s:%d -- check configuration!\n",
mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port, mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);
mgcp->actual.local_port + 1); if (retry_n_ports == 0)
mgcp->actual.local_port++; return -EINVAL;
} else {
/* Choose a new port number to try next */
LOGP(DLMGCP, LOGL_NOTICE,
"MGCPGW failed to bind to %s:%d, retrying with port %d -- check configuration!\n",
mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
mgcp->actual.local_port + 1);
mgcp->actual.local_port++;
}
} }
LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i); LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %u times -- check configuration!\n", i);
return -EINVAL; return -EINVAL;
} }
@ -889,7 +895,7 @@ int mgcp_client_connect(struct mgcp_client *mgcp)
osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0); osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
rc = init_socket(mgcp); rc = init_socket(mgcp, 99);
if (rc < 0) { if (rc < 0) {
LOGP(DLMGCP, LOGL_FATAL, LOGP(DLMGCP, LOGL_FATAL,
"Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n", "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",