blob: 7779e2c70859ed133b9ebcf02d4402d0d68fba70 [file] [log] [blame]
Neels Hofmeyr87203f22017-10-31 01:15:45 +01001/* MGCP client interface to quagga VTY */
Neels Hofmeyre9920f22017-07-10 15:07:22 +02002/* (C) 2016 by sysmocom s.m.f.c. GmbH <info@sysmocom.de>
3 * Based on OpenBSC interface to quagga VTY (libmsc/vty_interface_layer3.c)
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2009-2011 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte220f4872018-02-04 09:04:16 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyre9920f22017-07-10 15:07:22 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte220f4872018-02-04 09:04:16 +010016 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 *
Harald Welte220f4872018-02-04 09:04:16 +010018 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <inttypes.h>
24#include <stdlib.h>
25#include <talloc.h>
26
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020027#include <osmocom/vty/vty.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028#include <osmocom/vty/command.h>
Stefan Sperling29248252018-02-22 18:09:28 +010029#include <osmocom/vty/misc.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020030#include <osmocom/core/utils.h>
31
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020032#include <osmocom/mgcp_client/mgcp_client.h>
Philipp Maier3f2c15f2021-07-22 11:53:07 +020033#include <osmocom/mgcp_client/mgcp_client_internal.h>
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020034#include <osmocom/mgcp_client/mgcp_client_pool_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020035
Neels Hofmeyre6d8e912018-08-23 16:36:48 +020036#define MGW_STR MGCP_CLIENT_MGW_STR
Neels Hofmeyre9920f22017-07-10 15:07:22 +020037
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020038/* Only common (non-pooled) VTY connands will use this talloc context. All
39 * pooled VTY commands will use the pool (global_mgcp_client_pool) as
40 * talloc context. */
41static void *global_mgcp_client_ctx = NULL;
42
43/* MGCP Client configuration used with mgcp_client_vty_init(). (This pointer
44 * points to user provided memory, so it cannot be used as talloc context.) */
45static struct mgcp_client_conf *global_mgcp_client_conf = NULL;
46
47/* Pointer to the MGCP pool that is managed by mgcp_client_pool_vty_init() */
48static struct mgcp_client_pool *global_mgcp_client_pool = NULL;
49
50struct mgcp_client_conf *get_mgcp_client_config(struct vty *vty)
51{
52 if (global_mgcp_client_pool && vty->node == global_mgcp_client_pool->vty_node->node)
53 return vty->index;
54 else
55 return global_mgcp_client_conf;
56}
Neels Hofmeyre9920f22017-07-10 15:07:22 +020057
Neels Hofmeyr87203f22017-10-31 01:15:45 +010058DEFUN(cfg_mgw_local_ip, cfg_mgw_local_ip_cmd,
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020059 "mgw local-ip " VTY_IPV46_CMD,
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010060 MGW_STR "local bind to connect to MGW from\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020061 "local bind IPv4 address\n"
62 "local bind IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020063{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020064 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
65
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010066 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020067 (char **)&conf->local_addr,
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010068 argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020069 return CMD_SUCCESS;
70}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010071ALIAS_DEPRECATED(cfg_mgw_local_ip, cfg_mgcpgw_local_ip_cmd,
72 "mgcpgw local-ip A.B.C.D",
73 MGW_STR "local bind to connect to MGCP gateway with\n"
74 "local bind IP address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020075
Neels Hofmeyr87203f22017-10-31 01:15:45 +010076DEFUN(cfg_mgw_local_port, cfg_mgw_local_port_cmd,
77 "mgw local-port <0-65535>",
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010078 MGW_STR "local port to connect to MGW from\n"
Neels Hofmeyre9920f22017-07-10 15:07:22 +020079 "local bind port\n")
80{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020081 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
82
83 conf->local_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020084 return CMD_SUCCESS;
85}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010086ALIAS_DEPRECATED(cfg_mgw_local_port, cfg_mgcpgw_local_port_cmd,
87 "mgcpgw local-port <0-65535>",
88 MGW_STR "local bind to connect to MGCP gateway with\n"
89 "local bind port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020090
Neels Hofmeyr87203f22017-10-31 01:15:45 +010091DEFUN(cfg_mgw_remote_ip, cfg_mgw_remote_ip_cmd,
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020092 "mgw remote-ip " VTY_IPV46_CMD,
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010093 MGW_STR "remote IP address to reach the MGW at\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020094 "remote IPv4 address\n"
95 "remote IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020096{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020097 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
98
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010099 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200100 (char **)&conf->remote_addr, argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200101 return CMD_SUCCESS;
102}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100103ALIAS_DEPRECATED(cfg_mgw_remote_ip, cfg_mgcpgw_remote_ip_cmd,
104 "mgcpgw remote-ip A.B.C.D",
105 MGW_STR "remote bind to connect to MGCP gateway with\n"
106 "remote bind IP address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200107
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100108DEFUN(cfg_mgw_remote_port, cfg_mgw_remote_port_cmd,
109 "mgw remote-port <0-65535>",
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +0100110 MGW_STR "remote port to reach the MGW at\n"
111 "remote port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200112{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200113 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
114
115 conf->remote_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200116 return CMD_SUCCESS;
117}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100118ALIAS_DEPRECATED(cfg_mgw_remote_port, cfg_mgcpgw_remote_port_cmd,
119 "mgcpgw remote-port <0-65535>",
120 MGW_STR "remote bind to connect to MGCP gateway with\n"
121 "remote bind port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200122
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100123DEFUN_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgw_endpoint_range_cmd,
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100124 "mgw endpoint-range <1-65534> <1-65534>",
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100125 MGW_STR "DEPRECATED: the endpoint range cannot be defined by the client\n"
126 "-\n" "-\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200127{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100128 vty_out(vty, "Please do not use legacy config 'mgw endpoint-range'"
129 " (the range can no longer be defined by the MGCP client)%s",
130 VTY_NEWLINE);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200131 return CMD_SUCCESS;
132}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100133ALIAS_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgcpgw_endpoint_range_cmd,
134 "mgcpgw endpoint-range <1-65534> <1-65534>",
135 MGW_STR "usable range of endpoint identifiers\n"
136 "set first useable endpoint identifier\n"
137 "set the last useable endpoint identifier\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200138
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000139#define BTS_START_STR "First UDP port allocated for the BTS side\n"
140#define UDP_PORT_STR "UDP Port number\n"
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100141DEFUN_DEPRECATED(cfg_mgw_rtp_bts_base_port,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000142 cfg_mgw_rtp_bts_base_port_cmd,
143 "mgw bts-base <0-65534>",
144 MGW_STR
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100145 "DEPRECATED: there is no explicit BTS side in current osmo-mgw\n" "-\n")
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000146{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100147 vty_out(vty, "Please do not use legacy config 'mgw bts-base'"
148 " (there is no explicit BTS side in an MGW anymore)%s",
149 VTY_NEWLINE);
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000150 return CMD_SUCCESS;
151}
152ALIAS_DEPRECATED(cfg_mgw_rtp_bts_base_port,
153 cfg_mgcpgw_rtp_bts_base_port_cmd,
154 "mgcpgw bts-base <0-65534>",
155 MGW_STR
156 BTS_START_STR
157 UDP_PORT_STR)
158
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100159DEFUN(cfg_mgw_endpoint_domain_name,
160 cfg_mgw_endpoint_domain_name_cmd,
161 "mgw endpoint-domain NAME",
162 MGW_STR "Set the domain name to send in MGCP messages, e.g. the part 'foo' in 'rtpbridge/*@foo'.\n"
163 "Domain name, should be alphanumeric.\n")
164{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200165 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
166
167 if (osmo_strlcpy(conf->endpoint_domain_name, argv[0], sizeof(conf->endpoint_domain_name))
168 >= sizeof(conf->endpoint_domain_name)) {
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100169 vty_out(vty, "%% Error: 'mgw endpoint-domain' name too long, max length is %zu: '%s'%s",
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200170 sizeof(conf->endpoint_domain_name) - 1, argv[0], VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100171 return CMD_WARNING;
172 }
173 return CMD_SUCCESS;
174}
175
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200176DEFUN(cfg_mgw_reset_ep_name,
177 cfg_mgw_reset_ep_name_cmd,
178 "mgw reset-endpoint NAME",
179 MGW_STR "Add an endpoint name that should be reset (DLCX) on connect to the reset-endpoint list,"
180 "e.g. 'rtpbridge/*'\n"
181 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
182{
183 int rc;
184 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200185 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200186
187 /* stop when the address is already in the list */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200188 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200189 if (strcmp(argv[0], reset_ep->name) == 0) {
190 vty_out(vty, "%% duplicate endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
191 return CMD_WARNING;
192 }
193 }
194
195 /* the domain name is not part of the actual endpoint name */
196 if (strchr(argv[0], '@')) {
197 vty_out(vty, "%% the endpoint name must be given without domain name ('%s')%s",
198 argv[0], VTY_NEWLINE);
199 return CMD_WARNING;
200 }
201
202 reset_ep = talloc_zero(global_mgcp_client_ctx, struct reset_ep);
203 OSMO_ASSERT(reset_ep);
204
205 rc = osmo_strlcpy(reset_ep->name, argv[0], sizeof(reset_ep->name));
206 if (rc >= sizeof(reset_ep->name)) {
207 vty_out(vty, "%% Error: 'mgw reset-endpoint' name too long, max length is %zu: '%s'%s",
208 sizeof(reset_ep->name) - 1, argv[0], VTY_NEWLINE);
209 talloc_free(reset_ep);
210 return CMD_WARNING;
211 }
212
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200213 llist_add_tail(&reset_ep->list, &conf->reset_epnames);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200214
215 return CMD_SUCCESS;
216}
217
218DEFUN(cfg_mgw_no_reset_ep_name,
219 cfg_mgw_no_reset_ep_name_cmd,
220 "no mgw reset-endpoint NAME",
Oliver Smith29e671f2021-08-18 08:44:47 +0200221 NO_STR MGW_STR "remove an endpoint name from the reset-endpoint list, e.g. 'rtpbridge/*'\n"
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200222 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
223{
224 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200225 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200226
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200227 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200228 if (strcmp(argv[0], reset_ep->name) == 0) {
229 llist_del(&reset_ep->list);
230 talloc_free(reset_ep);
231 return CMD_SUCCESS;
232 }
233 }
234
235 vty_out(vty, "%% no such endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
236 return CMD_WARNING;
237}
238
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200239static int config_write(struct vty *vty, const char *indent, struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200240{
241 const char *addr;
242 int port;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200243 struct reset_ep *reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200244
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200245 addr = conf->local_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200246 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100247 vty_out(vty, "%smgw local-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200248 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200249 port = conf->local_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200250 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100251 vty_out(vty, "%smgw local-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200252 (uint16_t)port, VTY_NEWLINE);
253
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200254 addr = conf->remote_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200255 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100256 vty_out(vty, "%smgw remote-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200257 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200258 port = conf->remote_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200259 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100260 vty_out(vty, "%smgw remote-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200261 (uint16_t)port, VTY_NEWLINE);
262
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200263 if (conf->endpoint_domain_name[0])
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100264 vty_out(vty, "%smgw endpoint-domain %s%s", indent,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200265 conf->endpoint_domain_name, VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100266
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200267 llist_for_each_entry(reset_ep, &conf->reset_epnames, list)
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200268 vty_out(vty, "%smgw reset-endpoint %s%s", indent, reset_ep->name, VTY_NEWLINE);
269
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200270 return CMD_SUCCESS;
271}
272
Philipp Maierd6a7e172021-08-03 16:03:27 +0200273/*! Write out MGCP client config to VTY.
274 * \param[in] vty VTY to which we should print.
275 * \param[in] string used for indentation (e.g. " ").
276 * \returns CMD_SUCCESS on success, CMD_WARNING on error */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200277int mgcp_client_config_write(struct vty *vty, const char *indent)
278{
279 return config_write(vty, indent, global_mgcp_client_conf);
280}
281
282static void vty_init_common(void *talloc_ctx, int node)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200283{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200284 global_mgcp_client_ctx = talloc_ctx;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200285
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700286 install_lib_element(node, &cfg_mgw_local_ip_cmd);
287 install_lib_element(node, &cfg_mgw_local_port_cmd);
288 install_lib_element(node, &cfg_mgw_remote_ip_cmd);
289 install_lib_element(node, &cfg_mgw_remote_port_cmd);
290 install_lib_element(node, &cfg_mgw_endpoint_range_cmd);
291 install_lib_element(node, &cfg_mgw_rtp_bts_base_port_cmd);
292 install_lib_element(node, &cfg_mgw_endpoint_domain_name_cmd);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200293 install_lib_element(node, &cfg_mgw_reset_ep_name_cmd);
294 install_lib_element(node, &cfg_mgw_no_reset_ep_name_cmd);
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100295
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200296 osmo_fsm_vty_add_cmds();
297}
298
Philipp Maierd6a7e172021-08-03 16:03:27 +0200299/*! Set up MGCP client VTY
300 * (called once at startup by the application process).
301 * \param[in] talloc_ctx talloc context to be used by the VTY for allocating memory.
302 * \param[in] node identifier of the node on which the VTY commands should be installed.
303 * \param[in] conf user provided memory to to store the MGCP client configuration data. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200304void mgcp_client_vty_init(void *talloc_ctx, int node, struct mgcp_client_conf *conf)
305{
306 global_mgcp_client_conf = conf;
307
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100308 /* deprecated 'mgcpgw' commands */
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700309 install_lib_element(node, &cfg_mgcpgw_local_ip_cmd);
310 install_lib_element(node, &cfg_mgcpgw_local_port_cmd);
311 install_lib_element(node, &cfg_mgcpgw_remote_ip_cmd);
312 install_lib_element(node, &cfg_mgcpgw_remote_port_cmd);
313 install_lib_element(node, &cfg_mgcpgw_endpoint_range_cmd);
314 install_lib_element(node, &cfg_mgcpgw_rtp_bts_base_port_cmd);
Stefan Sperling29248252018-02-22 18:09:28 +0100315
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200316 vty_init_common(talloc_ctx, node);
317}
318
319static int config_write_pool(struct vty *vty)
320{
321 struct mgcp_client_pool *pool = global_mgcp_client_pool;
322 struct mgcp_client_pool_member *pool_member;
323 unsigned int indent_buf_len = strlen(pool->vty_indent) + 1 + 1;
324 char *indent = talloc_zero_size(vty, indent_buf_len);
325
326 snprintf(indent, indent_buf_len, "%s ", pool->vty_indent);
327
328 llist_for_each_entry(pool_member, &pool->pool, list) {
329 vty_out(vty, "%smgw %u%s", pool->vty_indent, pool_member->nr, VTY_NEWLINE);
330 config_write(vty, indent, &pool_member->conf);
331 }
332
333 talloc_free(indent);
334 return CMD_SUCCESS;
335}
336
337/* Lookup the selected MGCP client config by its reference number */
338static struct mgcp_client_pool_member *pool_member_by_nr(unsigned int nr)
339{
340 struct mgcp_client_pool_member *pool_member = NULL;
341 struct mgcp_client_pool_member *pool_member_tmp;
342
343 llist_for_each_entry(pool_member_tmp, &global_mgcp_client_pool->pool, list) {
344 if (pool_member_tmp->nr == nr) {
345 pool_member = pool_member_tmp;
346 break;
347 }
348 }
349
350 return pool_member;
351}
352
353DEFUN_ATTR(cfg_mgw,
354 cfg_mgw_cmd, "mgw <0-255>", "Select a MGCP client config to setup\n" "reference number", CMD_ATTR_IMMEDIATE)
355{
356 int nr = atoi(argv[0]);
357 struct mgcp_client_pool_member *pool_member;
358
359 pool_member = pool_member_by_nr(nr);
360 if (!pool_member) {
361 pool_member = talloc_zero(global_mgcp_client_pool, struct mgcp_client_pool_member);
362 OSMO_ASSERT(pool_member);
363 mgcp_client_conf_init(&pool_member->conf);
364 pool_member->nr = nr;
365 llist_add_tail(&pool_member->list, &global_mgcp_client_pool->pool);
366 }
367
368 vty->index = &pool_member->conf;
369 vty->index_sub = NULL;
370 vty->node = global_mgcp_client_pool->vty_node->node;
371
372 return CMD_SUCCESS;
373}
374
375DEFUN_ATTR(cfg_no_mgw,
376 cfg_no_mgw_cmd,
377 "no mgw <0-255>", "Select a MGCP client config to remove\n" "reference number", CMD_ATTR_IMMEDIATE)
378{
379 int nr = atoi(argv[0]);
380 struct mgcp_client_pool_member *pool_member;
381
382 pool_member = pool_member_by_nr(nr);
383 if (!pool_member) {
384 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
385 return CMD_WARNING;
386 }
387
388 /* Make sure that there are no ongoing calls */
389 if (pool_member->refcount > 0) {
390 vty_out(vty, "%% MGCP client (MGW %u) is still serving ongoing calls -- can't remove it now!%s",
391 pool_member->nr, VTY_NEWLINE);
392 return CMD_WARNING;
393 }
394
395 llist_del(&pool_member->list);
396 if (pool_member->client) {
397 mgcp_client_disconnect(pool_member->client);
398 talloc_free(pool_member->client);
399 }
400 talloc_free(pool_member);
401
402 return CMD_SUCCESS;
403}
404
405DEFUN_ATTR(mgw_reconnect, mgw_reconnect_cmd,
406 "mgw <0-255> reconnect",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200407 MGW_STR "reference number\n" "reconfigure and reconnect MGCP client\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200408{
409 int nr = atoi(argv[0]);
410 struct mgcp_client_pool_member *pool_member = NULL;
411
412 pool_member = pool_member_by_nr(nr);
413 if (!pool_member) {
414 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
415 return CMD_WARNING;
416 }
417
418 /* Make sure that there are no ongoing calls */
419 if (pool_member->refcount > 0) {
420 vty_out(vty, "%% MGCP client (MGW %u) is still serving ongoing calls -- can't reconnect it now!%s",
421 pool_member->nr, VTY_NEWLINE);
422 return CMD_WARNING;
423 }
424
425 /* Get rid of a possibly existing old MGCP client instance first */
426 if (pool_member->client) {
427 mgcp_client_disconnect(pool_member->client);
428 talloc_free(pool_member->client);
429 }
430
431 /* Create a new MGCP client instance with the current config */
432 pool_member->client = mgcp_client_init(pool_member, &pool_member->conf);
433 if (!pool_member->client) {
434 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %u initalization failed\n", pool_member->nr);
435 vty_out(vty, "%% MGCP client initalization failed ('%s')%s", argv[0], VTY_NEWLINE);
436 return CMD_WARNING;
437 }
438
439 /* Set backpointer so that we can detect later that this MGCP client is managed by this pool. */
440 pool_member->client->pool = global_mgcp_client_pool;
441
442 /* Connect client */
443 if (mgcp_client_connect(pool_member->client)) {
444 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %u connect failed at (%s:%u)\n",
445 pool_member->nr, pool_member->conf.remote_addr, pool_member->conf.remote_port);
446 talloc_free(pool_member->client);
447 pool_member->client = NULL;
448 vty_out(vty, "%% MGCP client initalization failed ('%s')%s", argv[0], VTY_NEWLINE);
449 return CMD_WARNING;
450 }
451
452 return CMD_SUCCESS;
453}
454
455DEFUN_ATTR(mgw_block, mgw_block_cmd,
456 "mgw <0-255> block",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200457 MGW_STR "reference number\n" "block MGCP client so that it won't be used for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200458{
459 int nr = atoi(argv[0]);
460 struct mgcp_client_pool_member *pool_member = NULL;
461
462 pool_member = pool_member_by_nr(nr);
463 if (!pool_member) {
464 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
465 return CMD_WARNING;
466 }
467
468 pool_member->blocked = true;
469 return CMD_SUCCESS;
470}
471
472DEFUN_ATTR(mgw_unblock, mgw_unblock_cmd,
473 "mgw <0-255> unblock",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200474 MGW_STR "reference number\n" "unblock MGCP client so that it will be available for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200475{
476 int nr = atoi(argv[0]);
477 struct mgcp_client_pool_member *pool_member = NULL;
478
479 pool_member = pool_member_by_nr(nr);
480 if (!pool_member) {
481 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
482 return CMD_WARNING;
483 }
484
485 pool_member->blocked = false;
486 return CMD_SUCCESS;
487}
488
489DEFUN(mgw_show, mgw_snow_cmd, "show mgw-pool", SHOW_STR "Display information about the MGW-Pool\n")
490{
491 vty_out(vty, "%% MGW-Pool:%s", VTY_NEWLINE);
492 struct mgcp_client_pool_member *pool_member;
493
494 if (llist_empty(&global_mgcp_client_pool->pool) && global_mgcp_client_pool->mgcp_client_single) {
495 vty_out(vty, "%% (pool is empty, single MGCP client will be used)%s", VTY_NEWLINE);
496 return CMD_SUCCESS;
497 } else if (llist_empty(&global_mgcp_client_pool->pool)) {
498 vty_out(vty, "%% (pool is empty)%s", VTY_NEWLINE);
499 return CMD_SUCCESS;
500 }
501
502 llist_for_each_entry(pool_member, &global_mgcp_client_pool->pool, list) {
503 vty_out(vty, "%% MGW %u%s", pool_member->nr, VTY_NEWLINE);
504 vty_out(vty, "%% mgcp-client: %s%s", pool_member->client ? "connected" : "disconnected",
505 VTY_NEWLINE);
506 vty_out(vty, "%% service: %s%s", pool_member->blocked ? "blocked" : "unblocked", VTY_NEWLINE);
507 vty_out(vty, "%% ongoing calls: %u%s", pool_member->refcount, VTY_NEWLINE);
508 }
509 return CMD_SUCCESS;
510}
511
512/*! Set up MGCP client VTY (pooled)
513 * (called once at startup by the application process).
514 * \param[in] parent_node identifier of the parent node on which the mgw node appears.
515 * \param[in] mgw_node identifier that should be used with the newly installed MGW node.
516 * \param[in] indent indentation string to match the indentation in the VTY config
517 * \param[in] pool user provided memory to store the configured MGCP client (MGW) pool. */
518void mgcp_client_pool_vty_init(int parent_node, int mgw_node, const char *indent, struct mgcp_client_pool *pool)
519{
Philipp Maierda3a5752021-08-19 14:24:14 +0200520 /* A pool must be allocated before this function can be called */
521 OSMO_ASSERT(pool);
522
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200523 /* Never allow this function to be called twice on the same pool */
524 OSMO_ASSERT(!pool->vty_indent);
525 OSMO_ASSERT(!pool->vty_node);
526
527 pool->vty_indent = talloc_strdup(pool, indent);
528 OSMO_ASSERT(pool->vty_indent);
529 pool->vty_node = talloc_zero(pool, struct cmd_node);
530 OSMO_ASSERT(pool->vty_node);
531 pool->vty_node->node = mgw_node;
532 pool->vty_node->vtysh = 1;
533 pool->vty_node->prompt = talloc_strdup(pool->vty_node, "%s(config-mgw)# ");
534
535 install_lib_element(parent_node, &cfg_mgw_cmd);
536 install_lib_element(parent_node, &cfg_no_mgw_cmd);
537
538 install_node(pool->vty_node, config_write_pool);
539 vty_init_common(pool, mgw_node);
540
541 install_lib_element(ENABLE_NODE, &mgw_reconnect_cmd);
542 install_lib_element(ENABLE_NODE, &mgw_block_cmd);
543 install_lib_element(ENABLE_NODE, &mgw_unblock_cmd);
544
545 install_lib_element_ve(&mgw_snow_cmd);
546
547 global_mgcp_client_pool = pool;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200548}