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 lnet_rc_data_t *rcd = event->md.user_ptr;
663 struct lnet_peer *lp;
665 LASSERT(rcd != NULL);
667 if (event->unlinked) {
668 LNetInvalidateHandle(&rcd->rcd_mdh);
672 LASSERT(event->type == LNET_EVENT_SEND ||
673 event->type == LNET_EVENT_REPLY);
675 lp = rcd->rcd_gateway;
679 if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
680 /* ignore if no longer a router or rcd is replaced */
684 if (event->type == LNET_EVENT_SEND) {
685 lp->lp_ping_notsent = 0;
686 if (event->status == 0)
690 /* LNET_EVENT_REPLY */
691 /* A successful REPLY means the router is up. If _any_ comms
692 * to the router fail I assume it's down (this will happen if
693 * we ping alive routers to try to detect router death before
694 * apps get burned). */
696 lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
697 /* The router checker will wake up very shortly and do the
698 * actual notification.
699 * XXX If 'lp' stops being a router before then, it will still
700 * have the notification pending!!! */
702 if (avoid_asym_router_failure && event->status == 0)
703 lnet_parse_rc_info(rcd);
710 lnet_wait_known_routerstate(void)
716 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
722 cfs_list_for_each (entry, &the_lnet.ln_routers) {
723 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
725 if (rtr->lp_alive_count == 0) {
737 lnet_router_checker();
739 cfs_pause(cfs_time_seconds(1));
744 lnet_update_ni_status_locked(void)
750 LASSERT(the_lnet.ln_routing);
752 timeout = router_ping_timeout +
753 MAX(live_router_check_interval, dead_router_check_interval);
755 now = cfs_time_current_sec();
756 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
757 if (ni->ni_lnd->lnd_type == LOLND)
760 if (now < ni->ni_last_alive + timeout)
763 LASSERT(ni->ni_status != NULL);
765 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
766 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
767 libcfs_nid2str(ni->ni_nid), timeout);
768 /* NB: so far, this is the only place to set
769 * NI status to "down" */
770 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
776 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
778 LASSERT(cfs_list_empty(&rcd->rcd_list));
779 /* detached from network */
780 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
782 if (rcd->rcd_gateway != NULL) {
784 lnet_peer_decref_locked(rcd->rcd_gateway);
788 if (rcd->rcd_pinginfo != NULL)
789 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
791 LIBCFS_FREE(rcd, sizeof(*rcd));
795 lnet_create_rc_data_locked(lnet_peer_t *gateway)
797 lnet_rc_data_t *rcd = NULL;
798 lnet_ping_info_t *pi;
804 LIBCFS_ALLOC(rcd, sizeof(*rcd));
808 LNetInvalidateHandle(&rcd->rcd_mdh);
809 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
811 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
815 memset(pi, 0, LNET_PINGINFO_SIZE);
816 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
817 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
818 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
820 rcd->rcd_pinginfo = pi;
822 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
823 rc = LNetMDBind((lnet_md_t){.start = pi,
825 .length = LNET_PINGINFO_SIZE,
826 .threshold = LNET_MD_THRESH_INF,
827 .options = LNET_MD_TRUNCATE,
828 .eq_handle = the_lnet.ln_rc_eqh},
832 CERROR("Can't bind MD: %d\n", rc);
838 /* router table changed or someone has created rcd for this gateway */
839 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
844 lnet_peer_addref_locked(gateway);
845 rcd->rcd_gateway = gateway;
846 gateway->lp_rcd = rcd;
847 gateway->lp_ping_notsent = 0;
853 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
854 rc = LNetMDUnlink(rcd->rcd_mdh);
857 lnet_destroy_rc_data(rcd);
861 return gateway->lp_rcd;
865 lnet_router_check_interval (lnet_peer_t *rtr)
869 secs = rtr->lp_alive ? live_router_check_interval :
870 dead_router_check_interval;
878 lnet_ping_router_locked (lnet_peer_t *rtr)
880 lnet_rc_data_t *rcd = NULL;
881 cfs_time_t now = cfs_time_current();
884 lnet_peer_addref_locked(rtr);
886 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
887 cfs_time_after(now, rtr->lp_ping_deadline))
888 lnet_notify_locked(rtr, 1, 0, now);
890 /* Run any outstanding notifications */
891 lnet_ni_notify_locked(rtr->lp_ni, rtr);
893 if (!lnet_isrouter(rtr) ||
894 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
895 /* router table changed or router checker is shutting down */
896 lnet_peer_decref_locked(rtr);
900 rcd = rtr->lp_rcd != NULL ?
901 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
906 secs = lnet_router_check_interval(rtr);
909 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
910 "alive_count %d lp_ping_timestamp %lu\n",
911 libcfs_nid2str(rtr->lp_nid), secs,
912 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
913 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
915 if (secs != 0 && !rtr->lp_ping_notsent &&
916 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
917 cfs_time_seconds(secs)))) {
919 lnet_process_id_t id;
920 lnet_handle_md_t mdh;
922 id.nid = rtr->lp_nid;
923 id.pid = LUSTRE_SRV_LNET_PID;
924 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
926 rtr->lp_ping_notsent = 1;
927 rtr->lp_ping_timestamp = now;
931 if (rtr->lp_ping_deadline == 0) {
932 rtr->lp_ping_deadline = \
933 cfs_time_shift(router_ping_timeout);
938 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
939 LNET_PROTO_PING_MATCHBITS, 0);
943 rtr->lp_ping_notsent = 0; /* no event pending */
946 lnet_peer_decref_locked(rtr);
951 lnet_router_checker_start(void)
959 int router_checker_max_eqsize = 10240;
961 LASSERT (check_routers_before_use);
962 LASSERT (dead_router_check_interval > 0);
966 /* As an approximation, allow each router the same number of
967 * outstanding events as it is allowed outstanding sends */
969 version = the_lnet.ln_routers_version;
970 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
971 lnet_ni_t *ni = rtr->lp_ni;
972 lnet_process_id_t id;
975 eqsz += ni->ni_peertxcredits;
977 /* one async ping reply per router */
978 id.nid = rtr->lp_nid;
979 id.pid = LUSTRE_SRV_LNET_PID;
983 rc = LNetSetAsync(id, 1);
985 CWARN("LNetSetAsync %s failed: %d\n",
986 libcfs_id2str(id), rc);
991 /* NB router list doesn't change in userspace */
992 LASSERT (version == the_lnet.ln_routers_version);
999 "No router found, not starting router checker\n");
1003 /* at least allow a SENT and a REPLY per router */
1004 if (router_checker_max_eqsize < 2 * nrtr)
1005 router_checker_max_eqsize = 2 * nrtr;
1008 if (eqsz > router_checker_max_eqsize)
1009 eqsz = router_checker_max_eqsize;
1012 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1014 if (check_routers_before_use &&
1015 dead_router_check_interval <= 0) {
1016 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1017 " set if 'check_routers_before_use' is set"
1022 if (!the_lnet.ln_routing &&
1023 live_router_check_interval <= 0 &&
1024 dead_router_check_interval <= 0)
1028 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1029 /* EQ size doesn't matter; the callback is guaranteed to get every
1032 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1033 &the_lnet.ln_rc_eqh);
1035 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1036 &the_lnet.ln_rc_eqh);
1039 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1043 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1045 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1047 CERROR("Can't start router checker thread: %d\n", rc);
1048 /* block until event callback signals exit */
1049 cfs_down(&the_lnet.ln_rc_signal);
1050 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1052 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1057 if (check_routers_before_use) {
1058 /* Note that a helpful side-effect of pinging all known routers
1059 * at startup is that it makes them drop stale connections they
1060 * may have to a previous instance of me. */
1061 lnet_wait_known_routerstate();
1068 lnet_router_checker_stop (void)
1072 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1075 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1076 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1079 /* block until event callback signals exit */
1080 cfs_down(&the_lnet.ln_rc_signal);
1082 lnet_router_checker();
1084 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1086 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1092 lnet_prune_rc_data(int wait_unlink)
1094 lnet_rc_data_t *rcd;
1095 lnet_rc_data_t *tmp;
1100 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1101 cfs_list_empty(&the_lnet.ln_rcd_deathrow) &&
1102 cfs_list_empty(&the_lnet.ln_rcd_zombie)))
1105 CFS_INIT_LIST_HEAD(&head);
1109 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1110 /* router checker is stopping, prune all */
1111 cfs_list_for_each_entry(lp, &the_lnet.ln_routers,
1113 if (lp->lp_rcd == NULL)
1116 LASSERT(cfs_list_empty(&lp->lp_rcd->rcd_list));
1117 cfs_list_add(&lp->lp_rcd->rcd_list,
1118 &the_lnet.ln_rcd_deathrow);
1123 /* unlink all RCDs on deathrow list */
1124 cfs_list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1126 if (!cfs_list_empty(&head)) {
1129 cfs_list_for_each_entry(rcd, &head, rcd_list)
1130 LNetMDUnlink(rcd->rcd_mdh);
1135 cfs_list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1137 /* release all zombie RCDs */
1138 while (!cfs_list_empty(&the_lnet.ln_rcd_zombie)) {
1139 cfs_list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1141 if (!LNetHandleIsInvalid(rcd->rcd_mdh))
1142 cfs_list_move(&rcd->rcd_list, &head);
1145 wait_unlink = wait_unlink &&
1146 !cfs_list_empty(&the_lnet.ln_rcd_zombie);
1150 while (!cfs_list_empty(&head)) {
1151 rcd = cfs_list_entry(head.next,
1152 lnet_rc_data_t, rcd_list);
1153 cfs_list_del_init(&rcd->rcd_list);
1154 lnet_destroy_rc_data(rcd);
1161 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1162 "Waiting for rc buffers to unlink\n");
1163 cfs_pause(cfs_time_seconds(1) / 4);
1170 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1173 lnet_router_checker(void *arg)
1178 cfs_daemonize("router_checker");
1179 cfs_block_allsigs();
1181 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1183 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1188 version = the_lnet.ln_routers_version;
1190 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1191 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1192 lnet_ping_router_locked(rtr);
1194 /* NB dropped lock */
1195 if (version != the_lnet.ln_routers_version) {
1196 /* the routers list has changed */
1201 if (the_lnet.ln_routing)
1202 lnet_update_ni_status_locked();
1206 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1208 /* Call cfs_pause() here always adds 1 to load average
1209 * because kernel counts # active tasks as nr_running
1210 * + nr_uninterruptible. */
1211 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1212 cfs_time_seconds(1));
1215 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1217 lnet_prune_rc_data(1); /* wait for UNLINK */
1219 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1220 cfs_up(&the_lnet.ln_rc_signal);
1221 /* The unlink event callback will signal final completion */
1226 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1228 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1230 while (--npages >= 0)
1231 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1233 LIBCFS_FREE(rb, sz);
1237 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1239 int npages = rbp->rbp_npages;
1240 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1245 LIBCFS_ALLOC(rb, sz);
1251 for (i = 0; i < npages; i++) {
1252 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1255 cfs_free_page(rb->rb_kiov[i].kiov_page);
1257 LIBCFS_FREE(rb, sz);
1261 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1262 rb->rb_kiov[i].kiov_offset = 0;
1263 rb->rb_kiov[i].kiov_page = page;
1270 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1272 int npages = rbp->rbp_npages;
1276 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1277 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1279 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1280 LASSERT (rbp->rbp_credits > 0);
1282 rb = cfs_list_entry(rbp->rbp_bufs.next,
1283 lnet_rtrbuf_t, rb_list);
1284 cfs_list_del(&rb->rb_list);
1285 lnet_destroy_rtrbuf(rb, npages);
1289 LASSERT (rbp->rbp_nbuffers == nbuffers);
1290 LASSERT (rbp->rbp_credits == nbuffers);
1292 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1296 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1301 if (rbp->rbp_nbuffers != 0) {
1302 LASSERT (rbp->rbp_nbuffers == nbufs);
1306 for (i = 0; i < nbufs; i++) {
1307 rb = lnet_new_rtrbuf(rbp);
1310 CERROR("Failed to allocate %d router bufs of %d pages\n",
1311 nbufs, rbp->rbp_npages);
1315 rbp->rbp_nbuffers++;
1317 rbp->rbp_mincredits++;
1318 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1320 /* No allocation "under fire" */
1321 /* Otherwise we'd need code to schedule blocked msgs etc */
1322 LASSERT (!the_lnet.ln_routing);
1325 LASSERT (rbp->rbp_credits == nbufs);
1330 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1332 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1333 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1335 rbp->rbp_npages = npages;
1336 rbp->rbp_credits = 0;
1337 rbp->rbp_mincredits = 0;
1341 lnet_free_rtrpools(void)
1343 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1344 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1345 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1349 lnet_init_rtrpools(void)
1351 int small_pages = 1;
1352 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1354 lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
1355 lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
1356 lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
1361 lnet_alloc_rtrpools(int im_a_router)
1365 if (!strcmp(forwarding, "")) {
1366 /* not set either way */
1369 } else if (!strcmp(forwarding, "disabled")) {
1370 /* explicitly disabled */
1372 } else if (!strcmp(forwarding, "enabled")) {
1373 /* explicitly enabled */
1375 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1376 "'enabled' or 'disabled'\n");
1380 if (tiny_router_buffers <= 0) {
1381 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1382 "routing enabled\n", tiny_router_buffers);
1387 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1388 tiny_router_buffers);
1392 if (small_router_buffers <= 0) {
1393 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1394 " routing enabled\n", small_router_buffers);
1399 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1400 small_router_buffers);
1404 if (large_router_buffers <= 0) {
1405 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1406 " routing enabled\n", large_router_buffers);
1411 rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1412 large_router_buffers);
1417 the_lnet.ln_routing = 1;
1423 lnet_free_rtrpools();
1428 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1430 lnet_peer_t *lp = NULL;
1431 cfs_time_t now = cfs_time_current();
1433 LASSERT (!cfs_in_interrupt ());
1435 CDEBUG (D_NET, "%s notifying %s: %s\n",
1436 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1437 libcfs_nid2str(nid),
1438 alive ? "up" : "down");
1441 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1442 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1443 libcfs_nid2str(nid), alive ? "birth" : "death",
1444 libcfs_nid2str(ni->ni_nid));
1448 /* can't do predictions... */
1449 if (cfs_time_after(when, now)) {
1450 CWARN ("Ignoring prediction from %s of %s %s "
1451 "%ld seconds in the future\n",
1452 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1453 libcfs_nid2str(nid), alive ? "up" : "down",
1454 cfs_duration_sec(cfs_time_sub(when, now)));
1458 if (ni != NULL && !alive && /* LND telling me she's down */
1459 !auto_down) { /* auto-down disabled */
1460 CDEBUG(D_NET, "Auto-down disabled\n");
1466 lp = lnet_find_peer_locked(nid);
1470 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1474 /* We can't fully trust LND on reporting exact peer last_alive
1475 * if he notifies us about dead peer. For example ksocklnd can
1476 * call us with when == _time_when_the_node_was_booted_ if
1477 * no connections were successfully established */
1478 if (ni != NULL && !alive && when < lp->lp_last_alive)
1479 when = lp->lp_last_alive;
1481 lnet_notify_locked(lp, ni == NULL, alive, when);
1483 lnet_ni_notify_locked(ni, lp);
1485 lnet_peer_decref_locked(lp);
1490 EXPORT_SYMBOL(lnet_notify);
1493 lnet_get_tunables (void)
1501 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1507 lnet_router_checker (void)
1509 static time_t last = 0;
1510 static int running = 0;
1512 time_t now = cfs_time_current_sec();
1513 int interval = now - last;
1518 /* It's no use to call me again within a sec - all intervals and
1519 * timeouts are measured in seconds */
1520 if (last != 0 && interval < 2)
1524 interval > MAX(live_router_check_interval,
1525 dead_router_check_interval))
1526 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1527 live_router_check_interval, dead_router_check_interval,
1531 LASSERT (!running); /* recursion check */
1537 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1538 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1540 /* consume all pending events */
1545 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1546 * recursion breaker in LNetEQPoll would fail */
1547 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1548 if (rc == 0) /* no event pending */
1551 /* NB a lost SENT prevents me from pinging a router again */
1552 if (rc == -EOVERFLOW) {
1553 CERROR("Dropped an event!!!\n");
1559 lnet_router_checker_event(&ev);
1562 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1563 lnet_prune_rc_data(1); /* release rcd */
1564 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1569 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1573 version = the_lnet.ln_routers_version;
1574 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1575 lnet_ping_router_locked(rtr);
1576 LASSERT (version == the_lnet.ln_routers_version);
1581 running = 0; /* lock only needed for the recursion check */
1585 /* NB lnet_peers_start_down depends on me,
1586 * so must be called before any peer creation */
1588 lnet_get_tunables (void)
1592 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1593 if (s != NULL) router_ping_timeout = atoi(s);
1595 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1596 if (s != NULL) live_router_check_interval = atoi(s);
1598 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1599 if (s != NULL) dead_router_check_interval = atoi(s);
1601 /* This replaces old lnd_notify mechanism */
1602 check_routers_before_use = 1;
1603 if (dead_router_check_interval <= 0)
1604 dead_router_check_interval = 30;
1608 lnet_free_rtrpools (void)
1613 lnet_init_rtrpools (void)
1618 lnet_alloc_rtrpools (int im_a_arouter)