net: Network initialization and tx/rx functionality

Functions to receive data from apps and transmit it to network, and
receive from network and pass data to apps.

Change-Id: I1b1b8c041e6c5e20294081d2cd403636e9909cdc
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2015-04-22 09:43:18 +03:00 committed by Anas Nashif
commit 4f5723a5d0
5 changed files with 549 additions and 1 deletions

159
include/net/net_buf.h Normal file
View file

@ -0,0 +1,159 @@
/*! @file
@brief Network buffer API
Network data is passed between application and IP stack via
a net_buf struct.
*/
/*
* 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.
*/
/* Data buffer API - used for all data to/from net */
#ifndef __NET_BUF_H
#define __NET_BUF_H
#include <stdint.h>
#include <net/core/ip/uip.h>
struct net_context;
/*! The default MTU is 1280 (minimum IPv6 packet size) + LL header
*/
#define NET_BUF_MAX_DATA (UIP_LINK_MTU + UIP_LLH_LEN)
struct net_buf {
/*! @cond ignore */
/* FIFO uses first 4 bytes itself, reserve space */
int __unused;
/* @endcond */
/*! Network connection context */
struct net_context *context;
/*! Buffer data length */
uint16_t len;
/*! Buffer head pointer */
uint8_t *data;
/*! Actual network buffer storage */
uint8_t buf[NET_BUF_MAX_DATA];
};
/*!
* @brief Get buffer from the available buffers pool.
*
* @details Get network buffer from buffer pool. You must have
* network context before able to use this function.
*
* @param context Network context that will be related to
* this buffer.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_buf_get(struct net_context *context);
/*!
* @brief Get buffer from pool but also reserve headroom for
* potential headers.
*
* @details Normally this version is not useful for applications
* but is mainly used by network fragmentation code.
*
* @param reserve How many bytes to reserve for headroom.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_buf_get_reserve(uint16_t reserve_head);
/*!
* @brief Place buffer back into the available buffers pool.
*
* @details Releases the buffer to other use. This needs to be
* called by application after it has finished with
* the buffer.
*
* @param buf Network buffer to release.
*
*/
void net_buf_put(struct net_buf *buf);
/*!
* @brief Prepare data to be added at the end of the buffer.
*
* @details Move the tail pointer forward.
*
* @param buf Network buffer.
* @param len Size of data to be added.
*
* @return Pointer to new tail.
*/
uint8_t *net_buf_add(struct net_buf *buf, uint16_t len);
/*!
* @brief Push data to the beginning of the buffer.
*
* @details Move the data pointer backwards.
*
* @param buf Network buffer.
* @param len Size of data to be added.
*
* @return Pointer to new head.
*/
uint8_t *net_buf_push(struct net_buf *buf, uint16_t len);
/*!
* @brief Remove data from the beginning of the buffer.
*
* @details Move the data pointer forward.
*
* @param buf Network buffer.
* @param len Size of data to be removed.
*
* @return Pointer to new head.
*/
uint8_t *net_buf_pull(struct net_buf *buf, uint16_t len);
/*! @def net_buf_tail
*
* @brief Return pointer to the end of the data in the buffer.
*
* @details This macro returns the tail of the buffer.
*
* @param buf Network buffer.
*
* @return Pointer to tail.
*/
#define net_buf_tail(buf) ((buf)->data + (buf)->len)
/*! @cond ignore */
void net_buf_init(void);
/* @endcond */
#endif /* __NET_BUF_H */

95
include/net/net_core.h Normal file
View file

@ -0,0 +1,95 @@
/*! @file
@brief Network core definitions
Definitions for networking support.
*/
/*
* 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 <misc/printk.h>
#include <string.h>
#include <net/net_buf.h>
#ifndef __NET_CORE_H
#define __NET_CORE_H
/* Network subsystem logging helpers */
#define NET_DBG(fmt, ...) printk("net: %s: " fmt, __func__, ##__VA_ARGS__)
#define NET_ERR(fmt, ...) printk("net: %s: " fmt, __func__, ##__VA_ARGS__)
#define NET_INFO(fmt, ...) printk("net: " fmt, ##__VA_ARGS__)
#define NET_PRINT(fmt, ...) printk(fmt, ##__VA_ARGS__)
/*!
* @brief Initialize network stack. This is will be automatically called
* by the OS.
*
* @details No harm will happen if application calls this multiple times.
*
* @return 0 if ok, < 0 in case of error.
*/
int net_init(void);
struct net_driver {
/*! How much headroom is needed for net transport headers */
size_t head_reserve;
/*! Open the net transport */
int (*open) (void);
/*! Send data to net */
int (*send) (struct net_buf *buf);
};
/*!
* @brief Register a new network driver to the network stack.
*
* @details Only one network device can be registered at a time.
*
* @param buf Network driver.
*
* @return 0 if ok, < 0 in case of error.
*/
int net_register_driver(struct net_driver *drv);
/*!
* @brief Unregister a previously registered network driver.
*
* @param buf Network driver.
*
* @details Networking will be disabled if there is no driver
* available.
*
*/
void net_unregister_driver(struct net_driver *drv);
#endif /* __NET_CORE_H */

View file

@ -1,5 +1,11 @@
UIP_MODULES = net/ip/contiki/ip \
net/ip/contiki/ipv6
net/ip/contiki/ipv6 \
net/ip/contiki/mac \
net/ip/contiki/mac/sicslowmac \
net/ip/contiki/llsec \
net/ip/contiki/os/dev \
net/ip/contiki/os/sys \
net/ip/contiki/os/lib
EXTRA_CFLAGS += ${PROJECTINCLUDE}
EXTRA_CFLAGS += -I${srctree}/net/ip/contiki
@ -17,5 +23,14 @@ MODULES_SOURCES = ${foreach d, $(UIP_MODULES), ${wildcard $(TIMO_BASE)/$(d)/*.c}
MODULES_FILES = ${foreach f, $(MODULES_SOURCES), ${subst $(TIMO_BASE)/net/ip/,,${f}}}
obj-y = ${foreach f, $(MODULES_FILES), ${subst .c,.o,${f}}}
obj-y += net_init.o \
net_buf.o \
contiki/netstack.o \
contiki/packetbuf.o \
contiki/uip-driver.o \
contiki/nbr-table.o \
contiki/linkaddr.o \
contiki/queuebuf.o
# workaround for include file location
CREATE_INCLUDE_LINK_net := $(shell ln -s ${TIMO_BASE}/net/ip/contiki ${TIMO_BASE}/net/ip/contiki/net > /dev/null 2>&1)

108
net/ip/net_buf.c Normal file
View file

@ -0,0 +1,108 @@
/*! @file
@brief Network buffers
Network data is passed between application and IP stack via
a net_buf struct.
*/
/*
* 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 <toolchain.h>
#include <string.h>
#include <stdint.h>
#include <net/net_core.h>
#include <net/net_buf.h>
/* Available (free) buffers queue */
#define NUM_BUFS 2
static struct net_buf buffers[NUM_BUFS];
static struct nano_fifo free_bufs;
struct net_buf *net_buf_get_reserve(uint16_t reserve_head)
{
struct net_buf *buf;
buf = nano_fifo_get(&free_bufs);
if (!buf) {
NET_ERR("Failed to get free buffer\n");
return NULL;
}
buf->data = buf->buf + reserve_head;
buf->len = 0;
NET_DBG("buf %p reserve %u\n", buf, reserve_head);
return buf;
}
struct net_buf *net_buf_get(struct net_context *context)
{
return net_buf_get_reserve(0);
}
void net_buf_put(struct net_buf *buf)
{
NET_DBG("buf %p\n", buf);
nano_fifo_put(&free_bufs, buf);
}
uint8_t *net_buf_add(struct net_buf *buf, uint16_t len)
{
uint8_t *tail = buf->data + buf->len;
buf->len += len;
return tail;
}
uint8_t *net_buf_push(struct net_buf *buf, uint16_t len)
{
buf->data -= len;
buf->len += len;
return buf->data;
}
uint8_t *net_buf_pull(struct net_buf *buf, uint16_t len)
{
buf->len -= len;
return buf->data += len;
}
void net_buf_init(void)
{
nano_fifo_init(&free_bufs);
for (int i = 0; i < NUM_BUFS; i++) {
nano_fifo_put(&free_bufs, &buffers[i]);
}
}

171
net/ip/net_init.c Normal file
View file

@ -0,0 +1,171 @@
/*! @file
@brief Network initialization
Initialize the network IP stack. Create two fibers, one for reading data
from applications (Tx fiber) and one for reading data from IP stack
and passing that data to applications (Rx fiber).
*/
/*
* 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.
*/
#define DEBUG DEBUG_PRINT
#include "contiki/ip/uip-debug.h"
#include <nanokernel.h>
#include <toolchain.h>
#include <string.h>
#include <errno.h>
#include <net/net_core.h>
#include <net/net_buf.h>
#include "net/core/netstack.h"
#include "net/core/uip-driver.h"
/* Stacks for the tx & rx fibers.
* FIXME: stack size needs fine-tuning
*/
#define STACKSIZE 2048
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;
/* Queue for outgoing packets from apps */
struct nano_fifo tx_queue;
/* Registered network driver, FIXME: how this is set? */
struct net_driver *drv;
} netdev;
static void net_tx_fiber(void)
{
struct net_driver *drv = netdev.drv;
NET_DBG("\n");
while (1) {
struct net_buf *buf;
/* Get next packet from application - wait if necessary */
buf = nano_fifo_get_wait(&netdev.tx_queue);
NET_DBG("Sending (buf %p, len %u) to stack\n", buf, buf->len);
//drv->send(buf); // This is for further studies
}
}
static void net_rx_fiber(void)
{
struct net_buf *buf;
NET_DBG("\n");
while (1) {
/* Wait next packet from network, pass it to IP stack */
buf = nano_fifo_get_wait(&netdev.rx_queue);
net_buf_put(buf);
}
}
static void init_rx_queue(void)
{
nano_fifo_init(&netdev.rx_queue);
fiber_start(rx_fiber_stack, STACKSIZE,
(nano_fiber_entry_t) net_rx_fiber, 0, 0, 7, 0);
}
static void init_tx_queue(void)
{
nano_fifo_init(&netdev.tx_queue);
fiber_start(tx_fiber_stack, STACKSIZE,
(nano_fiber_entry_t) net_tx_fiber, 0, 0, 7, 0);
}
static int network_initialization(void)
{
netstack_init();
return 0;
}
int net_register_driver(struct net_driver *drv)
{
if (netdev.drv) {
return -EALREADY;
}
if (!drv->open || !drv->send) {
return -EINVAL;
}
netdev.drv = drv;
return 0;
}
void net_unregister_driver(struct net_driver *drv)
{
netdev.drv = NULL;
}
int net_init(void)
{
struct net_driver *drv = netdev.drv;
int err;
if (!drv) {
NET_DBG("No network driver defined\n");
return -ENODEV;
}
net_buf_init();
init_tx_queue();
init_rx_queue();
err = drv->open();
if (err) {
return err;
}
return network_initialization();
}