show_sessions.c 14.8 KB
Newer Older
1 2 3 4 5 6
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <arpa/inet.h>
7
#include <linux/if_link.h>
8 9 10 11

#include "triton.h"
#include "events.h"
#include "ppp.h"
12
#include "ipdb.h"
13 14 15 16 17 18
#include "cli.h"
#include "utils.h"
#include "log.h"
#include "memdebug.h"

#define CELL_SIZE 128
19
#define DEF_COLUMNS "ifname,username,calling-sid,ip,rate-limit,type,comp,state,uptime"
20 21 22 23 24 25

struct column_t
{
	struct list_head entry;
	const char *name;
	const char *desc;
26
	void (*print)(struct ap_session *ses, char *buf);
27 28 29 30 31 32 33
};

struct col_t
{
	struct list_head entry;
	struct column_t *column;
	int width;
34
	int hidden;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
};

struct row_t
{
	struct list_head entry;
	char *match_key;
	char *order_key;
	struct list_head cell_list;
};

struct cell_t
{
	struct list_head entry;
	struct col_t *col;
	char buf[CELL_SIZE + 1];
};

static LIST_HEAD(col_list);
53
static char *conf_def_columns = NULL;
54

55 56 57 58
static __thread struct rtnl_link_stats stats;
static __thread int stats_set;

void __export cli_show_ses_register(const char *name, const char *desc, void (*print)(struct ap_session *ses, char *buf))
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
	struct column_t *c = malloc(sizeof(*c));
	c->name = name;
	c->desc = desc;
	c->print = print;
	list_add_tail(&c->entry, &col_list);
}

static void show_ses_help(char * const *f, int f_cnt, void *cli)
{
	struct column_t *col;
	char buf[129];

	cli_send(cli, "show sessions [columns] [order <column>] [match <column> <regexp>] - shows sessions\r\n");
	cli_send(cli, "\tcolumns:\r\n");

	list_for_each_entry(col, &col_list, entry) {
		snprintf(buf, 128, "\t\t%s - %s\r\n", col->name, col->desc);
		cli_send(cli, buf);
	}
}

static struct column_t *find_column(const char *name)
{
	struct column_t *col;

	list_for_each_entry(col, &col_list, entry) {
		if (strcmp(col->name, name))
			continue;
		return col;
	}

	return NULL;
}

static void free_row(struct row_t *row)
{
	struct cell_t *cell;

	while (!list_empty(&row->cell_list)) {
		cell = list_entry(row->cell_list.next, typeof(*cell), entry);
		list_del(&cell->entry);
		_free(cell);
	}

	_free(row);
}

static void insert_row(struct list_head *list, struct row_t *row)
{
	struct row_t *row2, *row3;
D
Dmitry Kozlov 已提交
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	row3 = NULL;
	list_for_each_entry(row2, list, entry) {
		if (strcmp(row->order_key, row2->order_key) <= 0) {
			row3 = row2;
			break;
		}
	}
	if (row3)
		list_add_tail(&row->entry, &row3->entry);
	else
		list_add_tail(&row->entry, list);
}

static int show_ses_exec(const char *cmd, char * const *f, int f_cnt, void *cli)
{
	char *columns = NULL;
	struct column_t *match_key = NULL;
	char *match_pattern = NULL;
	struct column_t *order_key = NULL;
	pcre *re = NULL;
	const char *pcre_err;
	int pcre_offset;
	struct column_t *column;
	struct col_t *col;
	struct row_t *row;
	struct cell_t *cell;
	char *ptr1, *ptr2;
138
	int i, n, total_width, def_columns = 0;
139
	struct ap_session *ses;
140
	char *buf = NULL;
141
	int match_key_f = 0, order_key_f = 0;
142 143 144 145 146 147 148 149 150 151 152 153 154 155
	LIST_HEAD(c_list);
	LIST_HEAD(r_list);
	LIST_HEAD(t_list);

	for (i = 2; i < f_cnt; i++) {
		if (!strcmp(f[i], "order")) {
			if (i == f_cnt - 1)
				return CLI_CMD_SYNTAX;
			order_key = find_column(f[++i]);
			if (!order_key) {
				cli_sendv(cli, "unknown column %s\r\n", f[i]);
				return CLI_CMD_OK;
			}
		} else if (!strcmp(f[i], "match")) {
K
Kozlov Dmitry 已提交
156
			if (i >= f_cnt - 2)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
				return CLI_CMD_SYNTAX;
			match_key = find_column(f[++i]);
			if (!match_key) {
				cli_sendv(cli, "unknown column %s\r\n", f[i]);
				return CLI_CMD_OK;
			}
			match_pattern = f[++i];
		} else if (!columns)
			columns = f[i];
		else
			return CLI_CMD_SYNTAX;
	}

	if (match_key) {
		re = pcre_compile2(match_pattern, 0, NULL, &pcre_err, &pcre_offset, NULL);
		if (!re) {
			cli_sendv(cli, "match: %s at %i\r\n", pcre_err, pcre_offset);
			return CLI_CMD_OK;
		}
	}

178
	if (!columns) {
179
		columns = (conf_def_columns) ? conf_def_columns : DEF_COLUMNS;
180 181
		def_columns = 1;
	}
182

183 184 185 186 187 188 189
	columns = _strdup(columns);
	ptr1 = columns;
	while (1) {
		ptr2 = strchr(ptr1, ',');
		if (ptr2)
			*ptr2 = 0;
		column = find_column(ptr1);
190 191 192 193
		if (column == match_key)
			match_key_f = 1;
		if (column == order_key)
			order_key_f = 1;
194 195 196 197
		if (column) {
			col = _malloc(sizeof(*col));
			col->column = column;
			col->width = strlen(column->name);
198
			col->hidden = 0;
199 200 201 202 203 204 205
			list_add_tail(&col->entry, &c_list);
		} else {
			if (!def_columns) {
				cli_sendv(cli, "unknown column %s\r\n", ptr1);
				_free(columns);
				goto out;
			}
206 207 208 209 210 211 212
		}
		if (!ptr2)
			break;
		ptr1 = ptr2 + 1;
	}
	_free(columns);

213 214 215 216 217 218 219
	if (match_key && !match_key_f) {
		col = _malloc(sizeof(*col));
		col->column = match_key;
		col->width = 0;
		col->hidden = 1;
		list_add_tail(&col->entry, &c_list);
	}
D
Dmitry Kozlov 已提交
220

221 222 223 224 225 226 227 228
	if (order_key && !order_key_f) {
		col = _malloc(sizeof(*col));
		col->column = order_key;
		col->width = 0;
		col->hidden = 1;
		list_add_tail(&col->entry, &c_list);
	}

229 230
	pthread_rwlock_rdlock(&ses_lock);
	list_for_each_entry(ses, &ses_list, entry) {
231
		stats_set = 0;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		row = _malloc(sizeof(*row));
		if (!row)
			goto oom;
		memset(row, 0, sizeof(*row));
		INIT_LIST_HEAD(&row->cell_list);
		if (match_key || order_key)
			list_add_tail(&row->entry, &t_list);
		else
			list_add_tail(&row->entry, &r_list);
		list_for_each_entry(col, &c_list, entry) {
			cell = _malloc(sizeof(*cell));
			if (!cell)
				goto oom;
			cell->col = col;
			list_add_tail(&cell->entry, &row->cell_list);
247
			col->column->print(ses, cell->buf);
248 249 250 251 252 253 254 255 256
			n = strlen(cell->buf);
			if (n > col->width)
				col->width = n;
			if (col->column == order_key)
				row->order_key = cell->buf;
			if (col->column == match_key)
				row->match_key = cell->buf;
		}
	}
257
	pthread_rwlock_unlock(&ses_lock);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

	if (order_key || match_key) {
		while(!list_empty(&t_list)) {
			row = list_entry(t_list.next, typeof(*row), entry);
			list_del(&row->entry);
			if (match_key) {
				if (pcre_exec(re, NULL, row->match_key, strlen(row->match_key), 0, 0, NULL, 0) < 0) {
					free_row(row);
					continue;
				}
			}
			if (order_key)
				insert_row(&r_list, row);
			else
				list_add_tail(&row->entry, &r_list);
		}
	}

	total_width = -1;
277 278 279
	list_for_each_entry(col, &c_list, entry) {
		if (col->hidden)
			continue;
280
		total_width += col->width + 3;
281
	}
282 283 284 285 286

	if (total_width < 0)
		/* No column to print */
		goto early_out;

287 288 289
	buf = _malloc(total_width + 3);
	if (!buf)
		goto oom;
290

291 292
	ptr1 = buf;
	list_for_each_entry(col, &c_list, entry) {
293 294
		if (col->hidden)
			continue;
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		n = strlen(col->column->name);
		if (col->width > n + 1) {
			ptr2 = ptr1;
			memset(ptr1, ' ', col->width/2 - n/2 + 1);
			ptr1 += col->width/2 - n/2 + 1;
			sprintf(ptr1, "%s", col->column->name);
			ptr1 = strchr(ptr1, 0);
			memset(ptr1, ' ', col->width + 2 - (ptr1 - ptr2));
			ptr1 += col->width + 2 - (ptr1 - ptr2);
			*ptr1 = '|';
			ptr1++;
		} else if (col->width > n) {
			sprintf(ptr1, " %s  |", col->column->name);
			ptr1 = strchr(ptr1, 0);
		} else {
			sprintf(ptr1, " %s |", col->column->name);
			ptr1 = strchr(ptr1, 0);
		}
	}

	strcpy(ptr1 - 1, "\r\n");
	cli_send(cli, buf);

	ptr1 = buf;
	list_for_each_entry(col, &c_list, entry) {
320 321
		if (col->hidden)
			continue;
322 323 324 325 326 327 328 329 330 331 332 333 334
		memset(ptr1, '-', col->width + 2);
		ptr1 += col->width + 2;
		*ptr1 = '+';
		ptr1++;
	}

	strcpy(ptr1 - 1, "\r\n");
	cli_send(cli, buf);

	while (!list_empty(&r_list)) {
		row = list_entry(r_list.next, typeof(*row), entry);
		ptr1 = buf;
		list_for_each_entry(cell, &row->cell_list, entry) {
335 336
			if (cell->col->hidden)
				continue;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			ptr2 = ptr1;
			sprintf(ptr1, " %s ", cell->buf);
			ptr1 = strchr(ptr1, 0);
			n = ptr1 - ptr2;
			if (n - 2 < cell->col->width) {
				memset(ptr1, ' ', cell->col->width + 2 - (ptr1 - ptr2));
				ptr1 += cell->col->width + 2 - (ptr1 - ptr2);
			}
			*ptr1 = '|';
			ptr1++;
		}
		strcpy(ptr1 - 1, "\r\n");
		cli_send(cli, buf);
		list_del(&row->entry);
		free_row(row);
	}

	_free(buf);

out:
	while (!list_empty(&c_list)) {
		col = list_entry(c_list.next, typeof(*col), entry);
		list_del(&col->entry);
		_free(col);
	}

	if (re)
		pcre_free(re);

	return CLI_CMD_OK;

oom:
369 370
	cli_send(cli, "out of memory");
early_out:
371 372 373 374 375 376 377 378 379 380 381
	if (buf)
		_free(buf);

	while (!list_empty(&t_list)) {
		row = list_entry(t_list.next, typeof(*row), entry);
		list_del(&row->entry);
		free_row(row);
	}
	goto out;
}

382 383 384 385 386
static void print_netns(struct ap_session *ses, char *buf)
{
	snprintf(buf, CELL_SIZE, "%s", ses->net->name);
}

387
static void print_ifname(struct ap_session *ses, char *buf)
388
{
389
	snprintf(buf, CELL_SIZE, "%s", ses->ifname);
390 391
}

392
static void print_username(struct ap_session *ses, char *buf)
393
{
394 395
	if (ses->username)
		snprintf(buf, CELL_SIZE, "%s", ses->username);
396 397
	else
		*buf = 0;
398 399
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static void print_ip6_dp(struct ap_session *ses, char *buf)
{
	struct ipv6db_addr_t *a;
	char *ptr;

	if (!ses->ipv6_dp) {
		*buf = 0;
		return;
	}

	a = list_entry(ses->ipv6_dp->prefix_list.next, typeof(*a), entry);
	inet_ntop(AF_INET6, &a->addr, buf, 64);
	ptr = strchr(buf, 0);
	sprintf(ptr, "/%i", a->prefix_len);
}

static void print_ip6(struct ap_session *ses, char *buf)
{
	struct ipv6db_addr_t *a;
	struct in6_addr addr;
	char *ptr;

422
	if (!ses->ipv6 || list_empty(&ses->ipv6->addr_list)) {
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
		*buf = 0;
		return;
	}

	a = list_entry(ses->ipv6->addr_list.next, typeof(*a), entry);
	if (a->prefix_len == 0) {
		*buf = 0;
		return;
	}

	build_ip6_addr(a, ses->ipv6->peer_intf_id, &addr);

	inet_ntop(AF_INET6, &addr, buf, 64);
	ptr = strchr(buf, 0);
	sprintf(ptr, "/%i", a->prefix_len);
}

440
static void print_ip(struct ap_session *ses, char *buf)
441
{
442 443 444 445 446 447
	if (!ses->ipv4) {
		print_ip6(ses,buf);
		return;
	}

	u_inet_ntoa(ses->ipv4->peer_addr, buf);
448 449
}

450
static void print_type(struct ap_session *ses, char *buf)
451
{
452
	snprintf(buf, CELL_SIZE, "%s", ses->ctrl->name);
453 454
}

455
static void print_state(struct ap_session *ses, char *buf)
456 457
{
	char *state;
458 459
	switch (ses->state) {
		case AP_STATE_STARTING:
460 461
			state = "start";
			break;
462
		case AP_STATE_ACTIVE:
463 464
			state = "active";
			break;
465
		case AP_STATE_FINISHING:
466 467 468 469 470 471 472 473
			state = "finish";
			break;
		default:
			state = "unk";
	}
	sprintf(buf, "%s", state);
}

474
static void print_uptime(struct ap_session *ses, char *buf)
475 476 477
{
	time_t uptime;
	int day,hour,min,sec;
478
	char time_str[24];
479

480 481
	if (ses->stop_time)
		uptime = ses->stop_time - ses->start_time;
482
	else {
483
		uptime = _time();
484
		uptime -= ses->start_time;
485 486 487 488 489 490 491
	}

	day = uptime/ (24*60*60); uptime %= (24*60*60);
	hour = uptime / (60*60); uptime %= (60*60);
	min = uptime / 60;
	sec = uptime % 60;
	if (day)
492
		snprintf(time_str, sizeof(time_str), "%i.%02i:%02i:%02i", day, hour, min, sec);
493
	else
494
		snprintf(time_str, sizeof(time_str), "%02i:%02i:%02i", hour, min, sec);
495 496 497 498

	sprintf(buf, "%s", time_str);
}

499 500 501 502 503 504 505 506 507 508 509 510
static void print_uptime_raw(struct ap_session *ses, char *buf)
{
	time_t uptime;

	if (ses->stop_time)
		uptime = ses->stop_time - ses->start_time;
	else
		uptime = _time() - ses->start_time;

	sprintf(buf, "%lu", (unsigned long)uptime);
}

511
static void print_calling_sid(struct ap_session *ses, char *buf)
512
{
513
	snprintf(buf, CELL_SIZE, "%s", ses->ctrl->calling_station_id);
514 515
}

516
static void print_called_sid(struct ap_session *ses, char *buf)
517
{
518
	snprintf(buf, CELL_SIZE, "%s", ses->ctrl->called_station_id);
519 520
}

521
static void print_sid(struct ap_session *ses, char *buf)
522
{
523
	snprintf(buf, CELL_SIZE, "%s", ses->sessionid);
524 525
}

526
static void print_comp(struct ap_session *ses, char *buf)
527
{
528 529
	if (ses->comp)
		snprintf(buf, CELL_SIZE, "%s", ses->comp);
K
Kozlov Dmitry 已提交
530 531
	else
		buf[0] = 0;
532 533
}

534 535 536 537 538 539 540
static void format_bytes(char *buf, unsigned long long bytes)
{
	const char *suffix;
	unsigned int m;
	double d;

	if (bytes < 1024) {
541
		sprintf(buf, "%u B", (unsigned)bytes);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		return;
	}

	if (bytes < 1024*1024) {
		suffix = "KiB";
		m = 1024;
	} else if (bytes < 1024*1024*1024) {
		suffix = "MiB";
		m = 1024*1024;
	} else {
		suffix = "GiB";
		m = 1024*1024*1024;
	}

	d = (double)bytes/m;
557
	sprintf(buf, "%.1f %s", d, suffix);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
}

static void print_rx_bytes(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	format_bytes(buf, 4294967296ll*ses->acct_input_gigawords + stats.rx_bytes);
}

static void print_tx_bytes(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	format_bytes(buf, 4294967296ll*ses->acct_output_gigawords + stats.tx_bytes);
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
static void print_rx_bytes_raw(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	sprintf(buf, "%llu", 4294967296ll*ses->acct_input_gigawords + stats.rx_bytes);
}

static void print_tx_bytes_raw(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	sprintf(buf, "%llu", 4294967296ll*ses->acct_output_gigawords + stats.tx_bytes);
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static void print_rx_pkts(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	sprintf(buf, "%u", stats.rx_packets);
}

static void print_tx_pkts(struct ap_session *ses, char *buf)
{
	if (!stats_set) {
		ap_session_read_stats(ses, &stats);
		stats_set = 1;
	}
	sprintf(buf, "%u", stats.tx_packets);
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
static void load_config(void *data)
{
	const char *opt = NULL;
	char *ptr = NULL;

	opt = conf_get_opt("cli", "sessions-columns");
	if (opt && strlen(opt) > 0) {
		ptr = _realloc(conf_def_columns, strlen(opt) + 1);
		if (ptr) {
			memcpy(ptr, opt, strlen(opt) + 1);
			conf_def_columns = ptr;
		} else
			log_error("cli: Discard option sessions-columns:"
				  " memory allocation error\n");
	}
	if (ptr == NULL && conf_def_columns) {
		_free(conf_def_columns);
		conf_def_columns = NULL;
	}
}

635
static void init(void)
636
{
637 638
	load_config(NULL);

639
	cli_register_simple_cmd2(show_ses_exec, show_ses_help, 2, "show", "sessions");
640

641
	cli_show_ses_register("netns", "network namespace name", print_netns);
642 643 644
	cli_show_ses_register("ifname", "interface name", print_ifname);
	cli_show_ses_register("username", "user name", print_username);
	cli_show_ses_register("ip", "IP address", print_ip);
645 646
	cli_show_ses_register("ip6", "IPv6 address", print_ip6);
	cli_show_ses_register("ip6-dp", "IPv6 delegated prefix", print_ip6_dp);
647 648
	cli_show_ses_register("type", "VPN type", print_type);
	cli_show_ses_register("state", "state of session", print_state);
D
Dmitry Kozlov 已提交
649 650
	cli_show_ses_register("uptime", "uptime (human readable)", print_uptime);
	cli_show_ses_register("uptime-raw", "uptime (in seconds)", print_uptime_raw);
651 652 653
	cli_show_ses_register("calling-sid", "calling station id", print_calling_sid);
	cli_show_ses_register("called-sid", "called station id", print_called_sid);
	cli_show_ses_register("sid", "session id", print_sid);
654
	cli_show_ses_register("comp", "compression/ecnryption method", print_comp);
655 656 657 658
	cli_show_ses_register("rx-bytes", "received bytes (human readable)", print_rx_bytes);
	cli_show_ses_register("tx-bytes", "transmitted bytes (human readable)", print_tx_bytes);
	cli_show_ses_register("rx-bytes-raw", "received bytes", print_rx_bytes_raw);
	cli_show_ses_register("tx-bytes-raw", "transmitted bytes", print_tx_bytes_raw);
659 660
	cli_show_ses_register("rx-pkts", "received packets", print_rx_pkts);
	cli_show_ses_register("tx-pkts", "transmitted packets", print_tx_pkts);
661 662

	triton_event_register_handler(EV_CONFIG_RELOAD, load_config);
663 664
}

665
DEFINE_INIT(12, init);