diff --git a/include/nuttx/net/iob.h b/include/nuttx/net/iob.h index 1db82d0a320..714b4fd80af 100644 --- a/include/nuttx/net/iob.h +++ b/include/nuttx/net/iob.h @@ -47,6 +47,11 @@ * Pre-processor Definitions ****************************************************************************/ +/* IOB flags */ + +#define IOBFLAGS_MCAST (1 << 0) /* Multicast packet */ +#define IOBFLAGS_VLANTAG (1 << 0) /* VLAN tag is value */ + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/net/iob/Make.defs b/net/iob/Make.defs index 6783e597bca..a1134be0b8e 100644 --- a/net/iob/Make.defs +++ b/net/iob/Make.defs @@ -40,8 +40,8 @@ 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 +NET_CSRCS += iob_alloc.c iob_concat.c iob_copyin.c iob_copyout.c iob_free.c +NET_CSRCS += iob_freeq.c iob_initialize.c iob_trimhead.c iob_trimtail.c # Include iob build support diff --git a/net/iob/iob_alloc.c b/net/iob/iob_alloc.c index c2c060d84fc..390883c835b 100644 --- a/net/iob/iob_alloc.c +++ b/net/iob/iob_alloc.c @@ -75,5 +75,18 @@ FAR struct iob_s *iob_alloc(void) { - return (FAR struct iob_s *)sq_remfirst(&g_iob_freelist); + FAR struct iob_s *iob; + + iob = (FAR struct iob_s *)sq_remfirst(&g_iob_freelist); + if (iob) + { + iob->io_link.flink = NULL; /* Not in a list */ + iob->io_flags = 0; /* Flags associated with the I/O buffer */ + iob->io_len = 0; /* Length of the data in the entry */ + iob->io_pktlen = 0; /* Total length of the packet */ + iob->io_vtag = 0; /* VLAN tag */ + iob->io_priv = NULL; /* User private data attached to the I/O buffer */ + } + + return iob; } diff --git a/net/iob/iob_copyin.c b/net/iob/iob_copyin.c new file mode 100644 index 00000000000..d485186790a --- /dev/null +++ b/net/iob/iob_copyin.c @@ -0,0 +1,156 @@ +/**************************************************************************** + * net/iob/iob_copyin.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include + +#include + +#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_copyin + * + * Description: + * Copy data 'len' bytes from a user buffer into the I/O buffer chain, + * starting at 'offset'. + * + ****************************************************************************/ + +int iob_copyin(FAR struct iob_s *iob, FAR const uint8_t *src, + unsigned int len, unsigned int offset) +{ + FAR uint8_t *dest; + 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 from the user buffer */ + + while (len > 0) + { + /* Get the source I/O buffer offset address and the amount of data + * available from that address. + */ + + dest = &iob->io_data[offset]; + avail = CONFIG_IOB_BUFSIZE - offset; + + /* Copy from the user buffer to the I/O 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; + src += ncopy; + + /* Skip to the next I/O buffer in the chain. First, check if we + * are at the end of the buffer chain. + */ + + if (iob->io_link.flink == NULL) + { + struct iob_s *newiob; + + /* Yes.. allocate a new buffer */ + + newiob = iob_alloc(); + if (newiob == NULL) + { + ndbg("ERROR: Failed to allocate I/O buffer\n"); + return -ENOMEM; + } + + /* Add the new I/O buffer to the end of the buffer chain. */ + + iob->io_link.flink = &newiob->io_link; + iob = newiob; + } + else + { + /* Otherwise, just move to the next buffer in the list */ + + iob = (FAR struct iob_s *)iob->io_link.flink; + } + + offset = 0; + } + + return 0; +} diff --git a/net/iob/iob_copyout.c b/net/iob/iob_copyout.c index 5997c7f973a..81512c38bf0 100644 --- a/net/iob/iob_copyout.c +++ b/net/iob/iob_copyout.c @@ -109,7 +109,7 @@ void iob_copyout(FAR uint8_t *dest, FAR const struct iob_s *iob, src = &iob->io_data[offset]; avail = iob->io_len - offset; - /* Copy the whole I/O buffer in to the user buffer */ + /* Copy the from the I/O buffer in to the user buffer */ ncopy = MIN(avail, len); memcpy(dest, src, ncopy);