[v1,02/12] node: add IP4 lookup FIB node

Message ID 20250415121052.1497155-3-adwivedi@marvell.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series add lookup fib nodes in graph library |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Ankur Dwivedi April 15, 2025, 12:10 p.m. UTC
Adds a lookup FIB node for IP4.

Signed-off-by: Ankur Dwivedi <adwivedi@marvell.com>
---
 lib/node/ip4_lookup_fib.c | 127 ++++++++++++++++++++++++++++++++++++++
 lib/node/meson.build      |   3 +-
 2 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 lib/node/ip4_lookup_fib.c
  

Comments

Nitin Saxena April 16, 2025, 7:32 a.m. UTC | #1
Hi Ankur,

Please see my comments inline below

Thanks,
Nitin

On Tue, Apr 15, 2025 at 5:41 PM Ankur Dwivedi <adwivedi@marvell.com> wrote:
>
> Adds a lookup FIB node for IP4.
>
> Signed-off-by: Ankur Dwivedi <adwivedi@marvell.com>
> ---
>  lib/node/ip4_lookup_fib.c | 127 ++++++++++++++++++++++++++++++++++++++
>  lib/node/meson.build      |   3 +-
>  2 files changed, 129 insertions(+), 1 deletion(-)
>  create mode 100644 lib/node/ip4_lookup_fib.c
>
> diff --git a/lib/node/ip4_lookup_fib.c b/lib/node/ip4_lookup_fib.c
> new file mode 100644
> index 0000000000..9c71610718
> --- /dev/null
> +++ b/lib/node/ip4_lookup_fib.c
> @@ -0,0 +1,127 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(C) 2025 Marvell.
> + */
> +
> +#include <rte_errno.h>
> +#include <rte_ether.h>
> +#include <rte_fib.h>
> +#include <rte_graph.h>
> +#include <rte_graph_worker.h>
> +#include <rte_ip.h>
> +
> +#include "rte_node_ip4_api.h"
> +
> +#include "node_private.h"
> +
> +/* IP4 Lookup global data struct */
> +struct ip4_lookup_fib_node_main {
> +       struct rte_fib *fib[RTE_MAX_NUMA_NODES];
> +};
> +
> +struct ip4_lookup_fib_node_ctx {
> +       /* Socket's FIB */
> +       struct rte_fib *fib;
> +       /* Dynamic offset to mbuf priv1 */
> +       int mbuf_priv1_off;
> +};
> +
> +static struct ip4_lookup_fib_node_main ip4_lookup_fib_nm;
> +
> +#define FIB_MAX_ROUTES (1 << 16)
> +#define FIB_NUM_TBL8   (1 << 15)
> +#define FIB_DEFAULT_NH 999

These macros may not be required if we expose public setup_api() with
arguments. See below

> +
> +#define IP4_LOOKUP_NODE_FIB(ctx) \
> +       (((struct ip4_lookup_fib_node_ctx *)ctx)->fib)
> +
> +#define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
> +       (((struct ip4_lookup_fib_node_ctx *)ctx)->mbuf_priv1_off)
> +
> +static int
> +setup_fib(unsigned int socket)

Should we add public API to allow applications to control MAX_ROUTES?
In a typical stack multiple fibs can be set up for each VRF (~~ port_id).

A public API:

int rte_ip4_lookup_fib_setup(int fib_index, int port_id, uint32_t
max_routes) where

For now we can assume fib_index == 0, is global fib table (can be
extended to VRF later). Socket_id can be determined from port_Id

> +{
> +       struct ip4_lookup_fib_node_main *nm = &ip4_lookup_fib_nm;
> +       struct rte_fib_conf conf;
> +       char s[RTE_FIB_NAMESIZE];
> +
> +       /* One fib per socket */
> +       if (nm->fib[socket])
> +               return 0;
> +
> +       conf.type = RTE_FIB_DIR24_8;
> +       conf.default_nh = FIB_DEFAULT_NH;

FIB_DEFAULT_NH can be defined in such a way, fast path can decode
next_edge from return of rte_fib_lookup_bulk()
Like
union {
   struct u64;
   struct {
       uint32_t next_hop_id;
       uint16_t  next_edge;
       uint16_t. reserved;
   };
} rte_ip4_lookup_fib_nexthop_t;

FIB_DEFAULT_NH should be set as

rte_ip4_lookup_fib_nexthop_t default_next_hop = {.next_edge =
IP4_FIB_LOOKUP_NEXT_DROP}

This way in fast path a return from fib_bulk() can be directly used to
send packet to pkt_drop node

> +       conf.max_routes = FIB_MAX_ROUTES;
> +       conf.rib_ext_sz = 0;
> +       conf.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
> +       conf.dir24_8.num_tbl8 = FIB_NUM_TBL8;
> +       conf.flags = 0;
> +       snprintf(s, sizeof(s), "IPV4_LOOKUP_FIB_%d", socket);
> +       nm->fib[socket] = rte_fib_create(s, socket, &conf);
> +       if (nm->fib[socket] == NULL)
> +               return -rte_errno;
> +
> +       return 0;
> +}
> +
> +static int
> +ip4_lookup_fib_node_init(const struct rte_graph *graph, struct rte_node *node)
> +{
> +       static uint8_t init_once;
> +       unsigned int socket;
> +       uint16_t lcore_id;
> +       int rc;
> +
> +       RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_fib_node_ctx) > RTE_NODE_CTX_SZ);
> +
> +       if (!init_once) {
> +               node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
> +                               &node_mbuf_priv1_dynfield_desc);
> +               if (node_mbuf_priv1_dynfield_offset < 0)
> +                       return -rte_errno;

You may need to rebase this patch on top of
https://patches.dpdk.org/project/dpdk/patch/20250409135554.2180390-2-nsaxena@marvell.com/
for using global mbuf field

> +
> +               /* Setup FIB for all sockets */
> +               RTE_LCORE_FOREACH(lcore_id)

Instead can be use rte_socket_count() which allow to loop for socket
instead of port? Ideally we may need to add fib for virtual interfaces
or VRF (later)

> +               {
> +                       socket = rte_lcore_to_socket_id(lcore_id);
> +                       rc = setup_fib(socket);
> +                       if (rc) {
> +                               node_err("ip4_lookup_fib",
> +                                        "Failed to setup fib for sock %u, rc=%d",
> +                                        socket, rc);
> +                               return rc;
> +                       }
> +               }
> +               init_once = 1;
> +       }
> +
> +       /* Update socket's FIB and mbuf dyn priv1 offset in node ctx */
> +       IP4_LOOKUP_NODE_FIB(node->ctx) = ip4_lookup_fib_nm.fib[graph->socket];
> +       IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) = node_mbuf_priv1_dynfield_offset;
> +
> +       node_dbg("ip4_lookup_fib", "Initialized ip4_lookup_fib node");
> +
> +       return 0;
> +}
> +
> +static struct rte_node_xstats ip4_lookup_fib_xstats = {
> +       .nb_xstats = 1,
> +       .xstat_desc = {
> +               [0] = "ip4_lookup_fib_error",
> +       },
> +};
> +
> +static struct rte_node_register ip4_lookup_fib_node = {
> +       .name = "ip4_lookup_fib",
> +
> +       .init = ip4_lookup_fib_node_init,
> +       .xstats = &ip4_lookup_fib_xstats,
> +
> +       .nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP + 1,
> +       .next_nodes = {
> +               [RTE_NODE_IP4_LOOKUP_NEXT_IP4_LOCAL] = "ip4_local",
> +               [RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
> +               [RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
> +       },
> +};
> +
> +RTE_NODE_REGISTER(ip4_lookup_fib_node);
> diff --git a/lib/node/meson.build b/lib/node/meson.build
> index 0bed97a96c..d2011c8f56 100644
> --- a/lib/node/meson.build
> +++ b/lib/node/meson.build
> @@ -13,6 +13,7 @@ sources = files(
>          'ethdev_tx.c',
>          'ip4_local.c',
>          'ip4_lookup.c',
> +        'ip4_lookup_fib.c',
>          'ip4_reassembly.c',
>          'ip4_rewrite.c',
>          'ip6_lookup.c',
> @@ -34,4 +35,4 @@ headers = files(
>
>  # Strict-aliasing rules are violated by uint8_t[] to context size casts.
>  cflags += '-fno-strict-aliasing'
> -deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag']
> +deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag', 'fib']
> --
> 2.25.1
>
  
Medvedkin, Vladimir April 16, 2025, 9:34 a.m. UTC | #2
Hi Ankur,

On 15/04/2025 13:10, Ankur Dwivedi wrote:
> Adds a lookup FIB node for IP4.
>
> Signed-off-by: Ankur Dwivedi <adwivedi@marvell.com>
> ---
>   lib/node/ip4_lookup_fib.c | 127 ++++++++++++++++++++++++++++++++++++++
>   lib/node/meson.build      |   3 +-
>   2 files changed, 129 insertions(+), 1 deletion(-)
>   create mode 100644 lib/node/ip4_lookup_fib.c
>
> diff --git a/lib/node/ip4_lookup_fib.c b/lib/node/ip4_lookup_fib.c
> new file mode 100644
> index 0000000000..9c71610718
> --- /dev/null
> +++ b/lib/node/ip4_lookup_fib.c
> @@ -0,0 +1,127 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(C) 2025 Marvell.
> + */
> +
> +#include <rte_errno.h>
> +#include <rte_ether.h>
> +#include <rte_fib.h>
> +#include <rte_graph.h>
> +#include <rte_graph_worker.h>
> +#include <rte_ip.h>
> +
> +#include "rte_node_ip4_api.h"
> +
> +#include "node_private.h"
> +
> +/* IP4 Lookup global data struct */
> +struct ip4_lookup_fib_node_main {
> +	struct rte_fib *fib[RTE_MAX_NUMA_NODES];
> +};
> +
> +struct ip4_lookup_fib_node_ctx {
> +	/* Socket's FIB */
> +	struct rte_fib *fib;
> +	/* Dynamic offset to mbuf priv1 */
> +	int mbuf_priv1_off;
> +};
> +
> +static struct ip4_lookup_fib_node_main ip4_lookup_fib_nm;
> +
> +#define FIB_MAX_ROUTES (1 << 16)
why only 64k routes? Modern BGP full view has about 1M prefixes
> +#define FIB_NUM_TBL8   (1 << 15)
> +#define FIB_DEFAULT_NH 999
why this particular value? It is ok to use magic values in examples, but 
not for libs. Consider something like 0 or UINT{8,16,32,64}MAX or some 
meaningful value within graph infra
> +
> +#define IP4_LOOKUP_NODE_FIB(ctx) \
> +	(((struct ip4_lookup_fib_node_ctx *)ctx)->fib)
> +
> +#define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
> +	(((struct ip4_lookup_fib_node_ctx *)ctx)->mbuf_priv1_off)
> +
> +static int
> +setup_fib(unsigned int socket)
> +{
> +	struct ip4_lookup_fib_node_main *nm = &ip4_lookup_fib_nm;
> +	struct rte_fib_conf conf;
> +	char s[RTE_FIB_NAMESIZE];
> +
> +	/* One fib per socket */
> +	if (nm->fib[socket])
> +		return 0;
> +
> +	conf.type = RTE_FIB_DIR24_8;
> +	conf.default_nh = FIB_DEFAULT_NH;
> +	conf.max_routes = FIB_MAX_ROUTES;
> +	conf.rib_ext_sz = 0;
> +	conf.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
> +	conf.dir24_8.num_tbl8 = FIB_NUM_TBL8;
> +	conf.flags = 0;
> +	snprintf(s, sizeof(s), "IPV4_LOOKUP_FIB_%d", socket);
> +	nm->fib[socket] = rte_fib_create(s, socket, &conf);
> +	if (nm->fib[socket] == NULL)
> +		return -rte_errno;
> +
> +	return 0;
> +}
> +
> +static int
> +ip4_lookup_fib_node_init(const struct rte_graph *graph, struct rte_node *node)
> +{
> +	static uint8_t init_once;
> +	unsigned int socket;
> +	uint16_t lcore_id;
> +	int rc;
> +
> +	RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_fib_node_ctx) > RTE_NODE_CTX_SZ);
> +
> +	if (!init_once) {
> +		node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
> +				&node_mbuf_priv1_dynfield_desc);
> +		if (node_mbuf_priv1_dynfield_offset < 0)
> +			return -rte_errno;
> +
> +		/* Setup FIB for all sockets */
> +		RTE_LCORE_FOREACH(lcore_id)
> +		{
> +			socket = rte_lcore_to_socket_id(lcore_id);
> +			rc = setup_fib(socket);
> +			if (rc) {
> +				node_err("ip4_lookup_fib",
> +					 "Failed to setup fib for sock %u, rc=%d",
> +					 socket, rc);
> +				return rc;
> +			}
> +		}
> +		init_once = 1;
> +	}
> +
> +	/* Update socket's FIB and mbuf dyn priv1 offset in node ctx */
> +	IP4_LOOKUP_NODE_FIB(node->ctx) = ip4_lookup_fib_nm.fib[graph->socket];
> +	IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) = node_mbuf_priv1_dynfield_offset;
> +
> +	node_dbg("ip4_lookup_fib", "Initialized ip4_lookup_fib node");
> +
> +	return 0;
> +}
> +
> +static struct rte_node_xstats ip4_lookup_fib_xstats = {
> +	.nb_xstats = 1,
> +	.xstat_desc = {
> +		[0] = "ip4_lookup_fib_error",
> +	},
> +};
> +
> +static struct rte_node_register ip4_lookup_fib_node = {
> +	.name = "ip4_lookup_fib",
> +
> +	.init = ip4_lookup_fib_node_init,
> +	.xstats = &ip4_lookup_fib_xstats,
> +
> +	.nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP + 1,
> +	.next_nodes = {
> +		[RTE_NODE_IP4_LOOKUP_NEXT_IP4_LOCAL] = "ip4_local",
> +		[RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
> +		[RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
> +	},
> +};
> +
> +RTE_NODE_REGISTER(ip4_lookup_fib_node);
> diff --git a/lib/node/meson.build b/lib/node/meson.build
> index 0bed97a96c..d2011c8f56 100644
> --- a/lib/node/meson.build
> +++ b/lib/node/meson.build
> @@ -13,6 +13,7 @@ sources = files(
>           'ethdev_tx.c',
>           'ip4_local.c',
>           'ip4_lookup.c',
> +        'ip4_lookup_fib.c',
>           'ip4_reassembly.c',
>           'ip4_rewrite.c',
>           'ip6_lookup.c',
> @@ -34,4 +35,4 @@ headers = files(
>   
>   # Strict-aliasing rules are violated by uint8_t[] to context size casts.
>   cflags += '-fno-strict-aliasing'
> -deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag']
> +deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag', 'fib']
  
Ankur Dwivedi April 16, 2025, 10:07 a.m. UTC | #3
Hi Vladimir,
>On 15/04/2025 13:10, Ankur Dwivedi wrote:
>> Adds a lookup FIB node for IP4.
>>
>> Signed-off-by: Ankur Dwivedi <adwivedi@marvell.com>
>> ---
>>   lib/node/ip4_lookup_fib.c | 127
>++++++++++++++++++++++++++++++++++++++
>>   lib/node/meson.build      |   3 +-
>>   2 files changed, 129 insertions(+), 1 deletion(-)
>>   create mode 100644 lib/node/ip4_lookup_fib.c
>>
>> diff --git a/lib/node/ip4_lookup_fib.c b/lib/node/ip4_lookup_fib.c new
>> file mode 100644 index 0000000000..9c71610718
>> --- /dev/null
>> +++ b/lib/node/ip4_lookup_fib.c
>> @@ -0,0 +1,127 @@
>> +/* SPDX-License-Identifier: BSD-3-Clause
>> + * Copyright(C) 2025 Marvell.
>> + */
>> +
>> +#include <rte_errno.h>
>> +#include <rte_ether.h>
>> +#include <rte_fib.h>
>> +#include <rte_graph.h>
>> +#include <rte_graph_worker.h>
>> +#include <rte_ip.h>
>> +
>> +#include "rte_node_ip4_api.h"
>> +
>> +#include "node_private.h"
>> +
>> +/* IP4 Lookup global data struct */
>> +struct ip4_lookup_fib_node_main {
>> +	struct rte_fib *fib[RTE_MAX_NUMA_NODES]; };
>> +
>> +struct ip4_lookup_fib_node_ctx {
>> +	/* Socket's FIB */
>> +	struct rte_fib *fib;
>> +	/* Dynamic offset to mbuf priv1 */
>> +	int mbuf_priv1_off;
>> +};
>> +
>> +static struct ip4_lookup_fib_node_main ip4_lookup_fib_nm;
>> +
>> +#define FIB_MAX_ROUTES (1 << 16)
>why only 64k routes? Modern BGP full view has about 1M prefixes
>> +#define FIB_NUM_TBL8   (1 << 15)
>> +#define FIB_DEFAULT_NH 999
>why this particular value? It is ok to use magic values in examples, but not for
>libs. Consider something like 0 or UINT{8,16,32,64}MAX or some meaningful
>value within graph infra
UINT{8,16,32,64}MAX should be fine.
Also, I am thinking about taking the values in fib conf as input from application.
>> +
>> +#define IP4_LOOKUP_NODE_FIB(ctx) \
>> +	(((struct ip4_lookup_fib_node_ctx *)ctx)->fib)
>> +
>> +#define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
>> +	(((struct ip4_lookup_fib_node_ctx *)ctx)->mbuf_priv1_off)
>> +
>> +static int
>> +setup_fib(unsigned int socket)
>> +{
>> +	struct ip4_lookup_fib_node_main *nm = &ip4_lookup_fib_nm;
>> +	struct rte_fib_conf conf;
>> +	char s[RTE_FIB_NAMESIZE];
>> +
>> +	/* One fib per socket */
>> +	if (nm->fib[socket])
>> +		return 0;
>> +
>> +	conf.type = RTE_FIB_DIR24_8;
>> +	conf.default_nh = FIB_DEFAULT_NH;
>> +	conf.max_routes = FIB_MAX_ROUTES;
>> +	conf.rib_ext_sz = 0;
>> +	conf.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
>> +	conf.dir24_8.num_tbl8 = FIB_NUM_TBL8;
>> +	conf.flags = 0;
>> +	snprintf(s, sizeof(s), "IPV4_LOOKUP_FIB_%d", socket);
>> +	nm->fib[socket] = rte_fib_create(s, socket, &conf);
>> +	if (nm->fib[socket] == NULL)
>> +		return -rte_errno;
>> +
>> +	return 0;
>> +}
>> +
>> +static int
>> +ip4_lookup_fib_node_init(const struct rte_graph *graph, struct
>> +rte_node *node) {
>> +	static uint8_t init_once;
>> +	unsigned int socket;
>> +	uint16_t lcore_id;
>> +	int rc;
>> +
>> +	RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_fib_node_ctx) >
>> +RTE_NODE_CTX_SZ);
>> +
>> +	if (!init_once) {
>> +		node_mbuf_priv1_dynfield_offset =
>rte_mbuf_dynfield_register(
>> +				&node_mbuf_priv1_dynfield_desc);
>> +		if (node_mbuf_priv1_dynfield_offset < 0)
>> +			return -rte_errno;
>> +
>> +		/* Setup FIB for all sockets */
>> +		RTE_LCORE_FOREACH(lcore_id)
>> +		{
>> +			socket = rte_lcore_to_socket_id(lcore_id);
>> +			rc = setup_fib(socket);
>> +			if (rc) {
>> +				node_err("ip4_lookup_fib",
>> +					 "Failed to setup fib for sock %u,
>rc=%d",
>> +					 socket, rc);
>> +				return rc;
>> +			}
>> +		}
>> +		init_once = 1;
>> +	}
>> +
>> +	/* Update socket's FIB and mbuf dyn priv1 offset in node ctx */
>> +	IP4_LOOKUP_NODE_FIB(node->ctx) = ip4_lookup_fib_nm.fib[graph-
>>socket];
>> +	IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) =
>> +node_mbuf_priv1_dynfield_offset;
>> +
>> +	node_dbg("ip4_lookup_fib", "Initialized ip4_lookup_fib node");
>> +
>> +	return 0;
>> +}
>> +
>> +static struct rte_node_xstats ip4_lookup_fib_xstats = {
>> +	.nb_xstats = 1,
>> +	.xstat_desc = {
>> +		[0] = "ip4_lookup_fib_error",
>> +	},
>> +};
>> +
>> +static struct rte_node_register ip4_lookup_fib_node = {
>> +	.name = "ip4_lookup_fib",
>> +
>> +	.init = ip4_lookup_fib_node_init,
>> +	.xstats = &ip4_lookup_fib_xstats,
>> +
>> +	.nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP + 1,
>> +	.next_nodes = {
>> +		[RTE_NODE_IP4_LOOKUP_NEXT_IP4_LOCAL] = "ip4_local",
>> +		[RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
>> +		[RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
>> +	},
>> +};
>> +
>> +RTE_NODE_REGISTER(ip4_lookup_fib_node);
>> diff --git a/lib/node/meson.build b/lib/node/meson.build index
>> 0bed97a96c..d2011c8f56 100644
>> --- a/lib/node/meson.build
>> +++ b/lib/node/meson.build
>> @@ -13,6 +13,7 @@ sources = files(
>>           'ethdev_tx.c',
>>           'ip4_local.c',
>>           'ip4_lookup.c',
>> +        'ip4_lookup_fib.c',
>>           'ip4_reassembly.c',
>>           'ip4_rewrite.c',
>>           'ip6_lookup.c',
>> @@ -34,4 +35,4 @@ headers = files(
>>
>>   # Strict-aliasing rules are violated by uint8_t[] to context size casts.
>>   cflags += '-fno-strict-aliasing'
>> -deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev',
>> 'ip_frag']
>> +deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev',
>> +'ip_frag', 'fib']
>
>--
>Regards,
>Vladimir
  
Ankur Dwivedi April 16, 2025, 10:26 a.m. UTC | #4
Hi Nitin,
>>  lib/node/ip4_lookup_fib.c | 127
>++++++++++++++++++++++++++++++++++++++
>>  lib/node/meson.build      |   3 +-
>>  2 files changed, 129 insertions(+), 1 deletion(-)  create mode 100644
>> lib/node/ip4_lookup_fib.c
>>
>> diff --git a/lib/node/ip4_lookup_fib.c b/lib/node/ip4_lookup_fib.c new
>> file mode 100644 index 0000000000..9c71610718
>> --- /dev/null
>> +++ b/lib/node/ip4_lookup_fib.c
>> @@ -0,0 +1,127 @@
>> +/* SPDX-License-Identifier: BSD-3-Clause
>> + * Copyright(C) 2025 Marvell.
>> + */
>> +
>> +#include <rte_errno.h>
>> +#include <rte_ether.h>
>> +#include <rte_fib.h>
>> +#include <rte_graph.h>
>> +#include <rte_graph_worker.h>
>> +#include <rte_ip.h>
>> +
>> +#include "rte_node_ip4_api.h"
>> +
>> +#include "node_private.h"
>> +
>> +/* IP4 Lookup global data struct */
>> +struct ip4_lookup_fib_node_main {
>> +       struct rte_fib *fib[RTE_MAX_NUMA_NODES]; };
>> +
>> +struct ip4_lookup_fib_node_ctx {
>> +       /* Socket's FIB */
>> +       struct rte_fib *fib;
>> +       /* Dynamic offset to mbuf priv1 */
>> +       int mbuf_priv1_off;
>> +};
>> +
>> +static struct ip4_lookup_fib_node_main ip4_lookup_fib_nm;
>> +
>> +#define FIB_MAX_ROUTES (1 << 16)
>> +#define FIB_NUM_TBL8   (1 << 15)
>> +#define FIB_DEFAULT_NH 999
>
>These macros may not be required if we expose public setup_api() with
>arguments. See below
>
>> +
>> +#define IP4_LOOKUP_NODE_FIB(ctx) \
>> +       (((struct ip4_lookup_fib_node_ctx *)ctx)->fib)
>> +
>> +#define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
>> +       (((struct ip4_lookup_fib_node_ctx *)ctx)->mbuf_priv1_off)
>> +
>> +static int
>> +setup_fib(unsigned int socket)
>
>Should we add public API to allow applications to control MAX_ROUTES?
>In a typical stack multiple fibs can be set up for each VRF (~~ port_id).
>
>A public API:
>
>int rte_ip4_lookup_fib_setup(int fib_index, int port_id, uint32_t
>max_routes) where
If a public API is used, then apart from max_routes other config values can also come from application.
>
>For now we can assume fib_index == 0, is global fib table (can be extended to
>VRF later). Socket_id can be determined from port_Id
>
>> +{
>> +       struct ip4_lookup_fib_node_main *nm = &ip4_lookup_fib_nm;
>> +       struct rte_fib_conf conf;
>> +       char s[RTE_FIB_NAMESIZE];
>> +
>> +       /* One fib per socket */
>> +       if (nm->fib[socket])
>> +               return 0;
>> +
>> +       conf.type = RTE_FIB_DIR24_8;
>> +       conf.default_nh = FIB_DEFAULT_NH;
>
>FIB_DEFAULT_NH can be defined in such a way, fast path can decode next_edge
>from return of rte_fib_lookup_bulk() Like union {
>   struct u64;
>   struct {
>       uint32_t next_hop_id;
>       uint16_t  next_edge;
>       uint16_t. reserved;
>   };
>} rte_ip4_lookup_fib_nexthop_t;
>
>FIB_DEFAULT_NH should be set as
>
>rte_ip4_lookup_fib_nexthop_t default_next_hop = {.next_edge =
>IP4_FIB_LOOKUP_NEXT_DROP}
>
>This way in fast path a return from fib_bulk() can be directly used to send
>packet to pkt_drop node
>
>> +       conf.max_routes = FIB_MAX_ROUTES;
>> +       conf.rib_ext_sz = 0;
>> +       conf.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
>> +       conf.dir24_8.num_tbl8 = FIB_NUM_TBL8;
>> +       conf.flags = 0;
>> +       snprintf(s, sizeof(s), "IPV4_LOOKUP_FIB_%d", socket);
>> +       nm->fib[socket] = rte_fib_create(s, socket, &conf);
>> +       if (nm->fib[socket] == NULL)
>> +               return -rte_errno;
>> +
>> +       return 0;
>> +}
>> +
>> +static int
>> +ip4_lookup_fib_node_init(const struct rte_graph *graph, struct
>> +rte_node *node) {
>> +       static uint8_t init_once;
>> +       unsigned int socket;
>> +       uint16_t lcore_id;
>> +       int rc;
>> +
>> +       RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_fib_node_ctx) >
>> + RTE_NODE_CTX_SZ);
>> +
>> +       if (!init_once) {
>> +               node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
>> +                               &node_mbuf_priv1_dynfield_desc);
>> +               if (node_mbuf_priv1_dynfield_offset < 0)
>> +                       return -rte_errno;
>
>You may need to rebase this patch on top of
>https://urldefense.proofpoint.com/v2/url?u=https-
>3A__patches.dpdk.org_project_dpdk_patch_20250409135554.2180390-
>2D2-2Dnsaxena-
>40marvell.com_&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=ILjiNF3GF25y6
>QdHZUxMl6JrStU0MIuCtO5dMzn3Ybk&m=1hUHMoAkw-
>1lUlg8qgybA7IfcnVOlVLxtms8KdhzsGpHV4vAjS2o5n7g-rJ-
>0xLu&s=OAkNegP6hCJA0d3swI7FZ34IzVXE8DN5QHkNj15_S38&e=
>for using global mbuf field
Ok.
>
>> +
>> +               /* Setup FIB for all sockets */
>> +               RTE_LCORE_FOREACH(lcore_id)
>
>Instead can be use rte_socket_count() which allow to loop for socket instead of
>port? Ideally we may need to add fib for virtual interfaces or VRF (later)
Ok.
>
>> +               {
>> +                       socket = rte_lcore_to_socket_id(lcore_id);
>> +                       rc = setup_fib(socket);
>> +                       if (rc) {
>> +                               node_err("ip4_lookup_fib",
>> +                                        "Failed to setup fib for sock %u, rc=%d",
>> +                                        socket, rc);
>> +                               return rc;
>> +                       }
>> +               }
>> +               init_once = 1;
>> +       }
>> +
>> +       /* Update socket's FIB and mbuf dyn priv1 offset in node ctx */
>> +       IP4_LOOKUP_NODE_FIB(node->ctx) = ip4_lookup_fib_nm.fib[graph-
>>socket];
>> +       IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) =
>> + node_mbuf_priv1_dynfield_offset;
>> +
>> +       node_dbg("ip4_lookup_fib", "Initialized ip4_lookup_fib node");
>> +
>> +       return 0;
>> +}
>> +
>> +static struct rte_node_xstats ip4_lookup_fib_xstats = {
>> +       .nb_xstats = 1,
>> +       .xstat_desc = {
>> +               [0] = "ip4_lookup_fib_error",
>> +       },
>> +};
>> +
>> +static struct rte_node_register ip4_lookup_fib_node = {
>> +       .name = "ip4_lookup_fib",
>> +
>> +       .init = ip4_lookup_fib_node_init,
>> +       .xstats = &ip4_lookup_fib_xstats,
>> +
>> +       .nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP + 1,
>> +       .next_nodes = {
>> +               [RTE_NODE_IP4_LOOKUP_NEXT_IP4_LOCAL] = "ip4_local",
>> +               [RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
>> +               [RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
>> +       },
>> +};
>> +
>> +RTE_NODE_REGISTER(ip4_lookup_fib_node);
>> diff --git a/lib/node/meson.build b/lib/node/meson.build index
>> 0bed97a96c..d2011c8f56 100644
>> --- a/lib/node/meson.build
>> +++ b/lib/node/meson.build
>> @@ -13,6 +13,7 @@ sources = files(
>>          'ethdev_tx.c',
>>          'ip4_local.c',
>>          'ip4_lookup.c',
>> +        'ip4_lookup_fib.c',
>>          'ip4_reassembly.c',
>>          'ip4_rewrite.c',
>>          'ip6_lookup.c',
>> @@ -34,4 +35,4 @@ headers = files(
>>
>>  # Strict-aliasing rules are violated by uint8_t[] to context size casts.
>>  cflags += '-fno-strict-aliasing'
>> -deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev',
>> 'ip_frag']
>> +deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev',
>> +'ip_frag', 'fib']
>> --
>> 2.25.1
>>
  

Patch

diff --git a/lib/node/ip4_lookup_fib.c b/lib/node/ip4_lookup_fib.c
new file mode 100644
index 0000000000..9c71610718
--- /dev/null
+++ b/lib/node/ip4_lookup_fib.c
@@ -0,0 +1,127 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2025 Marvell.
+ */
+
+#include <rte_errno.h>
+#include <rte_ether.h>
+#include <rte_fib.h>
+#include <rte_graph.h>
+#include <rte_graph_worker.h>
+#include <rte_ip.h>
+
+#include "rte_node_ip4_api.h"
+
+#include "node_private.h"
+
+/* IP4 Lookup global data struct */
+struct ip4_lookup_fib_node_main {
+	struct rte_fib *fib[RTE_MAX_NUMA_NODES];
+};
+
+struct ip4_lookup_fib_node_ctx {
+	/* Socket's FIB */
+	struct rte_fib *fib;
+	/* Dynamic offset to mbuf priv1 */
+	int mbuf_priv1_off;
+};
+
+static struct ip4_lookup_fib_node_main ip4_lookup_fib_nm;
+
+#define FIB_MAX_ROUTES (1 << 16)
+#define FIB_NUM_TBL8   (1 << 15)
+#define FIB_DEFAULT_NH 999
+
+#define IP4_LOOKUP_NODE_FIB(ctx) \
+	(((struct ip4_lookup_fib_node_ctx *)ctx)->fib)
+
+#define IP4_LOOKUP_NODE_PRIV1_OFF(ctx) \
+	(((struct ip4_lookup_fib_node_ctx *)ctx)->mbuf_priv1_off)
+
+static int
+setup_fib(unsigned int socket)
+{
+	struct ip4_lookup_fib_node_main *nm = &ip4_lookup_fib_nm;
+	struct rte_fib_conf conf;
+	char s[RTE_FIB_NAMESIZE];
+
+	/* One fib per socket */
+	if (nm->fib[socket])
+		return 0;
+
+	conf.type = RTE_FIB_DIR24_8;
+	conf.default_nh = FIB_DEFAULT_NH;
+	conf.max_routes = FIB_MAX_ROUTES;
+	conf.rib_ext_sz = 0;
+	conf.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
+	conf.dir24_8.num_tbl8 = FIB_NUM_TBL8;
+	conf.flags = 0;
+	snprintf(s, sizeof(s), "IPV4_LOOKUP_FIB_%d", socket);
+	nm->fib[socket] = rte_fib_create(s, socket, &conf);
+	if (nm->fib[socket] == NULL)
+		return -rte_errno;
+
+	return 0;
+}
+
+static int
+ip4_lookup_fib_node_init(const struct rte_graph *graph, struct rte_node *node)
+{
+	static uint8_t init_once;
+	unsigned int socket;
+	uint16_t lcore_id;
+	int rc;
+
+	RTE_BUILD_BUG_ON(sizeof(struct ip4_lookup_fib_node_ctx) > RTE_NODE_CTX_SZ);
+
+	if (!init_once) {
+		node_mbuf_priv1_dynfield_offset = rte_mbuf_dynfield_register(
+				&node_mbuf_priv1_dynfield_desc);
+		if (node_mbuf_priv1_dynfield_offset < 0)
+			return -rte_errno;
+
+		/* Setup FIB for all sockets */
+		RTE_LCORE_FOREACH(lcore_id)
+		{
+			socket = rte_lcore_to_socket_id(lcore_id);
+			rc = setup_fib(socket);
+			if (rc) {
+				node_err("ip4_lookup_fib",
+					 "Failed to setup fib for sock %u, rc=%d",
+					 socket, rc);
+				return rc;
+			}
+		}
+		init_once = 1;
+	}
+
+	/* Update socket's FIB and mbuf dyn priv1 offset in node ctx */
+	IP4_LOOKUP_NODE_FIB(node->ctx) = ip4_lookup_fib_nm.fib[graph->socket];
+	IP4_LOOKUP_NODE_PRIV1_OFF(node->ctx) = node_mbuf_priv1_dynfield_offset;
+
+	node_dbg("ip4_lookup_fib", "Initialized ip4_lookup_fib node");
+
+	return 0;
+}
+
+static struct rte_node_xstats ip4_lookup_fib_xstats = {
+	.nb_xstats = 1,
+	.xstat_desc = {
+		[0] = "ip4_lookup_fib_error",
+	},
+};
+
+static struct rte_node_register ip4_lookup_fib_node = {
+	.name = "ip4_lookup_fib",
+
+	.init = ip4_lookup_fib_node_init,
+	.xstats = &ip4_lookup_fib_xstats,
+
+	.nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP + 1,
+	.next_nodes = {
+		[RTE_NODE_IP4_LOOKUP_NEXT_IP4_LOCAL] = "ip4_local",
+		[RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
+		[RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
+	},
+};
+
+RTE_NODE_REGISTER(ip4_lookup_fib_node);
diff --git a/lib/node/meson.build b/lib/node/meson.build
index 0bed97a96c..d2011c8f56 100644
--- a/lib/node/meson.build
+++ b/lib/node/meson.build
@@ -13,6 +13,7 @@  sources = files(
         'ethdev_tx.c',
         'ip4_local.c',
         'ip4_lookup.c',
+        'ip4_lookup_fib.c',
         'ip4_reassembly.c',
         'ip4_rewrite.c',
         'ip6_lookup.c',
@@ -34,4 +35,4 @@  headers = files(
 
 # Strict-aliasing rules are violated by uint8_t[] to context size casts.
 cflags += '-fno-strict-aliasing'
-deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag']
+deps += ['graph', 'mbuf', 'lpm', 'ethdev', 'mempool', 'cryptodev', 'ip_frag', 'fib']