blob: 4f287ae92c0386546c85cd8cb49684681b40d316 [file] [log] [blame]
Alexander Couzensbeb10ef2016-11-01 22:05:13 +01001/* OpenBSC Abis receive lapd over a unix socket */
2
3/* (C) 2016 by sysmocom s.f.m.c. GmbH
4 *
5 * Author: Alexander Couzens <lynxis@fe80.eu>
6 * Based on other e1_input drivers.
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <sys/socket.h>
30#include <limits.h>
31#include <string.h>
32
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/logging.h>
35#include <osmocom/core/socket.h>
36
37#include <osmocom/abis/e1_input.h>
38#include <osmocom/abis/lapd.h>
39#include <osmocom/abis/e1_input.h>
40
41#include <osmocom/abis/unixsocket_proto.h>
42#include "internal.h"
43
44void *tall_unixsocket_ctx;
45#define UNIXSOCKET_ALLOC_SIZE 1600
46#define UNIXSOCKET_SOCK_PATH_DEFAULT "/tmp/osmo_abis_line_"
47
48struct unixsocket_line {
49 struct osmo_fd fd;
50};
51
52static int unixsocket_line_update(struct e1inp_line *line);
53static int ts_want_write(struct e1inp_ts *e1i_ts);
54
55static int unixsocket_exception_cb(struct osmo_fd *bfd)
56{
57 struct e1inp_line *line = bfd->data;
58
59 LOGP(DLINP, LOGL_ERROR,
60 "Socket connection failure, reconnecting... (line=%p, fd=%d)\n",
61 line, bfd->fd);
62
63 /* Unregister faulty file descriptor from select loop */
64 if(osmo_fd_is_registered(bfd)) {
65 LOGP(DLINP, LOGL_DEBUG,
66 "removing inactive socket from select loop... (line=%p, fd=%d)\n",
67 line, bfd->fd);
68 osmo_fd_unregister(bfd);
69 }
70
71 /* Close faulty file descriptor */
72 close(bfd->fd);
73
74 unixsocket_line_update(line);
75
76 return 0;
77}
78
79static int unixsocket_read_cb(struct osmo_fd *bfd)
80{
81 struct e1inp_line *line = bfd->data;
82 struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
83 uint8_t version;
84 uint8_t controldata;
85 int ret;
86
87 if (!msg)
88 return -ENOMEM;
89
90 ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
91 if (ret == 0) {
92 unixsocket_exception_cb(bfd);
93 goto fail;
94 } else if (ret < 0) {
95 perror("read ");
96 goto fail;
97 } else if (ret < 2) {
98 /* packet must be at least 2 byte long to hold version + control/data header */
99 LOGP(DLMI, LOGL_ERROR, "received to small packet: %d < 2", ret);
100 ret = -1;
101 goto fail;
102 }
103 msgb_put(msg, ret);
104
105 LOGP(DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n",
106 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
107
108 /* check version header */
109 version = msgb_pull_u8(msg);
110 controldata = msgb_pull_u8(msg);
111
112 if (version != UNIXSOCKET_PROTO_VERSION) {
113 LOGP(DLMI, LOGL_ERROR, "received message with invalid version %d. valid: %d",
114 ret, UNIXSOCKET_PROTO_VERSION);
115 ret = -1;
116 goto fail;
117 }
118
119 switch (controldata) {
120 case UNIXSOCKET_PROTO_DATA:
121 return e1inp_rx_ts_lapd(&line->ts[0], msg);
122 case UNIXSOCKET_PROTO_CONTROL:
123 LOGP(DLMI, LOGL_ERROR, "received (invalid) control message.");
124 ret = -1;
125 break;
126 default:
127 LOGP(DLMI, LOGL_ERROR, "received invalid message.");
128 ret = -1;
129 break;
130 }
131fail:
132 msgb_free(msg);
133 return ret;
134}
135
136static void timeout_ts1_write(void *data)
137{
138 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
139
140 /* trigger write of ts1, due to tx delay timer */
141 ts_want_write(e1i_ts);
142}
143
144static int unixsocket_write_cb(struct osmo_fd *bfd)
145{
146 struct e1inp_line *line = bfd->data;
147 struct e1inp_ts *e1i_ts = &line->ts[0];
148 struct msgb *msg;
149 struct e1inp_sign_link *sign_link;
150
151 bfd->when &= ~BSC_FD_WRITE;
152
153 /* get the next msg for this timeslot */
154 msg = e1inp_tx_ts(e1i_ts, &sign_link);
155 if (!msg) {
156 /* no message after tx delay timer */
157 LOGP(DLINP, LOGL_INFO,
158 "no message available (line=%p)\n", line);
159 return 0;
160 }
161
162 /* set tx delay timer for next event */
163 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
164 e1i_ts->sign.tx_timer.data = e1i_ts;
165
166 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
167
168 LOGP(DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n",
169 msgb_hexdump(msg), line);
170 lapd_transmit(e1i_ts->lapd, sign_link->tei,
171 sign_link->sapi, msg);
172
173 return 0;
174}
175
176static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
177{
178 int ret = 0;
179
180 if (what & BSC_FD_READ)
181 ret = unixsocket_read_cb(bfd);
182 if (what & BSC_FD_WRITE)
183 ret = unixsocket_write_cb(bfd);
184
185 return ret;
186}
187
188static int ts_want_write(struct e1inp_ts *e1i_ts)
189{
190 struct unixsocket_line *line = e1i_ts->line->driver_data;
191
192 line->fd.when |= BSC_FD_WRITE;
193
194 return 0;
195}
196
197static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd) {
198 int ret;
199
200 LOGP(DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
201 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
202
203 ret = write(bfd->fd, msg->data, msg->len);
204 msgb_free(msg);
205 if (ret == -1)
206 unixsocket_exception_cb(bfd);
207 else if (ret < 0)
208 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
209}
210
211/*!
212 * \brief unixsocket_write_msg lapd callback for data to unixsocket
213 * \param msg
214 * \param cbdata
215 */
216static void unixsocket_write_msg_lapd_cb(struct msgb *msg, void *cbdata)
217{
218 struct osmo_fd *bfd = cbdata;
219
220 /* data|control */
221 msgb_push_u8(msg, UNIXSOCKET_PROTO_DATA);
222 /* add version header */
223 msgb_push_u8(msg, UNIXSOCKET_PROTO_VERSION);
224
225 unixsocket_write_msg(msg, bfd);
226}
227
228static int unixsocket_line_update(struct e1inp_line *line)
229{
230 struct unixsocket_line *config;
231 char sock_path[PATH_MAX];
232 int ret = 0;
233 int i;
234
235 if (line->sock_path)
236 strcpy(sock_path, line->sock_path);
237 else
238 sprintf(sock_path, "%s%d", UNIXSOCKET_SOCK_PATH_DEFAULT,
239 line->num);
240
241 LOGP(DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);
242
243 if (!line->driver_data)
244 line->driver_data = talloc_zero(line, struct unixsocket_line);
245
246 if (!line->driver_data) {
247 LOGP(DLINP, LOGL_ERROR,
248 "OOM in line update (line=%p)\n", line);
249 return -ENOMEM;
250 }
251
252 config = line->driver_data;
253 config->fd.data = line;
254 config->fd.when = BSC_FD_READ;
255 config->fd.cb = unixsocket_cb;
256
257 /* Open unix domain socket */
258 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
259 OSMO_SOCK_F_CONNECT);
260 if (ret < 0) {
261 /* Note: We will not free the allocated driver_data memory if
262 * opening the socket fails. The caller may want to call this
263 * function multiple times using config->fd.data as line
264 * parameter. Freeing now would destroy that reference. */
265 LOGP(DLINP, LOGL_ERROR,
266 "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
267 line, config->fd.fd);
268 return ret;
269 }
270 LOGP(DLINP, LOGL_DEBUG,
271 "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
272 sock_path, line, config->fd.fd, ret);
273 config->fd.fd = ret;
274
275 /* Register socket in select loop */
276 if (osmo_fd_register(&config->fd) < 0) {
277 LOGP(DLINP, LOGL_ERROR,
278 "error registering new socket (line=%p, fd=%d)\n",
279 line, config->fd.fd);
280 close(config->fd.fd);
281 return -EIO;
282 }
283
284 /* Set line parameter */
285 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
286 struct e1inp_ts *e1i_ts = &line->ts[i];
287 if (!e1i_ts->lapd) {
288 e1i_ts->lapd = lapd_instance_alloc(1,
289 unixsocket_write_msg_lapd_cb, &config->fd,
290 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
291 }
292 }
293
294 /* Ensure ericsson-superchannel is turned of when
295 * a new connection is made */
296 e1inp_ericsson_set_altc(line, 0);
297
298 return ret;
299}
300
301struct e1inp_driver unixsocket_driver = {
302 .name = "unixsocket",
303 .want_write = ts_want_write,
304 .line_update = unixsocket_line_update,
305 .default_delay = 0,
306};
307
308void e1inp_unixsocket_init(void)
309{
310 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
311 e1inp_driver_register(&unixsocket_driver);
312}
313
314void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
315{
316 struct unixsocket_line *config;
317 struct msgb *msg;
318
319 if (!unixline)
320 return;
321
322 if (unixline->driver != &unixsocket_driver) {
323 LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
324 return;
325 }
326
327 config = unixline->driver_data;
328 if (!config) {
329 LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
330 return;
331 }
332
333
334 msg = msgb_alloc_headroom(200, 100, "ALTC");
335
336 /* version header */
337 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
338 /* data|control */
339 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
340
341 /* magic */
342 msgb_put_u32(msg, 0x23004200);
343 msgb_put_u8(msg, superchannel ? 1 : 0);
344
345 unixsocket_write_msg(msg, &config->fd);
346}
347