Whamcloud - gitweb
LU-56 lnet: cleanup for rtrpool and LNet counter
[fs/lustre-release.git] / lnet / include / lnet / lib-lnet.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/include/lnet/lib-lnet.h
35  *
36  * Top level include for library side routines
37  */
38
39 #ifndef __LNET_LIB_LNET_H__
40 #define __LNET_LIB_LNET_H__
41
42 #if defined(__linux__)
43 #include <lnet/linux/lib-lnet.h>
44 #elif defined(__APPLE__)
45 #include <lnet/darwin/lib-lnet.h>
46 #elif defined(__WINNT__)
47 #include <lnet/winnt/lib-lnet.h>
48 #else
49 #error Unsupported Operating System
50 #endif
51
52 #include <libcfs/libcfs.h>
53 #include <lnet/types.h>
54 #include <lnet/lnet.h>
55 #include <lnet/lib-types.h>
56
57 extern lnet_t  the_lnet;                        /* THE network */
58
59 #if !defined(__KERNEL__) || defined(LNET_USE_LIB_FREELIST)
60 /* 1 CPT, simplify implementation... */
61 # define LNET_CPT_MAX_BITS      0
62
63 #else /* KERNEL and no freelist */
64
65 # if (BITS_PER_LONG == 32)
66 /* 2 CPTs, allowing more CPTs might make us under memory pressure */
67 #  define LNET_CPT_MAX_BITS     1
68
69 # else /* 64-bit system */
70 /*
71  * 256 CPTs for thousands of CPUs, allowing more CPTs might make us
72  * under risk of consuming all lh_cooke.
73  */
74 #  define LNET_CPT_MAX_BITS     8
75 # endif /* BITS_PER_LONG == 32 */
76 #endif
77
78 /* max allowed CPT number */
79 #define LNET_CPT_MAX            (1 << LNET_CPT_MAX_BITS)
80
81 #define LNET_CPT_NUMBER         (the_lnet.ln_cpt_number)
82 #define LNET_CPT_BITS           (the_lnet.ln_cpt_bits)
83 #define LNET_CPT_MASK           ((1ULL << LNET_CPT_BITS) - 1)
84
85 /** exclusive lock */
86 #define LNET_LOCK_EX            CFS_PERCPT_LOCK_EX
87
88 static inline int lnet_is_wire_handle_none (lnet_handle_wire_t *wh)
89 {
90         return (wh->wh_interface_cookie == LNET_WIRE_HANDLE_COOKIE_NONE &&
91                 wh->wh_object_cookie == LNET_WIRE_HANDLE_COOKIE_NONE);
92 }
93
94 static inline int lnet_md_exhausted (lnet_libmd_t *md)
95 {
96         return (md->md_threshold == 0 ||
97                 ((md->md_options & LNET_MD_MAX_SIZE) != 0 &&
98                  md->md_offset + md->md_max_size > md->md_length));
99 }
100
101 static inline int lnet_md_unlinkable (lnet_libmd_t *md)
102 {
103         /* Should unlink md when its refcount is 0 and either:
104          *  - md has been flagged for deletion (by auto unlink or LNetM[DE]Unlink,
105          *    in the latter case md may not be exhausted).
106          *  - auto unlink is on and md is exhausted.
107          */
108         if (md->md_refcount != 0)
109                 return 0;
110
111         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) != 0)
112                 return 1;
113
114         return ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
115                 lnet_md_exhausted(md));
116 }
117
118 #define lnet_cpt_table()        (the_lnet.ln_cpt_table)
119 #define lnet_cpt_current()      cfs_cpt_current(the_lnet.ln_cpt_table, 1)
120
121 static inline int
122 lnet_cpt_of_cookie(__u64 cookie)
123 {
124         unsigned int cpt = (cookie >> LNET_COOKIE_TYPE_BITS) & LNET_CPT_MASK;
125
126         /* LNET_CPT_NUMBER doesn't have to be power2, which means we can
127          * get illegal cpt from it's invalid cookie */
128         return cpt < LNET_CPT_NUMBER ? cpt : cpt % LNET_CPT_NUMBER;
129 }
130
131 static inline void
132 lnet_res_lock(int cpt)
133 {
134         cfs_percpt_lock(the_lnet.ln_res_lock, cpt);
135 }
136
137 static inline void
138 lnet_res_unlock(int cpt)
139 {
140         cfs_percpt_unlock(the_lnet.ln_res_lock, cpt);
141 }
142
143 static inline int
144 lnet_res_lock_current(void)
145 {
146         int cpt = lnet_cpt_current();
147
148         lnet_res_lock(cpt);
149         return cpt;
150 }
151
152 #ifdef __KERNEL__
153
154 #define lnet_ptl_lock(ptl)      cfs_spin_lock(&(ptl)->ptl_lock)
155 #define lnet_ptl_unlock(ptl)    cfs_spin_unlock(&(ptl)->ptl_lock)
156 #define lnet_eq_wait_lock()     cfs_spin_lock(&the_lnet.ln_eq_wait_lock)
157 #define lnet_eq_wait_unlock()   cfs_spin_unlock(&the_lnet.ln_eq_wait_lock)
158 #define LNET_LOCK()             cfs_spin_lock(&the_lnet.ln_lock)
159 #define LNET_UNLOCK()           cfs_spin_unlock(&the_lnet.ln_lock)
160 #define LNET_MUTEX_LOCK(m)      cfs_mutex_lock(m)
161 #define LNET_MUTEX_UNLOCK(m)    cfs_mutex_unlock(m)
162
163 #else /* !__KERNEL__ */
164
165 # ifndef HAVE_LIBPTHREAD
166 #define LNET_SINGLE_THREADED_LOCK(l)            \
167 do {                                            \
168         LASSERT ((l) == 0);                     \
169         (l) = 1;                                \
170 } while (0)
171
172 #define LNET_SINGLE_THREADED_UNLOCK(l)          \
173 do {                                            \
174         LASSERT ((l) == 1);                     \
175         (l) = 0;                                \
176 } while (0)
177
178 #define LNET_LOCK()             LNET_SINGLE_THREADED_LOCK(the_lnet.ln_lock)
179 #define LNET_UNLOCK()           LNET_SINGLE_THREADED_UNLOCK(the_lnet.ln_lock)
180 #define LNET_MUTEX_LOCK(m)      LNET_SINGLE_THREADED_LOCK(*(m))
181 #define LNET_MUTEX_UNLOCK(m)    LNET_SINGLE_THREADED_UNLOCK(*(m))
182
183 #define lnet_ptl_lock(ptl)                      \
184         LNET_SINGLE_THREADED_LOCK((ptl)->ptl_lock)
185 #define lnet_ptl_unlock(ptl)                    \
186         LNET_SINGLE_THREADED_UNLOCK((ptl)->ptl_lock)
187
188 #define lnet_eq_wait_lock()                     \
189         LNET_SINGLE_THREADED_LOCK(the_lnet.ln_eq_wait_lock)
190 #define lnet_eq_wait_unlock()                   \
191         LNET_SINGLE_THREADED_UNLOCK(the_lnet.ln_eq_wait_lock)
192
193 # else /* HAVE_LIBPTHREAD */
194
195 #define LNET_LOCK()             pthread_mutex_lock(&the_lnet.ln_lock)
196 #define LNET_UNLOCK()           pthread_mutex_unlock(&the_lnet.ln_lock)
197 #define LNET_MUTEX_LOCK(m)      pthread_mutex_lock(m)
198 #define LNET_MUTEX_UNLOCK(m)    pthread_mutex_unlock(m)
199
200 #define lnet_ptl_lock(ptl)      pthread_mutex_lock(&(ptl)->ptl_lock)
201 #define lnet_ptl_unlock(ptl)    pthread_mutex_unlock(&(ptl)->ptl_lock)
202
203 #define lnet_eq_wait_lock()     pthread_mutex_lock(&the_lnet.ln_eq_wait_lock)
204 #define lnet_eq_wait_unlock()   pthread_mutex_unlock(&the_lnet.ln_eq_wait_lock)
205
206 # endif /* HAVE_LIBPTHREAD */
207 #endif /* __KERNEL__ */
208
209 #define MAX_PORTALS     64
210
211 /* these are only used by code with LNET_USE_LIB_FREELIST, but we still
212  * exported them to !LNET_USE_LIB_FREELIST for easy implemetation */
213 #define LNET_FL_MAX_MES         2048
214 #define LNET_FL_MAX_MDS         2048
215 #define LNET_FL_MAX_EQS         512
216 #define LNET_FL_MAX_MSGS        2048    /* Outstanding messages */
217
218 #ifdef LNET_USE_LIB_FREELIST
219
220 int lnet_freelist_init(lnet_freelist_t *fl, int n, int size);
221 void lnet_freelist_fini(lnet_freelist_t *fl);
222
223 static inline void *
224 lnet_freelist_alloc (lnet_freelist_t *fl)
225 {
226         /* ALWAYS called with liblock held */
227         lnet_freeobj_t *o;
228
229         if (cfs_list_empty (&fl->fl_list))
230                 return (NULL);
231
232         o = cfs_list_entry (fl->fl_list.next, lnet_freeobj_t, fo_list);
233         cfs_list_del (&o->fo_list);
234         return ((void *)&o->fo_contents);
235 }
236
237 static inline void
238 lnet_freelist_free (lnet_freelist_t *fl, void *obj)
239 {
240         /* ALWAYS called with liblock held */
241         lnet_freeobj_t *o = cfs_list_entry (obj, lnet_freeobj_t, fo_contents);
242
243         cfs_list_add (&o->fo_list, &fl->fl_list);
244 }
245
246
247 static inline lnet_eq_t *
248 lnet_eq_alloc (void)
249 {
250         /* NEVER called with resource lock held */
251         struct lnet_res_container *rec = &the_lnet.ln_eq_container;
252         lnet_eq_t                 *eq;
253
254         LASSERT(LNET_CPT_NUMBER == 1);
255
256         lnet_res_lock(0);
257         eq = (lnet_eq_t *)lnet_freelist_alloc(&rec->rec_freelist);
258         lnet_res_unlock(0);
259
260         return eq;
261 }
262
263 static inline void
264 lnet_eq_free_locked(lnet_eq_t *eq)
265 {
266         /* ALWAYS called with resource lock held */
267         struct lnet_res_container *rec = &the_lnet.ln_eq_container;
268
269         LASSERT(LNET_CPT_NUMBER == 1);
270         lnet_freelist_free(&rec->rec_freelist, eq);
271 }
272
273 static inline void
274 lnet_eq_free(lnet_eq_t *eq)
275 {
276         lnet_res_lock(0);
277         lnet_eq_free_locked(eq);
278         lnet_res_unlock(0);
279 }
280
281 static inline lnet_libmd_t *
282 lnet_md_alloc (lnet_md_t *umd)
283 {
284         /* NEVER called with resource lock held */
285         struct lnet_res_container *rec = the_lnet.ln_md_containers[0];
286         lnet_libmd_t              *md;
287
288         LASSERT(LNET_CPT_NUMBER == 1);
289
290         lnet_res_lock(0);
291         md = (lnet_libmd_t *)lnet_freelist_alloc(&rec->rec_freelist);
292         lnet_res_unlock(0);
293
294         if (md != NULL)
295                 CFS_INIT_LIST_HEAD(&md->md_list);
296
297         return md;
298 }
299
300 static inline void
301 lnet_md_free_locked(lnet_libmd_t *md)
302 {
303         /* ALWAYS called with resource lock held */
304         struct lnet_res_container *rec = the_lnet.ln_md_containers[0];
305
306         LASSERT(LNET_CPT_NUMBER == 1);
307         lnet_freelist_free(&rec->rec_freelist, md);
308 }
309
310 static inline void
311 lnet_md_free(lnet_libmd_t *md)
312 {
313         lnet_res_lock(0);
314         lnet_md_free_locked(md);
315         lnet_res_unlock(0);
316 }
317
318 static inline lnet_me_t *
319 lnet_me_alloc(void)
320 {
321         /* NEVER called with resource lock held */
322         struct lnet_res_container *rec = the_lnet.ln_me_containers[0];
323         lnet_me_t                 *me;
324
325         LASSERT(LNET_CPT_NUMBER == 1);
326
327         lnet_res_lock(0);
328         me = (lnet_me_t *)lnet_freelist_alloc(&rec->rec_freelist);
329         lnet_res_unlock(0);
330
331         return me;
332 }
333
334 static inline void
335 lnet_me_free_locked(lnet_me_t *me)
336 {
337         /* ALWAYS called with resource lock held */
338         struct lnet_res_container *rec = the_lnet.ln_me_containers[0];
339
340         LASSERT(LNET_CPT_NUMBER == 1);
341         lnet_freelist_free(&rec->rec_freelist, me);
342 }
343
344 static inline void
345 lnet_me_free(lnet_me_t *me)
346 {
347         lnet_res_lock(0);
348         lnet_me_free_locked(me);
349         lnet_res_unlock(0);
350 }
351
352 static inline lnet_msg_t *
353 lnet_msg_alloc (void)
354 {
355         /* NEVER called with network lock held */
356         struct lnet_msg_container *msc = &the_lnet.ln_msg_container;
357         lnet_msg_t                *msg;
358
359         LNET_LOCK();
360         msg = (lnet_msg_t *)lnet_freelist_alloc(&msc->msc_freelist);
361         LNET_UNLOCK();
362
363         if (msg != NULL) {
364                 /* NULL pointers, clear flags etc */
365                 memset(msg, 0, sizeof(*msg));
366 #ifdef CRAY_XT3
367                 msg->msg_ev.uid = LNET_UID_ANY;
368 #endif
369         }
370         return msg;
371 }
372
373 static inline void
374 lnet_msg_free_locked(lnet_msg_t *msg)
375 {
376         /* ALWAYS called with network lock held */
377         struct lnet_msg_container *msc = &the_lnet.ln_msg_container;
378
379         LASSERT(!msg->msg_onactivelist);
380         lnet_freelist_free(&msc->msc_freelist, msg);
381 }
382
383 static inline void
384 lnet_msg_free (lnet_msg_t *msg)
385 {
386         LNET_LOCK();
387         lnet_msg_free_locked(msg);
388         LNET_UNLOCK();
389 }
390
391 #else /* !LNET_USE_LIB_FREELIST */
392
393 static inline lnet_eq_t *
394 lnet_eq_alloc (void)
395 {
396         /* NEVER called with liblock held */
397         lnet_eq_t *eq;
398
399         LIBCFS_ALLOC(eq, sizeof(*eq));
400         return (eq);
401 }
402
403 static inline void
404 lnet_eq_free(lnet_eq_t *eq)
405 {
406         /* ALWAYS called with resource lock held */
407         LIBCFS_FREE(eq, sizeof(*eq));
408 }
409
410 static inline lnet_libmd_t *
411 lnet_md_alloc (lnet_md_t *umd)
412 {
413         /* NEVER called with liblock held */
414         lnet_libmd_t *md;
415         unsigned int  size;
416         unsigned int  niov;
417
418         if ((umd->options & LNET_MD_KIOV) != 0) {
419                 niov = umd->length;
420                 size = offsetof(lnet_libmd_t, md_iov.kiov[niov]);
421         } else {
422                 niov = ((umd->options & LNET_MD_IOVEC) != 0) ?
423                        umd->length : 1;
424                 size = offsetof(lnet_libmd_t, md_iov.iov[niov]);
425         }
426
427         LIBCFS_ALLOC(md, size);
428
429         if (md != NULL) {
430                 /* Set here in case of early free */
431                 md->md_options = umd->options;
432                 md->md_niov = niov;
433                 CFS_INIT_LIST_HEAD(&md->md_list);
434         }
435
436         return (md);
437 }
438
439 static inline void
440 lnet_md_free(lnet_libmd_t *md)
441 {
442         /* ALWAYS called with resource lock held */
443         unsigned int  size;
444
445         if ((md->md_options & LNET_MD_KIOV) != 0)
446                 size = offsetof(lnet_libmd_t, md_iov.kiov[md->md_niov]);
447         else
448                 size = offsetof(lnet_libmd_t, md_iov.iov[md->md_niov]);
449
450         LIBCFS_FREE(md, size);
451 }
452
453 static inline lnet_me_t *
454 lnet_me_alloc (void)
455 {
456         /* NEVER called with liblock held */
457         lnet_me_t *me;
458
459         LIBCFS_ALLOC(me, sizeof(*me));
460         return (me);
461 }
462
463 static inline void
464 lnet_me_free(lnet_me_t *me)
465 {
466         /* ALWAYS called with resource lock held */
467         LIBCFS_FREE(me, sizeof(*me));
468 }
469
470 static inline lnet_msg_t *
471 lnet_msg_alloc(void)
472 {
473         /* NEVER called with liblock held */
474         lnet_msg_t *msg;
475
476         LIBCFS_ALLOC(msg, sizeof(*msg));
477
478         /* no need to zero, LIBCFS_ALLOC does for us */
479
480 #ifdef CRAY_XT3
481         if (msg != NULL) {
482                 msg->msg_ev.uid = LNET_UID_ANY;
483         }
484 #endif
485         return (msg);
486 }
487
488 static inline void
489 lnet_msg_free(lnet_msg_t *msg)
490 {
491         /* ALWAYS called with network lock held */
492         LASSERT(!msg->msg_onactivelist);
493         LIBCFS_FREE(msg, sizeof(*msg));
494 }
495
496 #define lnet_eq_free_locked(eq)         lnet_eq_free(eq)
497 #define lnet_md_free_locked(md)         lnet_md_free(md)
498 #define lnet_me_free_locked(me)         lnet_me_free(me)
499 #define lnet_msg_free_locked(msg)       lnet_msg_free(msg)
500
501 #endif /* LNET_USE_LIB_FREELIST */
502
503 lnet_libhandle_t *lnet_res_lh_lookup(struct lnet_res_container *rec,
504                                      __u64 cookie);
505 void lnet_res_lh_initialize(struct lnet_res_container *rec,
506                             lnet_libhandle_t *lh);
507 static inline void
508 lnet_res_lh_invalidate(lnet_libhandle_t *lh)
509 {
510         /* ALWAYS called with resource lock held */
511         /* NB: cookie is still useful, don't reset it */
512         cfs_list_del(&lh->lh_hash_chain);
513 }
514
515 static inline void
516 lnet_eq2handle (lnet_handle_eq_t *handle, lnet_eq_t *eq)
517 {
518         if (eq == NULL) {
519                 LNetInvalidateHandle(handle);
520                 return;
521         }
522
523         handle->cookie = eq->eq_lh.lh_cookie;
524 }
525
526 static inline lnet_eq_t *
527 lnet_handle2eq(lnet_handle_eq_t *handle)
528 {
529         /* ALWAYS called with resource lock held */
530         lnet_libhandle_t *lh;
531
532         lh = lnet_res_lh_lookup(&the_lnet.ln_eq_container, handle->cookie);
533         if (lh == NULL)
534                 return NULL;
535
536         return lh_entry(lh, lnet_eq_t, eq_lh);
537 }
538
539 static inline void
540 lnet_md2handle (lnet_handle_md_t *handle, lnet_libmd_t *md)
541 {
542         handle->cookie = md->md_lh.lh_cookie;
543 }
544
545 static inline lnet_libmd_t *
546 lnet_handle2md(lnet_handle_md_t *handle)
547 {
548         /* ALWAYS called with resource lock held */
549         lnet_libhandle_t *lh;
550         int              cpt;
551
552         cpt = lnet_cpt_of_cookie(handle->cookie);
553         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
554                                 handle->cookie);
555         if (lh == NULL)
556                 return NULL;
557
558         return lh_entry(lh, lnet_libmd_t, md_lh);
559 }
560
561 static inline lnet_libmd_t *
562 lnet_wire_handle2md(lnet_handle_wire_t *wh)
563 {
564         /* ALWAYS called with resource lock held */
565         lnet_libhandle_t *lh;
566         int              cpt;
567
568         if (wh->wh_interface_cookie != the_lnet.ln_interface_cookie)
569                 return NULL;
570
571         cpt = lnet_cpt_of_cookie(wh->wh_object_cookie);
572         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
573                                 wh->wh_object_cookie);
574         if (lh == NULL)
575                 return NULL;
576
577         return lh_entry(lh, lnet_libmd_t, md_lh);
578 }
579
580 static inline void
581 lnet_me2handle (lnet_handle_me_t *handle, lnet_me_t *me)
582 {
583         handle->cookie = me->me_lh.lh_cookie;
584 }
585
586 static inline lnet_me_t *
587 lnet_handle2me(lnet_handle_me_t *handle)
588 {
589         /* ALWAYS called with resource lock held */
590         lnet_libhandle_t *lh;
591         int              cpt;
592
593         cpt = lnet_cpt_of_cookie(handle->cookie);
594         lh = lnet_res_lh_lookup(the_lnet.ln_me_containers[cpt],
595                                 handle->cookie);
596         if (lh == NULL)
597                 return NULL;
598
599         return lh_entry(lh, lnet_me_t, me_lh);
600 }
601
602 static inline void
603 lnet_peer_addref_locked(lnet_peer_t *lp)
604 {
605         LASSERT (lp->lp_refcount > 0);
606         lp->lp_refcount++;
607 }
608
609 extern void lnet_destroy_peer_locked(lnet_peer_t *lp);
610
611 static inline void
612 lnet_peer_decref_locked(lnet_peer_t *lp)
613 {
614         LASSERT (lp->lp_refcount > 0);
615         lp->lp_refcount--;
616         if (lp->lp_refcount == 0)
617                 lnet_destroy_peer_locked(lp);
618 }
619
620 static inline int
621 lnet_isrouter(lnet_peer_t *lp)
622 {
623         return lp->lp_rtr_refcount != 0;
624 }
625
626 static inline void
627 lnet_ni_addref_locked(lnet_ni_t *ni)
628 {
629         LASSERT (ni->ni_refcount > 0);
630         ni->ni_refcount++;
631 }
632
633 static inline void
634 lnet_ni_addref(lnet_ni_t *ni)
635 {
636         LNET_LOCK();
637         lnet_ni_addref_locked(ni);
638         LNET_UNLOCK();
639 }
640
641 static inline void
642 lnet_ni_decref_locked(lnet_ni_t *ni)
643 {
644         LASSERT (ni->ni_refcount > 0);
645         ni->ni_refcount--;
646         if (ni->ni_refcount == 0)
647                 cfs_list_add_tail(&ni->ni_list, &the_lnet.ln_zombie_nis);
648 }
649
650 static inline void
651 lnet_ni_decref(lnet_ni_t *ni)
652 {
653         LNET_LOCK();
654         lnet_ni_decref_locked(ni);
655         LNET_UNLOCK();
656 }
657
658 static inline int
659 lnet_nid2peerhash(lnet_nid_t nid)
660 {
661         return cfs_hash_long(nid, LNET_PEER_HASH_BITS);
662 }
663
664 extern lnd_t the_lolnd;
665
666 #ifndef __KERNEL__
667 /* unconditional registration */
668 #define LNET_REGISTER_ULND(lnd)                 \
669 do {                                            \
670         extern lnd_t lnd;                       \
671                                                 \
672         lnet_register_lnd(&(lnd));              \
673 } while (0)
674
675 /* conditional registration */
676 #define LNET_REGISTER_ULND_IF_PRESENT(lnd)                              \
677 do {                                                                    \
678         extern lnd_t lnd __attribute__ ((weak, alias("the_lolnd")));    \
679                                                                         \
680         if (&(lnd) != &the_lolnd)                                       \
681                 lnet_register_lnd(&(lnd));                              \
682 } while (0)
683 #endif
684
685 #ifdef CRAY_XT3
686 inline static void
687 lnet_set_msg_uid(lnet_ni_t *ni, lnet_msg_t *msg, lnet_uid_t uid)
688 {
689         LASSERT (msg->msg_ev.uid == LNET_UID_ANY);
690         msg->msg_ev.uid = uid;
691 }
692 #endif
693
694 extern int lnet_cpt_of_nid(lnet_nid_t nid);
695 extern lnet_ni_t *lnet_nid2ni_locked (lnet_nid_t nid);
696 extern lnet_ni_t *lnet_net2ni_locked (__u32 net);
697 static inline lnet_ni_t *
698 lnet_net2ni (__u32 net)
699 {
700         lnet_ni_t *ni;
701
702         LNET_LOCK();
703         ni = lnet_net2ni_locked(net);
704         LNET_UNLOCK();
705
706         return ni;
707 }
708
709 int lnet_notify(lnet_ni_t *ni, lnet_nid_t peer, int alive, cfs_time_t when);
710 void lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when);
711 int lnet_add_route(__u32 net, unsigned int hops, lnet_nid_t gateway_nid);
712 int lnet_check_routes(void);
713 int lnet_del_route(__u32 net, lnet_nid_t gw_nid);
714 void lnet_destroy_routes(void);
715 int lnet_get_route(int idx, __u32 *net, __u32 *hops,
716                    lnet_nid_t *gateway, __u32 *alive);
717 void lnet_proc_init(void);
718 void lnet_proc_fini(void);
719 int  lnet_rtrpools_alloc(int im_a_router);
720 void lnet_rtrpools_free(void);
721 lnet_remotenet_t *lnet_find_net_locked (__u32 net);
722
723 int lnet_islocalnid(lnet_nid_t nid);
724 int lnet_islocalnet(__u32 net);
725
726 void lnet_msg_attach_md(lnet_msg_t *msg, lnet_libmd_t *md,
727                         unsigned int offset, unsigned int mlen);
728 void lnet_msg_detach_md(lnet_msg_t *msg, int status);
729 void lnet_build_unlink_event(lnet_libmd_t *md, lnet_event_t *ev);
730 void lnet_build_msg_event(lnet_msg_t *msg, lnet_event_kind_t ev_type);
731 void lnet_msg_commit(lnet_msg_t *msg, int sending);
732 void lnet_msg_decommit(lnet_msg_t *msg, int status);
733
734 void lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev);
735 void lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
736                     unsigned int offset, unsigned int len);
737 int lnet_send(lnet_nid_t nid, lnet_msg_t *msg);
738 void lnet_return_tx_credits_locked(lnet_msg_t *msg);
739 void lnet_return_rx_credits_locked(lnet_msg_t *msg);
740
741 /* portals functions */
742 /* portals attributes */
743 static inline int
744 lnet_ptl_is_lazy(lnet_portal_t *ptl)
745 {
746         return !!(ptl->ptl_options & LNET_PTL_LAZY);
747 }
748
749 static inline int
750 lnet_ptl_is_unique(lnet_portal_t *ptl)
751 {
752         return !!(ptl->ptl_options & LNET_PTL_MATCH_UNIQUE);
753 }
754
755 static inline int
756 lnet_ptl_is_wildcard(lnet_portal_t *ptl)
757 {
758         return !!(ptl->ptl_options & LNET_PTL_MATCH_WILDCARD);
759 }
760
761 static inline void
762 lnet_ptl_setopt(lnet_portal_t *ptl, int opt)
763 {
764         ptl->ptl_options |= opt;
765 }
766
767 static inline void
768 lnet_ptl_unsetopt(lnet_portal_t *ptl, int opt)
769 {
770         ptl->ptl_options &= ~opt;
771 }
772
773 /* match-table functions */
774 cfs_list_t *lnet_mt_match_head(struct lnet_match_table *mtable,
775                                lnet_process_id_t id, __u64 mbits);
776 struct lnet_match_table *lnet_mt_of_attach(unsigned int index,
777                                            lnet_process_id_t id, __u64 mbits,
778                                            __u64 ignore_bits,
779                                            lnet_ins_pos_t pos);
780 struct lnet_match_table *lnet_mt_of_match(unsigned int index,
781                                           lnet_process_id_t id, __u64 mbits);
782 int lnet_mt_match_md(struct lnet_match_table *mtable,
783                      struct lnet_match_info *info, struct lnet_msg *msg);
784
785 /* portals match/attach functions */
786 void lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
787                         cfs_list_t *matches, cfs_list_t *drops);
788 void lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md);
789 int lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg);
790
791 /* initialized and finalize portals */
792 int lnet_portals_create(void);
793 void lnet_portals_destroy(void);
794
795 /* message functions */
796 int lnet_parse (lnet_ni_t *ni, lnet_hdr_t *hdr,
797                 lnet_nid_t fromnid, void *private, int rdma_req);
798 void lnet_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
799                unsigned int offset, unsigned int mlen, unsigned int rlen);
800 lnet_msg_t *lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *get_msg);
801 void lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *msg, unsigned int len);
802 void lnet_finalize(lnet_ni_t *ni, lnet_msg_t *msg, int rc);
803 void lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason);
804 void lnet_recv_delayed_msg_list(cfs_list_t *head);
805
806 int lnet_msg_container_setup(struct lnet_msg_container *container);
807 void lnet_msg_container_cleanup(struct lnet_msg_container *container);
808
809 char *lnet_msgtyp2str (int type);
810 void lnet_print_hdr (lnet_hdr_t * hdr);
811 int lnet_fail_nid(lnet_nid_t nid, unsigned int threshold);
812
813 void lnet_counters_get(lnet_counters_t *counters);
814 void lnet_counters_reset(void);
815
816 unsigned int lnet_iov_nob (unsigned int niov, struct iovec *iov);
817 int lnet_extract_iov (int dst_niov, struct iovec *dst,
818                       int src_niov, struct iovec *src,
819                       unsigned int offset, unsigned int len);
820
821 unsigned int lnet_kiov_nob (unsigned int niov, lnet_kiov_t *iov);
822 int lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
823                       int src_niov, lnet_kiov_t *src,
824                       unsigned int offset, unsigned int len);
825
826 void lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov,
827                         unsigned int doffset,
828                         unsigned int nsiov, struct iovec *siov,
829                         unsigned int soffset, unsigned int nob);
830 void lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov,
831                          unsigned int iovoffset,
832                          unsigned int nkiov, lnet_kiov_t *kiov,
833                          unsigned int kiovoffset, unsigned int nob);
834 void lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov,
835                          unsigned int kiovoffset,
836                          unsigned int niov, struct iovec *iov,
837                          unsigned int iovoffset, unsigned int nob);
838 void lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov,
839                           unsigned int doffset,
840                           unsigned int nskiov, lnet_kiov_t *skiov,
841                           unsigned int soffset, unsigned int nob);
842
843 static inline void
844 lnet_copy_iov2flat(int dlen, void *dest, unsigned int doffset,
845                    unsigned int nsiov, struct iovec *siov, unsigned int soffset,
846                    unsigned int nob)
847 {
848         struct iovec diov = {/*.iov_base = */ dest, /*.iov_len = */ dlen};
849
850         lnet_copy_iov2iov(1, &diov, doffset,
851                           nsiov, siov, soffset, nob);
852 }
853
854 static inline void
855 lnet_copy_kiov2flat(int dlen, void *dest, unsigned int doffset,
856                     unsigned int nsiov, lnet_kiov_t *skiov, unsigned int soffset,
857                     unsigned int nob)
858 {
859         struct iovec diov = {/* .iov_base = */ dest, /* .iov_len = */ dlen};
860
861         lnet_copy_kiov2iov(1, &diov, doffset,
862                            nsiov, skiov, soffset, nob);
863 }
864
865 static inline void
866 lnet_copy_flat2iov(unsigned int ndiov, struct iovec *diov, unsigned int doffset,
867                    int slen, void *src, unsigned int soffset, unsigned int nob)
868 {
869         struct iovec siov = {/*.iov_base = */ src, /*.iov_len = */slen};
870         lnet_copy_iov2iov(ndiov, diov, doffset,
871                           1, &siov, soffset, nob);
872 }
873
874 static inline void
875 lnet_copy_flat2kiov(unsigned int ndiov, lnet_kiov_t *dkiov, unsigned int doffset,
876                     int slen, void *src, unsigned int soffset, unsigned int nob)
877 {
878         struct iovec siov = {/* .iov_base = */ src, /* .iov_len = */ slen};
879         lnet_copy_iov2kiov(ndiov, dkiov, doffset,
880                            1, &siov, soffset, nob);
881 }
882
883 void lnet_me_unlink(lnet_me_t *me);
884
885 void lnet_md_unlink(lnet_libmd_t *md);
886 void lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd);
887
888 void lnet_register_lnd(lnd_t *lnd);
889 void lnet_unregister_lnd(lnd_t *lnd);
890 int lnet_set_ip_niaddr (lnet_ni_t *ni);
891
892 #ifdef __KERNEL__
893 int lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
894                  __u32 local_ip, __u32 peer_ip, int peer_port);
895 void lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
896                                 __u32 peer_ip, int port);
897 int lnet_count_acceptor_nis(void);
898 int lnet_acceptor_timeout(void);
899 int lnet_acceptor_port(void);
900 #else
901 void lnet_router_checker(void);
902 #endif
903
904 #ifdef HAVE_LIBPTHREAD
905 int lnet_count_acceptor_nis(void);
906 int lnet_acceptor_port(void);
907 #endif
908
909 int lnet_acceptor_start(void);
910 void lnet_acceptor_stop(void);
911
912 void lnet_get_tunables(void);
913 int lnet_peers_start_down(void);
914 int lnet_peer_buffer_credits(lnet_ni_t *ni);
915
916 int lnet_router_checker_start(void);
917 void lnet_router_checker_stop(void);
918 void lnet_swap_pinginfo(lnet_ping_info_t *info);
919
920 int lnet_ping_target_init(void);
921 void lnet_ping_target_fini(void);
922 int lnet_ping(lnet_process_id_t id, int timeout_ms,
923               lnet_process_id_t *ids, int n_ids);
924
925 int lnet_parse_ip2nets (char **networksp, char *ip2nets);
926 int lnet_parse_routes (char *route_str, int *im_a_router);
927 int lnet_parse_networks (cfs_list_t *nilist, char *networks);
928
929 int lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid);
930 lnet_peer_t *lnet_find_peer_locked (lnet_nid_t nid);
931 void lnet_peer_table_cleanup(void);
932 void lnet_peer_table_destroy(void);
933 int lnet_peer_table_create(void);
934 void lnet_debug_peer(lnet_nid_t nid);
935
936 #ifndef __KERNEL__
937 static inline int
938 lnet_parse_int_tunable(int *value, char *name)
939 {
940         char    *env = getenv(name);
941         char    *end;
942
943         if (env == NULL)
944                 return 0;
945
946         *value = strtoull(env, &end, 0);
947         if (*end == 0)
948                 return 0;
949
950         CERROR("Can't parse tunable %s=%s\n", name, env);
951         return -EINVAL;
952 }
953 #endif
954
955 #endif