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 lp->lp_ping_version = LNET_PROTO_PING_UNKNOWN; /* reset */
128 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
132 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
137 /* Notify only in 1 thread at any time to ensure ordered notification.
138 * NB individual events can be missed; the only guarantee is that you
139 * always get the most recent news */
141 if (lp->lp_notifying)
144 lp->lp_notifying = 1;
146 while (lp->lp_notify) {
147 alive = lp->lp_alive;
148 notifylnd = lp->lp_notifylnd;
150 lp->lp_notifylnd = 0;
153 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
156 /* A new notification could happen now; I'll handle it
157 * when control returns to me */
159 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
165 lp->lp_notifying = 0;
170 lnet_rtr_addref_locked(lnet_peer_t *lp)
172 LASSERT (lp->lp_refcount > 0);
173 LASSERT (lp->lp_rtr_refcount >= 0);
175 lp->lp_rtr_refcount++;
176 if (lp->lp_rtr_refcount == 1) {
179 /* a simple insertion sort */
180 cfs_list_for_each_prev(pos, &the_lnet.ln_routers) {
181 lnet_peer_t *rtr = cfs_list_entry(pos, lnet_peer_t,
184 if (rtr->lp_nid < lp->lp_nid)
188 cfs_list_add(&lp->lp_rtr_list, pos);
189 /* addref for the_lnet.ln_routers */
190 lnet_peer_addref_locked(lp);
191 the_lnet.ln_routers_version++;
196 lnet_rtr_decref_locked(lnet_peer_t *lp)
198 LASSERT (lp->lp_refcount > 0);
199 LASSERT (lp->lp_rtr_refcount > 0);
201 lp->lp_rtr_refcount--;
202 if (lp->lp_rtr_refcount == 0) {
203 LASSERT(cfs_list_empty(&lp->lp_routes));
205 if (lp->lp_rcd != NULL) {
206 cfs_list_add(&lp->lp_rcd->rcd_list,
207 &the_lnet.ln_rcd_deathrow);
211 cfs_list_del(&lp->lp_rtr_list);
212 /* decref for the_lnet.ln_routers */
213 lnet_peer_decref_locked(lp);
214 the_lnet.ln_routers_version++;
219 lnet_find_net_locked (__u32 net)
221 lnet_remotenet_t *rnet;
224 LASSERT (!the_lnet.ln_shutdown);
226 cfs_list_for_each (tmp, &the_lnet.ln_remote_nets) {
227 rnet = cfs_list_entry(tmp, lnet_remotenet_t, lrn_list);
229 if (rnet->lrn_net == net)
235 static void lnet_shuffle_seed(void)
237 static int seeded = 0;
238 int lnd_type, seed[2];
246 cfs_get_random_bytes(seed, sizeof(seed));
248 /* Nodes with small feet have little entropy
249 * the NID for this node gives the most entropy in the low bits */
250 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
251 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
252 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
254 if (lnd_type != LOLND)
255 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
258 cfs_gettimeofday(&tv);
259 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
264 /* NB expects LNET_LOCK held */
266 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
268 unsigned int len = 0;
269 unsigned int offset = 0;
274 cfs_list_for_each (e, &rnet->lrn_routes) {
278 /* len+1 positions to add a new entry, also prevents division by 0 */
279 offset = cfs_rand() % (len + 1);
280 cfs_list_for_each (e, &rnet->lrn_routes) {
285 cfs_list_add(&route->lr_list, e);
286 cfs_list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
288 the_lnet.ln_remote_nets_version++;
289 lnet_rtr_addref_locked(route->lr_gateway);
293 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
296 lnet_remotenet_t *rnet;
297 lnet_remotenet_t *rnet2;
303 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
304 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
306 if (gateway == LNET_NID_ANY ||
307 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
308 net == LNET_NIDNET(LNET_NID_ANY) ||
309 LNET_NETTYP(net) == LOLND ||
310 LNET_NIDNET(gateway) == net ||
311 hops < 1 || hops > 255)
314 if (lnet_islocalnet(net)) /* it's a local network */
315 return 0; /* ignore the route entry */
317 /* Assume net, route, all new */
318 LIBCFS_ALLOC(route, sizeof(*route));
319 LIBCFS_ALLOC(rnet, sizeof(*rnet));
320 if (route == NULL || rnet == NULL) {
321 CERROR("Out of memory creating route %s %d %s\n",
322 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
324 LIBCFS_FREE(route, sizeof(*route));
326 LIBCFS_FREE(rnet, sizeof(*rnet));
330 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
332 route->lr_hops = hops;
337 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
341 LIBCFS_FREE(route, sizeof(*route));
342 LIBCFS_FREE(rnet, sizeof(*rnet));
344 if (rc == -EHOSTUNREACH) { /* gateway is not on a local net */
345 return 0; /* ignore the route entry */
347 CERROR("Error %d creating route %s %d %s\n", rc,
348 libcfs_net2str(net), hops,
349 libcfs_nid2str(gateway));
354 LASSERT (!the_lnet.ln_shutdown);
356 rnet2 = lnet_find_net_locked(net);
359 cfs_list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
363 /* Search for a duplicate route (it's a NOOP if it is) */
365 cfs_list_for_each (e, &rnet2->lrn_routes) {
366 lnet_route_t *route2 = cfs_list_entry(e, lnet_route_t, lr_list);
368 if (route2->lr_gateway == route->lr_gateway) {
373 /* our lookups must be true */
374 LASSERT (route2->lr_gateway->lp_nid != gateway);
378 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
379 lnet_add_route_to_rnet(rnet2, route);
381 ni = route->lr_gateway->lp_ni;
384 /* XXX Assume alive */
385 if (ni->ni_lnd->lnd_notify != NULL)
386 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
391 /* -1 for notify or !add_route */
392 lnet_peer_decref_locked(route->lr_gateway);
396 LIBCFS_FREE(route, sizeof(*route));
399 LIBCFS_FREE(rnet, sizeof(*rnet));
405 lnet_check_routes (void)
407 lnet_remotenet_t *rnet;
409 lnet_route_t *route2;
415 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
416 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
419 cfs_list_for_each (e2, &rnet->lrn_routes) {
424 route = cfs_list_entry(e2, lnet_route_t, lr_list);
426 if (route2 == NULL) {
431 if (route->lr_gateway->lp_ni ==
432 route2->lr_gateway->lp_ni)
435 nid1 = route->lr_gateway->lp_nid;
436 nid2 = route2->lr_gateway->lp_nid;
441 CERROR("Routes to %s via %s and %s not supported\n",
442 libcfs_net2str(net), libcfs_nid2str(nid1),
443 libcfs_nid2str(nid2));
453 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
455 struct lnet_peer *gateway;
456 lnet_remotenet_t *rnet;
462 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
463 libcfs_net2str(net), libcfs_nid2str(gw_nid));
465 /* NB Caller may specify either all routes via the given gateway
466 * or a specific route entry actual NIDs) */
471 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
472 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
474 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
475 net == rnet->lrn_net))
478 cfs_list_for_each (e2, &rnet->lrn_routes) {
479 route = cfs_list_entry(e2, lnet_route_t, lr_list);
481 gateway = route->lr_gateway;
482 if (!(gw_nid == LNET_NID_ANY ||
483 gw_nid == gateway->lp_nid))
486 cfs_list_del(&route->lr_list);
487 cfs_list_del(&route->lr_gwlist);
488 the_lnet.ln_remote_nets_version++;
490 if (cfs_list_empty(&rnet->lrn_routes))
491 cfs_list_del(&rnet->lrn_list);
495 lnet_rtr_decref_locked(gateway);
496 lnet_peer_decref_locked(gateway);
499 LIBCFS_FREE(route, sizeof (*route));
502 LIBCFS_FREE(rnet, sizeof(*rnet));
514 lnet_destroy_routes (void)
516 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
520 lnet_get_route (int idx, __u32 *net, __u32 *hops,
521 lnet_nid_t *gateway, __u32 *alive)
525 lnet_remotenet_t *rnet;
530 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
531 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
533 cfs_list_for_each (e2, &rnet->lrn_routes) {
534 route = cfs_list_entry(e2, lnet_route_t, lr_list);
537 *net = rnet->lrn_net;
538 *hops = route->lr_hops;
539 *gateway = route->lr_gateway->lp_nid;
540 *alive = route->lr_gateway->lp_alive;
552 lnet_swap_pinginfo(lnet_ping_info_t *info)
555 lnet_ni_status_t *stat;
557 __swab32s(&info->pi_magic);
558 __swab32s(&info->pi_version);
559 __swab32s(&info->pi_pid);
560 __swab32s(&info->pi_nnis);
561 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
562 stat = &info->pi_ni[i];
563 __swab64s(&stat->ns_nid);
564 __swab32s(&stat->ns_status);
570 * parse router-checker pinginfo, record number of down NIs for remote
571 * networks on that router.
574 lnet_parse_rc_info(lnet_rc_data_t *rcd)
576 lnet_ping_info_t *info = rcd->rcd_pinginfo;
577 struct lnet_peer *gw = rcd->rcd_gateway;
583 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
584 lnet_swap_pinginfo(info);
586 /* NB always racing with network! */
587 if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
588 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
589 libcfs_nid2str(gw->lp_nid), info->pi_magic);
590 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
594 gw->lp_ping_version = info->pi_version;
595 if (gw->lp_ping_version == LNET_PROTO_PING_VERSION_1)
596 return; /* v1 doesn't carry NI status info */
598 if (gw->lp_ping_version != LNET_PROTO_PING_VERSION) {
599 CDEBUG(D_NET, "%s: Unexpected version 0x%x\n",
600 libcfs_nid2str(gw->lp_nid), gw->lp_ping_version);
601 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
605 cfs_list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
606 int ptl_status = LNET_NI_STATUS_INVALID;
611 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
612 lnet_ni_status_t *stat = &info->pi_ni[i];
613 lnet_nid_t nid = stat->ns_nid;
615 if (nid == LNET_NID_ANY) {
616 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
617 libcfs_nid2str(gw->lp_nid));
618 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
622 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
625 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
626 if (LNET_NETTYP(LNET_NIDNET(nid)) != PTLLND)
628 else if (ptl_status != LNET_NI_STATUS_UP)
629 ptl_status = LNET_NI_STATUS_DOWN;
633 if (stat->ns_status == LNET_NI_STATUS_UP) {
634 if (LNET_NIDNET(nid) == rtr->lr_net) {
638 /* ptl NIs are considered down only when
639 * they're all down */
640 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
641 ptl_status = LNET_NI_STATUS_UP;
645 CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
646 libcfs_nid2str(gw->lp_nid), stat->ns_status);
647 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
651 if (up) { /* ignore downed NIs if NI for dest network is up */
655 rtr->lr_downis = down + (ptl_status == LNET_NI_STATUS_DOWN);
660 lnet_router_checker_event(lnet_event_t *event)
662 /* CAVEAT EMPTOR: I'm called with lnet_res_locked */
663 lnet_rc_data_t *rcd = event->md.user_ptr;
664 struct lnet_peer *lp;
666 LASSERT(rcd != NULL);
668 if (event->unlinked) {
669 LNetInvalidateHandle(&rcd->rcd_mdh);
673 LASSERT(event->type == LNET_EVENT_SEND ||
674 event->type == LNET_EVENT_REPLY);
676 lp = rcd->rcd_gateway;
679 if (!lnet_isrouter(lp)) /* ignore if no longer a router */
682 if (event->type == LNET_EVENT_SEND) {
683 lp->lp_ping_notsent = 0; /* NB: re-enable another ping */
684 if (event->status == 0)
688 /* LNET_EVENT_REPLY */
689 /* A successful REPLY means the router is up. If _any_ comms
690 * to the router fail I assume it's down (this will happen if
691 * we ping alive routers to try to detect router death before
692 * apps get burned). */
694 lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
695 /* The router checker will wake up very shortly and do the
696 * actual notification.
697 * XXX If 'lp' stops being a router before then, it will still
698 * have the notification pending!!! */
700 if (avoid_asym_router_failure && event->status == 0)
701 lnet_parse_rc_info(rcd);
705 lnet_wait_known_routerstate(void)
711 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
717 cfs_list_for_each (entry, &the_lnet.ln_routers) {
718 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
720 if (rtr->lp_alive_count == 0) {
732 lnet_router_checker();
734 cfs_pause(cfs_time_seconds(1));
739 lnet_update_ni_status_locked(void)
745 LASSERT(the_lnet.ln_routing);
747 timeout = router_ping_timeout +
748 MAX(live_router_check_interval, dead_router_check_interval);
750 now = cfs_time_current_sec();
751 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
752 if (ni->ni_lnd->lnd_type == LOLND)
755 if (now < ni->ni_last_alive + timeout)
758 LASSERT(ni->ni_status != NULL);
760 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
761 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
762 libcfs_nid2str(ni->ni_nid), timeout);
763 /* NB: so far, this is the only place to set
764 * NI status to "down" */
765 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
771 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
773 LASSERT(cfs_list_empty(&rcd->rcd_list));
774 /* detached from network */
775 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
777 if (rcd->rcd_gateway != NULL) {
779 lnet_peer_decref_locked(rcd->rcd_gateway);
783 if (rcd->rcd_pinginfo != NULL)
784 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
786 LIBCFS_FREE(rcd, sizeof(*rcd));
790 lnet_create_rc_data_locked(lnet_peer_t *gateway)
792 lnet_rc_data_t *rcd = NULL;
793 lnet_ping_info_t *pi;
799 LIBCFS_ALLOC(rcd, sizeof(*rcd));
803 LNetInvalidateHandle(&rcd->rcd_mdh);
804 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
806 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
810 memset(pi, 0, LNET_PINGINFO_SIZE);
811 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
812 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
813 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
815 rcd->rcd_pinginfo = pi;
817 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
818 rc = LNetMDBind((lnet_md_t){.start = pi,
820 .length = LNET_PINGINFO_SIZE,
821 .threshold = LNET_MD_THRESH_INF,
822 .options = LNET_MD_TRUNCATE,
823 .eq_handle = the_lnet.ln_rc_eqh},
827 CERROR("Can't bind MD: %d\n", rc);
833 /* router table changed or someone has created rcd for this gateway */
834 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
839 lnet_peer_addref_locked(gateway);
840 rcd->rcd_gateway = gateway;
841 gateway->lp_rcd = rcd;
846 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
847 rc = LNetMDUnlink(rcd->rcd_mdh);
850 lnet_destroy_rc_data(rcd);
854 return gateway->lp_rcd;
858 lnet_router_check_interval (lnet_peer_t *rtr)
862 secs = rtr->lp_alive ? live_router_check_interval :
863 dead_router_check_interval;
871 lnet_ping_router_locked (lnet_peer_t *rtr)
873 lnet_rc_data_t *rcd = NULL;
874 cfs_time_t now = cfs_time_current();
877 lnet_peer_addref_locked(rtr);
879 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
880 cfs_time_after(now, rtr->lp_ping_deadline))
881 lnet_notify_locked(rtr, 1, 0, now);
883 /* Run any outstanding notifications */
884 lnet_ni_notify_locked(rtr->lp_ni, rtr);
886 if (!lnet_isrouter(rtr) ||
887 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
888 /* router table changed or router checker is shutting down */
889 lnet_peer_decref_locked(rtr);
893 rcd = rtr->lp_rcd != NULL ?
894 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
899 secs = lnet_router_check_interval(rtr);
902 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
903 "alive_count %d lp_ping_timestamp %lu\n",
904 libcfs_nid2str(rtr->lp_nid), secs,
905 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
906 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
908 if (secs != 0 && !rtr->lp_ping_notsent &&
909 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
910 cfs_time_seconds(secs)))) {
912 lnet_process_id_t id;
913 lnet_handle_md_t mdh;
915 id.nid = rtr->lp_nid;
916 id.pid = LUSTRE_SRV_LNET_PID;
917 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
919 rtr->lp_ping_notsent = 1;
920 rtr->lp_ping_timestamp = now;
924 if (rtr->lp_ping_deadline == 0) {
925 rtr->lp_ping_deadline = \
926 cfs_time_shift(router_ping_timeout);
931 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
932 LNET_PROTO_PING_MATCHBITS, 0);
936 rtr->lp_ping_notsent = 0; /* no event pending */
939 lnet_peer_decref_locked(rtr);
944 lnet_router_checker_start(void)
952 int router_checker_max_eqsize = 10240;
954 LASSERT (check_routers_before_use);
955 LASSERT (dead_router_check_interval > 0);
959 /* As an approximation, allow each router the same number of
960 * outstanding events as it is allowed outstanding sends */
962 version = the_lnet.ln_routers_version;
963 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
964 lnet_ni_t *ni = rtr->lp_ni;
965 lnet_process_id_t id;
968 eqsz += ni->ni_peertxcredits;
970 /* one async ping reply per router */
971 id.nid = rtr->lp_nid;
972 id.pid = LUSTRE_SRV_LNET_PID;
976 rc = LNetSetAsync(id, 1);
978 CWARN("LNetSetAsync %s failed: %d\n",
979 libcfs_id2str(id), rc);
984 /* NB router list doesn't change in userspace */
985 LASSERT (version == the_lnet.ln_routers_version);
992 "No router found, not starting router checker\n");
996 /* at least allow a SENT and a REPLY per router */
997 if (router_checker_max_eqsize < 2 * nrtr)
998 router_checker_max_eqsize = 2 * nrtr;
1001 if (eqsz > router_checker_max_eqsize)
1002 eqsz = router_checker_max_eqsize;
1005 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1007 if (check_routers_before_use &&
1008 dead_router_check_interval <= 0) {
1009 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1010 " set if 'check_routers_before_use' is set"
1015 if (!the_lnet.ln_routing &&
1016 live_router_check_interval <= 0 &&
1017 dead_router_check_interval <= 0)
1021 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1022 /* EQ size doesn't matter; the callback is guaranteed to get every
1025 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1026 &the_lnet.ln_rc_eqh);
1028 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1029 &the_lnet.ln_rc_eqh);
1032 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1036 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1038 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1040 CERROR("Can't start router checker thread: %d\n", rc);
1041 /* block until event callback signals exit */
1042 cfs_down(&the_lnet.ln_rc_signal);
1043 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1045 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1050 if (check_routers_before_use) {
1051 /* Note that a helpful side-effect of pinging all known routers
1052 * at startup is that it makes them drop stale connections they
1053 * may have to a previous instance of me. */
1054 lnet_wait_known_routerstate();
1061 lnet_router_checker_stop (void)
1065 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1068 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1069 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1072 /* block until event callback signals exit */
1073 cfs_down(&the_lnet.ln_rc_signal);
1075 lnet_router_checker();
1077 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1079 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1085 lnet_prune_rc_data(int wait_unlink)
1087 lnet_rc_data_t *rcd;
1088 lnet_rc_data_t *tmp;
1093 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1094 cfs_list_empty(&the_lnet.ln_rcd_deathrow) &&
1095 cfs_list_empty(&the_lnet.ln_rcd_zombie)))
1098 CFS_INIT_LIST_HEAD(&head);
1102 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1103 /* router checker is stopping, prune all */
1104 cfs_list_for_each_entry(lp, &the_lnet.ln_routers,
1106 if (lp->lp_rcd == NULL)
1109 LASSERT(cfs_list_empty(&lp->lp_rcd->rcd_list));
1110 cfs_list_add(&lp->lp_rcd->rcd_list,
1111 &the_lnet.ln_rcd_deathrow);
1116 /* unlink all RCDs on deathrow list */
1117 cfs_list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1119 if (!cfs_list_empty(&head)) {
1122 cfs_list_for_each_entry(rcd, &head, rcd_list)
1123 LNetMDUnlink(rcd->rcd_mdh);
1128 cfs_list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1130 /* release all zombie RCDs */
1131 while (!cfs_list_empty(&the_lnet.ln_rcd_zombie)) {
1132 cfs_list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1134 if (!LNetHandleIsInvalid(rcd->rcd_mdh))
1135 cfs_list_move(&rcd->rcd_list, &head);
1138 wait_unlink = wait_unlink &&
1139 !cfs_list_empty(&the_lnet.ln_rcd_zombie);
1143 while (!cfs_list_empty(&head)) {
1144 rcd = cfs_list_entry(head.next,
1145 lnet_rc_data_t, rcd_list);
1146 cfs_list_del_init(&rcd->rcd_list);
1147 lnet_destroy_rc_data(rcd);
1154 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1155 "Waiting for rc buffers to unlink\n");
1156 cfs_pause(cfs_time_seconds(1) / 4);
1163 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1166 lnet_router_checker(void *arg)
1171 cfs_daemonize("router_checker");
1172 cfs_block_allsigs();
1174 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1176 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1181 version = the_lnet.ln_routers_version;
1183 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1184 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1185 lnet_ping_router_locked(rtr);
1187 /* NB dropped lock */
1188 if (version != the_lnet.ln_routers_version) {
1189 /* the routers list has changed */
1194 if (the_lnet.ln_routing)
1195 lnet_update_ni_status_locked();
1199 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1201 /* Call cfs_pause() here always adds 1 to load average
1202 * because kernel counts # active tasks as nr_running
1203 * + nr_uninterruptible. */
1204 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1205 cfs_time_seconds(1));
1208 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1210 lnet_prune_rc_data(1); /* wait for UNLINK */
1212 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1213 cfs_up(&the_lnet.ln_rc_signal);
1214 /* The unlink event callback will signal final completion */
1219 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1221 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1223 while (--npages >= 0)
1224 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1226 LIBCFS_FREE(rb, sz);
1230 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1232 int npages = rbp->rbp_npages;
1233 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1238 LIBCFS_ALLOC(rb, sz);
1244 for (i = 0; i < npages; i++) {
1245 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1248 cfs_free_page(rb->rb_kiov[i].kiov_page);
1250 LIBCFS_FREE(rb, sz);
1254 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1255 rb->rb_kiov[i].kiov_offset = 0;
1256 rb->rb_kiov[i].kiov_page = page;
1263 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1265 int npages = rbp->rbp_npages;
1269 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1270 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1272 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1273 LASSERT (rbp->rbp_credits > 0);
1275 rb = cfs_list_entry(rbp->rbp_bufs.next,
1276 lnet_rtrbuf_t, rb_list);
1277 cfs_list_del(&rb->rb_list);
1278 lnet_destroy_rtrbuf(rb, npages);
1282 LASSERT (rbp->rbp_nbuffers == nbuffers);
1283 LASSERT (rbp->rbp_credits == nbuffers);
1285 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1289 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1294 if (rbp->rbp_nbuffers != 0) {
1295 LASSERT (rbp->rbp_nbuffers == nbufs);
1299 for (i = 0; i < nbufs; i++) {
1300 rb = lnet_new_rtrbuf(rbp);
1303 CERROR("Failed to allocate %d router bufs of %d pages\n",
1304 nbufs, rbp->rbp_npages);
1308 rbp->rbp_nbuffers++;
1310 rbp->rbp_mincredits++;
1311 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1313 /* No allocation "under fire" */
1314 /* Otherwise we'd need code to schedule blocked msgs etc */
1315 LASSERT (!the_lnet.ln_routing);
1318 LASSERT (rbp->rbp_credits == nbufs);
1323 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1325 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1326 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1328 rbp->rbp_npages = npages;
1329 rbp->rbp_credits = 0;
1330 rbp->rbp_mincredits = 0;
1334 lnet_free_rtrpools(void)
1336 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1337 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1338 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1342 lnet_init_rtrpools(void)
1344 int small_pages = 1;
1345 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1347 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1348 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1349 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1354 lnet_alloc_rtrpools(int im_a_router)
1358 if (!strcmp(forwarding, "")) {
1359 /* not set either way */
1362 } else if (!strcmp(forwarding, "disabled")) {
1363 /* explicitly disabled */
1365 } else if (!strcmp(forwarding, "enabled")) {
1366 /* explicitly enabled */
1368 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1369 "'enabled' or 'disabled'\n");
1373 if (tiny_router_buffers <= 0) {
1374 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1375 "routing enabled\n", tiny_router_buffers);
1380 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1381 tiny_router_buffers);
1385 if (small_router_buffers <= 0) {
1386 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1387 " routing enabled\n", small_router_buffers);
1392 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1393 small_router_buffers);
1397 if (large_router_buffers <= 0) {
1398 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1399 " routing enabled\n", large_router_buffers);
1404 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1405 large_router_buffers);
1410 the_lnet.ln_routing = 1;
1416 lnet_free_rtrpools();
1421 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1423 lnet_peer_t *lp = NULL;
1424 cfs_time_t now = cfs_time_current();
1426 LASSERT (!cfs_in_interrupt ());
1428 CDEBUG (D_NET, "%s notifying %s: %s\n",
1429 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1430 libcfs_nid2str(nid),
1431 alive ? "up" : "down");
1434 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1435 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1436 libcfs_nid2str(nid), alive ? "birth" : "death",
1437 libcfs_nid2str(ni->ni_nid));
1441 /* can't do predictions... */
1442 if (cfs_time_after(when, now)) {
1443 CWARN ("Ignoring prediction from %s of %s %s "
1444 "%ld seconds in the future\n",
1445 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1446 libcfs_nid2str(nid), alive ? "up" : "down",
1447 cfs_duration_sec(cfs_time_sub(when, now)));
1451 if (ni != NULL && !alive && /* LND telling me she's down */
1452 !auto_down) { /* auto-down disabled */
1453 CDEBUG(D_NET, "Auto-down disabled\n");
1459 lp = lnet_find_peer_locked(nid);
1463 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1467 /* We can't fully trust LND on reporting exact peer last_alive
1468 * if he notifies us about dead peer. For example ksocklnd can
1469 * call us with when == _time_when_the_node_was_booted_ if
1470 * no connections were successfully established */
1471 if (ni != NULL && !alive && when < lp->lp_last_alive)
1472 when = lp->lp_last_alive;
1474 lnet_notify_locked(lp, ni == NULL, alive, when);
1476 lnet_ni_notify_locked(ni, lp);
1478 lnet_peer_decref_locked(lp);
1483 EXPORT_SYMBOL(lnet_notify);
1486 lnet_get_tunables (void)
1494 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1500 lnet_router_checker (void)
1502 static time_t last = 0;
1503 static int running = 0;
1505 time_t now = cfs_time_current_sec();
1506 int interval = now - last;
1511 /* It's no use to call me again within a sec - all intervals and
1512 * timeouts are measured in seconds */
1513 if (last != 0 && interval < 2)
1517 interval > MAX(live_router_check_interval,
1518 dead_router_check_interval))
1519 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1520 live_router_check_interval, dead_router_check_interval,
1524 LASSERT (!running); /* recursion check */
1530 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1531 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1533 /* consume all pending events */
1538 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1539 * recursion breaker in LNetEQPoll would fail */
1540 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1541 if (rc == 0) /* no event pending */
1544 /* NB a lost SENT prevents me from pinging a router again */
1545 if (rc == -EOVERFLOW) {
1546 CERROR("Dropped an event!!!\n");
1553 lnet_router_checker_event(&ev);
1557 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1558 lnet_prune_rc_data(1); /* release rcd */
1559 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1564 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1568 version = the_lnet.ln_routers_version;
1569 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1570 lnet_ping_router_locked(rtr);
1571 LASSERT (version == the_lnet.ln_routers_version);
1576 running = 0; /* lock only needed for the recursion check */
1580 /* NB lnet_peers_start_down depends on me,
1581 * so must be called before any peer creation */
1583 lnet_get_tunables (void)
1587 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1588 if (s != NULL) router_ping_timeout = atoi(s);
1590 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1591 if (s != NULL) live_router_check_interval = atoi(s);
1593 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1594 if (s != NULL) dead_router_check_interval = atoi(s);
1596 /* This replaces old lnd_notify mechanism */
1597 check_routers_before_use = 1;
1598 if (dead_router_check_interval <= 0)
1599 dead_router_check_interval = 30;
1603 lnet_free_rtrpools (void)
1608 lnet_init_rtrpools (void)
1613 lnet_alloc_rtrpools (int im_a_arouter)