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(void)
741 cfs_time_t now = cfs_time_current();
746 LASSERT (the_lnet.ln_routing);
748 timeout = router_ping_timeout +
749 MAX(live_router_check_interval, dead_router_check_interval);
753 cfs_list_for_each_entry (ni, &the_lnet.ln_nis, ni_list) {
754 lnet_ni_status_t *ns = ni->ni_status;
756 LASSERT (ns != NULL);
758 status = LNET_NI_STATUS_UP;
759 if (ni->ni_lnd->lnd_type != LOLND && /* @lo forever alive */
760 cfs_time_after(now, cfs_time_add(ni->ni_last_alive,
761 cfs_time_seconds(timeout))))
762 status = LNET_NI_STATUS_DOWN;
764 if (ns->ns_status != status) {
765 ns->ns_status = status;
766 CDEBUG(D_NET, "NI(%s:%d) status changed to %s\n",
767 libcfs_nid2str(ni->ni_nid), timeout,
768 status == LNET_NI_STATUS_UP ? "up" : "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;
851 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
852 rc = LNetMDUnlink(rcd->rcd_mdh);
855 lnet_destroy_rc_data(rcd);
859 return gateway->lp_rcd;
863 lnet_router_check_interval (lnet_peer_t *rtr)
867 secs = rtr->lp_alive ? live_router_check_interval :
868 dead_router_check_interval;
876 lnet_ping_router_locked (lnet_peer_t *rtr)
878 lnet_rc_data_t *rcd = NULL;
879 cfs_time_t now = cfs_time_current();
882 lnet_peer_addref_locked(rtr);
884 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
885 cfs_time_after(now, rtr->lp_ping_deadline))
886 lnet_notify_locked(rtr, 1, 0, now);
888 /* Run any outstanding notifications */
889 lnet_ni_notify_locked(rtr->lp_ni, rtr);
891 if (!lnet_isrouter(rtr) ||
892 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
893 /* router table changed or router checker is shutting down */
894 lnet_peer_decref_locked(rtr);
898 rcd = rtr->lp_rcd != NULL ?
899 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
904 secs = lnet_router_check_interval(rtr);
907 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
908 "alive_count %d lp_ping_timestamp %lu\n",
909 libcfs_nid2str(rtr->lp_nid), secs,
910 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
911 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
913 if (secs != 0 && !rtr->lp_ping_notsent &&
914 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
915 cfs_time_seconds(secs)))) {
917 lnet_process_id_t id;
918 lnet_handle_md_t mdh;
920 id.nid = rtr->lp_nid;
921 id.pid = LUSTRE_SRV_LNET_PID;
922 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
924 rtr->lp_ping_notsent = 1;
925 rtr->lp_ping_timestamp = now;
929 if (rtr->lp_ping_deadline == 0) {
930 rtr->lp_ping_deadline = \
931 cfs_time_shift(router_ping_timeout);
936 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
937 LNET_PROTO_PING_MATCHBITS, 0);
941 rtr->lp_ping_notsent = 0; /* no event pending */
944 lnet_peer_decref_locked(rtr);
949 lnet_router_checker_start(void)
957 int router_checker_max_eqsize = 10240;
959 LASSERT (check_routers_before_use);
960 LASSERT (dead_router_check_interval > 0);
964 /* As an approximation, allow each router the same number of
965 * outstanding events as it is allowed outstanding sends */
967 version = the_lnet.ln_routers_version;
968 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
969 lnet_ni_t *ni = rtr->lp_ni;
970 lnet_process_id_t id;
973 eqsz += ni->ni_peertxcredits;
975 /* one async ping reply per router */
976 id.nid = rtr->lp_nid;
977 id.pid = LUSTRE_SRV_LNET_PID;
981 rc = LNetSetAsync(id, 1);
983 CWARN("LNetSetAsync %s failed: %d\n",
984 libcfs_id2str(id), rc);
989 /* NB router list doesn't change in userspace */
990 LASSERT (version == the_lnet.ln_routers_version);
997 "No router found, not starting router checker\n");
1001 /* at least allow a SENT and a REPLY per router */
1002 if (router_checker_max_eqsize < 2 * nrtr)
1003 router_checker_max_eqsize = 2 * nrtr;
1006 if (eqsz > router_checker_max_eqsize)
1007 eqsz = router_checker_max_eqsize;
1010 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1012 if (check_routers_before_use &&
1013 dead_router_check_interval <= 0) {
1014 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1015 " set if 'check_routers_before_use' is set"
1020 if (!the_lnet.ln_routing &&
1021 live_router_check_interval <= 0 &&
1022 dead_router_check_interval <= 0)
1026 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1027 /* EQ size doesn't matter; the callback is guaranteed to get every
1030 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1031 &the_lnet.ln_rc_eqh);
1033 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1034 &the_lnet.ln_rc_eqh);
1037 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1041 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1043 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1045 CERROR("Can't start router checker thread: %d\n", rc);
1046 /* block until event callback signals exit */
1047 cfs_down(&the_lnet.ln_rc_signal);
1048 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1050 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1055 if (check_routers_before_use) {
1056 /* Note that a helpful side-effect of pinging all known routers
1057 * at startup is that it makes them drop stale connections they
1058 * may have to a previous instance of me. */
1059 lnet_wait_known_routerstate();
1066 lnet_router_checker_stop (void)
1070 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1073 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1074 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1077 /* block until event callback signals exit */
1078 cfs_down(&the_lnet.ln_rc_signal);
1080 lnet_router_checker();
1082 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1084 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1090 lnet_prune_rc_data(int wait_unlink)
1092 lnet_rc_data_t *rcd;
1093 lnet_rc_data_t *tmp;
1098 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1099 cfs_list_empty(&the_lnet.ln_rcd_deathrow) &&
1100 cfs_list_empty(&the_lnet.ln_rcd_zombie)))
1103 CFS_INIT_LIST_HEAD(&head);
1107 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1108 /* router checker is stopping, prune all */
1109 cfs_list_for_each_entry(lp, &the_lnet.ln_routers,
1111 if (lp->lp_rcd == NULL)
1114 LASSERT(cfs_list_empty(&lp->lp_rcd->rcd_list));
1115 cfs_list_add(&lp->lp_rcd->rcd_list,
1116 &the_lnet.ln_rcd_deathrow);
1121 /* unlink all RCDs on deathrow list */
1122 cfs_list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1124 if (!cfs_list_empty(&head)) {
1127 cfs_list_for_each_entry(rcd, &head, rcd_list)
1128 LNetMDUnlink(rcd->rcd_mdh);
1133 cfs_list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1135 /* release all zombie RCDs */
1136 while (!cfs_list_empty(&the_lnet.ln_rcd_zombie)) {
1137 cfs_list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1139 if (!LNetHandleIsInvalid(rcd->rcd_mdh))
1140 cfs_list_move(&rcd->rcd_list, &head);
1143 wait_unlink = wait_unlink &&
1144 !cfs_list_empty(&the_lnet.ln_rcd_zombie);
1148 while (!cfs_list_empty(&head)) {
1149 rcd = cfs_list_entry(head.next,
1150 lnet_rc_data_t, rcd_list);
1151 cfs_list_del_init(&rcd->rcd_list);
1152 lnet_destroy_rc_data(rcd);
1159 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1160 "Waiting for rc buffers to unlink\n");
1161 cfs_pause(cfs_time_seconds(1) / 4);
1168 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1171 lnet_router_checker(void *arg)
1176 cfs_daemonize("router_checker");
1177 cfs_block_allsigs();
1179 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1181 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1186 version = the_lnet.ln_routers_version;
1188 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1189 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1190 lnet_ping_router_locked(rtr);
1192 /* NB dropped lock */
1193 if (version != the_lnet.ln_routers_version) {
1194 /* the routers list has changed */
1201 if (the_lnet.ln_routing)
1202 lnet_update_ni_status();
1204 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1206 /* Call cfs_pause() here always adds 1 to load average
1207 * because kernel counts # active tasks as nr_running
1208 * + nr_uninterruptible. */
1209 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1210 cfs_time_seconds(1));
1213 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1215 lnet_prune_rc_data(1); /* wait for UNLINK */
1217 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1218 cfs_up(&the_lnet.ln_rc_signal);
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);
1481 lnet_ni_notify_locked(ni, lp);
1483 lnet_peer_decref_locked(lp);
1488 EXPORT_SYMBOL(lnet_notify);
1491 lnet_get_tunables (void)
1499 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1505 lnet_router_checker (void)
1507 static time_t last = 0;
1508 static int running = 0;
1510 time_t now = cfs_time_current_sec();
1511 int interval = now - last;
1516 /* It's no use to call me again within a sec - all intervals and
1517 * timeouts are measured in seconds */
1518 if (last != 0 && interval < 2)
1522 interval > MAX(live_router_check_interval,
1523 dead_router_check_interval))
1524 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1525 live_router_check_interval, dead_router_check_interval,
1529 LASSERT (!running); /* recursion check */
1535 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1536 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1538 /* consume all pending events */
1543 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1544 * recursion breaker in LNetEQPoll would fail */
1545 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1546 if (rc == 0) /* no event pending */
1549 /* NB a lost SENT prevents me from pinging a router again */
1550 if (rc == -EOVERFLOW) {
1551 CERROR("Dropped an event!!!\n");
1558 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)