net/udp: add support for CONFIG_NET_ALLOC_CONNS
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
parent
38b7b3d26a
commit
26370cd2f7
@ -80,7 +80,9 @@
|
|||||||
|
|
||||||
/* The array containing all UDP connections. */
|
/* The array containing all UDP connections. */
|
||||||
|
|
||||||
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
||||||
struct udp_conn_s g_udp_connections[CONFIG_NET_UDP_CONNS];
|
struct udp_conn_s g_udp_connections[CONFIG_NET_UDP_CONNS];
|
||||||
|
#endif
|
||||||
|
|
||||||
/* A list of all free UDP connections */
|
/* A list of all free UDP connections */
|
||||||
|
|
||||||
@ -125,15 +127,12 @@ static FAR struct udp_conn_s *udp_find_conn(uint8_t domain,
|
|||||||
FAR union ip_binding_u *ipaddr,
|
FAR union ip_binding_u *ipaddr,
|
||||||
uint16_t portno)
|
uint16_t portno)
|
||||||
{
|
{
|
||||||
FAR struct udp_conn_s *conn;
|
FAR struct udp_conn_s *conn = NULL;
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Now search each connection structure. */
|
/* Now search each connection structure. */
|
||||||
|
|
||||||
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
while ((conn = udp_nextconn(conn)) != NULL)
|
||||||
{
|
{
|
||||||
conn = &g_udp_connections[i];
|
|
||||||
|
|
||||||
/* If the port local port number assigned to the connections matches
|
/* If the port local port number assigned to the connections matches
|
||||||
* AND the IP address of the connection matches, then return a
|
* AND the IP address of the connection matches, then return a
|
||||||
* reference to the connection structure. INADDR_ANY is a special
|
* reference to the connection structure. INADDR_ANY is a special
|
||||||
@ -453,6 +452,46 @@ static inline FAR struct udp_conn_s *
|
|||||||
}
|
}
|
||||||
#endif /* CONFIG_NET_IPv6 */
|
#endif /* CONFIG_NET_IPv6 */
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: udp_alloc_conn
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Allocate a uninitialized UDP connection structure.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef CONFIG_NET_ALLOC_CONNS
|
||||||
|
FAR struct udp_conn_s *udp_alloc_conn(void)
|
||||||
|
{
|
||||||
|
FAR struct udp_conn_s *conn;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Return the entry from the head of the free list */
|
||||||
|
|
||||||
|
if (dq_peek(&g_free_udp_connections) == NULL)
|
||||||
|
{
|
||||||
|
conn = kmm_zalloc(sizeof(struct udp_conn_s) *
|
||||||
|
CONFIG_NET_UDP_CONNS);
|
||||||
|
if (conn == NULL)
|
||||||
|
{
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now initialize each connection structure */
|
||||||
|
|
||||||
|
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
||||||
|
{
|
||||||
|
/* Mark the connection closed and move it to the free list */
|
||||||
|
|
||||||
|
conn[i].lport = 0;
|
||||||
|
dq_addlast(&conn[i].node, &g_free_udp_connections);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -538,7 +577,9 @@ uint16_t udp_select_port(uint8_t domain, FAR union ip_binding_u *u)
|
|||||||
|
|
||||||
void udp_initialize(void)
|
void udp_initialize(void)
|
||||||
{
|
{
|
||||||
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
||||||
int i;
|
int i;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Initialize the queues */
|
/* Initialize the queues */
|
||||||
|
|
||||||
@ -546,6 +587,7 @@ void udp_initialize(void)
|
|||||||
dq_init(&g_active_udp_connections);
|
dq_init(&g_active_udp_connections);
|
||||||
nxsem_init(&g_free_sem, 0, 1);
|
nxsem_init(&g_free_sem, 0, 1);
|
||||||
|
|
||||||
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
||||||
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
||||||
{
|
{
|
||||||
/* Mark the connection closed and move it to the free list */
|
/* Mark the connection closed and move it to the free list */
|
||||||
@ -553,6 +595,7 @@ void udp_initialize(void)
|
|||||||
g_udp_connections[i].lport = 0;
|
g_udp_connections[i].lport = 0;
|
||||||
dq_addlast(&g_udp_connections[i].node, &g_free_udp_connections);
|
dq_addlast(&g_udp_connections[i].node, &g_free_udp_connections);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -571,7 +614,11 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain)
|
|||||||
/* The free list is protected by a semaphore (that behaves like a mutex). */
|
/* The free list is protected by a semaphore (that behaves like a mutex). */
|
||||||
|
|
||||||
_udp_semtake(&g_free_sem);
|
_udp_semtake(&g_free_sem);
|
||||||
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
||||||
conn = (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
|
conn = (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
|
||||||
|
#else
|
||||||
|
conn = udp_alloc_conn();
|
||||||
|
#endif
|
||||||
if (conn)
|
if (conn)
|
||||||
{
|
{
|
||||||
/* Make sure that the connection is marked as uninitialized */
|
/* Make sure that the connection is marked as uninitialized */
|
||||||
|
Loading…
Reference in New Issue
Block a user