blob: f7a0b1827438b6edff856f604a8199499c0f7d6e [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"
26
27#ifdef HAVE_E1D
28
29#include <errno.h>
30#include <unistd.h>
31#include <string.h>
32
33#include <osmocom/core/bits.h>
34#include <osmocom/core/logging.h>
35
36#include <osmocom/vty/vty.h>
37
38#include <osmocom/abis/subchan_demux.h>
39#include <osmocom/abis/e1_input.h>
40#include <osmocom/abis/lapd.h>
41
42#include <osmocom/e1d/proto.h>
43#include <osmocom/e1d/proto_clnt.h>
44
45
46#define TS_SIGN_ALLOC_SIZE 300
47
48struct osmo_e1dp_client *g_e1d;
49
50/* pre-declaration */
51extern struct e1inp_driver e1d_driver;
52static int e1d_want_write(struct e1inp_ts *e1i_ts);
53
54
55static int
56handle_ts_sign_read(struct osmo_fd *bfd)
57{
58 struct e1inp_line *line = bfd->data;
59 unsigned int ts_nr = bfd->priv_nr;
60 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
61 struct msgb *msg = msgb_alloc(TS_SIGN_ALLOC_SIZE, "E1D Signaling TS");
62 int ret;
63
64 if (!msg)
65 return -ENOMEM;
66
67 ret = read(bfd->fd, msg->data, TS_SIGN_ALLOC_SIZE - 16);
68 if (ret < 0) {
Harald Welte28898d12020-01-12 13:34:07 +010069 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020070 return ret;
71 }
72
73 msgb_put(msg, ret);
74 if (ret <= 1) {
Harald Welte28898d12020-01-12 13:34:07 +010075 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020076 return ret;
77 }
78
79 return e1inp_rx_ts_lapd(e1i_ts, msg);
80}
81
82static void
83timeout_ts_sign_write(void *data)
84{
85 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
86
87 /* trigger write of ts1, due to tx delay timer */
88 e1d_want_write(e1i_ts);
89}
90
91static int
92handle_ts_sign_write(struct osmo_fd *bfd)
93{
94 struct e1inp_line *line = bfd->data;
95 unsigned int ts_nr = bfd->priv_nr;
96 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
97 struct e1inp_sign_link *sign_link;
98 struct msgb *msg;
99
100 bfd->when &= ~BSC_FD_WRITE;
101
102 /* get the next msg for this timeslot */
103 msg = e1inp_tx_ts(e1i_ts, &sign_link);
104 if (!msg) {
105 /* no message after tx delay timer */
106 return 0;
107 }
108
109 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
110 lapd_transmit(e1i_ts->lapd, sign_link->tei,
111 sign_link->sapi, msg);
112
113 /* set tx delay timer for next event */
114 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts_sign_write, e1i_ts);
115 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
116
117 return 0;
118}
119
120
121static void
122e1d_write_msg(struct msgb *msg, void *cbdata)
123{
124 struct osmo_fd *bfd = cbdata;
125 struct e1inp_line *line = bfd->data;
126 unsigned int ts_nr = bfd->priv_nr;
127 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
128 int ret;
129
130 ret = write(bfd->fd, msg->data, msg->len);
131 msgb_free(msg);
132 if (ret < 0)
Harald Welte28898d12020-01-12 13:34:07 +0100133 LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Sylvain Munautb559a532019-05-09 11:14:26 +0200134}
135
136static int
137e1d_fd_cb(struct osmo_fd *bfd, unsigned int what)
138{
139 struct e1inp_line *line = bfd->data;
140 unsigned int ts_nr = bfd->priv_nr;
141 unsigned int idx = ts_nr-1;
142 struct e1inp_ts *e1i_ts = &line->ts[idx];
143 int ret = 0;
144
145 switch (e1i_ts->type) {
146 case E1INP_TS_TYPE_SIGN:
Sylvain Munautb559a532019-05-09 11:14:26 +0200147 if (what & BSC_FD_READ)
148 ret = handle_ts_sign_read(bfd);
149 if (what & BSC_FD_WRITE)
150 ret = handle_ts_sign_write(bfd);
151 break;
152 default:
Harald Welte28898d12020-01-12 13:34:07 +0100153 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown/unsupported E1 TS type %u\n", e1i_ts->type);
Sylvain Munautb559a532019-05-09 11:14:26 +0200154 break;
155 }
156
157 return ret;
158}
159
160
161static int
162e1d_want_write(struct e1inp_ts *e1i_ts)
163{
164 /* We never include the DAHDI B-Channel FD into the writeset */
165 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Harald Welte28898d12020-01-12 13:34:07 +0100166 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Sylvain Munautb559a532019-05-09 11:14:26 +0200167 return 0;
168 }
169
170 e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE;
171
172 return 0;
173}
174
175static int
176e1d_line_update(struct e1inp_line *line)
177{
178 int ts;
179 int ret;
180
Harald Welteccf84ea2020-01-12 12:31:45 +0100181 /* we use higher 4 bits for interface, lower 4 bits for line,
182 * resulting in max. 16 interfaces with 16 lines each */
183 uint8_t e1d_intf = (line->port_nr >> 4) & 0xF;
184 uint8_t e1d_line = line->port_nr & 0xF;
Harald Welteeed39162020-01-12 13:46:34 +0100185 struct osmo_e1dp_ts_info *ts_info;
186 int num_ts_info;
Harald Welteccf84ea2020-01-12 12:31:45 +0100187
Sylvain Munautb559a532019-05-09 11:14:26 +0200188 if (line->driver != &e1d_driver)
189 return -EINVAL;
190
Harald Welteaccd6772020-01-12 13:36:56 +0100191 if (!g_e1d) {
192 /* Connect to daemon */
193 g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl");
194 if (!g_e1d) {
195 LOGPIL(line, DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n");
196 return -EPIPE;
197 }
198 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200199
Harald Welte28898d12020-01-12 13:34:07 +0100200 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 +0100201 e1d_intf, e1d_line, line->num_ts);
Sylvain Munautb559a532019-05-09 11:14:26 +0200202
Harald Welteeed39162020-01-12 13:46:34 +0100203 ret = osmo_e1dp_client_ts_query(g_e1d, &ts_info, &num_ts_info, e1d_intf, e1d_line, E1DP_INVALID);
204 if (ret < 0) {
205 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot query E1D for timeslot information: %d\n", ret);
206 return -EIO;
207 }
208
Sylvain Munautb559a532019-05-09 11:14:26 +0200209 for (ts=1; ts<line->num_ts; ts++)
210 {
211 unsigned int idx = ts-1;
212 struct e1inp_ts *e1i_ts = &line->ts[idx];
213 struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd;
214
215 /* unregister FD if it was already registered */
216 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
217 osmo_fd_unregister(bfd);
218
219 bfd->data = line;
220 bfd->priv_nr = ts;
221 bfd->cb = e1d_fd_cb;
222
Harald Welteeed39162020-01-12 13:46:34 +0100223 if (e1i_ts->type != E1INP_TS_TYPE_NONE && ts >= num_ts_info) {
224 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Timeslot configured, but not existant "
225 "on E1D side; skipping\n");
226 continue;
227 }
228
Sylvain Munautb559a532019-05-09 11:14:26 +0200229 switch (e1i_ts->type) {
230 case E1INP_TS_TYPE_NONE:
231 /* close/release LAPD instance, if any */
232 if (e1i_ts->lapd) {
233 lapd_instance_free(e1i_ts->lapd);
234 e1i_ts->lapd = NULL;
235 }
236 if (bfd->fd) {
237 close(bfd->fd);
238 bfd->fd = 0;
239 }
240 continue;
241 case E1INP_TS_TYPE_SIGN:
Harald Welteeed39162020-01-12 13:46:34 +0100242 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) {
243 close(bfd->fd);
244 bfd->fd = 0;
245 }
Harald Welteccf84ea2020-01-12 12:31:45 +0100246 if (bfd->fd <= 0) {
247 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
248 E1DP_TSMODE_HDLCFCS);
249 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200250 if (bfd->fd < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100251 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
Harald Welteeed39162020-01-12 13:46:34 +0100252 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200253 return -EIO;
254 }
Harald Welte6f674de2020-01-12 12:18:26 +0100255 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200256
Harald Welteb9031882020-05-02 21:09:15 +0200257 if (!e1i_ts->lapd) {
258 char name[32];
259 e1inp_ts_name(name, e1i_ts);
260 e1i_ts->lapd = lapd_instance_alloc2(1,
Sylvain Munautb559a532019-05-09 11:14:26 +0200261 e1d_write_msg, bfd, e1inp_dlsap_up,
Harald Welteb9031882020-05-02 21:09:15 +0200262 e1i_ts, &lapd_profile_abis, name);
263 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200264 break;
265 case E1INP_TS_TYPE_HDLC:
Harald Welteeed39162020-01-12 13:46:34 +0100266 /* close/release LAPD instance, if any */
267 if (e1i_ts->lapd) {
268 lapd_instance_free(e1i_ts->lapd);
269 e1i_ts->lapd = NULL;
270 }
271 /* close, if old timeslot mode doesn't match new config */
272 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) {
273 close(bfd->fd);
274 bfd->fd = 0;
275 }
276 if (bfd->fd <= 0) {
277 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
278 E1DP_TSMODE_HDLCFCS);
279 }
280 if (bfd->fd < 0) {
281 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
282 talloc_free(ts_info);
283 return -EIO;
284 }
285 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200286 break;
287 case E1INP_TS_TYPE_TRAU:
Sylvain Munautb559a532019-05-09 11:14:26 +0200288 case E1INP_TS_TYPE_RAW:
Harald Welteeed39162020-01-12 13:46:34 +0100289 /* close/release LAPD instance, if any */
290 if (e1i_ts->lapd) {
291 lapd_instance_free(e1i_ts->lapd);
292 e1i_ts->lapd = NULL;
293 }
294 /* close, if old timeslot mode doesn't match new config */
295 if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_RAW) {
296 close(bfd->fd);
297 bfd->fd = 0;
298 }
299 if (bfd->fd <= 0) {
300 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
301 E1DP_TSMODE_RAW);
302 }
303 if (bfd->fd < 0) {
304 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
305 talloc_free(ts_info);
306 return -EIO;
307 }
308 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200309 break;
310 };
311
312 ret = osmo_fd_register(bfd);
313 if (ret < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100314 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret));
Harald Welteeed39162020-01-12 13:46:34 +0100315 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200316 return ret;
317 }
318 }
319
Harald Welteeed39162020-01-12 13:46:34 +0100320 talloc_free(ts_info);
Sylvain Munautb559a532019-05-09 11:14:26 +0200321 return 0;
322}
323
Sylvain Munautb559a532019-05-09 11:14:26 +0200324struct e1inp_driver e1d_driver = {
325 .name = "e1d",
326 .want_write = e1d_want_write,
327 .line_update = e1d_line_update,
Sylvain Munautb559a532019-05-09 11:14:26 +0200328};
329
330int
331e1inp_e1d_init(void)
332{
Sylvain Munautb559a532019-05-09 11:14:26 +0200333 /* register the driver with the core */
334 return e1inp_driver_register(&e1d_driver);
335}
336
337#endif /* HAVE_E1D */