blob: 4ab4444e4d3a6a93629f99b5e345efcd01e44db2 [file] [log] [blame]
/*
<:copyright-BRCM:2016:DUAL/GPL:standard
Broadcom Proprietary and Confidential.(c) 2016 Broadcom
All Rights Reserved
Unless you and Broadcom execute a separate written software license
agreement governing use of this software, this software is licensed
to you under the terms of the GNU General Public License version 2
(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
with the following added to such license:
As a special exception, the copyright holders of this software give
you permission to link this software with independent modules, and
to copy and distribute the resulting executable under terms of your
choice, provided that you also meet, for each linked independent
module, the terms and conditions of the license of that module.
An independent module is a module which is not derived from this
software. The special exception does not apply to any modifications
of the software.
Not withstanding the above, under no circumstances may you combine
this software in any way with any other Broadcom software provided
under a license other than the GPL, without Broadcom's express prior
written consent.
:>
*/
#include <bcmos_system.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <bcmtr_plugin.h>
#include <bcm_config.h>
/* Last seen host IP and port */
static struct sockaddr_in peer_sa[BCMTR_MAX_OLTS];
/* This plugin stores both device id and per-device socket handle in
* transport's channel_id handle as
* (device_id << 24) | socket_handle.
* The following functions recover socket_handle and device_id from the channel_id handle
*/
static inline int _bcmtr_channel_to_socket(bcmtr_plugin_channel ch)
{
return (int)(ch & 0xffffff);
}
static inline int _bcmtr_channel_to_device(bcmtr_plugin_channel ch)
{
return (int)((ch >> 24) & 0xff);
}
/** Open communication channel */
static bcmos_errno bcmtr_udp_open(int device, bcmtr_plugin_cfg *cfg, bcmtr_plugin_channel *ch)
{
struct sockaddr_in sa = {};
struct timeval timeout = { .tv_sec = 0, .tv_usec = BCMTR_MSG_TIMEOUT * 1000 };
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
BCMOS_TRACE_RETURN(BCM_ERR_COMM_FAIL, "Can't create UDP socket\n");
}
/* Bind local */
sa.sin_family = AF_INET;
sa.sin_port = htons(cfg->x.udp.my_port);
sa.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr*)&sa, sizeof(sa) ) == -1)
{
close(s);
perror("bind");
BCMOS_TRACE_RETURN(BCM_ERR_COMM_FAIL, "Can't bind UDP socket to %d.%d.%d.%d:%d\n",
(int)(cfg->x.udp.my_ip >> 24), (int)((cfg->x.udp.my_ip >> 16) & 0xff),
(int)((cfg->x.udp.my_ip >> 8) & 0xff), (int)(cfg->x.udp.my_ip & 0xff),
(int)cfg->x.udp.my_port);
}
bcmos_printf("%s: socket bound to port %d\n", __FUNCTION__, (int)cfg->x.udp.my_port);
if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
BCMOS_TRACE_RETURN(BCM_ERR_COMM_FAIL, "Can't set socket option for send timeout\n");
}
/* Connect to remote */
if (cfg->x.udp.remote_port)
{
peer_sa[device].sin_family = AF_INET;
peer_sa[device].sin_port = htons(cfg->x.udp.remote_port);
peer_sa[device].sin_addr.s_addr = htonl(cfg->x.udp.remote_ip);
}
*ch = (device << 24) | s;
return BCM_ERR_OK;
}
/* print error message with throttling: not oftener than ones per second */
static void _bcmtr_udp_print_err(bcmos_errno err, const char *err_msg)
{
static int nerrors;
static uint32_t last_timestamp;
++nerrors;
if (!last_timestamp || bcmos_timestamp() - last_timestamp > 1000000)
{
BCMOS_TRACE_ERR("Error in low-level UDP transport. %s-%s. Errors since last report: %d\n",
err_msg, bcmos_strerror(err), nerrors);
last_timestamp = bcmos_timestamp();
nerrors = 0;
}
}
/** Close communication channel */
static bcmos_errno bcmtr_udp_close(bcmtr_plugin_channel ch)
{
close(_bcmtr_channel_to_socket(ch));
return BCM_ERR_OK;
}
/** Send data */
static bcmos_errno bcmtr_udp_send(bcmtr_plugin_channel ch, bcmolt_subchannel subch, bcmolt_buf *buf,
bcmtr_send_flags flags)
{
int sock = _bcmtr_channel_to_socket(ch);
int device = _bcmtr_channel_to_device(ch);
int buflen = bcmolt_buf_get_used(buf);
struct iovec iov = { .iov_base = buf->start, .iov_len = buflen };
struct msghdr msg = {
.msg_iov = &iov, .msg_iovlen = 1, .msg_flags = 0,
.msg_name = (void *)&peer_sa[device], .msg_namelen = sizeof(peer_sa[0])
};
int len;
len = sendmsg(sock, &msg, 0);
if (len < buflen)
{
bcmos_printf("%s: sock_sendmsg(%u) --> %d. errno=%d\n", __FUNCTION__, buflen, len, errno);
return BCM_ERR_COMM_FAIL;
}
return BCM_ERR_OK;
}
/** Receive data */
static bcmos_errno bcmtr_udp_recv(bcmtr_plugin_channel ch, bcmolt_subchannel *subch, bcmolt_buf *buf)
{
int sock = _bcmtr_channel_to_socket(ch);
int device = _bcmtr_channel_to_device(ch);
struct iovec iov;
struct msghdr msg = {
.msg_iov = &iov, .msg_iovlen = 1,
.msg_name = (void *)&peer_sa[device], .msg_namelen = sizeof(peer_sa[0])
};
int len;
fd_set read_fds;
struct timeval tv;
bcmos_errno err;
int rc;
FD_ZERO(&read_fds);
FD_SET(sock, &read_fds);
tv.tv_sec = 0;
tv.tv_usec = BCMTR_MSG_TIMEOUT * 1000;
rc = select(sock + 1, &read_fds, NULL, NULL, &tv);
if (rc < 0)
{
perror("select");
return BCM_ERR_COMM_FAIL;
}
if (!rc || !FD_ISSET(sock, &read_fds))
{
return BCM_ERR_TIMEOUT;
}
err = bcmolt_buf_alloc(buf, BCMTR_MAX_MTU_SIZE, BCMOLT_BUF_ENDIAN_FIXED);
if (err)
return BCM_ERR_NOMEM;
iov.iov_base = buf->start;
iov.iov_len = BCMTR_MAX_MTU_SIZE;
len = recvmsg(sock, &msg, 0);
if (len <= 0)
{
_bcmtr_udp_print_err(BCM_ERR_COMM_FAIL, "recvmsg() failed\n");
bcmolt_buf_free(buf);
return BCM_ERR_COMM_FAIL;
}
buf->len = len;
*subch = ntohs(peer_sa[device].sin_port);
return BCM_ERR_OK;
}
/** Initialize plugin callbacks
* \param[in,out] driver Transport plugin driver structure
* \return error code
*/
bcmos_errno bcmtr_plugin_init(bcmtr_plugin_cfg *cfg, bcmtr_driver *driver)
{
if (cfg->type != BCMTR_TYPE_UDP)
{
BCMOS_TRACE_RETURN(BCM_ERR_PARM, "Invalid plugin type. Expected UDP\n");
}
driver->open = bcmtr_udp_open;
driver->close = bcmtr_udp_close;
driver->recv = bcmtr_udp_recv;
driver->send = bcmtr_udp_send;
return BCM_ERR_OK;
}