blob: eb05c3c5971b7dcacf23e9062400eca88bd7e2f4 [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{
118 const char *T_arg = args[0];
119 const char *val_arg = args[1];
120 struct osmo_tdef *t = osmo_tdef_vty_parse_T_arg(vty, tdefs, T_arg);
121 if (!t)
122 return CMD_WARNING;
123 t->val = osmo_tdef_vty_parse_val_arg(val_arg, t->default_val);
124 return CMD_SUCCESS;
125}
126
127/*! Output one or all timers to the VTY, as for a VTY command like 'show timer [TNNNN]'.
128 * If T_arg is NULL, print all timers in tdefs to the VTY.
129 * 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
130 * VTY.
131 * \param[in] vty VTY context for vty_out() of error messages.
132 * \param[in] tdefs Array of timer definitions.
133 * \param[in] T_arg Argument string like "T1234", or NULL.
134 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
135 * \return CMD_SUCCESS, or CMD_WARNING if no such timer is found in tdefs.
136 */
137int osmo_tdef_vty_show_cmd(struct vty *vty, struct osmo_tdef *tdefs, const char *T_arg,
138 const char *prefix_fmt, ...)
139{
140 va_list va;
141 if (T_arg) {
142 struct osmo_tdef *t = osmo_tdef_vty_parse_T_arg(vty, tdefs, T_arg);
143 if (!t)
144 return CMD_WARNING;
145 va_start(va, prefix_fmt);
146 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
147 va_end(va);
148 } else {
149 va_start(va, prefix_fmt);
150 osmo_tdef_vty_out_all_va(vty, tdefs, prefix_fmt, va);
151 va_end(va);
152 }
153 return CMD_SUCCESS;
154}
155
156/*! Write to VTY the current status of one timer.
157 * \param[in] vty VTY context for vty_out().
158 * \param[in] t The timer to print.
159 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable vprintf like arguments.
160 * \param[in] va va_list instance. As always, call va_start() before, and va_end() after this call.
161 */
162void osmo_tdef_vty_out_one_va(struct vty *vty, struct osmo_tdef *t, const char *prefix_fmt, va_list va)
163{
164 if (!t) {
165 vty_out(vty, "%% Error: no such timer%s", VTY_NEWLINE);
166 return;
167 }
168 if (prefix_fmt)
169 vty_out_va(vty, prefix_fmt, va);
Neels Hofmeyr5734bff2019-02-21 02:27:48 +0100170 vty_out(vty, OSMO_T_FMT " = %lu%s%s\t%s (default: %lu%s%s)%s",
171 OSMO_T_FMT_ARGS(t->T), t->val,
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100172 t->unit == OSMO_TDEF_CUSTOM ? "" : " ", t->unit == OSMO_TDEF_CUSTOM ? "" : osmo_tdef_unit_name(t->unit),
173 t->desc, t->default_val,
174 t->unit == OSMO_TDEF_CUSTOM ? "" : " ", t->unit == OSMO_TDEF_CUSTOM ? "" : osmo_tdef_unit_name(t->unit),
175 VTY_NEWLINE);
176}
177
178/*! Write to VTY the current status of one timer.
179 * \param[in] vty VTY context for vty_out().
180 * \param[in] t The timer to print.
181 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
182 */
183void osmo_tdef_vty_out_one(struct vty *vty, struct osmo_tdef *t, const char *prefix_fmt, ...)
184{
185 va_list va;
186 va_start(va, prefix_fmt);
187 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
188 va_end(va);
189}
190
191/*! Write to VTY the current status of all given timers.
192 * \param[in] vty VTY context for vty_out().
193 * \param[in] tdefs Array of timers to print, ended with a fully zero-initialized entry.
194 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable vprintf like arguments.
195 * \param[in] va va_list instance. As always, call va_start() before, and va_end() after this call.
196 */
197void osmo_tdef_vty_out_all_va(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, va_list va)
198{
199 struct osmo_tdef *t;
200 if (!tdefs) {
201 vty_out(vty, "%% Error: no such timers%s", VTY_NEWLINE);
202 return;
203 }
204 osmo_tdef_for_each(t, tdefs) {
205 va_list va2;
206 va_copy(va2, va);
207 osmo_tdef_vty_out_one_va(vty, t, prefix_fmt, va);
208 va_end(va2);
209 }
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 printf like arguments.
216 */
217void osmo_tdef_vty_out_all(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, ...)
218{
219 va_list va;
220 va_start(va, prefix_fmt);
221 osmo_tdef_vty_out_all_va(vty, tdefs, prefix_fmt, va);
222 va_end(va);
223}
224
225/*! Write current timer configuration arguments to the vty. Skip all entries that reflect their default value.
226 * The passed prefix string must contain both necessary indent and the VTY command the specific implementation is using.
227 * See tdef_vty_test_config_subnode.c and tdef_vty_test_dynamic.c for examples.
228 * \param[in] vty VTY context.
229 * \param[in] tdefs Array of timers to print, ended with a fully zero-initialized entry.
230 * \param[in] prefix_fmt Arbitrary string to start each line with, with variable printf like arguments.
231 */
232void osmo_tdef_vty_write(struct vty *vty, struct osmo_tdef *tdefs, const char *prefix_fmt, ...)
233{
234 va_list va;
235 struct osmo_tdef *t;
236 osmo_tdef_for_each(t, tdefs) {
237 if (t->val == t->default_val)
238 continue;
239 if (prefix_fmt && *prefix_fmt) {
240 va_start(va, prefix_fmt);
241 vty_out_va(vty, prefix_fmt, va);
242 va_end(va);
243 }
Neels Hofmeyr5734bff2019-02-21 02:27:48 +0100244 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 +0100245 }
246}
247
248/*! Singleton Tnnn groups definition as set by osmo_tdef_vty_groups_init(). */
249static struct osmo_tdef_group *global_tdef_groups;
250
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100251DEFUN(show_timer, show_timer_cmd, "DYNAMIC", "DYNAMIC")
252 /* show timer [(alpha|beta|gamma)] [TNNNN] */
253{
254 const char *group_arg = argc > 0 ? argv[0] : NULL;
255 const char *T_arg = argc > 1 ? argv[1] : NULL;
256 struct osmo_tdef_group *g;
257
258 /* The argument should be either "tea" or "software", but the VTY also allows partial arguments
259 * like "softw" or "t" (which can also be ambiguous). */
260
261 osmo_tdef_groups_for_each(g, global_tdef_groups) {
Neels Hofmeyrd79ccc62019-03-07 23:08:40 +0100262 if (!group_arg || osmo_str_startswith(g->name, group_arg))
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100263 osmo_tdef_vty_show_cmd(vty, g->tdefs, T_arg, "%s: ", g->name);
264 }
265 return CMD_SUCCESS;
266}
267
268DEFUN(cfg_timer, cfg_timer_cmd, "DYNAMIC", "DYNAMIC")
269 /* show timer [(alpha|beta|gamma)] [TNNNN] [(<0-2147483647>|default)] */
270{
271 const char *group_arg;
272 const char **timer_args;
273 struct osmo_tdef *tdefs = NULL;
274 struct osmo_tdef_group *g = NULL;
275
276 /* If any arguments are missing, redirect to 'show' */
277 if (argc < 3)
278 return show_timer(self, vty, argc, argv);
279
280 /* If all arguments are passed, this is configuring a timer. */
Vadim Yanitskiy54f5f4d2019-02-23 17:19:35 +0700281 group_arg = argv[0];
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100282 timer_args = argv + 1;
283 osmo_tdef_groups_for_each(g, global_tdef_groups) {
284 if (strcmp(g->name, group_arg))
285 continue;
286 if (tdefs) {
287 vty_out(vty, "%% Error: ambiguous timer group match%s", VTY_NEWLINE);
288 return CMD_WARNING;
289 }
290 tdefs = g->tdefs;
291 }
292
293 return osmo_tdef_vty_set_cmd(vty, tdefs, timer_args);
294}
295
296static char *add_group_args(void *talloc_ctx, char *dest)
297{
298 struct osmo_tdef_group *g;
299 osmo_talloc_asprintf(talloc_ctx, dest, "[(");
300 osmo_tdef_groups_for_each(g, global_tdef_groups) {
301 osmo_talloc_asprintf(talloc_ctx, dest, "%s%s",
302 (g == global_tdef_groups) ? "" : "|",
303 g->name);
304 }
305 osmo_talloc_asprintf(talloc_ctx, dest, ")]");
306 return dest;
307}
308
309static char *add_group_docs(void *talloc_ctx, char *dest)
310{
311 struct osmo_tdef_group *g;
312 osmo_tdef_groups_for_each(g, global_tdef_groups) {
313 osmo_talloc_asprintf(talloc_ctx, dest, "%s\n", g->desc);
314 }
315 return dest;
316}
317
318static char *timer_command_string(const char *prefix, const char *suffix)
319{
320 char *dest = NULL;
321 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, "%s ", prefix);
322 dest = add_group_args(tall_vty_cmd_ctx, dest);
323 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, " %s", suffix);
324 return dest;
325}
326
327static char *timer_doc_string(const char *prefix, const char *suffix)
328{
329 char *dest = NULL;
330 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, "%s ", prefix);
331 dest = add_group_docs(tall_vty_cmd_ctx, dest);
332 osmo_talloc_asprintf(tall_vty_cmd_ctx, dest, " %s", suffix);
333 return dest;
334}
335
336/*! Convenience implementation for keeping a fixed set of timer groups in a program.
337 * Install a 'timer [(group|names|...)] [TNNN] [(<val>|default)]' command under the given parent_node,
338 * and install a 'show timer...' command on VIEW_NODE and ENABLE_NODE.
339 * For a usage example, see \ref tdef_test_config_root.c.
340 * The given timer definitions group is stored in a global pointer, so this can be done only once per main() scope.
341 * It would also be possible to have distinct timer groups on separate VTY subnodes, with a "manual" implementation, but
342 * not with this API.
343 * \param[in] parent_node VTY node id at which to add the timer group commands, e.g. CONFIG_NODE.
344 * \param[in] groups Global timer groups definition.
345 */
346void osmo_tdef_vty_groups_init(enum node_type parent_node, struct osmo_tdef_group *groups)
347{
348 struct osmo_tdef_group *g;
349 OSMO_ASSERT(!global_tdef_groups);
350 global_tdef_groups = groups;
351
352 osmo_tdef_groups_for_each(g, global_tdef_groups)
353 osmo_tdefs_reset(g->tdefs);
354
355 show_timer_cmd.string = timer_command_string("show timer", OSMO_TDEF_VTY_ARG_T_OPTIONAL);
356 show_timer_cmd.doc = timer_doc_string(SHOW_STR "Show timers\n", OSMO_TDEF_VTY_DOC_T);
357
358 cfg_timer_cmd.string = timer_command_string("timer", OSMO_TDEF_VTY_ARG_SET_OPTIONAL);
359 cfg_timer_cmd.doc = timer_doc_string("Configure or show timers\n", OSMO_TDEF_VTY_DOC_SET);
360
361 install_element_ve(&show_timer_cmd);
362 install_element(parent_node, &cfg_timer_cmd);
363}
364
365/*! Write the global osmo_tdef_group configuration to VTY, as previously passed to osmo_tdef_vty_groups_init().
366 * \param[in] vty VTY context.
367 * \param[in] indent String to print before each line.
368 */
369void osmo_tdef_vty_groups_write(struct vty *vty, const char *indent)
370{
371 struct osmo_tdef_group *g;
372 osmo_tdef_groups_for_each(g, global_tdef_groups)
373 osmo_tdef_vty_write(vty, g->tdefs, "%stimer %s ", indent ? : "", g->name);
374}
375
376/*! @} */