Whamcloud - gitweb
LU-9859 libcfs: replace cfs_rand() with prandom_u32_max()
[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 extern unsigned int lnet_current_net_count;
39
40 static char *forwarding = "";
41 module_param(forwarding, charp, 0444);
42 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
43
44 static int tiny_router_buffers;
45 module_param(tiny_router_buffers, int, 0444);
46 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
47 static int small_router_buffers;
48 module_param(small_router_buffers, int, 0444);
49 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
50 static int large_router_buffers;
51 module_param(large_router_buffers, int, 0444);
52 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
53 static int peer_buffer_credits;
54 module_param(peer_buffer_credits, int, 0444);
55 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
56
57 static int auto_down = 1;
58 module_param(auto_down, int, 0444);
59 MODULE_PARM_DESC(auto_down, "Automatically mark peers down on comms error");
60
61 int
62 lnet_peer_buffer_credits(struct lnet_net *net)
63 {
64         /* NI option overrides LNet default */
65         if (net->net_tunables.lct_peer_rtr_credits > 0)
66                 return net->net_tunables.lct_peer_rtr_credits;
67         if (peer_buffer_credits > 0)
68                 return peer_buffer_credits;
69
70         /* As an approximation, allow this peer the same number of router
71          * buffers as it is allowed outstanding sends */
72         return net->net_tunables.lct_peer_tx_credits;
73 }
74
75 static int check_routers_before_use;
76 module_param(check_routers_before_use, int, 0444);
77 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
78
79 int avoid_asym_router_failure = 1;
80 module_param(avoid_asym_router_failure, int, 0644);
81 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
82
83 int alive_router_check_interval = 60;
84 module_param(alive_router_check_interval, int, 0644);
85 MODULE_PARM_DESC(alive_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
86
87 static int router_ping_timeout = 50;
88 module_param(router_ping_timeout, int, 0644);
89 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
90
91 /*
92  * A value between 0 and 100. 0 meaning that even if router's interfaces
93  * have the worse health still consider the gateway usable.
94  * 100 means that at least one interface on the route's remote net is 100%
95  * healthy to consider the route alive.
96  * The default is set to 100 to ensure we maintain the original behavior.
97  */
98 unsigned int router_sensitivity_percentage = 100;
99 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
100 static struct kernel_param_ops param_ops_rtr_sensitivity = {
101         .set = rtr_sensitivity_set,
102         .get = param_get_int,
103 };
104 #define param_check_rtr_sensitivity(name, p) \
105                 __param_check(name, p, int)
106 #ifdef HAVE_KERNEL_PARAM_OPS
107 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
108 #else
109 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
110                   &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
111 #endif
112 MODULE_PARM_DESC(router_sensitivity_percentage,
113                 "How healthy a gateway should be to be used in percent");
114
115 static int
116 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
117 {
118         int rc;
119         unsigned *sen = (unsigned *)kp->arg;
120         unsigned long value;
121
122         rc = kstrtoul(val, 0, &value);
123         if (rc) {
124                 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
125                 return rc;
126         }
127
128         if (value < 0 || value > 100) {
129                 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
130                 return -EINVAL;
131         }
132
133         /*
134          * The purpose of locking the api_mutex here is to ensure that
135          * the correct value ends up stored properly.
136          */
137         mutex_lock(&the_lnet.ln_api_mutex);
138
139         *sen = value;
140
141         mutex_unlock(&the_lnet.ln_api_mutex);
142
143         return 0;
144 }
145
146 void
147 lnet_rtr_transfer_to_peer(struct lnet_peer *src, struct lnet_peer *target)
148 {
149         struct lnet_route *route;
150
151         lnet_net_lock(LNET_LOCK_EX);
152         target->lp_rtr_refcount += src->lp_rtr_refcount;
153         /* move the list of queued messages to the new peer */
154         list_splice_init(&src->lp_rtrq, &target->lp_rtrq);
155         /* move all the routes that reference the peer */
156         list_splice_init(&src->lp_routes, &target->lp_routes);
157         /* update all the routes to point to the new peer */
158         list_for_each_entry(route, &target->lp_routes, lr_gwlist)
159                 route->lr_gateway = target;
160         /* remove the old peer from the ln_routers list */
161         list_del_init(&src->lp_rtr_list);
162         /* add the new peer to the ln_routers list */
163         if (list_empty(&target->lp_rtr_list)) {
164                 lnet_peer_addref_locked(target);
165                 list_add_tail(&target->lp_rtr_list, &the_lnet.ln_routers);
166         }
167         /* reset the ref count on the old peer and decrement its ref count */
168         src->lp_rtr_refcount = 0;
169         lnet_peer_decref_locked(src);
170         /* update the router version */
171         the_lnet.ln_routers_version++;
172         lnet_net_unlock(LNET_LOCK_EX);
173 }
174
175 int
176 lnet_peers_start_down(void)
177 {
178         return check_routers_before_use;
179 }
180
181 /*
182  * A net is alive if at least one gateway NI on the network is alive.
183  */
184 static bool
185 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
186 {
187         struct lnet_peer_ni *lpni;
188
189         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
190                 if (lnet_is_peer_ni_alive(lpni))
191                         return true;
192         }
193
194         return false;
195 }
196
197 /*
198  * a gateway is alive only if all its nets are alive
199  * called with cpt lock held
200  */
201 bool lnet_is_gateway_alive(struct lnet_peer *gw)
202 {
203         struct lnet_peer_net *lpn;
204
205         list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
206                 if (!lnet_is_gateway_net_alive(lpn))
207                         return false;
208         }
209
210         return true;
211 }
212
213 /*
214  * lnet_is_route_alive() needs to be called with cpt lock held
215  * A route is alive if the gateway can route between the local network and
216  * the remote network of the route.
217  * This means at least one NI is alive on each of the local and remote
218  * networks of the gateway.
219  */
220 bool lnet_is_route_alive(struct lnet_route *route)
221 {
222         struct lnet_peer *gw = route->lr_gateway;
223         struct lnet_peer_net *llpn;
224         struct lnet_peer_net *rlpn;
225         bool route_alive;
226
227         /*
228          * check the gateway's interfaces on the route rnet to make sure
229          * that the gateway is viable.
230          */
231         llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
232         if (!llpn)
233                 return false;
234
235         route_alive = lnet_is_gateway_net_alive(llpn);
236
237         if (avoid_asym_router_failure) {
238                 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
239                 if (!rlpn)
240                         return false;
241                 route_alive = route_alive &&
242                               lnet_is_gateway_net_alive(rlpn);
243         }
244
245         if (!route_alive)
246                 return route_alive;
247
248         spin_lock(&gw->lp_lock);
249         if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
250                 if (gw->lp_rtr_refcount > 0)
251                         CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
252                                libcfs_nid2str(gw->lp_primary_nid));
253                 route_alive = false;
254         }
255         spin_unlock(&gw->lp_lock);
256
257         return route_alive;
258 }
259
260 void
261 lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
262                                struct lnet_peer *new_lp)
263 {
264         struct lnet_peer_ni *lpni;
265         struct lnet_route *route;
266
267         /*
268          * Although a route is correlated with a peer, but when it's added
269          * a specific NID is used. That NID refers to a peer_ni within
270          * a peer. There could be other peer_nis on the same net, which
271          * can be used to send to that gateway. However when we are
272          * consolidating gateways because of discovery, the nid used to
273          * add the route might've moved between gateway peers. In this
274          * case we want to move the route to the new gateway as well. The
275          * intent here is not to confuse the user who added the route.
276          */
277         list_for_each_entry(route, &orig_lp->lp_routes, lr_gwlist) {
278                 lpni = lnet_peer_get_ni_locked(orig_lp, route->lr_nid);
279                 if (!lpni) {
280                         lnet_net_lock(LNET_LOCK_EX);
281                         list_move(&route->lr_gwlist, &new_lp->lp_routes);
282                         lnet_net_unlock(LNET_LOCK_EX);
283                 }
284         }
285
286 }
287
288 void
289 lnet_router_discovery_complete(struct lnet_peer *lp)
290 {
291         struct lnet_peer_ni *lpni = NULL;
292
293         spin_lock(&lp->lp_lock);
294         lp->lp_state &= ~LNET_PEER_RTR_DISCOVERY;
295         spin_unlock(&lp->lp_lock);
296
297         /*
298          * Router discovery successful? All peer information would've been
299          * updated already. No need to do any more processing
300          */
301         if (!lp->lp_dc_error)
302                 return;
303         /*
304          * discovery failed? then we need to set the status of each lpni
305          * to DOWN. It will be updated the next time we discover the
306          * router. For router peer NIs not on local networks, we never send
307          * messages directly to them, so their health will always remain
308          * at maximum. We can only tell if they are up or down from the
309          * status returned in the PING response. If we fail to get that
310          * status in our scheduled router discovery, then we'll assume
311          * it's down until we're told otherwise.
312          */
313         CDEBUG(D_NET, "%s: Router discovery failed %d\n",
314                libcfs_nid2str(lp->lp_primary_nid), lp->lp_dc_error);
315         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
316                 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
317 }
318
319 static void
320 lnet_rtr_addref_locked(struct lnet_peer *lp)
321 {
322         LASSERT(lp->lp_rtr_refcount >= 0);
323
324         /* lnet_net_lock must be exclusively locked */
325         lp->lp_rtr_refcount++;
326         if (lp->lp_rtr_refcount == 1) {
327                 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
328                 /* addref for the_lnet.ln_routers */
329                 lnet_peer_addref_locked(lp);
330                 the_lnet.ln_routers_version++;
331         }
332 }
333
334 static void
335 lnet_rtr_decref_locked(struct lnet_peer *lp)
336 {
337         LASSERT(atomic_read(&lp->lp_refcount) > 0);
338         LASSERT(lp->lp_rtr_refcount > 0);
339
340         /* lnet_net_lock must be exclusively locked */
341         lp->lp_rtr_refcount--;
342         if (lp->lp_rtr_refcount == 0) {
343                 LASSERT(list_empty(&lp->lp_routes));
344
345                 list_del(&lp->lp_rtr_list);
346                 /* decref for the_lnet.ln_routers */
347                 lnet_peer_decref_locked(lp);
348                 the_lnet.ln_routers_version++;
349         }
350 }
351
352 struct lnet_remotenet *
353 lnet_find_rnet_locked(__u32 net)
354 {
355         struct lnet_remotenet *rnet;
356         struct list_head *tmp;
357         struct list_head *rn_list;
358
359         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
360
361         rn_list = lnet_net2rnethash(net);
362         list_for_each(tmp, rn_list) {
363                 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
364
365                 if (rnet->lrn_net == net)
366                         return rnet;
367         }
368         return NULL;
369 }
370
371 static void lnet_shuffle_seed(void)
372 {
373         static int seeded;
374         struct lnet_ni *ni = NULL;
375
376         if (seeded)
377                 return;
378
379         /* Nodes with small feet have little entropy
380          * the NID for this node gives the most entropy in the low bits */
381         while ((ni = lnet_get_next_ni_locked(NULL, ni)))
382                 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
383
384         seeded = 1;
385         return;
386 }
387
388 /* NB expects LNET_LOCK held */
389 static void
390 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
391 {
392         struct lnet_peer_net *lpn;
393         unsigned int offset = 0;
394         unsigned int len = 0;
395         struct list_head *e;
396
397         lnet_shuffle_seed();
398
399         list_for_each(e, &rnet->lrn_routes)
400                 len++;
401
402         /*
403          * Randomly adding routes to the list is done to ensure that when
404          * different nodes are using the same list of routers, they end up
405          * preferring different routers.
406          */
407         offset = prandom_u32_max(len + 1);
408         list_for_each(e, &rnet->lrn_routes) {
409                 if (offset == 0)
410                         break;
411                 offset--;
412         }
413         list_add(&route->lr_list, e);
414         /*
415          * force a router check on the gateway to make sure the route is
416          * alive
417          */
418         list_for_each_entry(lpn, &route->lr_gateway->lp_peer_nets,
419                             lpn_peer_nets) {
420                 lpn->lpn_rtrcheck_timestamp = 0;
421         }
422
423         the_lnet.ln_remote_nets_version++;
424
425         /* add the route on the gateway list */
426         list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
427
428         /* take a router reference count on the gateway */
429         lnet_rtr_addref_locked(route->lr_gateway);
430 }
431
432 int
433 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
434                __u32 priority, __u32 sensitivity)
435 {
436         struct list_head *route_entry;
437         struct lnet_remotenet *rnet;
438         struct lnet_remotenet *rnet2;
439         struct lnet_route *route;
440         struct lnet_peer_ni *lpni;
441         struct lnet_peer *gw;
442         int add_route;
443         int rc;
444
445         CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
446                libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
447
448         if (gateway == LNET_NID_ANY ||
449             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
450             net == LNET_NIDNET(LNET_NID_ANY) ||
451             LNET_NETTYP(net) == LOLND ||
452             LNET_NIDNET(gateway) == net ||
453             (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
454                 return -EINVAL;
455
456         /* it's a local network */
457         if (lnet_islocalnet(net))
458                 return -EEXIST;
459
460         if (!lnet_islocalnet(LNET_NIDNET(gateway))) {
461                 CERROR("Cannot add route with gateway %s. There is no local interface configured on LNet %s\n",
462                        libcfs_nid2str(gateway),
463                        libcfs_net2str(LNET_NIDNET(gateway)));
464                 return -EINVAL;
465         }
466
467         /* Assume net, route, all new */
468         LIBCFS_ALLOC(route, sizeof(*route));
469         LIBCFS_ALLOC(rnet, sizeof(*rnet));
470         if (route == NULL || rnet == NULL) {
471                 CERROR("Out of memory creating route %s %d %s\n",
472                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
473                 if (route != NULL)
474                         LIBCFS_FREE(route, sizeof(*route));
475                 if (rnet != NULL)
476                         LIBCFS_FREE(rnet, sizeof(*rnet));
477                 return -ENOMEM;
478         }
479
480         INIT_LIST_HEAD(&rnet->lrn_routes);
481         rnet->lrn_net = net;
482         /* store the local and remote net that the route represents */
483         route->lr_lnet = LNET_NIDNET(gateway);
484         route->lr_net = net;
485         route->lr_nid = gateway;
486         route->lr_priority = priority;
487         route->lr_hops = hops;
488
489         lnet_net_lock(LNET_LOCK_EX);
490
491         /*
492          * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
493          * lose that once we're done
494          */
495         lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
496         if (IS_ERR(lpni)) {
497                 lnet_net_unlock(LNET_LOCK_EX);
498
499                 LIBCFS_FREE(route, sizeof(*route));
500                 LIBCFS_FREE(rnet, sizeof(*rnet));
501
502                 rc = PTR_ERR(lpni);
503                 CERROR("Error %d creating route %s %d %s\n", rc,
504                         libcfs_net2str(net), hops,
505                         libcfs_nid2str(gateway));
506                 return rc;
507         }
508
509         LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
510         gw = lpni->lpni_peer_net->lpn_peer;
511
512         route->lr_gateway = gw;
513
514         rnet2 = lnet_find_rnet_locked(net);
515         if (rnet2 == NULL) {
516                 /* new network */
517                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
518                 rnet2 = rnet;
519         }
520
521         /* Search for a duplicate route (it's a NOOP if it is) */
522         add_route = 1;
523         list_for_each(route_entry, &rnet2->lrn_routes) {
524                 struct lnet_route *route2;
525
526                 route2 = list_entry(route_entry, struct lnet_route, lr_list);
527                 if (route2->lr_gateway == route->lr_gateway) {
528                         add_route = 0;
529                         break;
530                 }
531
532                 /* our lookups must be true */
533                 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
534         }
535
536         /*
537          * It is possible to add multiple routes through the same peer,
538          * but it'll be using a different NID of that peer. When the
539          * gateway is discovered, discovery will consolidate the different
540          * peers into one peer. In this case the discovery code will have
541          * to move the routes from the peer that's being deleted to the
542          * consolidated peer lp_routes list
543          */
544         if (add_route) {
545                 gw->lp_health_sensitivity = sensitivity;
546                 lnet_add_route_to_rnet(rnet2, route);
547         }
548
549         /*
550          * get rid of the reference on the lpni.
551          */
552         lnet_peer_ni_decref_locked(lpni);
553         lnet_net_unlock(LNET_LOCK_EX);
554
555         rc = 0;
556
557         if (!add_route) {
558                 rc = -EEXIST;
559                 LIBCFS_FREE(route, sizeof(*route));
560         }
561
562         if (rnet != rnet2)
563                 LIBCFS_FREE(rnet, sizeof(*rnet));
564
565         /* kick start the monitor thread to handle the added route */
566         wake_up(&the_lnet.ln_mt_waitq);
567
568         return rc;
569 }
570
571 static void
572 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
573                          struct list_head *zombies)
574 {
575         struct lnet_peer *gateway;
576         struct lnet_route *route;
577         struct lnet_route *tmp;
578
579         list_for_each_entry_safe(route, tmp, route_list, lr_list) {
580                 gateway = route->lr_gateway;
581                 if (gw_nid != LNET_NID_ANY &&
582                     gw_nid != gateway->lp_primary_nid)
583                         continue;
584
585                 /*
586                  * move to zombie to delete outside the lock
587                  * Note that this function is called with the
588                  * ln_api_mutex held as well as the exclusive net
589                  * lock. Adding to the remote net list happens
590                  * under the same conditions. Same goes for the
591                  * gateway router list
592                  */
593                 list_move(&route->lr_list, zombies);
594                 the_lnet.ln_remote_nets_version++;
595
596                 list_del(&route->lr_gwlist);
597                 lnet_rtr_decref_locked(gateway);
598         }
599 }
600
601 int
602 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
603 {
604         struct list_head rnet_zombies;
605         struct lnet_remotenet *rnet;
606         struct lnet_remotenet *tmp;
607         struct list_head *rn_list;
608         struct lnet_peer_ni *lpni;
609         struct lnet_route *route;
610         struct list_head zombies;
611         struct lnet_peer *lp;
612         int i = 0;
613
614         INIT_LIST_HEAD(&rnet_zombies);
615         INIT_LIST_HEAD(&zombies);
616
617         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
618                libcfs_net2str(net), libcfs_nid2str(gw_nid));
619
620         /* NB Caller may specify either all routes via the given gateway
621          * or a specific route entry actual NIDs) */
622
623         lnet_net_lock(LNET_LOCK_EX);
624
625         lpni = lnet_find_peer_ni_locked(gw_nid);
626         if (lpni) {
627                 lp = lpni->lpni_peer_net->lpn_peer;
628                 LASSERT(lp);
629                 gw_nid = lp->lp_primary_nid;
630                 lnet_peer_ni_decref_locked(lpni);
631         }
632
633         if (net != LNET_NIDNET(LNET_NID_ANY)) {
634                 rnet = lnet_find_rnet_locked(net);
635                 if (!rnet) {
636                         lnet_net_unlock(LNET_LOCK_EX);
637                         return -ENOENT;
638                 }
639                 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
640                                          &zombies);
641                 if (list_empty(&rnet->lrn_routes))
642                         list_move(&rnet->lrn_list, &rnet_zombies);
643                 goto delete_zombies;
644         }
645
646         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
647                 rn_list = &the_lnet.ln_remote_nets_hash[i];
648
649                 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
650                         lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
651                                                  &zombies);
652                         if (list_empty(&rnet->lrn_routes))
653                                 list_move(&rnet->lrn_list, &rnet_zombies);
654                 }
655         }
656
657 delete_zombies:
658         /*
659          * check if there are any routes remaining on the gateway
660          * If there are no more routes make sure to set the peer's
661          * lp_disc_net_id to 0 (invalid), in case we add more routes in
662          * the future on that gateway, then we start our discovery process
663          * from scratch
664          */
665         if (lpni) {
666                 if (list_empty(&lp->lp_routes))
667                         lp->lp_disc_net_id = 0;
668         }
669
670         lnet_net_unlock(LNET_LOCK_EX);
671
672         while (!list_empty(&zombies)) {
673                 route = list_first_entry(&zombies, struct lnet_route, lr_list);
674                 list_del(&route->lr_list);
675                 LIBCFS_FREE(route, sizeof(*route));
676         }
677
678         while (!list_empty(&rnet_zombies)) {
679                 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
680                                         lrn_list);
681                 list_del(&rnet->lrn_list);
682                 LIBCFS_FREE(rnet, sizeof(*rnet));
683         }
684
685         return 0;
686 }
687
688 void
689 lnet_destroy_routes (void)
690 {
691         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
692 }
693
694 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
695 {
696         struct lnet_rtrbufpool *rbp;
697         int i, rc = -ENOENT, j;
698
699         if (the_lnet.ln_rtrpools == NULL)
700                 return rc;
701
702
703         cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
704                 if (i != cpt)
705                         continue;
706
707                 lnet_net_lock(i);
708                 for (j = 0; j < LNET_NRBPOOLS; j++) {
709                         pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
710                         pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
711                         pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
712                         pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
713                 }
714                 lnet_net_unlock(i);
715                 rc = 0;
716                 break;
717         }
718
719         lnet_net_lock(LNET_LOCK_EX);
720         pool_cfg->pl_routing = the_lnet.ln_routing;
721         lnet_net_unlock(LNET_LOCK_EX);
722
723         return rc;
724 }
725
726 int
727 lnet_get_route(int idx, __u32 *net, __u32 *hops,
728                lnet_nid_t *gateway, __u32 *alive, __u32 *priority, __u32 *sensitivity)
729 {
730         struct lnet_remotenet *rnet;
731         struct list_head *rn_list;
732         struct lnet_route *route;
733         struct list_head *e1;
734         struct list_head *e2;
735         int cpt;
736         int i;
737
738         cpt = lnet_net_lock_current();
739
740         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
741                 rn_list = &the_lnet.ln_remote_nets_hash[i];
742                 list_for_each(e1, rn_list) {
743                         rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
744
745                         list_for_each(e2, &rnet->lrn_routes) {
746                                 route = list_entry(e2, struct lnet_route,
747                                                    lr_list);
748
749                                 if (idx-- == 0) {
750                                         *net      = rnet->lrn_net;
751                                         *gateway  = route->lr_nid;
752                                         *hops     = route->lr_hops;
753                                         *priority = route->lr_priority;
754                                         *sensitivity = route->lr_gateway->
755                                                 lp_health_sensitivity;
756                                         *alive    = lnet_is_route_alive(route);
757                                         lnet_net_unlock(cpt);
758                                         return 0;
759                                 }
760                         }
761                 }
762         }
763
764         lnet_net_unlock(cpt);
765         return -ENOENT;
766 }
767
768 static void
769 lnet_wait_known_routerstate(void)
770 {
771         struct lnet_peer *rtr;
772         struct list_head *entry;
773         int all_known;
774
775         LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
776
777         for (;;) {
778                 int cpt = lnet_net_lock_current();
779
780                 all_known = 1;
781                 list_for_each(entry, &the_lnet.ln_routers) {
782                         rtr = list_entry(entry, struct lnet_peer,
783                                          lp_rtr_list);
784
785                         spin_lock(&rtr->lp_lock);
786
787                         if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
788                                 all_known = 0;
789                                 spin_unlock(&rtr->lp_lock);
790                                 break;
791                         }
792                         spin_unlock(&rtr->lp_lock);
793                 }
794
795                 lnet_net_unlock(cpt);
796
797                 if (all_known)
798                         return;
799
800                 set_current_state(TASK_UNINTERRUPTIBLE);
801                 schedule_timeout(cfs_time_seconds(1));
802         }
803 }
804
805 static inline bool
806 lnet_net_set_status_locked(struct lnet_net *net, __u32 status)
807 {
808         struct lnet_ni *ni;
809         bool update = false;
810
811         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
812                 lnet_ni_lock(ni);
813                 if (ni->ni_status &&
814                     ni->ni_status->ns_status != status) {
815                     ni->ni_status->ns_status = status;
816                     update = true;
817                 }
818                 lnet_ni_unlock(ni);
819         }
820
821         return update;
822 }
823
824 static bool
825 lnet_update_ni_status_locked(void)
826 {
827         struct lnet_net *net;
828         bool push = false;
829         time64_t now;
830         time64_t timeout;
831
832         LASSERT(the_lnet.ln_routing);
833
834         timeout = router_ping_timeout + alive_router_check_interval;
835
836         now = ktime_get_real_seconds();
837         list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
838                 if (net->net_lnd->lnd_type == LOLND)
839                         continue;
840
841                 if (now < net->net_last_alive + timeout)
842                         continue;
843
844                 spin_lock(&net->net_lock);
845                 /* re-check with lock */
846                 if (now < net->net_last_alive + timeout) {
847                         spin_unlock(&net->net_lock);
848                         continue;
849                 }
850                 spin_unlock(&net->net_lock);
851
852                 /*
853                  * if the net didn't receive any traffic for past the
854                  * timeout on any of its constituent NIs, then mark all
855                  * the NIs down.
856                  */
857                 push = lnet_net_set_status_locked(net, LNET_NI_STATUS_DOWN);
858         }
859
860         return push;
861 }
862
863 void lnet_wait_router_start(void)
864 {
865         if (check_routers_before_use) {
866                 /* Note that a helpful side-effect of pinging all known routers
867                  * at startup is that it makes them drop stale connections they
868                  * may have to a previous instance of me. */
869                 lnet_wait_known_routerstate();
870         }
871 }
872
873 /*
874  * This function is called from the monitor thread to check if there are
875  * any active routers that need to be checked.
876  */
877 inline bool
878 lnet_router_checker_active(void)
879 {
880         /* Router Checker thread needs to run when routing is enabled in
881          * order to call lnet_update_ni_status_locked() */
882         if (the_lnet.ln_routing)
883                 return true;
884
885         return !list_empty(&the_lnet.ln_routers) &&
886                 alive_router_check_interval > 0;
887 }
888
889 void
890 lnet_check_routers(void)
891 {
892         struct lnet_peer_net *first_lpn = NULL;
893         struct lnet_peer_net *lpn;
894         struct lnet_peer_ni *lpni;
895         struct list_head *entry;
896         struct lnet_peer *rtr;
897         bool push = false;
898         bool found_lpn;
899         __u64 version;
900         __u32 net_id;
901         time64_t now;
902         int cpt;
903         int rc;
904
905         cpt = lnet_net_lock_current();
906 rescan:
907         version = the_lnet.ln_routers_version;
908
909         list_for_each(entry, &the_lnet.ln_routers) {
910                 rtr = list_entry(entry, struct lnet_peer,
911                                  lp_rtr_list);
912
913                 now = ktime_get_real_seconds();
914
915                 /*
916                  * only discover the router if we've passed
917                  * alive_router_check_interval seconds. Some of the router
918                  * interfaces could be down and in that case they would be
919                  * undergoing recovery separately from this discovery.
920                  */
921                 /* find next peer net which is also local */
922                 net_id = rtr->lp_disc_net_id;
923                 do {
924                         lpn = lnet_get_next_peer_net_locked(rtr, net_id);
925                         if (!lpn) {
926                                 CERROR("gateway %s has no networks\n",
927                                 libcfs_nid2str(rtr->lp_primary_nid));
928                                 break;
929                         }
930                         if (first_lpn == lpn)
931                                 break;
932                         if (!first_lpn)
933                                 first_lpn = lpn;
934                         found_lpn = lnet_islocalnet_locked(lpn->lpn_net_id);
935                         net_id = lpn->lpn_net_id;
936                 } while (!found_lpn);
937
938                 if (!found_lpn || !lpn) {
939                         CERROR("no local network found for gateway %s\n",
940                                libcfs_nid2str(rtr->lp_primary_nid));
941                         continue;
942                 }
943
944                 if (now - lpn->lpn_rtrcheck_timestamp <
945                     alive_router_check_interval / lnet_current_net_count)
946                        continue;
947
948                 /*
949                  * If we're currently discovering the peer then don't
950                  * issue another discovery
951                  */
952                 spin_lock(&rtr->lp_lock);
953                 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY) {
954                         spin_unlock(&rtr->lp_lock);
955                         continue;
956                 }
957                 /* make sure we actively discover the router */
958                 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
959                 rtr->lp_state |= LNET_PEER_RTR_DISCOVERY;
960                 spin_unlock(&rtr->lp_lock);
961
962                 /* find the peer_ni associated with the primary NID */
963                 lpni = lnet_peer_get_ni_locked(rtr, rtr->lp_primary_nid);
964                 if (!lpni) {
965                         CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
966                                libcfs_nid2str(rtr->lp_primary_nid));
967                         continue;
968                 }
969                 lnet_peer_ni_addref_locked(lpni);
970
971                 /* specify the net to use */
972                 rtr->lp_disc_net_id = lpn->lpn_net_id;
973
974                 /* discover the router */
975                 CDEBUG(D_NET, "discover %s, cpt = %d\n",
976                        libcfs_nid2str(lpni->lpni_nid), cpt);
977                 rc = lnet_discover_peer_locked(lpni, cpt, false);
978
979                 /* decrement ref count acquired by find_peer_ni_locked() */
980                 lnet_peer_ni_decref_locked(lpni);
981
982                 if (!rc)
983                         lpn->lpn_rtrcheck_timestamp = now;
984                 else
985                         CERROR("Failed to discover router %s\n",
986                                libcfs_nid2str(rtr->lp_primary_nid));
987
988                 /* NB dropped lock */
989                 if (version != the_lnet.ln_routers_version) {
990                         /* the routers list has changed */
991                         goto rescan;
992                 }
993         }
994
995         if (the_lnet.ln_routing)
996                 push = lnet_update_ni_status_locked();
997
998         lnet_net_unlock(cpt);
999
1000         /* if the status of the ni changed update the peers */
1001         if (push)
1002                 lnet_push_update_to_peers(1);
1003 }
1004
1005 void
1006 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1007 {
1008         int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1009
1010         while (--npages >= 0)
1011                 __free_page(rb->rb_kiov[npages].kiov_page);
1012
1013         LIBCFS_FREE(rb, sz);
1014 }
1015
1016 static struct lnet_rtrbuf *
1017 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1018 {
1019         int            npages = rbp->rbp_npages;
1020         int            sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1021         struct page   *page;
1022         struct lnet_rtrbuf *rb;
1023         int            i;
1024
1025         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1026         if (rb == NULL)
1027                 return NULL;
1028
1029         rb->rb_pool = rbp;
1030
1031         for (i = 0; i < npages; i++) {
1032                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1033                                           GFP_KERNEL | __GFP_ZERO);
1034                 if (page == NULL) {
1035                         while (--i >= 0)
1036                                 __free_page(rb->rb_kiov[i].kiov_page);
1037
1038                         LIBCFS_FREE(rb, sz);
1039                         return NULL;
1040                 }
1041
1042                 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
1043                 rb->rb_kiov[i].kiov_offset = 0;
1044                 rb->rb_kiov[i].kiov_page = page;
1045         }
1046
1047         return rb;
1048 }
1049
1050 static void
1051 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1052 {
1053         int npages = rbp->rbp_npages;
1054         struct lnet_rtrbuf *rb;
1055         struct list_head tmp;
1056
1057         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1058                 return;
1059
1060         INIT_LIST_HEAD(&tmp);
1061
1062         lnet_net_lock(cpt);
1063         list_splice_init(&rbp->rbp_msgs, &tmp);
1064         lnet_drop_routed_msgs_locked(&tmp, cpt);
1065         list_splice_init(&rbp->rbp_bufs, &tmp);
1066         rbp->rbp_req_nbuffers = 0;
1067         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1068         rbp->rbp_mincredits = 0;
1069         lnet_net_unlock(cpt);
1070
1071         /* Free buffers on the free list. */
1072         while (!list_empty(&tmp)) {
1073                 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1074                 list_del(&rb->rb_list);
1075                 lnet_destroy_rtrbuf(rb, npages);
1076         }
1077 }
1078
1079 static int
1080 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1081 {
1082         struct list_head rb_list;
1083         struct lnet_rtrbuf *rb;
1084         int             num_rb;
1085         int             num_buffers = 0;
1086         int             old_req_nbufs;
1087         int             npages = rbp->rbp_npages;
1088
1089         lnet_net_lock(cpt);
1090         /* If we are called for less buffers than already in the pool, we
1091          * just lower the req_nbuffers number and excess buffers will be
1092          * thrown away as they are returned to the free list.  Credits
1093          * then get adjusted as well.
1094          * If we already have enough buffers allocated to serve the
1095          * increase requested, then we can treat that the same way as we
1096          * do the decrease. */
1097         num_rb = nbufs - rbp->rbp_nbuffers;
1098         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1099                 rbp->rbp_req_nbuffers = nbufs;
1100                 lnet_net_unlock(cpt);
1101                 return 0;
1102         }
1103         /* store the older value of rbp_req_nbuffers and then set it to
1104          * the new request to prevent lnet_return_rx_credits_locked() from
1105          * freeing buffers that we need to keep around */
1106         old_req_nbufs = rbp->rbp_req_nbuffers;
1107         rbp->rbp_req_nbuffers = nbufs;
1108         lnet_net_unlock(cpt);
1109
1110         INIT_LIST_HEAD(&rb_list);
1111
1112         /* allocate the buffers on a local list first.  If all buffers are
1113          * allocated successfully then join this list to the rbp buffer
1114          * list.  If not then free all allocated buffers. */
1115         while (num_rb-- > 0) {
1116                 rb = lnet_new_rtrbuf(rbp, cpt);
1117                 if (rb == NULL) {
1118                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1119                                nbufs, npages);
1120
1121                         lnet_net_lock(cpt);
1122                         rbp->rbp_req_nbuffers = old_req_nbufs;
1123                         lnet_net_unlock(cpt);
1124
1125                         goto failed;
1126                 }
1127
1128                 list_add(&rb->rb_list, &rb_list);
1129                 num_buffers++;
1130         }
1131
1132         lnet_net_lock(cpt);
1133
1134         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1135         rbp->rbp_nbuffers += num_buffers;
1136         rbp->rbp_credits += num_buffers;
1137         rbp->rbp_mincredits = rbp->rbp_credits;
1138         /* We need to schedule blocked msg using the newly
1139          * added buffers. */
1140         while (!list_empty(&rbp->rbp_bufs) &&
1141                !list_empty(&rbp->rbp_msgs))
1142                 lnet_schedule_blocked_locked(rbp);
1143
1144         lnet_net_unlock(cpt);
1145
1146         return 0;
1147
1148 failed:
1149         while (!list_empty(&rb_list)) {
1150                 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1151                 list_del(&rb->rb_list);
1152                 lnet_destroy_rtrbuf(rb, npages);
1153         }
1154
1155         return -ENOMEM;
1156 }
1157
1158 static void
1159 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1160 {
1161         INIT_LIST_HEAD(&rbp->rbp_msgs);
1162         INIT_LIST_HEAD(&rbp->rbp_bufs);
1163
1164         rbp->rbp_npages = npages;
1165         rbp->rbp_credits = 0;
1166         rbp->rbp_mincredits = 0;
1167 }
1168
1169 void
1170 lnet_rtrpools_free(int keep_pools)
1171 {
1172         struct lnet_rtrbufpool *rtrp;
1173         int               i;
1174
1175         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1176                 return;
1177
1178         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1179                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1180                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1181                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1182         }
1183
1184         if (!keep_pools) {
1185                 cfs_percpt_free(the_lnet.ln_rtrpools);
1186                 the_lnet.ln_rtrpools = NULL;
1187         }
1188 }
1189
1190 static int
1191 lnet_nrb_tiny_calculate(void)
1192 {
1193         int     nrbs = LNET_NRB_TINY;
1194
1195         if (tiny_router_buffers < 0) {
1196                 LCONSOLE_ERROR_MSG(0x10c,
1197                                    "tiny_router_buffers=%d invalid when "
1198                                    "routing enabled\n", tiny_router_buffers);
1199                 return -EINVAL;
1200         }
1201
1202         if (tiny_router_buffers > 0)
1203                 nrbs = tiny_router_buffers;
1204
1205         nrbs /= LNET_CPT_NUMBER;
1206         return max(nrbs, LNET_NRB_TINY_MIN);
1207 }
1208
1209 static int
1210 lnet_nrb_small_calculate(void)
1211 {
1212         int     nrbs = LNET_NRB_SMALL;
1213
1214         if (small_router_buffers < 0) {
1215                 LCONSOLE_ERROR_MSG(0x10c,
1216                                    "small_router_buffers=%d invalid when "
1217                                    "routing enabled\n", small_router_buffers);
1218                 return -EINVAL;
1219         }
1220
1221         if (small_router_buffers > 0)
1222                 nrbs = small_router_buffers;
1223
1224         nrbs /= LNET_CPT_NUMBER;
1225         return max(nrbs, LNET_NRB_SMALL_MIN);
1226 }
1227
1228 static int
1229 lnet_nrb_large_calculate(void)
1230 {
1231         int     nrbs = LNET_NRB_LARGE;
1232
1233         if (large_router_buffers < 0) {
1234                 LCONSOLE_ERROR_MSG(0x10c,
1235                                    "large_router_buffers=%d invalid when "
1236                                    "routing enabled\n", large_router_buffers);
1237                 return -EINVAL;
1238         }
1239
1240         if (large_router_buffers > 0)
1241                 nrbs = large_router_buffers;
1242
1243         nrbs /= LNET_CPT_NUMBER;
1244         return max(nrbs, LNET_NRB_LARGE_MIN);
1245 }
1246
1247 int
1248 lnet_rtrpools_alloc(int im_a_router)
1249 {
1250         struct lnet_rtrbufpool *rtrp;
1251         int     nrb_tiny;
1252         int     nrb_small;
1253         int     nrb_large;
1254         int     rc;
1255         int     i;
1256
1257         if (!strcmp(forwarding, "")) {
1258                 /* not set either way */
1259                 if (!im_a_router)
1260                         return 0;
1261         } else if (!strcmp(forwarding, "disabled")) {
1262                 /* explicitly disabled */
1263                 return 0;
1264         } else if (!strcmp(forwarding, "enabled")) {
1265                 /* explicitly enabled */
1266         } else {
1267                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1268                                    "'enabled' or 'disabled'\n");
1269                 return -EINVAL;
1270         }
1271
1272         nrb_tiny = lnet_nrb_tiny_calculate();
1273         if (nrb_tiny < 0)
1274                 return -EINVAL;
1275
1276         nrb_small = lnet_nrb_small_calculate();
1277         if (nrb_small < 0)
1278                 return -EINVAL;
1279
1280         nrb_large = lnet_nrb_large_calculate();
1281         if (nrb_large < 0)
1282                 return -EINVAL;
1283
1284         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1285                                                 LNET_NRBPOOLS *
1286                                                 sizeof(struct lnet_rtrbufpool));
1287         if (the_lnet.ln_rtrpools == NULL) {
1288                 LCONSOLE_ERROR_MSG(0x10c,
1289                                    "Failed to initialize router buffe pool\n");
1290                 return -ENOMEM;
1291         }
1292
1293         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1294                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1295                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1296                                               nrb_tiny, i);
1297                 if (rc != 0)
1298                         goto failed;
1299
1300                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1301                                   LNET_NRB_SMALL_PAGES);
1302                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1303                                               nrb_small, i);
1304                 if (rc != 0)
1305                         goto failed;
1306
1307                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1308                                   LNET_NRB_LARGE_PAGES);
1309                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1310                                               nrb_large, i);
1311                 if (rc != 0)
1312                         goto failed;
1313         }
1314
1315         lnet_net_lock(LNET_LOCK_EX);
1316         the_lnet.ln_routing = 1;
1317         lnet_net_unlock(LNET_LOCK_EX);
1318         wake_up(&the_lnet.ln_mt_waitq);
1319         return 0;
1320
1321  failed:
1322         lnet_rtrpools_free(0);
1323         return rc;
1324 }
1325
1326 static int
1327 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1328 {
1329         int nrb = 0;
1330         int rc = 0;
1331         int i;
1332         struct lnet_rtrbufpool *rtrp;
1333
1334         /* If the provided values for each buffer pool are different than the
1335          * configured values, we need to take action. */
1336         if (tiny >= 0) {
1337                 tiny_router_buffers = tiny;
1338                 nrb = lnet_nrb_tiny_calculate();
1339                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1340                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1341                                                       nrb, i);
1342                         if (rc != 0)
1343                                 return rc;
1344                 }
1345         }
1346         if (small >= 0) {
1347                 small_router_buffers = small;
1348                 nrb = lnet_nrb_small_calculate();
1349                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1350                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1351                                                       nrb, i);
1352                         if (rc != 0)
1353                                 return rc;
1354                 }
1355         }
1356         if (large >= 0) {
1357                 large_router_buffers = large;
1358                 nrb = lnet_nrb_large_calculate();
1359                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1360                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1361                                                       nrb, i);
1362                         if (rc != 0)
1363                                 return rc;
1364                 }
1365         }
1366
1367         return 0;
1368 }
1369
1370 int
1371 lnet_rtrpools_adjust(int tiny, int small, int large)
1372 {
1373         /* this function doesn't revert the changes if adding new buffers
1374          * failed.  It's up to the user space caller to revert the
1375          * changes. */
1376
1377         if (!the_lnet.ln_routing)
1378                 return 0;
1379
1380         return lnet_rtrpools_adjust_helper(tiny, small, large);
1381 }
1382
1383 int
1384 lnet_rtrpools_enable(void)
1385 {
1386         int rc = 0;
1387
1388         if (the_lnet.ln_routing)
1389                 return 0;
1390
1391         if (the_lnet.ln_rtrpools == NULL)
1392                 /* If routing is turned off, and we have never
1393                  * initialized the pools before, just call the
1394                  * standard buffer pool allocation routine as
1395                  * if we are just configuring this for the first
1396                  * time. */
1397                 rc = lnet_rtrpools_alloc(1);
1398         else
1399                 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1400         if (rc != 0)
1401                 return rc;
1402
1403         lnet_net_lock(LNET_LOCK_EX);
1404         the_lnet.ln_routing = 1;
1405
1406         the_lnet.ln_ping_target->pb_info.pi_features &=
1407                 ~LNET_PING_FEAT_RTE_DISABLED;
1408         lnet_net_unlock(LNET_LOCK_EX);
1409
1410         return rc;
1411 }
1412
1413 void
1414 lnet_rtrpools_disable(void)
1415 {
1416         if (!the_lnet.ln_routing)
1417                 return;
1418
1419         lnet_net_lock(LNET_LOCK_EX);
1420         the_lnet.ln_routing = 0;
1421         the_lnet.ln_ping_target->pb_info.pi_features |=
1422                 LNET_PING_FEAT_RTE_DISABLED;
1423
1424         tiny_router_buffers = 0;
1425         small_router_buffers = 0;
1426         large_router_buffers = 0;
1427         lnet_net_unlock(LNET_LOCK_EX);
1428         lnet_rtrpools_free(1);
1429 }
1430
1431 static inline void
1432 lnet_notify_peer_down(struct lnet_ni *ni, lnet_nid_t nid)
1433 {
1434         if (ni->ni_net->net_lnd->lnd_notify_peer_down != NULL)
1435                 (ni->ni_net->net_lnd->lnd_notify_peer_down)(nid);
1436 }
1437
1438 /*
1439  * ni: local NI used to communicate with the peer
1440  * nid: peer NID
1441  * alive: true if peer is alive, false otherwise
1442  * reset: reset health value. This is requested by the LND.
1443  * when: notificaiton time.
1444  */
1445 int
1446 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, bool alive, bool reset,
1447             time64_t when)
1448 {
1449         struct lnet_peer_ni *lpni = NULL;
1450         time64_t now = ktime_get_seconds();
1451         int cpt;
1452
1453         LASSERT (!in_interrupt ());
1454
1455         CDEBUG (D_NET, "%s notifying %s: %s\n",
1456                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1457                 libcfs_nid2str(nid),
1458                 alive ? "up" : "down");
1459
1460         if (ni != NULL &&
1461             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1462                 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1463                       libcfs_nid2str(nid), alive ? "birth" : "death",
1464                       libcfs_nid2str(ni->ni_nid));
1465                 return -EINVAL;
1466         }
1467
1468         /* can't do predictions... */
1469         if (when > now) {
1470                 CWARN("Ignoring prediction from %s of %s %s "
1471                       "%lld seconds in the future\n",
1472                       (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1473                       libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1474                 return -EINVAL;
1475         }
1476
1477         if (ni != NULL && !alive &&             /* LND telling me she's down */
1478             !auto_down) {                       /* auto-down disabled */
1479                 CDEBUG(D_NET, "Auto-down disabled\n");
1480                 return 0;
1481         }
1482
1483         /* must lock 0 since this is used for synchronization */
1484         lnet_net_lock(0);
1485
1486         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1487                 lnet_net_unlock(0);
1488                 return -ESHUTDOWN;
1489         }
1490
1491         lpni = lnet_find_peer_ni_locked(nid);
1492         if (lpni == NULL) {
1493                 /* nid not found */
1494                 lnet_net_unlock(0);
1495                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1496                 return 0;
1497         }
1498
1499         if (alive) {
1500                 if (reset)
1501                         lnet_set_healthv(&lpni->lpni_healthv,
1502                                          LNET_MAX_HEALTH_VALUE);
1503                 else
1504                         lnet_inc_healthv(&lpni->lpni_healthv);
1505         } else {
1506                 lnet_handle_remote_failure_locked(lpni);
1507         }
1508
1509         /* recalculate aliveness */
1510         alive = lnet_is_peer_ni_alive(lpni);
1511         lnet_net_unlock(0);
1512
1513         if (ni != NULL && !alive)
1514                 lnet_notify_peer_down(ni, lpni->lpni_nid);
1515
1516         cpt = lpni->lpni_cpt;
1517         lnet_net_lock(cpt);
1518         lnet_peer_ni_decref_locked(lpni);
1519         lnet_net_unlock(cpt);
1520
1521         return 0;
1522 }
1523 EXPORT_SYMBOL(lnet_notify);