blob: 6b49347344cc388e64745173cd4476e550ca8821 [file] [log] [blame]
Harald Welted32cbbb2015-11-11 21:23:23 +01001/* osmobts_sock.cpp
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04002 *
3 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <stdio.h>
21#include <unistd.h>
22#include <stdlib.h>
23#include <string.h>
24#include <errno.h>
25#include <assert.h>
26#include <sys/socket.h>
27#include <sys/un.h>
28extern "C" {
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/select.h>
31#include <osmocom/core/msgb.h>
Max1187a772018-01-26 13:31:42 +010032#include <osmocom/core/linuxlist.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/timer.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040035}
36
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040037#include <pcu_l1_if.h>
38#include <gprs_debug.h>
39#include <gprs_bssgp_pcu.h>
Harald Welte68fc1272016-11-16 22:48:33 +010040#include <osmocom/pcu/pcuif_proto.h>
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020041#include <bts.h>
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +020042#include <tbf.h>
Max6dc90b82018-02-19 17:17:28 +010043#include <pdch.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040044
Andreas Eversberge266bd42012-07-13 14:00:21 +020045extern void *tall_pcu_ctx;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040046
Andreas Eversberg0f4541b2013-01-16 09:17:24 +010047extern "C" {
48int l1if_close_pdch(void *obj);
49}
50
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040051/*
Harald Welted32cbbb2015-11-11 21:23:23 +010052 * osmo-bts PCU socket functions
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040053 */
54
55struct pcu_sock_state {
56 struct osmo_fd conn_bfd; /* fd for connection to lcr */
57 struct osmo_timer_list timer; /* socket connect retry timer */
58 struct llist_head upqueue; /* queue for sending messages */
59} *pcu_sock_state = NULL;
60
Stefan Sperling5b22fb72018-02-14 19:46:33 +010061static void pcu_sock_timeout(void *_priv)
62{
63 pcu_l1if_open();
64}
65
66static void pcu_tx_txt_retry(void *_priv)
67{
68 struct gprs_rlcmac_bts *bts = bts_main_data();
69 struct pcu_sock_state *state = pcu_sock_state;
70
71 if (bts->active)
72 return;
73
74 LOGP(DL1IF, LOGL_INFO, "Sending version %s to BTS.\n", PACKAGE_VERSION);
75 pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
76 osmo_timer_schedule(&state->timer, 5, 0);
77}
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040078
79int pcu_sock_send(struct msgb *msg)
80{
81 struct pcu_sock_state *state = pcu_sock_state;
82 struct osmo_fd *conn_bfd;
83
84 if (!state) {
85 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
86 "message\n");
87 return -EINVAL;
88 }
89 conn_bfd = &state->conn_bfd;
90 if (conn_bfd->fd <= 0) {
91 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
92 "message\n");
93 return -EIO;
94 }
95 msgb_enqueue(&state->upqueue, msg);
96 conn_bfd->when |= BSC_FD_WRITE;
97
98 return 0;
99}
100
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200101static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400102{
103 struct osmo_fd *bfd = &state->conn_bfd;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200104 struct gprs_rlcmac_bts *bts = bts_main_data();
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200105 uint8_t trx, ts;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400106
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200107 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
108 (lost) ? "LOST" : "closed");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400109
110 close(bfd->fd);
111 bfd->fd = -1;
112 osmo_fd_unregister(bfd);
113
114 /* flush the queue */
115 while (!llist_empty(&state->upqueue)) {
116 struct msgb *msg = msgb_dequeue(&state->upqueue);
117 msgb_free(msg);
118 }
119
120 /* disable all slots, kick all TBFs */
121 for (trx = 0; trx < 8; trx++) {
Maxcad867e2016-04-21 14:35:55 +0200122#ifdef ENABLE_DIRECT_PHY
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100123 if (bts->trx[trx].fl1h) {
124 l1if_close_pdch(bts->trx[trx].fl1h);
125 bts->trx[trx].fl1h = NULL;
126 }
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100127#endif
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200128 for (ts = 0; ts < 8; ts++)
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200129 bts->trx[trx].pdch[ts].disable();
Max5a6bcfb2017-09-01 14:36:44 +0200130/* FIXME: NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c
131for the reset. */
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200132 gprs_rlcmac_tbf::free_all(&bts->trx[trx]);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400133 }
134
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200135 gprs_bssgp_destroy();
136 exit(0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400137}
138
139static int pcu_sock_read(struct osmo_fd *bfd)
140{
141 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
142 struct gsm_pcu_if *pcu_prim;
143 struct msgb *msg;
144 int rc;
145
146 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
147 if (!msg)
148 return -ENOMEM;
149
150 pcu_prim = (struct gsm_pcu_if *) msg->tail;
151
152 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
153 if (rc == 0)
154 goto close;
155
156 if (rc < 0) {
157 if (errno == EAGAIN)
158 return 0;
159 goto close;
160 }
161
162 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
163
164 /* as we always synchronously process the message in pcu_rx() and
165 * its callbacks, we can free the message here. */
166 msgb_free(msg);
167
168 return rc;
169
170close:
171 msgb_free(msg);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200172 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400173 return -1;
174}
175
176static int pcu_sock_write(struct osmo_fd *bfd)
177{
178 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
179 int rc;
180
181 while (!llist_empty(&state->upqueue)) {
182 struct msgb *msg, *msg2;
183 struct gsm_pcu_if *pcu_prim;
184
185 /* peek at the beginning of the queue */
186 msg = llist_entry(state->upqueue.next, struct msgb, list);
187 pcu_prim = (struct gsm_pcu_if *)msg->data;
188
189 bfd->when &= ~BSC_FD_WRITE;
190
191 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
192 if (!msgb_length(msg)) {
193 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
194 "bytes!\n", pcu_prim->msg_type);
195 goto dontsend;
196 }
197
198 /* try to send it over the socket */
199 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
200 if (rc == 0)
201 goto close;
202 if (rc < 0) {
203 if (errno == EAGAIN) {
204 bfd->when |= BSC_FD_WRITE;
205 break;
206 }
207 goto close;
208 }
209
210dontsend:
211 /* _after_ we send it, we can deueue */
212 msg2 = msgb_dequeue(&state->upqueue);
213 assert(msg == msg2);
214 msgb_free(msg);
215 }
216 return 0;
217
218close:
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200219 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400220
221 return -1;
222}
223
224static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
225{
226 int rc = 0;
227
228 if (flags & BSC_FD_READ)
229 rc = pcu_sock_read(bfd);
230 if (rc < 0)
231 return rc;
232
233 if (flags & BSC_FD_WRITE)
234 rc = pcu_sock_write(bfd);
235
236 return rc;
237}
238
239int pcu_l1if_open(void)
240{
241 struct pcu_sock_state *state;
242 struct osmo_fd *bfd;
243 struct sockaddr_un local;
244 unsigned int namelen;
245 int rc;
Pau Espin Pedrolc4178e52017-08-08 15:03:50 +0200246 struct gprs_rlcmac_bts *bts = bts_main_data();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400247
Harald Welte21848272015-11-12 01:07:41 +0100248 LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface to OsmoBTS\n");
249
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400250 state = pcu_sock_state;
251 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200252 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400253 if (!state)
254 return -ENOMEM;
255 INIT_LLIST_HEAD(&state->upqueue);
256 }
257
258 bfd = &state->conn_bfd;
259
260 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
261 if (bfd->fd < 0) {
Harald Welted32cbbb2015-11-11 21:23:23 +0100262 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU socket.\n");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400263 talloc_free(state);
264 return -1;
265 }
266
267 local.sun_family = AF_UNIX;
Stefan Sperling173d7fd2018-09-20 18:31:36 +0200268 if (osmo_strlcpy(local.sun_path, bts->pcu_sock_path, sizeof(local.sun_path)) >= sizeof(local.sun_path)) {
269 LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n",
270 sizeof(local.sun_path), bts->pcu_sock_path);
271 return -ENOSPC;
272 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400273
274 /* we use the same magic that X11 uses in Xtranssock.c for
275 * calculating the proper length of the sockaddr */
276#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
277 local.sun_len = strlen(local.sun_path);
278#endif
279#if defined(BSD44SOCKETS) || defined(SUN_LEN)
280 namelen = SUN_LEN(&local);
281#else
282 namelen = strlen(local.sun_path) +
283 offsetof(struct sockaddr_un, sun_path);
284#endif
285 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
286 if (rc != 0) {
Harald Welted32cbbb2015-11-11 21:23:23 +0100287 LOGP(DL1IF, LOGL_ERROR, "Failed to connect to the osmo-bts"
Pau Espin Pedrolda0a1942017-12-05 17:35:22 +0100288 " PCU socket (%s), delaying... '%s'\n",
289 strerror(errno), local.sun_path);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200290 pcu_sock_state = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400291 close(bfd->fd);
292 bfd->fd = -1;
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100293 osmo_timer_setup(&state->timer, pcu_sock_timeout, NULL);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400294 osmo_timer_schedule(&state->timer, 5, 0);
295 return 0;
296 }
297
298 bfd->when = BSC_FD_READ;
299 bfd->cb = pcu_sock_cb;
300 bfd->data = state;
301
302 rc = osmo_fd_register(bfd);
303 if (rc < 0) {
304 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
305 close(bfd->fd);
306 talloc_free(state);
307 return rc;
308 }
309
Maxb3df5862017-01-06 17:20:57 +0100310 LOGP(DL1IF, LOGL_NOTICE, "osmo-bts PCU socket %s has been connected\n",
311 local.sun_path);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400312
313 pcu_sock_state = state;
314
Max0a8fae82017-03-08 18:53:30 +0100315 LOGP(DL1IF, LOGL_INFO, "Sending version %s to BTS.\n", PACKAGE_VERSION);
316 pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
317
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100318 /* Schedule a timer so we keep trying until the BTS becomes active. */
319 osmo_timer_setup(&state->timer, pcu_tx_txt_retry, NULL);
320 osmo_timer_schedule(&state->timer, 5, 0);
321
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400322 return 0;
323}
324
325void pcu_l1if_close(void)
326{
327 struct pcu_sock_state *state = pcu_sock_state;
328 struct osmo_fd *bfd;
329
330 if (!state)
331 return;
332
Holger Hans Peter Freyther51c57042013-07-13 11:52:50 +0200333 osmo_timer_del(&state->timer);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400334
335 bfd = &state->conn_bfd;
336 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200337 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400338 talloc_free(state);
339 pcu_sock_state = NULL;
340}