blob: fe6d48bac84e697b2beaabd23c75b73126f10e32 [file] [log] [blame]
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +01001/*! \file tdef_vty.c
2 * Implementation to configure osmo_tdef Tnnn timers from VTY configuration.
3 */
4/* (C) 2018-2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
5 *
6 * Author: Neels Hofmeyr <neels@hofmeyr.de>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
Harald Welte581a34d2019-05-27 23:15:28 +020011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte581a34d2019-05-27 23:15:28 +020018 * GNU General Public License for more details.
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010019 *
Harald Welte581a34d2019-05-27 23:15:28 +020020 * You should have received a copy of the GNU General Public License
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
Harald Welte581a34d2019-05-27 23:15:28 +020023 * SPDX-License-Identifier: GPLv2+
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010024 */
25
26#include <string.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <limits.h>
30
31#include <osmocom/vty/vty.h>
32#include <osmocom/vty/command.h>
33#include <osmocom/vty/tdef_vty.h>
34#include <osmocom/core/tdef.h>
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010035#include <osmocom/core/fsm.h>
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010036
37/*! \addtogroup Tdef_VTY
38 *
39 * VTY API for \ref Tdef.
40 *
41 * @{
42 * \file tdef_vty.c
43 */
44
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010045/*! Parse an argument like "1234", "T1234", "t1234", or "X1234", "x1234", as from OSMO_TDEF_VTY_ARG_T.
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010046 * \param[in] vty VTY context for vty_out() of error messages.
47 * \param[in] tdefs Array of timer definitions to look up T timer.
48 * \param[in] T_str Argument string. It is not validated, expected to be checked by VTY input.
49 * \return the corresponding osmo_tdef entry from the tdefs array, or NULL if no such entry exists.
50 */
51struct osmo_tdef *osmo_tdef_vty_parse_T_arg(struct vty *vty, struct osmo_tdef *tdefs, const char *T_str)
52{
53 long l;
54 int T;
55 struct osmo_tdef *t;
56 char *endptr;
57 const char *T_nr_str;
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010058 int sign = 1;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010059
60 if (!tdefs) {
61 vty_out(vty, "%% Error: no timers found%s", VTY_NEWLINE);
62 return NULL;
63 }
64
65 T_nr_str = T_str;
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010066 if (T_nr_str[0] == 't' || T_nr_str[0] == 'T') {
67 sign = 1;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010068 T_nr_str++;
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010069 } else if (T_nr_str[0] == 'x' || T_nr_str[0] == 'X') {
70 T_nr_str++;
71 sign = -1;
72 }
73
74 /* Make sure to disallow any characters changing the signedness of the parsed int */
75 if (T_nr_str[0] < '0' || T_nr_str[0] > '9') {
76 vty_out(vty, "%% Invalid T timer argument (should be 'T1234' or 'X1234'): '%s'%s", T_str, VTY_NEWLINE);
77 return NULL;
78 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010079
80 errno = 0;
81 l = strtol(T_nr_str, &endptr, 10);
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010082 if (errno || *endptr || l > INT_MAX || l < 0) {
83 vty_out(vty, "%% Invalid T timer argument (should be 'T1234' or 'X1234'): '%s'%s", T_str, VTY_NEWLINE);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010084 return NULL;
85 }
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010086 T = l * sign;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010087
88 t = osmo_tdef_get_entry(tdefs, T);
89 if (!t)
Neels Hofmeyr5734bff2019-02-21 02:27:48 +010090 vty_out(vty, "%% No such timer: " OSMO_T_FMT "%s", OSMO_T_FMT_ARGS(T), VTY_NEWLINE);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010091 return t;
92}
93
94/*! Parse an argument of the form "(0-2147483647|default)", as from OSMO_TDEF_VTY_ARG_VAL.
95 * \param[in] val_arg Argument string (not format checked).
96 * \param[in] default_val Value to return in case of val_arg being "default".
97 * \return Parsed value or default_val.
98 */
99unsigned long osmo_tdef_vty_parse_val_arg(const char *val_arg, unsigned long default_val)
100{
101 if (!strcmp(val_arg, "default"))
102 return default_val;
103 return atoll(val_arg);
104}
105
106/*! Apply a timer configuration from VTY argument strings.
107 * Employ both osmo_tdef_vty_parse_T_arg() and osmo_tdef_vty_parse_val_arg() to configure a T timer in an array of
108 * tdefs. Evaluate two arguments, a "T1234" argument and a "(0-2147483647|default)" argument, as from
109 * OSMO_TDEF_VTY_ARGS. If the T timer given in the first argument is found in tdefs, set it to the value given in the
110 * second argument.
111 * \param[in] vty VTY context for vty_out() of error messages.
112 * \param[in] tdefs Array of timer definitions to look up T timer.
113 * \param[in] args Array of string arguments like { "T1234", "23" }.
114 * \return CMD_SUCCESS, or CMD_WARNING if no such timer is found in tdefs.
115 */
116int osmo_tdef_vty_set_cmd(struct vty *vty, struct osmo_tdef *tdefs, const char **args)
117{
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200118 unsigned long new_val;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100119 const char *T_arg = args[0];
120 const char *val_arg = args[1];
121 struct osmo_tdef *t = osmo_tdef_vty_parse_T_arg(vty, tdefs, T_arg);
122 if (!t)
123 return CMD_WARNING;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200124 new_val = osmo_tdef_vty_parse_val_arg(val_arg, t->default_val);
125
126 if (!osmo_tdef_val_in_range(t, new_val)) {
127 char range_str[64];
128 osmo_tdef_range_str_buf(range_str, sizeof(range_str), t);
129 vty_out(vty, "%% Timer " OSMO_T_FMT " value %lu is out of range %s%s",
130 OSMO_T_FMT_ARGS(t->T), new_val, range_str, VTY_NEWLINE);
131 return CMD_WARNING;
132 }
133 t->val = new_val;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100134 return CMD_SUCCESS;
135}
136
137/*! Output one or all timers to the VTY, as for a VTY command like 'show timer [TNNNN]'.
138 * If T_arg is NULL, print all timers in tdefs to the VTY.
139 * If T_arg is not NULL, employ osmo_tdef_vty_parse_T_arg() to select one timer from tdefs and print only that to the
140 * VTY.
141 * \param[in] vty VTY context for vty_out() of error messages.
142 * \param[in] tdefs Array of timer definitions.
143 * \param[in] T_arg Argument string like "T1234", or NULL.
144 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
145 * \return CMD_SUCCESS, or CMD_WARNING if no such timer is found in tdefs.
146 */
147int osmo_tdef_vty_show_cmd(struct vty *vty, struct osmo_tdef *tdefs, const char *T_arg,
148 const char *prefix_fmt, ...)
149{
150 va_list va;
151 if (T_arg) {
152 struct osmo_tdef *t = osmo_tdef_vty_parse_T_arg(vty, tdefs, T_arg);
153 if (!t)
154 return CMD_WARNING;
155 va_start(va, prefix_fmt);
156 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
157 va_end(va);
158 } else {
159 va_start(va, prefix_fmt);
160 osmo_tdef_vty_out_all_va(vty, tdefs, prefix_fmt, va);
161 va_end(va);
162 }
163 return CMD_SUCCESS;
164}
165
166/*! Write to VTY the current status of one timer.
167 * \param[in] vty VTY context for vty_out().
168 * \param[in] t The timer to print.
169 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable vprintf like arguments.
170 * \param[in] va va_list instance. As always, call va_start() before, and va_end() after this call.
171 */
172void osmo_tdef_vty_out_one_va(struct vty *vty, struct osmo_tdef *t, const char *prefix_fmt, va_list va)
173{
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200174 char range_str[64];
175
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100176 if (!t) {
177 vty_out(vty, "%% Error: no such timer%s", VTY_NEWLINE);
178 return;
179 }
180 if (prefix_fmt)
181 vty_out_va(vty, prefix_fmt, va);
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200182
183 vty_out(vty, OSMO_T_FMT " = %lu", OSMO_T_FMT_ARGS(t->T), t->val);
184 if (t->unit != OSMO_TDEF_CUSTOM)
185 vty_out(vty, " %s", osmo_tdef_unit_name(t->unit));
186
187 vty_out(vty, "\t%s (default: %lu", t->desc, t->default_val);
188 if (t->unit != OSMO_TDEF_CUSTOM)
189 vty_out(vty, " %s", osmo_tdef_unit_name(t->unit));
190
191 if (t->min_val || t->max_val) {
192 osmo_tdef_range_str_buf(range_str, sizeof(range_str), t);
193 vty_out(vty, ", range: %s", range_str);
194 }
195
196 vty_out(vty, ")%s", VTY_NEWLINE);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100197}
198
199/*! Write to VTY the current status of one timer.
200 * \param[in] vty VTY context for vty_out().
201 * \param[in] t The timer to print.
202 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
203 */
204void osmo_tdef_vty_out_one(struct vty *vty, struct osmo_tdef *t, const char *prefix_fmt, ...)
205{
206 va_list va;
207 va_start(va, prefix_fmt);
208 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
209 va_end(va);
210}
211
212/*! Write to VTY the current status of all given timers.
213 * \param[in] vty VTY context for vty_out().
214 * \param[in] tdefs Array of timers to print, ended with a fully zero-initialized entry.
215 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable vprintf like arguments.
216 * \param[in] va va_list instance. As always, call va_start() before, and va_end() after this call.
217 */
218void osmo_tdef_vty_out_all_va(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, va_list va)
219{
220 struct osmo_tdef *t;
221 if (!tdefs) {
222 vty_out(vty, "%% Error: no such timers%s", VTY_NEWLINE);
223 return;
224 }
225 osmo_tdef_for_each(t, tdefs) {
226 va_list va2;
227 va_copy(va2, va);
228 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
229 va_end(va2);
230 }
231}
232
233/*! Write to VTY the current status of all given timers.
234 * \param[in] vty VTY context for vty_out().
235 * \param[in] tdefs Array of timers to print, ended with a fully zero-initialized entry.
236 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
237 */
238void osmo_tdef_vty_out_all(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, ...)
239{
240 va_list va;
241 va_start(va, prefix_fmt);
242 osmo_tdef_vty_out_all_va(vty, tdefs, prefix_fmt, va);
243 va_end(va);
244}
245
246/*! Write current timer configuration arguments to the vty. Skip all entries that reflect their default value.
247 * The passed prefix string must contain both necessary indent and the VTY command the specific implementation is using.
248 * See tdef_vty_test_config_subnode.c and tdef_vty_test_dynamic.c for examples.
249 * \param[in] vty VTY context.
250 * \param[in] tdefs Array of timers to print, ended with a fully zero-initialized entry.
251 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
252 */
253void osmo_tdef_vty_write(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, ...)
254{
255 va_list va;
256 struct osmo_tdef *t;
257 osmo_tdef_for_each(t, tdefs) {
258 if (t->val == t->default_val)
259 continue;
260 if (prefix_fmt && *prefix_fmt) {
261 va_start(va, prefix_fmt);
262 vty_out_va(vty, prefix_fmt, va);
263 va_end(va);
264 }
Neels Hofmeyr5734bff2019-02-21 02:27:48 +0100265 vty_out(vty, OSMO_T_FMT " %lu%s", OSMO_T_FMT_ARGS(t->T), t->val, VTY_NEWLINE);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100266 }
267}
268
269/*! Singleton Tnnn groups definition as set by osmo_tdef_vty_groups_init(). */
270static struct osmo_tdef_group *global_tdef_groups;
271
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100272DEFUN(show_timer, show_timer_cmd, "DYNAMIC", "DYNAMIC")
273 /* show timer [(alpha|beta|gamma)] [TNNNN] */
274{
275 const char *group_arg = argc > 0 ? argv[0] : NULL;
276 const char *T_arg = argc > 1 ? argv[1] : NULL;
277 struct osmo_tdef_group *g;
278
279 /* The argument should be either "tea" or "software", but the VTY also allows partial arguments
280 * like "softw" or "t" (which can also be ambiguous). */
281
282 osmo_tdef_groups_for_each(g, global_tdef_groups) {
Neels Hofmeyrd79ccc62019-03-07 23:08:40 +0100283 if (!group_arg || osmo_str_startswith(g->name, group_arg))
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100284 osmo_tdef_vty_show_cmd(vty, g->tdefs, T_arg, "%s: ", g->name);
285 }
286 return CMD_SUCCESS;
287}
288
289DEFUN(cfg_timer, cfg_timer_cmd, "DYNAMIC", "DYNAMIC")
290 /* show timer [(alpha|beta|gamma)] [TNNNN] [(<0-2147483647>|default)] */
291{
292 const char *group_arg;
293 const char **timer_args;
294 struct osmo_tdef *tdefs = NULL;
295 struct osmo_tdef_group *g = NULL;
296
297 /* If any arguments are missing, redirect to 'show' */
298 if (argc < 3)
299 return show_timer(self, vty, argc, argv);
300
301 /* If all arguments are passed, this is configuring a timer. */
Vadim Yanitskiy54f5f4d2019-02-23 17:19:35 +0700302 group_arg = argv[0];
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100303 timer_args = argv + 1;
304 osmo_tdef_groups_for_each(g, global_tdef_groups) {
305 if (strcmp(g->name, group_arg))
306 continue;
307 if (tdefs) {
308 vty_out(vty, "%% Error: ambiguous timer group match%s", VTY_NEWLINE);
309 return CMD_WARNING;
310 }
311 tdefs = g->tdefs;
312 }
313
314 return osmo_tdef_vty_set_cmd(vty, tdefs, timer_args);
315}
316
317static char *add_group_args(void *talloc_ctx, char *dest)
318{
319 struct osmo_tdef_group *g;
320 osmo_talloc_asprintf(talloc_ctx, dest, "[(");
321 osmo_tdef_groups_for_each(g, global_tdef_groups) {
322 osmo_talloc_asprintf(talloc_ctx, dest, "%s%s",
323 (g == global_tdef_groups) ? "" : "|",
324 g->name);
325 }
326 osmo_talloc_asprintf(talloc_ctx, dest, ")]");
327 return dest;
328}
329
330static char *add_group_docs(void *talloc_ctx, char *dest)
331{
332 struct osmo_tdef_group *g;
333 osmo_tdef_groups_for_each(g, global_tdef_groups) {
334 osmo_talloc_asprintf(talloc_ctx, dest, "%s\n", g->desc);
335 }
336 return dest;
337}
338
339static char *timer_command_string(const char *prefix, const char *suffix)
340{
341 char *dest = NULL;
342 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, "%s ", prefix);
343 dest = add_group_args(tall_vty_cmd_ctx, dest);
344 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, " %s", suffix);
345 return dest;
346}
347
348static char *timer_doc_string(const char *prefix, const char *suffix)
349{
350 char *dest = NULL;
351 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, "%s ", prefix);
352 dest = add_group_docs(tall_vty_cmd_ctx, dest);
353 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, " %s", suffix);
354 return dest;
355}
356
357/*! Convenience implementation for keeping a fixed set of timer groups in a program.
358 * Install a 'timer [(group|names|...)] [TNNN] [(<val>|default)]' command under the given parent_node,
359 * and install a 'show timer...' command on VIEW_NODE and ENABLE_NODE.
360 * For a usage example, see \ref tdef_test_config_root.c.
361 * The given timer definitions group is stored in a global pointer, so this can be done only once per main() scope.
362 * It would also be possible to have distinct timer groups on separate VTY subnodes, with a "manual" implementation, but
363 * not with this API.
Vadim Yanitskiydf4f6082020-01-25 09:49:22 +0700364 * \param[in] parent_cfg_node VTY node at which to add the timer configuration commands, e.g. CONFIG_NODE.
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100365 * \param[in] groups Global timer groups definition.
366 */
Vadim Yanitskiydf4f6082020-01-25 09:49:22 +0700367void osmo_tdef_vty_groups_init(unsigned int parent_cfg_node, struct osmo_tdef_group *groups)
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100368{
369 struct osmo_tdef_group *g;
370 OSMO_ASSERT(!global_tdef_groups);
371 global_tdef_groups = groups;
372
373 osmo_tdef_groups_for_each(g, global_tdef_groups)
374 osmo_tdefs_reset(g->tdefs);
375
376 show_timer_cmd.string = timer_command_string("show timer", OSMO_TDEF_VTY_ARG_T_OPTIONAL);
377 show_timer_cmd.doc = timer_doc_string(SHOW_STR "Show timers\n", OSMO_TDEF_VTY_DOC_T);
378
379 cfg_timer_cmd.string = timer_command_string("timer", OSMO_TDEF_VTY_ARG_SET_OPTIONAL);
380 cfg_timer_cmd.doc = timer_doc_string("Configure or show timers\n", OSMO_TDEF_VTY_DOC_SET);
381
382 install_element_ve(&show_timer_cmd);
Vadim Yanitskiydf4f6082020-01-25 09:49:22 +0700383 install_element(parent_cfg_node, &cfg_timer_cmd);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100384}
385
386/*! Write the global osmo_tdef_group configuration to VTY, as previously passed to osmo_tdef_vty_groups_init().
387 * \param[in] vty VTY context.
388 * \param[in] indent String to print before each line.
389 */
390void osmo_tdef_vty_groups_write(struct vty *vty, const char *indent)
391{
392 struct osmo_tdef_group *g;
393 osmo_tdef_groups_for_each(g, global_tdef_groups)
394 osmo_tdef_vty_write(vty, g->tdefs, "%stimer %s ", indent ? : "", g->name);
395}
396
397/*! @} */