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