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