2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2012, Intel Corporation.
6 * This file is part of Portals
7 * http://sourceforge.net/projects/sandiaportals/
9 * Portals is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Portals is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Portals; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LNET
25 #include "../../include/linux/lnet/lib-lnet.h"
27 #define LNET_NRB_TINY_MIN 512 /* min value for each CPT */
28 #define LNET_NRB_TINY (LNET_NRB_TINY_MIN * 4)
29 #define LNET_NRB_SMALL_MIN 4096 /* min value for each CPT */
30 #define LNET_NRB_SMALL (LNET_NRB_SMALL_MIN * 4)
31 #define LNET_NRB_LARGE_MIN 256 /* min value for each CPT */
32 #define LNET_NRB_LARGE (LNET_NRB_LARGE_MIN * 4)
34 static char *forwarding = "";
35 module_param(forwarding, charp, 0444);
36 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
38 static int tiny_router_buffers;
39 module_param(tiny_router_buffers, int, 0444);
40 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
41 static int small_router_buffers;
42 module_param(small_router_buffers, int, 0444);
43 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
44 static int large_router_buffers;
45 module_param(large_router_buffers, int, 0444);
46 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
47 static int peer_buffer_credits;
48 module_param(peer_buffer_credits, int, 0444);
49 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
51 static int auto_down = 1;
52 module_param(auto_down, int, 0444);
53 MODULE_PARM_DESC(auto_down, "Automatically mark peers down on comms error");
56 lnet_peer_buffer_credits(lnet_ni_t *ni)
58 /* NI option overrides LNet default */
59 if (ni->ni_peerrtrcredits > 0)
60 return ni->ni_peerrtrcredits;
61 if (peer_buffer_credits > 0)
62 return peer_buffer_credits;
64 /* As an approximation, allow this peer the same number of router
65 * buffers as it is allowed outstanding sends */
66 return ni->ni_peertxcredits;
70 static int lnet_router_checker(void *);
72 static int check_routers_before_use;
73 module_param(check_routers_before_use, int, 0444);
74 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
76 int avoid_asym_router_failure = 1;
77 module_param(avoid_asym_router_failure, int, 0644);
78 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
80 static int dead_router_check_interval = 60;
81 module_param(dead_router_check_interval, int, 0644);
82 MODULE_PARM_DESC(dead_router_check_interval, "Seconds between dead router health checks (<= 0 to disable)");
84 static int live_router_check_interval = 60;
85 module_param(live_router_check_interval, int, 0644);
86 MODULE_PARM_DESC(live_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
88 static int router_ping_timeout = 50;
89 module_param(router_ping_timeout, int, 0644);
90 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
93 lnet_peers_start_down(void)
95 return check_routers_before_use;
99 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive,
102 if (time_before(when, lp->lp_timestamp)) { /* out of date information */
103 CDEBUG(D_NET, "Out of date\n");
107 lp->lp_timestamp = when; /* update timestamp */
108 lp->lp_ping_deadline = 0; /* disable ping timeout */
110 if (lp->lp_alive_count != 0 && /* got old news */
111 (!lp->lp_alive) == (!alive)) { /* new date for old news */
112 CDEBUG(D_NET, "Old news\n");
116 /* Flag that notification is outstanding */
118 lp->lp_alive_count++;
119 lp->lp_alive = !(!alive); /* 1 bit! */
121 lp->lp_notifylnd |= notifylnd;
123 lp->lp_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
125 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
129 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
134 /* Notify only in 1 thread at any time to ensure ordered notification.
135 * NB individual events can be missed; the only guarantee is that you
136 * always get the most recent news */
138 if (lp->lp_notifying || ni == NULL)
141 lp->lp_notifying = 1;
143 while (lp->lp_notify) {
144 alive = lp->lp_alive;
145 notifylnd = lp->lp_notifylnd;
147 lp->lp_notifylnd = 0;
150 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
151 lnet_net_unlock(lp->lp_cpt);
153 /* A new notification could happen now; I'll handle it
154 * when control returns to me */
156 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
158 lnet_net_lock(lp->lp_cpt);
162 lp->lp_notifying = 0;
166 lnet_rtr_addref_locked(lnet_peer_t *lp)
168 LASSERT(lp->lp_refcount > 0);
169 LASSERT(lp->lp_rtr_refcount >= 0);
171 /* lnet_net_lock must be exclusively locked */
172 lp->lp_rtr_refcount++;
173 if (lp->lp_rtr_refcount == 1) {
174 struct list_head *pos;
176 /* a simple insertion sort */
177 list_for_each_prev(pos, &the_lnet.ln_routers) {
178 lnet_peer_t *rtr = list_entry(pos, lnet_peer_t,
181 if (rtr->lp_nid < lp->lp_nid)
185 list_add(&lp->lp_rtr_list, pos);
186 /* addref for the_lnet.ln_routers */
187 lnet_peer_addref_locked(lp);
188 the_lnet.ln_routers_version++;
193 lnet_rtr_decref_locked(lnet_peer_t *lp)
195 LASSERT(lp->lp_refcount > 0);
196 LASSERT(lp->lp_rtr_refcount > 0);
198 /* lnet_net_lock must be exclusively locked */
199 lp->lp_rtr_refcount--;
200 if (lp->lp_rtr_refcount == 0) {
201 LASSERT(list_empty(&lp->lp_routes));
203 if (lp->lp_rcd != NULL) {
204 list_add(&lp->lp_rcd->rcd_list,
205 &the_lnet.ln_rcd_deathrow);
209 list_del(&lp->lp_rtr_list);
210 /* decref for the_lnet.ln_routers */
211 lnet_peer_decref_locked(lp);
212 the_lnet.ln_routers_version++;
217 lnet_find_net_locked(__u32 net)
219 lnet_remotenet_t *rnet;
220 struct list_head *tmp;
221 struct list_head *rn_list;
223 LASSERT(!the_lnet.ln_shutdown);
225 rn_list = lnet_net2rnethash(net);
226 list_for_each(tmp, rn_list) {
227 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
229 if (rnet->lrn_net == net)
235 static void lnet_shuffle_seed(void)
238 __u32 lnd_type, seed[2];
239 struct timespec64 ts;
241 struct list_head *tmp;
246 cfs_get_random_bytes(seed, sizeof(seed));
248 /* Nodes with small feet have little entropy
249 * the NID for this node gives the most entropy in the low bits */
250 list_for_each(tmp, &the_lnet.ln_nis) {
251 ni = list_entry(tmp, lnet_ni_t, ni_list);
252 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
254 if (lnd_type != LOLND)
255 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
259 cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
263 /* NB expects LNET_LOCK held */
265 lnet_add_route_to_rnet(lnet_remotenet_t *rnet, lnet_route_t *route)
267 unsigned int len = 0;
268 unsigned int offset = 0;
273 list_for_each(e, &rnet->lrn_routes) {
277 /* len+1 positions to add a new entry, also prevents division by 0 */
278 offset = cfs_rand() % (len + 1);
279 list_for_each(e, &rnet->lrn_routes) {
284 list_add(&route->lr_list, e);
285 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
287 the_lnet.ln_remote_nets_version++;
288 lnet_rtr_addref_locked(route->lr_gateway);
292 lnet_add_route(__u32 net, unsigned int hops, lnet_nid_t gateway,
293 unsigned int priority)
296 lnet_remotenet_t *rnet;
297 lnet_remotenet_t *rnet2;
303 CDEBUG(D_NET, "Add route: net %s hops %u priority %u gw %s\n",
304 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
306 if (gateway == LNET_NID_ANY ||
307 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
308 net == LNET_NIDNET(LNET_NID_ANY) ||
309 LNET_NETTYP(net) == LOLND ||
310 LNET_NIDNET(gateway) == net ||
311 hops < 1 || hops > 255)
314 if (lnet_islocalnet(net)) /* it's a local network */
315 return 0; /* ignore the route entry */
317 /* Assume net, route, all new */
318 LIBCFS_ALLOC(route, sizeof(*route));
319 LIBCFS_ALLOC(rnet, sizeof(*rnet));
320 if (route == NULL || rnet == NULL) {
321 CERROR("Out of memory creating route %s %d %s\n",
322 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
324 LIBCFS_FREE(route, sizeof(*route));
326 LIBCFS_FREE(rnet, sizeof(*rnet));
330 INIT_LIST_HEAD(&rnet->lrn_routes);
332 route->lr_hops = hops;
334 route->lr_priority = priority;
336 lnet_net_lock(LNET_LOCK_EX);
338 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway, LNET_LOCK_EX);
340 lnet_net_unlock(LNET_LOCK_EX);
342 LIBCFS_FREE(route, sizeof(*route));
343 LIBCFS_FREE(rnet, sizeof(*rnet));
345 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
346 return 0; /* ignore the route entry */
347 CERROR("Error %d creating route %s %d %s\n", rc,
348 libcfs_net2str(net), hops,
349 libcfs_nid2str(gateway));
354 LASSERT(!the_lnet.ln_shutdown);
356 rnet2 = lnet_find_net_locked(net);
359 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
363 /* Search for a duplicate route (it's a NOOP if it is) */
365 list_for_each(e, &rnet2->lrn_routes) {
366 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
368 if (route2->lr_gateway == route->lr_gateway) {
373 /* our lookups must be true */
374 LASSERT(route2->lr_gateway->lp_nid != gateway);
378 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
379 lnet_add_route_to_rnet(rnet2, route);
381 ni = route->lr_gateway->lp_ni;
382 lnet_net_unlock(LNET_LOCK_EX);
384 /* XXX Assume alive */
385 if (ni->ni_lnd->lnd_notify != NULL)
386 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
388 lnet_net_lock(LNET_LOCK_EX);
391 /* -1 for notify or !add_route */
392 lnet_peer_decref_locked(route->lr_gateway);
393 lnet_net_unlock(LNET_LOCK_EX);
396 LIBCFS_FREE(route, sizeof(*route));
399 LIBCFS_FREE(rnet, sizeof(*rnet));
405 lnet_check_routes(void)
407 lnet_remotenet_t *rnet;
409 lnet_route_t *route2;
410 struct list_head *e1;
411 struct list_head *e2;
413 struct list_head *rn_list;
416 cpt = lnet_net_lock_current();
418 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
419 rn_list = &the_lnet.ln_remote_nets_hash[i];
420 list_for_each(e1, rn_list) {
421 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
424 list_for_each(e2, &rnet->lrn_routes) {
429 route = list_entry(e2, lnet_route_t,
432 if (route2 == NULL) {
437 if (route->lr_gateway->lp_ni ==
438 route2->lr_gateway->lp_ni)
441 nid1 = route->lr_gateway->lp_nid;
442 nid2 = route2->lr_gateway->lp_nid;
445 lnet_net_unlock(cpt);
447 CERROR("Routes to %s via %s and %s not supported\n",
449 libcfs_nid2str(nid1),
450 libcfs_nid2str(nid2));
456 lnet_net_unlock(cpt);
461 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
463 struct lnet_peer *gateway;
464 lnet_remotenet_t *rnet;
466 struct list_head *e1;
467 struct list_head *e2;
469 struct list_head *rn_list;
472 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
473 libcfs_net2str(net), libcfs_nid2str(gw_nid));
475 /* NB Caller may specify either all routes via the given gateway
476 * or a specific route entry actual NIDs) */
478 lnet_net_lock(LNET_LOCK_EX);
479 if (net == LNET_NIDNET(LNET_NID_ANY))
480 rn_list = &the_lnet.ln_remote_nets_hash[0];
482 rn_list = lnet_net2rnethash(net);
485 list_for_each(e1, rn_list) {
486 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
488 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
489 net == rnet->lrn_net))
492 list_for_each(e2, &rnet->lrn_routes) {
493 route = list_entry(e2, lnet_route_t, lr_list);
495 gateway = route->lr_gateway;
496 if (!(gw_nid == LNET_NID_ANY ||
497 gw_nid == gateway->lp_nid))
500 list_del(&route->lr_list);
501 list_del(&route->lr_gwlist);
502 the_lnet.ln_remote_nets_version++;
504 if (list_empty(&rnet->lrn_routes))
505 list_del(&rnet->lrn_list);
509 lnet_rtr_decref_locked(gateway);
510 lnet_peer_decref_locked(gateway);
512 lnet_net_unlock(LNET_LOCK_EX);
514 LIBCFS_FREE(route, sizeof(*route));
517 LIBCFS_FREE(rnet, sizeof(*rnet));
520 lnet_net_lock(LNET_LOCK_EX);
525 if (net == LNET_NIDNET(LNET_NID_ANY) &&
526 ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
527 rn_list = &the_lnet.ln_remote_nets_hash[idx];
530 lnet_net_unlock(LNET_LOCK_EX);
536 lnet_destroy_routes(void)
538 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
542 lnet_get_route(int idx, __u32 *net, __u32 *hops,
543 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
545 struct list_head *e1;
546 struct list_head *e2;
547 lnet_remotenet_t *rnet;
551 struct list_head *rn_list;
553 cpt = lnet_net_lock_current();
555 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
556 rn_list = &the_lnet.ln_remote_nets_hash[i];
557 list_for_each(e1, rn_list) {
558 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
560 list_for_each(e2, &rnet->lrn_routes) {
561 route = list_entry(e2, lnet_route_t,
565 *net = rnet->lrn_net;
566 *hops = route->lr_hops;
567 *priority = route->lr_priority;
568 *gateway = route->lr_gateway->lp_nid;
569 *alive = route->lr_gateway->lp_alive;
570 lnet_net_unlock(cpt);
577 lnet_net_unlock(cpt);
582 lnet_swap_pinginfo(lnet_ping_info_t *info)
585 lnet_ni_status_t *stat;
587 __swab32s(&info->pi_magic);
588 __swab32s(&info->pi_features);
589 __swab32s(&info->pi_pid);
590 __swab32s(&info->pi_nnis);
591 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
592 stat = &info->pi_ni[i];
593 __swab64s(&stat->ns_nid);
594 __swab32s(&stat->ns_status);
599 * parse router-checker pinginfo, record number of down NIs for remote
600 * networks on that router.
603 lnet_parse_rc_info(lnet_rc_data_t *rcd)
605 lnet_ping_info_t *info = rcd->rcd_pinginfo;
606 struct lnet_peer *gw = rcd->rcd_gateway;
612 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
613 lnet_swap_pinginfo(info);
615 /* NB always racing with network! */
616 if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
617 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
618 libcfs_nid2str(gw->lp_nid), info->pi_magic);
619 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
623 gw->lp_ping_feats = info->pi_features;
624 if ((gw->lp_ping_feats & LNET_PING_FEAT_MASK) == 0) {
625 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
626 libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
627 return; /* nothing I can understand */
630 if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) == 0)
631 return; /* can't carry NI status info */
633 list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
634 int ptl_status = LNET_NI_STATUS_INVALID;
639 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
640 lnet_ni_status_t *stat = &info->pi_ni[i];
641 lnet_nid_t nid = stat->ns_nid;
643 if (nid == LNET_NID_ANY) {
644 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
645 libcfs_nid2str(gw->lp_nid));
646 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
650 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
653 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
654 if (LNET_NETTYP(LNET_NIDNET(nid)) != PTLLND)
656 else if (ptl_status != LNET_NI_STATUS_UP)
657 ptl_status = LNET_NI_STATUS_DOWN;
661 if (stat->ns_status == LNET_NI_STATUS_UP) {
662 if (LNET_NIDNET(nid) == rtr->lr_net) {
666 /* ptl NIs are considered down only when
667 * they're all down */
668 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
669 ptl_status = LNET_NI_STATUS_UP;
673 CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
674 libcfs_nid2str(gw->lp_nid), stat->ns_status);
675 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
679 if (up) { /* ignore downed NIs if NI for dest network is up */
683 rtr->lr_downis = down + (ptl_status == LNET_NI_STATUS_DOWN);
688 lnet_router_checker_event(lnet_event_t *event)
690 lnet_rc_data_t *rcd = event->md.user_ptr;
691 struct lnet_peer *lp;
693 LASSERT(rcd != NULL);
695 if (event->unlinked) {
696 LNetInvalidateHandle(&rcd->rcd_mdh);
700 LASSERT(event->type == LNET_EVENT_SEND ||
701 event->type == LNET_EVENT_REPLY);
703 lp = rcd->rcd_gateway;
706 /* NB: it's called with holding lnet_res_lock, we have a few
707 * places need to hold both locks at the same time, please take
708 * care of lock ordering */
709 lnet_net_lock(lp->lp_cpt);
710 if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
711 /* ignore if no longer a router or rcd is replaced */
715 if (event->type == LNET_EVENT_SEND) {
716 lp->lp_ping_notsent = 0;
717 if (event->status == 0)
721 /* LNET_EVENT_REPLY */
722 /* A successful REPLY means the router is up. If _any_ comms
723 * to the router fail I assume it's down (this will happen if
724 * we ping alive routers to try to detect router death before
725 * apps get burned). */
727 lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
728 /* The router checker will wake up very shortly and do the
729 * actual notification.
730 * XXX If 'lp' stops being a router before then, it will still
731 * have the notification pending!!! */
733 if (avoid_asym_router_failure && event->status == 0)
734 lnet_parse_rc_info(rcd);
737 lnet_net_unlock(lp->lp_cpt);
741 lnet_wait_known_routerstate(void)
744 struct list_head *entry;
747 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
750 int cpt = lnet_net_lock_current();
753 list_for_each(entry, &the_lnet.ln_routers) {
754 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
756 if (rtr->lp_alive_count == 0) {
762 lnet_net_unlock(cpt);
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 schedule_timeout(cfs_time_seconds(1));
773 lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net)
777 if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0) {
778 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
779 if (rte->lr_net == net) {
788 lnet_update_ni_status_locked(void)
794 LASSERT(the_lnet.ln_routing);
796 timeout = router_ping_timeout +
797 max(live_router_check_interval, dead_router_check_interval);
799 now = ktime_get_real_seconds();
800 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
801 if (ni->ni_lnd->lnd_type == LOLND)
804 if (now < ni->ni_last_alive + timeout)
808 /* re-check with lock */
809 if (now < ni->ni_last_alive + timeout) {
814 LASSERT(ni->ni_status != NULL);
816 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
817 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
818 libcfs_nid2str(ni->ni_nid), timeout);
819 /* NB: so far, this is the only place to set
820 * NI status to "down" */
821 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
828 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
830 LASSERT(list_empty(&rcd->rcd_list));
831 /* detached from network */
832 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
834 if (rcd->rcd_gateway != NULL) {
835 int cpt = rcd->rcd_gateway->lp_cpt;
838 lnet_peer_decref_locked(rcd->rcd_gateway);
839 lnet_net_unlock(cpt);
842 if (rcd->rcd_pinginfo != NULL)
843 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
845 LIBCFS_FREE(rcd, sizeof(*rcd));
848 static lnet_rc_data_t *
849 lnet_create_rc_data_locked(lnet_peer_t *gateway)
851 lnet_rc_data_t *rcd = NULL;
852 lnet_ping_info_t *pi;
856 lnet_net_unlock(gateway->lp_cpt);
858 LIBCFS_ALLOC(rcd, sizeof(*rcd));
862 LNetInvalidateHandle(&rcd->rcd_mdh);
863 INIT_LIST_HEAD(&rcd->rcd_list);
865 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
869 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
870 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
871 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
873 rcd->rcd_pinginfo = pi;
875 LASSERT(!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
876 rc = LNetMDBind((lnet_md_t){.start = pi,
878 .length = LNET_PINGINFO_SIZE,
879 .threshold = LNET_MD_THRESH_INF,
880 .options = LNET_MD_TRUNCATE,
881 .eq_handle = the_lnet.ln_rc_eqh},
885 CERROR("Can't bind MD: %d\n", rc);
890 lnet_net_lock(gateway->lp_cpt);
891 /* router table changed or someone has created rcd for this gateway */
892 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
893 lnet_net_unlock(gateway->lp_cpt);
897 lnet_peer_addref_locked(gateway);
898 rcd->rcd_gateway = gateway;
899 gateway->lp_rcd = rcd;
900 gateway->lp_ping_notsent = 0;
906 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
907 rc = LNetMDUnlink(rcd->rcd_mdh);
910 lnet_destroy_rc_data(rcd);
913 lnet_net_lock(gateway->lp_cpt);
914 return gateway->lp_rcd;
918 lnet_router_check_interval(lnet_peer_t *rtr)
922 secs = rtr->lp_alive ? live_router_check_interval :
923 dead_router_check_interval;
931 lnet_ping_router_locked(lnet_peer_t *rtr)
933 lnet_rc_data_t *rcd = NULL;
934 unsigned long now = cfs_time_current();
937 lnet_peer_addref_locked(rtr);
939 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
940 cfs_time_after(now, rtr->lp_ping_deadline))
941 lnet_notify_locked(rtr, 1, 0, now);
943 /* Run any outstanding notifications */
944 lnet_ni_notify_locked(rtr->lp_ni, rtr);
946 if (!lnet_isrouter(rtr) ||
947 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
948 /* router table changed or router checker is shutting down */
949 lnet_peer_decref_locked(rtr);
953 rcd = rtr->lp_rcd != NULL ?
954 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
959 secs = lnet_router_check_interval(rtr);
962 "rtr %s %d: deadline %lu ping_notsent %d alive %d alive_count %d lp_ping_timestamp %lu\n",
963 libcfs_nid2str(rtr->lp_nid), secs,
964 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
965 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
967 if (secs != 0 && !rtr->lp_ping_notsent &&
968 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
969 cfs_time_seconds(secs)))) {
971 lnet_process_id_t id;
972 lnet_handle_md_t mdh;
974 id.nid = rtr->lp_nid;
975 id.pid = LUSTRE_SRV_LNET_PID;
976 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
978 rtr->lp_ping_notsent = 1;
979 rtr->lp_ping_timestamp = now;
983 if (rtr->lp_ping_deadline == 0) {
984 rtr->lp_ping_deadline =
985 cfs_time_shift(router_ping_timeout);
988 lnet_net_unlock(rtr->lp_cpt);
990 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
991 LNET_PROTO_PING_MATCHBITS, 0);
993 lnet_net_lock(rtr->lp_cpt);
995 rtr->lp_ping_notsent = 0; /* no event pending */
998 lnet_peer_decref_locked(rtr);
1002 lnet_router_checker_start(void)
1007 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1009 if (check_routers_before_use &&
1010 dead_router_check_interval <= 0) {
1011 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be set if 'check_routers_before_use' is set\n");
1015 if (!the_lnet.ln_routing &&
1016 live_router_check_interval <= 0 &&
1017 dead_router_check_interval <= 0)
1020 sema_init(&the_lnet.ln_rc_signal, 0);
1021 /* EQ size doesn't matter; the callback is guaranteed to get every
1024 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1025 &the_lnet.ln_rc_eqh);
1027 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1031 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1032 rc = PTR_ERR(kthread_run(lnet_router_checker,
1033 NULL, "router_checker"));
1034 if (IS_ERR_VALUE(rc)) {
1035 CERROR("Can't start router checker thread: %d\n", rc);
1036 /* block until event callback signals exit */
1037 down(&the_lnet.ln_rc_signal);
1038 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1040 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1044 if (check_routers_before_use) {
1045 /* Note that a helpful side-effect of pinging all known routers
1046 * at startup is that it makes them drop stale connections they
1047 * may have to a previous instance of me. */
1048 lnet_wait_known_routerstate();
1055 lnet_router_checker_stop(void)
1059 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1062 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1063 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1065 /* block until event callback signals exit */
1066 down(&the_lnet.ln_rc_signal);
1067 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1069 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1074 lnet_prune_rc_data(int wait_unlink)
1076 lnet_rc_data_t *rcd;
1077 lnet_rc_data_t *tmp;
1079 struct list_head head;
1082 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1083 list_empty(&the_lnet.ln_rcd_deathrow) &&
1084 list_empty(&the_lnet.ln_rcd_zombie)))
1087 INIT_LIST_HEAD(&head);
1089 lnet_net_lock(LNET_LOCK_EX);
1091 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1092 /* router checker is stopping, prune all */
1093 list_for_each_entry(lp, &the_lnet.ln_routers,
1095 if (lp->lp_rcd == NULL)
1098 LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1099 list_add(&lp->lp_rcd->rcd_list,
1100 &the_lnet.ln_rcd_deathrow);
1105 /* unlink all RCDs on deathrow list */
1106 list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1108 if (!list_empty(&head)) {
1109 lnet_net_unlock(LNET_LOCK_EX);
1111 list_for_each_entry(rcd, &head, rcd_list)
1112 LNetMDUnlink(rcd->rcd_mdh);
1114 lnet_net_lock(LNET_LOCK_EX);
1117 list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1119 /* release all zombie RCDs */
1120 while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1121 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1123 if (LNetHandleIsInvalid(rcd->rcd_mdh))
1124 list_move(&rcd->rcd_list, &head);
1127 wait_unlink = wait_unlink &&
1128 !list_empty(&the_lnet.ln_rcd_zombie);
1130 lnet_net_unlock(LNET_LOCK_EX);
1132 while (!list_empty(&head)) {
1133 rcd = list_entry(head.next,
1134 lnet_rc_data_t, rcd_list);
1135 list_del_init(&rcd->rcd_list);
1136 lnet_destroy_rc_data(rcd);
1143 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1144 "Waiting for rc buffers to unlink\n");
1145 set_current_state(TASK_UNINTERRUPTIBLE);
1146 schedule_timeout(cfs_time_seconds(1) / 4);
1148 lnet_net_lock(LNET_LOCK_EX);
1151 lnet_net_unlock(LNET_LOCK_EX);
1155 lnet_router_checker(void *arg)
1158 struct list_head *entry;
1160 cfs_block_allsigs();
1162 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1164 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1169 cpt = lnet_net_lock_current();
1171 version = the_lnet.ln_routers_version;
1173 list_for_each(entry, &the_lnet.ln_routers) {
1174 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1176 cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1178 lnet_net_unlock(cpt);
1181 /* the routers list has changed */
1182 if (version != the_lnet.ln_routers_version)
1186 lnet_ping_router_locked(rtr);
1188 /* NB dropped lock */
1189 if (version != the_lnet.ln_routers_version) {
1190 /* the routers list has changed */
1195 if (the_lnet.ln_routing)
1196 lnet_update_ni_status_locked();
1198 lnet_net_unlock(cpt);
1200 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1202 /* Call schedule_timeout() here always adds 1 to load average
1203 * because kernel counts # active tasks as nr_running
1204 * + nr_uninterruptible. */
1205 set_current_state(TASK_INTERRUPTIBLE);
1206 schedule_timeout(cfs_time_seconds(1));
1209 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1211 lnet_prune_rc_data(1); /* wait for UNLINK */
1213 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1214 up(&the_lnet.ln_rc_signal);
1215 /* The unlink event callback will signal final completion */
1220 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1222 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1224 while (--npages >= 0)
1225 __free_page(rb->rb_kiov[npages].kiov_page);
1227 LIBCFS_FREE(rb, sz);
1230 static lnet_rtrbuf_t *
1231 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1233 int npages = rbp->rbp_npages;
1234 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1239 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1245 for (i = 0; i < npages; i++) {
1246 page = alloc_pages_node(
1247 cfs_cpt_spread_node(lnet_cpt_table(), cpt),
1248 GFP_KERNEL | __GFP_ZERO, 0);
1251 __free_page(rb->rb_kiov[i].kiov_page);
1253 LIBCFS_FREE(rb, sz);
1257 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1258 rb->rb_kiov[i].kiov_offset = 0;
1259 rb->rb_kiov[i].kiov_page = page;
1266 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1268 int npages = rbp->rbp_npages;
1272 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1275 LASSERT(list_empty(&rbp->rbp_msgs));
1276 LASSERT(rbp->rbp_credits == rbp->rbp_nbuffers);
1278 while (!list_empty(&rbp->rbp_bufs)) {
1279 LASSERT(rbp->rbp_credits > 0);
1281 rb = list_entry(rbp->rbp_bufs.next,
1282 lnet_rtrbuf_t, rb_list);
1283 list_del(&rb->rb_list);
1284 lnet_destroy_rtrbuf(rb, npages);
1288 LASSERT(rbp->rbp_nbuffers == nbuffers);
1289 LASSERT(rbp->rbp_credits == nbuffers);
1291 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1295 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
1300 if (rbp->rbp_nbuffers != 0) {
1301 LASSERT(rbp->rbp_nbuffers == nbufs);
1305 for (i = 0; i < nbufs; i++) {
1306 rb = lnet_new_rtrbuf(rbp, cpt);
1309 CERROR("Failed to allocate %d router bufs of %d pages\n",
1310 nbufs, rbp->rbp_npages);
1314 rbp->rbp_nbuffers++;
1316 rbp->rbp_mincredits++;
1317 list_add(&rb->rb_list, &rbp->rbp_bufs);
1319 /* No allocation "under fire" */
1320 /* Otherwise we'd need code to schedule blocked msgs etc */
1321 LASSERT(!the_lnet.ln_routing);
1324 LASSERT(rbp->rbp_credits == nbufs);
1329 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1331 INIT_LIST_HEAD(&rbp->rbp_msgs);
1332 INIT_LIST_HEAD(&rbp->rbp_bufs);
1334 rbp->rbp_npages = npages;
1335 rbp->rbp_credits = 0;
1336 rbp->rbp_mincredits = 0;
1340 lnet_rtrpools_free(void)
1342 lnet_rtrbufpool_t *rtrp;
1345 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1348 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1349 lnet_rtrpool_free_bufs(&rtrp[0]);
1350 lnet_rtrpool_free_bufs(&rtrp[1]);
1351 lnet_rtrpool_free_bufs(&rtrp[2]);
1354 cfs_percpt_free(the_lnet.ln_rtrpools);
1355 the_lnet.ln_rtrpools = NULL;
1359 lnet_nrb_tiny_calculate(int npages)
1361 int nrbs = LNET_NRB_TINY;
1363 if (tiny_router_buffers < 0) {
1364 LCONSOLE_ERROR_MSG(0x10c,
1365 "tiny_router_buffers=%d invalid when routing enabled\n",
1366 tiny_router_buffers);
1370 if (tiny_router_buffers > 0)
1371 nrbs = tiny_router_buffers;
1373 nrbs /= LNET_CPT_NUMBER;
1374 return max(nrbs, LNET_NRB_TINY_MIN);
1378 lnet_nrb_small_calculate(int npages)
1380 int nrbs = LNET_NRB_SMALL;
1382 if (small_router_buffers < 0) {
1383 LCONSOLE_ERROR_MSG(0x10c,
1384 "small_router_buffers=%d invalid when routing enabled\n",
1385 small_router_buffers);
1389 if (small_router_buffers > 0)
1390 nrbs = small_router_buffers;
1392 nrbs /= LNET_CPT_NUMBER;
1393 return max(nrbs, LNET_NRB_SMALL_MIN);
1397 lnet_nrb_large_calculate(int npages)
1399 int nrbs = LNET_NRB_LARGE;
1401 if (large_router_buffers < 0) {
1402 LCONSOLE_ERROR_MSG(0x10c,
1403 "large_router_buffers=%d invalid when routing enabled\n",
1404 large_router_buffers);
1408 if (large_router_buffers > 0)
1409 nrbs = large_router_buffers;
1411 nrbs /= LNET_CPT_NUMBER;
1412 return max(nrbs, LNET_NRB_LARGE_MIN);
1416 lnet_rtrpools_alloc(int im_a_router)
1418 lnet_rtrbufpool_t *rtrp;
1420 int small_pages = 1;
1427 large_pages = (LNET_MTU + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1429 if (!strcmp(forwarding, "")) {
1430 /* not set either way */
1433 } else if (!strcmp(forwarding, "disabled")) {
1434 /* explicitly disabled */
1436 } else if (!strcmp(forwarding, "enabled")) {
1437 /* explicitly enabled */
1439 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either 'enabled' or 'disabled'\n");
1443 nrb_tiny = lnet_nrb_tiny_calculate(0);
1447 nrb_small = lnet_nrb_small_calculate(small_pages);
1451 nrb_large = lnet_nrb_large_calculate(large_pages);
1455 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1457 sizeof(lnet_rtrbufpool_t));
1458 if (the_lnet.ln_rtrpools == NULL) {
1459 LCONSOLE_ERROR_MSG(0x10c,
1460 "Failed to initialize router buffe pool\n");
1464 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1465 lnet_rtrpool_init(&rtrp[0], 0);
1466 rc = lnet_rtrpool_alloc_bufs(&rtrp[0], nrb_tiny, i);
1470 lnet_rtrpool_init(&rtrp[1], small_pages);
1471 rc = lnet_rtrpool_alloc_bufs(&rtrp[1], nrb_small, i);
1475 lnet_rtrpool_init(&rtrp[2], large_pages);
1476 rc = lnet_rtrpool_alloc_bufs(&rtrp[2], nrb_large, i);
1481 lnet_net_lock(LNET_LOCK_EX);
1482 the_lnet.ln_routing = 1;
1483 lnet_net_unlock(LNET_LOCK_EX);
1488 lnet_rtrpools_free();
1493 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, unsigned long when)
1495 struct lnet_peer *lp = NULL;
1496 unsigned long now = cfs_time_current();
1497 int cpt = lnet_cpt_of_nid(nid);
1499 LASSERT(!in_interrupt());
1501 CDEBUG(D_NET, "%s notifying %s: %s\n",
1502 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1503 libcfs_nid2str(nid),
1504 alive ? "up" : "down");
1507 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1508 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1509 libcfs_nid2str(nid), alive ? "birth" : "death",
1510 libcfs_nid2str(ni->ni_nid));
1514 /* can't do predictions... */
1515 if (cfs_time_after(when, now)) {
1516 CWARN("Ignoring prediction from %s of %s %s %ld seconds in the future\n",
1517 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1518 libcfs_nid2str(nid), alive ? "up" : "down",
1519 cfs_duration_sec(cfs_time_sub(when, now)));
1523 if (ni != NULL && !alive && /* LND telling me she's down */
1524 !auto_down) { /* auto-down disabled */
1525 CDEBUG(D_NET, "Auto-down disabled\n");
1531 if (the_lnet.ln_shutdown) {
1532 lnet_net_unlock(cpt);
1536 lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1539 lnet_net_unlock(cpt);
1540 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1544 /* We can't fully trust LND on reporting exact peer last_alive
1545 * if he notifies us about dead peer. For example ksocklnd can
1546 * call us with when == _time_when_the_node_was_booted_ if
1547 * no connections were successfully established */
1548 if (ni != NULL && !alive && when < lp->lp_last_alive)
1549 when = lp->lp_last_alive;
1551 lnet_notify_locked(lp, ni == NULL, alive, when);
1553 lnet_ni_notify_locked(ni, lp);
1555 lnet_peer_decref_locked(lp);
1557 lnet_net_unlock(cpt);
1560 EXPORT_SYMBOL(lnet_notify);