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