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