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 static 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 static 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 static 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)
238 /* NB expects LNET_LOCK held */
240 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
242 unsigned int len = 0;
243 unsigned int offset = 0;
245 extern __u64 lnet_create_interface_cookie(void);
247 list_for_each (e, &rnet->lrn_routes) {
251 /* FIXME use Lustre random function when it's moved to libcfs.
253 /* len+1 positions to add a new entry, also prevents division by 0 */
254 offset = ((unsigned int) lnet_create_interface_cookie()) % (len + 1);
255 list_for_each (e, &rnet->lrn_routes) {
260 list_add(&route->lr_list, e);
262 the_lnet.ln_remote_nets_version++;
263 lnet_rtr_addref_locked(route->lr_gateway);
267 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
270 lnet_remotenet_t *rnet;
271 lnet_remotenet_t *rnet2;
277 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
278 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
280 if (gateway == LNET_NID_ANY ||
281 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
282 net == LNET_NIDNET(LNET_NID_ANY) ||
283 LNET_NETTYP(net) == LOLND ||
284 LNET_NIDNET(gateway) == net ||
285 hops < 1 || hops > 255)
288 if (lnet_islocalnet(net)) /* it's a local network */
289 return 0; /* ignore the route entry */
291 /* Assume net, route, all new */
292 LIBCFS_ALLOC(route, sizeof(*route));
293 LIBCFS_ALLOC(rnet, sizeof(*rnet));
294 if (route == NULL || rnet == NULL) {
295 CERROR("Out of memory creating route %s %d %s\n",
296 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
298 LIBCFS_FREE(route, sizeof(*route));
300 LIBCFS_FREE(rnet, sizeof(*rnet));
304 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
306 route->lr_hops = hops;
310 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
314 LIBCFS_FREE(route, sizeof(*route));
315 LIBCFS_FREE(rnet, sizeof(*rnet));
317 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
318 return 0; /* ignore the route entry */
320 CERROR("Error %d creating route %s %d %s\n", rc,
321 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
325 LASSERT (!the_lnet.ln_shutdown);
327 rnet2 = lnet_find_net_locked(net);
330 list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
334 /* Search for a duplicate route (it's a NOOP if it is) */
336 list_for_each (e, &rnet2->lrn_routes) {
337 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
339 if (route2->lr_gateway == route->lr_gateway) {
344 /* our lookups must be true */
345 LASSERT (route2->lr_gateway->lp_nid != gateway);
349 ni = route->lr_gateway->lp_ni;
350 lnet_ni_addref_locked(ni);
352 lnet_add_route_to_rnet(rnet2, route);
355 /* XXX Assume alive */
356 if (ni->ni_lnd->lnd_notify != NULL)
357 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
361 lnet_peer_decref_locked(route->lr_gateway);
363 LIBCFS_FREE(route, sizeof(*route));
367 LIBCFS_FREE(rnet, sizeof(*rnet));
373 lnet_check_routes (void)
375 lnet_remotenet_t *rnet;
377 lnet_route_t *route2;
378 struct list_head *e1;
379 struct list_head *e2;
383 list_for_each (e1, &the_lnet.ln_remote_nets) {
384 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
387 list_for_each (e2, &rnet->lrn_routes) {
388 route = list_entry(e2, lnet_route_t, lr_list);
392 else if (route->lr_gateway->lp_ni !=
393 route2->lr_gateway->lp_ni) {
396 CERROR("Routes to %s via %s and %s not supported\n",
397 libcfs_net2str(rnet->lrn_net),
398 libcfs_nid2str(route->lr_gateway->lp_nid),
399 libcfs_nid2str(route2->lr_gateway->lp_nid));
410 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
412 lnet_remotenet_t *rnet;
414 struct list_head *e1;
415 struct list_head *e2;
418 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
419 libcfs_net2str(net), libcfs_nid2str(gw_nid));
421 /* NB Caller may specify either all routes via the given gateway
422 * or a specific route entry actual NIDs) */
427 list_for_each (e1, &the_lnet.ln_remote_nets) {
428 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
430 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
431 net == rnet->lrn_net))
434 list_for_each (e2, &rnet->lrn_routes) {
435 route = list_entry(e2, lnet_route_t, lr_list);
437 if (!(gw_nid == LNET_NID_ANY ||
438 gw_nid == route->lr_gateway->lp_nid))
441 list_del(&route->lr_list);
442 the_lnet.ln_remote_nets_version++;
444 if (list_empty(&rnet->lrn_routes))
445 list_del(&rnet->lrn_list);
449 lnet_rtr_decref_locked(route->lr_gateway);
450 lnet_peer_decref_locked(route->lr_gateway);
453 LIBCFS_FREE(route, sizeof (*route));
456 LIBCFS_FREE(rnet, sizeof(*rnet));
468 lnet_destroy_routes (void)
470 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
474 lnet_get_route (int idx, __u32 *net, __u32 *hops,
475 lnet_nid_t *gateway, __u32 *alive)
477 struct list_head *e1;
478 struct list_head *e2;
479 lnet_remotenet_t *rnet;
484 list_for_each (e1, &the_lnet.ln_remote_nets) {
485 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
487 list_for_each (e2, &rnet->lrn_routes) {
488 route = list_entry(e2, lnet_route_t, lr_list);
491 *net = rnet->lrn_net;
492 *hops = route->lr_hops;
493 *gateway = route->lr_gateway->lp_nid;
494 *alive = route->lr_gateway->lp_alive;
506 lnet_swap_pinginfo(lnet_ping_info_t *info)
509 lnet_ni_status_t *stat;
511 __swab32s(&info->pi_version);
512 __swab32s(&info->pi_pid);
513 __swab32s(&info->pi_nnis);
514 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
515 stat = &info->pi_ni[i];
516 __swab64s(&stat->ns_nid);
517 __swab32s(&stat->ns_status);
522 /* Returns # of down NIs, or negative error codes; ignore downed NIs
523 * if a NI in 'net' is up */
525 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
531 lnet_ping_info_t *info;
533 if (!avoid_asym_router_failure)
536 if (rtr->lp_rcd == NULL)
540 return -EINVAL; /* stale lp_rcd */
542 info = rtr->lp_rcd->rcd_pinginfo;
543 LASSERT (info != NULL);
545 /* NB always racing with network! */
546 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
547 lnet_swap_pinginfo(info);
548 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
549 CDEBUG(D_NETERROR, "%s: Unexpected magic %08x\n",
550 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
554 if (info->pi_version == LNET_PROTO_PING_VERSION1)
555 return -ENOENT; /* v1 doesn't carry NI status info */
557 if (info->pi_version != LNET_PROTO_PING_VERSION) {
558 CDEBUG(D_NETERROR, "%s: Unexpected version 0x%x\n",
559 libcfs_nid2str(rtr->lp_nid), info->pi_version);
563 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
564 lnet_ni_status_t *stat = &info->pi_ni[i];
565 lnet_nid_t nid = stat->ns_nid;
567 if (nid == LNET_NID_ANY) {
568 CDEBUG(D_NETERROR, "%s: unexpected LNET_NID_ANY\n",
569 libcfs_nid2str(rtr->lp_nid));
573 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
576 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
577 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
584 if (stat->ns_status != LNET_NI_STATUS_UP) {
585 CDEBUG(D_NETERROR, "%s: Unexpected status 0x%x\n",
586 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
590 /* ignore downed NIs if there's a NI up for dest network */
591 if (LNET_NIDNET(nid) == net)
594 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
598 /* ptl NIs are considered down only when they're all down */
599 return down + (ptl_up ? 0 : ptl_down);
603 lnet_wait_known_routerstate(void)
606 struct list_head *entry;
609 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
615 list_for_each (entry, &the_lnet.ln_routers) {
616 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
618 if (rtr->lp_alive_count == 0) {
630 lnet_router_checker();
632 cfs_pause(cfs_time_seconds(1));
637 lnet_router_checker_event (lnet_event_t *event)
639 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
640 * drop it (that's how come I see _every_ event, even ones that would
642 lnet_rc_data_t *rcd = event->md.user_ptr;
646 if (event->unlinked) {
648 LNetInvalidateHandle(&rcd->rcd_mdh);
652 /* The router checker thread has unlinked the default rc_md
654 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
655 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED;
657 mutex_up(&the_lnet.ln_rc_signal);
662 LASSERT (event->type == LNET_EVENT_SEND ||
663 event->type == LNET_EVENT_REPLY);
665 nid = (event->type == LNET_EVENT_SEND) ?
666 event->target.nid : event->initiator.nid;
668 lp = lnet_find_peer_locked(nid);
670 /* router may have been removed */
671 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
675 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
676 lp->lp_ping_notsent = 0;
678 if (lnet_isrouter(lp) && /* ignore if no longer a router */
679 (event->status != 0 ||
680 event->type == LNET_EVENT_REPLY)) {
682 /* A successful REPLY means the router is up. If _any_ comms
683 * to the router fail I assume it's down (this will happen if
684 * we ping alive routers to try to detect router death before
685 * apps get burned). */
687 lnet_notify_locked(lp, 1, (event->status == 0),
690 /* The router checker will wake up very shortly and do the
691 * actual notification.
692 * XXX If 'lp' stops being a router before then, it will still
693 * have the notification pending!!! */
696 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
697 * was in the peer table and I've not dropped the lock, so no-one else
698 * can have reduced the refcount) */
699 LASSERT(lp->lp_refcount > 1);
701 lnet_peer_decref_locked(lp);
705 lnet_update_ni_status(void)
707 cfs_time_t now = cfs_time_current();
712 LASSERT (the_lnet.ln_routing);
714 timeout = router_ping_timeout +
715 MAX(live_router_check_interval, dead_router_check_interval);
719 list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
720 lnet_ni_status_t *ns = ni->ni_status;
722 LASSERT (ns != NULL);
724 status = LNET_NI_STATUS_UP;
725 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
726 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
727 cfs_time_seconds(timeout))))
728 status = LNET_NI_STATUS_DOWN;
730 if (ns->ns_status != status) {
731 ns->ns_status = status;
732 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
733 libcfs_nid2str(ni->ni_nid), timeout,
734 status == LNET_NI_STATUS_UP ? "up" : "down");
742 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
744 LASSERT (list_empty(&rcd->rcd_list));
745 /* detached from network */
746 LASSERT (LNetHandleIsInvalid(rcd->rcd_mdh));
748 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
749 LIBCFS_FREE(rcd, sizeof(*rcd));
754 lnet_create_rc_data (void)
758 lnet_ping_info_t *pi;
761 LIBCFS_ALLOC(rcd, sizeof(*rcd));
765 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
767 LIBCFS_FREE(rcd, sizeof(*rcd));
771 memset(pi, 0, LNET_PINGINFO_SIZE);
772 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
773 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
774 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
776 rcd->rcd_pinginfo = pi;
777 LNetInvalidateHandle(&rcd->rcd_mdh);
778 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
780 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
781 rc = LNetMDBind((lnet_md_t){.start = pi,
783 .length = LNET_PINGINFO_SIZE,
784 .threshold = LNET_MD_THRESH_INF,
785 .options = LNET_MD_TRUNCATE,
786 .eq_handle = the_lnet.ln_rc_eqh},
790 CERROR("Can't bind MD: %d\n", rc);
791 lnet_destroy_rc_data(rcd);
799 lnet_router_check_interval (lnet_peer_t *rtr)
803 secs = rtr->lp_alive ? live_router_check_interval :
804 dead_router_check_interval;
812 lnet_ping_router_locked (lnet_peer_t *rtr)
815 lnet_rc_data_t *rcd = NULL;
816 cfs_time_t now = cfs_time_current();
819 lnet_peer_addref_locked(rtr);
821 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
822 cfs_time_after(now, rtr->lp_ping_deadline))
823 lnet_notify_locked(rtr, 1, 0, now);
825 if (avoid_asym_router_failure && rtr->lp_rcd == NULL)
830 /* Run any outstanding notifications */
834 rcd = lnet_create_rc_data();
838 if (!lnet_isrouter(rtr)) {
839 lnet_peer_decref_locked(rtr);
841 list_add(&rcd->rcd_list, &the_lnet.ln_zombie_rcd);
842 return; /* router table changed! */
846 LASSERT (rtr->lp_rcd == NULL);
850 secs = lnet_router_check_interval(rtr);
853 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
854 "alive_count %d lp_ping_timestamp %lu\n",
855 libcfs_nid2str(rtr->lp_nid), secs,
856 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
857 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
859 if (secs != 0 && !rtr->lp_ping_notsent &&
860 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
861 cfs_time_seconds(secs)))) {
863 lnet_process_id_t id;
864 lnet_handle_md_t mdh;
866 id.nid = rtr->lp_nid;
867 id.pid = LUSTRE_SRV_LNET_PID;
868 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
870 rtr->lp_ping_notsent = 1;
871 rtr->lp_ping_timestamp = now;
872 mdh = (rtr->lp_rcd == NULL) ? the_lnet.ln_rc_mdh :
873 rtr->lp_rcd->rcd_mdh;
875 if (rtr->lp_ping_deadline == 0)
876 rtr->lp_ping_deadline = cfs_time_shift(router_ping_timeout);
880 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
881 LNET_PROTO_PING_MATCHBITS, 0);
885 rtr->lp_ping_notsent = 0; /* no event pending */
888 lnet_peer_decref_locked(rtr);
893 lnet_router_checker_start(void)
895 static lnet_ping_info_t pinginfo;
904 int router_checker_max_eqsize = 10240;
906 LASSERT (check_routers_before_use);
907 LASSERT (dead_router_check_interval > 0);
911 /* As an approximation, allow each router the same number of
912 * outstanding events as it is allowed outstanding sends */
914 version = the_lnet.ln_routers_version;
915 list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
916 lnet_ni_t *ni = rtr->lp_ni;
917 lnet_process_id_t id;
920 eqsz += ni->ni_peertxcredits;
922 /* one async ping reply per router */
923 id.nid = rtr->lp_nid;
924 id.pid = LUSTRE_SRV_LNET_PID;
928 rc = LNetSetAsync(id, 1);
930 CWARN("LNetSetAsync %s failed: %d\n",
931 libcfs_id2str(id), rc);
936 /* NB router list doesn't change in userspace */
937 LASSERT (version == the_lnet.ln_routers_version);
944 "No router found, not starting router checker\n");
948 /* at least allow a SENT and a REPLY per router */
949 if (router_checker_max_eqsize < 2 * nrtr)
950 router_checker_max_eqsize = 2 * nrtr;
953 if (eqsz > router_checker_max_eqsize)
954 eqsz = router_checker_max_eqsize;
957 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
959 if (check_routers_before_use &&
960 dead_router_check_interval <= 0) {
961 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
962 " set if 'check_routers_before_use' is set"
967 if (!the_lnet.ln_routing &&
968 live_router_check_interval <= 0 &&
969 dead_router_check_interval <= 0)
973 init_mutex_locked(&the_lnet.ln_rc_signal);
974 /* EQ size doesn't matter; the callback is guaranteed to get every
977 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
978 &the_lnet.ln_rc_eqh);
980 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
981 &the_lnet.ln_rc_eqh);
984 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
988 memset(&md, 0, sizeof(md));
990 md.start = &pinginfo;
991 md.length = sizeof(pinginfo);
992 md.options = LNET_MD_TRUNCATE;
993 md.threshold = LNET_MD_THRESH_INF;
994 md.eq_handle = the_lnet.ln_rc_eqh;
995 rc = LNetMDBind(md, LNET_UNLINK, &the_lnet.ln_rc_mdh);
997 CERROR("Can't bind MD: %d\n", rc);
998 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1004 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1006 rc = (int)cfs_kernel_thread(lnet_router_checker, NULL, 0);
1008 CERROR("Can't start router checker thread: %d\n", rc);
1009 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1010 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1012 /* block until event callback signals exit */
1013 mutex_down(&the_lnet.ln_rc_signal);
1014 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1016 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1021 if (check_routers_before_use) {
1022 /* Note that a helpful side-effect of pinging all known routers
1023 * at startup is that it makes them drop stale connections they
1024 * may have to a previous instance of me. */
1025 lnet_wait_known_routerstate();
1032 lnet_router_checker_stop (void)
1036 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1039 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1040 the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
1043 /* block until event callback signals exit */
1044 mutex_down(&the_lnet.ln_rc_signal);
1046 while (the_lnet.ln_rc_state != LNET_RC_STATE_UNLINKED) {
1047 lnet_router_checker();
1048 cfs_pause(cfs_time_seconds(1));
1051 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
1053 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1055 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1059 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1062 lnet_prune_zombie_rcd (int wait_unlink)
1064 lnet_rc_data_t *rcd;
1065 lnet_rc_data_t *tmp;
1066 struct list_head free_rcd;
1070 CFS_INIT_LIST_HEAD(&free_rcd);
1074 version = the_lnet.ln_routers_version;
1075 list_for_each_entry_safe (rcd, tmp, &the_lnet.ln_zombie_rcd, rcd_list) {
1076 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1077 list_del(&rcd->rcd_list);
1078 list_add(&rcd->rcd_list, &free_rcd);
1084 LNetMDUnlink(rcd->rcd_mdh);
1087 if (version != the_lnet.ln_routers_version)
1092 while (wait_unlink && !list_empty(&the_lnet.ln_zombie_rcd)) {
1093 rcd = list_entry(the_lnet.ln_zombie_rcd.next,
1094 lnet_rc_data_t, rcd_list);
1095 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1096 list_del(&rcd->rcd_list);
1097 list_add(&rcd->rcd_list, &free_rcd);
1103 LNetMDUnlink(rcd->rcd_mdh);
1106 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1107 "Waiting for rc buffers to unlink\n");
1108 cfs_pause(cfs_time_seconds(1));
1115 while (!list_empty(&free_rcd)) {
1116 rcd = list_entry(free_rcd.next, lnet_rc_data_t, rcd_list);
1117 list_del_init(&rcd->rcd_list);
1118 lnet_destroy_rc_data(rcd);
1124 lnet_router_checker(void *arg)
1128 struct list_head *entry;
1129 lnet_process_id_t rtr_id;
1131 cfs_daemonize("router_checker");
1132 cfs_block_allsigs();
1134 rtr_id.pid = LUSTRE_SRV_LNET_PID;
1136 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1138 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1143 version = the_lnet.ln_routers_version;
1145 list_for_each (entry, &the_lnet.ln_routers) {
1146 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1147 lnet_ping_router_locked(rtr);
1149 /* NB dropped lock */
1150 if (version != the_lnet.ln_routers_version) {
1151 /* the routers list has changed */
1158 if (the_lnet.ln_routing)
1159 lnet_update_ni_status();
1161 lnet_prune_zombie_rcd(0); /* don't wait for UNLINK */
1163 /* Call cfs_pause() here always adds 1 to load average
1164 * because kernel counts # active tasks as nr_running
1165 * + nr_uninterruptible. */
1166 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1167 cfs_time_seconds(1));
1172 list_for_each (entry, &the_lnet.ln_routers) {
1173 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1175 if (rtr->lp_rcd == NULL)
1178 LASSERT (list_empty(&rtr->lp_rcd->rcd_list));
1179 list_add(&rtr->lp_rcd->rcd_list, &the_lnet.ln_zombie_rcd);
1185 lnet_prune_zombie_rcd(1); /* wait for UNLINK */
1187 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
1188 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1190 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1193 /* The unlink event callback will signal final completion */
1198 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1200 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1202 while (--npages >= 0)
1203 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1205 LIBCFS_FREE(rb, sz);
1209 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1211 int npages = rbp->rbp_npages;
1212 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1217 LIBCFS_ALLOC(rb, sz);
1223 for (i = 0; i < npages; i++) {
1224 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1227 cfs_free_page(rb->rb_kiov[i].kiov_page);
1229 LIBCFS_FREE(rb, sz);
1233 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1234 rb->rb_kiov[i].kiov_offset = 0;
1235 rb->rb_kiov[i].kiov_page = page;
1242 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1244 int npages = rbp->rbp_npages;
1248 LASSERT (list_empty(&rbp->rbp_msgs));
1249 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1251 while (!list_empty(&rbp->rbp_bufs)) {
1252 LASSERT (rbp->rbp_credits > 0);
1254 rb = list_entry(rbp->rbp_bufs.next,
1255 lnet_rtrbuf_t, rb_list);
1256 list_del(&rb->rb_list);
1257 lnet_destroy_rtrbuf(rb, npages);
1261 LASSERT (rbp->rbp_nbuffers == nbuffers);
1262 LASSERT (rbp->rbp_credits == nbuffers);
1264 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1268 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1273 if (rbp->rbp_nbuffers != 0) {
1274 LASSERT (rbp->rbp_nbuffers == nbufs);
1278 for (i = 0; i < nbufs; i++) {
1279 rb = lnet_new_rtrbuf(rbp);
1282 CERROR("Failed to allocate %d router bufs of %d pages\n",
1283 nbufs, rbp->rbp_npages);
1287 rbp->rbp_nbuffers++;
1289 rbp->rbp_mincredits++;
1290 list_add(&rb->rb_list, &rbp->rbp_bufs);
1292 /* No allocation "under fire" */
1293 /* Otherwise we'd need code to schedule blocked msgs etc */
1294 LASSERT (!the_lnet.ln_routing);
1297 LASSERT (rbp->rbp_credits == nbufs);
1302 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1304 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1305 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1307 rbp->rbp_npages = npages;
1308 rbp->rbp_credits = 0;
1309 rbp->rbp_mincredits = 0;
1313 lnet_free_rtrpools(void)
1315 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1316 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1317 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1321 lnet_init_rtrpools(void)
1323 int small_pages = 1;
1324 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1326 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1327 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1328 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1333 lnet_alloc_rtrpools(int im_a_router)
1337 if (!strcmp(forwarding, "")) {
1338 /* not set either way */
1341 } else if (!strcmp(forwarding, "disabled")) {
1342 /* explicitly disabled */
1344 } else if (!strcmp(forwarding, "enabled")) {
1345 /* explicitly enabled */
1347 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1348 "'enabled' or 'disabled'\n");
1352 if (tiny_router_buffers <= 0) {
1353 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1354 "routing enabled\n", tiny_router_buffers);
1359 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1360 tiny_router_buffers);
1364 if (small_router_buffers <= 0) {
1365 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1366 " routing enabled\n", small_router_buffers);
1371 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1372 small_router_buffers);
1376 if (large_router_buffers <= 0) {
1377 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1378 " routing enabled\n", large_router_buffers);
1383 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1384 large_router_buffers);
1389 the_lnet.ln_routing = 1;
1395 lnet_free_rtrpools();
1400 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1402 lnet_peer_t *lp = NULL;
1403 cfs_time_t now = cfs_time_current();
1405 LASSERT (!in_interrupt ());
1407 CDEBUG (D_NET, "%s notifying %s: %s\n",
1408 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1409 libcfs_nid2str(nid),
1410 alive ? "up" : "down");
1413 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1414 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1415 libcfs_nid2str(nid), alive ? "birth" : "death",
1416 libcfs_nid2str(ni->ni_nid));
1420 /* can't do predictions... */
1421 if (cfs_time_after(when, now)) {
1422 CWARN ("Ignoring prediction from %s of %s %s "
1423 "%ld seconds in the future\n",
1424 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1425 libcfs_nid2str(nid), alive ? "up" : "down",
1426 cfs_duration_sec(cfs_time_sub(when, now)));
1430 if (ni != NULL && !alive && /* LND telling me she's down */
1431 !auto_down) { /* auto-down disabled */
1432 CDEBUG(D_NET, "Auto-down disabled\n");
1438 lp = lnet_find_peer_locked(nid);
1442 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1446 /* We can't fully trust LND on reporting exact peer last_alive
1447 * if he notifies us about dead peer. For example ksocklnd can
1448 * call us with when == _time_when_the_node_was_booted_ if
1449 * no connections were successfully established */
1450 if (ni != NULL && !alive && when < lp->lp_last_alive)
1451 when = lp->lp_last_alive;
1453 lnet_notify_locked(lp, ni == NULL, alive, when);
1461 lnet_peer_decref_locked(lp);
1466 EXPORT_SYMBOL(lnet_notify);
1469 lnet_get_tunables (void)
1477 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1483 lnet_router_checker (void)
1485 static time_t last = 0;
1486 static int running = 0;
1488 time_t now = cfs_time_current_sec();
1489 int interval = now - last;
1494 /* It's no use to call me again within a sec - all intervals and
1495 * timeouts are measured in seconds */
1496 if (last != 0 && interval < 2)
1500 interval > MAX(live_router_check_interval,
1501 dead_router_check_interval))
1502 CDEBUG(D_NETERROR, "Checker(%d/%d) not called for %d seconds\n",
1503 live_router_check_interval, dead_router_check_interval,
1507 LASSERT (!running); /* recursion check */
1513 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD) {
1514 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1515 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1519 /* consume all pending events */
1524 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1525 * recursion breaker in LNetEQPoll would fail */
1526 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1527 if (rc == 0) /* no event pending */
1530 /* NB a lost SENT prevents me from pinging a router again */
1531 if (rc == -EOVERFLOW) {
1532 CERROR("Dropped an event!!!\n");
1539 lnet_router_checker_event(&ev);
1543 if (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED ||
1544 the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING) {
1549 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1553 version = the_lnet.ln_routers_version;
1554 list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1555 lnet_ping_router_locked(rtr);
1556 LASSERT (version == the_lnet.ln_routers_version);
1561 running = 0; /* lock only needed for the recursion check */
1565 /* NB lnet_peers_start_down depends on me,
1566 * so must be called before any peer creation */
1568 lnet_get_tunables (void)
1572 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1573 if (s != NULL) router_ping_timeout = atoi(s);
1575 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1576 if (s != NULL) live_router_check_interval = atoi(s);
1578 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1579 if (s != NULL) dead_router_check_interval = atoi(s);
1581 /* This replaces old lnd_notify mechanism */
1582 check_routers_before_use = 1;
1583 if (dead_router_check_interval <= 0)
1584 dead_router_check_interval = 30;
1588 lnet_free_rtrpools (void)
1593 lnet_init_rtrpools (void)
1598 lnet_alloc_rtrpools (int im_a_arouter)