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