Skip to contentSkip to conversation

introduce encap hash calculation

PATCH Superseded Ori Kam 4 participants 31 messages Jan 28, 2024 – Feb 13, 2024 Network drivers

lore
Share & tools
lore

Ori Kam

author
#0 lore raw

cover introduce encap hash calculation

This patch set adds the support for encap hash calculation.

It is based on RFC:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/

Hamdan Igbaria (1):
net/mlx5/hws: introduce encap entropy hash calculation API

Ori Kam (3):
ethdev: introduce encap hash calculation
net/mlx5: add calc encap hash support
app/testpmd: add encap hash calculation

 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++--
 app/test-pmd/config.c                       | 30 ++++++++
 app/test-pmd/testpmd.h                      |  3 +
 doc/guides/prog_guide/rte_flow.rst          | 22 ++++++
 doc/guides/rel_notes/release_24_03.rst      |  4 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++-
 drivers/common/mlx5/mlx5_prm.h              |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h               | 38 ++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c           | 23 ++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h           |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c         | 78 +++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h         |  5 ++
 drivers/net/mlx5/mlx5_flow.c                | 29 ++++++++
 drivers/net/mlx5/mlx5_flow.h                |  8 +++
 drivers/net/mlx5/mlx5_flow_hw.c             | 66 +++++++++++++++++
 lib/ethdev/rte_flow.c                       | 25 +++++++
 lib/ethdev/rte_flow.h                       | 50 +++++++++++++
 lib/ethdev/rte_flow_driver.h                |  5 ++
 lib/ethdev/version.map                      |  1 +
 19 files changed, 470 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 ethdev: introduce encap hash calculation

During the encapsulation of a packet, it is expected to calculate the hash value which is based on the original packet (the outer values, which will become the inner values).

The tunnel protocol defines which tunnel field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets and then decides to offload the flow. As a result, there are two different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

This patch gives the SW a way to query the hash value for a given packet as if the packet was passed through the HW.

---
 doc/guides/prog_guide/rte_flow.rst     | 22 ++++++++++++
 doc/guides/rel_notes/release_24_03.rst |  4 +++
 lib/ethdev/rte_flow.c                  | 25 +++++++++++++
 lib/ethdev/rte_flow.h                  | 50 ++++++++++++++++++++++++++
 lib/ethdev/rte_flow_driver.h           |  5 +++
 lib/ethdev/version.map                 |  1 +
 6 files changed, 107 insertions(+)
doc/guides/prog_guide/rte_flow.rst +22 -0
index 900fdaefb6..0435dda3c7 100644
@@ -4211,6 +4211,28 @@ as it would be calculated in the HW.
                             uint8_t pattern_template_index,
 			                   uint32_t *hash, struct rte_flow_error *error);
 
+Calculate encap hash
+~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet in SW as it would be calculated in HW for the encap action
+
+When the HW execute an encapsulation action, it may calculate an hash value which is based
+on the original packet. This hash is stored depending on the encapsulation protocol, in one
+of the outer fields.
+
+This function allows the application to calculate the hash for a given packet as if the
+encapsulation was done in HW.
+
+.. code-block:: c
+
+   int
+   rte_flow_calc_encap_hash(uint16_t port_id,
+                            const struct rte_flow_item pattern[],
+			                   enum rte_flow_encap_hash_field dest_field,
+                            uint8_t hash_len,
+                            uint8_t *hash,
+			                   struct rte_flow_error *error);
+
 .. _flow_isolated_mode:
 
 Flow isolated mode
doc/guides/rel_notes/release_24_03.rst +4 -0
index 5e545da867..80af380172 100644
@@ -75,6 +75,10 @@ New Features
   * Added support for VXLAN-GPE matching in HW Steering flow engine
     (``dv_flow_en`` = 2).
 
+* **Added ability to calculate the encap hash as done by HW.**
+
+  * Added function that calculates the encap hash, as the HW calculates it:
+    ``rte_flow_calc_encap_hash()``
 
 Removed Items
 -------------
lib/ethdev/rte_flow.c +25 -0
index 3f58d792f9..7fce754be1 100644
@@ -2482,3 +2482,28 @@ rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table
 					hash, error);
 	return flow_err(port_id, ret, error);
 }
+
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT && hash_len != 2) ||
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID && hash_len != 1))
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash len doesn't match the requested field len");
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +50 -0

56 lines not on this page · raw message

lib/ethdev/rte_flow_driver.h +5 -0
index f35f659503..447163655a 100644
@@ -370,6 +370,11 @@ struct rte_flow_ops {
 		(struct rte_eth_dev *dev, const struct rte_flow_template_table *table,
 		 const struct rte_flow_item pattern[], uint8_t pattern_template_index,
 		 uint32_t *hash, struct rte_flow_error *error);
+	/** @see rte_flow_calc_encap_hash() */
+	int (*flow_calc_encap_hash)
+		(struct rte_eth_dev *dev, const struct rte_flow_item pattern[],
+		 enum rte_flow_encap_hash_field dest_field, uint8_t *hash,
+		 struct rte_flow_error *error);
 };
 
 /**
lib/ethdev/version.map +1 -0
index a050baab0f..360898d067 100644
@@ -319,6 +319,7 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_eth_find_rss_algo;
+	rte_flow_calc_encap_hash;
 };
 
 INTERNAL {
Signature · 1 line
2.34.1
author
#2 lore raw

RE: [PATCH 1/4] ethdev: introduce encap hash calculation

Hi Ferruh,

Can you please review my patch?

Thanks,
Ori

Quoted 232 lines

-----Original Message-----
From: Ori Kam <[email protected]>
Sent: Sunday, January 28, 2024 11:40 AM
To: Dariusz Sosnowski <[email protected]>; [email protected];

During the encapsulation of a packet, it is expected to calculate the hash value which is based on the original packet (the outer values, which will become the inner values).

The tunnel protocol defines which tunnel field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets and then decides to offload the flow. As a result, there are two different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

This patch gives the SW a way to query the hash value for a given packet as if the packet was passed through the HW.

---
 doc/guides/prog_guide/rte_flow.rst     | 22 ++++++++++++
 doc/guides/rel_notes/release_24_03.rst |  4 +++
 lib/ethdev/rte_flow.c                  | 25 +++++++++++++
 lib/ethdev/rte_flow.h                  | 50 ++++++++++++++++++++++++++
 lib/ethdev/rte_flow_driver.h           |  5 +++
 lib/ethdev/version.map                 |  1 +
 6 files changed, 107 insertions(+)
doc/guides/prog_guide/rte_flow.rst +4 -0
index 900fdaefb6..0435dda3c7 100644
@@ -4211,6 +4211,28 @@ as it would be calculated in the HW.
                             uint8_t pattern_template_index,
 			                   uint32_t *hash, struct rte_flow_error
 *error);
 
+Calculate encap hash
+~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet in SW as it would be calculated in HW for the

encap action

doc/guides/prog_guide/rte_flow.rst +2 -0
+
+When the HW execute an encapsulation action, it may calculate an hash

value which is based
+on the original packet. This hash is stored depending on the encapsulation protocol, in one

doc/guides/prog_guide/rte_flow.rst +3 -0
+of the outer fields.
+
+This function allows the application to calculate the hash for a given packet

as if the

doc/guides/prog_guide/rte_flow.rst +8 -0
+encapsulation was done in HW.
+
+.. code-block:: c
+
+   int
+   rte_flow_calc_encap_hash(uint16_t port_id,
+                            const struct rte_flow_item pattern[],
+			                   enum rte_flow_encap_hash_field

dest_field,

doc/guides/prog_guide/rte_flow.rst +4 -0
+                            uint8_t hash_len,
+                            uint8_t *hash,
+			                   struct rte_flow_error *error);
+
 .. _flow_isolated_mode:
 
 Flow isolated mode
doc/guides/rel_notes/release_24_03.rst +4 -0
index 5e545da867..80af380172 100644
@@ -75,6 +75,10 @@ New Features
   * Added support for VXLAN-GPE matching in HW Steering flow engine
     (``dv_flow_en`` = 2).
 
+* **Added ability to calculate the encap hash as done by HW.**
+
+  * Added function that calculates the encap hash, as the HW calculates it:
+    ``rte_flow_calc_encap_hash()``
 
 Removed Items
 -------------
lib/ethdev/rte_flow.c +0 -0
index 3f58d792f9..7fce754be1 100644
@@ -2482,3 +2482,28 @@ rte_flow_calc_table_hash(uint16_t port_id, const

struct rte_flow_template_table

lib/ethdev/rte_flow.c +3 -0
 					hash, error);
 	return flow_err(port_id, ret, error);
 }
+
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

lib/ethdev/rte_flow.c +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

lib/ethdev/rte_flow.c +2 -0
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT &&

hash_len != 2) ||

lib/ethdev/rte_flow.c +1 -0
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

&& hash_len != 1))

lib/ethdev/rte_flow.c +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

lib/ethdev/rte_flow.c +1 -0
+					  "hash len doesn't match the

requested field len");

lib/ethdev/rte_flow.c +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash,

error);

lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..ffbde58245 100644
@@ -6783,6 +6783,56 @@ rte_flow_calc_table_hash(uint16_t port_id, const

struct rte_flow_template_table
const struct rte_flow_item pattern[], uint8_t
pattern_template_index,

lib/ethdev/rte_flow.h +20 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is used.
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Simulates HW hash calculation that is done when encap action is being

used.
+ *
+ * @param[in] port_id
+ * Port identifier of Ethernet device.
+ * @param[in] pattern
+ * The values to be used in the hash calculation.
+ * @param[in] dest_field
+ * Type of destination field for hash calculation.
+ * @param[in] hash_len
+ * The length of the hash pointer in bytes. Should be according to
encap_hash_field.
+ * @param[out] hash

+ *   Used to return the calculated hash. It will be written in network order,
+ *   so hash[0] is the MSB.
+ *   The number of bytes is based on the destination field type.

+ * @param[out] error
+ * Perform verbose error reporting if not NULL.
+ * PMDs initialize this structure in case of error only.
+ *
+ * @return

+ *   - (0) if success.
+ *   - (-ENODEV) if *port_id* invalid.
+ *   - (-ENOTSUP) if underlying device does not support this functionality.
+ *   - (-EINVAL) if *pattern* doesn't hold enough information to calculate the

hash
+ * or the dest is not supported.
+ */

lib/ethdev/rte_flow.h +3 -0
+__rte_experimental
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

lib/ethdev/rte_flow.h +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.h +2 -0
+			 uint8_t *hash, struct rte_flow_error *error);
+
 #ifdef __cplusplus
 }
 #endif
lib/ethdev/rte_flow_driver.h +5 -0
index f35f659503..447163655a 100644
@@ -370,6 +370,11 @@ struct rte_flow_ops {
 		(struct rte_eth_dev *dev, const struct
 rte_flow_template_table *table,
 		 const struct rte_flow_item pattern[], uint8_t
 pattern_template_index,
 		 uint32_t *hash, struct rte_flow_error *error);
+	/** @see rte_flow_calc_encap_hash() */
+	int (*flow_calc_encap_hash)
+		(struct rte_eth_dev *dev, const struct rte_flow_item
 pattern[],
+		 enum rte_flow_encap_hash_field dest_field, uint8_t *hash,
+		 struct rte_flow_error *error);

};

/**

lib/ethdev/version.map +1 -1
index a050baab0f..360898d067 100644
@@ -319,6 +319,7 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_eth_find_rss_algo;
+	rte_flow_calc_encap_hash;
 };
 
 INTERNAL {
--

2.34.1

#3 lore raw

Re: [PATCH 1/4] ethdev: introduce encap hash calculation

In reply to Ori Kam

28/01/2024 10:39, Ori Kam:

Quoted 3 lines

During the encapsulation of a packet, it is expected to calculate the hash value which is based on the original packet (the outer values, which will become the inner values).

It is not clear what the hash is for.

Quoted 2 lines

The tunnel protocol defines which tunnel field should hold this hash, but it doesn't define the hash calculation algorithm.

If the hash is stored in the packet header, I expect it to be reproducible when being checked. How the algorithm may be undefined?

Quoted 13 lines

An application that uses flow offloads gets the first few packets and then decides to offload the flow. As a result, there are two different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

This patch gives the SW a way to query the hash value for a given packet as if the packet was passed through the HW.

---

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW. · line 4217

Quoted 4 lines
doc/guides/prog_guide/rte_flow.rst +4 -0
+Calculate encap hash
+~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet in SW as it would be calculated in HW for the encap action

We should give the real full name of the flow action.

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW. · line 4221

Quoted 4 lines
doc/guides/prog_guide/rte_flow.rst +4 -0
+
+When the HW execute an encapsulation action, it may calculate an hash value which is based
+on the original packet. This hash is stored depending on the encapsulation protocol, in one
+of the outer fields.

Give an example of such encapsulation protocol?

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW. · line 4234

Quoted 12 lines
doc/guides/prog_guide/rte_flow.rst +12 -0
+This function allows the application to calculate the hash for a given packet as if the
+encapsulation was done in HW.
+
+.. code-block:: c
+
+   int
+   rte_flow_calc_encap_hash(uint16_t port_id,
+                            const struct rte_flow_item pattern[],
+			                   enum rte_flow_encap_hash_field dest_field,
+                            uint8_t hash_len,
+                            uint8_t *hash,
+			                   struct rte_flow_error *error);

I don't think we should add the complete prototype in this guide.

[...]

Quoted 5 lines

+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Simulates HW hash calculation that is done when encap action is being used.

s/Simulates/Simulate/

Quoted 29 lines

+ *
+ * @param[in] port_id
+ * Port identifier of Ethernet device.
+ * @param[in] pattern
+ * The values to be used in the hash calculation.
+ * @param[in] dest_field
+ * Type of destination field for hash calculation.
+ * @param[in] hash_len
+ * The length of the hash pointer in bytes. Should be according to encap_hash_field.
+ * @param[out] hash

+ *   Used to return the calculated hash. It will be written in network order,
+ *   so hash[0] is the MSB.
+ *   The number of bytes is based on the destination field type.

+ * @param[out] error
+ * Perform verbose error reporting if not NULL.
+ * PMDs initialize this structure in case of error only.
+ *
+ * @return

+ *   - (0) if success.
+ *   - (-ENODEV) if *port_id* invalid.
+ *   - (-ENOTSUP) if underlying device does not support this functionality.
+ *   - (-EINVAL) if *pattern* doesn't hold enough information to calculate the hash

+ * or the dest is not supported.
+ */

lib/ethdev/rte_flow.h +5 -0
+__rte_experimental
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error);
author
#4 lore raw

RE: [PATCH 1/4] ethdev: introduce encap hash calculation

Hi Thomas,

Quoted 10 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Wednesday, February 7, 2024 12:40 AM

28/01/2024 10:39, Ori Kam:

Quoted 3 lines

During the encapsulation of a packet, it is expected to calculate the hash value which is based on the original packet (the outer values, which will become the inner values).

It is not clear what the hash is for.

Will add explanation.

Quoted 8 lines
Quoted 3 lines

The tunnel protocol defines which tunnel field should hold this hash, but it doesn't define the hash calculation algorithm.

If the hash is stored in the packet header, I expect it to be reproducible when being checked. How the algorithm may be undefined?

The hash is not being checked it is used for hash the packets to different queues. the actual value is not important. It is critical that all packets that belongs to the same flow will have the same hash value.

Quoted 21 lines
Quoted 13 lines

An application that uses flow offloads gets the first few packets and then decides to offload the flow. As a result, there are two different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

This patch gives the SW a way to query the hash value for a given packet as if the packet was passed through the HW.

---

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW.

Quoted 4 lines
doc/guides/prog_guide/rte_flow.rst +4 -0
+Calculate encap hash
+~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet in SW as it would be calculated in HW for the

encap action

We should give the real full name of the flow action.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+When the HW execute an encapsulation action, it may calculate an hash

value which is based

Quoted 1 line

+on the original packet. This hash is stored depending on the encapsulation

protocol, in one

Quoted 1 line

+of the outer fields.

Give an example of such encapsulation protocol?

Sure,
Just to be clear something like this?
When the HW execute an encapsulation action for example for VXLAN tunnel,, it may ...

Quoted 12 lines
Quoted 2 lines

+This function allows the application to calculate the hash for a given

packet as if the

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW.

Quoted 8 lines
doc/guides/prog_guide/rte_flow.rst +8 -0
+encapsulation was done in HW.
+
+.. code-block:: c
+
+   int
+   rte_flow_calc_encap_hash(uint16_t port_id,
+                            const struct rte_flow_item pattern[],
+			                   enum rte_flow_encap_hash_field

dest_field,

Quoted 5 lines

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW. · line 4234

Quoted 3 lines
doc/guides/prog_guide/rte_flow.rst +3 -0
+                            uint8_t hash_len,
+                            uint8_t *hash,
+			                   struct rte_flow_error *error);

I don't think we should add the complete prototype in this guide.

Agree, will remove.

Quoted 10 lines

[...]

Quoted 5 lines

+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Simulates HW hash calculation that is done when encap action is being

used.

s/Simulates/Simulate/

Will fix.

Quoted 36 lines
Quoted 10 lines

+ *
+ * @param[in] port_id
+ * Port identifier of Ethernet device.
+ * @param[in] pattern
+ * The values to be used in the hash calculation.
+ * @param[in] dest_field
+ * Type of destination field for hash calculation.
+ * @param[in] hash_len
+ * The length of the hash pointer in bytes. Should be according to

encap_hash_field.

Quoted 9 lines

+ * @param[out] hash

+ *   Used to return the calculated hash. It will be written in network order,
+ *   so hash[0] is the MSB.
+ *   The number of bytes is based on the destination field type.

+ * @param[out] error
+ * Perform verbose error reporting if not NULL.
+ * PMDs initialize this structure in case of error only.
+ *
+ * @return

Quoted 4 lines
+ *   - (0) if success.
+ *   - (-ENODEV) if *port_id* invalid.
+ *   - (-ENOTSUP) if underlying device does not support this functionality.
+ *   - (-EINVAL) if *pattern* doesn't hold enough information to calculate

the hash

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 5 lines

+ * or the dest is not supported.
+ */

lib/ethdev/rte_flow.h +3 -0
+__rte_experimental
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

Quoted 1 line
lib/ethdev/rte_flow.h +1 -0
+			 uint8_t *hash, struct rte_flow_error *error);
#5 lore raw

Re: [PATCH 1/4] ethdev: introduce encap hash calculation

07/02/2024 07:56, Ori Kam:

Quoted 19 lines

Hi Thomas,

Quoted 10 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Wednesday, February 7, 2024 12:40 AM

28/01/2024 10:39, Ori Kam:

Quoted 3 lines

During the encapsulation of a packet, it is expected to calculate the hash value which is based on the original packet (the outer values, which will become the inner values).

It is not clear what the hash is for.

Will add explanation.

Quoted 4 lines
Quoted 3 lines

The tunnel protocol defines which tunnel field should hold this hash, but it doesn't define the hash calculation algorithm.

Quoted 7 lines
Quoted 4 lines

If the hash is stored in the packet header, I expect it to be reproducible when being checked. How the algorithm may be undefined?

The hash is not being checked it is used for hash the packets to different queues. the actual value is not important. It is critical that all packets that belongs to the same flow will have the same hash value.

That's the missing explanation.
Please describe you are talking about an internal hash used for distributing packet in queues.
You should also explain how it differs from RSS.

Quoted 25 lines
Quoted 25 lines
Quoted 13 lines

An application that uses flow offloads gets the first few packets and then decides to offload the flow. As a result, there are two different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

This patch gives the SW a way to query the hash value for a given packet as if the packet was passed through the HW.

---

doc/guides/prog_guide/rte_flow.rst as it would be calculated in the HW.

Quoted 4 lines
doc/guides/prog_guide/rte_flow.rst +4 -0
+Calculate encap hash
+~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet in SW as it would be calculated in HW for the

encap action

We should give the real full name of the flow action.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+When the HW execute an encapsulation action, it may calculate an hash

value which is based

Quoted 1 line

+on the original packet. This hash is stored depending on the encapsulation

Quoted 8 lines
Quoted 4 lines

protocol, in one

Quoted 1 line

+of the outer fields.

Give an example of such encapsulation protocol?

Sure,
Just to be clear something like this?
When the HW execute an encapsulation action for example for VXLAN tunnel,, it may ...

I was more thinking about saying which fields are hashed in VXLAN, and more importantly how/when the hash is used.

author
#6 lore raw

PATCH 2/4 net/mlx5/hws: introduce encap entropy hash calculation API

In reply to Ori Kam

From: Hamdan Igbaria <[email protected]>

Add new function for encap entropy hash calculation, the function will check the device capability for the entropy hash type used by the device, and will calculate the entropy hash value of the user passed fields according this type.

---
 drivers/common/mlx5/mlx5_prm.h      |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h       | 38 ++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c   | 23 +++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h   |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c | 78 +++++++++++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h |  5 ++
 6 files changed, 154 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_prm.h +6 -2
index 69404b5ed8..82923d5f3f 100644
@@ -2130,7 +2130,10 @@ struct mlx5_ifc_flow_table_prop_layout_bits {
 struct mlx5_ifc_roce_caps_bits {
 	u8 reserved_0[0x1e];
 	u8 qp_ts_format[0x2];
-	u8 reserved_at_20[0x7e0];
+	u8 reserved_at_20[0xa0];
+	u8 r_roce_max_src_udp_port[0x10];
+	u8 r_roce_min_src_udp_port[0x10];
+	u8 reserved_at_e0[0x720];
 };
 
 struct mlx5_ifc_ft_fields_support_bits {
@@ -2368,7 +2371,8 @@ struct mlx5_ifc_cmd_hca_cap_2_bits {
 	u8 format_select_dw_gtpu_first_ext_dw_0[0x8];
 	u8 generate_wqe_type[0x20];
 	u8 reserved_at_2c0[0x160];
-	u8 reserved_at_420[0x1c];
+	u8 reserved_at_420[0x18];
+	u8 encap_entropy_hash_type[0x4];
 	u8 flow_table_hash_type[0x4];
 	u8 reserved_at_440[0x3c0];
 };
drivers/net/mlx5/hws/mlx5dr.h +38 -0
index d88f73ab57..321b649f8c 100644
@@ -279,6 +279,27 @@ struct mlx5dr_action_dest_attr {
 	} reformat;
 };
 
+union mlx5dr_crc_encap_entropy_hash_ip_field {
+	uint8_t  ipv6_addr[16];
+	struct {
+		uint8_t  reserved[12];
+		rte_be32_t  ipv4_addr;
+	};
+};
+
+struct mlx5dr_crc_encap_entropy_hash_fields {
+	union mlx5dr_crc_encap_entropy_hash_ip_field dst;
+	union mlx5dr_crc_encap_entropy_hash_ip_field src;
+	uint8_t next_protocol;
+	rte_be16_t dst_port;
+	rte_be16_t src_port;
+}__rte_packed;
+
+enum mlx5dr_crc_encap_entropy_hash_size {
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_8,
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16,
+};
+
 /* Open a context used for direct rule insertion using hardware steering.
  * Each context can contain multiple tables of different types.
  *
@@ -845,4 +866,21 @@ int mlx5dr_send_queue_action(struct mlx5dr_context *ctx,
  */
 int mlx5dr_debug_dump(struct mlx5dr_context *ctx, FILE *f);
 
+/* Calculate encap entropy hash value
+ *
+ * @param[in] ctx
+ *	The context to get from it's capabilities the entropy hash type.
+ * @param[in] data
+ *	The fields for the hash calculation.
+ * @param[in] entropy_res
+ *	An array to store the hash value to it.
+ * @param[in] res_size
+ *	The result size.
+ * @return zero on success non zero otherwise.
+ */
+int mlx5dr_crc_encap_entropy_hash_calc(struct mlx5dr_context *ctx,
+				       struct mlx5dr_crc_encap_entropy_hash_fields *data,
+				       uint8_t entropy_res[],
+				       enum mlx5dr_crc_encap_entropy_hash_size res_size);
+
 #endif
drivers/net/mlx5/hws/mlx5dr_cmd.c +23 -0
index 876a47147d..f77b194708 100644
@@ -1103,6 +1103,8 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->ipsec_offload = MLX5_GET(query_hca_cap_out, out,
 				      capability.cmd_hca_cap.ipsec_offload);
 
+	caps->roce = MLX5_GET(query_hca_cap_out, out, capability.cmd_hca_cap.roce);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE_2 |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1158,6 +1160,9 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->flow_table_hash_type = MLX5_GET(query_hca_cap_out, out,
 					      capability.cmd_hca_cap_2.flow_table_hash_type);
 
+	caps->encap_entropy_hash_type = MLX5_GET(query_hca_cap_out, out,
+						 capability.cmd_hca_cap_2.encap_entropy_hash_type);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_NIC_FLOW_TABLE |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1306,6 +1311,24 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 						capability.esw_cap.merged_eswitch);
 	}
 
+	if (caps->roce) {
+		MLX5_SET(query_hca_cap_in, in, op_mod,
+			 MLX5_GET_HCA_CAP_OP_MOD_ROCE |
+			 MLX5_HCA_CAP_OPMOD_GET_CUR);
+
+		ret = mlx5_glue->devx_general_cmd(ctx, in, sizeof(in), out, sizeof(out));
+		if (ret) {
+			DR_LOG(ERR, "Failed to query roce caps");
+			rte_errno = errno;
+			return rte_errno;
+		}
+
+		caps->roce_max_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_max_src_udp_port);
+		caps->roce_min_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_min_src_udp_port);
+	}
+
 	ret = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
 	if (ret) {
 		DR_LOG(ERR, "Failed to query device attributes");
drivers/net/mlx5/hws/mlx5dr_cmd.h +4 -0
index 18c2b07fc8..694231e08f 100644
@@ -246,6 +246,10 @@ struct mlx5dr_cmd_query_caps {
 	uint32_t shared_vhca_id;
 	char fw_ver[64];
 	bool ipsec_offload;
+	uint8_t encap_entropy_hash_type;
+	bool roce;
+	uint16_t roce_max_src_udp_port;
+	uint16_t roce_min_src_udp_port;
 };
 
 int mlx5dr_cmd_destroy_obj(struct mlx5dr_devx_obj *devx_obj);
drivers/net/mlx5/hws/mlx5dr_crc32.c +78 -0

87 lines not on this page · raw message

drivers/net/mlx5/hws/mlx5dr_crc32.h +5 -0
index 9aab9e06ca..75b6009a15 100644
@@ -10,4 +10,9 @@
  */
 uint32_t mlx5dr_crc32_calc(uint8_t *p, size_t len);
 
+/* Standard CRC16 calculation using the crc_tab16 param to indicate
+ * the pre-calculated polynome hash values.
+ */
+uint16_t mlx5dr_crc16_calc(uint8_t *p, size_t len, uint16_t crc_tab16[]);
+
 #endif /* MLX5DR_CRC32_C_ */
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 3/4 net/mlx5: add calc encap hash support

In reply to Ori Kam

This commit adds support for encap hash calculation.

---
 drivers/net/mlx5/mlx5_flow.c    | 29 +++++++++++++++
 drivers/net/mlx5/mlx5_flow.h    |  8 ++++
 drivers/net/mlx5/mlx5_flow_hw.c | 66 +++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
drivers/net/mlx5/mlx5_flow.c +29 -0
index acaf34ce52..d1a7ad9234 100644
@@ -1197,6 +1197,12 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 			  const struct rte_flow_item pattern[],
 			  uint8_t pattern_template_index,
 			  uint32_t *hash, struct rte_flow_error *error);
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error);
 
 static const struct rte_flow_ops mlx5_flow_ops = {
 	.validate = mlx5_flow_validate,
@@ -1253,6 +1259,7 @@ static const struct rte_flow_ops mlx5_flow_ops = {
 	.async_action_list_handle_query_update =
 		mlx5_flow_async_action_list_handle_query_update,
 	.flow_calc_table_hash = mlx5_flow_calc_table_hash,
+	.flow_calc_encap_hash = mlx5_flow_calc_encap_hash,
 };
 
 /* Tunnel information. */
@@ -11121,6 +11128,28 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 					  hash, error);
 }
 
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error)
+{
+	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, NULL);
+	const struct mlx5_flow_driver_ops *fops;
+
+	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "invalid driver type");
+	fops = flow_get_drv_ops(drv_type);
+	if (!fops || !fops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "no calc encap hash handler");
+	return fops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+}
+
 /**
  * Destroy all indirect actions (shared RSS).
  *
drivers/net/mlx5/mlx5_flow.h +8 -0
index fe4f46724b..a04dde9e93 100644
@@ -2109,6 +2109,13 @@ typedef int
 			 const struct rte_flow_item pattern[],
 			 uint8_t pattern_template_index,
 			 uint32_t *hash, struct rte_flow_error *error);
+typedef int
+(*mlx5_flow_calc_encap_hash_t)
+			(struct rte_eth_dev *dev,
+			 const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field,
+			 uint8_t *hash,
+			 struct rte_flow_error *error);
 
 struct mlx5_flow_driver_ops {
 	mlx5_flow_validate_t validate;
@@ -2182,6 +2189,7 @@ struct mlx5_flow_driver_ops {
 	mlx5_flow_async_action_list_handle_query_update_t
 		async_action_list_handle_query_update;
 	mlx5_flow_calc_table_hash_t flow_calc_table_hash;
+	mlx5_flow_calc_encap_hash_t flow_calc_encap_hash;
 };
 
 /* mlx5_flow.c */
drivers/net/mlx5/mlx5_flow_hw.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 4/4 app/testpmd: add encap hash calculation

In reply to Ori Kam

This commits add support for calculating the encap hash.

Command structure:
flow hash {port} encap {target field} pattern {item} [/ {item} [...] ] / end

Example:
calculate hash to be used by VXLAN encapsulation.
flow hash 0 encap hash_field_sport pattern ipv4 dst is 7.7.7.7 src is 8.8.8.8 / udp dst is 5678 src is 1234 / end

---
 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++++++++--
 app/test-pmd/config.c                       | 30 +++++++++++
 app/test-pmd/testpmd.h                      |  3 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++++-
 4 files changed, 106 insertions(+), 5 deletions(-)
app/test-pmd/cmdline_flow.c +53 -4
index 4062879552..a42f07276d 100644
@@ -219,6 +219,10 @@ enum index {
 	HASH_CALC_TABLE,
 	HASH_CALC_PATTERN_INDEX,
 	HASH_CALC_PATTERN,
+	HASH_CALC_ENCAP,
+	HASH_CALC_DEST,
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
 
 	/* Tunnel arguments. */
 	TUNNEL_CREATE,
@@ -1192,6 +1196,8 @@ struct buffer {
 			uint32_t pattern_n;
 			uint32_t actions_n;
 			uint8_t *data;
+			enum rte_flow_encap_hash_field field;
+			uint8_t encap_hash;
 		} vc; /**< Validate/create arguments. */
 		struct {
 			uint64_t *rule;
@@ -2550,6 +2556,18 @@ static const enum index action_represented_port[] = {
 	ZERO,
 };
 
+static const enum index next_hash_subcmd[] = {
+	HASH_CALC_TABLE,
+	HASH_CALC_ENCAP,
+	ZERO,
+};
+
+static const enum index next_hash_encap_dest_subcmd[] = {
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
+	ZERO,
+};
+
 static int parse_set_raw_encap_decap(struct context *, const struct token *,
 				     const char *, unsigned int,
 				     void *, unsigned int);
@@ -3789,7 +3807,7 @@ static const struct token token_list[] = {
 	[HASH] = {
 		.name = "hash",
 		.help = "calculate hash for a given pattern in a given template table",
-		.next = NEXT(NEXT_ENTRY(HASH_CALC_TABLE), NEXT_ENTRY(COMMON_PORT_ID)),
+		.next = NEXT(next_hash_subcmd, NEXT_ENTRY(COMMON_PORT_ID)),
 		.args = ARGS(ARGS_ENTRY(struct buffer, port)),
 		.call = parse_hash,
 	},
@@ -3803,6 +3821,12 @@ static const struct token token_list[] = {
 					args.vc.table_id)),
 		.call = parse_hash,
 	},
+	[HASH_CALC_ENCAP] = {
+		.name = "encap",
+		.help = "calculates encap hash",
+		.next = NEXT(next_hash_encap_dest_subcmd),
+		.call = parse_hash,
+	},
 	[HASH_CALC_PATTERN_INDEX] = {
 		.name = "pattern_template",
 		.help = "specify pattern template id",
@@ -3812,6 +3836,18 @@ static const struct token token_list[] = {
 					args.vc.pat_templ_id)),
 		.call = parse_hash,
 	},
+	[ENCAP_HASH_FIELD_SRC_PORT] = {
+		.name = "hash_field_sport",
+		.help = "the encap hash field is src port",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
+	[ENCAP_HASH_FIELD_GRE_FLOW_ID] = {
+		.name = "hash_field_flow_id",
+		.help = "the encap hash field is NVGRE flow id",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
 	/* Top-level command. */
 	[INDIRECT_ACTION] = {
 		.name = "indirect_action",
@@ -10691,6 +10727,15 @@ parse_hash(struct context *ctx, const struct token *token,
 		ctx->object = out->args.vc.pattern;
 		ctx->objmask = NULL;
 		return len;
+	case HASH_CALC_ENCAP:
+		out->args.vc.encap_hash = 1;
+		return len;
+	case ENCAP_HASH_FIELD_SRC_PORT:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT;
+		return len;
+	case ENCAP_HASH_FIELD_GRE_FLOW_ID:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID;
+		return len;
 	default:
 		return -1;
 	}
@@ -12651,9 +12696,13 @@ cmd_flow_parsed(const struct buffer *in)
 		port_queue_flow_pull(in->port, in->queue);
 		break;
 	case HASH:
-		port_flow_hash_calc(in->port, in->args.vc.table_id,
-				    in->args.vc.pat_templ_id,
-				    in->args.vc.pattern);
+		if (!in->args.vc.encap_hash)
+			port_flow_hash_calc(in->port, in->args.vc.table_id,
+					    in->args.vc.pat_templ_id,
+					    in->args.vc.pattern);
+		else
+			port_flow_hash_calc_encap(in->port, in->args.vc.field,
+						  in->args.vc.pattern);
 		break;
 	case QUEUE_AGED:
 		port_queue_flow_aged(in->port, in->queue,
app/test-pmd/config.c +30 -0
index cad7537bc6..e26b5bd18d 100644
@@ -3339,6 +3339,36 @@ port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 	return 0;
 }
 
+/** Calculate the encap hash result for a given pattern. */
+int
+port_flow_hash_calc_encap(portid_t port_id,
+			  enum rte_flow_encap_hash_field encap_hash_field,
+			  const struct rte_flow_item pattern[])
+{
+	struct rte_flow_error error;
+	int ret = 0;
+	uint16_t hash = 0;
+	uint8_t len = encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT ? 2 : 1;
+
+	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+	    port_id == (portid_t)RTE_PORT_ALL) {
+		printf("Failed to calculate encap hash - not a valid port");
+		return -EINVAL;
+	}
+
+	ret = rte_flow_calc_encap_hash(port_id, pattern, encap_hash_field, len,
+				       (uint8_t *)&hash, &error);
+	if (ret < 0) {
+		printf("Failed to calculate encap hash");
+		return ret;
+	}
+	if (encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT)
+		printf("encap hash result %#x\n", hash);
+	else
+		printf("encap hash result %#x\n", *(uint8_t *)&hash);
+	return 0;
+}
+
 /** Pull queue operation results from the queue. */
 static int
 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
app/test-pmd/testpmd.h +3 -0
index 9b10a9ea1c..e18546c46e 100644
@@ -1016,6 +1016,9 @@ int port_queue_flow_push(portid_t port_id, queueid_t queue_id);
 int port_queue_flow_pull(portid_t port_id, queueid_t queue_id);
 int port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 			uint8_t pattern_template_index, const struct rte_flow_item pattern[]);
+int port_flow_hash_calc_encap(portid_t port_id,
+			      enum rte_flow_encap_hash_field encap_hash_field,
+			      const struct rte_flow_item pattern[]);
 void port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy);
 int port_flow_validate(portid_t port_id,
 		       const struct rte_flow_attr *attr,
doc/guides/testpmd_app_ug/testpmd_funcs.rst +20 -1
index 38ab421547..37a508c967 100644
@@ -3297,7 +3297,7 @@ The usual error message is shown when operations results cannot be pulled::
 Calculating hash
 ~~~~~~~~~~~~~~~~
 
-``flow hash`` calculates the hash for a given pattern.
+``flow hash {port_id} template_table`` calculates the hash for a given pattern.
 It is bound to ``rte_flow_calc_table_hash()``::
 
    flow hash {port_id} template_table {table_id}
@@ -3316,6 +3316,25 @@ Otherwise, it will show an error message of the form::
 This command uses the same pattern items as ``flow create``,
 their format is described in `Creating flow rules`_.
 
+
+Simulate encap hash calculation
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``flow hash {port_id} encap`` adds hash query, that returns the hash value
+that the HW will calculate when encapsulating a packet.
+
+   flow hash {port_id} encap {target field} pattern {item} [/ {item} [...]] / end
+
+If successful, it will show::
+
+   encap hash result #[...]
+
+The value will be shown as uint16_t without endian conversion.
+
+Otherwise it will show an error message of the form::
+
+   Failed to calculate encap hash - [...]
+
 Creating a tunnel stub for offload
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Signature · 1 line
2.34.1
#9 lore raw
In reply to Ori Kam
Quoted 45 lines

-----Original Message-----
From: Ori Kam <[email protected]>
Sent: Sunday, January 28, 2024 10:40
To: Dariusz Sosnowski <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Ori Kam <[email protected]>; Raslan Darawsheh <[email protected]>
Subject: [PATCH 0/4] introduce encap hash calculation

This patch set adds the support for encap hash calculation.

It is based on RFC:
https://patchwork.dpdk.org/project/dpdk/patch/20231210083100.7893-
[email protected]/

Hamdan Igbaria (1):
net/mlx5/hws: introduce encap entropy hash calculation API

Ori Kam (3):
ethdev: introduce encap hash calculation
net/mlx5: add calc encap hash support
app/testpmd: add encap hash calculation

 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++--
 app/test-pmd/config.c                       | 30 ++++++++
 app/test-pmd/testpmd.h                      |  3 +
 doc/guides/prog_guide/rte_flow.rst          | 22 ++++++
 doc/guides/rel_notes/release_24_03.rst      |  4 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++-
 drivers/common/mlx5/mlx5_prm.h              |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h               | 38 ++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c           | 23 ++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h           |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c         | 78 +++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h         |  5 ++
 drivers/net/mlx5/mlx5_flow.c                | 29 ++++++++
 drivers/net/mlx5/mlx5_flow.h                |  8 +++
 drivers/net/mlx5/mlx5_flow_hw.c             | 66 +++++++++++++++++
 lib/ethdev/rte_flow.c                       | 25 +++++++
 lib/ethdev/rte_flow.h                       | 50 +++++++++++++
 lib/ethdev/rte_flow_driver.h                |  5 ++
 lib/ethdev/version.map                      |  1 +
 19 files changed, 470 insertions(+), 7 deletions(-)
Quoted 2 lines

--
2.34.1

Best regards,
Dariusz Sosnowski

author
#10 lore raw

PATCH v2 1/4 ethdev: introduce encap hash calculation

In reply to Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

---
 doc/guides/prog_guide/rte_flow.rst     | 14 +++++++
 doc/guides/rel_notes/release_24_03.rst |  6 ++-
 lib/ethdev/rte_flow.c                  | 25 +++++++++++++
 lib/ethdev/rte_flow.h                  | 51 ++++++++++++++++++++++++++
 lib/ethdev/rte_flow_driver.h           |  5 +++
 lib/ethdev/version.map                 |  1 +
 6 files changed, 101 insertions(+), 1 deletion(-)
doc/guides/prog_guide/rte_flow.rst +14 -0
index 900fdaefb6..d3491a3ac5 100644
@@ -4211,6 +4211,20 @@ as it would be calculated in the HW.
                             uint8_t pattern_template_index,
 			                   uint32_t *hash, struct rte_flow_error *error);
 
+Calculate encapsulation hash
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet as it would be calculated by the HW, when encapsulating
+a packet.
+
+When the HW execute an encapsulation action, for example VXLAN tunnel,
+it may calculate an hash of the packet to be encapsulated.
+This hash is stored in the outer header of the tunnel.
+This allow better spreading of traffic.
+
+This function can be used for packets of a flow that are not offloaded and
+pass through the SW instead of the HW, for example, SYN/FIN packets.
+
 .. _flow_isolated_mode:
 
 Flow isolated mode
doc/guides/rel_notes/release_24_03.rst +5 -1
index 2b91217943..9508dda362 100644
@@ -60,6 +60,11 @@ New Features
   * Added new function ``rte_eth_find_rss_algo`` to get RSS hash
     algorithm by its name.
 
+* **Added hash calculation of an encapsulated packet as done by the HW.**
+
+  Added function to calculate hash when doing tunnel encapsulation:
+  ``rte_flow_calc_encap_hash()``
+
 * **Added flow matching of random value.**
 
   * Added ``RTE_FLOW_ITEM_TYPE_RANDOM`` to match random value.
@@ -91,7 +96,6 @@ New Features
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_CLASS`` flow action.
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_DATA`` flow action.
 
-
 Removed Items
 -------------
 
lib/ethdev/rte_flow.c +25 -0
index 3f58d792f9..7fce754be1 100644
@@ -2482,3 +2482,28 @@ rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table
 					hash, error);
 	return flow_err(port_id, ret, error);
 }
+
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT && hash_len != 2) ||
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID && hash_len != 1))
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash len doesn't match the requested field len");
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +51 -0

57 lines not on this page · raw message

lib/ethdev/rte_flow_driver.h +5 -0
index f35f659503..447163655a 100644
@@ -370,6 +370,11 @@ struct rte_flow_ops {
 		(struct rte_eth_dev *dev, const struct rte_flow_template_table *table,
 		 const struct rte_flow_item pattern[], uint8_t pattern_template_index,
 		 uint32_t *hash, struct rte_flow_error *error);
+	/** @see rte_flow_calc_encap_hash() */
+	int (*flow_calc_encap_hash)
+		(struct rte_eth_dev *dev, const struct rte_flow_item pattern[],
+		 enum rte_flow_encap_hash_field dest_field, uint8_t *hash,
+		 struct rte_flow_error *error);
 };
 
 /**
lib/ethdev/version.map +1 -0
index a050baab0f..360898d067 100644
@@ -319,6 +319,7 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_eth_find_rss_algo;
+	rte_flow_calc_encap_hash;
 };
 
 INTERNAL {
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v2 2/4 net/mlx5/hws: introduce encap entropy hash calculation API

From: Hamdan Igbaria <[email protected]>

Add new function for encap entropy hash calculation, the function will check the device capability for the entropy hash type used by the device, and will calculate the entropy hash value of the user passed fields according this type.

---
 drivers/common/mlx5/mlx5_prm.h      |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h       | 38 ++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c   | 23 +++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h   |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c | 78 +++++++++++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h |  5 ++
 6 files changed, 154 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_prm.h +6 -2
index f64f25dbb7..ea765c1ea7 100644
@@ -2132,7 +2132,10 @@ struct mlx5_ifc_flow_table_prop_layout_bits {
 struct mlx5_ifc_roce_caps_bits {
 	u8 reserved_0[0x1e];
 	u8 qp_ts_format[0x2];
-	u8 reserved_at_20[0x7e0];
+	u8 reserved_at_20[0xa0];
+	u8 r_roce_max_src_udp_port[0x10];
+	u8 r_roce_min_src_udp_port[0x10];
+	u8 reserved_at_e0[0x720];
 };
 
 struct mlx5_ifc_ft_fields_support_bits {
@@ -2370,7 +2373,8 @@ struct mlx5_ifc_cmd_hca_cap_2_bits {
 	u8 format_select_dw_gtpu_first_ext_dw_0[0x8];
 	u8 generate_wqe_type[0x20];
 	u8 reserved_at_2c0[0x160];
-	u8 reserved_at_420[0x1c];
+	u8 reserved_at_420[0x18];
+	u8 encap_entropy_hash_type[0x4];
 	u8 flow_table_hash_type[0x4];
 	u8 reserved_at_440[0x3c0];
 };
drivers/net/mlx5/hws/mlx5dr.h +38 -0
index d88f73ab57..321b649f8c 100644
@@ -279,6 +279,27 @@ struct mlx5dr_action_dest_attr {
 	} reformat;
 };
 
+union mlx5dr_crc_encap_entropy_hash_ip_field {
+	uint8_t  ipv6_addr[16];
+	struct {
+		uint8_t  reserved[12];
+		rte_be32_t  ipv4_addr;
+	};
+};
+
+struct mlx5dr_crc_encap_entropy_hash_fields {
+	union mlx5dr_crc_encap_entropy_hash_ip_field dst;
+	union mlx5dr_crc_encap_entropy_hash_ip_field src;
+	uint8_t next_protocol;
+	rte_be16_t dst_port;
+	rte_be16_t src_port;
+}__rte_packed;
+
+enum mlx5dr_crc_encap_entropy_hash_size {
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_8,
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16,
+};
+
 /* Open a context used for direct rule insertion using hardware steering.
  * Each context can contain multiple tables of different types.
  *
@@ -845,4 +866,21 @@ int mlx5dr_send_queue_action(struct mlx5dr_context *ctx,
  */
 int mlx5dr_debug_dump(struct mlx5dr_context *ctx, FILE *f);
 
+/* Calculate encap entropy hash value
+ *
+ * @param[in] ctx
+ *	The context to get from it's capabilities the entropy hash type.
+ * @param[in] data
+ *	The fields for the hash calculation.
+ * @param[in] entropy_res
+ *	An array to store the hash value to it.
+ * @param[in] res_size
+ *	The result size.
+ * @return zero on success non zero otherwise.
+ */
+int mlx5dr_crc_encap_entropy_hash_calc(struct mlx5dr_context *ctx,
+				       struct mlx5dr_crc_encap_entropy_hash_fields *data,
+				       uint8_t entropy_res[],
+				       enum mlx5dr_crc_encap_entropy_hash_size res_size);
+
 #endif
drivers/net/mlx5/hws/mlx5dr_cmd.c +23 -0
index 876a47147d..f77b194708 100644
@@ -1103,6 +1103,8 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->ipsec_offload = MLX5_GET(query_hca_cap_out, out,
 				      capability.cmd_hca_cap.ipsec_offload);
 
+	caps->roce = MLX5_GET(query_hca_cap_out, out, capability.cmd_hca_cap.roce);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE_2 |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1158,6 +1160,9 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->flow_table_hash_type = MLX5_GET(query_hca_cap_out, out,
 					      capability.cmd_hca_cap_2.flow_table_hash_type);
 
+	caps->encap_entropy_hash_type = MLX5_GET(query_hca_cap_out, out,
+						 capability.cmd_hca_cap_2.encap_entropy_hash_type);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_NIC_FLOW_TABLE |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1306,6 +1311,24 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 						capability.esw_cap.merged_eswitch);
 	}
 
+	if (caps->roce) {
+		MLX5_SET(query_hca_cap_in, in, op_mod,
+			 MLX5_GET_HCA_CAP_OP_MOD_ROCE |
+			 MLX5_HCA_CAP_OPMOD_GET_CUR);
+
+		ret = mlx5_glue->devx_general_cmd(ctx, in, sizeof(in), out, sizeof(out));
+		if (ret) {
+			DR_LOG(ERR, "Failed to query roce caps");
+			rte_errno = errno;
+			return rte_errno;
+		}
+
+		caps->roce_max_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_max_src_udp_port);
+		caps->roce_min_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_min_src_udp_port);
+	}
+
 	ret = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
 	if (ret) {
 		DR_LOG(ERR, "Failed to query device attributes");
drivers/net/mlx5/hws/mlx5dr_cmd.h +4 -0
index 18c2b07fc8..694231e08f 100644
@@ -246,6 +246,10 @@ struct mlx5dr_cmd_query_caps {
 	uint32_t shared_vhca_id;
 	char fw_ver[64];
 	bool ipsec_offload;
+	uint8_t encap_entropy_hash_type;
+	bool roce;
+	uint16_t roce_max_src_udp_port;
+	uint16_t roce_min_src_udp_port;
 };
 
 int mlx5dr_cmd_destroy_obj(struct mlx5dr_devx_obj *devx_obj);
drivers/net/mlx5/hws/mlx5dr_crc32.c +78 -0

87 lines not on this page · raw message

drivers/net/mlx5/hws/mlx5dr_crc32.h +5 -0
index 9aab9e06ca..75b6009a15 100644
@@ -10,4 +10,9 @@
  */
 uint32_t mlx5dr_crc32_calc(uint8_t *p, size_t len);
 
+/* Standard CRC16 calculation using the crc_tab16 param to indicate
+ * the pre-calculated polynome hash values.
+ */
+uint16_t mlx5dr_crc16_calc(uint8_t *p, size_t len, uint16_t crc_tab16[]);
+
 #endif /* MLX5DR_CRC32_C_ */
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH v2 3/4 net/mlx5: add calc encap hash support

In reply to Ori Kam

This commit adds support for encap hash calculation.

---
 drivers/net/mlx5/mlx5_flow.c    | 29 +++++++++++++++
 drivers/net/mlx5/mlx5_flow.h    |  8 ++++
 drivers/net/mlx5/mlx5_flow_hw.c | 66 +++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
drivers/net/mlx5/mlx5_flow.c +29 -0
index 5159e8e773..0fb6b3b374 100644
@@ -1197,6 +1197,12 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 			  const struct rte_flow_item pattern[],
 			  uint8_t pattern_template_index,
 			  uint32_t *hash, struct rte_flow_error *error);
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error);
 
 static const struct rte_flow_ops mlx5_flow_ops = {
 	.validate = mlx5_flow_validate,
@@ -1253,6 +1259,7 @@ static const struct rte_flow_ops mlx5_flow_ops = {
 	.async_action_list_handle_query_update =
 		mlx5_flow_async_action_list_handle_query_update,
 	.flow_calc_table_hash = mlx5_flow_calc_table_hash,
+	.flow_calc_encap_hash = mlx5_flow_calc_encap_hash,
 };
 
 /* Tunnel information. */
@@ -11121,6 +11128,28 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 					  hash, error);
 }
 
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error)
+{
+	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, NULL);
+	const struct mlx5_flow_driver_ops *fops;
+
+	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "invalid driver type");
+	fops = flow_get_drv_ops(drv_type);
+	if (!fops || !fops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "no calc encap hash handler");
+	return fops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+}
+
 /**
  * Destroy all indirect actions (shared RSS).
  *
drivers/net/mlx5/mlx5_flow.h +8 -0
index c9cc942d80..5a8404b9b3 100644
@@ -2195,6 +2195,13 @@ typedef int
 			 const struct rte_flow_item pattern[],
 			 uint8_t pattern_template_index,
 			 uint32_t *hash, struct rte_flow_error *error);
+typedef int
+(*mlx5_flow_calc_encap_hash_t)
+			(struct rte_eth_dev *dev,
+			 const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field,
+			 uint8_t *hash,
+			 struct rte_flow_error *error);
 
 struct mlx5_flow_driver_ops {
 	mlx5_flow_validate_t validate;
@@ -2268,6 +2275,7 @@ struct mlx5_flow_driver_ops {
 	mlx5_flow_async_action_list_handle_query_update_t
 		async_action_list_handle_query_update;
 	mlx5_flow_calc_table_hash_t flow_calc_table_hash;
+	mlx5_flow_calc_encap_hash_t flow_calc_encap_hash;
 };
 
 /* mlx5_flow.c */
drivers/net/mlx5/mlx5_flow_hw.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v2 4/4 app/testpmd: add encap hash calculation

In reply to Ori Kam

This commits add support for calculating the encap hash.

Command structure:
flow hash {port} encap {target field} pattern {item} [/ {item} [...] ] / end

Example:
calculate hash to be used by VXLAN encapsulation.
flow hash 0 encap hash_field_sport pattern ipv4 dst is 7.7.7.7 src is 8.8.8.8 / udp dst is 5678 src is 1234 / end

---
 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++++++++--
 app/test-pmd/config.c                       | 30 +++++++++++
 app/test-pmd/testpmd.h                      |  3 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++++-
 4 files changed, 106 insertions(+), 5 deletions(-)
app/test-pmd/cmdline_flow.c +53 -4
index 4062879552..a42f07276d 100644
@@ -219,6 +219,10 @@ enum index {
 	HASH_CALC_TABLE,
 	HASH_CALC_PATTERN_INDEX,
 	HASH_CALC_PATTERN,
+	HASH_CALC_ENCAP,
+	HASH_CALC_DEST,
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
 
 	/* Tunnel arguments. */
 	TUNNEL_CREATE,
@@ -1192,6 +1196,8 @@ struct buffer {
 			uint32_t pattern_n;
 			uint32_t actions_n;
 			uint8_t *data;
+			enum rte_flow_encap_hash_field field;
+			uint8_t encap_hash;
 		} vc; /**< Validate/create arguments. */
 		struct {
 			uint64_t *rule;
@@ -2550,6 +2556,18 @@ static const enum index action_represented_port[] = {
 	ZERO,
 };
 
+static const enum index next_hash_subcmd[] = {
+	HASH_CALC_TABLE,
+	HASH_CALC_ENCAP,
+	ZERO,
+};
+
+static const enum index next_hash_encap_dest_subcmd[] = {
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
+	ZERO,
+};
+
 static int parse_set_raw_encap_decap(struct context *, const struct token *,
 				     const char *, unsigned int,
 				     void *, unsigned int);
@@ -3789,7 +3807,7 @@ static const struct token token_list[] = {
 	[HASH] = {
 		.name = "hash",
 		.help = "calculate hash for a given pattern in a given template table",
-		.next = NEXT(NEXT_ENTRY(HASH_CALC_TABLE), NEXT_ENTRY(COMMON_PORT_ID)),
+		.next = NEXT(next_hash_subcmd, NEXT_ENTRY(COMMON_PORT_ID)),
 		.args = ARGS(ARGS_ENTRY(struct buffer, port)),
 		.call = parse_hash,
 	},
@@ -3803,6 +3821,12 @@ static const struct token token_list[] = {
 					args.vc.table_id)),
 		.call = parse_hash,
 	},
+	[HASH_CALC_ENCAP] = {
+		.name = "encap",
+		.help = "calculates encap hash",
+		.next = NEXT(next_hash_encap_dest_subcmd),
+		.call = parse_hash,
+	},
 	[HASH_CALC_PATTERN_INDEX] = {
 		.name = "pattern_template",
 		.help = "specify pattern template id",
@@ -3812,6 +3836,18 @@ static const struct token token_list[] = {
 					args.vc.pat_templ_id)),
 		.call = parse_hash,
 	},
+	[ENCAP_HASH_FIELD_SRC_PORT] = {
+		.name = "hash_field_sport",
+		.help = "the encap hash field is src port",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
+	[ENCAP_HASH_FIELD_GRE_FLOW_ID] = {
+		.name = "hash_field_flow_id",
+		.help = "the encap hash field is NVGRE flow id",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
 	/* Top-level command. */
 	[INDIRECT_ACTION] = {
 		.name = "indirect_action",
@@ -10691,6 +10727,15 @@ parse_hash(struct context *ctx, const struct token *token,
 		ctx->object = out->args.vc.pattern;
 		ctx->objmask = NULL;
 		return len;
+	case HASH_CALC_ENCAP:
+		out->args.vc.encap_hash = 1;
+		return len;
+	case ENCAP_HASH_FIELD_SRC_PORT:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT;
+		return len;
+	case ENCAP_HASH_FIELD_GRE_FLOW_ID:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID;
+		return len;
 	default:
 		return -1;
 	}
@@ -12651,9 +12696,13 @@ cmd_flow_parsed(const struct buffer *in)
 		port_queue_flow_pull(in->port, in->queue);
 		break;
 	case HASH:
-		port_flow_hash_calc(in->port, in->args.vc.table_id,
-				    in->args.vc.pat_templ_id,
-				    in->args.vc.pattern);
+		if (!in->args.vc.encap_hash)
+			port_flow_hash_calc(in->port, in->args.vc.table_id,
+					    in->args.vc.pat_templ_id,
+					    in->args.vc.pattern);
+		else
+			port_flow_hash_calc_encap(in->port, in->args.vc.field,
+						  in->args.vc.pattern);
 		break;
 	case QUEUE_AGED:
 		port_queue_flow_aged(in->port, in->queue,
app/test-pmd/config.c +30 -0
index cad7537bc6..e26b5bd18d 100644
@@ -3339,6 +3339,36 @@ port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 	return 0;
 }
 
+/** Calculate the encap hash result for a given pattern. */
+int
+port_flow_hash_calc_encap(portid_t port_id,
+			  enum rte_flow_encap_hash_field encap_hash_field,
+			  const struct rte_flow_item pattern[])
+{
+	struct rte_flow_error error;
+	int ret = 0;
+	uint16_t hash = 0;
+	uint8_t len = encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT ? 2 : 1;
+
+	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+	    port_id == (portid_t)RTE_PORT_ALL) {
+		printf("Failed to calculate encap hash - not a valid port");
+		return -EINVAL;
+	}
+
+	ret = rte_flow_calc_encap_hash(port_id, pattern, encap_hash_field, len,
+				       (uint8_t *)&hash, &error);
+	if (ret < 0) {
+		printf("Failed to calculate encap hash");
+		return ret;
+	}
+	if (encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT)
+		printf("encap hash result %#x\n", hash);
+	else
+		printf("encap hash result %#x\n", *(uint8_t *)&hash);
+	return 0;
+}
+
 /** Pull queue operation results from the queue. */
 static int
 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
app/test-pmd/testpmd.h +3 -0
index 9b10a9ea1c..e18546c46e 100644
@@ -1016,6 +1016,9 @@ int port_queue_flow_push(portid_t port_id, queueid_t queue_id);
 int port_queue_flow_pull(portid_t port_id, queueid_t queue_id);
 int port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 			uint8_t pattern_template_index, const struct rte_flow_item pattern[]);
+int port_flow_hash_calc_encap(portid_t port_id,
+			      enum rte_flow_encap_hash_field encap_hash_field,
+			      const struct rte_flow_item pattern[]);
 void port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy);
 int port_flow_validate(portid_t port_id,
 		       const struct rte_flow_attr *attr,
doc/guides/testpmd_app_ug/testpmd_funcs.rst +20 -1
index 38ab421547..37a508c967 100644
@@ -3297,7 +3297,7 @@ The usual error message is shown when operations results cannot be pulled::
 Calculating hash
 ~~~~~~~~~~~~~~~~
 
-``flow hash`` calculates the hash for a given pattern.
+``flow hash {port_id} template_table`` calculates the hash for a given pattern.
 It is bound to ``rte_flow_calc_table_hash()``::
 
    flow hash {port_id} template_table {table_id}
@@ -3316,6 +3316,25 @@ Otherwise, it will show an error message of the form::
 This command uses the same pattern items as ``flow create``,
 their format is described in `Creating flow rules`_.
 
+
+Simulate encap hash calculation
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``flow hash {port_id} encap`` adds hash query, that returns the hash value
+that the HW will calculate when encapsulating a packet.
+
+   flow hash {port_id} encap {target field} pattern {item} [/ {item} [...]] / end
+
+If successful, it will show::
+
+   encap hash result #[...]
+
+The value will be shown as uint16_t without endian conversion.
+
+Otherwise it will show an error message of the form::
+
+   Failed to calculate encap hash - [...]
+
 Creating a tunnel stub for offload
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH v2 1/4] ethdev: introduce encap hash calculation

In reply to Ori Kam
Quoted 27 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

<...>

Quoted 8 lines
lib/ethdev/rte_flow.c +7 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table · line 2502

Quoted 10 lines
lib/ethdev/rte_flow.c +10 -0
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT && hash_len != 2) ||
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID && hash_len != 1))

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table · line 2507

Quoted 6 lines
lib/ethdev/rte_flow.c +5 -0
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash len doesn't match the requested field len");
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to driver?

Quoted 15 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +5 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table
 			 const struct rte_flow_item pattern[], uint8_t pattern_template_index,
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is used.

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table · line 6800

Quoted 10 lines
lib/ethdev/rte_flow.h +9 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-using 'enum rte_flow_field_id' work?

author
#15 lore raw

RE: [PATCH v2 1/4] ethdev: introduce encap hash calculation

Hi Ferruh,

Quoted 60 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, February 8, 2024 7:13 PM
To: Ori Kam <[email protected]>; Dariusz Sosnowski

Quoted 27 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

<...>

Review

Quoted 2 lines
(quoted diff) +2 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 11 lines
lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 2 lines
(quoted diff) +2 -0
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT &&

hash_len != 2) ||

Review

Quoted 1 line
(quoted diff) +1 -0
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

&& hash_len != 1))

Quoted 1 line
Quoted 8 lines

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

From my original email I think we only need the type, we don't need the size. On the RFC thread there was an objection. So I added the size,
If you think it is not needed lets remove it.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "hash len doesn't match the

requested field len");

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash,

error);

Quoted 1 line

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to driver?

Not sure I understand, like I wrote above this is pure verification from my point of view. The driver knows the size based on the dest.

Quoted 20 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id,

Quoted 9 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t port_id,

const struct rte_flow_template_table

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, · line 6783

Quoted 1 line
lib/ethdev/rte_flow.h +0 -0
 			 const struct rte_flow_item pattern[], uint8_t

pattern_template_index,

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 7 lines
lib/ethdev/rte_flow.h +5 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is

used.

Quoted 14 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 10 lines
lib/ethdev/rte_flow.h +9 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-using 'enum rte_flow_field_id' work?

Since the option are really limited and defined by standard, I prefer to have dedicated options.

Best,
Ori

#16 lore raw

Re: [PATCH v2 1/4] ethdev: introduce encap hash calculation

Quoted 66 lines · Ori Kam

Hi Ferruh,

Quoted 64 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, February 8, 2024 7:13 PM
To: Ori Kam <[email protected]>; Dariusz Sosnowski

Quoted 27 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

<...>

Review

Quoted 2 lines
(quoted diff) +2 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 11 lines
lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 2 lines
(quoted diff) +2 -0
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT &&

hash_len != 2) ||

Review

Quoted 1 line
(quoted diff) +1 -0
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

&& hash_len != 1))

Quoted 1 line

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

Quoted 9 lines
Quoted 4 lines

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

From my original email I think we only need the type, we don't need the size. On the RFC thread there was an objection. So I added the size,
If you think it is not needed lets remove it.

I am not saying length is not needed, but
API gets 'dest_field' & 'hash_len', and according checks in the API for each 'dest_field' there is an exact 'hash_len' requirement, this requirement is something impacts user but this information is embedded in the API, my suggestion is make it more visible to user.

My initial suggestion was put this into an object, like:

```
struct x {
	enum rte_flow_encap_hash_field dest_field;
	size_t expected size;
} y[] = {
	{ RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT, 2 },
	{ RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID, 1 }
};
```

But as you mentioned this is a limited set, perhaps it is sufficient to document size requirement in the "enum rte_flow_encap_hash_field" API doxygen comment.

Quoted 10 lines
Quoted 10 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "hash len doesn't match the

requested field len");

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash,

error);

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to driver?

Not sure I understand, like I wrote above this is pure verification from my point of view. The driver knows the size based on the dest.

My intention was similar to above comment, like dest_field type RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT implies that required size should be 2 bytes, and it seems driver already knows about this requirement.

Instead, it can be possible to verify 'hash_len' in the API level, pass this information to the driver and driver use 'hash_len' directly for its size parameter, so driver will rely on API provided 'hash_len' value instead of storing this information within driver.

Lets assume 10 drivers are implementing this feature, should all of them define MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16 equivalent enum/define withing the driver?

Quoted 19 lines
Quoted 19 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id,

Quoted 9 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t port_id,

const struct rte_flow_template_table

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, · line 6783

Quoted 1 line
lib/ethdev/rte_flow.h +0 -0
 			 const struct rte_flow_item pattern[], uint8_t

pattern_template_index,

Quoted 7 lines
lib/ethdev/rte_flow.h +5 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is
Quoted 7 lines
Quoted 7 lines

used.

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +5 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
 

Just recognized that comments are not doxygen comments.

Quoted 10 lines
Quoted 9 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 5 lines
lib/ethdev/rte_flow.h +4 -0
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-using 'enum rte_flow_field_id' work?

Quoted 2 lines

Since the option are really limited and defined by standard, I prefer to have dedicated options.

OK, my intention is to reduce the duplication. Just for brainstorm, what is the benefit of having 'RTE_FLOW_ENCAP_HASH_' specific enums, if we can present them as generic protocol fiels, like 'RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT' vs 'RTE_FLOW_FIELD_UDP_PORT_SRC,'?

author
#17 lore raw

RE: [PATCH v2 1/4] ethdev: introduce encap hash calculation

Hi Ferruh

Quoted 99 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Monday, February 12, 2024 7:05 PM

Quoted 66 lines · Ori Kam

Hi Ferruh,

Quoted 64 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, February 8, 2024 7:13 PM
To: Ori Kam <[email protected]>; Dariusz Sosnowski

Quoted 27 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

<...>

Review

Quoted 2 lines
(quoted diff) +2 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 11 lines
lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 2 lines
(quoted diff) +2 -0
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT &&

hash_len != 2) ||

Review

Quoted 1 line
(quoted diff) +1 -0
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

&& hash_len != 1))

Quoted 1 line

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

Quoted 6 lines
Quoted 4 lines

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

From my original email I think we only need the type, we don't need the

size.

Quoted 3 lines

On the RFC thread there was an objection. So I added the size, If you think it is not needed lets remove it.

I am not saying length is not needed, but
API gets 'dest_field' & 'hash_len', and according checks in the API for each 'dest_field' there is an exact 'hash_len' requirement, this requirement is something impacts user but this information is embedded in the API, my suggestion is make it more visible to user.

My initial suggestion was put this into an object, like:

```
struct x {
	enum rte_flow_encap_hash_field dest_field;
	size_t expected size;
} y[] = {
	{ RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT, 2 },
	{ RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID, 1 }
};
```
Quoted 3 lines

But as you mentioned this is a limited set, perhaps it is sufficient to document size requirement in the "enum rte_flow_encap_hash_field" API doxygen comment.

Will add it to the doxygen.

Quoted 18 lines
Quoted 13 lines
Quoted 13 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "hash len doesn't match the

requested field len");

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash,

error);

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to driver?

Not sure I understand, like I wrote above this is pure verification from my

Quoted 8 lines

point of view.

Quoted 2 lines

The driver knows the size based on the dest.

My intention was similar to above comment, like dest_field type RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT implies that required size should be
2 bytes, and it seems driver already knows about this requirement.

That is correct, that is why I don't think we need the size, add added it only for validation due to community request.

Quoted 10 lines

Instead, it can be possible to verify 'hash_len' in the API level, pass this information to the driver and driver use 'hash_len' directly for its size parameter, so driver will rely on API provided 'hash_len' value instead of storing this information within driver.

Lets assume 10 drivers are implementing this feature, should all of them define MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16 equivalent enum/define
withing the driver?

No, the driver implements hard-coded logic, which means that it just needs to know the dest field, in order to know what hash to calculate
It is possible that for each field the HW will calculate the hash using different algorithm.

Also it is possible that the HW doesn't support writing to the expected field, in which case we want the driver call to fail.

Field implies size.
Size doesn't implies field.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id,

Quoted 10 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t port_id,

const struct rte_flow_template_table

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, · line 6783

Quoted 1 line
lib/ethdev/rte_flow.h +0 -0
 			 const struct rte_flow_item pattern[], uint8_t

pattern_template_index,

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 7 lines
lib/ethdev/rte_flow.h +5 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is

used.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +5 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
 

Just recognized that comments are not doxygen comments.

Thanks,
Will fix.

Quoted 12 lines
Quoted 11 lines
Quoted 10 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +4 -0
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-using 'enum rte_flow_field_id' work?

Quoted 1 line

Since the option are really limited and defined by standard, I prefer to have

Quoted 8 lines

dedicated options.

Quoted 1 line

OK, my intention is to reduce the duplication. Just for brainstorm, what is the benefit of having 'RTE_FLOW_ENCAP_HASH_' specific enums, if we can present them as generic protocol fiels, like 'RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT' vs 'RTE_FLOW_FIELD_UDP_PORT_SRC,'?

I guess you want to go with 'RTE_FLOW_FIELD_UDP_PORT_SRC
right?
The main issue is since the options are really limited and used for a very dedicated function. When app developers / DPDK developers will look at it, it will be very unclear what is the use of this enum. We already have an enum for fields. Like you suggested we could have used it,
but this will show much more option than there are really.

Best,
Ori

#18 lore raw

Re: [PATCH v2 1/4] ethdev: introduce encap hash calculation

Quoted 143 lines · Ori Kam

Hi Ferruh

Quoted 99 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Monday, February 12, 2024 7:05 PM

Quoted 66 lines · Ori Kam

Hi Ferruh,

Quoted 64 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, February 8, 2024 7:13 PM
To: Ori Kam <[email protected]>; Dariusz Sosnowski

Quoted 27 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

<...>

Review

Quoted 2 lines
(quoted diff) +2 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field, uint8_t

hash_len,

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 11 lines
lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 2 lines
(quoted diff) +2 -0
+					  "calc encap hash is not supported");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT &&

hash_len != 2) ||

Review

Quoted 1 line
(quoted diff) +1 -0
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

&& hash_len != 1))

Quoted 1 line

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

Quoted 6 lines
Quoted 4 lines

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

From my original email I think we only need the type, we don't need the

size.

Quoted 3 lines

On the RFC thread there was an objection. So I added the size, If you think it is not needed lets remove it.

I am not saying length is not needed, but
API gets 'dest_field' & 'hash_len', and according checks in the API for each 'dest_field' there is an exact 'hash_len' requirement, this requirement is something impacts user but this information is embedded in the API, my suggestion is make it more visible to user.

My initial suggestion was put this into an object, like:

```
struct x {
	enum rte_flow_encap_hash_field dest_field;
	size_t expected size;
} y[] = {
	{ RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT, 2 },
	{ RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID, 1 }
};
```
Quoted 3 lines

But as you mentioned this is a limited set, perhaps it is sufficient to document size requirement in the "enum rte_flow_encap_hash_field" API doxygen comment.

Will add it to the doxygen.

Quoted 18 lines
Quoted 13 lines
Quoted 13 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "hash len doesn't match the

requested field len");

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash,

error);

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to driver?

Not sure I understand, like I wrote above this is pure verification from my

Quoted 8 lines

point of view.

Quoted 2 lines

The driver knows the size based on the dest.

My intention was similar to above comment, like dest_field type RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT implies that required size should be
2 bytes, and it seems driver already knows about this requirement.

That is correct, that is why I don't think we need the size, add added it only for validation due to community request.

Quoted 6 lines

Instead, it can be possible to verify 'hash_len' in the API level, pass this information to the driver and driver use 'hash_len' directly for its size parameter, so driver will rely on API provided 'hash_len' value instead of storing this information within driver.

Quoted 9 lines
Quoted 4 lines

Lets assume 10 drivers are implementing this feature, should all of them define MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16 equivalent enum/define
withing the driver?

No, the driver implements hard-coded logic, which means that it just needs to know the dest field, in order to know what hash to calculate
It is possible that for each field the HW will calculate the hash using different algorithm.

OK if HW already needs to know the size in advance, lets go with enum doxygen update only.

Quoted 53 lines

Also it is possible that the HW doesn't support writing to the expected field, in which case we want the driver call to fail.

Field implies size.
Size doesn't implies field.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id,

Quoted 10 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t port_id,

const struct rte_flow_template_table

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, · line 6783

Quoted 1 line
lib/ethdev/rte_flow.h +0 -0
 			 const struct rte_flow_item pattern[], uint8_t

pattern_template_index,

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 7 lines
lib/ethdev/rte_flow.h +5 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action is

used.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +5 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
 

Just recognized that comments are not doxygen comments.

Thanks,
Will fix.

Quoted 15 lines
Quoted 11 lines
Quoted 10 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +4 -0
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-using 'enum rte_flow_field_id' work?

Quoted 1 line

Since the option are really limited and defined by standard, I prefer to have

dedicated options.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

OK, my intention is to reduce the duplication. Just for brainstorm, what is the benefit of having 'RTE_FLOW_ENCAP_HASH_' specific enums, if we can present them as generic protocol fiels, like 'RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT' vs 'RTE_FLOW_FIELD_UDP_PORT_SRC,'?

I guess you want to go with 'RTE_FLOW_FIELD_UDP_PORT_SRC right?

I just want to discuss if redundancy can be eliminated.

Quoted 5 lines

The main issue is since the options are really limited and used for a very dedicated function. When app developers / DPDK developers will look at it, it will be very unclear what is the use of this enum. We already have an enum for fields. Like you suggested we could have used it,
but this will show much more option than there are really.

OK, lets use dedicated enums to clarify to the users the specific fields available for this set of APIs.

Btw, is boundary check like following required for the APIs:

```
if (dest_field > RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID)
	return -EINVAL;
```

In case user pass an invalid value as 'dest_filed'

(Note: I intentionally not used MAX enum something like 'RTE_FLOW_ENCAP_HASH_FIELD_MAX' to not need to deal with ABI issues in the future.)

author
#19 lore raw

RE: [PATCH v2 1/4] ethdev: introduce encap hash calculation

Quoted 261 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Monday, February 12, 2024 10:10 PM

Quoted 32 lines · Ori Kam

Hi Ferruh

Quoted 30 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Monday, February 12, 2024 7:05 PM

Quoted 25 lines · Ori Kam

Hi Ferruh,

Quoted 23 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, February 8, 2024 7:13 PM
To: Ori Kam <[email protected]>; Dariusz Sosnowski

Quoted 17 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the

packet

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

<...>

Review

Quoted 2 lines
(quoted diff) +2 -0
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct

rte_flow_item

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

pattern[],

Review

Quoted 1 line
(quoted diff) +1 -0
+			 enum rte_flow_encap_hash_field dest_field,

uint8_t

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

hash_len,

lib/ethdev/rte_flow.c rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 11 lines
lib/ethdev/rte_flow.c +11 -0
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "calc encap hash is not

supported");

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT

&&

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hash_len != 2) ||

Review

Quoted 1 line
(quoted diff) +1 -0
+	    (dest_field ==

RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID

Quoted 44 lines
Quoted 35 lines
Quoted 7 lines
Quoted 7 lines

&& hash_len != 1))

Quoted 1 line

If there is a fixed mapping with the dest_field and the size, instead of putting this information into check code, what do you think to put it into the data structure?

Quoted 6 lines
Quoted 4 lines

I mean instead of using enum for dest_filed, it can be a struct that is holding enum and its expected size, this clarifies what the expected size for that field.

From my original email I think we only need the type, we don't need the

size.

Quoted 3 lines

On the RFC thread there was an objection. So I added the size, If you think it is not needed lets remove it.

I am not saying length is not needed, but
API gets 'dest_field' & 'hash_len', and according checks in the API for each 'dest_field' there is an exact 'hash_len' requirement, this requirement is something impacts user but this information is embedded in the API, my suggestion is make it more visible to user.

My initial suggestion was put this into an object, like:

```
struct x {
	enum rte_flow_encap_hash_field dest_field;
	size_t expected size;
} y[] = {
	{ RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT, 2 },
	{ RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID, 1 }
};
```
Quoted 3 lines

But as you mentioned this is a limited set, perhaps it is sufficient to document size requirement in the "enum rte_flow_encap_hash_field" API doxygen comment.

Will add it to the doxygen.

Quoted 3 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return rte_flow_error_set(error, EINVAL,
+

RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,

Review

Quoted 1 line
(quoted diff) +1 -0
+					  "hash len doesn't match the

requested field len");

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field,

hash,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

error);

Quoted 1 line

'hash_len' is get by API, but it is not passed to dev_ops, does this mean this information hardcoded in the driver as well, if so why duplicate this information in driver instead off passing hash_len to

driver?

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Not sure I understand, like I wrote above this is pure verification from my

point of view.

Quoted 2 lines

The driver knows the size based on the dest.

My intention was similar to above comment, like dest_field type RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT implies that required size

should

Quoted 12 lines
Quoted 2 lines

be
2 bytes, and it seems driver already knows about this requirement.

That is correct, that is why I don't think we need the size, add added it only for validation due to community request.

Quoted 6 lines

Instead, it can be possible to verify 'hash_len' in the API level, pass this information to the driver and driver use 'hash_len' directly for its size parameter, so driver will rely on API provided 'hash_len' value instead of storing this information within driver.

Quoted 6 lines
Quoted 4 lines

Lets assume 10 drivers are implementing this feature, should all of them define MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16 equivalent enum/define
withing the driver?

No, the driver implements hard-coded logic, which means that it just needs

to know

Quoted 2 lines

the dest field, in order to know what hash to calculate
It is possible that for each field the HW will calculate the hash using

different algorithm.

Quoted 1 line

OK if HW already needs to know the size in advance, lets go with enum doxygen update only.

Quoted 1 line

Also it is possible that the HW doesn't support writing to the expected field,

in which case we

Quoted 8 lines

want the driver call to fail.

Field implies size.
Size doesn't implies field.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t

Quoted 7 lines
lib/ethdev/rte_flow.c +2 -0
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +0 -0
index 1267c146e5..2bdf3a4a17 100644
@@ -6783,6 +6783,57 @@ rte_flow_calc_table_hash(uint16_t

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

const struct rte_flow_template_table

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t · line 6783

Quoted 1 line
lib/ethdev/rte_flow.h +0 -0
 			 const struct rte_flow_item pattern[], uint8_t

pattern_template_index,

Quoted 7 lines
lib/ethdev/rte_flow.h +5 -0
 			 uint32_t *hash, struct rte_flow_error *error);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Destination field type for the hash calculation, when encap action

is

Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

used.

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +5 -0
+ *
+ * @see function rte_flow_calc_encap_hash
+ */
+enum rte_flow_encap_hash_field {
+	/* Calculate hash placed in UDP source port field. */
 

Just recognized that comments are not doxygen comments.

Quoted 11 lines

Thanks,
Will fix.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

lib/ethdev/rte_flow.h rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table

Quoted 6 lines
lib/ethdev/rte_flow.h +4 -0
+	RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT,
+	/* Calculate hash placed in NVGRE flow ID field. */
+	RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID,
+};
 

Indeed above enum represents a field in a network protocol, right? Instead of having a 'RTE_FLOW_ENCAP_HASH_' specific one, can re-

using

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

'enum rte_flow_field_id' work?

Since the option are really limited and defined by standard, I prefer to

have

Quoted 12 lines
Quoted 8 lines

dedicated options.

Quoted 1 line

OK, my intention is to reduce the duplication. Just for brainstorm, what is the benefit of having 'RTE_FLOW_ENCAP_HASH_' specific enums, if we can present them as generic protocol fiels, like 'RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT' vs 'RTE_FLOW_FIELD_UDP_PORT_SRC,'?

I guess you want to go with 'RTE_FLOW_FIELD_UDP_PORT_SRC right?

I just want to discuss if redundancy can be eliminated.

Quoted 1 line

The main issue is since the options are really limited and used for a very

dedicated function.

Quoted 1 line

When app developers / DPDK developers will look at it, it will be very

unclear what is the use of this enum.

Quoted 1 line

We already have an enum for fields. Like you suggested we could have

used it,

Quoted 2 lines

but this will show much more option than there are really.

OK, lets use dedicated enums to clarify to the users the specific fields available for this set of APIs.

Btw, is boundary check like following required for the APIs:

```
if (dest_field > RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID)
	return -EINVAL;
```
Quoted 5 lines

In case user pass an invalid value as 'dest_filed'

(Note: I intentionally not used MAX enum something like 'RTE_FLOW_ENCAP_HASH_FIELD_MAX' to not need to deal with ABI issues in the future.)

Good idea will add
Best,
Ori

author
#20 lore raw

PATCH v3 1/4 ethdev: introduce encap hash calculation

In reply to Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value fromm the driver for a given packet as if the packet was passed through the HW.

---
 doc/guides/prog_guide/rte_flow.rst     | 14 +++++++
 doc/guides/rel_notes/release_24_03.rst |  6 ++-
 lib/ethdev/rte_flow.c                  | 29 ++++++++++++++
 lib/ethdev/rte_flow.h                  | 53 ++++++++++++++++++++++++++
 lib/ethdev/rte_flow_driver.h           |  5 +++
 lib/ethdev/version.map                 |  1 +
 6 files changed, 107 insertions(+), 1 deletion(-)
doc/guides/prog_guide/rte_flow.rst +14 -0
index 34dc06ec66..294862d502 100644
@@ -4226,6 +4226,20 @@ as it would be calculated in the HW.
                             uint8_t pattern_template_index,
 			                   uint32_t *hash, struct rte_flow_error *error);
 
+Calculate encapsulation hash
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet as it would be calculated by the HW, when encapsulating
+a packet.
+
+When the HW execute an encapsulation action, for example VXLAN tunnel,
+it may calculate an hash of the packet to be encapsulated.
+This hash is stored in the outer header of the tunnel.
+This allow better spreading of traffic.
+
+This function can be used for packets of a flow that are not offloaded and
+pass through the SW instead of the HW, for example, SYN/FIN packets.
+
 .. _flow_isolated_mode:
 
 Flow isolated mode
doc/guides/rel_notes/release_24_03.rst +5 -1
index 358998e988..cf114ead89 100644
@@ -60,6 +60,11 @@ New Features
   * Added new function ``rte_eth_find_rss_algo`` to get RSS hash
     algorithm by its name.
 
+* **Added hash calculation of an encapsulated packet as done by the HW.**
+
+  Added function to calculate hash when doing tunnel encapsulation:
+  ``rte_flow_calc_encap_hash()``
+
 * **Added flow matching of random value.**
 
   * Added ``RTE_FLOW_ITEM_TYPE_RANDOM`` to match random value.
@@ -102,7 +107,6 @@ New Features
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_CLASS`` flow action.
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_DATA`` flow action.
 
-
 Removed Items
 -------------
 
lib/ethdev/rte_flow.c +29 -0
index b06da0cc00..7a4f7a94f6 100644
@@ -2484,3 +2484,32 @@ rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table
 					hash, error);
 	return flow_err(port_id, ret, error);
 }
+
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "calc encap hash is not supported");
+	if (dest_field > RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID)
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash dest field is not defined");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT && hash_len != 2) ||
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID && hash_len != 1))
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash len doesn't match the requested field len");
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +53 -0

59 lines not on this page · raw message

lib/ethdev/rte_flow_driver.h +5 -0
index f35f659503..447163655a 100644
@@ -370,6 +370,11 @@ struct rte_flow_ops {
 		(struct rte_eth_dev *dev, const struct rte_flow_template_table *table,
 		 const struct rte_flow_item pattern[], uint8_t pattern_template_index,
 		 uint32_t *hash, struct rte_flow_error *error);
+	/** @see rte_flow_calc_encap_hash() */
+	int (*flow_calc_encap_hash)
+		(struct rte_eth_dev *dev, const struct rte_flow_item pattern[],
+		 enum rte_flow_encap_hash_field dest_field, uint8_t *hash,
+		 struct rte_flow_error *error);
 };
 
 /**
lib/ethdev/version.map +1 -0
index a050baab0f..360898d067 100644
@@ -319,6 +319,7 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_eth_find_rss_algo;
+	rte_flow_calc_encap_hash;
 };
 
 INTERNAL {
Signature · 1 line
2.34.1
author
#21 lore raw

PATCH v3 2/4 net/mlx5/hws: introduce encap entropy hash calculation API

From: Hamdan Igbaria <[email protected]>

Add new function for encap entropy hash calculation, the function will check the device capability for the entropy hash type used by the device, and will calculate the entropy hash value of the user passed fields according this type.

---
 drivers/common/mlx5/mlx5_prm.h      |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h       | 38 ++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c   | 23 +++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h   |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c | 78 +++++++++++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h |  5 ++
 6 files changed, 154 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_prm.h +6 -2
index abff8e4dc3..50c681704a 100644
@@ -2132,7 +2132,10 @@ struct mlx5_ifc_flow_table_prop_layout_bits {
 struct mlx5_ifc_roce_caps_bits {
 	u8 reserved_0[0x1e];
 	u8 qp_ts_format[0x2];
-	u8 reserved_at_20[0x7e0];
+	u8 reserved_at_20[0xa0];
+	u8 r_roce_max_src_udp_port[0x10];
+	u8 r_roce_min_src_udp_port[0x10];
+	u8 reserved_at_e0[0x720];
 };
 
 struct mlx5_ifc_ft_fields_support_bits {
@@ -2370,7 +2373,8 @@ struct mlx5_ifc_cmd_hca_cap_2_bits {
 	u8 format_select_dw_gtpu_first_ext_dw_0[0x8];
 	u8 generate_wqe_type[0x20];
 	u8 reserved_at_2c0[0x160];
-	u8 reserved_at_420[0x1c];
+	u8 reserved_at_420[0x18];
+	u8 encap_entropy_hash_type[0x4];
 	u8 flow_table_hash_type[0x4];
 	u8 reserved_at_440[0x3c0];
 };
drivers/net/mlx5/hws/mlx5dr.h +38 -0
index d88f73ab57..321b649f8c 100644
@@ -279,6 +279,27 @@ struct mlx5dr_action_dest_attr {
 	} reformat;
 };
 
+union mlx5dr_crc_encap_entropy_hash_ip_field {
+	uint8_t  ipv6_addr[16];
+	struct {
+		uint8_t  reserved[12];
+		rte_be32_t  ipv4_addr;
+	};
+};
+
+struct mlx5dr_crc_encap_entropy_hash_fields {
+	union mlx5dr_crc_encap_entropy_hash_ip_field dst;
+	union mlx5dr_crc_encap_entropy_hash_ip_field src;
+	uint8_t next_protocol;
+	rte_be16_t dst_port;
+	rte_be16_t src_port;
+}__rte_packed;
+
+enum mlx5dr_crc_encap_entropy_hash_size {
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_8,
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16,
+};
+
 /* Open a context used for direct rule insertion using hardware steering.
  * Each context can contain multiple tables of different types.
  *
@@ -845,4 +866,21 @@ int mlx5dr_send_queue_action(struct mlx5dr_context *ctx,
  */
 int mlx5dr_debug_dump(struct mlx5dr_context *ctx, FILE *f);
 
+/* Calculate encap entropy hash value
+ *
+ * @param[in] ctx
+ *	The context to get from it's capabilities the entropy hash type.
+ * @param[in] data
+ *	The fields for the hash calculation.
+ * @param[in] entropy_res
+ *	An array to store the hash value to it.
+ * @param[in] res_size
+ *	The result size.
+ * @return zero on success non zero otherwise.
+ */
+int mlx5dr_crc_encap_entropy_hash_calc(struct mlx5dr_context *ctx,
+				       struct mlx5dr_crc_encap_entropy_hash_fields *data,
+				       uint8_t entropy_res[],
+				       enum mlx5dr_crc_encap_entropy_hash_size res_size);
+
 #endif
drivers/net/mlx5/hws/mlx5dr_cmd.c +23 -0
index 876a47147d..f77b194708 100644
@@ -1103,6 +1103,8 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->ipsec_offload = MLX5_GET(query_hca_cap_out, out,
 				      capability.cmd_hca_cap.ipsec_offload);
 
+	caps->roce = MLX5_GET(query_hca_cap_out, out, capability.cmd_hca_cap.roce);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE_2 |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1158,6 +1160,9 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->flow_table_hash_type = MLX5_GET(query_hca_cap_out, out,
 					      capability.cmd_hca_cap_2.flow_table_hash_type);
 
+	caps->encap_entropy_hash_type = MLX5_GET(query_hca_cap_out, out,
+						 capability.cmd_hca_cap_2.encap_entropy_hash_type);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_NIC_FLOW_TABLE |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1306,6 +1311,24 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 						capability.esw_cap.merged_eswitch);
 	}
 
+	if (caps->roce) {
+		MLX5_SET(query_hca_cap_in, in, op_mod,
+			 MLX5_GET_HCA_CAP_OP_MOD_ROCE |
+			 MLX5_HCA_CAP_OPMOD_GET_CUR);
+
+		ret = mlx5_glue->devx_general_cmd(ctx, in, sizeof(in), out, sizeof(out));
+		if (ret) {
+			DR_LOG(ERR, "Failed to query roce caps");
+			rte_errno = errno;
+			return rte_errno;
+		}
+
+		caps->roce_max_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_max_src_udp_port);
+		caps->roce_min_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_min_src_udp_port);
+	}
+
 	ret = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
 	if (ret) {
 		DR_LOG(ERR, "Failed to query device attributes");
drivers/net/mlx5/hws/mlx5dr_cmd.h +4 -0
index 18c2b07fc8..694231e08f 100644
@@ -246,6 +246,10 @@ struct mlx5dr_cmd_query_caps {
 	uint32_t shared_vhca_id;
 	char fw_ver[64];
 	bool ipsec_offload;
+	uint8_t encap_entropy_hash_type;
+	bool roce;
+	uint16_t roce_max_src_udp_port;
+	uint16_t roce_min_src_udp_port;
 };
 
 int mlx5dr_cmd_destroy_obj(struct mlx5dr_devx_obj *devx_obj);
drivers/net/mlx5/hws/mlx5dr_crc32.c +78 -0

87 lines not on this page · raw message

drivers/net/mlx5/hws/mlx5dr_crc32.h +5 -0
index 9aab9e06ca..75b6009a15 100644
@@ -10,4 +10,9 @@
  */
 uint32_t mlx5dr_crc32_calc(uint8_t *p, size_t len);
 
+/* Standard CRC16 calculation using the crc_tab16 param to indicate
+ * the pre-calculated polynome hash values.
+ */
+uint16_t mlx5dr_crc16_calc(uint8_t *p, size_t len, uint16_t crc_tab16[]);
+
 #endif /* MLX5DR_CRC32_C_ */
Signature · 1 line
2.34.1
author
#22 lore raw

PATCH v3 3/4 net/mlx5: add calc encap hash support

In reply to Ori Kam

This commit adds support for encap hash calculation.

---
 drivers/net/mlx5/mlx5_flow.c    | 29 +++++++++++++++
 drivers/net/mlx5/mlx5_flow.h    |  8 ++++
 drivers/net/mlx5/mlx5_flow_hw.c | 66 +++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
drivers/net/mlx5/mlx5_flow.c +29 -0
index 40376c99ba..7c5a5da8ec 100644
@@ -1197,6 +1197,12 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 			  const struct rte_flow_item pattern[],
 			  uint8_t pattern_template_index,
 			  uint32_t *hash, struct rte_flow_error *error);
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error);
 
 static const struct rte_flow_ops mlx5_flow_ops = {
 	.validate = mlx5_flow_validate,
@@ -1253,6 +1259,7 @@ static const struct rte_flow_ops mlx5_flow_ops = {
 	.async_action_list_handle_query_update =
 		mlx5_flow_async_action_list_handle_query_update,
 	.flow_calc_table_hash = mlx5_flow_calc_table_hash,
+	.flow_calc_encap_hash = mlx5_flow_calc_encap_hash,
 };
 
 /* Tunnel information. */
@@ -11121,6 +11128,28 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 					  hash, error);
 }
 
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error)
+{
+	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, NULL);
+	const struct mlx5_flow_driver_ops *fops;
+
+	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "invalid driver type");
+	fops = flow_get_drv_ops(drv_type);
+	if (!fops || !fops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "no calc encap hash handler");
+	return fops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+}
+
 /**
  * Destroy all indirect actions (shared RSS).
  *
drivers/net/mlx5/mlx5_flow.h +8 -0
index b086dfaf28..a4d0ff7b13 100644
@@ -2198,6 +2198,13 @@ typedef int
 			 const struct rte_flow_item pattern[],
 			 uint8_t pattern_template_index,
 			 uint32_t *hash, struct rte_flow_error *error);
+typedef int
+(*mlx5_flow_calc_encap_hash_t)
+			(struct rte_eth_dev *dev,
+			 const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field,
+			 uint8_t *hash,
+			 struct rte_flow_error *error);
 
 struct mlx5_flow_driver_ops {
 	mlx5_flow_validate_t validate;
@@ -2271,6 +2278,7 @@ struct mlx5_flow_driver_ops {
 	mlx5_flow_async_action_list_handle_query_update_t
 		async_action_list_handle_query_update;
 	mlx5_flow_calc_table_hash_t flow_calc_table_hash;
+	mlx5_flow_calc_encap_hash_t flow_calc_encap_hash;
 };
 
 /* mlx5_flow.c */
drivers/net/mlx5/mlx5_flow_hw.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#23 lore raw

PATCH v3 4/4 app/testpmd: add encap hash calculation

In reply to Ori Kam

This commits add support for calculating the encap hash.

Command structure:
flow hash {port} encap {target field} pattern {item} [/ {item} [...] ] / end

Example:
calculate hash to be used by VXLAN encapsulation.
flow hash 0 encap hash_field_sport pattern ipv4 dst is 7.7.7.7 src is 8.8.8.8 / udp dst is 5678 src is 1234 / end

---
 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++++++++--
 app/test-pmd/config.c                       | 30 +++++++++++
 app/test-pmd/testpmd.h                      |  3 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++++-
 4 files changed, 106 insertions(+), 5 deletions(-)
app/test-pmd/cmdline_flow.c +53 -4
index a4131e1b39..844840fe2d 100644
@@ -219,6 +219,10 @@ enum index {
 	HASH_CALC_TABLE,
 	HASH_CALC_PATTERN_INDEX,
 	HASH_CALC_PATTERN,
+	HASH_CALC_ENCAP,
+	HASH_CALC_DEST,
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
 
 	/* Tunnel arguments. */
 	TUNNEL_CREATE,
@@ -1220,6 +1224,8 @@ struct buffer {
 			uint32_t pattern_n;
 			uint32_t actions_n;
 			uint8_t *data;
+			enum rte_flow_encap_hash_field field;
+			uint8_t encap_hash;
 		} vc; /**< Validate/create arguments. */
 		struct {
 			uint64_t *rule;
@@ -2618,6 +2624,18 @@ static const enum index action_nat64[] = {
 	ZERO,
 };
 
+static const enum index next_hash_subcmd[] = {
+	HASH_CALC_TABLE,
+	HASH_CALC_ENCAP,
+	ZERO,
+};
+
+static const enum index next_hash_encap_dest_subcmd[] = {
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
+	ZERO,
+};
+
 static int parse_set_raw_encap_decap(struct context *, const struct token *,
 				     const char *, unsigned int,
 				     void *, unsigned int);
@@ -3875,7 +3893,7 @@ static const struct token token_list[] = {
 	[HASH] = {
 		.name = "hash",
 		.help = "calculate hash for a given pattern in a given template table",
-		.next = NEXT(NEXT_ENTRY(HASH_CALC_TABLE), NEXT_ENTRY(COMMON_PORT_ID)),
+		.next = NEXT(next_hash_subcmd, NEXT_ENTRY(COMMON_PORT_ID)),
 		.args = ARGS(ARGS_ENTRY(struct buffer, port)),
 		.call = parse_hash,
 	},
@@ -3889,6 +3907,12 @@ static const struct token token_list[] = {
 					args.vc.table_id)),
 		.call = parse_hash,
 	},
+	[HASH_CALC_ENCAP] = {
+		.name = "encap",
+		.help = "calculates encap hash",
+		.next = NEXT(next_hash_encap_dest_subcmd),
+		.call = parse_hash,
+	},
 	[HASH_CALC_PATTERN_INDEX] = {
 		.name = "pattern_template",
 		.help = "specify pattern template id",
@@ -3898,6 +3922,18 @@ static const struct token token_list[] = {
 					args.vc.pat_templ_id)),
 		.call = parse_hash,
 	},
+	[ENCAP_HASH_FIELD_SRC_PORT] = {
+		.name = "hash_field_sport",
+		.help = "the encap hash field is src port",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
+	[ENCAP_HASH_FIELD_GRE_FLOW_ID] = {
+		.name = "hash_field_flow_id",
+		.help = "the encap hash field is NVGRE flow id",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
 	/* Top-level command. */
 	[INDIRECT_ACTION] = {
 		.name = "indirect_action",
@@ -11075,6 +11111,15 @@ parse_hash(struct context *ctx, const struct token *token,
 		ctx->object = out->args.vc.pattern;
 		ctx->objmask = NULL;
 		return len;
+	case HASH_CALC_ENCAP:
+		out->args.vc.encap_hash = 1;
+		return len;
+	case ENCAP_HASH_FIELD_SRC_PORT:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT;
+		return len;
+	case ENCAP_HASH_FIELD_GRE_FLOW_ID:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID;
+		return len;
 	default:
 		return -1;
 	}
@@ -13068,9 +13113,13 @@ cmd_flow_parsed(const struct buffer *in)
 		port_queue_flow_pull(in->port, in->queue);
 		break;
 	case HASH:
-		port_flow_hash_calc(in->port, in->args.vc.table_id,
-				    in->args.vc.pat_templ_id,
-				    in->args.vc.pattern);
+		if (!in->args.vc.encap_hash)
+			port_flow_hash_calc(in->port, in->args.vc.table_id,
+					    in->args.vc.pat_templ_id,
+					    in->args.vc.pattern);
+		else
+			port_flow_hash_calc_encap(in->port, in->args.vc.field,
+						  in->args.vc.pattern);
 		break;
 	case QUEUE_AGED:
 		port_queue_flow_aged(in->port, in->queue,
app/test-pmd/config.c +30 -0
index cad7537bc6..e26b5bd18d 100644
@@ -3339,6 +3339,36 @@ port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 	return 0;
 }
 
+/** Calculate the encap hash result for a given pattern. */
+int
+port_flow_hash_calc_encap(portid_t port_id,
+			  enum rte_flow_encap_hash_field encap_hash_field,
+			  const struct rte_flow_item pattern[])
+{
+	struct rte_flow_error error;
+	int ret = 0;
+	uint16_t hash = 0;
+	uint8_t len = encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT ? 2 : 1;
+
+	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+	    port_id == (portid_t)RTE_PORT_ALL) {
+		printf("Failed to calculate encap hash - not a valid port");
+		return -EINVAL;
+	}
+
+	ret = rte_flow_calc_encap_hash(port_id, pattern, encap_hash_field, len,
+				       (uint8_t *)&hash, &error);
+	if (ret < 0) {
+		printf("Failed to calculate encap hash");
+		return ret;
+	}
+	if (encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT)
+		printf("encap hash result %#x\n", hash);
+	else
+		printf("encap hash result %#x\n", *(uint8_t *)&hash);
+	return 0;
+}
+
 /** Pull queue operation results from the queue. */
 static int
 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
app/test-pmd/testpmd.h +3 -0
index 9b10a9ea1c..e18546c46e 100644
@@ -1016,6 +1016,9 @@ int port_queue_flow_push(portid_t port_id, queueid_t queue_id);
 int port_queue_flow_pull(portid_t port_id, queueid_t queue_id);
 int port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 			uint8_t pattern_template_index, const struct rte_flow_item pattern[]);
+int port_flow_hash_calc_encap(portid_t port_id,
+			      enum rte_flow_encap_hash_field encap_hash_field,
+			      const struct rte_flow_item pattern[]);
 void port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy);
 int port_flow_validate(portid_t port_id,
 		       const struct rte_flow_attr *attr,
doc/guides/testpmd_app_ug/testpmd_funcs.rst +20 -1
index 08a0b59535..bf48f01b0a 100644
@@ -3297,7 +3297,7 @@ The usual error message is shown when operations results cannot be pulled::
 Calculating hash
 ~~~~~~~~~~~~~~~~
 
-``flow hash`` calculates the hash for a given pattern.
+``flow hash {port_id} template_table`` calculates the hash for a given pattern.
 It is bound to ``rte_flow_calc_table_hash()``::
 
    flow hash {port_id} template_table {table_id}
@@ -3316,6 +3316,25 @@ Otherwise, it will show an error message of the form::
 This command uses the same pattern items as ``flow create``,
 their format is described in `Creating flow rules`_.
 
+
+Simulate encap hash calculation
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``flow hash {port_id} encap`` adds hash query, that returns the hash value
+that the HW will calculate when encapsulating a packet.
+
+   flow hash {port_id} encap {target field} pattern {item} [/ {item} [...]] / end
+
+If successful, it will show::
+
+   encap hash result #[...]
+
+The value will be shown as uint16_t without endian conversion.
+
+Otherwise it will show an error message of the form::
+
+   Failed to calculate encap hash - [...]
+
 Creating a tunnel stub for offload
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Signature · 1 line
2.34.1
author
#24 lore raw

PATCH v4 1/4 ethdev: introduce encap hash calculation

In reply to Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value from the driver for a given packet as if the packet was passed through the HW.

---
 doc/guides/prog_guide/rte_flow.rst     | 14 +++++++
 doc/guides/rel_notes/release_24_03.rst |  6 ++-
 lib/ethdev/rte_flow.c                  | 29 ++++++++++++++
 lib/ethdev/rte_flow.h                  | 53 ++++++++++++++++++++++++++
 lib/ethdev/rte_flow_driver.h           |  5 +++
 lib/ethdev/version.map                 |  1 +
 6 files changed, 107 insertions(+), 1 deletion(-)
doc/guides/prog_guide/rte_flow.rst +14 -0
index 34dc06ec66..294862d502 100644
@@ -4226,6 +4226,20 @@ as it would be calculated in the HW.
                             uint8_t pattern_template_index,
 			                   uint32_t *hash, struct rte_flow_error *error);
 
+Calculate encapsulation hash
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Calculating hash of a packet as it would be calculated by the HW, when encapsulating
+a packet.
+
+When the HW execute an encapsulation action, for example VXLAN tunnel,
+it may calculate an hash of the packet to be encapsulated.
+This hash is stored in the outer header of the tunnel.
+This allow better spreading of traffic.
+
+This function can be used for packets of a flow that are not offloaded and
+pass through the SW instead of the HW, for example, SYN/FIN packets.
+
 .. _flow_isolated_mode:
 
 Flow isolated mode
doc/guides/rel_notes/release_24_03.rst +5 -1
index 358998e988..cf114ead89 100644
@@ -60,6 +60,11 @@ New Features
   * Added new function ``rte_eth_find_rss_algo`` to get RSS hash
     algorithm by its name.
 
+* **Added hash calculation of an encapsulated packet as done by the HW.**
+
+  Added function to calculate hash when doing tunnel encapsulation:
+  ``rte_flow_calc_encap_hash()``
+
 * **Added flow matching of random value.**
 
   * Added ``RTE_FLOW_ITEM_TYPE_RANDOM`` to match random value.
@@ -102,7 +107,6 @@ New Features
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_CLASS`` flow action.
   * Added HW steering support for modify field ``RTE_FLOW_FIELD_GENEVE_OPT_DATA`` flow action.
 
-
 Removed Items
 -------------
 
lib/ethdev/rte_flow.c +29 -0
index b06da0cc00..7a4f7a94f6 100644
@@ -2484,3 +2484,32 @@ rte_flow_calc_table_hash(uint16_t port_id, const struct rte_flow_template_table
 					hash, error);
 	return flow_err(port_id, ret, error);
 }
+
+int
+rte_flow_calc_encap_hash(uint16_t port_id, const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field, uint8_t hash_len,
+			 uint8_t *hash, struct rte_flow_error *error)
+{
+	int ret;
+	struct rte_eth_dev *dev;
+	const struct rte_flow_ops *ops;
+
+	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
+	ops = rte_flow_ops_get(port_id, error);
+	if (!ops || !ops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "calc encap hash is not supported");
+	if (dest_field > RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID)
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash dest field is not defined");
+	if ((dest_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT && hash_len != 2) ||
+	    (dest_field == RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID && hash_len != 1))
+		return rte_flow_error_set(error, EINVAL,
+					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "hash len doesn't match the requested field len");
+	dev = &rte_eth_devices[port_id];
+	ret = ops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+	return flow_err(port_id, ret, error);
+}
lib/ethdev/rte_flow.h +53 -0

59 lines not on this page · raw message

lib/ethdev/rte_flow_driver.h +5 -0
index f35f659503..447163655a 100644
@@ -370,6 +370,11 @@ struct rte_flow_ops {
 		(struct rte_eth_dev *dev, const struct rte_flow_template_table *table,
 		 const struct rte_flow_item pattern[], uint8_t pattern_template_index,
 		 uint32_t *hash, struct rte_flow_error *error);
+	/** @see rte_flow_calc_encap_hash() */
+	int (*flow_calc_encap_hash)
+		(struct rte_eth_dev *dev, const struct rte_flow_item pattern[],
+		 enum rte_flow_encap_hash_field dest_field, uint8_t *hash,
+		 struct rte_flow_error *error);
 };
 
 /**
lib/ethdev/version.map +1 -0
index a050baab0f..360898d067 100644
@@ -319,6 +319,7 @@ EXPERIMENTAL {
 
 	# added in 24.03
 	rte_eth_find_rss_algo;
+	rte_flow_calc_encap_hash;
 };
 
 INTERNAL {
Signature · 1 line
2.34.1
author
#25 lore raw

PATCH v4 2/4 net/mlx5/hws: introduce encap entropy hash calculation API

From: Hamdan Igbaria <[email protected]>

Add new function for encap entropy hash calculation, the function will check the device capability for the entropy hash type used by the device, and will calculate the entropy hash value of the user passed fields according this type.

---
 drivers/common/mlx5/mlx5_prm.h      |  8 ++-
 drivers/net/mlx5/hws/mlx5dr.h       | 38 ++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.c   | 23 +++++++++
 drivers/net/mlx5/hws/mlx5dr_cmd.h   |  4 ++
 drivers/net/mlx5/hws/mlx5dr_crc32.c | 78 +++++++++++++++++++++++++++++
 drivers/net/mlx5/hws/mlx5dr_crc32.h |  5 ++
 6 files changed, 154 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_prm.h +6 -2
index abff8e4dc3..50c681704a 100644
@@ -2132,7 +2132,10 @@ struct mlx5_ifc_flow_table_prop_layout_bits {
 struct mlx5_ifc_roce_caps_bits {
 	u8 reserved_0[0x1e];
 	u8 qp_ts_format[0x2];
-	u8 reserved_at_20[0x7e0];
+	u8 reserved_at_20[0xa0];
+	u8 r_roce_max_src_udp_port[0x10];
+	u8 r_roce_min_src_udp_port[0x10];
+	u8 reserved_at_e0[0x720];
 };
 
 struct mlx5_ifc_ft_fields_support_bits {
@@ -2370,7 +2373,8 @@ struct mlx5_ifc_cmd_hca_cap_2_bits {
 	u8 format_select_dw_gtpu_first_ext_dw_0[0x8];
 	u8 generate_wqe_type[0x20];
 	u8 reserved_at_2c0[0x160];
-	u8 reserved_at_420[0x1c];
+	u8 reserved_at_420[0x18];
+	u8 encap_entropy_hash_type[0x4];
 	u8 flow_table_hash_type[0x4];
 	u8 reserved_at_440[0x3c0];
 };
drivers/net/mlx5/hws/mlx5dr.h +38 -0
index d88f73ab57..9c5b068c93 100644
@@ -279,6 +279,27 @@ struct mlx5dr_action_dest_attr {
 	} reformat;
 };
 
+union mlx5dr_crc_encap_entropy_hash_ip_field {
+	uint8_t  ipv6_addr[16];
+	struct {
+		uint8_t  reserved[12];
+		rte_be32_t  ipv4_addr;
+	};
+};
+
+struct mlx5dr_crc_encap_entropy_hash_fields {
+	union mlx5dr_crc_encap_entropy_hash_ip_field dst;
+	union mlx5dr_crc_encap_entropy_hash_ip_field src;
+	uint8_t next_protocol;
+	rte_be16_t dst_port;
+	rte_be16_t src_port;
+} __rte_packed;
+
+enum mlx5dr_crc_encap_entropy_hash_size {
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_8,
+	MLX5DR_CRC_ENCAP_ENTROPY_HASH_SIZE_16,
+};
+
 /* Open a context used for direct rule insertion using hardware steering.
  * Each context can contain multiple tables of different types.
  *
@@ -845,4 +866,21 @@ int mlx5dr_send_queue_action(struct mlx5dr_context *ctx,
  */
 int mlx5dr_debug_dump(struct mlx5dr_context *ctx, FILE *f);
 
+/* Calculate encap entropy hash value
+ *
+ * @param[in] ctx
+ *	The context to get from it's capabilities the entropy hash type.
+ * @param[in] data
+ *	The fields for the hash calculation.
+ * @param[in] entropy_res
+ *	An array to store the hash value to it.
+ * @param[in] res_size
+ *	The result size.
+ * @return zero on success non zero otherwise.
+ */
+int mlx5dr_crc_encap_entropy_hash_calc(struct mlx5dr_context *ctx,
+				       struct mlx5dr_crc_encap_entropy_hash_fields *data,
+				       uint8_t entropy_res[],
+				       enum mlx5dr_crc_encap_entropy_hash_size res_size);
+
 #endif
drivers/net/mlx5/hws/mlx5dr_cmd.c +23 -0
index 876a47147d..f77b194708 100644
@@ -1103,6 +1103,8 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->ipsec_offload = MLX5_GET(query_hca_cap_out, out,
 				      capability.cmd_hca_cap.ipsec_offload);
 
+	caps->roce = MLX5_GET(query_hca_cap_out, out, capability.cmd_hca_cap.roce);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE_2 |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1158,6 +1160,9 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 	caps->flow_table_hash_type = MLX5_GET(query_hca_cap_out, out,
 					      capability.cmd_hca_cap_2.flow_table_hash_type);
 
+	caps->encap_entropy_hash_type = MLX5_GET(query_hca_cap_out, out,
+						 capability.cmd_hca_cap_2.encap_entropy_hash_type);
+
 	MLX5_SET(query_hca_cap_in, in, op_mod,
 		 MLX5_GET_HCA_CAP_OP_MOD_NIC_FLOW_TABLE |
 		 MLX5_HCA_CAP_OPMOD_GET_CUR);
@@ -1306,6 +1311,24 @@ int mlx5dr_cmd_query_caps(struct ibv_context *ctx,
 						capability.esw_cap.merged_eswitch);
 	}
 
+	if (caps->roce) {
+		MLX5_SET(query_hca_cap_in, in, op_mod,
+			 MLX5_GET_HCA_CAP_OP_MOD_ROCE |
+			 MLX5_HCA_CAP_OPMOD_GET_CUR);
+
+		ret = mlx5_glue->devx_general_cmd(ctx, in, sizeof(in), out, sizeof(out));
+		if (ret) {
+			DR_LOG(ERR, "Failed to query roce caps");
+			rte_errno = errno;
+			return rte_errno;
+		}
+
+		caps->roce_max_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_max_src_udp_port);
+		caps->roce_min_src_udp_port = MLX5_GET(query_hca_cap_out, out,
+						capability.roce_caps.r_roce_min_src_udp_port);
+	}
+
 	ret = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
 	if (ret) {
 		DR_LOG(ERR, "Failed to query device attributes");
drivers/net/mlx5/hws/mlx5dr_cmd.h +4 -0
index 18c2b07fc8..694231e08f 100644
@@ -246,6 +246,10 @@ struct mlx5dr_cmd_query_caps {
 	uint32_t shared_vhca_id;
 	char fw_ver[64];
 	bool ipsec_offload;
+	uint8_t encap_entropy_hash_type;
+	bool roce;
+	uint16_t roce_max_src_udp_port;
+	uint16_t roce_min_src_udp_port;
 };
 
 int mlx5dr_cmd_destroy_obj(struct mlx5dr_devx_obj *devx_obj);
drivers/net/mlx5/hws/mlx5dr_crc32.c +78 -0

87 lines not on this page · raw message

drivers/net/mlx5/hws/mlx5dr_crc32.h +5 -0
index 9aab9e06ca..75b6009a15 100644
@@ -10,4 +10,9 @@
  */
 uint32_t mlx5dr_crc32_calc(uint8_t *p, size_t len);
 
+/* Standard CRC16 calculation using the crc_tab16 param to indicate
+ * the pre-calculated polynome hash values.
+ */
+uint16_t mlx5dr_crc16_calc(uint8_t *p, size_t len, uint16_t crc_tab16[]);
+
 #endif /* MLX5DR_CRC32_C_ */
Signature · 1 line
2.34.1
author
#26 lore raw

PATCH v4 3/4 net/mlx5: add calc encap hash support

In reply to Ori Kam

This commit adds support for encap hash calculation.

---
 drivers/net/mlx5/mlx5_flow.c    | 29 +++++++++++++++
 drivers/net/mlx5/mlx5_flow.h    |  8 ++++
 drivers/net/mlx5/mlx5_flow_hw.c | 66 +++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
drivers/net/mlx5/mlx5_flow.c +29 -0
index 40376c99ba..7c5a5da8ec 100644
@@ -1197,6 +1197,12 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 			  const struct rte_flow_item pattern[],
 			  uint8_t pattern_template_index,
 			  uint32_t *hash, struct rte_flow_error *error);
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error);
 
 static const struct rte_flow_ops mlx5_flow_ops = {
 	.validate = mlx5_flow_validate,
@@ -1253,6 +1259,7 @@ static const struct rte_flow_ops mlx5_flow_ops = {
 	.async_action_list_handle_query_update =
 		mlx5_flow_async_action_list_handle_query_update,
 	.flow_calc_table_hash = mlx5_flow_calc_table_hash,
+	.flow_calc_encap_hash = mlx5_flow_calc_encap_hash,
 };
 
 /* Tunnel information. */
@@ -11121,6 +11128,28 @@ mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
 					  hash, error);
 }
 
+static int
+mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
+			  const struct rte_flow_item pattern[],
+			  enum rte_flow_encap_hash_field dest_field,
+			  uint8_t *hash,
+			  struct rte_flow_error *error)
+{
+	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, NULL);
+	const struct mlx5_flow_driver_ops *fops;
+
+	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "invalid driver type");
+	fops = flow_get_drv_ops(drv_type);
+	if (!fops || !fops->flow_calc_encap_hash)
+		return rte_flow_error_set(error, ENOTSUP,
+					  RTE_FLOW_ERROR_TYPE_ACTION,
+					  NULL, "no calc encap hash handler");
+	return fops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
+}
+
 /**
  * Destroy all indirect actions (shared RSS).
  *
drivers/net/mlx5/mlx5_flow.h +8 -0
index b086dfaf28..a4d0ff7b13 100644
@@ -2198,6 +2198,13 @@ typedef int
 			 const struct rte_flow_item pattern[],
 			 uint8_t pattern_template_index,
 			 uint32_t *hash, struct rte_flow_error *error);
+typedef int
+(*mlx5_flow_calc_encap_hash_t)
+			(struct rte_eth_dev *dev,
+			 const struct rte_flow_item pattern[],
+			 enum rte_flow_encap_hash_field dest_field,
+			 uint8_t *hash,
+			 struct rte_flow_error *error);
 
 struct mlx5_flow_driver_ops {
 	mlx5_flow_validate_t validate;
@@ -2271,6 +2278,7 @@ struct mlx5_flow_driver_ops {
 	mlx5_flow_async_action_list_handle_query_update_t
 		async_action_list_handle_query_update;
 	mlx5_flow_calc_table_hash_t flow_calc_table_hash;
+	mlx5_flow_calc_encap_hash_t flow_calc_encap_hash;
 };
 
 /* mlx5_flow.c */
drivers/net/mlx5/mlx5_flow_hw.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#27 lore raw

PATCH v4 4/4 app/testpmd: add encap hash calculation

In reply to Ori Kam

This commits add support for calculating the encap hash.

Command structure:
flow hash {port} encap {target field} pattern {item} [/ {item} [...] ] / end

Example:
calculate hash to be used by VXLAN encapsulation.
flow hash 0 encap hash_field_sport pattern ipv4 dst is 7.7.7.7 src is 8.8.8.8 / udp dst is 5678 src is 1234 / end

---
 app/test-pmd/cmdline_flow.c                 | 57 +++++++++++++++++++--
 app/test-pmd/config.c                       | 30 +++++++++++
 app/test-pmd/testpmd.h                      |  3 ++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst | 21 +++++++-
 4 files changed, 106 insertions(+), 5 deletions(-)
app/test-pmd/cmdline_flow.c +53 -4
index a4131e1b39..844840fe2d 100644
@@ -219,6 +219,10 @@ enum index {
 	HASH_CALC_TABLE,
 	HASH_CALC_PATTERN_INDEX,
 	HASH_CALC_PATTERN,
+	HASH_CALC_ENCAP,
+	HASH_CALC_DEST,
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
 
 	/* Tunnel arguments. */
 	TUNNEL_CREATE,
@@ -1220,6 +1224,8 @@ struct buffer {
 			uint32_t pattern_n;
 			uint32_t actions_n;
 			uint8_t *data;
+			enum rte_flow_encap_hash_field field;
+			uint8_t encap_hash;
 		} vc; /**< Validate/create arguments. */
 		struct {
 			uint64_t *rule;
@@ -2618,6 +2624,18 @@ static const enum index action_nat64[] = {
 	ZERO,
 };
 
+static const enum index next_hash_subcmd[] = {
+	HASH_CALC_TABLE,
+	HASH_CALC_ENCAP,
+	ZERO,
+};
+
+static const enum index next_hash_encap_dest_subcmd[] = {
+	ENCAP_HASH_FIELD_SRC_PORT,
+	ENCAP_HASH_FIELD_GRE_FLOW_ID,
+	ZERO,
+};
+
 static int parse_set_raw_encap_decap(struct context *, const struct token *,
 				     const char *, unsigned int,
 				     void *, unsigned int);
@@ -3875,7 +3893,7 @@ static const struct token token_list[] = {
 	[HASH] = {
 		.name = "hash",
 		.help = "calculate hash for a given pattern in a given template table",
-		.next = NEXT(NEXT_ENTRY(HASH_CALC_TABLE), NEXT_ENTRY(COMMON_PORT_ID)),
+		.next = NEXT(next_hash_subcmd, NEXT_ENTRY(COMMON_PORT_ID)),
 		.args = ARGS(ARGS_ENTRY(struct buffer, port)),
 		.call = parse_hash,
 	},
@@ -3889,6 +3907,12 @@ static const struct token token_list[] = {
 					args.vc.table_id)),
 		.call = parse_hash,
 	},
+	[HASH_CALC_ENCAP] = {
+		.name = "encap",
+		.help = "calculates encap hash",
+		.next = NEXT(next_hash_encap_dest_subcmd),
+		.call = parse_hash,
+	},
 	[HASH_CALC_PATTERN_INDEX] = {
 		.name = "pattern_template",
 		.help = "specify pattern template id",
@@ -3898,6 +3922,18 @@ static const struct token token_list[] = {
 					args.vc.pat_templ_id)),
 		.call = parse_hash,
 	},
+	[ENCAP_HASH_FIELD_SRC_PORT] = {
+		.name = "hash_field_sport",
+		.help = "the encap hash field is src port",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
+	[ENCAP_HASH_FIELD_GRE_FLOW_ID] = {
+		.name = "hash_field_flow_id",
+		.help = "the encap hash field is NVGRE flow id",
+		.next = NEXT(NEXT_ENTRY(ITEM_PATTERN)),
+		.call = parse_hash,
+	},
 	/* Top-level command. */
 	[INDIRECT_ACTION] = {
 		.name = "indirect_action",
@@ -11075,6 +11111,15 @@ parse_hash(struct context *ctx, const struct token *token,
 		ctx->object = out->args.vc.pattern;
 		ctx->objmask = NULL;
 		return len;
+	case HASH_CALC_ENCAP:
+		out->args.vc.encap_hash = 1;
+		return len;
+	case ENCAP_HASH_FIELD_SRC_PORT:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT;
+		return len;
+	case ENCAP_HASH_FIELD_GRE_FLOW_ID:
+		out->args.vc.field = RTE_FLOW_ENCAP_HASH_FIELD_NVGRE_FLOW_ID;
+		return len;
 	default:
 		return -1;
 	}
@@ -13068,9 +13113,13 @@ cmd_flow_parsed(const struct buffer *in)
 		port_queue_flow_pull(in->port, in->queue);
 		break;
 	case HASH:
-		port_flow_hash_calc(in->port, in->args.vc.table_id,
-				    in->args.vc.pat_templ_id,
-				    in->args.vc.pattern);
+		if (!in->args.vc.encap_hash)
+			port_flow_hash_calc(in->port, in->args.vc.table_id,
+					    in->args.vc.pat_templ_id,
+					    in->args.vc.pattern);
+		else
+			port_flow_hash_calc_encap(in->port, in->args.vc.field,
+						  in->args.vc.pattern);
 		break;
 	case QUEUE_AGED:
 		port_queue_flow_aged(in->port, in->queue,
app/test-pmd/config.c +30 -0
index cad7537bc6..e26b5bd18d 100644
@@ -3339,6 +3339,36 @@ port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 	return 0;
 }
 
+/** Calculate the encap hash result for a given pattern. */
+int
+port_flow_hash_calc_encap(portid_t port_id,
+			  enum rte_flow_encap_hash_field encap_hash_field,
+			  const struct rte_flow_item pattern[])
+{
+	struct rte_flow_error error;
+	int ret = 0;
+	uint16_t hash = 0;
+	uint8_t len = encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT ? 2 : 1;
+
+	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
+	    port_id == (portid_t)RTE_PORT_ALL) {
+		printf("Failed to calculate encap hash - not a valid port");
+		return -EINVAL;
+	}
+
+	ret = rte_flow_calc_encap_hash(port_id, pattern, encap_hash_field, len,
+				       (uint8_t *)&hash, &error);
+	if (ret < 0) {
+		printf("Failed to calculate encap hash");
+		return ret;
+	}
+	if (encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT)
+		printf("encap hash result %#x\n", hash);
+	else
+		printf("encap hash result %#x\n", *(uint8_t *)&hash);
+	return 0;
+}
+
 /** Pull queue operation results from the queue. */
 static int
 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
app/test-pmd/testpmd.h +3 -0
index 9b10a9ea1c..e18546c46e 100644
@@ -1016,6 +1016,9 @@ int port_queue_flow_push(portid_t port_id, queueid_t queue_id);
 int port_queue_flow_pull(portid_t port_id, queueid_t queue_id);
 int port_flow_hash_calc(portid_t port_id, uint32_t table_id,
 			uint8_t pattern_template_index, const struct rte_flow_item pattern[]);
+int port_flow_hash_calc_encap(portid_t port_id,
+			      enum rte_flow_encap_hash_field encap_hash_field,
+			      const struct rte_flow_item pattern[]);
 void port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy);
 int port_flow_validate(portid_t port_id,
 		       const struct rte_flow_attr *attr,
doc/guides/testpmd_app_ug/testpmd_funcs.rst +20 -1
index 08a0b59535..bf48f01b0a 100644
@@ -3297,7 +3297,7 @@ The usual error message is shown when operations results cannot be pulled::
 Calculating hash
 ~~~~~~~~~~~~~~~~
 
-``flow hash`` calculates the hash for a given pattern.
+``flow hash {port_id} template_table`` calculates the hash for a given pattern.
 It is bound to ``rte_flow_calc_table_hash()``::
 
    flow hash {port_id} template_table {table_id}
@@ -3316,6 +3316,25 @@ Otherwise, it will show an error message of the form::
 This command uses the same pattern items as ``flow create``,
 their format is described in `Creating flow rules`_.
 
+
+Simulate encap hash calculation
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+``flow hash {port_id} encap`` adds hash query, that returns the hash value
+that the HW will calculate when encapsulating a packet.
+
+   flow hash {port_id} encap {target field} pattern {item} [/ {item} [...]] / end
+
+If successful, it will show::
+
+   encap hash result #[...]
+
+The value will be shown as uint16_t without endian conversion.
+
+Otherwise it will show an error message of the form::
+
+   Failed to calculate encap hash - [...]
+
 Creating a tunnel stub for offload
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Signature · 1 line
2.34.1
Acked-by on v4 4/4
#28 lore raw

Re: [PATCH v4 4/4] app/testpmd: add encap hash calculation

Quoted 5 lines · Ori Kam

This commits add support for calculating the encap hash.

Command structure:
flow hash {port} encap {target field} pattern {item} [/ {item} [...] ] / end

Quoted 7 lines

Example:
calculate hash to be used by VXLAN encapsulation.
flow hash 0 encap hash_field_sport pattern ipv4 dst is 7.7.7.7 src is 8.8.8.8 / udp dst is 5678 src is 1234 / end

Acked-by on v4 1/4
#29 lore raw

Re: [PATCH v4 1/4] ethdev: introduce encap hash calculation

In reply to Ori Kam
Quoted 20 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

Quoted 7 lines

the new function rte_flow_calc_encap_hash can query the hash value from the driver for a given packet as if the packet was passed through the HW.

#30 lore raw

Re: [PATCH v4 1/4] ethdev: introduce encap hash calculation

Quoted 25 lines · Ferruh Yigit
Quoted 24 lines · Ori Kam

During encapsulation of a packet, it is possible to change some outer headers to improve flow destribution.
For example, from VXLAN RFC:
"It is recommended that the UDP source port number
be calculated using a hash of fields from the inner packet -- one example being a hash of the inner Ethernet frame's headers. This is to enable a level of entropy for the ECMP/load-balancing"

The tunnel protocol defines which outer field should hold this hash, but it doesn't define the hash calculation algorithm.

An application that uses flow offloads gets the first few packets (exception path) and then decides to offload the flow. As a result, there are two
different paths that a packet from a given flow may take. SW for the first few packets or HW for the rest.
When the packet goes through the SW, the SW encapsulates the packet and must use the same hash calculation as the HW will do for the rest of the packets in this flow.

the new function rte_flow_calc_encap_hash can query the hash value from the driver for a given packet as if the packet was passed through the HW.

Quoted 6 lines
Quoted 3 lines

Series applied to dpdk-next-net/main, thanks.