From a1bf9ca88b00f5bc9f865a6cef96f929ab140a5a Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Tue, 4 Jan 2022 11:53:15 +0800 Subject: [PATCH] net/icmp[v6]: add support for CONFIG_NET_ALLOC_CONNS Signed-off-by: chao.an --- net/icmp/icmp_conn.c | 29 +++++++++++++++++++++++++---- net/icmpv6/icmpv6_conn.c | 29 +++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/net/icmp/icmp_conn.c b/net/icmp/icmp_conn.c index 3cc82691c2..b28cf9edb2 100644 --- a/net/icmp/icmp_conn.c +++ b/net/icmp/icmp_conn.c @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -47,7 +48,9 @@ /* The array containing all IPPROTO_ICMP socket connections */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct icmp_conn_s g_icmp_connections[CONFIG_NET_ICMP_NCONNS]; +#endif /* A list of all free IPPROTO_ICMP socket connections */ @@ -73,7 +76,9 @@ static dq_queue_t g_active_icmp_connections; void icmp_sock_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ @@ -81,12 +86,14 @@ void icmp_sock_initialize(void) dq_init(&g_active_icmp_connections); nxsem_init(&g_free_sem, 0, 1); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_ICMP_NCONNS; i++) { /* Move the connection structure to the free list */ dq_addlast(&g_icmp_connections[i].node, &g_free_icmp_connections); } +#endif } /**************************************************************************** @@ -109,13 +116,23 @@ FAR struct icmp_conn_s *icmp_alloc(void) ret = net_lockedwait(&g_free_sem); if (ret >= 0) { +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_free_icmp_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_ICMP_NCONNS); + if (conn != NULL) + { + for (ret = 0; ret < CONFIG_NET_ICMP_NCONNS; ret++) + { + dq_addlast(&conn[ret].node, &g_free_retcmp_connectretons); + } + } + } +#endif + conn = (FAR struct icmp_conn_s *)dq_remfirst(&g_free_icmp_connections); if (conn != NULL) { - /* Clear the connection structure */ - - memset(conn, 0, sizeof(struct icmp_conn_s)); - /* Enqueue the connection into the active list */ dq_addlast(&conn->node, &g_active_icmp_connections); @@ -162,6 +179,10 @@ void icmp_free(FAR struct icmp_conn_s *conn) dq_rem(&conn->node, &g_active_icmp_connections); + /* Clear the connection structure */ + + memset(conn, 0, sizeof(*conn)); + /* Free the connection */ dq_addlast(&conn->node, &g_free_icmp_connections); diff --git a/net/icmpv6/icmpv6_conn.c b/net/icmpv6/icmpv6_conn.c index 4815ae91b7..fe4903b241 100644 --- a/net/icmpv6/icmpv6_conn.c +++ b/net/icmpv6/icmpv6_conn.c @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -47,7 +48,9 @@ /* The array containing all IPPROTO_ICMP socket connections */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct icmpv6_conn_s g_icmpv6_connections[CONFIG_NET_ICMPv6_NCONNS]; +#endif /* A list of all free IPPROTO_ICMP socket connections */ @@ -73,7 +76,9 @@ static dq_queue_t g_active_icmpv6_connections; void icmpv6_sock_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ @@ -81,12 +86,14 @@ void icmpv6_sock_initialize(void) dq_init(&g_active_icmpv6_connections); nxsem_init(&g_free_sem, 0, 1); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_ICMPv6_NCONNS; i++) { /* Move the connection structure to the free list */ dq_addlast(&g_icmpv6_connections[i].node, &g_free_icmpv6_connections); } +#endif } /**************************************************************************** @@ -109,14 +116,24 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void) ret = net_lockedwait(&g_free_sem); if (ret >= 0) { +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_active_icmpv6_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_ICMPv6_NCONNS); + if (conn != NULL) + { + for (ret = 0; ret < CONFIG_NET_ICMPv6_NCONNS; ret++) + { + dq_addlast(&conn[i].node, &g_free_icmpv6_connections); + } + } + } +#endif + conn = (FAR struct icmpv6_conn_s *) dq_remfirst(&g_free_icmpv6_connections); if (conn != NULL) { - /* Clear the connection structure */ - - memset(conn, 0, sizeof(struct icmpv6_conn_s)); - /* Enqueue the connection into the active list */ dq_addlast(&conn->node, &g_active_icmpv6_connections); @@ -151,6 +168,10 @@ void icmpv6_free(FAR struct icmpv6_conn_s *conn) dq_rem(&conn->node, &g_active_icmpv6_connections); + /* Clear the connection structure */ + + memset(conn, 0, sizeof(*conn)); + /* Free the connection */ dq_addlast(&conn->node, &g_free_icmpv6_connections);