Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2016 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
Pau Espin Pedrol | 88955fb | 2023-01-18 18:54:00 +0100 | [diff] [blame] | 22 | #include "config.h" |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 23 | |
| 24 | #include <osmocom/vty/command.h> |
| 25 | #include <osmocom/vty/buffer.h> |
| 26 | #include <osmocom/vty/vty.h> |
| 27 | #include <osmocom/vty/telnet_interface.h> |
| 28 | #include <osmocom/vty/misc.h> |
| 29 | |
| 30 | #include <osmocom/core/fsm.h> |
| 31 | #include <osmocom/core/logging.h> |
| 32 | #include <osmocom/core/linuxlist.h> |
| 33 | |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 34 | /*! \file fsm_vty.c |
| 35 | * Osmocom FSM introspection via VTY. |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 36 | * |
| 37 | * This is code implementing generic VTY access to Osmocom FSMs from |
| 38 | * libosmocore. This means that any application can expose all state |
| 39 | * of all instances of all registered FSM classes by calling a single |
| 40 | * command during startup: \ref osmo_fsm_vty_add_cmds |
| 41 | */ |
| 42 | |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 43 | /* we don't want to add this to a public header file; this is simply |
| 44 | * exported by libosmocore and used by libmsomvty but not for public |
| 45 | * consumption. */ |
| 46 | extern struct llist_head osmo_g_fsms; |
| 47 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 48 | /*! Print information about a FSM [class] to the given VTY |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 49 | * \param vty The VTY to which to print |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 50 | * \param[in] prefix prefix to print at start of each line (typically indenting) |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 51 | * \param[in] fsm The FSM class to print |
| 52 | */ |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 53 | void vty_out_fsm2(struct vty *vty, const char *prefix, struct osmo_fsm *fsm) |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 54 | { |
| 55 | unsigned int i; |
| 56 | const struct value_string *evt_name; |
| 57 | |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 58 | vty_out(vty, "%sFSM Name: '%s', Log Subsys: '%s'%s", prefix, fsm->name, |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 59 | log_category_name(fsm->log_subsys), VTY_NEWLINE); |
| 60 | /* list the events */ |
Stefan Sperling | cc81722 | 2018-02-26 19:02:30 +0100 | [diff] [blame] | 61 | if (fsm->event_names) { |
| 62 | for (evt_name = fsm->event_names; evt_name->str != NULL; evt_name++) { |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 63 | vty_out(vty, "%s Event %02u (0x%08x): '%s'%s", prefix, evt_name->value, |
Harald Welte | b17cc80 | 2022-05-16 22:14:48 +0200 | [diff] [blame] | 64 | (1U << evt_name->value), evt_name->str, VTY_NEWLINE); |
Stefan Sperling | cc81722 | 2018-02-26 19:02:30 +0100 | [diff] [blame] | 65 | } |
| 66 | } else |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 67 | vty_out(vty, "%s No event names are defined for this FSM! Please fix!%s", prefix, VTY_NEWLINE); |
Stefan Sperling | cc81722 | 2018-02-26 19:02:30 +0100 | [diff] [blame] | 68 | |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 69 | /* list the states */ |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 70 | vty_out(vty, "%s Number of States: %u%s", prefix, fsm->num_states, VTY_NEWLINE); |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 71 | for (i = 0; i < fsm->num_states; i++) { |
| 72 | const struct osmo_fsm_state *state = &fsm->states[i]; |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 73 | vty_out(vty, "%s State %-20s InEvtMask: 0x%08x, OutStateMask: 0x%08x%s", prefix, |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 74 | state->name, state->in_event_mask, state->out_state_mask, |
| 75 | VTY_NEWLINE); |
| 76 | } |
| 77 | } |
| 78 | |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 79 | /*! Print information about a FSM [class] to the given VTY |
| 80 | * \param vty The VTY to which to print |
| 81 | * \param[in] fsm The FSM class to print |
| 82 | */ |
| 83 | void vty_out_fsm(struct vty *vty, struct osmo_fsm *fsm) |
| 84 | { |
| 85 | vty_out_fsm2(vty, "", fsm); |
| 86 | } |
| 87 | |
| 88 | /*! Print a FSM instance to the given VTY |
| 89 | * \param vty The VTY to which to print |
| 90 | * \param[in] prefix prefix to print at start of each line (typically indenting) |
| 91 | * \param[in] fsmi The FSM instance to print |
| 92 | */ |
| 93 | void vty_out_fsm_inst2(struct vty *vty, const char *prefix, struct osmo_fsm_inst *fsmi) |
| 94 | { |
| 95 | struct osmo_fsm_inst *child; |
| 96 | |
| 97 | vty_out(vty, "%sFSM Instance Name: '%s', ID: '%s'%s", prefix, |
| 98 | fsmi->name, fsmi->id, VTY_NEWLINE); |
| 99 | vty_out(vty, "%s Log-Level: '%s', State: '%s'%s", prefix, |
| 100 | log_level_str(fsmi->log_level), |
| 101 | osmo_fsm_state_name(fsmi->fsm, fsmi->state), |
| 102 | VTY_NEWLINE); |
| 103 | if (fsmi->T) |
| 104 | vty_out(vty, "%s Timer: %u%s", prefix, fsmi->T, VTY_NEWLINE); |
| 105 | if (fsmi->proc.parent) { |
| 106 | vty_out(vty, "%s Parent: '%s', Term-Event: '%s'%s", prefix, |
| 107 | fsmi->proc.parent->name, |
| 108 | osmo_fsm_event_name(fsmi->proc.parent->fsm, |
| 109 | fsmi->proc.parent_term_event), |
| 110 | VTY_NEWLINE); |
| 111 | } |
| 112 | llist_for_each_entry(child, &fsmi->proc.children, proc.child) { |
| 113 | vty_out(vty, "%s Child: '%s'%s", prefix, child->name, VTY_NEWLINE); |
| 114 | } |
| 115 | } |
| 116 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 117 | /*! Print a FSM instance to the given VTY |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 118 | * \param vty The VTY to which to print |
| 119 | * \param[in] fsmi The FSM instance to print |
| 120 | */ |
| 121 | void vty_out_fsm_inst(struct vty *vty, struct osmo_fsm_inst *fsmi) |
| 122 | { |
Harald Welte | cec4bbe | 2021-01-19 20:55:00 +0100 | [diff] [blame] | 123 | vty_out_fsm_inst2(vty, "", fsmi); |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | #define SH_FSM_STR SHOW_STR "Show information about finite state machines\n" |
| 127 | #define SH_FSMI_STR SHOW_STR "Show information about finite state machine instances\n" |
| 128 | |
| 129 | DEFUN(show_fsms, show_fsms_cmd, |
| 130 | "show fsm all", |
| 131 | SH_FSM_STR |
| 132 | "Display a list of all registered finite state machines\n") |
| 133 | { |
| 134 | struct osmo_fsm *fsm; |
| 135 | |
| 136 | llist_for_each_entry(fsm, &osmo_g_fsms, list) |
| 137 | vty_out_fsm(vty, fsm); |
| 138 | |
| 139 | return CMD_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | DEFUN(show_fsm, show_fsm_cmd, |
| 143 | "show fsm NAME", |
| 144 | SH_FSM_STR |
| 145 | "Display information about a single named finite state machine\n") |
| 146 | { |
| 147 | struct osmo_fsm *fsm; |
| 148 | |
| 149 | fsm = osmo_fsm_find_by_name(argv[0]); |
| 150 | if (!fsm) { |
| 151 | vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s", |
| 152 | argv[0], VTY_NEWLINE); |
| 153 | return CMD_WARNING; |
| 154 | } |
| 155 | |
| 156 | vty_out_fsm(vty, fsm); |
| 157 | |
| 158 | return CMD_SUCCESS; |
| 159 | } |
| 160 | |
Vadim Yanitskiy | fb8cd65 | 2024-05-07 17:40:20 +0200 | [diff] [blame] | 161 | DEFUN(show_fsm_state_graph, show_fsm_state_graph_cmd, |
| 162 | "show fsm-state-graph NAME", |
| 163 | SHOW_STR "Generate a state transition graph (using DOT language)\n" |
| 164 | "FSM name\n") |
| 165 | { |
| 166 | const struct osmo_fsm *fsm; |
| 167 | |
| 168 | fsm = osmo_fsm_find_by_name(argv[0]); |
| 169 | if (!fsm) { |
| 170 | vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s", |
| 171 | argv[0], VTY_NEWLINE); |
| 172 | return CMD_WARNING; |
| 173 | } |
| 174 | |
| 175 | vty_out(vty, "digraph \"%s\" {%s", fsm->name, VTY_NEWLINE); |
| 176 | |
| 177 | for (unsigned int i = 0; i < fsm->num_states; i++) { |
| 178 | const struct osmo_fsm_state *st = &fsm->states[i]; |
| 179 | |
| 180 | vty_out(vty, "\t\"%s\"; # out_state_mask=0x%08x%s", |
| 181 | st->name, st->out_state_mask, VTY_NEWLINE); |
| 182 | |
| 183 | for (unsigned int j = 0; j < sizeof(st->out_state_mask) * 8; j++) { |
| 184 | if (~st->out_state_mask & (1 << j)) |
| 185 | continue; |
| 186 | vty_out(vty, "\t\"%s\" -> \"%s\";%s", |
| 187 | st->name, osmo_fsm_state_name(fsm, j), |
| 188 | VTY_NEWLINE); |
| 189 | } |
| 190 | |
| 191 | vty_out(vty, "%s", VTY_NEWLINE); |
| 192 | } |
| 193 | |
| 194 | vty_out(vty, "}%s", VTY_NEWLINE); |
| 195 | |
| 196 | return CMD_SUCCESS; |
| 197 | } |
| 198 | |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 199 | DEFUN(show_fsm_insts, show_fsm_insts_cmd, |
| 200 | "show fsm-instances all", |
| 201 | SH_FSMI_STR |
| 202 | "Display a list of all FSM instances of all finite state machine") |
| 203 | { |
| 204 | struct osmo_fsm *fsm; |
| 205 | |
| 206 | llist_for_each_entry(fsm, &osmo_g_fsms, list) { |
| 207 | struct osmo_fsm_inst *fsmi; |
| 208 | llist_for_each_entry(fsmi, &fsm->instances, list) |
| 209 | vty_out_fsm_inst(vty, fsmi); |
| 210 | } |
| 211 | |
| 212 | return CMD_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | DEFUN(show_fsm_inst, show_fsm_inst_cmd, |
| 216 | "show fsm-instances NAME", |
| 217 | SH_FSMI_STR |
| 218 | "Display a list of all FSM instances of the named finite state machine") |
| 219 | { |
| 220 | struct osmo_fsm *fsm; |
| 221 | struct osmo_fsm_inst *fsmi; |
| 222 | |
| 223 | fsm = osmo_fsm_find_by_name(argv[0]); |
| 224 | if (!fsm) { |
| 225 | vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s", |
| 226 | argv[0], VTY_NEWLINE); |
| 227 | return CMD_WARNING; |
| 228 | } |
| 229 | |
| 230 | llist_for_each_entry(fsmi, &fsm->instances, list) |
| 231 | vty_out_fsm_inst(vty, fsmi); |
| 232 | |
| 233 | return CMD_SUCCESS; |
| 234 | } |
| 235 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 236 | /*! Install VTY commands for FSM introspection |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 237 | * This installs a couple of VTY commands for introspection of FSM |
| 238 | * classes as well as FSM instances. Call this once from your |
| 239 | * application if you want to support those commands. */ |
| 240 | void osmo_fsm_vty_add_cmds(void) |
| 241 | { |
Stefan Sperling | 218c4de | 2018-02-22 18:16:29 +0100 | [diff] [blame] | 242 | static bool osmo_fsm_vty_cmds_installed; |
| 243 | |
| 244 | /* Make sure FSM commands get installed only once. |
| 245 | * We might be called from libraries or from an application. |
| 246 | * An application might be oblivious to the fact that one or |
| 247 | * more of its libaries are using osmo_fsm. And likewise, |
| 248 | * any given library will not know if another library has |
| 249 | * already installled these commands. */ |
| 250 | if (osmo_fsm_vty_cmds_installed) |
| 251 | return; |
| 252 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 253 | install_lib_element_ve(&show_fsm_cmd); |
| 254 | install_lib_element_ve(&show_fsms_cmd); |
Vadim Yanitskiy | fb8cd65 | 2024-05-07 17:40:20 +0200 | [diff] [blame] | 255 | install_lib_element_ve(&show_fsm_state_graph_cmd); |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 256 | install_lib_element_ve(&show_fsm_inst_cmd); |
| 257 | install_lib_element_ve(&show_fsm_insts_cmd); |
Stefan Sperling | 218c4de | 2018-02-22 18:16:29 +0100 | [diff] [blame] | 258 | osmo_fsm_vty_cmds_installed = true; |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 259 | } |