Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1 | /* (C) 2015 by sysmocom s.f.m.c. GmbH |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Neels Hofmeyr |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
Neels Hofmeyr | baaf7a4 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 23 | #include <ares.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
| 27 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 28 | #include <osmocom/core/talloc.h> |
| 29 | #include <osmocom/vty/command.h> |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 30 | #include <osmocom/vty/misc.h> |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 31 | |
| 32 | #include <openbsc/vty.h> |
| 33 | #include <openbsc/gtphub.h> |
| 34 | |
Neels Hofmeyr | baaf7a4 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 35 | /* TODO split GRX ares from sgsn into a separate struct and allow use without |
| 36 | * globals. */ |
| 37 | #include <openbsc/sgsn.h> |
| 38 | extern struct sgsn_instance *sgsn; |
| 39 | |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 40 | static struct gtphub *g_hub = 0; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 41 | static struct gtphub_cfg *g_cfg = 0; |
| 42 | |
| 43 | static struct cmd_node gtphub_node = { |
| 44 | GTPHUB_NODE, |
| 45 | "%s(config-gtphub)# ", |
| 46 | 1, |
| 47 | }; |
| 48 | |
| 49 | #define GTPH_DEFAULT_CONTROL_PORT 2123 |
| 50 | #define GTPH_DEFAULT_USER_PORT 2152 |
| 51 | |
| 52 | static void write_addrs(struct vty *vty, const char *name, |
| 53 | struct gtphub_cfg_addr *c, struct gtphub_cfg_addr *u) |
| 54 | { |
| 55 | if ((c->port == GTPH_DEFAULT_CONTROL_PORT) |
| 56 | && (u->port == GTPH_DEFAULT_USER_PORT) |
| 57 | && (strcmp(c->addr_str, u->addr_str) == 0)) { |
| 58 | /* Default port numbers and same IP address: write "short" |
| 59 | * variant. */ |
| 60 | vty_out(vty, " %s %s%s", |
| 61 | name, |
| 62 | c->addr_str, |
| 63 | VTY_NEWLINE); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | vty_out(vty, " %s ctrl %s %d user %s %d%s", |
| 68 | name, |
| 69 | c->addr_str, (int)c->port, |
| 70 | u->addr_str, (int)u->port, |
| 71 | VTY_NEWLINE); |
Neels Hofmeyr | baaf7a4 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 72 | |
| 73 | struct ares_addr_node *server; |
| 74 | for (server = sgsn->ares_servers; server; server = server->next) |
| 75 | vty_out(vty, " grx-dns-add %s%s", inet_ntoa(server->addr.addr4), VTY_NEWLINE); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int config_write_gtphub(struct vty *vty) |
| 79 | { |
| 80 | vty_out(vty, "gtphub%s", VTY_NEWLINE); |
| 81 | |
| 82 | write_addrs(vty, "bind-to-sgsns", |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 83 | &g_cfg->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].bind, |
| 84 | &g_cfg->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].bind); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 85 | |
| 86 | write_addrs(vty, "bind-to-ggsns", |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 87 | &g_cfg->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].bind, |
| 88 | &g_cfg->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].bind); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 89 | |
Neels Hofmeyr | e5cd246 | 2015-11-29 23:50:45 +0100 | [diff] [blame^] | 90 | if (g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].addr_str) { |
| 91 | write_addrs(vty, "sgsn-proxy", |
| 92 | &g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL], |
| 93 | &g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_USER]); |
| 94 | } |
| 95 | |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 96 | if (g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str) { |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 97 | write_addrs(vty, "ggsn-proxy", |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 98 | &g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL], |
| 99 | &g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_USER]); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return CMD_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | DEFUN(cfg_gtphub, cfg_gtphub_cmd, |
| 106 | "gtphub", |
| 107 | "Configure the GTP hub") |
| 108 | { |
| 109 | vty->node = GTPHUB_NODE; |
| 110 | return CMD_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | #define BIND_ARGS "ctrl ADDR <0-65535> user ADDR <0-65535>" |
| 114 | #define BIND_DOCS \ |
| 115 | "Set GTP-C bind\n" \ |
| 116 | "GTP-C local IP address (v4 or v6)\n" \ |
| 117 | "GTP-C local port\n" \ |
| 118 | "Set GTP-U bind\n" \ |
| 119 | "GTP-U local IP address (v4 or v6)\n" \ |
| 120 | "GTP-U local port\n" |
| 121 | |
| 122 | |
| 123 | DEFUN(cfg_gtphub_bind_to_sgsns_short, cfg_gtphub_bind_to_sgsns_short_cmd, |
| 124 | "bind-to-sgsns ADDR", |
| 125 | "GTP Hub Parameters\n" |
| 126 | "Set the local bind address to listen for SGSNs, for both GTP-C and GTP-U\n" |
| 127 | "Local IP address (v4 or v6)\n" |
| 128 | ) |
| 129 | { |
| 130 | int i; |
Neels Hofmeyr | 27544ac | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 131 | for_each_plane(i) |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 132 | g_cfg->to_gsns[GTPH_SIDE_SGSN][i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 133 | g_cfg->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT; |
| 134 | g_cfg->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 135 | return CMD_SUCCESS; |
| 136 | } |
| 137 | |
| 138 | DEFUN(cfg_gtphub_bind_to_ggsns_short, cfg_gtphub_bind_to_ggsns_short_cmd, |
| 139 | "bind-to-ggsns ADDR", |
| 140 | "GTP Hub Parameters\n" |
| 141 | "Set the local bind address to listen for GGSNs, for both GTP-C and GTP-U\n" |
| 142 | "Local IP address (v4 or v6)\n" |
| 143 | ) |
| 144 | { |
| 145 | int i; |
Neels Hofmeyr | 27544ac | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 146 | for_each_plane(i) |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 147 | g_cfg->to_gsns[GTPH_SIDE_GGSN][i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 148 | g_cfg->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT; |
| 149 | g_cfg->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 150 | return CMD_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static int handle_binds(struct gtphub_cfg_bind *b, const char **argv) |
| 155 | { |
| 156 | b[GTPH_PLANE_CTRL].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 157 | b[GTPH_PLANE_CTRL].bind.port = atoi(argv[1]); |
| 158 | b[GTPH_PLANE_USER].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[2]); |
| 159 | b[GTPH_PLANE_USER].bind.port = atoi(argv[3]); |
| 160 | return CMD_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | DEFUN(cfg_gtphub_bind_to_sgsns, cfg_gtphub_bind_to_sgsns_cmd, |
| 164 | "bind-to-sgsns " BIND_ARGS, |
| 165 | "GTP Hub Parameters\n" |
| 166 | "Set the local bind addresses and ports to listen for SGSNs\n" |
| 167 | BIND_DOCS |
| 168 | ) |
| 169 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 170 | return handle_binds(g_cfg->to_gsns[GTPH_SIDE_SGSN], argv); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | DEFUN(cfg_gtphub_bind_to_ggsns, cfg_gtphub_bind_to_ggsns_cmd, |
| 174 | "bind-to-ggsns " BIND_ARGS, |
| 175 | "GTP Hub Parameters\n" |
| 176 | "Set the local bind addresses and ports to listen for GGSNs\n" |
| 177 | BIND_DOCS |
| 178 | ) |
| 179 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 180 | return handle_binds(g_cfg->to_gsns[GTPH_SIDE_GGSN], argv); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | DEFUN(cfg_gtphub_ggsn_proxy_short, cfg_gtphub_ggsn_proxy_short_cmd, |
| 184 | "ggsn-proxy ADDR", |
| 185 | "GTP Hub Parameters\n" |
| 186 | "Redirect all GGSN bound traffic to default ports on this address (another gtphub)\n" |
| 187 | "Remote IP address (v4 or v6)\n" |
| 188 | ) |
| 189 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 190 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 191 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].port = GTPH_DEFAULT_CONTROL_PORT; |
| 192 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 193 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_USER].port = GTPH_DEFAULT_USER_PORT; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 194 | return CMD_SUCCESS; |
| 195 | } |
| 196 | |
| 197 | DEFUN(cfg_gtphub_ggsn_proxy, cfg_gtphub_ggsn_proxy_cmd, |
| 198 | "ggsn-proxy " BIND_ARGS, |
| 199 | "GTP Hub Parameters\n" |
| 200 | "Redirect all GGSN bound traffic to these addresses and ports (another gtphub)\n" |
| 201 | BIND_DOCS |
| 202 | ) |
| 203 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 204 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 205 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].port = atoi(argv[1]); |
| 206 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[2]); |
| 207 | g_cfg->proxy[GTPH_SIDE_GGSN][GTPH_PLANE_USER].port = atoi(argv[3]); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 208 | return CMD_SUCCESS; |
| 209 | } |
| 210 | |
| 211 | DEFUN(cfg_gtphub_sgsn_proxy_short, cfg_gtphub_sgsn_proxy_short_cmd, |
| 212 | "sgsn-proxy ADDR", |
| 213 | "GTP Hub Parameters\n" |
| 214 | "Redirect all SGSN bound traffic to default ports on this address (another gtphub)\n" |
| 215 | "Remote IP address (v4 or v6)\n" |
| 216 | ) |
| 217 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 218 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 219 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].port = GTPH_DEFAULT_CONTROL_PORT; |
| 220 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 221 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_USER].port = GTPH_DEFAULT_USER_PORT; |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 222 | return CMD_SUCCESS; |
| 223 | } |
| 224 | |
| 225 | DEFUN(cfg_gtphub_sgsn_proxy, cfg_gtphub_sgsn_proxy_cmd, |
| 226 | "sgsn-proxy " BIND_ARGS, |
| 227 | "GTP Hub Parameters\n" |
| 228 | "Redirect all SGSN bound traffic to these addresses and ports (another gtphub)\n" |
| 229 | BIND_DOCS |
| 230 | ) |
| 231 | { |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 232 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]); |
| 233 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].port = atoi(argv[1]); |
| 234 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[2]); |
| 235 | g_cfg->proxy[GTPH_SIDE_SGSN][GTPH_PLANE_USER].port = atoi(argv[3]); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 236 | return CMD_SUCCESS; |
| 237 | } |
| 238 | |
Neels Hofmeyr | baaf7a4 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 239 | |
| 240 | /* Copied from sgsn_vty.h */ |
| 241 | DEFUN(cfg_grx_ggsn, cfg_grx_ggsn_cmd, |
| 242 | "grx-dns-add A.B.C.D", |
| 243 | "Add DNS server\nIPv4 address\n") |
| 244 | { |
| 245 | struct ares_addr_node *node = talloc_zero(tall_bsc_ctx, struct ares_addr_node); |
| 246 | node->family = AF_INET; |
| 247 | inet_aton(argv[0], &node->addr.addr4); |
| 248 | |
| 249 | node->next = sgsn->ares_servers; |
| 250 | sgsn->ares_servers = node; |
| 251 | return CMD_SUCCESS; |
| 252 | } |
| 253 | |
| 254 | |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 255 | /* |
| 256 | (show gtphub all, show gtphub stats, show gtphub teidmap, |
| 257 | show gtphub peers, ...) |
| 258 | */ |
| 259 | |
| 260 | static void show_bind_stats_all(struct vty *vty) |
| 261 | { |
| 262 | int plane_idx; |
Neels Hofmeyr | 27544ac | 2015-11-27 00:05:56 +0100 | [diff] [blame] | 263 | for_each_plane(plane_idx) { |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 264 | vty_out(vty, "- %s Plane:%s", |
| 265 | gtphub_plane_idx_names[plane_idx], VTY_NEWLINE); |
| 266 | |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 267 | struct gtphub_bind *b = &g_hub->to_gsns[GTPH_SIDE_GGSN][plane_idx]; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 268 | vty_out(vty, " - to/from GGSNs: %s port %d%s", |
| 269 | gsn_addr_to_str(&b->local_addr), (int)b->local_port, |
| 270 | VTY_NEWLINE); |
| 271 | vty_out_rate_ctr_group(vty, " ", b->counters_io); |
| 272 | |
Neels Hofmeyr | 5da32ac | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 273 | b = &g_hub->to_gsns[GTPH_SIDE_SGSN][plane_idx]; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 274 | vty_out(vty, " - to/from SGSNs: %s port %d%s", |
| 275 | gsn_addr_to_str(&b->local_addr), (int)b->local_port, |
| 276 | VTY_NEWLINE); |
| 277 | vty_out_rate_ctr_group(vty, " ", b->counters_io); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | static void show_peers_summary(struct vty *vty) |
| 283 | { |
| 284 | int c |
| 285 | int plane_idx; |
| 286 | } |
| 287 | */ |
| 288 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 289 | static void show_tunnels_summary(struct vty *vty) |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 290 | { |
| 291 | time_t now = gtphub_now(); |
| 292 | |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 293 | const int w = 36; |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 294 | int max_expiry = g_hub->expire_slowly.expiry_in_seconds; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 295 | float seconds_per_step = ((float)max_expiry) / w; |
| 296 | |
| 297 | /* Print TEI mapping expiry in an ASCII histogram, like: |
| 298 | TEI map summary |
| 299 | Legend: '_'=0 '.'<=1% ':'<=2% '|'<=10% '#'>10% (10.0 m/step) |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 300 | CTRL: 30 mappings, valid for 360m[# :. | . : . ]1m |
| 301 | USER: 30 mappings, valid for 360m[# :. | . : . ]1m |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 302 | 4 TEI mappings in total, last expiry in 359.4 min |
| 303 | */ |
| 304 | vty_out(vty, |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 305 | "Tunnels summary%s" |
| 306 | " Legend: ' '=0 '.'<=1%% ':'<=2%% '|'<=10%% '#'>10%% (%.1f m/step)%s", |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 307 | VTY_NEWLINE, |
| 308 | seconds_per_step / 60., |
| 309 | VTY_NEWLINE); |
| 310 | |
| 311 | int last_expiry = 0; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 312 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 313 | unsigned int count = 0; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 314 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 315 | int histogram[w]; |
| 316 | memset(histogram, 0, sizeof(histogram)); |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 317 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 318 | struct gtphub_tunnel *t; |
| 319 | llist_for_each_entry(t, &g_hub->tunnels, entry) { |
| 320 | count ++; |
| 321 | int expiry = t->expiry_entry.expiry - now; |
| 322 | last_expiry = (last_expiry > expiry) ? last_expiry : expiry; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 323 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 324 | int hi = ((float)expiry) / seconds_per_step; |
| 325 | if (hi < 0) |
| 326 | hi = 0; |
| 327 | if (hi > (w - 1)) |
| 328 | hi = w - 1; |
| 329 | histogram[hi] ++; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 330 | } |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 331 | |
| 332 | vty_out(vty, |
| 333 | " %u tunnels, valid for %dm[", |
| 334 | count, max_expiry / 60); |
| 335 | |
| 336 | int i; |
| 337 | for (i = w - 1; i >= 0; i--) { |
| 338 | char c; |
| 339 | int val = histogram[i]; |
| 340 | int percent = 100. * val / count; |
| 341 | if (!val) |
| 342 | c = ' '; |
| 343 | else if (percent <= 1) |
| 344 | c = '.'; |
| 345 | else if (percent <= 2) |
| 346 | c = ':'; |
| 347 | else if (percent <= 10) |
| 348 | c = '|'; |
| 349 | else c = '#'; |
| 350 | vty_out(vty, "%c", c); |
| 351 | } |
| 352 | vty_out(vty, "]1m%s", VTY_NEWLINE); |
| 353 | |
| 354 | vty_out(vty, " last expiry in %.1f min%s", |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 355 | ((float)last_expiry) / 60., |
| 356 | VTY_NEWLINE); |
| 357 | } |
| 358 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 359 | static void show_tunnels_all(struct vty *vty) |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 360 | { |
| 361 | time_t now = gtphub_now(); |
| 362 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 363 | vty_out(vty, "All tunnels:%s" |
| 364 | "Legend: SGSN <-> GGSN, with each:%s" |
| 365 | " <IP-Ctrl>[/<IP-User>] (<TEI-Ctrl>=<mapped>/<TEI-User>=<mapped>)%s", |
| 366 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 367 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 368 | unsigned int count = 0; |
| 369 | unsigned int incomplete = 0; |
| 370 | struct gtphub_tunnel *t; |
| 371 | llist_for_each_entry(t, &g_hub->tunnels, entry) { |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 372 | vty_out(vty, |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 373 | "(%4dm) %s%s", |
| 374 | -(int)((t->expiry_entry.expiry - now) / 60), |
| 375 | gtphub_tunnel_str(t), |
| 376 | VTY_NEWLINE); |
| 377 | count ++; |
| 378 | if (!gtphub_tunnel_complete(t)) |
| 379 | incomplete ++; |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 380 | } |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 381 | vty_out(vty, "Total: %u tunnels%s", count, VTY_NEWLINE); |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 384 | DEFUN(show_gtphub_tunnels_summary, show_gtphub_tunnels_summary_cmd, "show gtphub tunnels summary", |
| 385 | SHOW_STR "Summary of all tunnels") |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 386 | { |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 387 | show_tunnels_summary(vty); |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 388 | return CMD_SUCCESS; |
| 389 | } |
| 390 | |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 391 | DEFUN(show_gtphub_tunnels_list, show_gtphub_tunnels_list_cmd, "show gtphub tunnels list", |
| 392 | SHOW_STR "List all tunnels") |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 393 | { |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 394 | show_tunnels_all(vty); |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 395 | return CMD_SUCCESS; |
| 396 | } |
| 397 | |
| 398 | DEFUN(show_gtphub, show_gtphub_cmd, "show gtphub all", |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 399 | SHOW_STR "Display information about the GTP hub") |
| 400 | { |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 401 | show_bind_stats_all(vty); |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 402 | show_tunnels_summary(vty); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 403 | return CMD_SUCCESS; |
| 404 | } |
| 405 | |
| 406 | |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 407 | int gtphub_vty_init(struct gtphub *global_hub, struct gtphub_cfg *global_cfg) |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 408 | { |
Neels Hofmeyr | c5f8fe0 | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 409 | g_hub = global_hub; |
| 410 | g_cfg = global_cfg; |
| 411 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 412 | install_element_ve(&show_gtphub_cmd); |
Neels Hofmeyr | 39da711 | 2015-11-24 13:31:06 +0100 | [diff] [blame] | 413 | install_element_ve(&show_gtphub_tunnels_summary_cmd); |
| 414 | install_element_ve(&show_gtphub_tunnels_list_cmd); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 415 | |
| 416 | install_element(CONFIG_NODE, &cfg_gtphub_cmd); |
| 417 | install_node(>phub_node, config_write_gtphub); |
| 418 | vty_install_default(GTPHUB_NODE); |
| 419 | |
| 420 | install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_sgsns_short_cmd); |
| 421 | install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_sgsns_cmd); |
| 422 | install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_ggsns_short_cmd); |
| 423 | install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_ggsns_cmd); |
| 424 | install_element(GTPHUB_NODE, &cfg_gtphub_ggsn_proxy_short_cmd); |
| 425 | install_element(GTPHUB_NODE, &cfg_gtphub_ggsn_proxy_cmd); |
| 426 | install_element(GTPHUB_NODE, &cfg_gtphub_sgsn_proxy_short_cmd); |
| 427 | install_element(GTPHUB_NODE, &cfg_gtphub_sgsn_proxy_cmd); |
Neels Hofmeyr | baaf7a4 | 2015-11-18 18:11:32 +0100 | [diff] [blame] | 428 | install_element(GTPHUB_NODE, &cfg_grx_ggsn_cmd); |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file) |
| 434 | { |
| 435 | int rc; |
| 436 | |
Neels Hofmeyr | 9f79664 | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 437 | rc = vty_read_config_file(config_file, NULL); |
| 438 | if (rc < 0) { |
| 439 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 440 | return rc; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |