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