2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2012, Intel Corporation.
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 <linux/lnet/lib-lnet.h>
27 #if defined(LNET_ROUTER)
29 #define LNET_NRB_TINY_MIN 512 /* min value for each CPT */
30 #define LNET_NRB_TINY (LNET_NRB_TINY_MIN * 4)
31 #define LNET_NRB_SMALL_MIN 4096 /* min value for each CPT */
32 #define LNET_NRB_SMALL (LNET_NRB_SMALL_MIN * 4)
33 #define LNET_NRB_LARGE_MIN 256 /* min value for each CPT */
34 #define LNET_NRB_LARGE (LNET_NRB_LARGE_MIN * 4)
36 static char *forwarding = "";
37 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
38 "Explicitly enable/disable forwarding between networks");
40 static int tiny_router_buffers;
41 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
42 "# of 0 payload messages to buffer in the router");
43 static int small_router_buffers;
44 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
45 "# of small (1 page) messages to buffer in the router");
46 static int large_router_buffers;
47 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
48 "# of large messages to buffer in the router");
49 static int peer_buffer_credits = 0;
50 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
51 "# router buffer credits per peer");
53 static int auto_down = 1;
54 CFS_MODULE_PARM(auto_down, "i", int, 0444,
55 "Automatically mark peers down on comms error");
58 lnet_peer_buffer_credits(lnet_ni_t *ni)
60 /* NI option overrides LNet default */
61 if (ni->ni_peerrtrcredits > 0)
62 return ni->ni_peerrtrcredits;
63 if (peer_buffer_credits > 0)
64 return peer_buffer_credits;
66 /* As an approximation, allow this peer the same number of router
67 * buffers as it is allowed outstanding sends */
68 return ni->ni_peertxcredits;
72 static int lnet_router_checker(void *);
76 lnet_peer_buffer_credits(lnet_ni_t *ni)
83 static int check_routers_before_use = 0;
84 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
85 "Assume routers are down and ping them before use");
87 static int avoid_asym_router_failure = 1;
88 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0644,
89 "Avoid asymmetrical router failures (0 to disable)");
91 static int dead_router_check_interval = 60;
92 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0644,
93 "Seconds between dead router health checks (<= 0 to disable)");
95 static int live_router_check_interval = 60;
96 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0644,
97 "Seconds between live router health checks (<= 0 to disable)");
99 static int router_ping_timeout = 50;
100 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0644,
101 "Seconds to wait for the reply to a router health query");
104 lnet_peers_start_down(void)
106 return check_routers_before_use;
110 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
112 if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
113 CDEBUG(D_NET, "Out of date\n");
117 lp->lp_timestamp = when; /* update timestamp */
118 lp->lp_ping_deadline = 0; /* disable ping timeout */
120 if (lp->lp_alive_count != 0 && /* got old news */
121 (!lp->lp_alive) == (!alive)) { /* new date for old news */
122 CDEBUG(D_NET, "Old news\n");
126 /* Flag that notification is outstanding */
128 lp->lp_alive_count++;
129 lp->lp_alive = !(!alive); /* 1 bit! */
131 lp->lp_notifylnd |= notifylnd;
133 lp->lp_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
135 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
139 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
144 /* Notify only in 1 thread at any time to ensure ordered notification.
145 * NB individual events can be missed; the only guarantee is that you
146 * always get the most recent news */
148 if (lp->lp_notifying)
151 lp->lp_notifying = 1;
153 while (lp->lp_notify) {
154 alive = lp->lp_alive;
155 notifylnd = lp->lp_notifylnd;
157 lp->lp_notifylnd = 0;
160 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
161 lnet_net_unlock(lp->lp_cpt);
163 /* A new notification could happen now; I'll handle it
164 * when control returns to me */
166 (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
168 lnet_net_lock(lp->lp_cpt);
172 lp->lp_notifying = 0;
177 lnet_rtr_addref_locked(lnet_peer_t *lp)
179 LASSERT(lp->lp_refcount > 0);
180 LASSERT(lp->lp_rtr_refcount >= 0);
182 /* lnet_net_lock must be exclusively locked */
183 lp->lp_rtr_refcount++;
184 if (lp->lp_rtr_refcount == 1) {
185 struct list_head *pos;
187 /* a simple insertion sort */
188 list_for_each_prev(pos, &the_lnet.ln_routers) {
189 lnet_peer_t *rtr = list_entry(pos, lnet_peer_t,
192 if (rtr->lp_nid < lp->lp_nid)
196 list_add(&lp->lp_rtr_list, pos);
197 /* addref for the_lnet.ln_routers */
198 lnet_peer_addref_locked(lp);
199 the_lnet.ln_routers_version++;
204 lnet_rtr_decref_locked(lnet_peer_t *lp)
206 LASSERT(lp->lp_refcount > 0);
207 LASSERT(lp->lp_rtr_refcount > 0);
209 /* lnet_net_lock must be exclusively locked */
210 lp->lp_rtr_refcount--;
211 if (lp->lp_rtr_refcount == 0) {
212 LASSERT(list_empty(&lp->lp_routes));
214 if (lp->lp_rcd != NULL) {
215 list_add(&lp->lp_rcd->rcd_list,
216 &the_lnet.ln_rcd_deathrow);
220 list_del(&lp->lp_rtr_list);
221 /* decref for the_lnet.ln_routers */
222 lnet_peer_decref_locked(lp);
223 the_lnet.ln_routers_version++;
228 lnet_find_net_locked (__u32 net)
230 lnet_remotenet_t *rnet;
231 struct list_head *tmp;
232 struct list_head *rn_list;
234 LASSERT(!the_lnet.ln_shutdown);
236 rn_list = lnet_net2rnethash(net);
237 list_for_each(tmp, rn_list) {
238 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
240 if (rnet->lrn_net == net)
246 static void lnet_shuffle_seed(void)
248 static int seeded = 0;
249 int lnd_type, seed[2];
252 struct list_head *tmp;
257 cfs_get_random_bytes(seed, sizeof(seed));
259 /* Nodes with small feet have little entropy
260 * the NID for this node gives the most entropy in the low bits */
261 list_for_each(tmp, &the_lnet.ln_nis) {
262 ni = list_entry(tmp, lnet_ni_t, ni_list);
263 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
265 if (lnd_type != LOLND)
266 seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
269 do_gettimeofday(&tv);
270 cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
275 /* NB expects LNET_LOCK held */
277 lnet_add_route_to_rnet (lnet_remotenet_t *rnet, lnet_route_t *route)
279 unsigned int len = 0;
280 unsigned int offset = 0;
285 list_for_each (e, &rnet->lrn_routes) {
289 /* len+1 positions to add a new entry, also prevents division by 0 */
290 offset = cfs_rand() % (len + 1);
291 list_for_each (e, &rnet->lrn_routes) {
296 list_add(&route->lr_list, e);
297 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
299 the_lnet.ln_remote_nets_version++;
300 lnet_rtr_addref_locked(route->lr_gateway);
304 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
307 lnet_remotenet_t *rnet;
308 lnet_remotenet_t *rnet2;
314 CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
315 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
317 if (gateway == LNET_NID_ANY ||
318 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
319 net == LNET_NIDNET(LNET_NID_ANY) ||
320 LNET_NETTYP(net) == LOLND ||
321 LNET_NIDNET(gateway) == net ||
322 hops < 1 || hops > 255)
325 if (lnet_islocalnet(net)) /* it's a local network */
326 return 0; /* ignore the route entry */
328 /* Assume net, route, all new */
329 LIBCFS_ALLOC(route, sizeof(*route));
330 LIBCFS_ALLOC(rnet, sizeof(*rnet));
331 if (route == NULL || rnet == NULL) {
332 CERROR("Out of memory creating route %s %d %s\n",
333 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
335 LIBCFS_FREE(route, sizeof(*route));
337 LIBCFS_FREE(rnet, sizeof(*rnet));
341 INIT_LIST_HEAD(&rnet->lrn_routes);
343 route->lr_hops = hops;
346 lnet_net_lock(LNET_LOCK_EX);
348 rc = lnet_nid2peer_locked(&route->lr_gateway, gateway, LNET_LOCK_EX);
350 lnet_net_unlock(LNET_LOCK_EX);
352 LIBCFS_FREE(route, sizeof(*route));
353 LIBCFS_FREE(rnet, sizeof(*rnet));
355 if (rc == -EHOSTUNREACH) { /* gateway is not on a local net */
356 return 0; /* ignore the route entry */
358 CERROR("Error %d creating route %s %d %s\n", rc,
359 libcfs_net2str(net), hops,
360 libcfs_nid2str(gateway));
365 LASSERT (!the_lnet.ln_shutdown);
367 rnet2 = lnet_find_net_locked(net);
370 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
374 /* Search for a duplicate route (it's a NOOP if it is) */
376 list_for_each (e, &rnet2->lrn_routes) {
377 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
379 if (route2->lr_gateway == route->lr_gateway) {
384 /* our lookups must be true */
385 LASSERT (route2->lr_gateway->lp_nid != gateway);
389 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
390 lnet_add_route_to_rnet(rnet2, route);
392 ni = route->lr_gateway->lp_ni;
393 lnet_net_unlock(LNET_LOCK_EX);
395 /* XXX Assume alive */
396 if (ni->ni_lnd->lnd_notify != NULL)
397 (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
399 lnet_net_lock(LNET_LOCK_EX);
402 /* -1 for notify or !add_route */
403 lnet_peer_decref_locked(route->lr_gateway);
404 lnet_net_unlock(LNET_LOCK_EX);
407 LIBCFS_FREE(route, sizeof(*route));
410 LIBCFS_FREE(rnet, sizeof(*rnet));
416 lnet_check_routes(void)
418 lnet_remotenet_t *rnet;
420 lnet_route_t *route2;
421 struct list_head *e1;
422 struct list_head *e2;
424 struct list_head *rn_list;
427 cpt = lnet_net_lock_current();
429 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
430 rn_list = &the_lnet.ln_remote_nets_hash[i];
431 list_for_each(e1, rn_list) {
432 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
435 list_for_each(e2, &rnet->lrn_routes) {
440 route = list_entry(e2, lnet_route_t,
443 if (route2 == NULL) {
448 if (route->lr_gateway->lp_ni ==
449 route2->lr_gateway->lp_ni)
452 nid1 = route->lr_gateway->lp_nid;
453 nid2 = route2->lr_gateway->lp_nid;
456 lnet_net_unlock(cpt);
458 CERROR("Routes to %s via %s and %s not "
461 libcfs_nid2str(nid1),
462 libcfs_nid2str(nid2));
468 lnet_net_unlock(cpt);
473 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
475 struct lnet_peer *gateway;
476 lnet_remotenet_t *rnet;
478 struct list_head *e1;
479 struct list_head *e2;
481 struct list_head *rn_list;
484 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
485 libcfs_net2str(net), libcfs_nid2str(gw_nid));
487 /* NB Caller may specify either all routes via the given gateway
488 * or a specific route entry actual NIDs) */
490 lnet_net_lock(LNET_LOCK_EX);
491 if (net == LNET_NIDNET(LNET_NID_ANY))
492 rn_list = &the_lnet.ln_remote_nets_hash[0];
494 rn_list = lnet_net2rnethash(net);
497 list_for_each(e1, rn_list) {
498 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
500 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
501 net == rnet->lrn_net))
504 list_for_each(e2, &rnet->lrn_routes) {
505 route = list_entry(e2, lnet_route_t, lr_list);
507 gateway = route->lr_gateway;
508 if (!(gw_nid == LNET_NID_ANY ||
509 gw_nid == gateway->lp_nid))
512 list_del(&route->lr_list);
513 list_del(&route->lr_gwlist);
514 the_lnet.ln_remote_nets_version++;
516 if (list_empty(&rnet->lrn_routes))
517 list_del(&rnet->lrn_list);
521 lnet_rtr_decref_locked(gateway);
522 lnet_peer_decref_locked(gateway);
524 lnet_net_unlock(LNET_LOCK_EX);
526 LIBCFS_FREE(route, sizeof(*route));
529 LIBCFS_FREE(rnet, sizeof(*rnet));
532 lnet_net_lock(LNET_LOCK_EX);
537 if (net == LNET_NIDNET(LNET_NID_ANY) &&
538 ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
539 rn_list = &the_lnet.ln_remote_nets_hash[idx];
542 lnet_net_unlock(LNET_LOCK_EX);
548 lnet_destroy_routes (void)
550 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
554 lnet_get_route(int idx, __u32 *net, __u32 *hops,
555 lnet_nid_t *gateway, __u32 *alive)
557 struct list_head *e1;
558 struct list_head *e2;
559 lnet_remotenet_t *rnet;
563 struct list_head *rn_list;
565 cpt = lnet_net_lock_current();
567 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
568 rn_list = &the_lnet.ln_remote_nets_hash[i];
569 list_for_each(e1, rn_list) {
570 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
572 list_for_each(e2, &rnet->lrn_routes) {
573 route = list_entry(e2, lnet_route_t,
577 *net = rnet->lrn_net;
578 *hops = route->lr_hops;
579 *gateway = route->lr_gateway->lp_nid;
580 *alive = route->lr_gateway->lp_alive;
581 lnet_net_unlock(cpt);
588 lnet_net_unlock(cpt);
593 lnet_swap_pinginfo(lnet_ping_info_t *info)
596 lnet_ni_status_t *stat;
598 __swab32s(&info->pi_magic);
599 __swab32s(&info->pi_features);
600 __swab32s(&info->pi_pid);
601 __swab32s(&info->pi_nnis);
602 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
603 stat = &info->pi_ni[i];
604 __swab64s(&stat->ns_nid);
605 __swab32s(&stat->ns_status);
611 * parse router-checker pinginfo, record number of down NIs for remote
612 * networks on that router.
615 lnet_parse_rc_info(lnet_rc_data_t *rcd)
617 lnet_ping_info_t *info = rcd->rcd_pinginfo;
618 struct lnet_peer *gw = rcd->rcd_gateway;
624 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
625 lnet_swap_pinginfo(info);
627 /* NB always racing with network! */
628 if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
629 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
630 libcfs_nid2str(gw->lp_nid), info->pi_magic);
631 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
635 gw->lp_ping_feats = info->pi_features;
636 if ((gw->lp_ping_feats & LNET_PING_FEAT_MASK) == 0) {
637 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
638 libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
639 return; /* nothing I can understand */
642 if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) == 0)
643 return; /* can't carry NI status info */
645 list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
646 int ptl_status = LNET_NI_STATUS_INVALID;
651 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
652 lnet_ni_status_t *stat = &info->pi_ni[i];
653 lnet_nid_t nid = stat->ns_nid;
655 if (nid == LNET_NID_ANY) {
656 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
657 libcfs_nid2str(gw->lp_nid));
658 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
662 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
665 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
666 if (LNET_NETTYP(LNET_NIDNET(nid)) != PTLLND)
668 else if (ptl_status != LNET_NI_STATUS_UP)
669 ptl_status = LNET_NI_STATUS_DOWN;
673 if (stat->ns_status == LNET_NI_STATUS_UP) {
674 if (LNET_NIDNET(nid) == rtr->lr_net) {
678 /* ptl NIs are considered down only when
679 * they're all down */
680 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
681 ptl_status = LNET_NI_STATUS_UP;
685 CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
686 libcfs_nid2str(gw->lp_nid), stat->ns_status);
687 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
691 if (up) { /* ignore downed NIs if NI for dest network is up */
695 rtr->lr_downis = down + (ptl_status == LNET_NI_STATUS_DOWN);
700 lnet_router_checker_event(lnet_event_t *event)
702 lnet_rc_data_t *rcd = event->md.user_ptr;
703 struct lnet_peer *lp;
705 LASSERT(rcd != NULL);
707 if (event->unlinked) {
708 LNetInvalidateHandle(&rcd->rcd_mdh);
712 LASSERT(event->type == LNET_EVENT_SEND ||
713 event->type == LNET_EVENT_REPLY);
715 lp = rcd->rcd_gateway;
718 /* NB: it's called with holding lnet_res_lock, we have a few
719 * places need to hold both locks at the same time, please take
720 * care of lock ordering */
721 lnet_net_lock(lp->lp_cpt);
722 if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
723 /* ignore if no longer a router or rcd is replaced */
727 if (event->type == LNET_EVENT_SEND) {
728 lp->lp_ping_notsent = 0;
729 if (event->status == 0)
733 /* LNET_EVENT_REPLY */
734 /* A successful REPLY means the router is up. If _any_ comms
735 * to the router fail I assume it's down (this will happen if
736 * we ping alive routers to try to detect router death before
737 * apps get burned). */
739 lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
740 /* The router checker will wake up very shortly and do the
741 * actual notification.
742 * XXX If 'lp' stops being a router before then, it will still
743 * have the notification pending!!! */
745 if (avoid_asym_router_failure && event->status == 0)
746 lnet_parse_rc_info(rcd);
749 lnet_net_unlock(lp->lp_cpt);
753 lnet_wait_known_routerstate(void)
756 struct list_head *entry;
759 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
762 int cpt = lnet_net_lock_current();
765 list_for_each (entry, &the_lnet.ln_routers) {
766 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
768 if (rtr->lp_alive_count == 0) {
774 lnet_net_unlock(cpt);
779 cfs_pause(cfs_time_seconds(1));
784 lnet_update_ni_status_locked(void)
790 LASSERT(the_lnet.ln_routing);
792 timeout = router_ping_timeout +
793 MAX(live_router_check_interval, dead_router_check_interval);
795 now = cfs_time_current_sec();
796 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
797 if (ni->ni_lnd->lnd_type == LOLND)
800 if (now < ni->ni_last_alive + timeout)
804 /* re-check with lock */
805 if (now < ni->ni_last_alive + timeout) {
810 LASSERT(ni->ni_status != NULL);
812 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
813 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
814 libcfs_nid2str(ni->ni_nid), timeout);
815 /* NB: so far, this is the only place to set
816 * NI status to "down" */
817 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
824 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
826 LASSERT(list_empty(&rcd->rcd_list));
827 /* detached from network */
828 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
830 if (rcd->rcd_gateway != NULL) {
831 int cpt = rcd->rcd_gateway->lp_cpt;
834 lnet_peer_decref_locked(rcd->rcd_gateway);
835 lnet_net_unlock(cpt);
838 if (rcd->rcd_pinginfo != NULL)
839 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
841 LIBCFS_FREE(rcd, sizeof(*rcd));
845 lnet_create_rc_data_locked(lnet_peer_t *gateway)
847 lnet_rc_data_t *rcd = NULL;
848 lnet_ping_info_t *pi;
852 lnet_net_unlock(gateway->lp_cpt);
854 LIBCFS_ALLOC(rcd, sizeof(*rcd));
858 LNetInvalidateHandle(&rcd->rcd_mdh);
859 INIT_LIST_HEAD(&rcd->rcd_list);
861 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
865 memset(pi, 0, LNET_PINGINFO_SIZE);
866 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
867 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
868 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
870 rcd->rcd_pinginfo = pi;
872 LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
873 rc = LNetMDBind((lnet_md_t){.start = pi,
875 .length = LNET_PINGINFO_SIZE,
876 .threshold = LNET_MD_THRESH_INF,
877 .options = LNET_MD_TRUNCATE,
878 .eq_handle = the_lnet.ln_rc_eqh},
882 CERROR("Can't bind MD: %d\n", rc);
887 lnet_net_lock(gateway->lp_cpt);
888 /* router table changed or someone has created rcd for this gateway */
889 if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
890 lnet_net_unlock(gateway->lp_cpt);
894 lnet_peer_addref_locked(gateway);
895 rcd->rcd_gateway = gateway;
896 gateway->lp_rcd = rcd;
897 gateway->lp_ping_notsent = 0;
903 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
904 rc = LNetMDUnlink(rcd->rcd_mdh);
907 lnet_destroy_rc_data(rcd);
910 lnet_net_lock(gateway->lp_cpt);
911 return gateway->lp_rcd;
915 lnet_router_check_interval (lnet_peer_t *rtr)
919 secs = rtr->lp_alive ? live_router_check_interval :
920 dead_router_check_interval;
928 lnet_ping_router_locked (lnet_peer_t *rtr)
930 lnet_rc_data_t *rcd = NULL;
931 cfs_time_t now = cfs_time_current();
934 lnet_peer_addref_locked(rtr);
936 if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
937 cfs_time_after(now, rtr->lp_ping_deadline))
938 lnet_notify_locked(rtr, 1, 0, now);
940 /* Run any outstanding notifications */
941 lnet_ni_notify_locked(rtr->lp_ni, rtr);
943 if (!lnet_isrouter(rtr) ||
944 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
945 /* router table changed or router checker is shutting down */
946 lnet_peer_decref_locked(rtr);
950 rcd = rtr->lp_rcd != NULL ?
951 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
956 secs = lnet_router_check_interval(rtr);
959 "rtr %s %d: deadline %lu ping_notsent %d alive %d "
960 "alive_count %d lp_ping_timestamp %lu\n",
961 libcfs_nid2str(rtr->lp_nid), secs,
962 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
963 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
965 if (secs != 0 && !rtr->lp_ping_notsent &&
966 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
967 cfs_time_seconds(secs)))) {
969 lnet_process_id_t id;
970 lnet_handle_md_t mdh;
972 id.nid = rtr->lp_nid;
973 id.pid = LUSTRE_SRV_LNET_PID;
974 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
976 rtr->lp_ping_notsent = 1;
977 rtr->lp_ping_timestamp = now;
981 if (rtr->lp_ping_deadline == 0) {
982 rtr->lp_ping_deadline =
983 cfs_time_shift(router_ping_timeout);
986 lnet_net_unlock(rtr->lp_cpt);
988 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
989 LNET_PROTO_PING_MATCHBITS, 0);
991 lnet_net_lock(rtr->lp_cpt);
993 rtr->lp_ping_notsent = 0; /* no event pending */
996 lnet_peer_decref_locked(rtr);
1001 lnet_router_checker_start(void)
1006 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1008 if (check_routers_before_use &&
1009 dead_router_check_interval <= 0) {
1010 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1011 " set if 'check_routers_before_use' is set"
1016 if (!the_lnet.ln_routing &&
1017 live_router_check_interval <= 0 &&
1018 dead_router_check_interval <= 0)
1021 sema_init(&the_lnet.ln_rc_signal, 0);
1022 /* EQ size doesn't matter; the callback is guaranteed to get every
1025 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1026 &the_lnet.ln_rc_eqh);
1028 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1032 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1033 rc = PTR_ERR(kthread_run(lnet_router_checker,
1034 NULL, "router_checker"));
1035 if (IS_ERR_VALUE(rc)) {
1036 CERROR("Can't start router checker thread: %d\n", rc);
1037 /* block until event callback signals exit */
1038 down(&the_lnet.ln_rc_signal);
1039 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1041 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1045 if (check_routers_before_use) {
1046 /* Note that a helpful side-effect of pinging all known routers
1047 * at startup is that it makes them drop stale connections they
1048 * may have to a previous instance of me. */
1049 lnet_wait_known_routerstate();
1056 lnet_router_checker_stop (void)
1060 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1063 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1064 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1066 /* block until event callback signals exit */
1067 down(&the_lnet.ln_rc_signal);
1068 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1070 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1076 lnet_prune_rc_data(int wait_unlink)
1078 lnet_rc_data_t *rcd;
1079 lnet_rc_data_t *tmp;
1081 struct list_head head;
1084 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1085 list_empty(&the_lnet.ln_rcd_deathrow) &&
1086 list_empty(&the_lnet.ln_rcd_zombie)))
1089 INIT_LIST_HEAD(&head);
1091 lnet_net_lock(LNET_LOCK_EX);
1093 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1094 /* router checker is stopping, prune all */
1095 list_for_each_entry(lp, &the_lnet.ln_routers,
1097 if (lp->lp_rcd == NULL)
1100 LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1101 list_add(&lp->lp_rcd->rcd_list,
1102 &the_lnet.ln_rcd_deathrow);
1107 /* unlink all RCDs on deathrow list */
1108 list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1110 if (!list_empty(&head)) {
1111 lnet_net_unlock(LNET_LOCK_EX);
1113 list_for_each_entry(rcd, &head, rcd_list)
1114 LNetMDUnlink(rcd->rcd_mdh);
1116 lnet_net_lock(LNET_LOCK_EX);
1119 list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1121 /* release all zombie RCDs */
1122 while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1123 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1125 if (LNetHandleIsInvalid(rcd->rcd_mdh))
1126 list_move(&rcd->rcd_list, &head);
1129 wait_unlink = wait_unlink &&
1130 !list_empty(&the_lnet.ln_rcd_zombie);
1132 lnet_net_unlock(LNET_LOCK_EX);
1134 while (!list_empty(&head)) {
1135 rcd = list_entry(head.next,
1136 lnet_rc_data_t, rcd_list);
1137 list_del_init(&rcd->rcd_list);
1138 lnet_destroy_rc_data(rcd);
1145 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1146 "Waiting for rc buffers to unlink\n");
1147 cfs_pause(cfs_time_seconds(1) / 4);
1149 lnet_net_lock(LNET_LOCK_EX);
1152 lnet_net_unlock(LNET_LOCK_EX);
1156 #if defined(LNET_ROUTER)
1159 lnet_router_checker(void *arg)
1162 struct list_head *entry;
1164 cfs_block_allsigs();
1166 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1168 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1173 cpt = lnet_net_lock_current();
1175 version = the_lnet.ln_routers_version;
1177 list_for_each(entry, &the_lnet.ln_routers) {
1178 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1180 cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1182 lnet_net_unlock(cpt);
1185 /* the routers list has changed */
1186 if (version != the_lnet.ln_routers_version)
1190 lnet_ping_router_locked(rtr);
1192 /* NB dropped lock */
1193 if (version != the_lnet.ln_routers_version) {
1194 /* the routers list has changed */
1199 if (the_lnet.ln_routing)
1200 lnet_update_ni_status_locked();
1202 lnet_net_unlock(cpt);
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 schedule_timeout_and_set_state(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 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 __free_page(rb->rb_kiov[npages].kiov_page);
1231 LIBCFS_FREE(rb, sz);
1235 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1237 int npages = rbp->rbp_npages;
1238 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1243 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1249 for (i = 0; i < npages; i++) {
1250 page = alloc_pages_node(
1251 cfs_cpt_spread_node(lnet_cpt_table(), cpt),
1252 __GFP_ZERO | GFP_IOFS, 0);
1255 __free_page(rb->rb_kiov[i].kiov_page);
1257 LIBCFS_FREE(rb, sz);
1261 rb->rb_kiov[i].kiov_len = PAGE_CACHE_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 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1279 LASSERT (list_empty(&rbp->rbp_msgs));
1280 LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
1282 while (!list_empty(&rbp->rbp_bufs)) {
1283 LASSERT (rbp->rbp_credits > 0);
1285 rb = list_entry(rbp->rbp_bufs.next,
1286 lnet_rtrbuf_t, rb_list);
1287 list_del(&rb->rb_list);
1288 lnet_destroy_rtrbuf(rb, npages);
1292 LASSERT (rbp->rbp_nbuffers == nbuffers);
1293 LASSERT (rbp->rbp_credits == nbuffers);
1295 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1299 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
1304 if (rbp->rbp_nbuffers != 0) {
1305 LASSERT (rbp->rbp_nbuffers == nbufs);
1309 for (i = 0; i < nbufs; i++) {
1310 rb = lnet_new_rtrbuf(rbp, cpt);
1313 CERROR("Failed to allocate %d router bufs of %d pages\n",
1314 nbufs, rbp->rbp_npages);
1318 rbp->rbp_nbuffers++;
1320 rbp->rbp_mincredits++;
1321 list_add(&rb->rb_list, &rbp->rbp_bufs);
1323 /* No allocation "under fire" */
1324 /* Otherwise we'd need code to schedule blocked msgs etc */
1325 LASSERT (!the_lnet.ln_routing);
1328 LASSERT (rbp->rbp_credits == nbufs);
1333 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1335 INIT_LIST_HEAD(&rbp->rbp_msgs);
1336 INIT_LIST_HEAD(&rbp->rbp_bufs);
1338 rbp->rbp_npages = npages;
1339 rbp->rbp_credits = 0;
1340 rbp->rbp_mincredits = 0;
1344 lnet_rtrpools_free(void)
1346 lnet_rtrbufpool_t *rtrp;
1349 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1352 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1353 lnet_rtrpool_free_bufs(&rtrp[0]);
1354 lnet_rtrpool_free_bufs(&rtrp[1]);
1355 lnet_rtrpool_free_bufs(&rtrp[2]);
1358 cfs_percpt_free(the_lnet.ln_rtrpools);
1359 the_lnet.ln_rtrpools = NULL;
1363 lnet_nrb_tiny_calculate(int npages)
1365 int nrbs = LNET_NRB_TINY;
1367 if (tiny_router_buffers < 0) {
1368 LCONSOLE_ERROR_MSG(0x10c,
1369 "tiny_router_buffers=%d invalid when "
1370 "routing enabled\n", tiny_router_buffers);
1374 if (tiny_router_buffers > 0)
1375 nrbs = tiny_router_buffers;
1377 nrbs /= LNET_CPT_NUMBER;
1378 return max(nrbs, LNET_NRB_TINY_MIN);
1382 lnet_nrb_small_calculate(int npages)
1384 int nrbs = LNET_NRB_SMALL;
1386 if (small_router_buffers < 0) {
1387 LCONSOLE_ERROR_MSG(0x10c,
1388 "small_router_buffers=%d invalid when "
1389 "routing enabled\n", small_router_buffers);
1393 if (small_router_buffers > 0)
1394 nrbs = small_router_buffers;
1396 nrbs /= LNET_CPT_NUMBER;
1397 return max(nrbs, LNET_NRB_SMALL_MIN);
1401 lnet_nrb_large_calculate(int npages)
1403 int nrbs = LNET_NRB_LARGE;
1405 if (large_router_buffers < 0) {
1406 LCONSOLE_ERROR_MSG(0x10c,
1407 "large_router_buffers=%d invalid when "
1408 "routing enabled\n", large_router_buffers);
1412 if (large_router_buffers > 0)
1413 nrbs = large_router_buffers;
1415 nrbs /= LNET_CPT_NUMBER;
1416 return max(nrbs, LNET_NRB_LARGE_MIN);
1420 lnet_rtrpools_alloc(int im_a_router)
1422 lnet_rtrbufpool_t *rtrp;
1423 int large_pages = (LNET_MTU + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1424 int small_pages = 1;
1431 if (!strcmp(forwarding, "")) {
1432 /* not set either way */
1435 } else if (!strcmp(forwarding, "disabled")) {
1436 /* explicitly disabled */
1438 } else if (!strcmp(forwarding, "enabled")) {
1439 /* explicitly enabled */
1441 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1442 "'enabled' or 'disabled'\n");
1446 nrb_tiny = lnet_nrb_tiny_calculate(0);
1450 nrb_small = lnet_nrb_small_calculate(small_pages);
1454 nrb_large = lnet_nrb_large_calculate(large_pages);
1458 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1460 sizeof(lnet_rtrbufpool_t));
1461 if (the_lnet.ln_rtrpools == NULL) {
1462 LCONSOLE_ERROR_MSG(0x10c,
1463 "Failed to initialize router buffe pool\n");
1467 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1468 lnet_rtrpool_init(&rtrp[0], 0);
1469 rc = lnet_rtrpool_alloc_bufs(&rtrp[0], nrb_tiny, i);
1473 lnet_rtrpool_init(&rtrp[1], small_pages);
1474 rc = lnet_rtrpool_alloc_bufs(&rtrp[1], nrb_small, i);
1478 lnet_rtrpool_init(&rtrp[2], large_pages);
1479 rc = lnet_rtrpool_alloc_bufs(&rtrp[2], nrb_large, i);
1484 lnet_net_lock(LNET_LOCK_EX);
1485 the_lnet.ln_routing = 1;
1486 lnet_net_unlock(LNET_LOCK_EX);
1491 lnet_rtrpools_free();
1496 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1498 struct lnet_peer *lp = NULL;
1499 cfs_time_t now = cfs_time_current();
1500 int cpt = lnet_cpt_of_nid(nid);
1502 LASSERT (!in_interrupt ());
1504 CDEBUG (D_NET, "%s notifying %s: %s\n",
1505 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1506 libcfs_nid2str(nid),
1507 alive ? "up" : "down");
1510 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1511 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1512 libcfs_nid2str(nid), alive ? "birth" : "death",
1513 libcfs_nid2str(ni->ni_nid));
1517 /* can't do predictions... */
1518 if (cfs_time_after(when, now)) {
1519 CWARN ("Ignoring prediction from %s of %s %s "
1520 "%ld seconds in the future\n",
1521 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1522 libcfs_nid2str(nid), alive ? "up" : "down",
1523 cfs_duration_sec(cfs_time_sub(when, now)));
1527 if (ni != NULL && !alive && /* LND telling me she's down */
1528 !auto_down) { /* auto-down disabled */
1529 CDEBUG(D_NET, "Auto-down disabled\n");
1535 if (the_lnet.ln_shutdown) {
1536 lnet_net_unlock(cpt);
1540 lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1543 lnet_net_unlock(cpt);
1544 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1548 /* We can't fully trust LND on reporting exact peer last_alive
1549 * if he notifies us about dead peer. For example ksocklnd can
1550 * call us with when == _time_when_the_node_was_booted_ if
1551 * no connections were successfully established */
1552 if (ni != NULL && !alive && when < lp->lp_last_alive)
1553 when = lp->lp_last_alive;
1555 lnet_notify_locked(lp, ni == NULL, alive, when);
1557 lnet_ni_notify_locked(ni, lp);
1559 lnet_peer_decref_locked(lp);
1561 lnet_net_unlock(cpt);
1564 EXPORT_SYMBOL(lnet_notify);
1567 lnet_get_tunables (void)
1575 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1581 lnet_router_checker (void)
1583 static time_t last = 0;
1584 static int running = 0;
1586 time_t now = cfs_time_current_sec();
1587 int interval = now - last;
1592 /* It's no use to call me again within a sec - all intervals and
1593 * timeouts are measured in seconds */
1594 if (last != 0 && interval < 2)
1598 interval > MAX(live_router_check_interval,
1599 dead_router_check_interval))
1600 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1601 live_router_check_interval, dead_router_check_interval,
1604 LASSERT(LNET_CPT_NUMBER == 1);
1607 LASSERT(!running); /* recursion check */
1613 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1614 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1616 /* consume all pending events */
1621 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1622 * recursion breaker in LNetEQPoll would fail */
1623 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1624 if (rc == 0) /* no event pending */
1627 /* NB a lost SENT prevents me from pinging a router again */
1628 if (rc == -EOVERFLOW) {
1629 CERROR("Dropped an event!!!\n");
1635 lnet_router_checker_event(&ev);
1638 if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1639 lnet_prune_rc_data(1); /* release rcd */
1640 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1645 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1649 version = the_lnet.ln_routers_version;
1650 list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1651 lnet_ping_router_locked(rtr);
1652 LASSERT (version == the_lnet.ln_routers_version);
1657 running = 0; /* lock only needed for the recursion check */
1661 /* NB lnet_peers_start_down depends on me,
1662 * so must be called before any peer creation */
1664 lnet_get_tunables (void)
1668 s = getenv("LNET_ROUTER_PING_TIMEOUT");
1669 if (s != NULL) router_ping_timeout = atoi(s);
1671 s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1672 if (s != NULL) live_router_check_interval = atoi(s);
1674 s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1675 if (s != NULL) dead_router_check_interval = atoi(s);
1677 /* This replaces old lnd_notify mechanism */
1678 check_routers_before_use = 1;
1679 if (dead_router_check_interval <= 0)
1680 dead_router_check_interval = 30;
1684 lnet_rtrpools_free(void)
1689 lnet_rtrpools_alloc(int im_a_arouter)