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