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 static int dead_router_check_interval = 60;
82 module_param(dead_router_check_interval, int, 0644);
83 MODULE_PARM_DESC(dead_router_check_interval, "Seconds between dead router health checks (<= 0 to disable)");
85 static int live_router_check_interval = 60;
86 module_param(live_router_check_interval, int, 0644);
87 MODULE_PARM_DESC(live_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
89 static int router_ping_timeout = 50;
90 module_param(router_ping_timeout, int, 0644);
91 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
94 * A value between 0 and 100. 0 meaning that even if router's interfaces
95 * have the worse health still consider the gateway usable.
96 * 100 means that at least one interface on the route's remote net is 100%
97 * healthy to consider the route alive.
98 * The default is set to 100 to ensure we maintain the original behavior.
100 unsigned int router_sensitivity_percentage = 100;
101 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
102 static struct kernel_param_ops param_ops_rtr_sensitivity = {
103 .set = rtr_sensitivity_set,
104 .get = param_get_int,
106 #define param_check_rtr_sensitivity(name, p) \
107 __param_check(name, p, int)
108 #ifdef HAVE_KERNEL_PARAM_OPS
109 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
111 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
112 &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
114 MODULE_PARM_DESC(router_sensitivity_percentage,
115 "How healthy a gateway should be to be used in percent");
118 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
121 unsigned *sen = (unsigned *)kp->arg;
124 rc = kstrtoul(val, 0, &value);
126 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
130 if (value < 0 || value > 100) {
131 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
136 * The purpose of locking the api_mutex here is to ensure that
137 * the correct value ends up stored properly.
139 mutex_lock(&the_lnet.ln_api_mutex);
143 mutex_unlock(&the_lnet.ln_api_mutex);
149 lnet_peers_start_down(void)
151 return check_routers_before_use;
155 * A net is alive if at least one gateway NI on the network is alive.
158 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
160 struct lnet_peer_ni *lpni;
162 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
163 if (lnet_is_peer_ni_alive(lpni))
171 * a gateway is alive only if all its nets are alive
172 * called with cpt lock held
174 bool lnet_is_gateway_alive(struct lnet_peer *gw)
176 struct lnet_peer_net *lpn;
178 list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
179 if (!lnet_is_gateway_net_alive(lpn))
187 * lnet_is_route_alive() needs to be called with cpt lock held
188 * A route is alive if the gateway can route between the local network and
189 * the remote network of the route.
190 * This means at least one NI is alive on each of the local and remote
191 * networks of the gateway.
193 bool lnet_is_route_alive(struct lnet_route *route)
195 struct lnet_peer *gw = route->lr_gateway;
196 struct lnet_peer_net *llpn;
197 struct lnet_peer_net *rlpn;
201 * check the gateway's interfaces on the route rnet to make sure
202 * that the gateway is viable.
204 llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
208 route_alive = lnet_is_gateway_net_alive(llpn);
210 if (avoid_asym_router_failure) {
211 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
214 route_alive = route_alive &&
215 lnet_is_gateway_net_alive(rlpn);
221 spin_lock(&gw->lp_lock);
222 if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
223 if (gw->lp_rtr_refcount > 0)
224 CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
225 libcfs_nid2str(gw->lp_primary_nid));
228 spin_unlock(&gw->lp_lock);
234 lnet_rtr_addref_locked(struct lnet_peer *lp)
236 LASSERT(lp->lp_rtr_refcount >= 0);
238 /* lnet_net_lock must be exclusively locked */
239 lp->lp_rtr_refcount++;
240 if (lp->lp_rtr_refcount == 1) {
241 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
242 /* addref for the_lnet.ln_routers */
243 lnet_peer_addref_locked(lp);
244 the_lnet.ln_routers_version++;
249 lnet_rtr_decref_locked(struct lnet_peer *lp)
251 LASSERT(atomic_read(&lp->lp_refcount) > 0);
252 LASSERT(lp->lp_rtr_refcount > 0);
254 /* lnet_net_lock must be exclusively locked */
255 lp->lp_rtr_refcount--;
256 if (lp->lp_rtr_refcount == 0) {
257 LASSERT(list_empty(&lp->lp_routes));
259 list_del(&lp->lp_rtr_list);
260 /* decref for the_lnet.ln_routers */
261 lnet_peer_decref_locked(lp);
262 the_lnet.ln_routers_version++;
266 struct lnet_remotenet *
267 lnet_find_rnet_locked(__u32 net)
269 struct lnet_remotenet *rnet;
270 struct list_head *tmp;
271 struct list_head *rn_list;
273 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
275 rn_list = lnet_net2rnethash(net);
276 list_for_each(tmp, rn_list) {
277 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
279 if (rnet->lrn_net == net)
285 static void lnet_shuffle_seed(void)
288 struct lnet_ni *ni = NULL;
293 /* Nodes with small feet have little entropy
294 * the NID for this node gives the most entropy in the low bits */
295 while ((ni = lnet_get_next_ni_locked(NULL, ni)))
296 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
302 /* NB expects LNET_LOCK held */
304 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
306 unsigned int len = 0;
307 unsigned int offset = 0;
312 list_for_each(e, &rnet->lrn_routes)
316 * Randomly adding routes to the list is done to ensure that when
317 * different nodes are using the same list of routers, they end up
318 * preferring different routers.
320 offset = cfs_rand() % (len + 1);
321 list_for_each(e, &rnet->lrn_routes) {
326 list_add(&route->lr_list, e);
328 * force a router check on the gateway to make sure the route is
331 route->lr_gateway->lp_rtrcheck_timestamp = 0;
333 the_lnet.ln_remote_nets_version++;
335 /* add the route on the gateway list */
336 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
338 /* take a router reference count on the gateway */
339 lnet_rtr_addref_locked(route->lr_gateway);
343 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
344 unsigned int priority)
346 struct list_head *route_entry;
347 struct lnet_remotenet *rnet;
348 struct lnet_remotenet *rnet2;
349 struct lnet_route *route;
350 struct lnet_peer_ni *lpni;
351 struct lnet_peer *gw;
355 CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
356 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
358 if (gateway == LNET_NID_ANY ||
359 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
360 net == LNET_NIDNET(LNET_NID_ANY) ||
361 LNET_NETTYP(net) == LOLND ||
362 LNET_NIDNET(gateway) == net ||
363 (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
366 /* it's a local network */
367 if (lnet_islocalnet(net))
370 /* Assume net, route, all new */
371 LIBCFS_ALLOC(route, sizeof(*route));
372 LIBCFS_ALLOC(rnet, sizeof(*rnet));
373 if (route == NULL || rnet == NULL) {
374 CERROR("Out of memory creating route %s %d %s\n",
375 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
377 LIBCFS_FREE(route, sizeof(*route));
379 LIBCFS_FREE(rnet, sizeof(*rnet));
383 INIT_LIST_HEAD(&rnet->lrn_routes);
385 /* store the local and remote net that the route represents */
386 route->lr_lnet = LNET_NIDNET(gateway);
388 route->lr_priority = priority;
389 route->lr_hops = hops;
391 lnet_net_lock(LNET_LOCK_EX);
394 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
395 * lose that once we're done
397 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
399 lnet_net_unlock(LNET_LOCK_EX);
401 LIBCFS_FREE(route, sizeof(*route));
402 LIBCFS_FREE(rnet, sizeof(*rnet));
405 CERROR("Error %d creating route %s %d %s\n", rc,
406 libcfs_net2str(net), hops,
407 libcfs_nid2str(gateway));
411 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
412 gw = lpni->lpni_peer_net->lpn_peer;
414 route->lr_gateway = gw;
416 rnet2 = lnet_find_rnet_locked(net);
419 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
423 /* Search for a duplicate route (it's a NOOP if it is) */
425 list_for_each(route_entry, &rnet2->lrn_routes) {
426 struct lnet_route *route2;
428 route2 = list_entry(route_entry, struct lnet_route, lr_list);
429 if (route2->lr_gateway == route->lr_gateway) {
434 /* our lookups must be true */
435 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
439 * It is possible to add multiple routes through the same peer,
440 * but it'll be using a different NID of that peer. When the
441 * gateway is discovered, discovery will consolidate the different
442 * peers into one peer. In this case the discovery code will have
443 * to move the routes from the peer that's being deleted to the
444 * consolidated peer lp_routes list
447 lnet_add_route_to_rnet(rnet2, route);
450 * get rid of the reference on the lpni.
452 lnet_peer_ni_decref_locked(lpni);
453 lnet_net_unlock(LNET_LOCK_EX);
459 LIBCFS_FREE(route, sizeof(*route));
463 LIBCFS_FREE(rnet, sizeof(*rnet));
465 /* kick start the monitor thread to handle the added route */
466 wake_up(&the_lnet.ln_mt_waitq);
472 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
473 struct list_head *zombies)
475 struct lnet_peer *gateway;
476 struct lnet_route *route;
477 struct lnet_route *tmp;
479 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
480 gateway = route->lr_gateway;
481 if (gw_nid != LNET_NID_ANY &&
482 gw_nid != gateway->lp_primary_nid)
486 * move to zombie to delete outside the lock
487 * Note that this function is called with the
488 * ln_api_mutex held as well as the exclusive net
489 * lock. Adding to the remote net list happens
490 * under the same conditions. Same goes for the
491 * gateway router list
493 list_move(&route->lr_list, zombies);
494 the_lnet.ln_remote_nets_version++;
496 list_del(&route->lr_gwlist);
497 lnet_rtr_decref_locked(gateway);
502 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
504 struct list_head rnet_zombies;
505 struct lnet_remotenet *rnet;
506 struct lnet_remotenet *tmp;
507 struct list_head *rn_list;
508 struct lnet_peer_ni *lpni;
509 struct lnet_route *route;
510 struct list_head zombies;
511 struct lnet_peer *lp;
514 INIT_LIST_HEAD(&rnet_zombies);
515 INIT_LIST_HEAD(&zombies);
517 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
518 libcfs_net2str(net), libcfs_nid2str(gw_nid));
520 /* NB Caller may specify either all routes via the given gateway
521 * or a specific route entry actual NIDs) */
523 lnet_net_lock(LNET_LOCK_EX);
525 lpni = lnet_find_peer_ni_locked(gw_nid);
527 lp = lpni->lpni_peer_net->lpn_peer;
529 gw_nid = lp->lp_primary_nid;
530 lnet_peer_ni_decref_locked(lpni);
533 if (net != LNET_NIDNET(LNET_NID_ANY)) {
534 rnet = lnet_find_rnet_locked(net);
536 lnet_net_unlock(LNET_LOCK_EX);
539 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
541 if (list_empty(&rnet->lrn_routes))
542 list_move(&rnet->lrn_list, &rnet_zombies);
546 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
547 rn_list = &the_lnet.ln_remote_nets_hash[i];
549 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
550 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
552 if (list_empty(&rnet->lrn_routes))
553 list_move(&rnet->lrn_list, &rnet_zombies);
558 lnet_net_unlock(LNET_LOCK_EX);
560 while (!list_empty(&zombies)) {
561 route = list_first_entry(&zombies, struct lnet_route, lr_list);
562 list_del(&route->lr_list);
563 LIBCFS_FREE(route, sizeof(*route));
566 while (!list_empty(&rnet_zombies)) {
567 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
569 list_del(&rnet->lrn_list);
570 LIBCFS_FREE(rnet, sizeof(*rnet));
577 lnet_destroy_routes (void)
579 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
582 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
584 struct lnet_rtrbufpool *rbp;
585 int i, rc = -ENOENT, j;
587 if (the_lnet.ln_rtrpools == NULL)
591 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
596 for (j = 0; j < LNET_NRBPOOLS; j++) {
597 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
598 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
599 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
600 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
607 lnet_net_lock(LNET_LOCK_EX);
608 pool_cfg->pl_routing = the_lnet.ln_routing;
609 lnet_net_unlock(LNET_LOCK_EX);
615 lnet_get_route(int idx, __u32 *net, __u32 *hops,
616 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
618 struct list_head *e1;
619 struct list_head *e2;
620 struct lnet_remotenet *rnet;
621 struct lnet_route *route;
624 struct list_head *rn_list;
626 cpt = lnet_net_lock_current();
628 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
629 rn_list = &the_lnet.ln_remote_nets_hash[i];
630 list_for_each(e1, rn_list) {
631 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
633 list_for_each(e2, &rnet->lrn_routes) {
634 route = list_entry(e2, struct lnet_route,
638 *net = rnet->lrn_net;
639 *hops = route->lr_hops;
640 *priority = route->lr_priority;
641 *gateway = route->lr_gateway->lp_primary_nid;
642 *alive = lnet_is_route_alive(route);
643 lnet_net_unlock(cpt);
650 lnet_net_unlock(cpt);
655 lnet_wait_known_routerstate(void)
657 struct lnet_peer *rtr;
658 struct list_head *entry;
661 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
664 int cpt = lnet_net_lock_current();
667 list_for_each(entry, &the_lnet.ln_routers) {
668 rtr = list_entry(entry, struct lnet_peer,
671 spin_lock(&rtr->lp_lock);
673 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
675 spin_unlock(&rtr->lp_lock);
678 spin_unlock(&rtr->lp_lock);
681 lnet_net_unlock(cpt);
686 set_current_state(TASK_UNINTERRUPTIBLE);
687 schedule_timeout(cfs_time_seconds(1));
692 lnet_update_ni_status_locked(void)
694 struct lnet_ni *ni = NULL;
698 LASSERT(the_lnet.ln_routing);
700 timeout = router_ping_timeout +
701 MAX(live_router_check_interval, dead_router_check_interval);
703 now = ktime_get_real_seconds();
704 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
705 if (ni->ni_net->net_lnd->lnd_type == LOLND)
708 if (now < ni->ni_last_alive + timeout)
712 /* re-check with lock */
713 if (now < ni->ni_last_alive + timeout) {
718 LASSERT(ni->ni_status != NULL);
720 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
721 CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
722 libcfs_nid2str(ni->ni_nid), timeout);
723 /* NB: so far, this is the only place to set
724 * NI status to "down" */
725 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
731 void lnet_router_post_mt_start(void)
733 if (check_routers_before_use) {
734 /* Note that a helpful side-effect of pinging all known routers
735 * at startup is that it makes them drop stale connections they
736 * may have to a previous instance of me. */
737 lnet_wait_known_routerstate();
742 * This function is called from the monitor thread to check if there are
743 * any active routers that need to be checked.
746 lnet_router_checker_active(void)
748 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
751 /* Router Checker thread needs to run when routing is enabled in
752 * order to call lnet_update_ni_status_locked() */
753 if (the_lnet.ln_routing)
756 return !list_empty(&the_lnet.ln_routers) &&
757 (live_router_check_interval > 0 ||
758 dead_router_check_interval > 0);
762 lnet_check_routers(void)
764 struct lnet_peer *rtr;
765 struct list_head *entry;
769 cpt = lnet_net_lock_current();
771 version = the_lnet.ln_routers_version;
773 list_for_each(entry, &the_lnet.ln_routers) {
774 rtr = list_entry(entry, struct lnet_peer,
777 /* TODO use discovery to determine if router is alive */
779 /* NB dropped lock */
780 if (version != the_lnet.ln_routers_version) {
781 /* the routers list has changed */
786 if (the_lnet.ln_routing)
787 lnet_update_ni_status_locked();
789 lnet_net_unlock(cpt);
793 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
795 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
797 while (--npages >= 0)
798 __free_page(rb->rb_kiov[npages].kiov_page);
803 static struct lnet_rtrbuf *
804 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
806 int npages = rbp->rbp_npages;
807 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
809 struct lnet_rtrbuf *rb;
812 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
818 for (i = 0; i < npages; i++) {
819 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
820 GFP_KERNEL | __GFP_ZERO);
823 __free_page(rb->rb_kiov[i].kiov_page);
829 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
830 rb->rb_kiov[i].kiov_offset = 0;
831 rb->rb_kiov[i].kiov_page = page;
838 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
840 int npages = rbp->rbp_npages;
841 struct lnet_rtrbuf *rb;
842 struct list_head tmp;
844 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
847 INIT_LIST_HEAD(&tmp);
850 list_splice_init(&rbp->rbp_msgs, &tmp);
851 lnet_drop_routed_msgs_locked(&tmp, cpt);
852 list_splice_init(&rbp->rbp_bufs, &tmp);
853 rbp->rbp_req_nbuffers = 0;
854 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
855 rbp->rbp_mincredits = 0;
856 lnet_net_unlock(cpt);
858 /* Free buffers on the free list. */
859 while (!list_empty(&tmp)) {
860 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
861 list_del(&rb->rb_list);
862 lnet_destroy_rtrbuf(rb, npages);
867 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
869 struct list_head rb_list;
870 struct lnet_rtrbuf *rb;
874 int npages = rbp->rbp_npages;
877 /* If we are called for less buffers than already in the pool, we
878 * just lower the req_nbuffers number and excess buffers will be
879 * thrown away as they are returned to the free list. Credits
880 * then get adjusted as well.
881 * If we already have enough buffers allocated to serve the
882 * increase requested, then we can treat that the same way as we
883 * do the decrease. */
884 num_rb = nbufs - rbp->rbp_nbuffers;
885 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
886 rbp->rbp_req_nbuffers = nbufs;
887 lnet_net_unlock(cpt);
890 /* store the older value of rbp_req_nbuffers and then set it to
891 * the new request to prevent lnet_return_rx_credits_locked() from
892 * freeing buffers that we need to keep around */
893 old_req_nbufs = rbp->rbp_req_nbuffers;
894 rbp->rbp_req_nbuffers = nbufs;
895 lnet_net_unlock(cpt);
897 INIT_LIST_HEAD(&rb_list);
899 /* allocate the buffers on a local list first. If all buffers are
900 * allocated successfully then join this list to the rbp buffer
901 * list. If not then free all allocated buffers. */
902 while (num_rb-- > 0) {
903 rb = lnet_new_rtrbuf(rbp, cpt);
905 CERROR("Failed to allocate %d route bufs of %d pages\n",
909 rbp->rbp_req_nbuffers = old_req_nbufs;
910 lnet_net_unlock(cpt);
915 list_add(&rb->rb_list, &rb_list);
921 list_splice_tail(&rb_list, &rbp->rbp_bufs);
922 rbp->rbp_nbuffers += num_buffers;
923 rbp->rbp_credits += num_buffers;
924 rbp->rbp_mincredits = rbp->rbp_credits;
925 /* We need to schedule blocked msg using the newly
927 while (!list_empty(&rbp->rbp_bufs) &&
928 !list_empty(&rbp->rbp_msgs))
929 lnet_schedule_blocked_locked(rbp);
931 lnet_net_unlock(cpt);
936 while (!list_empty(&rb_list)) {
937 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
938 list_del(&rb->rb_list);
939 lnet_destroy_rtrbuf(rb, npages);
946 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
948 INIT_LIST_HEAD(&rbp->rbp_msgs);
949 INIT_LIST_HEAD(&rbp->rbp_bufs);
951 rbp->rbp_npages = npages;
952 rbp->rbp_credits = 0;
953 rbp->rbp_mincredits = 0;
957 lnet_rtrpools_free(int keep_pools)
959 struct lnet_rtrbufpool *rtrp;
962 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
965 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
966 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
967 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
968 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
972 cfs_percpt_free(the_lnet.ln_rtrpools);
973 the_lnet.ln_rtrpools = NULL;
978 lnet_nrb_tiny_calculate(void)
980 int nrbs = LNET_NRB_TINY;
982 if (tiny_router_buffers < 0) {
983 LCONSOLE_ERROR_MSG(0x10c,
984 "tiny_router_buffers=%d invalid when "
985 "routing enabled\n", tiny_router_buffers);
989 if (tiny_router_buffers > 0)
990 nrbs = tiny_router_buffers;
992 nrbs /= LNET_CPT_NUMBER;
993 return max(nrbs, LNET_NRB_TINY_MIN);
997 lnet_nrb_small_calculate(void)
999 int nrbs = LNET_NRB_SMALL;
1001 if (small_router_buffers < 0) {
1002 LCONSOLE_ERROR_MSG(0x10c,
1003 "small_router_buffers=%d invalid when "
1004 "routing enabled\n", small_router_buffers);
1008 if (small_router_buffers > 0)
1009 nrbs = small_router_buffers;
1011 nrbs /= LNET_CPT_NUMBER;
1012 return max(nrbs, LNET_NRB_SMALL_MIN);
1016 lnet_nrb_large_calculate(void)
1018 int nrbs = LNET_NRB_LARGE;
1020 if (large_router_buffers < 0) {
1021 LCONSOLE_ERROR_MSG(0x10c,
1022 "large_router_buffers=%d invalid when "
1023 "routing enabled\n", large_router_buffers);
1027 if (large_router_buffers > 0)
1028 nrbs = large_router_buffers;
1030 nrbs /= LNET_CPT_NUMBER;
1031 return max(nrbs, LNET_NRB_LARGE_MIN);
1035 lnet_rtrpools_alloc(int im_a_router)
1037 struct lnet_rtrbufpool *rtrp;
1044 if (!strcmp(forwarding, "")) {
1045 /* not set either way */
1048 } else if (!strcmp(forwarding, "disabled")) {
1049 /* explicitly disabled */
1051 } else if (!strcmp(forwarding, "enabled")) {
1052 /* explicitly enabled */
1054 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1055 "'enabled' or 'disabled'\n");
1059 nrb_tiny = lnet_nrb_tiny_calculate();
1063 nrb_small = lnet_nrb_small_calculate();
1067 nrb_large = lnet_nrb_large_calculate();
1071 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1073 sizeof(struct lnet_rtrbufpool));
1074 if (the_lnet.ln_rtrpools == NULL) {
1075 LCONSOLE_ERROR_MSG(0x10c,
1076 "Failed to initialize router buffe pool\n");
1080 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1081 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1082 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1087 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1088 LNET_NRB_SMALL_PAGES);
1089 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1094 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1095 LNET_NRB_LARGE_PAGES);
1096 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1102 lnet_net_lock(LNET_LOCK_EX);
1103 the_lnet.ln_routing = 1;
1104 lnet_net_unlock(LNET_LOCK_EX);
1105 wake_up(&the_lnet.ln_mt_waitq);
1109 lnet_rtrpools_free(0);
1114 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1119 struct lnet_rtrbufpool *rtrp;
1121 /* If the provided values for each buffer pool are different than the
1122 * configured values, we need to take action. */
1124 tiny_router_buffers = tiny;
1125 nrb = lnet_nrb_tiny_calculate();
1126 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1127 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1134 small_router_buffers = small;
1135 nrb = lnet_nrb_small_calculate();
1136 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1137 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1144 large_router_buffers = large;
1145 nrb = lnet_nrb_large_calculate();
1146 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1147 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1158 lnet_rtrpools_adjust(int tiny, int small, int large)
1160 /* this function doesn't revert the changes if adding new buffers
1161 * failed. It's up to the user space caller to revert the
1164 if (!the_lnet.ln_routing)
1167 return lnet_rtrpools_adjust_helper(tiny, small, large);
1171 lnet_rtrpools_enable(void)
1175 if (the_lnet.ln_routing)
1178 if (the_lnet.ln_rtrpools == NULL)
1179 /* If routing is turned off, and we have never
1180 * initialized the pools before, just call the
1181 * standard buffer pool allocation routine as
1182 * if we are just configuring this for the first
1184 rc = lnet_rtrpools_alloc(1);
1186 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1190 lnet_net_lock(LNET_LOCK_EX);
1191 the_lnet.ln_routing = 1;
1193 the_lnet.ln_ping_target->pb_info.pi_features &=
1194 ~LNET_PING_FEAT_RTE_DISABLED;
1195 lnet_net_unlock(LNET_LOCK_EX);
1201 lnet_rtrpools_disable(void)
1203 if (!the_lnet.ln_routing)
1206 lnet_net_lock(LNET_LOCK_EX);
1207 the_lnet.ln_routing = 0;
1208 the_lnet.ln_ping_target->pb_info.pi_features |=
1209 LNET_PING_FEAT_RTE_DISABLED;
1211 tiny_router_buffers = 0;
1212 small_router_buffers = 0;
1213 large_router_buffers = 0;
1214 lnet_net_unlock(LNET_LOCK_EX);
1215 lnet_rtrpools_free(1);
1219 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, int alive, time64_t when)
1221 struct lnet_peer_ni *lp = NULL;
1222 time64_t now = ktime_get_seconds();
1223 int cpt = lnet_cpt_of_nid(nid, ni);
1225 LASSERT (!in_interrupt ());
1227 CDEBUG (D_NET, "%s notifying %s: %s\n",
1228 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1229 libcfs_nid2str(nid),
1230 alive ? "up" : "down");
1233 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1234 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1235 libcfs_nid2str(nid), alive ? "birth" : "death",
1236 libcfs_nid2str(ni->ni_nid));
1240 /* can't do predictions... */
1242 CWARN("Ignoring prediction from %s of %s %s "
1243 "%lld seconds in the future\n",
1244 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1245 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1249 if (ni != NULL && !alive && /* LND telling me she's down */
1250 !auto_down) { /* auto-down disabled */
1251 CDEBUG(D_NET, "Auto-down disabled\n");
1257 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1258 lnet_net_unlock(cpt);
1262 lp = lnet_find_peer_ni_locked(nid);
1265 lnet_net_unlock(cpt);
1266 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1271 * It is possible for this function to be called for the same peer
1272 * but with different NIs. We want to synchronize the notification
1273 * between the different calls. So we will use the lpni_cpt to
1274 * grab the net lock.
1276 if (lp->lpni_cpt != cpt) {
1277 lnet_net_unlock(cpt);
1282 lnet_peer_ni_decref_locked(lp);
1284 lnet_net_unlock(cpt);
1287 EXPORT_SYMBOL(lnet_notify);