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