Whamcloud - gitweb
LU-14895 osd-ldiskfs: combine checksum 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(lnet_nid_t gw_nid, struct list_head *route_list,
124                                      struct list_head *zombies);
125
126 static int
127 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
128 {
129         int rc;
130         unsigned *sen = (unsigned *)kp->arg;
131         unsigned long value;
132
133         rc = kstrtoul(val, 0, &value);
134         if (rc) {
135                 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
136                 return rc;
137         }
138
139         if (value < 0 || value > 100) {
140                 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
141                 return -EINVAL;
142         }
143
144         /*
145          * The purpose of locking the api_mutex here is to ensure that
146          * the correct value ends up stored properly.
147          */
148         mutex_lock(&the_lnet.ln_api_mutex);
149
150         *sen = value;
151
152         mutex_unlock(&the_lnet.ln_api_mutex);
153
154         return 0;
155 }
156
157 void
158 lnet_move_route(struct lnet_route *route, struct lnet_peer *lp,
159                 struct list_head *rt_list)
160 {
161         struct lnet_remotenet *rnet;
162         struct list_head zombies;
163         struct list_head *l;
164
165         INIT_LIST_HEAD(&zombies);
166
167         if (rt_list)
168                 l = rt_list;
169         else
170                 l = &zombies;
171
172         rnet = lnet_find_rnet_locked(route->lr_net);
173         LASSERT(rnet);
174
175         CDEBUG(D_NET, "deleting route %s->%s\n",
176                libcfs_net2str(route->lr_net),
177                libcfs_nidstr(&route->lr_nid));
178
179         /*
180          * use the gateway's lp_primary_nid to delete the route as the
181          * lr_nid can be a constituent NID of the peer
182          */
183         lnet_del_route_from_rnet(
184                 lnet_nid_to_nid4(&route->lr_gateway->lp_primary_nid),
185                 &rnet->lrn_routes, l);
186
187         if (lp) {
188                 route = list_first_entry(l, struct lnet_route,
189                                         lr_list);
190                 route->lr_gateway = lp;
191                 lnet_add_route_to_rnet(rnet, route);
192         } else {
193                 while (!list_empty(l) && !rt_list) {
194                         route = list_first_entry(l, struct lnet_route,
195                                  lr_list);
196                         list_del(&route->lr_list);
197                         LIBCFS_FREE(route, sizeof(*route));
198                 }
199         }
200 }
201
202 void
203 lnet_rtr_transfer_to_peer(struct lnet_peer *src, struct lnet_peer *target)
204 {
205         struct lnet_route *route;
206         struct lnet_route *tmp, *tmp2;
207
208         lnet_net_lock(LNET_LOCK_EX);
209         CDEBUG(D_NET, "transfering routes from %s -> %s\n",
210                libcfs_nidstr(&src->lp_primary_nid),
211                libcfs_nidstr(&target->lp_primary_nid));
212         list_for_each_entry(route, &src->lp_routes, lr_gwlist) {
213                 CDEBUG(D_NET, "%s: %s->%s\n",
214                        libcfs_nidstr(&src->lp_primary_nid),
215                        libcfs_net2str(route->lr_net),
216                        libcfs_nidstr(&route->lr_nid));
217         }
218         list_splice_init(&src->lp_rtrq, &target->lp_rtrq);
219         list_for_each_entry_safe(route, tmp, &src->lp_routes, lr_gwlist) {
220                 struct lnet_route *r2;
221                 bool present = false;
222                 list_for_each_entry_safe(r2, tmp2, &target->lp_routes, lr_gwlist) {
223                         if (route->lr_net == r2->lr_net) {
224                                 if (route->lr_priority >= r2->lr_priority)
225                                         present = true;
226                                 else if (route->lr_hops >= r2->lr_hops)
227                                         present = true;
228                                 else
229                                         lnet_move_route(r2, NULL, NULL);
230                         }
231                 }
232                 if (present)
233                         lnet_move_route(route, NULL, NULL);
234                 else
235                         lnet_move_route(route, target, NULL);
236         }
237
238         if (list_empty(&target->lp_rtr_list)) {
239                 lnet_peer_addref_locked(target);
240                 list_add_tail(&target->lp_rtr_list, &the_lnet.ln_routers);
241         }
242
243         the_lnet.ln_routers_version++;
244         lnet_net_unlock(LNET_LOCK_EX);
245 }
246
247 int
248 lnet_peers_start_down(void)
249 {
250         return check_routers_before_use;
251 }
252
253 /*
254  * The peer_net of a gateway is alive if at least one of the peer_ni's on
255  * that peer_net is alive.
256  */
257 static bool
258 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
259 {
260         struct lnet_peer_ni *lpni;
261
262         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
263                 if (lnet_is_peer_ni_alive(lpni))
264                         return true;
265         }
266
267         return false;
268 }
269
270 /*
271  * a gateway is alive only if all its nets are alive
272  * called with cpt lock held
273  */
274 bool lnet_is_gateway_alive(struct lnet_peer *gw)
275 {
276         struct lnet_peer_net *lpn;
277
278         if (!gw->lp_alive)
279                 return false;
280
281         list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
282                 if (!lnet_is_gateway_net_alive(lpn))
283                         return false;
284         }
285
286         return true;
287 }
288
289 /*
290  * lnet_is_route_alive() needs to be called with cpt lock held
291  * A route is alive if the gateway can route between the local network and
292  * the remote network of the route.
293  * This means at least one NI is alive on each of the local and remote
294  * networks of the gateway.
295  */
296 bool lnet_is_route_alive(struct lnet_route *route)
297 {
298         struct lnet_peer *gw = route->lr_gateway;
299         struct lnet_peer_net *llpn;
300         struct lnet_peer_net *rlpn;
301
302         /* If the gateway is down then all routes are considered down */
303         if (!gw->lp_alive)
304                 return false;
305
306         /*
307          * if discovery is disabled then rely on the cached aliveness
308          * information. This is handicapped information which we log when
309          * we receive the discovery ping response. The most uptodate
310          * aliveness information can only be obtained when discovery is
311          * enabled.
312          */
313         if (lnet_is_discovery_disabled(gw))
314                 return atomic_read(&route->lr_alive) == 1;
315
316         /*
317          * check the gateway's interfaces on the local network
318          */
319         llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
320         if (!llpn)
321                 return false;
322
323         if (!lnet_is_gateway_net_alive(llpn))
324                 return false;
325
326         /*
327          * For single hop routes avoid_asym_router_failure dictates
328          * that the remote net must exist on the gateway. For multi-hop
329          * routes the next-hop will not have the remote net.
330          */
331         if (avoid_asym_router_failure &&
332             (route->lr_hops == 1 || route->lr_hops == LNET_UNDEFINED_HOPS)) {
333                 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
334                 if (!rlpn)
335                         return false;
336                 if (!lnet_is_gateway_net_alive(rlpn))
337                         return false;
338         }
339
340         spin_lock(&gw->lp_lock);
341         if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
342                 spin_unlock(&gw->lp_lock);
343                 if (gw->lp_rtr_refcount > 0)
344                         CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
345                                libcfs_nidstr(&gw->lp_primary_nid));
346                 return false;
347         }
348         spin_unlock(&gw->lp_lock);
349
350         return true;
351 }
352
353 void
354 lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
355                                struct lnet_peer *new_lp)
356 {
357         struct lnet_peer_ni *lpni;
358         struct lnet_route *route;
359
360         /*
361          * Although a route is correlated with a peer, but when it's added
362          * a specific NID is used. That NID refers to a peer_ni within
363          * a peer. There could be other peer_nis on the same net, which
364          * can be used to send to that gateway. However when we are
365          * consolidating gateways because of discovery, the nid used to
366          * add the route might've moved between gateway peers. In this
367          * case we want to move the route to the new gateway as well. The
368          * intent here is not to confuse the user who added the route.
369          */
370         list_for_each_entry(route, &orig_lp->lp_routes, lr_gwlist) {
371                 lpni = lnet_peer_ni_get_locked(orig_lp, &route->lr_nid);
372                 if (!lpni) {
373                         lnet_net_lock(LNET_LOCK_EX);
374                         list_move(&route->lr_gwlist, &new_lp->lp_routes);
375                         lnet_net_unlock(LNET_LOCK_EX);
376                 }
377         }
378 }
379
380 static inline void
381 lnet_check_route_inconsistency(struct lnet_route *route)
382 {
383         if (!route->lr_single_hop &&
384             (route->lr_hops == 1 || route->lr_hops == LNET_UNDEFINED_HOPS)) {
385                 CWARN("route %s->%s is detected to be multi-hop but hop count is set to %d\n",
386                         libcfs_net2str(route->lr_net),
387                         libcfs_nidstr(&route->lr_gateway->lp_primary_nid),
388                         (int) route->lr_hops);
389         }
390 }
391
392 static void
393 lnet_set_route_hop_type(struct lnet_peer *gw, struct lnet_route *route)
394 {
395         struct lnet_peer_net *lpn;
396         bool single_hop = false;
397
398         list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
399                 if (route->lr_net == lpn->lpn_net_id) {
400                         single_hop = true;
401                         break;
402                 }
403         }
404         route->lr_single_hop = single_hop;
405         lnet_check_route_inconsistency(route);
406 }
407
408 /* Must hold net_lock/EX */
409 void
410 lnet_router_discovery_ping_reply(struct lnet_peer *lp)
411 {
412         struct lnet_ping_buffer *pbuf = lp->lp_data;
413         struct lnet_peer_net *llpn;
414         struct lnet_route *route;
415         bool single_hop = false;
416         bool net_up = false;
417         unsigned lp_state;
418         __u32 net;
419         int i;
420
421
422         spin_lock(&lp->lp_lock);
423         lp_state = lp->lp_state;
424
425         /* only handle replies if discovery is disabled. */
426         if (!lnet_is_discovery_disabled_locked(lp)) {
427                 spin_unlock(&lp->lp_lock);
428                 return;
429         }
430
431         spin_unlock(&lp->lp_lock);
432
433         if (lp_state & LNET_PEER_PING_FAILED ||
434             pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED) {
435                 CDEBUG(D_NET, "Set routes down for gw %s because %s %d\n",
436                        libcfs_nidstr(&lp->lp_primary_nid),
437                        lp_state & LNET_PEER_PING_FAILED ? "ping failed" :
438                        "route feature is disabled", lp->lp_ping_error);
439                 /* If the ping failed or the peer has routing disabled then
440                  * mark the routes served by this peer down
441                  */
442                 list_for_each_entry(route, &lp->lp_routes, lr_gwlist)
443                         lnet_set_route_aliveness(route, false);
444                 return;
445         }
446
447         CDEBUG(D_NET, "Discovery is disabled. Processing reply for gw: %s:%d\n",
448                libcfs_nidstr(&lp->lp_primary_nid), pbuf->pb_info.pi_nnis);
449
450         /*
451          * examine the ping response to determine if the routes on that
452          * gateway should be declared alive.
453          * The route is alive if:
454          *  1. local network to reach the route is alive and
455          *  2. route is single hop, avoid_async_router_failure is set and
456          *     there exists at least one NI on the route's remote net
457          */
458         list_for_each_entry(route, &lp->lp_routes, lr_gwlist) {
459                 llpn = lnet_peer_get_net_locked(lp, route->lr_lnet);
460                 if (!llpn) {
461                         lnet_set_route_aliveness(route, false);
462                         continue;
463                 }
464
465                 if (!lnet_is_gateway_net_alive(llpn)) {
466                         lnet_set_route_aliveness(route, false);
467                         continue;
468                 }
469
470                 single_hop = net_up = false;
471                 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
472                         net = LNET_NIDNET(pbuf->pb_info.pi_ni[i].ns_nid);
473
474                         if (route->lr_net == net) {
475                                 single_hop = true;
476                                 if (pbuf->pb_info.pi_ni[i].ns_status ==
477                                     LNET_NI_STATUS_UP) {
478                                         net_up = true;
479                                         break;
480                                 }
481                         }
482                 }
483
484                 route->lr_single_hop = single_hop;
485                 if (avoid_asym_router_failure &&
486                     (route->lr_hops == 1 ||
487                      route->lr_hops == LNET_UNDEFINED_HOPS))
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, LNET_LOCK_EX);
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->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
744         gw = lpni->lpni_peer_net->lpn_peer;
745
746         route->lr_gateway = gw;
747
748         rnet2 = lnet_find_rnet_locked(net);
749         if (rnet2 == NULL) {
750                 /* new network */
751                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
752                 rnet2 = rnet;
753         }
754
755         /* Search for a duplicate route (it's a NOOP if it is) */
756         add_route = 1;
757         list_for_each(route_entry, &rnet2->lrn_routes) {
758                 struct lnet_route *route2;
759
760                 route2 = list_entry(route_entry, struct lnet_route, lr_list);
761                 if (route2->lr_gateway == route->lr_gateway) {
762                         add_route = 0;
763                         break;
764                 }
765
766                 /* our lookups must be true */
767                 LASSERT(!nid_same(&route2->lr_gateway->lp_primary_nid,
768                                   gateway));
769         }
770
771         /*
772          * It is possible to add multiple routes through the same peer,
773          * but it'll be using a different NID of that peer. When the
774          * gateway is discovered, discovery will consolidate the different
775          * peers into one peer. In this case the discovery code will have
776          * to move the routes from the peer that's being deleted to the
777          * consolidated peer lp_routes list
778          */
779         if (add_route) {
780                 gw->lp_health_sensitivity = sensitivity;
781                 lnet_add_route_to_rnet(rnet2, route);
782                 if (lnet_peer_discovery_disabled)
783                         CWARN("Consider turning discovery on to enable full "
784                               "Multi-Rail routing functionality\n");
785         }
786
787         /*
788          * get rid of the reference on the lpni.
789          */
790         lnet_peer_ni_decref_locked(lpni);
791         lnet_net_unlock(LNET_LOCK_EX);
792
793         rc = 0;
794
795         if (!add_route) {
796                 rc = -EEXIST;
797                 LIBCFS_FREE(route, sizeof(*route));
798         }
799
800         if (rnet != rnet2)
801                 LIBCFS_FREE(rnet, sizeof(*rnet));
802
803         /* kick start the monitor thread to handle the added route */
804         complete(&the_lnet.ln_mt_wait_complete);
805
806         return rc;
807 }
808
809 void
810 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
811                          struct list_head *zombies)
812 {
813         struct lnet_peer *gateway;
814         struct lnet_route *route;
815         struct lnet_route *tmp;
816
817         list_for_each_entry_safe(route, tmp, route_list, lr_list) {
818                 gateway = route->lr_gateway;
819                 if (gw_nid != LNET_NID_ANY &&
820                     gw_nid != lnet_nid_to_nid4(&gateway->lp_primary_nid))
821                         continue;
822
823                 /*
824                  * move to zombie to delete outside the lock
825                  * Note that this function is called with the
826                  * ln_api_mutex held as well as the exclusive net
827                  * lock. Adding to the remote net list happens
828                  * under the same conditions. Same goes for the
829                  * gateway router list
830                  */
831                 list_move(&route->lr_list, zombies);
832                 the_lnet.ln_remote_nets_version++;
833
834                 list_del(&route->lr_gwlist);
835                 lnet_rtr_decref_locked(gateway);
836         }
837 }
838
839 int
840 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
841 {
842         LIST_HEAD(rnet_zombies);
843         struct lnet_remotenet *rnet;
844         struct lnet_remotenet *tmp;
845         struct list_head *rn_list;
846         struct lnet_peer_ni *lpni;
847         struct lnet_route *route;
848         LIST_HEAD(zombies);
849         struct lnet_peer *lp = NULL;
850         int i = 0;
851
852         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
853                libcfs_net2str(net), libcfs_nid2str(gw_nid));
854
855         /* NB Caller may specify either all routes via the given gateway
856          * or a specific route entry actual NIDs) */
857
858         lnet_net_lock(LNET_LOCK_EX);
859
860         lpni = lnet_find_peer_ni_locked(gw_nid);
861         if (lpni) {
862                 lp = lpni->lpni_peer_net->lpn_peer;
863                 LASSERT(lp);
864                 gw_nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
865                 lnet_peer_ni_decref_locked(lpni);
866         }
867
868         if (net != LNET_NET_ANY) {
869                 rnet = lnet_find_rnet_locked(net);
870                 if (!rnet) {
871                         lnet_net_unlock(LNET_LOCK_EX);
872                         return -ENOENT;
873                 }
874                 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
875                                          &zombies);
876                 if (list_empty(&rnet->lrn_routes))
877                         list_move(&rnet->lrn_list, &rnet_zombies);
878                 goto delete_zombies;
879         }
880
881         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
882                 rn_list = &the_lnet.ln_remote_nets_hash[i];
883
884                 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
885                         lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
886                                                  &zombies);
887                         if (list_empty(&rnet->lrn_routes))
888                                 list_move(&rnet->lrn_list, &rnet_zombies);
889                 }
890         }
891
892 delete_zombies:
893         /*
894          * check if there are any routes remaining on the gateway
895          * If there are no more routes make sure to set the peer's
896          * lp_disc_net_id to 0 (invalid), in case we add more routes in
897          * the future on that gateway, then we start our discovery process
898          * from scratch
899          */
900         if (lpni) {
901                 if (list_empty(&lp->lp_routes))
902                         lp->lp_disc_net_id = 0;
903         }
904
905         lnet_net_unlock(LNET_LOCK_EX);
906
907         while (!list_empty(&zombies)) {
908                 route = list_first_entry(&zombies, struct lnet_route, lr_list);
909                 list_del(&route->lr_list);
910                 LIBCFS_FREE(route, sizeof(*route));
911         }
912
913         while (!list_empty(&rnet_zombies)) {
914                 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
915                                         lrn_list);
916                 list_del(&rnet->lrn_list);
917                 LIBCFS_FREE(rnet, sizeof(*rnet));
918         }
919
920         return 0;
921 }
922
923 void
924 lnet_destroy_routes (void)
925 {
926         lnet_del_route(LNET_NET_ANY, LNET_NID_ANY);
927 }
928
929 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
930 {
931         struct lnet_rtrbufpool *rbp;
932         int i, rc = -ENOENT, j;
933
934         if (the_lnet.ln_rtrpools == NULL)
935                 return rc;
936
937
938         cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
939                 if (i != cpt)
940                         continue;
941
942                 lnet_net_lock(i);
943                 for (j = 0; j < LNET_NRBPOOLS; j++) {
944                         pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
945                         pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
946                         pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
947                         pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
948                 }
949                 lnet_net_unlock(i);
950                 rc = 0;
951                 break;
952         }
953
954         lnet_net_lock(LNET_LOCK_EX);
955         pool_cfg->pl_routing = the_lnet.ln_routing;
956         lnet_net_unlock(LNET_LOCK_EX);
957
958         return rc;
959 }
960
961 int
962 lnet_get_route(int idx, __u32 *net, __u32 *hops, lnet_nid_t *gateway,
963                __u32 *flags, __u32 *priority, __u32 *sensitivity)
964 {
965         struct lnet_remotenet *rnet;
966         struct list_head *rn_list;
967         struct lnet_route *route;
968         struct list_head *e1;
969         struct list_head *e2;
970         int cpt;
971         int i;
972
973         cpt = lnet_net_lock_current();
974
975         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
976                 rn_list = &the_lnet.ln_remote_nets_hash[i];
977                 list_for_each(e1, rn_list) {
978                         rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
979
980                         list_for_each(e2, &rnet->lrn_routes) {
981                                 route = list_entry(e2, struct lnet_route,
982                                                    lr_list);
983
984                                 if (idx-- == 0) {
985                                         *net      = rnet->lrn_net;
986                                         *gateway  = lnet_nid_to_nid4(&route->lr_nid);
987                                         *hops     = route->lr_hops;
988                                         *priority = route->lr_priority;
989                                         *sensitivity = route->lr_gateway->
990                                                 lp_health_sensitivity;
991                                         if (lnet_is_route_alive(route))
992                                                 *flags |= LNET_RT_ALIVE;
993                                         else
994                                                 *flags &= ~LNET_RT_ALIVE;
995                                         if (route->lr_single_hop)
996                                                 *flags &= ~LNET_RT_MULTI_HOP;
997                                         else
998                                                 *flags |= LNET_RT_MULTI_HOP;
999                                         lnet_net_unlock(cpt);
1000                                         return 0;
1001                                 }
1002                         }
1003                 }
1004         }
1005
1006         lnet_net_unlock(cpt);
1007         return -ENOENT;
1008 }
1009
1010 static void
1011 lnet_wait_known_routerstate(void)
1012 {
1013         struct lnet_peer *rtr;
1014         struct list_head *entry;
1015         int all_known;
1016
1017         LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
1018
1019         for (;;) {
1020                 int cpt = lnet_net_lock_current();
1021
1022                 all_known = 1;
1023                 list_for_each(entry, &the_lnet.ln_routers) {
1024                         rtr = list_entry(entry, struct lnet_peer,
1025                                          lp_rtr_list);
1026
1027                         spin_lock(&rtr->lp_lock);
1028
1029                         if ((rtr->lp_state & LNET_PEER_RTR_DISCOVERED) == 0) {
1030                                 all_known = 0;
1031                                 spin_unlock(&rtr->lp_lock);
1032                                 break;
1033                         }
1034                         spin_unlock(&rtr->lp_lock);
1035                 }
1036
1037                 lnet_net_unlock(cpt);
1038
1039                 if (all_known)
1040                         return;
1041
1042                 schedule_timeout_uninterruptible(cfs_time_seconds(1));
1043         }
1044 }
1045
1046 static inline bool
1047 lnet_net_set_status_locked(struct lnet_net *net, __u32 status)
1048 {
1049         struct lnet_ni *ni;
1050         bool update = false;
1051
1052         list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
1053                 if (lnet_ni_set_status(ni, status))
1054                         update = true;
1055
1056         return update;
1057 }
1058
1059 static bool
1060 lnet_update_ni_status_locked(void)
1061 {
1062         struct lnet_net *net;
1063         struct lnet_ni *ni;
1064         bool push = false;
1065         time64_t now;
1066         time64_t timeout;
1067
1068         LASSERT(the_lnet.ln_routing);
1069
1070         timeout = router_ping_timeout + alive_router_check_interval;
1071
1072         now = ktime_get_real_seconds();
1073         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1074                 if (net->net_lnd->lnd_type == LOLND)
1075                         continue;
1076
1077                 if (now < net->net_last_alive + timeout)
1078                         goto check_ni_fatal;
1079
1080                 spin_lock(&net->net_lock);
1081                 /* re-check with lock */
1082                 if (now < net->net_last_alive + timeout) {
1083                         spin_unlock(&net->net_lock);
1084                         goto check_ni_fatal;
1085                 }
1086                 spin_unlock(&net->net_lock);
1087
1088                 /*
1089                  * if the net didn't receive any traffic for past the
1090                  * timeout on any of its constituent NIs, then mark all
1091                  * the NIs down.
1092                  */
1093                 if (lnet_net_set_status_locked(net, LNET_NI_STATUS_DOWN)) {
1094                         push = true;
1095                         continue;
1096                 }
1097
1098 check_ni_fatal:
1099                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1100                         /* lnet_ni_set_status() will perform the same check of
1101                          * ni_status while holding the ni lock. We can safely
1102                          * check ni_status without that lock because it is only
1103                          * written to under net_lock/EX and our caller is
1104                          * holding a net lock.
1105                          */
1106                         if (atomic_read(&ni->ni_fatal_error_on) &&
1107                             ni->ni_status &&
1108                             ni->ni_status->ns_status != LNET_NI_STATUS_DOWN &&
1109                             lnet_ni_set_status(ni, LNET_NI_STATUS_DOWN))
1110                                 push = true;
1111                 }
1112         }
1113
1114         return push;
1115 }
1116
1117 void lnet_wait_router_start(void)
1118 {
1119         if (check_routers_before_use) {
1120                 /* Note that a helpful side-effect of pinging all known routers
1121                  * at startup is that it makes them drop stale connections they
1122                  * may have to a previous instance of me. */
1123                 lnet_wait_known_routerstate();
1124         }
1125 }
1126
1127 /*
1128  * This function is called from the monitor thread to check if there are
1129  * any active routers that need to be checked.
1130  */
1131 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 list_head *entry;
1149         struct lnet_peer *rtr;
1150         bool push = false;
1151         bool needs_ping;
1152         bool found_lpn;
1153         __u64 version;
1154         __u32 net_id;
1155         time64_t now;
1156         int cpt;
1157         int rc;
1158
1159         cpt = lnet_net_lock_current();
1160 rescan:
1161         version = the_lnet.ln_routers_version;
1162
1163         list_for_each(entry, &the_lnet.ln_routers) {
1164                 rtr = list_entry(entry, struct lnet_peer,
1165                                  lp_rtr_list);
1166
1167                 /* If we're currently discovering the peer then don't
1168                  * issue another discovery
1169                  */
1170                 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY)
1171                         continue;
1172
1173                 now = ktime_get_real_seconds();
1174
1175                 /* find the next local peer net which needs to be ping'd */
1176                 needs_ping = false;
1177                 first_lpn = NULL;
1178                 found_lpn = false;
1179                 net_id = rtr->lp_disc_net_id;
1180                 do {
1181                         lpn = lnet_get_next_peer_net_locked(rtr, net_id);
1182                         if (!lpn) {
1183                                 CERROR("gateway %s has no networks\n",
1184                                 libcfs_nidstr(&rtr->lp_primary_nid));
1185                                 break;
1186                         }
1187
1188                         /* We looped back to the first peer net */
1189                         if (first_lpn == lpn)
1190                                 break;
1191                         if (!first_lpn)
1192                                 first_lpn = lpn;
1193
1194                         net_id = lpn->lpn_net_id;
1195                         if (!lnet_islocalnet_locked(net_id))
1196                                 continue;
1197
1198                         found_lpn = true;
1199
1200                         CDEBUG(D_NET, "rtr %s(%p) %s(%p) next ping %lld\n",
1201                                libcfs_nidstr(&rtr->lp_primary_nid), rtr,
1202                                libcfs_net2str(net_id), lpn,
1203                                lpn->lpn_next_ping);
1204
1205                         needs_ping = now >= lpn->lpn_next_ping;
1206
1207                 } while (!needs_ping);
1208
1209                 if (!found_lpn || !lpn) {
1210                         CERROR("no local network found for gateway %s\n",
1211                                libcfs_nidstr(&rtr->lp_primary_nid));
1212                         continue;
1213                 }
1214
1215                 if (!needs_ping)
1216                         continue;
1217
1218                 spin_lock(&rtr->lp_lock);
1219                 /* make sure we fully discover the router */
1220                 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1221                 rtr->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH |
1222                         LNET_PEER_RTR_DISCOVERY;
1223                 spin_unlock(&rtr->lp_lock);
1224
1225                 /* find the peer_ni associated with the primary NID */
1226                 lpni = lnet_peer_get_ni_locked(
1227                         rtr, lnet_nid_to_nid4(&rtr->lp_primary_nid));
1228                 if (!lpni) {
1229                         CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
1230                                libcfs_nidstr(&rtr->lp_primary_nid));
1231                         continue;
1232                 }
1233                 lnet_peer_ni_addref_locked(lpni);
1234
1235                 /* specify the net to use */
1236                 rtr->lp_disc_net_id = lpn->lpn_net_id;
1237
1238                 /* discover the router */
1239                 CDEBUG(D_NET, "discover %s, cpt = %d\n",
1240                        libcfs_nidstr(&lpni->lpni_nid), cpt);
1241                 rc = lnet_discover_peer_locked(lpni, cpt, false);
1242
1243                 /* drop ref taken above */
1244                 lnet_peer_ni_decref_locked(lpni);
1245
1246                 if (!rc)
1247                         lpn->lpn_next_ping = now + alive_router_check_interval;
1248                 else
1249                         CERROR("Failed to discover router %s\n",
1250                                libcfs_nidstr(&rtr->lp_primary_nid));
1251
1252                 /* NB cpt lock was dropped in lnet_discover_peer_locked() */
1253                 if (version != the_lnet.ln_routers_version) {
1254                         /* the routers list has changed */
1255                         goto rescan;
1256                 }
1257         }
1258
1259         if (the_lnet.ln_routing)
1260                 push = lnet_update_ni_status_locked();
1261
1262         lnet_net_unlock(cpt);
1263
1264         /* if the status of the ni changed update the peers */
1265         if (push)
1266                 lnet_push_update_to_peers(1);
1267 }
1268
1269 void
1270 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1271 {
1272         int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1273
1274         while (--npages >= 0)
1275                 __free_page(rb->rb_kiov[npages].bv_page);
1276
1277         LIBCFS_FREE(rb, sz);
1278 }
1279
1280 static struct lnet_rtrbuf *
1281 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1282 {
1283         int            npages = rbp->rbp_npages;
1284         int            sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1285         struct page   *page;
1286         struct lnet_rtrbuf *rb;
1287         int            i;
1288
1289         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1290         if (rb == NULL)
1291                 return NULL;
1292
1293         rb->rb_pool = rbp;
1294
1295         for (i = 0; i < npages; i++) {
1296                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1297                                           GFP_KERNEL | __GFP_ZERO);
1298                 if (page == NULL) {
1299                         while (--i >= 0)
1300                                 __free_page(rb->rb_kiov[i].bv_page);
1301
1302                         LIBCFS_FREE(rb, sz);
1303                         return NULL;
1304                 }
1305
1306                 rb->rb_kiov[i].bv_len = PAGE_SIZE;
1307                 rb->rb_kiov[i].bv_offset = 0;
1308                 rb->rb_kiov[i].bv_page = page;
1309         }
1310
1311         return rb;
1312 }
1313
1314 static void
1315 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1316 {
1317         int npages = rbp->rbp_npages;
1318         struct lnet_rtrbuf *rb;
1319         LIST_HEAD(tmp);
1320
1321         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1322                 return;
1323
1324         lnet_net_lock(cpt);
1325         list_splice_init(&rbp->rbp_msgs, &tmp);
1326         lnet_drop_routed_msgs_locked(&tmp, cpt);
1327         list_splice_init(&rbp->rbp_bufs, &tmp);
1328         rbp->rbp_req_nbuffers = 0;
1329         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1330         rbp->rbp_mincredits = 0;
1331         lnet_net_unlock(cpt);
1332
1333         /* Free buffers on the free list. */
1334         while (!list_empty(&tmp)) {
1335                 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1336                 list_del(&rb->rb_list);
1337                 lnet_destroy_rtrbuf(rb, npages);
1338         }
1339 }
1340
1341 static int
1342 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1343 {
1344         LIST_HEAD(rb_list);
1345         struct lnet_rtrbuf *rb;
1346         int             num_rb;
1347         int             num_buffers = 0;
1348         int             old_req_nbufs;
1349         int             npages = rbp->rbp_npages;
1350
1351         lnet_net_lock(cpt);
1352         /* If we are called for less buffers than already in the pool, we
1353          * just lower the req_nbuffers number and excess buffers will be
1354          * thrown away as they are returned to the free list.  Credits
1355          * then get adjusted as well.
1356          * If we already have enough buffers allocated to serve the
1357          * increase requested, then we can treat that the same way as we
1358          * do the decrease. */
1359         num_rb = nbufs - rbp->rbp_nbuffers;
1360         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1361                 rbp->rbp_req_nbuffers = nbufs;
1362                 lnet_net_unlock(cpt);
1363                 return 0;
1364         }
1365         /* store the older value of rbp_req_nbuffers and then set it to
1366          * the new request to prevent lnet_return_rx_credits_locked() from
1367          * freeing buffers that we need to keep around */
1368         old_req_nbufs = rbp->rbp_req_nbuffers;
1369         rbp->rbp_req_nbuffers = nbufs;
1370         lnet_net_unlock(cpt);
1371
1372         /* allocate the buffers on a local list first.  If all buffers are
1373          * allocated successfully then join this list to the rbp buffer
1374          * list.  If not then free all allocated buffers. */
1375         while (num_rb-- > 0) {
1376                 rb = lnet_new_rtrbuf(rbp, cpt);
1377                 if (rb == NULL) {
1378                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1379                                nbufs, npages);
1380
1381                         lnet_net_lock(cpt);
1382                         rbp->rbp_req_nbuffers = old_req_nbufs;
1383                         lnet_net_unlock(cpt);
1384
1385                         goto failed;
1386                 }
1387
1388                 list_add(&rb->rb_list, &rb_list);
1389                 num_buffers++;
1390         }
1391
1392         lnet_net_lock(cpt);
1393
1394         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1395         rbp->rbp_nbuffers += num_buffers;
1396         rbp->rbp_credits += num_buffers;
1397         rbp->rbp_mincredits = rbp->rbp_credits;
1398         /* We need to schedule blocked msg using the newly
1399          * added buffers. */
1400         while (!list_empty(&rbp->rbp_bufs) &&
1401                !list_empty(&rbp->rbp_msgs))
1402                 lnet_schedule_blocked_locked(rbp);
1403
1404         lnet_net_unlock(cpt);
1405
1406         return 0;
1407
1408 failed:
1409         while (!list_empty(&rb_list)) {
1410                 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1411                 list_del(&rb->rb_list);
1412                 lnet_destroy_rtrbuf(rb, npages);
1413         }
1414
1415         return -ENOMEM;
1416 }
1417
1418 static void
1419 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1420 {
1421         INIT_LIST_HEAD(&rbp->rbp_msgs);
1422         INIT_LIST_HEAD(&rbp->rbp_bufs);
1423
1424         rbp->rbp_npages = npages;
1425         rbp->rbp_credits = 0;
1426         rbp->rbp_mincredits = 0;
1427 }
1428
1429 void
1430 lnet_rtrpools_free(int keep_pools)
1431 {
1432         struct lnet_rtrbufpool *rtrp;
1433         int               i;
1434
1435         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1436                 return;
1437
1438         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1439                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1440                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1441                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1442         }
1443
1444         if (!keep_pools) {
1445                 cfs_percpt_free(the_lnet.ln_rtrpools);
1446                 the_lnet.ln_rtrpools = NULL;
1447         }
1448 }
1449
1450 static int
1451 lnet_nrb_tiny_calculate(void)
1452 {
1453         int     nrbs = LNET_NRB_TINY;
1454
1455         if (tiny_router_buffers < 0) {
1456                 LCONSOLE_ERROR_MSG(0x10c,
1457                                    "tiny_router_buffers=%d invalid when "
1458                                    "routing enabled\n", tiny_router_buffers);
1459                 return -EINVAL;
1460         }
1461
1462         if (tiny_router_buffers > 0)
1463                 nrbs = tiny_router_buffers;
1464
1465         nrbs /= LNET_CPT_NUMBER;
1466         return max(nrbs, LNET_NRB_TINY_MIN);
1467 }
1468
1469 static int
1470 lnet_nrb_small_calculate(void)
1471 {
1472         int     nrbs = LNET_NRB_SMALL;
1473
1474         if (small_router_buffers < 0) {
1475                 LCONSOLE_ERROR_MSG(0x10c,
1476                                    "small_router_buffers=%d invalid when "
1477                                    "routing enabled\n", small_router_buffers);
1478                 return -EINVAL;
1479         }
1480
1481         if (small_router_buffers > 0)
1482                 nrbs = small_router_buffers;
1483
1484         nrbs /= LNET_CPT_NUMBER;
1485         return max(nrbs, LNET_NRB_SMALL_MIN);
1486 }
1487
1488 static int
1489 lnet_nrb_large_calculate(void)
1490 {
1491         int     nrbs = LNET_NRB_LARGE;
1492
1493         if (large_router_buffers < 0) {
1494                 LCONSOLE_ERROR_MSG(0x10c,
1495                                    "large_router_buffers=%d invalid when "
1496                                    "routing enabled\n", large_router_buffers);
1497                 return -EINVAL;
1498         }
1499
1500         if (large_router_buffers > 0)
1501                 nrbs = large_router_buffers;
1502
1503         nrbs /= LNET_CPT_NUMBER;
1504         return max(nrbs, LNET_NRB_LARGE_MIN);
1505 }
1506
1507 int
1508 lnet_rtrpools_alloc(int im_a_router)
1509 {
1510         struct lnet_rtrbufpool *rtrp;
1511         int     nrb_tiny;
1512         int     nrb_small;
1513         int     nrb_large;
1514         int     rc;
1515         int     i;
1516
1517         if (!strcmp(forwarding, "")) {
1518                 /* not set either way */
1519                 if (!im_a_router)
1520                         return 0;
1521         } else if (!strcmp(forwarding, "disabled")) {
1522                 /* explicitly disabled */
1523                 return 0;
1524         } else if (!strcmp(forwarding, "enabled")) {
1525                 /* explicitly enabled */
1526         } else {
1527                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1528                                    "'enabled' or 'disabled'\n");
1529                 return -EINVAL;
1530         }
1531
1532         nrb_tiny = lnet_nrb_tiny_calculate();
1533         if (nrb_tiny < 0)
1534                 return -EINVAL;
1535
1536         nrb_small = lnet_nrb_small_calculate();
1537         if (nrb_small < 0)
1538                 return -EINVAL;
1539
1540         nrb_large = lnet_nrb_large_calculate();
1541         if (nrb_large < 0)
1542                 return -EINVAL;
1543
1544         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1545                                                 LNET_NRBPOOLS *
1546                                                 sizeof(struct lnet_rtrbufpool));
1547         if (the_lnet.ln_rtrpools == NULL) {
1548                 LCONSOLE_ERROR_MSG(0x10c,
1549                                    "Failed to initialize router buffe pool\n");
1550                 return -ENOMEM;
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 != 0)
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 != 0)
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 != 0)
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, lnet_nid_t 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 nid, 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         int cpt;
1718
1719         LASSERT(!in_interrupt());
1720
1721         CDEBUG(D_NET, "%s notifying %s: %s\n",
1722                (ni == NULL) ? "userspace" : libcfs_nidstr(&ni->ni_nid),
1723                libcfs_nid2str(nid), alive ? "up" : "down");
1724
1725         if (ni != NULL &&
1726             LNET_NID_NET(&ni->ni_nid) != LNET_NIDNET(nid)) {
1727                 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1728                       libcfs_nid2str(nid), alive ? "birth" : "death",
1729                       libcfs_nidstr(&ni->ni_nid));
1730                 return -EINVAL;
1731         }
1732
1733         /* can't do predictions... */
1734         if (when > now) {
1735                 CWARN("Ignoring prediction from %s of %s %s %lld seconds in the future\n",
1736                         ni ? libcfs_nidstr(&ni->ni_nid) :  "userspace",
1737                         libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1738                 return -EINVAL;
1739         }
1740
1741         if (ni != NULL && !alive &&             /* LND telling me she's down */
1742             !auto_down) {                       /* auto-down disabled */
1743                 CDEBUG(D_NET, "Auto-down disabled\n");
1744                 return 0;
1745         }
1746
1747         /* must lock 0 since this is used for synchronization */
1748         lnet_net_lock(0);
1749
1750         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1751                 lnet_net_unlock(0);
1752                 return -ESHUTDOWN;
1753         }
1754
1755         lpni = lnet_find_peer_ni_locked(nid);
1756         if (lpni == NULL) {
1757                 /* nid not found */
1758                 lnet_net_unlock(0);
1759                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1760                 return 0;
1761         }
1762
1763         if (alive) {
1764                 if (reset) {
1765                         lpni->lpni_ns_status = LNET_NI_STATUS_UP;
1766                         lnet_set_lpni_healthv_locked(lpni,
1767                                                      LNET_MAX_HEALTH_VALUE);
1768                 } else {
1769                         __u32 sensitivity = lpni->lpni_peer_net->
1770                                         lpn_peer->lp_health_sensitivity;
1771
1772                         lnet_inc_lpni_healthv_locked(lpni,
1773                                         (sensitivity) ? sensitivity :
1774                                         lnet_health_sensitivity);
1775                 }
1776         } else if (reset) {
1777                 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
1778         }
1779
1780         /* recalculate aliveness */
1781         alive = lnet_is_peer_ni_alive(lpni);
1782
1783         lp = lpni->lpni_peer_net->lpn_peer;
1784         /* If this is an LNet router then update route aliveness */
1785         if (lp->lp_rtr_refcount) {
1786                 if (reset)
1787                         /* reset flag indicates gateway peer went up or down */
1788                         lp->lp_alive = alive;
1789
1790                 /* If discovery is disabled, locally or on the gateway, then
1791                  * any routes using lpni as next-hop need to be updated
1792                  *
1793                  * NB: We can get many notifications while a route is down, so
1794                  * we try and avoid the expensive net_lock/EX here for the
1795                  * common case of receiving duplicate lnet_notify() calls (i.e.
1796                  * only grab EX lock when we actually need to update the route
1797                  * aliveness).
1798                  */
1799                 if (lnet_is_discovery_disabled(lp)) {
1800                         list_for_each_entry(route, &lp->lp_routes, lr_gwlist) {
1801                                 if (nid_same(&route->lr_nid, &lpni->lpni_nid))
1802                                         lnet_set_route_aliveness(route, alive);
1803                         }
1804                 }
1805         }
1806
1807         lnet_net_unlock(0);
1808
1809         if (ni != NULL && !alive)
1810                 lnet_notify_peer_down(ni, lnet_nid_to_nid4(&lpni->lpni_nid));
1811
1812         cpt = lpni->lpni_cpt;
1813         lnet_net_lock(cpt);
1814         lnet_peer_ni_decref_locked(lpni);
1815         lnet_net_unlock(cpt);
1816
1817         return 0;
1818 }
1819 EXPORT_SYMBOL(lnet_notify);