From 6296aeecdb7aa5b3340ef8efefa3f62e96b244d6 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 28 Jul 2025 15:28:02 +0800 Subject: [PATCH] net/route/ramroute: replace prealloc with netpool reuse the netpool module to optimize the code implementation. Signed-off-by: zhanghongyu --- net/route/net_add_ramroute.c | 10 +-- net/route/net_alloc_ramroute.c | 149 ++++++++++++------------------- net/route/net_foreach_ramroute.c | 12 +-- net/route/net_initroute.c | 4 - net/route/ramroute.h | 45 ++++++---- 5 files changed, 92 insertions(+), 128 deletions(-) diff --git a/net/route/net_add_ramroute.c b/net/route/net_add_ramroute.c index e0c95943d51..140a21f2bbf 100644 --- a/net/route/net_add_ramroute.c +++ b/net/route/net_add_ramroute.c @@ -82,13 +82,13 @@ int net_addroute_ipv4(in_addr_t target, in_addr_t netmask, in_addr_t router) /* Get exclusive address to the networking data structures */ - net_lock(); + net_lockroute_ipv4(); /* Then add the new entry to the table */ ramroute_ipv4_addlast((FAR struct net_route_ipv4_entry_s *)route, &g_ipv4_routes); - net_unlock(); + net_unlockroute_ipv4(); netlink_route_notify(route, RTM_NEWROUTE, AF_INET); return OK; @@ -119,13 +119,11 @@ int net_addroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask, /* Get exclusive address to the networking data structures */ - net_lock(); - - /* Then add the new entry to the table */ + net_lockroute_ipv6(); ramroute_ipv6_addlast((FAR struct net_route_ipv6_entry_s *)route, &g_ipv6_routes); - net_unlock(); + net_unlockroute_ipv6(); netlink_route_notify(route, RTM_NEWROUTE, AF_INET6); return OK; diff --git a/net/route/net_alloc_ramroute.c b/net/route/net_alloc_ramroute.c index e5ec79ece31..2f4c62617fa 100644 --- a/net/route/net_alloc_ramroute.c +++ b/net/route/net_alloc_ramroute.c @@ -33,6 +33,7 @@ #include #include +#include "utils/utils.h" #include "route/ramroute.h" #include "route/route.h" @@ -58,80 +59,22 @@ FAR struct net_route_ipv6_queue_s g_ipv6_routes; * Private Data ****************************************************************************/ -/* These are lists of free routing table entries */ +/* The array containing all routing table entries. */ #ifdef CONFIG_ROUTE_IPv4_RAMROUTE -static struct net_route_ipv4_queue_s g_free_ipv4routes; +NET_BUFPOOL_DECLARE(g_ipv4routes, sizeof(struct net_route_ipv4_entry_s), + CONFIG_ROUTE_MAX_IPv4_RAMROUTES, 0, 0); #endif #ifdef CONFIG_ROUTE_IPv6_RAMROUTE -static struct net_route_ipv6_queue_s g_free_ipv6routes; -#endif - -/* These are arrays of pre-allocated network routes */ - -#ifdef CONFIG_ROUTE_IPv4_RAMROUTE -static struct net_route_ipv4_entry_s - g_prealloc_ipv4routes[CONFIG_ROUTE_MAX_IPv4_RAMROUTES]; -#endif - -#ifdef CONFIG_ROUTE_IPv6_RAMROUTE -static struct net_route_ipv6_entry_s - g_prealloc_ipv6routes[CONFIG_ROUTE_MAX_IPv6_RAMROUTES]; +NET_BUFPOOL_DECLARE(g_ipv6routes, sizeof(struct net_route_ipv6_entry_s), + CONFIG_ROUTE_MAX_IPv6_RAMROUTES, 0, 0); #endif /**************************************************************************** * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: net_init_ramroute - * - * Description: - * Initialize the in-memory, RAM routing table - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - * Assumptions: - * Called early in initialization so that no special protection is needed. - * - ****************************************************************************/ - -void net_init_ramroute(void) -{ - int i; - - /* Initialize the routing table and the free list */ - -#ifdef CONFIG_ROUTE_IPv4_RAMROUTE - ramroute_init(&g_ipv4_routes); - ramroute_init(&g_free_ipv4routes); - - /* Add all of the pre-allocated routing table entries to a free list */ - - for (i = 0; i < CONFIG_ROUTE_MAX_IPv4_RAMROUTES; i++) - { - ramroute_ipv4_addlast(&g_prealloc_ipv4routes[i], &g_free_ipv4routes); - } -#endif - -#ifdef CONFIG_ROUTE_IPv6_RAMROUTE - ramroute_init(&g_ipv6_routes); - ramroute_init(&g_free_ipv6routes); - - /* Add all of the pre-allocated routing table entries to a free list */ - - for (i = 0; i < CONFIG_ROUTE_MAX_IPv6_RAMROUTES; i++) - { - ramroute_ipv6_addlast(&g_prealloc_ipv6routes[i], &g_free_ipv6routes); - } -#endif -} - /**************************************************************************** * Name: net_allocroute_ipv4 and net_allocroute_ipv6 * @@ -152,15 +95,11 @@ FAR struct net_route_ipv4_s *net_allocroute_ipv4(void) { FAR struct net_route_ipv4_entry_s *route; - /* Get exclusive address to the networking data structures */ + /* Get exclusive address to the networking data structures and + * then remove the first entry from the g_ipv4routes pool + */ - net_lock(); - - /* Then add the remove the first entry from the table */ - - route = ramroute_ipv4_remfirst(&g_free_ipv4routes); - - net_unlock(); + route = NET_BUFPOOL_TRYALLOC(g_ipv4routes); if (!route) { return NULL; @@ -175,15 +114,11 @@ FAR struct net_route_ipv6_s *net_allocroute_ipv6(void) { FAR struct net_route_ipv6_entry_s *route; - /* Get exclusive address to the networking data structures */ + /* Get exclusive address to the networking data structures and + * then remove the first entry from the g_ipv6routes pool + */ - net_lock(); - - /* Then add the remove the first entry from the table */ - - route = ramroute_ipv6_remfirst(&g_free_ipv6routes); - - net_unlock(); + route = NET_BUFPOOL_TRYALLOC(g_ipv6routes); if (!route) { return NULL; @@ -212,15 +147,9 @@ void net_freeroute_ipv4(FAR struct net_route_ipv4_s *route) { DEBUGASSERT(route); - /* Get exclusive address to the networking data structures */ + /* Add the new entry to the g_ipv4routes pool */ - net_lock(); - - /* Then add the new entry to the table */ - - ramroute_ipv4_addlast((FAR struct net_route_ipv4_entry_s *)route, - &g_free_ipv4routes); - net_unlock(); + NET_BUFPOOL_FREE(g_ipv4routes, (FAR struct net_route_ipv4_entry_s *)route); } #endif @@ -229,15 +158,49 @@ void net_freeroute_ipv6(FAR struct net_route_ipv6_s *route) { DEBUGASSERT(route); - /* Get exclusive address to the networking data structures */ + /* Add the new entry to the g_ipv6routes pool */ - net_lock(); + NET_BUFPOOL_FREE(g_ipv6routes, (FAR struct net_route_ipv6_entry_s *)route); +} +#endif - /* Then add the new entry to the table */ +/**************************************************************************** + * Name: net_lockroute_ipv4 and net_unlockroute_ipv4 + * + * Description: + * Lock and unlock the g_ipv4routes pool + * + ****************************************************************************/ - ramroute_ipv6_addlast((FAR struct net_route_ipv6_entry_s *)route, - &g_free_ipv6routes); - net_unlock(); +#ifdef CONFIG_ROUTE_IPv4_RAMROUTE +void net_lockroute_ipv4(void) +{ + NET_BUFPOOL_LOCK(g_ipv4routes); +} + +void net_unlockroute_ipv4(void) +{ + NET_BUFPOOL_UNLOCK(g_ipv4routes); +} +#endif + +/**************************************************************************** + * Name: net_lockroute_ipv6 and net_unlockroute_ipv6 + * + * Description: + * Lock and unlock the g_ipv6routes pool + * + ****************************************************************************/ + +#ifdef CONFIG_ROUTE_IPv6_RAMROUTE +void net_lockroute_ipv6(void) +{ + NET_BUFPOOL_LOCK(g_ipv6routes); +} + +void net_unlockroute_ipv6(void) +{ + NET_BUFPOOL_UNLOCK(g_ipv6routes); } #endif diff --git a/net/route/net_foreach_ramroute.c b/net/route/net_foreach_ramroute.c index 97b2fcfe093..90f5959f0a6 100644 --- a/net/route/net_foreach_ramroute.c +++ b/net/route/net_foreach_ramroute.c @@ -68,7 +68,7 @@ int net_foreachroute_ipv4(route_handler_ipv4_t handler, FAR void *arg) /* Prevent concurrent access to the routing table */ - net_lock(); + net_lockroute_ipv4(); /* Visit each entry in the routing table */ @@ -82,9 +82,9 @@ int net_foreachroute_ipv4(route_handler_ipv4_t handler, FAR void *arg) ret = handler(&route->entry, arg); } - /* Unlock the network */ + /* Unlock the g_ipv4_routes */ - net_unlock(); + net_unlockroute_ipv4(); return ret; } #endif @@ -98,7 +98,7 @@ int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg) /* Prevent concurrent access to the routing table */ - net_lock(); + net_lockroute_ipv6(); /* Visit each entry in the routing table */ @@ -112,9 +112,9 @@ int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg) ret = handler(&route->entry, arg); } - /* Unlock the network */ + /* Unlock the g_ipv6_routes */ - net_unlock(); + net_unlockroute_ipv6(); return ret; } #endif diff --git a/net/route/net_initroute.c b/net/route/net_initroute.c index aedaa758bac..ac4a1fa50d9 100644 --- a/net/route/net_initroute.c +++ b/net/route/net_initroute.c @@ -52,10 +52,6 @@ void net_init_route(void) { -#if defined(CONFIG_ROUTE_IPv4_RAMROUTE) || defined(CONFIG_ROUTE_IPv6_RAMROUTE) - net_init_ramroute(); -#endif - #if defined(CONFIG_ROUTE_IPv4_CACHEROUTE) || defined(CONFIG_ROUTE_IPv6_CACHEROUTE) net_init_cacheroute(); #endif diff --git a/net/route/ramroute.h b/net/route/ramroute.h index 74aff0c0c67..b3c9cf0f6af 100644 --- a/net/route/ramroute.h +++ b/net/route/ramroute.h @@ -119,25 +119,6 @@ extern struct net_route_ipv6_queue_s g_ipv6_routes; * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: net_init_ramroute - * - * Description: - * Initialize the in-memory, RAM routing table - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - * Assumptions: - * Called early in initialization so that no special protection is needed. - * - ****************************************************************************/ - -void net_init_ramroute(void); - /**************************************************************************** * Name: net_allocroute_ipv4 and net_allocroute_ipv6 * @@ -183,6 +164,32 @@ void net_freeroute_ipv4(FAR struct net_route_ipv4_s *route); void net_freeroute_ipv6(FAR struct net_route_ipv6_s *route); #endif +/**************************************************************************** + * Name: net_lockroute_ipv4 and net_unlockroute_ipv4 + * + * Description: + * Lock and unlock the g_ipv4routes pool + * + ****************************************************************************/ + +#ifdef CONFIG_ROUTE_IPv4_RAMROUTE +void net_lockroute_ipv4(void); +void net_unlockroute_ipv4(void); +#endif + +/**************************************************************************** + * Name: net_lockroute_ipv6 and net_unlockroute_ipv6 + * + * Description: + * Lock and unlock the g_ipv6routes pool + * + ****************************************************************************/ + +#ifdef CONFIG_ROUTE_IPv6_RAMROUTE +void net_lockroute_ipv6(void); +void net_unlockroute_ipv6(void); +#endif + /**************************************************************************** * Name: (various low-level list operations) *