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 | |
| 67 | INIT_LLIST_HEAD(&pool->pool); |
| 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 | |
| 80 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 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; |
| 127 | unsigned int n_pool_members = llist_count(&pool->pool); |
| 128 | |
| 129 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 130 | if (pool_member->blocked == false && pool_member->client) { |
| 131 | if (!pool_member_picked) |
| 132 | pool_member_picked = pool_member; |
| 133 | else if (pool_member_picked->refcount > pool_member->refcount) |
| 134 | pool_member_picked = pool_member; |
| 135 | } else { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 136 | LOGPPMGW(pool_member, LOGL_DEBUG, "MGW pool has %u members -- MGW %u is unusable\n", n_pool_members, |
| 137 | pool_member->nr); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | if (pool_member_picked) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 142 | LOGPPMGW(pool_member_picked, LOGL_DEBUG, "MGW pool has %u members -- using MGW %u (active calls: %u)\n", |
| 143 | n_pool_members, pool_member_picked->nr, pool_member_picked->refcount); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 144 | return pool_member_picked; |
| 145 | } |
| 146 | |
| 147 | LOGP(DLMGCP, LOGL_ERROR, |
| 148 | "MGW pool has %u members, but no functional MGW pool member found -- check configuration!\n", |
| 149 | n_pool_members); |
| 150 | |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /*! get an MGCP client from the pool (increment reference counter). |
| 155 | * \param[in,out] pool MGCP client pool descriptor. |
| 156 | * \returns MGCP client descriptor, NULL if no member was found (empty pool). */ |
| 157 | struct mgcp_client *mgcp_client_pool_get(struct mgcp_client_pool *pool) |
| 158 | { |
| 159 | struct mgcp_client_pool_member *pool_member; |
| 160 | |
| 161 | /*! When an MGCP client is taken from the pool it is still available for other calls. In fact only a reference |
| 162 | * counter is incremented to keep track on how many references to a specific MGCP client are currently used |
| 163 | * by the application code. */ |
| 164 | |
| 165 | /* When the pool is empty, return a single MGCP client if it is registered. */ |
| 166 | if (llist_empty(&pool->pool) && pool->mgcp_client_single) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 167 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW %s\n", |
| 168 | mgcp_client_name(pool->mgcp_client_single)); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 169 | return pool->mgcp_client_single; |
| 170 | } |
| 171 | |
| 172 | /* Abort when the pool is empty */ |
| 173 | if (llist_empty(&pool->pool)) { |
| 174 | LOGP(DLMGCP, LOGL_ERROR, "MGW pool is empty -- no MGW available!\n"); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | /* Pick a suitable pool member */ |
| 179 | pool_member = mgcp_client_pool_pick(pool); |
| 180 | if (pool_member) { |
| 181 | pool_member->refcount++; |
| 182 | return pool_member->client; |
| 183 | } |
| 184 | |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | /*! put an MGCP client back into the pool (decrement reference counter). |
| 189 | * \param[in,out] pool MGCP client pool descriptor. |
| 190 | * \param[in] mgcp MGCP client descriptor. |
| 191 | * |
| 192 | * This function is able to detect automatically to which pool the mgcp_client belongs. If the mgcp_client does |
| 193 | * not belong to a pool at all, the function call will have no effect. */ |
| 194 | void mgcp_client_pool_put(struct mgcp_client *mgcp_client) |
| 195 | { |
| 196 | struct mgcp_client_pool_member *pool_member; |
| 197 | struct mgcp_client_pool *pool; |
| 198 | |
| 199 | if (!mgcp_client) |
| 200 | return; |
| 201 | |
| 202 | if (mgcp_client->pool) |
| 203 | pool = mgcp_client->pool; |
| 204 | else |
| 205 | return; |
| 206 | |
| 207 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 208 | if (pool_member->client == mgcp_client) { |
| 209 | if (pool_member->refcount == 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 210 | LOGPPMGW(pool_member, LOGL_ERROR, "MGW pool member has invalid refcount\n"); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | pool_member->refcount--; |
| 214 | } |
| 215 | } |
| 216 | } |