1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
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 <lnet/lib-lnet.h>
27 #if defined(__KERNEL__) && defined(LNET_ROUTER)
29 static char *forwarding = "";
30 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
31 "Explicitly enable/disable forwarding between networks");
33 static int tiny_router_buffers = 1024;
34 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
35 "# of 0 payload messages to buffer in the router");
36 static int small_router_buffers = 8192;
37 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
38 "# of small (1 page) messages to buffer in the router");
39 static int large_router_buffers = 512;
40 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
41 "# of large messages to buffer in the router");
42 static int peer_buffer_credits = 0;
43 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
44 "# router buffer credits per peer");
46 static int auto_down = 1;
47 CFS_MODULE_PARM(auto_down, "i", int, 0444,
48 "Automatically mark peers down on comms error");
51 lnet_peer_buffer_credits(lnet_ni_t *ni)
53 /* NI option overrides LNet default */
54 if (ni->ni_peerrtrcredits > 0)
55 return ni->ni_peerrtrcredits;
56 if (peer_buffer_credits > 0)
57 return peer_buffer_credits;
59 /* As an approximation, allow this peer the same number of router
60 * buffers as it is allowed outstanding sends */
61 return ni->ni_peertxcredits;
65 static int lnet_router_checker(void *);
69 lnet_peer_buffer_credits(lnet_ni_t *ni)
76 static int check_routers_before_use = 0;
77 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
78 "Assume routers are down and ping them before use");
80 static int avoid_asym_router_failure = 0;
81 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0444,
82 "Avoid asymmetrical failures: reserved, use at your own risk");
84 int dead_router_check_interval = 0;
85 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0444,
86 "Seconds between dead router health checks (<= 0 to disable)");
88 int live_router_check_interval = 0;
89 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0444,
90 "Seconds between live router health checks (<= 0 to disable)");
92 int router_ping_timeout = 50;
93 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0444,
94 "Seconds to wait for the reply to a router health query");
97 lnet_peers_start_down(void)
99 return check_routers_before_use;
103 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
105 if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
106 CDEBUG(D_NET, "Out of date\n");
110 lp->lp_timestamp = when; /* update timestamp */
111 lp->lp_ping_deadline = 0; /* disable ping timeout */
113 if (lp->lp_alive_count != 0 && /* got old news */
114 (!lp->lp_alive) == (!alive)) { /* new date for old news */
115 CDEBUG(D_NET, "Old news\n");
119 /* Flag that notification is outstanding */
121 lp->lp_alive_count++;
122 lp->lp_alive = !(!alive); /* 1 bit! */
124 lp->lp_notifylnd |= notifylnd;
126 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
130 lnet_do_notify (lnet_peer_t *lp)
132 lnet_ni_t *ni = lp->lp_ni;
138 /* Notify only in 1 thread at any time to ensure ordered notification.
139 * NB individual events can be missed; the only guarantee is that you
140 * always get the most recent news */
142 if (lp->lp_notifying) {
147 lp->lp_notifying = 1;
149 while (lp->lp_notify) {
150 alive = lp->lp_alive;
151 notifylnd = lp->lp_notifylnd;
153 lp->lp_notifylnd = 0;
156 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
159 /* A new notification could happen now; I'll handle it
160 * when control returns to me */
162 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
168 lp->lp_notifying = 0;
175 lnet_rtr_addref_locked(lnet_peer_t *lp)
177 LASSERT (lp->lp_refcount > 0);
178 LASSERT (lp->lp_rtr_refcount >= 0);
180 lp->lp_rtr_refcount++;
181 if (lp->lp_rtr_refcount == 1) {
182 struct list_head *pos;
184 /* a simple insertion sort */
185 list_for_each_prev(pos, &the_lnet.ln_routers) {
186 lnet_peer_t *rtr = list_entry(pos, lnet_peer_t,
189 if (rtr->lp_nid < lp->lp_nid)
193 list_add(&lp->lp_rtr_list, pos);
194 /* addref for the_lnet.ln_routers */
195 lnet_peer_addref_locked(lp);
196 the_lnet.ln_routers_version++;
201 lnet_rtr_decref_locked(lnet_peer_t *lp)
203 LASSERT (lp->lp_refcount > 0);
204 LASSERT (lp->lp_rtr_refcount > 0);
206 lp->lp_rtr_refcount--;
207 if (lp->lp_rtr_refcount == 0) {
208 if (lp->lp_rcd != NULL) {
209 list_add(&lp->lp_rcd->rcd_list,
210 &the_lnet.ln_zombie_rcd);
214 list_del(&lp->lp_rtr_list);
215 /* decref for the_lnet.ln_routers */
216 lnet_peer_decref_locked(lp);
217 the_lnet.ln_routers_version++;
222 lnet_find_net_locked (__u32 net)
224 lnet_remotenet_t *rnet;
225 struct list_head *tmp;
227 LASSERT (!the_lnet.ln_shutdown);
229 list_for_each (tmp, &the_lnet.ln_remote_nets) {
230 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
232 if (rnet->lrn_net == net)
239 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
241 struct list_head zombies;
243 lnet_remotenet_t *rnet;
244 lnet_remotenet_t *rnet2;
246 lnet_route_t *route2;
251 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
252 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
254 if (gateway == LNET_NID_ANY ||
255 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
256 net == LNET_NIDNET(LNET_NID_ANY) ||
257 LNET_NETTYP(net) == LOLND ||
258 LNET_NIDNET(gateway) == net ||
259 hops < 1 || hops > 255)
262 if (lnet_islocalnet(net)) /* it's a local network */
263 return 0; /* ignore the route entry */
265 /* Assume net, route, all new */
266 LIBCFS_ALLOC(route, sizeof(*route));
267 LIBCFS_ALLOC(rnet, sizeof(*rnet));
268 if (route == NULL || rnet == NULL) {
269 CERROR("Out of memory creating route %s %d %s\n",
270 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
272 LIBCFS_FREE(route, sizeof(*route));
274 LIBCFS_FREE(rnet, sizeof(*rnet));
278 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
280 rnet->lrn_hops = hops;
284 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
288 LIBCFS_FREE(route, sizeof(*route));
289 LIBCFS_FREE(rnet, sizeof(*rnet));
291 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
292 return 0; /* ignore the route entry */
294 CERROR("Error %d creating route %s %d %s\n", rc,
295 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
299 LASSERT (!the_lnet.ln_shutdown);
300 CFS_INIT_LIST_HEAD(&zombies);
302 rnet2 = lnet_find_net_locked(net);
305 list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
309 if (hops > rnet2->lrn_hops) {
310 /* New route is longer; ignore it */
312 } else if (hops < rnet2->lrn_hops) {
313 /* new route supercedes all currently known routes to this
315 list_add(&zombies, &rnet2->lrn_routes);
316 list_del_init(&rnet2->lrn_routes);
320 /* New route has the same hopcount as existing routes; search
321 * for a duplicate route (it's a NOOP if it is) */
322 list_for_each (e, &rnet2->lrn_routes) {
323 route2 = list_entry(e, lnet_route_t, lr_list);
325 if (route2->lr_gateway == route->lr_gateway) {
330 /* our loopups must be true */
331 LASSERT (route2->lr_gateway->lp_nid != gateway);
336 ni = route->lr_gateway->lp_ni;
337 lnet_ni_addref_locked(ni);
340 list_add_tail(&route->lr_list, &rnet2->lrn_routes);
341 the_lnet.ln_remote_nets_version++;
343 lnet_rtr_addref_locked(route->lr_gateway);
347 /* XXX Assume alive */
348 if (ni->ni_lnd->lnd_notify != NULL)
349 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
353 lnet_peer_decref_locked(route->lr_gateway);
355 LIBCFS_FREE(route, sizeof(*route));
359 LIBCFS_FREE(rnet, sizeof(*rnet));
361 while (!list_empty(&zombies)) {
362 route = list_entry(zombies.next, lnet_route_t, lr_list);
363 list_del(&route->lr_list);
366 lnet_rtr_decref_locked(route->lr_gateway);
367 lnet_peer_decref_locked(route->lr_gateway);
369 LIBCFS_FREE(route, sizeof(*route));
376 lnet_check_routes (void)
378 lnet_remotenet_t *rnet;
380 lnet_route_t *route2;
381 struct list_head *e1;
382 struct list_head *e2;
386 list_for_each (e1, &the_lnet.ln_remote_nets) {
387 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
390 list_for_each (e2, &rnet->lrn_routes) {
391 route = list_entry(e2, lnet_route_t, lr_list);
395 else if (route->lr_gateway->lp_ni !=
396 route2->lr_gateway->lp_ni) {
399 CERROR("Routes to %s via %s and %s not supported\n",
400 libcfs_net2str(rnet->lrn_net),
401 libcfs_nid2str(route->lr_gateway->lp_nid),
402 libcfs_nid2str(route2->lr_gateway->lp_nid));
413 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
415 lnet_remotenet_t *rnet;
417 struct list_head *e1;
418 struct list_head *e2;
421 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
422 libcfs_net2str(net), libcfs_nid2str(gw_nid));
424 /* NB Caller may specify either all routes via the given gateway
425 * or a specific route entry actual NIDs) */
430 list_for_each (e1, &the_lnet.ln_remote_nets) {
431 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
433 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
434 net == rnet->lrn_net))
437 list_for_each (e2, &rnet->lrn_routes) {
438 route = list_entry(e2, lnet_route_t, lr_list);
440 if (!(gw_nid == LNET_NID_ANY ||
441 gw_nid == route->lr_gateway->lp_nid))
444 list_del(&route->lr_list);
445 the_lnet.ln_remote_nets_version++;
447 if (list_empty(&rnet->lrn_routes))
448 list_del(&rnet->lrn_list);
452 lnet_rtr_decref_locked(route->lr_gateway);
453 lnet_peer_decref_locked(route->lr_gateway);
456 LIBCFS_FREE(route, sizeof (*route));
459 LIBCFS_FREE(rnet, sizeof(*rnet));
471 lnet_destroy_routes (void)
473 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
477 lnet_get_route (int idx, __u32 *net, __u32 *hops,
478 lnet_nid_t *gateway, __u32 *alive)
480 struct list_head *e1;
481 struct list_head *e2;
482 lnet_remotenet_t *rnet;
487 list_for_each (e1, &the_lnet.ln_remote_nets) {
488 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
490 list_for_each (e2, &rnet->lrn_routes) {
491 route = list_entry(e2, lnet_route_t, lr_list);
494 *net = rnet->lrn_net;
495 *hops = rnet->lrn_hops;
496 *gateway = route->lr_gateway->lp_nid;
497 *alive = route->lr_gateway->lp_alive;
509 lnet_swap_pinginfo(lnet_ping_info_t *info)
512 lnet_ni_status_t *stat;
514 __swab32s(&info->pi_version);
515 __swab32s(&info->pi_pid);
516 __swab32s(&info->pi_nnis);
517 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
518 stat = &info->pi_ni[i];
519 __swab64s(&stat->ns_nid);
520 __swab32s(&stat->ns_status);
525 /* Returns # of down NIs, or negative error codes; ignore downed NIs
526 * if a NI in 'net' is up */
528 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
534 lnet_ping_info_t *info;
536 if (!avoid_asym_router_failure)
539 if (rtr->lp_rcd == NULL)
543 return -EINVAL; /* stale lp_rcd */
545 info = rtr->lp_rcd->rcd_pinginfo;
546 LASSERT (info != NULL);
548 /* NB always racing with network! */
549 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
550 lnet_swap_pinginfo(info);
551 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
552 CDEBUG(D_NETERROR, "%s: Unexpected magic %08x\n",
553 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
557 if (info->pi_version == LNET_PROTO_PING_VERSION1)
558 return -ENOENT; /* v1 doesn't carry NI status info */
560 if (info->pi_version != LNET_PROTO_PING_VERSION) {
561 CDEBUG(D_NETERROR, "%s: Unexpected version 0x%x\n",
562 libcfs_nid2str(rtr->lp_nid), info->pi_version);
566 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
567 lnet_ni_status_t *stat = &info->pi_ni[i];
568 lnet_nid_t nid = stat->ns_nid;
570 if (nid == LNET_NID_ANY) {
571 CDEBUG(D_NETERROR, "%s: unexpected LNET_NID_ANY\n",
572 libcfs_nid2str(rtr->lp_nid));
576 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
579 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
580 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
587 if (stat->ns_status != LNET_NI_STATUS_UP) {
588 CDEBUG(D_NETERROR, "%s: Unexpected status 0x%x\n",
589 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
593 /* ignore downed NIs if there's a NI up for dest network */
594 if (LNET_NIDNET(nid) == net)
597 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
601 /* ptl NIs are considered down only when they're all down */
602 return down + (ptl_up ? 0 : ptl_down);
606 lnet_wait_known_routerstate(void)
609 struct list_head *entry;
612 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
618 list_for_each (entry, &the_lnet.ln_routers) {
619 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
621 if (rtr->lp_alive_count == 0) {
633 lnet_router_checker();
635 cfs_pause(cfs_time_seconds(1));
640 lnet_router_checker_event (lnet_event_t *event)
642 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
643 * drop it (that's how come I see _every_ event, even ones that would
645 lnet_rc_data_t *rcd = event->md.user_ptr;
649 if (event->unlinked) {
651 LNetInvalidateHandle(&rcd->rcd_mdh);
655 /* The router checker thread has unlinked the default rc_md
657 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
658 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED;
660 mutex_up(&the_lnet.ln_rc_signal);
665 LASSERT (event->type == LNET_EVENT_SEND ||
666 event->type == LNET_EVENT_REPLY);
668 nid = (event->type == LNET_EVENT_SEND) ?
669 event->target.nid : event->initiator.nid;
671 lp = lnet_find_peer_locked(nid);
673 /* router may have been removed */
674 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
678 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
679 lp->lp_ping_notsent = 0;
681 if (lnet_isrouter(lp) && /* ignore if no longer a router */
682 (event->status != 0 ||
683 event->type == LNET_EVENT_REPLY)) {
685 /* A successful REPLY means the router is up. If _any_ comms
686 * to the router fail I assume it's down (this will happen if
687 * we ping alive routers to try to detect router death before
688 * apps get burned). */
690 lnet_notify_locked(lp, 1, (event->status == 0),
693 /* The router checker will wake up very shortly and do the
694 * actual notification.
695 * XXX If 'lp' stops being a router before then, it will still
696 * have the notification pending!!! */
699 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
700 * was in the peer table and I've not dropped the lock, so no-one else
701 * can have reduced the refcount) */
702 LASSERT(lp->lp_refcount > 1);
704 lnet_peer_decref_locked(lp);
708 lnet_update_ni_status(void)
710 cfs_time_t now = cfs_time_current();
715 LASSERT (the_lnet.ln_routing);
717 timeout = router_ping_timeout +
718 MAX(live_router_check_interval, dead_router_check_interval);
722 list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
723 lnet_ni_status_t *ns = ni->ni_status;
725 LASSERT (ns != NULL);
727 status = LNET_NI_STATUS_UP;
728 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
729 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
730 cfs_time_seconds(timeout))))
731 status = LNET_NI_STATUS_DOWN;
733 if (ns->ns_status != status) {
734 ns->ns_status = status;
735 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
736 libcfs_nid2str(ni->ni_nid), timeout,
737 status == LNET_NI_STATUS_UP ? "up" : "down");
745 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
747 LASSERT (list_empty(&rcd->rcd_list));
748 /* detached from network */
749 LASSERT (LNetHandleIsInvalid(rcd->rcd_mdh));
751 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
752 LIBCFS_FREE(rcd, sizeof(*rcd));
757 lnet_create_rc_data (void)
761 lnet_ping_info_t *pi;
764 LIBCFS_ALLOC(rcd, sizeof(*rcd));
768 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
770 LIBCFS_FREE(rcd, sizeof(*rcd));
774 memset(pi, 0, LNET_PINGINFO_SIZE);
775 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
776 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
777 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
779 rcd->rcd_pinginfo = pi;
780 LNetInvalidateHandle(&rcd->rcd_mdh);
781 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
783 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
784 rc = LNetMDBind((lnet_md_t){.start = pi,
786 .length = LNET_PINGINFO_SIZE,
787 .threshold = LNET_MD_THRESH_INF,
788 .options = LNET_MD_TRUNCATE,
789 .eq_handle = the_lnet.ln_rc_eqh},
793 CERROR("Can't bind MD: %d\n", rc);
794 lnet_destroy_rc_data(rcd);
802 lnet_router_check_interval (lnet_peer_t *rtr)
806 secs = rtr->lp_alive ? live_router_check_interval :
807 dead_router_check_interval;
815 lnet_ping_router_locked (lnet_peer_t *rtr)
818 lnet_rc_data_t *rcd = NULL;
819 cfs_time_t now = cfs_time_current();
822 lnet_peer_addref_locked(rtr);
824 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
825 cfs_time_after(now, rtr->lp_ping_deadline))
826 lnet_notify_locked(rtr, 1, 0, now);
828 if (avoid_asym_router_failure && rtr->lp_rcd == NULL)
833 /* Run any outstanding notifications */
837 rcd = lnet_create_rc_data();
841 if (!lnet_isrouter(rtr)) {
842 lnet_peer_decref_locked(rtr);
844 list_add(&rcd->rcd_list, &the_lnet.ln_zombie_rcd);
845 return; /* router table changed! */
849 LASSERT (rtr->lp_rcd == NULL);
853 secs = lnet_router_check_interval(rtr);
856 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
857 "alive_count %d lp_ping_timestamp %lu\n",
858 libcfs_nid2str(rtr->lp_nid), secs,
859 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
860 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
862 if (secs != 0 && !rtr->lp_ping_notsent &&
863 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
864 cfs_time_seconds(secs)))) {
866 lnet_process_id_t id;
867 lnet_handle_md_t mdh;
869 id.nid = rtr->lp_nid;
870 id.pid = LUSTRE_SRV_LNET_PID;
871 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
873 rtr->lp_ping_notsent = 1;
874 rtr->lp_ping_timestamp = now;
875 mdh = (rtr->lp_rcd == NULL) ? the_lnet.ln_rc_mdh :
876 rtr->lp_rcd->rcd_mdh;
878 if (rtr->lp_ping_deadline == 0)
879 rtr->lp_ping_deadline = cfs_time_shift(router_ping_timeout);
883 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
884 LNET_PROTO_PING_MATCHBITS, 0);
888 rtr->lp_ping_notsent = 0; /* no event pending */
891 lnet_peer_decref_locked(rtr);
896 lnet_router_checker_start(void)
898 static lnet_ping_info_t pinginfo;
907 int router_checker_max_eqsize = 10240;
909 LASSERT (check_routers_before_use);
910 LASSERT (dead_router_check_interval > 0);
914 /* As an approximation, allow each router the same number of
915 * outstanding events as it is allowed outstanding sends */
917 version = the_lnet.ln_routers_version;
918 list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
919 lnet_ni_t *ni = rtr->lp_ni;
920 lnet_process_id_t id;
923 eqsz += ni->ni_peertxcredits;
925 /* one async ping reply per router */
926 id.nid = rtr->lp_nid;
927 id.pid = LUSTRE_SRV_LNET_PID;
931 rc = LNetSetAsync(id, 1);
933 CWARN("LNetSetAsync %s failed: %d\n",
934 libcfs_id2str(id), rc);
939 /* NB router list doesn't change in userspace */
940 LASSERT (version == the_lnet.ln_routers_version);
947 "No router found, not starting router checker\n");
951 /* at least allow a SENT and a REPLY per router */
952 if (router_checker_max_eqsize < 2 * nrtr)
953 router_checker_max_eqsize = 2 * nrtr;
956 if (eqsz > router_checker_max_eqsize)
957 eqsz = router_checker_max_eqsize;
960 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
962 if (check_routers_before_use &&
963 dead_router_check_interval <= 0) {
964 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
965 " set if 'check_routers_before_use' is set"
970 if (!the_lnet.ln_routing &&
971 live_router_check_interval <= 0 &&
972 dead_router_check_interval <= 0)
976 init_mutex_locked(&the_lnet.ln_rc_signal);
977 /* EQ size doesn't matter; the callback is guaranteed to get every
980 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
981 &the_lnet.ln_rc_eqh);
983 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
984 &the_lnet.ln_rc_eqh);
987 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
991 memset(&md, 0, sizeof(md));
993 md.start = &pinginfo;
994 md.length = sizeof(pinginfo);
995 md.options = LNET_MD_TRUNCATE;
996 md.threshold = LNET_MD_THRESH_INF;
997 md.eq_handle = the_lnet.ln_rc_eqh;
998 rc = LNetMDBind(md, LNET_UNLINK, &the_lnet.ln_rc_mdh);
1000 CERROR("Can't bind MD: %d\n", rc);
1001 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1007 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1009 rc = (int)cfs_kernel_thread(lnet_router_checker, NULL, 0);
1011 CERROR("Can't start router checker thread: %d\n", rc);
1012 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1013 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1015 /* block until event callback signals exit */
1016 mutex_down(&the_lnet.ln_rc_signal);
1017 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1019 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1024 if (check_routers_before_use) {
1025 /* Note that a helpful side-effect of pinging all known routers
1026 * at startup is that it makes them drop stale connections they
1027 * may have to a previous instance of me. */
1028 lnet_wait_known_routerstate();
1035 lnet_router_checker_stop (void)
1039 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1042 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1043 the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
1046 /* block until event callback signals exit */
1047 mutex_down(&the_lnet.ln_rc_signal);
1049 while (the_lnet.ln_rc_state != LNET_RC_STATE_UNLINKED) {
1050 lnet_router_checker();
1051 cfs_pause(cfs_time_seconds(1));
1054 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
1056 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1058 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1062 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1065 lnet_prune_zombie_rcd (int wait_unlink)
1067 lnet_rc_data_t *rcd;
1068 lnet_rc_data_t *tmp;
1069 struct list_head free_rcd;
1073 CFS_INIT_LIST_HEAD(&free_rcd);
1077 version = the_lnet.ln_routers_version;
1078 list_for_each_entry_safe (rcd, tmp, &the_lnet.ln_zombie_rcd, rcd_list) {
1079 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1080 list_del(&rcd->rcd_list);
1081 list_add(&rcd->rcd_list, &free_rcd);
1087 LNetMDUnlink(rcd->rcd_mdh);
1090 if (version != the_lnet.ln_routers_version)
1095 while (wait_unlink && !list_empty(&the_lnet.ln_zombie_rcd)) {
1096 rcd = list_entry(the_lnet.ln_zombie_rcd.next,
1097 lnet_rc_data_t, rcd_list);
1098 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1099 list_del(&rcd->rcd_list);
1100 list_add(&rcd->rcd_list, &free_rcd);
1106 LNetMDUnlink(rcd->rcd_mdh);
1109 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1110 "Waiting for rc buffers to unlink\n");
1111 cfs_pause(cfs_time_seconds(1));
1118 while (!list_empty(&free_rcd)) {
1119 rcd = list_entry(free_rcd.next, lnet_rc_data_t, rcd_list);
1120 list_del_init(&rcd->rcd_list);
1121 lnet_destroy_rc_data(rcd);
1127 lnet_router_checker(void *arg)
1131 struct list_head *entry;
1132 lnet_process_id_t rtr_id;
1134 cfs_daemonize("router_checker");
1135 cfs_block_allsigs();
1137 rtr_id.pid = LUSTRE_SRV_LNET_PID;
1139 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1141 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1146 version = the_lnet.ln_routers_version;
1148 list_for_each (entry, &the_lnet.ln_routers) {
1149 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1150 lnet_ping_router_locked(rtr);
1152 /* NB dropped lock */
1153 if (version != the_lnet.ln_routers_version) {
1154 /* the routers list has changed */
1161 if (the_lnet.ln_routing)
1162 lnet_update_ni_status();
1164 lnet_prune_zombie_rcd(0); /* don't wait for UNLINK */
1166 /* Call cfs_pause() here always adds 1 to load average
1167 * because kernel counts # active tasks as nr_running
1168 * + nr_uninterruptible. */
1169 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1170 cfs_time_seconds(1));
1175 list_for_each (entry, &the_lnet.ln_routers) {
1176 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1178 if (rtr->lp_rcd == NULL)
1181 LASSERT (list_empty(&rtr->lp_rcd->rcd_list));
1182 list_add(&rtr->lp_rcd->rcd_list, &the_lnet.ln_zombie_rcd);
1188 lnet_prune_zombie_rcd(1); /* wait for UNLINK */
1190 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
1191 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1193 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1196 /* The unlink event callback will signal final completion */
1201 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1203 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1205 while (--npages >= 0)
1206 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1208 LIBCFS_FREE(rb, sz);
1212 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1214 int npages = rbp->rbp_npages;
1215 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1220 LIBCFS_ALLOC(rb, sz);
1226 for (i = 0; i < npages; i++) {
1227 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1230 cfs_free_page(rb->rb_kiov[i].kiov_page);
1232 LIBCFS_FREE(rb, sz);
1236 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1237 rb->rb_kiov[i].kiov_offset = 0;
1238 rb->rb_kiov[i].kiov_page = page;
1245 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1247 int npages = rbp->rbp_npages;
1251 LASSERT (list_empty(&rbp->rbp_msgs));
1252 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1254 while (!list_empty(&rbp->rbp_bufs)) {
1255 LASSERT (rbp->rbp_credits > 0);
1257 rb = list_entry(rbp->rbp_bufs.next,
1258 lnet_rtrbuf_t, rb_list);
1259 list_del(&rb->rb_list);
1260 lnet_destroy_rtrbuf(rb, npages);
1264 LASSERT (rbp->rbp_nbuffers == nbuffers);
1265 LASSERT (rbp->rbp_credits == nbuffers);
1267 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1271 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1276 if (rbp->rbp_nbuffers != 0) {
1277 LASSERT (rbp->rbp_nbuffers == nbufs);
1281 for (i = 0; i < nbufs; i++) {
1282 rb = lnet_new_rtrbuf(rbp);
1285 CERROR("Failed to allocate %d router bufs of %d pages\n",
1286 nbufs, rbp->rbp_npages);
1290 rbp->rbp_nbuffers++;
1292 rbp->rbp_mincredits++;
1293 list_add(&rb->rb_list, &rbp->rbp_bufs);
1295 /* No allocation "under fire" */
1296 /* Otherwise we'd need code to schedule blocked msgs etc */
1297 LASSERT (!the_lnet.ln_routing);
1300 LASSERT (rbp->rbp_credits == nbufs);
1305 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1307 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1308 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1310 rbp->rbp_npages = npages;
1311 rbp->rbp_credits = 0;
1312 rbp->rbp_mincredits = 0;
1316 lnet_free_rtrpools(void)
1318 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1319 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1320 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1324 lnet_init_rtrpools(void)
1326 int small_pages = 1;
1327 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1329 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1330 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1331 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1336 lnet_alloc_rtrpools(int im_a_router)
1340 if (!strcmp(forwarding, "")) {
1341 /* not set either way */
1344 } else if (!strcmp(forwarding, "disabled")) {
1345 /* explicitly disabled */
1347 } else if (!strcmp(forwarding, "enabled")) {
1348 /* explicitly enabled */
1350 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1351 "'enabled' or 'disabled'\n");
1355 if (tiny_router_buffers <= 0) {
1356 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1357 "routing enabled\n", tiny_router_buffers);
1362 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1363 tiny_router_buffers);
1367 if (small_router_buffers <= 0) {
1368 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1369 " routing enabled\n", small_router_buffers);
1374 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1375 small_router_buffers);
1379 if (large_router_buffers <= 0) {
1380 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1381 " routing enabled\n", large_router_buffers);
1386 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1387 large_router_buffers);
1392 the_lnet.ln_routing = 1;
1398 lnet_free_rtrpools();
1403 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1405 lnet_peer_t *lp = NULL;
1406 cfs_time_t now = cfs_time_current();
1408 LASSERT (!in_interrupt ());
1410 CDEBUG (D_NET, "%s notifying %s: %s\n",
1411 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1412 libcfs_nid2str(nid),
1413 alive ? "up" : "down");
1416 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1417 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1418 libcfs_nid2str(nid), alive ? "birth" : "death",
1419 libcfs_nid2str(ni->ni_nid));
1423 /* can't do predictions... */
1424 if (cfs_time_after(when, now)) {
1425 CWARN ("Ignoring prediction from %s of %s %s "
1426 "%ld seconds in the future\n",
1427 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1428 libcfs_nid2str(nid), alive ? "up" : "down",
1429 cfs_duration_sec(cfs_time_sub(when, now)));
1433 if (ni != NULL && !alive && /* LND telling me she's down */
1434 !auto_down) { /* auto-down disabled */
1435 CDEBUG(D_NET, "Auto-down disabled\n");
1441 lp = lnet_find_peer_locked(nid);
1445 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1449 /* We can't fully trust LND on reporting exact peer last_alive
1450 * if he notifies us about dead peer. For example ksocklnd can
1451 * call us with when == _time_when_the_node_was_booted_ if
1452 * no connections were successfully established */
1453 if (ni != NULL && !alive && when < lp->lp_last_alive)
1454 when = lp->lp_last_alive;
1456 lnet_notify_locked(lp, ni == NULL, alive, when);
1464 lnet_peer_decref_locked(lp);
1469 EXPORT_SYMBOL(lnet_notify);
1472 lnet_get_tunables (void)
1480 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1486 lnet_router_checker (void)
1488 static time_t last = 0;
1489 static int running = 0;
1491 time_t now = cfs_time_current_sec();
1492 int interval = now - last;
1497 /* It's no use to call me again within a sec - all intervals and
1498 * timeouts are measured in seconds */
1499 if (last != 0 && interval < 2)
1503 interval > MAX(live_router_check_interval,
1504 dead_router_check_interval))
1505 CDEBUG(D_NETERROR, "Checker(%d/%d) not called for %d seconds\n",
1506 live_router_check_interval, dead_router_check_interval,
1510 LASSERT (!running); /* recursion check */
1516 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD) {
1517 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1518 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1522 /* consume all pending events */
1527 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1528 * recursion breaker in LNetEQPoll would fail */
1529 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1530 if (rc == 0) /* no event pending */
1533 /* NB a lost SENT prevents me from pinging a router again */
1534 if (rc == -EOVERFLOW) {
1535 CERROR("Dropped an event!!!\n");
1542 lnet_router_checker_event(&ev);
1546 if (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED ||
1547 the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING) {
1552 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1556 version = the_lnet.ln_routers_version;
1557 list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1558 lnet_ping_router_locked(rtr);
1559 LASSERT (version == the_lnet.ln_routers_version);
1564 running = 0; /* lock only needed for the recursion check */
1568 /* NB lnet_peers_start_down depends on me,
1569 * so must be called before any peer creation */
1571 lnet_get_tunables (void)
1575 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1576 if (s != NULL) router_ping_timeout = atoi(s);
1578 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1579 if (s != NULL) live_router_check_interval = atoi(s);
1581 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1582 if (s != NULL) dead_router_check_interval = atoi(s);
1584 /* This replaces old lnd_notify mechanism */
1585 check_routers_before_use = 1;
1586 if (dead_router_check_interval <= 0)
1587 dead_router_check_interval = 30;
1591 lnet_free_rtrpools (void)
1596 lnet_init_rtrpools (void)
1601 lnet_alloc_rtrpools (int im_a_arouter)