2015-11-11 20:23:23 +00:00
|
|
|
/* osmobts_sock.cpp
|
2012-07-12 10:49:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2021-01-18 16:54:30 +00:00
|
|
|
|
2012-07-12 10:49:15 +00:00
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/msgb.h>
|
2018-01-26 12:31:42 +00:00
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
2021-01-18 16:54:30 +00:00
|
|
|
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
#include <pcu_l1_if.h>
|
|
|
|
#include <gprs_debug.h>
|
|
|
|
#include <gprs_bssgp_pcu.h>
|
2016-11-16 21:48:33 +00:00
|
|
|
#include <osmocom/pcu/pcuif_proto.h>
|
2013-10-19 19:10:38 +00:00
|
|
|
#include <bts.h>
|
2013-10-16 15:48:12 +00:00
|
|
|
#include <tbf.h>
|
2018-02-19 16:17:28 +00:00
|
|
|
#include <pdch.h>
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2013-01-16 08:17:24 +00:00
|
|
|
int l1if_close_pdch(void *obj);
|
|
|
|
|
2012-07-12 10:49:15 +00:00
|
|
|
/*
|
2015-11-11 20:23:23 +00:00
|
|
|
* osmo-bts PCU socket functions
|
2012-07-12 10:49:15 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
static struct {
|
2020-02-06 10:20:55 +00:00
|
|
|
struct osmo_fd conn_bfd; /* fd for connection to the BTS */
|
2012-07-12 10:49:15 +00:00
|
|
|
struct osmo_timer_list timer; /* socket connect retry timer */
|
|
|
|
struct llist_head upqueue; /* queue for sending messages */
|
2020-02-06 10:15:11 +00:00
|
|
|
} pcu_sock_state;
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2018-02-14 18:46:33 +00:00
|
|
|
static void pcu_sock_timeout(void *_priv)
|
|
|
|
{
|
|
|
|
pcu_l1if_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pcu_tx_txt_retry(void *_priv)
|
|
|
|
{
|
2021-01-18 16:14:14 +00:00
|
|
|
struct gprs_rlcmac_bts *bts;
|
|
|
|
bool retry = llist_empty(&the_pcu->bts_list);
|
|
|
|
|
|
|
|
llist_for_each_entry(bts, &the_pcu->bts_list, list) {
|
|
|
|
if (bts->active)
|
|
|
|
continue;
|
|
|
|
retry = true;
|
|
|
|
pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
|
|
|
|
break;
|
|
|
|
}
|
2018-02-14 18:46:33 +00:00
|
|
|
|
2021-01-18 16:14:14 +00:00
|
|
|
/* If no BTS (or not all) yet active, retry */
|
|
|
|
if (retry)
|
|
|
|
osmo_timer_schedule(&pcu_sock_state.timer, 5, 0);
|
2018-02-14 18:46:33 +00:00
|
|
|
}
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
int pcu_sock_send(struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct osmo_fd *conn_bfd;
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
conn_bfd = &pcu_sock_state.conn_bfd;
|
2012-07-12 10:49:15 +00:00
|
|
|
if (conn_bfd->fd <= 0) {
|
|
|
|
LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping "
|
|
|
|
"message\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-02-06 10:15:11 +00:00
|
|
|
msgb_enqueue(&pcu_sock_state.upqueue, msg);
|
2020-10-19 10:59:13 +00:00
|
|
|
osmo_fd_write_enable(conn_bfd);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
static void pcu_sock_close(int lost)
|
2012-07-12 10:49:15 +00:00
|
|
|
{
|
2020-02-06 10:15:11 +00:00
|
|
|
struct osmo_fd *bfd = &pcu_sock_state.conn_bfd;
|
2021-01-18 16:14:14 +00:00
|
|
|
struct gprs_rlcmac_bts *bts;
|
2013-10-16 15:53:23 +00:00
|
|
|
uint8_t trx, ts;
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2012-09-28 20:46:33 +00:00
|
|
|
LOGP(DL1IF, LOGL_NOTICE, "PCU socket has %s connection\n",
|
|
|
|
(lost) ? "LOST" : "closed");
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
close(bfd->fd);
|
|
|
|
bfd->fd = -1;
|
|
|
|
osmo_fd_unregister(bfd);
|
|
|
|
|
|
|
|
/* flush the queue */
|
2020-02-06 10:15:11 +00:00
|
|
|
while (!llist_empty(&pcu_sock_state.upqueue)) {
|
|
|
|
struct msgb *msg = msgb_dequeue(&pcu_sock_state.upqueue);
|
2012-07-12 10:49:15 +00:00
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:14:14 +00:00
|
|
|
llist_for_each_entry(bts, &the_pcu->bts_list, list) {
|
|
|
|
/* disable all slots, kick all TBFs */
|
|
|
|
for (trx = 0; trx < 8; trx++) {
|
2016-04-21 12:35:55 +00:00
|
|
|
#ifdef ENABLE_DIRECT_PHY
|
2021-01-18 16:14:14 +00:00
|
|
|
if (bts->trx[trx].fl1h) {
|
|
|
|
l1if_close_pdch(bts->trx[trx].fl1h);
|
|
|
|
bts->trx[trx].fl1h = NULL;
|
|
|
|
}
|
2013-01-25 06:22:59 +00:00
|
|
|
#endif
|
2021-01-18 16:14:14 +00:00
|
|
|
for (ts = 0; ts < 8; ts++)
|
2021-06-30 14:03:06 +00:00
|
|
|
if (pdch_is_enabled(&bts->trx[trx].pdch[ts]))
|
|
|
|
pdch_disable(&bts->trx[trx].pdch[ts]);
|
2021-01-18 16:14:14 +00:00
|
|
|
/* FIXME: NOT ALL RESOURCES are freed in this case... inconsistent with the other code. Share the code with pcu_l1if.c
|
|
|
|
for the reset. */
|
|
|
|
bts_trx_free_all_tbf(&bts->trx[trx]);
|
|
|
|
}
|
|
|
|
gprs_bssgp_destroy(bts);
|
2012-07-12 10:49:15 +00:00
|
|
|
}
|
2014-06-04 16:30:59 +00:00
|
|
|
exit(0);
|
2012-07-12 10:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pcu_sock_read(struct osmo_fd *bfd)
|
|
|
|
{
|
2021-06-23 17:46:07 +00:00
|
|
|
const size_t max_len = sizeof(struct gsm_pcu_if) + 1000;
|
|
|
|
uint8_t *buf = alloca(max_len);
|
|
|
|
struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *)buf;
|
2012-07-12 10:49:15 +00:00
|
|
|
int rc;
|
|
|
|
|
2021-06-23 17:46:07 +00:00
|
|
|
rc = recv(bfd->fd, buf, max_len, 0);
|
2019-08-23 16:03:14 +00:00
|
|
|
if (rc < 0 && errno == EAGAIN)
|
|
|
|
return 0; /* Try again later */
|
|
|
|
if (rc <= 0) {
|
2020-02-06 10:15:11 +00:00
|
|
|
pcu_sock_close(1);
|
2019-08-23 16:03:14 +00:00
|
|
|
return -EIO;
|
2012-07-12 10:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:46:07 +00:00
|
|
|
if (rc < PCUIF_HDR_SIZE) {
|
|
|
|
LOGP(DL1IF, LOGL_ERROR, "Received %d bytes on PCU Socket, but primitive "
|
|
|
|
"hdr size is %zu, discarding\n", rc, PCUIF_HDR_SIZE);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pcu_rx(pcu_prim, rc);
|
2012-07-12 10:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pcu_sock_write(struct osmo_fd *bfd)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
while (!llist_empty(&pcu_sock_state.upqueue)) {
|
2012-07-12 10:49:15 +00:00
|
|
|
struct msgb *msg, *msg2;
|
|
|
|
struct gsm_pcu_if *pcu_prim;
|
|
|
|
|
|
|
|
/* peek at the beginning of the queue */
|
2020-02-06 10:15:11 +00:00
|
|
|
msg = llist_entry(pcu_sock_state.upqueue.next, struct msgb, list);
|
2012-07-12 10:49:15 +00:00
|
|
|
pcu_prim = (struct gsm_pcu_if *)msg->data;
|
|
|
|
|
2020-10-19 10:59:13 +00:00
|
|
|
osmo_fd_write_disable(bfd);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
/* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
|
|
|
|
if (!msgb_length(msg)) {
|
|
|
|
LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO "
|
|
|
|
"bytes!\n", pcu_prim->msg_type);
|
|
|
|
goto dontsend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to send it over the socket */
|
|
|
|
rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
|
|
|
|
if (rc == 0)
|
|
|
|
goto close;
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EAGAIN) {
|
2020-10-19 10:59:13 +00:00
|
|
|
osmo_fd_write_enable(bfd);
|
2012-07-12 10:49:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
dontsend:
|
|
|
|
/* _after_ we send it, we can deueue */
|
2020-02-06 10:15:11 +00:00
|
|
|
msg2 = msgb_dequeue(&pcu_sock_state.upqueue);
|
2012-07-12 10:49:15 +00:00
|
|
|
assert(msg == msg2);
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
close:
|
2020-02-06 10:15:11 +00:00
|
|
|
pcu_sock_close(1);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
2020-05-09 17:18:06 +00:00
|
|
|
if (flags & OSMO_FD_READ)
|
2012-07-12 10:49:15 +00:00
|
|
|
rc = pcu_sock_read(bfd);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
2020-05-09 17:18:06 +00:00
|
|
|
if (flags & OSMO_FD_WRITE)
|
2012-07-12 10:49:15 +00:00
|
|
|
rc = pcu_sock_write(bfd);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pcu_l1if_open(void)
|
|
|
|
{
|
|
|
|
int rc;
|
2015-11-12 00:07:41 +00:00
|
|
|
LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface to OsmoBTS\n");
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
memset(&pcu_sock_state, 0x00, sizeof(pcu_sock_state));
|
|
|
|
INIT_LLIST_HEAD(&pcu_sock_state.upqueue);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
rc = osmo_sock_unix_init_ofd(&pcu_sock_state.conn_bfd, SOCK_SEQPACKET, 0,
|
Split PCU global PCU object from BTS object
Currently the BTS object (and gprs_rlcmac_bts struct) are used to hold
both PCU global fields and BTS specific fields, all mangled together.
The BTS is even accessed in lots of places by means of a singleton.
This patch introduces a new struct gprs_pcu object aimed at holding all
global state, and several fields are already moved from BTS to it. The
new object can be accessed as global variable "the_pcu", reusing and
including an already exisitng "the_pcu" global variable only used for
bssgp related purposes so far.
This is only a first step towards having a complete split global pcu and
BTS, some fields are still kept in BTS and will be moved over follow-up
smaller patches in the future (since this patch is already quite big).
So far, the code still only supports one BTS, which can be accessed
using the_pcu->bts. In the future that field will be replaced with a
list, and the BTS singletons will be removed.
The cur_fn output changes in TbfTest are actually a side effect fix,
since the singleton main_bts() now points internally to the_pcu->bts,
hence the same we allocate and assign in the test. Beforehand, "the_bts"
was allocated in the stack while main_bts() still returned an unrelated
singleton BTS object instance.
Related: OS#4935
Change-Id: I88e3c6471b80245ce3798223f1a61190f14aa840
2021-01-13 17:54:38 +00:00
|
|
|
the_pcu->pcu_sock_path, OSMO_SOCK_F_CONNECT);
|
2019-08-23 14:50:01 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DL1IF, LOGL_ERROR, "Failed to connect to the BTS (%s). "
|
Split PCU global PCU object from BTS object
Currently the BTS object (and gprs_rlcmac_bts struct) are used to hold
both PCU global fields and BTS specific fields, all mangled together.
The BTS is even accessed in lots of places by means of a singleton.
This patch introduces a new struct gprs_pcu object aimed at holding all
global state, and several fields are already moved from BTS to it. The
new object can be accessed as global variable "the_pcu", reusing and
including an already exisitng "the_pcu" global variable only used for
bssgp related purposes so far.
This is only a first step towards having a complete split global pcu and
BTS, some fields are still kept in BTS and will be moved over follow-up
smaller patches in the future (since this patch is already quite big).
So far, the code still only supports one BTS, which can be accessed
using the_pcu->bts. In the future that field will be replaced with a
list, and the BTS singletons will be removed.
The cur_fn output changes in TbfTest are actually a side effect fix,
since the singleton main_bts() now points internally to the_pcu->bts,
hence the same we allocate and assign in the test. Beforehand, "the_bts"
was allocated in the stack while main_bts() still returned an unrelated
singleton BTS object instance.
Related: OS#4935
Change-Id: I88e3c6471b80245ce3798223f1a61190f14aa840
2021-01-13 17:54:38 +00:00
|
|
|
"Retrying...\n", the_pcu->pcu_sock_path);
|
2020-02-06 10:15:11 +00:00
|
|
|
osmo_timer_setup(&pcu_sock_state.timer, pcu_sock_timeout, NULL);
|
|
|
|
osmo_timer_schedule(&pcu_sock_state.timer, 5, 0);
|
2012-07-12 10:49:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
pcu_sock_state.conn_bfd.cb = pcu_sock_cb;
|
|
|
|
pcu_sock_state.conn_bfd.data = NULL;
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2017-01-06 16:20:57 +00:00
|
|
|
LOGP(DL1IF, LOGL_NOTICE, "osmo-bts PCU socket %s has been connected\n",
|
Split PCU global PCU object from BTS object
Currently the BTS object (and gprs_rlcmac_bts struct) are used to hold
both PCU global fields and BTS specific fields, all mangled together.
The BTS is even accessed in lots of places by means of a singleton.
This patch introduces a new struct gprs_pcu object aimed at holding all
global state, and several fields are already moved from BTS to it. The
new object can be accessed as global variable "the_pcu", reusing and
including an already exisitng "the_pcu" global variable only used for
bssgp related purposes so far.
This is only a first step towards having a complete split global pcu and
BTS, some fields are still kept in BTS and will be moved over follow-up
smaller patches in the future (since this patch is already quite big).
So far, the code still only supports one BTS, which can be accessed
using the_pcu->bts. In the future that field will be replaced with a
list, and the BTS singletons will be removed.
The cur_fn output changes in TbfTest are actually a side effect fix,
since the singleton main_bts() now points internally to the_pcu->bts,
hence the same we allocate and assign in the test. Beforehand, "the_bts"
was allocated in the stack while main_bts() still returned an unrelated
singleton BTS object instance.
Related: OS#4935
Change-Id: I88e3c6471b80245ce3798223f1a61190f14aa840
2021-01-13 17:54:38 +00:00
|
|
|
the_pcu->pcu_sock_path);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2017-03-08 17:53:30 +00:00
|
|
|
pcu_tx_txt_ind(PCU_VERSION, "%s", PACKAGE_VERSION);
|
|
|
|
|
2018-02-14 18:46:33 +00:00
|
|
|
/* Schedule a timer so we keep trying until the BTS becomes active. */
|
2020-02-06 10:15:11 +00:00
|
|
|
osmo_timer_setup(&pcu_sock_state.timer, pcu_tx_txt_retry, NULL);
|
|
|
|
osmo_timer_schedule(&pcu_sock_state.timer, 5, 0);
|
2018-02-14 18:46:33 +00:00
|
|
|
|
2012-07-12 10:49:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcu_l1if_close(void)
|
|
|
|
{
|
|
|
|
struct osmo_fd *bfd;
|
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
osmo_timer_del(&pcu_sock_state.timer);
|
2012-07-12 10:49:15 +00:00
|
|
|
|
2020-02-06 10:15:11 +00:00
|
|
|
bfd = &pcu_sock_state.conn_bfd;
|
2012-07-12 10:49:15 +00:00
|
|
|
if (bfd->fd > 0)
|
2020-02-06 10:15:11 +00:00
|
|
|
pcu_sock_close(0);
|
2012-07-12 10:49:15 +00:00
|
|
|
}
|