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