xref: /freebsd/tools/tools/netmap/pkt_hash.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /*
2  ** Copyright (c) 2015, Asim Jamshed, Robin Sommer, Seth Hall
3  ** and the International Computer Science Institute. All rights reserved.
4  **
5  ** Redistribution and use in source and binary forms, with or without
6  ** modification, are permitted provided that the following conditions are met:
7  **
8  ** (1) Redistributions of source code must retain the above copyright
9  **     notice, this list of conditions and the following disclaimer.
10  **
11  ** (2) Redistributions in binary form must reproduce the above copyright
12  **     notice, this list of conditions and the following disclaimer in the
13  **     documentation and/or other materials provided with the distribution.
14  **
15  **
16  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  ** POSSIBILITY OF SUCH DAMAGE.
27  **/
28 /* $FreeBSD$ */
29 #ifndef LB_PKT_HASH_H
30 #define LB_PKT_HASH_H
31 /*---------------------------------------------------------------------*/
32 /**
33  ** Packet header hashing function utility - This file contains functions
34  ** that parse the packet headers and computes hash functions based on
35  ** the header fields. Please see pkt_hash.c for more details...
36  **/
37 /*---------------------------------------------------------------------*/
38 /* for type def'n */
39 #include <stdint.h>
40 /*---------------------------------------------------------------------*/
41 #ifdef __GNUC__
42 #define likely(x)       __builtin_expect(!!(x), 1)
43 #define unlikely(x)     __builtin_expect(!!(x), 0)
44 #else
45 #define likely(x)       (x)
46 #define unlikely(x)     (x)
47 #endif
48 
49 #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | \
50 		  (((unsigned short)(n) & 0xFF00) >> 8))
51 #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | \
52 		  (((unsigned short)(n) & 0xFF00) >> 8))
53 
54 #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
55         ((((unsigned long)(n) & 0xFF00)) << 8) | \
56         ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
57 		  ((((unsigned long)(n) & 0xFF000000)) >> 24))
58 
59 #define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
60         ((((unsigned long)(n) & 0xFF00)) << 8) | \
61         ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
62 		  ((((unsigned long)(n) & 0xFF000000)) >> 24))
63 /*---------------------------------------------------------------------*/
64 typedef struct vlanhdr {
65 	uint16_t pri_cfi_vlan;
66 	uint16_t proto;
67 } vlanhdr;
68 /*---------------------------------------------------------------------*/
69 /**
70  ** Analyzes the packet header of computes a corresponding
71  ** hash function.
72  **/
73 uint32_t
74 pkt_hdr_hash(const unsigned char *buffer,
75 	     uint8_t hash_split,
76 	     uint8_t seed);
77 /*---------------------------------------------------------------------*/
78 #endif /* LB_PKT_HASH_H */
79 
80