blob: 4bf6cd017c3c92416869b932e0adfe141f7b7831 [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 Maierdf9192e2021-09-03 17:50:51 +0200245 if (conf->description)
246 vty_out(vty, "%sdescription %s%s", indent, conf->description, VTY_NEWLINE);
247
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200248 addr = conf->local_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100250 vty_out(vty, "%smgw local-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200251 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200252 port = conf->local_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200253 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100254 vty_out(vty, "%smgw local-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200255 (uint16_t)port, VTY_NEWLINE);
256
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200257 addr = conf->remote_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200258 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100259 vty_out(vty, "%smgw remote-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200260 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200261 port = conf->remote_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200262 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100263 vty_out(vty, "%smgw remote-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200264 (uint16_t)port, VTY_NEWLINE);
265
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200266 if (conf->endpoint_domain_name[0])
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100267 vty_out(vty, "%smgw endpoint-domain %s%s", indent,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200268 conf->endpoint_domain_name, VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100269
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200270 llist_for_each_entry(reset_ep, &conf->reset_epnames, list)
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200271 vty_out(vty, "%smgw reset-endpoint %s%s", indent, reset_ep->name, VTY_NEWLINE);
272
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200273 return CMD_SUCCESS;
274}
275
Philipp Maierd6a7e172021-08-03 16:03:27 +0200276/*! Write out MGCP client config to VTY.
277 * \param[in] vty VTY to which we should print.
278 * \param[in] string used for indentation (e.g. " ").
279 * \returns CMD_SUCCESS on success, CMD_WARNING on error */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200280int mgcp_client_config_write(struct vty *vty, const char *indent)
281{
282 return config_write(vty, indent, global_mgcp_client_conf);
283}
284
285static void vty_init_common(void *talloc_ctx, int node)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200286{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200287 global_mgcp_client_ctx = talloc_ctx;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200288
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700289 install_lib_element(node, &cfg_mgw_local_ip_cmd);
290 install_lib_element(node, &cfg_mgw_local_port_cmd);
291 install_lib_element(node, &cfg_mgw_remote_ip_cmd);
292 install_lib_element(node, &cfg_mgw_remote_port_cmd);
293 install_lib_element(node, &cfg_mgw_endpoint_range_cmd);
294 install_lib_element(node, &cfg_mgw_rtp_bts_base_port_cmd);
295 install_lib_element(node, &cfg_mgw_endpoint_domain_name_cmd);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200296 install_lib_element(node, &cfg_mgw_reset_ep_name_cmd);
297 install_lib_element(node, &cfg_mgw_no_reset_ep_name_cmd);
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100298
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200299 osmo_fsm_vty_add_cmds();
300}
301
Philipp Maierd6a7e172021-08-03 16:03:27 +0200302/*! Set up MGCP client VTY
303 * (called once at startup by the application process).
304 * \param[in] talloc_ctx talloc context to be used by the VTY for allocating memory.
305 * \param[in] node identifier of the node on which the VTY commands should be installed.
306 * \param[in] conf user provided memory to to store the MGCP client configuration data. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200307void mgcp_client_vty_init(void *talloc_ctx, int node, struct mgcp_client_conf *conf)
308{
309 global_mgcp_client_conf = conf;
310
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100311 /* deprecated 'mgcpgw' commands */
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700312 install_lib_element(node, &cfg_mgcpgw_local_ip_cmd);
313 install_lib_element(node, &cfg_mgcpgw_local_port_cmd);
314 install_lib_element(node, &cfg_mgcpgw_remote_ip_cmd);
315 install_lib_element(node, &cfg_mgcpgw_remote_port_cmd);
316 install_lib_element(node, &cfg_mgcpgw_endpoint_range_cmd);
317 install_lib_element(node, &cfg_mgcpgw_rtp_bts_base_port_cmd);
Stefan Sperling29248252018-02-22 18:09:28 +0100318
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200319 vty_init_common(talloc_ctx, node);
320}
321
322static int config_write_pool(struct vty *vty)
323{
324 struct mgcp_client_pool *pool = global_mgcp_client_pool;
325 struct mgcp_client_pool_member *pool_member;
326 unsigned int indent_buf_len = strlen(pool->vty_indent) + 1 + 1;
327 char *indent = talloc_zero_size(vty, indent_buf_len);
328
329 snprintf(indent, indent_buf_len, "%s ", pool->vty_indent);
330
331 llist_for_each_entry(pool_member, &pool->pool, list) {
332 vty_out(vty, "%smgw %u%s", pool->vty_indent, pool_member->nr, VTY_NEWLINE);
333 config_write(vty, indent, &pool_member->conf);
334 }
335
336 talloc_free(indent);
337 return CMD_SUCCESS;
338}
339
340/* Lookup the selected MGCP client config by its reference number */
341static struct mgcp_client_pool_member *pool_member_by_nr(unsigned int nr)
342{
343 struct mgcp_client_pool_member *pool_member = NULL;
344 struct mgcp_client_pool_member *pool_member_tmp;
345
346 llist_for_each_entry(pool_member_tmp, &global_mgcp_client_pool->pool, list) {
347 if (pool_member_tmp->nr == nr) {
348 pool_member = pool_member_tmp;
349 break;
350 }
351 }
352
353 return pool_member;
354}
355
356DEFUN_ATTR(cfg_mgw,
Philipp Maierd55be052021-08-19 14:58:31 +0200357 cfg_mgw_cmd, "mgw <0-255>", "Select a MGCP client config to setup\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200358{
359 int nr = atoi(argv[0]);
360 struct mgcp_client_pool_member *pool_member;
361
362 pool_member = pool_member_by_nr(nr);
363 if (!pool_member) {
364 pool_member = talloc_zero(global_mgcp_client_pool, struct mgcp_client_pool_member);
365 OSMO_ASSERT(pool_member);
366 mgcp_client_conf_init(&pool_member->conf);
367 pool_member->nr = nr;
368 llist_add_tail(&pool_member->list, &global_mgcp_client_pool->pool);
369 }
370
371 vty->index = &pool_member->conf;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200372 vty->index_sub = &pool_member->conf.description;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200373 vty->node = global_mgcp_client_pool->vty_node->node;
374
375 return CMD_SUCCESS;
376}
377
378DEFUN_ATTR(cfg_no_mgw,
379 cfg_no_mgw_cmd,
Philipp Maier74e83d32021-08-20 09:57:27 +0200380 "no mgw <0-255>", NO_STR "Select a MGCP client config to remove\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200381{
382 int nr = atoi(argv[0]);
383 struct mgcp_client_pool_member *pool_member;
384
385 pool_member = pool_member_by_nr(nr);
386 if (!pool_member) {
387 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
388 return CMD_WARNING;
389 }
390
391 /* Make sure that there are no ongoing calls */
392 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200393 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't remove it now!%s",
394 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200395 return CMD_WARNING;
396 }
397
398 llist_del(&pool_member->list);
399 if (pool_member->client) {
400 mgcp_client_disconnect(pool_member->client);
401 talloc_free(pool_member->client);
402 }
403 talloc_free(pool_member);
404
405 return CMD_SUCCESS;
406}
407
408DEFUN_ATTR(mgw_reconnect, mgw_reconnect_cmd,
409 "mgw <0-255> reconnect",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200410 MGW_STR "reference number\n" "reconfigure and reconnect MGCP client\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200411{
412 int nr = atoi(argv[0]);
413 struct mgcp_client_pool_member *pool_member = NULL;
414
415 pool_member = pool_member_by_nr(nr);
416 if (!pool_member) {
417 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
418 return CMD_WARNING;
419 }
420
421 /* Make sure that there are no ongoing calls */
422 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200423 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't reconnect it now!%s",
424 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200425 return CMD_WARNING;
426 }
427
428 /* Get rid of a possibly existing old MGCP client instance first */
429 if (pool_member->client) {
430 mgcp_client_disconnect(pool_member->client);
431 talloc_free(pool_member->client);
432 }
433
434 /* Create a new MGCP client instance with the current config */
435 pool_member->client = mgcp_client_init(pool_member, &pool_member->conf);
436 if (!pool_member->client) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200437 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s initalization failed\n",
438 mgcp_client_pool_member_name(pool_member));
439 vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s",
440 mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200441 return CMD_WARNING;
442 }
443
444 /* Set backpointer so that we can detect later that this MGCP client is managed by this pool. */
445 pool_member->client->pool = global_mgcp_client_pool;
446
447 /* Connect client */
448 if (mgcp_client_connect(pool_member->client)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200449 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s connect failed at (%s:%u)\n",
450 mgcp_client_pool_member_name(pool_member), pool_member->conf.remote_addr,
451 pool_member->conf.remote_port);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200452 talloc_free(pool_member->client);
453 pool_member->client = NULL;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200454 vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s",
455 mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200456 return CMD_WARNING;
457 }
458
459 return CMD_SUCCESS;
460}
461
462DEFUN_ATTR(mgw_block, mgw_block_cmd,
463 "mgw <0-255> block",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200464 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 +0200465{
466 int nr = atoi(argv[0]);
467 struct mgcp_client_pool_member *pool_member = NULL;
468
469 pool_member = pool_member_by_nr(nr);
470 if (!pool_member) {
471 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
472 return CMD_WARNING;
473 }
474
475 pool_member->blocked = true;
476 return CMD_SUCCESS;
477}
478
479DEFUN_ATTR(mgw_unblock, mgw_unblock_cmd,
480 "mgw <0-255> unblock",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200481 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 +0200482{
483 int nr = atoi(argv[0]);
484 struct mgcp_client_pool_member *pool_member = NULL;
485
486 pool_member = pool_member_by_nr(nr);
487 if (!pool_member) {
488 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
489 return CMD_WARNING;
490 }
491
492 pool_member->blocked = false;
493 return CMD_SUCCESS;
494}
495
496DEFUN(mgw_show, mgw_snow_cmd, "show mgw-pool", SHOW_STR "Display information about the MGW-Pool\n")
497{
498 vty_out(vty, "%% MGW-Pool:%s", VTY_NEWLINE);
499 struct mgcp_client_pool_member *pool_member;
500
501 if (llist_empty(&global_mgcp_client_pool->pool) && global_mgcp_client_pool->mgcp_client_single) {
502 vty_out(vty, "%% (pool is empty, single MGCP client will be used)%s", VTY_NEWLINE);
503 return CMD_SUCCESS;
504 } else if (llist_empty(&global_mgcp_client_pool->pool)) {
505 vty_out(vty, "%% (pool is empty)%s", VTY_NEWLINE);
506 return CMD_SUCCESS;
507 }
508
509 llist_for_each_entry(pool_member, &global_mgcp_client_pool->pool, list) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200510 vty_out(vty, "%% MGW %s%s", mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200511 vty_out(vty, "%% mgcp-client: %s%s", pool_member->client ? "connected" : "disconnected",
512 VTY_NEWLINE);
513 vty_out(vty, "%% service: %s%s", pool_member->blocked ? "blocked" : "unblocked", VTY_NEWLINE);
514 vty_out(vty, "%% ongoing calls: %u%s", pool_member->refcount, VTY_NEWLINE);
515 }
516 return CMD_SUCCESS;
517}
518
519/*! Set up MGCP client VTY (pooled)
520 * (called once at startup by the application process).
521 * \param[in] parent_node identifier of the parent node on which the mgw node appears.
522 * \param[in] mgw_node identifier that should be used with the newly installed MGW node.
523 * \param[in] indent indentation string to match the indentation in the VTY config
524 * \param[in] pool user provided memory to store the configured MGCP client (MGW) pool. */
525void mgcp_client_pool_vty_init(int parent_node, int mgw_node, const char *indent, struct mgcp_client_pool *pool)
526{
Philipp Maierda3a5752021-08-19 14:24:14 +0200527 /* A pool must be allocated before this function can be called */
528 OSMO_ASSERT(pool);
529
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200530 /* Never allow this function to be called twice on the same pool */
531 OSMO_ASSERT(!pool->vty_indent);
532 OSMO_ASSERT(!pool->vty_node);
533
534 pool->vty_indent = talloc_strdup(pool, indent);
535 OSMO_ASSERT(pool->vty_indent);
536 pool->vty_node = talloc_zero(pool, struct cmd_node);
537 OSMO_ASSERT(pool->vty_node);
538 pool->vty_node->node = mgw_node;
539 pool->vty_node->vtysh = 1;
540 pool->vty_node->prompt = talloc_strdup(pool->vty_node, "%s(config-mgw)# ");
541
542 install_lib_element(parent_node, &cfg_mgw_cmd);
543 install_lib_element(parent_node, &cfg_no_mgw_cmd);
544
545 install_node(pool->vty_node, config_write_pool);
546 vty_init_common(pool, mgw_node);
547
Philipp Maierdf9192e2021-09-03 17:50:51 +0200548 install_element(mgw_node, &cfg_description_cmd);
549
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200550 install_lib_element(ENABLE_NODE, &mgw_reconnect_cmd);
551 install_lib_element(ENABLE_NODE, &mgw_block_cmd);
552 install_lib_element(ENABLE_NODE, &mgw_unblock_cmd);
553
554 install_lib_element_ve(&mgw_snow_cmd);
555
556 global_mgcp_client_pool = pool;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200557}