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