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