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 | |
| 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 Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 27 | #define LOGPPMGW(pool_member, level, fmt, args...) \ |
| 28 | LOGP(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. */ |
| 31 | const 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 Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 56 | /*! 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. */ |
| 59 | struct 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 Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 67 | INIT_LLIST_HEAD(&pool->member_list); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 68 | |
| 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. */ |
| 75 | unsigned 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 Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 80 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 81 | |
| 82 | /* Initialize client */ |
| 83 | pool_member->client = mgcp_client_init(pool_member, &pool_member->conf); |
| 84 | if (!pool_member->client) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 85 | LOGPPMGW(pool_member, LOGL_ERROR, "MGCP client initialization failed\n"); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 86 | 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 Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 95 | 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 Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 97 | 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. */ |
| 111 | void 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). */ |
| 123 | static 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 Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 127 | unsigned int n_pool_members = 0; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 128 | |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 129 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
Pau Espin Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 130 | n_pool_members++; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 131 | 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 Pedrol | 58877f0 | 2022-10-13 14:05:50 +0200 | [diff] [blame] | 137 | 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 Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | if (pool_member_picked) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 143 | 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 Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 145 | 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). */ |
| 158 | struct 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 Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 167 | if (llist_empty(&pool->member_list) && pool->mgcp_client_single) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 168 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW %s\n", |
| 169 | mgcp_client_name(pool->mgcp_client_single)); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 170 | return pool->mgcp_client_single; |
| 171 | } |
| 172 | |
| 173 | /* Abort when the pool is empty */ |
Pau Espin Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 174 | if (llist_empty(&pool->member_list)) { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 175 | 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. */ |
| 195 | void 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 Pedrol | d4ad77d | 2022-10-13 16:37:12 +0200 | [diff] [blame^] | 208 | llist_for_each_entry(pool_member, &pool->member_list, list) { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 209 | if (pool_member->client == mgcp_client) { |
| 210 | if (pool_member->refcount == 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 211 | LOGPPMGW(pool_member, LOGL_ERROR, "MGW pool member has invalid refcount\n"); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | pool_member->refcount--; |
| 215 | } |
| 216 | } |
| 217 | } |