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