2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
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.
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.
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.
23 #define DEBUG_SUBSYSTEM S_LNET
25 #include <linux/random.h>
26 #include <lnet/lib-lnet.h>
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) >> \
38 static char *forwarding = "";
39 module_param(forwarding, charp, 0444);
40 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
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");
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");
60 lnet_peer_buffer_credits(struct lnet_net *net)
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;
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;
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");
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)");
81 int alive_router_check_interval = 60;
82 module_param(alive_router_check_interval, int, 0644);
83 MODULE_PARM_DESC(alive_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
85 static int router_ping_timeout = 50;
86 module_param(router_ping_timeout, int, 0644);
87 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
90 * A value between 0 and 100. 0 meaning that even if router's interfaces
91 * have the worse health still consider the gateway usable.
92 * 100 means that at least one interface on the route's remote net is 100%
93 * healthy to consider the route alive.
94 * The default is set to 100 to ensure we maintain the original behavior.
96 unsigned int router_sensitivity_percentage = 100;
97 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
98 static struct kernel_param_ops param_ops_rtr_sensitivity = {
99 .set = rtr_sensitivity_set,
100 .get = param_get_int,
102 #define param_check_rtr_sensitivity(name, p) \
103 __param_check(name, p, int)
104 #ifdef HAVE_KERNEL_PARAM_OPS
105 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
107 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
108 &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
110 MODULE_PARM_DESC(router_sensitivity_percentage,
111 "How healthy a gateway should be to be used in percent");
114 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
117 unsigned *sen = (unsigned *)kp->arg;
120 rc = kstrtoul(val, 0, &value);
122 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
126 if (value < 0 || value > 100) {
127 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
132 * The purpose of locking the api_mutex here is to ensure that
133 * the correct value ends up stored properly.
135 mutex_lock(&the_lnet.ln_api_mutex);
139 mutex_unlock(&the_lnet.ln_api_mutex);
145 lnet_peers_start_down(void)
147 return check_routers_before_use;
151 * A net is alive if at least one gateway NI on the network is alive.
154 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
156 struct lnet_peer_ni *lpni;
158 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
159 if (lnet_is_peer_ni_alive(lpni))
167 * a gateway is alive only if all its nets are alive
168 * called with cpt lock held
170 bool lnet_is_gateway_alive(struct lnet_peer *gw)
172 struct lnet_peer_net *lpn;
174 list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
175 if (!lnet_is_gateway_net_alive(lpn))
183 * lnet_is_route_alive() needs to be called with cpt lock held
184 * A route is alive if the gateway can route between the local network and
185 * the remote network of the route.
186 * This means at least one NI is alive on each of the local and remote
187 * networks of the gateway.
189 bool lnet_is_route_alive(struct lnet_route *route)
191 struct lnet_peer *gw = route->lr_gateway;
192 struct lnet_peer_net *llpn;
193 struct lnet_peer_net *rlpn;
197 * check the gateway's interfaces on the route rnet to make sure
198 * that the gateway is viable.
200 llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
204 route_alive = lnet_is_gateway_net_alive(llpn);
206 if (avoid_asym_router_failure) {
207 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
210 route_alive = route_alive &&
211 lnet_is_gateway_net_alive(rlpn);
217 spin_lock(&gw->lp_lock);
218 if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
219 if (gw->lp_rtr_refcount > 0)
220 CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
221 libcfs_nid2str(gw->lp_primary_nid));
224 spin_unlock(&gw->lp_lock);
230 lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
231 struct lnet_peer *new_lp)
233 struct lnet_peer_ni *lpni;
234 struct lnet_route *route;
237 * Although a route is correlated with a peer, but when it's added
238 * a specific NID is used. That NID refers to a peer_ni within
239 * a peer. There could be other peer_nis on the same net, which
240 * can be used to send to that gateway. However when we are
241 * consolidating gateways because of discovery, the nid used to
242 * add the route might've moved between gateway peers. In this
243 * case we want to move the route to the new gateway as well. The
244 * intent here is not to confuse the user who added the route.
246 list_for_each_entry(route, &orig_lp->lp_routes, lr_gwlist) {
247 lpni = lnet_peer_get_ni_locked(orig_lp, route->lr_nid);
249 lnet_net_lock(LNET_LOCK_EX);
250 list_move(&route->lr_gwlist, &new_lp->lp_routes);
251 lnet_net_unlock(LNET_LOCK_EX);
258 lnet_router_discovery_complete(struct lnet_peer *lp)
260 struct lnet_peer_ni *lpni = NULL;
262 spin_lock(&lp->lp_lock);
263 lp->lp_state &= ~LNET_PEER_RTR_DISCOVERY;
264 spin_unlock(&lp->lp_lock);
267 * Router discovery successful? All peer information would've been
268 * updated already. No need to do any more processing
270 if (!lp->lp_dc_error)
273 * discovery failed? then we need to set the status of each lpni
274 * to DOWN. It will be updated the next time we discover the
275 * router. For router peer NIs not on local networks, we never send
276 * messages directly to them, so their health will always remain
277 * at maximum. We can only tell if they are up or down from the
278 * status returned in the PING response. If we fail to get that
279 * status in our scheduled router discovery, then we'll assume
280 * it's down until we're told otherwise.
282 CDEBUG(D_NET, "%s: Router discovery failed %d\n",
283 libcfs_nid2str(lp->lp_primary_nid), lp->lp_dc_error);
284 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
285 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
289 lnet_rtr_addref_locked(struct lnet_peer *lp)
291 LASSERT(lp->lp_rtr_refcount >= 0);
293 /* lnet_net_lock must be exclusively locked */
294 lp->lp_rtr_refcount++;
295 if (lp->lp_rtr_refcount == 1) {
296 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
297 /* addref for the_lnet.ln_routers */
298 lnet_peer_addref_locked(lp);
299 the_lnet.ln_routers_version++;
304 lnet_rtr_decref_locked(struct lnet_peer *lp)
306 LASSERT(atomic_read(&lp->lp_refcount) > 0);
307 LASSERT(lp->lp_rtr_refcount > 0);
309 /* lnet_net_lock must be exclusively locked */
310 lp->lp_rtr_refcount--;
311 if (lp->lp_rtr_refcount == 0) {
312 LASSERT(list_empty(&lp->lp_routes));
314 list_del(&lp->lp_rtr_list);
315 /* decref for the_lnet.ln_routers */
316 lnet_peer_decref_locked(lp);
317 the_lnet.ln_routers_version++;
321 struct lnet_remotenet *
322 lnet_find_rnet_locked(__u32 net)
324 struct lnet_remotenet *rnet;
325 struct list_head *tmp;
326 struct list_head *rn_list;
328 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
330 rn_list = lnet_net2rnethash(net);
331 list_for_each(tmp, rn_list) {
332 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
334 if (rnet->lrn_net == net)
340 static void lnet_shuffle_seed(void)
343 struct lnet_ni *ni = NULL;
348 /* Nodes with small feet have little entropy
349 * the NID for this node gives the most entropy in the low bits */
350 while ((ni = lnet_get_next_ni_locked(NULL, ni)))
351 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
357 /* NB expects LNET_LOCK held */
359 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
361 unsigned int len = 0;
362 unsigned int offset = 0;
367 list_for_each(e, &rnet->lrn_routes)
371 * Randomly adding routes to the list is done to ensure that when
372 * different nodes are using the same list of routers, they end up
373 * preferring different routers.
375 offset = cfs_rand() % (len + 1);
376 list_for_each(e, &rnet->lrn_routes) {
381 list_add(&route->lr_list, e);
383 * force a router check on the gateway to make sure the route is
386 route->lr_gateway->lp_rtrcheck_timestamp = 0;
388 the_lnet.ln_remote_nets_version++;
390 /* add the route on the gateway list */
391 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
393 /* take a router reference count on the gateway */
394 lnet_rtr_addref_locked(route->lr_gateway);
398 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
399 unsigned int priority)
401 struct list_head *route_entry;
402 struct lnet_remotenet *rnet;
403 struct lnet_remotenet *rnet2;
404 struct lnet_route *route;
405 struct lnet_peer_ni *lpni;
406 struct lnet_peer *gw;
410 CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
411 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
413 if (gateway == LNET_NID_ANY ||
414 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
415 net == LNET_NIDNET(LNET_NID_ANY) ||
416 LNET_NETTYP(net) == LOLND ||
417 LNET_NIDNET(gateway) == net ||
418 (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
421 /* it's a local network */
422 if (lnet_islocalnet(net))
425 /* Assume net, route, all new */
426 LIBCFS_ALLOC(route, sizeof(*route));
427 LIBCFS_ALLOC(rnet, sizeof(*rnet));
428 if (route == NULL || rnet == NULL) {
429 CERROR("Out of memory creating route %s %d %s\n",
430 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
432 LIBCFS_FREE(route, sizeof(*route));
434 LIBCFS_FREE(rnet, sizeof(*rnet));
438 INIT_LIST_HEAD(&rnet->lrn_routes);
440 /* store the local and remote net that the route represents */
441 route->lr_lnet = LNET_NIDNET(gateway);
443 route->lr_nid = gateway;
444 route->lr_priority = priority;
445 route->lr_hops = hops;
447 lnet_net_lock(LNET_LOCK_EX);
450 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
451 * lose that once we're done
453 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
455 lnet_net_unlock(LNET_LOCK_EX);
457 LIBCFS_FREE(route, sizeof(*route));
458 LIBCFS_FREE(rnet, sizeof(*rnet));
461 CERROR("Error %d creating route %s %d %s\n", rc,
462 libcfs_net2str(net), hops,
463 libcfs_nid2str(gateway));
467 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
468 gw = lpni->lpni_peer_net->lpn_peer;
470 route->lr_gateway = gw;
472 rnet2 = lnet_find_rnet_locked(net);
475 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
479 /* Search for a duplicate route (it's a NOOP if it is) */
481 list_for_each(route_entry, &rnet2->lrn_routes) {
482 struct lnet_route *route2;
484 route2 = list_entry(route_entry, struct lnet_route, lr_list);
485 if (route2->lr_gateway == route->lr_gateway) {
490 /* our lookups must be true */
491 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
495 * It is possible to add multiple routes through the same peer,
496 * but it'll be using a different NID of that peer. When the
497 * gateway is discovered, discovery will consolidate the different
498 * peers into one peer. In this case the discovery code will have
499 * to move the routes from the peer that's being deleted to the
500 * consolidated peer lp_routes list
503 lnet_add_route_to_rnet(rnet2, route);
506 * get rid of the reference on the lpni.
508 lnet_peer_ni_decref_locked(lpni);
509 lnet_net_unlock(LNET_LOCK_EX);
515 LIBCFS_FREE(route, sizeof(*route));
519 LIBCFS_FREE(rnet, sizeof(*rnet));
521 /* kick start the monitor thread to handle the added route */
522 wake_up(&the_lnet.ln_mt_waitq);
528 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
529 struct list_head *zombies)
531 struct lnet_peer *gateway;
532 struct lnet_route *route;
533 struct lnet_route *tmp;
535 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
536 gateway = route->lr_gateway;
537 if (gw_nid != LNET_NID_ANY &&
538 gw_nid != gateway->lp_primary_nid)
542 * move to zombie to delete outside the lock
543 * Note that this function is called with the
544 * ln_api_mutex held as well as the exclusive net
545 * lock. Adding to the remote net list happens
546 * under the same conditions. Same goes for the
547 * gateway router list
549 list_move(&route->lr_list, zombies);
550 the_lnet.ln_remote_nets_version++;
552 list_del(&route->lr_gwlist);
553 lnet_rtr_decref_locked(gateway);
558 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
560 struct list_head rnet_zombies;
561 struct lnet_remotenet *rnet;
562 struct lnet_remotenet *tmp;
563 struct list_head *rn_list;
564 struct lnet_peer_ni *lpni;
565 struct lnet_route *route;
566 struct list_head zombies;
567 struct lnet_peer *lp;
570 INIT_LIST_HEAD(&rnet_zombies);
571 INIT_LIST_HEAD(&zombies);
573 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
574 libcfs_net2str(net), libcfs_nid2str(gw_nid));
576 /* NB Caller may specify either all routes via the given gateway
577 * or a specific route entry actual NIDs) */
579 lnet_net_lock(LNET_LOCK_EX);
581 lpni = lnet_find_peer_ni_locked(gw_nid);
583 lp = lpni->lpni_peer_net->lpn_peer;
585 gw_nid = lp->lp_primary_nid;
586 lnet_peer_ni_decref_locked(lpni);
589 if (net != LNET_NIDNET(LNET_NID_ANY)) {
590 rnet = lnet_find_rnet_locked(net);
592 lnet_net_unlock(LNET_LOCK_EX);
595 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
597 if (list_empty(&rnet->lrn_routes))
598 list_move(&rnet->lrn_list, &rnet_zombies);
602 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
603 rn_list = &the_lnet.ln_remote_nets_hash[i];
605 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
606 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
608 if (list_empty(&rnet->lrn_routes))
609 list_move(&rnet->lrn_list, &rnet_zombies);
614 lnet_net_unlock(LNET_LOCK_EX);
616 while (!list_empty(&zombies)) {
617 route = list_first_entry(&zombies, struct lnet_route, lr_list);
618 list_del(&route->lr_list);
619 LIBCFS_FREE(route, sizeof(*route));
622 while (!list_empty(&rnet_zombies)) {
623 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
625 list_del(&rnet->lrn_list);
626 LIBCFS_FREE(rnet, sizeof(*rnet));
633 lnet_destroy_routes (void)
635 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
638 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
640 struct lnet_rtrbufpool *rbp;
641 int i, rc = -ENOENT, j;
643 if (the_lnet.ln_rtrpools == NULL)
647 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
652 for (j = 0; j < LNET_NRBPOOLS; j++) {
653 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
654 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
655 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
656 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
663 lnet_net_lock(LNET_LOCK_EX);
664 pool_cfg->pl_routing = the_lnet.ln_routing;
665 lnet_net_unlock(LNET_LOCK_EX);
671 lnet_get_route(int idx, __u32 *net, __u32 *hops,
672 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
674 struct list_head *e1;
675 struct list_head *e2;
676 struct lnet_remotenet *rnet;
677 struct lnet_route *route;
680 struct list_head *rn_list;
682 cpt = lnet_net_lock_current();
684 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
685 rn_list = &the_lnet.ln_remote_nets_hash[i];
686 list_for_each(e1, rn_list) {
687 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
689 list_for_each(e2, &rnet->lrn_routes) {
690 route = list_entry(e2, struct lnet_route,
694 *net = rnet->lrn_net;
695 *gateway = route->lr_nid;
696 *hops = route->lr_hops;
697 *priority = route->lr_priority;
698 *alive = lnet_is_route_alive(route);
699 lnet_net_unlock(cpt);
706 lnet_net_unlock(cpt);
711 lnet_wait_known_routerstate(void)
713 struct lnet_peer *rtr;
714 struct list_head *entry;
717 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
720 int cpt = lnet_net_lock_current();
723 list_for_each(entry, &the_lnet.ln_routers) {
724 rtr = list_entry(entry, struct lnet_peer,
727 spin_lock(&rtr->lp_lock);
729 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
731 spin_unlock(&rtr->lp_lock);
734 spin_unlock(&rtr->lp_lock);
737 lnet_net_unlock(cpt);
742 set_current_state(TASK_UNINTERRUPTIBLE);
743 schedule_timeout(cfs_time_seconds(1));
748 lnet_update_ni_status_locked(void)
750 struct lnet_ni *ni = NULL;
754 LASSERT(the_lnet.ln_routing);
756 timeout = router_ping_timeout + alive_router_check_interval;
758 now = ktime_get_real_seconds();
759 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
760 if (ni->ni_net->net_lnd->lnd_type == LOLND)
763 if (now < ni->ni_last_alive + timeout)
767 /* re-check with lock */
768 if (now < ni->ni_last_alive + timeout) {
773 LASSERT(ni->ni_status != NULL);
775 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
776 CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
777 libcfs_nid2str(ni->ni_nid), timeout);
778 /* NB: so far, this is the only place to set
779 * NI status to "down" */
780 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
786 void lnet_wait_router_start(void)
788 if (check_routers_before_use) {
789 /* Note that a helpful side-effect of pinging all known routers
790 * at startup is that it makes them drop stale connections they
791 * may have to a previous instance of me. */
792 lnet_wait_known_routerstate();
797 * This function is called from the monitor thread to check if there are
798 * any active routers that need to be checked.
801 lnet_router_checker_active(void)
803 /* Router Checker thread needs to run when routing is enabled in
804 * order to call lnet_update_ni_status_locked() */
805 if (the_lnet.ln_routing)
808 return !list_empty(&the_lnet.ln_routers) &&
809 alive_router_check_interval > 0;
813 lnet_check_routers(void)
815 struct lnet_peer_ni *lpni;
816 struct list_head *entry;
817 struct lnet_peer *rtr;
823 cpt = lnet_net_lock_current();
825 version = the_lnet.ln_routers_version;
827 list_for_each(entry, &the_lnet.ln_routers) {
828 rtr = list_entry(entry, struct lnet_peer,
831 now = ktime_get_real_seconds();
834 * only discover the router if we've passed
835 * alive_router_check_interval seconds. Some of the router
836 * interfaces could be down and in that case they would be
837 * undergoing recovery separately from this discovery.
839 if (now - rtr->lp_rtrcheck_timestamp <
840 alive_router_check_interval)
844 * If we're currently discovering the peer then don't
845 * issue another discovery
847 spin_lock(&rtr->lp_lock);
848 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY) {
849 spin_unlock(&rtr->lp_lock);
852 /* make sure we actively discover the router */
853 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
854 rtr->lp_state |= LNET_PEER_RTR_DISCOVERY;
855 spin_unlock(&rtr->lp_lock);
857 /* find the peer_ni associated with the primary NID */
858 lpni = lnet_peer_get_ni_locked(rtr, rtr->lp_primary_nid);
860 CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
861 libcfs_nid2str(rtr->lp_primary_nid));
864 lnet_peer_ni_addref_locked(lpni);
866 /* discover the router */
867 CDEBUG(D_NET, "discover %s, cpt = %d\n",
868 libcfs_nid2str(lpni->lpni_nid), cpt);
869 rc = lnet_discover_peer_locked(lpni, cpt, false);
871 /* decrement ref count acquired by find_peer_ni_locked() */
872 lnet_peer_ni_decref_locked(lpni);
875 rtr->lp_rtrcheck_timestamp = now;
877 CERROR("Failed to discover router %s\n",
878 libcfs_nid2str(rtr->lp_primary_nid));
880 /* NB dropped lock */
881 if (version != the_lnet.ln_routers_version) {
882 /* the routers list has changed */
887 if (the_lnet.ln_routing)
888 lnet_update_ni_status_locked();
890 lnet_net_unlock(cpt);
894 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
896 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
898 while (--npages >= 0)
899 __free_page(rb->rb_kiov[npages].kiov_page);
904 static struct lnet_rtrbuf *
905 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
907 int npages = rbp->rbp_npages;
908 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
910 struct lnet_rtrbuf *rb;
913 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
919 for (i = 0; i < npages; i++) {
920 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
921 GFP_KERNEL | __GFP_ZERO);
924 __free_page(rb->rb_kiov[i].kiov_page);
930 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
931 rb->rb_kiov[i].kiov_offset = 0;
932 rb->rb_kiov[i].kiov_page = page;
939 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
941 int npages = rbp->rbp_npages;
942 struct lnet_rtrbuf *rb;
943 struct list_head tmp;
945 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
948 INIT_LIST_HEAD(&tmp);
951 list_splice_init(&rbp->rbp_msgs, &tmp);
952 lnet_drop_routed_msgs_locked(&tmp, cpt);
953 list_splice_init(&rbp->rbp_bufs, &tmp);
954 rbp->rbp_req_nbuffers = 0;
955 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
956 rbp->rbp_mincredits = 0;
957 lnet_net_unlock(cpt);
959 /* Free buffers on the free list. */
960 while (!list_empty(&tmp)) {
961 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
962 list_del(&rb->rb_list);
963 lnet_destroy_rtrbuf(rb, npages);
968 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
970 struct list_head rb_list;
971 struct lnet_rtrbuf *rb;
975 int npages = rbp->rbp_npages;
978 /* If we are called for less buffers than already in the pool, we
979 * just lower the req_nbuffers number and excess buffers will be
980 * thrown away as they are returned to the free list. Credits
981 * then get adjusted as well.
982 * If we already have enough buffers allocated to serve the
983 * increase requested, then we can treat that the same way as we
984 * do the decrease. */
985 num_rb = nbufs - rbp->rbp_nbuffers;
986 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
987 rbp->rbp_req_nbuffers = nbufs;
988 lnet_net_unlock(cpt);
991 /* store the older value of rbp_req_nbuffers and then set it to
992 * the new request to prevent lnet_return_rx_credits_locked() from
993 * freeing buffers that we need to keep around */
994 old_req_nbufs = rbp->rbp_req_nbuffers;
995 rbp->rbp_req_nbuffers = nbufs;
996 lnet_net_unlock(cpt);
998 INIT_LIST_HEAD(&rb_list);
1000 /* allocate the buffers on a local list first. If all buffers are
1001 * allocated successfully then join this list to the rbp buffer
1002 * list. If not then free all allocated buffers. */
1003 while (num_rb-- > 0) {
1004 rb = lnet_new_rtrbuf(rbp, cpt);
1006 CERROR("Failed to allocate %d route bufs of %d pages\n",
1010 rbp->rbp_req_nbuffers = old_req_nbufs;
1011 lnet_net_unlock(cpt);
1016 list_add(&rb->rb_list, &rb_list);
1022 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1023 rbp->rbp_nbuffers += num_buffers;
1024 rbp->rbp_credits += num_buffers;
1025 rbp->rbp_mincredits = rbp->rbp_credits;
1026 /* We need to schedule blocked msg using the newly
1028 while (!list_empty(&rbp->rbp_bufs) &&
1029 !list_empty(&rbp->rbp_msgs))
1030 lnet_schedule_blocked_locked(rbp);
1032 lnet_net_unlock(cpt);
1037 while (!list_empty(&rb_list)) {
1038 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1039 list_del(&rb->rb_list);
1040 lnet_destroy_rtrbuf(rb, npages);
1047 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1049 INIT_LIST_HEAD(&rbp->rbp_msgs);
1050 INIT_LIST_HEAD(&rbp->rbp_bufs);
1052 rbp->rbp_npages = npages;
1053 rbp->rbp_credits = 0;
1054 rbp->rbp_mincredits = 0;
1058 lnet_rtrpools_free(int keep_pools)
1060 struct lnet_rtrbufpool *rtrp;
1063 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1066 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1067 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1068 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1069 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1073 cfs_percpt_free(the_lnet.ln_rtrpools);
1074 the_lnet.ln_rtrpools = NULL;
1079 lnet_nrb_tiny_calculate(void)
1081 int nrbs = LNET_NRB_TINY;
1083 if (tiny_router_buffers < 0) {
1084 LCONSOLE_ERROR_MSG(0x10c,
1085 "tiny_router_buffers=%d invalid when "
1086 "routing enabled\n", tiny_router_buffers);
1090 if (tiny_router_buffers > 0)
1091 nrbs = tiny_router_buffers;
1093 nrbs /= LNET_CPT_NUMBER;
1094 return max(nrbs, LNET_NRB_TINY_MIN);
1098 lnet_nrb_small_calculate(void)
1100 int nrbs = LNET_NRB_SMALL;
1102 if (small_router_buffers < 0) {
1103 LCONSOLE_ERROR_MSG(0x10c,
1104 "small_router_buffers=%d invalid when "
1105 "routing enabled\n", small_router_buffers);
1109 if (small_router_buffers > 0)
1110 nrbs = small_router_buffers;
1112 nrbs /= LNET_CPT_NUMBER;
1113 return max(nrbs, LNET_NRB_SMALL_MIN);
1117 lnet_nrb_large_calculate(void)
1119 int nrbs = LNET_NRB_LARGE;
1121 if (large_router_buffers < 0) {
1122 LCONSOLE_ERROR_MSG(0x10c,
1123 "large_router_buffers=%d invalid when "
1124 "routing enabled\n", large_router_buffers);
1128 if (large_router_buffers > 0)
1129 nrbs = large_router_buffers;
1131 nrbs /= LNET_CPT_NUMBER;
1132 return max(nrbs, LNET_NRB_LARGE_MIN);
1136 lnet_rtrpools_alloc(int im_a_router)
1138 struct lnet_rtrbufpool *rtrp;
1145 if (!strcmp(forwarding, "")) {
1146 /* not set either way */
1149 } else if (!strcmp(forwarding, "disabled")) {
1150 /* explicitly disabled */
1152 } else if (!strcmp(forwarding, "enabled")) {
1153 /* explicitly enabled */
1155 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1156 "'enabled' or 'disabled'\n");
1160 nrb_tiny = lnet_nrb_tiny_calculate();
1164 nrb_small = lnet_nrb_small_calculate();
1168 nrb_large = lnet_nrb_large_calculate();
1172 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1174 sizeof(struct lnet_rtrbufpool));
1175 if (the_lnet.ln_rtrpools == NULL) {
1176 LCONSOLE_ERROR_MSG(0x10c,
1177 "Failed to initialize router buffe pool\n");
1181 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1182 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1183 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1188 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1189 LNET_NRB_SMALL_PAGES);
1190 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1195 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1196 LNET_NRB_LARGE_PAGES);
1197 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1203 lnet_net_lock(LNET_LOCK_EX);
1204 the_lnet.ln_routing = 1;
1205 lnet_net_unlock(LNET_LOCK_EX);
1206 wake_up(&the_lnet.ln_mt_waitq);
1210 lnet_rtrpools_free(0);
1215 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1220 struct lnet_rtrbufpool *rtrp;
1222 /* If the provided values for each buffer pool are different than the
1223 * configured values, we need to take action. */
1225 tiny_router_buffers = tiny;
1226 nrb = lnet_nrb_tiny_calculate();
1227 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1228 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1235 small_router_buffers = small;
1236 nrb = lnet_nrb_small_calculate();
1237 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1238 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1245 large_router_buffers = large;
1246 nrb = lnet_nrb_large_calculate();
1247 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1248 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1259 lnet_rtrpools_adjust(int tiny, int small, int large)
1261 /* this function doesn't revert the changes if adding new buffers
1262 * failed. It's up to the user space caller to revert the
1265 if (!the_lnet.ln_routing)
1268 return lnet_rtrpools_adjust_helper(tiny, small, large);
1272 lnet_rtrpools_enable(void)
1276 if (the_lnet.ln_routing)
1279 if (the_lnet.ln_rtrpools == NULL)
1280 /* If routing is turned off, and we have never
1281 * initialized the pools before, just call the
1282 * standard buffer pool allocation routine as
1283 * if we are just configuring this for the first
1285 rc = lnet_rtrpools_alloc(1);
1287 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1291 lnet_net_lock(LNET_LOCK_EX);
1292 the_lnet.ln_routing = 1;
1294 the_lnet.ln_ping_target->pb_info.pi_features &=
1295 ~LNET_PING_FEAT_RTE_DISABLED;
1296 lnet_net_unlock(LNET_LOCK_EX);
1302 lnet_rtrpools_disable(void)
1304 if (!the_lnet.ln_routing)
1307 lnet_net_lock(LNET_LOCK_EX);
1308 the_lnet.ln_routing = 0;
1309 the_lnet.ln_ping_target->pb_info.pi_features |=
1310 LNET_PING_FEAT_RTE_DISABLED;
1312 tiny_router_buffers = 0;
1313 small_router_buffers = 0;
1314 large_router_buffers = 0;
1315 lnet_net_unlock(LNET_LOCK_EX);
1316 lnet_rtrpools_free(1);
1320 lnet_notify_peer_down(struct lnet_ni *ni, lnet_nid_t nid)
1322 if (ni->ni_net->net_lnd->lnd_notify_peer_down != NULL)
1323 (ni->ni_net->net_lnd->lnd_notify_peer_down)(nid);
1327 * ni: local NI used to communicate with the peer
1329 * alive: true if peer is alive, false otherwise
1330 * reset: reset health value. This is requested by the LND.
1331 * when: notificaiton time.
1334 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, bool alive, bool reset,
1337 struct lnet_peer_ni *lpni = NULL;
1338 time64_t now = ktime_get_seconds();
1341 LASSERT (!in_interrupt ());
1343 CDEBUG (D_NET, "%s notifying %s: %s\n",
1344 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1345 libcfs_nid2str(nid),
1346 alive ? "up" : "down");
1349 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1350 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1351 libcfs_nid2str(nid), alive ? "birth" : "death",
1352 libcfs_nid2str(ni->ni_nid));
1356 /* can't do predictions... */
1358 CWARN("Ignoring prediction from %s of %s %s "
1359 "%lld seconds in the future\n",
1360 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1361 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1365 if (ni != NULL && !alive && /* LND telling me she's down */
1366 !auto_down) { /* auto-down disabled */
1367 CDEBUG(D_NET, "Auto-down disabled\n");
1371 /* must lock 0 since this is used for synchronization */
1374 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1379 lpni = lnet_find_peer_ni_locked(nid);
1383 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1389 lnet_set_healthv(&lpni->lpni_healthv,
1390 LNET_MAX_HEALTH_VALUE);
1392 lnet_inc_healthv(&lpni->lpni_healthv);
1394 lnet_handle_remote_failure_locked(lpni);
1397 /* recalculate aliveness */
1398 alive = lnet_is_peer_ni_alive(lpni);
1401 if (ni != NULL && !alive)
1402 lnet_notify_peer_down(ni, lpni->lpni_nid);
1404 cpt = lpni->lpni_cpt;
1406 lnet_peer_ni_decref_locked(lpni);
1407 lnet_net_unlock(cpt);
1411 EXPORT_SYMBOL(lnet_notify);