summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2020-03-10 17:19:47 +0100
committerHarald Welte <laforge@osmocom.org>2020-03-10 18:58:55 +0100
commit17a9089452c074c184845e53ecb70139f91816d8 (patch)
treef3934149a404f1799b1f41fd5882127eb0d30790
parent419f617a3c04a68cd3054bb7bab9d0ab488937e2 (diff)
virtphy: Sync virtual_um.[ch] with osmo-bts
The files are used in both projects, and while the osmo-bts code has evolved, this copy didn't. Let's sync again (to libosmocore change-Id I303f2e616d2d32b5a8005c3dcf0f5fad19ad3445). Change-Id: I189ee28a85a6d7a7a07b062f6b07012478503e8f Depends: libosmocore.git Ib52d22710020b56965aefcef09bde8247ace4a9c Related: OS#2966
-rw-r--r--src/host/virt_phy/include/virtphy/virtual_um.h4
-rw-r--r--src/host/virt_phy/src/shared/virtual_um.c74
-rw-r--r--src/host/virt_phy/src/virtphy.c2
3 files changed, 54 insertions, 26 deletions
diff --git a/src/host/virt_phy/include/virtphy/virtual_um.h b/src/host/virt_phy/include/virtphy/virtual_um.h
index 52f2df64..fe060929 100644
--- a/src/host/virt_phy/include/virtphy/virtual_um.h
+++ b/src/host/virt_phy/include/virtphy/virtual_um.h
@@ -17,7 +17,9 @@
#define VIRT_UM_MSGB_SIZE 256
#define DEFAULT_MS_MCAST_GROUP "239.193.23.1"
+#define DEFAULT_MS_MCAST_PORT 4729 /* IANA-registered port for GSMTAP */
#define DEFAULT_BTS_MCAST_GROUP "239.193.23.2"
+#define DEFAULT_BTS_MCAST_PORT 4729 /* IANA-registered port for GSMTAP */
struct virt_um_inst {
void *priv;
@@ -27,7 +29,7 @@ struct virt_um_inst {
struct virt_um_inst *virt_um_init(
void *ctx, char *tx_mcast_group, uint16_t tx_mcast_port,
- char *rx_mcast_group, uint16_t rx_mcast_port,
+ char *rx_mcast_group, uint16_t rx_mcast_port, int ttl, const char *dev_name,
void (*recv_cb)(struct virt_um_inst *vui, struct msgb *msg));
void virt_um_destroy(struct virt_um_inst *vui);
diff --git a/src/host/virt_phy/src/shared/virtual_um.c b/src/host/virt_phy/src/shared/virtual_um.c
index 9415bfbb..14a444cf 100644
--- a/src/host/virt_phy/src/shared/virtual_um.c
+++ b/src/host/virt_phy/src/shared/virtual_um.c
@@ -27,7 +27,9 @@
#include <osmocom/core/talloc.h>
#include <virtphy/osmo_mcast_sock.h>
#include <virtphy/virtual_um.h>
+
#include <unistd.h>
+#include <errno.h>
/**
* Virtual UM interface file descriptor callback.
@@ -37,49 +39,71 @@ static int virt_um_fd_cb(struct osmo_fd *ofd, unsigned int what)
{
struct virt_um_inst *vui = ofd->data;
- // check if the read flag is set
if (what & BSC_FD_READ) {
- // allocate message buffer of specified size
- struct msgb *msg = msgb_alloc(VIRT_UM_MSGB_SIZE,
- "Virtual UM Rx");
+ struct msgb *msg = msgb_alloc(VIRT_UM_MSGB_SIZE, "Virtual UM Rx");
int rc;
- // read message from fd in message buffer
- rc = mcast_bidir_sock_rx(vui->mcast_sock, msgb_data(msg),
- msgb_tailroom(msg));
- // rc is number of bytes actually read
+ /* read message from fd into message buffer */
+ rc = mcast_bidir_sock_rx(vui->mcast_sock, msgb_data(msg), msgb_tailroom(msg));
if (rc > 0) {
msgb_put(msg, rc);
msg->l1h = msgb_data(msg);
- // call the l1 callback function for a received msg
+ /* call the l1 callback function for a received msg */
vui->recv_cb(vui, msg);
- } else {
- // TODO: this kind of error handling might be a bit harsh
+ } else if (rc == 0) {
vui->recv_cb(vui, NULL);
- // Unregister fd from select loop
- osmo_fd_unregister(ofd);
- close(ofd->fd);
- ofd->fd = -1;
- ofd->when = 0;
- }
+ osmo_fd_close(ofd);
+ } else
+ perror("Read from multicast socket");
+
}
return 0;
}
-struct virt_um_inst *virt_um_init(
- void *ctx, char *tx_mcast_group, uint16_t tx_mcast_port,
- char *rx_mcast_group, uint16_t rx_mcast_port,
- void (*recv_cb)(struct virt_um_inst *vui, struct msgb *msg))
+struct virt_um_inst *virt_um_init(void *ctx, char *tx_mcast_group, uint16_t tx_mcast_port,
+ char *rx_mcast_group, uint16_t rx_mcast_port, int ttl, const char *dev_name,
+ void (*recv_cb)(struct virt_um_inst *vui, struct msgb *msg))
{
struct virt_um_inst *vui = talloc_zero(ctx, struct virt_um_inst);
- vui->mcast_sock = mcast_bidir_sock_setup(ctx, tx_mcast_group,
- tx_mcast_port, rx_mcast_group, rx_mcast_port, 1,
- virt_um_fd_cb, vui);
+ int rc;
+
+ vui->mcast_sock = mcast_bidir_sock_setup(ctx, tx_mcast_group, tx_mcast_port,
+ rx_mcast_group, rx_mcast_port, 1, virt_um_fd_cb, vui);
+ if (!vui->mcast_sock) {
+ perror("Unable to create VirtualUm multicast socket");
+ talloc_free(vui);
+ return NULL;
+ }
vui->recv_cb = recv_cb;
+ if (ttl >= 0) {
+ rc = osmo_sock_mcast_ttl_set(vui->mcast_sock->tx_ofd.fd, ttl);
+ if (rc < 0) {
+ perror("Cannot set TTL of Virtual Um transmit socket");
+ goto out_close;
+ }
+ }
+
+ if (dev_name) {
+ rc = osmo_sock_mcast_iface_set(vui->mcast_sock->tx_ofd.fd, dev_name);
+ if (rc < 0) {
+ perror("Cannot bind multicast tx to given device");
+ goto out_close;
+ }
+ rc = osmo_sock_mcast_iface_set(vui->mcast_sock->rx_ofd.fd, dev_name);
+ if (rc < 0) {
+ perror("Cannot bind multicast rx to given device");
+ goto out_close;
+ }
+ }
+
return vui;
+out_close:
+ mcast_bidir_sock_close(vui->mcast_sock);
+ talloc_free(vui);
+ return NULL;
}
void virt_um_destroy(struct virt_um_inst *vui)
@@ -97,6 +121,8 @@ int virt_um_write_msg(struct virt_um_inst *vui, struct msgb *msg)
rc = mcast_bidir_sock_tx(vui->mcast_sock, msgb_data(msg),
msgb_length(msg));
+ if (rc < 0)
+ rc = -errno;
msgb_free(msg);
return rc;
diff --git a/src/host/virt_phy/src/virtphy.c b/src/host/virt_phy/src/virtphy.c
index d0a2ddbf..3290dd8e 100644
--- a/src/host/virt_phy/src/virtphy.c
+++ b/src/host/virt_phy/src/virtphy.c
@@ -231,7 +231,7 @@ int main(int argc, char *argv[])
LOGP(DVIRPHY, LOGL_INFO, "Virtual physical layer starting up...\n");
- g_vphy.virt_um = virt_um_init(tall_vphy_ctx, ul_tx_grp, port, dl_rx_grp, port,
+ g_vphy.virt_um = virt_um_init(tall_vphy_ctx, ul_tx_grp, port, dl_rx_grp, port, -1, NULL,
gsmtapl1_rx_from_virt_um_inst_cb);
g_vphy.l1ctl_sock = l1ctl_sock_init(tall_vphy_ctx, l1ctl_sap_rx_from_l23_inst_cb,