xref: /freebsd/tools/tools/netmap/pkt_hash.h (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
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 #ifndef LB_PKT_HASH_H
29 #define LB_PKT_HASH_H
30 /*---------------------------------------------------------------------*/
31 /**
32  ** Packet header hashing function utility - This file contains functions
33  ** that parse the packet headers and computes hash functions based on
34  ** the header fields. Please see pkt_hash.c for more details...
35  **/
36 /*---------------------------------------------------------------------*/
37 /* for type def'n */
38 #include <stdint.h>
39 /*---------------------------------------------------------------------*/
40 #ifdef __GNUC__
41 #define likely(x)       __builtin_expect(!!(x), 1)
42 #define unlikely(x)     __builtin_expect(!!(x), 0)
43 #else
44 #define likely(x)       (x)
45 #define unlikely(x)     (x)
46 #endif
47 
48 #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | \
49 		  (((unsigned short)(n) & 0xFF00) >> 8))
50 #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | \
51 		  (((unsigned short)(n) & 0xFF00) >> 8))
52 
53 #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
54         ((((unsigned long)(n) & 0xFF00)) << 8) | \
55         ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
56 		  ((((unsigned long)(n) & 0xFF000000)) >> 24))
57 
58 #define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
59         ((((unsigned long)(n) & 0xFF00)) << 8) | \
60         ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
61 		  ((((unsigned long)(n) & 0xFF000000)) >> 24))
62 /*---------------------------------------------------------------------*/
63 typedef struct vlanhdr {
64 	uint16_t pri_cfi_vlan;
65 	uint16_t proto;
66 } vlanhdr;
67 /*---------------------------------------------------------------------*/
68 /**
69  ** Analyzes the packet header of computes a corresponding
70  ** hash function.
71  **/
72 uint32_t
73 pkt_hdr_hash(const unsigned char *buffer,
74 	     uint8_t hash_split,
75 	     uint8_t seed);
76 /*---------------------------------------------------------------------*/
77 #endif /* LB_PKT_HASH_H */
78 
79