blob: e6c323761c7bea612e793fc3793036d0f011096d [file] [log] [blame]
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001/* (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 * All Rights Reserved
3 *
4 * Author: Philipp Maier
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/mgcp_client/mgcp_client.h>
22#include <osmocom/mgcp_client/mgcp_client_internal.h>
23#include <osmocom/mgcp_client/mgcp_client_pool_internal.h>
24#include <osmocom/mgcp_client/mgcp_client_pool.h>
25#include <stddef.h>
26
Philipp Maierdf9192e2021-09-03 17:50:51 +020027#define LOGPPMGW(pool_member, level, fmt, args...) \
28LOGP(DLMGCP, level, "MGW-pool(%s) " fmt, mgcp_client_pool_member_name(pool_member), ## args)
29
30/* Get a human readable name for a given pool member. */
31const char *mgcp_client_pool_member_name(const struct mgcp_client_pool_member *pool_member)
32{
33 const struct mgcp_client *mpcp_client;
34 struct mgcp_client mpcp_client_dummy;
35 static char name[512];
36 const char *description;
37
38 if (!pool_member)
39 return "(null)";
40
41 /* It is not guranteed that a pool_member has an MGCP client. The client may not yet be initialized or the
42 * initalization may have been failed. In this case we will generate a dummy MGCP client to work with. */
43 if (!pool_member->client) {
44 memcpy(&mpcp_client_dummy.actual, &pool_member->conf, sizeof(mpcp_client_dummy.actual));
45 mpcp_client = &mpcp_client_dummy;
46 } else {
47 mpcp_client = pool_member->client;
48 }
49
50 description = mgcp_client_name(mpcp_client);
51 snprintf(name, sizeof(name), "%d:%s", pool_member->nr, description);
52
53 return name;
54}
55
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020056/*! Allocate MGCP client pool. This is called once on startup and before the pool is used with
57 * mgcp_client_pool_vty_init(). Since the pool is linked with the VTY it must exist througout the entire runtime.
58 * \param[in] talloc_ctx talloc context. */
59struct mgcp_client_pool *mgcp_client_pool_alloc(void *talloc_ctx)
60{
61 struct mgcp_client_pool *pool;
62
63 pool = talloc_zero(talloc_ctx, struct mgcp_client_pool);
64 if (!pool)
65 return NULL;
66
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +020067 INIT_LLIST_HEAD(&pool->member_list);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020068
69 return pool;
70}
71
72/*! Initialize and connect an mcgp client pool.
73 * \param[in,out] mgcp MGCP client pool descriptor.
74 * \returns number of successfully initialized pool members. */
75unsigned int mgcp_client_pool_connect(struct mgcp_client_pool *pool)
76{
77 struct mgcp_client_pool_member *pool_member;
78 unsigned int pool_members_initialized = 0;
79
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +020080 llist_for_each_entry(pool_member, &pool->member_list, list) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020081
82 /* Initialize client */
83 pool_member->client = mgcp_client_init(pool_member, &pool_member->conf);
84 if (!pool_member->client) {
Philipp Maierdf9192e2021-09-03 17:50:51 +020085 LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client initialization failed\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020086 continue;
87 }
88
89 /* Set backpointer so that we can detect later that this MGCP client is managed
90 * by this pool. */
91 pool_member->client->pool = pool;
92
93 /* Connect client */
94 if (mgcp_client_connect2(pool_member->client, 0)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +020095 LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client connect failed at (%s:%u)\n",
96 pool_member->conf.remote_addr, pool_member->conf.remote_port);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020097 talloc_free(pool_member->client);
98 pool_member->client = NULL;
99 continue;
100 }
101
102 pool_members_initialized++;
103 }
104
105 return pool_members_initialized;
106}
107
108/*! register a single mgcp_client instance to the pool.
109 * \param[out] pool MGCP client pool descriptor.
110 * \param[in] mgcp MGCP client descriptor. */
111void mgcp_client_pool_register_single(struct mgcp_client_pool *pool, struct mgcp_client *mgcp_client)
112{
113 /*! Some applications still support the non-pooled MGW VTY configuration variant provided by
114 * mgcp_client_vty_init(). If this is the case the mgcp_client instance created by mgcp_client_init()
115 * can be registered here so that it will appear as if it were part of the pool. When the user actively
116 * configures MGW pool members, the MGCP client registered here will be ignored. (The registration of
117 * multiple singe mgcp_client instances is not possible.) */
118 pool->mgcp_client_single = mgcp_client;
119}
120
121/* Not every pool member may have a functional MGCP client, we will run through the pool once until we meet a
122 * pool member that is suitable (has a client, is not blocked, has a low load). */
123static struct mgcp_client_pool_member *mgcp_client_pool_pick(struct mgcp_client_pool *pool)
124{
125 struct mgcp_client_pool_member *pool_member;
126 struct mgcp_client_pool_member *pool_member_picked = NULL;
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200127 unsigned int n_pool_members = 0;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200128
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200129 llist_for_each_entry(pool_member, &pool->member_list, list) {
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200130 n_pool_members++;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200131 if (pool_member->blocked == false && pool_member->client) {
132 if (!pool_member_picked)
133 pool_member_picked = pool_member;
134 else if (pool_member_picked->refcount > pool_member->refcount)
135 pool_member_picked = pool_member;
136 } else {
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200137 LOGPPMGW(pool_member, LOGL_DEBUG, "%s -- MGW %u is unusable (blocked=%u, cli=%u)\n",
138 __func__, pool_member->nr, pool_member->blocked, !!pool_member->client);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200139 }
140 }
141
142 if (pool_member_picked) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200143 LOGPPMGW(pool_member_picked, LOGL_DEBUG, "MGW pool has %u members -- using MGW %u (active calls: %u)\n",
144 n_pool_members, pool_member_picked->nr, pool_member_picked->refcount);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200145 return pool_member_picked;
146 }
147
148 LOGP(DLMGCP, LOGL_ERROR,
149 "MGW pool has %u members, but no functional MGW pool member found -- check configuration!\n",
150 n_pool_members);
151
152 return NULL;
153}
154
155/*! get an MGCP client from the pool (increment reference counter).
156 * \param[in,out] pool MGCP client pool descriptor.
157 * \returns MGCP client descriptor, NULL if no member was found (empty pool). */
158struct mgcp_client *mgcp_client_pool_get(struct mgcp_client_pool *pool)
159{
160 struct mgcp_client_pool_member *pool_member;
161
162 /*! When an MGCP client is taken from the pool it is still available for other calls. In fact only a reference
163 * counter is incremented to keep track on how many references to a specific MGCP client are currently used
164 * by the application code. */
165
166 /* When the pool is empty, return a single MGCP client if it is registered. */
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200167 if (llist_empty(&pool->member_list) && pool->mgcp_client_single) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200168 LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW %s\n",
169 mgcp_client_name(pool->mgcp_client_single));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200170 return pool->mgcp_client_single;
171 }
172
173 /* Abort when the pool is empty */
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200174 if (llist_empty(&pool->member_list)) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200175 LOGP(DLMGCP, LOGL_ERROR, "MGW pool is empty -- no MGW available!\n");
176 return NULL;
177 }
178
179 /* Pick a suitable pool member */
180 pool_member = mgcp_client_pool_pick(pool);
181 if (pool_member) {
182 pool_member->refcount++;
183 return pool_member->client;
184 }
185
186 return NULL;
187}
188
189/*! put an MGCP client back into the pool (decrement reference counter).
190 * \param[in,out] pool MGCP client pool descriptor.
191 * \param[in] mgcp MGCP client descriptor.
192 *
193 * This function is able to detect automatically to which pool the mgcp_client belongs. If the mgcp_client does
194 * not belong to a pool at all, the function call will have no effect. */
195void mgcp_client_pool_put(struct mgcp_client *mgcp_client)
196{
197 struct mgcp_client_pool_member *pool_member;
198 struct mgcp_client_pool *pool;
199
200 if (!mgcp_client)
201 return;
202
203 if (mgcp_client->pool)
204 pool = mgcp_client->pool;
205 else
206 return;
207
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200208 llist_for_each_entry(pool_member, &pool->member_list, list) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200209 if (pool_member->client == mgcp_client) {
210 if (pool_member->refcount == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200211 LOGPPMGW(pool_member, LOGL_ERROR, "MGW pool member has invalid refcount\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200212 return;
213 }
214 pool_member->refcount--;
215 }
216 }
217}