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)
242 lnet_remotenet_t *rnet;
243 lnet_remotenet_t *rnet2;
249 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
250 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
252 if (gateway == LNET_NID_ANY ||
253 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
254 net == LNET_NIDNET(LNET_NID_ANY) ||
255 LNET_NETTYP(net) == LOLND ||
256 LNET_NIDNET(gateway) == net ||
257 hops < 1 || hops > 255)
260 if (lnet_islocalnet(net)) /* it's a local network */
261 return 0; /* ignore the route entry */
263 /* Assume net, route, all new */
264 LIBCFS_ALLOC(route, sizeof(*route));
265 LIBCFS_ALLOC(rnet, sizeof(*rnet));
266 if (route == NULL || rnet == NULL) {
267 CERROR("Out of memory creating route %s %d %s\n",
268 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
270 LIBCFS_FREE(route, sizeof(*route));
272 LIBCFS_FREE(rnet, sizeof(*rnet));
276 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
278 route->lr_hops = hops;
282 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
286 LIBCFS_FREE(route, sizeof(*route));
287 LIBCFS_FREE(rnet, sizeof(*rnet));
289 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
290 return 0; /* ignore the route entry */
292 CERROR("Error %d creating route %s %d %s\n", rc,
293 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
297 LASSERT (!the_lnet.ln_shutdown);
299 rnet2 = lnet_find_net_locked(net);
302 list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
306 /* Search for a duplicate route (it's a NOOP if it is) */
308 list_for_each (e, &rnet2->lrn_routes) {
309 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
311 if (route2->lr_gateway == route->lr_gateway) {
316 /* our lookups must be true */
317 LASSERT (route2->lr_gateway->lp_nid != gateway);
321 ni = route->lr_gateway->lp_ni;
322 lnet_ni_addref_locked(ni);
324 list_add_tail(&route->lr_list, &rnet2->lrn_routes);
325 the_lnet.ln_remote_nets_version++;
327 lnet_rtr_addref_locked(route->lr_gateway);
331 /* XXX Assume alive */
332 if (ni->ni_lnd->lnd_notify != NULL)
333 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
337 lnet_peer_decref_locked(route->lr_gateway);
339 LIBCFS_FREE(route, sizeof(*route));
343 LIBCFS_FREE(rnet, sizeof(*rnet));
349 lnet_check_routes (void)
351 lnet_remotenet_t *rnet;
353 lnet_route_t *route2;
354 struct list_head *e1;
355 struct list_head *e2;
359 list_for_each (e1, &the_lnet.ln_remote_nets) {
360 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
363 list_for_each (e2, &rnet->lrn_routes) {
364 route = list_entry(e2, lnet_route_t, lr_list);
368 else if (route->lr_gateway->lp_ni !=
369 route2->lr_gateway->lp_ni) {
372 CERROR("Routes to %s via %s and %s not supported\n",
373 libcfs_net2str(rnet->lrn_net),
374 libcfs_nid2str(route->lr_gateway->lp_nid),
375 libcfs_nid2str(route2->lr_gateway->lp_nid));
386 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
388 lnet_remotenet_t *rnet;
390 struct list_head *e1;
391 struct list_head *e2;
394 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
395 libcfs_net2str(net), libcfs_nid2str(gw_nid));
397 /* NB Caller may specify either all routes via the given gateway
398 * or a specific route entry actual NIDs) */
403 list_for_each (e1, &the_lnet.ln_remote_nets) {
404 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
406 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
407 net == rnet->lrn_net))
410 list_for_each (e2, &rnet->lrn_routes) {
411 route = list_entry(e2, lnet_route_t, lr_list);
413 if (!(gw_nid == LNET_NID_ANY ||
414 gw_nid == route->lr_gateway->lp_nid))
417 list_del(&route->lr_list);
418 the_lnet.ln_remote_nets_version++;
420 if (list_empty(&rnet->lrn_routes))
421 list_del(&rnet->lrn_list);
425 lnet_rtr_decref_locked(route->lr_gateway);
426 lnet_peer_decref_locked(route->lr_gateway);
429 LIBCFS_FREE(route, sizeof (*route));
432 LIBCFS_FREE(rnet, sizeof(*rnet));
444 lnet_destroy_routes (void)
446 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
450 lnet_get_route (int idx, __u32 *net, __u32 *hops,
451 lnet_nid_t *gateway, __u32 *alive)
453 struct list_head *e1;
454 struct list_head *e2;
455 lnet_remotenet_t *rnet;
460 list_for_each (e1, &the_lnet.ln_remote_nets) {
461 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
463 list_for_each (e2, &rnet->lrn_routes) {
464 route = list_entry(e2, lnet_route_t, lr_list);
467 *net = rnet->lrn_net;
468 *hops = route->lr_hops;
469 *gateway = route->lr_gateway->lp_nid;
470 *alive = route->lr_gateway->lp_alive;
482 lnet_swap_pinginfo(lnet_ping_info_t *info)
485 lnet_ni_status_t *stat;
487 __swab32s(&info->pi_version);
488 __swab32s(&info->pi_pid);
489 __swab32s(&info->pi_nnis);
490 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
491 stat = &info->pi_ni[i];
492 __swab64s(&stat->ns_nid);
493 __swab32s(&stat->ns_status);
498 /* Returns # of down NIs, or negative error codes; ignore downed NIs
499 * if a NI in 'net' is up */
501 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
507 lnet_ping_info_t *info;
509 if (!avoid_asym_router_failure)
512 if (rtr->lp_rcd == NULL)
516 return -EINVAL; /* stale lp_rcd */
518 info = rtr->lp_rcd->rcd_pinginfo;
519 LASSERT (info != NULL);
521 /* NB always racing with network! */
522 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
523 lnet_swap_pinginfo(info);
524 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
525 CDEBUG(D_NETERROR, "%s: Unexpected magic %08x\n",
526 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
530 if (info->pi_version == LNET_PROTO_PING_VERSION1)
531 return -ENOENT; /* v1 doesn't carry NI status info */
533 if (info->pi_version != LNET_PROTO_PING_VERSION) {
534 CDEBUG(D_NETERROR, "%s: Unexpected version 0x%x\n",
535 libcfs_nid2str(rtr->lp_nid), info->pi_version);
539 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
540 lnet_ni_status_t *stat = &info->pi_ni[i];
541 lnet_nid_t nid = stat->ns_nid;
543 if (nid == LNET_NID_ANY) {
544 CDEBUG(D_NETERROR, "%s: unexpected LNET_NID_ANY\n",
545 libcfs_nid2str(rtr->lp_nid));
549 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
552 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
553 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
560 if (stat->ns_status != LNET_NI_STATUS_UP) {
561 CDEBUG(D_NETERROR, "%s: Unexpected status 0x%x\n",
562 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
566 /* ignore downed NIs if there's a NI up for dest network */
567 if (LNET_NIDNET(nid) == net)
570 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
574 /* ptl NIs are considered down only when they're all down */
575 return down + (ptl_up ? 0 : ptl_down);
579 lnet_wait_known_routerstate(void)
582 struct list_head *entry;
585 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
591 list_for_each (entry, &the_lnet.ln_routers) {
592 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
594 if (rtr->lp_alive_count == 0) {
606 lnet_router_checker();
608 cfs_pause(cfs_time_seconds(1));
613 lnet_router_checker_event (lnet_event_t *event)
615 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
616 * drop it (that's how come I see _every_ event, even ones that would
618 lnet_rc_data_t *rcd = event->md.user_ptr;
622 if (event->unlinked) {
624 LNetInvalidateHandle(&rcd->rcd_mdh);
628 /* The router checker thread has unlinked the default rc_md
630 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
631 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED;
633 mutex_up(&the_lnet.ln_rc_signal);
638 LASSERT (event->type == LNET_EVENT_SEND ||
639 event->type == LNET_EVENT_REPLY);
641 nid = (event->type == LNET_EVENT_SEND) ?
642 event->target.nid : event->initiator.nid;
644 lp = lnet_find_peer_locked(nid);
646 /* router may have been removed */
647 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
651 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
652 lp->lp_ping_notsent = 0;
654 if (lnet_isrouter(lp) && /* ignore if no longer a router */
655 (event->status != 0 ||
656 event->type == LNET_EVENT_REPLY)) {
658 /* A successful REPLY means the router is up. If _any_ comms
659 * to the router fail I assume it's down (this will happen if
660 * we ping alive routers to try to detect router death before
661 * apps get burned). */
663 lnet_notify_locked(lp, 1, (event->status == 0),
666 /* The router checker will wake up very shortly and do the
667 * actual notification.
668 * XXX If 'lp' stops being a router before then, it will still
669 * have the notification pending!!! */
672 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
673 * was in the peer table and I've not dropped the lock, so no-one else
674 * can have reduced the refcount) */
675 LASSERT(lp->lp_refcount > 1);
677 lnet_peer_decref_locked(lp);
681 lnet_update_ni_status(void)
683 cfs_time_t now = cfs_time_current();
688 LASSERT (the_lnet.ln_routing);
690 timeout = router_ping_timeout +
691 MAX(live_router_check_interval, dead_router_check_interval);
695 list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
696 lnet_ni_status_t *ns = ni->ni_status;
698 LASSERT (ns != NULL);
700 status = LNET_NI_STATUS_UP;
701 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
702 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
703 cfs_time_seconds(timeout))))
704 status = LNET_NI_STATUS_DOWN;
706 if (ns->ns_status != status) {
707 ns->ns_status = status;
708 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
709 libcfs_nid2str(ni->ni_nid), timeout,
710 status == LNET_NI_STATUS_UP ? "up" : "down");
718 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
720 LASSERT (list_empty(&rcd->rcd_list));
721 /* detached from network */
722 LASSERT (LNetHandleIsInvalid(rcd->rcd_mdh));
724 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
725 LIBCFS_FREE(rcd, sizeof(*rcd));
730 lnet_create_rc_data (void)
734 lnet_ping_info_t *pi;
737 LIBCFS_ALLOC(rcd, sizeof(*rcd));
741 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
743 LIBCFS_FREE(rcd, sizeof(*rcd));
747 memset(pi, 0, LNET_PINGINFO_SIZE);
748 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
749 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
750 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
752 rcd->rcd_pinginfo = pi;
753 LNetInvalidateHandle(&rcd->rcd_mdh);
754 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
756 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
757 rc = LNetMDBind((lnet_md_t){.start = pi,
759 .length = LNET_PINGINFO_SIZE,
760 .threshold = LNET_MD_THRESH_INF,
761 .options = LNET_MD_TRUNCATE,
762 .eq_handle = the_lnet.ln_rc_eqh},
766 CERROR("Can't bind MD: %d\n", rc);
767 lnet_destroy_rc_data(rcd);
775 lnet_router_check_interval (lnet_peer_t *rtr)
779 secs = rtr->lp_alive ? live_router_check_interval :
780 dead_router_check_interval;
788 lnet_ping_router_locked (lnet_peer_t *rtr)
791 lnet_rc_data_t *rcd = NULL;
792 cfs_time_t now = cfs_time_current();
795 lnet_peer_addref_locked(rtr);
797 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
798 cfs_time_after(now, rtr->lp_ping_deadline))
799 lnet_notify_locked(rtr, 1, 0, now);
801 if (avoid_asym_router_failure && rtr->lp_rcd == NULL)
806 /* Run any outstanding notifications */
810 rcd = lnet_create_rc_data();
814 if (!lnet_isrouter(rtr)) {
815 lnet_peer_decref_locked(rtr);
817 list_add(&rcd->rcd_list, &the_lnet.ln_zombie_rcd);
818 return; /* router table changed! */
822 LASSERT (rtr->lp_rcd == NULL);
826 secs = lnet_router_check_interval(rtr);
829 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
830 "alive_count %d lp_ping_timestamp %lu\n",
831 libcfs_nid2str(rtr->lp_nid), secs,
832 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
833 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
835 if (secs != 0 && !rtr->lp_ping_notsent &&
836 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
837 cfs_time_seconds(secs)))) {
839 lnet_process_id_t id;
840 lnet_handle_md_t mdh;
842 id.nid = rtr->lp_nid;
843 id.pid = LUSTRE_SRV_LNET_PID;
844 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
846 rtr->lp_ping_notsent = 1;
847 rtr->lp_ping_timestamp = now;
848 mdh = (rtr->lp_rcd == NULL) ? the_lnet.ln_rc_mdh :
849 rtr->lp_rcd->rcd_mdh;
851 if (rtr->lp_ping_deadline == 0)
852 rtr->lp_ping_deadline = cfs_time_shift(router_ping_timeout);
856 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
857 LNET_PROTO_PING_MATCHBITS, 0);
861 rtr->lp_ping_notsent = 0; /* no event pending */
864 lnet_peer_decref_locked(rtr);
869 lnet_router_checker_start(void)
871 static lnet_ping_info_t pinginfo;
880 int router_checker_max_eqsize = 10240;
882 LASSERT (check_routers_before_use);
883 LASSERT (dead_router_check_interval > 0);
887 /* As an approximation, allow each router the same number of
888 * outstanding events as it is allowed outstanding sends */
890 version = the_lnet.ln_routers_version;
891 list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
892 lnet_ni_t *ni = rtr->lp_ni;
893 lnet_process_id_t id;
896 eqsz += ni->ni_peertxcredits;
898 /* one async ping reply per router */
899 id.nid = rtr->lp_nid;
900 id.pid = LUSTRE_SRV_LNET_PID;
904 rc = LNetSetAsync(id, 1);
906 CWARN("LNetSetAsync %s failed: %d\n",
907 libcfs_id2str(id), rc);
912 /* NB router list doesn't change in userspace */
913 LASSERT (version == the_lnet.ln_routers_version);
920 "No router found, not starting router checker\n");
924 /* at least allow a SENT and a REPLY per router */
925 if (router_checker_max_eqsize < 2 * nrtr)
926 router_checker_max_eqsize = 2 * nrtr;
929 if (eqsz > router_checker_max_eqsize)
930 eqsz = router_checker_max_eqsize;
933 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
935 if (check_routers_before_use &&
936 dead_router_check_interval <= 0) {
937 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
938 " set if 'check_routers_before_use' is set"
943 if (!the_lnet.ln_routing &&
944 live_router_check_interval <= 0 &&
945 dead_router_check_interval <= 0)
949 init_mutex_locked(&the_lnet.ln_rc_signal);
950 /* EQ size doesn't matter; the callback is guaranteed to get every
953 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
954 &the_lnet.ln_rc_eqh);
956 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
957 &the_lnet.ln_rc_eqh);
960 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
964 memset(&md, 0, sizeof(md));
966 md.start = &pinginfo;
967 md.length = sizeof(pinginfo);
968 md.options = LNET_MD_TRUNCATE;
969 md.threshold = LNET_MD_THRESH_INF;
970 md.eq_handle = the_lnet.ln_rc_eqh;
971 rc = LNetMDBind(md, LNET_UNLINK, &the_lnet.ln_rc_mdh);
973 CERROR("Can't bind MD: %d\n", rc);
974 rc = LNetEQFree(the_lnet.ln_rc_eqh);
980 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
982 rc = (int)cfs_kernel_thread(lnet_router_checker, NULL, 0);
984 CERROR("Can't start router checker thread: %d\n", rc);
985 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
986 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
988 /* block until event callback signals exit */
989 mutex_down(&the_lnet.ln_rc_signal);
990 rc = LNetEQFree(the_lnet.ln_rc_eqh);
992 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
997 if (check_routers_before_use) {
998 /* Note that a helpful side-effect of pinging all known routers
999 * at startup is that it makes them drop stale connections they
1000 * may have to a previous instance of me. */
1001 lnet_wait_known_routerstate();
1008 lnet_router_checker_stop (void)
1012 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1015 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1016 the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
1019 /* block until event callback signals exit */
1020 mutex_down(&the_lnet.ln_rc_signal);
1022 while (the_lnet.ln_rc_state != LNET_RC_STATE_UNLINKED) {
1023 lnet_router_checker();
1024 cfs_pause(cfs_time_seconds(1));
1027 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
1029 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1031 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1035 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1038 lnet_prune_zombie_rcd (int wait_unlink)
1040 lnet_rc_data_t *rcd;
1041 lnet_rc_data_t *tmp;
1042 struct list_head free_rcd;
1046 CFS_INIT_LIST_HEAD(&free_rcd);
1050 version = the_lnet.ln_routers_version;
1051 list_for_each_entry_safe (rcd, tmp, &the_lnet.ln_zombie_rcd, rcd_list) {
1052 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1053 list_del(&rcd->rcd_list);
1054 list_add(&rcd->rcd_list, &free_rcd);
1060 LNetMDUnlink(rcd->rcd_mdh);
1063 if (version != the_lnet.ln_routers_version)
1068 while (wait_unlink && !list_empty(&the_lnet.ln_zombie_rcd)) {
1069 rcd = list_entry(the_lnet.ln_zombie_rcd.next,
1070 lnet_rc_data_t, rcd_list);
1071 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1072 list_del(&rcd->rcd_list);
1073 list_add(&rcd->rcd_list, &free_rcd);
1079 LNetMDUnlink(rcd->rcd_mdh);
1082 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1083 "Waiting for rc buffers to unlink\n");
1084 cfs_pause(cfs_time_seconds(1));
1091 while (!list_empty(&free_rcd)) {
1092 rcd = list_entry(free_rcd.next, lnet_rc_data_t, rcd_list);
1093 list_del_init(&rcd->rcd_list);
1094 lnet_destroy_rc_data(rcd);
1100 lnet_router_checker(void *arg)
1104 struct list_head *entry;
1105 lnet_process_id_t rtr_id;
1107 cfs_daemonize("router_checker");
1108 cfs_block_allsigs();
1110 rtr_id.pid = LUSTRE_SRV_LNET_PID;
1112 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1114 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1119 version = the_lnet.ln_routers_version;
1121 list_for_each (entry, &the_lnet.ln_routers) {
1122 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1123 lnet_ping_router_locked(rtr);
1125 /* NB dropped lock */
1126 if (version != the_lnet.ln_routers_version) {
1127 /* the routers list has changed */
1134 if (the_lnet.ln_routing)
1135 lnet_update_ni_status();
1137 lnet_prune_zombie_rcd(0); /* don't wait for UNLINK */
1139 /* Call cfs_pause() here always adds 1 to load average
1140 * because kernel counts # active tasks as nr_running
1141 * + nr_uninterruptible. */
1142 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1143 cfs_time_seconds(1));
1148 list_for_each (entry, &the_lnet.ln_routers) {
1149 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1151 if (rtr->lp_rcd == NULL)
1154 LASSERT (list_empty(&rtr->lp_rcd->rcd_list));
1155 list_add(&rtr->lp_rcd->rcd_list, &the_lnet.ln_zombie_rcd);
1161 lnet_prune_zombie_rcd(1); /* wait for UNLINK */
1163 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
1164 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1166 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1169 /* The unlink event callback will signal final completion */
1174 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1176 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1178 while (--npages >= 0)
1179 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1181 LIBCFS_FREE(rb, sz);
1185 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1187 int npages = rbp->rbp_npages;
1188 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1193 LIBCFS_ALLOC(rb, sz);
1199 for (i = 0; i < npages; i++) {
1200 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1203 cfs_free_page(rb->rb_kiov[i].kiov_page);
1205 LIBCFS_FREE(rb, sz);
1209 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1210 rb->rb_kiov[i].kiov_offset = 0;
1211 rb->rb_kiov[i].kiov_page = page;
1218 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1220 int npages = rbp->rbp_npages;
1224 LASSERT (list_empty(&rbp->rbp_msgs));
1225 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1227 while (!list_empty(&rbp->rbp_bufs)) {
1228 LASSERT (rbp->rbp_credits > 0);
1230 rb = list_entry(rbp->rbp_bufs.next,
1231 lnet_rtrbuf_t, rb_list);
1232 list_del(&rb->rb_list);
1233 lnet_destroy_rtrbuf(rb, npages);
1237 LASSERT (rbp->rbp_nbuffers == nbuffers);
1238 LASSERT (rbp->rbp_credits == nbuffers);
1240 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1244 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1249 if (rbp->rbp_nbuffers != 0) {
1250 LASSERT (rbp->rbp_nbuffers == nbufs);
1254 for (i = 0; i < nbufs; i++) {
1255 rb = lnet_new_rtrbuf(rbp);
1258 CERROR("Failed to allocate %d router bufs of %d pages\n",
1259 nbufs, rbp->rbp_npages);
1263 rbp->rbp_nbuffers++;
1265 rbp->rbp_mincredits++;
1266 list_add(&rb->rb_list, &rbp->rbp_bufs);
1268 /* No allocation "under fire" */
1269 /* Otherwise we'd need code to schedule blocked msgs etc */
1270 LASSERT (!the_lnet.ln_routing);
1273 LASSERT (rbp->rbp_credits == nbufs);
1278 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1280 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1281 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1283 rbp->rbp_npages = npages;
1284 rbp->rbp_credits = 0;
1285 rbp->rbp_mincredits = 0;
1289 lnet_free_rtrpools(void)
1291 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1292 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1293 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1297 lnet_init_rtrpools(void)
1299 int small_pages = 1;
1300 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1302 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1303 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1304 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1309 lnet_alloc_rtrpools(int im_a_router)
1313 if (!strcmp(forwarding, "")) {
1314 /* not set either way */
1317 } else if (!strcmp(forwarding, "disabled")) {
1318 /* explicitly disabled */
1320 } else if (!strcmp(forwarding, "enabled")) {
1321 /* explicitly enabled */
1323 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1324 "'enabled' or 'disabled'\n");
1328 if (tiny_router_buffers <= 0) {
1329 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1330 "routing enabled\n", tiny_router_buffers);
1335 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1336 tiny_router_buffers);
1340 if (small_router_buffers <= 0) {
1341 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1342 " routing enabled\n", small_router_buffers);
1347 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1348 small_router_buffers);
1352 if (large_router_buffers <= 0) {
1353 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1354 " routing enabled\n", large_router_buffers);
1359 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1360 large_router_buffers);
1365 the_lnet.ln_routing = 1;
1371 lnet_free_rtrpools();
1376 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1378 lnet_peer_t *lp = NULL;
1379 cfs_time_t now = cfs_time_current();
1381 LASSERT (!in_interrupt ());
1383 CDEBUG (D_NET, "%s notifying %s: %s\n",
1384 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1385 libcfs_nid2str(nid),
1386 alive ? "up" : "down");
1389 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1390 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1391 libcfs_nid2str(nid), alive ? "birth" : "death",
1392 libcfs_nid2str(ni->ni_nid));
1396 /* can't do predictions... */
1397 if (cfs_time_after(when, now)) {
1398 CWARN ("Ignoring prediction from %s of %s %s "
1399 "%ld seconds in the future\n",
1400 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1401 libcfs_nid2str(nid), alive ? "up" : "down",
1402 cfs_duration_sec(cfs_time_sub(when, now)));
1406 if (ni != NULL && !alive && /* LND telling me she's down */
1407 !auto_down) { /* auto-down disabled */
1408 CDEBUG(D_NET, "Auto-down disabled\n");
1414 lp = lnet_find_peer_locked(nid);
1418 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1422 /* We can't fully trust LND on reporting exact peer last_alive
1423 * if he notifies us about dead peer. For example ksocklnd can
1424 * call us with when == _time_when_the_node_was_booted_ if
1425 * no connections were successfully established */
1426 if (ni != NULL && !alive && when < lp->lp_last_alive)
1427 when = lp->lp_last_alive;
1429 lnet_notify_locked(lp, ni == NULL, alive, when);
1437 lnet_peer_decref_locked(lp);
1442 EXPORT_SYMBOL(lnet_notify);
1445 lnet_get_tunables (void)
1453 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1459 lnet_router_checker (void)
1461 static time_t last = 0;
1462 static int running = 0;
1464 time_t now = cfs_time_current_sec();
1465 int interval = now - last;
1470 /* It's no use to call me again within a sec - all intervals and
1471 * timeouts are measured in seconds */
1472 if (last != 0 && interval < 2)
1476 interval > MAX(live_router_check_interval,
1477 dead_router_check_interval))
1478 CDEBUG(D_NETERROR, "Checker(%d/%d) not called for %d seconds\n",
1479 live_router_check_interval, dead_router_check_interval,
1483 LASSERT (!running); /* recursion check */
1489 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD) {
1490 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1491 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1495 /* consume all pending events */
1500 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1501 * recursion breaker in LNetEQPoll would fail */
1502 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1503 if (rc == 0) /* no event pending */
1506 /* NB a lost SENT prevents me from pinging a router again */
1507 if (rc == -EOVERFLOW) {
1508 CERROR("Dropped an event!!!\n");
1515 lnet_router_checker_event(&ev);
1519 if (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED ||
1520 the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING) {
1525 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1529 version = the_lnet.ln_routers_version;
1530 list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1531 lnet_ping_router_locked(rtr);
1532 LASSERT (version == the_lnet.ln_routers_version);
1537 running = 0; /* lock only needed for the recursion check */
1541 /* NB lnet_peers_start_down depends on me,
1542 * so must be called before any peer creation */
1544 lnet_get_tunables (void)
1548 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1549 if (s != NULL) router_ping_timeout = atoi(s);
1551 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1552 if (s != NULL) live_router_check_interval = atoi(s);
1554 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1555 if (s != NULL) dead_router_check_interval = atoi(s);
1557 /* This replaces old lnd_notify mechanism */
1558 check_routers_before_use = 1;
1559 if (dead_router_check_interval <= 0)
1560 dead_router_check_interval = 30;
1564 lnet_free_rtrpools (void)
1569 lnet_init_rtrpools (void)
1574 lnet_alloc_rtrpools (int im_a_arouter)