net: Simple socket API introduced

The network context defines a network connection.

Change-Id: I29a186be6c9de9d824f10b6442fa1dfd3711d24d
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2015-04-23 11:20:42 +03:00 committed by Anas Nashif
commit da36c6371c
7 changed files with 383 additions and 6 deletions

View file

@ -92,4 +92,6 @@ int net_register_driver(struct net_driver *drv);
*/
void net_unregister_driver(struct net_driver *drv);
void net_context_init(void);
#endif /* __NET_CORE_H */

109
include/net/net_ip.h Normal file
View file

@ -0,0 +1,109 @@
/*! @file
@brief IPv6 and IPv4 definitions
Generic IPv6 and IPv4 address definitions.
*/
/*
* Copyright (c) 2015 Intel Corporation
*
* 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 of Intel Corporation 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 HOLDER 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.
*/
#include <stdint.h>
#ifndef __NET_IP_H
#define __NET_IP_H
/*! Protocol families */
#define PF_UNSPEC 0 /* Unspecified. */
#define PF_INET 2 /* IP protocol family. */
#define PF_INET6 10 /* IP version 6. */
/*! Address families. */
#define AF_UNSPEC PF_UNSPEC
#define AF_INET PF_INET
#define AF_INET6 PF_INET6
/*! Protocol numbers from IANA */
enum ip_protocol {
IPPROTO_TCP = 6,
IPPROTO_UDP = 17,
IPPROTO_ICMPV6 = 58,
};
/*! IPv6 address structure */
struct in6_addr {
union {
uint8_t u6_addr8[16];
uint16_t u6_addr16[8]; /* In big endian */
uint32_t u6_addr32[4]; /* In big endian */
} in6_u;
#define s6_addr in6_u.u6_addr8
#define s6_addr16 in6_u.u6_addr16
#define s6_addr32 in6_u.u6_addr32
};
/*! IPv4 address */
struct in_addr {
union {
uint8_t u4_addr8[4];
uint16_t u4_addr16[2]; /* In big endian */
uint32_t u4_addr32[1]; /* In big endian */
} in4_u;
#define s4_addr in4_u.u4_addr8
#define s4_addr16 in4_u.u4_addr16
#define s4_addr32 in4_u.u4_addr32
#define s_addr s4_addr32
};
typedef unsigned short int sa_family_t;
struct net_addr {
sa_family_t family;
union {
struct in6_addr in6_addr;
struct in_addr in_addr;
} addr;
};
/*! IPv6/IPv4 network connection tuple */
struct net_tuple {
/*! IPv6/IPv4 remote address */
struct net_addr *remote_addr;
/*! IPv6/IPv4 local address */
struct net_addr *local_addr;
/*! UDP/TCP remote port */
uint16_t remote_port;
/*! UDP/TCP local port */
uint16_t local_port;
/*! IP protocol */
enum ip_protocol ip_proto;
};
#endif /* __NET_IP_H */

118
include/net/net_socket.h Normal file
View file

@ -0,0 +1,118 @@
/*! @file
@brief Simple socket API
Simple socket API for applications to connection establishment and
disconnection.
*/
/*
* Copyright (c) 2015 Intel Corporation
*
* 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 of Intel Corporation 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 HOLDER 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.
*/
#include <stdint.h>
#include <net/net_ip.h>
#include <net/net_buf.h>
#ifndef __NET_SOCKET_H
#define __NET_SOCKET_H
/*!
* @brief Get network context.
*
* @details Network context is used to define the connection
* 5-tuple (protocol, remote address, remote port, source
* address and source port).
*
* @param protocol Protocol to use. Currently only UDP is supported.
* @param remote_addr Remote IPv6/IPv4 address.
* @param remote_port Remote UDP/TCP port.
* @param local_addr Local IPv6/IPv4 address. If the local addres is NULL
* or set to be anyaddr (all zeros), the IP stack will use the link
* local address defined for the system.
* @param local_port Local UDP/TCP port. If the local port is 0,
* then a random port will be allocated.
*
* @return Network context if successful, NULL otherwise.
*/
struct net_context *net_context_get(enum ip_protocol ip_proto,
const struct net_addr *remote_addr,
uint16_t remote_port,
const struct net_addr *local_addr,
uint16_t local_port);
/*!
* @brief Get network tuple.
*
* @details This function returns the used connection tuple.
*
* @param context Network context.
*
* @return Network tuple if successful, NULL otherwise.
*/
struct net_tuple *net_context_get_tuple(struct net_context *context);
/*!
* @brief Release network context.
*
* @details Free the resources allocated for the context.
* All network listeners tied to this context are removed.
*
* @param context Network context.
*
*/
void net_context_put(struct net_context *context);
/*!
* @brief Send data to network.
*
* @details Send user specified data to network. This
* requires that net_buf is tied to context. This means
* that the net_buf was allocated using net_buf_get().
*
* @param buf Network buffer.
*
* @return 0 if ok, <0 if error.
*/
int net_send(struct net_buf *buf);
/*!
* @brief Receive data from network.
*
* @details Application uses this to get data from network
* connection. This function will not wait so if there is
* no data to return, then NULL is returned. Caller is
* responsible to release the returned net_buf.
*
* @param context Network context.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_receive(struct net_context *context);
#endif /* __NET_SOCKET_H */

View file

@ -25,6 +25,7 @@ obj-y = ${foreach f, $(MODULES_FILES), ${subst .c,.o,${f}}}
obj-y += net_init.o \
net_buf.o \
net_context.o \
contiki/netstack.o \
contiki/packetbuf.o \
contiki/uip-driver.o \

View file

@ -68,7 +68,15 @@ struct net_buf *net_buf_get_reserve(uint16_t reserve_head)
struct net_buf *net_buf_get(struct net_context *context)
{
return net_buf_get_reserve(0);
struct net_buf *buf = net_buf_get_reserve(0);
if (!buf) {
return buf;
}
buf->context = context;
return buf;
}
void net_buf_put(struct net_buf *buf)

143
net/ip/net_context.c Normal file
View file

@ -0,0 +1,143 @@
/*! @file
@brief Network context API
An API for applications to define a network connection.
*/
/*
* Copyright (c) 2015 Intel Corporation
*
* 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 of Intel Corporation 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 HOLDER 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.
*/
#include <nanokernel.h>
#include <string.h>
#include <errno.h>
#include <net/net_ip.h>
#include <net/net_socket.h>
struct net_context {
/* Connection tuple identifies the connection */
struct net_tuple tuple;
/* Application receives data via this fifo */
struct nano_fifo rx_queue;
};
/* Override this in makefile if needed */
#ifndef NET_MAX_CONTEXT
#define NET_MAX_CONTEXT 5
#endif
static struct net_context contexts[NET_MAX_CONTEXT];
static struct nano_sem contexts_lock;
static void context_sem_give(struct nano_sem *chan)
{
switch (context_type_get()) {
case NANO_CTX_FIBER:
nano_fiber_sem_give(chan);
break;
case NANO_CTX_TASK:
nano_task_sem_give(chan);
break;
case NANO_CTX_ISR:
default:
/* Invalid context type */
break;
}
}
struct net_context *net_context_get(enum ip_protocol ip_proto,
const struct net_addr *remote_addr,
uint16_t remote_port,
const struct net_addr *local_addr,
uint16_t local_port)
{
int i;
struct net_context *context = NULL;
nano_sem_take_wait(&contexts_lock);
for (i = 0; i < NET_MAX_CONTEXT; i++) {
if (!contexts[i].tuple.remote_port) {
contexts[i].tuple.ip_proto = ip_proto;
contexts[i].tuple.remote_addr = (struct net_addr *)remote_addr;
contexts[i].tuple.remote_port = remote_port;
contexts[i].tuple.local_addr = (struct net_addr *)local_addr;
contexts[i].tuple.local_port = local_port;
context = &contexts[i];
break;
}
}
context_sem_give(&contexts_lock);
return context;
}
void net_context_put(struct net_context *context)
{
int i;
nano_sem_take_wait(&contexts_lock);
for (i = 0; i < NET_MAX_CONTEXT; i++) {
if (contexts[i].tuple.remote_port) {
memset(&contexts[i].tuple, 0,
sizeof(contexts[i].tuple));
break;
}
}
context_sem_give(&contexts_lock);
}
struct net_tuple *net_context_get_tuple(struct net_context *context)
{
if (!context) {
return NULL;
}
return &context->tuple;
}
void net_context_init(void)
{
int i;
nano_sem_init(&contexts_lock);
memset(contexts, 0, sizeof(contexts));
for (i = 0; i < NET_MAX_CONTEXT; i++) {
nano_fifo_init(&contexts[i].rx_queue);
}
context_sem_give(&contexts_lock);
}

View file

@ -57,11 +57,6 @@
static char rx_fiber_stack[STACKSIZE];
static char tx_fiber_stack[STACKSIZE];
struct net_context {
/* Application receives data via this fifo */
struct nano_fifo rx_queue;
};
static struct net_dev {
/* Queue for incoming packets from driver */
struct nano_fifo rx_queue;
@ -158,6 +153,7 @@ int net_init(void)
return -ENODEV;
}
net_context_init();
net_buf_init();
init_tx_queue();
init_rx_queue();