blob: beef93eb2d1e7aaee2938305252aeb73890d64cd [file] [log] [blame]
Sylvain Munautb559a532019-05-09 11:14:26 +02001/* OpenBSC Abis input driver for osmo-e1d */
2
3/* (C) 2019 by Sylvain Munaut <tnt@246tNt.com>
4 *
5 * All Rights Reserved
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include "config.h"
Harald Welteac290ee2020-08-02 10:43:42 +020026#include "internal.h"
Sylvain Munautb559a532019-05-09 11:14:26 +020027
28#ifdef HAVE_E1D
29
30#include <errno.h>
31#include <unistd.h>
32#include <string.h>
33
34#include <osmocom/core/bits.h>
35#include <osmocom/core/logging.h>
36
37#include <osmocom/vty/vty.h>
38
39#include <osmocom/abis/subchan_demux.h>
40#include <osmocom/abis/e1_input.h>
41#include <osmocom/abis/lapd.h>
42
43#include <osmocom/e1d/proto.h>
44#include <osmocom/e1d/proto_clnt.h>
45
46
47#define TS_SIGN_ALLOC_SIZE 300
48
49struct osmo_e1dp_client *g_e1d;
50
Harald Weltef7766692020-06-30 19:04:07 +020051static int invertbits = 1;
52
Sylvain Munautb559a532019-05-09 11:14:26 +020053/* pre-declaration */
54extern struct e1inp_driver e1d_driver;
55static int e1d_want_write(struct e1inp_ts *e1i_ts);
56
57
58static int
59handle_ts_sign_read(struct osmo_fd *bfd)
60{
61 struct e1inp_line *line = bfd->data;
62 unsigned int ts_nr = bfd->priv_nr;
63 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
64 struct msgb *msg = msgb_alloc(TS_SIGN_ALLOC_SIZE, "E1D Signaling TS");
65 int ret;
66
67 if (!msg)
68 return -ENOMEM;
69
70 ret = read(bfd->fd, msg->data, TS_SIGN_ALLOC_SIZE - 16);
71 if (ret < 0) {
Harald Welte28898d12020-01-12 13:34:07 +010072 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020073 return ret;
74 }
75
76 msgb_put(msg, ret);
77 if (ret <= 1) {
Harald Welte28898d12020-01-12 13:34:07 +010078 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020079 return ret;
80 }
81
82 return e1inp_rx_ts_lapd(e1i_ts, msg);
83}
84
85static void
86timeout_ts_sign_write(void *data)
87{
88 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
89
90 /* trigger write of ts1, due to tx delay timer */
91 e1d_want_write(e1i_ts);
92}
93
94static int
95handle_ts_sign_write(struct osmo_fd *bfd)
96{
97 struct e1inp_line *line = bfd->data;
98 unsigned int ts_nr = bfd->priv_nr;
99 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
100 struct e1inp_sign_link *sign_link;
101 struct msgb *msg;
102
103 bfd->when &= ~BSC_FD_WRITE;
104
105 /* get the next msg for this timeslot */
106 msg = e1inp_tx_ts(e1i_ts, &sign_link);
107 if (!msg) {
108 /* no message after tx delay timer */
109 return 0;
110 }
111
112 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
113 lapd_transmit(e1i_ts->lapd, sign_link->tei,
114 sign_link->sapi, msg);
115
116 /* set tx delay timer for next event */
117 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts_sign_write, e1i_ts);
118 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
119
120 return 0;
121}
122
Harald Weltef7766692020-06-30 19:04:07 +0200123#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
124
125static int
126handle_ts_trau_write(struct osmo_fd *bfd)
127{
128 struct e1inp_line *line = bfd->data;
129 unsigned int ts_nr = bfd->priv_nr;
130 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
131 uint8_t tx_buf[D_BCHAN_TX_GRAN];
132 struct subch_mux *mx = &e1i_ts->trau.mux;
133 int ret;
134
135 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
136
137 if (ret != D_BCHAN_TX_GRAN) {
138 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret);
139 if (ret < 0)
140 return ret;
141 }
142
143 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN TX: %s\n", osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
144
145 if (invertbits)
146 osmo_revbytebits_buf(tx_buf, ret);
147
148 ret = write(bfd->fd, tx_buf, ret);
149 if (ret < D_BCHAN_TX_GRAN)
150 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
151 ret, D_BCHAN_TX_GRAN);
152
153 return ret;
154}
155
156
157static int
158handle_ts_trau_read(struct osmo_fd *bfd)
159{
160 struct e1inp_line *line = bfd->data;
161 unsigned int ts_nr = bfd->priv_nr;
162 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
163 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "E1D Rx TSx");
164 int ret;
165
166 if (!msg)
167 return -ENOMEM;
168
169 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
170 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
171 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno));
172 return ret;
173 }
174
175 if (invertbits)
176 osmo_revbytebits_buf(msg->data, ret);
177
178 msgb_put(msg, ret);
179
180 msg->l2h = msg->data;
181 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
182 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
183 /* physical layer indicates that data has been sent,
184 * we thus can send some more data */
185 ret = handle_ts_trau_write(bfd);
186
187 return ret;
188}
189
Harald Welteca712962020-06-30 19:12:42 +0200190/* write to a raw channel TS */
191static int handle_ts_raw_write(struct osmo_fd *bfd)
192{
193 struct e1inp_line *line = bfd->data;
194 unsigned int ts_nr = bfd->priv_nr;
195 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
196 struct msgb *msg;
197 int ret;
198
199 /* get the next msg for this timeslot */
200 msg = e1inp_tx_ts(e1i_ts, NULL);
201 if (!msg)
202 return 0;
203
204 if (msg->len != D_BCHAN_TX_GRAN) {
205 /* This might lead to a transmit underrun, as we call tx
206 * from the rx path, as there's no select/poll on dahdi
207 * */
208 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unexpected msg->len = %u, "
209 "expected %u\n", msg->len, D_BCHAN_TX_GRAN);
210 }
211
212 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN TX: %s\n", osmo_hexdump(msg->data, msg->len));
213
214 if (0/*invertbits*/)
215 osmo_revbytebits_buf(msg->data, msg->len);
216
217 ret = write(bfd->fd, msg->data, msg->len);
218 if (ret < msg->len)
219 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", ret, msg->len);
220 msgb_free(msg);
221
222 return ret;
223}
224
225static int handle_ts_raw_read(struct osmo_fd *bfd)
226{
227 struct e1inp_line *line = bfd->data;
228 unsigned int ts_nr = bfd->priv_nr;
229 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
230 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "E1D Raw TS");
231 int ret;
232
233 if (!msg)
234 return -ENOMEM;
235
236 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
237 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
238 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno));
239 return ret;
240 }
241
242 if (0/*invertbits*/)
243 osmo_revbytebits_buf(msg->data, ret);
244
245 msgb_put(msg, ret);
246
247 msg->l2h = msg->data;
248 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
249 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
250 /* physical layer indicates that data has been sent,
251 * we thus can send some more data */
252 ret = handle_ts_raw_write(bfd);
253
254 return ret;
255}
Sylvain Munautb559a532019-05-09 11:14:26 +0200256
257static void
258e1d_write_msg(struct msgb *msg, void *cbdata)
259{
260 struct osmo_fd *bfd = cbdata;
261 struct e1inp_line *line = bfd->data;
262 unsigned int ts_nr = bfd->priv_nr;
263 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
264 int ret;
265
266 ret = write(bfd->fd, msg->data, msg->len);
267 msgb_free(msg);
268 if (ret < 0)
Harald Welte28898d12020-01-12 13:34:07 +0100269 LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Sylvain Munautb559a532019-05-09 11:14:26 +0200270}
271
272static int
273e1d_fd_cb(struct osmo_fd *bfd, unsigned int what)
274{
275 struct e1inp_line *line = bfd->data;
276 unsigned int ts_nr = bfd->priv_nr;
277 unsigned int idx = ts_nr-1;
278 struct e1inp_ts *e1i_ts = &line->ts[idx];
279 int ret = 0;
280
281 switch (e1i_ts->type) {
282 case E1INP_TS_TYPE_SIGN:
Sylvain Munautb559a532019-05-09 11:14:26 +0200283 if (what & BSC_FD_READ)
284 ret = handle_ts_sign_read(bfd);
285 if (what & BSC_FD_WRITE)
286 ret = handle_ts_sign_write(bfd);
287 break;
Harald Weltef7766692020-06-30 19:04:07 +0200288 case E1INP_TS_TYPE_TRAU:
289 if (what & BSC_FD_READ)
290 ret = handle_ts_trau_read(bfd);
291 if (what & BSC_FD_WRITE)
292 ret = handle_ts_trau_write(bfd);
293 break;
Harald Welteca712962020-06-30 19:12:42 +0200294 case E1INP_TS_TYPE_RAW:
295 if (what & BSC_FD_READ)
296 ret = handle_ts_raw_read(bfd);
297 if (what & BSC_FD_WRITE)
298 ret = handle_ts_raw_write(bfd);
299 break;
Sylvain Munautb559a532019-05-09 11:14:26 +0200300 default:
Harald Welte28898d12020-01-12 13:34:07 +0100301 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown/unsupported E1 TS type %u\n", e1i_ts->type);
Sylvain Munautb559a532019-05-09 11:14:26 +0200302 break;
303 }
304
305 return ret;
306}
307
308
309static int
310e1d_want_write(struct e1inp_ts *e1i_ts)
311{
312 /* We never include the DAHDI B-Channel FD into the writeset */
313 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Harald Welte28898d12020-01-12 13:34:07 +0100314 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Sylvain Munautb559a532019-05-09 11:14:26 +0200315 return 0;
316 }
317
318 e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE;
319
320 return 0;
321}
322
323static int
324e1d_line_update(struct e1inp_line *line)
325{
326 int ts;
327 int ret;
328
Harald Welteccf84ea2020-01-12 12:31:45 +0100329 /* we use higher 4 bits for interface, lower 4 bits for line,
330 * resulting in max. 16 interfaces with 16 lines each */
331 uint8_t e1d_intf = (line->port_nr >> 4) & 0xF;
332 uint8_t e1d_line = line->port_nr & 0xF;
Harald Welteeed39162020-01-12 13:46:34 +0100333 struct osmo_e1dp_ts_info *ts_info;
334 int num_ts_info;
Harald Welteccf84ea2020-01-12 12:31:45 +0100335
Sylvain Munautb559a532019-05-09 11:14:26 +0200336 if (line->driver != &e1d_driver)
337 return -EINVAL;
338
Harald Welteaccd6772020-01-12 13:36:56 +0100339 if (!g_e1d) {
340 /* Connect to daemon */
341 g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl");
342 if (!g_e1d) {
343 LOGPIL(line, DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n");
344 return -EPIPE;
345 }
346 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200347
Harald Welte28898d12020-01-12 13:34:07 +0100348 LOGPIL(line, DLINP, LOGL_NOTICE, "Line update %d %d=E1D(%d:%d) %d\n", line->num, line->port_nr,
Harald Welteccf84ea2020-01-12 12:31:45 +0100349 e1d_intf, e1d_line, line->num_ts);
Sylvain Munautb559a532019-05-09 11:14:26 +0200350
Harald Welteeed39162020-01-12 13:46:34 +0100351 ret = osmo_e1dp_client_ts_query(g_e1d, &ts_info, &num_ts_info, e1d_intf, e1d_line, E1DP_INVALID);
352 if (ret < 0) {
353 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot query E1D for timeslot information: %d\n", ret);
354 return -EIO;
355 }
356
Sylvain Munautb559a532019-05-09 11:14:26 +0200357 for (ts=1; ts<line->num_ts; ts++)
358 {
359 unsigned int idx = ts-1;
360 struct e1inp_ts *e1i_ts = &line->ts[idx];
361 struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd;
362
363 /* unregister FD if it was already registered */
364 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
365 osmo_fd_unregister(bfd);
366
367 bfd->data = line;
368 bfd->priv_nr = ts;
369 bfd->cb = e1d_fd_cb;
370
Harald Welteeed39162020-01-12 13:46:34 +0100371 if (e1i_ts->type != E1INP_TS_TYPE_NONE && ts >= num_ts_info) {
372 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Timeslot configured, but not existant "
373 "on E1D side; skipping\n");
374 continue;
375 }
376
Sylvain Munautb559a532019-05-09 11:14:26 +0200377 switch (e1i_ts->type) {
378 case E1INP_TS_TYPE_NONE:
379 /* close/release LAPD instance, if any */
380 if (e1i_ts->lapd) {
381 lapd_instance_free(e1i_ts->lapd);
382 e1i_ts->lapd = NULL;
383 }
384 if (bfd->fd) {
385 close(bfd->fd);
386 bfd->fd = 0;
387 }
388 continue;
389 case E1INP_TS_TYPE_SIGN:
Harald Welteeed39162020-01-12 13:46:34 +0100390 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) {
391 close(bfd->fd);
392 bfd->fd = 0;
393 }
Harald Welteccf84ea2020-01-12 12:31:45 +0100394 if (bfd->fd <= 0) {
395 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
Harald Welteac290ee2020-08-02 10:43:42 +0200396 E1DP_TSMODE_HDLCFCS, D_BCHAN_TX_GRAN);
Harald Welteccf84ea2020-01-12 12:31:45 +0100397 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200398 if (bfd->fd < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100399 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
Harald Welteeed39162020-01-12 13:46:34 +0100400 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200401 return -EIO;
402 }
Harald Welte6f674de2020-01-12 12:18:26 +0100403 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200404
Harald Welteb9031882020-05-02 21:09:15 +0200405 if (!e1i_ts->lapd) {
406 char name[32];
Harald Welte36166d02020-06-30 18:10:31 +0200407 e1inp_ts_name(name, sizeof(name), e1i_ts);
Harald Welteb9031882020-05-02 21:09:15 +0200408 e1i_ts->lapd = lapd_instance_alloc2(1,
Sylvain Munautb559a532019-05-09 11:14:26 +0200409 e1d_write_msg, bfd, e1inp_dlsap_up,
Harald Welteb9031882020-05-02 21:09:15 +0200410 e1i_ts, &lapd_profile_abis, name);
411 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200412 break;
413 case E1INP_TS_TYPE_HDLC:
Harald Welteeed39162020-01-12 13:46:34 +0100414 /* close/release LAPD instance, if any */
415 if (e1i_ts->lapd) {
416 lapd_instance_free(e1i_ts->lapd);
417 e1i_ts->lapd = NULL;
418 }
419 /* close, if old timeslot mode doesn't match new config */
420 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) {
421 close(bfd->fd);
422 bfd->fd = 0;
423 }
424 if (bfd->fd <= 0) {
425 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
Harald Welteac290ee2020-08-02 10:43:42 +0200426 E1DP_TSMODE_HDLCFCS, D_BCHAN_TX_GRAN);
Harald Welteeed39162020-01-12 13:46:34 +0100427 }
428 if (bfd->fd < 0) {
429 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
430 talloc_free(ts_info);
431 return -EIO;
432 }
433 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200434 break;
435 case E1INP_TS_TYPE_TRAU:
Sylvain Munautb559a532019-05-09 11:14:26 +0200436 case E1INP_TS_TYPE_RAW:
Harald Welteeed39162020-01-12 13:46:34 +0100437 /* close/release LAPD instance, if any */
438 if (e1i_ts->lapd) {
439 lapd_instance_free(e1i_ts->lapd);
440 e1i_ts->lapd = NULL;
441 }
442 /* close, if old timeslot mode doesn't match new config */
443 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_RAW) {
444 close(bfd->fd);
445 bfd->fd = 0;
446 }
447 if (bfd->fd <= 0) {
448 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
Harald Welteac290ee2020-08-02 10:43:42 +0200449 E1DP_TSMODE_RAW, D_BCHAN_TX_GRAN);
Harald Welteeed39162020-01-12 13:46:34 +0100450 }
451 if (bfd->fd < 0) {
452 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
453 talloc_free(ts_info);
454 return -EIO;
455 }
456 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200457 break;
458 };
459
460 ret = osmo_fd_register(bfd);
461 if (ret < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100462 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret));
Harald Welteeed39162020-01-12 13:46:34 +0100463 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200464 return ret;
465 }
466 }
467
Harald Welteeed39162020-01-12 13:46:34 +0100468 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200469 return 0;
470}
471
Sylvain Munautb559a532019-05-09 11:14:26 +0200472struct e1inp_driver e1d_driver = {
473 .name = "e1d",
474 .want_write = e1d_want_write,
475 .line_update = e1d_line_update,
Sylvain Munautb559a532019-05-09 11:14:26 +0200476};
477
478int
479e1inp_e1d_init(void)
480{
Sylvain Munautb559a532019-05-09 11:14:26 +0200481 /* register the driver with the core */
482 return e1inp_driver_register(&e1d_driver);
483}
484
485#endif /* HAVE_E1D */