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