blob: 1982472b076c0be91f564ce842f44f091e38e927 [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>
39
Andreas Eversberge266bd42012-07-13 14:00:21 +020040extern void *tall_pcu_ctx;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040041
Andreas Eversberg0f4541b2013-01-16 09:17:24 +010042extern "C" {
43int l1if_close_pdch(void *obj);
44}
45
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040046/*
47 * SYSMO-PCU socket functions
48 */
49
50struct pcu_sock_state {
51 struct osmo_fd conn_bfd; /* fd for connection to lcr */
52 struct osmo_timer_list timer; /* socket connect retry timer */
53 struct llist_head upqueue; /* queue for sending messages */
54} *pcu_sock_state = NULL;
55
56static void pcu_sock_timeout(void *_priv);
57
58int pcu_sock_send(struct msgb *msg)
59{
60 struct pcu_sock_state *state = pcu_sock_state;
61 struct osmo_fd *conn_bfd;
62
63 if (!state) {
64 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
65 "message\n");
66 return -EINVAL;
67 }
68 conn_bfd = &state->conn_bfd;
69 if (conn_bfd->fd <= 0) {
70 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
71 "message\n");
72 return -EIO;
73 }
74 msgb_enqueue(&state->upqueue, msg);
75 conn_bfd->when |= BSC_FD_WRITE;
76
77 return 0;
78}
79
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020080static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040081{
82 struct osmo_fd *bfd = &state->conn_bfd;
83 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
84 struct gprs_rlcmac_tbf *tbf;
85 uint8_t trx, ts, tfi;
86
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 Eversberg0f4541b2013-01-16 09:17:24 +0100102 if (bts->trx[trx].fl1h) {
103 l1if_close_pdch(bts->trx[trx].fl1h);
104 bts->trx[trx].fl1h = NULL;
105 }
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200106 for (ts = 0; ts < 8; ts++)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400107 bts->trx[trx].pdch[ts].enable = 0;
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200108 for (tfi = 0; tfi < 32; tfi++) {
109 tbf = bts->trx[trx].ul_tbf[tfi];
110 if (tbf)
111 tbf_free(tbf);
112 tbf = bts->trx[trx].dl_tbf[tfi];
113 if (tbf)
114 tbf_free(tbf);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400115 }
116 }
117
118 gprs_bssgp_destroy();
119
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200120 if (lost) {
121 state->timer.cb = pcu_sock_timeout;
122 osmo_timer_schedule(&state->timer, 5, 0);
123 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400124}
125
126static int pcu_sock_read(struct osmo_fd *bfd)
127{
128 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
129 struct gsm_pcu_if *pcu_prim;
130 struct msgb *msg;
131 int rc;
132
133 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
134 if (!msg)
135 return -ENOMEM;
136
137 pcu_prim = (struct gsm_pcu_if *) msg->tail;
138
139 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
140 if (rc == 0)
141 goto close;
142
143 if (rc < 0) {
144 if (errno == EAGAIN)
145 return 0;
146 goto close;
147 }
148
149 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
150
151 /* as we always synchronously process the message in pcu_rx() and
152 * its callbacks, we can free the message here. */
153 msgb_free(msg);
154
155 return rc;
156
157close:
158 msgb_free(msg);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200159 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400160 return -1;
161}
162
163static int pcu_sock_write(struct osmo_fd *bfd)
164{
165 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
166 int rc;
167
168 while (!llist_empty(&state->upqueue)) {
169 struct msgb *msg, *msg2;
170 struct gsm_pcu_if *pcu_prim;
171
172 /* peek at the beginning of the queue */
173 msg = llist_entry(state->upqueue.next, struct msgb, list);
174 pcu_prim = (struct gsm_pcu_if *)msg->data;
175
176 bfd->when &= ~BSC_FD_WRITE;
177
178 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
179 if (!msgb_length(msg)) {
180 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
181 "bytes!\n", pcu_prim->msg_type);
182 goto dontsend;
183 }
184
185 /* try to send it over the socket */
186 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
187 if (rc == 0)
188 goto close;
189 if (rc < 0) {
190 if (errno == EAGAIN) {
191 bfd->when |= BSC_FD_WRITE;
192 break;
193 }
194 goto close;
195 }
196
197dontsend:
198 /* _after_ we send it, we can deueue */
199 msg2 = msgb_dequeue(&state->upqueue);
200 assert(msg == msg2);
201 msgb_free(msg);
202 }
203 return 0;
204
205close:
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200206 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400207
208 return -1;
209}
210
211static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
212{
213 int rc = 0;
214
215 if (flags & BSC_FD_READ)
216 rc = pcu_sock_read(bfd);
217 if (rc < 0)
218 return rc;
219
220 if (flags & BSC_FD_WRITE)
221 rc = pcu_sock_write(bfd);
222
223 return rc;
224}
225
226int pcu_l1if_open(void)
227{
228 struct pcu_sock_state *state;
229 struct osmo_fd *bfd;
230 struct sockaddr_un local;
231 unsigned int namelen;
232 int rc;
233
234 state = pcu_sock_state;
235 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200236 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400237 if (!state)
238 return -ENOMEM;
239 INIT_LLIST_HEAD(&state->upqueue);
240 }
241
242 bfd = &state->conn_bfd;
243
244 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
245 if (bfd->fd < 0) {
246 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
247 talloc_free(state);
248 return -1;
249 }
250
251 local.sun_family = AF_UNIX;
252 strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
253 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
254
255 /* we use the same magic that X11 uses in Xtranssock.c for
256 * calculating the proper length of the sockaddr */
257#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
258 local.sun_len = strlen(local.sun_path);
259#endif
260#if defined(BSD44SOCKETS) || defined(SUN_LEN)
261 namelen = SUN_LEN(&local);
262#else
263 namelen = strlen(local.sun_path) +
264 offsetof(struct sockaddr_un, sun_path);
265#endif
266 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
267 if (rc != 0) {
268 LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
269 "socket, delaying... '%s'\n", local.sun_path);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200270 pcu_sock_state = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400271 close(bfd->fd);
272 bfd->fd = -1;
273 state->timer.cb = pcu_sock_timeout;
274 osmo_timer_schedule(&state->timer, 5, 0);
275 return 0;
276 }
277
278 bfd->when = BSC_FD_READ;
279 bfd->cb = pcu_sock_cb;
280 bfd->data = state;
281
282 rc = osmo_fd_register(bfd);
283 if (rc < 0) {
284 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
285 close(bfd->fd);
286 talloc_free(state);
287 return rc;
288 }
289
290 LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
291
292 pcu_sock_state = state;
293
294 return 0;
295}
296
297void pcu_l1if_close(void)
298{
299 struct pcu_sock_state *state = pcu_sock_state;
300 struct osmo_fd *bfd;
301
302 if (!state)
303 return;
304
305 if (osmo_timer_pending(&state->timer))
306 osmo_timer_del(&state->timer);
307
308 bfd = &state->conn_bfd;
309 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200310 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400311 talloc_free(state);
312 pcu_sock_state = NULL;
313}
314
315static void pcu_sock_timeout(void *_priv)
316{
317 pcu_l1if_open();
318}