Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 1 | /* SNMP-like status interface |
| 2 | * |
| 3 | * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de> |
| 4 | * (C) 2010-2011 by On-Waves |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
Daniel Willmann | 965e8fc | 2011-07-19 12:12:10 +0200 | [diff] [blame] | 25 | #include <inttypes.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <arpa/inet.h> |
| 33 | |
| 34 | #include <netinet/tcp.h> |
| 35 | |
| 36 | #include <sys/fcntl.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/types.h> |
| 40 | |
| 41 | #include <openbsc/control_cmd.h> |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 42 | #include <openbsc/control_if.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 43 | #include <openbsc/debug.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 44 | #include <openbsc/gsm_data.h> |
| 45 | #include <openbsc/ipaccess.h> |
| 46 | #include <openbsc/socket.h> |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 47 | #include <osmocom/abis/subchan_demux.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 48 | |
| 49 | #include <openbsc/abis_rsl.h> |
| 50 | #include <openbsc/abis_nm.h> |
| 51 | |
| 52 | #include <osmocom/core/msgb.h> |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 53 | #include <osmocom/core/rate_ctr.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 54 | #include <osmocom/core/select.h> |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 55 | #include <osmocom/core/statistics.h> |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 56 | #include <osmocom/core/talloc.h> |
| 57 | |
| 58 | #include <osmocom/gsm/tlv.h> |
| 59 | |
| 60 | #include <osmocom/vty/command.h> |
| 61 | #include <osmocom/vty/vector.h> |
| 62 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 63 | #include <osmocom/abis/e1_input.h> |
| 64 | #include <osmocom/abis/ipa.h> |
| 65 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 66 | vector ctrl_node_vec; |
| 67 | |
Daniel Willmann | 95dd9d3 | 2011-05-06 16:38:11 +0200 | [diff] [blame] | 68 | /* Send command to all */ |
| 69 | int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd) |
| 70 | { |
| 71 | struct ctrl_connection *ccon; |
| 72 | int ret = 0; |
| 73 | |
| 74 | llist_for_each_entry(ccon, &ctrl->ccon_list, list_entry) { |
| 75 | if (ccon == cmd->ccon) |
| 76 | continue; |
| 77 | if (ctrl_cmd_send(&ccon->write_queue, cmd)) |
| 78 | ret++; |
| 79 | } |
| 80 | return ret; |
| 81 | } |
| 82 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 83 | int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd) |
| 84 | { |
| 85 | int ret; |
| 86 | struct msgb *msg; |
| 87 | |
| 88 | msg = ctrl_cmd_make(cmd); |
| 89 | if (!msg) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 90 | LOGP(DCTRL, LOGL_ERROR, "Could not generate msg\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL); |
| 95 | ipaccess_prepend_header(msg, IPAC_PROTO_OSMO); |
| 96 | |
| 97 | ret = osmo_wqueue_enqueue(queue, msg); |
| 98 | if (ret != 0) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 99 | LOGP(DCTRL, LOGL_ERROR, "Failed to enqueue the command.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 100 | msgb_free(msg); |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
Daniel Willmann | b9c0264 | 2011-07-22 18:08:31 +0200 | [diff] [blame] | 105 | struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd) |
| 106 | { |
| 107 | struct ctrl_cmd *trap; |
| 108 | |
| 109 | trap = ctrl_cmd_cpy(tall_bsc_ctx, cmd); |
| 110 | if (!trap) |
| 111 | return NULL; |
| 112 | |
| 113 | trap->ccon = cmd->ccon; |
| 114 | trap->type = CTRL_TYPE_TRAP; |
| 115 | return trap; |
| 116 | } |
| 117 | |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 118 | static int get_num(vector vline, int i, long *num) |
| 119 | { |
| 120 | char *token, *tmp; |
| 121 | |
| 122 | if (i >= vector_active(vline)) |
| 123 | return 0; |
| 124 | token = vector_slot(vline, i); |
| 125 | |
| 126 | errno = 0; |
| 127 | if (token[0] == '\0') |
| 128 | return 0; |
| 129 | |
| 130 | *num = strtol(token, &tmp, 10); |
| 131 | if (tmp[0] != '\0' || errno != 0) |
| 132 | return 0; |
| 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 137 | int ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data) |
| 138 | { |
| 139 | char *token, *request; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 140 | long num; |
| 141 | int i, j, ret, node; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 142 | struct gsm_network *gsmnet = data; |
| 143 | |
| 144 | struct gsm_network *net = NULL; |
| 145 | struct gsm_bts *bts = NULL; |
| 146 | struct gsm_bts_trx *trx = NULL; |
| 147 | struct gsm_bts_trx_ts *ts = NULL; |
| 148 | vector vline, cmdvec, cmds_vec; |
| 149 | |
| 150 | ret = CTRL_CMD_ERROR; |
| 151 | cmd->reply = "Someone forgot to fill in the reply."; |
| 152 | cmd->node = NULL; |
| 153 | node = CTRL_NODE_ROOT; |
| 154 | |
| 155 | request = talloc_strdup(tall_bsc_ctx, cmd->variable); |
| 156 | if (!request) |
| 157 | goto err; |
| 158 | |
| 159 | for (i=0;i<strlen(request);i++) { |
| 160 | if (request[i] == '.') |
| 161 | request[i] = ' '; |
| 162 | } |
| 163 | |
| 164 | vline = cmd_make_strvec(request); |
| 165 | talloc_free(request); |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 166 | if (!vline) { |
| 167 | cmd->reply = "cmd_make_strvec failed."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 168 | goto err; |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 169 | } |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 170 | |
| 171 | for (i=0;i<vector_active(vline);i++) { |
| 172 | token = vector_slot(vline, i); |
| 173 | /* TODO: We need to make sure that the following chars are digits |
| 174 | * and/or use strtol to check if number conversion was successful |
| 175 | * Right now something like net.bts_stats will not work */ |
| 176 | if (!strcmp(token, "net")) { |
| 177 | net = gsmnet; |
| 178 | if (!net) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 179 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 180 | cmd->node = net; |
| 181 | node = CTRL_NODE_NET; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 182 | } else if (!strcmp(token, "bts")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 183 | if (!net) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 184 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 185 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 186 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 187 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 188 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 189 | bts = gsm_bts_num(net, num); |
| 190 | if (!bts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 191 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 192 | cmd->node = bts; |
| 193 | node = CTRL_NODE_BTS; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 194 | } else if (!strcmp(token, "trx")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 195 | if (!bts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 196 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 197 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 198 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 199 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 200 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 201 | trx = gsm_bts_trx_num(bts, num); |
| 202 | if (!trx) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 203 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 204 | cmd->node = trx; |
| 205 | node = CTRL_NODE_TRX; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 206 | } else if (!strcmp(token, "ts")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 207 | if (!trx) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 208 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 209 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 210 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 211 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame^] | 212 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 213 | if ((num >= 0) && (num < TRX_NR_TS)) |
| 214 | ts = &trx->ts[num]; |
| 215 | if (!ts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 216 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 217 | cmd->node = ts; |
| 218 | node = CTRL_NODE_TS; |
| 219 | } else { |
| 220 | /* If we're here the rest must be the command */ |
| 221 | cmdvec = vector_init(vector_active(vline)-i); |
| 222 | for (j=i; j<vector_active(vline); j++) { |
| 223 | vector_set(cmdvec, vector_slot(vline, j)); |
| 224 | } |
| 225 | |
| 226 | /* Get the command vector of the right node */ |
| 227 | cmds_vec = vector_lookup(ctrl_node_vec, node); |
| 228 | |
| 229 | if (!cmds_vec) { |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 230 | cmd->reply = "Command not found."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 231 | vector_free(cmdvec); |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data); |
| 236 | |
| 237 | vector_free(cmdvec); |
| 238 | break; |
| 239 | } |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 240 | |
| 241 | if (i+1 == vector_active(vline)) |
| 242 | cmd->reply = "Command not present."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | cmd_free_strvec(vline); |
| 246 | |
| 247 | err: |
| 248 | if (ret == CTRL_CMD_ERROR) |
| 249 | cmd->type = CTRL_TYPE_ERROR; |
| 250 | return ret; |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 251 | |
| 252 | err_missing: |
| 253 | cmd_free_strvec(vline); |
| 254 | cmd->type = CTRL_TYPE_ERROR; |
| 255 | cmd->reply = "Error while resolving object"; |
| 256 | return ret; |
| 257 | err_index: |
| 258 | cmd_free_strvec(vline); |
| 259 | cmd->type = CTRL_TYPE_ERROR; |
| 260 | cmd->reply = "Error while parsing the index."; |
| 261 | return ret; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void control_close_conn(struct ctrl_connection *ccon) |
| 265 | { |
| 266 | close(ccon->write_queue.bfd.fd); |
| 267 | osmo_fd_unregister(&ccon->write_queue.bfd); |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 268 | llist_del(&ccon->list_entry); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 269 | if (ccon->closed_cb) |
| 270 | ccon->closed_cb(ccon); |
| 271 | talloc_free(ccon); |
| 272 | } |
| 273 | |
| 274 | static int handle_control_read(struct osmo_fd * bfd) |
| 275 | { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 276 | int ret = -1; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 277 | struct osmo_wqueue *queue; |
| 278 | struct ctrl_connection *ccon; |
| 279 | struct ipaccess_head *iph; |
| 280 | struct ipaccess_head_ext *iph_ext; |
Alexander Huemer | db55ace | 2011-10-12 00:29:30 +0200 | [diff] [blame] | 281 | struct msgb *msg = NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 282 | struct ctrl_cmd *cmd; |
| 283 | struct ctrl_handle *ctrl = bfd->data; |
| 284 | |
| 285 | queue = container_of(bfd, struct osmo_wqueue, bfd); |
| 286 | ccon = container_of(queue, struct ctrl_connection, write_queue); |
| 287 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 288 | ret = ipa_msg_recv(bfd->fd, &msg); |
| 289 | if (ret <= 0) { |
| 290 | if (ret == 0) |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 291 | LOGP(DCTRL, LOGL_INFO, "The control connection was closed\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 292 | else |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 293 | LOGP(DCTRL, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 294 | |
| 295 | goto err; |
| 296 | } |
| 297 | |
| 298 | if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 299 | LOGP(DCTRL, LOGL_ERROR, "The message is too short.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 300 | goto err; |
| 301 | } |
| 302 | |
| 303 | iph = (struct ipaccess_head *) msg->data; |
| 304 | if (iph->proto != IPAC_PROTO_OSMO) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 305 | LOGP(DCTRL, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 306 | goto err; |
| 307 | } |
| 308 | |
| 309 | iph_ext = (struct ipaccess_head_ext *) iph->data; |
| 310 | if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 311 | LOGP(DCTRL, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | |
| 315 | msg->l2h = iph_ext->data; |
| 316 | |
| 317 | cmd = ctrl_cmd_parse(ccon, msg); |
| 318 | |
| 319 | if (cmd) { |
| 320 | cmd->ccon = ccon; |
| 321 | if (ctrl_cmd_handle(cmd, ctrl->gsmnet) != CTRL_CMD_HANDLED) { |
| 322 | ctrl_cmd_send(queue, cmd); |
| 323 | talloc_free(cmd); |
| 324 | } |
| 325 | } else { |
| 326 | cmd = talloc_zero(ccon, struct ctrl_cmd); |
| 327 | if (!cmd) |
| 328 | goto err; |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 329 | LOGP(DCTRL, LOGL_ERROR, "Command parser error.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 330 | cmd->type = CTRL_TYPE_ERROR; |
| 331 | cmd->id = "err"; |
| 332 | cmd->reply = "Command parser error."; |
| 333 | ctrl_cmd_send(queue, cmd); |
| 334 | talloc_free(cmd); |
| 335 | } |
| 336 | |
| 337 | msgb_free(msg); |
| 338 | return 0; |
| 339 | |
| 340 | err: |
| 341 | control_close_conn(ccon); |
| 342 | msgb_free(msg); |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
| 347 | { |
| 348 | int rc; |
| 349 | |
| 350 | rc = write(bfd->fd, msg->data, msg->len); |
| 351 | if (rc != msg->len) |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 352 | LOGP(DCTRL, LOGL_ERROR, "Failed to write message to the control connection.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 353 | |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | static struct ctrl_connection *ctrl_connection_alloc(void *ctx) |
| 358 | { |
| 359 | struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection); |
| 360 | if (!ccon) |
| 361 | return NULL; |
| 362 | |
| 363 | osmo_wqueue_init(&ccon->write_queue, 100); |
| 364 | /* Error handling here? */ |
| 365 | |
| 366 | INIT_LLIST_HEAD(&ccon->cmds); |
| 367 | return ccon; |
| 368 | } |
| 369 | |
| 370 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 371 | { |
| 372 | int ret, fd, on; |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 373 | struct ctrl_handle *ctrl; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 374 | struct ctrl_connection *ccon; |
| 375 | struct sockaddr_in sa; |
| 376 | socklen_t sa_len = sizeof(sa); |
| 377 | |
| 378 | |
| 379 | if (!(what & BSC_FD_READ)) |
| 380 | return 0; |
| 381 | |
| 382 | fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 383 | if (fd < 0) { |
| 384 | perror("accept"); |
| 385 | return fd; |
| 386 | } |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 387 | LOGP(DCTRL, LOGL_INFO, "accept()ed new control connection from %s\n", |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 388 | inet_ntoa(sa.sin_addr)); |
| 389 | |
| 390 | on = 1; |
| 391 | ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 392 | if (ret != 0) { |
| 393 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 394 | close(fd); |
| 395 | return ret; |
| 396 | } |
| 397 | ccon = ctrl_connection_alloc(listen_bfd->data); |
| 398 | if (!ccon) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 399 | LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 400 | close(fd); |
| 401 | return -1; |
| 402 | } |
| 403 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 404 | ctrl = listen_bfd->data; |
| 405 | ccon->write_queue.bfd.data = ctrl; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 406 | ccon->write_queue.bfd.fd = fd; |
| 407 | ccon->write_queue.bfd.when = BSC_FD_READ; |
| 408 | ccon->write_queue.read_cb = handle_control_read; |
| 409 | ccon->write_queue.write_cb = control_write_cb; |
| 410 | |
| 411 | ret = osmo_fd_register(&ccon->write_queue.bfd); |
| 412 | if (ret < 0) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 413 | LOGP(DCTRL, LOGL_ERROR, "Could not register FD.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 414 | close(ccon->write_queue.bfd.fd); |
| 415 | talloc_free(ccon); |
| 416 | } |
| 417 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 418 | llist_add(&ccon->list_entry, &ctrl->ccon_list); |
| 419 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 423 | static uint64_t get_rate_ctr_value(const struct rate_ctr *ctr, int intv) |
| 424 | { |
| 425 | if (intv >= RATE_CTR_INTV_NUM) |
| 426 | return 0; |
| 427 | |
| 428 | /* Absolute value */ |
| 429 | if (intv == -1) { |
| 430 | return ctr->current; |
| 431 | } else { |
| 432 | return ctr->intv[intv].rate; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | static char *get_all_rate_ctr_in_group(const struct rate_ctr_group *ctrg, int intv) |
| 437 | { |
| 438 | int i; |
| 439 | char *counters = talloc_strdup(tall_bsc_ctx, ""); |
| 440 | if (!counters) |
| 441 | return NULL; |
| 442 | |
| 443 | for (i=0;i<ctrg->desc->num_ctr;i++) { |
Daniel Willmann | 965e8fc | 2011-07-19 12:12:10 +0200 | [diff] [blame] | 444 | counters = talloc_asprintf_append(counters, "\n%s.%u.%s %"PRIu64, |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 445 | ctrg->desc->group_name_prefix, ctrg->idx, |
| 446 | ctrg->desc->ctr_desc[i].name, |
| 447 | get_rate_ctr_value(&ctrg->ctr[i], intv)); |
| 448 | if (!counters) |
| 449 | return NULL; |
| 450 | } |
| 451 | return counters; |
| 452 | } |
| 453 | |
| 454 | static int get_rate_ctr_group(const char *ctr_group, int intv, struct ctrl_cmd *cmd) |
| 455 | { |
| 456 | int i; |
| 457 | char *counters; |
| 458 | struct rate_ctr_group *ctrg; |
| 459 | |
| 460 | cmd->reply = talloc_asprintf(cmd, "All counters in group %s", ctr_group); |
| 461 | if (!cmd->reply) |
| 462 | goto oom; |
| 463 | |
| 464 | for (i=0;;i++) { |
| 465 | ctrg = rate_ctr_get_group_by_name_idx(ctr_group, i); |
| 466 | if (!ctrg) |
| 467 | break; |
| 468 | |
| 469 | counters = get_all_rate_ctr_in_group(ctrg, intv); |
| 470 | if (!counters) |
| 471 | goto oom; |
| 472 | |
| 473 | cmd->reply = talloc_asprintf_append(cmd->reply, "%s", counters); |
| 474 | talloc_free(counters); |
| 475 | if (!cmd->reply) |
| 476 | goto oom; |
| 477 | } |
| 478 | |
| 479 | /* We found no counter group by that name */ |
| 480 | if (i == 0) { |
| 481 | cmd->reply = talloc_asprintf(cmd, "No counter group with name %s.", ctr_group); |
| 482 | return CTRL_CMD_ERROR; |
| 483 | } |
| 484 | |
| 485 | return CTRL_CMD_REPLY; |
| 486 | oom: |
| 487 | cmd->reply = "OOM."; |
| 488 | return CTRL_CMD_ERROR; |
| 489 | } |
| 490 | |
| 491 | static int get_rate_ctr_group_idx(const struct rate_ctr_group *ctrg, int intv, struct ctrl_cmd *cmd) |
| 492 | { |
| 493 | char *counters; |
| 494 | |
| 495 | counters = get_all_rate_ctr_in_group(ctrg, intv); |
| 496 | if (!counters) |
| 497 | goto oom; |
| 498 | |
| 499 | cmd->reply = talloc_asprintf(cmd, "All counters in %s.%u%s", |
| 500 | ctrg->desc->group_name_prefix, ctrg->idx, counters); |
| 501 | talloc_free(counters); |
| 502 | if (!cmd->reply) |
| 503 | goto oom; |
| 504 | |
| 505 | return CTRL_CMD_REPLY; |
| 506 | oom: |
| 507 | cmd->reply = "OOM."; |
| 508 | return CTRL_CMD_ERROR; |
| 509 | } |
| 510 | |
| 511 | /* rate_ctr */ |
| 512 | CTRL_CMD_DEFINE(rate_ctr, "rate_ctr *"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 513 | static int get_rate_ctr(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 514 | { |
| 515 | int intv; |
| 516 | unsigned int idx; |
| 517 | char *ctr_group, *ctr_idx, *ctr_name, *tmp, *dup, *saveptr, *interval; |
| 518 | struct rate_ctr_group *ctrg; |
| 519 | const struct rate_ctr *ctr; |
| 520 | |
| 521 | dup = talloc_strdup(cmd, cmd->variable); |
| 522 | if (!dup) |
| 523 | goto oom; |
| 524 | |
| 525 | /* Skip over possible prefixes (net.) */ |
| 526 | tmp = strstr(dup, "rate_ctr"); |
| 527 | if (!tmp) { |
| 528 | talloc_free(dup); |
| 529 | cmd->reply = "rate_ctr not a token in rate_ctr command!"; |
| 530 | goto err; |
| 531 | } |
| 532 | |
| 533 | strtok_r(tmp, ".", &saveptr); |
| 534 | interval = strtok_r(NULL, ".", &saveptr); |
| 535 | if (!interval) { |
| 536 | talloc_free(dup); |
| 537 | cmd->reply = "Missing interval."; |
| 538 | goto err; |
| 539 | } |
| 540 | |
| 541 | if (!strcmp(interval, "abs")) { |
| 542 | intv = -1; |
| 543 | } else if (!strcmp(interval, "per_sec")) { |
| 544 | intv = RATE_CTR_INTV_SEC; |
| 545 | } else if (!strcmp(interval, "per_min")) { |
| 546 | intv = RATE_CTR_INTV_MIN; |
| 547 | } else if (!strcmp(interval, "per_hour")) { |
| 548 | intv = RATE_CTR_INTV_HOUR; |
| 549 | } else if (!strcmp(interval, "per_day")) { |
| 550 | intv = RATE_CTR_INTV_DAY; |
| 551 | } else { |
| 552 | talloc_free(dup); |
| 553 | cmd->reply = "Wrong interval."; |
| 554 | goto err; |
| 555 | } |
| 556 | |
| 557 | ctr_group = strtok_r(NULL, ".", &saveptr); |
| 558 | tmp = strtok_r(NULL, ".", &saveptr); |
| 559 | if (!ctr_group || !tmp) { |
| 560 | talloc_free(dup); |
| 561 | cmd->reply = "Counter group must be of form a.b"; |
| 562 | goto err; |
| 563 | } |
| 564 | ctr_group[strlen(ctr_group)] = '.'; |
| 565 | |
| 566 | ctr_idx = strtok_r(NULL, ".", &saveptr); |
| 567 | if (!ctr_idx) { |
| 568 | talloc_free(dup); |
| 569 | return get_rate_ctr_group(ctr_group, intv, cmd); |
| 570 | } |
| 571 | idx = atoi(ctr_idx); |
| 572 | |
| 573 | ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx); |
| 574 | if (!ctrg) { |
| 575 | talloc_free(dup); |
| 576 | cmd->reply = "Counter group not found."; |
| 577 | goto err; |
| 578 | } |
| 579 | |
| 580 | ctr_name = strtok_r(NULL, "\0", &saveptr); |
| 581 | if (!ctr_name) { |
| 582 | talloc_free(dup); |
| 583 | return get_rate_ctr_group_idx(ctrg, intv, cmd); |
| 584 | } |
| 585 | |
| 586 | ctr = rate_ctr_get_by_name(ctrg, ctr_name); |
| 587 | if (!ctr) { |
| 588 | cmd->reply = "Counter name not found."; |
| 589 | talloc_free(dup); |
| 590 | goto err; |
| 591 | } |
| 592 | |
| 593 | talloc_free(dup); |
| 594 | |
Holger Hans Peter Freyther | 5a65444 | 2012-03-16 12:18:39 +0100 | [diff] [blame] | 595 | cmd->reply = talloc_asprintf(cmd, "%"PRIu64, get_rate_ctr_value(ctr, intv)); |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 596 | if (!cmd->reply) |
| 597 | goto oom; |
| 598 | |
| 599 | return CTRL_CMD_REPLY; |
| 600 | oom: |
| 601 | cmd->reply = "OOM"; |
| 602 | err: |
| 603 | return CTRL_CMD_ERROR; |
| 604 | } |
| 605 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 606 | static int set_rate_ctr(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 607 | { |
| 608 | cmd->reply = "Can't set rate counter."; |
| 609 | |
| 610 | return CTRL_CMD_ERROR; |
| 611 | } |
| 612 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 613 | static int verify_rate_ctr(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 614 | { |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | /* counter */ |
| 619 | CTRL_CMD_DEFINE(counter, "counter *"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 620 | static int get_counter(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 621 | { |
| 622 | char *ctr_name, *tmp, *dup, *saveptr; |
| 623 | struct osmo_counter *counter; |
| 624 | |
| 625 | cmd->reply = "OOM"; |
| 626 | dup = talloc_strdup(cmd, cmd->variable); |
| 627 | if (!dup) |
| 628 | goto err; |
| 629 | |
| 630 | |
| 631 | tmp = strstr(dup, "counter"); |
| 632 | if (!tmp) { |
| 633 | talloc_free(dup); |
| 634 | goto err; |
| 635 | } |
| 636 | |
| 637 | strtok_r(tmp, ".", &saveptr); |
| 638 | ctr_name = strtok_r(NULL, "\0", &saveptr); |
| 639 | |
| 640 | if (!ctr_name) |
| 641 | goto err; |
| 642 | |
| 643 | counter = osmo_counter_get_by_name(ctr_name); |
| 644 | if (!counter) { |
| 645 | cmd->reply = "Counter name not found."; |
| 646 | talloc_free(dup); |
| 647 | goto err; |
| 648 | } |
| 649 | |
| 650 | talloc_free(dup); |
| 651 | |
| 652 | cmd->reply = talloc_asprintf(cmd, "%lu", counter->value); |
| 653 | if (!cmd->reply) { |
| 654 | cmd->reply = "OOM"; |
| 655 | goto err; |
| 656 | } |
| 657 | |
| 658 | return CTRL_CMD_REPLY; |
| 659 | err: |
| 660 | return CTRL_CMD_ERROR; |
| 661 | } |
| 662 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 663 | static int set_counter(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 664 | { |
| 665 | |
| 666 | cmd->reply = "Can't set counter."; |
| 667 | |
| 668 | return CTRL_CMD_ERROR; |
| 669 | } |
| 670 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 671 | static int verify_counter(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 672 | { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 676 | struct ctrl_handle *controlif_setup(struct gsm_network *gsmnet, uint16_t port) |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 677 | { |
| 678 | int ret; |
| 679 | struct ctrl_handle *ctrl; |
| 680 | |
| 681 | ctrl = talloc_zero(tall_bsc_ctx, struct ctrl_handle); |
| 682 | if (!ctrl) |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 683 | return NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 684 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 685 | INIT_LLIST_HEAD(&ctrl->ccon_list); |
| 686 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 687 | ctrl->gsmnet = gsmnet; |
| 688 | |
| 689 | ctrl_node_vec = vector_init(5); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 690 | if (!ctrl_node_vec) |
| 691 | goto err; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 692 | |
| 693 | /* Listen for control connections */ |
Daniel Willmann | 0d441b6 | 2011-07-29 10:41:24 +0200 | [diff] [blame] | 694 | ret = make_sock(&ctrl->listen_fd, IPPROTO_TCP, INADDR_LOOPBACK, port, |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 695 | 0, listen_fd_cb, ctrl); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 696 | if (ret < 0) |
| 697 | goto err_vec; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 698 | |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 699 | ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr); |
| 700 | if (ret) |
| 701 | goto err_vec; |
| 702 | ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter); |
| 703 | if (ret) |
| 704 | goto err_vec; |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 705 | |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 706 | return ctrl; |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 707 | err_vec: |
| 708 | vector_free(ctrl_node_vec); |
| 709 | ctrl_node_vec = NULL; |
| 710 | err: |
| 711 | talloc_free(ctrl); |
| 712 | return NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 713 | } |