aboutsummaryrefslogtreecommitdiffstats
path: root/src/vty_interface_cmds.c
blob: 8697961168d8e6f131c9ef19131eb9cecd9e248e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* VTY code for the Cellmgr */
/*
 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2010-2011 by On-Waves
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <bsc_data.h>
#include <mtp_pcap.h>
#include <msc_connection.h>
#include <sctp_m2ua.h>

#include <osmocom/core/rate_ctr.h>

#include <osmocom/vty/command.h>
#include <osmocom/vty/logging.h>
#include <osmocom/vty/vty.h>
#include <osmocom/vty/misc.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

extern struct bsc_data *bsc;

static void dump_stats(struct vty *vty, struct mtp_link_set *set)
{
	struct mtp_link *link;

	vty_out(vty, "Linkset name: %s opc: %d%s", set->name, set->opc, VTY_NEWLINE);
	vty_out_rate_ctr_group(vty, " ", set->ctrg);

	llist_for_each_entry(link, &set->links, entry) {
		vty_out(vty, " Link %d%s", link->nr, VTY_NEWLINE);
		vty_out_rate_ctr_group(vty, "  ", link->ctrg);
	}
}

DEFUN(show_stats, show_stats_cmd,
      "show statistics",
      SHOW_STR "Display Linkset statistics\n")
{
	struct mtp_link_set *set;

	llist_for_each_entry(set, &bsc->linksets, entry)
		dump_stats(vty, set);

	return CMD_SUCCESS;
}

static void dump_state(struct vty *vty, struct mtp_link_set *set)
{
	struct mtp_link *link;

	if (!set->app) {
		vty_out(vty, "LinkSet %d not assigned to an application.%s",
			set->nr, VTY_NEWLINE);
		return;
	}

	vty_out(vty, "LinkSet for %d/%s is %s, remote sccp is %s.%s",
		set->nr, set->name,
		set->available == 0 ? "not available" : "available",
		set->sccp_up == 0? "not established" : "established",
		VTY_NEWLINE);

	llist_for_each_entry(link, &set->links, entry) {
		if (link->blocked)
			vty_out(vty, " Link %d is blocked.%s",
				link->nr, VTY_NEWLINE);
		else
			vty_out(vty, " Link %d is %s.%s",
				link->nr,
				link->available == 0 ? "not available" : "available",
				VTY_NEWLINE);
	}
}

DEFUN(show_linksets, show_linksets_cmd,
      "show link-sets",
      SHOW_STR "Display current state of linksets\n")
{
	struct mtp_link_set *set;

	llist_for_each_entry(set, &bsc->linksets, entry)
		dump_state(vty, set);
	return CMD_SUCCESS;
}

DEFUN(show_msc, show_msc_cmd,
      "show msc",
      SHOW_STR "Display the status of the MSC\n")
{
	struct msc_connection *msc;

	llist_for_each_entry(msc, &bsc->mscs, entry) {
		vty_out(vty, "MSC link is %s and had %s.%s",
			msc->msc_link_down == 0 ? "up" : "down",
			msc->first_contact == 1 ? "no contact" : "contact",
			VTY_NEWLINE);
	}

	return CMD_SUCCESS;
}

DEFUN(show_mscs, show_mscs_cmd,
      "show mscs",
      SHOW_STR "Display the status of all MSCs\n")
{
	struct msc_connection *msc;

	llist_for_each_entry(msc, &bsc->mscs, entry) {
		vty_out(vty, "MSC link nr %d name '%s' is %s and had %s.%s",
			msc->nr, msc->name,
			msc->msc_link_down == 0 ? "up" : "down",
			msc->first_contact == 1 ? "no contact" : "contact",
			VTY_NEWLINE);
	}

	return CMD_SUCCESS;
}


DEFUN(show_slc, show_slc_cmd,
      "show link-set <0-100> slc",
      SHOW_STR "LinkSet\n" "Linkset nr\n" "SLS to SLC\n")
{
	struct mtp_link_set *set = NULL;
	int i;

	set = mtp_link_set_num(bsc, atoi(argv[0]));

	if (!set) {
		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	vty_out(vty, "LinkSet for %s.%s", argv[0], VTY_NEWLINE);
	for (i = 0; i < ARRAY_SIZE(set->slc); ++i) {
		if (set->slc[i])
			vty_out(vty, " SLC[%.2d] is on link %d.%s",
				i, set->slc[i]->nr, VTY_NEWLINE);
		else
			vty_out(vty, " SLC[%d] is down.%s",
				i, VTY_NEWLINE);
	}

	return CMD_SUCCESS;
}

DEFUN(pcap_set, pcap_set_cmd,
      "trace-pcap <0-100> NAME FILE",
      "Trace to a PCAP file\n" "Linkset nr.\n"
      "Trace Linkset\n" "Filename to trace\n")
{
	struct mtp_link_set *set = NULL;

	set = mtp_link_set_num(bsc, atoi(argv[0]));

	if (!set) {
		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}


	if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd)
		close(set->pcap_fd);
	set->pcap_fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT,
			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
	if (set->pcap_fd < 0) {
		vty_out(vty, "Failed to open file for writing.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	mtp_pcap_write_header(set->pcap_fd);
	return CMD_SUCCESS;
}

DEFUN(pcap_set_stop, pcap_set_stop_cmd,
      "trace-pcap <0-100> NAME stop",
      "Trace to a PCAP file\n" "Linkset nr\n"
      "Trace Linkset\n" "Stop the tracing\n")
{
	struct mtp_link_set *set = NULL;

	set = mtp_link_set_num(bsc, atoi(argv[0]));

	if (!set) {
		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd)
		close(set->pcap_fd);
	set->pcap_fd = -1;
	return CMD_SUCCESS;
}

#define FIND_LINK(vty, set_no, nr) ({						\
	struct mtp_link_set *set = NULL;					\
	struct mtp_link *link = NULL;						\
	set = mtp_link_set_num(bsc, set_no);					\
	if (!set) {								\
		vty_out(vty, "Unknown Linkset nr %d.%s", set_no, VTY_NEWLINE);	\
		return CMD_WARNING;						\
	}									\
	if (!set->app) {							\
		vty_out(vty, "Linkset nr %d has no application.%s",		\
			set_no, VTY_NEWLINE);					\
	}									\
	link = mtp_link_num(set, nr);						\
	if (!link) {								\
		vty_out(vty, "Can not find link %d.%s", nr, VTY_NEWLINE);	\
		return CMD_WARNING;						\
	}									\
	link; })

#define LINK_STR "Operations on the link\n"					\
		 "Linkset number\n"						\
		 "Link number\n"

DEFUN(lnk_block, lnk_block_cmd,
      "link <0-100> <0-15> block",
      LINK_STR "Block it\n")
{
	struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1]));
	mtp_link_block(link);
	return CMD_SUCCESS;
}

DEFUN(lnk_unblock, lnk_unblock_cmd,
      "link <0-100> <0-15> unblock",
      LINK_STR "Unblock it\n")
{
	struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1]));
	mtp_link_unblock(link);
	return CMD_SUCCESS;
}

DEFUN(lnk_reset, lnk_reset_cmd,
      "link <0-100> <0-15> reset",
      LINK_STR "Reset it\n")
{
	struct mtp_link *link = FIND_LINK(vty, atoi(argv[0]), atoi(argv[1]));
	mtp_link_failure(link);
	return CMD_SUCCESS;
}

DEFUN(allow_inject, allow_inject_cmd,
      "allow-inject (0|1)",
      "Allow to inject messages\n" "Disable\n" "Enable\n")
{
	bsc->allow_inject = atoi(argv[0]);
	return CMD_SUCCESS;
}

DEFUN(show_sctp_count, show_sctp_count_cmd,
      "show sctp-connections count",
      SHOW_STR "Number of SCTP connections\n")
{
	int count = sctp_m2ua_conn_count(bsc->m2ua_trans);
	vty_out(vty, "Active SCTP connections are: %d.%s", count, VTY_NEWLINE);
	return CMD_SUCCESS;
}

DEFUN(show_sctp_details, show_sctp_details_cmd,
      "show sctp-connections details",
      SHOW_STR "Details of SCTP connections\n")
{
	struct sctp_m2ua_conn *conn;

	llist_for_each_entry(conn, &bsc->m2ua_trans->conns, entry) {
		vty_out(vty,
			"SCTP Conn ASP UP: %d, ident: %d,%d,%d,%d fd: %d ptr: %p.%s",
			conn->asp_up, conn->asp_ident[0], conn->asp_ident[1],
			conn->asp_ident[2], conn->asp_ident[3],
			conn->queue.bfd.fd, conn, VTY_NEWLINE);
	}

	return CMD_WARNING;
}

void cell_vty_init_cmds(void)
{
	/* special commands */
	install_element(ENABLE_NODE, &pcap_set_cmd);
	install_element(ENABLE_NODE, &pcap_set_stop_cmd);
	install_element(ENABLE_NODE, &lnk_block_cmd);
	install_element(ENABLE_NODE, &lnk_unblock_cmd);
	install_element(ENABLE_NODE, &lnk_reset_cmd);
	install_element(ENABLE_NODE, &allow_inject_cmd);

	/* show commands */
	install_element_ve(&show_stats_cmd);
	install_element_ve(&show_linksets_cmd);
	install_element_ve(&show_slc_cmd);

	install_element_ve(&show_msc_cmd);
	install_element_ve(&show_mscs_cmd);
	install_element_ve(&show_sctp_count_cmd);
	install_element_ve(&show_sctp_details_cmd);
}