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 | |
Daniel Willmann | bcc2a1c | 2011-08-25 16:37:45 +0200 | [diff] [blame^] | 143 | struct gsm_network *net = data; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 144 | struct gsm_bts *bts = NULL; |
| 145 | struct gsm_bts_trx *trx = NULL; |
| 146 | struct gsm_bts_trx_ts *ts = NULL; |
| 147 | vector vline, cmdvec, cmds_vec; |
| 148 | |
| 149 | ret = CTRL_CMD_ERROR; |
| 150 | cmd->reply = "Someone forgot to fill in the reply."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 151 | node = CTRL_NODE_ROOT; |
Daniel Willmann | bcc2a1c | 2011-08-25 16:37:45 +0200 | [diff] [blame^] | 152 | cmd->node = net; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 153 | |
| 154 | request = talloc_strdup(tall_bsc_ctx, cmd->variable); |
| 155 | if (!request) |
| 156 | goto err; |
| 157 | |
| 158 | for (i=0;i<strlen(request);i++) { |
| 159 | if (request[i] == '.') |
| 160 | request[i] = ' '; |
| 161 | } |
| 162 | |
| 163 | vline = cmd_make_strvec(request); |
| 164 | talloc_free(request); |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 165 | if (!vline) { |
| 166 | cmd->reply = "cmd_make_strvec failed."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 167 | goto err; |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 168 | } |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 169 | |
| 170 | for (i=0;i<vector_active(vline);i++) { |
| 171 | token = vector_slot(vline, i); |
| 172 | /* TODO: We need to make sure that the following chars are digits |
| 173 | * and/or use strtol to check if number conversion was successful |
| 174 | * Right now something like net.bts_stats will not work */ |
Daniel Willmann | bcc2a1c | 2011-08-25 16:37:45 +0200 | [diff] [blame^] | 175 | if (!strcmp(token, "bts")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 176 | if (!net) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 177 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 178 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 179 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 180 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 181 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 182 | bts = gsm_bts_num(net, num); |
| 183 | if (!bts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 184 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 185 | cmd->node = bts; |
| 186 | node = CTRL_NODE_BTS; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 187 | } else if (!strcmp(token, "trx")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 188 | if (!bts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 189 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 190 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 191 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 192 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 193 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 194 | trx = gsm_bts_trx_num(bts, num); |
| 195 | if (!trx) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 196 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 197 | cmd->node = trx; |
| 198 | node = CTRL_NODE_TRX; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 199 | } else if (!strcmp(token, "ts")) { |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 200 | if (!trx) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 201 | goto err_missing; |
Daniel Willmann | f760889 | 2011-08-18 13:44:49 +0200 | [diff] [blame] | 202 | i++; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 203 | if (!get_num(vline, i, &num)) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 204 | goto err_index; |
Daniel Willmann | 77f808b | 2011-08-18 15:22:01 +0200 | [diff] [blame] | 205 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 206 | if ((num >= 0) && (num < TRX_NR_TS)) |
| 207 | ts = &trx->ts[num]; |
| 208 | if (!ts) |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 209 | goto err_missing; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 210 | cmd->node = ts; |
| 211 | node = CTRL_NODE_TS; |
| 212 | } else { |
| 213 | /* If we're here the rest must be the command */ |
| 214 | cmdvec = vector_init(vector_active(vline)-i); |
| 215 | for (j=i; j<vector_active(vline); j++) { |
| 216 | vector_set(cmdvec, vector_slot(vline, j)); |
| 217 | } |
| 218 | |
| 219 | /* Get the command vector of the right node */ |
| 220 | cmds_vec = vector_lookup(ctrl_node_vec, node); |
| 221 | |
| 222 | if (!cmds_vec) { |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 223 | cmd->reply = "Command not found."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 224 | vector_free(cmdvec); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data); |
| 229 | |
| 230 | vector_free(cmdvec); |
| 231 | break; |
| 232 | } |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 233 | |
| 234 | if (i+1 == vector_active(vline)) |
| 235 | cmd->reply = "Command not present."; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | cmd_free_strvec(vline); |
| 239 | |
| 240 | err: |
| 241 | if (ret == CTRL_CMD_ERROR) |
| 242 | cmd->type = CTRL_TYPE_ERROR; |
| 243 | return ret; |
Daniel Willmann | 7b5ef01 | 2011-08-18 13:48:26 +0200 | [diff] [blame] | 244 | |
| 245 | err_missing: |
| 246 | cmd_free_strvec(vline); |
| 247 | cmd->type = CTRL_TYPE_ERROR; |
| 248 | cmd->reply = "Error while resolving object"; |
| 249 | return ret; |
| 250 | err_index: |
| 251 | cmd_free_strvec(vline); |
| 252 | cmd->type = CTRL_TYPE_ERROR; |
| 253 | cmd->reply = "Error while parsing the index."; |
| 254 | return ret; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void control_close_conn(struct ctrl_connection *ccon) |
| 258 | { |
| 259 | close(ccon->write_queue.bfd.fd); |
| 260 | osmo_fd_unregister(&ccon->write_queue.bfd); |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 261 | llist_del(&ccon->list_entry); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 262 | if (ccon->closed_cb) |
| 263 | ccon->closed_cb(ccon); |
| 264 | talloc_free(ccon); |
| 265 | } |
| 266 | |
| 267 | static int handle_control_read(struct osmo_fd * bfd) |
| 268 | { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 269 | int ret = -1; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 270 | struct osmo_wqueue *queue; |
| 271 | struct ctrl_connection *ccon; |
| 272 | struct ipaccess_head *iph; |
| 273 | struct ipaccess_head_ext *iph_ext; |
Alexander Huemer | db55ace | 2011-10-12 00:29:30 +0200 | [diff] [blame] | 274 | struct msgb *msg = NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 275 | struct ctrl_cmd *cmd; |
| 276 | struct ctrl_handle *ctrl = bfd->data; |
| 277 | |
| 278 | queue = container_of(bfd, struct osmo_wqueue, bfd); |
| 279 | ccon = container_of(queue, struct ctrl_connection, write_queue); |
| 280 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 281 | ret = ipa_msg_recv(bfd->fd, &msg); |
| 282 | if (ret <= 0) { |
| 283 | if (ret == 0) |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 284 | LOGP(DCTRL, LOGL_INFO, "The control connection was closed\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 285 | else |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 286 | 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] | 287 | |
| 288 | goto err; |
| 289 | } |
| 290 | |
| 291 | if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 292 | LOGP(DCTRL, LOGL_ERROR, "The message is too short.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 293 | goto err; |
| 294 | } |
| 295 | |
| 296 | iph = (struct ipaccess_head *) msg->data; |
| 297 | if (iph->proto != IPAC_PROTO_OSMO) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 298 | 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] | 299 | goto err; |
| 300 | } |
| 301 | |
| 302 | iph_ext = (struct ipaccess_head_ext *) iph->data; |
| 303 | if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 304 | 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] | 305 | goto err; |
| 306 | } |
| 307 | |
| 308 | msg->l2h = iph_ext->data; |
| 309 | |
| 310 | cmd = ctrl_cmd_parse(ccon, msg); |
| 311 | |
| 312 | if (cmd) { |
| 313 | cmd->ccon = ccon; |
| 314 | if (ctrl_cmd_handle(cmd, ctrl->gsmnet) != CTRL_CMD_HANDLED) { |
| 315 | ctrl_cmd_send(queue, cmd); |
| 316 | talloc_free(cmd); |
| 317 | } |
| 318 | } else { |
| 319 | cmd = talloc_zero(ccon, struct ctrl_cmd); |
| 320 | if (!cmd) |
| 321 | goto err; |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 322 | LOGP(DCTRL, LOGL_ERROR, "Command parser error.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 323 | cmd->type = CTRL_TYPE_ERROR; |
| 324 | cmd->id = "err"; |
| 325 | cmd->reply = "Command parser error."; |
| 326 | ctrl_cmd_send(queue, cmd); |
| 327 | talloc_free(cmd); |
| 328 | } |
| 329 | |
| 330 | msgb_free(msg); |
| 331 | return 0; |
| 332 | |
| 333 | err: |
| 334 | control_close_conn(ccon); |
| 335 | msgb_free(msg); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
| 340 | { |
| 341 | int rc; |
| 342 | |
| 343 | rc = write(bfd->fd, msg->data, msg->len); |
| 344 | if (rc != msg->len) |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 345 | 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] | 346 | |
| 347 | return rc; |
| 348 | } |
| 349 | |
| 350 | static struct ctrl_connection *ctrl_connection_alloc(void *ctx) |
| 351 | { |
| 352 | struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection); |
| 353 | if (!ccon) |
| 354 | return NULL; |
| 355 | |
| 356 | osmo_wqueue_init(&ccon->write_queue, 100); |
| 357 | /* Error handling here? */ |
| 358 | |
| 359 | INIT_LLIST_HEAD(&ccon->cmds); |
| 360 | return ccon; |
| 361 | } |
| 362 | |
| 363 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 364 | { |
| 365 | int ret, fd, on; |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 366 | struct ctrl_handle *ctrl; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 367 | struct ctrl_connection *ccon; |
| 368 | struct sockaddr_in sa; |
| 369 | socklen_t sa_len = sizeof(sa); |
| 370 | |
| 371 | |
| 372 | if (!(what & BSC_FD_READ)) |
| 373 | return 0; |
| 374 | |
| 375 | fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 376 | if (fd < 0) { |
| 377 | perror("accept"); |
| 378 | return fd; |
| 379 | } |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 380 | LOGP(DCTRL, LOGL_INFO, "accept()ed new control connection from %s\n", |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 381 | inet_ntoa(sa.sin_addr)); |
| 382 | |
| 383 | on = 1; |
| 384 | ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 385 | if (ret != 0) { |
| 386 | LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 387 | close(fd); |
| 388 | return ret; |
| 389 | } |
| 390 | ccon = ctrl_connection_alloc(listen_bfd->data); |
| 391 | if (!ccon) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 392 | LOGP(DCTRL, LOGL_ERROR, "Failed to allocate.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 393 | close(fd); |
| 394 | return -1; |
| 395 | } |
| 396 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 397 | ctrl = listen_bfd->data; |
| 398 | ccon->write_queue.bfd.data = ctrl; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 399 | ccon->write_queue.bfd.fd = fd; |
| 400 | ccon->write_queue.bfd.when = BSC_FD_READ; |
| 401 | ccon->write_queue.read_cb = handle_control_read; |
| 402 | ccon->write_queue.write_cb = control_write_cb; |
| 403 | |
| 404 | ret = osmo_fd_register(&ccon->write_queue.bfd); |
| 405 | if (ret < 0) { |
Holger Hans Peter Freyther | 643656b | 2011-08-22 23:37:02 +0200 | [diff] [blame] | 406 | LOGP(DCTRL, LOGL_ERROR, "Could not register FD.\n"); |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 407 | close(ccon->write_queue.bfd.fd); |
| 408 | talloc_free(ccon); |
| 409 | } |
| 410 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 411 | llist_add(&ccon->list_entry, &ctrl->ccon_list); |
| 412 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 413 | return ret; |
| 414 | } |
| 415 | |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 416 | static uint64_t get_rate_ctr_value(const struct rate_ctr *ctr, int intv) |
| 417 | { |
| 418 | if (intv >= RATE_CTR_INTV_NUM) |
| 419 | return 0; |
| 420 | |
| 421 | /* Absolute value */ |
| 422 | if (intv == -1) { |
| 423 | return ctr->current; |
| 424 | } else { |
| 425 | return ctr->intv[intv].rate; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static char *get_all_rate_ctr_in_group(const struct rate_ctr_group *ctrg, int intv) |
| 430 | { |
| 431 | int i; |
| 432 | char *counters = talloc_strdup(tall_bsc_ctx, ""); |
| 433 | if (!counters) |
| 434 | return NULL; |
| 435 | |
| 436 | for (i=0;i<ctrg->desc->num_ctr;i++) { |
Daniel Willmann | 965e8fc | 2011-07-19 12:12:10 +0200 | [diff] [blame] | 437 | counters = talloc_asprintf_append(counters, "\n%s.%u.%s %"PRIu64, |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 438 | ctrg->desc->group_name_prefix, ctrg->idx, |
| 439 | ctrg->desc->ctr_desc[i].name, |
| 440 | get_rate_ctr_value(&ctrg->ctr[i], intv)); |
| 441 | if (!counters) |
| 442 | return NULL; |
| 443 | } |
| 444 | return counters; |
| 445 | } |
| 446 | |
| 447 | static int get_rate_ctr_group(const char *ctr_group, int intv, struct ctrl_cmd *cmd) |
| 448 | { |
| 449 | int i; |
| 450 | char *counters; |
| 451 | struct rate_ctr_group *ctrg; |
| 452 | |
| 453 | cmd->reply = talloc_asprintf(cmd, "All counters in group %s", ctr_group); |
| 454 | if (!cmd->reply) |
| 455 | goto oom; |
| 456 | |
| 457 | for (i=0;;i++) { |
| 458 | ctrg = rate_ctr_get_group_by_name_idx(ctr_group, i); |
| 459 | if (!ctrg) |
| 460 | break; |
| 461 | |
| 462 | counters = get_all_rate_ctr_in_group(ctrg, intv); |
| 463 | if (!counters) |
| 464 | goto oom; |
| 465 | |
| 466 | cmd->reply = talloc_asprintf_append(cmd->reply, "%s", counters); |
| 467 | talloc_free(counters); |
| 468 | if (!cmd->reply) |
| 469 | goto oom; |
| 470 | } |
| 471 | |
| 472 | /* We found no counter group by that name */ |
| 473 | if (i == 0) { |
| 474 | cmd->reply = talloc_asprintf(cmd, "No counter group with name %s.", ctr_group); |
| 475 | return CTRL_CMD_ERROR; |
| 476 | } |
| 477 | |
| 478 | return CTRL_CMD_REPLY; |
| 479 | oom: |
| 480 | cmd->reply = "OOM."; |
| 481 | return CTRL_CMD_ERROR; |
| 482 | } |
| 483 | |
| 484 | static int get_rate_ctr_group_idx(const struct rate_ctr_group *ctrg, int intv, struct ctrl_cmd *cmd) |
| 485 | { |
| 486 | char *counters; |
| 487 | |
| 488 | counters = get_all_rate_ctr_in_group(ctrg, intv); |
| 489 | if (!counters) |
| 490 | goto oom; |
| 491 | |
| 492 | cmd->reply = talloc_asprintf(cmd, "All counters in %s.%u%s", |
| 493 | ctrg->desc->group_name_prefix, ctrg->idx, counters); |
| 494 | talloc_free(counters); |
| 495 | if (!cmd->reply) |
| 496 | goto oom; |
| 497 | |
| 498 | return CTRL_CMD_REPLY; |
| 499 | oom: |
| 500 | cmd->reply = "OOM."; |
| 501 | return CTRL_CMD_ERROR; |
| 502 | } |
| 503 | |
| 504 | /* rate_ctr */ |
| 505 | CTRL_CMD_DEFINE(rate_ctr, "rate_ctr *"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 506 | static int get_rate_ctr(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 507 | { |
| 508 | int intv; |
| 509 | unsigned int idx; |
| 510 | char *ctr_group, *ctr_idx, *ctr_name, *tmp, *dup, *saveptr, *interval; |
| 511 | struct rate_ctr_group *ctrg; |
| 512 | const struct rate_ctr *ctr; |
| 513 | |
| 514 | dup = talloc_strdup(cmd, cmd->variable); |
| 515 | if (!dup) |
| 516 | goto oom; |
| 517 | |
| 518 | /* Skip over possible prefixes (net.) */ |
| 519 | tmp = strstr(dup, "rate_ctr"); |
| 520 | if (!tmp) { |
| 521 | talloc_free(dup); |
| 522 | cmd->reply = "rate_ctr not a token in rate_ctr command!"; |
| 523 | goto err; |
| 524 | } |
| 525 | |
| 526 | strtok_r(tmp, ".", &saveptr); |
| 527 | interval = strtok_r(NULL, ".", &saveptr); |
| 528 | if (!interval) { |
| 529 | talloc_free(dup); |
| 530 | cmd->reply = "Missing interval."; |
| 531 | goto err; |
| 532 | } |
| 533 | |
| 534 | if (!strcmp(interval, "abs")) { |
| 535 | intv = -1; |
| 536 | } else if (!strcmp(interval, "per_sec")) { |
| 537 | intv = RATE_CTR_INTV_SEC; |
| 538 | } else if (!strcmp(interval, "per_min")) { |
| 539 | intv = RATE_CTR_INTV_MIN; |
| 540 | } else if (!strcmp(interval, "per_hour")) { |
| 541 | intv = RATE_CTR_INTV_HOUR; |
| 542 | } else if (!strcmp(interval, "per_day")) { |
| 543 | intv = RATE_CTR_INTV_DAY; |
| 544 | } else { |
| 545 | talloc_free(dup); |
| 546 | cmd->reply = "Wrong interval."; |
| 547 | goto err; |
| 548 | } |
| 549 | |
| 550 | ctr_group = strtok_r(NULL, ".", &saveptr); |
| 551 | tmp = strtok_r(NULL, ".", &saveptr); |
| 552 | if (!ctr_group || !tmp) { |
| 553 | talloc_free(dup); |
| 554 | cmd->reply = "Counter group must be of form a.b"; |
| 555 | goto err; |
| 556 | } |
| 557 | ctr_group[strlen(ctr_group)] = '.'; |
| 558 | |
| 559 | ctr_idx = strtok_r(NULL, ".", &saveptr); |
| 560 | if (!ctr_idx) { |
| 561 | talloc_free(dup); |
| 562 | return get_rate_ctr_group(ctr_group, intv, cmd); |
| 563 | } |
| 564 | idx = atoi(ctr_idx); |
| 565 | |
| 566 | ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx); |
| 567 | if (!ctrg) { |
| 568 | talloc_free(dup); |
| 569 | cmd->reply = "Counter group not found."; |
| 570 | goto err; |
| 571 | } |
| 572 | |
| 573 | ctr_name = strtok_r(NULL, "\0", &saveptr); |
| 574 | if (!ctr_name) { |
| 575 | talloc_free(dup); |
| 576 | return get_rate_ctr_group_idx(ctrg, intv, cmd); |
| 577 | } |
| 578 | |
| 579 | ctr = rate_ctr_get_by_name(ctrg, ctr_name); |
| 580 | if (!ctr) { |
| 581 | cmd->reply = "Counter name not found."; |
| 582 | talloc_free(dup); |
| 583 | goto err; |
| 584 | } |
| 585 | |
| 586 | talloc_free(dup); |
| 587 | |
Holger Hans Peter Freyther | 5a65444 | 2012-03-16 12:18:39 +0100 | [diff] [blame] | 588 | cmd->reply = talloc_asprintf(cmd, "%"PRIu64, get_rate_ctr_value(ctr, intv)); |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 589 | if (!cmd->reply) |
| 590 | goto oom; |
| 591 | |
| 592 | return CTRL_CMD_REPLY; |
| 593 | oom: |
| 594 | cmd->reply = "OOM"; |
| 595 | err: |
| 596 | return CTRL_CMD_ERROR; |
| 597 | } |
| 598 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 599 | static int set_rate_ctr(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 600 | { |
| 601 | cmd->reply = "Can't set rate counter."; |
| 602 | |
| 603 | return CTRL_CMD_ERROR; |
| 604 | } |
| 605 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 606 | 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] | 607 | { |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* counter */ |
| 612 | CTRL_CMD_DEFINE(counter, "counter *"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 613 | static int get_counter(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 614 | { |
| 615 | char *ctr_name, *tmp, *dup, *saveptr; |
| 616 | struct osmo_counter *counter; |
| 617 | |
| 618 | cmd->reply = "OOM"; |
| 619 | dup = talloc_strdup(cmd, cmd->variable); |
| 620 | if (!dup) |
| 621 | goto err; |
| 622 | |
| 623 | |
| 624 | tmp = strstr(dup, "counter"); |
| 625 | if (!tmp) { |
| 626 | talloc_free(dup); |
| 627 | goto err; |
| 628 | } |
| 629 | |
| 630 | strtok_r(tmp, ".", &saveptr); |
| 631 | ctr_name = strtok_r(NULL, "\0", &saveptr); |
| 632 | |
| 633 | if (!ctr_name) |
| 634 | goto err; |
| 635 | |
| 636 | counter = osmo_counter_get_by_name(ctr_name); |
| 637 | if (!counter) { |
| 638 | cmd->reply = "Counter name not found."; |
| 639 | talloc_free(dup); |
| 640 | goto err; |
| 641 | } |
| 642 | |
| 643 | talloc_free(dup); |
| 644 | |
| 645 | cmd->reply = talloc_asprintf(cmd, "%lu", counter->value); |
| 646 | if (!cmd->reply) { |
| 647 | cmd->reply = "OOM"; |
| 648 | goto err; |
| 649 | } |
| 650 | |
| 651 | return CTRL_CMD_REPLY; |
| 652 | err: |
| 653 | return CTRL_CMD_ERROR; |
| 654 | } |
| 655 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 656 | static int set_counter(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 657 | { |
| 658 | |
| 659 | cmd->reply = "Can't set counter."; |
| 660 | |
| 661 | return CTRL_CMD_ERROR; |
| 662 | } |
| 663 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 664 | 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] | 665 | { |
| 666 | return 0; |
| 667 | } |
| 668 | |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 669 | struct ctrl_handle *controlif_setup(struct gsm_network *gsmnet, uint16_t port) |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 670 | { |
| 671 | int ret; |
| 672 | struct ctrl_handle *ctrl; |
| 673 | |
| 674 | ctrl = talloc_zero(tall_bsc_ctx, struct ctrl_handle); |
| 675 | if (!ctrl) |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 676 | return NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 677 | |
Daniel Willmann | 7f1586f | 2011-05-06 16:37:25 +0200 | [diff] [blame] | 678 | INIT_LLIST_HEAD(&ctrl->ccon_list); |
| 679 | |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 680 | ctrl->gsmnet = gsmnet; |
| 681 | |
| 682 | ctrl_node_vec = vector_init(5); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 683 | if (!ctrl_node_vec) |
| 684 | goto err; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 685 | |
| 686 | /* Listen for control connections */ |
Daniel Willmann | 0d441b6 | 2011-07-29 10:41:24 +0200 | [diff] [blame] | 687 | ret = make_sock(&ctrl->listen_fd, IPPROTO_TCP, INADDR_LOOPBACK, port, |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 688 | 0, listen_fd_cb, ctrl); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 689 | if (ret < 0) |
| 690 | goto err_vec; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 691 | |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 692 | ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr); |
| 693 | if (ret) |
| 694 | goto err_vec; |
| 695 | ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter); |
| 696 | if (ret) |
| 697 | goto err_vec; |
Daniel Willmann | 84edde2 | 2011-04-21 11:43:55 +0200 | [diff] [blame] | 698 | |
Daniel Willmann | 5c3bad9 | 2011-07-22 17:55:42 +0200 | [diff] [blame] | 699 | return ctrl; |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 700 | err_vec: |
| 701 | vector_free(ctrl_node_vec); |
| 702 | ctrl_node_vec = NULL; |
| 703 | err: |
| 704 | talloc_free(ctrl); |
| 705 | return NULL; |
Daniel Willmann | 27da244 | 2010-10-21 15:00:36 +0200 | [diff] [blame] | 706 | } |