Whamcloud - gitweb
replace tag with rtag to better deal with added/removed files
[fs/lustre-release.git] / lnet / include / libcfs / kp30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef __LIBCFS_KP30_H__
5 #define __LIBCFS_KP30_H__
6
7 #define PORTAL_DEBUG
8 #include <libcfs/libcfs.h>
9 #include <portals/types.h>
10
11 #if defined(__linux__)
12 #include <libcfs/linux/kp30.h>
13 #elif defined(__APPLE__)
14 #include <libcfs/darwin/kp30.h>
15 #else
16 #error Unsupported operating system
17 #endif
18
19 #include <portals/types.h>
20
21 #ifdef __KERNEL__
22
23 # ifndef DEBUG_SUBSYSTEM
24 #  define DEBUG_SUBSYSTEM S_UNDEFINED
25 # endif
26
27 #ifdef PORTAL_DEBUG
28 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
29                                      const int line);
30 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__,  \
31                                                         __FUNCTION__, __LINE__))
32 #define LASSERTF(cond, fmt...)                                                \
33         do {                                                                  \
34                 if (unlikely(!(cond))) {                                      \
35                         portals_debug_msg(DEBUG_SUBSYSTEM, D_EMERG,  __FILE__,\
36                                           __FUNCTION__,__LINE__, CDEBUG_STACK,\
37                                           "ASSERTION(" #cond ") failed:" fmt);\
38                         LBUG();                                               \
39                 }                                                             \
40         } while (0)
41
42 #else
43 #define LASSERT(e)
44 #define LASSERTF(cond, fmt...) do { } while (0)
45 #endif
46
47 /* LBUG_WITH_LOC defined in portals/<os>/kp30.h */
48 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
49
50 /*
51  * Memory
52  */
53 #ifdef PORTAL_DEBUG
54 extern atomic_t portal_kmemory;
55
56 # define portal_kmem_inc(ptr, size)                                           \
57 do {                                                                          \
58         atomic_add(size, &portal_kmemory);                                    \
59 } while (0)
60
61 # define portal_kmem_dec(ptr, size) do {                                      \
62         atomic_sub(size, &portal_kmemory);                                    \
63 } while (0)
64
65 #else
66 # define portal_kmem_inc(ptr, size) do {} while (0)
67 # define portal_kmem_dec(ptr, size) do {} while (0)
68 #endif /* PORTAL_DEBUG */
69
70 #define PORTAL_VMALLOC_SIZE        16384
71
72 #define PORTAL_ALLOC_GFP(ptr, size, mask)                                 \
73 do {                                                                      \
74         LASSERT(!in_interrupt() ||                                        \
75                (size <= PORTAL_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
76         if ((size) > PORTAL_VMALLOC_SIZE)                                 \
77                 (ptr) = cfs_alloc_large(size);                            \
78         else                                                              \
79                 (ptr) = cfs_alloc((size), (mask));                        \
80         if ((ptr) == NULL) {                                              \
81                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
82                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
83                 CERROR("PORTALS: %d total bytes allocated by portals\n",  \
84                        atomic_read(&portal_kmemory));                     \
85         } else {                                                          \
86                 portal_kmem_inc((ptr), (size));                           \
87                 if (!((mask) & CFS_ALLOC_ZERO))                           \
88                        memset((ptr), 0, (size));                          \
89         }                                                                 \
90         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
91                (int)(size), (ptr), atomic_read (&portal_kmemory));        \
92 } while (0)
93
94 #define PORTAL_ALLOC(ptr, size) \
95         PORTAL_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
96
97 #define PORTAL_ALLOC_ATOMIC(ptr, size) \
98         PORTAL_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
99
100 #define PORTAL_FREE(ptr, size)                                          \
101 do {                                                                    \
102         int s = (size);                                                 \
103         if ((ptr) == NULL) {                                            \
104                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
105                        "%s:%d\n", s, __FILE__, __LINE__);               \
106                 break;                                                  \
107         }                                                               \
108         if (s > PORTAL_VMALLOC_SIZE)                                    \
109                 cfs_free_large(ptr);                                    \
110         else                                                            \
111                 cfs_free(ptr);                                          \
112         portal_kmem_dec((ptr), s);                                      \
113         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
114                s, (ptr), atomic_read(&portal_kmemory));                 \
115 } while (0)
116
117 /******************************************************************************/
118
119 #ifdef PORTALS_PROFILING
120 #define prof_enum(FOO) PROF__##FOO
121 enum {
122         prof_enum(our_recvmsg),
123         prof_enum(our_sendmsg),
124         prof_enum(socknal_recv),
125         prof_enum(lib_parse),
126         prof_enum(conn_list_walk),
127         prof_enum(memcpy),
128         prof_enum(lib_finalize),
129         prof_enum(pingcli_time),
130         prof_enum(gmnal_send),
131         prof_enum(gmnal_recv),
132         MAX_PROFS
133 };
134
135 struct prof_ent {
136         char *str;
137         /* hrmph.  wrap-tastic. */
138         u32       starts;
139         u32       finishes;
140         cycles_t  total_cycles;
141         cycles_t  start;
142         cycles_t  end;
143 };
144
145 extern struct prof_ent prof_ents[MAX_PROFS];
146
147 #define PROF_START(FOO)                                         \
148         do {                                                    \
149                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
150                 pe->starts++;                                   \
151                 pe->start = get_cycles();                       \
152         } while (0)
153
154 #define PROF_FINISH(FOO)                                        \
155         do {                                                    \
156                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
157                 pe->finishes++;                                 \
158                 pe->end = get_cycles();                         \
159                 pe->total_cycles += (pe->end - pe->start);      \
160         } while (0)
161 #else /* !PORTALS_PROFILING */
162 #define PROF_START(FOO) do {} while(0)
163 #define PROF_FINISH(FOO) do {} while(0)
164 #endif /* PORTALS_PROFILING */
165
166 /* debug.c */
167 extern spinlock_t stack_backtrace_lock;
168
169 void portals_debug_dumpstack(cfs_task_t *tsk);
170 void portals_run_upcall(char **argv);
171 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
172 void portals_debug_dumplog(void);
173 int portals_debug_init(unsigned long bufsize);
174 int portals_debug_cleanup(void);
175 int portals_debug_clear_buffer(void);
176 int portals_debug_mark_buffer(char *text);
177 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
178                              char *file, unsigned int size);
179 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
180 /* Use the special GNU C __attribute__ hack to have the compiler check the
181  * printf style argument string against the actual argument count and
182  * types.
183  */
184 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
185                        const int line, unsigned long stack,
186                        char *format, ...)
187         __attribute__ ((format (printf, 7, 8)));
188 void portals_debug_set_level(unsigned int debug_level);
189
190 extern void kportal_daemonize (char *name);
191 extern void kportal_blockallsigs (void);
192
193 #else  /* !__KERNEL__ */
194 # ifndef DEBUG_SUBSYSTEM
195 #  define DEBUG_SUBSYSTEM S_UNDEFINED
196 # endif
197 # ifdef PORTAL_DEBUG
198 #  undef NDEBUG
199 #  include <assert.h>
200 #  define LASSERT(e)     assert(e)
201 #  define LASSERTF(cond, args...)                                              \
202 do {                                                                           \
203           if (!(cond))                                                         \
204                 CERROR(args);                                                  \
205           assert(cond);                                                        \
206 } while (0)
207 # else
208 #  define LASSERT(e)
209 #  define LASSERTF(cond, args...) do { } while (0)
210 # endif
211 # define printk(format, args...) printf (format, ## args)
212 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
213 # define PORTAL_FREE(a, b) do { free(a); } while (0);
214 void portals_debug_dumplog(void);
215 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
216     printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
217            (subsys), (mask), (long)time(0), file, fn, line,                   \
218            getpid(), (unsigned long)stack, ## a);
219
220 #undef CWARN
221 #undef CERROR
222 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
223 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
224 #endif
225
226 /*
227  * compile-time assertions. @cond has to be constant expression.
228  * ISO C Standard:
229  *
230  *        6.8.4.2  The switch statement
231  *
232  *       ....
233  *
234  *       [#3] The expression of each case label shall be  an  integer
235  *       constant   expression  and  no  two  of  the  case  constant
236  *       expressions in the same switch statement shall have the same
237  *       value  after  conversion...
238  *
239  */
240 #define CLASSERT(cond) ({ switch(42) { case (cond): case 0: break; } })
241
242 /* support decl needed both by kernel and liblustre */
243 char *portals_nid2str(int nal, ptl_nid_t nid, char *str);
244 char *portals_id2str(int nal, ptl_process_id_t nid, char *str);
245
246 #ifndef CURRENT_TIME
247 # define CURRENT_TIME time(0)
248 #endif
249
250 /* --------------------------------------------------------------------
251  * Light-weight trace
252  * Support for temporary event tracing with minimal Heisenberg effect.
253  * All stuff about lwt are put in arch/kp30.h
254  * -------------------------------------------------------------------- */
255
256 struct portals_device_userstate
257 {
258         int          pdu_memhog_pages;
259         cfs_page_t   *pdu_memhog_root_page;
260 };
261
262 #include <libcfs/portals_lib.h>
263
264 /*
265  * USER LEVEL STUFF BELOW
266  */
267
268 #define PORTAL_IOCTL_VERSION 0x00010008
269 #define PING_SYNC       0
270 #define PING_ASYNC      1
271
272 struct portal_ioctl_hdr {
273         __u32 ioc_len;
274         __u32 ioc_version;
275 };
276
277 struct portals_debug_ioctl_data
278 {
279         struct portal_ioctl_hdr hdr;
280         unsigned int subs;
281         unsigned int debug;
282 };
283
284 #define PORTAL_IOC_INIT(data)                           \
285 do {                                                    \
286         memset(&data, 0, sizeof(data));                 \
287         data.ioc_version = PORTAL_IOCTL_VERSION;        \
288         data.ioc_len = sizeof(data);                    \
289 } while (0)
290
291 /* FIXME check conflict with lustre_lib.h */
292 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
293
294 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
295 {
296         int len = sizeof(*data);
297         len += size_round(data->ioc_inllen1);
298         len += size_round(data->ioc_inllen2);
299         return len;
300 }
301
302 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
303 {
304         if (data->ioc_len > (1<<30)) {
305                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
306                 return 1;
307         }
308         if (data->ioc_inllen1 > (1<<30)) {
309                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
310                 return 1;
311         }
312         if (data->ioc_inllen2 > (1<<30)) {
313                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
314                 return 1;
315         }
316         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
317                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
318                 return 1;
319         }
320         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
321                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
322                 return 1;
323         }
324         if (data->ioc_pbuf1 && !data->ioc_plen1) {
325                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
326                 return 1;
327         }
328         if (data->ioc_pbuf2 && !data->ioc_plen2) {
329                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
330                 return 1;
331         }
332         if (data->ioc_plen1 && !data->ioc_pbuf1) {
333                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
334                 return 1;
335         }
336         if (data->ioc_plen2 && !data->ioc_pbuf2) {
337                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
338                 return 1;
339         }
340         if (portal_ioctl_packlen(data) != data->ioc_len ) {
341                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
342                 return 1;
343         }
344         if (data->ioc_inllen1 &&
345             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
346                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
347                 return 1;
348         }
349         if (data->ioc_inllen2 &&
350             data->ioc_bulk[size_round(data->ioc_inllen1) +
351                            data->ioc_inllen2 - 1] != '\0') {
352                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
353                 return 1;
354         }
355         return 0;
356 }
357
358 #ifndef __KERNEL__
359 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
360                                     int max)
361 {
362         char *ptr;
363         struct portal_ioctl_data *overlay;
364         data->ioc_len = portal_ioctl_packlen(data);
365         data->ioc_version = PORTAL_IOCTL_VERSION;
366
367         if (*pbuf && portal_ioctl_packlen(data) > max)
368                 return 1;
369         if (*pbuf == NULL) {
370                 *pbuf = malloc(data->ioc_len);
371         }
372         if (!*pbuf)
373                 return 1;
374         overlay = (struct portal_ioctl_data *)*pbuf;
375         memcpy(*pbuf, data, sizeof(*data));
376
377         ptr = overlay->ioc_bulk;
378         if (data->ioc_inlbuf1)
379                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
380         if (data->ioc_inlbuf2)
381                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
382         if (portal_ioctl_is_invalid(overlay))
383                 return 1;
384
385         return 0;
386 }
387
388 #else
389
390 extern int portal_ioctl_getdata(char *buf, char *end, void *arg);
391
392 #endif
393
394 /* ioctls for manipulating snapshots 30- */
395 #define IOC_PORTAL_TYPE                   'e'
396 #define IOC_PORTAL_MIN_NR                 30
397
398 #define IOC_PORTAL_PING                    _IOWR('e', 30, IOCTL_PORTAL_TYPE)
399
400 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, IOCTL_PORTAL_TYPE)
401 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, IOCTL_PORTAL_TYPE)
402 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, IOCTL_PORTAL_TYPE)
403 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 35, IOCTL_PORTAL_TYPE)
404 #define IOC_PORTAL_GET_NID                 _IOWR('e', 36, IOCTL_PORTAL_TYPE)
405 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 37, IOCTL_PORTAL_TYPE)
406 #define IOC_PORTAL_LOOPBACK                _IOWR('e', 38, IOCTL_PORTAL_TYPE)
407 #define IOC_PORTAL_LWT_CONTROL             _IOWR('e', 39, IOCTL_PORTAL_TYPE)
408 #define IOC_PORTAL_LWT_SNAPSHOT            _IOWR('e', 40, IOCTL_PORTAL_TYPE)
409 #define IOC_PORTAL_LWT_LOOKUP_STRING       _IOWR('e', 41, IOCTL_PORTAL_TYPE)
410 #define IOC_PORTAL_MEMHOG                  _IOWR('e', 42, IOCTL_PORTAL_TYPE)
411 #define IOC_PORTAL_DMSG                    _IOWR('e', 43, IOCTL_PORTAL_TYPE)
412 #define IOC_PORTAL_MAX_NR                             43
413
414 enum {
415         QSWNAL    = 1,
416         SOCKNAL   = 2,
417         GMNAL     = 3,
418         /*          4 unused */
419         TCPNAL    = 5,
420         ROUTER    = 6,
421         OPENIBNAL = 7,
422         IIBNAL    = 8,
423         LONAL     = 9,
424         RANAL     = 10,
425         VIBNAL    = 11,
426         NAL_ENUM_END_MARKER
427 };
428
429 #define PTL_NALFMT_SIZE             32 /* %u:%u.%u.%u.%u,%u (10+4+4+4+3+5+1) */
430 #ifndef CRAY_PORTALS
431 #define NALID_FROM_IFACE(nal) (nal)
432 #endif
433
434 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
435
436 #define NAL_CMD_REGISTER_PEER_FD     100
437 #define NAL_CMD_CLOSE_CONNECTION     101
438 #define NAL_CMD_REGISTER_MYNID       102
439 #define NAL_CMD_PUSH_CONNECTION      103
440 #define NAL_CMD_GET_CONN             104
441 #define NAL_CMD_DEL_PEER             105
442 #define NAL_CMD_ADD_PEER             106
443 #define NAL_CMD_GET_PEER             107
444 #define NAL_CMD_GET_TXDESC           108
445 #define NAL_CMD_ADD_ROUTE            109
446 #define NAL_CMD_DEL_ROUTE            110
447 #define NAL_CMD_GET_ROUTE            111
448 #define NAL_CMD_NOTIFY_ROUTER        112
449 #define NAL_CMD_ADD_INTERFACE        113
450 #define NAL_CMD_DEL_INTERFACE        114
451 #define NAL_CMD_GET_INTERFACE        115
452
453
454 enum {
455         DEBUG_DAEMON_START       =  1,
456         DEBUG_DAEMON_STOP        =  2,
457         DEBUG_DAEMON_PAUSE       =  3,
458         DEBUG_DAEMON_CONTINUE    =  4,
459 };
460
461
462 enum cfg_record_type {
463         PORTALS_CFG_TYPE = 1,
464         LUSTRE_CFG_TYPE = 123,
465 };
466
467 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
468
469 /* lustre_id output helper macros */
470 #define DLID4   "%lu/%lu/%lu/%lu"
471
472 #define OLID4(id)                              \
473     (unsigned long)(id)->li_fid.lf_id,         \
474     (unsigned long)(id)->li_fid.lf_group,      \
475     (unsigned long)(id)->li_stc.u.e3s.l3s_ino, \
476     (unsigned long)(id)->li_stc.u.e3s.l3s_gen
477
478 #endif