Whamcloud - gitweb
b=22745 Removed gmlnd from lnet.
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_private.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/libcfs_private.h
37  *
38  * Various defines for libcfs.
39  *
40  */
41
42 #ifndef __LIBCFS_PRIVATE_H__
43 #define __LIBCFS_PRIVATE_H__
44
45 /* XXX this layering violation is for nidstrings */
46 #include <lnet/types.h>
47
48 #ifndef DEBUG_SUBSYSTEM
49 # define DEBUG_SUBSYSTEM S_UNDEFINED
50 #endif
51
52 #ifdef __KERNEL__
53
54 #ifdef LIBCFS_DEBUG
55
56 /*
57  * When this is on, LASSERT macro includes check for assignment used instead
58  * of equality check, but doesn't have unlikely(). Turn this on from time to
59  * time to make test-builds. This shouldn't be on for production release.
60  */
61 #define LASSERT_CHECKED (0)
62
63 #if LASSERT_CHECKED
64 /*
65  * Assertion.
66  *
67  * Strange construction with empty "then" clause is used to trigger compiler
68  * warnings on the assertions of the form LASSERT(a = b);
69  *
70  * "warning: suggest parentheses around assignment used as truth value"
71  *
72  * requires -Wall. Unfortunately this rules out use of likely/unlikely.
73  */
74 #define LASSERT(cond)                                                   \
75 do {                                                                    \
76         if (cond)                                                       \
77                 ;                                                       \
78         else                                                            \
79                 libcfs_assertion_failed( #cond , __FILE__,              \
80                         __FUNCTION__, __LINE__);                        \
81 } while(0)
82
83 #define LASSERTF(cond, fmt, ...)                                        \
84 do {                                                                    \
85          if (cond)                                                      \
86                  ;                                                      \
87          else {                                                         \
88                  libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,       \
89                                   __FILE__, __FUNCTION__,__LINE__,      \
90                                   "ASSERTION(" #cond ") failed: " fmt,  \
91                                   ## __VA_ARGS__);                      \
92                  LBUG();                                                \
93          }                                                              \
94 } while(0)
95
96 #else /* !LASSERT_CHECKED */
97
98 #define LASSERT(cond)                                                   \
99 do {                                                                    \
100         if (unlikely(!(cond)))                                          \
101                 libcfs_assertion_failed(#cond , __FILE__,               \
102                         __FUNCTION__, __LINE__);                        \
103 } while(0)
104
105 #define LASSERTF(cond, fmt, ...)                                        \
106 do {                                                                    \
107         if (unlikely(!(cond))) {                                        \
108                 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,        \
109                                  __FILE__, __FUNCTION__,__LINE__,       \
110                                  "ASSERTION(" #cond ") failed: " fmt,   \
111                                  ## __VA_ARGS__ );                      \
112                 LBUG();                                                 \
113         }                                                               \
114 } while(0)
115
116 #endif /* !LASSERT_CHECKED */
117 #else /* !LIBCFS_DEBUG */
118 /* sizeof is to use expression without evaluating it. */
119 # define LASSERT(e) ((void)sizeof!!(e))
120 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
121 #endif /* !LIBCFS_DEBUG */
122
123 #ifdef INVARIANT_CHECK
124 /**
125  * This is for more expensive checks that one doesn't want to be enabled all
126  * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
127  * configure option.
128  */
129 # define LINVRNT(exp) LASSERT(exp)
130 #else
131 # define LINVRNT(exp) ((void)sizeof!!(exp))
132 #endif
133
134 #define KLASSERT(e) LASSERT(e)
135
136 void lbug_with_loc(const char *file, const char *func, const int line)
137         __attribute__((noreturn));
138
139 #define LBUG() lbug_with_loc(__FILE__, __FUNCTION__, __LINE__)
140
141 extern cfs_atomic_t libcfs_kmemory;
142 /*
143  * Memory
144  */
145 #ifdef LIBCFS_DEBUG
146
147 # define libcfs_kmem_inc(ptr, size)             \
148 do {                                            \
149         cfs_atomic_add(size, &libcfs_kmemory);  \
150 } while (0)
151
152 # define libcfs_kmem_dec(ptr, size) do {        \
153         cfs_atomic_sub(size, &libcfs_kmemory);  \
154 } while (0)
155
156 #else
157 # define libcfs_kmem_inc(ptr, size) do {} while (0)
158 # define libcfs_kmem_dec(ptr, size) do {} while (0)
159 #endif /* LIBCFS_DEBUG */
160
161 #define LIBCFS_VMALLOC_SIZE        (2 << CFS_PAGE_SHIFT) /* 2 pages */
162
163 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                 \
164 do {                                                                      \
165         LASSERT(!cfs_in_interrupt() ||                                    \
166                (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
167         if (unlikely((size) > LIBCFS_VMALLOC_SIZE))                       \
168                 (ptr) = cfs_alloc_large(size);                            \
169         else                                                              \
170                 (ptr) = cfs_alloc((size), (mask));                        \
171         if (unlikely((ptr) == NULL)) {                                    \
172                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"   \
173                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
174                 CERROR("LNET: %d total bytes allocated by lnet\n",        \
175                        cfs_atomic_read(&libcfs_kmemory));                 \
176                 break;                                                    \
177         }                                                                 \
178         libcfs_kmem_inc((ptr), (size));                                   \
179         memset((ptr), 0, (size));                                         \
180         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
181                (int)(size), (ptr), cfs_atomic_read (&libcfs_kmemory));    \
182 } while (0)
183
184 #define LIBCFS_ALLOC(ptr, size) \
185         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
186
187 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
188         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
189
190 #define LIBCFS_FREE(ptr, size)                                          \
191 do {                                                                    \
192         int s = (size);                                                 \
193         if (unlikely((ptr) == NULL)) {                                  \
194                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
195                        "%s:%d\n", s, __FILE__, __LINE__);               \
196                 break;                                                  \
197         }                                                               \
198         libcfs_kmem_dec((ptr), s);                                      \
199         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
200                s, (ptr), cfs_atomic_read(&libcfs_kmemory));             \
201         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
202                 cfs_free_large(ptr);                                    \
203         else                                                            \
204                 cfs_free(ptr);                                          \
205 } while (0)
206
207 /******************************************************************************/
208
209 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
210 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
211 #define ___htonl(x) __cpu_to_be32(x)
212 #define ___htons(x) __cpu_to_be16(x)
213 #define ___ntohl(x) __be32_to_cpu(x)
214 #define ___ntohs(x) __be16_to_cpu(x)
215 #define htonl(x) ___htonl(x)
216 #define ntohl(x) ___ntohl(x)
217 #define htons(x) ___htons(x)
218 #define ntohs(x) ___ntohs(x)
219 #endif
220
221 void libcfs_debug_dumpstack(cfs_task_t *tsk);
222 void libcfs_run_upcall(char **argv);
223 void libcfs_run_lbug_upcall(const char * file, const char *fn, const int line);
224 void libcfs_debug_dumplog(void);
225 int libcfs_debug_init(unsigned long bufsize);
226 int libcfs_debug_cleanup(void);
227 int libcfs_debug_clear_buffer(void);
228 int libcfs_debug_mark_buffer(const char *text);
229
230 void libcfs_debug_set_level(unsigned int debug_level);
231
232 #else  /* !__KERNEL__ */
233 # ifdef LIBCFS_DEBUG
234 #  undef NDEBUG
235 #  include <assert.h>
236 #  define LASSERT(e)     assert(e)
237 #  define LASSERTF(cond, ...)                                                  \
238 do {                                                                           \
239           if (!(cond))                                                         \
240                 CERROR(__VA_ARGS__);                                           \
241           assert(cond);                                                        \
242 } while (0)
243 #  define LBUG()   assert(0)
244 #  ifdef INVARIANT_CHECK
245 #   define LINVRNT(exp) LASSERT(exp)
246 #  else
247 #   define LINVRNT(exp) ((void)sizeof!!(exp))
248 #  endif
249 # else
250 #  define LASSERT(e) ((void)sizeof!!(e))
251 #  define LASSERTF(cond, ...) ((void)sizeof!!(cond))
252 #  define LBUG()   ((void)(0))
253 #  define LINVRNT(exp) ((void)sizeof!!(exp))
254 # endif /* LIBCFS_DEBUG */
255 # define KLASSERT(e) ((void)0)
256 # define printk printf
257 # ifdef CRAY_XT3                                /* buggy calloc! */
258 #  define LIBCFS_ALLOC(ptr, size)               \
259    do {                                         \
260         (ptr) = malloc(size);                   \
261         memset(ptr, 0, size);                   \
262    } while (0)
263 # else
264 #  define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
265 # endif
266 # define LIBCFS_FREE(a, b) do { free(a); } while (0)
267
268 void libcfs_debug_dumplog(void);
269 int libcfs_debug_init(unsigned long bufsize);
270 int libcfs_debug_cleanup(void);
271
272 #define libcfs_debug_dumpstack(tsk)     ((void)0)
273
274 /*
275  * Generic compiler-dependent macros required for kernel
276  * build go below this comment. Actual compiler/compiler version
277  * specific implementations come from the above header files
278  */
279 #ifdef __GNUC__
280 #define likely(x)      __builtin_expect(!!(x), 1)
281 #define unlikely(x)    __builtin_expect(!!(x), 0)
282 #else
283 #define likely(x)       (!!(x))
284 #define unlikely(x)     (!!(x))
285 #endif
286 /* !__KERNEL__ */
287 #endif
288
289 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
290 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
291
292 /** Compile-time assertion.
293
294  * Check an invariant described by a constant expression at compile time by
295  * forcing a compiler error if it does not hold.  \a cond must be a constant
296  * expression as defined by the ISO C Standard:
297  *
298  *       6.8.4.2  The switch statement
299  *       ....
300  *       [#3] The expression of each case label shall be  an  integer
301  *       constant   expression  and  no  two  of  the  case  constant
302  *       expressions in the same switch statement shall have the same
303  *       value  after  conversion...
304  *
305  */
306 #define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
307
308 /* support decl needed both by kernel and liblustre */
309 int         libcfs_isknown_lnd(int type);
310 char       *libcfs_lnd2modname(int type);
311 char       *libcfs_lnd2str(int type);
312 int         libcfs_str2lnd(const char *str);
313 char       *libcfs_net2str(__u32 net);
314 char       *libcfs_nid2str(lnet_nid_t nid);
315 __u32       libcfs_str2net(const char *str);
316 lnet_nid_t  libcfs_str2nid(const char *str);
317 int         libcfs_str2anynid(lnet_nid_t *nid, const char *str);
318 char       *libcfs_id2str(lnet_process_id_t id);
319 int         cfs_iswhite(char c);
320 void        cfs_free_nidlist(cfs_list_t *list);
321 int         cfs_parse_nidlist(char *str, int len, cfs_list_t *list);
322 int         cfs_match_nid(lnet_nid_t nid, cfs_list_t *list);
323
324 /* how an LNET NID encodes net:address */
325 #define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
326 #define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
327 #define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
328 /* how net encodes type:number */
329 #define LNET_NETNUM(net)       ((net) & 0xffff)
330 #define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
331 #define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
332
333 /* max value for numeric network address */
334 #define MAX_NUMERIC_VALUE 0xffffffff
335
336 /* implication */
337 #define ergo(a, b) (!(a) || (b))
338 /* logical equivalence */
339 #define equi(a, b) (!!(a) == !!(b))
340
341 #ifndef CFS_CURRENT_TIME
342 # define CFS_CURRENT_TIME time(0)
343 #endif
344
345 /* --------------------------------------------------------------------
346  * Light-weight trace
347  * Support for temporary event tracing with minimal Heisenberg effect.
348  * All stuff about lwt are put in arch/kp30.h
349  * -------------------------------------------------------------------- */
350
351 struct libcfs_device_userstate
352 {
353         int           ldu_memhog_pages;
354         cfs_page_t   *ldu_memhog_root_page;
355 };
356
357 /* what used to be in portals_lib.h */
358 #ifndef MIN
359 # define MIN(a,b) (((a)<(b)) ? (a): (b))
360 #endif
361 #ifndef MAX
362 # define MAX(a,b) (((a)>(b)) ? (a): (b))
363 #endif
364
365 #define MKSTR(ptr) ((ptr))? (ptr) : ""
366
367 static inline int cfs_size_round4 (int val)
368 {
369         return (val + 3) & (~0x3);
370 }
371
372 static inline int cfs_size_round (int val)
373 {
374         return (val + 7) & (~0x7);
375 }
376
377 static inline int cfs_size_round16(int val)
378 {
379         return (val + 0xf) & (~0xf);
380 }
381
382 static inline int cfs_size_round32(int val)
383 {
384         return (val + 0x1f) & (~0x1f);
385 }
386
387 static inline int cfs_size_round0(int val)
388 {
389         if (!val)
390                 return 0;
391         return (val + 1 + 7) & (~0x7);
392 }
393
394 static inline size_t cfs_round_strlen(char *fset)
395 {
396         return (size_t)cfs_size_round((int)strlen(fset) + 1);
397 }
398
399 #define LOGL(var,len,ptr)                                       \
400 do {                                                            \
401         if (var)                                                \
402                 memcpy((char *)ptr, (const char *)var, len);    \
403         ptr += cfs_size_round(len);                             \
404 } while (0)
405
406 #define LOGU(var,len,ptr)                                       \
407 do {                                                            \
408         if (var)                                                \
409                 memcpy((char *)var, (const char *)ptr, len);    \
410         ptr += cfs_size_round(len);                             \
411 } while (0)
412
413 #define LOGL0(var,len,ptr)                              \
414 do {                                                    \
415         if (!len)                                       \
416                 break;                                  \
417         memcpy((char *)ptr, (const char *)var, len);    \
418         *((char *)(ptr) + len) = 0;                     \
419         ptr += cfs_size_round(len + 1);                 \
420 } while (0)
421
422 /**
423  *  Lustre Network Driver types.
424  */
425 enum {
426         /* Only add to these values (i.e. don't ever change or redefine them):
427          * network addresses depend on them... */
428         QSWLND    = 1,
429         SOCKLND   = 2,
430         GMLND     = 3, /* obsolete, keep it so that libcfs_nid2str works */
431         PTLLND    = 4,
432         O2IBLND   = 5,
433         CIBLND    = 6,
434         OPENIBLND = 7,
435         IIBLND    = 8,
436         LOLND     = 9,
437         RALND     = 10,
438         VIBLND    = 11,
439         MXLND     = 12,
440 };
441
442 #endif