Search code examples
c++dpdkmellanoxmultiqueue

rss hash dpdk compute as NIC


I am using mellanox Connectx6, dpdk 22

'MT2892 Family [ConnectX-6 Dx] 101d' if=ens5f1 drv=mlx5_core unused=igb_uio

I configure port with multiqueue and split traffic according to ip+port

I want to calculate the hash as the nic do, to be able to load balance traffic ( from another card ) - the information is inside the packet and not in in ip and transport layer. For this purpose i need to be able to calculate the hash value as the nic do for the first nic.

Here is the code i use to split traffic to rx queues

        /*rte flow*/
        const int MAX_PATTERN_IN_FLOW = 10;
        const int MAX_ACTIONS_IN_FLOW = 10;
        
        struct rte_flow_attr attr;
        
        struct rte_flow_item pattern[MAX_PATTERN_IN_FLOW];
        struct rte_flow_action actions[MAX_ACTIONS_IN_FLOW];
        struct rte_flow *flow;
        struct rte_flow_error error;

        memset(pattern, 0, sizeof(pattern));
        memset(actions, 0, sizeof(actions));

        /* Set the rule attribute, only ingress packets will be checked. 8< */
        memset(&attr, 0, sizeof(struct rte_flow_attr));
        attr.ingress = 1;

        pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
        pattern[0].spec = NULL;

        pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
        pattern[1].spec = NULL;

        pattern[2].type = RTE_FLOW_ITEM_TYPE_GRE;
        pattern[2].spec = NULL;

        pattern[3].type = RTE_FLOW_ITEM_TYPE_ETH;
        pattern[3].spec = NULL;

        pattern[4].type = RTE_FLOW_ITEM_TYPE_IPV4;
        pattern[4].spec = NULL;

        pattern[5].type = RTE_FLOW_ITEM_TYPE_UDP;
        pattern[5].spec = NULL;

        // end the pattern array 
        pattern[6].type = RTE_FLOW_ITEM_TYPE_END;

        struct rte_flow_action_rss rss_conf;
        uint16_t queues[pi_nNumRxQueues];
        rss_conf.func = RTE_ETH_HASH_FUNCTION_DEFAULT;
        uint64_t hf = RTE_ETH_RSS_IP | RTE_ETH_RSS_TCP | RTE_ETH_RSS_UDP | RTE_ETH_RSS_SCTP;
        hf &= pi_devInfo.flow_type_rss_offloads;
        rss_conf.types = hf;
        rss_conf.queue_num = pi_nNumRxQueues;
        for (int nqQueueIndex= 0; nqQueueIndex < pi_nNumRxQueues; nqQueueIndex++)
                queues[nqQueueIndex] = nqQueueIndex;

        rss_conf.queue = queues;
        
        rss_conf.key_len = 0;
        rss_conf.key = NULL;
        rss_conf.level = 2;

        // create the drop action 
        actions[0].type = RTE_FLOW_ACTION_TYPE_RSS;
        actions[0].conf = &rss_conf;
        actions[1].type = RTE_FLOW_ACTION_TYPE_END;

        // validate and create the flow rule 
        if (rte_flow_validate(pi_nPort, &attr, pattern, actions, &error)==0)
        {
            flow = rte_flow_create(pi_nPort, &attr, pattern, actions, &error);
            if(flow){//success}
            else{//error}
        }
        else {error}
        
    }

And this is how i tried to get the hash value saved in the mbuf, but failed

uint8_t rss_hash_default_key[] = {
    0x2c, 0xc6, 0x81, 0xd1,
    0x5b, 0xdb, 0xf4, 0xf7,
    0xfc, 0xa2, 0x83, 0x19,
    0xdb, 0x1a, 0x3e, 0x94,
    0x6b, 0x9e, 0x38, 0xd9,
    0x2c, 0x9c, 0x03, 0xd1,
    0xad, 0x99, 0x44, 0xa7,
    0xd9, 0x56, 0x3d, 0x59,
    0x06, 0x3c, 0x25, 0xf3,
    0xfc, 0x1f, 0xdc, 0x2a,
};

static inline uint32_t
do_softrss(struct rte_mbuf *m)
{
    uint32_t input_len;
    struct rte_ipv4_tuple ipv4_tuple;
    
    char * pRawPacket = static_cast<char*>(rte_pktmbuf_mtod(pi_mbuf, void* ));
    IpHeader * pIpHeader = (IpHeader *)(pRawPacket + offsetOfIp);
    if(pIpHeader->GetVersion()==4)
    {
        ipv4_tuple.src_addr = rte_be_to_cpu_32(pIpHeader->dwSrcAddressBigEndian);
        ipv4_tuple.dst_addr = rte_be_to_cpu_32(pIpHeader->dwDstAddressBigEndian);
        ipv4_tuple.sport = *(uint16_t*)(pRawPacket + transportLayerOffset);
        ipv4_tuple.dport = *(uint16_t*)(pRawPacket + transportLayerOffset+2);
        input_len = RTE_THASH_V4_L3_LEN;
        return rte_softrss_be((uint32_t *)&ipv4_tuple, input_len, rss_key_be);

    }
    return 0;
}
new_rss = do_softrss(mbuf_pointer);
std::cout<< std::hex << mbuf_pointer->hash.rss << " -> " << new_rss << std::dec << std::endl;

And i get a different value than the mbuf_pointer->hash.rss

5ed28a5c -> 33eb33eb
974c1896 -> 24e224e2
1edf1638 -> 21752175
8a54c19 -> 80638063
459a6f76 -> 1b351b35
1cdf1d1c -> e53be53b

I understand it is possible to do it, but i don't get the same value

ethtool -i ens5f0
driver: mlx5_core
version: 5.8-3.0.7
firmware-version: 22.32.2004 (MT_0000000437)
expansion-rom-version:
bus-info: 0000:83:00.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: yes

Solution

  • There might be two main reasons why the mismatch is observed:

    • L4 port numbers having unsuitable byte order when passed to rte_softrss_be
    • the use of RTE_THASH_V4_L3_LEN instead of RTE_THASH_V4_L4_LEN

    Given the OP code snippet, consider to fix this as follows:

    ipv4_tuple.src_addr = rte_be_to_cpu_32(pIpHeader->dwSrcAddressBigEndian);
    ipv4_tuple.dst_addr = rte_be_to_cpu_32(pIpHeader->dwDstAddressBigEndian);
    ipv4_tuple.sport = rte_be_to_cpu_16(*(uint16_t*)(pRawPacket + transportLayerOffset));
    ipv4_tuple.dport = rte_be_to_cpu_16(*(uint16_t*)(pRawPacket + transportLayerOffset+2));
    input_len = RTE_THASH_V4_L4_LEN;
    

    As for the key, please make sure that

    • the given key (rss_hash_default_key from the OP snippet) is passed to flow action RSS:
    rss_conf.key_len = RTE_DIM(rss_hash_default_key);
    rss_conf.key = rss_hash_default_key;
    
    • rss_key_be passed to rte_softrss_be is derived from rss_hash_default_key as follows:
    uint8_t rss_key_be[40];
    
    rte_convert_rss_key((uint32_t *)rss_hash_default_key,
                        (uint32_t *)rss_key_be,
                        RTE_DIM(rss_key_be));