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