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