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