blob: 63bd796d95f3cd8d19ec6a63792517afef4d7e49 [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 */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200163 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100164
165 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
166
167 LOGP(DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n",
168 msgb_hexdump(msg), line);
169 lapd_transmit(e1i_ts->lapd, sign_link->tei,
170 sign_link->sapi, msg);
171
172 return 0;
173}
174
175static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
176{
177 int ret = 0;
178
179 if (what & BSC_FD_READ)
180 ret = unixsocket_read_cb(bfd);
181 if (what & BSC_FD_WRITE)
182 ret = unixsocket_write_cb(bfd);
183
184 return ret;
185}
186
187static int ts_want_write(struct e1inp_ts *e1i_ts)
188{
189 struct unixsocket_line *line = e1i_ts->line->driver_data;
190
191 line->fd.when |= BSC_FD_WRITE;
192
193 return 0;
194}
195
196static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd) {
197 int ret;
198
199 LOGP(DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
200 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
201
202 ret = write(bfd->fd, msg->data, msg->len);
203 msgb_free(msg);
204 if (ret == -1)
205 unixsocket_exception_cb(bfd);
206 else if (ret < 0)
207 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
208}
209
210/*!
211 * \brief unixsocket_write_msg lapd callback for data to unixsocket
212 * \param msg
213 * \param cbdata
214 */
215static void unixsocket_write_msg_lapd_cb(struct msgb *msg, void *cbdata)
216{
217 struct osmo_fd *bfd = cbdata;
218
219 /* data|control */
220 msgb_push_u8(msg, UNIXSOCKET_PROTO_DATA);
221 /* add version header */
222 msgb_push_u8(msg, UNIXSOCKET_PROTO_VERSION);
223
224 unixsocket_write_msg(msg, bfd);
225}
226
227static int unixsocket_line_update(struct e1inp_line *line)
228{
229 struct unixsocket_line *config;
230 char sock_path[PATH_MAX];
231 int ret = 0;
232 int i;
233
234 if (line->sock_path)
235 strcpy(sock_path, line->sock_path);
236 else
237 sprintf(sock_path, "%s%d", UNIXSOCKET_SOCK_PATH_DEFAULT,
238 line->num);
239
240 LOGP(DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);
241
242 if (!line->driver_data)
243 line->driver_data = talloc_zero(line, struct unixsocket_line);
244
245 if (!line->driver_data) {
246 LOGP(DLINP, LOGL_ERROR,
247 "OOM in line update (line=%p)\n", line);
248 return -ENOMEM;
249 }
250
251 config = line->driver_data;
252 config->fd.data = line;
253 config->fd.when = BSC_FD_READ;
254 config->fd.cb = unixsocket_cb;
255
256 /* Open unix domain socket */
257 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
258 OSMO_SOCK_F_CONNECT);
259 if (ret < 0) {
260 /* Note: We will not free the allocated driver_data memory if
261 * opening the socket fails. The caller may want to call this
262 * function multiple times using config->fd.data as line
263 * parameter. Freeing now would destroy that reference. */
264 LOGP(DLINP, LOGL_ERROR,
265 "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
266 line, config->fd.fd);
267 return ret;
268 }
269 LOGP(DLINP, LOGL_DEBUG,
270 "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
271 sock_path, line, config->fd.fd, ret);
272 config->fd.fd = ret;
273
274 /* Register socket in select loop */
275 if (osmo_fd_register(&config->fd) < 0) {
276 LOGP(DLINP, LOGL_ERROR,
277 "error registering new socket (line=%p, fd=%d)\n",
278 line, config->fd.fd);
279 close(config->fd.fd);
280 return -EIO;
281 }
282
283 /* Set line parameter */
284 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
285 struct e1inp_ts *e1i_ts = &line->ts[i];
286 if (!e1i_ts->lapd) {
287 e1i_ts->lapd = lapd_instance_alloc(1,
288 unixsocket_write_msg_lapd_cb, &config->fd,
289 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
290 }
291 }
292
293 /* Ensure ericsson-superchannel is turned of when
294 * a new connection is made */
295 e1inp_ericsson_set_altc(line, 0);
296
297 return ret;
298}
299
300struct e1inp_driver unixsocket_driver = {
301 .name = "unixsocket",
302 .want_write = ts_want_write,
303 .line_update = unixsocket_line_update,
304 .default_delay = 0,
305};
306
307void e1inp_unixsocket_init(void)
308{
309 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
310 e1inp_driver_register(&unixsocket_driver);
311}
312
313void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
314{
315 struct unixsocket_line *config;
316 struct msgb *msg;
317
318 if (!unixline)
319 return;
320
321 if (unixline->driver != &unixsocket_driver) {
322 LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
323 return;
324 }
325
326 config = unixline->driver_data;
327 if (!config) {
328 LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
329 return;
330 }
331
332
333 msg = msgb_alloc_headroom(200, 100, "ALTC");
334
335 /* version header */
336 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
337 /* data|control */
338 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
339
340 /* magic */
341 msgb_put_u32(msg, 0x23004200);
342 msgb_put_u8(msg, superchannel ? 1 : 0);
343
344 unixsocket_write_msg(msg, &config->fd);
345}
346