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