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 lnet_peers_start_down(void)
96 return check_routers_before_use;
100 lnet_notify_locked(struct lnet_peer_ni *lp, int notifylnd, int alive,
103 if (lp->lpni_timestamp > when) { /* out of date information */
104 CDEBUG(D_NET, "Out of date\n");
109 * This function can be called with different cpt locks being
110 * held. lpni_alive_count modification needs to be properly protected.
111 * Significant reads to lpni_alive_count are also protected with
114 spin_lock(&lp->lpni_lock);
116 lp->lpni_timestamp = when; /* update timestamp */
118 if (lp->lpni_alive_count != 0 && /* got old news */
119 (!lp->lpni_alive) == (!alive)) { /* new date for old news */
120 spin_unlock(&lp->lpni_lock);
121 CDEBUG(D_NET, "Old news\n");
125 /* Flag that notification is outstanding */
127 lp->lpni_alive_count++;
128 lp->lpni_alive = (alive) ? 1 : 0;
130 lp->lpni_notifylnd = notifylnd;
132 lp->lpni_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
134 spin_unlock(&lp->lpni_lock);
136 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lpni_nid), alive);
140 * This function will always be called with lp->lpni_cpt lock held.
143 lnet_ni_notify_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
148 /* Notify only in 1 thread at any time to ensure ordered notification.
149 * NB individual events can be missed; the only guarantee is that you
150 * always get the most recent news */
152 spin_lock(&lp->lpni_lock);
154 if (lp->lpni_notifying || ni == NULL) {
155 spin_unlock(&lp->lpni_lock);
159 lp->lpni_notifying = 1;
162 * lp->lpni_notify needs to be protected because it can be set in
163 * lnet_notify_locked().
165 while (lp->lpni_notify) {
166 alive = lp->lpni_alive;
167 notifylnd = lp->lpni_notifylnd;
169 lp->lpni_notifylnd = 0;
172 if (notifylnd && ni->ni_net->net_lnd->lnd_notify != NULL) {
173 spin_unlock(&lp->lpni_lock);
174 lnet_net_unlock(lp->lpni_cpt);
176 /* A new notification could happen now; I'll handle it
177 * when control returns to me */
179 (ni->ni_net->net_lnd->lnd_notify)(ni, lp->lpni_nid,
182 lnet_net_lock(lp->lpni_cpt);
183 spin_lock(&lp->lpni_lock);
187 lp->lpni_notifying = 0;
188 spin_unlock(&lp->lpni_lock);
192 lnet_rtr_addref_locked(struct lnet_peer *lp)
194 LASSERT(lp->lp_rtr_refcount >= 0);
196 /* lnet_net_lock must be exclusively locked */
197 lp->lp_rtr_refcount++;
198 if (lp->lp_rtr_refcount == 1) {
199 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
200 /* addref for the_lnet.ln_routers */
201 lnet_peer_addref_locked(lp);
202 the_lnet.ln_routers_version++;
207 lnet_rtr_decref_locked(struct lnet_peer *lp)
209 LASSERT(atomic_read(&lp->lp_refcount) > 0);
210 LASSERT(lp->lp_rtr_refcount > 0);
212 /* lnet_net_lock must be exclusively locked */
213 lp->lp_rtr_refcount--;
214 if (lp->lp_rtr_refcount == 0) {
215 LASSERT(list_empty(&lp->lp_routes));
217 list_del(&lp->lp_rtr_list);
218 /* decref for the_lnet.ln_routers */
219 lnet_peer_decref_locked(lp);
220 the_lnet.ln_routers_version++;
224 struct lnet_remotenet *
225 lnet_find_rnet_locked(__u32 net)
227 struct lnet_remotenet *rnet;
228 struct list_head *tmp;
229 struct list_head *rn_list;
231 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
233 rn_list = lnet_net2rnethash(net);
234 list_for_each(tmp, rn_list) {
235 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
237 if (rnet->lrn_net == net)
243 static void lnet_shuffle_seed(void)
246 struct lnet_ni *ni = NULL;
251 /* Nodes with small feet have little entropy
252 * the NID for this node gives the most entropy in the low bits */
253 while ((ni = lnet_get_next_ni_locked(NULL, ni)))
254 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
260 /* NB expects LNET_LOCK held */
262 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
264 unsigned int len = 0;
265 unsigned int offset = 0;
270 list_for_each(e, &rnet->lrn_routes)
274 * Randomly adding routes to the list is done to ensure that when
275 * different nodes are using the same list of routers, they end up
276 * preferring different routers.
278 offset = cfs_rand() % (len + 1);
279 list_for_each(e, &rnet->lrn_routes) {
284 list_add(&route->lr_list, e);
286 * force a router check on the gateway to make sure the route is
289 route->lr_gateway->lp_rtrcheck_timestamp = 0;
291 the_lnet.ln_remote_nets_version++;
293 /* add the route on the gateway list */
294 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
296 /* take a router reference count on the gateway */
297 lnet_rtr_addref_locked(route->lr_gateway);
301 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
302 unsigned int priority)
304 struct list_head *route_entry;
305 struct lnet_remotenet *rnet;
306 struct lnet_remotenet *rnet2;
307 struct lnet_route *route;
308 struct lnet_peer_ni *lpni;
309 struct lnet_peer *gw;
313 CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
314 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
316 if (gateway == LNET_NID_ANY ||
317 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
318 net == LNET_NIDNET(LNET_NID_ANY) ||
319 LNET_NETTYP(net) == LOLND ||
320 LNET_NIDNET(gateway) == net ||
321 (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
324 /* it's a local network */
325 if (lnet_islocalnet(net))
328 /* Assume net, route, all new */
329 LIBCFS_ALLOC(route, sizeof(*route));
330 LIBCFS_ALLOC(rnet, sizeof(*rnet));
331 if (route == NULL || rnet == NULL) {
332 CERROR("Out of memory creating route %s %d %s\n",
333 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
335 LIBCFS_FREE(route, sizeof(*route));
337 LIBCFS_FREE(rnet, sizeof(*rnet));
341 INIT_LIST_HEAD(&rnet->lrn_routes);
343 /* store the local and remote net that the route represents */
344 route->lr_lnet = LNET_NIDNET(gateway);
346 route->lr_priority = priority;
347 route->lr_hops = hops;
349 lnet_net_lock(LNET_LOCK_EX);
352 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
353 * lose that once we're done
355 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
357 lnet_net_unlock(LNET_LOCK_EX);
359 LIBCFS_FREE(route, sizeof(*route));
360 LIBCFS_FREE(rnet, sizeof(*rnet));
363 CERROR("Error %d creating route %s %d %s\n", rc,
364 libcfs_net2str(net), hops,
365 libcfs_nid2str(gateway));
369 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
370 gw = lpni->lpni_peer_net->lpn_peer;
372 route->lr_gateway = gw;
374 rnet2 = lnet_find_rnet_locked(net);
377 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
381 /* Search for a duplicate route (it's a NOOP if it is) */
383 list_for_each(route_entry, &rnet2->lrn_routes) {
384 struct lnet_route *route2;
386 route2 = list_entry(route_entry, struct lnet_route, lr_list);
387 if (route2->lr_gateway == route->lr_gateway) {
392 /* our lookups must be true */
393 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
397 * It is possible to add multiple routes through the same peer,
398 * but it'll be using a different NID of that peer. When the
399 * gateway is discovered, discovery will consolidate the different
400 * peers into one peer. In this case the discovery code will have
401 * to move the routes from the peer that's being deleted to the
402 * consolidated peer lp_routes list
405 lnet_add_route_to_rnet(rnet2, route);
408 * get rid of the reference on the lpni.
410 lnet_peer_ni_decref_locked(lpni);
411 lnet_net_unlock(LNET_LOCK_EX);
417 LIBCFS_FREE(route, sizeof(*route));
421 LIBCFS_FREE(rnet, sizeof(*rnet));
423 /* kick start the monitor thread to handle the added route */
424 wake_up(&the_lnet.ln_mt_waitq);
430 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
431 struct list_head *zombies)
433 struct lnet_peer *gateway;
434 struct lnet_route *route;
435 struct lnet_route *tmp;
437 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
438 gateway = route->lr_gateway;
439 if (gw_nid != LNET_NID_ANY &&
440 gw_nid != gateway->lp_primary_nid)
444 * move to zombie to delete outside the lock
445 * Note that this function is called with the
446 * ln_api_mutex held as well as the exclusive net
447 * lock. Adding to the remote net list happens
448 * under the same conditions. Same goes for the
449 * gateway router list
451 list_move(&route->lr_list, zombies);
452 the_lnet.ln_remote_nets_version++;
454 list_del(&route->lr_gwlist);
455 lnet_rtr_decref_locked(gateway);
460 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
462 struct list_head rnet_zombies;
463 struct lnet_remotenet *rnet;
464 struct lnet_remotenet *tmp;
465 struct list_head *rn_list;
466 struct lnet_peer_ni *lpni;
467 struct lnet_route *route;
468 struct list_head zombies;
469 struct lnet_peer *lp;
472 INIT_LIST_HEAD(&rnet_zombies);
473 INIT_LIST_HEAD(&zombies);
475 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
476 libcfs_net2str(net), libcfs_nid2str(gw_nid));
478 /* NB Caller may specify either all routes via the given gateway
479 * or a specific route entry actual NIDs) */
481 lnet_net_lock(LNET_LOCK_EX);
483 lpni = lnet_find_peer_ni_locked(gw_nid);
485 lp = lpni->lpni_peer_net->lpn_peer;
487 gw_nid = lp->lp_primary_nid;
488 lnet_peer_ni_decref_locked(lpni);
491 if (net != LNET_NIDNET(LNET_NID_ANY)) {
492 rnet = lnet_find_rnet_locked(net);
494 lnet_net_unlock(LNET_LOCK_EX);
497 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
499 if (list_empty(&rnet->lrn_routes))
500 list_move(&rnet->lrn_list, &rnet_zombies);
504 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
505 rn_list = &the_lnet.ln_remote_nets_hash[i];
507 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
508 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
510 if (list_empty(&rnet->lrn_routes))
511 list_move(&rnet->lrn_list, &rnet_zombies);
516 lnet_net_unlock(LNET_LOCK_EX);
518 while (!list_empty(&zombies)) {
519 route = list_first_entry(&zombies, struct lnet_route, lr_list);
520 list_del(&route->lr_list);
521 LIBCFS_FREE(route, sizeof(*route));
524 while (!list_empty(&rnet_zombies)) {
525 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
527 list_del(&rnet->lrn_list);
528 LIBCFS_FREE(rnet, sizeof(*rnet));
535 lnet_destroy_routes (void)
537 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
540 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
542 struct lnet_rtrbufpool *rbp;
543 int i, rc = -ENOENT, j;
545 if (the_lnet.ln_rtrpools == NULL)
549 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
554 for (j = 0; j < LNET_NRBPOOLS; j++) {
555 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
556 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
557 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
558 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
565 lnet_net_lock(LNET_LOCK_EX);
566 pool_cfg->pl_routing = the_lnet.ln_routing;
567 lnet_net_unlock(LNET_LOCK_EX);
573 lnet_get_route(int idx, __u32 *net, __u32 *hops,
574 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
576 struct list_head *e1;
577 struct list_head *e2;
578 struct lnet_remotenet *rnet;
579 struct lnet_route *route;
582 struct list_head *rn_list;
584 cpt = lnet_net_lock_current();
586 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
587 rn_list = &the_lnet.ln_remote_nets_hash[i];
588 list_for_each(e1, rn_list) {
589 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
591 list_for_each(e2, &rnet->lrn_routes) {
592 route = list_entry(e2, struct lnet_route,
596 *net = rnet->lrn_net;
597 *hops = route->lr_hops;
598 *priority = route->lr_priority;
599 *gateway = route->lr_gateway->lp_primary_nid;
600 *alive = lnet_is_route_alive(route);
601 lnet_net_unlock(cpt);
608 lnet_net_unlock(cpt);
613 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
615 struct lnet_ni_status *stat;
619 __swab32s(&pbuf->pb_info.pi_magic);
620 __swab32s(&pbuf->pb_info.pi_features);
621 __swab32s(&pbuf->pb_info.pi_pid);
622 __swab32s(&pbuf->pb_info.pi_nnis);
623 nnis = pbuf->pb_info.pi_nnis;
624 if (nnis > pbuf->pb_nnis)
625 nnis = pbuf->pb_nnis;
626 for (i = 0; i < nnis; i++) {
627 stat = &pbuf->pb_info.pi_ni[i];
628 __swab64s(&stat->ns_nid);
629 __swab32s(&stat->ns_status);
638 lnet_parse_rc_info(struct lnet_rc_data *rcd)
644 lnet_router_checker_event(struct lnet_event *event)
646 struct lnet_rc_data *rcd = event->md.user_ptr;
647 struct lnet_peer_ni *lp;
649 LASSERT(rcd != NULL);
651 if (event->unlinked) {
652 LNetInvalidateMDHandle(&rcd->rcd_mdh);
656 LASSERT(event->type == LNET_EVENT_SEND ||
657 event->type == LNET_EVENT_REPLY);
659 lp = rcd->rcd_gateway;
662 /* NB: it's called with holding lnet_res_lock, we have a few
663 * places need to hold both locks at the same time, please take
664 * care of lock ordering */
665 lnet_net_lock(lp->lpni_cpt);
666 if (!lnet_isrouter(lp) || lp->lpni_rcd != rcd) {
667 /* ignore if no longer a router or rcd is replaced */
671 if (event->type == LNET_EVENT_SEND) {
672 if (event->status == 0)
676 /* LNET_EVENT_REPLY */
677 /* A successful REPLY means the router is up. If _any_ comms
678 * to the router fail I assume it's down (this will happen if
679 * we ping alive routers to try to detect router death before
680 * apps get burned). */
682 lnet_notify_locked(lp, 1, !event->status, ktime_get_seconds());
683 /* The router checker will wake up very shortly and do the
684 * actual notification.
685 * XXX If 'lp' stops being a router before then, it will still
686 * have the notification pending!!! */
688 if (avoid_asym_router_failure && event->status == 0)
689 lnet_parse_rc_info(rcd);
692 lnet_net_unlock(lp->lpni_cpt);
696 lnet_wait_known_routerstate(void)
698 struct lnet_peer *rtr;
699 struct list_head *entry;
702 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
705 int cpt = lnet_net_lock_current();
708 list_for_each(entry, &the_lnet.ln_routers) {
709 rtr = list_entry(entry, struct lnet_peer,
712 spin_lock(&rtr->lp_lock);
714 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
716 spin_unlock(&rtr->lp_lock);
719 spin_unlock(&rtr->lp_lock);
722 lnet_net_unlock(cpt);
727 set_current_state(TASK_UNINTERRUPTIBLE);
728 schedule_timeout(cfs_time_seconds(1));
732 /* TODO: reimplement */
734 lnet_router_ni_update_locked(struct lnet_peer_ni *gw, __u32 net)
736 struct lnet_route *rte;
737 struct lnet_peer *lp;
739 if ((gw->lpni_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0)
740 lp = gw->lpni_peer_net->lpn_peer;
744 list_for_each_entry(rte, &lp->lp_routes, lr_gwlist) {
745 if (rte->lr_net == net) {
753 lnet_update_ni_status_locked(void)
755 struct lnet_ni *ni = NULL;
759 LASSERT(the_lnet.ln_routing);
761 timeout = router_ping_timeout +
762 MAX(live_router_check_interval, dead_router_check_interval);
764 now = ktime_get_real_seconds();
765 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
766 if (ni->ni_net->net_lnd->lnd_type == LOLND)
769 if (now < ni->ni_last_alive + timeout)
773 /* re-check with lock */
774 if (now < ni->ni_last_alive + timeout) {
779 LASSERT(ni->ni_status != NULL);
781 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
782 CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
783 libcfs_nid2str(ni->ni_nid), timeout);
784 /* NB: so far, this is the only place to set
785 * NI status to "down" */
786 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
792 int lnet_router_pre_mt_start(void)
796 if (check_routers_before_use &&
797 dead_router_check_interval <= 0) {
798 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
799 " set if 'check_routers_before_use' is set"
804 rc = LNetEQAlloc(0, lnet_router_checker_event, &the_lnet.ln_rc_eqh);
806 CERROR("Can't allocate EQ(0): %d\n", rc);
813 void lnet_router_post_mt_start(void)
815 if (check_routers_before_use) {
816 /* Note that a helpful side-effect of pinging all known routers
817 * at startup is that it makes them drop stale connections they
818 * may have to a previous instance of me. */
819 lnet_wait_known_routerstate();
824 lnet_router_cleanup(void)
828 rc = LNetEQFree(the_lnet.ln_rc_eqh);
834 lnet_prune_rc_data(int wait_unlink)
836 wait_unlink = wait_unlink;
840 * This function is called from the monitor thread to check if there are
841 * any active routers that need to be checked.
844 lnet_router_checker_active(void)
846 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
849 /* Router Checker thread needs to run when routing is enabled in
850 * order to call lnet_update_ni_status_locked() */
851 if (the_lnet.ln_routing)
854 /* if there are routers that need to be cleaned up then do so */
855 if (!list_empty(&the_lnet.ln_rcd_deathrow) ||
856 !list_empty(&the_lnet.ln_rcd_zombie))
859 return !list_empty(&the_lnet.ln_routers) &&
860 (live_router_check_interval > 0 ||
861 dead_router_check_interval > 0);
865 lnet_check_routers(void)
867 struct lnet_peer *rtr;
868 struct list_head *entry;
872 cpt = lnet_net_lock_current();
874 version = the_lnet.ln_routers_version;
876 list_for_each(entry, &the_lnet.ln_routers) {
877 rtr = list_entry(entry, struct lnet_peer,
880 /* TODO use discovery to determine if router is alive */
882 /* NB dropped lock */
883 if (version != the_lnet.ln_routers_version) {
884 /* the routers list has changed */
889 if (the_lnet.ln_routing)
890 lnet_update_ni_status_locked();
892 lnet_net_unlock(cpt);
894 lnet_prune_rc_data(0); /* don't wait for UNLINK */
898 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
900 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
902 while (--npages >= 0)
903 __free_page(rb->rb_kiov[npages].kiov_page);
908 static struct lnet_rtrbuf *
909 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
911 int npages = rbp->rbp_npages;
912 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
914 struct lnet_rtrbuf *rb;
917 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
923 for (i = 0; i < npages; i++) {
924 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
925 GFP_KERNEL | __GFP_ZERO);
928 __free_page(rb->rb_kiov[i].kiov_page);
934 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
935 rb->rb_kiov[i].kiov_offset = 0;
936 rb->rb_kiov[i].kiov_page = page;
943 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
945 int npages = rbp->rbp_npages;
946 struct lnet_rtrbuf *rb;
947 struct list_head tmp;
949 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
952 INIT_LIST_HEAD(&tmp);
955 list_splice_init(&rbp->rbp_msgs, &tmp);
956 lnet_drop_routed_msgs_locked(&tmp, cpt);
957 list_splice_init(&rbp->rbp_bufs, &tmp);
958 rbp->rbp_req_nbuffers = 0;
959 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
960 rbp->rbp_mincredits = 0;
961 lnet_net_unlock(cpt);
963 /* Free buffers on the free list. */
964 while (!list_empty(&tmp)) {
965 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
966 list_del(&rb->rb_list);
967 lnet_destroy_rtrbuf(rb, npages);
972 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
974 struct list_head rb_list;
975 struct lnet_rtrbuf *rb;
979 int npages = rbp->rbp_npages;
982 /* If we are called for less buffers than already in the pool, we
983 * just lower the req_nbuffers number and excess buffers will be
984 * thrown away as they are returned to the free list. Credits
985 * then get adjusted as well.
986 * If we already have enough buffers allocated to serve the
987 * increase requested, then we can treat that the same way as we
988 * do the decrease. */
989 num_rb = nbufs - rbp->rbp_nbuffers;
990 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
991 rbp->rbp_req_nbuffers = nbufs;
992 lnet_net_unlock(cpt);
995 /* store the older value of rbp_req_nbuffers and then set it to
996 * the new request to prevent lnet_return_rx_credits_locked() from
997 * freeing buffers that we need to keep around */
998 old_req_nbufs = rbp->rbp_req_nbuffers;
999 rbp->rbp_req_nbuffers = nbufs;
1000 lnet_net_unlock(cpt);
1002 INIT_LIST_HEAD(&rb_list);
1004 /* allocate the buffers on a local list first. If all buffers are
1005 * allocated successfully then join this list to the rbp buffer
1006 * list. If not then free all allocated buffers. */
1007 while (num_rb-- > 0) {
1008 rb = lnet_new_rtrbuf(rbp, cpt);
1010 CERROR("Failed to allocate %d route bufs of %d pages\n",
1014 rbp->rbp_req_nbuffers = old_req_nbufs;
1015 lnet_net_unlock(cpt);
1020 list_add(&rb->rb_list, &rb_list);
1026 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1027 rbp->rbp_nbuffers += num_buffers;
1028 rbp->rbp_credits += num_buffers;
1029 rbp->rbp_mincredits = rbp->rbp_credits;
1030 /* We need to schedule blocked msg using the newly
1032 while (!list_empty(&rbp->rbp_bufs) &&
1033 !list_empty(&rbp->rbp_msgs))
1034 lnet_schedule_blocked_locked(rbp);
1036 lnet_net_unlock(cpt);
1041 while (!list_empty(&rb_list)) {
1042 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1043 list_del(&rb->rb_list);
1044 lnet_destroy_rtrbuf(rb, npages);
1051 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1053 INIT_LIST_HEAD(&rbp->rbp_msgs);
1054 INIT_LIST_HEAD(&rbp->rbp_bufs);
1056 rbp->rbp_npages = npages;
1057 rbp->rbp_credits = 0;
1058 rbp->rbp_mincredits = 0;
1062 lnet_rtrpools_free(int keep_pools)
1064 struct lnet_rtrbufpool *rtrp;
1067 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1070 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1071 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1072 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1073 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1077 cfs_percpt_free(the_lnet.ln_rtrpools);
1078 the_lnet.ln_rtrpools = NULL;
1083 lnet_nrb_tiny_calculate(void)
1085 int nrbs = LNET_NRB_TINY;
1087 if (tiny_router_buffers < 0) {
1088 LCONSOLE_ERROR_MSG(0x10c,
1089 "tiny_router_buffers=%d invalid when "
1090 "routing enabled\n", tiny_router_buffers);
1094 if (tiny_router_buffers > 0)
1095 nrbs = tiny_router_buffers;
1097 nrbs /= LNET_CPT_NUMBER;
1098 return max(nrbs, LNET_NRB_TINY_MIN);
1102 lnet_nrb_small_calculate(void)
1104 int nrbs = LNET_NRB_SMALL;
1106 if (small_router_buffers < 0) {
1107 LCONSOLE_ERROR_MSG(0x10c,
1108 "small_router_buffers=%d invalid when "
1109 "routing enabled\n", small_router_buffers);
1113 if (small_router_buffers > 0)
1114 nrbs = small_router_buffers;
1116 nrbs /= LNET_CPT_NUMBER;
1117 return max(nrbs, LNET_NRB_SMALL_MIN);
1121 lnet_nrb_large_calculate(void)
1123 int nrbs = LNET_NRB_LARGE;
1125 if (large_router_buffers < 0) {
1126 LCONSOLE_ERROR_MSG(0x10c,
1127 "large_router_buffers=%d invalid when "
1128 "routing enabled\n", large_router_buffers);
1132 if (large_router_buffers > 0)
1133 nrbs = large_router_buffers;
1135 nrbs /= LNET_CPT_NUMBER;
1136 return max(nrbs, LNET_NRB_LARGE_MIN);
1140 lnet_rtrpools_alloc(int im_a_router)
1142 struct lnet_rtrbufpool *rtrp;
1149 if (!strcmp(forwarding, "")) {
1150 /* not set either way */
1153 } else if (!strcmp(forwarding, "disabled")) {
1154 /* explicitly disabled */
1156 } else if (!strcmp(forwarding, "enabled")) {
1157 /* explicitly enabled */
1159 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1160 "'enabled' or 'disabled'\n");
1164 nrb_tiny = lnet_nrb_tiny_calculate();
1168 nrb_small = lnet_nrb_small_calculate();
1172 nrb_large = lnet_nrb_large_calculate();
1176 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1178 sizeof(struct lnet_rtrbufpool));
1179 if (the_lnet.ln_rtrpools == NULL) {
1180 LCONSOLE_ERROR_MSG(0x10c,
1181 "Failed to initialize router buffe pool\n");
1185 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1186 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1187 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1192 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1193 LNET_NRB_SMALL_PAGES);
1194 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1199 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1200 LNET_NRB_LARGE_PAGES);
1201 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1207 lnet_net_lock(LNET_LOCK_EX);
1208 the_lnet.ln_routing = 1;
1209 lnet_net_unlock(LNET_LOCK_EX);
1210 wake_up(&the_lnet.ln_mt_waitq);
1214 lnet_rtrpools_free(0);
1219 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1224 struct lnet_rtrbufpool *rtrp;
1226 /* If the provided values for each buffer pool are different than the
1227 * configured values, we need to take action. */
1229 tiny_router_buffers = tiny;
1230 nrb = lnet_nrb_tiny_calculate();
1231 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1232 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1239 small_router_buffers = small;
1240 nrb = lnet_nrb_small_calculate();
1241 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1242 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1249 large_router_buffers = large;
1250 nrb = lnet_nrb_large_calculate();
1251 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1252 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1263 lnet_rtrpools_adjust(int tiny, int small, int large)
1265 /* this function doesn't revert the changes if adding new buffers
1266 * failed. It's up to the user space caller to revert the
1269 if (!the_lnet.ln_routing)
1272 return lnet_rtrpools_adjust_helper(tiny, small, large);
1276 lnet_rtrpools_enable(void)
1280 if (the_lnet.ln_routing)
1283 if (the_lnet.ln_rtrpools == NULL)
1284 /* If routing is turned off, and we have never
1285 * initialized the pools before, just call the
1286 * standard buffer pool allocation routine as
1287 * if we are just configuring this for the first
1289 rc = lnet_rtrpools_alloc(1);
1291 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1295 lnet_net_lock(LNET_LOCK_EX);
1296 the_lnet.ln_routing = 1;
1298 the_lnet.ln_ping_target->pb_info.pi_features &=
1299 ~LNET_PING_FEAT_RTE_DISABLED;
1300 lnet_net_unlock(LNET_LOCK_EX);
1306 lnet_rtrpools_disable(void)
1308 if (!the_lnet.ln_routing)
1311 lnet_net_lock(LNET_LOCK_EX);
1312 the_lnet.ln_routing = 0;
1313 the_lnet.ln_ping_target->pb_info.pi_features |=
1314 LNET_PING_FEAT_RTE_DISABLED;
1316 tiny_router_buffers = 0;
1317 small_router_buffers = 0;
1318 large_router_buffers = 0;
1319 lnet_net_unlock(LNET_LOCK_EX);
1320 lnet_rtrpools_free(1);
1324 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, int alive, time64_t when)
1326 struct lnet_peer_ni *lp = NULL;
1327 time64_t now = ktime_get_seconds();
1328 int cpt = lnet_cpt_of_nid(nid, ni);
1330 LASSERT (!in_interrupt ());
1332 CDEBUG (D_NET, "%s notifying %s: %s\n",
1333 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1334 libcfs_nid2str(nid),
1335 alive ? "up" : "down");
1338 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1339 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1340 libcfs_nid2str(nid), alive ? "birth" : "death",
1341 libcfs_nid2str(ni->ni_nid));
1345 /* can't do predictions... */
1347 CWARN("Ignoring prediction from %s of %s %s "
1348 "%lld seconds in the future\n",
1349 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1350 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1354 if (ni != NULL && !alive && /* LND telling me she's down */
1355 !auto_down) { /* auto-down disabled */
1356 CDEBUG(D_NET, "Auto-down disabled\n");
1362 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1363 lnet_net_unlock(cpt);
1367 lp = lnet_find_peer_ni_locked(nid);
1370 lnet_net_unlock(cpt);
1371 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1376 * It is possible for this function to be called for the same peer
1377 * but with different NIs. We want to synchronize the notification
1378 * between the different calls. So we will use the lpni_cpt to
1379 * grab the net lock.
1381 if (lp->lpni_cpt != cpt) {
1382 lnet_net_unlock(cpt);
1387 /* We can't fully trust LND on reporting exact peer last_alive
1388 * if he notifies us about dead peer. For example ksocklnd can
1389 * call us with when == _time_when_the_node_was_booted_ if
1390 * no connections were successfully established */
1391 if (ni != NULL && !alive && when < lp->lpni_last_alive)
1392 when = lp->lpni_last_alive;
1394 lnet_notify_locked(lp, ni == NULL, alive, when);
1397 lnet_ni_notify_locked(ni, lp);
1399 lnet_peer_ni_decref_locked(lp);
1401 lnet_net_unlock(cpt);
1404 EXPORT_SYMBOL(lnet_notify);