calc_single_hash.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Go to the documentation of this file.

1/* 2 * %NT_SOFTWARE_LICENSE% 3 */ 4 5/** 6 * @example hashref/calc_single_hash/calc_single_hash.c 7 * @section calc_single_hash_description Description 8 * 9 * This source file shows how to use the hash reference library 10 * to calculate the hash value(s) for some variants of key data. 11 * 12 * The following NTAPI functions are used: 13 * - @ref NT_HashRefOpen() 14 * - @ref NT_HashRefCalc() 15 * - @ref NT_HashRefClose() 16 * 17 * @section calc_single_hash_prerequisites Prerequisites 18 * 19 * - The driver must be installed such that the utility library and 20 * its accompanying header files are available. The example program 21 * does not require that a Napatech accelerator to be installed, or the driver the be 22 * up and running, or for a valid configuration file. 23 * 24 *<hr> 25 * @section calc_single_hash_code Code 26 * @} 27 */ 28 29#include <string.h> /* memset */ 30#include <stdio.h> /* printf */ 31#ifdef _WIN32 32#include <winsock2.h> /* htonl, htons */ 33#else 34#include <arpa/inet.h> /* htonl, htons */ 35#endif 36 37#include "ntutil.h" /* Hash reference library */ 38 39static int 40set_adapter_info(NtHashRefConfig_t *config, enum NtHashRefHashMode_e hash_mode) 41{ 42 config->config = NT_HASHREF_CONFIG_V0; 43 config->u.config_v0.adapterType = NT_ADAPTER_TYPE_NT20E2; 44 config->u.config_v0.fpgaid.s.item = 200; 45 config->u.config_v0.fpgaid.s.product = 9220; 46 config->u.config_v0.fpgaid.s.ver = 50; 47 config->u.config_v0.fpgaid.s.rev = 3; 48 config->u.config_v0.fpgaid.s.build = 0; 49 config->u.config_v0.hashmode = hash_mode; 50 /* Set the hash mask to its default value */ 51 (void)memset(config->u.config_v0.hashmask, 0xff, 52 sizeof(config->u.config_v0.hashmask)); 53 config->u.config_v0.streams = 4; /* Distribute to four streams */ 54 config->u.config_v0.seed = 0xffffffff; /* ntservice default */ 55 56 return 0; 57} 58 59static int do_2tuple_ipv4(void) 60{ 61 NtHashRefConfig_t config; 62 NtHashRef_t href; 63 NtHashRefInput_t input; 64 NtHashRefResult_t result; 65 int rc; 66 struct NtTuple2IPv4_s *hinput = &input.u.tuple2IPv4; 67 68 (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_2_TUPLE); 69 70 rc = NT_HashRefOpen(&href, &config); 71 if (rc) { 72 (void)fprintf(stderr, "Failed to open hash reference library\n"); 73 return rc; 74 } 75 76 /* Note that the hash mode must correspond to the input type 77 input.hashinput.tuple2IPv4, as specified by input.hashInputType. */ 78 input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_2_IP_V4; 79 hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 80 hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 81 82 rc = NT_HashRefCalc(href, &input, &result); 83 if (rc) { 84 (void)fprintf(stderr, "Cannot calculate hash value\n"); 85 return rc; 86 } 87 88 (void)printf("2-tuple hash of (src = 10.10.10.10, dst = 192.168.1.1):\n" 89 "\tvalue = 0x%x, stream number = %d\n", 90 result.hashvalue, result.stream); 91 92 /* Swap the two IP addresses */ 93 hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 94 hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 95 96 rc = NT_HashRefCalc(href, &input, &result); 97 if (rc) { 98 (void)fprintf(stderr, "Cannot calculate hash value\n"); 99 return rc; 100 } 101 102 (void)printf("2-tuple hash of (src = 192.168.1.1, dst = 10.10.10.10):\n" 103 "\tvalue = 0x%x, stream number = %d\n", 104 result.hashvalue, result.stream); 105 106 rc = NT_HashRefClose(href); 107 if (rc) { 108 (void)fprintf(stderr, "Failed to close hash reference library\n"); 109 return rc; 110 } 111 112 return 0; 113} 114 115static int do_2tuple_ipv6(void) 116{ 117 NtHashRefConfig_t config; 118 NtHashRef_t href; 119 NtHashRefInput_t input; 120 NtHashRefResult_t result; 121 int rc; 122 const unsigned char src_ip[16] = 123 { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 124 0x5a, 0x94, 0x6b, 0xff, 0xfe, 0x6a, 0x09, 0x48 }; 125 const unsigned char dst_ip[16] = 126 { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 127 0x12, 0x34, 0x39, 0x10, 0x12, 0x90, 0x44, 0x01 }; 128 struct NtTuple2IPv6_s *hinput = &input.u.tuple2IPv6; 129 130 (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_2_TUPLE); 131 132 rc = NT_HashRefOpen(&href, &config); 133 if (rc) { 134 (void)fprintf(stderr, "Failed to open hash reference library\n"); 135 return rc; 136 } 137 138 /* Note that the hash mode must correspond to the input type 139 input.hashinput.tuple2IPv6, as specified by input.hashInputType. */ 140 input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_2_IP_V6; 141 (void)memcpy(hinput->srcIP, src_ip, sizeof(src_ip)); 142 (void)memcpy(hinput->dstIP, dst_ip, sizeof(dst_ip)); 143 144 rc = NT_HashRefCalc(href, &input, &result); 145 if (rc) { 146 (void)fprintf(stderr, "Cannot calculate hash value\n"); 147 return rc; 148 } 149 150 (void)printf("2-tuple hash of (src = fe80::5a94:6bff:fe6a:948,\n" 151 "dst = fe80::1234:3910:1290:4401)\n" 152 "\tvalue = 0x%x, stream number = %d\n", 153 result.hashvalue, result.stream); 154 155 /* Swap the two IP addresses */ 156 (void)memcpy(hinput->srcIP, dst_ip, sizeof(dst_ip)); 157 (void)memcpy(hinput->dstIP, src_ip, sizeof(src_ip)); 158 159 rc = NT_HashRefCalc(href, &input, &result); 160 if (rc) { 161 (void)fprintf(stderr, "Cannot calculate hash value\n"); 162 return rc; 163 } 164 165 166 (void)printf("2-tuple hash of (src = fe80::1234:3910:1290:4401,\n" 167 "dst = fe80::5a94:6bff:fe6a:948)\n" 168 "\tvalue = 0x%x, stream number = %d\n", 169 result.hashvalue, result.stream); 170 171 rc = NT_HashRefClose(href); 172 if (rc) { 173 (void)fprintf(stderr, "Failed to close hash reference library\n"); 174 return rc; 175 } 176 177 return 0; 178} 179 180static int do_5tuple_ipv4(void) 181{ 182 NtHashRefConfig_t config; 183 NtHashRef_t href; 184 NtHashRefInput_t input; 185 NtHashRefResult_t result; 186 int rc; 187 struct NtTuple5IPv4_s *hinput = &input.u.tuple5IPv4; 188 189 (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_5_TUPLE); 190 191 rc = NT_HashRefOpen(&href, &config); 192 if (rc) { 193 (void)fprintf(stderr, "Failed to open hash reference library\n"); 194 return rc; 195 } 196 197 /* Note that the hash mode must correspond to the input type 198 input.hashinput.tuple5IPv4, as specified by input.hashInputType. */ 199 input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_5_IP_V4; 200 hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 201 hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 202 hinput->srcPort = htons(14532); 203 hinput->dstPort = htons(80); 204 hinput->protocol = 4; 205 206 rc = NT_HashRefCalc(href, &input, &result); 207 if (rc) { 208 (void)fprintf(stderr, "Cannot calculate hash value\n"); 209 return rc; 210 } 211 212 (void)printf("5-tuple hash of (src = 10.10.10.10, dst = 192.168.1.1, " 213 "src_port = 14532, dst_port = 80, protocol = 4):\n" 214 "\tvalue = 0x%x, stream number = %d\n", 215 result.hashvalue, result.stream); 216 217 /* Swap IP addresses (and ports) */ 218 hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 219 hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 220 hinput->dstPort = htons(14532); 221 hinput->srcPort = htons(80); 222 223 rc = NT_HashRefCalc(href, &input, &result); 224 if (rc) { 225 (void)fprintf(stderr, "Cannot calculate hash value\n"); 226 return rc; 227 } 228 229 (void)printf("5-tuple hash of (src = 192.168.1.1, dst = 10.10.10.10, " 230 "src_port = 14532, dst_port = 80, protocol = 4):\n" 231 "\tvalue = 0x%x, stream number = %d\n", 232 result.hashvalue, result.stream); 233 234 rc = NT_HashRefClose(href); 235 if (rc) { 236 (void)fprintf(stderr, "Failed to close hash reference library\n"); 237 return rc; 238 } 239 240 return 0; 241} 242 243static int do_5tuple_ipv6(void) 244{ 245 NtHashRefConfig_t config; 246 NtHashRef_t href; 247 NtHashRefInput_t input; 248 NtHashRefResult_t result; 249 int rc; 250 const unsigned char src_ip[16] = 251 { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 252 0x5a, 0x94, 0x6b, 0xff, 0xfe, 0x6a, 0x09, 0x48 }; 253 const unsigned char dst_ip[16] = 254 { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 255 0x12, 0x34, 0x39, 0x10, 0x12, 0x90, 0x44, 0x01 }; 256 struct NtTuple5IPv6_s *hinput = &input.u.tuple5IPv6; 257 258 (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_5_TUPLE); 259 260 rc = NT_HashRefOpen(&href, &config); 261 if (rc) { 262 (void)fprintf(stderr, "Failed to open hash reference library\n"); 263 return rc; 264 } 265 266 /* Note that the hash mode must correspond to the input type 267 input.hashinput.tuple5IPv6, as specified by input.hashInputType. */ 268 input.inputType = NT_HASHREF_INPUT_TYPE_TUPLE_5_IP_V6; 269 assert(sizeof(hinput->srcIP) >= sizeof(src_ip)); 270 assert(sizeof(hinput->dstIP) >= sizeof(dst_ip)); 271 (void)memcpy(hinput->srcIP, src_ip, sizeof(src_ip)); 272 (void)memcpy(hinput->dstIP, dst_ip, sizeof(dst_ip)); 273 hinput->srcPort = htons(14532); 274 hinput->dstPort = htons(80); 275 hinput->protocol = 4; 276 277 rc = NT_HashRefCalc(href, &input, &result); 278 if (rc) { 279 (void)fprintf(stderr, "Cannot calculate hash value\n"); 280 return rc; 281 } 282 283 (void)printf("5-tuple hash of (src = fe80::5a94:6bff:fe6a:948, " 284 "dst = fe80::1234:3910:1290:4401, dst_port = 80, protocol = 4):\n" 285 "\tvalue = 0x%x, stream number = %d\n", 286 result.hashvalue, result.stream); 287 288 /* Swap IP addresses (and ports) */ 289 (void)memcpy(hinput->srcIP, dst_ip, sizeof(dst_ip)); 290 (void)memcpy(hinput->dstIP, src_ip, sizeof(src_ip)); 291 hinput->dstPort = htons(14532); 292 hinput->srcPort = htons(80); 293 294 rc = NT_HashRefCalc(href, &input, &result); 295 if (rc) { 296 (void)fprintf(stderr, "Cannot calculate hash value\n"); 297 return rc; 298 } 299 300 (void)printf("5-tuple hash of (src = fe80::1234:3910:1290:4401, " 301 "dst = fe80::5a94:6bff:fe6a:948, dst_port = 80, protocol = 4):\n" 302 "\tvalue = 0x%x, stream number = %d\n", 303 result.hashvalue, result.stream); 304 305 rc = NT_HashRefClose(href); 306 if (rc) { 307 (void)fprintf(stderr, "Failed to close hash reference library\n"); 308 return rc; 309 } 310 311 return 0; 312} 313 314static int do_ipfragmenttuple_ipv4(void) 315{ 316 NtHashRefConfig_t config; 317 NtHashRef_t href; 318 NtHashRefInput_t input; 319 NtHashRefResult_t result; 320 int rc; 321 struct NtIpFragmentTupleIPv4_s *hinput = &input.u.ipFragmentTupleIPv4; 322 323 (void)set_adapter_info(&config, NT_HASHREF_HASHMODE_IP_FRAGMENT_TUPLE); 324 325 rc = NT_HashRefOpen(&href, &config); 326 if (rc) { 327 (void)fprintf(stderr, "Failed to open hash reference library\n"); 328 return rc; 329 } 330 331 /* Note that the hash mode must correspond to the input type 332 input.hashinput.ipFragmentTupleIPv4, 333 as specified by input.hashInputType. */ 334 input.inputType = NT_HASHREF_INPUT_TYPE_IP_FRAGMENT_TUPLE_IP_V4; 335 hinput->srcIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 336 hinput->dstIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 337 hinput->ipId = htons(0x1234); /* 16 bits IP ID */ 338 hinput->ipProt = (uint8_t)1; /* 1 = ICMP */ 339 340 rc = NT_HashRefCalc(href, &input, &result); 341 if (rc) { 342 (void)fprintf(stderr, "Cannot calculate hash value\n"); 343 return rc; 344 } 345 346 (void)printf("IP fragment hash of (src = 10.10.10.10, dst = 192.168.1.1):\n" 347 "\tvalue = 0x%x, stream number = %d\n", 348 result.hashvalue, result.stream); 349 350 hinput->srcIP = htonl(0xc0a80101); /* IP V4: 192.168.1.1 */ 351 hinput->dstIP = htonl(0x0a0a0a0a); /* IP V4: 10.10.10.10 */ 352 353 rc = NT_HashRefCalc(href, &input, &result); 354 if (rc) { 355 (void)fprintf(stderr, "Cannot calculate hash value\n"); 356 return rc; 357 } 358 359 (void)printf("IP fragment hash of (src = 192.168.1.1, dst = 10.10.10.10):\n" 360 "\tvalue = 0x%x, stream number = %d\n", 361 result.hashvalue, result.stream); 362 363 rc = NT_HashRefClose(href); 364 if (rc) { 365 (void)fprintf(stderr, "Failed to close hash reference library\n"); 366 return rc; 367 } 368 369 return 0; 370} 371 372int main(void) 373{ 374 (void)do_2tuple_ipv4(); 375 (void)do_2tuple_ipv6(); 376 (void)do_5tuple_ipv4(); 377 (void)do_5tuple_ipv6(); 378 (void)do_ipfragmenttuple_ipv4(); 379 380 return 0; 381}