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