blob: 5b921f10c6a8a13d13332aaef3af581c7e1dfd9e [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
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020030/*! Allocate MGCP client pool. This is called once on startup and before the pool is used with
31 * mgcp_client_pool_vty_init(). Since the pool is linked with the VTY it must exist througout the entire runtime.
32 * \param[in] talloc_ctx talloc context. */
33struct mgcp_client_pool *mgcp_client_pool_alloc(void *talloc_ctx)
34{
35 struct mgcp_client_pool *pool;
36
37 pool = talloc_zero(talloc_ctx, struct mgcp_client_pool);
38 if (!pool)
39 return NULL;
40
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +020041 INIT_LLIST_HEAD(&pool->member_list);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020042
43 return pool;
44}
45
46/*! Initialize and connect an mcgp client pool.
47 * \param[in,out] mgcp MGCP client pool descriptor.
48 * \returns number of successfully initialized pool members. */
49unsigned int mgcp_client_pool_connect(struct mgcp_client_pool *pool)
50{
51 struct mgcp_client_pool_member *pool_member;
52 unsigned int pool_members_initialized = 0;
53
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +020054 llist_for_each_entry(pool_member, &pool->member_list, list) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020055
56 /* Initialize client */
57 pool_member->client = mgcp_client_init(pool_member, &pool_member->conf);
58 if (!pool_member->client) {
Philipp Maierdf9192e2021-09-03 17:50:51 +020059 LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client initialization failed\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020060 continue;
61 }
62
63 /* Set backpointer so that we can detect later that this MGCP client is managed
64 * by this pool. */
65 pool_member->client->pool = pool;
66
67 /* Connect client */
68 if (mgcp_client_connect2(pool_member->client, 0)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +020069 LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client connect failed at (%s:%u)\n",
70 pool_member->conf.remote_addr, pool_member->conf.remote_port);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020071 talloc_free(pool_member->client);
72 pool_member->client = NULL;
73 continue;
74 }
75
76 pool_members_initialized++;
77 }
78
79 return pool_members_initialized;
80}
81
82/*! register a single mgcp_client instance to the pool.
83 * \param[out] pool MGCP client pool descriptor.
84 * \param[in] mgcp MGCP client descriptor. */
85void mgcp_client_pool_register_single(struct mgcp_client_pool *pool, struct mgcp_client *mgcp_client)
86{
87 /*! Some applications still support the non-pooled MGW VTY configuration variant provided by
88 * mgcp_client_vty_init(). If this is the case the mgcp_client instance created by mgcp_client_init()
89 * can be registered here so that it will appear as if it were part of the pool. When the user actively
90 * configures MGW pool members, the MGCP client registered here will be ignored. (The registration of
91 * multiple singe mgcp_client instances is not possible.) */
92 pool->mgcp_client_single = mgcp_client;
93}
94
Pau Espin Pedrolff592152022-10-13 16:46:25 +020095/*! Lookup the selected MGCP client config by its reference number */
96struct mgcp_client_pool_member *mgcp_client_pool_find_member_by_nr(struct mgcp_client_pool *pool, unsigned int nr)
97{
98 struct mgcp_client_pool_member *pool_member;
99
100 llist_for_each_entry(pool_member, &pool->member_list, list) {
101 if (pool_member->nr == nr)
102 return pool_member;
103 }
104
105 return NULL;
106}
107
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200108/* Not every pool member may have a functional MGCP client, we will run through the pool once until we meet a
109 * pool member that is suitable (has a client, is not blocked, has a low load). */
110static struct mgcp_client_pool_member *mgcp_client_pool_pick(struct mgcp_client_pool *pool)
111{
112 struct mgcp_client_pool_member *pool_member;
113 struct mgcp_client_pool_member *pool_member_picked = NULL;
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200114 unsigned int n_pool_members = 0;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200115
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200116 llist_for_each_entry(pool_member, &pool->member_list, list) {
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200117 n_pool_members++;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200118 if (pool_member->blocked == false && pool_member->client) {
119 if (!pool_member_picked)
120 pool_member_picked = pool_member;
121 else if (pool_member_picked->refcount > pool_member->refcount)
122 pool_member_picked = pool_member;
123 } else {
Pau Espin Pedrol58877f02022-10-13 14:05:50 +0200124 LOGPPMGW(pool_member, LOGL_DEBUG, "%s -- MGW %u is unusable (blocked=%u, cli=%u)\n",
125 __func__, pool_member->nr, pool_member->blocked, !!pool_member->client);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200126 }
127 }
128
129 if (pool_member_picked) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200130 LOGPPMGW(pool_member_picked, LOGL_DEBUG, "MGW pool has %u members -- using MGW %u (active calls: %u)\n",
131 n_pool_members, pool_member_picked->nr, pool_member_picked->refcount);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200132 return pool_member_picked;
133 }
134
135 LOGP(DLMGCP, LOGL_ERROR,
136 "MGW pool has %u members, but no functional MGW pool member found -- check configuration!\n",
137 n_pool_members);
138
139 return NULL;
140}
141
142/*! get an MGCP client from the pool (increment reference counter).
143 * \param[in,out] pool MGCP client pool descriptor.
144 * \returns MGCP client descriptor, NULL if no member was found (empty pool). */
145struct mgcp_client *mgcp_client_pool_get(struct mgcp_client_pool *pool)
146{
147 struct mgcp_client_pool_member *pool_member;
148
149 /*! When an MGCP client is taken from the pool it is still available for other calls. In fact only a reference
150 * counter is incremented to keep track on how many references to a specific MGCP client are currently used
151 * by the application code. */
152
153 /* When the pool is empty, return a single MGCP client if it is registered. */
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200154 if (llist_empty(&pool->member_list) && pool->mgcp_client_single) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200155 LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW %s\n",
156 mgcp_client_name(pool->mgcp_client_single));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200157 return pool->mgcp_client_single;
158 }
159
160 /* Abort when the pool is empty */
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200161 if (llist_empty(&pool->member_list)) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200162 LOGP(DLMGCP, LOGL_ERROR, "MGW pool is empty -- no MGW available!\n");
163 return NULL;
164 }
165
166 /* Pick a suitable pool member */
167 pool_member = mgcp_client_pool_pick(pool);
168 if (pool_member) {
169 pool_member->refcount++;
170 return pool_member->client;
171 }
172
173 return NULL;
174}
175
176/*! put an MGCP client back into the pool (decrement reference counter).
177 * \param[in,out] pool MGCP client pool descriptor.
178 * \param[in] mgcp MGCP client descriptor.
179 *
180 * This function is able to detect automatically to which pool the mgcp_client belongs. If the mgcp_client does
181 * not belong to a pool at all, the function call will have no effect. */
182void mgcp_client_pool_put(struct mgcp_client *mgcp_client)
183{
184 struct mgcp_client_pool_member *pool_member;
185 struct mgcp_client_pool *pool;
186
187 if (!mgcp_client)
188 return;
189
190 if (mgcp_client->pool)
191 pool = mgcp_client->pool;
192 else
193 return;
194
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200195 llist_for_each_entry(pool_member, &pool->member_list, list) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200196 if (pool_member->client == mgcp_client) {
197 if (pool_member->refcount == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200198 LOGPPMGW(pool_member, LOGL_ERROR, "MGW pool member has invalid refcount\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200199 return;
200 }
201 pool_member->refcount--;
202 }
203 }
204}
Pau Espin Pedrol6ddc79b2022-10-13 17:03:42 +0200205
206/***************************
207 * mgcp_client_pool_member:
208 ***************************/
209
210/*! Allocate an mgcp_client_pool_member.
211 * \param[in] pool MGCP client pool descriptor.
212 * \param[in] nr Reference number of the pool member.
213 */
214struct mgcp_client_pool_member *mgcp_client_pool_member_alloc(struct mgcp_client_pool *pool, unsigned int nr)
215{
216 struct mgcp_client_pool_member *pool_member;
217
218 pool_member = talloc_zero(pool, struct mgcp_client_pool_member);
219 OSMO_ASSERT(pool_member);
220 mgcp_client_conf_init(&pool_member->conf);
221 pool_member->nr = nr;
222 llist_add_tail(&pool_member->list, &pool->member_list);
223 return pool_member;
224}
225
226/*! Free an mgcp_client_pool_member allocated through mgcp_client_pool_member_alloc().
227 * \param[in] pool_member MGCP client pool descriptor.
228 *
229 * It also frees the associated MGCP client if present.
230 */
231void mgcp_client_pool_member_free(struct mgcp_client_pool_member *pool_member)
232{
233 llist_del(&pool_member->list);
234 if (pool_member->client) {
235 mgcp_client_disconnect(pool_member->client);
236 talloc_free(pool_member->client);
237 }
238 talloc_free(pool_member);
239}
Pau Espin Pedrol7558c202022-10-13 17:05:19 +0200240
241/* Get a human readable name for a given pool member. */
242const char *mgcp_client_pool_member_name(const struct mgcp_client_pool_member *pool_member)
243{
244 const struct mgcp_client *mpcp_client;
245 struct mgcp_client mpcp_client_dummy;
246 static char name[512];
247 const char *description;
248
249 if (!pool_member)
250 return "(null)";
251
252 /* It is not guranteed that a pool_member has an MGCP client. The client may not yet be initialized or the
253 * initalization may have been failed. In this case we will generate a dummy MGCP client to work with. */
254 if (!pool_member->client) {
255 memcpy(&mpcp_client_dummy.actual, &pool_member->conf, sizeof(mpcp_client_dummy.actual));
256 mpcp_client = &mpcp_client_dummy;
257 } else {
258 mpcp_client = pool_member->client;
259 }
260
261 description = mgcp_client_name(mpcp_client);
262 snprintf(name, sizeof(name), "%d:%s", pool_member->nr, description);
263
264 return name;
265}