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> |
| 25 | |
Harald Welte | ff397ed | 2011-05-08 10:29:23 +0200 | [diff] [blame^] | 26 | #include <osmocom/core/rate_ctr.h> |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/vty/command.h> |
| 29 | #include <osmocom/vty/logging.h> |
| 30 | #include <osmocom/vty/vty.h> |
Harald Welte | ff397ed | 2011-05-08 10:29:23 +0200 | [diff] [blame^] | 31 | #include <osmocom/vty/misc.h> |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | extern struct bsc_data *bsc; |
| 39 | |
| 40 | static void dump_stats(struct vty *vty, struct mtp_link_set *set) |
| 41 | { |
| 42 | struct mtp_link *link; |
| 43 | |
| 44 | vty_out(vty, "Linkset name: %s opc: %d%s", set->name, set->opc, VTY_NEWLINE); |
| 45 | vty_out_rate_ctr_group(vty, " ", set->ctrg); |
| 46 | |
| 47 | llist_for_each_entry(link, &set->links, entry) { |
| 48 | vty_out(vty, " Link %d%s", link->nr, VTY_NEWLINE); |
| 49 | vty_out_rate_ctr_group(vty, " ", link->ctrg); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | DEFUN(show_stats, show_stats_cmd, |
| 54 | "show statistics", |
| 55 | SHOW_STR "Display Linkset statistics\n") |
| 56 | { |
| 57 | struct mtp_link_set *set; |
| 58 | |
| 59 | llist_for_each_entry(set, &bsc->linksets, entry) |
| 60 | dump_stats(vty, set); |
| 61 | |
| 62 | return CMD_SUCCESS; |
| 63 | } |
| 64 | |
| 65 | static void dump_state(struct vty *vty, struct mtp_link_set *set) |
| 66 | { |
| 67 | struct mtp_link *link; |
| 68 | |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 69 | if (!set->app) { |
| 70 | vty_out(vty, "LinkSet %d not assigned to an application.%s", |
| 71 | set->nr, VTY_NEWLINE); |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 75 | vty_out(vty, "LinkSet for %d/%s is %s, remote sccp is %s.%s", |
| 76 | set->nr, set->name, |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 77 | set->available == 0 ? "not available" : "available", |
| 78 | set->sccp_up == 0? "not established" : "established", |
| 79 | VTY_NEWLINE); |
| 80 | |
| 81 | llist_for_each_entry(link, &set->links, entry) { |
| 82 | if (link->blocked) |
| 83 | vty_out(vty, " Link %d is blocked.%s", |
| 84 | link->nr, VTY_NEWLINE); |
| 85 | else |
| 86 | vty_out(vty, " Link %d is %s.%s", |
| 87 | link->nr, |
| 88 | link->available == 0 ? "not available" : "available", |
| 89 | VTY_NEWLINE); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | DEFUN(show_linksets, show_linksets_cmd, |
| 94 | "show link-sets", |
| 95 | SHOW_STR "Display current state of linksets\n") |
| 96 | { |
| 97 | struct mtp_link_set *set; |
| 98 | |
| 99 | llist_for_each_entry(set, &bsc->linksets, entry) |
| 100 | dump_state(vty, set); |
| 101 | return CMD_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | DEFUN(show_msc, show_msc_cmd, |
| 105 | "show msc", |
| 106 | SHOW_STR "Display the status of the MSC\n") |
| 107 | { |
| 108 | struct msc_connection *msc = msc_connection_num(bsc, 0); |
| 109 | |
| 110 | if (!msc) { |
| 111 | vty_out(vty, "%%No MSC Connection defined in this app.%s", VTY_NEWLINE); |
| 112 | return CMD_WARNING; |
| 113 | } |
| 114 | |
| 115 | vty_out(vty, "MSC link is %s and had %s.%s", |
| 116 | msc->msc_link_down == 0 ? "up" : "down", |
| 117 | msc->first_contact == 1 ? "no contact" : "contact", |
| 118 | VTY_NEWLINE); |
| 119 | return CMD_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | DEFUN(show_slc, show_slc_cmd, |
| 123 | "show link-set <0-100> slc", |
| 124 | SHOW_STR "LinkSet\n" "Linkset nr\n" "SLS to SLC\n") |
| 125 | { |
| 126 | struct mtp_link_set *set = NULL; |
| 127 | int i; |
| 128 | |
| 129 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 130 | |
| 131 | if (!set) { |
| 132 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 133 | return CMD_WARNING; |
| 134 | } |
| 135 | |
| 136 | vty_out(vty, "LinkSet for %s.%s", argv[0], VTY_NEWLINE); |
| 137 | for (i = 0; i < ARRAY_SIZE(set->slc); ++i) { |
| 138 | if (set->slc[i]) |
| 139 | vty_out(vty, " SLC[%.2d] is on link %d.%s", |
| 140 | i, set->slc[i]->nr, VTY_NEWLINE); |
| 141 | else |
| 142 | vty_out(vty, " SLC[%d] is down.%s", |
| 143 | i, VTY_NEWLINE); |
| 144 | } |
| 145 | |
| 146 | return CMD_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | DEFUN(pcap_set, pcap_set_cmd, |
| 150 | "trace-pcap <0-100> NAME FILE", |
| 151 | "Trace to a PCAP file\n" "Linkset nr.\n" |
| 152 | "Trace Linkset\n" "Filename to trace\n") |
| 153 | { |
| 154 | struct mtp_link_set *set = NULL; |
| 155 | |
| 156 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 157 | |
| 158 | if (!set) { |
| 159 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 160 | return CMD_WARNING; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd) |
| 165 | close(set->pcap_fd); |
| 166 | set->pcap_fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, |
| 167 | S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
| 168 | if (set->pcap_fd < 0) { |
| 169 | vty_out(vty, "Failed to open file for writing.%s", VTY_NEWLINE); |
| 170 | return CMD_WARNING; |
| 171 | } |
| 172 | |
| 173 | mtp_pcap_write_header(set->pcap_fd); |
| 174 | return CMD_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | DEFUN(pcap_set_stop, pcap_set_stop_cmd, |
| 178 | "trace-pcap <0-100> NAME stop", |
| 179 | "Trace to a PCAP file\n" "Linkset nr\n" |
| 180 | "Trace Linkset\n" "Stop the tracing\n") |
| 181 | { |
| 182 | struct mtp_link_set *set = NULL; |
| 183 | |
| 184 | set = mtp_link_set_num(bsc, atoi(argv[0])); |
| 185 | |
| 186 | if (!set) { |
| 187 | vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE); |
| 188 | return CMD_WARNING; |
| 189 | } |
| 190 | |
| 191 | if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd) |
| 192 | close(set->pcap_fd); |
| 193 | set->pcap_fd = -1; |
| 194 | return CMD_SUCCESS; |
| 195 | } |
| 196 | |
| 197 | #define FIND_LINK(vty, set_no, nr) ({ \ |
| 198 | struct mtp_link_set *set = NULL; \ |
| 199 | struct mtp_link *link = NULL; \ |
| 200 | set = mtp_link_set_num(bsc, set_no); \ |
| 201 | if (!set) { \ |
| 202 | vty_out(vty, "Unknown Linkset nr %d.%s", set_no, VTY_NEWLINE); \ |
| 203 | return CMD_WARNING; \ |
| 204 | } \ |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 205 | if (!set->app) { \ |
| 206 | vty_out(vty, "Linkset nr %d has no application.%s", \ |
| 207 | set_no, VTY_NEWLINE); \ |
| 208 | } \ |
Holger Hans Peter Freyther | 2917644 | 2011-02-22 16:00:36 +0100 | [diff] [blame] | 209 | link = mtp_link_num(set, nr); \ |
| 210 | if (!link) { \ |
| 211 | vty_out(vty, "Can not find link %d.%s", nr, VTY_NEWLINE); \ |
| 212 | return CMD_WARNING; \ |
| 213 | } \ |
| 214 | link; }) |
| 215 | |
| 216 | #define LINK_STR "Operations on the link\n" \ |
| 217 | "Linkset number\n" \ |
| 218 | "Link number\n" |
| 219 | |
| 220 | DEFUN(lnk_block, lnk_block_cmd, |
| 221 | "link <0-100> <0-15> block", |
| 222 | LINK_STR "Block it\n") |
| 223 | { |
| 224 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 225 | mtp_link_block(link); |
| 226 | return CMD_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | DEFUN(lnk_unblock, lnk_unblock_cmd, |
| 230 | "link <0-100> <0-15> unblock", |
| 231 | LINK_STR "Unblock it\n") |
| 232 | { |
| 233 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 234 | mtp_link_unblock(link); |
| 235 | return CMD_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | DEFUN(lnk_reset, lnk_reset_cmd, |
| 239 | "link <0-100> <0-15> reset", |
| 240 | LINK_STR "Reset it\n") |
| 241 | { |
| 242 | struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1])); |
| 243 | mtp_link_failure(link); |
| 244 | return CMD_SUCCESS; |
| 245 | } |
| 246 | |
| 247 | DEFUN(allow_inject, allow_inject_cmd, |
| 248 | "allow-inject (0|1)", |
| 249 | "Allow to inject messages\n" "Disable\n" "Enable\n") |
| 250 | { |
| 251 | bsc->allow_inject = atoi(argv[0]); |
| 252 | return CMD_SUCCESS; |
| 253 | } |
| 254 | |
| 255 | void cell_vty_init_cmds(void) |
| 256 | { |
| 257 | /* special commands */ |
| 258 | install_element(ENABLE_NODE, &pcap_set_cmd); |
| 259 | install_element(ENABLE_NODE, &pcap_set_stop_cmd); |
| 260 | install_element(ENABLE_NODE, &lnk_block_cmd); |
| 261 | install_element(ENABLE_NODE, &lnk_unblock_cmd); |
| 262 | install_element(ENABLE_NODE, &lnk_reset_cmd); |
| 263 | install_element(ENABLE_NODE, &allow_inject_cmd); |
| 264 | |
| 265 | /* show commands */ |
| 266 | install_element_ve(&show_stats_cmd); |
| 267 | install_element_ve(&show_linksets_cmd); |
| 268 | install_element_ve(&show_slc_cmd); |
| 269 | |
| 270 | install_element_ve(&show_msc_cmd); |
| 271 | } |