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 #define LNET_NRB_TINY 1024
30 #define LNET_NRB_SMALL 8192
31 #define LNET_NRB_LARGE 512
33 static char *forwarding = "";
34 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
35 "Explicitly enable/disable forwarding between networks");
37 static int tiny_router_buffers;
38 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
39 "# of 0 payload messages to buffer in the router");
40 static int small_router_buffers;
41 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
42 "# of small (1 page) messages to buffer in the router");
43 static int large_router_buffers;
44 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
45 "# of large messages to buffer in the router");
46 static int peer_buffer_credits = 0;
47 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
48 "# router buffer credits per peer");
50 static int auto_down = 1;
51 CFS_MODULE_PARM(auto_down, "i", int, 0444,
52 "Automatically mark peers down on comms error");
55 lnet_peer_buffer_credits(lnet_ni_t *ni)
57 /* NI option overrides LNet default */
58 if (ni->ni_peerrtrcredits > 0)
59 return ni->ni_peerrtrcredits;
60 if (peer_buffer_credits > 0)
61 return peer_buffer_credits;
63 /* As an approximation, allow this peer the same number of router
64 * buffers as it is allowed outstanding sends */
65 return ni->ni_peertxcredits;
69 static int lnet_router_checker(void *);
73 lnet_peer_buffer_credits(lnet_ni_t *ni)
80 static int check_routers_before_use = 0;
81 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
82 "Assume routers are down and ping them before use");
84 static int avoid_asym_router_failure = 0;
85 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0444,
86 "Avoid asymmetrical failures: reserved, use at your own risk");
88 static int dead_router_check_interval = 0;
89 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0444,
90 "Seconds between dead router health checks (<= 0 to disable)");
92 static int live_router_check_interval = 0;
93 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0444,
94 "Seconds between live router health checks (<= 0 to disable)");
96 static int router_ping_timeout = 50;
97 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0444,
98 "Seconds to wait for the reply to a router health query");
101 lnet_peers_start_down(void)
103 return check_routers_before_use;
107 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
109 if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
110 CDEBUG(D_NET, "Out of date\n");
114 lp->lp_timestamp = when; /* update timestamp */
115 lp->lp_ping_deadline = 0; /* disable ping timeout */
117 if (lp->lp_alive_count != 0 && /* got old news */
118 (!lp->lp_alive) == (!alive)) { /* new date for old news */
119 CDEBUG(D_NET, "Old news\n");
123 /* Flag that notification is outstanding */
125 lp->lp_alive_count++;
126 lp->lp_alive = !(!alive); /* 1 bit! */
128 lp->lp_notifylnd |= notifylnd;
130 lp->lp_ping_version = LNET_PROTO_PING_UNKNOWN; /* reset */
132 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
136 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
141 /* Notify only in 1 thread at any time to ensure ordered notification.
142 * NB individual events can be missed; the only guarantee is that you
143 * always get the most recent news */
145 if (lp->lp_notifying)
148 lp->lp_notifying = 1;
150 while (lp->lp_notify) {
151 alive = lp->lp_alive;
152 notifylnd = lp->lp_notifylnd;
154 lp->lp_notifylnd = 0;
157 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
160 /* A new notification could happen now; I'll handle it
161 * when control returns to me */
163 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
169 lp->lp_notifying = 0;
174 lnet_rtr_addref_locked(lnet_peer_t *lp)
176 LASSERT (lp->lp_refcount > 0);
177 LASSERT (lp->lp_rtr_refcount >= 0);
179 lp->lp_rtr_refcount++;
180 if (lp->lp_rtr_refcount == 1) {
183 /* a simple insertion sort */
184 cfs_list_for_each_prev(pos, &the_lnet.ln_routers) {
185 lnet_peer_t *rtr = cfs_list_entry(pos, lnet_peer_t,
188 if (rtr->lp_nid < lp->lp_nid)
192 cfs_list_add(&lp->lp_rtr_list, pos);
193 /* addref for the_lnet.ln_routers */
194 lnet_peer_addref_locked(lp);
195 the_lnet.ln_routers_version++;
200 lnet_rtr_decref_locked(lnet_peer_t *lp)
202 LASSERT (lp->lp_refcount > 0);
203 LASSERT (lp->lp_rtr_refcount > 0);
205 lp->lp_rtr_refcount--;
206 if (lp->lp_rtr_refcount == 0) {
207 LASSERT(cfs_list_empty(&lp->lp_routes));
209 if (lp->lp_rcd != NULL) {
210 cfs_list_add(&lp->lp_rcd->rcd_list,
211 &the_lnet.ln_rcd_deathrow);
215 cfs_list_del(&lp->lp_rtr_list);
216 /* decref for the_lnet.ln_routers */
217 lnet_peer_decref_locked(lp);
218 the_lnet.ln_routers_version++;
223 lnet_find_net_locked (__u32 net)
225 lnet_remotenet_t *rnet;
228 LASSERT (!the_lnet.ln_shutdown);
230 cfs_list_for_each (tmp, &the_lnet.ln_remote_nets) {
231 rnet = cfs_list_entry(tmp, lnet_remotenet_t, lrn_list);
233 if (rnet->lrn_net == net)
239 static void lnet_shuffle_seed(void)
241 static int seeded = 0;
242 int lnd_type, seed[2];
250 cfs_get_random_bytes(seed, sizeof(seed));
252 /* Nodes with small feet have little entropy
253 * the NID for this node gives the most entropy in the low bits */
254 cfs_list_for_each(tmp, &the_lnet.ln_nis) {
255 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
256 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
258 if (lnd_type != LOLND)
259 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
262 cfs_gettimeofday(&tv);
263 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
268 /* NB expects LNET_LOCK held */
270 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
272 unsigned int len = 0;
273 unsigned int offset = 0;
278 cfs_list_for_each (e, &rnet->lrn_routes) {
282 /* len+1 positions to add a new entry, also prevents division by 0 */
283 offset = cfs_rand() % (len + 1);
284 cfs_list_for_each (e, &rnet->lrn_routes) {
289 cfs_list_add(&route->lr_list, e);
290 cfs_list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
292 the_lnet.ln_remote_nets_version++;
293 lnet_rtr_addref_locked(route->lr_gateway);
297 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
300 lnet_remotenet_t *rnet;
301 lnet_remotenet_t *rnet2;
307 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
308 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
310 if (gateway == LNET_NID_ANY ||
311 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
312 net == LNET_NIDNET(LNET_NID_ANY) ||
313 LNET_NETTYP(net) == LOLND ||
314 LNET_NIDNET(gateway) == net ||
315 hops < 1 || hops > 255)
318 if (lnet_islocalnet(net)) /* it's a local network */
319 return 0; /* ignore the route entry */
321 /* Assume net, route, all new */
322 LIBCFS_ALLOC(route, sizeof(*route));
323 LIBCFS_ALLOC(rnet, sizeof(*rnet));
324 if (route == NULL || rnet == NULL) {
325 CERROR("Out of memory creating route %s %d %s\n",
326 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
328 LIBCFS_FREE(route, sizeof(*route));
330 LIBCFS_FREE(rnet, sizeof(*rnet));
334 CFS_INIT_LIST_HEAD(&rnet->lrn_routes);
336 route->lr_hops = hops;
341 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
345 LIBCFS_FREE(route, sizeof(*route));
346 LIBCFS_FREE(rnet, sizeof(*rnet));
348 if (rc == -EHOSTUNREACH) { /* gateway is not on a local net */
349 return 0; /* ignore the route entry */
351 CERROR("Error %d creating route %s %d %s\n", rc,
352 libcfs_net2str(net), hops,
353 libcfs_nid2str(gateway));
358 LASSERT (!the_lnet.ln_shutdown);
360 rnet2 = lnet_find_net_locked(net);
363 cfs_list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
367 /* Search for a duplicate route (it's a NOOP if it is) */
369 cfs_list_for_each (e, &rnet2->lrn_routes) {
370 lnet_route_t *route2 = cfs_list_entry(e, lnet_route_t, lr_list);
372 if (route2->lr_gateway == route->lr_gateway) {
377 /* our lookups must be true */
378 LASSERT (route2->lr_gateway->lp_nid != gateway);
382 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
383 lnet_add_route_to_rnet(rnet2, route);
385 ni = route->lr_gateway->lp_ni;
388 /* XXX Assume alive */
389 if (ni->ni_lnd->lnd_notify != NULL)
390 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
395 /* -1 for notify or !add_route */
396 lnet_peer_decref_locked(route->lr_gateway);
400 LIBCFS_FREE(route, sizeof(*route));
403 LIBCFS_FREE(rnet, sizeof(*rnet));
409 lnet_check_routes (void)
411 lnet_remotenet_t *rnet;
413 lnet_route_t *route2;
419 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
420 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
423 cfs_list_for_each (e2, &rnet->lrn_routes) {
428 route = cfs_list_entry(e2, lnet_route_t, lr_list);
430 if (route2 == NULL) {
435 if (route->lr_gateway->lp_ni ==
436 route2->lr_gateway->lp_ni)
439 nid1 = route->lr_gateway->lp_nid;
440 nid2 = route2->lr_gateway->lp_nid;
445 CERROR("Routes to %s via %s and %s not supported\n",
446 libcfs_net2str(net), libcfs_nid2str(nid1),
447 libcfs_nid2str(nid2));
457 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
459 struct lnet_peer *gateway;
460 lnet_remotenet_t *rnet;
466 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
467 libcfs_net2str(net), libcfs_nid2str(gw_nid));
469 /* NB Caller may specify either all routes via the given gateway
470 * or a specific route entry actual NIDs) */
475 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
476 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
478 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
479 net == rnet->lrn_net))
482 cfs_list_for_each (e2, &rnet->lrn_routes) {
483 route = cfs_list_entry(e2, lnet_route_t, lr_list);
485 gateway = route->lr_gateway;
486 if (!(gw_nid == LNET_NID_ANY ||
487 gw_nid == gateway->lp_nid))
490 cfs_list_del(&route->lr_list);
491 cfs_list_del(&route->lr_gwlist);
492 the_lnet.ln_remote_nets_version++;
494 if (cfs_list_empty(&rnet->lrn_routes))
495 cfs_list_del(&rnet->lrn_list);
499 lnet_rtr_decref_locked(gateway);
500 lnet_peer_decref_locked(gateway);
503 LIBCFS_FREE(route, sizeof (*route));
506 LIBCFS_FREE(rnet, sizeof(*rnet));
518 lnet_destroy_routes (void)
520 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
524 lnet_get_route (int idx, __u32 *net, __u32 *hops,
525 lnet_nid_t *gateway, __u32 *alive)
529 lnet_remotenet_t *rnet;
534 cfs_list_for_each (e1, &the_lnet.ln_remote_nets) {
535 rnet = cfs_list_entry(e1, lnet_remotenet_t, lrn_list);
537 cfs_list_for_each (e2, &rnet->lrn_routes) {
538 route = cfs_list_entry(e2, lnet_route_t, lr_list);
541 *net = rnet->lrn_net;
542 *hops = route->lr_hops;
543 *gateway = route->lr_gateway->lp_nid;
544 *alive = route->lr_gateway->lp_alive;
556 lnet_swap_pinginfo(lnet_ping_info_t *info)
559 lnet_ni_status_t *stat;
561 __swab32s(&info->pi_magic);
562 __swab32s(&info->pi_version);
563 __swab32s(&info->pi_pid);
564 __swab32s(&info->pi_nnis);
565 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
566 stat = &info->pi_ni[i];
567 __swab64s(&stat->ns_nid);
568 __swab32s(&stat->ns_status);
574 * parse router-checker pinginfo, record number of down NIs for remote
575 * networks on that router.
578 lnet_parse_rc_info(lnet_rc_data_t *rcd)
580 lnet_ping_info_t *info = rcd->rcd_pinginfo;
581 struct lnet_peer *gw = rcd->rcd_gateway;
587 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
588 lnet_swap_pinginfo(info);
590 /* NB always racing with network! */
591 if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
592 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
593 libcfs_nid2str(gw->lp_nid), info->pi_magic);
594 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
598 gw->lp_ping_version = info->pi_version;
599 if (gw->lp_ping_version == LNET_PROTO_PING_VERSION_1)
600 return; /* v1 doesn't carry NI status info */
602 if (gw->lp_ping_version != LNET_PROTO_PING_VERSION) {
603 CDEBUG(D_NET, "%s: Unexpected version 0x%x\n",
604 libcfs_nid2str(gw->lp_nid), gw->lp_ping_version);
605 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
609 cfs_list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
610 int ptl_status = LNET_NI_STATUS_INVALID;
615 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
616 lnet_ni_status_t *stat = &info->pi_ni[i];
617 lnet_nid_t nid = stat->ns_nid;
619 if (nid == LNET_NID_ANY) {
620 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
621 libcfs_nid2str(gw->lp_nid));
622 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
626 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
629 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
630 if (LNET_NETTYP(LNET_NIDNET(nid)) != PTLLND)
632 else if (ptl_status != LNET_NI_STATUS_UP)
633 ptl_status = LNET_NI_STATUS_DOWN;
637 if (stat->ns_status == LNET_NI_STATUS_UP) {
638 if (LNET_NIDNET(nid) == rtr->lr_net) {
642 /* ptl NIs are considered down only when
643 * they're all down */
644 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
645 ptl_status = LNET_NI_STATUS_UP;
649 CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
650 libcfs_nid2str(gw->lp_nid), stat->ns_status);
651 gw->lp_ping_version = LNET_PROTO_PING_UNKNOWN;
655 if (up) { /* ignore downed NIs if NI for dest network is up */
659 rtr->lr_downis = down + (ptl_status == LNET_NI_STATUS_DOWN);
664 lnet_router_checker_event(lnet_event_t *event)
666 lnet_rc_data_t *rcd = event->md.user_ptr;
667 struct lnet_peer *lp;
669 LASSERT(rcd != NULL);
671 if (event->unlinked) {
672 LNetInvalidateHandle(&rcd->rcd_mdh);
676 LASSERT(event->type == LNET_EVENT_SEND ||
677 event->type == LNET_EVENT_REPLY);
679 lp = rcd->rcd_gateway;
683 if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
684 /* ignore if no longer a router or rcd is replaced */
688 if (event->type == LNET_EVENT_SEND) {
689 lp->lp_ping_notsent = 0;
690 if (event->status == 0)
694 /* LNET_EVENT_REPLY */
695 /* A successful REPLY means the router is up. If _any_ comms
696 * to the router fail I assume it's down (this will happen if
697 * we ping alive routers to try to detect router death before
698 * apps get burned). */
700 lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
701 /* The router checker will wake up very shortly and do the
702 * actual notification.
703 * XXX If 'lp' stops being a router before then, it will still
704 * have the notification pending!!! */
706 if (avoid_asym_router_failure && event->status == 0)
707 lnet_parse_rc_info(rcd);
714 lnet_wait_known_routerstate(void)
720 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
726 cfs_list_for_each (entry, &the_lnet.ln_routers) {
727 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
729 if (rtr->lp_alive_count == 0) {
741 lnet_router_checker();
743 cfs_pause(cfs_time_seconds(1));
748 lnet_update_ni_status_locked(void)
754 LASSERT(the_lnet.ln_routing);
756 timeout = router_ping_timeout +
757 MAX(live_router_check_interval, dead_router_check_interval);
759 now = cfs_time_current_sec();
760 cfs_list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
761 if (ni->ni_lnd->lnd_type == LOLND)
764 if (now < ni->ni_last_alive + timeout)
767 LASSERT(ni->ni_status != NULL);
769 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
770 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
771 libcfs_nid2str(ni->ni_nid), timeout);
772 /* NB: so far, this is the only place to set
773 * NI status to "down" */
774 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
780 lnet_destroy_rc_data (lnet_rc_data_t *rcd)
782 LASSERT(cfs_list_empty(&rcd->rcd_list));
783 /* detached from network */
784 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
786 if (rcd->rcd_gateway != NULL) {
788 lnet_peer_decref_locked(rcd->rcd_gateway);
792 if (rcd->rcd_pinginfo != NULL)
793 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
795 LIBCFS_FREE(rcd, sizeof(*rcd));
799 lnet_create_rc_data_locked(lnet_peer_t *gateway)
801 lnet_rc_data_t *rcd = NULL;
802 lnet_ping_info_t *pi;
808 LIBCFS_ALLOC(rcd, sizeof(*rcd));
812 LNetInvalidateHandle(&rcd->rcd_mdh);
813 CFS_INIT_LIST_HEAD(&rcd->rcd_list);
815 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
819 memset(pi, 0, LNET_PINGINFO_SIZE);
820 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
821 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
822 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
824 rcd->rcd_pinginfo = pi;
826 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
827 rc = LNetMDBind((lnet_md_t){.start = pi,
829 .length = LNET_PINGINFO_SIZE,
830 .threshold = LNET_MD_THRESH_INF,
831 .options = LNET_MD_TRUNCATE,
832 .eq_handle = the_lnet.ln_rc_eqh},
836 CERROR("Can't bind MD: %d\n", rc);
842 /* router table changed or someone has created rcd for this gateway */
843 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
848 lnet_peer_addref_locked(gateway);
849 rcd->rcd_gateway = gateway;
850 gateway->lp_rcd = rcd;
851 gateway->lp_ping_notsent = 0;
857 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
858 rc = LNetMDUnlink(rcd->rcd_mdh);
861 lnet_destroy_rc_data(rcd);
865 return gateway->lp_rcd;
869 lnet_router_check_interval (lnet_peer_t *rtr)
873 secs = rtr->lp_alive ? live_router_check_interval :
874 dead_router_check_interval;
882 lnet_ping_router_locked (lnet_peer_t *rtr)
884 lnet_rc_data_t *rcd = NULL;
885 cfs_time_t now = cfs_time_current();
888 lnet_peer_addref_locked(rtr);
890 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
891 cfs_time_after(now, rtr->lp_ping_deadline))
892 lnet_notify_locked(rtr, 1, 0, now);
894 /* Run any outstanding notifications */
895 lnet_ni_notify_locked(rtr->lp_ni, rtr);
897 if (!lnet_isrouter(rtr) ||
898 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
899 /* router table changed or router checker is shutting down */
900 lnet_peer_decref_locked(rtr);
904 rcd = rtr->lp_rcd != NULL ?
905 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
910 secs = lnet_router_check_interval(rtr);
913 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
914 "alive_count %d lp_ping_timestamp %lu\n",
915 libcfs_nid2str(rtr->lp_nid), secs,
916 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
917 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
919 if (secs != 0 && !rtr->lp_ping_notsent &&
920 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
921 cfs_time_seconds(secs)))) {
923 lnet_process_id_t id;
924 lnet_handle_md_t mdh;
926 id.nid = rtr->lp_nid;
927 id.pid = LUSTRE_SRV_LNET_PID;
928 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
930 rtr->lp_ping_notsent = 1;
931 rtr->lp_ping_timestamp = now;
935 if (rtr->lp_ping_deadline == 0) {
936 rtr->lp_ping_deadline = \
937 cfs_time_shift(router_ping_timeout);
942 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
943 LNET_PROTO_PING_MATCHBITS, 0);
947 rtr->lp_ping_notsent = 0; /* no event pending */
950 lnet_peer_decref_locked(rtr);
955 lnet_router_checker_start(void)
963 int router_checker_max_eqsize = 10240;
965 LASSERT (check_routers_before_use);
966 LASSERT (dead_router_check_interval > 0);
970 /* As an approximation, allow each router the same number of
971 * outstanding events as it is allowed outstanding sends */
973 version = the_lnet.ln_routers_version;
974 cfs_list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
975 lnet_ni_t *ni = rtr->lp_ni;
976 lnet_process_id_t id;
979 eqsz += ni->ni_peertxcredits;
981 /* one async ping reply per router */
982 id.nid = rtr->lp_nid;
983 id.pid = LUSTRE_SRV_LNET_PID;
987 rc = LNetSetAsync(id, 1);
989 CWARN("LNetSetAsync %s failed: %d\n",
990 libcfs_id2str(id), rc);
995 /* NB router list doesn't change in userspace */
996 LASSERT (version == the_lnet.ln_routers_version);
1003 "No router found, not starting router checker\n");
1007 /* at least allow a SENT and a REPLY per router */
1008 if (router_checker_max_eqsize < 2 * nrtr)
1009 router_checker_max_eqsize = 2 * nrtr;
1012 if (eqsz > router_checker_max_eqsize)
1013 eqsz = router_checker_max_eqsize;
1016 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1018 if (check_routers_before_use &&
1019 dead_router_check_interval <= 0) {
1020 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1021 " set if 'check_routers_before_use' is set"
1026 if (!the_lnet.ln_routing &&
1027 live_router_check_interval <= 0 &&
1028 dead_router_check_interval <= 0)
1032 cfs_sema_init(&the_lnet.ln_rc_signal, 0);
1033 /* EQ size doesn't matter; the callback is guaranteed to get every
1036 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1037 &the_lnet.ln_rc_eqh);
1039 rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1040 &the_lnet.ln_rc_eqh);
1043 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1047 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1049 rc = cfs_create_thread(lnet_router_checker, NULL, 0);
1051 CERROR("Can't start router checker thread: %d\n", rc);
1052 /* block until event callback signals exit */
1053 cfs_down(&the_lnet.ln_rc_signal);
1054 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1056 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1061 if (check_routers_before_use) {
1062 /* Note that a helpful side-effect of pinging all known routers
1063 * at startup is that it makes them drop stale connections they
1064 * may have to a previous instance of me. */
1065 lnet_wait_known_routerstate();
1072 lnet_router_checker_stop (void)
1076 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1079 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1080 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1083 /* block until event callback signals exit */
1084 cfs_down(&the_lnet.ln_rc_signal);
1086 lnet_router_checker();
1088 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1090 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1096 lnet_prune_rc_data(int wait_unlink)
1098 lnet_rc_data_t *rcd;
1099 lnet_rc_data_t *tmp;
1104 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1105 cfs_list_empty(&the_lnet.ln_rcd_deathrow) &&
1106 cfs_list_empty(&the_lnet.ln_rcd_zombie)))
1109 CFS_INIT_LIST_HEAD(&head);
1113 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1114 /* router checker is stopping, prune all */
1115 cfs_list_for_each_entry(lp, &the_lnet.ln_routers,
1117 if (lp->lp_rcd == NULL)
1120 LASSERT(cfs_list_empty(&lp->lp_rcd->rcd_list));
1121 cfs_list_add(&lp->lp_rcd->rcd_list,
1122 &the_lnet.ln_rcd_deathrow);
1127 /* unlink all RCDs on deathrow list */
1128 cfs_list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1130 if (!cfs_list_empty(&head)) {
1133 cfs_list_for_each_entry(rcd, &head, rcd_list)
1134 LNetMDUnlink(rcd->rcd_mdh);
1139 cfs_list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1141 /* release all zombie RCDs */
1142 while (!cfs_list_empty(&the_lnet.ln_rcd_zombie)) {
1143 cfs_list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1145 if (!LNetHandleIsInvalid(rcd->rcd_mdh))
1146 cfs_list_move(&rcd->rcd_list, &head);
1149 wait_unlink = wait_unlink &&
1150 !cfs_list_empty(&the_lnet.ln_rcd_zombie);
1154 while (!cfs_list_empty(&head)) {
1155 rcd = cfs_list_entry(head.next,
1156 lnet_rc_data_t, rcd_list);
1157 cfs_list_del_init(&rcd->rcd_list);
1158 lnet_destroy_rc_data(rcd);
1165 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1166 "Waiting for rc buffers to unlink\n");
1167 cfs_pause(cfs_time_seconds(1) / 4);
1174 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1177 lnet_router_checker(void *arg)
1182 cfs_daemonize("router_checker");
1183 cfs_block_allsigs();
1185 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1187 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1192 version = the_lnet.ln_routers_version;
1194 cfs_list_for_each (entry, &the_lnet.ln_routers) {
1195 rtr = cfs_list_entry(entry, lnet_peer_t, lp_rtr_list);
1196 lnet_ping_router_locked(rtr);
1198 /* NB dropped lock */
1199 if (version != the_lnet.ln_routers_version) {
1200 /* the routers list has changed */
1205 if (the_lnet.ln_routing)
1206 lnet_update_ni_status_locked();
1210 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1212 /* Call cfs_pause() here always adds 1 to load average
1213 * because kernel counts # active tasks as nr_running
1214 * + nr_uninterruptible. */
1215 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1216 cfs_time_seconds(1));
1219 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1221 lnet_prune_rc_data(1); /* wait for UNLINK */
1223 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1224 cfs_up(&the_lnet.ln_rc_signal);
1225 /* The unlink event callback will signal final completion */
1230 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1232 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1234 while (--npages >= 0)
1235 cfs_free_page(rb->rb_kiov[npages].kiov_page);
1237 LIBCFS_FREE(rb, sz);
1241 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
1243 int npages = rbp->rbp_npages;
1244 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1249 LIBCFS_ALLOC(rb, sz);
1255 for (i = 0; i < npages; i++) {
1256 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
1259 cfs_free_page(rb->rb_kiov[i].kiov_page);
1261 LIBCFS_FREE(rb, sz);
1265 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
1266 rb->rb_kiov[i].kiov_offset = 0;
1267 rb->rb_kiov[i].kiov_page = page;
1274 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1276 int npages = rbp->rbp_npages;
1280 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1283 LASSERT (cfs_list_empty(&rbp->rbp_msgs));
1284 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1286 while (!cfs_list_empty(&rbp->rbp_bufs)) {
1287 LASSERT (rbp->rbp_credits > 0);
1289 rb = cfs_list_entry(rbp->rbp_bufs.next,
1290 lnet_rtrbuf_t, rb_list);
1291 cfs_list_del(&rb->rb_list);
1292 lnet_destroy_rtrbuf(rb, npages);
1296 LASSERT (rbp->rbp_nbuffers == nbuffers);
1297 LASSERT (rbp->rbp_credits == nbuffers);
1299 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1303 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
1308 if (rbp->rbp_nbuffers != 0) {
1309 LASSERT (rbp->rbp_nbuffers == nbufs);
1313 for (i = 0; i < nbufs; i++) {
1314 rb = lnet_new_rtrbuf(rbp);
1317 CERROR("Failed to allocate %d router bufs of %d pages\n",
1318 nbufs, rbp->rbp_npages);
1322 rbp->rbp_nbuffers++;
1324 rbp->rbp_mincredits++;
1325 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1327 /* No allocation "under fire" */
1328 /* Otherwise we'd need code to schedule blocked msgs etc */
1329 LASSERT (!the_lnet.ln_routing);
1332 LASSERT (rbp->rbp_credits == nbufs);
1337 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1339 CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
1340 CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
1342 rbp->rbp_npages = npages;
1343 rbp->rbp_credits = 0;
1344 rbp->rbp_mincredits = 0;
1348 lnet_rtrpools_free(void)
1350 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1353 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
1354 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
1355 lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
1357 LIBCFS_FREE(the_lnet.ln_rtrpools,
1358 sizeof(lnet_rtrbufpool_t) * LNET_NRBPOOLS);
1359 the_lnet.ln_rtrpools = NULL;
1363 lnet_nrb_tiny_calculate(int npages)
1365 if (tiny_router_buffers > 0)
1366 return tiny_router_buffers;
1368 if (tiny_router_buffers == 0)
1369 return LNET_NRB_TINY;
1371 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
1372 "routing enabled\n", tiny_router_buffers);
1377 lnet_nrb_small_calculate(int npages)
1379 if (small_router_buffers > 0)
1380 return tiny_router_buffers;
1382 if (small_router_buffers == 0)
1383 return LNET_NRB_SMALL;
1385 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when "
1386 "routing enabled\n", small_router_buffers);
1391 lnet_nrb_large_calculate(int npages)
1393 if (large_router_buffers > 0)
1394 return large_router_buffers;
1396 if (large_router_buffers == 0)
1397 return LNET_NRB_LARGE;
1399 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1400 " routing enabled\n", large_router_buffers);
1405 lnet_rtrpools_alloc(int im_a_router)
1407 lnet_rtrbufpool_t *rtrp;
1408 int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1409 int small_pages = 1;
1415 if (!strcmp(forwarding, "")) {
1416 /* not set either way */
1419 } else if (!strcmp(forwarding, "disabled")) {
1420 /* explicitly disabled */
1422 } else if (!strcmp(forwarding, "enabled")) {
1423 /* explicitly enabled */
1425 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1426 "'enabled' or 'disabled'\n");
1430 nrb_tiny = lnet_nrb_tiny_calculate(0);
1434 nrb_small = lnet_nrb_small_calculate(small_pages);
1438 nrb_large = lnet_nrb_large_calculate(large_pages);
1442 LIBCFS_ALLOC(the_lnet.ln_rtrpools,
1443 sizeof(lnet_rtrbufpool_t) * LNET_NRBPOOLS);
1444 if (the_lnet.ln_rtrpools == NULL) {
1445 LCONSOLE_ERROR_MSG(0x10c,
1446 "Failed to initialize router buffe pool\n");
1450 do { /* iterate over rtrpools on all CPTs in upcoming patches */
1451 rtrp = the_lnet.ln_rtrpools;
1453 lnet_rtrpool_init(&rtrp[0], 0);
1454 rc = lnet_rtrpool_alloc_bufs(&rtrp[0], nrb_tiny);
1458 lnet_rtrpool_init(&rtrp[1], small_pages);
1459 rc = lnet_rtrpool_alloc_bufs(&rtrp[1], nrb_small);
1463 lnet_rtrpool_init(&rtrp[2], large_pages);
1464 rc = lnet_rtrpool_alloc_bufs(&rtrp[2], nrb_large);
1470 the_lnet.ln_routing = 1;
1476 lnet_rtrpools_free();
1481 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1483 lnet_peer_t *lp = NULL;
1484 cfs_time_t now = cfs_time_current();
1486 LASSERT (!cfs_in_interrupt ());
1488 CDEBUG (D_NET, "%s notifying %s: %s\n",
1489 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1490 libcfs_nid2str(nid),
1491 alive ? "up" : "down");
1494 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1495 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1496 libcfs_nid2str(nid), alive ? "birth" : "death",
1497 libcfs_nid2str(ni->ni_nid));
1501 /* can't do predictions... */
1502 if (cfs_time_after(when, now)) {
1503 CWARN ("Ignoring prediction from %s of %s %s "
1504 "%ld seconds in the future\n",
1505 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1506 libcfs_nid2str(nid), alive ? "up" : "down",
1507 cfs_duration_sec(cfs_time_sub(when, now)));
1511 if (ni != NULL && !alive && /* LND telling me she's down */
1512 !auto_down) { /* auto-down disabled */
1513 CDEBUG(D_NET, "Auto-down disabled\n");
1519 lp = lnet_find_peer_locked(nid);
1523 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1527 /* We can't fully trust LND on reporting exact peer last_alive
1528 * if he notifies us about dead peer. For example ksocklnd can
1529 * call us with when == _time_when_the_node_was_booted_ if
1530 * no connections were successfully established */
1531 if (ni != NULL && !alive && when < lp->lp_last_alive)
1532 when = lp->lp_last_alive;
1534 lnet_notify_locked(lp, ni == NULL, alive, when);
1536 lnet_ni_notify_locked(ni, lp);
1538 lnet_peer_decref_locked(lp);
1543 EXPORT_SYMBOL(lnet_notify);
1546 lnet_get_tunables (void)
1554 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1560 lnet_router_checker (void)
1562 static time_t last = 0;
1563 static int running = 0;
1565 time_t now = cfs_time_current_sec();
1566 int interval = now - last;
1571 /* It's no use to call me again within a sec - all intervals and
1572 * timeouts are measured in seconds */
1573 if (last != 0 && interval < 2)
1577 interval > MAX(live_router_check_interval,
1578 dead_router_check_interval))
1579 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1580 live_router_check_interval, dead_router_check_interval,
1584 LASSERT (!running); /* recursion check */
1590 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1591 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1593 /* consume all pending events */
1598 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1599 * recursion breaker in LNetEQPoll would fail */
1600 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1601 if (rc == 0) /* no event pending */
1604 /* NB a lost SENT prevents me from pinging a router again */
1605 if (rc == -EOVERFLOW) {
1606 CERROR("Dropped an event!!!\n");
1612 lnet_router_checker_event(&ev);
1615 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1616 lnet_prune_rc_data(1); /* release rcd */
1617 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1622 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1626 version = the_lnet.ln_routers_version;
1627 cfs_list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1628 lnet_ping_router_locked(rtr);
1629 LASSERT (version == the_lnet.ln_routers_version);
1634 running = 0; /* lock only needed for the recursion check */
1638 /* NB lnet_peers_start_down depends on me,
1639 * so must be called before any peer creation */
1641 lnet_get_tunables (void)
1645 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1646 if (s != NULL) router_ping_timeout = atoi(s);
1648 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1649 if (s != NULL) live_router_check_interval = atoi(s);
1651 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1652 if (s != NULL) dead_router_check_interval = atoi(s);
1654 /* This replaces old lnd_notify mechanism */
1655 check_routers_before_use = 1;
1656 if (dead_router_check_interval <= 0)
1657 dead_router_check_interval = 30;
1661 lnet_rtrpools_free(void)
1666 lnet_rtrpools_alloc(int im_a_arouter)