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