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