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