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 extern unsigned int lnet_current_net_count;
40 static char *forwarding = "";
41 module_param(forwarding, charp, 0444);
42 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
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");
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");
62 lnet_peer_buffer_credits(struct lnet_net *net)
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;
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;
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");
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)");
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)");
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");
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.
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,
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);
109 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
110 &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
112 MODULE_PARM_DESC(router_sensitivity_percentage,
113 "How healthy a gateway should be to be used in percent");
116 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
119 unsigned *sen = (unsigned *)kp->arg;
122 rc = kstrtoul(val, 0, &value);
124 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
128 if (value < 0 || value > 100) {
129 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
134 * The purpose of locking the api_mutex here is to ensure that
135 * the correct value ends up stored properly.
137 mutex_lock(&the_lnet.ln_api_mutex);
141 mutex_unlock(&the_lnet.ln_api_mutex);
147 lnet_rtr_transfer_to_peer(struct lnet_peer *src, struct lnet_peer *target)
149 struct lnet_route *route;
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);
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);
176 lnet_peers_start_down(void)
178 return check_routers_before_use;
182 * A net is alive if at least one gateway NI on the network is alive.
185 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
187 struct lnet_peer_ni *lpni;
189 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
190 if (lnet_is_peer_ni_alive(lpni))
198 * a gateway is alive only if all its nets are alive
199 * called with cpt lock held
201 bool lnet_is_gateway_alive(struct lnet_peer *gw)
203 struct lnet_peer_net *lpn;
205 list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
206 if (!lnet_is_gateway_net_alive(lpn))
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.
220 bool lnet_is_route_alive(struct lnet_route *route)
222 struct lnet_peer *gw = route->lr_gateway;
223 struct lnet_peer_net *llpn;
224 struct lnet_peer_net *rlpn;
228 * check the gateway's interfaces on the route rnet to make sure
229 * that the gateway is viable.
231 llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
235 route_alive = lnet_is_gateway_net_alive(llpn);
237 if (avoid_asym_router_failure) {
238 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
241 route_alive = route_alive &&
242 lnet_is_gateway_net_alive(rlpn);
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));
255 spin_unlock(&gw->lp_lock);
261 lnet_consolidate_routes_locked(struct lnet_peer *orig_lp,
262 struct lnet_peer *new_lp)
264 struct lnet_peer_ni *lpni;
265 struct lnet_route *route;
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.
277 list_for_each_entry(route, &orig_lp->lp_routes, lr_gwlist) {
278 lpni = lnet_peer_get_ni_locked(orig_lp, route->lr_nid);
280 lnet_net_lock(LNET_LOCK_EX);
281 list_move(&route->lr_gwlist, &new_lp->lp_routes);
282 lnet_net_unlock(LNET_LOCK_EX);
289 lnet_router_discovery_complete(struct lnet_peer *lp)
291 struct lnet_peer_ni *lpni = NULL;
293 spin_lock(&lp->lp_lock);
294 lp->lp_state &= ~LNET_PEER_RTR_DISCOVERY;
295 spin_unlock(&lp->lp_lock);
298 * Router discovery successful? All peer information would've been
299 * updated already. No need to do any more processing
301 if (!lp->lp_dc_error)
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.
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;
320 lnet_rtr_addref_locked(struct lnet_peer *lp)
322 LASSERT(lp->lp_rtr_refcount >= 0);
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++;
335 lnet_rtr_decref_locked(struct lnet_peer *lp)
337 LASSERT(atomic_read(&lp->lp_refcount) > 0);
338 LASSERT(lp->lp_rtr_refcount > 0);
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));
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++;
352 struct lnet_remotenet *
353 lnet_find_rnet_locked(__u32 net)
355 struct lnet_remotenet *rnet;
356 struct list_head *tmp;
357 struct list_head *rn_list;
359 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
361 rn_list = lnet_net2rnethash(net);
362 list_for_each(tmp, rn_list) {
363 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
365 if (rnet->lrn_net == net)
371 static void lnet_shuffle_seed(void)
374 struct lnet_ni *ni = NULL;
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));
388 /* NB expects LNET_LOCK held */
390 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
392 struct lnet_peer_net *lpn;
393 unsigned int offset = 0;
394 unsigned int len = 0;
399 list_for_each(e, &rnet->lrn_routes)
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.
407 offset = prandom_u32_max(len + 1);
408 list_for_each(e, &rnet->lrn_routes) {
413 list_add(&route->lr_list, e);
415 * force a router check on the gateway to make sure the route is
418 list_for_each_entry(lpn, &route->lr_gateway->lp_peer_nets,
420 lpn->lpn_rtrcheck_timestamp = 0;
423 the_lnet.ln_remote_nets_version++;
425 /* add the route on the gateway list */
426 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
428 /* take a router reference count on the gateway */
429 lnet_rtr_addref_locked(route->lr_gateway);
433 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
434 __u32 priority, __u32 sensitivity)
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;
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));
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)))
456 /* it's a local network */
457 if (lnet_islocalnet(net))
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)));
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));
474 LIBCFS_FREE(route, sizeof(*route));
476 LIBCFS_FREE(rnet, sizeof(*rnet));
480 INIT_LIST_HEAD(&rnet->lrn_routes);
482 /* store the local and remote net that the route represents */
483 route->lr_lnet = LNET_NIDNET(gateway);
485 route->lr_nid = gateway;
486 route->lr_priority = priority;
487 route->lr_hops = hops;
489 lnet_net_lock(LNET_LOCK_EX);
492 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
493 * lose that once we're done
495 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
497 lnet_net_unlock(LNET_LOCK_EX);
499 LIBCFS_FREE(route, sizeof(*route));
500 LIBCFS_FREE(rnet, sizeof(*rnet));
503 CERROR("Error %d creating route %s %d %s\n", rc,
504 libcfs_net2str(net), hops,
505 libcfs_nid2str(gateway));
509 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
510 gw = lpni->lpni_peer_net->lpn_peer;
512 route->lr_gateway = gw;
514 rnet2 = lnet_find_rnet_locked(net);
517 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
521 /* Search for a duplicate route (it's a NOOP if it is) */
523 list_for_each(route_entry, &rnet2->lrn_routes) {
524 struct lnet_route *route2;
526 route2 = list_entry(route_entry, struct lnet_route, lr_list);
527 if (route2->lr_gateway == route->lr_gateway) {
532 /* our lookups must be true */
533 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
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
545 gw->lp_health_sensitivity = sensitivity;
546 lnet_add_route_to_rnet(rnet2, route);
547 if (lnet_peer_discovery_disabled)
548 CWARN("Consider turning discovery on to enable full "
549 "Multi-Rail routing functionality\n");
553 * get rid of the reference on the lpni.
555 lnet_peer_ni_decref_locked(lpni);
556 lnet_net_unlock(LNET_LOCK_EX);
562 LIBCFS_FREE(route, sizeof(*route));
566 LIBCFS_FREE(rnet, sizeof(*rnet));
568 /* kick start the monitor thread to handle the added route */
569 wake_up(&the_lnet.ln_mt_waitq);
575 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
576 struct list_head *zombies)
578 struct lnet_peer *gateway;
579 struct lnet_route *route;
580 struct lnet_route *tmp;
582 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
583 gateway = route->lr_gateway;
584 if (gw_nid != LNET_NID_ANY &&
585 gw_nid != gateway->lp_primary_nid)
589 * move to zombie to delete outside the lock
590 * Note that this function is called with the
591 * ln_api_mutex held as well as the exclusive net
592 * lock. Adding to the remote net list happens
593 * under the same conditions. Same goes for the
594 * gateway router list
596 list_move(&route->lr_list, zombies);
597 the_lnet.ln_remote_nets_version++;
599 list_del(&route->lr_gwlist);
600 lnet_rtr_decref_locked(gateway);
605 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
607 struct list_head rnet_zombies;
608 struct lnet_remotenet *rnet;
609 struct lnet_remotenet *tmp;
610 struct list_head *rn_list;
611 struct lnet_peer_ni *lpni;
612 struct lnet_route *route;
613 struct list_head zombies;
614 struct lnet_peer *lp;
617 INIT_LIST_HEAD(&rnet_zombies);
618 INIT_LIST_HEAD(&zombies);
620 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
621 libcfs_net2str(net), libcfs_nid2str(gw_nid));
623 /* NB Caller may specify either all routes via the given gateway
624 * or a specific route entry actual NIDs) */
626 lnet_net_lock(LNET_LOCK_EX);
628 lpni = lnet_find_peer_ni_locked(gw_nid);
630 lp = lpni->lpni_peer_net->lpn_peer;
632 gw_nid = lp->lp_primary_nid;
633 lnet_peer_ni_decref_locked(lpni);
636 if (net != LNET_NIDNET(LNET_NID_ANY)) {
637 rnet = lnet_find_rnet_locked(net);
639 lnet_net_unlock(LNET_LOCK_EX);
642 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
644 if (list_empty(&rnet->lrn_routes))
645 list_move(&rnet->lrn_list, &rnet_zombies);
649 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
650 rn_list = &the_lnet.ln_remote_nets_hash[i];
652 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
653 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
655 if (list_empty(&rnet->lrn_routes))
656 list_move(&rnet->lrn_list, &rnet_zombies);
662 * check if there are any routes remaining on the gateway
663 * If there are no more routes make sure to set the peer's
664 * lp_disc_net_id to 0 (invalid), in case we add more routes in
665 * the future on that gateway, then we start our discovery process
669 if (list_empty(&lp->lp_routes))
670 lp->lp_disc_net_id = 0;
673 lnet_net_unlock(LNET_LOCK_EX);
675 while (!list_empty(&zombies)) {
676 route = list_first_entry(&zombies, struct lnet_route, lr_list);
677 list_del(&route->lr_list);
678 LIBCFS_FREE(route, sizeof(*route));
681 while (!list_empty(&rnet_zombies)) {
682 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
684 list_del(&rnet->lrn_list);
685 LIBCFS_FREE(rnet, sizeof(*rnet));
692 lnet_destroy_routes (void)
694 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
697 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
699 struct lnet_rtrbufpool *rbp;
700 int i, rc = -ENOENT, j;
702 if (the_lnet.ln_rtrpools == NULL)
706 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
711 for (j = 0; j < LNET_NRBPOOLS; j++) {
712 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
713 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
714 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
715 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
722 lnet_net_lock(LNET_LOCK_EX);
723 pool_cfg->pl_routing = the_lnet.ln_routing;
724 lnet_net_unlock(LNET_LOCK_EX);
730 lnet_get_route(int idx, __u32 *net, __u32 *hops,
731 lnet_nid_t *gateway, __u32 *alive, __u32 *priority, __u32 *sensitivity)
733 struct lnet_remotenet *rnet;
734 struct list_head *rn_list;
735 struct lnet_route *route;
736 struct list_head *e1;
737 struct list_head *e2;
741 cpt = lnet_net_lock_current();
743 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
744 rn_list = &the_lnet.ln_remote_nets_hash[i];
745 list_for_each(e1, rn_list) {
746 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
748 list_for_each(e2, &rnet->lrn_routes) {
749 route = list_entry(e2, struct lnet_route,
753 *net = rnet->lrn_net;
754 *gateway = route->lr_nid;
755 *hops = route->lr_hops;
756 *priority = route->lr_priority;
757 *sensitivity = route->lr_gateway->
758 lp_health_sensitivity;
759 *alive = lnet_is_route_alive(route);
760 lnet_net_unlock(cpt);
767 lnet_net_unlock(cpt);
772 lnet_wait_known_routerstate(void)
774 struct lnet_peer *rtr;
775 struct list_head *entry;
778 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
781 int cpt = lnet_net_lock_current();
784 list_for_each(entry, &the_lnet.ln_routers) {
785 rtr = list_entry(entry, struct lnet_peer,
788 spin_lock(&rtr->lp_lock);
790 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
792 spin_unlock(&rtr->lp_lock);
795 spin_unlock(&rtr->lp_lock);
798 lnet_net_unlock(cpt);
803 set_current_state(TASK_UNINTERRUPTIBLE);
804 schedule_timeout(cfs_time_seconds(1));
809 lnet_net_set_status_locked(struct lnet_net *net, __u32 status)
814 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
817 ni->ni_status->ns_status != status) {
818 ni->ni_status->ns_status = status;
828 lnet_update_ni_status_locked(void)
830 struct lnet_net *net;
835 LASSERT(the_lnet.ln_routing);
837 timeout = router_ping_timeout + alive_router_check_interval;
839 now = ktime_get_real_seconds();
840 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
841 if (net->net_lnd->lnd_type == LOLND)
844 if (now < net->net_last_alive + timeout)
847 spin_lock(&net->net_lock);
848 /* re-check with lock */
849 if (now < net->net_last_alive + timeout) {
850 spin_unlock(&net->net_lock);
853 spin_unlock(&net->net_lock);
856 * if the net didn't receive any traffic for past the
857 * timeout on any of its constituent NIs, then mark all
860 push = lnet_net_set_status_locked(net, LNET_NI_STATUS_DOWN);
866 void lnet_wait_router_start(void)
868 if (check_routers_before_use) {
869 /* Note that a helpful side-effect of pinging all known routers
870 * at startup is that it makes them drop stale connections they
871 * may have to a previous instance of me. */
872 lnet_wait_known_routerstate();
877 * This function is called from the monitor thread to check if there are
878 * any active routers that need to be checked.
881 lnet_router_checker_active(void)
883 /* Router Checker thread needs to run when routing is enabled in
884 * order to call lnet_update_ni_status_locked() */
885 if (the_lnet.ln_routing)
888 return !list_empty(&the_lnet.ln_routers) &&
889 alive_router_check_interval > 0;
893 lnet_check_routers(void)
895 struct lnet_peer_net *first_lpn = NULL;
896 struct lnet_peer_net *lpn;
897 struct lnet_peer_ni *lpni;
898 struct list_head *entry;
899 struct lnet_peer *rtr;
908 cpt = lnet_net_lock_current();
910 version = the_lnet.ln_routers_version;
912 list_for_each(entry, &the_lnet.ln_routers) {
913 rtr = list_entry(entry, struct lnet_peer,
916 now = ktime_get_real_seconds();
919 * only discover the router if we've passed
920 * alive_router_check_interval seconds. Some of the router
921 * interfaces could be down and in that case they would be
922 * undergoing recovery separately from this discovery.
924 /* find next peer net which is also local */
925 net_id = rtr->lp_disc_net_id;
927 lpn = lnet_get_next_peer_net_locked(rtr, net_id);
929 CERROR("gateway %s has no networks\n",
930 libcfs_nid2str(rtr->lp_primary_nid));
933 if (first_lpn == lpn)
937 found_lpn = lnet_islocalnet_locked(lpn->lpn_net_id);
938 net_id = lpn->lpn_net_id;
939 } while (!found_lpn);
941 if (!found_lpn || !lpn) {
942 CERROR("no local network found for gateway %s\n",
943 libcfs_nid2str(rtr->lp_primary_nid));
947 if (now - lpn->lpn_rtrcheck_timestamp <
948 alive_router_check_interval / lnet_current_net_count)
952 * If we're currently discovering the peer then don't
953 * issue another discovery
955 spin_lock(&rtr->lp_lock);
956 if (rtr->lp_state & LNET_PEER_RTR_DISCOVERY) {
957 spin_unlock(&rtr->lp_lock);
960 /* make sure we actively discover the router */
961 rtr->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
962 rtr->lp_state |= LNET_PEER_RTR_DISCOVERY;
963 spin_unlock(&rtr->lp_lock);
965 /* find the peer_ni associated with the primary NID */
966 lpni = lnet_peer_get_ni_locked(rtr, rtr->lp_primary_nid);
968 CDEBUG(D_NET, "Expected to find an lpni for %s, but non found\n",
969 libcfs_nid2str(rtr->lp_primary_nid));
972 lnet_peer_ni_addref_locked(lpni);
974 /* specify the net to use */
975 rtr->lp_disc_net_id = lpn->lpn_net_id;
977 /* discover the router */
978 CDEBUG(D_NET, "discover %s, cpt = %d\n",
979 libcfs_nid2str(lpni->lpni_nid), cpt);
980 rc = lnet_discover_peer_locked(lpni, cpt, false);
982 /* decrement ref count acquired by find_peer_ni_locked() */
983 lnet_peer_ni_decref_locked(lpni);
986 lpn->lpn_rtrcheck_timestamp = now;
988 CERROR("Failed to discover router %s\n",
989 libcfs_nid2str(rtr->lp_primary_nid));
991 /* NB dropped lock */
992 if (version != the_lnet.ln_routers_version) {
993 /* the routers list has changed */
998 if (the_lnet.ln_routing)
999 push = lnet_update_ni_status_locked();
1001 lnet_net_unlock(cpt);
1003 /* if the status of the ni changed update the peers */
1005 lnet_push_update_to_peers(1);
1009 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1011 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1013 while (--npages >= 0)
1014 __free_page(rb->rb_kiov[npages].kiov_page);
1016 LIBCFS_FREE(rb, sz);
1019 static struct lnet_rtrbuf *
1020 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1022 int npages = rbp->rbp_npages;
1023 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1025 struct lnet_rtrbuf *rb;
1028 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1034 for (i = 0; i < npages; i++) {
1035 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1036 GFP_KERNEL | __GFP_ZERO);
1039 __free_page(rb->rb_kiov[i].kiov_page);
1041 LIBCFS_FREE(rb, sz);
1045 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
1046 rb->rb_kiov[i].kiov_offset = 0;
1047 rb->rb_kiov[i].kiov_page = page;
1054 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1056 int npages = rbp->rbp_npages;
1057 struct lnet_rtrbuf *rb;
1058 struct list_head tmp;
1060 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1063 INIT_LIST_HEAD(&tmp);
1066 list_splice_init(&rbp->rbp_msgs, &tmp);
1067 lnet_drop_routed_msgs_locked(&tmp, cpt);
1068 list_splice_init(&rbp->rbp_bufs, &tmp);
1069 rbp->rbp_req_nbuffers = 0;
1070 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1071 rbp->rbp_mincredits = 0;
1072 lnet_net_unlock(cpt);
1074 /* Free buffers on the free list. */
1075 while (!list_empty(&tmp)) {
1076 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1077 list_del(&rb->rb_list);
1078 lnet_destroy_rtrbuf(rb, npages);
1083 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1085 struct list_head rb_list;
1086 struct lnet_rtrbuf *rb;
1088 int num_buffers = 0;
1090 int npages = rbp->rbp_npages;
1093 /* If we are called for less buffers than already in the pool, we
1094 * just lower the req_nbuffers number and excess buffers will be
1095 * thrown away as they are returned to the free list. Credits
1096 * then get adjusted as well.
1097 * If we already have enough buffers allocated to serve the
1098 * increase requested, then we can treat that the same way as we
1099 * do the decrease. */
1100 num_rb = nbufs - rbp->rbp_nbuffers;
1101 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1102 rbp->rbp_req_nbuffers = nbufs;
1103 lnet_net_unlock(cpt);
1106 /* store the older value of rbp_req_nbuffers and then set it to
1107 * the new request to prevent lnet_return_rx_credits_locked() from
1108 * freeing buffers that we need to keep around */
1109 old_req_nbufs = rbp->rbp_req_nbuffers;
1110 rbp->rbp_req_nbuffers = nbufs;
1111 lnet_net_unlock(cpt);
1113 INIT_LIST_HEAD(&rb_list);
1115 /* allocate the buffers on a local list first. If all buffers are
1116 * allocated successfully then join this list to the rbp buffer
1117 * list. If not then free all allocated buffers. */
1118 while (num_rb-- > 0) {
1119 rb = lnet_new_rtrbuf(rbp, cpt);
1121 CERROR("Failed to allocate %d route bufs of %d pages\n",
1125 rbp->rbp_req_nbuffers = old_req_nbufs;
1126 lnet_net_unlock(cpt);
1131 list_add(&rb->rb_list, &rb_list);
1137 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1138 rbp->rbp_nbuffers += num_buffers;
1139 rbp->rbp_credits += num_buffers;
1140 rbp->rbp_mincredits = rbp->rbp_credits;
1141 /* We need to schedule blocked msg using the newly
1143 while (!list_empty(&rbp->rbp_bufs) &&
1144 !list_empty(&rbp->rbp_msgs))
1145 lnet_schedule_blocked_locked(rbp);
1147 lnet_net_unlock(cpt);
1152 while (!list_empty(&rb_list)) {
1153 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1154 list_del(&rb->rb_list);
1155 lnet_destroy_rtrbuf(rb, npages);
1162 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1164 INIT_LIST_HEAD(&rbp->rbp_msgs);
1165 INIT_LIST_HEAD(&rbp->rbp_bufs);
1167 rbp->rbp_npages = npages;
1168 rbp->rbp_credits = 0;
1169 rbp->rbp_mincredits = 0;
1173 lnet_rtrpools_free(int keep_pools)
1175 struct lnet_rtrbufpool *rtrp;
1178 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1181 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1182 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1183 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1184 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1188 cfs_percpt_free(the_lnet.ln_rtrpools);
1189 the_lnet.ln_rtrpools = NULL;
1194 lnet_nrb_tiny_calculate(void)
1196 int nrbs = LNET_NRB_TINY;
1198 if (tiny_router_buffers < 0) {
1199 LCONSOLE_ERROR_MSG(0x10c,
1200 "tiny_router_buffers=%d invalid when "
1201 "routing enabled\n", tiny_router_buffers);
1205 if (tiny_router_buffers > 0)
1206 nrbs = tiny_router_buffers;
1208 nrbs /= LNET_CPT_NUMBER;
1209 return max(nrbs, LNET_NRB_TINY_MIN);
1213 lnet_nrb_small_calculate(void)
1215 int nrbs = LNET_NRB_SMALL;
1217 if (small_router_buffers < 0) {
1218 LCONSOLE_ERROR_MSG(0x10c,
1219 "small_router_buffers=%d invalid when "
1220 "routing enabled\n", small_router_buffers);
1224 if (small_router_buffers > 0)
1225 nrbs = small_router_buffers;
1227 nrbs /= LNET_CPT_NUMBER;
1228 return max(nrbs, LNET_NRB_SMALL_MIN);
1232 lnet_nrb_large_calculate(void)
1234 int nrbs = LNET_NRB_LARGE;
1236 if (large_router_buffers < 0) {
1237 LCONSOLE_ERROR_MSG(0x10c,
1238 "large_router_buffers=%d invalid when "
1239 "routing enabled\n", large_router_buffers);
1243 if (large_router_buffers > 0)
1244 nrbs = large_router_buffers;
1246 nrbs /= LNET_CPT_NUMBER;
1247 return max(nrbs, LNET_NRB_LARGE_MIN);
1251 lnet_rtrpools_alloc(int im_a_router)
1253 struct lnet_rtrbufpool *rtrp;
1260 if (!strcmp(forwarding, "")) {
1261 /* not set either way */
1264 } else if (!strcmp(forwarding, "disabled")) {
1265 /* explicitly disabled */
1267 } else if (!strcmp(forwarding, "enabled")) {
1268 /* explicitly enabled */
1270 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1271 "'enabled' or 'disabled'\n");
1275 nrb_tiny = lnet_nrb_tiny_calculate();
1279 nrb_small = lnet_nrb_small_calculate();
1283 nrb_large = lnet_nrb_large_calculate();
1287 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1289 sizeof(struct lnet_rtrbufpool));
1290 if (the_lnet.ln_rtrpools == NULL) {
1291 LCONSOLE_ERROR_MSG(0x10c,
1292 "Failed to initialize router buffe pool\n");
1296 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1297 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1298 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1303 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1304 LNET_NRB_SMALL_PAGES);
1305 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1310 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1311 LNET_NRB_LARGE_PAGES);
1312 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1318 lnet_net_lock(LNET_LOCK_EX);
1319 the_lnet.ln_routing = 1;
1320 lnet_net_unlock(LNET_LOCK_EX);
1321 wake_up(&the_lnet.ln_mt_waitq);
1325 lnet_rtrpools_free(0);
1330 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1335 struct lnet_rtrbufpool *rtrp;
1337 /* If the provided values for each buffer pool are different than the
1338 * configured values, we need to take action. */
1340 tiny_router_buffers = tiny;
1341 nrb = lnet_nrb_tiny_calculate();
1342 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1343 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1350 small_router_buffers = small;
1351 nrb = lnet_nrb_small_calculate();
1352 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1353 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1360 large_router_buffers = large;
1361 nrb = lnet_nrb_large_calculate();
1362 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1363 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1374 lnet_rtrpools_adjust(int tiny, int small, int large)
1376 /* this function doesn't revert the changes if adding new buffers
1377 * failed. It's up to the user space caller to revert the
1380 if (!the_lnet.ln_routing)
1383 return lnet_rtrpools_adjust_helper(tiny, small, large);
1387 lnet_rtrpools_enable(void)
1391 if (the_lnet.ln_routing)
1394 if (the_lnet.ln_rtrpools == NULL)
1395 /* If routing is turned off, and we have never
1396 * initialized the pools before, just call the
1397 * standard buffer pool allocation routine as
1398 * if we are just configuring this for the first
1400 rc = lnet_rtrpools_alloc(1);
1402 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1406 lnet_net_lock(LNET_LOCK_EX);
1407 the_lnet.ln_routing = 1;
1409 the_lnet.ln_ping_target->pb_info.pi_features &=
1410 ~LNET_PING_FEAT_RTE_DISABLED;
1411 lnet_net_unlock(LNET_LOCK_EX);
1413 if (lnet_peer_discovery_disabled)
1414 CWARN("Consider turning discovery on to enable full "
1415 "Multi-Rail routing functionality\n");
1421 lnet_rtrpools_disable(void)
1423 if (!the_lnet.ln_routing)
1426 lnet_net_lock(LNET_LOCK_EX);
1427 the_lnet.ln_routing = 0;
1428 the_lnet.ln_ping_target->pb_info.pi_features |=
1429 LNET_PING_FEAT_RTE_DISABLED;
1431 tiny_router_buffers = 0;
1432 small_router_buffers = 0;
1433 large_router_buffers = 0;
1434 lnet_net_unlock(LNET_LOCK_EX);
1435 lnet_rtrpools_free(1);
1439 lnet_notify_peer_down(struct lnet_ni *ni, lnet_nid_t nid)
1441 if (ni->ni_net->net_lnd->lnd_notify_peer_down != NULL)
1442 (ni->ni_net->net_lnd->lnd_notify_peer_down)(nid);
1446 * ni: local NI used to communicate with the peer
1448 * alive: true if peer is alive, false otherwise
1449 * reset: reset health value. This is requested by the LND.
1450 * when: notificaiton time.
1453 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, bool alive, bool reset,
1456 struct lnet_peer_ni *lpni = NULL;
1457 time64_t now = ktime_get_seconds();
1460 LASSERT (!in_interrupt ());
1462 CDEBUG (D_NET, "%s notifying %s: %s\n",
1463 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1464 libcfs_nid2str(nid),
1465 alive ? "up" : "down");
1468 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1469 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1470 libcfs_nid2str(nid), alive ? "birth" : "death",
1471 libcfs_nid2str(ni->ni_nid));
1475 /* can't do predictions... */
1477 CWARN("Ignoring prediction from %s of %s %s "
1478 "%lld seconds in the future\n",
1479 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1480 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1484 if (ni != NULL && !alive && /* LND telling me she's down */
1485 !auto_down) { /* auto-down disabled */
1486 CDEBUG(D_NET, "Auto-down disabled\n");
1490 /* must lock 0 since this is used for synchronization */
1493 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1498 lpni = lnet_find_peer_ni_locked(nid);
1502 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1508 lnet_set_healthv(&lpni->lpni_healthv,
1509 LNET_MAX_HEALTH_VALUE);
1511 lnet_inc_healthv(&lpni->lpni_healthv);
1513 lnet_handle_remote_failure_locked(lpni);
1516 /* recalculate aliveness */
1517 alive = lnet_is_peer_ni_alive(lpni);
1520 if (ni != NULL && !alive)
1521 lnet_notify_peer_down(ni, lpni->lpni_nid);
1523 cpt = lpni->lpni_cpt;
1525 lnet_peer_ni_decref_locked(lpni);
1526 lnet_net_unlock(cpt);
1530 EXPORT_SYMBOL(lnet_notify);