Harald Welte | d32cbbb | 2015-11-11 21:23:23 +0100 | [diff] [blame] | 1 | /* osmobts_sock.cpp |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 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> |
| 28 | extern "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> |
Harald Welte | 68fc127 | 2016-11-16 22:48:33 +0100 | [diff] [blame] | 38 | #include <osmocom/pcu/pcuif_proto.h> |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 39 | #include <bts.h> |
Holger Hans Peter Freyther | 9e21d84 | 2013-10-16 17:48:12 +0200 | [diff] [blame] | 40 | #include <tbf.h> |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 41 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 42 | extern void *tall_pcu_ctx; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 43 | |
Andreas Eversberg | 0f4541b | 2013-01-16 09:17:24 +0100 | [diff] [blame] | 44 | extern "C" { |
| 45 | int l1if_close_pdch(void *obj); |
| 46 | } |
| 47 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 48 | /* |
Harald Welte | d32cbbb | 2015-11-11 21:23:23 +0100 | [diff] [blame] | 49 | * osmo-bts PCU socket functions |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | struct pcu_sock_state { |
| 53 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
| 54 | struct osmo_timer_list timer; /* socket connect retry timer */ |
| 55 | struct llist_head upqueue; /* queue for sending messages */ |
| 56 | } *pcu_sock_state = NULL; |
| 57 | |
| 58 | static void pcu_sock_timeout(void *_priv); |
| 59 | |
| 60 | int pcu_sock_send(struct msgb *msg) |
| 61 | { |
| 62 | struct pcu_sock_state *state = pcu_sock_state; |
| 63 | struct osmo_fd *conn_bfd; |
| 64 | |
| 65 | if (!state) { |
| 66 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping " |
| 67 | "message\n"); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | conn_bfd = &state->conn_bfd; |
| 71 | if (conn_bfd->fd <= 0) { |
| 72 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping " |
| 73 | "message\n"); |
| 74 | return -EIO; |
| 75 | } |
| 76 | msgb_enqueue(&state->upqueue, msg); |
| 77 | conn_bfd->when |= BSC_FD_WRITE; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 82 | static void pcu_sock_close(struct pcu_sock_state *state, int lost) |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 83 | { |
| 84 | struct osmo_fd *bfd = &state->conn_bfd; |
Holger Hans Peter Freyther | b6acfda | 2013-10-17 19:41:11 +0200 | [diff] [blame] | 85 | struct gprs_rlcmac_bts *bts = bts_main_data(); |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 86 | uint8_t trx, ts; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 87 | |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 88 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n", |
| 89 | (lost) ? "LOST" : "closed"); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 90 | |
| 91 | close(bfd->fd); |
| 92 | bfd->fd = -1; |
| 93 | osmo_fd_unregister(bfd); |
| 94 | |
| 95 | /* flush the queue */ |
| 96 | while (!llist_empty(&state->upqueue)) { |
| 97 | struct msgb *msg = msgb_dequeue(&state->upqueue); |
| 98 | msgb_free(msg); |
| 99 | } |
| 100 | |
| 101 | /* disable all slots, kick all TBFs */ |
| 102 | for (trx = 0; trx < 8; trx++) { |
Max | cad867e | 2016-04-21 14:35:55 +0200 | [diff] [blame] | 103 | #ifdef ENABLE_DIRECT_PHY |
Andreas Eversberg | 0f4541b | 2013-01-16 09:17:24 +0100 | [diff] [blame] | 104 | if (bts->trx[trx].fl1h) { |
| 105 | l1if_close_pdch(bts->trx[trx].fl1h); |
| 106 | bts->trx[trx].fl1h = NULL; |
| 107 | } |
Andreas Eversberg | 3afe56d | 2013-01-25 07:22:59 +0100 | [diff] [blame] | 108 | #endif |
Andreas Eversberg | adb2f18 | 2012-08-07 17:06:08 +0200 | [diff] [blame] | 109 | for (ts = 0; ts < 8; ts++) |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 110 | bts->trx[trx].pdch[ts].disable(); |
Max | 5a6bcfb | 2017-09-01 14:36:44 +0200 | [diff] [blame] | 111 | /* FIXME: NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c |
| 112 | for the reset. */ |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 113 | gprs_rlcmac_tbf::free_all(&bts->trx[trx]); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Willmann | 6d8884d | 2014-06-04 18:30:59 +0200 | [diff] [blame] | 116 | gprs_bssgp_destroy(); |
| 117 | exit(0); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int pcu_sock_read(struct osmo_fd *bfd) |
| 121 | { |
| 122 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 123 | struct gsm_pcu_if *pcu_prim; |
| 124 | struct msgb *msg; |
| 125 | int rc; |
| 126 | |
| 127 | msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx"); |
| 128 | if (!msg) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | pcu_prim = (struct gsm_pcu_if *) msg->tail; |
| 132 | |
| 133 | rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0); |
| 134 | if (rc == 0) |
| 135 | goto close; |
| 136 | |
| 137 | if (rc < 0) { |
| 138 | if (errno == EAGAIN) |
| 139 | return 0; |
| 140 | goto close; |
| 141 | } |
| 142 | |
| 143 | rc = pcu_rx(pcu_prim->msg_type, pcu_prim); |
| 144 | |
| 145 | /* as we always synchronously process the message in pcu_rx() and |
| 146 | * its callbacks, we can free the message here. */ |
| 147 | msgb_free(msg); |
| 148 | |
| 149 | return rc; |
| 150 | |
| 151 | close: |
| 152 | msgb_free(msg); |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 153 | pcu_sock_close(state, 1); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | static int pcu_sock_write(struct osmo_fd *bfd) |
| 158 | { |
| 159 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 160 | int rc; |
| 161 | |
| 162 | while (!llist_empty(&state->upqueue)) { |
| 163 | struct msgb *msg, *msg2; |
| 164 | struct gsm_pcu_if *pcu_prim; |
| 165 | |
| 166 | /* peek at the beginning of the queue */ |
| 167 | msg = llist_entry(state->upqueue.next, struct msgb, list); |
| 168 | pcu_prim = (struct gsm_pcu_if *)msg->data; |
| 169 | |
| 170 | bfd->when &= ~BSC_FD_WRITE; |
| 171 | |
| 172 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 173 | if (!msgb_length(msg)) { |
| 174 | LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO " |
| 175 | "bytes!\n", pcu_prim->msg_type); |
| 176 | goto dontsend; |
| 177 | } |
| 178 | |
| 179 | /* try to send it over the socket */ |
| 180 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 181 | if (rc == 0) |
| 182 | goto close; |
| 183 | if (rc < 0) { |
| 184 | if (errno == EAGAIN) { |
| 185 | bfd->when |= BSC_FD_WRITE; |
| 186 | break; |
| 187 | } |
| 188 | goto close; |
| 189 | } |
| 190 | |
| 191 | dontsend: |
| 192 | /* _after_ we send it, we can deueue */ |
| 193 | msg2 = msgb_dequeue(&state->upqueue); |
| 194 | assert(msg == msg2); |
| 195 | msgb_free(msg); |
| 196 | } |
| 197 | return 0; |
| 198 | |
| 199 | close: |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 200 | pcu_sock_close(state, 1); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 201 | |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
| 206 | { |
| 207 | int rc = 0; |
| 208 | |
| 209 | if (flags & BSC_FD_READ) |
| 210 | rc = pcu_sock_read(bfd); |
| 211 | if (rc < 0) |
| 212 | return rc; |
| 213 | |
| 214 | if (flags & BSC_FD_WRITE) |
| 215 | rc = pcu_sock_write(bfd); |
| 216 | |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | int pcu_l1if_open(void) |
| 221 | { |
| 222 | struct pcu_sock_state *state; |
| 223 | struct osmo_fd *bfd; |
| 224 | struct sockaddr_un local; |
| 225 | unsigned int namelen; |
| 226 | int rc; |
Pau Espin Pedrol | c4178e5 | 2017-08-08 15:03:50 +0200 | [diff] [blame] | 227 | struct gprs_rlcmac_bts *bts = bts_main_data(); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 228 | |
Harald Welte | 2184827 | 2015-11-12 01:07:41 +0100 | [diff] [blame] | 229 | LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface to OsmoBTS\n"); |
| 230 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 231 | state = pcu_sock_state; |
| 232 | if (!state) { |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 233 | state = talloc_zero(tall_pcu_ctx, struct pcu_sock_state); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 234 | if (!state) |
| 235 | return -ENOMEM; |
| 236 | INIT_LLIST_HEAD(&state->upqueue); |
| 237 | } |
| 238 | |
| 239 | bfd = &state->conn_bfd; |
| 240 | |
| 241 | bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 242 | if (bfd->fd < 0) { |
Harald Welte | d32cbbb | 2015-11-11 21:23:23 +0100 | [diff] [blame] | 243 | LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU socket.\n"); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 244 | talloc_free(state); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | local.sun_family = AF_UNIX; |
Pau Espin Pedrol | c4178e5 | 2017-08-08 15:03:50 +0200 | [diff] [blame] | 249 | strncpy(local.sun_path, bts->pcu_sock_path, sizeof(local.sun_path)); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 250 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 251 | |
| 252 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 253 | * calculating the proper length of the sockaddr */ |
| 254 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 255 | local.sun_len = strlen(local.sun_path); |
| 256 | #endif |
| 257 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 258 | namelen = SUN_LEN(&local); |
| 259 | #else |
| 260 | namelen = strlen(local.sun_path) + |
| 261 | offsetof(struct sockaddr_un, sun_path); |
| 262 | #endif |
| 263 | rc = connect(bfd->fd, (struct sockaddr *) &local, namelen); |
| 264 | if (rc != 0) { |
Harald Welte | d32cbbb | 2015-11-11 21:23:23 +0100 | [diff] [blame] | 265 | LOGP(DL1IF, LOGL_ERROR, "Failed to connect to the osmo-bts" |
Harald Welte | 19d1e92 | 2015-11-12 01:08:19 +0100 | [diff] [blame] | 266 | " PCU socket, delaying... '%s'\n", local.sun_path); |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 267 | pcu_sock_state = state; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 268 | close(bfd->fd); |
| 269 | bfd->fd = -1; |
| 270 | state->timer.cb = pcu_sock_timeout; |
| 271 | osmo_timer_schedule(&state->timer, 5, 0); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | bfd->when = BSC_FD_READ; |
| 276 | bfd->cb = pcu_sock_cb; |
| 277 | bfd->data = state; |
| 278 | |
| 279 | rc = osmo_fd_register(bfd); |
| 280 | if (rc < 0) { |
| 281 | LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc); |
| 282 | close(bfd->fd); |
| 283 | talloc_free(state); |
| 284 | return rc; |
| 285 | } |
| 286 | |
Max | b3df586 | 2017-01-06 17:20:57 +0100 | [diff] [blame] | 287 | LOGP(DL1IF, LOGL_NOTICE, "osmo-bts PCU socket %s has been connected\n", |
| 288 | local.sun_path); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 289 | |
| 290 | pcu_sock_state = state; |
| 291 | |
Max | 0a8fae8 | 2017-03-08 18:53:30 +0100 | [diff] [blame] | 292 | LOGP(DL1IF, LOGL_INFO, "Sending version %s to BTS.\n", PACKAGE_VERSION); |
| 293 | pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION); |
| 294 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | void pcu_l1if_close(void) |
| 299 | { |
| 300 | struct pcu_sock_state *state = pcu_sock_state; |
| 301 | struct osmo_fd *bfd; |
| 302 | |
| 303 | if (!state) |
| 304 | return; |
| 305 | |
Holger Hans Peter Freyther | 51c5704 | 2013-07-13 11:52:50 +0200 | [diff] [blame] | 306 | osmo_timer_del(&state->timer); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 307 | |
| 308 | bfd = &state->conn_bfd; |
| 309 | if (bfd->fd > 0) |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 310 | pcu_sock_close(state, 0); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 311 | talloc_free(state); |
| 312 | pcu_sock_state = NULL; |
| 313 | } |
| 314 | |
| 315 | static void pcu_sock_timeout(void *_priv) |
| 316 | { |
| 317 | pcu_l1if_open(); |
| 318 | } |