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