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