1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
7 #include <linux/libcfs.h>
11 # include <linux/vmalloc.h>
12 # include <linux/time.h>
13 # include <linux/slab.h>
14 # include <linux/interrupt.h>
15 # include <linux/highmem.h>
16 # include <linux/module.h>
17 # include <linux/version.h>
18 # include <portals/p30.h>
19 # include <linux/smp_lock.h>
20 # include <asm/atomic.h>
22 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
23 #define schedule_work schedule_task
24 #define prepare_work(wq,cb,cbdata) \
26 INIT_TQUEUE((wq), 0, 0); \
27 PREPARE_TQUEUE((wq), (cb), (cbdata)); \
30 #define PageUptodate Page_Uptodate
31 #define our_recalc_sigpending(current) recalc_sigpending(current)
32 #define num_online_cpus() smp_num_cpus
33 static inline void our_cond_resched(void)
35 if (current->need_resched)
38 #define work_struct_t struct tq_struct
42 #define prepare_work(wq,cb,cbdata) \
44 INIT_WORK((wq), (void *)(cb), (void *)(cbdata)); \
46 #define wait_on_page wait_on_page_locked
47 #define our_recalc_sigpending(current) recalc_sigpending()
48 #define strtok(a,b) strpbrk(a, b)
49 static inline void our_cond_resched(void)
53 #define work_struct_t struct work_struct
55 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
58 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
60 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__, \
61 __FUNCTION__, __LINE__))
62 #define LASSERTF(cond, fmt...) \
64 if (unlikely(!(cond))) { \
65 portals_debug_msg(DEBUG_SUBSYSTEM, D_EMERG, __FILE__,\
66 __FUNCTION__,__LINE__, CDEBUG_STACK,\
67 "ASSERTION(" #cond ") failed:" fmt);\
74 #define LASSERTF(cond, fmt...) do { } while (0)
78 #define LASSERT_SPIN_LOCKED(lock) LASSERT(spin_is_locked(lock))
80 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
84 #define LBUG_WITH_LOC(file, func, line) \
86 CEMERG("LBUG - trying to dump log to /tmp/lustre-log\n"); \
87 portals_debug_dumplog(); \
88 portals_run_lbug_upcall(file, func, line); \
92 #define LBUG_WITH_LOC(file, func, line) \
95 CERROR("STACK: %s\n", portals_debug_dumpstack()); \
96 portals_debug_dumplog(); \
97 portals_run_lbug_upcall(file, func, line); \
98 set_task_state(current, TASK_UNINTERRUPTIBLE); \
101 #endif /* __arch_um__ */
103 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
109 extern atomic_t portal_kmemory;
111 # define portal_kmem_inc(ptr, size) \
113 atomic_add(size, &portal_kmemory); \
116 # define portal_kmem_dec(ptr, size) do { \
117 atomic_sub(size, &portal_kmemory); \
121 # define portal_kmem_inc(ptr, size) do {} while (0)
122 # define portal_kmem_dec(ptr, size) do {} while (0)
123 #endif /* PORTAL_DEBUG */
125 #define PORTAL_VMALLOC_SIZE 16384
127 #define PORTAL_ALLOC_GFP(ptr, size, mask) \
129 LASSERT(!in_interrupt() || \
130 (size <= PORTAL_VMALLOC_SIZE && mask == GFP_ATOMIC)); \
131 if ((size) > PORTAL_VMALLOC_SIZE) \
132 (ptr) = vmalloc(size); \
134 (ptr) = kmalloc((size), (mask)); \
135 if ((ptr) == NULL) { \
136 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
137 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
138 CERROR("PORTALS: %d total bytes allocated by portals\n", \
139 atomic_read(&portal_kmemory)); \
141 portal_kmem_inc((ptr), (size)); \
142 memset((ptr), 0, (size)); \
144 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n", \
145 (int)(size), (ptr), atomic_read (&portal_kmemory)); \
148 #define PORTAL_ALLOC(ptr, size) \
149 PORTAL_ALLOC_GFP(ptr, size, GFP_NOFS)
151 #define PORTAL_ALLOC_ATOMIC(ptr, size) \
152 PORTAL_ALLOC_GFP(ptr, size, GFP_ATOMIC)
154 #define PORTAL_FREE(ptr, size) \
157 if ((ptr) == NULL) { \
158 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at " \
159 "%s:%d\n", s, __FILE__, __LINE__); \
162 if (s > PORTAL_VMALLOC_SIZE) \
166 portal_kmem_dec((ptr), s); \
167 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
168 s, (ptr), atomic_read(&portal_kmemory)); \
171 /* ------------------------------------------------------------------- */
173 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
175 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
176 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
178 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
179 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
181 #define PORTAL_MODULE_USE MOD_INC_USE_COUNT
182 #define PORTAL_MODULE_UNUSE MOD_DEC_USE_COUNT
185 #define PORTAL_SYMBOL_REGISTER(x)
186 #define PORTAL_SYMBOL_UNREGISTER(x)
188 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
189 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
191 #define PORTAL_MODULE_USE try_module_get(THIS_MODULE)
192 #define PORTAL_MODULE_UNUSE module_put(THIS_MODULE)
196 /******************************************************************************/
198 #ifdef PORTALS_PROFILING
199 #define prof_enum(FOO) PROF__##FOO
201 prof_enum(our_recvmsg),
202 prof_enum(our_sendmsg),
203 prof_enum(socknal_recv),
204 prof_enum(lib_parse),
205 prof_enum(conn_list_walk),
207 prof_enum(lib_finalize),
208 prof_enum(pingcli_time),
209 prof_enum(gmnal_send),
210 prof_enum(gmnal_recv),
216 /* hrmph. wrap-tastic. */
219 cycles_t total_cycles;
224 extern struct prof_ent prof_ents[MAX_PROFS];
226 #define PROF_START(FOO) \
228 struct prof_ent *pe = &prof_ents[PROF__##FOO]; \
230 pe->start = get_cycles(); \
233 #define PROF_FINISH(FOO) \
235 struct prof_ent *pe = &prof_ents[PROF__##FOO]; \
237 pe->end = get_cycles(); \
238 pe->total_cycles += (pe->end - pe->start); \
240 #else /* !PORTALS_PROFILING */
241 #define PROF_START(FOO) do {} while(0)
242 #define PROF_FINISH(FOO) do {} while(0)
243 #endif /* PORTALS_PROFILING */
246 extern spinlock_t stack_backtrace_lock;
248 char *portals_debug_dumpstack(void);
249 void portals_run_upcall(char **argv);
250 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
251 void portals_debug_dumplog(void);
252 int portals_debug_init(unsigned long bufsize);
253 int portals_debug_cleanup(void);
254 int portals_debug_clear_buffer(void);
255 int portals_debug_mark_buffer(char *text);
256 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
257 char *file, unsigned int size);
258 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
260 /* Use the special GNU C __attribute__ hack to have the compiler check the
261 * printf style argument string against the actual argument count and
265 # warning printf has been defined as a macro...
268 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
269 const int line, unsigned long stack,
271 __attribute__ ((format (printf, 7, 8)));
273 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
274 const int line, unsigned long stack,
275 const char *format, ...);
276 #endif /* __GNUC__ */
277 void portals_debug_set_level(unsigned int debug_level);
279 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
280 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
281 # define time(a) CURRENT_TIME
283 extern void kportal_daemonize (char *name);
284 extern void kportal_blockallsigs (void);
286 #else /* !__KERNEL__ */
292 # include <cygwin-ioctl.h>
297 # ifndef DEBUG_SUBSYSTEM
298 # define DEBUG_SUBSYSTEM S_UNDEFINED
303 # define LASSERT(e) assert(e)
304 # define LASSERTF(cond, args...) \
312 # define LASSERTF(cond, args...) do { } while (0)
314 # define printk(format, args...) printf (format, ## args)
315 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
316 # define PORTAL_FREE(a, b) do { free(a); } while (0);
317 void portals_debug_dumplog(void);
318 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
319 printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format, \
320 (subsys), (mask), (long)time(0), file, fn, line, \
321 getpid() , stack, ## a);
325 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
326 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
329 /* support decl needed both by kernel and liblustre */
330 char *portals_nid2str(int nal, ptl_nid_t nid, char *str);
331 char *portals_id2str(int nal, ptl_process_id_t nid, char *str);
334 # define CURRENT_TIME time(0)
337 /******************************************************************************/
338 /* Light-weight trace
339 * Support for temporary event tracing with minimal Heisenberg effect. */
340 #define LWT_SUPPORT 0
342 #define LWT_MEMORY (16<<20)
345 # if defined(__KERNEL__)
346 # if !defined(BITS_PER_LONG)
347 # error "BITS_PER_LONG not defined"
349 # elif !defined(__WORDSIZE)
350 # error "__WORDSIZE not defined"
352 # define BITS_PER_LONG __WORDSIZE
355 /* kernel hasn't defined this? */
364 # if BITS_PER_LONG > 32
368 #endif /* !KLWT_SUPPORT */
374 typedef struct _lwt_page {
375 struct list_head lwtp_list;
376 struct page *lwtp_page;
377 lwt_event_t *lwtp_events;
381 int lwtc_current_index;
382 lwt_page_t *lwtc_current_page;
385 extern int lwt_enabled;
386 extern lwt_cpu_t lwt_cpus[];
388 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
389 * This stuff is meant for finding specific problems; it never stays in
390 * production code... */
393 #define LWTWHERE(f,l) f ":" LWTSTR(l)
394 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
396 #define LWT_EVENT(p1, p2, p3, p4) \
398 unsigned long flags; \
404 local_irq_save (flags); \
406 cpu = &lwt_cpus[smp_processor_id()]; \
407 p = cpu->lwtc_current_page; \
408 e = &p->lwtp_events[cpu->lwtc_current_index++]; \
410 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) { \
411 cpu->lwtc_current_page = \
412 list_entry (p->lwtp_list.next, \
413 lwt_page_t, lwtp_list); \
414 cpu->lwtc_current_index = 0; \
417 e->lwte_when = get_cycles(); \
418 e->lwte_where = LWTWHERE(__FILE__,__LINE__); \
419 e->lwte_task = current; \
420 e->lwte_p1 = (long)(p1); \
421 e->lwte_p2 = (long)(p2); \
422 e->lwte_p3 = (long)(p3); \
423 e->lwte_p4 = (long)(p4); \
425 local_irq_restore (flags); \
429 #endif /* !KLWT_SUPPORT */
431 extern int lwt_init (void);
432 extern void lwt_fini (void);
433 extern int lwt_lookup_string (int *size, char *knlptr,
434 char *usrptr, int usrsize);
435 extern int lwt_control (int enable, int clear);
436 extern int lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
437 void *user_ptr, int user_size);
438 # else /* __KERNEL__ */
439 # define LWT_EVENT(p1,p2,p3,p4) /* no userland implementation yet */
440 # endif /* __KERNEL__ */
441 #endif /* LWT_SUPPORT */
443 struct portals_device_userstate
445 int pdu_memhog_pages;
446 struct page *pdu_memhog_root_page;
449 #include <linux/portals_lib.h>
452 * USER LEVEL STUFF BELOW
455 #define PORTAL_IOCTL_VERSION 0x00010007
459 struct portal_ioctl_hdr {
464 struct portals_debug_ioctl_data
466 struct portal_ioctl_hdr hdr;
471 #define PORTAL_IOC_INIT(data) \
473 memset(&data, 0, sizeof(data)); \
474 data.ioc_version = PORTAL_IOCTL_VERSION; \
475 data.ioc_len = sizeof(data); \
478 /* FIXME check conflict with lustre_lib.h */
479 #define PTL_IOC_DEBUG_MASK _IOWR('f', 250, long)
481 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
483 int len = sizeof(*data);
484 len += size_round(data->ioc_inllen1);
485 len += size_round(data->ioc_inllen2);
489 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
491 if (data->ioc_len > (1<<30)) {
492 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
495 if (data->ioc_inllen1 > (1<<30)) {
496 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
499 if (data->ioc_inllen2 > (1<<30)) {
500 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
503 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
504 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
507 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
508 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
511 if (data->ioc_pbuf1 && !data->ioc_plen1) {
512 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
515 if (data->ioc_pbuf2 && !data->ioc_plen2) {
516 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
519 if (data->ioc_plen1 && !data->ioc_pbuf1) {
520 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
523 if (data->ioc_plen2 && !data->ioc_pbuf2) {
524 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
527 if (portal_ioctl_packlen(data) != data->ioc_len ) {
528 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
531 if (data->ioc_inllen1 &&
532 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
533 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
536 if (data->ioc_inllen2 &&
537 data->ioc_bulk[size_round(data->ioc_inllen1) +
538 data->ioc_inllen2 - 1] != '\0') {
539 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
546 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
550 struct portal_ioctl_data *overlay;
551 data->ioc_len = portal_ioctl_packlen(data);
552 data->ioc_version = PORTAL_IOCTL_VERSION;
554 if (*pbuf && portal_ioctl_packlen(data) > max)
557 *pbuf = malloc(data->ioc_len);
561 overlay = (struct portal_ioctl_data *)*pbuf;
562 memcpy(*pbuf, data, sizeof(*data));
564 ptr = overlay->ioc_bulk;
565 if (data->ioc_inlbuf1)
566 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
567 if (data->ioc_inlbuf2)
568 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
569 if (portal_ioctl_is_invalid(overlay))
575 #include <asm/uaccess.h>
577 /* buffer MUST be at least the size of portal_ioctl_hdr */
578 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
580 struct portal_ioctl_hdr *hdr;
581 struct portal_ioctl_data *data;
585 hdr = (struct portal_ioctl_hdr *)buf;
586 data = (struct portal_ioctl_data *)buf;
588 err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
592 if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
593 CERROR("PORTALS: version mismatch kernel vs application\n");
597 if (hdr->ioc_len + buf >= end) {
598 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
603 if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
604 CERROR("PORTALS: user buffer too small for ioctl\n");
608 err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
612 if (portal_ioctl_is_invalid(data)) {
613 CERROR("PORTALS: ioctl not correctly formatted\n");
617 if (data->ioc_inllen1)
618 data->ioc_inlbuf1 = &data->ioc_bulk[0];
620 if (data->ioc_inllen2)
621 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
622 size_round(data->ioc_inllen1);
628 /* ioctls for manipulating snapshots 30- */
629 #define IOC_PORTAL_TYPE 'e'
630 #define IOC_PORTAL_MIN_NR 30
632 #define IOC_PORTAL_PING _IOWR('e', 30, long)
634 #define IOC_PORTAL_CLEAR_DEBUG _IOWR('e', 32, long)
635 #define IOC_PORTAL_MARK_DEBUG _IOWR('e', 33, long)
636 #define IOC_PORTAL_PANIC _IOWR('e', 34, long)
637 #define IOC_PORTAL_NAL_CMD _IOWR('e', 35, long)
638 #define IOC_PORTAL_GET_NID _IOWR('e', 36, long)
639 #define IOC_PORTAL_FAIL_NID _IOWR('e', 37, long)
641 #define IOC_PORTAL_LWT_CONTROL _IOWR('e', 39, long)
642 #define IOC_PORTAL_LWT_SNAPSHOT _IOWR('e', 40, long)
643 #define IOC_PORTAL_LWT_LOOKUP_STRING _IOWR('e', 41, long)
644 #define IOC_PORTAL_MEMHOG _IOWR('e', 42, long)
645 #define IOC_PORTAL_MAX_NR 42
659 #define PTL_NALFMT_SIZE 32 /* %u:%u.%u.%u.%u,%u (10+4+4+4+3+5+1) */
661 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
663 #define NAL_CMD_REGISTER_PEER_FD 100
664 #define NAL_CMD_CLOSE_CONNECTION 101
665 #define NAL_CMD_REGISTER_MYNID 102
666 #define NAL_CMD_PUSH_CONNECTION 103
667 #define NAL_CMD_GET_CONN 104
668 #define NAL_CMD_DEL_PEER 105
669 #define NAL_CMD_ADD_PEER 106
670 #define NAL_CMD_GET_PEER 107
671 #define NAL_CMD_GET_TXDESC 108
672 #define NAL_CMD_ADD_ROUTE 109
673 #define NAL_CMD_DEL_ROUTE 110
674 #define NAL_CMD_GET_ROUTE 111
675 #define NAL_CMD_NOTIFY_ROUTER 112
676 #define NAL_CMD_ADD_INTERFACE 113
677 #define NAL_CMD_DEL_INTERFACE 114
678 #define NAL_CMD_GET_INTERFACE 115
682 DEBUG_DAEMON_START = 1,
683 DEBUG_DAEMON_STOP = 2,
684 DEBUG_DAEMON_PAUSE = 3,
685 DEBUG_DAEMON_CONTINUE = 4,
689 enum cfg_record_type {
690 PORTALS_CFG_TYPE = 1,
691 LUSTRE_CFG_TYPE = 123,
694 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
697 # ifndef BITS_PER_LONG
698 # if (~0UL) == 0xffffffffUL
699 # define BITS_PER_LONG 32
701 # define BITS_PER_LONG 64
706 #if BITS_PER_LONG > 32
707 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
708 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
709 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
711 # define LI_POISON ((int)0x5a5a5a5a)
712 # define LL_POISON ((long)0x5a5a5a5a)
713 # define LP_POISON ((void *)(long)0x5a5a5a5a)
716 #if defined(__x86_64__)
719 # define LPX64 "%#Lx"
722 #elif (BITS_PER_LONG == 32 || __WORDSIZE == 32)
725 # define LPX64 "%#Lx"
728 #elif (BITS_PER_LONG == 64 || __WORDSIZE == 64)
731 # define LPX64 "%#lx"
736 # error "No word size defined"