1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2011, Whamcloud, Inc.
8 * This file is part of Portals
9 * http://sourceforge.net/projects/sandiaportals/
11 * Portals is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Portals is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Portals; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_LNET
27 #include <lnet/lib-lnet.h>
29 #if defined(__KERNEL__) && defined(LNET_ROUTER)
31 static char *forwarding = "";
32 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
33 "Explicitly enable/disable forwarding between networks");
35 static int tiny_router_buffers = 1024;
36 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
37 "# of 0 payload messages to buffer in the router");
38 static int small_router_buffers = 8192;
39 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
40 "# of small (1 page) messages to buffer in the router");
41 static int large_router_buffers = 512;
42 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
43 "# of large messages to buffer in the router");
44 static int peer_buffer_credits = 0;
45 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
46 "# router buffer credits per peer");
48 static int auto_down = 1;
49 CFS_MODULE_PARM(auto_down, "i", int, 0444,
50 "Automatically mark peers down on comms error");
53 lnet_peer_buffer_credits(lnet_ni_t *ni)
55 /* NI option overrides LNet default */
56 if (ni->ni_peerrtrcredits > 0)
57 return ni->ni_peerrtrcredits;
58 if (peer_buffer_credits > 0)
59 return peer_buffer_credits;
61 /* As an approximation, allow this peer the same number of router
62 * buffers as it is allowed outstanding sends */
63 return ni->ni_peertxcredits;
67 static int lnet_router_checker(void *);
71 lnet_peer_buffer_credits(lnet_ni_t *ni)
78 static int check_routers_before_use = 0;
79 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
80 "Assume routers are down and ping them before use");
82 static int avoid_asym_router_failure = 0;
83 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0444,
84 "Avoid asymmetrical failures: reserved, use at your own risk");
86 static int dead_router_check_interval = 0;
87 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0444,
88 "Seconds between dead router health checks (<= 0 to disable)");
90 static int live_router_check_interval = 0;
91 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0444,
92 "Seconds between live router health checks (<= 0 to disable)");
94 static int router_ping_timeout = 50;
95 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0444,
96 "Seconds to wait for the reply to a router health query");
99 lnet_peers_start_down(void)
101 return check_routers_before_use;
105 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
107 if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
108 CDEBUG(D_NET, "Out of date\n");
112 lp->lp_timestamp = when; /* update timestamp */
113 lp->lp_ping_deadline = 0; /* disable ping timeout */
115 if (lp->lp_alive_count != 0 && /* got old news */
116 (!lp->lp_alive) == (!alive)) { /* new date for old news */
117 CDEBUG(D_NET, "Old news\n");
121 /* Flag that notification is outstanding */
123 lp->lp_alive_count++;
124 lp->lp_alive = !(!alive); /* 1 bit! */
126 lp->lp_notifylnd |= notifylnd;
128 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
132 lnet_do_notify (lnet_peer_t *lp)
134 lnet_ni_t *ni = lp->lp_ni;
140 /* Notify only in 1 thread at any time to ensure ordered notification.
141 * NB individual events can be missed; the only guarantee is that you
142 * always get the most recent news */
144 if (lp->lp_notifying) {
149 lp->lp_notifying = 1;
151 while (lp->lp_notify) {
152 alive = lp->lp_alive;
153 notifylnd = lp->lp_notifylnd;
155 lp->lp_notifylnd = 0;
158 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
161 /* A new notification could happen now; I'll handle it
162 * when control returns to me */
164 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
170 lp->lp_notifying = 0;
177 lnet_rtr_addref_locked(lnet_peer_t *lp)
179 LASSERT (lp->lp_refcount > 0);
180 LASSERT (lp->lp_rtr_refcount >= 0);
182 lp->lp_rtr_refcount++;
183 if (lp->lp_rtr_refcount == 1) {
186 /* a simple insertion sort */
187 cfs_list_for_each_prev(pos, &the_lnet.ln_routers) {
188 lnet_peer_t *rtr = cfs_list_entry(pos, lnet_peer_t,
191 if (rtr->lp_nid < lp->lp_nid)
195 cfs_list_add(&lp->lp_rtr_list, pos);
196 /* addref for the_lnet.ln_routers */
197 lnet_peer_addref_locked(lp);
198 the_lnet.ln_routers_version++;
203 lnet_rtr_decref_locked(lnet_peer_t *lp)
205 LASSERT (lp->lp_refcount > 0);
206 LASSERT (lp->lp_rtr_refcount > 0);
208 lp->lp_rtr_refcount--;
209 if (lp->lp_rtr_refcount == 0) {
210 if (lp->lp_rcd != NULL) {
211 cfs_list_add(&lp->lp_rcd->rcd_list,
212 &the_lnet.ln_zombie_rcd);
216 cfs_list_del(&lp->lp_rtr_list);
217 /* decref for the_lnet.ln_routers */
218 lnet_peer_decref_locked(lp);
219 the_lnet.ln_routers_version++;
224 lnet_find_net_locked (__u32 net)
226 lnet_remotenet_t *rnet;
229 LASSERT (!the_lnet.ln_shutdown);
231 cfs_list_for_each (tmp, &the_lnet.ln_remote_nets) {
232 rnet = cfs_list_entry(tmp, lnet_remotenet_t, lrn_list);
234 if (rnet->lrn_net == net)
240 static void lnet_shuffle_seed(void)
242 static int seeded = 0;
243 int lnd_type, seed[2];
251 cfs_get_random_bytes(seed, sizeof(seed));
253 /* Nodes with small feet have little entropy
254 * the NID for this node gives the most entropy in the low bits */
255 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
256 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
257 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
259 if (lnd_type != LOLND)
260 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
263 cfs_gettimeofday(&tv);
264 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
269 /* NB expects LNET_LOCK held */
271 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
273 unsigned int len = 0;
274 unsigned int offset = 0;
279 cfs_list_for_each (e, &rnet->lrn_routes) {
283 /* len+1 positions to add a new entry, also prevents division by 0 */
284 offset = cfs_rand() % (len + 1);
285 cfs_list_for_each (e, &rnet->lrn_routes) {
290 cfs_list_add(&route->lr_list, e);
292 the_lnet.ln_remote_nets_version++;
293 lnet_rtr_addref_locked(route->lr_gateway);
297 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
300 lnet_remotenet_t *rnet;
301 lnet_remotenet_t *rnet2;
307 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
308 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
310 if (gateway == LNET_NID_ANY ||
311 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
312 net == LNET_NIDNET(LNET_NID_ANY) ||
313 LNET_NETTYP(net) == LOLND ||
314 LNET_NIDNET(gateway) == net ||
315 hops < 1 || hops > 255)
318 if (lnet_islocalnet(net)) /* it's a local network */
319 return 0; /* ignore the route entry */
321 /* Assume net, route, all new */
322 LIBCFS_ALLOC(route, sizeof(*route));
323 LIBCFS_ALLOC(rnet, sizeof(*rnet));
324 if (route == NULL || rnet == NULL) {
325 CERROR("Out of memory creating route %s %d %s\n",
326 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
328 LIBCFS_FREE(route, sizeof(*route));
330 LIBCFS_FREE(rnet, sizeof(*rnet));
334 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
336 route->lr_hops = hops;
340 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
344 LIBCFS_FREE(route, sizeof(*route));
345 LIBCFS_FREE(rnet, sizeof(*rnet));
347 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
348 return 0; /* ignore the route entry */
350 CERROR("Error %d creating route %s %d %s\n", rc,
351 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
355 LASSERT (!the_lnet.ln_shutdown);
357 rnet2 = lnet_find_net_locked(net);
360 cfs_list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
364 /* Search for a duplicate route (it's a NOOP if it is) */
366 cfs_list_for_each (e, &rnet2->lrn_routes) {
367 lnet_route_t *route2 = cfs_list_entry(e, lnet_route_t, lr_list);
369 if (route2->lr_gateway == route->lr_gateway) {
374 /* our lookups must be true */
375 LASSERT (route2->lr_gateway->lp_nid != gateway);
379 ni = route->lr_gateway->lp_ni;
380 lnet_ni_addref_locked(ni);
382 lnet_add_route_to_rnet(rnet2, route);
385 /* XXX Assume alive */
386 if (ni->ni_lnd->lnd_notify != NULL)
387 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
391 lnet_peer_decref_locked(route->lr_gateway);
393 LIBCFS_FREE(route, sizeof(*route));
397 LIBCFS_FREE(rnet, sizeof(*rnet));
403 lnet_check_routes (void)
405 lnet_remotenet_t *rnet;
407 lnet_route_t *route2;
413 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
414 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
417 cfs_list_for_each (e2, &rnet->lrn_routes) {
418 route = cfs_list_entry(e2, lnet_route_t, lr_list);
422 else if (route->lr_gateway->lp_ni !=
423 route2->lr_gateway->lp_ni) {
426 CERROR("Routes to %s via %s and %s not supported\n",
427 libcfs_net2str(rnet->lrn_net),
428 libcfs_nid2str(route->lr_gateway->lp_nid),
429 libcfs_nid2str(route2->lr_gateway->lp_nid));
440 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
442 lnet_remotenet_t *rnet;
448 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
449 libcfs_net2str(net), libcfs_nid2str(gw_nid));
451 /* NB Caller may specify either all routes via the given gateway
452 * or a specific route entry actual NIDs) */
457 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
458 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
460 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
461 net == rnet->lrn_net))
464 cfs_list_for_each (e2, &rnet->lrn_routes) {
465 route = cfs_list_entry(e2, lnet_route_t, lr_list);
467 if (!(gw_nid == LNET_NID_ANY ||
468 gw_nid == route->lr_gateway->lp_nid))
471 cfs_list_del(&route->lr_list);
472 the_lnet.ln_remote_nets_version++;
474 if (cfs_list_empty(&rnet->lrn_routes))
475 cfs_list_del(&rnet->lrn_list);
479 lnet_rtr_decref_locked(route->lr_gateway);
480 lnet_peer_decref_locked(route->lr_gateway);
483 LIBCFS_FREE(route, sizeof (*route));
486 LIBCFS_FREE(rnet, sizeof(*rnet));
498 lnet_destroy_routes (void)
500 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
504 lnet_get_route (int idx, __u32 *net, __u32 *hops,
505 lnet_nid_t *gateway, __u32 *alive)
509 lnet_remotenet_t *rnet;
514 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
515 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
517 cfs_list_for_each (e2, &rnet->lrn_routes) {
518 route = cfs_list_entry(e2, lnet_route_t, lr_list);
521 *net = rnet->lrn_net;
522 *hops = route->lr_hops;
523 *gateway = route->lr_gateway->lp_nid;
524 *alive = route->lr_gateway->lp_alive;
536 lnet_swap_pinginfo(lnet_ping_info_t *info)
539 lnet_ni_status_t *stat;
541 __swab32s(&info->pi_version);
542 __swab32s(&info->pi_pid);
543 __swab32s(&info->pi_nnis);
544 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
545 stat = &info->pi_ni[i];
546 __swab64s(&stat->ns_nid);
547 __swab32s(&stat->ns_status);
552 /* Returns # of down NIs, or negative error codes; ignore downed NIs
553 * if a NI in 'net' is up */
555 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
561 lnet_ping_info_t *info;
563 if (!avoid_asym_router_failure)
566 if (rtr->lp_rcd == NULL)
570 return -EINVAL; /* stale lp_rcd */
572 info = rtr->lp_rcd->rcd_pinginfo;
573 LASSERT (info != NULL);
575 /* NB always racing with network! */
576 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
577 lnet_swap_pinginfo(info);
578 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
579 CNETERR("%s: Unexpected magic %08x\n",
580 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
584 if (info->pi_version == LNET_PROTO_PING_VERSION1)
585 return -ENOENT; /* v1 doesn't carry NI status info */
587 if (info->pi_version != LNET_PROTO_PING_VERSION) {
588 CNETERR("%s: Unexpected version 0x%x\n",
589 libcfs_nid2str(rtr->lp_nid), info->pi_version);
593 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
594 lnet_ni_status_t *stat = &info->pi_ni[i];
595 lnet_nid_t nid = stat->ns_nid;
597 if (nid == LNET_NID_ANY) {
598 CNETERR("%s: unexpected LNET_NID_ANY\n",
599 libcfs_nid2str(rtr->lp_nid));
603 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
606 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
607 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
614 if (stat->ns_status != LNET_NI_STATUS_UP) {
615 CNETERR("%s: Unexpected status 0x%x\n",
616 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
620 /* ignore downed NIs if there's a NI up for dest network */
621 if (LNET_NIDNET(nid) == net)
624 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
628 /* ptl NIs are considered down only when they're all down */
629 return down + (ptl_up ? 0 : ptl_down);
633 lnet_wait_known_routerstate(void)
639 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
645 cfs_list_for_each (entry, &the_lnet.ln_routers) {
646 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
648 if (rtr->lp_alive_count == 0) {
660 lnet_router_checker();
662 cfs_pause(cfs_time_seconds(1));
667 lnet_router_checker_event (lnet_event_t *event)
669 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
670 * drop it (that's how come I see _every_ event, even ones that would
672 lnet_rc_data_t *rcd = event->md.user_ptr;
676 if (event->unlinked) {
678 LNetInvalidateHandle(&rcd->rcd_mdh);
682 /* The router checker thread has unlinked the default rc_md
684 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
685 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED;
687 cfs_up(&the_lnet.ln_rc_signal);
692 LASSERT (event->type == LNET_EVENT_SEND ||
693 event->type == LNET_EVENT_REPLY);
695 nid = (event->type == LNET_EVENT_SEND) ?
696 event->target.nid : event->initiator.nid;
698 lp = lnet_find_peer_locked(nid);
700 /* router may have been removed */
701 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
705 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
706 lp->lp_ping_notsent = 0;
708 if (lnet_isrouter(lp) && /* ignore if no longer a router */
709 (event->status != 0 ||
710 event->type == LNET_EVENT_REPLY)) {
712 /* A successful REPLY means the router is up. If _any_ comms
713 * to the router fail I assume it's down (this will happen if
714 * we ping alive routers to try to detect router death before
715 * apps get burned). */
717 lnet_notify_locked(lp, 1, (event->status == 0),
720 /* The router checker will wake up very shortly and do the
721 * actual notification.
722 * XXX If 'lp' stops being a router before then, it will still
723 * have the notification pending!!! */
726 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
727 * was in the peer table and I've not dropped the lock, so no-one else
728 * can have reduced the refcount) */
729 LASSERT(lp->lp_refcount > 1);
731 lnet_peer_decref_locked(lp);
735 lnet_update_ni_status(void)
737 cfs_time_t now = cfs_time_current();
742 LASSERT (the_lnet.ln_routing);
744 timeout = router_ping_timeout +
745 MAX(live_router_check_interval, dead_router_check_interval);
749 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
750 lnet_ni_status_t *ns = ni->ni_status;
752 LASSERT (ns != NULL);
754 status = LNET_NI_STATUS_UP;
755 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
756 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
757 cfs_time_seconds(timeout))))
758 status = LNET_NI_STATUS_DOWN;
760 if (ns->ns_status != status) {
761 ns->ns_status = status;
762 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
763 libcfs_nid2str(ni->ni_nid), timeout,
764 status == LNET_NI_STATUS_UP ? "up" : "down");
772 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
774 LASSERT (cfs_list_empty(&rcd->rcd_list));
775 /* detached from network */
776 LASSERT (LNetHandleIsInvalid(rcd->rcd_mdh));
778 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
779 LIBCFS_FREE(rcd, sizeof(*rcd));
784 lnet_create_rc_data (void)
788 lnet_ping_info_t *pi;
791 LIBCFS_ALLOC(rcd, sizeof(*rcd));
795 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
797 LIBCFS_FREE(rcd, sizeof(*rcd));
801 memset(pi, 0, LNET_PINGINFO_SIZE);
802 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
803 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
804 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
806 rcd->rcd_pinginfo = pi;
807 LNetInvalidateHandle(&rcd->rcd_mdh);
808 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
810 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
811 rc = LNetMDBind((lnet_md_t){.start = pi,
813 .length = LNET_PINGINFO_SIZE,
814 .threshold = LNET_MD_THRESH_INF,
815 .options = LNET_MD_TRUNCATE,
816 .eq_handle = the_lnet.ln_rc_eqh},
820 CERROR("Can't bind MD: %d\n", rc);
821 lnet_destroy_rc_data(rcd);
829 lnet_router_check_interval (lnet_peer_t *rtr)
833 secs = rtr->lp_alive ? live_router_check_interval :
834 dead_router_check_interval;
842 lnet_ping_router_locked (lnet_peer_t *rtr)
845 lnet_rc_data_t *rcd = NULL;
846 cfs_time_t now = cfs_time_current();
849 lnet_peer_addref_locked(rtr);
851 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
852 cfs_time_after(now, rtr->lp_ping_deadline))
853 lnet_notify_locked(rtr, 1, 0, now);
855 if (avoid_asym_router_failure && rtr->lp_rcd == NULL)
860 /* Run any outstanding notifications */
864 rcd = lnet_create_rc_data();
868 if (!lnet_isrouter(rtr)) {
869 lnet_peer_decref_locked(rtr);
871 cfs_list_add(&rcd->rcd_list, &the_lnet.ln_zombie_rcd);
872 return; /* router table changed! */
876 LASSERT (rtr->lp_rcd == NULL);
880 secs = lnet_router_check_interval(rtr);
883 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
884 "alive_count %d lp_ping_timestamp %lu\n",
885 libcfs_nid2str(rtr->lp_nid), secs,
886 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
887 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
889 if (secs != 0 && !rtr->lp_ping_notsent &&
890 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
891 cfs_time_seconds(secs)))) {
893 lnet_process_id_t id;
894 lnet_handle_md_t mdh;
896 id.nid = rtr->lp_nid;
897 id.pid = LUSTRE_SRV_LNET_PID;
898 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
900 rtr->lp_ping_notsent = 1;
901 rtr->lp_ping_timestamp = now;
902 mdh = (rtr->lp_rcd == NULL) ? the_lnet.ln_rc_mdh :
903 rtr->lp_rcd->rcd_mdh;
905 if (rtr->lp_ping_deadline == 0)
906 rtr->lp_ping_deadline = cfs_time_shift(router_ping_timeout);
910 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
911 LNET_PROTO_PING_MATCHBITS, 0);
915 rtr->lp_ping_notsent = 0; /* no event pending */
918 lnet_peer_decref_locked(rtr);
923 lnet_router_checker_start(void)
925 static lnet_ping_info_t pinginfo;
934 int router_checker_max_eqsize = 10240;
936 LASSERT (check_routers_before_use);
937 LASSERT (dead_router_check_interval > 0);
941 /* As an approximation, allow each router the same number of
942 * outstanding events as it is allowed outstanding sends */
944 version = the_lnet.ln_routers_version;
945 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
946 lnet_ni_t *ni = rtr->lp_ni;
947 lnet_process_id_t id;
950 eqsz += ni->ni_peertxcredits;
952 /* one async ping reply per router */
953 id.nid = rtr->lp_nid;
954 id.pid = LUSTRE_SRV_LNET_PID;
958 rc = LNetSetAsync(id, 1);
960 CWARN("LNetSetAsync %s failed: %d\n",
961 libcfs_id2str(id), rc);
966 /* NB router list doesn't change in userspace */
967 LASSERT (version == the_lnet.ln_routers_version);
974 "No router found, not starting router checker\n");
978 /* at least allow a SENT and a REPLY per router */
979 if (router_checker_max_eqsize < 2 * nrtr)
980 router_checker_max_eqsize = 2 * nrtr;
983 if (eqsz > router_checker_max_eqsize)
984 eqsz = router_checker_max_eqsize;
987 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
989 if (check_routers_before_use &&
990 dead_router_check_interval <= 0) {
991 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
992 " set if 'check_routers_before_use' is set"
997 if (!the_lnet.ln_routing &&
998 live_router_check_interval <= 0 &&
999 dead_router_check_interval <= 0)
1003 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1004 /* EQ size doesn't matter; the callback is guaranteed to get every
1007 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1008 &the_lnet.ln_rc_eqh);
1010 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1011 &the_lnet.ln_rc_eqh);
1014 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1018 memset(&md, 0, sizeof(md));
1020 md.start = &pinginfo;
1021 md.length = sizeof(pinginfo);
1022 md.options = LNET_MD_TRUNCATE;
1023 md.threshold = LNET_MD_THRESH_INF;
1024 md.eq_handle = the_lnet.ln_rc_eqh;
1025 rc = LNetMDBind(md, LNET_UNLINK, &the_lnet.ln_rc_mdh);
1027 CERROR("Can't bind MD: %d\n", rc);
1028 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1034 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1036 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1038 CERROR("Can't start router checker thread: %d\n", rc);
1039 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1040 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1042 /* block until event callback signals exit */
1043 cfs_down(&the_lnet.ln_rc_signal);
1044 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1046 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1051 if (check_routers_before_use) {
1052 /* Note that a helpful side-effect of pinging all known routers
1053 * at startup is that it makes them drop stale connections they
1054 * may have to a previous instance of me. */
1055 lnet_wait_known_routerstate();
1062 lnet_router_checker_stop (void)
1066 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1069 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1070 the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
1073 /* block until event callback signals exit */
1074 cfs_down(&the_lnet.ln_rc_signal);
1076 while (the_lnet.ln_rc_state != LNET_RC_STATE_UNLINKED) {
1077 lnet_router_checker();
1078 cfs_pause(cfs_time_seconds(1));
1081 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
1083 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1085 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1089 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1092 lnet_prune_zombie_rcd (int wait_unlink)
1094 lnet_rc_data_t *rcd;
1095 lnet_rc_data_t *tmp;
1096 cfs_list_t free_rcd;
1100 CFS_INIT_LIST_HEAD(&free_rcd);
1104 version = the_lnet.ln_routers_version;
1105 cfs_list_for_each_entry_safe (rcd, tmp, &the_lnet.ln_zombie_rcd,
1107 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1108 cfs_list_del(&rcd->rcd_list);
1109 cfs_list_add(&rcd->rcd_list, &free_rcd);
1115 LNetMDUnlink(rcd->rcd_mdh);
1118 if (version != the_lnet.ln_routers_version)
1123 while (wait_unlink && !cfs_list_empty(&the_lnet.ln_zombie_rcd)) {
1124 rcd = cfs_list_entry(the_lnet.ln_zombie_rcd.next,
1125 lnet_rc_data_t, rcd_list);
1126 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1127 cfs_list_del(&rcd->rcd_list);
1128 cfs_list_add(&rcd->rcd_list, &free_rcd);
1134 LNetMDUnlink(rcd->rcd_mdh);
1137 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1138 "Waiting for rc buffers to unlink\n");
1139 cfs_pause(cfs_time_seconds(1));
1146 while (!cfs_list_empty(&free_rcd)) {
1147 rcd = cfs_list_entry(free_rcd.next, lnet_rc_data_t, rcd_list);
1148 cfs_list_del_init(&rcd->rcd_list);
1149 lnet_destroy_rc_data(rcd);
1155 lnet_router_checker(void *arg)
1161 cfs_daemonize("router_checker");
1162 cfs_block_allsigs();
1164 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1166 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1171 version = the_lnet.ln_routers_version;
1173 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1174 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1175 lnet_ping_router_locked(rtr);
1177 /* NB dropped lock */
1178 if (version != the_lnet.ln_routers_version) {
1179 /* the routers list has changed */
1186 if (the_lnet.ln_routing)
1187 lnet_update_ni_status();
1189 lnet_prune_zombie_rcd(0); /* don't wait for UNLINK */
1191 /* Call cfs_pause() here always adds 1 to load average
1192 * because kernel counts # active tasks as nr_running
1193 * + nr_uninterruptible. */
1194 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1195 cfs_time_seconds(1));
1200 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1201 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1203 if (rtr->lp_rcd == NULL)
1206 LASSERT (cfs_list_empty(&rtr->lp_rcd->rcd_list));
1207 cfs_list_add(&rtr->lp_rcd->rcd_list, &the_lnet.ln_zombie_rcd);
1213 lnet_prune_zombie_rcd(1); /* wait for UNLINK */
1215 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
1216 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1218 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1221 /* The unlink event callback will signal final completion */
1226 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1228 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1230 while (--npages >= 0)
1231 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1233 LIBCFS_FREE(rb, sz);
1237 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1239 int npages = rbp->rbp_npages;
1240 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1245 LIBCFS_ALLOC(rb, sz);
1251 for (i = 0; i < npages; i++) {
1252 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1255 cfs_free_page(rb->rb_kiov[i].kiov_page);
1257 LIBCFS_FREE(rb, sz);
1261 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1262 rb->rb_kiov[i].kiov_offset = 0;
1263 rb->rb_kiov[i].kiov_page = page;
1270 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1272 int npages = rbp->rbp_npages;
1276 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1277 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1279 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1280 LASSERT (rbp->rbp_credits > 0);
1282 rb = cfs_list_entry(rbp->rbp_bufs.next,
1283 lnet_rtrbuf_t, rb_list);
1284 cfs_list_del(&rb->rb_list);
1285 lnet_destroy_rtrbuf(rb, npages);
1289 LASSERT (rbp->rbp_nbuffers == nbuffers);
1290 LASSERT (rbp->rbp_credits == nbuffers);
1292 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1296 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1301 if (rbp->rbp_nbuffers != 0) {
1302 LASSERT (rbp->rbp_nbuffers == nbufs);
1306 for (i = 0; i < nbufs; i++) {
1307 rb = lnet_new_rtrbuf(rbp);
1310 CERROR("Failed to allocate %d router bufs of %d pages\n",
1311 nbufs, rbp->rbp_npages);
1315 rbp->rbp_nbuffers++;
1317 rbp->rbp_mincredits++;
1318 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1320 /* No allocation "under fire" */
1321 /* Otherwise we'd need code to schedule blocked msgs etc */
1322 LASSERT (!the_lnet.ln_routing);
1325 LASSERT (rbp->rbp_credits == nbufs);
1330 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1332 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1333 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1335 rbp->rbp_npages = npages;
1336 rbp->rbp_credits = 0;
1337 rbp->rbp_mincredits = 0;
1341 lnet_free_rtrpools(void)
1343 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1344 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1345 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1349 lnet_init_rtrpools(void)
1351 int small_pages = 1;
1352 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1354 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1355 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1356 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1361 lnet_alloc_rtrpools(int im_a_router)
1365 if (!strcmp(forwarding, "")) {
1366 /* not set either way */
1369 } else if (!strcmp(forwarding, "disabled")) {
1370 /* explicitly disabled */
1372 } else if (!strcmp(forwarding, "enabled")) {
1373 /* explicitly enabled */
1375 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1376 "'enabled' or 'disabled'\n");
1380 if (tiny_router_buffers <= 0) {
1381 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1382 "routing enabled\n", tiny_router_buffers);
1387 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1388 tiny_router_buffers);
1392 if (small_router_buffers <= 0) {
1393 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1394 " routing enabled\n", small_router_buffers);
1399 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1400 small_router_buffers);
1404 if (large_router_buffers <= 0) {
1405 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1406 " routing enabled\n", large_router_buffers);
1411 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1412 large_router_buffers);
1417 the_lnet.ln_routing = 1;
1423 lnet_free_rtrpools();
1428 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1430 lnet_peer_t *lp = NULL;
1431 cfs_time_t now = cfs_time_current();
1433 LASSERT (!cfs_in_interrupt ());
1435 CDEBUG (D_NET, "%s notifying %s: %s\n",
1436 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1437 libcfs_nid2str(nid),
1438 alive ? "up" : "down");
1441 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1442 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1443 libcfs_nid2str(nid), alive ? "birth" : "death",
1444 libcfs_nid2str(ni->ni_nid));
1448 /* can't do predictions... */
1449 if (cfs_time_after(when, now)) {
1450 CWARN ("Ignoring prediction from %s of %s %s "
1451 "%ld seconds in the future\n",
1452 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1453 libcfs_nid2str(nid), alive ? "up" : "down",
1454 cfs_duration_sec(cfs_time_sub(when, now)));
1458 if (ni != NULL && !alive && /* LND telling me she's down */
1459 !auto_down) { /* auto-down disabled */
1460 CDEBUG(D_NET, "Auto-down disabled\n");
1466 lp = lnet_find_peer_locked(nid);
1470 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1474 /* We can't fully trust LND on reporting exact peer last_alive
1475 * if he notifies us about dead peer. For example ksocklnd can
1476 * call us with when == _time_when_the_node_was_booted_ if
1477 * no connections were successfully established */
1478 if (ni != NULL && !alive && when < lp->lp_last_alive)
1479 when = lp->lp_last_alive;
1481 lnet_notify_locked(lp, ni == NULL, alive, when);
1489 lnet_peer_decref_locked(lp);
1494 EXPORT_SYMBOL(lnet_notify);
1497 lnet_get_tunables (void)
1505 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1511 lnet_router_checker (void)
1513 static time_t last = 0;
1514 static int running = 0;
1516 time_t now = cfs_time_current_sec();
1517 int interval = now - last;
1522 /* It's no use to call me again within a sec - all intervals and
1523 * timeouts are measured in seconds */
1524 if (last != 0 && interval < 2)
1528 interval > MAX(live_router_check_interval,
1529 dead_router_check_interval))
1530 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1531 live_router_check_interval, dead_router_check_interval,
1535 LASSERT (!running); /* recursion check */
1541 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD) {
1542 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1543 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1547 /* consume all pending events */
1552 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1553 * recursion breaker in LNetEQPoll would fail */
1554 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1555 if (rc == 0) /* no event pending */
1558 /* NB a lost SENT prevents me from pinging a router again */
1559 if (rc == -EOVERFLOW) {
1560 CERROR("Dropped an event!!!\n");
1567 lnet_router_checker_event(&ev);
1571 if (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED ||
1572 the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING) {
1577 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1581 version = the_lnet.ln_routers_version;
1582 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1583 lnet_ping_router_locked(rtr);
1584 LASSERT (version == the_lnet.ln_routers_version);
1589 running = 0; /* lock only needed for the recursion check */
1593 /* NB lnet_peers_start_down depends on me,
1594 * so must be called before any peer creation */
1596 lnet_get_tunables (void)
1600 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1601 if (s != NULL) router_ping_timeout = atoi(s);
1603 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1604 if (s != NULL) live_router_check_interval = atoi(s);
1606 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1607 if (s != NULL) dead_router_check_interval = atoi(s);
1609 /* This replaces old lnd_notify mechanism */
1610 check_routers_before_use = 1;
1611 if (dead_router_check_interval <= 0)
1612 dead_router_check_interval = 30;
1616 lnet_free_rtrpools (void)
1621 lnet_init_rtrpools (void)
1626 lnet_alloc_rtrpools (int im_a_arouter)