Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lnet / include / lnet / lib-lnet.h
1 // SPDX-License-Identifier: GPL-2.0
2
3 /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4  * Use is subject to license terms.
5  *
6  * Copyright (c) 2012, 2017, Intel Corporation.
7  */
8
9 /* This file is part of Lustre, http://www.lustre.org/
10  *
11  * Top level include for library side routines
12  */
13
14 #ifndef __LNET_LIB_LNET_H__
15 #define __LNET_LIB_LNET_H__
16
17 /* LNET has 0xeXXX */
18 #define CFS_FAIL_PTLRPC_OST_BULK_CB2    0xe000
19 #define CFS_FAIL_MATCH_MD_NID           0xe001
20
21 #include <linux/netdevice.h>
22
23 #include <libcfs/libcfs.h>
24 #include <lnet/api.h>
25 #include <lnet/lib-cpt.h>
26 #include <lnet/lib-types.h>
27 #include <uapi/linux/lnet/lnet-dlc.h>
28 #include <uapi/linux/lnet/lnet-types.h>
29 #include <uapi/linux/lnet/lnetctl.h>
30 #include <uapi/linux/lnet/nidstr.h>
31
32 #include "lock.h"
33
34 extern struct lnet the_lnet;                    /* THE network */
35
36 #if (BITS_PER_LONG == 32)
37 /* 2 CPTs, allowing more CPTs might make us under memory pressure */
38 # define LNET_CPT_MAX_BITS     1
39
40 #else /* 64-bit system */
41 /*
42  * 256 CPTs for thousands of CPUs, allowing more CPTs might make us
43  * under risk of consuming all lh_cookie.
44  */
45 # define LNET_CPT_MAX_BITS     8
46 #endif /* BITS_PER_LONG == 32 */
47
48 /* max allowed CPT number */
49 #define LNET_CPT_MAX            (1 << LNET_CPT_MAX_BITS)
50
51 #define LNET_CPT_NUMBER         (the_lnet.ln_cpt_number)
52 #define LNET_CPT_BITS           (the_lnet.ln_cpt_bits)
53 #define LNET_CPT_MASK           ((1ULL << LNET_CPT_BITS) - 1)
54
55 /** exclusive lock */
56 #define LNET_LOCK_EX            CFS_PERCPT_LOCK_EX
57
58 /* need both kernel and user-land acceptor */
59 #define LNET_ACCEPTOR_MIN_RESERVED_PORT 512
60 #define LNET_ACCEPTOR_MAX_RESERVED_PORT 1023
61
62 /* default timeout and credits */
63 #define DEFAULT_PEER_TIMEOUT    180
64 #define DEFAULT_PEER_CREDITS    8
65 #define DEFAULT_CREDITS         256
66
67 /* default number of connections per peer */
68 #define DEFAULT_CONNS_PER_PEER  0
69
70 #ifdef HAVE_KERN_SOCK_GETNAME_2ARGS
71 #define lnet_kernel_getpeername(sock, addr, addrlen) \
72                 kernel_getpeername(sock, addr)
73 #define lnet_kernel_getsockname(sock, addr, addrlen) \
74                 kernel_getsockname(sock, addr)
75 #else
76 #define lnet_kernel_getpeername(sock, addr, addrlen) \
77                 kernel_getpeername(sock, addr, addrlen)
78 #define lnet_kernel_getsockname(sock, addr, addrlen) \
79                 kernel_getsockname(sock, addr, addrlen)
80 #endif
81
82 /*
83  * kernel 5.3: commit ef11db3310e272d3d8dbe8739e0770820dd20e52
84  * kernel 4.18.0-193.el8:
85  * added in_dev_for_each_ifa_rtnl and in_dev_for_each_ifa_rcu
86  * and removed for_ifa and endfor_ifa.
87  * Use the _rntl variant as the current locking is rtnl.
88  */
89 #ifdef HAVE_IN_DEV_FOR_EACH_IFA_RTNL
90 #define DECLARE_CONST_IN_IFADDR(ifa)            const struct in_ifaddr *ifa
91 #define endfor_ifa(in_dev)
92 #else
93 #define DECLARE_CONST_IN_IFADDR(ifa)
94 #define in_dev_for_each_ifa_rtnl(ifa, in_dev)   for_ifa((in_dev))
95 #define in_dev_for_each_ifa_rcu(ifa, in_dev)    for_ifa((in_dev))
96 #endif
97
98 #ifndef fallthrough
99 # if defined(__GNUC__) && __GNUC__ >= 7
100 #  define fallthrough  __attribute__((fallthrough)) /* fallthrough */
101 # else
102 #  define fallthrough do {} while (0)  /* fallthrough */
103 # endif
104 #endif
105
106 int choose_ipv4_src(__u32 *ret,
107                     int interface, __u32 dst_ipaddr, struct net *ns);
108
109 bool lnet_is_route_alive(struct lnet_route *route);
110 bool lnet_is_gateway_alive(struct lnet_peer *gw);
111
112 static inline int lnet_is_wire_handle_none(struct lnet_handle_wire *wh)
113 {
114         return (wh->wh_interface_cookie == LNET_WIRE_HANDLE_COOKIE_NONE &&
115                 wh->wh_object_cookie == LNET_WIRE_HANDLE_COOKIE_NONE);
116 }
117
118 static inline int lnet_md_exhausted(struct lnet_libmd *md)
119 {
120         return (md->md_threshold == 0 ||
121                 ((md->md_options & LNET_MD_MAX_SIZE) != 0 &&
122                  md->md_offset + md->md_max_size > md->md_length));
123 }
124
125 static inline int lnet_md_unlinkable(struct lnet_libmd *md)
126 {
127         /* Should unlink md when its refcount is 0 and either:
128          *  - md has been flagged for deletion (by auto unlink or LNetM[DE]Unlink,
129          *    in the latter case md may not be exhausted).
130          *  - auto unlink is on and md is exhausted.
131          */
132         if (md->md_refcount != 0)
133                 return 0;
134
135         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) != 0)
136                 return 1;
137
138         return ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
139                 lnet_md_exhausted(md));
140 }
141
142 #define lnet_cpt_table()        (the_lnet.ln_cpt_table)
143 #define lnet_cpt_current()      cfs_cpt_current(the_lnet.ln_cpt_table, 1)
144
145 static inline int
146 lnet_cpt_of_cookie(__u64 cookie)
147 {
148         unsigned int cpt = (cookie >> LNET_COOKIE_TYPE_BITS) & LNET_CPT_MASK;
149
150         /* LNET_CPT_NUMBER doesn't have to be power2, which means we can
151          * get illegal cpt from it's invalid cookie */
152         return cpt < LNET_CPT_NUMBER ? cpt : cpt % LNET_CPT_NUMBER;
153 }
154
155 static inline void
156 lnet_res_lock(int cpt)
157 {
158         cfs_percpt_lock(the_lnet.ln_res_lock, cpt);
159 }
160
161 static inline void
162 lnet_res_unlock(int cpt)
163 {
164         cfs_percpt_unlock(the_lnet.ln_res_lock, cpt);
165 }
166
167 static inline int
168 lnet_res_lock_current(void)
169 {
170         int cpt = lnet_cpt_current();
171
172         lnet_res_lock(cpt);
173         return cpt;
174 }
175
176 static inline void
177 lnet_net_lock(int cpt)
178 {
179         cfs_percpt_lock(the_lnet.ln_net_lock, cpt);
180 }
181
182 static inline void
183 lnet_net_unlock(int cpt)
184 {
185         cfs_percpt_unlock(the_lnet.ln_net_lock, cpt);
186 }
187
188 static inline int
189 lnet_net_lock_current(void)
190 {
191         int cpt = lnet_cpt_current();
192
193         lnet_net_lock(cpt);
194         return cpt;
195 }
196
197 #define LNET_LOCK()             lnet_net_lock(LNET_LOCK_EX)
198 #define LNET_UNLOCK()           lnet_net_unlock(LNET_LOCK_EX)
199
200 #define lnet_ptl_lock(ptl)      spin_lock(&(ptl)->ptl_lock)
201 #define lnet_ptl_unlock(ptl)    spin_unlock(&(ptl)->ptl_lock)
202 #define lnet_ni_lock(ni)        spin_lock(&(ni)->ni_lock)
203 #define lnet_ni_unlock(ni)      spin_unlock(&(ni)->ni_lock)
204
205 #define MAX_PORTALS     64
206
207 #define LNET_SMALL_MD_SIZE   offsetof(struct lnet_libmd, md_kiov[1])
208 extern struct kmem_cache *lnet_mes_cachep;       /* MEs kmem_cache */
209 extern struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
210                                                   * MDs kmem_cache */
211 extern struct kmem_cache *lnet_udsp_cachep;
212 extern struct kmem_cache *lnet_rspt_cachep;
213 extern struct kmem_cache *lnet_msg_cachep;
214
215 static inline bool
216 lnet_ni_set_status_locked(struct lnet_ni *ni, __u32 status)
217 __must_hold(&ni->ni_lock)
218 {
219         bool update = false;
220
221         if (ni->ni_status && *ni->ni_status != status) {
222                 CDEBUG(D_NET, "ni %s status changed from %#x to %#x\n",
223                        libcfs_nidstr(&ni->ni_nid),
224                        *ni->ni_status, status);
225                 *ni->ni_status = status;
226                 update = true;
227         }
228
229         return update;
230 }
231
232 static inline unsigned int
233 lnet_ni_get_status_locked(struct lnet_ni *ni)
234 __must_hold(&ni->ni_lock)
235 {
236         if (nid_is_lo0(&ni->ni_nid))
237                 return LNET_NI_STATUS_UP;
238         else if (atomic_read(&ni->ni_fatal_error_on))
239                 return LNET_NI_STATUS_DOWN;
240         else if (the_lnet.ln_routing && ni->ni_status)
241                 return *ni->ni_status;
242         else
243                 return LNET_NI_STATUS_UP;
244 }
245
246 static inline bool
247 lnet_ni_set_status(struct lnet_ni *ni, __u32 status)
248 {
249         bool update;
250
251         lnet_ni_lock(ni);
252         update = lnet_ni_set_status_locked(ni, status);
253         lnet_ni_unlock(ni);
254
255         return update;
256 }
257
258 static inline void lnet_md_wait_handling(struct lnet_libmd *md, int cpt)
259 {
260         wait_queue_head_t *wq = __var_waitqueue(md);
261 #if defined(HAVE_WAIT_BIT_QUEUE_ENTRY) || !defined(HAVE_WAIT_VAR_EVENT)
262         struct wait_bit_queue_entry entry;
263         wait_queue_entry_t *wqe = &entry.wq_entry;
264 #else
265         struct wait_bit_queue entry;
266         wait_queue_entry_t *wqe = &entry.wait;
267 #endif
268         init_wait_var_entry(&entry, md, 0);
269         prepare_to_wait_event(wq, wqe, TASK_IDLE);
270         if (md->md_flags & LNET_MD_FLAG_HANDLING) {
271                 /* Race with unlocked call to ->md_handler.
272                  * It is safe to drop the res_lock here as the
273                  * caller has only just claimed it.
274                  */
275                 lnet_res_unlock(cpt);
276                 schedule();
277                 /* Cannot check md now, it might be freed.  Caller
278                  * must reclaim reference and check.
279                  */
280                 lnet_res_lock(cpt);
281         }
282         finish_wait(wq, wqe);
283 }
284
285 static inline void
286 lnet_md_free(struct lnet_libmd *md)
287 {
288         unsigned int  size;
289
290         LASSERTF(!md->md_rspt_ptr, "md %px rsp %px\n", md, md->md_rspt_ptr);
291
292         size = offsetof(struct lnet_libmd, md_kiov[md->md_niov]);
293
294         if (size <= LNET_SMALL_MD_SIZE) {
295                 LIBCFS_MEM_MSG(md, size, "slab-freed");
296                 kmem_cache_free(lnet_small_mds_cachep, md);
297         } else {
298                 LIBCFS_FREE(md, size);
299         }
300 }
301
302 struct lnet_libhandle *lnet_res_lh_lookup(struct lnet_res_container *rec,
303                                      __u64 cookie);
304 void lnet_res_lh_initialize(struct lnet_res_container *rec,
305                             struct lnet_libhandle *lh);
306 static inline void
307 lnet_res_lh_invalidate(struct lnet_libhandle *lh)
308 {
309         /* ALWAYS called with resource lock held */
310         /* NB: cookie is still useful, don't reset it */
311         list_del(&lh->lh_hash_chain);
312 }
313
314 static inline void
315 lnet_md2handle(struct lnet_handle_md *handle, struct lnet_libmd *md)
316 {
317         handle->cookie = md->md_lh.lh_cookie;
318 }
319
320 static inline struct lnet_libmd *
321 lnet_handle2md(struct lnet_handle_md *handle)
322 {
323         /* ALWAYS called with resource lock held */
324         struct lnet_libhandle *lh;
325         int              cpt;
326
327         cpt = lnet_cpt_of_cookie(handle->cookie);
328         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
329                                 handle->cookie);
330         if (lh == NULL)
331                 return NULL;
332
333         return lh_entry(lh, struct lnet_libmd, md_lh);
334 }
335
336 static inline struct lnet_libmd *
337 lnet_wire_handle2md(struct lnet_handle_wire *wh)
338 {
339         /* ALWAYS called with resource lock held */
340         struct lnet_libhandle *lh;
341         int              cpt;
342
343         if (wh->wh_interface_cookie != the_lnet.ln_interface_cookie)
344                 return NULL;
345
346         cpt = lnet_cpt_of_cookie(wh->wh_object_cookie);
347         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
348                                 wh->wh_object_cookie);
349         if (lh == NULL)
350                 return NULL;
351
352         return lh_entry(lh, struct lnet_libmd, md_lh);
353 }
354
355 static inline void
356 lnet_peer_net_addref_locked(struct lnet_peer_net *lpn)
357 {
358         atomic_inc(&lpn->lpn_refcount);
359 }
360
361 extern void lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn);
362
363 static inline void
364 lnet_peer_net_decref_locked(struct lnet_peer_net *lpn)
365 {
366         if (atomic_dec_and_test(&lpn->lpn_refcount))
367                 lnet_destroy_peer_net_locked(lpn);
368 }
369
370 static inline void
371 lnet_peer_addref_locked(struct lnet_peer *lp)
372 {
373         atomic_inc(&lp->lp_refcount);
374 }
375
376 extern void lnet_destroy_peer_locked(struct lnet_peer *lp);
377
378 static inline void
379 lnet_peer_decref_locked(struct lnet_peer *lp)
380 {
381         if (atomic_dec_and_test(&lp->lp_refcount))
382                 lnet_destroy_peer_locked(lp);
383 }
384
385 static inline void
386 lnet_peer_ni_addref_locked(struct lnet_peer_ni *lp)
387 {
388         kref_get(&lp->lpni_kref);
389 }
390
391 extern void lnet_destroy_peer_ni_locked(struct kref *ref);
392
393 static inline void
394 lnet_peer_ni_decref_locked(struct lnet_peer_ni *lp)
395 {
396         kref_put(&lp->lpni_kref, lnet_destroy_peer_ni_locked);
397 }
398
399 static inline int
400 lnet_isrouter(struct lnet_peer_ni *lpni)
401 {
402         return lpni->lpni_peer_net->lpn_peer->lp_rtr_refcount != 0;
403 }
404
405 static inline void
406 lnet_ni_addref_locked(struct lnet_ni *ni, int cpt)
407 {
408         LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
409         LASSERT(*ni->ni_refs[cpt] >= 0);
410
411         (*ni->ni_refs[cpt])++;
412 }
413
414 static inline void
415 lnet_ni_addref(struct lnet_ni *ni)
416 {
417         lnet_net_lock(0);
418         lnet_ni_addref_locked(ni, 0);
419         lnet_net_unlock(0);
420 }
421
422 static inline void
423 lnet_ni_decref_locked(struct lnet_ni *ni, int cpt)
424 {
425         LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
426         LASSERT(*ni->ni_refs[cpt] > 0);
427
428         (*ni->ni_refs[cpt])--;
429 }
430
431 static inline void
432 lnet_ni_decref(struct lnet_ni *ni)
433 {
434         lnet_net_lock(0);
435         lnet_ni_decref_locked(ni, 0);
436         lnet_net_unlock(0);
437 }
438
439 static inline struct lnet_msg *
440 lnet_msg_alloc(void)
441 {
442         struct lnet_msg *msg;
443
444         msg = kmem_cache_zalloc(lnet_msg_cachep, GFP_NOFS);
445
446         return (msg);
447 }
448
449 static inline void
450 lnet_msg_free(struct lnet_msg *msg)
451 {
452         LASSERT(!msg->msg_onactivelist);
453         kmem_cache_free(lnet_msg_cachep, msg);
454 }
455
456 static inline struct lnet_rsp_tracker *
457 lnet_rspt_alloc(int cpt)
458 {
459         struct lnet_rsp_tracker *rspt;
460
461         rspt = kmem_cache_zalloc(lnet_rspt_cachep, GFP_NOFS);
462         if (rspt) {
463                 lnet_net_lock(cpt);
464                 the_lnet.ln_counters[cpt]->lct_health.lch_rst_alloc++;
465                 lnet_net_unlock(cpt);
466         }
467         LIBCFS_ALLOC_POST(rspt, sizeof(*rspt), "alloc");
468         return rspt;
469 }
470
471 static inline void
472 lnet_rspt_free(struct lnet_rsp_tracker *rspt, int cpt)
473 {
474         LIBCFS_FREE_PRE(rspt, sizeof(*rspt), "free");
475         kmem_cache_free(lnet_rspt_cachep, rspt);
476         lnet_net_lock(cpt);
477         the_lnet.ln_counters[cpt]->lct_health.lch_rst_alloc--;
478         lnet_net_unlock(cpt);
479 }
480
481 int lnet_configure(void *arg);
482 int lnet_unconfigure(void);
483
484 void lnet_ni_free(struct lnet_ni *ni);
485 void lnet_net_free(struct lnet_net *net);
486
487 struct lnet_net *
488 lnet_net_alloc(__u32 net_type, struct list_head *netlist);
489
490 struct lnet_ni *
491 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el,
492               char *iface);
493 struct lnet_ni *
494 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, struct lnet_nid *nid,
495                           u32 *cpts, u32 ncpts, char *iface);
496 int lnet_ni_add_interface(struct lnet_ni *ni, char *iface);
497
498 static inline int
499 lnet_nid2peerhash(struct lnet_nid *nid)
500 {
501         u32 h = 0;
502         int i;
503
504         for (i = 0; i < 4; i++)
505                 h = cfs_hash_32(nid->nid_addr[i]^h, 32);
506         return cfs_hash_32(LNET_NID_NET(nid) ^ h, LNET_PEER_HASH_BITS);
507 }
508
509 static inline struct list_head *
510 lnet_net2rnethash(__u32 net)
511 {
512         return &the_lnet.ln_remote_nets_hash[(LNET_NETNUM(net) +
513                 LNET_NETTYP(net)) &
514                 ((1U << the_lnet.ln_remote_nets_hbits) - 1)];
515 }
516
517 static inline void lnet_hdr_from_nid4(struct lnet_hdr *hdr,
518                                     const struct lnet_hdr_nid4 *vhdr)
519 {
520         const struct _lnet_hdr_nid4 *hdr_nid4 = (void *)vhdr;
521
522         lnet_nid4_to_nid(le64_to_cpu(hdr_nid4->dest_nid), &hdr->dest_nid);
523         lnet_nid4_to_nid(le64_to_cpu(hdr_nid4->src_nid), &hdr->src_nid);
524         hdr->dest_pid = le32_to_cpu(hdr_nid4->dest_pid);
525         hdr->src_pid = le32_to_cpu(hdr_nid4->src_pid);
526         hdr->type = le32_to_cpu(hdr_nid4->type);
527         hdr->payload_length = le32_to_cpu(hdr_nid4->payload_length);
528
529         hdr->msg = hdr_nid4->msg;
530 }
531
532 static inline void lnet_hdr_to_nid4(const struct lnet_hdr *hdr,
533                                       struct lnet_hdr_nid4 *vhdr)
534 {
535         struct _lnet_hdr_nid4 *hdr_nid4 = (void *)vhdr;
536
537         hdr_nid4->dest_nid = cpu_to_le64(lnet_nid_to_nid4(&hdr->dest_nid));
538         hdr_nid4->src_nid = cpu_to_le64(lnet_nid_to_nid4(&hdr->src_nid));
539         hdr_nid4->dest_pid = cpu_to_le32(hdr->dest_pid);
540         hdr_nid4->src_pid = cpu_to_le32(hdr->src_pid);
541         hdr_nid4->type = cpu_to_le32(hdr->type);
542         hdr_nid4->payload_length = cpu_to_le32(hdr->payload_length);
543
544         hdr_nid4->msg = hdr->msg;
545 }
546
547 static inline void lnet_hdr_from_nid16(struct lnet_hdr *hdr,
548                                         const struct lnet_hdr_nid16 *vhdr)
549 {
550         const struct lnet_hdr *hdr16 = (void *)vhdr;
551
552         hdr->dest_nid = hdr16->dest_nid;
553         hdr->src_nid = hdr16->src_nid;
554         hdr->dest_pid = le32_to_cpu(hdr16->dest_pid);
555         hdr->src_pid = le32_to_cpu(hdr16->src_pid);
556         hdr->type = le32_to_cpu(hdr16->type);
557         hdr->payload_length = le32_to_cpu(hdr16->payload_length);
558
559         hdr->msg = hdr16->msg;
560 }
561
562 static inline void lnet_hdr_to_nid16(const struct lnet_hdr *hdr,
563                                       struct lnet_hdr_nid16 *vhdr)
564 {
565         struct lnet_hdr *hdr16 = (void *)vhdr;
566
567         hdr16->dest_nid = hdr->dest_nid;
568         hdr16->src_nid = hdr->src_nid;
569         hdr16->dest_pid = cpu_to_le32(hdr->dest_pid);
570         hdr16->src_pid = cpu_to_le32(hdr->src_pid);
571         hdr16->type = cpu_to_le32(hdr->type);
572         hdr16->payload_length = cpu_to_le32(hdr->payload_length);
573
574         hdr16->msg = hdr->msg;
575 }
576
577 extern const struct lnet_lnd the_lolnd;
578 extern int avoid_asym_router_failure;
579
580 extern unsigned int lnet_nid_cpt_hash(struct lnet_nid *nid,
581                                       unsigned int number);
582 extern int lnet_cpt_of_nid_locked(struct lnet_nid *nid, struct lnet_ni *ni);
583 extern int lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni);
584 extern int lnet_nid2cpt(struct lnet_nid *nid, struct lnet_ni *ni);
585 extern struct lnet_ni *lnet_nid_to_ni_locked(struct lnet_nid *nid, int cpt);
586 extern struct lnet_ni *lnet_net2ni_locked(__u32 net, int cpt);
587 extern struct lnet_ni *lnet_net2ni_addref(__u32 net);
588 extern struct lnet_ni *lnet_nid_to_ni_addref(struct lnet_nid *nid);
589 struct lnet_net *lnet_get_net_locked(__u32 net_id);
590
591 int lnet_lib_init(void);
592 void lnet_lib_exit(void);
593 void lnet_router_exit(void);
594
595 extern unsigned int lnet_response_tracking;
596 extern unsigned lnet_transaction_timeout;
597 extern unsigned lnet_retry_count;
598 extern unsigned int lnet_lnd_timeout;
599 extern unsigned int lnet_numa_range;
600 extern unsigned int lnet_health_sensitivity;
601 extern unsigned int lnet_recovery_interval;
602 extern unsigned int lnet_recovery_limit;
603 extern unsigned int lnet_peer_discovery_disabled;
604 extern unsigned int lnet_drop_asym_route;
605 extern unsigned int lnet_max_recovery_ping_interval;
606 extern unsigned int lnet_max_recovery_ping_count;
607 extern unsigned int router_sensitivity_percentage;
608 extern int alive_router_check_interval;
609 extern int live_router_check_interval;
610 extern int dead_router_check_interval;
611 extern int portal_rotor;
612 extern int lock_prim_nid;
613
614 void lnet_mt_event_handler(struct lnet_event *event);
615
616 int lnet_notify(struct lnet_ni *ni, struct lnet_nid *peer, bool alive,
617                 bool reset, time64_t when);
618 void lnet_notify_locked(struct lnet_peer_ni *lp, int notifylnd, int alive,
619                         time64_t when);
620 int lnet_add_route(__u32 net, __u32 hops, struct lnet_nid *gateway,
621                    __u32 priority, __u32 sensitivity);
622 int lnet_del_route(__u32 net, struct lnet_nid *gw_nid);
623 void lnet_move_route(struct lnet_route *route, struct lnet_peer *lp,
624                      struct list_head *rt_list);
625 void lnet_destroy_routes(void);
626 int lnet_get_route(int idx, __u32 *net, __u32 *hops,
627                    lnet_nid_t *gateway, __u32 *alive, __u32 *priority,
628                    __u32 *sensitivity);
629 int lnet_get_rtr_pool_cfg(int idx, struct lnet_ioctl_pool_cfg *pool_cfg);
630 struct lnet_ni *lnet_get_next_ni_locked(struct lnet_net *mynet,
631                                         struct lnet_ni *prev);
632 struct lnet_ni *lnet_get_ni_idx_locked(int idx);
633 int lnet_get_net_healthv_locked(struct lnet_net *net);
634
635 extern int lnet_get_peer_list(__u32 *countp, __u32 *sizep,
636                               struct lnet_process_id __user *ids);
637 void lnet_peer_ni_set_healthv(struct lnet_nid *nid, int value, bool all);
638 extern void lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni,
639                                                  struct list_head *queue,
640                                                  time64_t now);
641 extern int lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni,
642                                   struct lnet_nid *nid);
643 extern void lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni);
644 extern int lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni,
645                                   struct lnet_nid *nid);
646 void lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni,
647                                          __u32 priority);
648 extern void lnet_ni_add_to_recoveryq_locked(struct lnet_ni *ni,
649                                             struct list_head *queue,
650                                             time64_t now);
651
652 void lnet_router_debugfs_init(void);
653 void lnet_router_debugfs_fini(void);
654 int  lnet_rtrpools_alloc(int im_a_router);
655 void lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages);
656 int  lnet_rtrpools_adjust(int tiny, int small, int large);
657 int lnet_rtrpools_enable(void);
658 void lnet_rtrpools_disable(void);
659 void lnet_rtrpools_free(int keep_pools);
660 void lnet_rtr_transfer_to_peer(struct lnet_peer *src,
661                                struct lnet_peer *target);
662 struct lnet_remotenet *lnet_find_rnet_locked(__u32 net);
663 int lnet_dyn_add_net(struct lnet_ioctl_config_data *conf);
664 int lnet_dyn_del_net(__u32 net);
665 int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf, u32 net,
666                     struct lnet_nid *nid,
667                     struct lnet_ioctl_config_lnd_tunables *tun);
668 int lnet_dyn_del_ni(struct lnet_nid *nid);
669 int lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason);
670 struct lnet_net *lnet_get_net_locked(__u32 net_id);
671 void lnet_net_clr_pref_rtrs(struct lnet_net *net);
672 int lnet_net_add_pref_rtr(struct lnet_net *net, struct lnet_nid *gw_nid);
673
674 int lnet_islocalnid(struct lnet_nid *nid);
675 int lnet_islocalnet(__u32 net);
676 int lnet_islocalnet_locked(__u32 net);
677
678 void lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
679                         unsigned int offset, unsigned int mlen);
680 void lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev);
681 void lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type);
682 void lnet_msg_commit(struct lnet_msg *msg, int cpt);
683 void lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status);
684
685 void lnet_prep_send(struct lnet_msg *msg, int type,
686                     struct lnet_processid *target, unsigned int offset,
687                     unsigned int len);
688 int lnet_send(struct lnet_nid *nid, struct lnet_msg *msg,
689               struct lnet_nid *rtr_nid);
690 int lnet_send_ping(struct lnet_nid *dest_nid, struct lnet_handle_md *mdh,
691                    int bytes, void *user_ptr, lnet_handler_t handler,
692                    bool recovery);
693 void lnet_return_tx_credits_locked(struct lnet_msg *msg);
694 void lnet_return_rx_credits_locked(struct lnet_msg *msg);
695 void lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp);
696 void lnet_drop_routed_msgs_locked(struct list_head *list, int cpt);
697
698 struct list_head **lnet_create_array_of_queues(void);
699
700 /* portals functions */
701 /* portals attributes */
702 static inline int
703 lnet_ptl_is_lazy(struct lnet_portal *ptl)
704 {
705         return !!(ptl->ptl_options & LNET_PTL_LAZY);
706 }
707
708 static inline int
709 lnet_ptl_is_unique(struct lnet_portal *ptl)
710 {
711         return !!(ptl->ptl_options & LNET_PTL_MATCH_UNIQUE);
712 }
713
714 static inline int
715 lnet_ptl_is_wildcard(struct lnet_portal *ptl)
716 {
717         return !!(ptl->ptl_options & LNET_PTL_MATCH_WILDCARD);
718 }
719
720 static inline void
721 lnet_ptl_setopt(struct lnet_portal *ptl, int opt)
722 {
723         ptl->ptl_options |= opt;
724 }
725
726 static inline void
727 lnet_ptl_unsetopt(struct lnet_portal *ptl, int opt)
728 {
729         ptl->ptl_options &= ~opt;
730 }
731
732 /* match-table functions */
733 struct list_head *lnet_mt_match_head(struct lnet_match_table *mtable,
734                                struct lnet_processid *id, __u64 mbits);
735 struct lnet_match_table *lnet_mt_of_attach(unsigned int index,
736                                            struct lnet_processid *id,
737                                            __u64 mbits, __u64 ignore_bits,
738                                            enum lnet_ins_pos pos);
739 int lnet_mt_match_md(struct lnet_match_table *mtable,
740                      struct lnet_match_info *info, struct lnet_msg *msg);
741
742 /* portals match/attach functions */
743 void lnet_ptl_attach_md(struct lnet_me *me, struct lnet_libmd *md,
744                         struct list_head *matches, struct list_head *drops);
745 void lnet_ptl_detach_md(struct lnet_me *me, struct lnet_libmd *md);
746 int lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg);
747
748 /* initialized and finalize portals */
749 int lnet_portals_create(void);
750 void lnet_portals_destroy(void);
751
752 /* message functions */
753 int lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr,
754                struct lnet_nid *fromnid, void *private, int rdma_req);
755 int lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg);
756 int lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg);
757
758 void lnet_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
759                int delayed, unsigned int offset, unsigned int mlen,
760                unsigned int rlen);
761 void lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
762                   int delayed, unsigned int offset,
763                   unsigned int mlen, unsigned int rlen);
764 void lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg);
765
766 struct lnet_msg *lnet_create_reply_msg(struct lnet_ni *ni,
767                                        struct lnet_msg *get_msg);
768 void lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *msg,
769                             unsigned int len);
770 void lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt);
771 void lnet_clean_zombie_rstqs(void);
772
773 bool lnet_md_discarded(struct lnet_libmd *md);
774 void lnet_finalize(struct lnet_msg *msg, int rc);
775 bool lnet_send_error_simulation(struct lnet_msg *msg,
776                                 enum lnet_msg_hstatus *hstatus);
777 void lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni);
778
779 void lnet_drop_message(struct lnet_ni *ni, int cpt, void *private,
780                        unsigned int nob, __u32 msg_type);
781 void lnet_drop_delayed_msg_list(struct list_head *head, char *reason);
782 void lnet_recv_delayed_msg_list(struct list_head *head);
783
784 int lnet_msg_container_setup(struct lnet_msg_container *container, int cpt);
785 void lnet_msg_container_cleanup(struct lnet_msg_container *container);
786 void lnet_msg_containers_destroy(void);
787 int lnet_msg_containers_create(void);
788
789 char *lnet_health_error2str(enum lnet_msg_hstatus hstatus);
790 char *lnet_msgtyp2str(int type);
791 int lnet_fail_nid(lnet_nid_t nid, unsigned int threshold);
792
793 /** \addtogroup lnet_fault_simulation @{ */
794
795 /* See struct lnet_fault_attr4 for a description of these fields */
796 struct lnet_fault_large_attr {
797         struct lnet_nid                 fa_src;
798         struct lnet_nid                 fa_dst;
799         struct lnet_nid                 fa_local_nid;
800         u64                             fa_ptl_mask;
801         u32                             fa_msg_mask;
802         union {
803                 struct {
804                         u32             da_rate;
805                         u32             da_interval;
806                         u32             da_health_error_mask;
807                         u32             da_random:1,
808                                         da_drop_all:1;
809                 } drop;
810                 struct {
811                         u32             la_rate;
812                         u32             la_interval;
813                         u32             la_latency;
814                 } delay;
815         } u;
816 };
817
818 int lnet_fault_ctl(int cmd, struct libcfs_ioctl_data *data);
819 int lnet_fault_init(void);
820 void lnet_fault_fini(void);
821
822 bool lnet_drop_rule_match(struct lnet_hdr *hdr, struct lnet_nid *local_nid,
823                           enum lnet_msg_hstatus *hstatus);
824
825 int lnet_delay_rule_add(struct lnet_fault_large_attr *attr);
826 int lnet_delay_rule_del(struct lnet_nid *src, struct lnet_nid *dst,
827                         bool shutdown);
828 int lnet_delay_rule_list(int pos, struct lnet_fault_large_attr *attr,
829                          struct lnet_fault_stat *stat);
830 void lnet_delay_rule_reset(void);
831 void lnet_delay_rule_check(void);
832 bool lnet_delay_rule_match_locked(struct lnet_hdr *hdr, struct lnet_msg *msg);
833
834 /** @} lnet_fault_simulation */
835
836 void lnet_counters_get_common(struct lnet_counters_common *common);
837 int lnet_counters_get(struct lnet_counters *counters);
838 void lnet_counters_reset(void);
839 static inline void
840 lnet_ni_set_sel_priority_locked(struct lnet_ni *ni, __u32 priority)
841 {
842         ni->ni_sel_priority = priority;
843 }
844
845 static inline void
846 lnet_net_set_sel_priority_locked(struct lnet_net *net, __u32 priority)
847 {
848         net->net_sel_priority = priority;
849 }
850
851 unsigned int lnet_iov_nob(unsigned int niov, struct kvec *iov);
852 unsigned int lnet_kiov_nob(unsigned int niov, struct bio_vec *iov);
853 int lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
854                       int src_niov, struct bio_vec *src,
855                       unsigned int offset, unsigned int len);
856
857 void lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov,
858                        unsigned int doffset,
859                        unsigned int nsiov, struct kvec *siov,
860                        unsigned int soffset, unsigned int nob);
861 void lnet_copy_kiov2iov(unsigned int niov, struct kvec *iov,
862                         unsigned int iovoffset,
863                         unsigned int nkiov, struct bio_vec *kiov,
864                         unsigned int kiovoffset, unsigned int nob);
865 void lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
866                         unsigned int kiovoffset,
867                         unsigned int niov, struct kvec *iov,
868                         unsigned int iovoffset, unsigned int nob);
869 void lnet_copy_kiov2kiov(unsigned int ndkiov, struct bio_vec *dkiov,
870                          unsigned int doffset,
871                          unsigned int nskiov, struct bio_vec *skiov,
872                          unsigned int soffset, unsigned int nob);
873
874 static inline void
875 lnet_copy_kiov2flat(int dlen, void *dest, unsigned int doffset,
876                     unsigned int nsiov, struct bio_vec *skiov,
877                     unsigned int soffset, unsigned int nob)
878 {
879         struct kvec diov = { .iov_base = dest, .iov_len = dlen };
880
881         lnet_copy_kiov2iov(1, &diov, doffset,
882                            nsiov, skiov, soffset, nob);
883 }
884
885 static inline void
886 lnet_copy_flat2kiov(unsigned int ndiov, struct bio_vec *dkiov,
887                     unsigned int doffset, int slen, void *src,
888                     unsigned int soffset, unsigned int nob)
889 {
890         struct kvec siov = { .iov_base = src, .iov_len = slen };
891         lnet_copy_iov2kiov(ndiov, dkiov, doffset,
892                            1, &siov, soffset, nob);
893 }
894
895 void lnet_me_unlink(struct lnet_me *me);
896
897 void lnet_md_unlink(struct lnet_libmd *md);
898 void lnet_md_deconstruct(struct lnet_libmd *lmd, struct lnet_event *ev);
899 struct page *lnet_get_first_page(struct lnet_libmd *md, unsigned int offset);
900 int lnet_cpt_of_md(struct lnet_libmd *md, unsigned int offset);
901
902 unsigned int lnet_get_lnd_timeout(void);
903 void lnet_register_lnd(const struct lnet_lnd *lnd);
904 void lnet_unregister_lnd(const struct lnet_lnd *lnd);
905
906 struct socket *lnet_connect(struct lnet_nid *peer_nid, int interface,
907                             struct sockaddr *peeraddr, struct net *ns);
908 void lnet_connect_console_error(int rc, struct lnet_nid *peer_nid,
909                                 struct sockaddr *sa);
910 int lnet_count_acceptor_nets(void);
911 int lnet_acceptor_timeout(void);
912 int lnet_acceptor_port(void);
913 int lnet_acceptor_start(void);
914 void lnet_acceptor_stop(void);
915
916 struct lnet_inetdev {
917         u32     li_cpt;
918         union {
919                 struct {
920                         u32     li_ipaddr;
921                         u32     li_netmask;
922                 };
923                 u32     li_ipv6addr[4];
924         };
925         u32     li_index;
926         bool    li_iff_master;
927         u32     li_size;
928         char    li_name[IFNAMSIZ];
929 };
930
931 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns,
932                         bool v6);
933 int lnet_inet_select(struct lnet_ni *ni, struct lnet_inetdev *ifaces,
934                      int num_ifaces);
935
936 void lnet_sock_setbuf(struct socket *socket, int txbufsize, int rxbufsize);
937 void lnet_sock_getbuf(struct socket *socket, int *txbufsize, int *rxbufsize);
938 int lnet_sock_getaddr(struct socket *socket, bool remote,
939                       struct sockaddr_storage *peer);
940 int lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout);
941 int lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout);
942
943 struct socket *lnet_sock_listen(int port, int backlog,
944                                 struct net *ns);
945 struct socket *lnet_sock_connect(int interface, int local_port,
946                                  struct sockaddr *peeraddr,
947                                  struct net *ns);
948
949 int lnet_peers_start_down(void);
950 int lnet_peer_buffer_credits(struct lnet_net *net);
951 void lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
952                                     struct lnet_peer *new_lp);
953 void lnet_router_discovery_complete(struct lnet_peer *lp);
954 void lnet_router_discovery_ping_reply(struct lnet_peer *lp,
955                                       struct lnet_ping_buffer *pbuf);
956
957 int lnet_monitor_thr_start(void);
958 void lnet_monitor_thr_stop(void);
959
960 bool lnet_router_checker_active(void);
961 void lnet_check_routers(void);
962 void lnet_wait_router_start(void);
963 void lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf);
964
965 int lnet_ping_info_validate(struct lnet_ping_info *pinfo);
966 struct lnet_ping_buffer *lnet_ping_buffer_alloc(int bytes, gfp_t gfp);
967 void lnet_ping_buffer_free(struct lnet_ping_buffer *pbuf);
968 int lnet_get_link_status(struct net_device *dev);
969 __u32 lnet_set_link_fatal_state(struct lnet_ni *ni, unsigned int link_state);
970
971 static inline void lnet_ping_buffer_addref(struct lnet_ping_buffer *pbuf)
972 {
973         atomic_inc(&pbuf->pb_refcnt);
974 }
975
976 static inline void lnet_ping_buffer_decref(struct lnet_ping_buffer *pbuf)
977 {
978         if (atomic_dec_and_test(&pbuf->pb_refcnt)) {
979                 wake_up_var(&pbuf->pb_refcnt);
980                 lnet_ping_buffer_free(pbuf);
981         }
982 }
983
984 struct lnet_ping_iter {
985         struct lnet_ping_info   *pinfo;
986         void                    *pos, *end;
987 };
988
989 u32 *ping_iter_first(struct lnet_ping_iter *pi, struct lnet_ping_buffer *pbuf,
990                      struct lnet_nid *nid);
991 u32 *ping_iter_next(struct lnet_ping_iter *pi, struct lnet_nid *nid);
992 int ping_info_count_entries(struct lnet_ping_buffer *pbuf);
993
994 static inline int lnet_push_target_resize_needed(void)
995 {
996         return the_lnet.ln_push_target->pb_nbytes < the_lnet.ln_push_target_nbytes;
997 }
998
999 int lnet_push_target_resize(void);
1000 int lnet_push_target_post(struct lnet_ping_buffer *pbuf,
1001                           struct lnet_handle_md *mdh);
1002 void lnet_peer_push_event(struct lnet_event *ev);
1003
1004 int lnet_parse_ip2nets(const char **networksp, const char *ip2nets);
1005 int lnet_parse_routes(const char *route_str, int *im_a_router);
1006 int lnet_parse_networks(struct list_head *nilist, const char *networks);
1007 bool lnet_net_unique(__u32 net_id, struct list_head *nilist,
1008                      struct lnet_net **net);
1009 bool lnet_ni_unique_net(struct list_head *nilist, char *iface);
1010 void lnet_incr_dlc_seq(void);
1011 __u32 lnet_get_dlc_seq_locked(void);
1012
1013 struct lnet_peer_net *lnet_get_next_peer_net_locked(struct lnet_peer *lp,
1014                                                     __u32 prev_lpn_id);
1015 struct lnet_peer_ni *lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
1016                                                   struct lnet_peer_net *peer_net,
1017                                                   struct lnet_peer_ni *prev);
1018 struct lnet_peer_ni *lnet_peerni_by_nid_locked(struct lnet_nid *nid,
1019                                                struct lnet_nid *pref,
1020                                                int cpt);
1021 struct lnet_peer_ni *lnet_nid2peerni_ex(struct lnet_nid *nid);
1022 struct lnet_peer_ni *lnet_peer_ni_get_locked(struct lnet_peer *lp,
1023                                              struct lnet_nid *nid);
1024 struct lnet_peer_ni *lnet_peer_ni_find_locked(struct lnet_nid *nid);
1025 struct lnet_peer *lnet_find_peer(struct lnet_nid *nid);
1026 void lnet_peer_net_added(struct lnet_net *net);
1027 void lnet_peer_primary_nid_locked(struct lnet_nid *nid,
1028                                   struct lnet_nid *result);
1029 int lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block);
1030 void lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg);
1031 int lnet_peer_discovery_start(void);
1032 void lnet_peer_discovery_stop(void);
1033 void lnet_push_update_to_peers(int force);
1034 void lnet_peer_tables_cleanup(struct lnet_net *net);
1035 void lnet_peer_uninit(void);
1036 int lnet_peer_tables_create(void);
1037 void lnet_debug_peer(struct lnet_nid *nid);
1038 struct lnet_peer_net *lnet_peer_get_net_locked(struct lnet_peer *peer,
1039                                                __u32 net_id);
1040 bool lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni,
1041                                   struct lnet_nid *nid);
1042 int lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid);
1043 void lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni);
1044 bool lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
1045                                   struct lnet_nid *gw_nid);
1046 void lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni);
1047 int lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni, struct lnet_nid *nid);
1048 int lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni,
1049                                      struct lnet_nid *nid);
1050 int lnet_user_add_peer_ni(struct lnet_nid *key_nid, struct lnet_nid *nid,
1051                           bool mr, bool lock_prim);
1052 int lnet_del_peer_ni(struct lnet_nid *key_nid, struct lnet_nid *nid,
1053                      int force);
1054 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk);
1055 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1056                           char alivness[LNET_MAX_STR_LEN],
1057                           __u32 *cpt_iter, __u32 *refcount,
1058                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1059                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credtis,
1060                           __u32 *peer_tx_qnob);
1061 int lnet_get_peer_ni_hstats(struct lnet_ioctl_peer_ni_hstats *stats);
1062
1063 static inline void
1064 lnet_peer_net_set_sel_priority_locked(struct lnet_peer_net *lpn, __u32 priority)
1065 {
1066         lpn->lpn_sel_priority = priority;
1067 }
1068
1069
1070 static inline struct lnet_peer_net *
1071 lnet_find_peer_net_locked(struct lnet_peer *peer, __u32 net_id)
1072 {
1073         struct lnet_peer_net *peer_net;
1074
1075         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1076                 if (peer_net->lpn_net_id == net_id)
1077                         return peer_net;
1078         }
1079
1080         return NULL;
1081 }
1082
1083 static inline bool
1084 lnet_peer_is_multi_rail(struct lnet_peer *lp)
1085 {
1086         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1087                 return true;
1088         return false;
1089 }
1090
1091 static inline bool
1092 lnet_peer_ni_is_configured(struct lnet_peer_ni *lpni)
1093 {
1094         if (lpni->lpni_peer_net->lpn_peer->lp_state & LNET_PEER_CONFIGURED)
1095                 return true;
1096         return false;
1097 }
1098
1099 static inline bool
1100 lnet_peer_ni_is_primary(struct lnet_peer_ni *lpni)
1101 {
1102         return nid_same(&lpni->lpni_nid,
1103                          &lpni->lpni_peer_net->lpn_peer->lp_primary_nid);
1104 }
1105
1106 bool lnet_peer_is_uptodate(struct lnet_peer *lp);
1107 bool lnet_peer_is_uptodate_locked(struct lnet_peer *lp);
1108 bool lnet_is_discovery_disabled(struct lnet_peer *lp);
1109 bool lnet_is_discovery_disabled_locked(struct lnet_peer *lp);
1110 bool lnet_peer_gw_discovery(struct lnet_peer *lp);
1111
1112 static inline bool
1113 lnet_peer_needs_push(struct lnet_peer *lp)
1114 {
1115         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL))
1116                 return false;
1117         if (lp->lp_state & LNET_PEER_MARK_DELETED)
1118                 return false;
1119         if (lp->lp_state & LNET_PEER_FORCE_PUSH)
1120                 return true;
1121         if (lp->lp_state & LNET_PEER_NO_DISCOVERY)
1122                 return false;
1123         /* if discovery is not enabled then no need to push */
1124         if (lnet_peer_discovery_disabled)
1125                 return false;
1126         if (lp->lp_node_seqno < atomic_read(&the_lnet.ln_ping_target_seqno))
1127                 return true;
1128         return false;
1129 }
1130
1131 static inline unsigned int
1132 lnet_get_next_recovery_ping(unsigned int ping_count, time64_t now)
1133 {
1134         unsigned int interval;
1135
1136         /* lnet_max_recovery_interval <= 2^lnet_max_recovery_ping_count */
1137         if (ping_count > lnet_max_recovery_ping_count)
1138                 interval = lnet_max_recovery_ping_interval;
1139         else
1140                 interval = 1 << ping_count;
1141
1142         return now + interval;
1143 }
1144
1145 static inline void
1146 lnet_peer_ni_set_next_ping(struct lnet_peer_ni *lpni, time64_t now)
1147 {
1148         lpni->lpni_next_ping =
1149                 lnet_get_next_recovery_ping(lpni->lpni_ping_count, now);
1150 }
1151
1152 static inline void
1153 lnet_ni_set_next_ping(struct lnet_ni *ni, time64_t now)
1154 {
1155         ni->ni_next_ping = lnet_get_next_recovery_ping(ni->ni_ping_count, now);
1156 }
1157
1158 /*
1159  * A peer NI is alive if it satisfies the following two conditions:
1160  *  1. peer NI health >= LNET_MAX_HEALTH_VALUE * router_sensitivity_percentage
1161  *  2. the cached NI status received when we discover the peer is UP
1162  */
1163 static inline bool
1164 lnet_is_peer_ni_alive(struct lnet_peer_ni *lpni)
1165 {
1166         bool halive = false;
1167
1168         halive = (atomic_read(&lpni->lpni_healthv) >=
1169                  (LNET_MAX_HEALTH_VALUE * router_sensitivity_percentage / 100));
1170
1171         return halive && lpni->lpni_ns_status == LNET_NI_STATUS_UP;
1172 }
1173
1174 static inline void
1175 lnet_update_peer_net_healthv(struct lnet_peer_ni *lpni)
1176 {
1177         struct lnet_peer_net *lpn;
1178         int best_healthv = 0;
1179
1180         lpn = lpni->lpni_peer_net;
1181
1182         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
1183                 int lpni_healthv = atomic_read(&lpni->lpni_healthv);
1184                 if (best_healthv < lpni_healthv)
1185                         best_healthv = lpni_healthv;
1186         }
1187
1188         lpn->lpn_healthv = best_healthv;
1189 }
1190
1191 static inline void
1192 lnet_set_lpni_healthv_locked(struct lnet_peer_ni *lpni, int value)
1193 {
1194         if (atomic_read(&lpni->lpni_healthv) == value)
1195                 return;
1196         atomic_set(&lpni->lpni_healthv, value);
1197         lnet_update_peer_net_healthv(lpni);
1198 }
1199
1200 static inline bool
1201 lnet_atomic_add_unless_max(atomic_t *v, int a, int u)
1202 {
1203         int c = atomic_read(v);
1204         bool mod = false;
1205         int old;
1206         int m;
1207
1208         if (c == u)
1209                 return mod;
1210
1211         for (;;) {
1212                 if (c + a >= u)
1213                         m = u;
1214                 else
1215                         m = c + a;
1216                 old = atomic_cmpxchg(v, c, m);
1217
1218                 if (old == u)
1219                         break;
1220
1221                 if (old == c) {
1222                         mod = true;
1223                         break;
1224                 }
1225                 c = old;
1226         }
1227
1228         return mod;
1229 }
1230
1231 static bool
1232 lnet_dec_healthv_locked(atomic_t *healthv, int sensitivity)
1233 {
1234         int h = atomic_read(healthv);
1235
1236         if (h == 0)
1237                 return false;
1238
1239         if (h < sensitivity)
1240                 h = 0;
1241         else
1242                 h -= sensitivity;
1243
1244         return (atomic_xchg(healthv, h) != h);
1245 }
1246
1247 static inline void
1248 lnet_dec_lpni_healthv_locked(struct lnet_peer_ni *lpni)
1249 {
1250         /* If there is a health sensitivity in the peer then use that
1251          * instead of the globally set one.
1252          * only adjust the net health if the lpni health value changed
1253          */
1254         if (lnet_dec_healthv_locked(&lpni->lpni_healthv,
1255                         lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity ? :
1256                         lnet_health_sensitivity)) {
1257                 lnet_update_peer_net_healthv(lpni);
1258         }
1259 }
1260
1261 static inline void
1262 lnet_inc_lpni_healthv_locked(struct lnet_peer_ni *lpni)
1263 {
1264         /* If there is a health sensitivity in the peer then use that
1265          * instead of the globally set one.
1266          * only adjust the net health if the lpni health value changed
1267          */
1268         if (lnet_atomic_add_unless_max(&lpni->lpni_healthv,
1269                         lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity ? :
1270                         lnet_health_sensitivity,
1271                                        LNET_MAX_HEALTH_VALUE)) {
1272                 lnet_update_peer_net_healthv(lpni);
1273         }
1274 }
1275
1276 static inline void
1277 lnet_inc_healthv(atomic_t *healthv, int value)
1278 {
1279         lnet_atomic_add_unless_max(healthv, value, LNET_MAX_HEALTH_VALUE);
1280 }
1281
1282 static inline int
1283 lnet_get_list_len(struct list_head *list)
1284 {
1285         struct list_head *l;
1286         int count = 0;
1287
1288         list_for_each(l, list)
1289                 count++;
1290
1291         return count;
1292 }
1293
1294 void lnet_incr_stats(struct lnet_element_stats *stats,
1295                      enum lnet_msg_type msg_type,
1296                      enum lnet_stats_type stats_type);
1297
1298 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
1299                      enum lnet_stats_type stats_type);
1300
1301 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
1302                               struct lnet_element_stats *stats);
1303 static inline void
1304 lnet_set_route_aliveness(struct lnet_route *route, bool alive)
1305 {
1306         bool old = atomic_xchg(&route->lr_alive, alive);
1307
1308         if (old != alive)
1309                 CERROR("route to %s through %s has gone from %s to %s\n",
1310                        libcfs_net2str(route->lr_net),
1311                        libcfs_nidstr(&route->lr_gateway->lp_primary_nid),
1312                        old ? "up" : "down",
1313                        alive ? "up" : "down");
1314 }
1315
1316 extern struct blocking_notifier_head lnet_ioctl_list;
1317 static inline int notifier_from_ioctl_errno(int err)
1318 {
1319         if (err == -EINVAL)
1320                 return NOTIFY_OK;
1321         return notifier_from_errno(err) | NOTIFY_STOP_MASK;
1322 }
1323
1324 void lnet_mark_ping_buffer_for_update(void);
1325 void lnet_queue_ping_buffer_update(void);
1326 #endif