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