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_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) {
184 /* a simple insertion sort */
185 cfs_list_for_each_prev(pos, &the_lnet.ln_routers) {
186 lnet_peer_t *rtr = cfs_list_entry(pos, lnet_peer_t,
189 if (rtr->lp_nid < lp->lp_nid)
193 cfs_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 cfs_list_add(&lp->lp_rcd->rcd_list,
210 &the_lnet.ln_zombie_rcd);
214 cfs_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;
227 LASSERT (!the_lnet.ln_shutdown);
229 cfs_list_for_each (tmp, &the_lnet.ln_remote_nets) {
230 rnet = cfs_list_entry(tmp, lnet_remotenet_t, lrn_list);
232 if (rnet->lrn_net == net)
238 static void lnet_shuffle_seed(void)
240 static int seeded = 0;
241 int lnd_type, seed[2];
249 cfs_get_random_bytes(seed, sizeof(seed));
251 /* Nodes with small feet have little entropy
252 * the NID for this node gives the most entropy in the low bits */
253 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
254 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
255 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
257 if (lnd_type != LOLND)
258 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
261 cfs_gettimeofday(&tv);
262 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
267 /* NB expects LNET_LOCK held */
269 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
271 unsigned int len = 0;
272 unsigned int offset = 0;
277 cfs_list_for_each (e, &rnet->lrn_routes) {
281 /* len+1 positions to add a new entry, also prevents division by 0 */
282 offset = cfs_rand() % (len + 1);
283 cfs_list_for_each (e, &rnet->lrn_routes) {
288 cfs_list_add(&route->lr_list, e);
290 the_lnet.ln_remote_nets_version++;
291 lnet_rtr_addref_locked(route->lr_gateway);
295 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
298 lnet_remotenet_t *rnet;
299 lnet_remotenet_t *rnet2;
305 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
306 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
308 if (gateway == LNET_NID_ANY ||
309 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
310 net == LNET_NIDNET(LNET_NID_ANY) ||
311 LNET_NETTYP(net) == LOLND ||
312 LNET_NIDNET(gateway) == net ||
313 hops < 1 || hops > 255)
316 if (lnet_islocalnet(net)) /* it's a local network */
317 return 0; /* ignore the route entry */
319 /* Assume net, route, all new */
320 LIBCFS_ALLOC(route, sizeof(*route));
321 LIBCFS_ALLOC(rnet, sizeof(*rnet));
322 if (route == NULL || rnet == NULL) {
323 CERROR("Out of memory creating route %s %d %s\n",
324 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
326 LIBCFS_FREE(route, sizeof(*route));
328 LIBCFS_FREE(rnet, sizeof(*rnet));
332 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
334 route->lr_hops = hops;
338 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
342 LIBCFS_FREE(route, sizeof(*route));
343 LIBCFS_FREE(rnet, sizeof(*rnet));
345 if (rc == -EHOSTUNREACH) /* gateway is not on a local net */
346 return 0; /* ignore the route entry */
348 CERROR("Error %d creating route %s %d %s\n", rc,
349 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
353 LASSERT (!the_lnet.ln_shutdown);
355 rnet2 = lnet_find_net_locked(net);
358 cfs_list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
362 /* Search for a duplicate route (it's a NOOP if it is) */
364 cfs_list_for_each (e, &rnet2->lrn_routes) {
365 lnet_route_t *route2 = cfs_list_entry(e, lnet_route_t, lr_list);
367 if (route2->lr_gateway == route->lr_gateway) {
372 /* our lookups must be true */
373 LASSERT (route2->lr_gateway->lp_nid != gateway);
377 ni = route->lr_gateway->lp_ni;
378 lnet_ni_addref_locked(ni);
380 lnet_add_route_to_rnet(rnet2, route);
383 /* XXX Assume alive */
384 if (ni->ni_lnd->lnd_notify != NULL)
385 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
389 lnet_peer_decref_locked(route->lr_gateway);
391 LIBCFS_FREE(route, sizeof(*route));
395 LIBCFS_FREE(rnet, sizeof(*rnet));
401 lnet_check_routes (void)
403 lnet_remotenet_t *rnet;
405 lnet_route_t *route2;
411 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
412 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
415 cfs_list_for_each (e2, &rnet->lrn_routes) {
416 route = cfs_list_entry(e2, lnet_route_t, lr_list);
420 else if (route->lr_gateway->lp_ni !=
421 route2->lr_gateway->lp_ni) {
424 CERROR("Routes to %s via %s and %s not supported\n",
425 libcfs_net2str(rnet->lrn_net),
426 libcfs_nid2str(route->lr_gateway->lp_nid),
427 libcfs_nid2str(route2->lr_gateway->lp_nid));
438 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
440 lnet_remotenet_t *rnet;
446 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
447 libcfs_net2str(net), libcfs_nid2str(gw_nid));
449 /* NB Caller may specify either all routes via the given gateway
450 * or a specific route entry actual NIDs) */
455 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
456 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
458 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
459 net == rnet->lrn_net))
462 cfs_list_for_each (e2, &rnet->lrn_routes) {
463 route = cfs_list_entry(e2, lnet_route_t, lr_list);
465 if (!(gw_nid == LNET_NID_ANY ||
466 gw_nid == route->lr_gateway->lp_nid))
469 cfs_list_del(&route->lr_list);
470 the_lnet.ln_remote_nets_version++;
472 if (cfs_list_empty(&rnet->lrn_routes))
473 cfs_list_del(&rnet->lrn_list);
477 lnet_rtr_decref_locked(route->lr_gateway);
478 lnet_peer_decref_locked(route->lr_gateway);
481 LIBCFS_FREE(route, sizeof (*route));
484 LIBCFS_FREE(rnet, sizeof(*rnet));
496 lnet_destroy_routes (void)
498 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
502 lnet_get_route (int idx, __u32 *net, __u32 *hops,
503 lnet_nid_t *gateway, __u32 *alive)
507 lnet_remotenet_t *rnet;
512 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
513 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
515 cfs_list_for_each (e2, &rnet->lrn_routes) {
516 route = cfs_list_entry(e2, lnet_route_t, lr_list);
519 *net = rnet->lrn_net;
520 *hops = route->lr_hops;
521 *gateway = route->lr_gateway->lp_nid;
522 *alive = route->lr_gateway->lp_alive;
534 lnet_swap_pinginfo(lnet_ping_info_t *info)
537 lnet_ni_status_t *stat;
539 __swab32s(&info->pi_version);
540 __swab32s(&info->pi_pid);
541 __swab32s(&info->pi_nnis);
542 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
543 stat = &info->pi_ni[i];
544 __swab64s(&stat->ns_nid);
545 __swab32s(&stat->ns_status);
550 /* Returns # of down NIs, or negative error codes; ignore downed NIs
551 * if a NI in 'net' is up */
553 lnet_router_down_ni(lnet_peer_t *rtr, __u32 net)
559 lnet_ping_info_t *info;
561 if (!avoid_asym_router_failure)
564 if (rtr->lp_rcd == NULL)
568 return -EINVAL; /* stale lp_rcd */
570 info = rtr->lp_rcd->rcd_pinginfo;
571 LASSERT (info != NULL);
573 /* NB always racing with network! */
574 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
575 lnet_swap_pinginfo(info);
576 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
577 CNETERR("%s: Unexpected magic %08x\n",
578 libcfs_nid2str(rtr->lp_nid), info->pi_magic);
582 if (info->pi_version == LNET_PROTO_PING_VERSION1)
583 return -ENOENT; /* v1 doesn't carry NI status info */
585 if (info->pi_version != LNET_PROTO_PING_VERSION) {
586 CNETERR("%s: Unexpected version 0x%x\n",
587 libcfs_nid2str(rtr->lp_nid), info->pi_version);
591 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
592 lnet_ni_status_t *stat = &info->pi_ni[i];
593 lnet_nid_t nid = stat->ns_nid;
595 if (nid == LNET_NID_ANY) {
596 CNETERR("%s: unexpected LNET_NID_ANY\n",
597 libcfs_nid2str(rtr->lp_nid));
601 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
604 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
605 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
612 if (stat->ns_status != LNET_NI_STATUS_UP) {
613 CNETERR("%s: Unexpected status 0x%x\n",
614 libcfs_nid2str(rtr->lp_nid), stat->ns_status);
618 /* ignore downed NIs if there's a NI up for dest network */
619 if (LNET_NIDNET(nid) == net)
622 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
626 /* ptl NIs are considered down only when they're all down */
627 return down + (ptl_up ? 0 : ptl_down);
631 lnet_wait_known_routerstate(void)
637 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
643 cfs_list_for_each (entry, &the_lnet.ln_routers) {
644 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
646 if (rtr->lp_alive_count == 0) {
658 lnet_router_checker();
660 cfs_pause(cfs_time_seconds(1));
665 lnet_router_checker_event (lnet_event_t *event)
667 /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
668 * drop it (that's how come I see _every_ event, even ones that would
670 lnet_rc_data_t *rcd = event->md.user_ptr;
674 if (event->unlinked) {
676 LNetInvalidateHandle(&rcd->rcd_mdh);
680 /* The router checker thread has unlinked the default rc_md
682 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
683 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED;
685 cfs_up(&the_lnet.ln_rc_signal);
690 LASSERT (event->type == LNET_EVENT_SEND ||
691 event->type == LNET_EVENT_REPLY);
693 nid = (event->type == LNET_EVENT_SEND) ?
694 event->target.nid : event->initiator.nid;
696 lp = lnet_find_peer_locked(nid);
698 /* router may have been removed */
699 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
703 if (event->type == LNET_EVENT_SEND) /* re-enable another ping */
704 lp->lp_ping_notsent = 0;
706 if (lnet_isrouter(lp) && /* ignore if no longer a router */
707 (event->status != 0 ||
708 event->type == LNET_EVENT_REPLY)) {
710 /* A successful REPLY means the router is up. If _any_ comms
711 * to the router fail I assume it's down (this will happen if
712 * we ping alive routers to try to detect router death before
713 * apps get burned). */
715 lnet_notify_locked(lp, 1, (event->status == 0),
718 /* The router checker will wake up very shortly and do the
719 * actual notification.
720 * XXX If 'lp' stops being a router before then, it will still
721 * have the notification pending!!! */
724 /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
725 * was in the peer table and I've not dropped the lock, so no-one else
726 * can have reduced the refcount) */
727 LASSERT(lp->lp_refcount > 1);
729 lnet_peer_decref_locked(lp);
733 lnet_update_ni_status(void)
735 cfs_time_t now = cfs_time_current();
740 LASSERT (the_lnet.ln_routing);
742 timeout = router_ping_timeout +
743 MAX(live_router_check_interval, dead_router_check_interval);
747 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
748 lnet_ni_status_t *ns = ni->ni_status;
750 LASSERT (ns != NULL);
752 status = LNET_NI_STATUS_UP;
753 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
754 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
755 cfs_time_seconds(timeout))))
756 status = LNET_NI_STATUS_DOWN;
758 if (ns->ns_status != status) {
759 ns->ns_status = status;
760 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
761 libcfs_nid2str(ni->ni_nid), timeout,
762 status == LNET_NI_STATUS_UP ? "up" : "down");
770 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
772 LASSERT (cfs_list_empty(&rcd->rcd_list));
773 /* detached from network */
774 LASSERT (LNetHandleIsInvalid(rcd->rcd_mdh));
776 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
777 LIBCFS_FREE(rcd, sizeof(*rcd));
782 lnet_create_rc_data (void)
786 lnet_ping_info_t *pi;
789 LIBCFS_ALLOC(rcd, sizeof(*rcd));
793 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
795 LIBCFS_FREE(rcd, sizeof(*rcd));
799 memset(pi, 0, LNET_PINGINFO_SIZE);
800 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
801 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
802 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
804 rcd->rcd_pinginfo = pi;
805 LNetInvalidateHandle(&rcd->rcd_mdh);
806 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
808 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
809 rc = LNetMDBind((lnet_md_t){.start = pi,
811 .length = LNET_PINGINFO_SIZE,
812 .threshold = LNET_MD_THRESH_INF,
813 .options = LNET_MD_TRUNCATE,
814 .eq_handle = the_lnet.ln_rc_eqh},
818 CERROR("Can't bind MD: %d\n", rc);
819 lnet_destroy_rc_data(rcd);
827 lnet_router_check_interval (lnet_peer_t *rtr)
831 secs = rtr->lp_alive ? live_router_check_interval :
832 dead_router_check_interval;
840 lnet_ping_router_locked (lnet_peer_t *rtr)
843 lnet_rc_data_t *rcd = NULL;
844 cfs_time_t now = cfs_time_current();
847 lnet_peer_addref_locked(rtr);
849 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
850 cfs_time_after(now, rtr->lp_ping_deadline))
851 lnet_notify_locked(rtr, 1, 0, now);
853 if (avoid_asym_router_failure && rtr->lp_rcd == NULL)
858 /* Run any outstanding notifications */
862 rcd = lnet_create_rc_data();
866 if (!lnet_isrouter(rtr)) {
867 lnet_peer_decref_locked(rtr);
869 cfs_list_add(&rcd->rcd_list, &the_lnet.ln_zombie_rcd);
870 return; /* router table changed! */
874 LASSERT (rtr->lp_rcd == NULL);
878 secs = lnet_router_check_interval(rtr);
881 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
882 "alive_count %d lp_ping_timestamp %lu\n",
883 libcfs_nid2str(rtr->lp_nid), secs,
884 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
885 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
887 if (secs != 0 && !rtr->lp_ping_notsent &&
888 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
889 cfs_time_seconds(secs)))) {
891 lnet_process_id_t id;
892 lnet_handle_md_t mdh;
894 id.nid = rtr->lp_nid;
895 id.pid = LUSTRE_SRV_LNET_PID;
896 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
898 rtr->lp_ping_notsent = 1;
899 rtr->lp_ping_timestamp = now;
900 mdh = (rtr->lp_rcd == NULL) ? the_lnet.ln_rc_mdh :
901 rtr->lp_rcd->rcd_mdh;
903 if (rtr->lp_ping_deadline == 0)
904 rtr->lp_ping_deadline = cfs_time_shift(router_ping_timeout);
908 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
909 LNET_PROTO_PING_MATCHBITS, 0);
913 rtr->lp_ping_notsent = 0; /* no event pending */
916 lnet_peer_decref_locked(rtr);
921 lnet_router_checker_start(void)
923 static lnet_ping_info_t pinginfo;
932 int router_checker_max_eqsize = 10240;
934 LASSERT (check_routers_before_use);
935 LASSERT (dead_router_check_interval > 0);
939 /* As an approximation, allow each router the same number of
940 * outstanding events as it is allowed outstanding sends */
942 version = the_lnet.ln_routers_version;
943 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
944 lnet_ni_t *ni = rtr->lp_ni;
945 lnet_process_id_t id;
948 eqsz += ni->ni_peertxcredits;
950 /* one async ping reply per router */
951 id.nid = rtr->lp_nid;
952 id.pid = LUSTRE_SRV_LNET_PID;
956 rc = LNetSetAsync(id, 1);
958 CWARN("LNetSetAsync %s failed: %d\n",
959 libcfs_id2str(id), rc);
964 /* NB router list doesn't change in userspace */
965 LASSERT (version == the_lnet.ln_routers_version);
972 "No router found, not starting router checker\n");
976 /* at least allow a SENT and a REPLY per router */
977 if (router_checker_max_eqsize < 2 * nrtr)
978 router_checker_max_eqsize = 2 * nrtr;
981 if (eqsz > router_checker_max_eqsize)
982 eqsz = router_checker_max_eqsize;
985 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
987 if (check_routers_before_use &&
988 dead_router_check_interval <= 0) {
989 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
990 " set if 'check_routers_before_use' is set"
995 if (!the_lnet.ln_routing &&
996 live_router_check_interval <= 0 &&
997 dead_router_check_interval <= 0)
1001 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1002 /* EQ size doesn't matter; the callback is guaranteed to get every
1005 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1006 &the_lnet.ln_rc_eqh);
1008 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1009 &the_lnet.ln_rc_eqh);
1012 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1016 memset(&md, 0, sizeof(md));
1018 md.start = &pinginfo;
1019 md.length = sizeof(pinginfo);
1020 md.options = LNET_MD_TRUNCATE;
1021 md.threshold = LNET_MD_THRESH_INF;
1022 md.eq_handle = the_lnet.ln_rc_eqh;
1023 rc = LNetMDBind(md, LNET_UNLINK, &the_lnet.ln_rc_mdh);
1025 CERROR("Can't bind MD: %d\n", rc);
1026 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1032 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1034 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1036 CERROR("Can't start router checker thread: %d\n", rc);
1037 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1038 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1040 /* block until event callback signals exit */
1041 cfs_down(&the_lnet.ln_rc_signal);
1042 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1044 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1049 if (check_routers_before_use) {
1050 /* Note that a helpful side-effect of pinging all known routers
1051 * at startup is that it makes them drop stale connections they
1052 * may have to a previous instance of me. */
1053 lnet_wait_known_routerstate();
1060 lnet_router_checker_stop (void)
1064 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1067 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1068 the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
1071 /* block until event callback signals exit */
1072 cfs_down(&the_lnet.ln_rc_signal);
1074 while (the_lnet.ln_rc_state != LNET_RC_STATE_UNLINKED) {
1075 lnet_router_checker();
1076 cfs_pause(cfs_time_seconds(1));
1079 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
1081 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1083 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1087 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1090 lnet_prune_zombie_rcd (int wait_unlink)
1092 lnet_rc_data_t *rcd;
1093 lnet_rc_data_t *tmp;
1094 cfs_list_t free_rcd;
1098 CFS_INIT_LIST_HEAD(&free_rcd);
1102 version = the_lnet.ln_routers_version;
1103 cfs_list_for_each_entry_safe (rcd, tmp, &the_lnet.ln_zombie_rcd,
1105 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1106 cfs_list_del(&rcd->rcd_list);
1107 cfs_list_add(&rcd->rcd_list, &free_rcd);
1113 LNetMDUnlink(rcd->rcd_mdh);
1116 if (version != the_lnet.ln_routers_version)
1121 while (wait_unlink && !cfs_list_empty(&the_lnet.ln_zombie_rcd)) {
1122 rcd = cfs_list_entry(the_lnet.ln_zombie_rcd.next,
1123 lnet_rc_data_t, rcd_list);
1124 if (LNetHandleIsInvalid(rcd->rcd_mdh)) {
1125 cfs_list_del(&rcd->rcd_list);
1126 cfs_list_add(&rcd->rcd_list, &free_rcd);
1132 LNetMDUnlink(rcd->rcd_mdh);
1135 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1136 "Waiting for rc buffers to unlink\n");
1137 cfs_pause(cfs_time_seconds(1));
1144 while (!cfs_list_empty(&free_rcd)) {
1145 rcd = cfs_list_entry(free_rcd.next, lnet_rc_data_t, rcd_list);
1146 cfs_list_del_init(&rcd->rcd_list);
1147 lnet_destroy_rc_data(rcd);
1153 lnet_router_checker(void *arg)
1159 cfs_daemonize("router_checker");
1160 cfs_block_allsigs();
1162 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1164 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1169 version = the_lnet.ln_routers_version;
1171 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1172 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1173 lnet_ping_router_locked(rtr);
1175 /* NB dropped lock */
1176 if (version != the_lnet.ln_routers_version) {
1177 /* the routers list has changed */
1184 if (the_lnet.ln_routing)
1185 lnet_update_ni_status();
1187 lnet_prune_zombie_rcd(0); /* don't wait for UNLINK */
1189 /* Call cfs_pause() here always adds 1 to load average
1190 * because kernel counts # active tasks as nr_running
1191 * + nr_uninterruptible. */
1192 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1193 cfs_time_seconds(1));
1198 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1199 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1201 if (rtr->lp_rcd == NULL)
1204 LASSERT (cfs_list_empty(&rtr->lp_rcd->rcd_list));
1205 cfs_list_add(&rtr->lp_rcd->rcd_list, &the_lnet.ln_zombie_rcd);
1211 lnet_prune_zombie_rcd(1); /* wait for UNLINK */
1213 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
1214 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1216 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1219 /* The unlink event callback will signal final completion */
1224 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1226 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1228 while (--npages >= 0)
1229 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1231 LIBCFS_FREE(rb, sz);
1235 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1237 int npages = rbp->rbp_npages;
1238 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1243 LIBCFS_ALLOC(rb, sz);
1249 for (i = 0; i < npages; i++) {
1250 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1253 cfs_free_page(rb->rb_kiov[i].kiov_page);
1255 LIBCFS_FREE(rb, sz);
1259 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1260 rb->rb_kiov[i].kiov_offset = 0;
1261 rb->rb_kiov[i].kiov_page = page;
1268 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1270 int npages = rbp->rbp_npages;
1274 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1275 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1277 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1278 LASSERT (rbp->rbp_credits > 0);
1280 rb = cfs_list_entry(rbp->rbp_bufs.next,
1281 lnet_rtrbuf_t, rb_list);
1282 cfs_list_del(&rb->rb_list);
1283 lnet_destroy_rtrbuf(rb, npages);
1287 LASSERT (rbp->rbp_nbuffers == nbuffers);
1288 LASSERT (rbp->rbp_credits == nbuffers);
1290 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1294 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1299 if (rbp->rbp_nbuffers != 0) {
1300 LASSERT (rbp->rbp_nbuffers == nbufs);
1304 for (i = 0; i < nbufs; i++) {
1305 rb = lnet_new_rtrbuf(rbp);
1308 CERROR("Failed to allocate %d router bufs of %d pages\n",
1309 nbufs, rbp->rbp_npages);
1313 rbp->rbp_nbuffers++;
1315 rbp->rbp_mincredits++;
1316 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1318 /* No allocation "under fire" */
1319 /* Otherwise we'd need code to schedule blocked msgs etc */
1320 LASSERT (!the_lnet.ln_routing);
1323 LASSERT (rbp->rbp_credits == nbufs);
1328 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1330 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1331 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1333 rbp->rbp_npages = npages;
1334 rbp->rbp_credits = 0;
1335 rbp->rbp_mincredits = 0;
1339 lnet_free_rtrpools(void)
1341 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1342 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1343 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1347 lnet_init_rtrpools(void)
1349 int small_pages = 1;
1350 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1352 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1353 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1354 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1359 lnet_alloc_rtrpools(int im_a_router)
1363 if (!strcmp(forwarding, "")) {
1364 /* not set either way */
1367 } else if (!strcmp(forwarding, "disabled")) {
1368 /* explicitly disabled */
1370 } else if (!strcmp(forwarding, "enabled")) {
1371 /* explicitly enabled */
1373 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1374 "'enabled' or 'disabled'\n");
1378 if (tiny_router_buffers <= 0) {
1379 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1380 "routing enabled\n", tiny_router_buffers);
1385 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1386 tiny_router_buffers);
1390 if (small_router_buffers <= 0) {
1391 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1392 " routing enabled\n", small_router_buffers);
1397 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1398 small_router_buffers);
1402 if (large_router_buffers <= 0) {
1403 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1404 " routing enabled\n", large_router_buffers);
1409 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1410 large_router_buffers);
1415 the_lnet.ln_routing = 1;
1421 lnet_free_rtrpools();
1426 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1428 lnet_peer_t *lp = NULL;
1429 cfs_time_t now = cfs_time_current();
1431 LASSERT (!cfs_in_interrupt ());
1433 CDEBUG (D_NET, "%s notifying %s: %s\n",
1434 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1435 libcfs_nid2str(nid),
1436 alive ? "up" : "down");
1439 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1440 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1441 libcfs_nid2str(nid), alive ? "birth" : "death",
1442 libcfs_nid2str(ni->ni_nid));
1446 /* can't do predictions... */
1447 if (cfs_time_after(when, now)) {
1448 CWARN ("Ignoring prediction from %s of %s %s "
1449 "%ld seconds in the future\n",
1450 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1451 libcfs_nid2str(nid), alive ? "up" : "down",
1452 cfs_duration_sec(cfs_time_sub(when, now)));
1456 if (ni != NULL && !alive && /* LND telling me she's down */
1457 !auto_down) { /* auto-down disabled */
1458 CDEBUG(D_NET, "Auto-down disabled\n");
1464 lp = lnet_find_peer_locked(nid);
1468 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1472 /* We can't fully trust LND on reporting exact peer last_alive
1473 * if he notifies us about dead peer. For example ksocklnd can
1474 * call us with when == _time_when_the_node_was_booted_ if
1475 * no connections were successfully established */
1476 if (ni != NULL && !alive && when < lp->lp_last_alive)
1477 when = lp->lp_last_alive;
1479 lnet_notify_locked(lp, ni == NULL, alive, when);
1487 lnet_peer_decref_locked(lp);
1492 EXPORT_SYMBOL(lnet_notify);
1495 lnet_get_tunables (void)
1503 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1509 lnet_router_checker (void)
1511 static time_t last = 0;
1512 static int running = 0;
1514 time_t now = cfs_time_current_sec();
1515 int interval = now - last;
1520 /* It's no use to call me again within a sec - all intervals and
1521 * timeouts are measured in seconds */
1522 if (last != 0 && interval < 2)
1526 interval > MAX(live_router_check_interval,
1527 dead_router_check_interval))
1528 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1529 live_router_check_interval, dead_router_check_interval,
1533 LASSERT (!running); /* recursion check */
1539 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD) {
1540 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
1541 rc = LNetMDUnlink(the_lnet.ln_rc_mdh);
1545 /* consume all pending events */
1550 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1551 * recursion breaker in LNetEQPoll would fail */
1552 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1553 if (rc == 0) /* no event pending */
1556 /* NB a lost SENT prevents me from pinging a router again */
1557 if (rc == -EOVERFLOW) {
1558 CERROR("Dropped an event!!!\n");
1565 lnet_router_checker_event(&ev);
1569 if (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED ||
1570 the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING) {
1575 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1579 version = the_lnet.ln_routers_version;
1580 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1581 lnet_ping_router_locked(rtr);
1582 LASSERT (version == the_lnet.ln_routers_version);
1587 running = 0; /* lock only needed for the recursion check */
1591 /* NB lnet_peers_start_down depends on me,
1592 * so must be called before any peer creation */
1594 lnet_get_tunables (void)
1598 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1599 if (s != NULL) router_ping_timeout = atoi(s);
1601 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1602 if (s != NULL) live_router_check_interval = atoi(s);
1604 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1605 if (s != NULL) dead_router_check_interval = atoi(s);
1607 /* This replaces old lnd_notify mechanism */
1608 check_routers_before_use = 1;
1609 if (dead_router_check_interval <= 0)
1610 dead_router_check_interval = 30;
1614 lnet_free_rtrpools (void)
1619 lnet_init_rtrpools (void)
1624 lnet_alloc_rtrpools (int im_a_arouter)