2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, Whamcloud, Inc.
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_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
135 /* Notify only in 1 thread at any time to ensure ordered notification.
136 * NB individual events can be missed; the only guarantee is that you
137 * always get the most recent news */
139 if (lp->lp_notifying)
142 lp->lp_notifying = 1;
144 while (lp->lp_notify) {
145 alive = lp->lp_alive;
146 notifylnd = lp->lp_notifylnd;
148 lp->lp_notifylnd = 0;
151 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
154 /* A new notification could happen now; I'll handle it
155 * when control returns to me */
157 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
163 lp->lp_notifying = 0;
168 lnet_rtr_addref_locked(lnet_peer_t *lp)
170 LASSERT (lp->lp_refcount > 0);
171 LASSERT (lp->lp_rtr_refcount >= 0);
173 lp->lp_rtr_refcount++;
174 if (lp->lp_rtr_refcount == 1) {
177 /* a simple insertion sort */
178 cfs_list_for_each_prev(pos, &the_lnet.ln_routers) {
179 lnet_peer_t *rtr = cfs_list_entry(pos, lnet_peer_t,
182 if (rtr->lp_nid < lp->lp_nid)
186 cfs_list_add(&lp->lp_rtr_list, pos);
187 /* addref for the_lnet.ln_routers */
188 lnet_peer_addref_locked(lp);
189 the_lnet.ln_routers_version++;
194 lnet_rtr_decref_locked(lnet_peer_t *lp)
196 LASSERT (lp->lp_refcount > 0);
197 LASSERT (lp->lp_rtr_refcount > 0);
199 lp->lp_rtr_refcount--;
200 if (lp->lp_rtr_refcount == 0) {
201 if (lp->lp_rcd != NULL) {
202 cfs_list_add(&lp->lp_rcd->rcd_list,
203 &the_lnet.ln_rcd_deathrow);
207 cfs_list_del(&lp->lp_rtr_list);
208 /* decref for the_lnet.ln_routers */
209 lnet_peer_decref_locked(lp);
210 the_lnet.ln_routers_version++;
215 lnet_find_net_locked (__u32 net)
217 lnet_remotenet_t *rnet;
220 LASSERT (!the_lnet.ln_shutdown);
222 cfs_list_for_each (tmp, &the_lnet.ln_remote_nets) {
223 rnet = cfs_list_entry(tmp, lnet_remotenet_t, lrn_list);
225 if (rnet->lrn_net == net)
231 static void lnet_shuffle_seed(void)
233 static int seeded = 0;
234 int lnd_type, seed[2];
242 cfs_get_random_bytes(seed, sizeof(seed));
244 /* Nodes with small feet have little entropy
245 * the NID for this node gives the most entropy in the low bits */
246 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
247 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
248 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
250 if (lnd_type != LOLND)
251 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
254 cfs_gettimeofday(&tv);
255 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
260 /* NB expects LNET_LOCK held */
262 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
264 unsigned int len = 0;
265 unsigned int offset = 0;
270 cfs_list_for_each (e, &rnet->lrn_routes) {
274 /* len+1 positions to add a new entry, also prevents division by 0 */
275 offset = cfs_rand() % (len + 1);
276 cfs_list_for_each (e, &rnet->lrn_routes) {
281 cfs_list_add(&route->lr_list, e);
283 the_lnet.ln_remote_nets_version++;
284 lnet_rtr_addref_locked(route->lr_gateway);
288 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
291 lnet_remotenet_t *rnet;
292 lnet_remotenet_t *rnet2;
298 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
299 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
301 if (gateway == LNET_NID_ANY ||
302 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
303 net == LNET_NIDNET(LNET_NID_ANY) ||
304 LNET_NETTYP(net) == LOLND ||
305 LNET_NIDNET(gateway) == net ||
306 hops < 1 || hops > 255)
309 if (lnet_islocalnet(net)) /* it's a local network */
310 return 0; /* ignore the route entry */
312 /* Assume net, route, all new */
313 LIBCFS_ALLOC(route, sizeof(*route));
314 LIBCFS_ALLOC(rnet, sizeof(*rnet));
315 if (route == NULL || rnet == NULL) {
316 CERROR("Out of memory creating route %s %d %s\n",
317 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
319 LIBCFS_FREE(route, sizeof(*route));
321 LIBCFS_FREE(rnet, sizeof(*rnet));
325 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
327 route->lr_hops = hops;
331 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
335 LIBCFS_FREE(route, sizeof(*route));
336 LIBCFS_FREE(rnet, sizeof(*rnet));
338 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
339 return 0; /* ignore the route entry */
341 CERROR("Error %d creating route %s %d %s\n", rc,
342 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
346 LASSERT (!the_lnet.ln_shutdown);
348 rnet2 = lnet_find_net_locked(net);
351 cfs_list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
355 /* Search for a duplicate route (it's a NOOP if it is) */
357 cfs_list_for_each (e, &rnet2->lrn_routes) {
358 lnet_route_t *route2 = cfs_list_entry(e, lnet_route_t, lr_list);
360 if (route2->lr_gateway == route->lr_gateway) {
365 /* our lookups must be true */
366 LASSERT (route2->lr_gateway->lp_nid != gateway);
370 ni = route->lr_gateway->lp_ni;
371 lnet_ni_addref_locked(ni);
373 lnet_add_route_to_rnet(rnet2, route);
376 /* XXX Assume alive */
377 if (ni->ni_lnd->lnd_notify != NULL)
378 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
382 lnet_peer_decref_locked(route->lr_gateway);
384 LIBCFS_FREE(route, sizeof(*route));
388 LIBCFS_FREE(rnet, sizeof(*rnet));
394 lnet_check_routes (void)
396 lnet_remotenet_t *rnet;
398 lnet_route_t *route2;
404 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
405 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
408 cfs_list_for_each (e2, &rnet->lrn_routes) {
409 route = cfs_list_entry(e2, lnet_route_t, lr_list);
413 else if (route->lr_gateway->lp_ni !=
414 route2->lr_gateway->lp_ni) {
417 CERROR("Routes to %s via %s and %s not supported\n",
418 libcfs_net2str(rnet->lrn_net),
419 libcfs_nid2str(route->lr_gateway->lp_nid),
420 libcfs_nid2str(route2->lr_gateway->lp_nid));
431 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
433 lnet_remotenet_t *rnet;
439 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
440 libcfs_net2str(net), libcfs_nid2str(gw_nid));
442 /* NB Caller may specify either all routes via the given gateway
443 * or a specific route entry actual NIDs) */
448 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
449 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
451 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
452 net == rnet->lrn_net))
455 cfs_list_for_each (e2, &rnet->lrn_routes) {
456 route = cfs_list_entry(e2, lnet_route_t, lr_list);
458 if (!(gw_nid == LNET_NID_ANY ||
459 gw_nid == route->lr_gateway->lp_nid))
462 cfs_list_del(&route->lr_list);
463 the_lnet.ln_remote_nets_version++;
465 if (cfs_list_empty(&rnet->lrn_routes))
466 cfs_list_del(&rnet->lrn_list);
470 lnet_rtr_decref_locked(route->lr_gateway);
471 lnet_peer_decref_locked(route->lr_gateway);
474 LIBCFS_FREE(route, sizeof (*route));
477 LIBCFS_FREE(rnet, sizeof(*rnet));
489 lnet_destroy_routes (void)
491 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
495 lnet_get_route (int idx, __u32 *net, __u32 *hops,
496 lnet_nid_t *gateway, __u32 *alive)
500 lnet_remotenet_t *rnet;
505 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
506 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
508 cfs_list_for_each (e2, &rnet->lrn_routes) {
509 route = cfs_list_entry(e2, lnet_route_t, lr_list);
512 *net = rnet->lrn_net;
513 *hops = route->lr_hops;
514 *gateway = route->lr_gateway->lp_nid;
515 *alive = route->lr_gateway->lp_alive;
527 lnet_swap_pinginfo(lnet_ping_info_t *info)
530 lnet_ni_status_t *stat;
532 __swab32s(&info->pi_version);
533 __swab32s(&info->pi_pid);
534 __swab32s(&info->pi_nnis);
535 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
536 stat = &info->pi_ni[i];
537 __swab64s(&stat->ns_nid);
538 __swab32s(&stat->ns_status);
543 /* Returns # of down NIs, or negative error codes; ignore downed NIs
544 * if a NI in 'net' is up */
546 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
552 lnet_ping_info_t *info;
554 if (!avoid_asym_router_failure)
557 if (rtr->lp_rcd == NULL)
561 return -EINVAL; /* stale lp_rcd */
563 info = rtr->lp_rcd->rcd_pinginfo;
564 LASSERT (info != NULL);
566 /* NB always racing with network! */
567 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
568 lnet_swap_pinginfo(info);
569 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
570 CNETERR("%s: Unexpected magic %08x\n",
571 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
575 if (info->pi_version == LNET_PROTO_PING_VERSION1)
576 return -ENOENT; /* v1 doesn't carry NI status info */
578 if (info->pi_version != LNET_PROTO_PING_VERSION) {
579 CNETERR("%s: Unexpected version 0x%x\n",
580 libcfs_nid2str(rtr->lp_nid), info->pi_version);
584 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
585 lnet_ni_status_t *stat = &info->pi_ni[i];
586 lnet_nid_t nid = stat->ns_nid;
588 if (nid == LNET_NID_ANY) {
589 CNETERR("%s: unexpected LNET_NID_ANY\n",
590 libcfs_nid2str(rtr->lp_nid));
594 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
597 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
598 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
605 if (stat->ns_status != LNET_NI_STATUS_UP) {
606 CNETERR("%s: Unexpected status 0x%x\n",
607 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
611 /* ignore downed NIs if there's a NI up for dest network */
612 if (LNET_NIDNET(nid) == net)
615 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
619 /* ptl NIs are considered down only when they're all down */
620 return down + (ptl_up ? 0 : ptl_down);
624 lnet_wait_known_routerstate(void)
630 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
636 cfs_list_for_each (entry, &the_lnet.ln_routers) {
637 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
639 if (rtr->lp_alive_count == 0) {
651 lnet_router_checker();
653 cfs_pause(cfs_time_seconds(1));
658 lnet_router_checker_event (lnet_event_t *event)
660 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
661 * drop it (that's how come I see _every_ event, even ones that would
663 lnet_rc_data_t *rcd = event->md.user_ptr;
667 LASSERT(rcd != NULL);
669 if (event->unlinked) {
670 LNetInvalidateHandle(&rcd->rcd_mdh);
674 LASSERT (event->type == LNET_EVENT_SEND ||
675 event->type == LNET_EVENT_REPLY);
677 nid = (event->type == LNET_EVENT_SEND) ?
678 event->target.nid : event->initiator.nid;
680 lp = lnet_find_peer_locked(nid);
682 /* router may have been removed */
683 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
687 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
688 lp->lp_ping_notsent = 0;
690 if (lnet_isrouter(lp) && /* ignore if no longer a router */
691 (event->status != 0 ||
692 event->type == LNET_EVENT_REPLY)) {
694 /* A successful REPLY means the router is up. If _any_ comms
695 * to the router fail I assume it's down (this will happen if
696 * we ping alive routers to try to detect router death before
697 * apps get burned). */
699 lnet_notify_locked(lp, 1, (event->status == 0),
702 /* The router checker will wake up very shortly and do the
703 * actual notification.
704 * XXX If 'lp' stops being a router before then, it will still
705 * have the notification pending!!! */
708 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
709 * was in the peer table and I've not dropped the lock, so no-one else
710 * can have reduced the refcount) */
711 LASSERT(lp->lp_refcount > 1);
713 lnet_peer_decref_locked(lp);
717 lnet_update_ni_status(void)
719 cfs_time_t now = cfs_time_current();
724 LASSERT (the_lnet.ln_routing);
726 timeout = router_ping_timeout +
727 MAX(live_router_check_interval, dead_router_check_interval);
731 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
732 lnet_ni_status_t *ns = ni->ni_status;
734 LASSERT (ns != NULL);
736 status = LNET_NI_STATUS_UP;
737 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
738 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
739 cfs_time_seconds(timeout))))
740 status = LNET_NI_STATUS_DOWN;
742 if (ns->ns_status != status) {
743 ns->ns_status = status;
744 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
745 libcfs_nid2str(ni->ni_nid), timeout,
746 status == LNET_NI_STATUS_UP ? "up" : "down");
754 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
756 LASSERT(cfs_list_empty(&rcd->rcd_list));
757 /* detached from network */
758 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
760 if (rcd->rcd_pinginfo != NULL)
761 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
763 LIBCFS_FREE(rcd, sizeof(*rcd));
767 lnet_create_rc_data_locked(lnet_peer_t *gateway)
769 lnet_rc_data_t *rcd = NULL;
770 lnet_ping_info_t *pi;
776 LIBCFS_ALLOC(rcd, sizeof(*rcd));
780 LNetInvalidateHandle(&rcd->rcd_mdh);
781 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
783 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
787 memset(pi, 0, LNET_PINGINFO_SIZE);
788 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
789 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
790 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
792 rcd->rcd_pinginfo = pi;
794 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
795 rc = LNetMDBind((lnet_md_t){.start = pi,
797 .length = LNET_PINGINFO_SIZE,
798 .threshold = LNET_MD_THRESH_INF,
799 .options = LNET_MD_TRUNCATE,
800 .eq_handle = the_lnet.ln_rc_eqh},
804 CERROR("Can't bind MD: %d\n", rc);
810 /* router table changed or someone has created rcd for this gateway */
811 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
816 gateway->lp_rcd = rcd;
821 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
822 rc = LNetMDUnlink(rcd->rcd_mdh);
825 lnet_destroy_rc_data(rcd);
829 return gateway->lp_rcd;
833 lnet_router_check_interval (lnet_peer_t *rtr)
837 secs = rtr->lp_alive ? live_router_check_interval :
838 dead_router_check_interval;
846 lnet_ping_router_locked (lnet_peer_t *rtr)
848 lnet_rc_data_t *rcd = NULL;
849 cfs_time_t now = cfs_time_current();
852 lnet_peer_addref_locked(rtr);
854 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
855 cfs_time_after(now, rtr->lp_ping_deadline))
856 lnet_notify_locked(rtr, 1, 0, now);
858 /* Run any outstanding notifications */
859 lnet_ni_notify_locked(rtr->lp_ni, rtr);
861 if (!lnet_isrouter(rtr) ||
862 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
863 /* router table changed or router checker is shutting down */
864 lnet_peer_decref_locked(rtr);
868 rcd = rtr->lp_rcd != NULL ?
869 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
874 secs = lnet_router_check_interval(rtr);
877 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
878 "alive_count %d lp_ping_timestamp %lu\n",
879 libcfs_nid2str(rtr->lp_nid), secs,
880 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
881 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
883 if (secs != 0 && !rtr->lp_ping_notsent &&
884 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
885 cfs_time_seconds(secs)))) {
887 lnet_process_id_t id;
888 lnet_handle_md_t mdh;
890 id.nid = rtr->lp_nid;
891 id.pid = LUSTRE_SRV_LNET_PID;
892 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
894 rtr->lp_ping_notsent = 1;
895 rtr->lp_ping_timestamp = now;
899 if (rtr->lp_ping_deadline == 0) {
900 rtr->lp_ping_deadline = \
901 cfs_time_shift(router_ping_timeout);
906 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
907 LNET_PROTO_PING_MATCHBITS, 0);
911 rtr->lp_ping_notsent = 0; /* no event pending */
914 lnet_peer_decref_locked(rtr);
919 lnet_router_checker_start(void)
927 int router_checker_max_eqsize = 10240;
929 LASSERT (check_routers_before_use);
930 LASSERT (dead_router_check_interval > 0);
934 /* As an approximation, allow each router the same number of
935 * outstanding events as it is allowed outstanding sends */
937 version = the_lnet.ln_routers_version;
938 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
939 lnet_ni_t *ni = rtr->lp_ni;
940 lnet_process_id_t id;
943 eqsz += ni->ni_peertxcredits;
945 /* one async ping reply per router */
946 id.nid = rtr->lp_nid;
947 id.pid = LUSTRE_SRV_LNET_PID;
951 rc = LNetSetAsync(id, 1);
953 CWARN("LNetSetAsync %s failed: %d\n",
954 libcfs_id2str(id), rc);
959 /* NB router list doesn't change in userspace */
960 LASSERT (version == the_lnet.ln_routers_version);
967 "No router found, not starting router checker\n");
971 /* at least allow a SENT and a REPLY per router */
972 if (router_checker_max_eqsize < 2 * nrtr)
973 router_checker_max_eqsize = 2 * nrtr;
976 if (eqsz > router_checker_max_eqsize)
977 eqsz = router_checker_max_eqsize;
980 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
982 if (check_routers_before_use &&
983 dead_router_check_interval <= 0) {
984 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
985 " set if 'check_routers_before_use' is set"
990 if (!the_lnet.ln_routing &&
991 live_router_check_interval <= 0 &&
992 dead_router_check_interval <= 0)
996 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
997 /* EQ size doesn't matter; the callback is guaranteed to get every
1000 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1001 &the_lnet.ln_rc_eqh);
1003 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1004 &the_lnet.ln_rc_eqh);
1007 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1011 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1013 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1015 CERROR("Can't start router checker thread: %d\n", rc);
1016 /* block until event callback signals exit */
1017 cfs_down(&the_lnet.ln_rc_signal);
1018 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1020 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1025 if (check_routers_before_use) {
1026 /* Note that a helpful side-effect of pinging all known routers
1027 * at startup is that it makes them drop stale connections they
1028 * may have to a previous instance of me. */
1029 lnet_wait_known_routerstate();
1036 lnet_router_checker_stop (void)
1040 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1043 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1044 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1047 /* block until event callback signals exit */
1048 cfs_down(&the_lnet.ln_rc_signal);
1050 lnet_router_checker();
1052 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1054 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1060 lnet_prune_rc_data(int wait_unlink)
1062 lnet_rc_data_t *rcd;
1063 lnet_rc_data_t *tmp;
1068 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1069 cfs_list_empty(&the_lnet.ln_rcd_deathrow) &&
1070 cfs_list_empty(&the_lnet.ln_rcd_zombie)))
1073 CFS_INIT_LIST_HEAD(&head);
1077 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1078 /* router checker is stopping, prune all */
1079 cfs_list_for_each_entry(lp, &the_lnet.ln_routers,
1081 if (lp->lp_rcd == NULL)
1084 LASSERT(cfs_list_empty(&lp->lp_rcd->rcd_list));
1085 cfs_list_add(&lp->lp_rcd->rcd_list,
1086 &the_lnet.ln_rcd_deathrow);
1091 /* unlink all RCDs on deathrow list */
1092 cfs_list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1094 if (!cfs_list_empty(&head)) {
1097 cfs_list_for_each_entry(rcd, &head, rcd_list)
1098 LNetMDUnlink(rcd->rcd_mdh);
1103 cfs_list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1105 /* release all zombie RCDs */
1106 while (!cfs_list_empty(&the_lnet.ln_rcd_zombie)) {
1107 cfs_list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1109 if (!LNetHandleIsInvalid(rcd->rcd_mdh))
1110 cfs_list_move(&rcd->rcd_list, &head);
1113 wait_unlink = wait_unlink &&
1114 !cfs_list_empty(&the_lnet.ln_rcd_zombie);
1118 while (!cfs_list_empty(&head)) {
1119 rcd = cfs_list_entry(head.next,
1120 lnet_rc_data_t, rcd_list);
1121 cfs_list_del_init(&rcd->rcd_list);
1122 lnet_destroy_rc_data(rcd);
1129 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1130 "Waiting for rc buffers to unlink\n");
1131 cfs_pause(cfs_time_seconds(1) / 4);
1138 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1141 lnet_router_checker(void *arg)
1146 cfs_daemonize("router_checker");
1147 cfs_block_allsigs();
1149 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1151 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1156 version = the_lnet.ln_routers_version;
1158 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1159 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1160 lnet_ping_router_locked(rtr);
1162 /* NB dropped lock */
1163 if (version != the_lnet.ln_routers_version) {
1164 /* the routers list has changed */
1171 if (the_lnet.ln_routing)
1172 lnet_update_ni_status();
1174 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1176 /* Call cfs_pause() here always adds 1 to load average
1177 * because kernel counts # active tasks as nr_running
1178 * + nr_uninterruptible. */
1179 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1180 cfs_time_seconds(1));
1183 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1185 lnet_prune_rc_data(1); /* wait for UNLINK */
1187 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1188 cfs_up(&the_lnet.ln_rc_signal);
1189 /* The unlink event callback will signal final completion */
1194 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1196 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1198 while (--npages >= 0)
1199 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1201 LIBCFS_FREE(rb, sz);
1205 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1207 int npages = rbp->rbp_npages;
1208 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1213 LIBCFS_ALLOC(rb, sz);
1219 for (i = 0; i < npages; i++) {
1220 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1223 cfs_free_page(rb->rb_kiov[i].kiov_page);
1225 LIBCFS_FREE(rb, sz);
1229 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1230 rb->rb_kiov[i].kiov_offset = 0;
1231 rb->rb_kiov[i].kiov_page = page;
1238 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1240 int npages = rbp->rbp_npages;
1244 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1245 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1247 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1248 LASSERT (rbp->rbp_credits > 0);
1250 rb = cfs_list_entry(rbp->rbp_bufs.next,
1251 lnet_rtrbuf_t, rb_list);
1252 cfs_list_del(&rb->rb_list);
1253 lnet_destroy_rtrbuf(rb, npages);
1257 LASSERT (rbp->rbp_nbuffers == nbuffers);
1258 LASSERT (rbp->rbp_credits == nbuffers);
1260 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1264 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1269 if (rbp->rbp_nbuffers != 0) {
1270 LASSERT (rbp->rbp_nbuffers == nbufs);
1274 for (i = 0; i < nbufs; i++) {
1275 rb = lnet_new_rtrbuf(rbp);
1278 CERROR("Failed to allocate %d router bufs of %d pages\n",
1279 nbufs, rbp->rbp_npages);
1283 rbp->rbp_nbuffers++;
1285 rbp->rbp_mincredits++;
1286 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1288 /* No allocation "under fire" */
1289 /* Otherwise we'd need code to schedule blocked msgs etc */
1290 LASSERT (!the_lnet.ln_routing);
1293 LASSERT (rbp->rbp_credits == nbufs);
1298 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1300 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1301 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1303 rbp->rbp_npages = npages;
1304 rbp->rbp_credits = 0;
1305 rbp->rbp_mincredits = 0;
1309 lnet_free_rtrpools(void)
1311 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1312 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1313 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1317 lnet_init_rtrpools(void)
1319 int small_pages = 1;
1320 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1322 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1323 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1324 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1329 lnet_alloc_rtrpools(int im_a_router)
1333 if (!strcmp(forwarding, "")) {
1334 /* not set either way */
1337 } else if (!strcmp(forwarding, "disabled")) {
1338 /* explicitly disabled */
1340 } else if (!strcmp(forwarding, "enabled")) {
1341 /* explicitly enabled */
1343 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1344 "'enabled' or 'disabled'\n");
1348 if (tiny_router_buffers <= 0) {
1349 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1350 "routing enabled\n", tiny_router_buffers);
1355 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1356 tiny_router_buffers);
1360 if (small_router_buffers <= 0) {
1361 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1362 " routing enabled\n", small_router_buffers);
1367 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1368 small_router_buffers);
1372 if (large_router_buffers <= 0) {
1373 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1374 " routing enabled\n", large_router_buffers);
1379 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1380 large_router_buffers);
1385 the_lnet.ln_routing = 1;
1391 lnet_free_rtrpools();
1396 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1398 lnet_peer_t *lp = NULL;
1399 cfs_time_t now = cfs_time_current();
1401 LASSERT (!cfs_in_interrupt ());
1403 CDEBUG (D_NET, "%s notifying %s: %s\n",
1404 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1405 libcfs_nid2str(nid),
1406 alive ? "up" : "down");
1409 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1410 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1411 libcfs_nid2str(nid), alive ? "birth" : "death",
1412 libcfs_nid2str(ni->ni_nid));
1416 /* can't do predictions... */
1417 if (cfs_time_after(when, now)) {
1418 CWARN ("Ignoring prediction from %s of %s %s "
1419 "%ld seconds in the future\n",
1420 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1421 libcfs_nid2str(nid), alive ? "up" : "down",
1422 cfs_duration_sec(cfs_time_sub(when, now)));
1426 if (ni != NULL && !alive && /* LND telling me she's down */
1427 !auto_down) { /* auto-down disabled */
1428 CDEBUG(D_NET, "Auto-down disabled\n");
1434 lp = lnet_find_peer_locked(nid);
1438 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1442 /* We can't fully trust LND on reporting exact peer last_alive
1443 * if he notifies us about dead peer. For example ksocklnd can
1444 * call us with when == _time_when_the_node_was_booted_ if
1445 * no connections were successfully established */
1446 if (ni != NULL && !alive && when < lp->lp_last_alive)
1447 when = lp->lp_last_alive;
1449 lnet_notify_locked(lp, ni == NULL, alive, when);
1451 lnet_ni_notify_locked(ni, lp);
1453 lnet_peer_decref_locked(lp);
1458 EXPORT_SYMBOL(lnet_notify);
1461 lnet_get_tunables (void)
1469 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1475 lnet_router_checker (void)
1477 static time_t last = 0;
1478 static int running = 0;
1480 time_t now = cfs_time_current_sec();
1481 int interval = now - last;
1486 /* It's no use to call me again within a sec - all intervals and
1487 * timeouts are measured in seconds */
1488 if (last != 0 && interval < 2)
1492 interval > MAX(live_router_check_interval,
1493 dead_router_check_interval))
1494 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1495 live_router_check_interval, dead_router_check_interval,
1499 LASSERT (!running); /* recursion check */
1505 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1506 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1508 /* consume all pending events */
1513 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1514 * recursion breaker in LNetEQPoll would fail */
1515 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1516 if (rc == 0) /* no event pending */
1519 /* NB a lost SENT prevents me from pinging a router again */
1520 if (rc == -EOVERFLOW) {
1521 CERROR("Dropped an event!!!\n");
1528 lnet_router_checker_event(&ev);
1532 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1533 lnet_prune_rc_data(1); /* release rcd */
1534 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1539 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1543 version = the_lnet.ln_routers_version;
1544 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1545 lnet_ping_router_locked(rtr);
1546 LASSERT (version == the_lnet.ln_routers_version);
1551 running = 0; /* lock only needed for the recursion check */
1555 /* NB lnet_peers_start_down depends on me,
1556 * so must be called before any peer creation */
1558 lnet_get_tunables (void)
1562 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1563 if (s != NULL) router_ping_timeout = atoi(s);
1565 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1566 if (s != NULL) live_router_check_interval = atoi(s);
1568 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1569 if (s != NULL) dead_router_check_interval = atoi(s);
1571 /* This replaces old lnd_notify mechanism */
1572 check_routers_before_use = 1;
1573 if (dead_router_check_interval <= 0)
1574 dead_router_check_interval = 30;
1578 lnet_free_rtrpools (void)
1583 lnet_init_rtrpools (void)
1588 lnet_alloc_rtrpools (int im_a_arouter)