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 | |
| 27 | /*! Allocate MGCP client pool. This is called once on startup and before the pool is used with |
| 28 | * mgcp_client_pool_vty_init(). Since the pool is linked with the VTY it must exist througout the entire runtime. |
| 29 | * \param[in] talloc_ctx talloc context. */ |
| 30 | struct mgcp_client_pool *mgcp_client_pool_alloc(void *talloc_ctx) |
| 31 | { |
| 32 | struct mgcp_client_pool *pool; |
| 33 | |
| 34 | pool = talloc_zero(talloc_ctx, struct mgcp_client_pool); |
| 35 | if (!pool) |
| 36 | return NULL; |
| 37 | |
| 38 | INIT_LLIST_HEAD(&pool->pool); |
| 39 | |
| 40 | return pool; |
| 41 | } |
| 42 | |
| 43 | /*! Initialize and connect an mcgp client pool. |
| 44 | * \param[in,out] mgcp MGCP client pool descriptor. |
| 45 | * \returns number of successfully initialized pool members. */ |
| 46 | unsigned int mgcp_client_pool_connect(struct mgcp_client_pool *pool) |
| 47 | { |
| 48 | struct mgcp_client_pool_member *pool_member; |
| 49 | unsigned int pool_members_initialized = 0; |
| 50 | |
| 51 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 52 | |
| 53 | /* Initialize client */ |
| 54 | pool_member->client = mgcp_client_init(pool_member, &pool_member->conf); |
| 55 | if (!pool_member->client) { |
| 56 | LOGP(DLMGCP, LOGL_ERROR, "MGW %u initialization failed\n", pool_member->nr); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | /* Set backpointer so that we can detect later that this MGCP client is managed |
| 61 | * by this pool. */ |
| 62 | pool_member->client->pool = pool; |
| 63 | |
| 64 | /* Connect client */ |
| 65 | if (mgcp_client_connect2(pool_member->client, 0)) { |
| 66 | LOGP(DLMGCP, LOGL_ERROR, "MGW %u connect failed at (%s:%u)\n", |
| 67 | pool_member->nr, pool_member->conf.remote_addr, pool_member->conf.remote_port); |
| 68 | talloc_free(pool_member->client); |
| 69 | pool_member->client = NULL; |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | pool_members_initialized++; |
| 74 | } |
| 75 | |
| 76 | return pool_members_initialized; |
| 77 | } |
| 78 | |
| 79 | /*! register a single mgcp_client instance to the pool. |
| 80 | * \param[out] pool MGCP client pool descriptor. |
| 81 | * \param[in] mgcp MGCP client descriptor. */ |
| 82 | void mgcp_client_pool_register_single(struct mgcp_client_pool *pool, struct mgcp_client *mgcp_client) |
| 83 | { |
| 84 | /*! Some applications still support the non-pooled MGW VTY configuration variant provided by |
| 85 | * mgcp_client_vty_init(). If this is the case the mgcp_client instance created by mgcp_client_init() |
| 86 | * can be registered here so that it will appear as if it were part of the pool. When the user actively |
| 87 | * configures MGW pool members, the MGCP client registered here will be ignored. (The registration of |
| 88 | * multiple singe mgcp_client instances is not possible.) */ |
| 89 | pool->mgcp_client_single = mgcp_client; |
| 90 | } |
| 91 | |
| 92 | /* Not every pool member may have a functional MGCP client, we will run through the pool once until we meet a |
| 93 | * pool member that is suitable (has a client, is not blocked, has a low load). */ |
| 94 | static struct mgcp_client_pool_member *mgcp_client_pool_pick(struct mgcp_client_pool *pool) |
| 95 | { |
| 96 | struct mgcp_client_pool_member *pool_member; |
| 97 | struct mgcp_client_pool_member *pool_member_picked = NULL; |
| 98 | unsigned int n_pool_members = llist_count(&pool->pool); |
| 99 | |
| 100 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 101 | if (pool_member->blocked == false && pool_member->client) { |
| 102 | if (!pool_member_picked) |
| 103 | pool_member_picked = pool_member; |
| 104 | else if (pool_member_picked->refcount > pool_member->refcount) |
| 105 | pool_member_picked = pool_member; |
| 106 | } else { |
| 107 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool has %u members -- MGW %u is unusable\n", n_pool_members, |
| 108 | pool_member->nr); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (pool_member_picked) { |
| 113 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool has %u members -- using MGW %u (active calls: %u)\n", |
| 114 | n_pool_members, pool_member_picked->nr, pool_member_picked->refcount); |
| 115 | return pool_member_picked; |
| 116 | } |
| 117 | |
| 118 | LOGP(DLMGCP, LOGL_ERROR, |
| 119 | "MGW pool has %u members, but no functional MGW pool member found -- check configuration!\n", |
| 120 | n_pool_members); |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | /*! get an MGCP client from the pool (increment reference counter). |
| 126 | * \param[in,out] pool MGCP client pool descriptor. |
| 127 | * \returns MGCP client descriptor, NULL if no member was found (empty pool). */ |
| 128 | struct mgcp_client *mgcp_client_pool_get(struct mgcp_client_pool *pool) |
| 129 | { |
| 130 | struct mgcp_client_pool_member *pool_member; |
| 131 | |
| 132 | /*! When an MGCP client is taken from the pool it is still available for other calls. In fact only a reference |
| 133 | * counter is incremented to keep track on how many references to a specific MGCP client are currently used |
| 134 | * by the application code. */ |
| 135 | |
| 136 | /* When the pool is empty, return a single MGCP client if it is registered. */ |
| 137 | if (llist_empty(&pool->pool) && pool->mgcp_client_single) { |
| 138 | LOGP(DLMGCP, LOGL_DEBUG, "MGW pool is empty -- using (single) MGW\n"); |
| 139 | return pool->mgcp_client_single; |
| 140 | } |
| 141 | |
| 142 | /* Abort when the pool is empty */ |
| 143 | if (llist_empty(&pool->pool)) { |
| 144 | LOGP(DLMGCP, LOGL_ERROR, "MGW pool is empty -- no MGW available!\n"); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | /* Pick a suitable pool member */ |
| 149 | pool_member = mgcp_client_pool_pick(pool); |
| 150 | if (pool_member) { |
| 151 | pool_member->refcount++; |
| 152 | return pool_member->client; |
| 153 | } |
| 154 | |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /*! put an MGCP client back into the pool (decrement reference counter). |
| 159 | * \param[in,out] pool MGCP client pool descriptor. |
| 160 | * \param[in] mgcp MGCP client descriptor. |
| 161 | * |
| 162 | * This function is able to detect automatically to which pool the mgcp_client belongs. If the mgcp_client does |
| 163 | * not belong to a pool at all, the function call will have no effect. */ |
| 164 | void mgcp_client_pool_put(struct mgcp_client *mgcp_client) |
| 165 | { |
| 166 | struct mgcp_client_pool_member *pool_member; |
| 167 | struct mgcp_client_pool *pool; |
| 168 | |
| 169 | if (!mgcp_client) |
| 170 | return; |
| 171 | |
| 172 | if (mgcp_client->pool) |
| 173 | pool = mgcp_client->pool; |
| 174 | else |
| 175 | return; |
| 176 | |
| 177 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 178 | if (pool_member->client == mgcp_client) { |
| 179 | if (pool_member->refcount == 0) { |
| 180 | LOGP(DLMGCP, LOGL_ERROR, "MGW %u has invalid refcount\n", pool_member->nr); |
| 181 | return; |
| 182 | } |
| 183 | pool_member->refcount--; |
| 184 | } |
| 185 | } |
| 186 | } |