Skip to content

Commit

Permalink
Add user-space networking stack API (usrsock)
Browse files Browse the repository at this point in the history
User-space networking stack API allows user-space daemon to
provide TCP/IP stack implementation for NuttX network.

Main use for this is to allow use and seamless integration of
HW-provided TCP/IP stacks to NuttX.

For example, user-space daemon can translate /dev/usrsock
API requests to HW TCP/IP API requests while rest of the
user-space can access standard socket API, with socket
descriptors that can be used with NuttX system calls.
  • Loading branch information
Jussi Kivilinna authored and gregory-nutt committed Mar 31, 2017
1 parent c999519 commit cd3c963
Show file tree
Hide file tree
Showing 51 changed files with 6,283 additions and 124 deletions.
14 changes: 13 additions & 1 deletion include/nuttx/net/netconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
*
* - Maximum Transfer Unit (MTU)
* - TCP Receive Window size (See TCP configuration options below)
*
*
* A better solution would be to support device-by-device MTU and receive
* window sizes. This minimum support is require to support the optimal
* SLIP MTU of 296 bytes and the standard Ethernet MTU of 1500
Expand Down Expand Up @@ -547,6 +547,18 @@
# define CONFIG_NET_ARP_MAXAGE 120
#endif

/* Usrsock configuration options */

/* The maximum amount of concurrent usrsock connections, Default: 6 */

#ifndef CONFIG_NET_USRSOCK_CONNS
# ifdef CONFIG_NET_USRSOCK
# define CONFIG_NET_USRSOCK_CONNS 6
# else
# define CONFIG_NET_USRSOCK_CONNS 0
# endif
#endif

/* General configuration options */

/* Delay after receive to catch a following packet. No delay should be
Expand Down
233 changes: 233 additions & 0 deletions include/nuttx/net/usrsock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
/****************************************************************************
* include/nuttx/net/usrsock.h
*
* Copyright (C) 2015, 2017 Haltian Ltd. All rights reserved.
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#ifndef __INCLUDE_NUTTX_NET_USRSOCK_H
#define __INCLUDE_NUTTX_NET_USRSOCK_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <stdbool.h>

#include <nuttx/net/netconfig.h>
#include <nuttx/compiler.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Event message flags */

#define USRSOCK_EVENT_ABORT (1 << 0)
#define USRSOCK_EVENT_SENDTO_READY (1 << 1)
#define USRSOCK_EVENT_RECVFROM_AVAIL (1 << 2)
#define USRSOCK_EVENT_REMOTE_CLOSED (1 << 3)

/* Response message flags */

#define USRSOCK_MESSAGE_FLAG_REQ_IN_PROGRESS (1 << 0)
#define USRSOCK_MESSAGE_FLAG_EVENT (1 << 1)

#define USRSOCK_MESSAGE_IS_EVENT(flags) \
(!!((flags) & USRSOCK_MESSAGE_FLAG_EVENT))
#define USRSOCK_MESSAGE_IS_REQ_RESPONSE(flags) \
(!USRSOCK_MESSAGE_IS_EVENT(flags))

#define USRSOCK_MESSAGE_REQ_IN_PROGRESS(flags) \
(!!((flags) & USRSOCK_MESSAGE_FLAG_REQ_IN_PROGRESS))
#define USRSOCK_MESSAGE_REQ_COMPLETED(flags) \
(!USRSOCK_MESSAGE_REQ_IN_PROGRESS(flags))

/****************************************************************************
* Public Types
****************************************************************************/

/* Request types */

enum usrsock_request_types_e
{
USRSOCK_REQUEST_SOCKET = 0,
USRSOCK_REQUEST_CLOSE,
USRSOCK_REQUEST_CONNECT,
USRSOCK_REQUEST_SENDTO,
USRSOCK_REQUEST_RECVFROM,
USRSOCK_REQUEST_SETSOCKOPT,
USRSOCK_REQUEST_GETSOCKOPT,
USRSOCK_REQUEST_GETSOCKNAME,
USRSOCK_REQUEST_BIND,
USRSOCK_REQUEST__MAX
};

/* Response/event message types */

enum usrsock_message_types_e
{
USRSOCK_MESSAGE_RESPONSE_ACK = 0,
USRSOCK_MESSAGE_RESPONSE_DATA_ACK,
USRSOCK_MESSAGE_SOCKET_EVENT,
};

/* Request structures (kernel => /dev/usrsock => daemon) */

begin_packed_struct struct usrsock_request_common_s
{
int8_t reqid;
uint8_t xid;
} end_packed_struct;

begin_packed_struct struct usrsock_request_socket_s
{
struct usrsock_request_common_s head;

int16_t domain;
int16_t type;
int16_t protocol;
} end_packed_struct;

begin_packed_struct struct usrsock_request_close_s
{
struct usrsock_request_common_s head;

int16_t usockid;
} end_packed_struct;

begin_packed_struct struct usrsock_request_bind_s
{
struct usrsock_request_common_s head;

int16_t usockid;
uint16_t addrlen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_connect_s
{
struct usrsock_request_common_s head;

int16_t usockid;
uint16_t addrlen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_sendto_s
{
struct usrsock_request_common_s head;

int16_t usockid;
uint16_t addrlen;
uint16_t buflen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_recvfrom_s
{
struct usrsock_request_common_s head;

int16_t usockid;
uint16_t max_buflen;
uint16_t max_addrlen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_setsockopt_s
{
struct usrsock_request_common_s head;

int16_t usockid;
int16_t level;
int16_t option;
uint16_t valuelen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_getsockopt_s
{
struct usrsock_request_common_s head;

int16_t usockid;
int16_t level;
int16_t option;
uint16_t max_valuelen;
} end_packed_struct;

begin_packed_struct struct usrsock_request_getsockname_s
{
struct usrsock_request_common_s head;

int16_t usockid;
uint16_t max_addrlen;
} end_packed_struct;

/* Response/event message structures (kernel <= /dev/usrsock <= daemon) */

begin_packed_struct struct usrsock_message_common_s
{
int8_t msgid;
int8_t flags;
} end_packed_struct;

/* Request acknowledgment/completion message */

begin_packed_struct struct usrsock_message_req_ack_s
{
struct usrsock_message_common_s head;

uint8_t xid;
int32_t result;
} end_packed_struct;

/* Request acknowledgment/completion message */

begin_packed_struct struct usrsock_message_datareq_ack_s
{
struct usrsock_message_req_ack_s reqack;

/* head.result => positive buflen, negative error-code. */

uint16_t valuelen; /* length of value returned after buffer */
uint16_t valuelen_nontrunc; /* actual non-truncated length of value at
* daemon-sïde. */
} end_packed_struct;

/* Socket event message */

begin_packed_struct struct usrsock_message_socket_event_s
{
struct usrsock_message_common_s head;

int16_t usockid;
uint16_t events;
} end_packed_struct;

#endif /* __INCLUDE_NUTTX_NET_USRSOCK_H */
18 changes: 18 additions & 0 deletions net/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ config TUN_LPWORK
endchoice # Work queue
endif # NET_TUN

config NET_USRSOCK
bool "User-space networking stack API"
default n
---help---
Enable or disable user-space networking stack support.

User-space networking stack API allows user-space daemon to
provide TCP/IP stack implementation for NuttX network.

Main use for this is to allow use and integration of
HW-provided TCP/IP stacks for NuttX.

For example, user-space daemon can translate /dev/usrsock API
requests to HW TCP/IP API requests while rest of the user-space
can access standard socket API, with socket descriptors that
can be used with NuttX system calls.

endmenu # Data link support

source "net/netdev/Kconfig"
Expand Down Expand Up @@ -271,6 +288,7 @@ source "net/arp/Kconfig"
source "net/loopback/Kconfig"
source "net/iob/Kconfig"
source "net/procfs/Kconfig"
source "net/usrsock/Kconfig"
source "net/utils/Kconfig"

config NET_STATISTICS
Expand Down
1 change: 1 addition & 0 deletions net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ include devif/Make.defs
include loopback/Make.defs
include route/Make.defs
include procfs/Make.defs
include usrsock/Make.defs
include utils/Make.defs
endif

Expand Down
34 changes: 19 additions & 15 deletions net/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ Directory Structure
+- route - Routing table support
+- tcp - Transmission Control Protocol
+- udp - User Datagram Protocol
+- usrsock - User socket API for user-space networking stack
`- utils - Miscellaneous utility functions


+----------------------------------------------------------------+
| Application layer |
+----------------------------------------------------------------+
+----------------------------------------------------------------+
| Socket layer (socket/) |
+----------------------------------------------------------------+
+------------++--------------------------------------------------+
| Network || Protocol stacks (arp, ipv6, icmp, pkt, tcp, udp) |
| Device |+--------------------------------------------------+
| Interface |+------------------------------------++------------+
| (netdev/) || Network Device Interface (devif/) || Utilities |
+------------++------------------------------------++------------+
+----------------------------------------------------------------+
| Network Device Drivers |
+----------------------------------------------------------------+
+-------------------------------------------------------------------++------------------------+
| Application layer || usrsock daemon |
+-------------------------------------------------------------------++------------------------+
+-------------------------------------------------------------------++----------------+ +-----+
| Socket layer (socket/) || /dev/usrsock | | |
+-------------------------------------------------------------------++----------------+ | |
+------------++--------------------------------------------------++-------------------+ | |
| Network || Protocol stacks (arp, ipv6, icmp, pkt, tcp, udp) || usrsock/ | | |
| Device |+--------------------------------------------------++-------------------+ | |
| Interface |+------------------------------------++---------------------------------+ | |
| (netdev/) || Network Device Interface (devif/) || Utilities | | |
+------------++------------------------------------++---------------------------------+ | |
+----------------------------------------------------------------+ | |
| Network Device Drivers | | HAL |
+----------------------------------------------------------------+ +-----+
+----------------------------------------------------------------+ +--------------------------+
| Networking Hardware | | Hardware TCP/IP Stack |
+----------------------------------------------------------------+ +--------------------------+
9 changes: 7 additions & 2 deletions net/devif/devif.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,17 @@
*/

struct net_driver_s; /* Forward reference */

typedef CODE uint16_t (*devif_callback_event_t)(FAR struct net_driver_s *dev,
FAR void *pvconn,
FAR void *pvpriv,
uint16_t flags);

struct devif_callback_s
{
FAR struct devif_callback_s *nxtconn;
FAR struct devif_callback_s *nxtdev;
uint16_t (*event)(FAR struct net_driver_s *dev, FAR void *pvconn,
FAR void *pvpriv, uint16_t flags);
FAR devif_callback_event_t event;
FAR void *priv;
uint16_t flags;
};
Expand Down
Loading

0 comments on commit cd3c963

Please sign in to comment.