local_socket: add SO_SNDBUF & SO_RCVBUF support

lets a user program modify the size of the local_socket buffer
using setsockopt.

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2024-08-14 20:41:23 +08:00 committed by Xiang Xiao
parent 420648b0c6
commit 98c6cd45db
8 changed files with 98 additions and 18 deletions

View File

@ -49,8 +49,6 @@
#define LOCAL_NPOLLWAITERS 2
#define LOCAL_NCONTROLFDS 4
#define LOCAL_SEND_LIMIT (CONFIG_DEV_FIFO_SIZE - sizeof(uint16_t))
/****************************************************************************
* Public Type Definitions
****************************************************************************/
@ -122,6 +120,8 @@ struct local_conn_s
char lc_path[UNIX_PATH_MAX]; /* Path assigned by bind() */
int32_t lc_instance_id; /* Connection instance ID for stream
* server<->client connection pair */
uint32_t lc_sndsize; /* Send buffer size */
uint32_t lc_rcvsize; /* Receive buffer size */
FAR struct local_conn_s *
lc_peer; /* Peer connection instance */
@ -558,7 +558,8 @@ int local_getaddr(FAR struct local_conn_s *conn, FAR struct sockaddr *addr,
*
****************************************************************************/
int local_create_fifos(FAR struct local_conn_s *conn);
int local_create_fifos(FAR struct local_conn_s *conn,
uint32_t cssize, uint32_t scsize);
/****************************************************************************
* Name: local_create_halfduplex
@ -570,7 +571,7 @@ int local_create_fifos(FAR struct local_conn_s *conn);
#ifdef CONFIG_NET_LOCAL_DGRAM
int local_create_halfduplex(FAR struct local_conn_s *conn,
FAR const char *path);
FAR const char *path, uint32_t bufsize);
#endif
/****************************************************************************

View File

@ -153,6 +153,8 @@ FAR struct local_conn_s *local_alloc(void)
*/
conn->lc_crefs = 1;
conn->lc_sndsize = CONFIG_DEV_FIFO_SIZE;
conn->lc_rcvsize = CONFIG_DEV_FIFO_SIZE;
#ifdef CONFIG_NET_LOCAL_STREAM
nxsem_init(&conn->lc_waitsem, 0, 0);

View File

@ -35,6 +35,7 @@
#include <arch/irq.h>
#include <sys/stat.h>
#include <sys/param.h>
#include "utils/utils.h"
#include "socket/socket.h"
@ -87,7 +88,9 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
/* Create the FIFOs needed for the connection */
ret = local_create_fifos(client);
ret = local_create_fifos(client,
MIN(client->lc_sndsize, server->lc_rcvsize),
MIN(client->lc_rcvsize, server->lc_sndsize));
if (ret < 0)
{
nerr("ERROR: Failed to create FIFOs for %s: %d\n",

View File

@ -166,7 +166,7 @@ static bool local_fifo_exists(FAR const char *path)
*
****************************************************************************/
static int local_create_fifo(FAR const char *path)
static int local_create_fifo(FAR const char *path, uint32_t bufsize)
{
int ret;
@ -174,7 +174,7 @@ static int local_create_fifo(FAR const char *path)
if (!local_fifo_exists(path))
{
ret = nx_mkfifo(path, 0644, CONFIG_DEV_FIFO_SIZE);
ret = nx_mkfifo(path, 0644, bufsize);
if (ret < 0)
{
nerr("ERROR: Failed to create FIFO %s: %d\n", path, ret);
@ -422,7 +422,8 @@ int local_set_pollthreshold(FAR struct local_conn_s *conn,
*
****************************************************************************/
int local_create_fifos(FAR struct local_conn_s *conn)
int local_create_fifos(FAR struct local_conn_s *conn,
uint32_t cssize, uint32_t scsize)
{
char path[LOCAL_FULLPATH_LEN];
int ret;
@ -430,13 +431,13 @@ int local_create_fifos(FAR struct local_conn_s *conn)
/* Create the client-to-server FIFO if it does not already exist. */
local_cs_name(conn, path);
ret = local_create_fifo(path);
ret = local_create_fifo(path, cssize);
if (ret >= 0)
{
/* Create the server-to-client FIFO if it does not already exist. */
local_sc_name(conn, path);
ret = local_create_fifo(path);
ret = local_create_fifo(path, scsize);
}
return ret;
@ -452,14 +453,14 @@ int local_create_fifos(FAR struct local_conn_s *conn)
#ifdef CONFIG_NET_LOCAL_DGRAM
int local_create_halfduplex(FAR struct local_conn_s *conn,
FAR const char *path)
FAR const char *path, uint32_t bufsize)
{
char fullpath[LOCAL_FULLPATH_LEN];
/* Create the half duplex FIFO if it does not already exist. */
local_hd_name(path, fullpath);
return local_create_fifo(fullpath);
return local_create_fifo(fullpath, bufsize);
}
#endif /* CONFIG_NET_LOCAL_DGRAM */

View File

@ -397,7 +397,7 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
/* Make sure that half duplex FIFO has been created */
ret = local_create_halfduplex(conn, conn->lc_path);
ret = local_create_halfduplex(conn, conn->lc_path, conn->lc_rcvsize);
if (ret < 0)
{
nerr("ERROR: Failed to create FIFO for %s: %d\n",

View File

@ -335,7 +335,7 @@ static ssize_t local_sendto(FAR struct socket *psock,
* REVISIT: Or should be just make sure that it already exists?
*/
ret = local_create_halfduplex(conn, unaddr->sun_path);
ret = local_create_halfduplex(conn, unaddr->sun_path, conn->lc_sndsize);
if (ret < 0)
{
nerr("ERROR: Failed to create FIFO for %s: %zd\n",

View File

@ -136,7 +136,7 @@ int local_send_preamble(FAR struct local_conn_s *conn,
len16 += iov->iov_len;
}
if (len16 > LOCAL_SEND_LIMIT)
if (len16 > conn->lc_sndsize - sizeof(uint32_t))
{
nerr("ERROR: Packet is too big: %d\n", len16);
return -EMSGSIZE;

View File

@ -34,6 +34,7 @@
#include <debug.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/net/net.h>
@ -545,6 +546,8 @@ static int local_getpeername(FAR struct socket *psock,
static int local_getsockopt(FAR struct socket *psock, int level, int option,
FAR void *value, FAR socklen_t *value_len)
{
FAR struct local_conn_s *conn = psock->s_conn;
DEBUGASSERT(psock->s_domain == PF_LOCAL);
if (level == SOL_SOCKET)
@ -554,7 +557,6 @@ static int local_getsockopt(FAR struct socket *psock, int level, int option,
#ifdef CONFIG_NET_LOCAL_SCM
case SO_PEERCRED:
{
FAR struct local_conn_s *conn = psock->s_conn;
if (*value_len != sizeof(struct ucred))
{
return -EINVAL;
@ -572,7 +574,36 @@ static int local_getsockopt(FAR struct socket *psock, int level, int option,
return -EINVAL;
}
*(FAR int *)value = LOCAL_SEND_LIMIT;
if (psock->s_type == SOCK_STREAM)
{
*(FAR int *)value = conn->lc_sndsize;
}
else
{
*(FAR int *)value = conn->lc_sndsize -
sizeof(uint32_t) -
UNIX_PATH_MAX;
}
return OK;
}
case SO_RCVBUF:
{
if (*value_len != sizeof(int))
{
return -EINVAL;
}
if (psock->s_type == SOCK_STREAM)
{
*(FAR int *)value = conn->lc_rcvsize;
}
else
{
*(FAR int *)value = conn->lc_rcvsize -
sizeof(uint32_t) -
UNIX_PATH_MAX;
}
return OK;
}
}
@ -606,6 +637,46 @@ static int local_getsockopt(FAR struct socket *psock, int level, int option,
static int local_setsockopt(FAR struct socket *psock, int level, int option,
FAR const void *value, socklen_t value_len)
{
FAR struct local_conn_s *conn = psock->s_conn;
DEBUGASSERT(psock->s_domain == PF_LOCAL);
if (level == SOL_SOCKET)
{
switch (option)
{
case SO_SNDBUF:
{
if (psock->s_type == SOCK_STREAM)
{
conn->lc_sndsize = *(FAR const int *)value;
}
else
{
conn->lc_sndsize = *(FAR const int *)value +
sizeof(uint32_t) +
UNIX_PATH_MAX;
}
return OK;
}
case SO_RCVBUF:
{
if (psock->s_type == SOCK_STREAM)
{
conn->lc_rcvsize = *(FAR const int *)value;
}
else
{
conn->lc_rcvsize = *(FAR const int *)value +
sizeof(uint32_t) +
UNIX_PATH_MAX;
}
return OK;
}
}
}
return -ENOPROTOOPT;
}
@ -902,7 +973,9 @@ static int local_socketpair(FAR struct socket *psocks[2])
/* Create the FIFOs needed for the connection */
ret = local_create_fifos(conns[0]);
ret = local_create_fifos(conns[0],
MIN(conns[0]->lc_sndsize, conns[1]->lc_rcvsize),
MIN(conns[0]->lc_rcvsize, conns[1]->lc_sndsize));
if (ret < 0)
{
goto errout;