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