aboutsummaryrefslogtreecommitdiffstats
path: root/src/intf_line.c
blob: a645cd3b89c5af82c828ef2f4c7b5fde2c9ec917 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * intf_line.c
 *
 * (C) 2019 by Sylvain Munaut <tnt@246tNt.com>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <talloc.h>

#include <osmocom/core/isdnhdlc.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/stats.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/stat_item.h>
#include <osmocom/core/timer.h>
#include <osmocom/e1d/proto.h>

#include "e1d.h"
#include "log.h"
#include <osmocom/octoi/octoi.h>

const struct value_string e1_driver_names[] = {
	{ E1_DRIVER_USB, "usb" },
	{ E1_DRIVER_VPAIR, "vpair" },
	{ 0, NULL }
};

static const struct rate_ctr_desc line_ctr_description[] = {
	[LINE_CTR_LOS] =	{ "rx:signal_lost",		"Rx Signal Lost" },
	[LINE_CTR_LOA] =	{ "rx:alignment_lost",		"Rx Alignment Lost" },
	[LINE_CTR_CRC_ERR] =	{ "rx:crc_errors",		"E1 Rx CRC Errors" },
	[LINE_CTR_RX_OVFL] =	{ "rx:overflow",		"E1 Rx Overflow" },
	[LINE_CTR_TX_UNFL] =	{ "tx:underflow",		"E1 Tx Underflow" },
	[LINE_CTR_RX_REMOTE_E] ={ "rx:remote_crc_errors",	"Rx Frames Reporting Remote CRC Error"},
	[LINE_CTR_RX_REMOTE_A] ={ "rx:remote_alarm",		"Rx Frames Reporting Remote Alarm"},
	[LINE_CTR_FRAMES_MUXED_E1T] = { "tx:frames_muxed",	"E1 Tx Frames multiplexed" },
	[LINE_CTR_FRAMES_DEMUXED_E1O] = { "rx:frames_demuxed",	"E1 Rx Frames demultiplexed" },
};

static const struct rate_ctr_group_desc line_ctrg_desc = {
	.group_name_prefix = "e1d_line",
	.group_description = "Counters for each line in e1d",
	.class_id = OSMO_STATS_CLASS_GLOBAL,
	.num_ctr = ARRAY_SIZE(line_ctr_description),
	.ctr_desc = line_ctr_description,
};

static const struct osmo_stat_item_desc line_stat_description[] = {
	[LINE_GPSDO_STATE]	= { "gpsdo:state", "GPSDO State" },
	[LINE_GPSDO_ANTENNA]	= { "gpsdo:antenna", "GSPDO Antenna State" },
	[LINE_GPSDO_TUNE_COARSE]= { "gpsdo:tune:coarse", "GSPDO Coarse Tuning" },
	[LINE_GPSDO_TUNE_FINE]	= { "gpsdo:tune:fine", "GSPDO Fine Tuning" },
	[LINE_GPSDO_FREQ_EST]	= { "gpsdo:freq_est", "GSPDO Frequency Estimate" },
};

static const struct osmo_stat_item_group_desc line_stats_desc = {
	.group_name_prefix = "e1d_line",
	.group_description = "Stat items for E1 line",
	.class_id = OSMO_STATS_CLASS_GLOBAL,
	.num_items = ARRAY_SIZE(line_stat_description),
	.item_desc = line_stat_description,
};


/* watchdog timer, called once per second to check if we still receive data on the line */
static void line_watchdog_cb(void *data)
{
	struct e1_line *line = data;

	if (line->watchdog.rx_bytes < 240000) {
		LOGPLI(line, DE1D, LOGL_ERROR, "Received Only %u bytes/s (expected: 262144): Line dead?\n",
			line->watchdog.rx_bytes);
	}

	line->watchdog.rx_bytes = 0;
	osmo_timer_schedule(&line->watchdog.timer, 1, 0);
}

// ---------------------------------------------------------------------------
// e1d structures
// ---------------------------------------------------------------------------

struct e1_intf *
e1d_find_intf(struct e1_daemon *e1d, uint8_t id)
{
	struct e1_intf *intf;

	llist_for_each_entry(intf, &e1d->interfaces, list)
		if (intf->id == id)
			return intf;

	return NULL;
}

struct e1_intf *
e1d_find_intf_by_usb_serial(struct e1_daemon *e1d, const char *serial_str)
{
	struct e1_intf *intf;

	if (!serial_str)
		return NULL;

	llist_for_each_entry(intf, &e1d->interfaces, list) {
		if (intf->usb.serial_str && !strcmp(intf->usb.serial_str, serial_str))
			return intf;
	}

	return NULL;
}

struct e1_line *
e1_intf_find_line(struct e1_intf *intf, uint8_t id)
{
	struct e1_line *line;

	llist_for_each_entry(line, &intf->lines, list) {
		if (line->id == id)
			return line;
	}

	return NULL;
}

/* intf_id can be specified as '-1' to mean "auto-allocate intf->id" */
struct e1_intf *
e1_intf_new(struct e1_daemon *e1d, int intf_id, void *drv_data)
{
	struct e1_intf *intf;

	if (intf_id != -1) {
		/* ensure non-duplicate interface number */
		intf = e1d_find_intf(e1d, intf_id);
		if (intf) {
			LOGPIF(intf, DE1D, LOGL_ERROR, "Cannot create duplicate interface %d\n", intf_id);
			return NULL;
		}
	}

	intf = talloc_zero(e1d->ctx, struct e1_intf);
	OSMO_ASSERT(intf);

	intf->e1d = e1d;
	intf->drv_data = drv_data;

	INIT_LLIST_HEAD(&intf->list);
	INIT_LLIST_HEAD(&intf->lines);

	if (intf_id == -1) {
		if (!llist_empty(&e1d->interfaces)) {
			struct e1_intf *f = llist_last_entry(&e1d->interfaces, struct e1_intf, list);
			intf->id = f->id + 1;
		} else
			intf->id = 0;
	} else
		intf->id = intf_id;

	llist_add_tail(&intf->list, &e1d->interfaces);

	LOGPIF(intf, DE1D, LOGL_NOTICE, "Created\n");

	return intf;
}

void
e1_intf_destroy(struct e1_intf *intf)
{
	struct e1_line *line, *line2;

	LOGPIF(intf, DE1D, LOGL_NOTICE, "Destroying\n");

	/* destroy all lines */
	llist_for_each_entry_safe(line, line2, &intf->lines, list)
		e1_line_destroy(line);

	/* remove from global list of interfaces */
	llist_del(&intf->list);

	talloc_free(intf);
}

static void
_ts0_tmr_cb(void *_line)
{
	struct e1_line *line = (struct e1_line *) _line;

	if ((line->ts0.cur_errmask & E1L_TS0_RX_CRC4_ERR) !=
	    (line->ts0.prev_errmask & E1L_TS0_RX_CRC4_ERR)) {
		LOGPLI(line, DE1D, LOGL_NOTICE, "Remote CRC4 Error report %s\n",
			line->ts0.cur_errmask & E1L_TS0_RX_CRC4_ERR ? "STARTED" : "CEASED");
	}

	if ((line->ts0.cur_errmask & E1L_TS0_RX_ALARM) !=
	    (line->ts0.prev_errmask & E1L_TS0_RX_ALARM)) {
		LOGPLI(line, DE1D, LOGL_NOTICE, "Remote ALARM condition %s\n",
			line->ts0.cur_errmask & E1L_TS0_RX_ALARM ? "STARTED" : "CEASED");
	}

	line->ts0.prev_errmask = line->ts0.cur_errmask;
	line->ts0.cur_errmask = 0;
	osmo_timer_schedule(&line->ts0.timer, 1, 0);
}

static void
_ts_init(struct e1_ts *ts, struct e1_line *line, int id)
{
	ts->line = line;
	ts->id = id;
	ts->fd = -1;
}

/* line_id can be specified as '-1' to mean "auto-allocate intf->id" */
struct e1_line *
e1_line_new(struct e1_intf *intf, int line_id, void *drv_data)
{
	struct e1_line *line;
	char name[32];

	if (line_id != -1) {
		line = e1_intf_find_line(intf, line_id);
		if (line) {
			LOGPLI(line, DE1D, LOGL_ERROR, "Cannot create duplicate line %d\n", line_id);
			return NULL;
		}
	}

	line = talloc_zero(intf->e1d->ctx, struct e1_line);
	OSMO_ASSERT(line);

	line->intf = intf;
	line->drv_data = drv_data;
	line->mode = E1_LINE_MODE_CHANNELIZED;

	for (int i = 0; i < 32; i++)
		_ts_init(&line->ts[i], line, i);
	_ts_init(&line->superchan, line, E1DP_TS_SUPERCHAN);

	INIT_LLIST_HEAD(&line->list);

	if (line_id == -1) {
		if (!llist_empty(&intf->lines)) {
			struct e1_line *l = llist_last_entry(&intf->lines, struct e1_line, list);
			line->id = l->id + 1;
		} else
			line->id = 0;
	} else
		line->id = line_id;

	line->ctrs = rate_ctr_group_alloc(line, &line_ctrg_desc, intf->id << 8 | line->id);
	OSMO_ASSERT(line->ctrs);

	line->stats = osmo_stat_item_group_alloc(line, &line_stats_desc, intf->id << 8 | line->id);
	OSMO_ASSERT(line->stats);

	snprintf(name, sizeof(name), "I%u:L%u", intf->id, line->id);
	rate_ctr_group_set_name(line->ctrs, name);
	osmo_stat_item_group_set_name(line->stats, name);

	llist_add_tail(&line->list, &intf->lines);

	LOGPLI(line, DE1D, LOGL_NOTICE, "Created\n");

	return line;
}

/* find an octoi client (if any) for the given line */
static struct octoi_client *octoi_client_by_line(struct e1_line *line)
{
	struct octoi_client *clnt;

	llist_for_each_entry(clnt, &g_octoi->clients, list) {
		struct octoi_account *acc = clnt->cfg.account;
		switch (acc->mode) {
		case ACCOUNT_MODE_ICE1USB:
			if (!strcmp(line->intf->usb.serial_str, acc->u.ice1usb.usb_serial) &&
			    line->id == acc->u.ice1usb.line_nr)
				return clnt;
			break;
		case ACCOUNT_MODE_NONE:
		case ACCOUNT_MODE_REDIRECT:
			break;
		default:
			OSMO_ASSERT(0);
		}
	}
	return NULL;
}

/* mark given line as 'active' (hardware present + enabled) */
void
e1_line_active(struct e1_line *line)
{
	struct octoi_client *clnt;

	LOGPLI(line, DE1D, LOGL_NOTICE, "Activated\n");

	osmo_timer_setup(&line->ts0.timer, _ts0_tmr_cb, line);
	osmo_timer_schedule(&line->ts0.timer, 1, 0);

	/* start watchdog timer */
	osmo_timer_setup(&line->watchdog.timer, line_watchdog_cb, line);
	osmo_timer_schedule(&line->watchdog.timer, 1, 0);

	switch (line->mode) {
	case E1_LINE_MODE_E1OIP:
		OSMO_ASSERT(!line->octoi_peer);
		/* find a client for this line */
		clnt = octoi_client_by_line(line);
		if (!clnt)
			return;
		/* start the peer for this client */
		line->octoi_peer = octoi_client_get_peer(clnt);
		OSMO_ASSERT(line->octoi_peer);
		octoi_clnt_start_for_peer(line->octoi_peer, clnt->cfg.account);
		break;
	default:
		break;
	}
}

void
e1_line_destroy(struct e1_line *line)
{
	LOGPLI(line, DE1D, LOGL_NOTICE, "Destroying\n");

	osmo_timer_del(&line->watchdog.timer);

	/* close all [peer] file descriptors */
	for (int i = 0; i < 32; i++)
		e1_ts_stop(&line->ts[i]);

	/* remove from per-interface list of lines */
	llist_del(&line->list);

	talloc_free(line);
}