summaryrefslogtreecommitdiff
path: root/nuttx/net/iob
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-06-03 12:41:34 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-06-03 12:41:34 -0600
commit6d7446a054fdfa9bed57d003e085175a5ed89b92 (patch)
treed703850da16c7635da749542ff387681026b0e2f /nuttx/net/iob
parente8f16d76fb1dc7ec47dde9558a59472223f68547 (diff)
downloadnuttx-6d7446a054fdfa9bed57d003e085175a5ed89b92.tar.gz
nuttx-6d7446a054fdfa9bed57d003e085175a5ed89b92.tar.bz2
nuttx-6d7446a054fdfa9bed57d003e085175a5ed89b92.zip
NET: Add generic I/O buffering logic
Diffstat (limited to 'nuttx/net/iob')
-rw-r--r--nuttx/net/iob/Kconfig31
-rw-r--r--nuttx/net/iob/Make.defs52
-rw-r--r--nuttx/net/iob/iob.h78
-rw-r--r--nuttx/net/iob/iob_alloc.c79
-rw-r--r--nuttx/net/iob/iob_concat.c158
-rw-r--r--nuttx/net/iob/iob_copyout.c129
-rw-r--r--nuttx/net/iob/iob_free.c83
-rw-r--r--nuttx/net/iob/iob_freeq.c96
-rw-r--r--nuttx/net/iob/iob_initialize.c103
-rw-r--r--nuttx/net/iob/iob_trimhead.c113
-rw-r--r--nuttx/net/iob/iob_trimtail.c151
11 files changed, 1073 insertions, 0 deletions
diff --git a/nuttx/net/iob/Kconfig b/nuttx/net/iob/Kconfig
new file mode 100644
index 000000000..0a5c8522a
--- /dev/null
+++ b/nuttx/net/iob/Kconfig
@@ -0,0 +1,31 @@
+#
+# For a description of the syntax of this configuration file,
+# see misc/tools/kconfig-language.txt.
+#
+
+config NET_IOB
+ bool "Network I/O buffer support"
+ default n
+ ---help---
+ This setting will build the networking I/O buffer (IOB) support
+ library.
+
+if NET_IOB
+
+config IOB_NBUFFERS
+ int "Number of pre-allocated network I/O buffers"
+ default 16
+ ---help---
+ Each packet is represented by a series of small I/O buffers in a
+ chain. This setting determines the number of preallocated I/O
+ buffers available for packet data.
+
+config IOB_BUFSIZE
+ int "Payload size of one network I/O buffer"
+ default 256
+ ---help---
+ Each packet is represented by a series of small I/O buffers in a
+ chain. This setting determines the data payload each preallocated
+ I/O buffer.
+
+endif # NET_IOB
diff --git a/nuttx/net/iob/Make.defs b/nuttx/net/iob/Make.defs
new file mode 100644
index 000000000..6783e597b
--- /dev/null
+++ b/nuttx/net/iob/Make.defs
@@ -0,0 +1,52 @@
+############################################################################
+# net/iob/Make.defs
+#
+# Copyright (C) 2014 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# 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.
+#
+############################################################################
+
+NET_ASRCS =
+NET_CSRCS =
+
+ifeq ($(CONFIG_NET_IOB),y)
+
+# Include IOB src files
+
+NET_CSRCS += iob_alloc.c iob_concat.c iob_copyout.c iob_free.c iob_freeq.c
+NET_CSRCS += iob_initialize.c iob_trimhead.c iob_trimtail.c
+
+# Include iob build support
+
+DEPPATH += --dep-path iob
+VPATH += :iob
+CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)net$(DELIM)iob}
+
+endif
diff --git a/nuttx/net/iob/iob.h b/nuttx/net/iob/iob.h
new file mode 100644
index 000000000..1a6fe540c
--- /dev/null
+++ b/nuttx/net/iob/iob.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+ * net/iob/iob.h
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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 __NET_IOB_IOB_H
+#define __NET_IOB_IOB_H 1
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* This is a pool of pre-allocated I/O buffers */
+
+extern struct iob_s g_iob_pool[CONFIG_IOB_NBUFFERS];
+
+/* A list of all free, unallocated I/O buffers */
+
+extern sq_queue_t g_iob_freelist;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+
+#endif /* __NET_IOB_IOB_H */
diff --git a/nuttx/net/iob/iob_alloc.c b/nuttx/net/iob/iob_alloc.c
new file mode 100644
index 000000000..c2c060d84
--- /dev/null
+++ b/nuttx/net/iob/iob_alloc.c
@@ -0,0 +1,79 @@
+/****************************************************************************
+ * net/iob/iob_alloc.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_alloc
+ *
+ * Description:
+ * Allocate an I/O buffer by take the buffer at the head of the free list.
+ *
+ ****************************************************************************/
+
+FAR struct iob_s *iob_alloc(void)
+{
+ return (FAR struct iob_s *)sq_remfirst(&g_iob_freelist);
+}
diff --git a/nuttx/net/iob/iob_concat.c b/nuttx/net/iob/iob_concat.c
new file mode 100644
index 000000000..d7b03e1fc
--- /dev/null
+++ b/nuttx/net/iob/iob_concat.c
@@ -0,0 +1,158 @@
+/****************************************************************************
+ * net/iob/iob_concat.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <string.h>
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_concat
+ *
+ * Description:
+ * Concatenate iob_s chain iob2 to iob1.
+ *
+ ****************************************************************************/
+
+void iob_concat(FAR struct iob_s *iob1, FAR struct iob_s *iob2)
+{
+ unsigned int offset2;
+ unsigned int ncopy;
+ unsigned int navail;
+
+ /* Find the last buffer in the iob1 buffer chain */
+
+ while (iob1->io_link.flink)
+ {
+ iob1 = (FAR struct iob_s *)iob1->io_link.flink;
+ }
+
+ /* Then add data to the end of iob1 */
+
+ offset2 = 0;
+ while (iob2)
+ {
+ /* Is the iob1 tail buffer full? */
+
+ if (iob1->io_len >= CONFIG_IOB_BUFSIZE)
+ {
+ /* Yes.. Just connect the chains */
+
+ iob1->io_link.flink = iob2->io_link.flink;
+
+ /* Has the data offset in iob2? */
+
+ if (offset2 > 0)
+ {
+ /* Yes, move the data down and adjust the size */
+
+ iob2->io_len -= offset2;
+ memcpy(iob2->io_data, &iob2->io_data[offset2], iob2->io_len);
+
+ /* Set up to continue packing, but now into iob2 */
+
+ iob1 = iob2;
+ iob2 = (FAR struct iob_s *)iob2->io_link.flink;
+
+ iob1->io_link.flink = NULL;
+ offset2 = 0;
+ }
+ else
+ {
+ /* Otherwise, we are done */
+
+ return;
+ }
+ }
+
+ /* How many bytes can we copy from the source (iob2) */
+
+ ncopy = iob2->io_len - offset2;
+
+ /* Limit the size of the copy to the amount of free space in iob1 */
+
+ navail = CONFIG_IOB_BUFSIZE - iob1->io_len;
+ if (ncopy > navail)
+ {
+ ncopy = navail;
+ }
+
+ /* Copy the data from iob2 into iob1 */
+
+ memcpy(iob1->io_data + iob1->io_len, iob2->io_data, ncopy);
+ iob1->io_len += ncopy;
+ offset2 += ncopy;
+
+ /* Have we consumed all of the data in the iob2 entry? */
+
+ if (offset2 >= iob2->io_len)
+ {
+ /* Yes.. free the iob2 entry and start processing the next I/O
+ * buffer in the iob2 chain.
+ */
+
+ iob2 = iob_free(iob2);
+ offset2 = 0;
+ }
+ }
+}
diff --git a/nuttx/net/iob/iob_copyout.c b/nuttx/net/iob/iob_copyout.c
new file mode 100644
index 000000000..5997c7f97
--- /dev/null
+++ b/nuttx/net/iob/iob_copyout.c
@@ -0,0 +1,129 @@
+/****************************************************************************
+ * net/iob/iob_copyout.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef MIN
+# define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_copyout
+ *
+ * Description:
+ * Copy data 'len' bytes of data into the user buffer starting at 'offset'
+ * in the I/O buffer.
+ *
+ ****************************************************************************/
+
+void iob_copyout(FAR uint8_t *dest, FAR const struct iob_s *iob,
+ unsigned int len, unsigned int offset)
+{
+ FAR const uint8_t *src;
+ unsigned int ncopy;
+ unsigned int avail;
+
+ /* Skip to the I/O buffer containing the offset */
+
+ while (offset >= iob->io_len)
+ {
+ offset -= iob->io_len;
+ iob = (FAR struct iob_s *)iob->io_link.flink;
+ }
+
+ /* Then loop until all of the I/O data is copied to the user buffer */
+
+ while (len > 0)
+ {
+ ASSERT(iob);
+
+ /* Get the source I/O buffer offset address and the amount of data
+ * available from that address.
+ */
+
+ src = &iob->io_data[offset];
+ avail = iob->io_len - offset;
+
+ /* Copy the whole I/O buffer in to the user buffer */
+
+ ncopy = MIN(avail, len);
+ memcpy(dest, src, ncopy);
+
+ /* Adjust the total length of the copy and the destination address in
+ * the user buffer.
+ */
+
+ len -= ncopy;
+ dest += ncopy;
+
+ /* Skip to the next I/O buffer in the chain */
+
+ iob = (FAR struct iob_s *)iob->io_link.flink;
+ offset = 0;
+ }
+}
diff --git a/nuttx/net/iob/iob_free.c b/nuttx/net/iob/iob_free.c
new file mode 100644
index 000000000..273f4b5a0
--- /dev/null
+++ b/nuttx/net/iob/iob_free.c
@@ -0,0 +1,83 @@
+/****************************************************************************
+ * net/iob/iob_free.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_free
+ *
+ * Description:
+ * Free the I/O buffer at the head of a buffer chain returning it to the
+ * free list. The link to the next I/O buffer in the chain is return.
+ *
+ ****************************************************************************/
+
+FAR struct iob_s *iob_free(FAR struct iob_s *iob)
+{
+ sq_entry_t *next = iob->io_link.flink;
+
+ sq_addlast(&iob->io_link, &g_iob_freelist);
+ return (FAR struct iob_s *)next;
+}
diff --git a/nuttx/net/iob/iob_freeq.c b/nuttx/net/iob/iob_freeq.c
new file mode 100644
index 000000000..fa9cce8ab
--- /dev/null
+++ b/nuttx/net/iob/iob_freeq.c
@@ -0,0 +1,96 @@
+/****************************************************************************
+ * net/iob/iob_freeq.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_freeq
+ *
+ * Description:
+ * Free an entire buffer chain
+ *
+ ****************************************************************************/
+
+void iob_freeq(FAR sq_queue_t *q)
+{
+ /* If the free list is empty, then just move the entry queue to the the
+ * free list. Otherwise, append the list to the end of the free list.
+ */
+
+ if (g_iob_freelist.tail)
+ {
+ g_iob_freelist.tail->flink = q->head;
+ }
+ else
+ {
+ g_iob_freelist.head = q->head;
+ }
+
+ /* In either case, the tail of the queue is the tail of queue becomes the
+ * tail of the free list.
+ */
+
+ g_iob_freelist.tail = q->tail;
+}
diff --git a/nuttx/net/iob/iob_initialize.c b/nuttx/net/iob/iob_initialize.c
new file mode 100644
index 000000000..c5f72c397
--- /dev/null
+++ b/nuttx/net/iob/iob_initialize.c
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * net/iob/iob_initialize.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* This is a pool of pre-allocated I/O buffers */
+
+struct iob_s g_iob_pool[CONFIG_IOB_NBUFFERS];
+
+/* A list of all free, unallocated I/O buffers */
+
+sq_queue_t g_iob_freelist;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_initialize
+ *
+ * Description:
+ * Set up the I/O buffers for normal operations.
+ *
+ ****************************************************************************/
+
+void iob_initialize(void)
+{
+ static bool initialized = false;
+ int i;
+
+ /* Perform one-time initialization */
+
+ if (!initialized)
+ {
+ /* Add each I/O buffer to the free list */
+
+ for (i = 0; i < CONFIG_IOB_NBUFFERS; i++)
+ {
+ sq_addlast(&g_iob_pool[i].io_link, &g_iob_freelist);
+ }
+
+ initialized = true;
+ }
+}
diff --git a/nuttx/net/iob/iob_trimhead.c b/nuttx/net/iob/iob_trimhead.c
new file mode 100644
index 000000000..ef6e1c2f6
--- /dev/null
+++ b/nuttx/net/iob/iob_trimhead.c
@@ -0,0 +1,113 @@
+/****************************************************************************
+ * net/iob/iob_trimhead.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <string.h>
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_trimhead
+ *
+ * Description:
+ * Remove bytes from the beginning of an I/O chain
+ *
+ ****************************************************************************/
+
+void iob_trimhead(FAR struct iob_s *iob, unsigned int trimlen)
+{
+ FAR struct iob_s *entry;
+ unsigned int len;
+
+ if (iob && trimlen > 0)
+ {
+ entry = iob;
+ len = trimlen;
+
+ /* Trim from the head of the I/IO buffer chain */
+
+ while (entry != NULL && len > 0)
+ {
+ /* Do we trim this entire I/O buffer away? */
+
+ if (entry->io_len <= len)
+ {
+ /* Yes.. just set is length to zero and skip to the next */
+
+ len -= entry->io_len;
+ entry->io_len = 0;
+ entry = (FAR struct iob_s *)entry->io_link.flink;
+ }
+ else
+ {
+ /* No, then just take what we need from this I/O buffer and
+ * stop the trim.
+ */
+
+ entry->io_len -= len;
+ memcpy(entry->io_data, &entry->io_data[len], entry->io_len);
+ len = 0;
+ }
+ }
+ }
+}
diff --git a/nuttx/net/iob/iob_trimtail.c b/nuttx/net/iob/iob_trimtail.c
new file mode 100644
index 000000000..030042be9
--- /dev/null
+++ b/nuttx/net/iob/iob_trimtail.c
@@ -0,0 +1,151 @@
+/****************************************************************************
+ * net/iob/iob_trimtail.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <string.h>
+#include <queue.h>
+
+#include <nuttx/net/iob.h>
+
+#include "iob.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: iob_trimtail
+ *
+ * Description:
+ * Remove bytes from the end of an I/O chain
+ *
+ ****************************************************************************/
+
+void iob_trimtail(FAR struct iob_s *iob, unsigned int trimlen)
+{
+ FAR struct iob_s *entry;
+ FAR struct iob_s *penultimate;
+ FAR struct iob_s *last;
+ unsigned int iosize;
+ int len;
+
+ if (iob && trimlen > 0)
+ {
+ len = trimlen;
+
+ /* Loop until complete the trim */
+
+ while (len > 0)
+ {
+ /* Calculate the total length of the data in the I/O buffer
+ * chain and find the last entry in the chain.
+ */
+
+ penultimate = NULL;
+ last = NULL;
+ iosize = 0;
+
+ for (entry = iob;
+ entry;
+ entry = (FAR struct iob_s *)entry->io_link.flink)
+ {
+ /* Accumulate the total size of all buffers in the list */
+
+ iosize += entry->io_len;
+
+ /* Remember the last and the next to the last in the chain */
+
+ penultimate = last;
+ last = entry;
+ }
+
+ /* Trim from the last entry in the chain. Do we trim this entire
+ * I/O buffer away?
+ */
+
+ if (last->io_len <= len)
+ {
+ /* Yes.. just set is length to zero and skip to the next */
+
+ len -= last->io_len;
+ last->io_len = 0;
+
+ /* There should be a buffer before this one */
+
+ if (!penultimate)
+ {
+ return;
+ }
+
+ /* Free the last, empty buffer in the list */
+
+ iob_free(last);
+ penultimate->io_link.flink = NULL;
+ }
+
+ else
+ {
+ /* No, then just take what we need from this I/O buffer and
+ * stop the trim.
+ */
+
+ last->io_len -= len;
+ memcpy(last->io_data, &last->io_data[len], last->io_len);
+ len = 0;
+ }
+ }
+ }
+}