Whamcloud - gitweb
LU-11299 lnet: use discovery for routing
[fs/lustre-release.git] / lnet / lnet / router.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2017, Intel Corporation.
5  *
6  *   This file is part of Lustre, https://wiki.whamcloud.com/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <linux/random.h>
26 #include <lnet/lib-lnet.h>
27
28 #define LNET_NRB_TINY_MIN       512     /* min value for each CPT */
29 #define LNET_NRB_TINY           (LNET_NRB_TINY_MIN * 4)
30 #define LNET_NRB_SMALL_MIN      4096    /* min value for each CPT */
31 #define LNET_NRB_SMALL          (LNET_NRB_SMALL_MIN * 4)
32 #define LNET_NRB_SMALL_PAGES    1
33 #define LNET_NRB_LARGE_MIN      256     /* min value for each CPT */
34 #define LNET_NRB_LARGE          (LNET_NRB_LARGE_MIN * 4)
35 #define LNET_NRB_LARGE_PAGES    ((LNET_MTU + PAGE_SIZE - 1) >> \
36                                   PAGE_SHIFT)
37
38 static char *forwarding = "";
39 module_param(forwarding, charp, 0444);
40 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
41
42 static int tiny_router_buffers;
43 module_param(tiny_router_buffers, int, 0444);
44 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
45 static int small_router_buffers;
46 module_param(small_router_buffers, int, 0444);
47 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
48 static int large_router_buffers;
49 module_param(large_router_buffers, int, 0444);
50 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
51 static int peer_buffer_credits;
52 module_param(peer_buffer_credits, int, 0444);
53 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
54
55 static int auto_down = 1;
56 module_param(auto_down, int, 0444);
57 MODULE_PARM_DESC(auto_down, "Automatically mark peers down on comms error");
58
59 int
60 lnet_peer_buffer_credits(struct lnet_net *net)
61 {
62         /* NI option overrides LNet default */
63         if (net->net_tunables.lct_peer_rtr_credits > 0)
64                 return net->net_tunables.lct_peer_rtr_credits;
65         if (peer_buffer_credits > 0)
66                 return peer_buffer_credits;
67
68         /* As an approximation, allow this peer the same number of router
69          * buffers as it is allowed outstanding sends */
70         return net->net_tunables.lct_peer_tx_credits;
71 }
72
73 static int check_routers_before_use;
74 module_param(check_routers_before_use, int, 0444);
75 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
76
77 int avoid_asym_router_failure = 1;
78 module_param(avoid_asym_router_failure, int, 0644);
79 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
80
81 int alive_router_check_interval = 60;
82 module_param(alive_router_check_interval, int, 0644);
83 MODULE_PARM_DESC(alive_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
84
85 static int router_ping_timeout = 50;
86 module_param(router_ping_timeout, int, 0644);
87 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
88
89 /*
90  * A value between 0 and 100. 0 meaning that even if router's interfaces
91  * have the worse health still consider the gateway usable.
92  * 100 means that at least one interface on the route's remote net is 100%
93  * healthy to consider the route alive.
94  * The default is set to 100 to ensure we maintain the original behavior.
95  */
96 unsigned int router_sensitivity_percentage = 100;
97 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
98 static struct kernel_param_ops param_ops_rtr_sensitivity = {
99         .set = rtr_sensitivity_set,
100         .get = param_get_int,
101 };
102 #define param_check_rtr_sensitivity(name, p) \
103                 __param_check(name, p, int)
104 #ifdef HAVE_KERNEL_PARAM_OPS
105 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
106 #else
107 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
108                   &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
109 #endif
110 MODULE_PARM_DESC(router_sensitivity_percentage,
111                 "How healthy a gateway should be to be used in percent");
112
113 static int
114 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
115 {
116         int rc;
117         unsigned *sen = (unsigned *)kp->arg;
118         unsigned long value;
119
120         rc = kstrtoul(val, 0, &value);
121         if (rc) {
122                 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
123                 return rc;
124         }
125
126         if (value < 0 || value > 100) {
127                 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
128                 return -EINVAL;
129         }
130
131         /*
132          * The purpose of locking the api_mutex here is to ensure that
133          * the correct value ends up stored properly.
134          */
135         mutex_lock(&the_lnet.ln_api_mutex);
136
137         *sen = value;
138
139         mutex_unlock(&the_lnet.ln_api_mutex);
140
141         return 0;
142 }
143
144 int
145 lnet_peers_start_down(void)
146 {
147         return check_routers_before_use;
148 }
149
150 /*
151  * A net is alive if at least one gateway NI on the network is alive.
152  */
153 static bool
154 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
155 {
156         struct lnet_peer_ni *lpni;
157
158         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
159                 if (lnet_is_peer_ni_alive(lpni))
160                         return true;
161         }
162
163         return false;
164 }
165
166 /*
167  * a gateway is alive only if all its nets are alive
168  * called with cpt lock held
169  */
170 bool lnet_is_gateway_alive(struct lnet_peer *gw)
171 {
172         struct lnet_peer_net *lpn;
173
174         list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
175                 if (!lnet_is_gateway_net_alive(lpn))
176                         return false;
177         }
178
179         return true;
180 }
181
182 /*
183  * lnet_is_route_alive() needs to be called with cpt lock held
184  * A route is alive if the gateway can route between the local network and
185  * the remote network of the route.
186  * This means at least one NI is alive on each of the local and remote
187  * networks of the gateway.
188  */
189 bool lnet_is_route_alive(struct lnet_route *route)
190 {
191         struct lnet_peer *gw = route->lr_gateway;
192         struct lnet_peer_net *llpn;
193         struct lnet_peer_net *rlpn;
194         bool route_alive;
195
196         /*
197          * check the gateway's interfaces on the route rnet to make sure
198          * that the gateway is viable.
199          */
200         llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
201         if (!llpn)
202                 return false;
203
204         route_alive = lnet_is_gateway_net_alive(llpn);
205
206         if (avoid_asym_router_failure) {
207                 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
208                 if (!rlpn)
209                         return false;
210                 route_alive = route_alive &&
211                               lnet_is_gateway_net_alive(rlpn);
212         }
213
214         if (!route_alive)
215                 return route_alive;
216
217         spin_lock(&gw->lp_lock);
218         if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
219                 if (gw->lp_rtr_refcount > 0)
220                         CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
221                                libcfs_nid2str(gw->lp_primary_nid));
222                 route_alive = false;
223         }
224         spin_unlock(&gw->lp_lock);
225
226         return route_alive;
227 }
228
229 void
230 lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
231                                struct lnet_peer *new_lp)
232 {
233         struct lnet_peer_ni *lpni;
234         struct lnet_route *route;
235
236         /*
237          * Although a route is correlated with a peer, but when it's added
238          * a specific NID is used. That NID refers to a peer_ni within
239          * a peer. There could be other peer_nis on the same net, which
240          * can be used to send to that gateway. However when we are
241          * consolidating gateways because of discovery, the nid used to
242          * add the route might've moved between gateway peers. In this
243          * case we want to move the route to the new gateway as well. The
244          * intent here is not to confuse the user who added the route.
245          */
246         list_for_each_entry(route, &orig_lp->lp_routes, lr_gwlist) {
247                 lpni = lnet_peer_get_ni_locked(orig_lp, route->lr_nid);
248                 if (!lpni) {
249                         lnet_net_lock(LNET_LOCK_EX);
250                         list_move(&route->lr_gwlist, &new_lp->lp_routes);
251                         lnet_net_unlock(LNET_LOCK_EX);
252                 }
253         }
254
255 }
256
257 void
258 lnet_router_discovery_complete(struct lnet_peer *lp)
259 {
260         struct lnet_peer_ni *lpni = NULL;
261
262         spin_lock(&lp->lp_lock);
263         lp->lp_state &= ~LNET_PEER_RTR_DISCOVERY;
264         spin_unlock(&lp->lp_lock);
265
266         /*
267          * Router discovery successful? All peer information would've been
268          * updated already. No need to do any more processing
269          */
270         if (!lp->lp_dc_error)
271                 return;
272         /*
273          * discovery failed? then we need to set the status of each lpni
274          * to DOWN. It will be updated the next time we discover the
275          * router. For router peer NIs not on local networks, we never send
276          * messages directly to them, so their health will always remain
277          * at maximum. We can only tell if they are up or down from the
278          * status returned in the PING response. If we fail to get that
279          * status in our scheduled router discovery, then we'll assume
280          * it's down until we're told otherwise.
281          */
282         CDEBUG(D_NET, "%s: Router discovery failed %d\n",
283                libcfs_nid2str(lp->lp_primary_nid), lp->lp_dc_error);
284         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
285                 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
286 }
287
288 static void
289 lnet_rtr_addref_locked(struct lnet_peer *lp)
290 {
291         LASSERT(lp->lp_rtr_refcount >= 0);
292
293         /* lnet_net_lock must be exclusively locked */
294         lp->lp_rtr_refcount++;
295         if (lp->lp_rtr_refcount == 1) {
296                 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
297                 /* addref for the_lnet.ln_routers */
298                 lnet_peer_addref_locked(lp);
299                 the_lnet.ln_routers_version++;
300         }
301 }
302
303 static void
304 lnet_rtr_decref_locked(struct lnet_peer *lp)
305 {
306         LASSERT(atomic_read(&lp->lp_refcount) > 0);
307         LASSERT(lp->lp_rtr_refcount > 0);
308
309         /* lnet_net_lock must be exclusively locked */
310         lp->lp_rtr_refcount--;
311         if (lp->lp_rtr_refcount == 0) {
312                 LASSERT(list_empty(&lp->lp_routes));
313
314                 list_del(&lp->lp_rtr_list);
315                 /* decref for the_lnet.ln_routers */
316                 lnet_peer_decref_locked(lp);
317                 the_lnet.ln_routers_version++;
318         }
319 }
320
321 struct lnet_remotenet *
322 lnet_find_rnet_locked(__u32 net)
323 {
324         struct lnet_remotenet *rnet;
325         struct list_head *tmp;
326         struct list_head *rn_list;
327
328         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
329
330         rn_list = lnet_net2rnethash(net);
331         list_for_each(tmp, rn_list) {
332                 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
333
334                 if (rnet->lrn_net == net)
335                         return rnet;
336         }
337         return NULL;
338 }
339
340 static void lnet_shuffle_seed(void)
341 {
342         static int seeded;
343         struct lnet_ni *ni = NULL;
344
345         if (seeded)
346                 return;
347
348         /* Nodes with small feet have little entropy
349          * the NID for this node gives the most entropy in the low bits */
350         while ((ni = lnet_get_next_ni_locked(NULL, ni)))
351                 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
352
353         seeded = 1;
354         return;
355 }
356
357 /* NB expects LNET_LOCK held */
358 static void
359 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
360 {
361         unsigned int len = 0;
362         unsigned int offset = 0;
363         struct list_head *e;
364
365         lnet_shuffle_seed();
366
367         list_for_each(e, &rnet->lrn_routes)
368                 len++;
369
370         /*
371          * Randomly adding routes to the list is done to ensure that when
372          * different nodes are using the same list of routers, they end up
373          * preferring different routers.
374          */
375         offset = cfs_rand() % (len + 1);
376         list_for_each(e, &rnet->lrn_routes) {
377                 if (offset == 0)
378                         break;
379                 offset--;
380         }
381         list_add(&route->lr_list, e);
382         /*
383          * force a router check on the gateway to make sure the route is
384          * alive
385          */
386         route->lr_gateway->lp_rtrcheck_timestamp = 0;
387
388         the_lnet.ln_remote_nets_version++;
389
390         /* add the route on the gateway list */
391         list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
392
393         /* take a router reference count on the gateway */
394         lnet_rtr_addref_locked(route->lr_gateway);
395 }
396
397 int
398 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
399                unsigned int priority)
400 {
401         struct list_head *route_entry;
402         struct lnet_remotenet *rnet;
403         struct lnet_remotenet *rnet2;
404         struct lnet_route *route;
405         struct lnet_peer_ni *lpni;
406         struct lnet_peer *gw;
407         int add_route;
408         int rc;
409
410         CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
411                libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
412
413         if (gateway == LNET_NID_ANY ||
414             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
415             net == LNET_NIDNET(LNET_NID_ANY) ||
416             LNET_NETTYP(net) == LOLND ||
417             LNET_NIDNET(gateway) == net ||
418             (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
419                 return -EINVAL;
420
421         /* it's a local network */
422         if (lnet_islocalnet(net))
423                 return -EEXIST;
424
425         /* Assume net, route, all new */
426         LIBCFS_ALLOC(route, sizeof(*route));
427         LIBCFS_ALLOC(rnet, sizeof(*rnet));
428         if (route == NULL || rnet == NULL) {
429                 CERROR("Out of memory creating route %s %d %s\n",
430                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
431                 if (route != NULL)
432                         LIBCFS_FREE(route, sizeof(*route));
433                 if (rnet != NULL)
434                         LIBCFS_FREE(rnet, sizeof(*rnet));
435                 return -ENOMEM;
436         }
437
438         INIT_LIST_HEAD(&rnet->lrn_routes);
439         rnet->lrn_net = net;
440         /* store the local and remote net that the route represents */
441         route->lr_lnet = LNET_NIDNET(gateway);
442         route->lr_net = net;
443         route->lr_nid = gateway;
444         route->lr_priority = priority;
445         route->lr_hops = hops;
446
447         lnet_net_lock(LNET_LOCK_EX);
448
449         /*
450          * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
451          * lose that once we're done
452          */
453         lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
454         if (IS_ERR(lpni)) {
455                 lnet_net_unlock(LNET_LOCK_EX);
456
457                 LIBCFS_FREE(route, sizeof(*route));
458                 LIBCFS_FREE(rnet, sizeof(*rnet));
459
460                 rc = PTR_ERR(lpni);
461                 CERROR("Error %d creating route %s %d %s\n", rc,
462                         libcfs_net2str(net), hops,
463                         libcfs_nid2str(gateway));
464                 return rc;
465         }
466
467         LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
468         gw = lpni->lpni_peer_net->lpn_peer;
469
470         route->lr_gateway = gw;
471
472         rnet2 = lnet_find_rnet_locked(net);
473         if (rnet2 == NULL) {
474                 /* new network */
475                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
476                 rnet2 = rnet;
477         }
478
479         /* Search for a duplicate route (it's a NOOP if it is) */
480         add_route = 1;
481         list_for_each(route_entry, &rnet2->lrn_routes) {
482                 struct lnet_route *route2;
483
484                 route2 = list_entry(route_entry, struct lnet_route, lr_list);
485                 if (route2->lr_gateway == route->lr_gateway) {
486                         add_route = 0;
487                         break;
488                 }
489
490                 /* our lookups must be true */
491                 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
492         }
493
494         /*
495          * It is possible to add multiple routes through the same peer,
496          * but it'll be using a different NID of that peer. When the
497          * gateway is discovered, discovery will consolidate the different
498          * peers into one peer. In this case the discovery code will have
499          * to move the routes from the peer that's being deleted to the
500          * consolidated peer lp_routes list
501          */
502         if (add_route)
503                 lnet_add_route_to_rnet(rnet2, route);
504
505         /*
506          * get rid of the reference on the lpni.
507          */
508         lnet_peer_ni_decref_locked(lpni);
509         lnet_net_unlock(LNET_LOCK_EX);
510
511         rc = 0;
512
513         if (!add_route) {
514                 rc = -EEXIST;
515                 LIBCFS_FREE(route, sizeof(*route));
516         }
517
518         if (rnet != rnet2)
519                 LIBCFS_FREE(rnet, sizeof(*rnet));
520
521         /* kick start the monitor thread to handle the added route */
522         wake_up(&the_lnet.ln_mt_waitq);
523
524         return rc;
525 }
526
527 static void
528 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
529                          struct list_head *zombies)
530 {
531         struct lnet_peer *gateway;
532         struct lnet_route *route;
533         struct lnet_route *tmp;
534
535         list_for_each_entry_safe(route, tmp, route_list, lr_list) {
536                 gateway = route->lr_gateway;
537                 if (gw_nid != LNET_NID_ANY &&
538                     gw_nid != gateway->lp_primary_nid)
539                         continue;
540
541                 /*
542                  * move to zombie to delete outside the lock
543                  * Note that this function is called with the
544                  * ln_api_mutex held as well as the exclusive net
545                  * lock. Adding to the remote net list happens
546                  * under the same conditions. Same goes for the
547                  * gateway router list
548                  */
549                 list_move(&route->lr_list, zombies);
550                 the_lnet.ln_remote_nets_version++;
551
552                 list_del(&route->lr_gwlist);
553                 lnet_rtr_decref_locked(gateway);
554         }
555 }
556
557 int
558 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
559 {
560         struct list_head rnet_zombies;
561         struct lnet_remotenet *rnet;
562         struct lnet_remotenet *tmp;
563         struct list_head *rn_list;
564         struct lnet_peer_ni *lpni;
565         struct lnet_route *route;
566         struct list_head zombies;
567         struct lnet_peer *lp;
568         int i = 0;
569
570         INIT_LIST_HEAD(&rnet_zombies);
571         INIT_LIST_HEAD(&zombies);
572
573         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
574                libcfs_net2str(net), libcfs_nid2str(gw_nid));
575
576         /* NB Caller may specify either all routes via the given gateway
577          * or a specific route entry actual NIDs) */
578
579         lnet_net_lock(LNET_LOCK_EX);
580
581         lpni = lnet_find_peer_ni_locked(gw_nid);
582         if (lpni) {
583                 lp = lpni->lpni_peer_net->lpn_peer;
584                 LASSERT(lp);
585                 gw_nid = lp->lp_primary_nid;
586                 lnet_peer_ni_decref_locked(lpni);
587         }
588
589         if (net != LNET_NIDNET(LNET_NID_ANY)) {
590                 rnet = lnet_find_rnet_locked(net);
591                 if (!rnet) {
592                         lnet_net_unlock(LNET_LOCK_EX);
593                         return -ENOENT;
594                 }
595                 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
596                                          &zombies);
597                 if (list_empty(&rnet->lrn_routes))
598                         list_move(&rnet->lrn_list, &rnet_zombies);
599                 goto delete_zombies;
600         }
601
602         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
603                 rn_list = &the_lnet.ln_remote_nets_hash[i];
604
605                 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
606                         lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
607                                                  &zombies);
608                         if (list_empty(&rnet->lrn_routes))
609                                 list_move(&rnet->lrn_list, &rnet_zombies);
610                 }
611         }
612
613 delete_zombies:
614         lnet_net_unlock(LNET_LOCK_EX);
615
616         while (!list_empty(&zombies)) {
617                 route = list_first_entry(&zombies, struct lnet_route, lr_list);
618                 list_del(&route->lr_list);
619                 LIBCFS_FREE(route, sizeof(*route));
620         }
621
622         while (!list_empty(&rnet_zombies)) {
623                 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
624                                         lrn_list);
625                 list_del(&rnet->lrn_list);
626                 LIBCFS_FREE(rnet, sizeof(*rnet));
627         }
628
629         return 0;
630 }
631
632 void
633 lnet_destroy_routes (void)
634 {
635         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
636 }
637
638 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
639 {
640         struct lnet_rtrbufpool *rbp;
641         int i, rc = -ENOENT, j;
642
643         if (the_lnet.ln_rtrpools == NULL)
644                 return rc;
645
646
647         cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
648                 if (i != cpt)
649                         continue;
650
651                 lnet_net_lock(i);
652                 for (j = 0; j < LNET_NRBPOOLS; j++) {
653                         pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
654                         pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
655                         pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
656                         pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
657                 }
658                 lnet_net_unlock(i);
659                 rc = 0;
660                 break;
661         }
662
663         lnet_net_lock(LNET_LOCK_EX);
664         pool_cfg->pl_routing = the_lnet.ln_routing;
665         lnet_net_unlock(LNET_LOCK_EX);
666
667         return rc;
668 }
669
670 int
671 lnet_get_route(int idx, __u32 *net, __u32 *hops,
672                lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
673 {
674         struct list_head *e1;
675         struct list_head *e2;
676         struct lnet_remotenet *rnet;
677         struct lnet_route        *route;
678         int               cpt;
679         int               i;
680         struct list_head *rn_list;
681
682         cpt = lnet_net_lock_current();
683
684         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
685                 rn_list = &the_lnet.ln_remote_nets_hash[i];
686                 list_for_each(e1, rn_list) {
687                         rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
688
689                         list_for_each(e2, &rnet->lrn_routes) {
690                                 route = list_entry(e2, struct lnet_route,
691                                                    lr_list);
692
693                                 if (idx-- == 0) {
694                                         *net      = rnet->lrn_net;
695                                         *gateway  = route->lr_nid;
696                                         *hops     = route->lr_hops;
697                                         *priority = route->lr_priority;
698                                         *alive    = lnet_is_route_alive(route);
699                                         lnet_net_unlock(cpt);
700                                         return 0;
701                                 }
702                         }
703                 }
704         }
705
706         lnet_net_unlock(cpt);
707         return -ENOENT;
708 }
709
710 static void
711 lnet_wait_known_routerstate(void)
712 {
713         struct lnet_peer *rtr;
714         struct list_head *entry;
715         int all_known;
716
717         LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
718
719         for (;;) {
720                 int cpt = lnet_net_lock_current();
721
722                 all_known = 1;
723                 list_for_each(entry, &the_lnet.ln_routers) {
724                         rtr = list_entry(entry, struct lnet_peer,
725                                          lp_rtr_list);
726
727                         spin_lock(&rtr->lp_lock);
728
729                         if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
730                                 all_known = 0;
731                                 spin_unlock(&rtr->lp_lock);
732                                 break;
733                         }
734                         spin_unlock(&rtr->lp_lock);
735                 }
736
737                 lnet_net_unlock(cpt);
738
739                 if (all_known)
740                         return;
741
742                 set_current_state(TASK_UNINTERRUPTIBLE);
743                 schedule_timeout(cfs_time_seconds(1));
744         }
745 }
746
747 static void
748 lnet_update_ni_status_locked(void)
749 {
750         struct lnet_ni *ni = NULL;
751         time64_t now;
752         time64_t timeout;
753
754         LASSERT(the_lnet.ln_routing);
755
756         timeout = router_ping_timeout + alive_router_check_interval;
757
758         now = ktime_get_real_seconds();
759         while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
760                 if (ni->ni_net->net_lnd->lnd_type == LOLND)
761                         continue;
762
763                 if (now < ni->ni_last_alive + timeout)
764                         continue;
765
766                 lnet_ni_lock(ni);
767                 /* re-check with lock */
768                 if (now < ni->ni_last_alive + timeout) {
769                         lnet_ni_unlock(ni);
770                         continue;
771                 }
772
773                 LASSERT(ni->ni_status != NULL);
774
775                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
776                         CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
777                                libcfs_nid2str(ni->ni_nid), timeout);
778                         /* NB: so far, this is the only place to set
779                          * NI status to "down" */
780                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
781                 }
782                 lnet_ni_unlock(ni);
783         }
784 }
785
786 void lnet_wait_router_start(void)
787 {
788         if (check_routers_before_use) {
789                 /* Note that a helpful side-effect of pinging all known routers
790                  * at startup is that it makes them drop stale connections they
791                  * may have to a previous instance of me. */
792                 lnet_wait_known_routerstate();
793         }
794 }
795
796 /*
797  * This function is called from the monitor thread to check if there are
798  * any active routers that need to be checked.
799  */
800 inline bool
801 lnet_router_checker_active(void)
802 {
803         /* Router Checker thread needs to run when routing is enabled in
804          * order to call lnet_update_ni_status_locked() */
805         if (the_lnet.ln_routing)
806                 return true;
807
808         return !list_empty(&the_lnet.ln_routers) &&
809                 alive_router_check_interval > 0;
810 }
811
812 void
813 lnet_check_routers(void)
814 {
815         struct lnet_peer_ni *lpni;
816         struct list_head *entry;
817         struct lnet_peer *rtr;
818         __u64 version;
819         time64_t now;
820         int cpt;
821         int rc;
822
823         cpt = lnet_net_lock_current();
824 rescan:
825         version = the_lnet.ln_routers_version;
826
827         list_for_each(entry, &the_lnet.ln_routers) {
828                 rtr = list_entry(entry, struct lnet_peer,
829                                  lp_rtr_list);
830
831                 now = ktime_get_real_seconds();
832
833                 /*
834                  * only discover the router if we've passed
835                  * alive_router_check_interval seconds. Some of the router
836                  * interfaces could be down and in that case they would be
837                  * undergoing recovery separately from this discovery.
838                  */
839                 if (now - rtr->lp_rtrcheck_timestamp <
840                     alive_router_check_interval)
841                        continue;
842
843                 /*
844                  * If we're currently discovering the peer then don't
845                  * issue another discovery
846                  */
847                 spin_lock(&rtr->lp_lock);
848                 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY) {
849                         spin_unlock(&rtr->lp_lock);
850                         continue;
851                 }
852                 /* make sure we actively discover the router */
853                 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
854                 rtr->lp_state |= LNET_PEER_RTR_DISCOVERY;
855                 spin_unlock(&rtr->lp_lock);
856
857                 /* find the peer_ni associated with the primary NID */
858                 lpni = lnet_peer_get_ni_locked(rtr, rtr->lp_primary_nid);
859                 if (!lpni) {
860                         CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
861                                libcfs_nid2str(rtr->lp_primary_nid));
862                         continue;
863                 }
864                 lnet_peer_ni_addref_locked(lpni);
865
866                 /* discover the router */
867                 CDEBUG(D_NET, "discover %s, cpt = %d\n",
868                        libcfs_nid2str(lpni->lpni_nid), cpt);
869                 rc = lnet_discover_peer_locked(lpni, cpt, false);
870
871                 /* decrement ref count acquired by find_peer_ni_locked() */
872                 lnet_peer_ni_decref_locked(lpni);
873
874                 if (!rc)
875                         rtr->lp_rtrcheck_timestamp = now;
876                 else
877                         CERROR("Failed to discover router %s\n",
878                                libcfs_nid2str(rtr->lp_primary_nid));
879
880                 /* NB dropped lock */
881                 if (version != the_lnet.ln_routers_version) {
882                         /* the routers list has changed */
883                         goto rescan;
884                 }
885         }
886
887         if (the_lnet.ln_routing)
888                 lnet_update_ni_status_locked();
889
890         lnet_net_unlock(cpt);
891 }
892
893 void
894 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
895 {
896         int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
897
898         while (--npages >= 0)
899                 __free_page(rb->rb_kiov[npages].kiov_page);
900
901         LIBCFS_FREE(rb, sz);
902 }
903
904 static struct lnet_rtrbuf *
905 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
906 {
907         int            npages = rbp->rbp_npages;
908         int            sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
909         struct page   *page;
910         struct lnet_rtrbuf *rb;
911         int            i;
912
913         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
914         if (rb == NULL)
915                 return NULL;
916
917         rb->rb_pool = rbp;
918
919         for (i = 0; i < npages; i++) {
920                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
921                                           GFP_KERNEL | __GFP_ZERO);
922                 if (page == NULL) {
923                         while (--i >= 0)
924                                 __free_page(rb->rb_kiov[i].kiov_page);
925
926                         LIBCFS_FREE(rb, sz);
927                         return NULL;
928                 }
929
930                 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
931                 rb->rb_kiov[i].kiov_offset = 0;
932                 rb->rb_kiov[i].kiov_page = page;
933         }
934
935         return rb;
936 }
937
938 static void
939 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
940 {
941         int npages = rbp->rbp_npages;
942         struct lnet_rtrbuf *rb;
943         struct list_head tmp;
944
945         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
946                 return;
947
948         INIT_LIST_HEAD(&tmp);
949
950         lnet_net_lock(cpt);
951         list_splice_init(&rbp->rbp_msgs, &tmp);
952         lnet_drop_routed_msgs_locked(&tmp, cpt);
953         list_splice_init(&rbp->rbp_bufs, &tmp);
954         rbp->rbp_req_nbuffers = 0;
955         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
956         rbp->rbp_mincredits = 0;
957         lnet_net_unlock(cpt);
958
959         /* Free buffers on the free list. */
960         while (!list_empty(&tmp)) {
961                 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
962                 list_del(&rb->rb_list);
963                 lnet_destroy_rtrbuf(rb, npages);
964         }
965 }
966
967 static int
968 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
969 {
970         struct list_head rb_list;
971         struct lnet_rtrbuf *rb;
972         int             num_rb;
973         int             num_buffers = 0;
974         int             old_req_nbufs;
975         int             npages = rbp->rbp_npages;
976
977         lnet_net_lock(cpt);
978         /* If we are called for less buffers than already in the pool, we
979          * just lower the req_nbuffers number and excess buffers will be
980          * thrown away as they are returned to the free list.  Credits
981          * then get adjusted as well.
982          * If we already have enough buffers allocated to serve the
983          * increase requested, then we can treat that the same way as we
984          * do the decrease. */
985         num_rb = nbufs - rbp->rbp_nbuffers;
986         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
987                 rbp->rbp_req_nbuffers = nbufs;
988                 lnet_net_unlock(cpt);
989                 return 0;
990         }
991         /* store the older value of rbp_req_nbuffers and then set it to
992          * the new request to prevent lnet_return_rx_credits_locked() from
993          * freeing buffers that we need to keep around */
994         old_req_nbufs = rbp->rbp_req_nbuffers;
995         rbp->rbp_req_nbuffers = nbufs;
996         lnet_net_unlock(cpt);
997
998         INIT_LIST_HEAD(&rb_list);
999
1000         /* allocate the buffers on a local list first.  If all buffers are
1001          * allocated successfully then join this list to the rbp buffer
1002          * list.  If not then free all allocated buffers. */
1003         while (num_rb-- > 0) {
1004                 rb = lnet_new_rtrbuf(rbp, cpt);
1005                 if (rb == NULL) {
1006                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1007                                nbufs, npages);
1008
1009                         lnet_net_lock(cpt);
1010                         rbp->rbp_req_nbuffers = old_req_nbufs;
1011                         lnet_net_unlock(cpt);
1012
1013                         goto failed;
1014                 }
1015
1016                 list_add(&rb->rb_list, &rb_list);
1017                 num_buffers++;
1018         }
1019
1020         lnet_net_lock(cpt);
1021
1022         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1023         rbp->rbp_nbuffers += num_buffers;
1024         rbp->rbp_credits += num_buffers;
1025         rbp->rbp_mincredits = rbp->rbp_credits;
1026         /* We need to schedule blocked msg using the newly
1027          * added buffers. */
1028         while (!list_empty(&rbp->rbp_bufs) &&
1029                !list_empty(&rbp->rbp_msgs))
1030                 lnet_schedule_blocked_locked(rbp);
1031
1032         lnet_net_unlock(cpt);
1033
1034         return 0;
1035
1036 failed:
1037         while (!list_empty(&rb_list)) {
1038                 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1039                 list_del(&rb->rb_list);
1040                 lnet_destroy_rtrbuf(rb, npages);
1041         }
1042
1043         return -ENOMEM;
1044 }
1045
1046 static void
1047 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1048 {
1049         INIT_LIST_HEAD(&rbp->rbp_msgs);
1050         INIT_LIST_HEAD(&rbp->rbp_bufs);
1051
1052         rbp->rbp_npages = npages;
1053         rbp->rbp_credits = 0;
1054         rbp->rbp_mincredits = 0;
1055 }
1056
1057 void
1058 lnet_rtrpools_free(int keep_pools)
1059 {
1060         struct lnet_rtrbufpool *rtrp;
1061         int               i;
1062
1063         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1064                 return;
1065
1066         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1067                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1068                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1069                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1070         }
1071
1072         if (!keep_pools) {
1073                 cfs_percpt_free(the_lnet.ln_rtrpools);
1074                 the_lnet.ln_rtrpools = NULL;
1075         }
1076 }
1077
1078 static int
1079 lnet_nrb_tiny_calculate(void)
1080 {
1081         int     nrbs = LNET_NRB_TINY;
1082
1083         if (tiny_router_buffers < 0) {
1084                 LCONSOLE_ERROR_MSG(0x10c,
1085                                    "tiny_router_buffers=%d invalid when "
1086                                    "routing enabled\n", tiny_router_buffers);
1087                 return -EINVAL;
1088         }
1089
1090         if (tiny_router_buffers > 0)
1091                 nrbs = tiny_router_buffers;
1092
1093         nrbs /= LNET_CPT_NUMBER;
1094         return max(nrbs, LNET_NRB_TINY_MIN);
1095 }
1096
1097 static int
1098 lnet_nrb_small_calculate(void)
1099 {
1100         int     nrbs = LNET_NRB_SMALL;
1101
1102         if (small_router_buffers < 0) {
1103                 LCONSOLE_ERROR_MSG(0x10c,
1104                                    "small_router_buffers=%d invalid when "
1105                                    "routing enabled\n", small_router_buffers);
1106                 return -EINVAL;
1107         }
1108
1109         if (small_router_buffers > 0)
1110                 nrbs = small_router_buffers;
1111
1112         nrbs /= LNET_CPT_NUMBER;
1113         return max(nrbs, LNET_NRB_SMALL_MIN);
1114 }
1115
1116 static int
1117 lnet_nrb_large_calculate(void)
1118 {
1119         int     nrbs = LNET_NRB_LARGE;
1120
1121         if (large_router_buffers < 0) {
1122                 LCONSOLE_ERROR_MSG(0x10c,
1123                                    "large_router_buffers=%d invalid when "
1124                                    "routing enabled\n", large_router_buffers);
1125                 return -EINVAL;
1126         }
1127
1128         if (large_router_buffers > 0)
1129                 nrbs = large_router_buffers;
1130
1131         nrbs /= LNET_CPT_NUMBER;
1132         return max(nrbs, LNET_NRB_LARGE_MIN);
1133 }
1134
1135 int
1136 lnet_rtrpools_alloc(int im_a_router)
1137 {
1138         struct lnet_rtrbufpool *rtrp;
1139         int     nrb_tiny;
1140         int     nrb_small;
1141         int     nrb_large;
1142         int     rc;
1143         int     i;
1144
1145         if (!strcmp(forwarding, "")) {
1146                 /* not set either way */
1147                 if (!im_a_router)
1148                         return 0;
1149         } else if (!strcmp(forwarding, "disabled")) {
1150                 /* explicitly disabled */
1151                 return 0;
1152         } else if (!strcmp(forwarding, "enabled")) {
1153                 /* explicitly enabled */
1154         } else {
1155                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1156                                    "'enabled' or 'disabled'\n");
1157                 return -EINVAL;
1158         }
1159
1160         nrb_tiny = lnet_nrb_tiny_calculate();
1161         if (nrb_tiny < 0)
1162                 return -EINVAL;
1163
1164         nrb_small = lnet_nrb_small_calculate();
1165         if (nrb_small < 0)
1166                 return -EINVAL;
1167
1168         nrb_large = lnet_nrb_large_calculate();
1169         if (nrb_large < 0)
1170                 return -EINVAL;
1171
1172         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1173                                                 LNET_NRBPOOLS *
1174                                                 sizeof(struct lnet_rtrbufpool));
1175         if (the_lnet.ln_rtrpools == NULL) {
1176                 LCONSOLE_ERROR_MSG(0x10c,
1177                                    "Failed to initialize router buffe pool\n");
1178                 return -ENOMEM;
1179         }
1180
1181         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1182                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1183                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1184                                               nrb_tiny, i);
1185                 if (rc != 0)
1186                         goto failed;
1187
1188                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1189                                   LNET_NRB_SMALL_PAGES);
1190                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1191                                               nrb_small, i);
1192                 if (rc != 0)
1193                         goto failed;
1194
1195                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1196                                   LNET_NRB_LARGE_PAGES);
1197                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1198                                               nrb_large, i);
1199                 if (rc != 0)
1200                         goto failed;
1201         }
1202
1203         lnet_net_lock(LNET_LOCK_EX);
1204         the_lnet.ln_routing = 1;
1205         lnet_net_unlock(LNET_LOCK_EX);
1206         wake_up(&the_lnet.ln_mt_waitq);
1207         return 0;
1208
1209  failed:
1210         lnet_rtrpools_free(0);
1211         return rc;
1212 }
1213
1214 static int
1215 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1216 {
1217         int nrb = 0;
1218         int rc = 0;
1219         int i;
1220         struct lnet_rtrbufpool *rtrp;
1221
1222         /* If the provided values for each buffer pool are different than the
1223          * configured values, we need to take action. */
1224         if (tiny >= 0) {
1225                 tiny_router_buffers = tiny;
1226                 nrb = lnet_nrb_tiny_calculate();
1227                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1228                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1229                                                       nrb, i);
1230                         if (rc != 0)
1231                                 return rc;
1232                 }
1233         }
1234         if (small >= 0) {
1235                 small_router_buffers = small;
1236                 nrb = lnet_nrb_small_calculate();
1237                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1238                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1239                                                       nrb, i);
1240                         if (rc != 0)
1241                                 return rc;
1242                 }
1243         }
1244         if (large >= 0) {
1245                 large_router_buffers = large;
1246                 nrb = lnet_nrb_large_calculate();
1247                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1248                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1249                                                       nrb, i);
1250                         if (rc != 0)
1251                                 return rc;
1252                 }
1253         }
1254
1255         return 0;
1256 }
1257
1258 int
1259 lnet_rtrpools_adjust(int tiny, int small, int large)
1260 {
1261         /* this function doesn't revert the changes if adding new buffers
1262          * failed.  It's up to the user space caller to revert the
1263          * changes. */
1264
1265         if (!the_lnet.ln_routing)
1266                 return 0;
1267
1268         return lnet_rtrpools_adjust_helper(tiny, small, large);
1269 }
1270
1271 int
1272 lnet_rtrpools_enable(void)
1273 {
1274         int rc = 0;
1275
1276         if (the_lnet.ln_routing)
1277                 return 0;
1278
1279         if (the_lnet.ln_rtrpools == NULL)
1280                 /* If routing is turned off, and we have never
1281                  * initialized the pools before, just call the
1282                  * standard buffer pool allocation routine as
1283                  * if we are just configuring this for the first
1284                  * time. */
1285                 rc = lnet_rtrpools_alloc(1);
1286         else
1287                 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1288         if (rc != 0)
1289                 return rc;
1290
1291         lnet_net_lock(LNET_LOCK_EX);
1292         the_lnet.ln_routing = 1;
1293
1294         the_lnet.ln_ping_target->pb_info.pi_features &=
1295                 ~LNET_PING_FEAT_RTE_DISABLED;
1296         lnet_net_unlock(LNET_LOCK_EX);
1297
1298         return rc;
1299 }
1300
1301 void
1302 lnet_rtrpools_disable(void)
1303 {
1304         if (!the_lnet.ln_routing)
1305                 return;
1306
1307         lnet_net_lock(LNET_LOCK_EX);
1308         the_lnet.ln_routing = 0;
1309         the_lnet.ln_ping_target->pb_info.pi_features |=
1310                 LNET_PING_FEAT_RTE_DISABLED;
1311
1312         tiny_router_buffers = 0;
1313         small_router_buffers = 0;
1314         large_router_buffers = 0;
1315         lnet_net_unlock(LNET_LOCK_EX);
1316         lnet_rtrpools_free(1);
1317 }
1318
1319 static inline void
1320 lnet_notify_peer_down(struct lnet_ni *ni, lnet_nid_t nid)
1321 {
1322         if (ni->ni_net->net_lnd->lnd_notify_peer_down != NULL)
1323                 (ni->ni_net->net_lnd->lnd_notify_peer_down)(nid);
1324 }
1325
1326 /*
1327  * ni: local NI used to communicate with the peer
1328  * nid: peer NID
1329  * alive: true if peer is alive, false otherwise
1330  * reset: reset health value. This is requested by the LND.
1331  * when: notificaiton time.
1332  */
1333 int
1334 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, bool alive, bool reset,
1335             time64_t when)
1336 {
1337         struct lnet_peer_ni *lpni = NULL;
1338         time64_t now = ktime_get_seconds();
1339         int cpt;
1340
1341         LASSERT (!in_interrupt ());
1342
1343         CDEBUG (D_NET, "%s notifying %s: %s\n",
1344                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1345                 libcfs_nid2str(nid),
1346                 alive ? "up" : "down");
1347
1348         if (ni != NULL &&
1349             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1350                 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1351                       libcfs_nid2str(nid), alive ? "birth" : "death",
1352                       libcfs_nid2str(ni->ni_nid));
1353                 return -EINVAL;
1354         }
1355
1356         /* can't do predictions... */
1357         if (when > now) {
1358                 CWARN("Ignoring prediction from %s of %s %s "
1359                       "%lld seconds in the future\n",
1360                       (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1361                       libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1362                 return -EINVAL;
1363         }
1364
1365         if (ni != NULL && !alive &&             /* LND telling me she's down */
1366             !auto_down) {                       /* auto-down disabled */
1367                 CDEBUG(D_NET, "Auto-down disabled\n");
1368                 return 0;
1369         }
1370
1371         /* must lock 0 since this is used for synchronization */
1372         lnet_net_lock(0);
1373
1374         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1375                 lnet_net_unlock(0);
1376                 return -ESHUTDOWN;
1377         }
1378
1379         lpni = lnet_find_peer_ni_locked(nid);
1380         if (lpni == NULL) {
1381                 /* nid not found */
1382                 lnet_net_unlock(0);
1383                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1384                 return 0;
1385         }
1386
1387         if (alive) {
1388                 if (reset)
1389                         lnet_set_healthv(&lpni->lpni_healthv,
1390                                          LNET_MAX_HEALTH_VALUE);
1391                 else
1392                         lnet_inc_healthv(&lpni->lpni_healthv);
1393         } else {
1394                 lnet_handle_remote_failure_locked(lpni);
1395         }
1396
1397         /* recalculate aliveness */
1398         alive = lnet_is_peer_ni_alive(lpni);
1399         lnet_net_unlock(0);
1400
1401         if (ni != NULL && !alive)
1402                 lnet_notify_peer_down(ni, lpni->lpni_nid);
1403
1404         cpt = lpni->lpni_cpt;
1405         lnet_net_lock(cpt);
1406         lnet_peer_ni_decref_locked(lpni);
1407         lnet_net_unlock(cpt);
1408
1409         return 0;
1410 }
1411 EXPORT_SYMBOL(lnet_notify);