1 /*
2  * Copyright (c) 2004-2007 Voltaire Inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #ifndef __COMMON_H__
34 #define __COMMON_H__
35 
36 #include <stdio.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
39 #include <dirent.h>
40 #include <stdint.h>
41 #include <infiniband/byteswap.h>
42 
43 #ifdef __cplusplus
44 #  define BEGIN_C_DECLS extern "C" {
45 #  define END_C_DECLS   }
46 #else /* !__cplusplus */
47 #  define BEGIN_C_DECLS
48 #  define END_C_DECLS
49 #endif /* __cplusplus */
50 
51 BEGIN_C_DECLS
52 
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
54 #ifndef ntohll
ntohll(uint64_t x)55 static inline uint64_t ntohll(uint64_t x) {
56 	return bswap_64(x);
57 }
58 #endif
59 #ifndef htonll
htonll(uint64_t x)60 static inline uint64_t htonll(uint64_t x) {
61 	return bswap_64(x);
62 }
63 #endif
64 #elif __BYTE_ORDER == __BIG_ENDIAN
65 #ifndef ntohll
66 static inline uint64_t ntohll(uint64_t x) {
67 	return x;
68 }
69 #endif
70 #ifndef htonll
71 static inline uint64_t htonll(uint64_t x) {
72 	return x;
73 }
74 #endif
75 #endif	/* __BYTE_ORDER == __BIG_ENDIAN */
76 
77 /*****************************
78  * COMMON MACHINE INDEPENDENT
79  */
80 
81 /* Misc. macros: */
82 /** align value \a l to \a size (ceil) */
83 #ifndef ALIGN
84 #define ALIGN(l, size) (((l) + ((size) - 1)) / (size) * (size))
85 
86 /** align value \a l to \a sizeof 32 bit int (ceil) */
87 #define ALIGN32(l) (ALIGN((l), sizeof(uint32)))
88 #endif
89 
90 /** printf style debugging MACRO, conmmon header includes name of function */
91 #define IBWARN(fmt, args...)	ibwarn(__FUNCTION__, fmt, ## args)
92 
93 /** printf style debugging MACRO, conmmon header includes name of function */
94 #define LOG(fmt, args...)	logmsg(__FUNCTION__, fmt, ## args)
95 
96 /** printf style abort MACRO, common header includes name of function */
97 #define IBPANIC(fmt, args...)	ibpanic(__FUNCTION__, fmt, ## args)
98 
99 /** abort program if expression \a x is \b false */
100 #define SANITY(x)		if (common.sanity && !(x))\
101 					ibpanic(__FUNCTION__,\
102 					"sanity check <%s> failed: line %d",\
103 					(x), __LINE__)
104 
105 /** avoid unused compilation warning  */
106 #ifndef USED
107 #define USED(x)	while(0) {void *v = &(x); printf("%p", v);}
108 #endif
109 
110 /** define index macro for string array generated by enumstr.awk */
111 #define ENUM_STR_DEF(enumname, last, val) 	(((unsigned)(val) < last) ? enumname ## _str[val] : "???")
112 #define ENUM_STR_ARRAY(name)		char * name ## _str[]
113 
114 #ifdef __GNUC__
115 #define IBCOMMON_STRICT_FORMAT __attribute__((format(printf, 2, 3)))
116 #else
117 #define IBCOMMON_STRICT_FORMAT
118 #endif
119 
120 /* util.c: debugging and tracing */
121 void	ibwarn(const char * const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
122 void	ibpanic(const char * const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
123 void	logmsg(const char *const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
124 
125 void	xdump(FILE *file, char *msg, void *p, int size);
126 
127 /* sysfs.c: /sys utilities */
128 int	sys_read_string(char *dir_name, char *file_name, char *str, int max_len);
129 int	sys_read_guid(char *dir_name, char *file_name, uint64_t *net_guid);
130 int	sys_read_gid(char *dir_name, char *file_name, uint8_t *gid);
131 int	sys_read_uint64(char *dir_name, char *file_name, uint64_t *u);
132 int	sys_read_uint(char *dir_name, char *file_name, unsigned *u);
133 int	sys_scandir(const char *dirname, struct dirent ***namelist,
134 	    int (*select)(const struct dirent *),
135 	    int (*compar)(const struct dirent **, const struct dirent **));
136 
137 /* stack.c */
138 void	stack_dump(void);
139 void	enable_stack_dump(int loop);
140 
141 /* time.c */
142 uint64_t getcurrenttime(void);
143 
144 /* hash.c */
145 uint32_t fhash(uint8_t *k, int length, uint32_t initval);
146 
147 END_C_DECLS
148 
149 #endif /* __COMMON_H__ */
150