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. |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <errno.h> |
| 21 | #include <assert.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
Pau Espin Pedrol | e91c4c7 | 2021-01-18 17:54:30 +0100 | [diff] [blame] | 24 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 25 | #include <osmocom/core/select.h> |
| 26 | #include <osmocom/core/msgb.h> |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 27 | #include <osmocom/core/linuxlist.h> |
| 28 | #include <osmocom/core/logging.h> |
| 29 | #include <osmocom/core/timer.h> |
Pau Espin Pedrol | e91c4c7 | 2021-01-18 17:54:30 +0100 | [diff] [blame] | 30 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 31 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 32 | #include <pcu_l1_if.h> |
Philipp Maier | eaf5da3 | 2023-02-13 13:37:22 +0100 | [diff] [blame] | 33 | #include <pcu_l1_if_phy.h> |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 34 | #include <gprs_debug.h> |
| 35 | #include <gprs_bssgp_pcu.h> |
Harald Welte | 68fc127 | 2016-11-16 22:48:33 +0100 | [diff] [blame] | 36 | #include <osmocom/pcu/pcuif_proto.h> |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 37 | #include <bts.h> |
Holger Hans Peter Freyther | 9e21d84 | 2013-10-16 17:48:12 +0200 | [diff] [blame] | 38 | #include <tbf.h> |
Max | 6dc90b8 | 2018-02-19 17:17:28 +0100 | [diff] [blame] | 39 | #include <pdch.h> |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 40 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 41 | /* |
Harald Welte | d32cbbb | 2015-11-11 21:23:23 +0100 | [diff] [blame] | 42 | * osmo-bts PCU socket functions |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 43 | */ |
| 44 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 45 | static struct { |
Vadim Yanitskiy | 3ff1a3c | 2020-02-06 17:20:55 +0700 | [diff] [blame] | 46 | struct osmo_fd conn_bfd; /* fd for connection to the BTS */ |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 47 | struct osmo_timer_list timer; /* socket connect retry timer */ |
| 48 | struct llist_head upqueue; /* queue for sending messages */ |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 49 | } pcu_sock_state; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 50 | |
Stefan Sperling | 5b22fb7 | 2018-02-14 19:46:33 +0100 | [diff] [blame] | 51 | static void pcu_sock_timeout(void *_priv) |
| 52 | { |
| 53 | pcu_l1if_open(); |
| 54 | } |
| 55 | |
| 56 | static void pcu_tx_txt_retry(void *_priv) |
| 57 | { |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 58 | struct gprs_rlcmac_bts *bts; |
| 59 | bool retry = llist_empty(&the_pcu->bts_list); |
Stefan Sperling | 5b22fb7 | 2018-02-14 19:46:33 +0100 | [diff] [blame] | 60 | |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 61 | llist_for_each_entry(bts, &the_pcu->bts_list, list) { |
| 62 | if (bts->active) |
| 63 | continue; |
| 64 | retry = true; |
| 65 | pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION); |
| 66 | break; |
| 67 | } |
Stefan Sperling | 5b22fb7 | 2018-02-14 19:46:33 +0100 | [diff] [blame] | 68 | |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 69 | /* If no BTS (or not all) yet active, retry */ |
| 70 | if (retry) |
| 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); |
Harald Welte | 2bbdf2e | 2020-10-19 12:59:13 +0200 | [diff] [blame] | 85 | osmo_fd_write_enable(conn_bfd); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 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; |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 93 | struct gprs_rlcmac_bts *bts; |
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 | |
Pau Espin Pedrol | 93d7ece | 2023-03-14 11:13:31 +0100 | [diff] [blame] | 99 | osmo_fd_unregister(bfd); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 100 | close(bfd->fd); |
| 101 | bfd->fd = -1; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 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 | |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 109 | llist_for_each_entry(bts, &the_pcu->bts_list, list) { |
| 110 | /* disable all slots, kick all TBFs */ |
| 111 | for (trx = 0; trx < 8; trx++) { |
Max | cad867e | 2016-04-21 14:35:55 +0200 | [diff] [blame] | 112 | #ifdef ENABLE_DIRECT_PHY |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 113 | if (bts->trx[trx].fl1h) { |
| 114 | l1if_close_pdch(bts->trx[trx].fl1h); |
| 115 | bts->trx[trx].fl1h = NULL; |
| 116 | } |
Andreas Eversberg | 3afe56d | 2013-01-25 07:22:59 +0100 | [diff] [blame] | 117 | #endif |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 118 | for (ts = 0; ts < 8; ts++) |
Pau Espin Pedrol | 4f67a9b | 2021-06-30 16:03:06 +0200 | [diff] [blame] | 119 | if (pdch_is_enabled(&bts->trx[trx].pdch[ts])) |
| 120 | pdch_disable(&bts->trx[trx].pdch[ts]); |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 121 | /* FIXME: NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c |
| 122 | for the reset. */ |
| 123 | bts_trx_free_all_tbf(&bts->trx[trx]); |
| 124 | } |
| 125 | gprs_bssgp_destroy(bts); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 126 | } |
Daniel Willmann | 6d8884d | 2014-06-04 18:30:59 +0200 | [diff] [blame] | 127 | exit(0); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int pcu_sock_read(struct osmo_fd *bfd) |
| 131 | { |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 132 | const size_t max_len = sizeof(struct gsm_pcu_if) + 1000; |
| 133 | uint8_t *buf = alloca(max_len); |
| 134 | struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *)buf; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 135 | int rc; |
| 136 | |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 137 | rc = recv(bfd->fd, buf, max_len, 0); |
Vadim Yanitskiy | 9e5ef54 | 2019-08-23 18:03:14 +0200 | [diff] [blame] | 138 | if (rc < 0 && errno == EAGAIN) |
| 139 | return 0; /* Try again later */ |
| 140 | if (rc <= 0) { |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 141 | pcu_sock_close(1); |
Vadim Yanitskiy | 9e5ef54 | 2019-08-23 18:03:14 +0200 | [diff] [blame] | 142 | return -EIO; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 143 | } |
| 144 | |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 145 | if (rc < PCUIF_HDR_SIZE) { |
| 146 | LOGP(DL1IF, LOGL_ERROR, "Received %d bytes on PCU Socket, but primitive " |
| 147 | "hdr size is %zu, discarding\n", rc, PCUIF_HDR_SIZE); |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
| 151 | return pcu_rx(pcu_prim, rc); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int pcu_sock_write(struct osmo_fd *bfd) |
| 155 | { |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 156 | int rc; |
| 157 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 158 | while (!llist_empty(&pcu_sock_state.upqueue)) { |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 159 | struct msgb *msg, *msg2; |
| 160 | struct gsm_pcu_if *pcu_prim; |
| 161 | |
| 162 | /* peek at the beginning of the queue */ |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 163 | msg = llist_entry(pcu_sock_state.upqueue.next, struct msgb, list); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 164 | pcu_prim = (struct gsm_pcu_if *)msg->data; |
| 165 | |
Harald Welte | 2bbdf2e | 2020-10-19 12:59:13 +0200 | [diff] [blame] | 166 | osmo_fd_write_disable(bfd); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 167 | |
| 168 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 169 | if (!msgb_length(msg)) { |
| 170 | LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO " |
| 171 | "bytes!\n", pcu_prim->msg_type); |
| 172 | goto dontsend; |
| 173 | } |
| 174 | |
| 175 | /* try to send it over the socket */ |
| 176 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 177 | if (rc == 0) |
| 178 | goto close; |
| 179 | if (rc < 0) { |
| 180 | if (errno == EAGAIN) { |
Harald Welte | 2bbdf2e | 2020-10-19 12:59:13 +0200 | [diff] [blame] | 181 | osmo_fd_write_enable(bfd); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | goto close; |
| 185 | } |
| 186 | |
| 187 | dontsend: |
| 188 | /* _after_ we send it, we can deueue */ |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 189 | msg2 = msgb_dequeue(&pcu_sock_state.upqueue); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 190 | assert(msg == msg2); |
| 191 | msgb_free(msg); |
| 192 | } |
| 193 | return 0; |
| 194 | |
| 195 | close: |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 196 | pcu_sock_close(1); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 197 | |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
| 202 | { |
| 203 | int rc = 0; |
| 204 | |
Pau Espin Pedrol | db9ea55 | 2020-05-09 19:18:06 +0200 | [diff] [blame] | 205 | if (flags & OSMO_FD_READ) |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 206 | rc = pcu_sock_read(bfd); |
| 207 | if (rc < 0) |
| 208 | return rc; |
| 209 | |
Pau Espin Pedrol | db9ea55 | 2020-05-09 19:18:06 +0200 | [diff] [blame] | 210 | if (flags & OSMO_FD_WRITE) |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 211 | rc = pcu_sock_write(bfd); |
| 212 | |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | int pcu_l1if_open(void) |
| 217 | { |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 218 | int rc; |
arehbein | 1a5eb18 | 2023-03-25 22:03:56 +0100 | [diff] [blame] | 219 | LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface v%u to OsmoBTS\n", PCU_IF_VERSION); |
Harald Welte | 2184827 | 2015-11-12 01:07:41 +0100 | [diff] [blame] | 220 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 221 | memset(&pcu_sock_state, 0x00, sizeof(pcu_sock_state)); |
| 222 | INIT_LLIST_HEAD(&pcu_sock_state.upqueue); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 223 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 224 | 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] | 225 | the_pcu->pcu_sock_path, OSMO_SOCK_F_CONNECT); |
Vadim Yanitskiy | aef6bf4 | 2019-08-23 16:50:01 +0200 | [diff] [blame] | 226 | if (rc < 0) { |
| 227 | LOGP(DL1IF, LOGL_ERROR, "Failed to connect to the BTS (%s). " |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 228 | "Retrying...\n", the_pcu->pcu_sock_path); |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 229 | osmo_timer_setup(&pcu_sock_state.timer, pcu_sock_timeout, NULL); |
| 230 | osmo_timer_schedule(&pcu_sock_state.timer, 5, 0); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 234 | pcu_sock_state.conn_bfd.cb = pcu_sock_cb; |
| 235 | pcu_sock_state.conn_bfd.data = NULL; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 236 | |
Max | b3df586 | 2017-01-06 17:20:57 +0100 | [diff] [blame] | 237 | 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] | 238 | the_pcu->pcu_sock_path); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 239 | |
Max | 0a8fae8 | 2017-03-08 18:53:30 +0100 | [diff] [blame] | 240 | pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION); |
| 241 | |
Stefan Sperling | 5b22fb7 | 2018-02-14 19:46:33 +0100 | [diff] [blame] | 242 | /* Schedule a timer so we keep trying until the BTS becomes active. */ |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 243 | osmo_timer_setup(&pcu_sock_state.timer, pcu_tx_txt_retry, NULL); |
| 244 | osmo_timer_schedule(&pcu_sock_state.timer, 5, 0); |
Stefan Sperling | 5b22fb7 | 2018-02-14 19:46:33 +0100 | [diff] [blame] | 245 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | void pcu_l1if_close(void) |
| 250 | { |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 251 | struct osmo_fd *bfd; |
| 252 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 253 | osmo_timer_del(&pcu_sock_state.timer); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 254 | |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 255 | bfd = &pcu_sock_state.conn_bfd; |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 256 | if (bfd->fd > 0) |
Vadim Yanitskiy | 8832c2e | 2020-02-06 17:15:11 +0700 | [diff] [blame] | 257 | pcu_sock_close(0); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 258 | } |