blob: 2e2d9d39d1f30571ad733bde342fac12bf4c2e29 [file] [log] [blame]
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001/* sysmo_sock.cpp
2 *
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>
32}
33
34#include <gprs_rlcmac.h>
35#include <pcu_l1_if.h>
36#include <gprs_debug.h>
37#include <gprs_bssgp_pcu.h>
38#include <pcuif_proto.h>
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020039#include <bts.h>
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +020040#include <tbf.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040041
Andreas Eversberge266bd42012-07-13 14:00:21 +020042extern void *tall_pcu_ctx;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040043
Andreas Eversberg0f4541b2013-01-16 09:17:24 +010044extern "C" {
45int l1if_close_pdch(void *obj);
46}
47
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040048/*
49 * SYSMO-PCU socket functions
50 */
51
52struct pcu_sock_state {
53 struct osmo_fd conn_bfd; /* fd for connection to lcr */
54 struct osmo_timer_list timer; /* socket connect retry timer */
55 struct llist_head upqueue; /* queue for sending messages */
56} *pcu_sock_state = NULL;
57
58static void pcu_sock_timeout(void *_priv);
59
60int pcu_sock_send(struct msgb *msg)
61{
62 struct pcu_sock_state *state = pcu_sock_state;
63 struct osmo_fd *conn_bfd;
64
65 if (!state) {
66 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
67 "message\n");
68 return -EINVAL;
69 }
70 conn_bfd = &state->conn_bfd;
71 if (conn_bfd->fd <= 0) {
72 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
73 "message\n");
74 return -EIO;
75 }
76 msgb_enqueue(&state->upqueue, msg);
77 conn_bfd->when |= BSC_FD_WRITE;
78
79 return 0;
80}
81
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020082static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040083{
84 struct osmo_fd *bfd = &state->conn_bfd;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020085 struct gprs_rlcmac_bts *bts = bts_main_data();
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +020086 uint8_t trx, ts;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040087
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020088 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
89 (lost) ? "LOST" : "closed");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040090
91 close(bfd->fd);
92 bfd->fd = -1;
93 osmo_fd_unregister(bfd);
94
95 /* flush the queue */
96 while (!llist_empty(&state->upqueue)) {
97 struct msgb *msg = msgb_dequeue(&state->upqueue);
98 msgb_free(msg);
99 }
100
101 /* disable all slots, kick all TBFs */
102 for (trx = 0; trx < 8; trx++) {
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100103#ifdef ENABLE_SYSMODSP
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100104 if (bts->trx[trx].fl1h) {
105 l1if_close_pdch(bts->trx[trx].fl1h);
106 bts->trx[trx].fl1h = NULL;
107 }
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100108#endif
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200109 for (ts = 0; ts < 8; ts++)
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200110 bts->trx[trx].pdch[ts].disable();
111#warning "NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c for the reset."
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200112 gprs_rlcmac_tbf::free_all(&bts->trx[trx]);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400113 }
114
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200115 gprs_bssgp_destroy_or_exit();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400116
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200117 if (lost) {
118 state->timer.cb = pcu_sock_timeout;
119 osmo_timer_schedule(&state->timer, 5, 0);
120 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400121}
122
123static int pcu_sock_read(struct osmo_fd *bfd)
124{
125 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
126 struct gsm_pcu_if *pcu_prim;
127 struct msgb *msg;
128 int rc;
129
130 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
131 if (!msg)
132 return -ENOMEM;
133
134 pcu_prim = (struct gsm_pcu_if *) msg->tail;
135
136 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
137 if (rc == 0)
138 goto close;
139
140 if (rc < 0) {
141 if (errno == EAGAIN)
142 return 0;
143 goto close;
144 }
145
146 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
147
148 /* as we always synchronously process the message in pcu_rx() and
149 * its callbacks, we can free the message here. */
150 msgb_free(msg);
151
152 return rc;
153
154close:
155 msgb_free(msg);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200156 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400157 return -1;
158}
159
160static int pcu_sock_write(struct osmo_fd *bfd)
161{
162 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
163 int rc;
164
165 while (!llist_empty(&state->upqueue)) {
166 struct msgb *msg, *msg2;
167 struct gsm_pcu_if *pcu_prim;
168
169 /* peek at the beginning of the queue */
170 msg = llist_entry(state->upqueue.next, struct msgb, list);
171 pcu_prim = (struct gsm_pcu_if *)msg->data;
172
173 bfd->when &= ~BSC_FD_WRITE;
174
175 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
176 if (!msgb_length(msg)) {
177 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
178 "bytes!\n", pcu_prim->msg_type);
179 goto dontsend;
180 }
181
182 /* try to send it over the socket */
183 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
184 if (rc == 0)
185 goto close;
186 if (rc < 0) {
187 if (errno == EAGAIN) {
188 bfd->when |= BSC_FD_WRITE;
189 break;
190 }
191 goto close;
192 }
193
194dontsend:
195 /* _after_ we send it, we can deueue */
196 msg2 = msgb_dequeue(&state->upqueue);
197 assert(msg == msg2);
198 msgb_free(msg);
199 }
200 return 0;
201
202close:
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200203 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400204
205 return -1;
206}
207
208static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
209{
210 int rc = 0;
211
212 if (flags & BSC_FD_READ)
213 rc = pcu_sock_read(bfd);
214 if (rc < 0)
215 return rc;
216
217 if (flags & BSC_FD_WRITE)
218 rc = pcu_sock_write(bfd);
219
220 return rc;
221}
222
223int pcu_l1if_open(void)
224{
225 struct pcu_sock_state *state;
226 struct osmo_fd *bfd;
227 struct sockaddr_un local;
228 unsigned int namelen;
229 int rc;
230
231 state = pcu_sock_state;
232 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200233 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400234 if (!state)
235 return -ENOMEM;
236 INIT_LLIST_HEAD(&state->upqueue);
237 }
238
239 bfd = &state->conn_bfd;
240
241 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
242 if (bfd->fd < 0) {
243 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
244 talloc_free(state);
245 return -1;
246 }
247
248 local.sun_family = AF_UNIX;
249 strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
250 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
251
252 /* we use the same magic that X11 uses in Xtranssock.c for
253 * calculating the proper length of the sockaddr */
254#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
255 local.sun_len = strlen(local.sun_path);
256#endif
257#if defined(BSD44SOCKETS) || defined(SUN_LEN)
258 namelen = SUN_LEN(&local);
259#else
260 namelen = strlen(local.sun_path) +
261 offsetof(struct sockaddr_un, sun_path);
262#endif
263 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
264 if (rc != 0) {
265 LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
266 "socket, delaying... '%s'\n", local.sun_path);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200267 pcu_sock_state = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400268 close(bfd->fd);
269 bfd->fd = -1;
270 state->timer.cb = pcu_sock_timeout;
271 osmo_timer_schedule(&state->timer, 5, 0);
272 return 0;
273 }
274
275 bfd->when = BSC_FD_READ;
276 bfd->cb = pcu_sock_cb;
277 bfd->data = state;
278
279 rc = osmo_fd_register(bfd);
280 if (rc < 0) {
281 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
282 close(bfd->fd);
283 talloc_free(state);
284 return rc;
285 }
286
287 LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
288
289 pcu_sock_state = state;
290
291 return 0;
292}
293
294void pcu_l1if_close(void)
295{
296 struct pcu_sock_state *state = pcu_sock_state;
297 struct osmo_fd *bfd;
298
299 if (!state)
300 return;
301
Holger Hans Peter Freyther51c57042013-07-13 11:52:50 +0200302 osmo_timer_del(&state->timer);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400303
304 bfd = &state->conn_bfd;
305 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200306 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400307 talloc_free(state);
308 pcu_sock_state = NULL;
309}
310
311static void pcu_sock_timeout(void *_priv)
312{
313 pcu_l1if_open();
314}