Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 1 | /* (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 | |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 21 | #include <asm-generic/errno.h> |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 22 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 23 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
| 24 | #include <osmocom/mgcp_client/mgcp_client_pool_internal.h> |
| 25 | #include <osmocom/mgcp_client/mgcp_client_pool.h> |
| 26 | #include <stddef.h> |
| 27 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 28 | #define LOGPPMGW(pool_member, level, fmt, args...) \ |
| 29 | LOGP(DLMGCP, level, "MGW-pool(%s) " fmt, mgcp_client_pool_member_name(pool_member), ## args) |
| 30 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 31 | /*! Allocate MGCP client pool. This is called once on startup and before the pool is used with |
| 32 | * mgcp_client_pool_vty_init(). Since the pool is linked with the VTY it must exist througout the entire runtime. |
| 33 | * \param[in] talloc_ctx talloc context. */ |
| 34 | struct mgcp_client_pool *mgcp_client_pool_alloc(void *talloc_ctx) |
| 35 | { |
| 36 | struct mgcp_client_pool *pool; |
| 37 | |
| 38 | pool = talloc_zero(talloc_ctx, struct mgcp_client_pool); |
| 39 | if (!pool) |
| 40 | return NULL; |
| 41 | |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame] | 42 | INIT_LLIST_HEAD(&pool->member_list); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 43 | |
| 44 | return pool; |
| 45 | } |
| 46 | |
| 47 | /*! Initialize and connect an mcgp client pool. |
| 48 | * \param[in,out] mgcp MGCP client pool descriptor. |
| 49 | * \returns number of successfully initialized pool members. */ |
| 50 | unsigned int mgcp_client_pool_connect(struct mgcp_client_pool *pool) |
| 51 | { |
| 52 | struct mgcp_client_pool_member *pool_member; |
| 53 | unsigned int pool_members_initialized = 0; |
| 54 | |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame] | 55 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 56 | |
| 57 | /* Initialize client */ |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 58 | if (mgcp_client_pool_member_reinit_client(pool_member) == 0) |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 59 | pool_members_initialized++; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return pool_members_initialized; |
| 63 | } |
| 64 | |
| 65 | /*! register a single mgcp_client instance to the pool. |
| 66 | * \param[out] pool MGCP client pool descriptor. |
| 67 | * \param[in] mgcp MGCP client descriptor. */ |
| 68 | void mgcp_client_pool_register_single(struct mgcp_client_pool *pool, struct mgcp_client *mgcp_client) |
| 69 | { |
| 70 | /*! Some applications still support the non-pooled MGW VTY configuration variant provided by |
| 71 | * mgcp_client_vty_init(). If this is the case the mgcp_client instance created by mgcp_client_init() |
| 72 | * can be registered here so that it will appear as if it were part of the pool. When the user actively |
| 73 | * configures MGW pool members, the MGCP client registered here will be ignored. (The registration of |
| 74 | * multiple singe mgcp_client instances is not possible.) */ |
| 75 | pool->mgcp_client_single = mgcp_client; |
| 76 | } |
| 77 | |
Oliver Smith | 833ab4b | 2023-04-04 14:13:30 +0200 | [diff] [blame^] | 78 | bool mgcp_client_pool_empty(const struct mgcp_client_pool *pool) |
| 79 | { |
| 80 | return llist_empty(&pool->member_list); |
| 81 | } |
| 82 | |
Pau Espin Pedrol | ff59215 | 2022-10-13 16:46:25 +0200 | [diff] [blame] | 83 | /*! Lookup the selected MGCP client config by its reference number */ |
| 84 | struct mgcp_client_pool_member *mgcp_client_pool_find_member_by_nr(struct mgcp_client_pool *pool, unsigned int nr) |
| 85 | { |
| 86 | struct mgcp_client_pool_member *pool_member; |
| 87 | |
| 88 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
| 89 | if (pool_member->nr == nr) |
| 90 | return pool_member; |
| 91 | } |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 96 | /* Not every pool member may have a functional MGCP client, we will run through the pool once until we meet a |
| 97 | * pool member that is suitable (has a client, is not blocked, has a low load). */ |
| 98 | static struct mgcp_client_pool_member *mgcp_client_pool_pick(struct mgcp_client_pool *pool) |
| 99 | { |
| 100 | struct mgcp_client_pool_member *pool_member; |
| 101 | struct mgcp_client_pool_member *pool_member_picked = NULL; |
Pau Espin Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 102 | unsigned int n_pool_members = 0; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 103 | |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame] | 104 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
Pau Espin Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 105 | n_pool_members++; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 106 | if (pool_member->blocked == false && pool_member->client) { |
| 107 | if (!pool_member_picked) |
| 108 | pool_member_picked = pool_member; |
| 109 | else if (pool_member_picked->refcount > pool_member->refcount) |
| 110 | pool_member_picked = pool_member; |
| 111 | } else { |
Pau Espin Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 112 | LOGPPMGW(pool_member, LOGL_DEBUG, "%s -- MGW %u is unusable (blocked=%u, cli=%u)\n", |
| 113 | __func__, pool_member->nr, pool_member->blocked, !!pool_member->client); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | if (pool_member_picked) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 118 | LOGPPMGW(pool_member_picked, LOGL_DEBUG, "MGW pool has %u members -- using MGW %u (active calls: %u)\n", |
| 119 | n_pool_members, pool_member_picked->nr, pool_member_picked->refcount); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 120 | return pool_member_picked; |
| 121 | } |
| 122 | |
| 123 | LOGP(DLMGCP, LOGL_ERROR, |
| 124 | "MGW pool has %u members, but no functional MGW pool member found -- check configuration!\n", |
| 125 | n_pool_members); |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /*! get an MGCP client from the pool (increment reference counter). |
| 131 | * \param[in,out] pool MGCP client pool descriptor. |
| 132 | * \returns MGCP client descriptor, NULL if no member was found (empty pool). */ |
| 133 | struct mgcp_client *mgcp_client_pool_get(struct mgcp_client_pool *pool) |
| 134 | { |
| 135 | struct mgcp_client_pool_member *pool_member; |
| 136 | |
| 137 | /*! When an MGCP client is taken from the pool it is still available for other calls. In fact only a reference |
| 138 | * counter is incremented to keep track on how many references to a specific MGCP client are currently used |
| 139 | * by the application code. */ |
| 140 | |
| 141 | /* When the pool is empty, return a single MGCP client if it is registered. */ |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame] | 142 | if (llist_empty(&pool->member_list) && pool->mgcp_client_single) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 143 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW %s\n", |
| 144 | mgcp_client_name(pool->mgcp_client_single)); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 145 | return pool->mgcp_client_single; |
| 146 | } |
| 147 | |
| 148 | /* Abort when the pool is empty */ |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame] | 149 | if (llist_empty(&pool->member_list)) { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 150 | LOGP(DLMGCP, LOGL_ERROR, "MGW pool is empty -- no MGW available!\n"); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /* Pick a suitable pool member */ |
| 155 | pool_member = mgcp_client_pool_pick(pool); |
Pau Espin Pedrol | 5d8b5b0 | 2022-10-13 17:52:40 +0200 | [diff] [blame] | 156 | if (!pool_member) |
| 157 | return NULL; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 158 | |
Pau Espin Pedrol | 5d8b5b0 | 2022-10-13 17:52:40 +0200 | [diff] [blame] | 159 | return mgcp_client_pool_member_get(pool_member); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /*! put an MGCP client back into the pool (decrement reference counter). |
| 163 | * \param[in,out] pool MGCP client pool descriptor. |
| 164 | * \param[in] mgcp MGCP client descriptor. |
| 165 | * |
| 166 | * This function is able to detect automatically to which pool the mgcp_client belongs. If the mgcp_client does |
| 167 | * not belong to a pool at all, the function call will have no effect. */ |
| 168 | void mgcp_client_pool_put(struct mgcp_client *mgcp_client) |
| 169 | { |
| 170 | struct mgcp_client_pool_member *pool_member; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 171 | |
| 172 | if (!mgcp_client) |
| 173 | return; |
| 174 | |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 175 | if (!mgcp_client->pool_member) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 176 | return; |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 177 | pool_member = mgcp_client->pool_member; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 178 | |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 179 | if (pool_member->refcount == 0) { |
| 180 | LOGPPMGW(pool_member, LOGL_ERROR, "MGW pool member has invalid refcount\n"); |
| 181 | return; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 182 | } |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 183 | pool_member->refcount--; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 184 | } |
Pau Espin Pedrol | 6ddc79b | 2022-10-13 17:03:42 +0200 | [diff] [blame] | 185 | |
| 186 | /*************************** |
| 187 | * mgcp_client_pool_member: |
| 188 | ***************************/ |
| 189 | |
| 190 | /*! Allocate an mgcp_client_pool_member. |
| 191 | * \param[in] pool MGCP client pool descriptor. |
| 192 | * \param[in] nr Reference number of the pool member. |
| 193 | */ |
| 194 | struct mgcp_client_pool_member *mgcp_client_pool_member_alloc(struct mgcp_client_pool *pool, unsigned int nr) |
| 195 | { |
| 196 | struct mgcp_client_pool_member *pool_member; |
| 197 | |
| 198 | pool_member = talloc_zero(pool, struct mgcp_client_pool_member); |
| 199 | OSMO_ASSERT(pool_member); |
| 200 | mgcp_client_conf_init(&pool_member->conf); |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 201 | pool_member->pool = pool; |
Pau Espin Pedrol | 6ddc79b | 2022-10-13 17:03:42 +0200 | [diff] [blame] | 202 | pool_member->nr = nr; |
| 203 | llist_add_tail(&pool_member->list, &pool->member_list); |
| 204 | return pool_member; |
| 205 | } |
| 206 | |
| 207 | /*! Free an mgcp_client_pool_member allocated through mgcp_client_pool_member_alloc(). |
| 208 | * \param[in] pool_member MGCP client pool descriptor. |
| 209 | * |
| 210 | * It also frees the associated MGCP client if present. |
| 211 | */ |
| 212 | void mgcp_client_pool_member_free(struct mgcp_client_pool_member *pool_member) |
| 213 | { |
| 214 | llist_del(&pool_member->list); |
| 215 | if (pool_member->client) { |
| 216 | mgcp_client_disconnect(pool_member->client); |
| 217 | talloc_free(pool_member->client); |
| 218 | } |
| 219 | talloc_free(pool_member); |
| 220 | } |
Pau Espin Pedrol | 7558c20 | 2022-10-13 17:05:19 +0200 | [diff] [blame] | 221 | |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 222 | /*! Recreate and reconnect the MGCP client associated to the pool descriptor. |
| 223 | * \param[in] pool_member MGCP client pool descriptor. |
| 224 | */ |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 225 | int mgcp_client_pool_member_reinit_client(struct mgcp_client_pool_member *pool_member) |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 226 | { |
| 227 | /* Get rid of a possibly existing old MGCP client instance first */ |
| 228 | if (pool_member->client) { |
| 229 | mgcp_client_disconnect(pool_member->client); |
| 230 | talloc_free(pool_member->client); |
| 231 | } |
| 232 | |
| 233 | /* Initialize client */ |
| 234 | pool_member->client = mgcp_client_init(pool_member, &pool_member->conf); |
| 235 | if (!pool_member->client) { |
| 236 | LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client initialization failed\n"); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | /* Set backpointer so that we can detect later that this MGCP client is managed by this pool. */ |
Pau Espin Pedrol | 35bd252 | 2022-10-13 17:26:33 +0200 | [diff] [blame] | 241 | pool_member->client->pool_member = pool_member; |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 242 | |
| 243 | /* Connect client */ |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 244 | if (mgcp_client_connect(pool_member->client)) { |
Pau Espin Pedrol | f48cd95 | 2022-10-13 17:18:40 +0200 | [diff] [blame] | 245 | LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client connect failed at (%s:%u)\n", |
| 246 | pool_member->conf.remote_addr, pool_member->conf.remote_port); |
| 247 | talloc_free(pool_member->client); |
| 248 | pool_member->client = NULL; |
| 249 | return -ECONNABORTED; |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
Pau Espin Pedrol | 7558c20 | 2022-10-13 17:05:19 +0200 | [diff] [blame] | 254 | /* Get a human readable name for a given pool member. */ |
| 255 | const char *mgcp_client_pool_member_name(const struct mgcp_client_pool_member *pool_member) |
| 256 | { |
| 257 | const struct mgcp_client *mpcp_client; |
| 258 | struct mgcp_client mpcp_client_dummy; |
| 259 | static char name[512]; |
| 260 | const char *description; |
| 261 | |
| 262 | if (!pool_member) |
| 263 | return "(null)"; |
| 264 | |
| 265 | /* It is not guranteed that a pool_member has an MGCP client. The client may not yet be initialized or the |
| 266 | * initalization may have been failed. In this case we will generate a dummy MGCP client to work with. */ |
| 267 | if (!pool_member->client) { |
| 268 | memcpy(&mpcp_client_dummy.actual, &pool_member->conf, sizeof(mpcp_client_dummy.actual)); |
| 269 | mpcp_client = &mpcp_client_dummy; |
| 270 | } else { |
| 271 | mpcp_client = pool_member->client; |
| 272 | } |
| 273 | |
| 274 | description = mgcp_client_name(mpcp_client); |
| 275 | snprintf(name, sizeof(name), "%d:%s", pool_member->nr, description); |
| 276 | |
| 277 | return name; |
| 278 | } |
Pau Espin Pedrol | 5d8b5b0 | 2022-10-13 17:52:40 +0200 | [diff] [blame] | 279 | |
| 280 | /*! Get the MGCP client associated with the pool reference from the pool (increment reference counter). |
| 281 | * \param[in] pool_member MGCP client pool descriptor. |
| 282 | * \returns MGCP client descriptor, NULL if no member was not ready. |
| 283 | */ |
| 284 | struct mgcp_client *mgcp_client_pool_member_get(struct mgcp_client_pool_member *pool_member) |
| 285 | { |
| 286 | pool_member->refcount++; |
| 287 | return pool_member->client; |
| 288 | } |
| 289 | |
| 290 | /*! Get whether the MGCP client associated with the pool reference is blocked by policy. |
| 291 | * \param[in] pool_member MGCP client pool descriptor. |
| 292 | * \returns true if blocked, false otherwise |
| 293 | */ |
| 294 | bool mgcp_client_pool_member_is_blocked(const struct mgcp_client_pool_member *pool_member) |
| 295 | { |
| 296 | return pool_member->blocked; |
| 297 | } |