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
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_PKT)
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2022-01-04 04:53:55 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.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>
|
2018-11-09 21:25:44 +01:00
|
|
|
#include <nuttx/net/ethernet.h>
|
2014-06-12 19:52:06 +02:00
|
|
|
|
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
|
|
|
|
2020-01-13 21:26:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define eth_addr_cmp(addr1, addr2) \
|
|
|
|
((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-12 19:52:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The array containing all packet socket connections */
|
|
|
|
|
2023-02-13 14:05:40 +01:00
|
|
|
#if CONFIG_NET_PKT_PREALLOC_CONNS > 0
|
|
|
|
static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_PREALLOC_CONNS];
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/* A list of all free packet socket connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_pkt_connections;
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
|
2014-06-12 19:52:06 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2017-10-23 16:45:12 +02:00
|
|
|
* only from the network initialization layer.
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 18:34:52 +02:00
|
|
|
void pkt_initialize(void)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
2023-02-13 14:05:40 +01:00
|
|
|
#if CONFIG_NET_PKT_PREALLOC_CONNS > 0
|
2014-06-12 19:52:06 +02:00
|
|
|
int i;
|
|
|
|
|
2023-02-13 14:05:40 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_PKT_PREALLOC_CONNS; i++)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
2022-02-07 04:40:02 +01:00
|
|
|
dq_addlast(&g_pkt_connections[i].sconn.node, &g_free_pkt_connections);
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-18 17:36:07 +02:00
|
|
|
* Name: pkt_alloc()
|
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;
|
2023-02-13 14:05:40 +01:00
|
|
|
#if CONFIG_NET_PKT_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
int i;
|
|
|
|
#endif
|
2014-06-12 19:52:06 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2014-06-12 19:52:06 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2023-02-13 14:05:40 +01:00
|
|
|
#if CONFIG_NET_PKT_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
if (dq_peek(&g_free_pkt_connections) == NULL)
|
|
|
|
{
|
2023-02-13 14:05:40 +01:00
|
|
|
#if CONFIG_NET_PKT_MAX_CONNS > 0
|
2024-04-06 15:07:06 +02:00
|
|
|
if (dq_count(&g_active_pkt_connections) +
|
|
|
|
CONFIG_NET_PKT_ALLOC_CONNS > CONFIG_NET_PKT_MAX_CONNS)
|
2023-02-13 14:05:40 +01:00
|
|
|
{
|
|
|
|
nxmutex_unlock(&g_free_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_PKT_ALLOC_CONNS);
|
2022-01-04 04:53:55 +01:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
2023-02-13 14:05:40 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_PKT_ALLOC_CONNS; i++)
|
2022-01-04 04:53:55 +01:00
|
|
|
{
|
2022-02-07 04:40:02 +01:00
|
|
|
dq_addlast(&conn[i].sconn.node, &g_free_pkt_connections);
|
2022-01-04 04:53:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
2022-02-07 04:40:02 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_active_pkt_connections);
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
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
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2014-06-12 19:52:06 +02:00
|
|
|
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
2022-02-07 04:40:02 +01:00
|
|
|
dq_rem(&conn->sconn.node, &g_active_pkt_connections);
|
2014-06-12 19:52:06 +02:00
|
|
|
|
2023-02-13 14:05:40 +01:00
|
|
|
/* If this is a preallocated or a batch allocated connection store it in
|
|
|
|
* the free connections list. Else free it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NET_PKT_ALLOC_CONNS == 1
|
|
|
|
if (conn < g_pkt_connections || conn >= (g_pkt_connections +
|
|
|
|
CONFIG_NET_PKT_PREALLOC_CONNS))
|
|
|
|
{
|
|
|
|
kmm_free(conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2023-02-28 11:31:21 +01:00
|
|
|
memset(conn, 0, sizeof(*conn));
|
2023-02-13 14:05:40 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_free_pkt_connections);
|
|
|
|
}
|
2014-06-12 19:52:06 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
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:
|
2017-10-23 16:45:12 +02:00
|
|
|
* Find a connection structure that is the appropriate connection to be
|
2024-02-19 04:46:04 +01:00
|
|
|
* used with the provided network device
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-10-23 16:45:12 +02:00
|
|
|
* This function is called from network logic at with the network locked.
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-02-19 04:46:04 +01:00
|
|
|
FAR struct pkt_conn_s *pkt_active(FAR struct net_driver_s *dev)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
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)
|
|
|
|
{
|
2024-02-19 04:46:04 +01:00
|
|
|
if (dev->d_ifindex == conn->ifindex)
|
2014-06-12 19:52:06 +02:00
|
|
|
{
|
|
|
|
/* Matching connection found.. return a reference to it */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2022-02-07 04:40:02 +01:00
|
|
|
conn = (FAR struct pkt_conn_s *)conn->sconn.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:
|
2017-10-23 16:45:12 +02:00
|
|
|
* This function is called from network logic at with the network locked.
|
2014-06-12 19:52:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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
|
|
|
|
{
|
2022-02-07 04:40:02 +01:00
|
|
|
return (FAR struct pkt_conn_s *)conn->sconn.node.flink;
|
2014-06-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_PKT */
|