Whamcloud - gitweb
LU-10391 lnet: discard some peer_ni lookup functions
[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 *tmp;
586         struct list_head *rn_list;
587
588         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
589
590         rn_list = lnet_net2rnethash(net);
591         list_for_each(tmp, rn_list) {
592                 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
593
594                 if (rnet->lrn_net == net)
595                         return rnet;
596         }
597         return NULL;
598 }
599
600 static void lnet_shuffle_seed(void)
601 {
602         static int seeded;
603         struct lnet_ni *ni = NULL;
604
605         if (seeded)
606                 return;
607
608         /* Nodes with small feet have little entropy
609          * the NID for this node gives the most entropy in the low bits */
610         while ((ni = lnet_get_next_ni_locked(NULL, ni)))
611                 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
612
613         seeded = 1;
614 }
615
616 /* NB expects LNET_LOCK held */
617 static void
618 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
619 {
620         struct lnet_peer_net *lpn;
621         unsigned int offset = 0;
622         unsigned int len = 0;
623         struct list_head *e;
624         time64_t now;
625
626         lnet_shuffle_seed();
627
628         list_for_each(e, &rnet->lrn_routes)
629                 len++;
630
631         /*
632          * Randomly adding routes to the list is done to ensure that when
633          * different nodes are using the same list of routers, they end up
634          * preferring different routers.
635          */
636         offset = prandom_u32_max(len + 1);
637         list_for_each(e, &rnet->lrn_routes) {
638                 if (offset == 0)
639                         break;
640                 offset--;
641         }
642         list_add(&route->lr_list, e);
643         /*
644          * force a router check on the gateway to make sure the route is
645          * alive
646          */
647         now = ktime_get_real_seconds();
648         list_for_each_entry(lpn, &route->lr_gateway->lp_peer_nets,
649                             lpn_peer_nets) {
650                 lpn->lpn_next_ping = now;
651         }
652
653         the_lnet.ln_remote_nets_version++;
654
655         /* add the route on the gateway list */
656         list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
657
658         /* take a router reference count on the gateway */
659         lnet_rtr_addref_locked(route->lr_gateway);
660 }
661
662 int
663 lnet_add_route(__u32 net, __u32 hops, struct lnet_nid *gateway,
664                __u32 priority, __u32 sensitivity)
665 {
666         struct list_head *route_entry;
667         struct lnet_remotenet *rnet;
668         struct lnet_remotenet *rnet2;
669         struct lnet_route *route;
670         struct lnet_peer_ni *lpni;
671         struct lnet_peer *gw;
672         int add_route;
673         int rc;
674
675         CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
676                libcfs_net2str(net), hops, priority, libcfs_nidstr(gateway));
677
678         if (LNET_NID_IS_ANY(gateway) ||
679             nid_is_lo0(gateway) ||
680             net == LNET_NET_ANY ||
681             LNET_NETTYP(net) == LOLND ||
682             LNET_NID_NET(gateway) == net ||
683             (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
684                 return -EINVAL;
685
686         /* it's a local network */
687         if (lnet_islocalnet(net))
688                 return -EEXIST;
689
690         if (!lnet_islocalnet(LNET_NID_NET(gateway))) {
691                 CERROR("Cannot add route with gateway %s. There is no local interface configured on LNet %s\n",
692                        libcfs_nidstr(gateway),
693                        libcfs_net2str(LNET_NID_NET(gateway)));
694                 return -EHOSTUNREACH;
695         }
696
697         /* Assume net, route, all new */
698         LIBCFS_ALLOC(route, sizeof(*route));
699         LIBCFS_ALLOC(rnet, sizeof(*rnet));
700         if (route == NULL || rnet == NULL) {
701                 CERROR("Out of memory creating route %s %d %s\n",
702                        libcfs_net2str(net), hops, libcfs_nidstr(gateway));
703                 if (route != NULL)
704                         LIBCFS_FREE(route, sizeof(*route));
705                 if (rnet != NULL)
706                         LIBCFS_FREE(rnet, sizeof(*rnet));
707                 return -ENOMEM;
708         }
709
710         INIT_LIST_HEAD(&rnet->lrn_routes);
711         rnet->lrn_net = net;
712         /* store the local and remote net that the route represents */
713         route->lr_lnet = LNET_NID_NET(gateway);
714         route->lr_net = net;
715         route->lr_nid = *gateway;
716         route->lr_priority = priority;
717         route->lr_hops = hops;
718         if (lnet_peers_start_down())
719                 atomic_set(&route->lr_alive, 0);
720         else
721                 atomic_set(&route->lr_alive, 1);
722
723         lnet_net_lock(LNET_LOCK_EX);
724
725         /*
726          * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
727          * lose that once we're done
728          */
729         lpni = lnet_nid2peerni_ex(gateway);
730         if (IS_ERR(lpni)) {
731                 lnet_net_unlock(LNET_LOCK_EX);
732
733                 LIBCFS_FREE(route, sizeof(*route));
734                 LIBCFS_FREE(rnet, sizeof(*rnet));
735
736                 rc = PTR_ERR(lpni);
737                 CERROR("Error %d creating route %s %d %s\n", rc,
738                         libcfs_net2str(net), hops,
739                         libcfs_nidstr(gateway));
740                 return rc;
741         }
742
743         LASSERT(lpni);
744         LASSERT(lpni->lpni_peer_net);
745         LASSERT(lpni->lpni_peer_net->lpn_peer);
746         gw = lpni->lpni_peer_net->lpn_peer;
747
748         route->lr_gateway = gw;
749
750         rnet2 = lnet_find_rnet_locked(net);
751         if (rnet2 == NULL) {
752                 /* new network */
753                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
754                 rnet2 = rnet;
755         }
756
757         /* Search for a duplicate route (it's a NOOP if it is) */
758         add_route = 1;
759         list_for_each(route_entry, &rnet2->lrn_routes) {
760                 struct lnet_route *route2;
761
762                 route2 = list_entry(route_entry, struct lnet_route, lr_list);
763                 if (route2->lr_gateway == route->lr_gateway) {
764                         add_route = 0;
765                         break;
766                 }
767
768                 /* our lookups must be true */
769                 LASSERT(!nid_same(&route2->lr_gateway->lp_primary_nid,
770                                   gateway));
771         }
772
773         /*
774          * It is possible to add multiple routes through the same peer,
775          * but it'll be using a different NID of that peer. When the
776          * gateway is discovered, discovery will consolidate the different
777          * peers into one peer. In this case the discovery code will have
778          * to move the routes from the peer that's being deleted to the
779          * consolidated peer lp_routes list
780          */
781         if (add_route) {
782                 gw->lp_health_sensitivity = sensitivity;
783                 lnet_add_route_to_rnet(rnet2, route);
784                 if (lnet_peer_discovery_disabled)
785                         CWARN("Consider turning discovery on to enable full Multi-Rail routing functionality\n");
786         }
787
788         /*
789          * get rid of the reference on the lpni.
790          */
791         lnet_peer_ni_decref_locked(lpni);
792         lnet_net_unlock(LNET_LOCK_EX);
793
794         /* If avoid_asym_router_failure is enabled and hop count is not
795          * set to 1 for a route that is actually single-hop, then the
796          * feature will fail to prevent the router from being selected
797          * if it is missing a NI on the remote network due to misconfiguration.
798          */
799         if (avoid_asym_router_failure && hops == LNET_UNDEFINED_HOPS)
800                 CWARN("Use hops = 1 for a single-hop route when avoid_asym_router_failure feature is enabled\n");
801
802         rc = 0;
803
804         if (!add_route) {
805                 rc = -EEXIST;
806                 LIBCFS_FREE(route, sizeof(*route));
807         }
808
809         if (rnet != rnet2)
810                 LIBCFS_FREE(rnet, sizeof(*rnet));
811
812         /* kick start the monitor thread to handle the added route */
813         complete(&the_lnet.ln_mt_wait_complete);
814
815         return rc;
816 }
817
818 void
819 lnet_del_route_from_rnet(struct lnet_nid *gw_nid,
820                          struct list_head *route_list,
821                          struct list_head *zombies)
822 {
823         struct lnet_peer *gateway;
824         struct lnet_route *route;
825         struct lnet_route *tmp;
826
827         list_for_each_entry_safe(route, tmp, route_list, lr_list) {
828                 gateway = route->lr_gateway;
829                 if (gw_nid && !nid_same(gw_nid, &gateway->lp_primary_nid))
830                         continue;
831
832                 /*
833                  * move to zombie to delete outside the lock
834                  * Note that this function is called with the
835                  * ln_api_mutex held as well as the exclusive net
836                  * lock. Adding to the remote net list happens
837                  * under the same conditions. Same goes for the
838                  * gateway router list
839                  */
840                 list_move(&route->lr_list, zombies);
841                 the_lnet.ln_remote_nets_version++;
842
843                 list_del(&route->lr_gwlist);
844                 lnet_rtr_decref_locked(gateway);
845         }
846 }
847
848 int
849 lnet_del_route(__u32 net, struct lnet_nid *gw)
850 {
851         LIST_HEAD(rnet_zombies);
852         struct lnet_remotenet *rnet;
853         struct lnet_remotenet *tmp;
854         struct list_head *rn_list;
855         struct lnet_peer_ni *lpni;
856         struct lnet_route *route;
857         struct lnet_nid gw_nid;
858         LIST_HEAD(zombies);
859         struct lnet_peer *lp = NULL;
860         int i = 0;
861
862         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
863                libcfs_net2str(net), libcfs_nidstr(gw));
864
865         /* NB Caller may specify either all routes via the given gateway
866          * or a specific route entry actual NIDs) */
867
868         lnet_net_lock(LNET_LOCK_EX);
869
870         if (gw)
871                 lpni = lnet_peer_ni_find_locked(gw);
872         else
873                 lpni = NULL;
874         if (lpni) {
875                 lp = lpni->lpni_peer_net->lpn_peer;
876                 LASSERT(lp);
877                 gw_nid = lp->lp_primary_nid;
878                 gw = &gw_nid;
879                 lnet_peer_ni_decref_locked(lpni);
880         }
881
882         if (net != LNET_NET_ANY) {
883                 rnet = lnet_find_rnet_locked(net);
884                 if (!rnet) {
885                         lnet_net_unlock(LNET_LOCK_EX);
886                         return -ENOENT;
887                 }
888                 lnet_del_route_from_rnet(gw, &rnet->lrn_routes,
889                                          &zombies);
890                 if (list_empty(&rnet->lrn_routes))
891                         list_move(&rnet->lrn_list, &rnet_zombies);
892                 goto delete_zombies;
893         }
894
895         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
896                 rn_list = &the_lnet.ln_remote_nets_hash[i];
897
898                 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
899                         lnet_del_route_from_rnet(gw, &rnet->lrn_routes,
900                                                  &zombies);
901                         if (list_empty(&rnet->lrn_routes))
902                                 list_move(&rnet->lrn_list, &rnet_zombies);
903                 }
904         }
905
906 delete_zombies:
907         /*
908          * check if there are any routes remaining on the gateway
909          * If there are no more routes make sure to set the peer's
910          * lp_disc_net_id to 0 (invalid), in case we add more routes in
911          * the future on that gateway, then we start our discovery process
912          * from scratch
913          */
914         if (lpni) {
915                 if (list_empty(&lp->lp_routes))
916                         lp->lp_disc_net_id = 0;
917         }
918
919         lnet_net_unlock(LNET_LOCK_EX);
920
921         while (!list_empty(&zombies)) {
922                 route = list_first_entry(&zombies, struct lnet_route, lr_list);
923                 list_del(&route->lr_list);
924                 LIBCFS_FREE(route, sizeof(*route));
925         }
926
927         while (!list_empty(&rnet_zombies)) {
928                 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
929                                         lrn_list);
930                 list_del(&rnet->lrn_list);
931                 LIBCFS_FREE(rnet, sizeof(*rnet));
932         }
933
934         return 0;
935 }
936
937 void
938 lnet_destroy_routes(void)
939 {
940         lnet_del_route(LNET_NET_ANY, NULL);
941 }
942
943 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
944 {
945         struct lnet_rtrbufpool *rbp;
946         int i, rc = -ENOENT, j;
947
948         if (the_lnet.ln_rtrpools == NULL)
949                 return rc;
950
951
952         cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
953                 if (i != cpt)
954                         continue;
955
956                 lnet_net_lock(i);
957                 for (j = 0; j < LNET_NRBPOOLS; j++) {
958                         pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
959                         pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
960                         pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
961                         pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
962                 }
963                 lnet_net_unlock(i);
964                 rc = 0;
965                 break;
966         }
967
968         lnet_net_lock(LNET_LOCK_EX);
969         pool_cfg->pl_routing = the_lnet.ln_routing;
970         lnet_net_unlock(LNET_LOCK_EX);
971
972         return rc;
973 }
974
975 int
976 lnet_get_route(int idx, __u32 *net, __u32 *hops, lnet_nid_t *gateway,
977                __u32 *flags, __u32 *priority, __u32 *sensitivity)
978 {
979         struct lnet_remotenet *rnet;
980         struct list_head *rn_list;
981         struct lnet_route *route;
982         struct list_head *e1;
983         struct list_head *e2;
984         int cpt;
985         int i;
986
987         cpt = lnet_net_lock_current();
988
989         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
990                 rn_list = &the_lnet.ln_remote_nets_hash[i];
991                 list_for_each(e1, rn_list) {
992                         rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
993
994                         list_for_each(e2, &rnet->lrn_routes) {
995                                 route = list_entry(e2, struct lnet_route,
996                                                    lr_list);
997
998                                 if (idx-- == 0) {
999                                         *net      = rnet->lrn_net;
1000                                         *gateway  = lnet_nid_to_nid4(&route->lr_nid);
1001                                         *hops     = route->lr_hops;
1002                                         *priority = route->lr_priority;
1003                                         *sensitivity = route->lr_gateway->
1004                                                 lp_health_sensitivity;
1005                                         if (lnet_is_route_alive(route))
1006                                                 *flags |= LNET_RT_ALIVE;
1007                                         else
1008                                                 *flags &= ~LNET_RT_ALIVE;
1009                                         if (route->lr_single_hop)
1010                                                 *flags &= ~LNET_RT_MULTI_HOP;
1011                                         else
1012                                                 *flags |= LNET_RT_MULTI_HOP;
1013                                         lnet_net_unlock(cpt);
1014                                         return 0;
1015                                 }
1016                         }
1017                 }
1018         }
1019
1020         lnet_net_unlock(cpt);
1021         return -ENOENT;
1022 }
1023
1024 static void
1025 lnet_wait_known_routerstate(void)
1026 {
1027         struct lnet_peer *rtr;
1028         struct list_head *entry;
1029         int all_known;
1030
1031         LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
1032
1033         for (;;) {
1034                 int cpt = lnet_net_lock_current();
1035
1036                 all_known = 1;
1037                 list_for_each(entry, &the_lnet.ln_routers) {
1038                         rtr = list_entry(entry, struct lnet_peer,
1039                                          lp_rtr_list);
1040
1041                         spin_lock(&rtr->lp_lock);
1042
1043                         if ((rtr->lp_state & LNET_PEER_RTR_DISCOVERED) == 0) {
1044                                 all_known = 0;
1045                                 spin_unlock(&rtr->lp_lock);
1046                                 break;
1047                         }
1048                         spin_unlock(&rtr->lp_lock);
1049                 }
1050
1051                 lnet_net_unlock(cpt);
1052
1053                 if (all_known)
1054                         return;
1055
1056                 schedule_timeout_uninterruptible(cfs_time_seconds(1));
1057         }
1058 }
1059
1060 static inline bool
1061 lnet_net_set_status_locked(struct lnet_net *net, __u32 status)
1062 {
1063         struct lnet_ni *ni;
1064         bool update = false;
1065
1066         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1067                 if (lnet_ni_set_status(ni, status))
1068                         update = true;
1069
1070         return update;
1071 }
1072
1073 static bool
1074 lnet_update_ni_status_locked(void)
1075 {
1076         struct lnet_net *net;
1077         struct lnet_ni *ni;
1078         bool push = false;
1079         time64_t now;
1080         time64_t timeout;
1081
1082         LASSERT(the_lnet.ln_routing);
1083
1084         timeout = router_ping_timeout + alive_router_check_interval;
1085
1086         now = ktime_get_seconds();
1087         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1088                 if (net->net_lnd->lnd_type == LOLND)
1089                         continue;
1090
1091                 if (now < net->net_last_alive + timeout)
1092                         goto check_ni_fatal;
1093
1094                 spin_lock(&net->net_lock);
1095                 /* re-check with lock */
1096                 if (now < net->net_last_alive + timeout) {
1097                         spin_unlock(&net->net_lock);
1098                         goto check_ni_fatal;
1099                 }
1100                 spin_unlock(&net->net_lock);
1101
1102                 /*
1103                  * if the net didn't receive any traffic for past the
1104                  * timeout on any of its constituent NIs, then mark all
1105                  * the NIs down.
1106                  */
1107                 if (lnet_net_set_status_locked(net, LNET_NI_STATUS_DOWN)) {
1108                         push = true;
1109                         continue;
1110                 }
1111
1112 check_ni_fatal:
1113                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1114                         /* lnet_ni_set_status() will perform the same check of
1115                          * ni_status while holding the ni lock. We can safely
1116                          * check ni_status without that lock because it is only
1117                          * written to under net_lock/EX and our caller is
1118                          * holding a net lock.
1119                          */
1120                         if (atomic_read(&ni->ni_fatal_error_on) &&
1121                             ni->ni_status &&
1122                             ni->ni_status->ns_status != LNET_NI_STATUS_DOWN &&
1123                             lnet_ni_set_status(ni, LNET_NI_STATUS_DOWN))
1124                                 push = true;
1125                 }
1126         }
1127
1128         return push;
1129 }
1130
1131 void lnet_wait_router_start(void)
1132 {
1133         if (check_routers_before_use) {
1134                 /* Note that a helpful side-effect of pinging all known routers
1135                  * at startup is that it makes them drop stale connections they
1136                  * may have to a previous instance of me. */
1137                 lnet_wait_known_routerstate();
1138         }
1139 }
1140
1141 /*
1142  * This function is called from the monitor thread to check if there are
1143  * any active routers that need to be checked.
1144  */
1145 bool lnet_router_checker_active(void)
1146 {
1147         /* Router Checker thread needs to run when routing is enabled in
1148          * order to call lnet_update_ni_status_locked() */
1149         if (the_lnet.ln_routing)
1150                 return true;
1151
1152         return !list_empty(&the_lnet.ln_routers) &&
1153                 alive_router_check_interval > 0;
1154 }
1155
1156 void
1157 lnet_check_routers(void)
1158 {
1159         struct lnet_peer_net *first_lpn;
1160         struct lnet_peer_net *lpn;
1161         struct lnet_peer_ni *lpni;
1162         struct list_head *entry;
1163         struct lnet_peer *rtr;
1164         bool push = false;
1165         bool needs_ping;
1166         bool found_lpn;
1167         __u64 version;
1168         __u32 net_id;
1169         time64_t now;
1170         int cpt;
1171         int rc;
1172
1173         cpt = lnet_net_lock_current();
1174 rescan:
1175         version = the_lnet.ln_routers_version;
1176
1177         list_for_each(entry, &the_lnet.ln_routers) {
1178                 rtr = list_entry(entry, struct lnet_peer,
1179                                  lp_rtr_list);
1180
1181                 /* If we're currently discovering the peer then don't
1182                  * issue another discovery
1183                  */
1184                 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY)
1185                         continue;
1186
1187                 now = ktime_get_real_seconds();
1188
1189                 /* find the next local peer net which needs to be ping'd */
1190                 needs_ping = false;
1191                 first_lpn = NULL;
1192                 found_lpn = false;
1193                 net_id = rtr->lp_disc_net_id;
1194                 do {
1195                         lpn = lnet_get_next_peer_net_locked(rtr, net_id);
1196                         if (!lpn) {
1197                                 CERROR("gateway %s has no networks\n",
1198                                 libcfs_nidstr(&rtr->lp_primary_nid));
1199                                 break;
1200                         }
1201
1202                         /* We looped back to the first peer net */
1203                         if (first_lpn == lpn)
1204                                 break;
1205                         if (!first_lpn)
1206                                 first_lpn = lpn;
1207
1208                         net_id = lpn->lpn_net_id;
1209                         if (!lnet_islocalnet_locked(net_id))
1210                                 continue;
1211
1212                         found_lpn = true;
1213
1214                         CDEBUG(D_NET, "rtr %s(%p) %s(%p) next ping %lld\n",
1215                                libcfs_nidstr(&rtr->lp_primary_nid), rtr,
1216                                libcfs_net2str(net_id), lpn,
1217                                lpn->lpn_next_ping);
1218
1219                         needs_ping = now >= lpn->lpn_next_ping;
1220
1221                 } while (!needs_ping);
1222
1223                 if (!found_lpn || !lpn) {
1224                         CERROR("no local network found for gateway %s\n",
1225                                libcfs_nidstr(&rtr->lp_primary_nid));
1226                         continue;
1227                 }
1228
1229                 if (!needs_ping)
1230                         continue;
1231
1232                 spin_lock(&rtr->lp_lock);
1233                 /* make sure we fully discover the router */
1234                 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1235                 rtr->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH |
1236                         LNET_PEER_RTR_DISCOVERY;
1237                 spin_unlock(&rtr->lp_lock);
1238
1239                 /* find the peer_ni associated with the primary NID */
1240                 lpni = lnet_peer_ni_get_locked(rtr, &rtr->lp_primary_nid);
1241                 if (!lpni) {
1242                         CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
1243                                libcfs_nidstr(&rtr->lp_primary_nid));
1244                         continue;
1245                 }
1246                 lnet_peer_ni_addref_locked(lpni);
1247
1248                 /* specify the net to use */
1249                 rtr->lp_disc_net_id = lpn->lpn_net_id;
1250
1251                 /* discover the router */
1252                 CDEBUG(D_NET, "discover %s, cpt = %d\n",
1253                        libcfs_nidstr(&lpni->lpni_nid), cpt);
1254                 rc = lnet_discover_peer_locked(lpni, cpt, false);
1255
1256                 /* drop ref taken above */
1257                 lnet_peer_ni_decref_locked(lpni);
1258
1259                 if (!rc)
1260                         lpn->lpn_next_ping = now + alive_router_check_interval;
1261                 else
1262                         CERROR("Failed to discover router %s\n",
1263                                libcfs_nidstr(&rtr->lp_primary_nid));
1264
1265                 /* NB cpt lock was dropped in lnet_discover_peer_locked() */
1266                 if (version != the_lnet.ln_routers_version) {
1267                         /* the routers list has changed */
1268                         goto rescan;
1269                 }
1270         }
1271
1272         if (the_lnet.ln_routing)
1273                 push = lnet_update_ni_status_locked();
1274
1275         lnet_net_unlock(cpt);
1276
1277         /* if the status of the ni changed update the peers */
1278         if (push)
1279                 lnet_push_update_to_peers(1);
1280 }
1281
1282 void
1283 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1284 {
1285         int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1286
1287         while (--npages >= 0)
1288                 __free_page(rb->rb_kiov[npages].bv_page);
1289
1290         LIBCFS_FREE(rb, sz);
1291 }
1292
1293 static struct lnet_rtrbuf *
1294 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1295 {
1296         int            npages = rbp->rbp_npages;
1297         int            sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1298         struct page   *page;
1299         struct lnet_rtrbuf *rb;
1300         int            i;
1301
1302         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1303         if (rb == NULL)
1304                 return NULL;
1305
1306         rb->rb_pool = rbp;
1307
1308         for (i = 0; i < npages; i++) {
1309                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt, GFP_KERNEL |
1310                                           __GFP_ZERO | __GFP_NORETRY);
1311                 if (page == NULL) {
1312                         while (--i >= 0)
1313                                 __free_page(rb->rb_kiov[i].bv_page);
1314
1315                         LIBCFS_FREE(rb, sz);
1316                         return NULL;
1317                 }
1318
1319                 rb->rb_kiov[i].bv_len = PAGE_SIZE;
1320                 rb->rb_kiov[i].bv_offset = 0;
1321                 rb->rb_kiov[i].bv_page = page;
1322         }
1323
1324         return rb;
1325 }
1326
1327 static void
1328 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1329 {
1330         int npages = rbp->rbp_npages;
1331         struct lnet_rtrbuf *rb;
1332         LIST_HEAD(tmp);
1333
1334         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1335                 return;
1336
1337         lnet_net_lock(cpt);
1338         list_splice_init(&rbp->rbp_msgs, &tmp);
1339         lnet_drop_routed_msgs_locked(&tmp, cpt);
1340         list_splice_init(&rbp->rbp_bufs, &tmp);
1341         rbp->rbp_req_nbuffers = 0;
1342         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1343         rbp->rbp_mincredits = 0;
1344         lnet_net_unlock(cpt);
1345
1346         /* Free buffers on the free list. */
1347         while (!list_empty(&tmp)) {
1348                 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1349                 list_del(&rb->rb_list);
1350                 lnet_destroy_rtrbuf(rb, npages);
1351         }
1352 }
1353
1354 static int
1355 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1356 {
1357         LIST_HEAD(rb_list);
1358         struct lnet_rtrbuf *rb;
1359         int             num_rb;
1360         int             num_buffers = 0;
1361         int             old_req_nbufs;
1362         int             npages = rbp->rbp_npages;
1363
1364         lnet_net_lock(cpt);
1365         /* If we are called for less buffers than already in the pool, we
1366          * just lower the req_nbuffers number and excess buffers will be
1367          * thrown away as they are returned to the free list.  Credits
1368          * then get adjusted as well.
1369          * If we already have enough buffers allocated to serve the
1370          * increase requested, then we can treat that the same way as we
1371          * do the decrease. */
1372         num_rb = nbufs - rbp->rbp_nbuffers;
1373         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1374                 rbp->rbp_req_nbuffers = nbufs;
1375                 lnet_net_unlock(cpt);
1376                 return 0;
1377         }
1378         /* store the older value of rbp_req_nbuffers and then set it to
1379          * the new request to prevent lnet_return_rx_credits_locked() from
1380          * freeing buffers that we need to keep around */
1381         old_req_nbufs = rbp->rbp_req_nbuffers;
1382         rbp->rbp_req_nbuffers = nbufs;
1383         lnet_net_unlock(cpt);
1384
1385         /* allocate the buffers on a local list first.  If all buffers are
1386          * allocated successfully then join this list to the rbp buffer
1387          * list.  If not then free all allocated buffers. */
1388         while (num_rb-- > 0) {
1389                 rb = lnet_new_rtrbuf(rbp, cpt);
1390                 if (rb == NULL) {
1391                         CERROR("lnet: error allocating %ux%u page router buffers on CPT %u: rc = %d\n",
1392                                nbufs, npages, cpt, -ENOMEM);
1393
1394                         lnet_net_lock(cpt);
1395                         rbp->rbp_req_nbuffers = old_req_nbufs;
1396                         lnet_net_unlock(cpt);
1397
1398                         goto failed;
1399                 }
1400
1401                 list_add(&rb->rb_list, &rb_list);
1402                 num_buffers++;
1403         }
1404
1405         lnet_net_lock(cpt);
1406
1407         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1408         rbp->rbp_nbuffers += num_buffers;
1409         rbp->rbp_credits += num_buffers;
1410         rbp->rbp_mincredits = rbp->rbp_credits;
1411         /* We need to schedule blocked msg using the newly
1412          * added buffers. */
1413         while (!list_empty(&rbp->rbp_bufs) &&
1414                !list_empty(&rbp->rbp_msgs))
1415                 lnet_schedule_blocked_locked(rbp);
1416
1417         lnet_net_unlock(cpt);
1418
1419         return 0;
1420
1421 failed:
1422         while (!list_empty(&rb_list)) {
1423                 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1424                 list_del(&rb->rb_list);
1425                 lnet_destroy_rtrbuf(rb, npages);
1426         }
1427
1428         return -ENOMEM;
1429 }
1430
1431 static void
1432 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1433 {
1434         INIT_LIST_HEAD(&rbp->rbp_msgs);
1435         INIT_LIST_HEAD(&rbp->rbp_bufs);
1436
1437         rbp->rbp_npages = npages;
1438         rbp->rbp_credits = 0;
1439         rbp->rbp_mincredits = 0;
1440 }
1441
1442 void
1443 lnet_rtrpools_free(int keep_pools)
1444 {
1445         struct lnet_rtrbufpool *rtrp;
1446         int               i;
1447
1448         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1449                 return;
1450
1451         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1452                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1453                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1454                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1455         }
1456
1457         if (!keep_pools) {
1458                 cfs_percpt_free(the_lnet.ln_rtrpools);
1459                 the_lnet.ln_rtrpools = NULL;
1460         }
1461 }
1462
1463 static int
1464 lnet_nrb_tiny_calculate(void)
1465 {
1466         int     nrbs = LNET_NRB_TINY;
1467
1468         if (tiny_router_buffers < 0) {
1469                 LCONSOLE_ERROR_MSG(0x10c,
1470                                    "tiny_router_buffers=%d invalid when "
1471                                    "routing enabled\n", tiny_router_buffers);
1472                 return -EINVAL;
1473         }
1474
1475         if (tiny_router_buffers > 0)
1476                 nrbs = tiny_router_buffers;
1477
1478         nrbs /= LNET_CPT_NUMBER;
1479         return max(nrbs, LNET_NRB_TINY_MIN);
1480 }
1481
1482 static int
1483 lnet_nrb_small_calculate(void)
1484 {
1485         int     nrbs = LNET_NRB_SMALL;
1486
1487         if (small_router_buffers < 0) {
1488                 LCONSOLE_ERROR_MSG(0x10c,
1489                                    "small_router_buffers=%d invalid when "
1490                                    "routing enabled\n", small_router_buffers);
1491                 return -EINVAL;
1492         }
1493
1494         if (small_router_buffers > 0)
1495                 nrbs = small_router_buffers;
1496
1497         nrbs /= LNET_CPT_NUMBER;
1498         return max(nrbs, LNET_NRB_SMALL_MIN);
1499 }
1500
1501 static int
1502 lnet_nrb_large_calculate(void)
1503 {
1504         int     nrbs = LNET_NRB_LARGE;
1505
1506         if (large_router_buffers < 0) {
1507                 LCONSOLE_ERROR_MSG(0x10c,
1508                                    "large_router_buffers=%d invalid when "
1509                                    "routing enabled\n", large_router_buffers);
1510                 return -EINVAL;
1511         }
1512
1513         if (large_router_buffers > 0)
1514                 nrbs = large_router_buffers;
1515
1516         nrbs /= LNET_CPT_NUMBER;
1517         return max(nrbs, LNET_NRB_LARGE_MIN);
1518 }
1519
1520 int
1521 lnet_rtrpools_alloc(int im_a_router)
1522 {
1523         struct lnet_rtrbufpool *rtrp;
1524         int     nrb_tiny;
1525         int     nrb_small;
1526         int     nrb_large;
1527         int     rc;
1528         int     i;
1529
1530         if (!strcmp(forwarding, "")) {
1531                 /* not set either way */
1532                 if (!im_a_router)
1533                         return 0;
1534         } else if (!strcmp(forwarding, "disabled")) {
1535                 /* explicitly disabled */
1536                 return 0;
1537         } else if (!strcmp(forwarding, "enabled")) {
1538                 /* explicitly enabled */
1539         } else {
1540                 rc = -EINVAL;
1541                 LCONSOLE_ERROR_MSG(0x10b,
1542                                    "lnet: forwarding='%s' not set to either 'enabled' or 'disabled': rc = %d\n",
1543                                    forwarding, rc);
1544                 return rc;
1545         }
1546
1547         nrb_tiny = lnet_nrb_tiny_calculate();
1548         if (nrb_tiny < 0)
1549                 return -EINVAL;
1550
1551         nrb_small = lnet_nrb_small_calculate();
1552         if (nrb_small < 0)
1553                 return -EINVAL;
1554
1555         nrb_large = lnet_nrb_large_calculate();
1556         if (nrb_large < 0)
1557                 return -EINVAL;
1558
1559         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1560                                                 LNET_NRBPOOLS *
1561                                                 sizeof(struct lnet_rtrbufpool));
1562         if (the_lnet.ln_rtrpools == NULL) {
1563                 rc = -ENOMEM;
1564                 LCONSOLE_ERROR_MSG(0x10c,
1565                         "lnet: error allocating router buffer pool: rc = %d\n",
1566                         rc);
1567                 return rc;
1568         }
1569
1570         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1571                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1572                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1573                                               nrb_tiny, i);
1574                 if (rc)
1575                         goto failed;
1576
1577                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1578                                   LNET_NRB_SMALL_PAGES);
1579                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1580                                               nrb_small, i);
1581                 if (rc)
1582                         goto failed;
1583
1584                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1585                                   LNET_NRB_LARGE_PAGES);
1586                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1587                                               nrb_large, i);
1588                 if (rc)
1589                         goto failed;
1590         }
1591
1592         lnet_net_lock(LNET_LOCK_EX);
1593         the_lnet.ln_routing = 1;
1594         lnet_net_unlock(LNET_LOCK_EX);
1595         complete(&the_lnet.ln_mt_wait_complete);
1596         return 0;
1597
1598  failed:
1599         lnet_rtrpools_free(0);
1600         return rc;
1601 }
1602
1603 static int
1604 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1605 {
1606         int nrb = 0;
1607         int rc = 0;
1608         int i;
1609         struct lnet_rtrbufpool *rtrp;
1610
1611         /* If the provided values for each buffer pool are different than the
1612          * configured values, we need to take action. */
1613         if (tiny >= 0) {
1614                 tiny_router_buffers = tiny;
1615                 nrb = lnet_nrb_tiny_calculate();
1616                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1617                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1618                                                       nrb, i);
1619                         if (rc != 0)
1620                                 return rc;
1621                 }
1622         }
1623         if (small >= 0) {
1624                 small_router_buffers = small;
1625                 nrb = lnet_nrb_small_calculate();
1626                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1627                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1628                                                       nrb, i);
1629                         if (rc != 0)
1630                                 return rc;
1631                 }
1632         }
1633         if (large >= 0) {
1634                 large_router_buffers = large;
1635                 nrb = lnet_nrb_large_calculate();
1636                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1637                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1638                                                       nrb, i);
1639                         if (rc != 0)
1640                                 return rc;
1641                 }
1642         }
1643
1644         return 0;
1645 }
1646
1647 int
1648 lnet_rtrpools_adjust(int tiny, int small, int large)
1649 {
1650         /* this function doesn't revert the changes if adding new buffers
1651          * failed.  It's up to the user space caller to revert the
1652          * changes. */
1653
1654         if (!the_lnet.ln_routing)
1655                 return 0;
1656
1657         return lnet_rtrpools_adjust_helper(tiny, small, large);
1658 }
1659
1660 int
1661 lnet_rtrpools_enable(void)
1662 {
1663         int rc = 0;
1664
1665         if (the_lnet.ln_routing)
1666                 return 0;
1667
1668         if (the_lnet.ln_rtrpools == NULL)
1669                 /* If routing is turned off, and we have never
1670                  * initialized the pools before, just call the
1671                  * standard buffer pool allocation routine as
1672                  * if we are just configuring this for the first
1673                  * time. */
1674                 rc = lnet_rtrpools_alloc(1);
1675         else
1676                 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1677         if (rc != 0)
1678                 return rc;
1679
1680         lnet_net_lock(LNET_LOCK_EX);
1681         the_lnet.ln_routing = 1;
1682
1683         the_lnet.ln_ping_target->pb_info.pi_features &=
1684                 ~LNET_PING_FEAT_RTE_DISABLED;
1685         lnet_net_unlock(LNET_LOCK_EX);
1686
1687         if (lnet_peer_discovery_disabled)
1688                 CWARN("Consider turning discovery on to enable full "
1689                       "Multi-Rail routing functionality\n");
1690
1691         return rc;
1692 }
1693
1694 void
1695 lnet_rtrpools_disable(void)
1696 {
1697         if (!the_lnet.ln_routing)
1698                 return;
1699
1700         lnet_net_lock(LNET_LOCK_EX);
1701         the_lnet.ln_routing = 0;
1702         the_lnet.ln_ping_target->pb_info.pi_features |=
1703                 LNET_PING_FEAT_RTE_DISABLED;
1704
1705         tiny_router_buffers = 0;
1706         small_router_buffers = 0;
1707         large_router_buffers = 0;
1708         lnet_net_unlock(LNET_LOCK_EX);
1709         lnet_rtrpools_free(1);
1710 }
1711
1712 static inline void
1713 lnet_notify_peer_down(struct lnet_ni *ni, struct lnet_nid *nid)
1714 {
1715         if (ni->ni_net->net_lnd->lnd_notify_peer_down != NULL)
1716                 (ni->ni_net->net_lnd->lnd_notify_peer_down)(nid);
1717 }
1718
1719 /*
1720  * ni: local NI used to communicate with the peer
1721  * nid: peer NID
1722  * alive: true if peer is alive, false otherwise
1723  * reset: reset health value. This is requested by the LND.
1724  * when: notificaiton time.
1725  */
1726 int
1727 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid4, bool alive, bool reset,
1728             time64_t when)
1729 {
1730         struct lnet_peer_ni *lpni = NULL;
1731         struct lnet_route *route;
1732         struct lnet_peer *lp;
1733         time64_t now = ktime_get_seconds();
1734         struct lnet_nid nid;
1735         int cpt;
1736
1737         lnet_nid4_to_nid(nid4, &nid);
1738         LASSERT(!in_interrupt());
1739
1740         CDEBUG(D_NET, "%s notifying %s: %s\n",
1741                (ni == NULL) ? "userspace" : libcfs_nidstr(&ni->ni_nid),
1742                libcfs_nidstr(&nid), alive ? "up" : "down");
1743
1744         if (ni != NULL &&
1745             LNET_NID_NET(&ni->ni_nid) != LNET_NID_NET(&nid)) {
1746                 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1747                       libcfs_nidstr(&nid), alive ? "birth" : "death",
1748                       libcfs_nidstr(&ni->ni_nid));
1749                 return -EINVAL;
1750         }
1751
1752         /* can't do predictions... */
1753         if (when > now) {
1754                 CWARN("Ignoring prediction from %s of %s %s %lld seconds in the future\n",
1755                         ni ? libcfs_nidstr(&ni->ni_nid) :  "userspace",
1756                         libcfs_nidstr(&nid), alive ? "up" : "down", when - now);
1757                 return -EINVAL;
1758         }
1759
1760         if (ni != NULL && !alive &&             /* LND telling me she's down */
1761             !auto_down) {                       /* auto-down disabled */
1762                 CDEBUG(D_NET, "Auto-down disabled\n");
1763                 return 0;
1764         }
1765
1766         /* must lock 0 since this is used for synchronization */
1767         lnet_net_lock(0);
1768
1769         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1770                 lnet_net_unlock(0);
1771                 return -ESHUTDOWN;
1772         }
1773
1774         lpni = lnet_peer_ni_find_locked(&nid);
1775         if (lpni == NULL) {
1776                 /* nid not found */
1777                 lnet_net_unlock(0);
1778                 CDEBUG(D_NET, "%s not found\n", libcfs_nidstr(&nid));
1779                 return 0;
1780         }
1781
1782         if (alive) {
1783                 if (reset) {
1784                         lpni->lpni_ns_status = LNET_NI_STATUS_UP;
1785                         lnet_set_lpni_healthv_locked(lpni,
1786                                                      LNET_MAX_HEALTH_VALUE);
1787                 } else {
1788                         __u32 sensitivity = lpni->lpni_peer_net->
1789                                         lpn_peer->lp_health_sensitivity;
1790
1791                         lnet_inc_lpni_healthv_locked(lpni,
1792                                         (sensitivity) ? sensitivity :
1793                                         lnet_health_sensitivity);
1794                 }
1795         } else if (reset) {
1796                 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
1797         }
1798
1799         /* recalculate aliveness */
1800         alive = lnet_is_peer_ni_alive(lpni);
1801
1802         lp = lpni->lpni_peer_net->lpn_peer;
1803         /* If this is an LNet router then update route aliveness */
1804         if (lp->lp_rtr_refcount) {
1805                 if (reset)
1806                         /* reset flag indicates gateway peer went up or down */
1807                         lp->lp_alive = alive;
1808
1809                 /* If discovery is disabled, locally or on the gateway, then
1810                  * any routes using lpni as next-hop need to be updated
1811                  *
1812                  * NB: We can get many notifications while a route is down, so
1813                  * we try and avoid the expensive net_lock/EX here for the
1814                  * common case of receiving duplicate lnet_notify() calls (i.e.
1815                  * only grab EX lock when we actually need to update the route
1816                  * aliveness).
1817                  */
1818                 if (lnet_is_discovery_disabled(lp)) {
1819                         list_for_each_entry(route, &lp->lp_routes, lr_gwlist) {
1820                                 if (nid_same(&route->lr_nid, &lpni->lpni_nid))
1821                                         lnet_set_route_aliveness(route, alive);
1822                         }
1823                 }
1824         }
1825
1826         lnet_net_unlock(0);
1827
1828         if (ni != NULL && !alive)
1829                 lnet_notify_peer_down(ni, &lpni->lpni_nid);
1830
1831         cpt = lpni->lpni_cpt;
1832         lnet_net_lock(cpt);
1833         lnet_peer_ni_decref_locked(lpni);
1834         lnet_net_unlock(cpt);
1835
1836         return 0;
1837 }
1838 EXPORT_SYMBOL(lnet_notify);