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