Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 1 | /* VTY code for the Cellmgr */ |
| 2 | /* |
| 3 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * (C) 2010-2011 by On-Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 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 Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <bsc_data.h> |
| 23 | #include <mtp_pcap.h> |
| 24 | #include <msc_connection.h> |
Holger Hans Peter Freyther | ab79b9b | 2011-08-10 06:11:39 +0200 | [diff] [blame] | 25 | #include <sctp_m2ua.h> |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 26 | |
Harald Welte | ff397ed | 2011-05-08 10:29:23 +0200 | [diff] [blame] | 27 | #include <osmocom/core/rate_ctr.h> |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 28 | |
| 29 | #include <osmocom/vty/command.h> |
| 30 | #include <osmocom/vty/logging.h> |
| 31 | #include <osmocom/vty/vty.h> |
Harald Welte | ff397ed | 2011-05-08 10:29:23 +0200 | [diff] [blame] | 32 | #include <osmocom/vty/misc.h> |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 33 | |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | extern struct bsc_data *bsc; |
| 40 | |
| 41 | static void dump_stats(struct vty *vty, struct mtp_link_set *set) |
| 42 | { |
| 43 | struct mtp_link *link; |
| 44 | |
| 45 | vty_out(vty, "Linkset name: %s opc: %d%s", set->name, set->opc, VTY_NEWLINE); |
| 46 | vty_out_rate_ctr_group(vty, " ", set->ctrg); |
| 47 | |
| 48 | llist_for_each_entry(link, &set->links, entry) { |
| 49 | vty_out(vty, " Link %d%s", link->nr, VTY_NEWLINE); |
| 50 | vty_out_rate_ctr_group(vty, " ", link->ctrg); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | DEFUN(show_stats, show_stats_cmd, |
| 55 | "show statistics", |
| 56 | SHOW_STR "Display Linkset statistics\n") |
| 57 | { |
| 58 | struct mtp_link_set *set; |
| 59 | |
| 60 | llist_for_each_entry(set, &bsc->linksets, entry) |
| 61 | dump_stats(vty, set); |
| 62 | |
| 63 | return CMD_SUCCESS; |
| 64 | } |
| 65 | |
| 66 | static void dump_state(struct vty *vty, struct mtp_link_set *set) |
| 67 | { |
| 68 | struct mtp_link *link; |
| 69 | |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 70 | if (!set->app) { |
| 71 | vty_out(vty, "LinkSet %d not assigned to an application.%s", |
| 72 | set->nr, VTY_NEWLINE); |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 76 | vty_out(vty, "LinkSet for %d/%s is %s, remote sccp is %s.%s", |
| 77 | set->nr, set->name, |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 78 | set->available == 0 ? "not available" : "available", |
| 79 | set->sccp_up == 0? "not established" : "established", |
| 80 | VTY_NEWLINE); |
| 81 | |
| 82 | llist_for_each_entry(link, &set->links, entry) { |
| 83 | if (link->blocked) |
| 84 | vty_out(vty, " Link %d is blocked.%s", |
| 85 | link->nr, VTY_NEWLINE); |
| 86 | else |
| 87 | vty_out(vty, " Link %d is %s.%s", |
| 88 | link->nr, |
| 89 | link->available == 0 ? "not available" : "available", |
| 90 | VTY_NEWLINE); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | DEFUN(show_linksets, show_linksets_cmd, |
| 95 | "show link-sets", |
| 96 | SHOW_STR "Display current state of linksets\n") |
| 97 | { |
| 98 | struct mtp_link_set *set; |
| 99 | |
| 100 | llist_for_each_entry(set, &bsc->linksets, entry) |
| 101 | dump_state(vty, set); |
| 102 | return CMD_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | DEFUN(show_msc, show_msc_cmd, |
| 106 | "show msc", |
| 107 | SHOW_STR "Display the status of the MSC\n") |
| 108 | { |
| 109 | struct msc_connection *msc = msc_connection_num(bsc, 0); |
| 110 | |
| 111 | if (!msc) { |
| 112 | vty_out(vty, "%%No MSC Connection defined in this app.%s", VTY_NEWLINE); |
| 113 | return CMD_WARNING; |
| 114 | } |
| 115 | |
| 116 | vty_out(vty, "MSC link is %s and had %s.%s", |
| 117 | msc->msc_link_down == 0 ? "up" : "down", |
| 118 | msc->first_contact == 1 ? "no contact" : "contact", |
| 119 | VTY_NEWLINE); |
| 120 | return CMD_SUCCESS; |
| 121 | } |
| 122 | |
| 123 | DEFUN(show_slc, show_slc_cmd, |
| 124 | "show link-set <0-100> slc", |
| 125 | SHOW_STR "LinkSet\n" "Linkset nr\n" "SLS to SLC\n") |
| 126 | { |
| 127 | struct mtp_link_set *set = NULL; |
| 128 | int i; |
| 129 | |
| 130 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 131 | |
| 132 | if (!set) { |
| 133 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 134 | return CMD_WARNING; |
| 135 | } |
| 136 | |
| 137 | vty_out(vty, "LinkSet for %s.%s", argv[0], VTY_NEWLINE); |
| 138 | for (i = 0; i < ARRAY_SIZE(set->slc); ++i) { |
| 139 | if (set->slc[i]) |
| 140 | vty_out(vty, " SLC[%.2d] is on link %d.%s", |
| 141 | i, set->slc[i]->nr, VTY_NEWLINE); |
| 142 | else |
| 143 | vty_out(vty, " SLC[%d] is down.%s", |
| 144 | i, VTY_NEWLINE); |
| 145 | } |
| 146 | |
| 147 | return CMD_SUCCESS; |
| 148 | } |
| 149 | |
| 150 | DEFUN(pcap_set, pcap_set_cmd, |
| 151 | "trace-pcap <0-100> NAME FILE", |
| 152 | "Trace to a PCAP file\n" "Linkset nr.\n" |
| 153 | "Trace Linkset\n" "Filename to trace\n") |
| 154 | { |
| 155 | struct mtp_link_set *set = NULL; |
| 156 | |
| 157 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 158 | |
| 159 | if (!set) { |
| 160 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 161 | return CMD_WARNING; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd) |
| 166 | close(set->pcap_fd); |
| 167 | set->pcap_fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, |
| 168 | S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
| 169 | if (set->pcap_fd < 0) { |
| 170 | vty_out(vty, "Failed to open file for writing.%s", VTY_NEWLINE); |
| 171 | return CMD_WARNING; |
| 172 | } |
| 173 | |
| 174 | mtp_pcap_write_header(set->pcap_fd); |
| 175 | return CMD_SUCCESS; |
| 176 | } |
| 177 | |
| 178 | DEFUN(pcap_set_stop, pcap_set_stop_cmd, |
| 179 | "trace-pcap <0-100> NAME stop", |
| 180 | "Trace to a PCAP file\n" "Linkset nr\n" |
| 181 | "Trace Linkset\n" "Stop the tracing\n") |
| 182 | { |
| 183 | struct mtp_link_set *set = NULL; |
| 184 | |
| 185 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 186 | |
| 187 | if (!set) { |
| 188 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 189 | return CMD_WARNING; |
| 190 | } |
| 191 | |
| 192 | if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd) |
| 193 | close(set->pcap_fd); |
| 194 | set->pcap_fd = -1; |
| 195 | return CMD_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | #define FIND_LINK(vty, set_no, nr) ({ \ |
| 199 | struct mtp_link_set *set = NULL; \ |
| 200 | struct mtp_link *link = NULL; \ |
| 201 | set = mtp_link_set_num(bsc, set_no); \ |
| 202 | if (!set) { \ |
| 203 | vty_out(vty, "Unknown Linkset nr %d.%s", set_no, VTY_NEWLINE); \ |
| 204 | return CMD_WARNING; \ |
| 205 | } \ |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 206 | if (!set->app) { \ |
| 207 | vty_out(vty, "Linkset nr %d has no application.%s", \ |
| 208 | set_no, VTY_NEWLINE); \ |
| 209 | } \ |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 210 | link = mtp_link_num(set, nr); \ |
| 211 | if (!link) { \ |
| 212 | vty_out(vty, "Can not find link %d.%s", nr, VTY_NEWLINE); \ |
| 213 | return CMD_WARNING; \ |
| 214 | } \ |
| 215 | link; }) |
| 216 | |
| 217 | #define LINK_STR "Operations on the link\n" \ |
| 218 | "Linkset number\n" \ |
| 219 | "Link number\n" |
| 220 | |
| 221 | DEFUN(lnk_block, lnk_block_cmd, |
| 222 | "link <0-100> <0-15> block", |
| 223 | LINK_STR "Block it\n") |
| 224 | { |
| 225 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 226 | mtp_link_block(link); |
| 227 | return CMD_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | DEFUN(lnk_unblock, lnk_unblock_cmd, |
| 231 | "link <0-100> <0-15> unblock", |
| 232 | LINK_STR "Unblock it\n") |
| 233 | { |
| 234 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 235 | mtp_link_unblock(link); |
| 236 | return CMD_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | DEFUN(lnk_reset, lnk_reset_cmd, |
| 240 | "link <0-100> <0-15> reset", |
| 241 | LINK_STR "Reset it\n") |
| 242 | { |
| 243 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 244 | mtp_link_failure(link); |
| 245 | return CMD_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | DEFUN(allow_inject, allow_inject_cmd, |
| 249 | "allow-inject (0|1)", |
| 250 | "Allow to inject messages\n" "Disable\n" "Enable\n") |
| 251 | { |
| 252 | bsc->allow_inject = atoi(argv[0]); |
| 253 | return CMD_SUCCESS; |
| 254 | } |
| 255 | |
Holger Hans Peter Freyther | cf60a01 | 2011-08-10 06:26:44 +0200 | [diff] [blame^] | 256 | DEFUN(show_sctp_count, show_sctp_count_cmd, |
| 257 | "show sctp-connections count", |
| 258 | SHOW_STR "Number of SCTP connections\n") |
Holger Hans Peter Freyther | ab79b9b | 2011-08-10 06:11:39 +0200 | [diff] [blame] | 259 | { |
| 260 | int count = sctp_m2ua_conn_count(bsc->m2ua_trans); |
| 261 | vty_out(vty, "Active SCTP connections are: %d.%s", count, VTY_NEWLINE); |
| 262 | return CMD_SUCCESS; |
| 263 | } |
| 264 | |
Holger Hans Peter Freyther | cf60a01 | 2011-08-10 06:26:44 +0200 | [diff] [blame^] | 265 | DEFUN(show_sctp_details, show_sctp_details_cmd, |
| 266 | "show sctp-connections details", |
| 267 | SHOW_STR "Details of SCTP connections\n") |
| 268 | { |
| 269 | struct sctp_m2ua_conn *conn; |
| 270 | |
| 271 | llist_for_each_entry(conn, &bsc->m2ua_trans->conns, entry) { |
| 272 | vty_out(vty, |
| 273 | "SCTP Conn ASP UP: %d, ident: %d,%d,%d,%d fd: %d ptr: %p.%s", |
| 274 | conn->asp_up, conn->asp_ident[0], conn->asp_ident[1], |
| 275 | conn->asp_ident[2], conn->asp_ident[3], |
| 276 | conn->queue.bfd.fd, conn, VTY_NEWLINE); |
| 277 | } |
| 278 | |
| 279 | return CMD_WARNING; |
| 280 | } |
| 281 | |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 282 | void cell_vty_init_cmds(void) |
| 283 | { |
| 284 | /* special commands */ |
| 285 | install_element(ENABLE_NODE, &pcap_set_cmd); |
| 286 | install_element(ENABLE_NODE, &pcap_set_stop_cmd); |
| 287 | install_element(ENABLE_NODE, &lnk_block_cmd); |
| 288 | install_element(ENABLE_NODE, &lnk_unblock_cmd); |
| 289 | install_element(ENABLE_NODE, &lnk_reset_cmd); |
| 290 | install_element(ENABLE_NODE, &allow_inject_cmd); |
| 291 | |
| 292 | /* show commands */ |
| 293 | install_element_ve(&show_stats_cmd); |
| 294 | install_element_ve(&show_linksets_cmd); |
| 295 | install_element_ve(&show_slc_cmd); |
| 296 | |
| 297 | install_element_ve(&show_msc_cmd); |
Holger Hans Peter Freyther | cf60a01 | 2011-08-10 06:26:44 +0200 | [diff] [blame^] | 298 | install_element_ve(&show_sctp_count_cmd); |
| 299 | install_element_ve(&show_sctp_details_cmd); |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 300 | } |