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