[v4,23/29] node: add ipv4 rewrite and lookup ctrl API

Message ID 20200405085613.1336841-24-jerinj@marvell.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series graph: introduce graph subsystem |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Jerin Jacob Kollanukkaran April 5, 2020, 8:56 a.m. UTC
  From: Nithin Dabilpuram <ndabilpuram@marvell.com>

Add ip4_rewrite and ip4_lookup ctrl API. ip4_lookup ctrl
API is used to add route entries for LPM lookup with
result data containing next hop id and next proto.
ip4_rewrite ctrl API is used to add rewrite data for
every next hop.

Signed-off-by: Nithin Dabilpuram <ndabilpuram@marvell.com>
Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
Signed-off-by: Kiran Kumar K <kirankumark@marvell.com>
---
 lib/librte_node/ethdev_ctrl.c        | 18 ++++++-
 lib/librte_node/ip4_lookup.c         | 80 ++++++++++++++++++++++++++++
 lib/librte_node/ip4_rewrite.c        | 56 +++++++++++++++++++
 lib/librte_node/ip4_rewrite_priv.h   | 22 ++++++++
 lib/librte_node/rte_node_ip4_api.h   | 44 +++++++++++++++
 lib/librte_node/rte_node_version.map |  2 +
 6 files changed, 221 insertions(+), 1 deletion(-)
  

Comments

Andrzej Ostruszka April 9, 2020, 11:04 p.m. UTC | #1
On 4/5/20 10:56 AM, jerinj@marvell.com wrote:
> From: Nithin Dabilpuram <ndabilpuram@marvell.com>
> 
> Add ip4_rewrite and ip4_lookup ctrl API. ip4_lookup ctrl
> API is used to add route entries for LPM lookup with
> result data containing next hop id and next proto.
> ip4_rewrite ctrl API is used to add rewrite data for
> every next hop.
> 
> Signed-off-by: Nithin Dabilpuram <ndabilpuram@marvell.com>
> Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
> Signed-off-by: Kiran Kumar K <kirankumark@marvell.com>
> ---
[...]
> @@ -93,6 +97,18 @@ rte_node_eth_config(struct rte_node_ethdev_config *conf, uint16_t nb_confs,
>  
>  		node_dbg("ethdev", "Tx node %s-%s: is at %u", tx_node->name,
>  			 name, id);
> +
> +		/* Prepare the actual name of the cloned node */
> +		snprintf(name, sizeof(name), "ethdev_tx-%u", port_id);
> +
> +		/* Add this tx port node as next to ip4_rewrite_node */
> +		rte_node_edge_update(ip4_rewrite_node->id, RTE_EDGE_ID_INVALID,
> +				     &next_nodes, 1);

Maybe I've missed something but it looks to me like all uses are of
"append" kind.  Either during initialization (with 0) or here, so maybe
a chance to simplify API?

> +		/* Assuming edge id is the last one alloc'ed */
> +		rc = ip4_rewrite_set_next(
> +			port_id, rte_node_edge_count(ip4_rewrite_node->id) - 1);
> +		if (rc < 0)
> +			return rc;
>  	}
>  
>  	ctrl.nb_graphs = nb_graphs;
> diff --git a/lib/librte_node/ip4_lookup.c b/lib/librte_node/ip4_lookup.c
> index 3a38f5ad8..d10d17879 100644
> --- a/lib/librte_node/ip4_lookup.c
> +++ b/lib/librte_node/ip4_lookup.c
> @@ -28,6 +28,8 @@ struct ip4_lookup_node_main {
>  	struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
>  };
>  
> +static struct ip4_lookup_node_main ip4_lookup_nm;
> +
>  #if defined(RTE_MACHINE_CPUFLAG_NEON)
>  #include "ip4_lookup_neon.h"
>  #elif defined(RTE_ARCH_X86)
> @@ -109,12 +111,90 @@ ip4_lookup_node_process(struct rte_graph *graph, struct rte_node *node,
>  
>  #endif
>  
> +int
> +rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
> +		       enum rte_node_ip4_lookup_next next_node)
> +{
> +	char abuf[INET6_ADDRSTRLEN];
> +	struct in_addr in;
> +	uint8_t socket;
> +	uint32_t val;
> +	int ret;
> +
> +	in.s_addr = htonl(ip);
> +	inet_ntop(AF_INET, &in, abuf, sizeof(abuf));
> +	/* Embedded next node id in next hop */
> +	val = (next_node << 16) | next_hop;

I guess this assumes that the next hop is 32 bits.  I might be
misunderstanding the implementation but it looks to me like it is 24bits
(the docs still say "the user data is 1-byte long" though), at least
this is my impression from struct rte_lpm_tbl_entry.

> +	node_dbg("ip4_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
> +		 depth, val);
> +
> +	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
> +		if (!ip4_lookup_nm.lpm_tbl[socket])
> +			continue;
> +
> +		ret = rte_lpm_add(ip4_lookup_nm.lpm_tbl[socket], ip, depth,
> +				  val);
> +
> +		if (ret < 0) {
> +			node_err("ip4_lookup",
> +				 "Unable to add entry %s / %d nh (%x) to LPM table on sock %d, rc=%d\n",
> +				 abuf, depth, val, socket, ret);
> +			return ret;
> +		}
> +	}
> +
> +	return 0;
> +}

With regards
Andrzej Ostruszka
  
Nithin Dabilpuram April 10, 2020, 7:24 a.m. UTC | #2
On Fri, Apr 10, 2020 at 01:04:37AM +0200, Andrzej Ostruszka wrote:
> On 4/5/20 10:56 AM, jerinj@marvell.com wrote:
> > From: Nithin Dabilpuram <ndabilpuram@marvell.com>
> > 
> > Add ip4_rewrite and ip4_lookup ctrl API. ip4_lookup ctrl
> > API is used to add route entries for LPM lookup with
> > result data containing next hop id and next proto.
> > ip4_rewrite ctrl API is used to add rewrite data for
> > every next hop.
> > 
> > Signed-off-by: Nithin Dabilpuram <ndabilpuram@marvell.com>
> > Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
> > Signed-off-by: Kiran Kumar K <kirankumark@marvell.com>
> > ---
> [...]
> > @@ -93,6 +97,18 @@ rte_node_eth_config(struct rte_node_ethdev_config *conf, uint16_t nb_confs,
> >  
> >  		node_dbg("ethdev", "Tx node %s-%s: is at %u", tx_node->name,
> >  			 name, id);
> > +
> > +		/* Prepare the actual name of the cloned node */
> > +		snprintf(name, sizeof(name), "ethdev_tx-%u", port_id);
> > +
> > +		/* Add this tx port node as next to ip4_rewrite_node */
> > +		rte_node_edge_update(ip4_rewrite_node->id, RTE_EDGE_ID_INVALID,
> > +				     &next_nodes, 1);
> 
> Maybe I've missed something but it looks to me like all uses are of
> "append" kind.  Either during initialization (with 0) or here, so maybe
> a chance to simplify API?
> 
> > +		/* Assuming edge id is the last one alloc'ed */
> > +		rc = ip4_rewrite_set_next(
> > +			port_id, rte_node_edge_count(ip4_rewrite_node->id) - 1);
> > +		if (rc < 0)
> > +			return rc;
> >  	}
> >  
> >  	ctrl.nb_graphs = nb_graphs;
> > diff --git a/lib/librte_node/ip4_lookup.c b/lib/librte_node/ip4_lookup.c
> > index 3a38f5ad8..d10d17879 100644
> > --- a/lib/librte_node/ip4_lookup.c
> > +++ b/lib/librte_node/ip4_lookup.c
> > @@ -28,6 +28,8 @@ struct ip4_lookup_node_main {
> >  	struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
> >  };
> >  
> > +static struct ip4_lookup_node_main ip4_lookup_nm;
> > +
> >  #if defined(RTE_MACHINE_CPUFLAG_NEON)
> >  #include "ip4_lookup_neon.h"
> >  #elif defined(RTE_ARCH_X86)
> > @@ -109,12 +111,90 @@ ip4_lookup_node_process(struct rte_graph *graph, struct rte_node *node,
> >  
> >  #endif
> >  
> > +int
> > +rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
> > +		       enum rte_node_ip4_lookup_next next_node)
> > +{
> > +	char abuf[INET6_ADDRSTRLEN];
> > +	struct in_addr in;
> > +	uint8_t socket;
> > +	uint32_t val;
> > +	int ret;
> > +
> > +	in.s_addr = htonl(ip);
> > +	inet_ntop(AF_INET, &in, abuf, sizeof(abuf));
> > +	/* Embedded next node id in next hop */
> > +	val = (next_node << 16) | next_hop;
> 
> I guess this assumes that the next hop is 32 bits.  I might be
> misunderstanding the implementation but it looks to me like it is 24bits
> (the docs still say "the user data is 1-byte long" though), at least
> this is my impression from struct rte_lpm_tbl_entry.

Agree. I'll mask out upper 8 bits here in next patch. 
Since next_node is enum and is in control of lookup node implementation, 
BIT(23:16) for "next_node" should be sufficient with space for 256 next nodes
for now.

> 
> > +	node_dbg("ip4_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
> > +		 depth, val);
> > +
> > +	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
> > +		if (!ip4_lookup_nm.lpm_tbl[socket])
> > +			continue;
> > +
> > +		ret = rte_lpm_add(ip4_lookup_nm.lpm_tbl[socket], ip, depth,
> > +				  val);
> > +
> > +		if (ret < 0) {
> > +			node_err("ip4_lookup",
> > +				 "Unable to add entry %s / %d nh (%x) to LPM table on sock %d, rc=%d\n",
> > +				 abuf, depth, val, socket, ret);
> > +			return ret;
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> 
> With regards
> Andrzej Ostruszka
>
  

Patch

diff --git a/lib/librte_node/ethdev_ctrl.c b/lib/librte_node/ethdev_ctrl.c
index b2ac5e2c4..845d92987 100644
--- a/lib/librte_node/ethdev_ctrl.c
+++ b/lib/librte_node/ethdev_ctrl.c
@@ -11,6 +11,7 @@ 
 
 #include "ethdev_rx_priv.h"
 #include "ethdev_tx_priv.h"
+#include "ip4_rewrite_priv.h"
 #include "node_private.h"
 
 static struct ethdev_ctrl {
@@ -21,14 +22,17 @@  int
 rte_node_eth_config(struct rte_node_ethdev_config *conf, uint16_t nb_confs,
 		    uint16_t nb_graphs)
 {
+	struct rte_node_register *ip4_rewrite_node;
 	struct ethdev_tx_node_main *tx_node_data;
 	uint16_t tx_q_used, rx_q_used, port_id;
 	struct rte_node_register *tx_node;
 	char name[RTE_NODE_NAMESIZE];
+	const char *next_nodes = name;
 	struct rte_mempool *mp;
+	int i, j, rc;
 	uint32_t id;
-	int i, j;
 
+	ip4_rewrite_node = ip4_rewrite_node_get();
 	tx_node_data = ethdev_tx_node_data_get();
 	tx_node = ethdev_tx_node_get();
 	for (i = 0; i < nb_confs; i++) {
@@ -93,6 +97,18 @@  rte_node_eth_config(struct rte_node_ethdev_config *conf, uint16_t nb_confs,
 
 		node_dbg("ethdev", "Tx node %s-%s: is at %u", tx_node->name,
 			 name, id);
+
+		/* Prepare the actual name of the cloned node */
+		snprintf(name, sizeof(name), "ethdev_tx-%u", port_id);
+
+		/* Add this tx port node as next to ip4_rewrite_node */
+		rte_node_edge_update(ip4_rewrite_node->id, RTE_EDGE_ID_INVALID,
+				     &next_nodes, 1);
+		/* Assuming edge id is the last one alloc'ed */
+		rc = ip4_rewrite_set_next(
+			port_id, rte_node_edge_count(ip4_rewrite_node->id) - 1);
+		if (rc < 0)
+			return rc;
 	}
 
 	ctrl.nb_graphs = nb_graphs;
diff --git a/lib/librte_node/ip4_lookup.c b/lib/librte_node/ip4_lookup.c
index 3a38f5ad8..d10d17879 100644
--- a/lib/librte_node/ip4_lookup.c
+++ b/lib/librte_node/ip4_lookup.c
@@ -28,6 +28,8 @@  struct ip4_lookup_node_main {
 	struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
 };
 
+static struct ip4_lookup_node_main ip4_lookup_nm;
+
 #if defined(RTE_MACHINE_CPUFLAG_NEON)
 #include "ip4_lookup_neon.h"
 #elif defined(RTE_ARCH_X86)
@@ -109,12 +111,90 @@  ip4_lookup_node_process(struct rte_graph *graph, struct rte_node *node,
 
 #endif
 
+int
+rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
+		       enum rte_node_ip4_lookup_next next_node)
+{
+	char abuf[INET6_ADDRSTRLEN];
+	struct in_addr in;
+	uint8_t socket;
+	uint32_t val;
+	int ret;
+
+	in.s_addr = htonl(ip);
+	inet_ntop(AF_INET, &in, abuf, sizeof(abuf));
+	/* Embedded next node id in next hop */
+	val = (next_node << 16) | next_hop;
+	node_dbg("ip4_lookup", "LPM: Adding route %s / %d nh (0x%x)", abuf,
+		 depth, val);
+
+	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
+		if (!ip4_lookup_nm.lpm_tbl[socket])
+			continue;
+
+		ret = rte_lpm_add(ip4_lookup_nm.lpm_tbl[socket], ip, depth,
+				  val);
+
+		if (ret < 0) {
+			node_err("ip4_lookup",
+				 "Unable to add entry %s / %d nh (%x) to LPM table on sock %d, rc=%d\n",
+				 abuf, depth, val, socket, ret);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int
+setup_lpm(struct ip4_lookup_node_main *nm, int socket)
+{
+	struct rte_lpm_config config_ipv4;
+	char s[RTE_LPM_NAMESIZE];
+
+	/* One LPM table per socket */
+	if (nm->lpm_tbl[socket])
+		return 0;
+
+	/* create the LPM table */
+	config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
+	config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
+	config_ipv4.flags = 0;
+	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socket);
+	nm->lpm_tbl[socket] = rte_lpm_create(s, socket, &config_ipv4);
+	if (nm->lpm_tbl[socket] == NULL)
+		return -rte_errno;
+
+	return 0;
+}
+
 static int
 ip4_lookup_node_init(const struct rte_graph *graph, struct rte_node *node)
 {
+	struct rte_lpm **lpm_p = (struct rte_lpm **)&node->ctx;
+	uint16_t socket, lcore_id;
+	static uint8_t init_once;
+	int rc;
+
 	RTE_SET_USED(graph);
 	RTE_SET_USED(node);
 
+	if (!init_once) {
+		/* Setup LPM tables for all sockets */
+		RTE_LCORE_FOREACH(lcore_id)
+		{
+			socket = rte_lcore_to_socket_id(lcore_id);
+			rc = setup_lpm(&ip4_lookup_nm, socket);
+			if (rc) {
+				node_err("ip4_lookup",
+					 "Failed to setup lpm tbl for sock %u, rc=%d",
+					 socket, rc);
+				return rc;
+			}
+		}
+		init_once = 1;
+	}
+	*lpm_p = ip4_lookup_nm.lpm_tbl[graph->socket];
 	node_dbg("ip4_lookup", "Initialized ip4_lookup node");
 
 	return 0;
diff --git a/lib/librte_node/ip4_rewrite.c b/lib/librte_node/ip4_rewrite.c
index ef49ccea0..5663f1eb1 100644
--- a/lib/librte_node/ip4_rewrite.c
+++ b/lib/librte_node/ip4_rewrite.c
@@ -256,6 +256,56 @@  ip4_rewrite_node_init(const struct rte_graph *graph, struct rte_node *node)
 	return 0;
 }
 
+int
+ip4_rewrite_set_next(uint16_t port_id, uint16_t next_index)
+{
+	if (ip4_rewrite_nm == NULL) {
+		ip4_rewrite_nm = rte_zmalloc(
+			"ip4_rewrite", sizeof(struct ip4_rewrite_node_main),
+			RTE_CACHE_LINE_SIZE);
+		if (ip4_rewrite_nm == NULL)
+			return -ENOMEM;
+	}
+	ip4_rewrite_nm->next_index[port_id] = next_index;
+
+	return 0;
+}
+
+int
+rte_node_ip4_rewrite_add(uint16_t next_hop, uint8_t *rewrite_data,
+			 uint8_t rewrite_len, uint16_t dst_port)
+{
+	struct ip4_rewrite_nh_header *nh;
+
+	if (next_hop >= RTE_GRAPH_IP4_REWRITE_MAX_NH)
+		return -EINVAL;
+
+	if (rewrite_len > RTE_GRAPH_IP4_REWRITE_MAX_LEN)
+		return -EINVAL;
+
+	if (ip4_rewrite_nm == NULL) {
+		ip4_rewrite_nm = rte_zmalloc(
+			"ip4_rewrite", sizeof(struct ip4_rewrite_node_main),
+			RTE_CACHE_LINE_SIZE);
+		if (ip4_rewrite_nm == NULL)
+			return -ENOMEM;
+	}
+
+	/* Check if dst port doesn't exist as edge */
+	if (!ip4_rewrite_nm->next_index[dst_port])
+		return -EINVAL;
+
+	/* Update next hop */
+	nh = &ip4_rewrite_nm->nh[next_hop];
+
+	memcpy(nh->rewrite_data, rewrite_data, rewrite_len);
+	nh->tx_node = ip4_rewrite_nm->next_index[dst_port];
+	nh->rewrite_len = rewrite_len;
+	nh->enabled = true;
+
+	return 0;
+}
+
 static struct rte_node_register ip4_rewrite_node = {
 	.process = ip4_rewrite_node_process,
 	.name = "ip4_rewrite",
@@ -267,4 +317,10 @@  static struct rte_node_register ip4_rewrite_node = {
 	.init = ip4_rewrite_node_init,
 };
 
+struct rte_node_register *
+ip4_rewrite_node_get(void)
+{
+	return &ip4_rewrite_node;
+}
+
 RTE_NODE_REGISTER(ip4_rewrite_node);
diff --git a/lib/librte_node/ip4_rewrite_priv.h b/lib/librte_node/ip4_rewrite_priv.h
index 420996a03..80f0abdc9 100644
--- a/lib/librte_node/ip4_rewrite_priv.h
+++ b/lib/librte_node/ip4_rewrite_priv.h
@@ -48,6 +48,28 @@  struct ip4_rewrite_node_main {
 	/**< Next index of each configured port. */
 };
 
+/**
+ * @internal
+ *
+ * Get the ipv4 rewrite node.
+ *
+ * @retrun
+ *   Pointer to the ipv4 rewrite node.
+ */
+struct rte_node_register *ip4_rewrite_node_get(void);
+
+/**
+ * @internal
+ *
+ * Set the Edge index of a given port_id.
+ *
+ * @param port_id
+ *   Ethernet port identifier.
+ * @param next_index
+ *   Edge index of the Given Tx node.
+ */
+int ip4_rewrite_set_next(uint16_t port_id, uint16_t next_index);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/librte_node/rte_node_ip4_api.h b/lib/librte_node/rte_node_ip4_api.h
index 37c12bf82..394cac097 100644
--- a/lib/librte_node/rte_node_ip4_api.h
+++ b/lib/librte_node/rte_node_ip4_api.h
@@ -36,6 +36,50 @@  enum rte_node_ip4_lookup_next {
 	/**< Number of next nodes of lookup node. */
 };
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add ipv4 route to lookup table.
+ *
+ * @param ip
+ *   IP address of route to be added.
+ * @param depth
+ *   Depth of the rule to be added.
+ * @param next_hop
+ *   Next hop id of the rule result to be added.
+ * @param next_node
+ *   Next node to redirect traffic to.
+ *
+ * @return
+ *   0 on success, negative otherwise.
+ */
+__rte_experimental
+int rte_node_ip4_route_add(uint32_t ip, uint8_t depth, uint16_t next_hop,
+			   enum rte_node_ip4_lookup_next next_node);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Add a next hop's rewrite data.
+ *
+ * @param next_hop
+ *   Next hop id to add rewrite data to.
+ * @param rewrite_data
+ *   Rewrite data.
+ * @param rewrite_len
+ *   Length of rewrite data.
+ * @param dst_port
+ *   Destination port to redirect traffic to.
+ *
+ * @return
+ *   0 on success, negative otherwise.
+ */
+__rte_experimental
+int rte_node_ip4_rewrite_add(uint16_t next_hop, uint8_t *rewrite_data,
+			     uint8_t rewrite_len, uint16_t dst_port);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/librte_node/rte_node_version.map b/lib/librte_node/rte_node_version.map
index c6c71bd02..a799b0d38 100644
--- a/lib/librte_node/rte_node_version.map
+++ b/lib/librte_node/rte_node_version.map
@@ -2,6 +2,8 @@  EXPERIMENTAL {
 	global:
 
 	rte_node_eth_config;
+	rte_node_ip4_route_add;
+	rte_node_ip4_rewrite_add;
 	rte_node_logtype;
 	local: *;
 };