blob: 20fb174bca576377a18ae6b2eff44b13220fc7a9 [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
Stefan Sperling5b22fb72018-02-14 19:46:33 +010074 pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
75 osmo_timer_schedule(&state->timer, 5, 0);
76}
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040077
78int pcu_sock_send(struct msgb *msg)
79{
80 struct pcu_sock_state *state = pcu_sock_state;
81 struct osmo_fd *conn_bfd;
82
83 if (!state) {
84 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
85 "message\n");
86 return -EINVAL;
87 }
88 conn_bfd = &state->conn_bfd;
89 if (conn_bfd->fd <= 0) {
90 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
91 "message\n");
92 return -EIO;
93 }
94 msgb_enqueue(&state->upqueue, msg);
95 conn_bfd->when |= BSC_FD_WRITE;
96
97 return 0;
98}
99
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200100static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400101{
102 struct osmo_fd *bfd = &state->conn_bfd;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200103 struct gprs_rlcmac_bts *bts = bts_main_data();
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200104 uint8_t trx, ts;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400105
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200106 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
107 (lost) ? "LOST" : "closed");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400108
109 close(bfd->fd);
110 bfd->fd = -1;
111 osmo_fd_unregister(bfd);
112
113 /* flush the queue */
114 while (!llist_empty(&state->upqueue)) {
115 struct msgb *msg = msgb_dequeue(&state->upqueue);
116 msgb_free(msg);
117 }
118
119 /* disable all slots, kick all TBFs */
120 for (trx = 0; trx < 8; trx++) {
Maxcad867e2016-04-21 14:35:55 +0200121#ifdef ENABLE_DIRECT_PHY
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100122 if (bts->trx[trx].fl1h) {
123 l1if_close_pdch(bts->trx[trx].fl1h);
124 bts->trx[trx].fl1h = NULL;
125 }
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100126#endif
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200127 for (ts = 0; ts < 8; ts++)
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200128 bts->trx[trx].pdch[ts].disable();
Max5a6bcfb2017-09-01 14:36:44 +0200129/* FIXME: NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c
130for the reset. */
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200131 gprs_rlcmac_tbf::free_all(&bts->trx[trx]);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400132 }
133
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200134 gprs_bssgp_destroy();
135 exit(0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400136}
137
138static int pcu_sock_read(struct osmo_fd *bfd)
139{
140 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
Vadim Yanitskiyc7849c22019-08-23 17:38:43 +0200141 struct gsm_pcu_if pcu_prim;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400142 int rc;
143
Vadim Yanitskiyc7849c22019-08-23 17:38:43 +0200144 rc = recv(bfd->fd, &pcu_prim, sizeof(pcu_prim), 0);
Vadim Yanitskiy9e5ef542019-08-23 18:03:14 +0200145 if (rc < 0 && errno == EAGAIN)
146 return 0; /* Try again later */
147 if (rc <= 0) {
148 pcu_sock_close(state, 1);
149 return -EIO;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400150 }
151
Vadim Yanitskiy9e5ef542019-08-23 18:03:14 +0200152 return pcu_rx(pcu_prim.msg_type, &pcu_prim);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400153}
154
155static int pcu_sock_write(struct osmo_fd *bfd)
156{
157 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
158 int rc;
159
160 while (!llist_empty(&state->upqueue)) {
161 struct msgb *msg, *msg2;
162 struct gsm_pcu_if *pcu_prim;
163
164 /* peek at the beginning of the queue */
165 msg = llist_entry(state->upqueue.next, struct msgb, list);
166 pcu_prim = (struct gsm_pcu_if *)msg->data;
167
168 bfd->when &= ~BSC_FD_WRITE;
169
170 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
171 if (!msgb_length(msg)) {
172 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
173 "bytes!\n", pcu_prim->msg_type);
174 goto dontsend;
175 }
176
177 /* try to send it over the socket */
178 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
179 if (rc == 0)
180 goto close;
181 if (rc < 0) {
182 if (errno == EAGAIN) {
183 bfd->when |= BSC_FD_WRITE;
184 break;
185 }
186 goto close;
187 }
188
189dontsend:
190 /* _after_ we send it, we can deueue */
191 msg2 = msgb_dequeue(&state->upqueue);
192 assert(msg == msg2);
193 msgb_free(msg);
194 }
195 return 0;
196
197close:
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200198 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400199
200 return -1;
201}
202
203static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
204{
205 int rc = 0;
206
207 if (flags & BSC_FD_READ)
208 rc = pcu_sock_read(bfd);
209 if (rc < 0)
210 return rc;
211
212 if (flags & BSC_FD_WRITE)
213 rc = pcu_sock_write(bfd);
214
215 return rc;
216}
217
218int pcu_l1if_open(void)
219{
220 struct pcu_sock_state *state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400221 int rc;
Pau Espin Pedrolc4178e52017-08-08 15:03:50 +0200222 struct gprs_rlcmac_bts *bts = bts_main_data();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400223
Harald Welte21848272015-11-12 01:07:41 +0100224 LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface to OsmoBTS\n");
225
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400226 state = pcu_sock_state;
227 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200228 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400229 if (!state)
230 return -ENOMEM;
231 INIT_LLIST_HEAD(&state->upqueue);
232 }
233
Vadim Yanitskiyaef6bf42019-08-23 16:50:01 +0200234 rc = osmo_sock_unix_init_ofd(&state->conn_bfd, SOCK_SEQPACKET, 0,
235 bts->pcu_sock_path, OSMO_SOCK_F_CONNECT);
236 if (rc < 0) {
237 LOGP(DL1IF, LOGL_ERROR, "Failed to connect to the BTS (%s). "
238 "Retrying...\n", bts->pcu_sock_path);
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100239 osmo_timer_setup(&state->timer, pcu_sock_timeout, NULL);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400240 osmo_timer_schedule(&state->timer, 5, 0);
241 return 0;
242 }
243
Vadim Yanitskiyaef6bf42019-08-23 16:50:01 +0200244 state->conn_bfd.cb = pcu_sock_cb;
245 state->conn_bfd.data = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400246
Maxb3df5862017-01-06 17:20:57 +0100247 LOGP(DL1IF, LOGL_NOTICE, "osmo-bts PCU socket %s has been connected\n",
Vadim Yanitskiyaef6bf42019-08-23 16:50:01 +0200248 bts->pcu_sock_path);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400249
250 pcu_sock_state = state;
251
Max0a8fae82017-03-08 18:53:30 +0100252 pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
253
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100254 /* Schedule a timer so we keep trying until the BTS becomes active. */
255 osmo_timer_setup(&state->timer, pcu_tx_txt_retry, NULL);
256 osmo_timer_schedule(&state->timer, 5, 0);
257
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400258 return 0;
259}
260
261void pcu_l1if_close(void)
262{
263 struct pcu_sock_state *state = pcu_sock_state;
264 struct osmo_fd *bfd;
265
266 if (!state)
267 return;
268
Holger Hans Peter Freyther51c57042013-07-13 11:52:50 +0200269 osmo_timer_del(&state->timer);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400270
271 bfd = &state->conn_bfd;
272 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200273 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400274 talloc_free(state);
275 pcu_sock_state = NULL;
276}