blob: 6af2e9abd2ab4bd76534b0cdc9eb6828218af843 [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 Freyther9e21d842013-10-16 17:48:12 +020039#include <tbf.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040040
Andreas Eversberge266bd42012-07-13 14:00:21 +020041extern void *tall_pcu_ctx;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040042
Andreas Eversberg0f4541b2013-01-16 09:17:24 +010043extern "C" {
44int l1if_close_pdch(void *obj);
45}
46
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040047/*
48 * SYSMO-PCU socket functions
49 */
50
51struct pcu_sock_state {
52 struct osmo_fd conn_bfd; /* fd for connection to lcr */
53 struct osmo_timer_list timer; /* socket connect retry timer */
54 struct llist_head upqueue; /* queue for sending messages */
55} *pcu_sock_state = NULL;
56
57static void pcu_sock_timeout(void *_priv);
58
59int pcu_sock_send(struct msgb *msg)
60{
61 struct pcu_sock_state *state = pcu_sock_state;
62 struct osmo_fd *conn_bfd;
63
64 if (!state) {
65 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
66 "message\n");
67 return -EINVAL;
68 }
69 conn_bfd = &state->conn_bfd;
70 if (conn_bfd->fd <= 0) {
71 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
72 "message\n");
73 return -EIO;
74 }
75 msgb_enqueue(&state->upqueue, msg);
76 conn_bfd->when |= BSC_FD_WRITE;
77
78 return 0;
79}
80
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020081static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040082{
83 struct osmo_fd *bfd = &state->conn_bfd;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020084 struct gprs_rlcmac_bts *bts = bts_main_data();
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +020085 uint8_t trx, ts;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040086
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020087 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
88 (lost) ? "LOST" : "closed");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040089
90 close(bfd->fd);
91 bfd->fd = -1;
92 osmo_fd_unregister(bfd);
93
94 /* flush the queue */
95 while (!llist_empty(&state->upqueue)) {
96 struct msgb *msg = msgb_dequeue(&state->upqueue);
97 msgb_free(msg);
98 }
99
100 /* disable all slots, kick all TBFs */
101 for (trx = 0; trx < 8; trx++) {
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100102#ifdef ENABLE_SYSMODSP
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100103 if (bts->trx[trx].fl1h) {
104 l1if_close_pdch(bts->trx[trx].fl1h);
105 bts->trx[trx].fl1h = NULL;
106 }
Andreas Eversberg3afe56d2013-01-25 07:22:59 +0100107#endif
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200108 for (ts = 0; ts < 8; ts++)
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200109 bts->trx[trx].pdch[ts].disable();
110#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 +0200111 gprs_rlcmac_tbf::free_all(&bts->trx[trx]);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400112 }
113
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200114 gprs_bssgp_destroy_or_exit();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400115
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200116 if (lost) {
117 state->timer.cb = pcu_sock_timeout;
118 osmo_timer_schedule(&state->timer, 5, 0);
119 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400120}
121
122static int pcu_sock_read(struct osmo_fd *bfd)
123{
124 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
125 struct gsm_pcu_if *pcu_prim;
126 struct msgb *msg;
127 int rc;
128
129 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
130 if (!msg)
131 return -ENOMEM;
132
133 pcu_prim = (struct gsm_pcu_if *) msg->tail;
134
135 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
136 if (rc == 0)
137 goto close;
138
139 if (rc < 0) {
140 if (errno == EAGAIN)
141 return 0;
142 goto close;
143 }
144
145 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
146
147 /* as we always synchronously process the message in pcu_rx() and
148 * its callbacks, we can free the message here. */
149 msgb_free(msg);
150
151 return rc;
152
153close:
154 msgb_free(msg);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200155 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400156 return -1;
157}
158
159static int pcu_sock_write(struct osmo_fd *bfd)
160{
161 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
162 int rc;
163
164 while (!llist_empty(&state->upqueue)) {
165 struct msgb *msg, *msg2;
166 struct gsm_pcu_if *pcu_prim;
167
168 /* peek at the beginning of the queue */
169 msg = llist_entry(state->upqueue.next, struct msgb, list);
170 pcu_prim = (struct gsm_pcu_if *)msg->data;
171
172 bfd->when &= ~BSC_FD_WRITE;
173
174 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
175 if (!msgb_length(msg)) {
176 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
177 "bytes!\n", pcu_prim->msg_type);
178 goto dontsend;
179 }
180
181 /* try to send it over the socket */
182 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
183 if (rc == 0)
184 goto close;
185 if (rc < 0) {
186 if (errno == EAGAIN) {
187 bfd->when |= BSC_FD_WRITE;
188 break;
189 }
190 goto close;
191 }
192
193dontsend:
194 /* _after_ we send it, we can deueue */
195 msg2 = msgb_dequeue(&state->upqueue);
196 assert(msg == msg2);
197 msgb_free(msg);
198 }
199 return 0;
200
201close:
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200202 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400203
204 return -1;
205}
206
207static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
208{
209 int rc = 0;
210
211 if (flags & BSC_FD_READ)
212 rc = pcu_sock_read(bfd);
213 if (rc < 0)
214 return rc;
215
216 if (flags & BSC_FD_WRITE)
217 rc = pcu_sock_write(bfd);
218
219 return rc;
220}
221
222int pcu_l1if_open(void)
223{
224 struct pcu_sock_state *state;
225 struct osmo_fd *bfd;
226 struct sockaddr_un local;
227 unsigned int namelen;
228 int rc;
229
230 state = pcu_sock_state;
231 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200232 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400233 if (!state)
234 return -ENOMEM;
235 INIT_LLIST_HEAD(&state->upqueue);
236 }
237
238 bfd = &state->conn_bfd;
239
240 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
241 if (bfd->fd < 0) {
242 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
243 talloc_free(state);
244 return -1;
245 }
246
247 local.sun_family = AF_UNIX;
248 strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
249 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
250
251 /* we use the same magic that X11 uses in Xtranssock.c for
252 * calculating the proper length of the sockaddr */
253#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
254 local.sun_len = strlen(local.sun_path);
255#endif
256#if defined(BSD44SOCKETS) || defined(SUN_LEN)
257 namelen = SUN_LEN(&local);
258#else
259 namelen = strlen(local.sun_path) +
260 offsetof(struct sockaddr_un, sun_path);
261#endif
262 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
263 if (rc != 0) {
264 LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
265 "socket, delaying... '%s'\n", local.sun_path);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200266 pcu_sock_state = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400267 close(bfd->fd);
268 bfd->fd = -1;
269 state->timer.cb = pcu_sock_timeout;
270 osmo_timer_schedule(&state->timer, 5, 0);
271 return 0;
272 }
273
274 bfd->when = BSC_FD_READ;
275 bfd->cb = pcu_sock_cb;
276 bfd->data = state;
277
278 rc = osmo_fd_register(bfd);
279 if (rc < 0) {
280 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
281 close(bfd->fd);
282 talloc_free(state);
283 return rc;
284 }
285
286 LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
287
288 pcu_sock_state = state;
289
290 return 0;
291}
292
293void pcu_l1if_close(void)
294{
295 struct pcu_sock_state *state = pcu_sock_state;
296 struct osmo_fd *bfd;
297
298 if (!state)
299 return;
300
Holger Hans Peter Freyther51c57042013-07-13 11:52:50 +0200301 osmo_timer_del(&state->timer);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400302
303 bfd = &state->conn_bfd;
304 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200305 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400306 talloc_free(state);
307 pcu_sock_state = NULL;
308}
309
310static void pcu_sock_timeout(void *_priv)
311{
312 pcu_l1if_open();
313}