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