Implement functions for generating an RSS hash in SW for IPv4 and IPv6, with optional TCP/UDP src/dst included. Where the system supports AVX-512 + GFNI instruction set, those optimized paths are taken, otherwise a default scalar path is used.
Signed-off-by: Bruce Richardson <[email protected]> --- doc/guides/rel_notes/release_27_03.rst | 6 ++ lib/hash/rte_thash_rss.h | 87 ++++++++++++++++++++++++++ lib/hash/thash_rss.c | 78 +++++++++++++++++++++++ 3 files changed, 171 insertions(+) diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index a91813faca..0714b8bd46 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Added software RSS hashing for IP packets.** + + Added ``rte_thash_rss_*`` APIs to calculate RSS hashes for IPv4 and IPv6 addresses, + with optional TCP/UDP ports. + Runtime dispatch selects GFNI implementations when supported and uses scalar hashing otherwise. + Removed Items ------------- diff --git a/lib/hash/rte_thash_rss.h b/lib/hash/rte_thash_rss.h index f76dd667fb..274eab2503 100644 --- a/lib/hash/rte_thash_rss.h +++ b/lib/hash/rte_thash_rss.h @@ -14,6 +14,7 @@ #include <stdint.h> #include <rte_compat.h> +#include <rte_ip.h> #ifdef __cplusplus extern "C" { @@ -62,6 +63,92 @@ __rte_experimental void rte_thash_rss_ctx_free(struct rte_thash_rss_ctx *ctx); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Calculate an RSS hash over the source and destination IPv4 addresses. + * + * @param ctx + * RSS hash context. + * @param ip + * Pointer to a complete fixed IPv4 header structure. + * @param hash + * Location to store the calculated hash. + * @return + * 0 on success, or -EINVAL for invalid parameters. + */ +__rte_experimental +int +rte_thash_rss_ipv4_l3(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv4_hdr *ip, uint32_t *hash); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Calculate an RSS hash over the IPv4 addresses and TCP or UDP ports. + * + * @param ctx + * RSS hash context. + * @param ip + * Pointer to a complete fixed IPv4 header structure. + * @param l4_hdr + * Pointer to at least the first 4 bytes of the TCP or UDP header. + * The caller must ensure that L4 hashing is appropriate for the packet. + * @param hash + * Location to store the calculated hash. + * @return + * 0 on success, or -EINVAL for invalid parameters. + */ +__rte_experimental +int +rte_thash_rss_ipv4_l4(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv4_hdr *ip, const void *l4_hdr, uint32_t *hash); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Calculate an RSS hash over the source and destination IPv6 addresses. + * + * @param ctx + * RSS hash context. + * @param ip + * Pointer to a complete IPv6 header structure. + * @param hash + * Location to store the calculated hash. + * @return + * 0 on success, or -EINVAL for invalid parameters. + */ +__rte_experimental +int +rte_thash_rss_ipv6_l3(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv6_hdr *ip, uint32_t *hash); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Calculate an RSS hash over the IPv6 addresses and TCP or UDP ports. + * + * @param ctx + * RSS hash context. + * @param ip + * Pointer to a complete IPv6 header structure. + * @param l4_hdr + * Pointer to at least the first 4 bytes of the TCP or UDP header. + * The caller must ensure that L4 hashing is appropriate for the packet. + * @param hash + * Location to store the calculated hash. + * @return + * 0 on success, or -EINVAL for invalid parameters. + */ +__rte_experimental +int +rte_thash_rss_ipv6_l4(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv6_hdr *ip, const void *l4_hdr, uint32_t *hash); + #ifdef __cplusplus } #endif diff --git a/lib/hash/thash_rss.c b/lib/hash/thash_rss.c index c53e9dbefa..ddc07720a3 100644 --- a/lib/hash/thash_rss.c +++ b/lib/hash/thash_rss.c @@ -105,3 +105,81 @@ rte_thash_rss_ctx_free(struct rte_thash_rss_ctx *ctx) { free(ctx); } + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv4_l3, 27.03) +int +rte_thash_rss_ipv4_l3(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv4_hdr *ip, uint32_t *hash) +{ + if (ctx == NULL || ip == NULL || hash == NULL) + return -EINVAL; + + *hash = ctx->ops->hash_8(ctx, (const uint8_t *)&ip->src_addr); + return 0; +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv6_l3, 27.03) +int +rte_thash_rss_ipv6_l3(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv6_hdr *ip, uint32_t *hash) +{ + if (ctx == NULL || ip == NULL || hash == NULL) + return -EINVAL; + + *hash = ctx->ops->hash_32(ctx, (const uint8_t *)&ip->src_addr); + return 0; +} + +static __rte_noinline uint32_t +thash_ipv4_l4_gather(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv4_hdr *ip, const void *l4_hdr) +{ + uint8_t tuple[12]; + + memcpy(tuple, &ip->src_addr, 2 * sizeof(ip->src_addr)); + memcpy(&tuple[8], l4_hdr, 4); + return ctx->ops->hash_12(ctx, tuple); +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv4_l4, 27.03) +int +rte_thash_rss_ipv4_l4(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv4_hdr *ip, const void *l4_hdr, uint32_t *hash) +{ + if (ctx == NULL || ip == NULL || l4_hdr == NULL || hash == NULL) + return -EINVAL; + + if (l4_hdr == ip + 1) + *hash = ctx->ops->hash_12(ctx, (const uint8_t *)&ip->src_addr); + else + *hash = thash_ipv4_l4_gather(ctx, ip, l4_hdr); + + return 0; +} + +static __rte_noinline uint32_t +thash_ipv6_l4_gather(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv6_hdr *ip, const void *l4_hdr) +{ + uint8_t tuple[36]; + + memcpy(tuple, &ip->src_addr, 2 * sizeof(ip->src_addr)); + memcpy(&tuple[32], l4_hdr, 4); + return ctx->ops->hash_36(ctx, tuple); +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_thash_rss_ipv6_l4, 27.03) +int +rte_thash_rss_ipv6_l4(const struct rte_thash_rss_ctx *ctx, + const struct rte_ipv6_hdr *ip, const void *l4_hdr, uint32_t *hash) +{ + if (ctx == NULL || ip == NULL || l4_hdr == NULL || hash == NULL) + return -EINVAL; + + if (l4_hdr == ip + 1) + *hash = ctx->ops->hash_36(ctx, (const uint8_t *)&ip->src_addr); + else + *hash = thash_ipv6_l4_gather(ctx, ip, l4_hdr); + + return 0; +} -- 2.55.0

