network: Move USRSOCK specific code from from inet_sockif to usrsock_sockif

This commit is contained in:
Jussi Kivilinna 2017-07-31 09:33:59 -06:00 committed by Gregory Nutt
parent 00d8dd3912
commit 7dfb01dbce
14 changed files with 105 additions and 315 deletions

View File

@ -562,45 +562,6 @@ int inet_close(FAR struct socket *psock)
break;
#endif /* CONFIG_NET_UDP */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
int ret;
/* Is this the last reference to the connection structure (there
* could be more if the socket was dup'ed).
*/
if (conn->crefs <= 1)
{
/* Yes... inform user-space daemon of socket close. */
ret = usrsock_close(conn);
/* Free the connection structure */
conn->crefs = 0;
usrsock_free(psock->s_conn);
if (ret < 0)
{
/* Return with error code, but free resources. */
nerr("ERROR: usrsock_close failed: %d\n", ret);
return ret;
}
}
else
{
/* No.. Just decrement the reference count */
conn->crefs--;
}
}
break;
#endif
default:
return -EBADF;
}

View File

@ -504,13 +504,6 @@ int inet_connect(FAR struct socket *psock, FAR const struct sockaddr *addr,
#endif
default:
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
break;
}
#endif
DEBUGPANIC();
return -EAFNOSUPPORT;
}
@ -552,13 +545,6 @@ int inet_connect(FAR struct socket *psock, FAR const struct sockaddr *addr,
}
#endif /* CONFIG_NET_UDP */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
return usrsock_connect(psock, addr, addrlen);
}
#endif /* CONFIG_NET_USRSOCK */
default:
return -EBADF;
}

View File

@ -392,26 +392,6 @@ int inet_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
{
int ret;
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn);
/* Handle usrsock getsockname */
ret = usrsock_getsockname(conn, addr, addrlen);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return OK;
}
#endif
/* Handle by address domain */
switch (psock->s_domain)

View File

@ -1611,14 +1611,6 @@ ssize_t inet_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
#endif
default:
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
minlen = 0;
break;
}
#endif
DEBUGPANIC();
return -EINVAL;
}
@ -1658,16 +1650,6 @@ ssize_t inet_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
break;
#endif /* CONFIG_NET_UDP */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
/* Perform the usrsock recvfrom operation */
ret = usrsock_recvfrom(psock, buf, len, from, fromlen);
}
break;
#endif
default:
{
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);

View File

@ -51,7 +51,6 @@
#include "tcp/tcp.h"
#include "udp/udp.h"
#include "sixlowpan/sixlowpan.h"
#include "usrsock/usrsock.h"
#include "socket/socket.h"
#ifdef HAVE_INET_SOCKETS
@ -187,73 +186,6 @@ static int inet_udp_alloc(FAR struct socket *psock)
}
#endif /* NET_UDP_HAVE_STACK */
/****************************************************************************
* Name: usrsock_socket_setup
*
* Description:
* Special socket setup may be required by user sockets.
*
* Parameters:
* domain (see sys/socket.h)
* type (see sys/socket.h)
* protocol (see sys/socket.h)
* psock A pointer to a user allocated socket structure to be initialized.
*
* Returned Value:
* 0 on success; a negated errno value is returned on failure.
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
static int usrsock_socket_setup(int domain, int type, int protocol,
FAR struct socket *psock)
{
int ret;
switch (domain)
{
default:
return OK;
case PF_INET:
case PF_INET6:
{
#ifndef CONFIG_NET_USRSOCK_UDP
if (type == SOCK_DGRAM)
{
return -ENETDOWN;
}
#endif
#ifndef CONFIG_NET_USRSOCK_TCP
if (type == SOCK_STREAM)
{
return -ENETDOWN;
}
#endif
psock->s_type = PF_UNSPEC;
psock->s_conn = NULL;
/* Let the user socket logic handle the setup...
*
* A return value of zero means that the operation was
* successfully handled by usrsock. A negative value means that
* an error occurred. The special error value -ENETDOWN means
* that usrsock daemon is not running. The caller should attempt
* to open socket with kernel networking stack in this case.
*/
ret = usrsock_socket(domain, type, protocol, psock);
if (ret == -ENETDOWN)
{
nwarn("WARNING: usrsock daemon is not running\n");
}
return ret;
}
}
}
#endif /* CONFIG_NET_USRSOCK */
/****************************************************************************
* Name: inet_setup
*
@ -277,24 +209,6 @@ static int usrsock_socket_setup(int domain, int type, int protocol,
static int inet_setup(FAR struct socket *psock, int protocol)
{
#ifdef CONFIG_NET_USRSOCK
int ret;
/* Handle special setup for user INET sockets */
ret = usrsock_socket_setup(psock->s_domain, psock->s_type, protocol, psock);
if (ret == -ENETDOWN)
{
/* -ENETDOWN means that usrsock daemon is not running. Attempt to
* open socket with kernel networking stack.
*/
}
else
{
return ret;
}
#endif /* CONFIG_NET_USRSOCK */
/* Allocate the appropriate connection structure. This reserves the
* the connection structure is is unallocated at this point. It will
* not actually be initialized until the socket is connected.
@ -383,11 +297,6 @@ static sockcaps_t inet_sockcaps(FAR struct socket *psock)
#endif
#endif
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
return SOCKCAP_NONBLOCKING;
#endif
default:
return 0;
}
@ -397,7 +306,7 @@ static sockcaps_t inet_sockcaps(FAR struct socket *psock)
* Name: inet_addref
*
* Description:
* Increment the refernce count on the underlying connection structure.
* Increment the reference count on the underlying connection structure.
*
* Parameters:
* psock - Socket structure of the socket whose reference count will be
@ -429,15 +338,6 @@ static void inet_addref(FAR struct socket *psock)
conn->crefs++;
}
else
#endif
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn->crefs > 0 && conn->crefs < 255);
conn->crefs++;
}
else
#endif
{
nerr("ERROR: Unsupported type: %d\n", psock->s_type);
@ -501,20 +401,6 @@ static int inet_bind(FAR struct socket *psock,
switch (psock->s_type)
{
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn != NULL);
/* Perform the usrsock bind operation */
ret = usrsock_bind(conn, addr, addrlen);
}
break;
#endif
#ifdef CONFIG_NET_TCP
case SOCK_STREAM:
{
@ -600,19 +486,6 @@ static int inet_getsockname(FAR struct socket *psock,
FAR struct sockaddr *addr,
FAR socklen_t *addrlen)
{
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn != NULL);
/* Handle usrsock getsockname */
return usrsock_getsockname(conn, addr, addrlen);
}
#endif
/* Handle by address domain */
switch (psock->s_domain)
@ -671,13 +544,6 @@ int inet_listen(FAR struct socket *psock, int backlog)
if (psock->s_type != SOCK_STREAM)
{
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
#warning "Missing logic"
}
#endif
nerr("ERROR: Unsupported socket type: %d\n",
psock->s_type);
return -EOPNOTSUPP;
@ -775,13 +641,6 @@ static int inet_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
if (psock->s_type != SOCK_STREAM)
{
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
#warning "Missing logic"
}
#endif
nerr("ERROR: Inappropreat socket type: %d\n", psock->s_type);
return -EOPNOTSUPP;
}
@ -985,15 +844,6 @@ static inline int inet_pollteardown(FAR struct socket *psock,
static int inet_poll(FAR struct socket *psock, FAR struct pollfd *fds,
bool setup)
{
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
/* Perform usrsock setup/teardown. */
return usrsock_poll(psock, fds, setup);
}
else
#endif
#if defined(HAVE_TCP_POLL) || defined(HAVE_UDP_POLL)
/* Check if we are setting up or tearing down the poll */
@ -1097,16 +947,6 @@ static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf,
break;
#endif /* CONFIG_NET_UDP */
/* Special case user sockets */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
ret = usrsock_sendto(psock, buf, len, NULL, 0);
}
break;
#endif /*CONFIG_NET_USRSOCK*/
default:
{
/* EDESTADDRREQ. Signifies that the socket is not connection-mode
@ -1151,78 +991,67 @@ static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf,
socklen_t minlen;
ssize_t nsent;
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
/* Perform the usrsock sendto operation */
/* Verify that a valid address has been provided */
nsent = usrsock_sendto(psock, buf, len, to, tolen);
}
else
#endif
switch (to->sa_family)
{
/* Verify that a valid address has been provided */
switch (to->sa_family)
{
#ifdef CONFIG_NET_IPv4
case AF_INET:
minlen = sizeof(struct sockaddr_in);
break;
case AF_INET:
minlen = sizeof(struct sockaddr_in);
break;
#endif
#ifdef CONFIG_NET_IPv6
case AF_INET6:
minlen = sizeof(struct sockaddr_in6);
break;
case AF_INET6:
minlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
nerr("ERROR: Unrecognized address family: %d\n", to->sa_family);
return -EAFNOSUPPORT;
}
default:
nerr("ERROR: Unrecognized address family: %d\n", to->sa_family);
return -EAFNOSUPPORT;
}
if (tolen < minlen)
{
nerr("ERROR: Invalid address length: %d < %d\n", tolen, minlen);
return -EBADF;
}
if (tolen < minlen)
{
nerr("ERROR: Invalid address length: %d < %d\n", tolen, minlen);
return -EBADF;
}
#ifdef CONFIG_NET_UDP
/* If this is a connected socket, then return EISCONN */
/* If this is a connected socket, then return EISCONN */
if (psock->s_type != SOCK_DGRAM)
{
nerr("ERROR: Connected socket\n");
return -EBADF;
}
if (psock->s_type != SOCK_DGRAM)
{
nerr("ERROR: Connected socket\n");
return -EBADF;
}
/* Now handle the INET sendto() operation */
/* Now handle the INET sendto() operation */
#if defined(CONFIG_NET_6LOWPAN)
/* Try 6LoWPAN UDP packet sendto() */
/* Try 6LoWPAN UDP packet sendto() */
nsent = psock_6lowpan_udp_sendto(psock, buf, len, flags, to, tolen);
nsent = psock_6lowpan_udp_sendto(psock, buf, len, flags, to, tolen);
#if defined(CONFIG_NETDEV_MULTINIC) && defined(NET_UDP_HAVE_STACK)
if (nsent < 0)
{
/* UDP/IP packet sendto */
if (nsent < 0)
{
/* UDP/IP packet sendto */
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
}
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
}
#endif /* CONFIG_NETDEV_MULTINIC && NET_UDP_HAVE_STACK */
#elif defined(NET_UDP_HAVE_STACK)
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
#else
nwarn("WARNING: UDP not available in this configuiration\n");
nsent = -ENOSYS;
nwarn("WARNING: UDP not available in this configuiration\n");
nsent = -ENOSYS;
#endif /* CONFIG_NET_6LOWPAN */
#else
nwarn("WARNING: UDP not enabled in this configuiration\n");
nsent = -EISCONN;
nwarn("WARNING: UDP not enabled in this configuiration\n");
nsent = -EISCONN;
#endif /* CONFIG_NET_UDP */
}
return nsent;
}

View File

@ -44,6 +44,7 @@
#include <assert.h>
#include <debug.h>
#include "usrsock/usrsock.h"
#include "socket/socket.h"
#ifdef CONFIG_NET
@ -95,7 +96,7 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
FAR const struct sock_intf_s *sockif = NULL;
int errcode;
int ret;
/* Initialize the socket structure */
psock->s_domain = domain;
@ -105,6 +106,35 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
psock->s_sndcb = NULL;
#endif
#ifdef CONFIG_NET_USRSOCK
if (domain != PF_LOCAL && domain != PF_UNSPEC)
{
/* Handle special setup for USRSOCK sockets (user-space networking
* stack).
*/
ret = g_usrsock_sockif.si_setup(psock, protocol);
if (ret == -ENETDOWN)
{
/* -ENETDOWN means that USRSOCK daemon is not running. Attempt to
* open socket with kernel networking stack.
*/
}
else
{
psock->s_sockif = &g_usrsock_sockif;
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return ret;
}
}
#endif /* CONFIG_NET_USRSOCK */
/* Get the socket interface */
sockif = net_sockif(domain);

View File

@ -60,15 +60,14 @@
#undef HAVE_PFINET_SOCKETS
#undef HAVE_PFINET6_SOCKETS
#if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6) || \
defined(CONFIG_NET_USRSOCK)
#if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6)
# define HAVE_INET_SOCKETS
# if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_USRSOCK)
# if defined(CONFIG_NET_IPv4)
# define HAVE_PFINET_SOCKETS
# endif
# if defined(CONFIG_NET_IPv6) || defined(CONFIG_NET_USRSOCK)
# if defined(CONFIG_NET_IPv6)
# define HAVE_PFINET6_SOCKETS
# endif
#endif

View File

@ -23,6 +23,16 @@ config NET_USRSOCK_CONNS
Note: Usrsock daemon can impose additional restrictions for
maximum number of concurrent connections supported.
config NET_USRSOCK_NO_INET
bool "Disable PF_INET for usrsock"
default n
---help---
config NET_USRSOCK_NO_INET6
bool "Disable PF_INET6 for usrsock"
default n
---help---
config NET_USRSOCK_UDP
bool "User-space daemon provides UDP sockets"
default n
@ -35,5 +45,10 @@ config NET_USRSOCK_TCP
select NET_TCP
---help---
config NET_USRSOCK_OTHER
bool "Enable other protocol families in addition of INET & INET6"
default n
---help---
endif # NET_USRSOCK
endmenu # User-space networking stack API

View File

@ -41,7 +41,7 @@ NET_CSRCS += usrsock_close.c usrsock_conn.c usrsock_bind.c usrsock_connect.c
NET_CSRCS += usrsock_dev.c
NET_CSRCS += usrsock_event.c usrsock_getsockname.c usrsock_getsockopt.c
NET_CSRCS += usrsock_poll.c usrsock_recvfrom.c usrsock_sendto.c
NET_CSRCS += usrsock_setsockopt.c usrsock_socket.c
NET_CSRCS += usrsock_setsockopt.c usrsock_socket.c usrsock_sockif.c
# Include User Socket build support

View File

@ -153,6 +153,8 @@ extern "C"
# define EXTERN extern
#endif
EXTERN const struct sock_intf_s g_usrsock_sockif;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@ -368,7 +370,7 @@ int usrsock_close(FAR struct usrsock_conn_s *conn);
*
****************************************************************************/
int usrsock_bind(FAR struct usrsock_conn_s *conn,
int usrsock_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr,
socklen_t addrlen);
@ -427,6 +429,7 @@ int usrsock_poll(FAR struct socket *psock, FAR struct pollfd *fds, bool setup);
* psock A reference to the socket structure of the socket to be connected
* buf Data to send
* len Length of data to send
* flags Send flags (ignored)
* to Address of recipient
* tolen The length of the address structure
*
@ -438,7 +441,7 @@ int usrsock_poll(FAR struct socket *psock, FAR struct pollfd *fds, bool setup);
****************************************************************************/
ssize_t usrsock_sendto(FAR struct socket *psock, FAR const void *buf,
size_t len, FAR const struct sockaddr *to,
size_t len, int flags, FAR const struct sockaddr *to,
socklen_t tolen);
/****************************************************************************
@ -457,14 +460,15 @@ ssize_t usrsock_sendto(FAR struct socket *psock, FAR const void *buf,
* psock A pointer to a NuttX-specific, internal socket structure
* buf Buffer to receive data
* len Length of buffer
* flags Receive flags
* flags Receive flags (ignored)
* from Address of source (may be NULL)
* fromlen The length of the address structure
*
****************************************************************************/
ssize_t usrsock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
FAR struct sockaddr *from, FAR socklen_t *fromlen);
int flags, FAR struct sockaddr *from,
FAR socklen_t *fromlen);
/****************************************************************************
* Name: usrsock_getsockopt
@ -542,7 +546,7 @@ int usrsock_setsockopt(FAR struct usrsock_conn_s *conn, int level, int option,
*
****************************************************************************/
int usrsock_getsockname(FAR struct usrsock_conn_s *conn,
int usrsock_getsockname(FAR struct socket *psock,
FAR struct sockaddr *addr, FAR socklen_t *addrlen);
#undef EXTERN

View File

@ -162,10 +162,11 @@ static int do_bind_request(FAR struct usrsock_conn_s *conn,
*
****************************************************************************/
int usrsock_bind(FAR struct usrsock_conn_s *conn,
int usrsock_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr,
socklen_t addrlen)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
struct usrsock_reqstate_s state = {};
ssize_t ret;

View File

@ -189,9 +189,10 @@ static void setup_conn_getsockname(FAR struct usrsock_conn_s *conn,
*
****************************************************************************/
int usrsock_getsockname(FAR struct usrsock_conn_s *conn,
int usrsock_getsockname(FAR struct socket *psock,
FAR struct sockaddr *addr, FAR socklen_t *addrlen)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
struct usrsock_data_reqstate_s state = {};
struct iovec inbufs[1];
ssize_t ret;

View File

@ -229,13 +229,15 @@ static void setup_conn_recvfrom(FAR struct usrsock_conn_s *conn,
* psock A pointer to a NuttX-specific, internal socket structure
* buf Buffer to receive data
* len Length of buffer
* flags Receive flags (ignored)
* from Address of source (may be NULL)
* fromlen The length of the address structure
*
****************************************************************************/
ssize_t usrsock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
FAR struct sockaddr *from, FAR socklen_t *fromlen)
int flags, FAR struct sockaddr *from,
FAR socklen_t *fromlen)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
struct usrsock_data_reqstate_s state = {};

View File

@ -201,14 +201,14 @@ static int do_sendto_request(FAR struct usrsock_conn_s *conn,
* psock A pointer to a NuttX-specific, internal socket structure
* buf Data to send
* len Length of data to send
* flags Send flags
* flags Send flags (ignored)
* to Address of recipient
* tolen The length of the address structure
*
****************************************************************************/
ssize_t usrsock_sendto(FAR struct socket *psock, FAR const void *buf,
size_t len, FAR const struct sockaddr *to,
size_t len, int flags, FAR const struct sockaddr *to,
socklen_t tolen)
{
FAR struct usrsock_conn_s *conn = psock->s_conn;