2 * Copyright (c) 2004-2007 Voltaire Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
37 #include <sys/select.h>
38 #include <sys/types.h>
41 #include <infiniband/byteswap.h>
44 # define BEGIN_C_DECLS extern "C" {
45 # define END_C_DECLS }
46 #else /* !__cplusplus */
47 # define BEGIN_C_DECLS
49 #endif /* __cplusplus */
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
55 static inline uint64_t ntohll(uint64_t x) {
60 static inline uint64_t htonll(uint64_t x) {
64 #elif __BYTE_ORDER == __BIG_ENDIAN
66 static inline uint64_t ntohll(uint64_t x) {
71 static inline uint64_t htonll(uint64_t x) {
75 #endif /* __BYTE_ORDER == __BIG_ENDIAN */
77 /*****************************
78 * COMMON MACHINE INDEPENDENT
82 /** align value \a l to \a size (ceil) */
84 #define ALIGN(l, size) (((l) + ((size) - 1)) / (size) * (size))
86 /** align value \a l to \a sizeof 32 bit int (ceil) */
87 #define ALIGN32(l) (ALIGN((l), sizeof(uint32)))
90 /** printf style debugging MACRO, conmmon header includes name of function */
91 #define IBWARN(fmt, args...) ibwarn(__FUNCTION__, fmt, ## args)
93 /** printf style debugging MACRO, conmmon header includes name of function */
94 #define LOG(fmt, args...) logmsg(__FUNCTION__, fmt, ## args)
96 /** printf style abort MACRO, common header includes name of function */
97 #define IBPANIC(fmt, args...) ibpanic(__FUNCTION__, fmt, ## args)
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",\
105 /** avoid unused compilation warning */
107 #define USED(x) while(0) {void *v = &(x); printf("%p", v);}
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[]
115 #define IBCOMMON_STRICT_FORMAT __attribute__((format(printf, 2, 3)))
117 #define IBCOMMON_STRICT_FORMAT
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;
125 void xdump(FILE *file, char *msg, void *p, int size);
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 **));
138 void stack_dump(void);
139 void enable_stack_dump(int loop);
142 uint64_t getcurrenttime(void);
145 uint32_t fhash(uint8_t *k, int length, uint32_t initval);
149 #endif /* __COMMON_H__ */