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