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