2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
25 #include <linux/random.h>
26 #include <lnet/lib-lnet.h>
28 #define LNET_NRB_TINY_MIN 512 /* min value for each CPT */
29 #define LNET_NRB_TINY (LNET_NRB_TINY_MIN * 4)
30 #define LNET_NRB_SMALL_MIN 4096 /* min value for each CPT */
31 #define LNET_NRB_SMALL (LNET_NRB_SMALL_MIN * 4)
32 #define LNET_NRB_SMALL_PAGES 1
33 #define LNET_NRB_LARGE_MIN 256 /* min value for each CPT */
34 #define LNET_NRB_LARGE (LNET_NRB_LARGE_MIN * 4)
35 #define LNET_NRB_LARGE_PAGES ((LNET_MTU + PAGE_SIZE - 1) >> \
38 static char *forwarding = "";
39 module_param(forwarding, charp, 0444);
40 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
42 static int tiny_router_buffers;
43 module_param(tiny_router_buffers, int, 0444);
44 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
45 static int small_router_buffers;
46 module_param(small_router_buffers, int, 0444);
47 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
48 static int large_router_buffers;
49 module_param(large_router_buffers, int, 0444);
50 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
51 static int peer_buffer_credits;
52 module_param(peer_buffer_credits, int, 0444);
53 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
55 static int auto_down = 1;
56 module_param(auto_down, int, 0444);
57 MODULE_PARM_DESC(auto_down, "Automatically mark peers down on comms error");
60 lnet_peer_buffer_credits(struct lnet_net *net)
62 /* NI option overrides LNet default */
63 if (net->net_tunables.lct_peer_rtr_credits > 0)
64 return net->net_tunables.lct_peer_rtr_credits;
65 if (peer_buffer_credits > 0)
66 return peer_buffer_credits;
68 /* As an approximation, allow this peer the same number of router
69 * buffers as it is allowed outstanding sends */
70 return net->net_tunables.lct_peer_tx_credits;
73 static int check_routers_before_use;
74 module_param(check_routers_before_use, int, 0444);
75 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
77 int avoid_asym_router_failure = 1;
78 module_param(avoid_asym_router_failure, int, 0644);
79 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
81 static int dead_router_check_interval = 60;
82 module_param(dead_router_check_interval, int, 0644);
83 MODULE_PARM_DESC(dead_router_check_interval, "Seconds between dead router health checks (<= 0 to disable)");
85 static int live_router_check_interval = 60;
86 module_param(live_router_check_interval, int, 0644);
87 MODULE_PARM_DESC(live_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
89 static int router_ping_timeout = 50;
90 module_param(router_ping_timeout, int, 0644);
91 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
94 * A value between 0 and 100. 0 meaning that even if router's interfaces
95 * have the worse health still consider the gateway usable.
96 * 100 means that at least one interface on the route's remote net is 100%
97 * healthy to consider the route alive.
98 * The default is set to 100 to ensure we maintain the original behavior.
100 unsigned int router_sensitivity_percentage = 100;
101 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
102 static struct kernel_param_ops param_ops_rtr_sensitivity = {
103 .set = rtr_sensitivity_set,
104 .get = param_get_int,
106 #define param_check_rtr_sensitivity(name, p) \
107 __param_check(name, p, int)
108 #ifdef HAVE_KERNEL_PARAM_OPS
109 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
111 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
112 &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
114 MODULE_PARM_DESC(router_sensitivity_percentage,
115 "How healthy a gateway should be to be used in percent");
118 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
121 unsigned *sen = (unsigned *)kp->arg;
124 rc = kstrtoul(val, 0, &value);
126 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
130 if (value < 0 || value > 100) {
131 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
136 * The purpose of locking the api_mutex here is to ensure that
137 * the correct value ends up stored properly.
139 mutex_lock(&the_lnet.ln_api_mutex);
143 mutex_unlock(&the_lnet.ln_api_mutex);
149 lnet_peers_start_down(void)
151 return check_routers_before_use;
155 * A net is alive if at least one gateway NI on the network is alive.
158 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
160 struct lnet_peer_ni *lpni;
162 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
163 if (lnet_is_peer_ni_alive(lpni))
171 * a gateway is alive only if all its nets are alive
172 * called with cpt lock held
174 bool lnet_is_gateway_alive(struct lnet_peer *gw)
176 struct lnet_peer_net *lpn;
178 list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
179 if (!lnet_is_gateway_net_alive(lpn))
187 * lnet_is_route_alive() needs to be called with cpt lock held
188 * A route is alive if the gateway can route between the local network and
189 * the remote network of the route.
190 * This means at least one NI is alive on each of the local and remote
191 * networks of the gateway.
193 bool lnet_is_route_alive(struct lnet_route *route)
195 struct lnet_peer *gw = route->lr_gateway;
196 struct lnet_peer_net *llpn;
197 struct lnet_peer_net *rlpn;
201 * check the gateway's interfaces on the route rnet to make sure
202 * that the gateway is viable.
204 llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
208 route_alive = lnet_is_gateway_net_alive(llpn);
210 if (avoid_asym_router_failure) {
211 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
214 route_alive = route_alive &&
215 lnet_is_gateway_net_alive(rlpn);
221 spin_lock(&gw->lp_lock);
222 if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
223 if (gw->lp_rtr_refcount > 0)
224 CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
225 libcfs_nid2str(gw->lp_primary_nid));
228 spin_unlock(&gw->lp_lock);
234 lnet_notify_locked(struct lnet_peer_ni *lp, int notifylnd, int alive,
237 if (lp->lpni_timestamp > when) { /* out of date information */
238 CDEBUG(D_NET, "Out of date\n");
243 * This function can be called with different cpt locks being
244 * held. lpni_alive_count modification needs to be properly protected.
245 * Significant reads to lpni_alive_count are also protected with
248 spin_lock(&lp->lpni_lock);
250 lp->lpni_timestamp = when; /* update timestamp */
253 if (lp->lpni_alive_count != 0 &&
254 /* new date for old news */
255 (!lnet_is_peer_ni_alive(lp)) == (!alive)) {
256 spin_unlock(&lp->lpni_lock);
257 CDEBUG(D_NET, "Old news\n");
261 /* Flag that notification is outstanding */
263 lp->lpni_alive_count++;
265 lp->lpni_notifylnd = notifylnd;
266 if (lnet_is_peer_ni_alive(lp))
267 lp->lpni_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
269 spin_unlock(&lp->lpni_lock);
271 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lpni_nid), alive);
275 * This function will always be called with lp->lpni_cpt lock held.
278 lnet_ni_notify_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
283 /* Notify only in 1 thread at any time to ensure ordered notification.
284 * NB individual events can be missed; the only guarantee is that you
285 * always get the most recent news */
287 spin_lock(&lp->lpni_lock);
289 if (lp->lpni_notifying || ni == NULL) {
290 spin_unlock(&lp->lpni_lock);
294 lp->lpni_notifying = 1;
297 * lp->lpni_notify needs to be protected because it can be set in
298 * lnet_notify_locked().
300 while (lp->lpni_notify) {
301 alive = lnet_is_peer_ni_alive(lp);
302 notifylnd = lp->lpni_notifylnd;
304 lp->lpni_notifylnd = 0;
307 if (notifylnd && ni->ni_net->net_lnd->lnd_notify != NULL) {
308 spin_unlock(&lp->lpni_lock);
309 lnet_net_unlock(lp->lpni_cpt);
311 /* A new notification could happen now; I'll handle it
312 * when control returns to me */
314 (ni->ni_net->net_lnd->lnd_notify)(ni, lp->lpni_nid,
317 lnet_net_lock(lp->lpni_cpt);
318 spin_lock(&lp->lpni_lock);
322 lp->lpni_notifying = 0;
323 spin_unlock(&lp->lpni_lock);
327 lnet_rtr_addref_locked(struct lnet_peer *lp)
329 LASSERT(lp->lp_rtr_refcount >= 0);
331 /* lnet_net_lock must be exclusively locked */
332 lp->lp_rtr_refcount++;
333 if (lp->lp_rtr_refcount == 1) {
334 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
335 /* addref for the_lnet.ln_routers */
336 lnet_peer_addref_locked(lp);
337 the_lnet.ln_routers_version++;
342 lnet_rtr_decref_locked(struct lnet_peer *lp)
344 LASSERT(atomic_read(&lp->lp_refcount) > 0);
345 LASSERT(lp->lp_rtr_refcount > 0);
347 /* lnet_net_lock must be exclusively locked */
348 lp->lp_rtr_refcount--;
349 if (lp->lp_rtr_refcount == 0) {
350 LASSERT(list_empty(&lp->lp_routes));
352 list_del(&lp->lp_rtr_list);
353 /* decref for the_lnet.ln_routers */
354 lnet_peer_decref_locked(lp);
355 the_lnet.ln_routers_version++;
359 struct lnet_remotenet *
360 lnet_find_rnet_locked(__u32 net)
362 struct lnet_remotenet *rnet;
363 struct list_head *tmp;
364 struct list_head *rn_list;
366 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
368 rn_list = lnet_net2rnethash(net);
369 list_for_each(tmp, rn_list) {
370 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
372 if (rnet->lrn_net == net)
378 static void lnet_shuffle_seed(void)
381 struct lnet_ni *ni = NULL;
386 /* Nodes with small feet have little entropy
387 * the NID for this node gives the most entropy in the low bits */
388 while ((ni = lnet_get_next_ni_locked(NULL, ni)))
389 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
395 /* NB expects LNET_LOCK held */
397 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
399 unsigned int len = 0;
400 unsigned int offset = 0;
405 list_for_each(e, &rnet->lrn_routes)
409 * Randomly adding routes to the list is done to ensure that when
410 * different nodes are using the same list of routers, they end up
411 * preferring different routers.
413 offset = cfs_rand() % (len + 1);
414 list_for_each(e, &rnet->lrn_routes) {
419 list_add(&route->lr_list, e);
421 * force a router check on the gateway to make sure the route is
424 route->lr_gateway->lp_rtrcheck_timestamp = 0;
426 the_lnet.ln_remote_nets_version++;
428 /* add the route on the gateway list */
429 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
431 /* take a router reference count on the gateway */
432 lnet_rtr_addref_locked(route->lr_gateway);
436 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
437 unsigned int priority)
439 struct list_head *route_entry;
440 struct lnet_remotenet *rnet;
441 struct lnet_remotenet *rnet2;
442 struct lnet_route *route;
443 struct lnet_peer_ni *lpni;
444 struct lnet_peer *gw;
448 CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
449 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
451 if (gateway == LNET_NID_ANY ||
452 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
453 net == LNET_NIDNET(LNET_NID_ANY) ||
454 LNET_NETTYP(net) == LOLND ||
455 LNET_NIDNET(gateway) == net ||
456 (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
459 /* it's a local network */
460 if (lnet_islocalnet(net))
463 /* Assume net, route, all new */
464 LIBCFS_ALLOC(route, sizeof(*route));
465 LIBCFS_ALLOC(rnet, sizeof(*rnet));
466 if (route == NULL || rnet == NULL) {
467 CERROR("Out of memory creating route %s %d %s\n",
468 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
470 LIBCFS_FREE(route, sizeof(*route));
472 LIBCFS_FREE(rnet, sizeof(*rnet));
476 INIT_LIST_HEAD(&rnet->lrn_routes);
478 /* store the local and remote net that the route represents */
479 route->lr_lnet = LNET_NIDNET(gateway);
481 route->lr_priority = priority;
482 route->lr_hops = hops;
484 lnet_net_lock(LNET_LOCK_EX);
487 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
488 * lose that once we're done
490 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
492 lnet_net_unlock(LNET_LOCK_EX);
494 LIBCFS_FREE(route, sizeof(*route));
495 LIBCFS_FREE(rnet, sizeof(*rnet));
498 CERROR("Error %d creating route %s %d %s\n", rc,
499 libcfs_net2str(net), hops,
500 libcfs_nid2str(gateway));
504 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
505 gw = lpni->lpni_peer_net->lpn_peer;
507 route->lr_gateway = gw;
509 rnet2 = lnet_find_rnet_locked(net);
512 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
516 /* Search for a duplicate route (it's a NOOP if it is) */
518 list_for_each(route_entry, &rnet2->lrn_routes) {
519 struct lnet_route *route2;
521 route2 = list_entry(route_entry, struct lnet_route, lr_list);
522 if (route2->lr_gateway == route->lr_gateway) {
527 /* our lookups must be true */
528 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
532 * It is possible to add multiple routes through the same peer,
533 * but it'll be using a different NID of that peer. When the
534 * gateway is discovered, discovery will consolidate the different
535 * peers into one peer. In this case the discovery code will have
536 * to move the routes from the peer that's being deleted to the
537 * consolidated peer lp_routes list
540 lnet_add_route_to_rnet(rnet2, route);
543 * get rid of the reference on the lpni.
545 lnet_peer_ni_decref_locked(lpni);
546 lnet_net_unlock(LNET_LOCK_EX);
552 LIBCFS_FREE(route, sizeof(*route));
556 LIBCFS_FREE(rnet, sizeof(*rnet));
558 /* kick start the monitor thread to handle the added route */
559 wake_up(&the_lnet.ln_mt_waitq);
565 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
566 struct list_head *zombies)
568 struct lnet_peer *gateway;
569 struct lnet_route *route;
570 struct lnet_route *tmp;
572 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
573 gateway = route->lr_gateway;
574 if (gw_nid != LNET_NID_ANY &&
575 gw_nid != gateway->lp_primary_nid)
579 * move to zombie to delete outside the lock
580 * Note that this function is called with the
581 * ln_api_mutex held as well as the exclusive net
582 * lock. Adding to the remote net list happens
583 * under the same conditions. Same goes for the
584 * gateway router list
586 list_move(&route->lr_list, zombies);
587 the_lnet.ln_remote_nets_version++;
589 list_del(&route->lr_gwlist);
590 lnet_rtr_decref_locked(gateway);
595 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
597 struct list_head rnet_zombies;
598 struct lnet_remotenet *rnet;
599 struct lnet_remotenet *tmp;
600 struct list_head *rn_list;
601 struct lnet_peer_ni *lpni;
602 struct lnet_route *route;
603 struct list_head zombies;
604 struct lnet_peer *lp;
607 INIT_LIST_HEAD(&rnet_zombies);
608 INIT_LIST_HEAD(&zombies);
610 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
611 libcfs_net2str(net), libcfs_nid2str(gw_nid));
613 /* NB Caller may specify either all routes via the given gateway
614 * or a specific route entry actual NIDs) */
616 lnet_net_lock(LNET_LOCK_EX);
618 lpni = lnet_find_peer_ni_locked(gw_nid);
620 lp = lpni->lpni_peer_net->lpn_peer;
622 gw_nid = lp->lp_primary_nid;
623 lnet_peer_ni_decref_locked(lpni);
626 if (net != LNET_NIDNET(LNET_NID_ANY)) {
627 rnet = lnet_find_rnet_locked(net);
629 lnet_net_unlock(LNET_LOCK_EX);
632 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
634 if (list_empty(&rnet->lrn_routes))
635 list_move(&rnet->lrn_list, &rnet_zombies);
639 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
640 rn_list = &the_lnet.ln_remote_nets_hash[i];
642 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
643 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
645 if (list_empty(&rnet->lrn_routes))
646 list_move(&rnet->lrn_list, &rnet_zombies);
651 lnet_net_unlock(LNET_LOCK_EX);
653 while (!list_empty(&zombies)) {
654 route = list_first_entry(&zombies, struct lnet_route, lr_list);
655 list_del(&route->lr_list);
656 LIBCFS_FREE(route, sizeof(*route));
659 while (!list_empty(&rnet_zombies)) {
660 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
662 list_del(&rnet->lrn_list);
663 LIBCFS_FREE(rnet, sizeof(*rnet));
670 lnet_destroy_routes (void)
672 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
675 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
677 struct lnet_rtrbufpool *rbp;
678 int i, rc = -ENOENT, j;
680 if (the_lnet.ln_rtrpools == NULL)
684 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
689 for (j = 0; j < LNET_NRBPOOLS; j++) {
690 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
691 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
692 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
693 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
700 lnet_net_lock(LNET_LOCK_EX);
701 pool_cfg->pl_routing = the_lnet.ln_routing;
702 lnet_net_unlock(LNET_LOCK_EX);
708 lnet_get_route(int idx, __u32 *net, __u32 *hops,
709 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
711 struct list_head *e1;
712 struct list_head *e2;
713 struct lnet_remotenet *rnet;
714 struct lnet_route *route;
717 struct list_head *rn_list;
719 cpt = lnet_net_lock_current();
721 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
722 rn_list = &the_lnet.ln_remote_nets_hash[i];
723 list_for_each(e1, rn_list) {
724 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
726 list_for_each(e2, &rnet->lrn_routes) {
727 route = list_entry(e2, struct lnet_route,
731 *net = rnet->lrn_net;
732 *hops = route->lr_hops;
733 *priority = route->lr_priority;
734 *gateway = route->lr_gateway->lp_primary_nid;
735 *alive = lnet_is_route_alive(route);
736 lnet_net_unlock(cpt);
743 lnet_net_unlock(cpt);
748 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
750 struct lnet_ni_status *stat;
754 __swab32s(&pbuf->pb_info.pi_magic);
755 __swab32s(&pbuf->pb_info.pi_features);
756 __swab32s(&pbuf->pb_info.pi_pid);
757 __swab32s(&pbuf->pb_info.pi_nnis);
758 nnis = pbuf->pb_info.pi_nnis;
759 if (nnis > pbuf->pb_nnis)
760 nnis = pbuf->pb_nnis;
761 for (i = 0; i < nnis; i++) {
762 stat = &pbuf->pb_info.pi_ni[i];
763 __swab64s(&stat->ns_nid);
764 __swab32s(&stat->ns_status);
773 lnet_parse_rc_info(struct lnet_rc_data *rcd)
779 lnet_router_checker_event(struct lnet_event *event)
781 struct lnet_rc_data *rcd = event->md.user_ptr;
782 struct lnet_peer_ni *lp;
784 LASSERT(rcd != NULL);
786 if (event->unlinked) {
787 LNetInvalidateMDHandle(&rcd->rcd_mdh);
791 LASSERT(event->type == LNET_EVENT_SEND ||
792 event->type == LNET_EVENT_REPLY);
794 lp = rcd->rcd_gateway;
797 /* NB: it's called with holding lnet_res_lock, we have a few
798 * places need to hold both locks at the same time, please take
799 * care of lock ordering */
800 lnet_net_lock(lp->lpni_cpt);
801 if (!lnet_isrouter(lp) || lp->lpni_rcd != rcd) {
802 /* ignore if no longer a router or rcd is replaced */
806 if (event->type == LNET_EVENT_SEND) {
807 if (event->status == 0)
811 /* LNET_EVENT_REPLY */
812 /* A successful REPLY means the router is up. If _any_ comms
813 * to the router fail I assume it's down (this will happen if
814 * we ping alive routers to try to detect router death before
815 * apps get burned). */
817 lnet_notify_locked(lp, 1, !event->status, ktime_get_seconds());
818 /* The router checker will wake up very shortly and do the
819 * actual notification.
820 * XXX If 'lp' stops being a router before then, it will still
821 * have the notification pending!!! */
823 if (avoid_asym_router_failure && event->status == 0)
824 lnet_parse_rc_info(rcd);
827 lnet_net_unlock(lp->lpni_cpt);
831 lnet_wait_known_routerstate(void)
833 struct lnet_peer *rtr;
834 struct list_head *entry;
837 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
840 int cpt = lnet_net_lock_current();
843 list_for_each(entry, &the_lnet.ln_routers) {
844 rtr = list_entry(entry, struct lnet_peer,
847 spin_lock(&rtr->lp_lock);
849 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
851 spin_unlock(&rtr->lp_lock);
854 spin_unlock(&rtr->lp_lock);
857 lnet_net_unlock(cpt);
862 set_current_state(TASK_UNINTERRUPTIBLE);
863 schedule_timeout(cfs_time_seconds(1));
867 /* TODO: reimplement */
869 lnet_router_ni_update_locked(struct lnet_peer_ni *gw, __u32 net)
871 struct lnet_route *rte;
872 struct lnet_peer *lp;
874 if ((gw->lpni_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0)
875 lp = gw->lpni_peer_net->lpn_peer;
879 list_for_each_entry(rte, &lp->lp_routes, lr_gwlist) {
880 if (rte->lr_net == net) {
888 lnet_update_ni_status_locked(void)
890 struct lnet_ni *ni = NULL;
894 LASSERT(the_lnet.ln_routing);
896 timeout = router_ping_timeout +
897 MAX(live_router_check_interval, dead_router_check_interval);
899 now = ktime_get_real_seconds();
900 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
901 if (ni->ni_net->net_lnd->lnd_type == LOLND)
904 if (now < ni->ni_last_alive + timeout)
908 /* re-check with lock */
909 if (now < ni->ni_last_alive + timeout) {
914 LASSERT(ni->ni_status != NULL);
916 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
917 CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
918 libcfs_nid2str(ni->ni_nid), timeout);
919 /* NB: so far, this is the only place to set
920 * NI status to "down" */
921 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
927 int lnet_router_pre_mt_start(void)
931 if (check_routers_before_use &&
932 dead_router_check_interval <= 0) {
933 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
934 " set if 'check_routers_before_use' is set"
939 rc = LNetEQAlloc(0, lnet_router_checker_event, &the_lnet.ln_rc_eqh);
941 CERROR("Can't allocate EQ(0): %d\n", rc);
948 void lnet_router_post_mt_start(void)
950 if (check_routers_before_use) {
951 /* Note that a helpful side-effect of pinging all known routers
952 * at startup is that it makes them drop stale connections they
953 * may have to a previous instance of me. */
954 lnet_wait_known_routerstate();
959 lnet_router_cleanup(void)
963 rc = LNetEQFree(the_lnet.ln_rc_eqh);
969 lnet_prune_rc_data(int wait_unlink)
971 wait_unlink = wait_unlink;
975 * This function is called from the monitor thread to check if there are
976 * any active routers that need to be checked.
979 lnet_router_checker_active(void)
981 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
984 /* Router Checker thread needs to run when routing is enabled in
985 * order to call lnet_update_ni_status_locked() */
986 if (the_lnet.ln_routing)
989 /* if there are routers that need to be cleaned up then do so */
990 if (!list_empty(&the_lnet.ln_rcd_deathrow) ||
991 !list_empty(&the_lnet.ln_rcd_zombie))
994 return !list_empty(&the_lnet.ln_routers) &&
995 (live_router_check_interval > 0 ||
996 dead_router_check_interval > 0);
1000 lnet_check_routers(void)
1002 struct lnet_peer *rtr;
1003 struct list_head *entry;
1007 cpt = lnet_net_lock_current();
1009 version = the_lnet.ln_routers_version;
1011 list_for_each(entry, &the_lnet.ln_routers) {
1012 rtr = list_entry(entry, struct lnet_peer,
1015 /* TODO use discovery to determine if router is alive */
1017 /* NB dropped lock */
1018 if (version != the_lnet.ln_routers_version) {
1019 /* the routers list has changed */
1024 if (the_lnet.ln_routing)
1025 lnet_update_ni_status_locked();
1027 lnet_net_unlock(cpt);
1029 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1033 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1035 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1037 while (--npages >= 0)
1038 __free_page(rb->rb_kiov[npages].kiov_page);
1040 LIBCFS_FREE(rb, sz);
1043 static struct lnet_rtrbuf *
1044 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1046 int npages = rbp->rbp_npages;
1047 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1049 struct lnet_rtrbuf *rb;
1052 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1058 for (i = 0; i < npages; i++) {
1059 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1060 GFP_KERNEL | __GFP_ZERO);
1063 __free_page(rb->rb_kiov[i].kiov_page);
1065 LIBCFS_FREE(rb, sz);
1069 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
1070 rb->rb_kiov[i].kiov_offset = 0;
1071 rb->rb_kiov[i].kiov_page = page;
1078 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1080 int npages = rbp->rbp_npages;
1081 struct lnet_rtrbuf *rb;
1082 struct list_head tmp;
1084 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1087 INIT_LIST_HEAD(&tmp);
1090 list_splice_init(&rbp->rbp_msgs, &tmp);
1091 lnet_drop_routed_msgs_locked(&tmp, cpt);
1092 list_splice_init(&rbp->rbp_bufs, &tmp);
1093 rbp->rbp_req_nbuffers = 0;
1094 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1095 rbp->rbp_mincredits = 0;
1096 lnet_net_unlock(cpt);
1098 /* Free buffers on the free list. */
1099 while (!list_empty(&tmp)) {
1100 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1101 list_del(&rb->rb_list);
1102 lnet_destroy_rtrbuf(rb, npages);
1107 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1109 struct list_head rb_list;
1110 struct lnet_rtrbuf *rb;
1112 int num_buffers = 0;
1114 int npages = rbp->rbp_npages;
1117 /* If we are called for less buffers than already in the pool, we
1118 * just lower the req_nbuffers number and excess buffers will be
1119 * thrown away as they are returned to the free list. Credits
1120 * then get adjusted as well.
1121 * If we already have enough buffers allocated to serve the
1122 * increase requested, then we can treat that the same way as we
1123 * do the decrease. */
1124 num_rb = nbufs - rbp->rbp_nbuffers;
1125 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1126 rbp->rbp_req_nbuffers = nbufs;
1127 lnet_net_unlock(cpt);
1130 /* store the older value of rbp_req_nbuffers and then set it to
1131 * the new request to prevent lnet_return_rx_credits_locked() from
1132 * freeing buffers that we need to keep around */
1133 old_req_nbufs = rbp->rbp_req_nbuffers;
1134 rbp->rbp_req_nbuffers = nbufs;
1135 lnet_net_unlock(cpt);
1137 INIT_LIST_HEAD(&rb_list);
1139 /* allocate the buffers on a local list first. If all buffers are
1140 * allocated successfully then join this list to the rbp buffer
1141 * list. If not then free all allocated buffers. */
1142 while (num_rb-- > 0) {
1143 rb = lnet_new_rtrbuf(rbp, cpt);
1145 CERROR("Failed to allocate %d route bufs of %d pages\n",
1149 rbp->rbp_req_nbuffers = old_req_nbufs;
1150 lnet_net_unlock(cpt);
1155 list_add(&rb->rb_list, &rb_list);
1161 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1162 rbp->rbp_nbuffers += num_buffers;
1163 rbp->rbp_credits += num_buffers;
1164 rbp->rbp_mincredits = rbp->rbp_credits;
1165 /* We need to schedule blocked msg using the newly
1167 while (!list_empty(&rbp->rbp_bufs) &&
1168 !list_empty(&rbp->rbp_msgs))
1169 lnet_schedule_blocked_locked(rbp);
1171 lnet_net_unlock(cpt);
1176 while (!list_empty(&rb_list)) {
1177 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1178 list_del(&rb->rb_list);
1179 lnet_destroy_rtrbuf(rb, npages);
1186 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1188 INIT_LIST_HEAD(&rbp->rbp_msgs);
1189 INIT_LIST_HEAD(&rbp->rbp_bufs);
1191 rbp->rbp_npages = npages;
1192 rbp->rbp_credits = 0;
1193 rbp->rbp_mincredits = 0;
1197 lnet_rtrpools_free(int keep_pools)
1199 struct lnet_rtrbufpool *rtrp;
1202 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1205 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1206 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1207 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1208 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1212 cfs_percpt_free(the_lnet.ln_rtrpools);
1213 the_lnet.ln_rtrpools = NULL;
1218 lnet_nrb_tiny_calculate(void)
1220 int nrbs = LNET_NRB_TINY;
1222 if (tiny_router_buffers < 0) {
1223 LCONSOLE_ERROR_MSG(0x10c,
1224 "tiny_router_buffers=%d invalid when "
1225 "routing enabled\n", tiny_router_buffers);
1229 if (tiny_router_buffers > 0)
1230 nrbs = tiny_router_buffers;
1232 nrbs /= LNET_CPT_NUMBER;
1233 return max(nrbs, LNET_NRB_TINY_MIN);
1237 lnet_nrb_small_calculate(void)
1239 int nrbs = LNET_NRB_SMALL;
1241 if (small_router_buffers < 0) {
1242 LCONSOLE_ERROR_MSG(0x10c,
1243 "small_router_buffers=%d invalid when "
1244 "routing enabled\n", small_router_buffers);
1248 if (small_router_buffers > 0)
1249 nrbs = small_router_buffers;
1251 nrbs /= LNET_CPT_NUMBER;
1252 return max(nrbs, LNET_NRB_SMALL_MIN);
1256 lnet_nrb_large_calculate(void)
1258 int nrbs = LNET_NRB_LARGE;
1260 if (large_router_buffers < 0) {
1261 LCONSOLE_ERROR_MSG(0x10c,
1262 "large_router_buffers=%d invalid when "
1263 "routing enabled\n", large_router_buffers);
1267 if (large_router_buffers > 0)
1268 nrbs = large_router_buffers;
1270 nrbs /= LNET_CPT_NUMBER;
1271 return max(nrbs, LNET_NRB_LARGE_MIN);
1275 lnet_rtrpools_alloc(int im_a_router)
1277 struct lnet_rtrbufpool *rtrp;
1284 if (!strcmp(forwarding, "")) {
1285 /* not set either way */
1288 } else if (!strcmp(forwarding, "disabled")) {
1289 /* explicitly disabled */
1291 } else if (!strcmp(forwarding, "enabled")) {
1292 /* explicitly enabled */
1294 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1295 "'enabled' or 'disabled'\n");
1299 nrb_tiny = lnet_nrb_tiny_calculate();
1303 nrb_small = lnet_nrb_small_calculate();
1307 nrb_large = lnet_nrb_large_calculate();
1311 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1313 sizeof(struct lnet_rtrbufpool));
1314 if (the_lnet.ln_rtrpools == NULL) {
1315 LCONSOLE_ERROR_MSG(0x10c,
1316 "Failed to initialize router buffe pool\n");
1320 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1321 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1322 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1327 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1328 LNET_NRB_SMALL_PAGES);
1329 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1334 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1335 LNET_NRB_LARGE_PAGES);
1336 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1342 lnet_net_lock(LNET_LOCK_EX);
1343 the_lnet.ln_routing = 1;
1344 lnet_net_unlock(LNET_LOCK_EX);
1345 wake_up(&the_lnet.ln_mt_waitq);
1349 lnet_rtrpools_free(0);
1354 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1359 struct lnet_rtrbufpool *rtrp;
1361 /* If the provided values for each buffer pool are different than the
1362 * configured values, we need to take action. */
1364 tiny_router_buffers = tiny;
1365 nrb = lnet_nrb_tiny_calculate();
1366 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1367 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1374 small_router_buffers = small;
1375 nrb = lnet_nrb_small_calculate();
1376 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1377 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1384 large_router_buffers = large;
1385 nrb = lnet_nrb_large_calculate();
1386 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1387 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1398 lnet_rtrpools_adjust(int tiny, int small, int large)
1400 /* this function doesn't revert the changes if adding new buffers
1401 * failed. It's up to the user space caller to revert the
1404 if (!the_lnet.ln_routing)
1407 return lnet_rtrpools_adjust_helper(tiny, small, large);
1411 lnet_rtrpools_enable(void)
1415 if (the_lnet.ln_routing)
1418 if (the_lnet.ln_rtrpools == NULL)
1419 /* If routing is turned off, and we have never
1420 * initialized the pools before, just call the
1421 * standard buffer pool allocation routine as
1422 * if we are just configuring this for the first
1424 rc = lnet_rtrpools_alloc(1);
1426 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1430 lnet_net_lock(LNET_LOCK_EX);
1431 the_lnet.ln_routing = 1;
1433 the_lnet.ln_ping_target->pb_info.pi_features &=
1434 ~LNET_PING_FEAT_RTE_DISABLED;
1435 lnet_net_unlock(LNET_LOCK_EX);
1441 lnet_rtrpools_disable(void)
1443 if (!the_lnet.ln_routing)
1446 lnet_net_lock(LNET_LOCK_EX);
1447 the_lnet.ln_routing = 0;
1448 the_lnet.ln_ping_target->pb_info.pi_features |=
1449 LNET_PING_FEAT_RTE_DISABLED;
1451 tiny_router_buffers = 0;
1452 small_router_buffers = 0;
1453 large_router_buffers = 0;
1454 lnet_net_unlock(LNET_LOCK_EX);
1455 lnet_rtrpools_free(1);
1459 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, int alive, time64_t when)
1461 struct lnet_peer_ni *lp = NULL;
1462 time64_t now = ktime_get_seconds();
1463 int cpt = lnet_cpt_of_nid(nid, ni);
1465 LASSERT (!in_interrupt ());
1467 CDEBUG (D_NET, "%s notifying %s: %s\n",
1468 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1469 libcfs_nid2str(nid),
1470 alive ? "up" : "down");
1473 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1474 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1475 libcfs_nid2str(nid), alive ? "birth" : "death",
1476 libcfs_nid2str(ni->ni_nid));
1480 /* can't do predictions... */
1482 CWARN("Ignoring prediction from %s of %s %s "
1483 "%lld seconds in the future\n",
1484 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1485 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1489 if (ni != NULL && !alive && /* LND telling me she's down */
1490 !auto_down) { /* auto-down disabled */
1491 CDEBUG(D_NET, "Auto-down disabled\n");
1497 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1498 lnet_net_unlock(cpt);
1502 lp = lnet_find_peer_ni_locked(nid);
1505 lnet_net_unlock(cpt);
1506 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1511 * It is possible for this function to be called for the same peer
1512 * but with different NIs. We want to synchronize the notification
1513 * between the different calls. So we will use the lpni_cpt to
1514 * grab the net lock.
1516 if (lp->lpni_cpt != cpt) {
1517 lnet_net_unlock(cpt);
1522 /* We can't fully trust LND on reporting exact peer last_alive
1523 * if he notifies us about dead peer. For example ksocklnd can
1524 * call us with when == _time_when_the_node_was_booted_ if
1525 * no connections were successfully established */
1526 if (ni != NULL && !alive && when < lp->lpni_last_alive)
1527 when = lp->lpni_last_alive;
1529 lnet_notify_locked(lp, ni == NULL, alive, when);
1532 lnet_ni_notify_locked(ni, lp);
1534 lnet_peer_ni_decref_locked(lp);
1536 lnet_net_unlock(cpt);
1539 EXPORT_SYMBOL(lnet_notify);