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