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