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