2014-06-12 19:52:06 +02:00
|
|
|
/****************************************************************************
|
2014-06-18 19:02:52 +02:00
|
|
|
* net/pkt/pkt_conn.c
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Large parts of this file were leveraged from uIP logic:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_PKT)
|
|
|
|
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-12 19:52:06 +02:00
|
|
|
#include <nuttx/net/arp.h>
|
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 18:34:52 +02:00
|
|
|
#include "pkt/pkt.h"
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The array containing all packet socket connections */
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_CONNS];
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/* A list of all free packet socket connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_pkt_connections;
|
|
|
|
static sem_t g_free_sem;
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
/* A list of all allocated packet socket connections */
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
static dq_queue_t g_active_pkt_connections;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Name: _pkt_semtake() and _pkt_semgive()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take/give semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static inline void _pkt_semtake(sem_t *sem)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
while (net_lockedwait(sem) != 0)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(*get_errno_ptr() == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
#define _pkt_semgive(sem) sem_post(sem)
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 18:34:52 +02:00
|
|
|
* Name: pkt_initialize()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the packet socket connection structures. Called once and
|
|
|
|
* only from the UIP layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
void pkt_initialize(void)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize the queues */
|
|
|
|
|
|
|
|
dq_init(&g_free_pkt_connections);
|
|
|
|
dq_init(&g_active_pkt_connections);
|
|
|
|
sem_init(&g_free_sem, 0, 1);
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_NET_PKT_CONNS; i++)
|
|
|
|
{
|
|
|
|
/* Mark the connection closed and move it to the free list */
|
2015-01-25 14:36:16 +01:00
|
|
|
|
2014-06-12 19:52:06 +02:00
|
|
|
g_pkt_connections[i].ifindex = 0;
|
|
|
|
dq_addlast(&g_pkt_connections[i].node, &g_free_pkt_connections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 18:34:52 +02:00
|
|
|
* Name: pkt_palloc()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-05 21:59:22 +02:00
|
|
|
* Allocate a new, uninitialized packet socket connection structure. This
|
|
|
|
* is normally something done by the implementation of the socket() API
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
FAR struct pkt_conn_s *pkt_alloc(void)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
2014-06-25 18:34:52 +02:00
|
|
|
FAR struct pkt_conn_s *conn;
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/* The free list is only accessed from user, non-interrupt level and
|
|
|
|
* is protected by a semaphore (that behaves like a mutex).
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_pkt_semtake(&g_free_sem);
|
2014-06-25 18:34:52 +02:00
|
|
|
conn = (FAR struct pkt_conn_s *)dq_remfirst(&g_free_pkt_connections);
|
2014-06-12 19:52:06 +02:00
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
/* Make sure that the connection is marked as uninitialized */
|
|
|
|
|
|
|
|
conn->ifindex = 0;
|
|
|
|
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
|
|
|
dq_addlast(&conn->node, &g_active_pkt_connections);
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_pkt_semgive(&g_free_sem);
|
2014-06-12 19:52:06 +02:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 18:34:52 +02:00
|
|
|
* Name: pkt_free()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a packet socket connection structure that is no longer in use.
|
|
|
|
* This should be done by the implementation of close().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
void pkt_free(FAR struct pkt_conn_s *conn)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
/* The free list is only accessed from user, non-interrupt level and
|
|
|
|
* is protected by a semaphore (that behaves like a mutex).
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_pkt_semtake(&g_free_sem);
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
|
|
|
dq_rem(&conn->node, &g_active_pkt_connections);
|
|
|
|
|
|
|
|
/* Free the connection */
|
|
|
|
|
|
|
|
dq_addlast(&conn->node, &g_free_pkt_connections);
|
2014-06-29 03:26:16 +02:00
|
|
|
_pkt_semgive(&g_free_sem);
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 18:34:52 +02:00
|
|
|
* Name: pkt_active()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a connection structure that is the appropriate
|
|
|
|
* connection to be used with the provided Ethernet header
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
FAR struct pkt_conn_s *pkt_active(struct eth_hdr_s *buf)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
2014-06-29 21:20:00 +02:00
|
|
|
#define eth_addr_cmp(addr1, addr2) \
|
2014-06-12 19:52:06 +02:00
|
|
|
((addr1[0] == addr2[0]) && (addr1[1] == addr2[1]) && \
|
|
|
|
(addr1[2] == addr2[2]) && (addr1[3] == addr2[3]) && \
|
|
|
|
(addr1[4] == addr2[4]) && (addr1[5] == addr2[5]))
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
FAR struct pkt_conn_s *conn =
|
|
|
|
(FAR struct pkt_conn_s *)g_active_pkt_connections.head;
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
while (conn)
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
/* FIXME lmac in conn should have been set by pkt_bind() */
|
2014-06-12 19:52:06 +02:00
|
|
|
|
2014-06-29 21:20:00 +02:00
|
|
|
if (eth_addr_cmp(buf->dest, conn->lmac))
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
/* Matching connection found.. return a reference to it */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
conn = (FAR struct pkt_conn_s *)conn->node.flink;
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-07-01 02:03:58 +02:00
|
|
|
* Name: pkt_nextconn()
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of allocated packet connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level (or with
|
|
|
|
* interrupts disabled).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-01 02:03:58 +02:00
|
|
|
FAR struct pkt_conn_s *pkt_nextconn(FAR struct pkt_conn_s *conn)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
if (!conn)
|
|
|
|
{
|
2014-06-25 18:34:52 +02:00
|
|
|
return (FAR struct pkt_conn_s *)g_active_pkt_connections.head;
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-25 18:34:52 +02:00
|
|
|
return (FAR struct pkt_conn_s *)conn->node.flink;
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_PKT */
|