hashref/calc_single_hash/calc_single_hash.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Description

This source file shows how to use the hash reference library to calculate the hash value(s) for some variants of key data.

The following NTAPI functions are used:

Prerequisites

  • The driver must be installed such that the utility library and its accompanying header files are available. The example program does not require that a Napatech accelerator to be installed, or the driver the be up and running, or for a valid configuration file.

Code

/* * %NT_SOFTWARE_LICENSE% */ /** * @example hashref/calc_single_hash/calc_single_hash.c * @section calc_single_hash_description Description * * This source file shows how to use the hash reference library * to calculate the hash value(s) for some variants of key data. * * The following NTAPI functions are used: * - @ref NT_HashRefOpen() * - @ref NT_HashRefCalc() * - @ref NT_HashRefClose() * * @section calc_single_hash_prerequisites Prerequisites * * - The driver must be installed such that the utility library and * its accompanying header files are available. The example program * does not require that a Napatech accelerator to be installed, or the driver the be * up and running, or for a valid configuration file. * *<hr> * @section calc_single_hash_code Code * @} */ #include <string.h> /* memset */ #include <stdio.h> /* printf */ #ifdef _WIN32 #include <winsock2.h> /* htonl, htons */ #else #include <arpa/inet.h> /* htonl, htons */ #endif #include "ntutil.h" /* Hash reference library */ static int set_adapter_info(NtHashRefConfig_t *config, enum NtHashRefHashMode_e hash_mode) { config->config = NT_HASHREF_CONFIG_V0; config->u.config_v0.adapterType = NT_ADAPTER_TYPE_NT20E2; config->u.config_v0.fpgaid.s.item = 200; config->u.config_v0.fpgaid.s.product = 9220; config->u.config_v0.fpgaid.s.ver = 50; config->u.config_v0.fpgaid.s.rev = 3; config->u.config_v0.fpgaid.s.build = 0; config->u.config_v0.hashmode = hash_mode; /* Set the hash mask to its default value */ (void)memset(config->u.config_v0.hashmask, 0xff, sizeof(config->u.config_v0.hashmask)); config->u.config_v0.streams = 4; /* Distribute to four streams */ config->u.config_v0.seed = 0xffffffff; /* ntservice default */ return 0; } static int do_2tuple_ipv4(void) { NtHashRefConfig_t config; NtHashRef_t href; NtHashRefInput_t input; NtHashRefResult_t result; int rc; struct NtTuple2IPv4_s *hinput = &input.u.tuple2IPv4; (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_2_TUPLE); rc = NT_HashRefOpen(&href, &config); if (rc) { (void)fprintf(stderr, "Failed to open hash reference library\n"); return rc; } /* Note that the hash mode must correspond to the input type input.hashinput.tuple2IPv4, as specified by input.hashInputType. */ input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_2_IP_V4; hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("2-tuple hash of (src = 10.10.10.10, dst = 192.168.1.1):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); /* Swap the two IP addresses */ hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("2-tuple hash of (src = 192.168.1.1, dst = 10.10.10.10):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); rc = NT_HashRefClose(href); if (rc) { (void)fprintf(stderr, "Failed to close hash reference library\n"); return rc; } return 0; } static int do_2tuple_ipv6(void) { NtHashRefConfig_t config; NtHashRef_t href; NtHashRefInput_t input; NtHashRefResult_t result; int rc; const unsigned char src_ip[16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x94, 0x6b, 0xff, 0xfe, 0x6a, 0x09, 0x48 }; const unsigned char dst_ip[16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x39, 0x10, 0x12, 0x90, 0x44, 0x01 }; struct NtTuple2IPv6_s *hinput = &input.u.tuple2IPv6; (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_2_TUPLE); rc = NT_HashRefOpen(&href, &config); if (rc) { (void)fprintf(stderr, "Failed to open hash reference library\n"); return rc; } /* Note that the hash mode must correspond to the input type input.hashinput.tuple2IPv6, as specified by input.hashInputType. */ input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_2_IP_V6; (void)memcpy(hinput->srcIP, src_ip, sizeof(src_ip)); (void)memcpy(hinput->dstIP, dst_ip, sizeof(dst_ip)); rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("2-tuple hash of (src = fe80::5a94:6bff:fe6a:948,\n" "dst = fe80::1234:3910:1290:4401)\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); /* Swap the two IP addresses */ (void)memcpy(hinput->srcIP, dst_ip, sizeof(dst_ip)); (void)memcpy(hinput->dstIP, src_ip, sizeof(src_ip)); rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("2-tuple hash of (src = fe80::1234:3910:1290:4401,\n" "dst = fe80::5a94:6bff:fe6a:948)\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); rc = NT_HashRefClose(href); if (rc) { (void)fprintf(stderr, "Failed to close hash reference library\n"); return rc; } return 0; } static int do_5tuple_ipv4(void) { NtHashRefConfig_t config; NtHashRef_t href; NtHashRefInput_t input; NtHashRefResult_t result; int rc; struct NtTuple5IPv4_s *hinput = &input.u.tuple5IPv4; (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_5_TUPLE); rc = NT_HashRefOpen(&href, &config); if (rc) { (void)fprintf(stderr, "Failed to open hash reference library\n"); return rc; } /* Note that the hash mode must correspond to the input type input.hashinput.tuple5IPv4, as specified by input.hashInputType. */ input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_5_IP_V4; hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ hinput->srcPort = htons(14532); hinput->dstPort = htons(80); hinput->protocol = 4; rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("5-tuple hash of (src = 10.10.10.10, dst = 192.168.1.1, " "src_port = 14532, dst_port = 80, protocol = 4):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); /* Swap IP addresses (and ports) */ hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ hinput->dstPort = htons(14532); hinput->srcPort = htons(80); rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("5-tuple hash of (src = 192.168.1.1, dst = 10.10.10.10, " "src_port = 14532, dst_port = 80, protocol = 4):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); rc = NT_HashRefClose(href); if (rc) { (void)fprintf(stderr, "Failed to close hash reference library\n"); return rc; } return 0; } static int do_5tuple_ipv6(void) { NtHashRefConfig_t config; NtHashRef_t href; NtHashRefInput_t input; NtHashRefResult_t result; int rc; const unsigned char src_ip[16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x94, 0x6b, 0xff, 0xfe, 0x6a, 0x09, 0x48 }; const unsigned char dst_ip[16] = { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x39, 0x10, 0x12, 0x90, 0x44, 0x01 }; struct NtTuple5IPv6_s *hinput = &input.u.tuple5IPv6; (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_5_TUPLE); rc = NT_HashRefOpen(&href, &config); if (rc) { (void)fprintf(stderr, "Failed to open hash reference library\n"); return rc; } /* Note that the hash mode must correspond to the input type input.hashinput.tuple5IPv6, as specified by input.hashInputType. */ input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_5_IP_V6; assert(sizeof(hinput->srcIP) >= sizeof(src_ip)); assert(sizeof(hinput->dstIP) >= sizeof(dst_ip)); (void)memcpy(hinput->srcIP, src_ip, sizeof(src_ip)); (void)memcpy(hinput->dstIP, dst_ip, sizeof(dst_ip)); hinput->srcPort = htons(14532); hinput->dstPort = htons(80); hinput->protocol = 4; rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("5-tuple hash of (src = fe80::5a94:6bff:fe6a:948, " "dst = fe80::1234:3910:1290:4401, dst_port = 80, protocol = 4):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); /* Swap IP addresses (and ports) */ (void)memcpy(hinput->srcIP, dst_ip, sizeof(dst_ip)); (void)memcpy(hinput->dstIP, src_ip, sizeof(src_ip)); hinput->dstPort = htons(14532); hinput->srcPort = htons(80); rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("5-tuple hash of (src = fe80::1234:3910:1290:4401, " "dst = fe80::5a94:6bff:fe6a:948, dst_port = 80, protocol = 4):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); rc = NT_HashRefClose(href); if (rc) { (void)fprintf(stderr, "Failed to close hash reference library\n"); return rc; } return 0; } static int do_ipfragmenttuple_ipv4(void) { NtHashRefConfig_t config; NtHashRef_t href; NtHashRefInput_t input; NtHashRefResult_t result; int rc; struct NtIpFragmentTupleIPv4_s *hinput = &input.u.ipFragmentTupleIPv4; (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_IP_FRAGMENT_TUPLE); rc = NT_HashRefOpen(&href, &config); if (rc) { (void)fprintf(stderr, "Failed to open hash reference library\n"); return rc; } /* Note that the hash mode must correspond to the input type input.hashinput.ipFragmentTupleIPv4, as specified by input.hashInputType. */ input.inputType = NT_HASHREF_INPUT_TYPE_IP_FRAGMENT_TUPLE_IP_V4; hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ hinput->ipId = htons(0x1234); /* 16 bits IP ID */ hinput->ipProt = (uint8_t)1; /* 1 = ICMP */ rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("IP fragment hash of (src = 10.10.10.10, dst = 192.168.1.1):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ rc = NT_HashRefCalc(href, &input, &result); if (rc) { (void)fprintf(stderr, "Cannot calculate hash value\n"); return rc; } (void)printf("IP fragment hash of (src = 192.168.1.1, dst = 10.10.10.10):\n" "\tvalue = 0x%x, stream number = %d\n", result.hashvalue, result.stream); rc = NT_HashRefClose(href); if (rc) { (void)fprintf(stderr, "Failed to close hash reference library\n"); return rc; } return 0; } int main(void) { (void)do_2tuple_ipv4(); (void)do_2tuple_ipv6(); (void)do_5tuple_ipv4(); (void)do_5tuple_ipv6(); (void)do_ipfragmenttuple_ipv4(); return 0; }