blob: 390f3f694609dde6bce0b2c59d74f4b8a49154e3 [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
76static void pcu_sock_close(struct pcu_sock_state *state)
77{
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
83 LOGP(DL1IF, LOGL_NOTICE, "PCU socket has LOST connection\n");
84
85 close(bfd->fd);
86 bfd->fd = -1;
87 osmo_fd_unregister(bfd);
88
89 /* flush the queue */
90 while (!llist_empty(&state->upqueue)) {
91 struct msgb *msg = msgb_dequeue(&state->upqueue);
92 msgb_free(msg);
93 }
94
95 /* disable all slots, kick all TBFs */
96 for (trx = 0; trx < 8; trx++) {
97 for (ts = 0; ts < 8; ts++) {
98 bts->trx[trx].pdch[ts].enable = 0;
99 for (tfi = 0; tfi < 32; tfi++) {
100 tbf = bts->trx[trx].pdch[ts].tbf[tfi];
101 if (tbf)
102 tbf_free(tbf);
103 }
104 }
105 }
106
107 gprs_bssgp_destroy();
108
109 state->timer.cb = pcu_sock_timeout;
110 osmo_timer_schedule(&state->timer, 5, 0);
111}
112
113static int pcu_sock_read(struct osmo_fd *bfd)
114{
115 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
116 struct gsm_pcu_if *pcu_prim;
117 struct msgb *msg;
118 int rc;
119
120 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
121 if (!msg)
122 return -ENOMEM;
123
124 pcu_prim = (struct gsm_pcu_if *) msg->tail;
125
126 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
127 if (rc == 0)
128 goto close;
129
130 if (rc < 0) {
131 if (errno == EAGAIN)
132 return 0;
133 goto close;
134 }
135
136 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
137
138 /* as we always synchronously process the message in pcu_rx() and
139 * its callbacks, we can free the message here. */
140 msgb_free(msg);
141
142 return rc;
143
144close:
145 msgb_free(msg);
146 pcu_sock_close(state);
147 return -1;
148}
149
150static int pcu_sock_write(struct osmo_fd *bfd)
151{
152 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
153 int rc;
154
155 while (!llist_empty(&state->upqueue)) {
156 struct msgb *msg, *msg2;
157 struct gsm_pcu_if *pcu_prim;
158
159 /* peek at the beginning of the queue */
160 msg = llist_entry(state->upqueue.next, struct msgb, list);
161 pcu_prim = (struct gsm_pcu_if *)msg->data;
162
163 bfd->when &= ~BSC_FD_WRITE;
164
165 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
166 if (!msgb_length(msg)) {
167 LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
168 "bytes!\n", pcu_prim->msg_type);
169 goto dontsend;
170 }
171
172 /* try to send it over the socket */
173 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
174 if (rc == 0)
175 goto close;
176 if (rc < 0) {
177 if (errno == EAGAIN) {
178 bfd->when |= BSC_FD_WRITE;
179 break;
180 }
181 goto close;
182 }
183
184dontsend:
185 /* _after_ we send it, we can deueue */
186 msg2 = msgb_dequeue(&state->upqueue);
187 assert(msg == msg2);
188 msgb_free(msg);
189 }
190 return 0;
191
192close:
193 pcu_sock_close(state);
194
195 return -1;
196}
197
198static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
199{
200 int rc = 0;
201
202 if (flags & BSC_FD_READ)
203 rc = pcu_sock_read(bfd);
204 if (rc < 0)
205 return rc;
206
207 if (flags & BSC_FD_WRITE)
208 rc = pcu_sock_write(bfd);
209
210 return rc;
211}
212
213int pcu_l1if_open(void)
214{
215 struct pcu_sock_state *state;
216 struct osmo_fd *bfd;
217 struct sockaddr_un local;
218 unsigned int namelen;
219 int rc;
220
221 state = pcu_sock_state;
222 if (!state) {
Andreas Eversberge266bd42012-07-13 14:00:21 +0200223 state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400224 if (!state)
225 return -ENOMEM;
226 INIT_LLIST_HEAD(&state->upqueue);
227 }
228
229 bfd = &state->conn_bfd;
230
231 bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
232 if (bfd->fd < 0) {
233 LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n");
234 talloc_free(state);
235 return -1;
236 }
237
238 local.sun_family = AF_UNIX;
239 strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path));
240 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
241
242 /* we use the same magic that X11 uses in Xtranssock.c for
243 * calculating the proper length of the sockaddr */
244#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
245 local.sun_len = strlen(local.sun_path);
246#endif
247#if defined(BSD44SOCKETS) || defined(SUN_LEN)
248 namelen = SUN_LEN(&local);
249#else
250 namelen = strlen(local.sun_path) +
251 offsetof(struct sockaddr_un, sun_path);
252#endif
253 rc = connect(bfd->fd, (struct sockaddr *) &local, namelen);
254 if (rc != 0) {
255 LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO "
256 "socket, delaying... '%s'\n", local.sun_path);
257 close(bfd->fd);
258 bfd->fd = -1;
259 state->timer.cb = pcu_sock_timeout;
260 osmo_timer_schedule(&state->timer, 5, 0);
261 return 0;
262 }
263
264 bfd->when = BSC_FD_READ;
265 bfd->cb = pcu_sock_cb;
266 bfd->data = state;
267
268 rc = osmo_fd_register(bfd);
269 if (rc < 0) {
270 LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc);
271 close(bfd->fd);
272 talloc_free(state);
273 return rc;
274 }
275
276 LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n");
277
278 pcu_sock_state = state;
279
280 return 0;
281}
282
283void pcu_l1if_close(void)
284{
285 struct pcu_sock_state *state = pcu_sock_state;
286 struct osmo_fd *bfd;
287
288 if (!state)
289 return;
290
291 if (osmo_timer_pending(&state->timer))
292 osmo_timer_del(&state->timer);
293
294 bfd = &state->conn_bfd;
295 if (bfd->fd > 0)
296 pcu_sock_close(state);
297 talloc_free(state);
298 pcu_sock_state = NULL;
299}
300
301static void pcu_sock_timeout(void *_priv)
302{
303 pcu_l1if_open();
304}