blob: c4565952fd8e01e27e71cbd61efb5156689d5b78 [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
42/*
43 * SYSMO-PCU socket functions
44 */
45
46struct pcu_sock_state {
47 struct osmo_fd conn_bfd; /* fd for connection to lcr */
48 struct osmo_timer_list timer; /* socket connect retry timer */
49 struct llist_head upqueue; /* queue for sending messages */
50} *pcu_sock_state = NULL;
51
52static void pcu_sock_timeout(void *_priv);
53
54int pcu_sock_send(struct msgb *msg)
55{
56 struct pcu_sock_state *state = pcu_sock_state;
57 struct osmo_fd *conn_bfd;
58
59 if (!state) {
60 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping "
61 "message\n");
62 return -EINVAL;
63 }
64 conn_bfd = &state->conn_bfd;
65 if (conn_bfd->fd <= 0) {
66 LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
67 "message\n");
68 return -EIO;
69 }
70 msgb_enqueue(&state->upqueue, msg);
71 conn_bfd->when |= BSC_FD_WRITE;
72
73 return 0;
74}
75
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020076static void pcu_sock_close(struct pcu_sock_state *state, int lost)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040077{
78 struct osmo_fd *bfd = &state->conn_bfd;
79 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
80 struct gprs_rlcmac_tbf *tbf;
81 uint8_t trx, ts, tfi;
82
Andreas Eversberga3c12fb2012-09-28 22:46:33 +020083 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
84 (lost) ? "LOST" : "closed");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040085
86 close(bfd->fd);
87 bfd->fd = -1;
88 osmo_fd_unregister(bfd);
89
90 /* flush the queue */
91 while (!llist_empty(&state->upqueue)) {
92 struct msgb *msg = msgb_dequeue(&state->upqueue);
93 msgb_free(msg);
94 }
95
96 /* disable all slots, kick all TBFs */
97 for (trx = 0; trx < 8; trx++) {
Andreas Eversbergadb2f182012-08-07 17:06:08 +020098 for (ts = 0; ts < 8; ts++)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040099 bts->trx[trx].pdch[ts].enable = 0;
Andreas Eversbergadb2f182012-08-07 17:06:08 +0200100 for (tfi = 0; tfi < 32; tfi++) {
101 tbf = bts->trx[trx].ul_tbf[tfi];
102 if (tbf)
103 tbf_free(tbf);
104 tbf = bts->trx[trx].dl_tbf[tfi];
105 if (tbf)
106 tbf_free(tbf);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400107 }
108 }
109
110 gprs_bssgp_destroy();
111
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200112 if (lost) {
113 state->timer.cb = pcu_sock_timeout;
114 osmo_timer_schedule(&state->timer, 5, 0);
115 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400116}
117
118static int pcu_sock_read(struct osmo_fd *bfd)
119{
120 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
121 struct gsm_pcu_if *pcu_prim;
122 struct msgb *msg;
123 int rc;
124
125 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
126 if (!msg)
127 return -ENOMEM;
128
129 pcu_prim = (struct gsm_pcu_if *) msg->tail;
130
131 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
132 if (rc == 0)
133 goto close;
134
135 if (rc < 0) {
136 if (errno == EAGAIN)
137 return 0;
138 goto close;
139 }
140
141 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
142
143 /* as we always synchronously process the message in pcu_rx() and
144 * its callbacks, we can free the message here. */
145 msgb_free(msg);
146
147 return rc;
148
149close:
150 msgb_free(msg);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200151 pcu_sock_close(state, 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400152 return -1;
153}
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;
221 struct osmo_fd *bfd;
222 struct sockaddr_un local;
223 unsigned int namelen;
224 int rc;
225
226 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
234 bfd = &state->conn_bfd;
235
236 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
237 if (bfd->fd < 0) {
238 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
239 talloc_free(state);
240 return -1;
241 }
242
243 local.sun_family = AF_UNIX;
244 strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
245 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
246
247 /* we use the same magic that X11 uses in Xtranssock.c for
248 * calculating the proper length of the sockaddr */
249#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
250 local.sun_len = strlen(local.sun_path);
251#endif
252#if defined(BSD44SOCKETS) || defined(SUN_LEN)
253 namelen = SUN_LEN(&local);
254#else
255 namelen = strlen(local.sun_path) +
256 offsetof(struct sockaddr_un, sun_path);
257#endif
258 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
259 if (rc != 0) {
260 LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
261 "socket, delaying... '%s'\n", local.sun_path);
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200262 pcu_sock_state = state;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400263 close(bfd->fd);
264 bfd->fd = -1;
265 state->timer.cb = pcu_sock_timeout;
266 osmo_timer_schedule(&state->timer, 5, 0);
267 return 0;
268 }
269
270 bfd->when = BSC_FD_READ;
271 bfd->cb = pcu_sock_cb;
272 bfd->data = state;
273
274 rc = osmo_fd_register(bfd);
275 if (rc < 0) {
276 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
277 close(bfd->fd);
278 talloc_free(state);
279 return rc;
280 }
281
282 LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
283
284 pcu_sock_state = state;
285
286 return 0;
287}
288
289void pcu_l1if_close(void)
290{
291 struct pcu_sock_state *state = pcu_sock_state;
292 struct osmo_fd *bfd;
293
294 if (!state)
295 return;
296
297 if (osmo_timer_pending(&state->timer))
298 osmo_timer_del(&state->timer);
299
300 bfd = &state->conn_bfd;
301 if (bfd->fd > 0)
Andreas Eversberga3c12fb2012-09-28 22:46:33 +0200302 pcu_sock_close(state, 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400303 talloc_free(state);
304 pcu_sock_state = NULL;
305}
306
307static void pcu_sock_timeout(void *_priv)
308{
309 pcu_l1if_open();
310}