blob: 91c69e09449b9e1e14efc8fc8a313517db4ad0ba [file] [log] [blame]
Daniel Willmann1264cb42010-10-21 15:00:36 +02001/* 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 Willmann0834a342011-07-19 12:12:10 +020025#include <inttypes.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020026#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>
42#include <openbsc/debug.h>
43#include <openbsc/e1_input.h>
44#include <openbsc/gsm_data.h>
45#include <openbsc/ipaccess.h>
46#include <openbsc/socket.h>
47#include <openbsc/subchan_demux.h>
48
49#include <openbsc/abis_rsl.h>
50#include <openbsc/abis_nm.h>
51
52#include <osmocom/core/msgb.h>
Daniel Willmanne9f58942011-04-21 11:43:55 +020053#include <osmocom/core/rate_ctr.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020054#include <osmocom/core/select.h>
Daniel Willmanne9f58942011-04-21 11:43:55 +020055#include <osmocom/core/statistics.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020056#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
63struct ctrl_handle {
64 struct osmo_fd listen_fd;
65 struct gsm_network *gsmnet;
66};
67
68vector ctrl_node_vec;
69
70int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
71{
72 int ret;
73 struct msgb *msg;
74
75 msg = ctrl_cmd_make(cmd);
76 if (!msg) {
77 LOGP(DINP, LOGL_ERROR, "Could not generate msg\n");
78 return -1;
79 }
80
81 ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL);
82 ipaccess_prepend_header(msg, IPAC_PROTO_OSMO);
83
84 ret = osmo_wqueue_enqueue(queue, msg);
85 if (ret != 0) {
86 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the command.\n");
87 msgb_free(msg);
88 }
89 return ret;
90}
91
92int ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data)
93{
94 char *token, *request;
95 int num, i, j, ret, node;
96 struct gsm_network *gsmnet = data;
97
98 struct gsm_network *net = NULL;
99 struct gsm_bts *bts = NULL;
100 struct gsm_bts_trx *trx = NULL;
101 struct gsm_bts_trx_ts *ts = NULL;
102 vector vline, cmdvec, cmds_vec;
103
104 ret = CTRL_CMD_ERROR;
105 cmd->reply = "Someone forgot to fill in the reply.";
106 cmd->node = NULL;
107 node = CTRL_NODE_ROOT;
108
109 request = talloc_strdup(tall_bsc_ctx, cmd->variable);
110 if (!request)
111 goto err;
112
113 for (i=0;i<strlen(request);i++) {
114 if (request[i] == '.')
115 request[i] = ' ';
116 }
117
118 vline = cmd_make_strvec(request);
119 talloc_free(request);
120 if (!vline)
121 goto err;
122
123 for (i=0;i<vector_active(vline);i++) {
124 token = vector_slot(vline, i);
125 /* TODO: We need to make sure that the following chars are digits
126 * and/or use strtol to check if number conversion was successful
127 * Right now something like net.bts_stats will not work */
128 if (!strcmp(token, "net")) {
129 net = gsmnet;
130 if (!net)
131 break;
132 cmd->node = net;
133 node = CTRL_NODE_NET;
134 } else if (!strncmp(token, "bts", 3)) {
135 if (!net)
136 break;
137 num = atoi(&token[3]);
138 bts = gsm_bts_num(net, num);
139 if (!bts)
140 break;
141 cmd->node = bts;
142 node = CTRL_NODE_BTS;
143 } else if (!strncmp(token, "trx", 3)) {
144 if (!bts)
145 break;
146 num = atoi(&token[3]);
147 trx = gsm_bts_trx_num(bts, num);
148 if (!trx)
149 break;
150 cmd->node = trx;
151 node = CTRL_NODE_TRX;
152 } else if (!strncmp(token, "ts", 2)) {
153 if (!trx)
154 break;
155 num = atoi(&token[2]);
156 if ((num >= 0) && (num < TRX_NR_TS))
157 ts = &trx->ts[num];
158 if (!ts)
159 break;
160 cmd->node = ts;
161 node = CTRL_NODE_TS;
162 } else {
163 /* If we're here the rest must be the command */
164 cmdvec = vector_init(vector_active(vline)-i);
165 for (j=i; j<vector_active(vline); j++) {
166 vector_set(cmdvec, vector_slot(vline, j));
167 }
168
169 /* Get the command vector of the right node */
170 cmds_vec = vector_lookup(ctrl_node_vec, node);
171
172 if (!cmds_vec) {
173 cmd->reply = "Command not found";
174 vector_free(cmdvec);
175 break;
176 }
177
178 ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
179
180 vector_free(cmdvec);
181 break;
182 }
183 }
184
185 cmd_free_strvec(vline);
186
187err:
188 if (ret == CTRL_CMD_ERROR)
189 cmd->type = CTRL_TYPE_ERROR;
190 return ret;
191}
192
193static void control_close_conn(struct ctrl_connection *ccon)
194{
195 close(ccon->write_queue.bfd.fd);
196 osmo_fd_unregister(&ccon->write_queue.bfd);
197 if (ccon->closed_cb)
198 ccon->closed_cb(ccon);
199 talloc_free(ccon);
200}
201
202static int handle_control_read(struct osmo_fd * bfd)
203{
204 int ret = -1, error;
205 struct osmo_wqueue *queue;
206 struct ctrl_connection *ccon;
207 struct ipaccess_head *iph;
208 struct ipaccess_head_ext *iph_ext;
209 struct msgb *msg;
210 struct ctrl_cmd *cmd;
211 struct ctrl_handle *ctrl = bfd->data;
212
213 queue = container_of(bfd, struct osmo_wqueue, bfd);
214 ccon = container_of(queue, struct ctrl_connection, write_queue);
215
216 msg = ipaccess_read_msg(bfd, &error);
217
218 if (!msg) {
219 if (error == 0)
220 LOGP(DINP, LOGL_INFO, "The control connection was closed\n");
221 else
222 LOGP(DINP, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
223
224 goto err;
225 }
226
227 if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) {
228 LOGP(DINP, LOGL_ERROR, "The message is too short.\n");
229 goto err;
230 }
231
232 iph = (struct ipaccess_head *) msg->data;
233 if (iph->proto != IPAC_PROTO_OSMO) {
234 LOGP(DINP, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto);
235 goto err;
236 }
237
238 iph_ext = (struct ipaccess_head_ext *) iph->data;
239 if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) {
240 LOGP(DINP, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto);
241 goto err;
242 }
243
244 msg->l2h = iph_ext->data;
245
246 cmd = ctrl_cmd_parse(ccon, msg);
247
248 if (cmd) {
249 cmd->ccon = ccon;
250 if (ctrl_cmd_handle(cmd, ctrl->gsmnet) != CTRL_CMD_HANDLED) {
251 ctrl_cmd_send(queue, cmd);
252 talloc_free(cmd);
253 }
254 } else {
255 cmd = talloc_zero(ccon, struct ctrl_cmd);
256 if (!cmd)
257 goto err;
258 LOGP(DINP, LOGL_ERROR, "Command parser error.\n");
259 cmd->type = CTRL_TYPE_ERROR;
260 cmd->id = "err";
261 cmd->reply = "Command parser error.";
262 ctrl_cmd_send(queue, cmd);
263 talloc_free(cmd);
264 }
265
266 msgb_free(msg);
267 return 0;
268
269err:
270 control_close_conn(ccon);
271 msgb_free(msg);
272 return ret;
273}
274
275static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg)
276{
277 int rc;
278
279 rc = write(bfd->fd, msg->data, msg->len);
280 if (rc != msg->len)
281 LOGP(DINP, LOGL_ERROR, "Failed to write message to the control connection.\n");
282
283 return rc;
284}
285
286static struct ctrl_connection *ctrl_connection_alloc(void *ctx)
287{
288 struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection);
289 if (!ccon)
290 return NULL;
291
292 osmo_wqueue_init(&ccon->write_queue, 100);
293 /* Error handling here? */
294
295 INIT_LLIST_HEAD(&ccon->cmds);
296 return ccon;
297}
298
299static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
300{
301 int ret, fd, on;
302 struct ctrl_connection *ccon;
303 struct sockaddr_in sa;
304 socklen_t sa_len = sizeof(sa);
305
306
307 if (!(what & BSC_FD_READ))
308 return 0;
309
310 fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
311 if (fd < 0) {
312 perror("accept");
313 return fd;
314 }
315 LOGP(DINP, LOGL_INFO, "accept()ed new control connection from %s\n",
316 inet_ntoa(sa.sin_addr));
317
318 on = 1;
319 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
320 if (ret != 0) {
321 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
322 close(fd);
323 return ret;
324 }
325 ccon = ctrl_connection_alloc(listen_bfd->data);
326 if (!ccon) {
327 LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n");
328 close(fd);
329 return -1;
330 }
331
332 ccon->write_queue.bfd.data = listen_bfd->data;
333 ccon->write_queue.bfd.fd = fd;
334 ccon->write_queue.bfd.when = BSC_FD_READ;
335 ccon->write_queue.read_cb = handle_control_read;
336 ccon->write_queue.write_cb = control_write_cb;
337
338 ret = osmo_fd_register(&ccon->write_queue.bfd);
339 if (ret < 0) {
340 LOGP(DINP, LOGL_ERROR, "Could not register FD.\n");
341 close(ccon->write_queue.bfd.fd);
342 talloc_free(ccon);
343 }
344
345 return ret;
346}
347
Daniel Willmanne9f58942011-04-21 11:43:55 +0200348static uint64_t get_rate_ctr_value(const struct rate_ctr *ctr, int intv)
349{
350 if (intv >= RATE_CTR_INTV_NUM)
351 return 0;
352
353 /* Absolute value */
354 if (intv == -1) {
355 return ctr->current;
356 } else {
357 return ctr->intv[intv].rate;
358 }
359}
360
361static char *get_all_rate_ctr_in_group(const struct rate_ctr_group *ctrg, int intv)
362{
363 int i;
364 char *counters = talloc_strdup(tall_bsc_ctx, "");
365 if (!counters)
366 return NULL;
367
368 for (i=0;i<ctrg->desc->num_ctr;i++) {
Daniel Willmann0834a342011-07-19 12:12:10 +0200369 counters = talloc_asprintf_append(counters, "\n%s.%u.%s %"PRIu64,
Daniel Willmanne9f58942011-04-21 11:43:55 +0200370 ctrg->desc->group_name_prefix, ctrg->idx,
371 ctrg->desc->ctr_desc[i].name,
372 get_rate_ctr_value(&ctrg->ctr[i], intv));
373 if (!counters)
374 return NULL;
375 }
376 return counters;
377}
378
379static int get_rate_ctr_group(const char *ctr_group, int intv, struct ctrl_cmd *cmd)
380{
381 int i;
382 char *counters;
383 struct rate_ctr_group *ctrg;
384
385 cmd->reply = talloc_asprintf(cmd, "All counters in group %s", ctr_group);
386 if (!cmd->reply)
387 goto oom;
388
389 for (i=0;;i++) {
390 ctrg = rate_ctr_get_group_by_name_idx(ctr_group, i);
391 if (!ctrg)
392 break;
393
394 counters = get_all_rate_ctr_in_group(ctrg, intv);
395 if (!counters)
396 goto oom;
397
398 cmd->reply = talloc_asprintf_append(cmd->reply, "%s", counters);
399 talloc_free(counters);
400 if (!cmd->reply)
401 goto oom;
402 }
403
404 /* We found no counter group by that name */
405 if (i == 0) {
406 cmd->reply = talloc_asprintf(cmd, "No counter group with name %s.", ctr_group);
407 return CTRL_CMD_ERROR;
408 }
409
410 return CTRL_CMD_REPLY;
411oom:
412 cmd->reply = "OOM.";
413 return CTRL_CMD_ERROR;
414}
415
416static int get_rate_ctr_group_idx(const struct rate_ctr_group *ctrg, int intv, struct ctrl_cmd *cmd)
417{
418 char *counters;
419
420 counters = get_all_rate_ctr_in_group(ctrg, intv);
421 if (!counters)
422 goto oom;
423
424 cmd->reply = talloc_asprintf(cmd, "All counters in %s.%u%s",
425 ctrg->desc->group_name_prefix, ctrg->idx, counters);
426 talloc_free(counters);
427 if (!cmd->reply)
428 goto oom;
429
430 return CTRL_CMD_REPLY;
431oom:
432 cmd->reply = "OOM.";
433 return CTRL_CMD_ERROR;
434}
435
436/* rate_ctr */
437CTRL_CMD_DEFINE(rate_ctr, "rate_ctr *");
438int get_rate_ctr(struct ctrl_cmd *cmd, void *data)
439{
440 int intv;
441 unsigned int idx;
442 char *ctr_group, *ctr_idx, *ctr_name, *tmp, *dup, *saveptr, *interval;
443 struct rate_ctr_group *ctrg;
444 const struct rate_ctr *ctr;
445
446 dup = talloc_strdup(cmd, cmd->variable);
447 if (!dup)
448 goto oom;
449
450 /* Skip over possible prefixes (net.) */
451 tmp = strstr(dup, "rate_ctr");
452 if (!tmp) {
453 talloc_free(dup);
454 cmd->reply = "rate_ctr not a token in rate_ctr command!";
455 goto err;
456 }
457
458 strtok_r(tmp, ".", &saveptr);
459 interval = strtok_r(NULL, ".", &saveptr);
460 if (!interval) {
461 talloc_free(dup);
462 cmd->reply = "Missing interval.";
463 goto err;
464 }
465
466 if (!strcmp(interval, "abs")) {
467 intv = -1;
468 } else if (!strcmp(interval, "per_sec")) {
469 intv = RATE_CTR_INTV_SEC;
470 } else if (!strcmp(interval, "per_min")) {
471 intv = RATE_CTR_INTV_MIN;
472 } else if (!strcmp(interval, "per_hour")) {
473 intv = RATE_CTR_INTV_HOUR;
474 } else if (!strcmp(interval, "per_day")) {
475 intv = RATE_CTR_INTV_DAY;
476 } else {
477 talloc_free(dup);
478 cmd->reply = "Wrong interval.";
479 goto err;
480 }
481
482 ctr_group = strtok_r(NULL, ".", &saveptr);
483 tmp = strtok_r(NULL, ".", &saveptr);
484 if (!ctr_group || !tmp) {
485 talloc_free(dup);
486 cmd->reply = "Counter group must be of form a.b";
487 goto err;
488 }
489 ctr_group[strlen(ctr_group)] = '.';
490
491 ctr_idx = strtok_r(NULL, ".", &saveptr);
492 if (!ctr_idx) {
493 talloc_free(dup);
494 return get_rate_ctr_group(ctr_group, intv, cmd);
495 }
496 idx = atoi(ctr_idx);
497
498 ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx);
499 if (!ctrg) {
500 talloc_free(dup);
501 cmd->reply = "Counter group not found.";
502 goto err;
503 }
504
505 ctr_name = strtok_r(NULL, "\0", &saveptr);
506 if (!ctr_name) {
507 talloc_free(dup);
508 return get_rate_ctr_group_idx(ctrg, intv, cmd);
509 }
510
511 ctr = rate_ctr_get_by_name(ctrg, ctr_name);
512 if (!ctr) {
513 cmd->reply = "Counter name not found.";
514 talloc_free(dup);
515 goto err;
516 }
517
518 talloc_free(dup);
519
520 cmd->reply = talloc_asprintf(cmd, "%lu", get_rate_ctr_value(ctr, intv));
521 if (!cmd->reply)
522 goto oom;
523
524 return CTRL_CMD_REPLY;
525oom:
526 cmd->reply = "OOM";
527err:
528 return CTRL_CMD_ERROR;
529}
530
531int set_rate_ctr(struct ctrl_cmd *cmd, void *data)
532{
533 cmd->reply = "Can't set rate counter.";
534
535 return CTRL_CMD_ERROR;
536}
537
538int verify_rate_ctr(struct ctrl_cmd *cmd, const char *value, void *data)
539{
540 return 0;
541}
542
543/* counter */
544CTRL_CMD_DEFINE(counter, "counter *");
545int get_counter(struct ctrl_cmd *cmd, void *data)
546{
547 char *ctr_name, *tmp, *dup, *saveptr;
548 struct osmo_counter *counter;
549
550 cmd->reply = "OOM";
551 dup = talloc_strdup(cmd, cmd->variable);
552 if (!dup)
553 goto err;
554
555
556 tmp = strstr(dup, "counter");
557 if (!tmp) {
558 talloc_free(dup);
559 goto err;
560 }
561
562 strtok_r(tmp, ".", &saveptr);
563 ctr_name = strtok_r(NULL, "\0", &saveptr);
564
565 if (!ctr_name)
566 goto err;
567
568 counter = osmo_counter_get_by_name(ctr_name);
569 if (!counter) {
570 cmd->reply = "Counter name not found.";
571 talloc_free(dup);
572 goto err;
573 }
574
575 talloc_free(dup);
576
577 cmd->reply = talloc_asprintf(cmd, "%lu", counter->value);
578 if (!cmd->reply) {
579 cmd->reply = "OOM";
580 goto err;
581 }
582
583 return CTRL_CMD_REPLY;
584err:
585 return CTRL_CMD_ERROR;
586}
587
588int set_counter(struct ctrl_cmd *cmd, void *data)
589{
590
591 cmd->reply = "Can't set counter.";
592
593 return CTRL_CMD_ERROR;
594}
595
596int verify_counter(struct ctrl_cmd *cmd, const char *value, void *data)
597{
598 return 0;
599}
600
Daniel Willmann1264cb42010-10-21 15:00:36 +0200601int controlif_setup(struct gsm_network *gsmnet, uint16_t port)
602{
603 int ret;
604 struct ctrl_handle *ctrl;
605
606 ctrl = talloc_zero(tall_bsc_ctx, struct ctrl_handle);
607 if (!ctrl)
608 return -ENOMEM;
609
610 ctrl->gsmnet = gsmnet;
611
612 ctrl_node_vec = vector_init(5);
613 if (!ctrl_node_vec)
614 return -ENOMEM;
615
616 /* Listen for control connections */
617 ret = make_sock(&ctrl->listen_fd, IPPROTO_TCP, 0, port,
618 0, listen_fd_cb, ctrl);
619 if (ret < 0) {
620 talloc_free(ctrl);
621 return ret;
622 }
623
Daniel Willmanne9f58942011-04-21 11:43:55 +0200624 ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr);
625 ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter);
626
Daniel Willmann1264cb42010-10-21 15:00:36 +0200627 return ret;
628}