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