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 lnet_notify_locked(struct lnet_peer_ni *lp, int notifylnd, int alive,
158 if (lp->lpni_timestamp > when) { /* out of date information */
159 CDEBUG(D_NET, "Out of date\n");
164 * This function can be called with different cpt locks being
165 * held. lpni_alive_count modification needs to be properly protected.
166 * Significant reads to lpni_alive_count are also protected with
169 spin_lock(&lp->lpni_lock);
171 lp->lpni_timestamp = when; /* update timestamp */
174 if (lp->lpni_alive_count != 0 &&
175 /* new date for old news */
176 (!lnet_is_peer_ni_alive(lp)) == (!alive)) {
177 spin_unlock(&lp->lpni_lock);
178 CDEBUG(D_NET, "Old news\n");
182 /* Flag that notification is outstanding */
184 lp->lpni_alive_count++;
186 lp->lpni_notifylnd = notifylnd;
187 if (lnet_is_peer_ni_alive(lp))
188 lp->lpni_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
190 spin_unlock(&lp->lpni_lock);
192 CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lpni_nid), alive);
196 * This function will always be called with lp->lpni_cpt lock held.
199 lnet_ni_notify_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
204 /* Notify only in 1 thread at any time to ensure ordered notification.
205 * NB individual events can be missed; the only guarantee is that you
206 * always get the most recent news */
208 spin_lock(&lp->lpni_lock);
210 if (lp->lpni_notifying || ni == NULL) {
211 spin_unlock(&lp->lpni_lock);
215 lp->lpni_notifying = 1;
218 * lp->lpni_notify needs to be protected because it can be set in
219 * lnet_notify_locked().
221 while (lp->lpni_notify) {
222 alive = lnet_is_peer_ni_alive(lp);
223 notifylnd = lp->lpni_notifylnd;
225 lp->lpni_notifylnd = 0;
228 if (notifylnd && ni->ni_net->net_lnd->lnd_notify != NULL) {
229 spin_unlock(&lp->lpni_lock);
230 lnet_net_unlock(lp->lpni_cpt);
232 /* A new notification could happen now; I'll handle it
233 * when control returns to me */
235 (ni->ni_net->net_lnd->lnd_notify)(ni, lp->lpni_nid,
238 lnet_net_lock(lp->lpni_cpt);
239 spin_lock(&lp->lpni_lock);
243 lp->lpni_notifying = 0;
244 spin_unlock(&lp->lpni_lock);
248 lnet_rtr_addref_locked(struct lnet_peer *lp)
250 LASSERT(lp->lp_rtr_refcount >= 0);
252 /* lnet_net_lock must be exclusively locked */
253 lp->lp_rtr_refcount++;
254 if (lp->lp_rtr_refcount == 1) {
255 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
256 /* addref for the_lnet.ln_routers */
257 lnet_peer_addref_locked(lp);
258 the_lnet.ln_routers_version++;
263 lnet_rtr_decref_locked(struct lnet_peer *lp)
265 LASSERT(atomic_read(&lp->lp_refcount) > 0);
266 LASSERT(lp->lp_rtr_refcount > 0);
268 /* lnet_net_lock must be exclusively locked */
269 lp->lp_rtr_refcount--;
270 if (lp->lp_rtr_refcount == 0) {
271 LASSERT(list_empty(&lp->lp_routes));
273 list_del(&lp->lp_rtr_list);
274 /* decref for the_lnet.ln_routers */
275 lnet_peer_decref_locked(lp);
276 the_lnet.ln_routers_version++;
280 struct lnet_remotenet *
281 lnet_find_rnet_locked(__u32 net)
283 struct lnet_remotenet *rnet;
284 struct list_head *tmp;
285 struct list_head *rn_list;
287 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
289 rn_list = lnet_net2rnethash(net);
290 list_for_each(tmp, rn_list) {
291 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
293 if (rnet->lrn_net == net)
299 static void lnet_shuffle_seed(void)
302 struct lnet_ni *ni = NULL;
307 /* Nodes with small feet have little entropy
308 * the NID for this node gives the most entropy in the low bits */
309 while ((ni = lnet_get_next_ni_locked(NULL, ni)))
310 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
316 /* NB expects LNET_LOCK held */
318 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
320 unsigned int len = 0;
321 unsigned int offset = 0;
326 list_for_each(e, &rnet->lrn_routes)
330 * Randomly adding routes to the list is done to ensure that when
331 * different nodes are using the same list of routers, they end up
332 * preferring different routers.
334 offset = cfs_rand() % (len + 1);
335 list_for_each(e, &rnet->lrn_routes) {
340 list_add(&route->lr_list, e);
342 * force a router check on the gateway to make sure the route is
345 route->lr_gateway->lp_rtrcheck_timestamp = 0;
347 the_lnet.ln_remote_nets_version++;
349 /* add the route on the gateway list */
350 list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
352 /* take a router reference count on the gateway */
353 lnet_rtr_addref_locked(route->lr_gateway);
357 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
358 unsigned int priority)
360 struct list_head *route_entry;
361 struct lnet_remotenet *rnet;
362 struct lnet_remotenet *rnet2;
363 struct lnet_route *route;
364 struct lnet_peer_ni *lpni;
365 struct lnet_peer *gw;
369 CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
370 libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
372 if (gateway == LNET_NID_ANY ||
373 LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
374 net == LNET_NIDNET(LNET_NID_ANY) ||
375 LNET_NETTYP(net) == LOLND ||
376 LNET_NIDNET(gateway) == net ||
377 (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
380 /* it's a local network */
381 if (lnet_islocalnet(net))
384 /* Assume net, route, all new */
385 LIBCFS_ALLOC(route, sizeof(*route));
386 LIBCFS_ALLOC(rnet, sizeof(*rnet));
387 if (route == NULL || rnet == NULL) {
388 CERROR("Out of memory creating route %s %d %s\n",
389 libcfs_net2str(net), hops, libcfs_nid2str(gateway));
391 LIBCFS_FREE(route, sizeof(*route));
393 LIBCFS_FREE(rnet, sizeof(*rnet));
397 INIT_LIST_HEAD(&rnet->lrn_routes);
399 /* store the local and remote net that the route represents */
400 route->lr_lnet = LNET_NIDNET(gateway);
402 route->lr_priority = priority;
403 route->lr_hops = hops;
405 lnet_net_lock(LNET_LOCK_EX);
408 * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
409 * lose that once we're done
411 lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
413 lnet_net_unlock(LNET_LOCK_EX);
415 LIBCFS_FREE(route, sizeof(*route));
416 LIBCFS_FREE(rnet, sizeof(*rnet));
419 CERROR("Error %d creating route %s %d %s\n", rc,
420 libcfs_net2str(net), hops,
421 libcfs_nid2str(gateway));
425 LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
426 gw = lpni->lpni_peer_net->lpn_peer;
428 route->lr_gateway = gw;
430 rnet2 = lnet_find_rnet_locked(net);
433 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
437 /* Search for a duplicate route (it's a NOOP if it is) */
439 list_for_each(route_entry, &rnet2->lrn_routes) {
440 struct lnet_route *route2;
442 route2 = list_entry(route_entry, struct lnet_route, lr_list);
443 if (route2->lr_gateway == route->lr_gateway) {
448 /* our lookups must be true */
449 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
453 * It is possible to add multiple routes through the same peer,
454 * but it'll be using a different NID of that peer. When the
455 * gateway is discovered, discovery will consolidate the different
456 * peers into one peer. In this case the discovery code will have
457 * to move the routes from the peer that's being deleted to the
458 * consolidated peer lp_routes list
461 lnet_add_route_to_rnet(rnet2, route);
464 * get rid of the reference on the lpni.
466 lnet_peer_ni_decref_locked(lpni);
467 lnet_net_unlock(LNET_LOCK_EX);
473 LIBCFS_FREE(route, sizeof(*route));
477 LIBCFS_FREE(rnet, sizeof(*rnet));
479 /* kick start the monitor thread to handle the added route */
480 wake_up(&the_lnet.ln_mt_waitq);
486 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
487 struct list_head *zombies)
489 struct lnet_peer *gateway;
490 struct lnet_route *route;
491 struct lnet_route *tmp;
493 list_for_each_entry_safe(route, tmp, route_list, lr_list) {
494 gateway = route->lr_gateway;
495 if (gw_nid != LNET_NID_ANY &&
496 gw_nid != gateway->lp_primary_nid)
500 * move to zombie to delete outside the lock
501 * Note that this function is called with the
502 * ln_api_mutex held as well as the exclusive net
503 * lock. Adding to the remote net list happens
504 * under the same conditions. Same goes for the
505 * gateway router list
507 list_move(&route->lr_list, zombies);
508 the_lnet.ln_remote_nets_version++;
510 list_del(&route->lr_gwlist);
511 lnet_rtr_decref_locked(gateway);
516 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
518 struct list_head rnet_zombies;
519 struct lnet_remotenet *rnet;
520 struct lnet_remotenet *tmp;
521 struct list_head *rn_list;
522 struct lnet_peer_ni *lpni;
523 struct lnet_route *route;
524 struct list_head zombies;
525 struct lnet_peer *lp;
528 INIT_LIST_HEAD(&rnet_zombies);
529 INIT_LIST_HEAD(&zombies);
531 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
532 libcfs_net2str(net), libcfs_nid2str(gw_nid));
534 /* NB Caller may specify either all routes via the given gateway
535 * or a specific route entry actual NIDs) */
537 lnet_net_lock(LNET_LOCK_EX);
539 lpni = lnet_find_peer_ni_locked(gw_nid);
541 lp = lpni->lpni_peer_net->lpn_peer;
543 gw_nid = lp->lp_primary_nid;
544 lnet_peer_ni_decref_locked(lpni);
547 if (net != LNET_NIDNET(LNET_NID_ANY)) {
548 rnet = lnet_find_rnet_locked(net);
550 lnet_net_unlock(LNET_LOCK_EX);
553 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
555 if (list_empty(&rnet->lrn_routes))
556 list_move(&rnet->lrn_list, &rnet_zombies);
560 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
561 rn_list = &the_lnet.ln_remote_nets_hash[i];
563 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
564 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
566 if (list_empty(&rnet->lrn_routes))
567 list_move(&rnet->lrn_list, &rnet_zombies);
572 lnet_net_unlock(LNET_LOCK_EX);
574 while (!list_empty(&zombies)) {
575 route = list_first_entry(&zombies, struct lnet_route, lr_list);
576 list_del(&route->lr_list);
577 LIBCFS_FREE(route, sizeof(*route));
580 while (!list_empty(&rnet_zombies)) {
581 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
583 list_del(&rnet->lrn_list);
584 LIBCFS_FREE(rnet, sizeof(*rnet));
591 lnet_destroy_routes (void)
593 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
596 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
598 struct lnet_rtrbufpool *rbp;
599 int i, rc = -ENOENT, j;
601 if (the_lnet.ln_rtrpools == NULL)
605 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
610 for (j = 0; j < LNET_NRBPOOLS; j++) {
611 pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
612 pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
613 pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
614 pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
621 lnet_net_lock(LNET_LOCK_EX);
622 pool_cfg->pl_routing = the_lnet.ln_routing;
623 lnet_net_unlock(LNET_LOCK_EX);
629 lnet_get_route(int idx, __u32 *net, __u32 *hops,
630 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
632 struct list_head *e1;
633 struct list_head *e2;
634 struct lnet_remotenet *rnet;
635 struct lnet_route *route;
638 struct list_head *rn_list;
640 cpt = lnet_net_lock_current();
642 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
643 rn_list = &the_lnet.ln_remote_nets_hash[i];
644 list_for_each(e1, rn_list) {
645 rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
647 list_for_each(e2, &rnet->lrn_routes) {
648 route = list_entry(e2, struct lnet_route,
652 *net = rnet->lrn_net;
653 *hops = route->lr_hops;
654 *priority = route->lr_priority;
655 *gateway = route->lr_gateway->lp_primary_nid;
656 *alive = lnet_is_route_alive(route);
657 lnet_net_unlock(cpt);
664 lnet_net_unlock(cpt);
669 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
671 struct lnet_ni_status *stat;
675 __swab32s(&pbuf->pb_info.pi_magic);
676 __swab32s(&pbuf->pb_info.pi_features);
677 __swab32s(&pbuf->pb_info.pi_pid);
678 __swab32s(&pbuf->pb_info.pi_nnis);
679 nnis = pbuf->pb_info.pi_nnis;
680 if (nnis > pbuf->pb_nnis)
681 nnis = pbuf->pb_nnis;
682 for (i = 0; i < nnis; i++) {
683 stat = &pbuf->pb_info.pi_ni[i];
684 __swab64s(&stat->ns_nid);
685 __swab32s(&stat->ns_status);
694 lnet_parse_rc_info(struct lnet_rc_data *rcd)
700 lnet_router_checker_event(struct lnet_event *event)
702 struct lnet_rc_data *rcd = event->md.user_ptr;
703 struct lnet_peer_ni *lp;
705 LASSERT(rcd != NULL);
707 if (event->unlinked) {
708 LNetInvalidateMDHandle(&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->lpni_cpt);
722 if (!lnet_isrouter(lp) || lp->lpni_rcd != rcd) {
723 /* ignore if no longer a router or rcd is replaced */
727 if (event->type == LNET_EVENT_SEND) {
728 if (event->status == 0)
732 /* LNET_EVENT_REPLY */
733 /* A successful REPLY means the router is up. If _any_ comms
734 * to the router fail I assume it's down (this will happen if
735 * we ping alive routers to try to detect router death before
736 * apps get burned). */
738 lnet_notify_locked(lp, 1, !event->status, ktime_get_seconds());
739 /* The router checker will wake up very shortly and do the
740 * actual notification.
741 * XXX If 'lp' stops being a router before then, it will still
742 * have the notification pending!!! */
744 if (avoid_asym_router_failure && event->status == 0)
745 lnet_parse_rc_info(rcd);
748 lnet_net_unlock(lp->lpni_cpt);
752 lnet_wait_known_routerstate(void)
754 struct lnet_peer *rtr;
755 struct list_head *entry;
758 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
761 int cpt = lnet_net_lock_current();
764 list_for_each(entry, &the_lnet.ln_routers) {
765 rtr = list_entry(entry, struct lnet_peer,
768 spin_lock(&rtr->lp_lock);
770 if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
772 spin_unlock(&rtr->lp_lock);
775 spin_unlock(&rtr->lp_lock);
778 lnet_net_unlock(cpt);
783 set_current_state(TASK_UNINTERRUPTIBLE);
784 schedule_timeout(cfs_time_seconds(1));
788 /* TODO: reimplement */
790 lnet_router_ni_update_locked(struct lnet_peer_ni *gw, __u32 net)
792 struct lnet_route *rte;
793 struct lnet_peer *lp;
795 if ((gw->lpni_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0)
796 lp = gw->lpni_peer_net->lpn_peer;
800 list_for_each_entry(rte, &lp->lp_routes, lr_gwlist) {
801 if (rte->lr_net == net) {
809 lnet_update_ni_status_locked(void)
811 struct lnet_ni *ni = NULL;
815 LASSERT(the_lnet.ln_routing);
817 timeout = router_ping_timeout +
818 MAX(live_router_check_interval, dead_router_check_interval);
820 now = ktime_get_real_seconds();
821 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
822 if (ni->ni_net->net_lnd->lnd_type == LOLND)
825 if (now < ni->ni_last_alive + timeout)
829 /* re-check with lock */
830 if (now < ni->ni_last_alive + timeout) {
835 LASSERT(ni->ni_status != NULL);
837 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
838 CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
839 libcfs_nid2str(ni->ni_nid), timeout);
840 /* NB: so far, this is the only place to set
841 * NI status to "down" */
842 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
848 int lnet_router_pre_mt_start(void)
852 if (check_routers_before_use &&
853 dead_router_check_interval <= 0) {
854 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
855 " set if 'check_routers_before_use' is set"
860 rc = LNetEQAlloc(0, lnet_router_checker_event, &the_lnet.ln_rc_eqh);
862 CERROR("Can't allocate EQ(0): %d\n", rc);
869 void lnet_router_post_mt_start(void)
871 if (check_routers_before_use) {
872 /* Note that a helpful side-effect of pinging all known routers
873 * at startup is that it makes them drop stale connections they
874 * may have to a previous instance of me. */
875 lnet_wait_known_routerstate();
880 lnet_router_cleanup(void)
884 rc = LNetEQFree(the_lnet.ln_rc_eqh);
890 lnet_prune_rc_data(int wait_unlink)
892 wait_unlink = wait_unlink;
896 * This function is called from the monitor thread to check if there are
897 * any active routers that need to be checked.
900 lnet_router_checker_active(void)
902 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
905 /* Router Checker thread needs to run when routing is enabled in
906 * order to call lnet_update_ni_status_locked() */
907 if (the_lnet.ln_routing)
910 /* if there are routers that need to be cleaned up then do so */
911 if (!list_empty(&the_lnet.ln_rcd_deathrow) ||
912 !list_empty(&the_lnet.ln_rcd_zombie))
915 return !list_empty(&the_lnet.ln_routers) &&
916 (live_router_check_interval > 0 ||
917 dead_router_check_interval > 0);
921 lnet_check_routers(void)
923 struct lnet_peer *rtr;
924 struct list_head *entry;
928 cpt = lnet_net_lock_current();
930 version = the_lnet.ln_routers_version;
932 list_for_each(entry, &the_lnet.ln_routers) {
933 rtr = list_entry(entry, struct lnet_peer,
936 /* TODO use discovery to determine if router is alive */
938 /* NB dropped lock */
939 if (version != the_lnet.ln_routers_version) {
940 /* the routers list has changed */
945 if (the_lnet.ln_routing)
946 lnet_update_ni_status_locked();
948 lnet_net_unlock(cpt);
950 lnet_prune_rc_data(0); /* don't wait for UNLINK */
954 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
956 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
958 while (--npages >= 0)
959 __free_page(rb->rb_kiov[npages].kiov_page);
964 static struct lnet_rtrbuf *
965 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
967 int npages = rbp->rbp_npages;
968 int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
970 struct lnet_rtrbuf *rb;
973 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
979 for (i = 0; i < npages; i++) {
980 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
981 GFP_KERNEL | __GFP_ZERO);
984 __free_page(rb->rb_kiov[i].kiov_page);
990 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
991 rb->rb_kiov[i].kiov_offset = 0;
992 rb->rb_kiov[i].kiov_page = page;
999 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1001 int npages = rbp->rbp_npages;
1002 struct lnet_rtrbuf *rb;
1003 struct list_head tmp;
1005 if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1008 INIT_LIST_HEAD(&tmp);
1011 list_splice_init(&rbp->rbp_msgs, &tmp);
1012 lnet_drop_routed_msgs_locked(&tmp, cpt);
1013 list_splice_init(&rbp->rbp_bufs, &tmp);
1014 rbp->rbp_req_nbuffers = 0;
1015 rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1016 rbp->rbp_mincredits = 0;
1017 lnet_net_unlock(cpt);
1019 /* Free buffers on the free list. */
1020 while (!list_empty(&tmp)) {
1021 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1022 list_del(&rb->rb_list);
1023 lnet_destroy_rtrbuf(rb, npages);
1028 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1030 struct list_head rb_list;
1031 struct lnet_rtrbuf *rb;
1033 int num_buffers = 0;
1035 int npages = rbp->rbp_npages;
1038 /* If we are called for less buffers than already in the pool, we
1039 * just lower the req_nbuffers number and excess buffers will be
1040 * thrown away as they are returned to the free list. Credits
1041 * then get adjusted as well.
1042 * If we already have enough buffers allocated to serve the
1043 * increase requested, then we can treat that the same way as we
1044 * do the decrease. */
1045 num_rb = nbufs - rbp->rbp_nbuffers;
1046 if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1047 rbp->rbp_req_nbuffers = nbufs;
1048 lnet_net_unlock(cpt);
1051 /* store the older value of rbp_req_nbuffers and then set it to
1052 * the new request to prevent lnet_return_rx_credits_locked() from
1053 * freeing buffers that we need to keep around */
1054 old_req_nbufs = rbp->rbp_req_nbuffers;
1055 rbp->rbp_req_nbuffers = nbufs;
1056 lnet_net_unlock(cpt);
1058 INIT_LIST_HEAD(&rb_list);
1060 /* allocate the buffers on a local list first. If all buffers are
1061 * allocated successfully then join this list to the rbp buffer
1062 * list. If not then free all allocated buffers. */
1063 while (num_rb-- > 0) {
1064 rb = lnet_new_rtrbuf(rbp, cpt);
1066 CERROR("Failed to allocate %d route bufs of %d pages\n",
1070 rbp->rbp_req_nbuffers = old_req_nbufs;
1071 lnet_net_unlock(cpt);
1076 list_add(&rb->rb_list, &rb_list);
1082 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1083 rbp->rbp_nbuffers += num_buffers;
1084 rbp->rbp_credits += num_buffers;
1085 rbp->rbp_mincredits = rbp->rbp_credits;
1086 /* We need to schedule blocked msg using the newly
1088 while (!list_empty(&rbp->rbp_bufs) &&
1089 !list_empty(&rbp->rbp_msgs))
1090 lnet_schedule_blocked_locked(rbp);
1092 lnet_net_unlock(cpt);
1097 while (!list_empty(&rb_list)) {
1098 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1099 list_del(&rb->rb_list);
1100 lnet_destroy_rtrbuf(rb, npages);
1107 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1109 INIT_LIST_HEAD(&rbp->rbp_msgs);
1110 INIT_LIST_HEAD(&rbp->rbp_bufs);
1112 rbp->rbp_npages = npages;
1113 rbp->rbp_credits = 0;
1114 rbp->rbp_mincredits = 0;
1118 lnet_rtrpools_free(int keep_pools)
1120 struct lnet_rtrbufpool *rtrp;
1123 if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1126 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1127 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1128 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1129 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1133 cfs_percpt_free(the_lnet.ln_rtrpools);
1134 the_lnet.ln_rtrpools = NULL;
1139 lnet_nrb_tiny_calculate(void)
1141 int nrbs = LNET_NRB_TINY;
1143 if (tiny_router_buffers < 0) {
1144 LCONSOLE_ERROR_MSG(0x10c,
1145 "tiny_router_buffers=%d invalid when "
1146 "routing enabled\n", tiny_router_buffers);
1150 if (tiny_router_buffers > 0)
1151 nrbs = tiny_router_buffers;
1153 nrbs /= LNET_CPT_NUMBER;
1154 return max(nrbs, LNET_NRB_TINY_MIN);
1158 lnet_nrb_small_calculate(void)
1160 int nrbs = LNET_NRB_SMALL;
1162 if (small_router_buffers < 0) {
1163 LCONSOLE_ERROR_MSG(0x10c,
1164 "small_router_buffers=%d invalid when "
1165 "routing enabled\n", small_router_buffers);
1169 if (small_router_buffers > 0)
1170 nrbs = small_router_buffers;
1172 nrbs /= LNET_CPT_NUMBER;
1173 return max(nrbs, LNET_NRB_SMALL_MIN);
1177 lnet_nrb_large_calculate(void)
1179 int nrbs = LNET_NRB_LARGE;
1181 if (large_router_buffers < 0) {
1182 LCONSOLE_ERROR_MSG(0x10c,
1183 "large_router_buffers=%d invalid when "
1184 "routing enabled\n", large_router_buffers);
1188 if (large_router_buffers > 0)
1189 nrbs = large_router_buffers;
1191 nrbs /= LNET_CPT_NUMBER;
1192 return max(nrbs, LNET_NRB_LARGE_MIN);
1196 lnet_rtrpools_alloc(int im_a_router)
1198 struct lnet_rtrbufpool *rtrp;
1205 if (!strcmp(forwarding, "")) {
1206 /* not set either way */
1209 } else if (!strcmp(forwarding, "disabled")) {
1210 /* explicitly disabled */
1212 } else if (!strcmp(forwarding, "enabled")) {
1213 /* explicitly enabled */
1215 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1216 "'enabled' or 'disabled'\n");
1220 nrb_tiny = lnet_nrb_tiny_calculate();
1224 nrb_small = lnet_nrb_small_calculate();
1228 nrb_large = lnet_nrb_large_calculate();
1232 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1234 sizeof(struct lnet_rtrbufpool));
1235 if (the_lnet.ln_rtrpools == NULL) {
1236 LCONSOLE_ERROR_MSG(0x10c,
1237 "Failed to initialize router buffe pool\n");
1241 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1242 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1243 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1248 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1249 LNET_NRB_SMALL_PAGES);
1250 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1255 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1256 LNET_NRB_LARGE_PAGES);
1257 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1263 lnet_net_lock(LNET_LOCK_EX);
1264 the_lnet.ln_routing = 1;
1265 lnet_net_unlock(LNET_LOCK_EX);
1266 wake_up(&the_lnet.ln_mt_waitq);
1270 lnet_rtrpools_free(0);
1275 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1280 struct lnet_rtrbufpool *rtrp;
1282 /* If the provided values for each buffer pool are different than the
1283 * configured values, we need to take action. */
1285 tiny_router_buffers = tiny;
1286 nrb = lnet_nrb_tiny_calculate();
1287 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1288 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1295 small_router_buffers = small;
1296 nrb = lnet_nrb_small_calculate();
1297 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1298 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1305 large_router_buffers = large;
1306 nrb = lnet_nrb_large_calculate();
1307 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1308 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1319 lnet_rtrpools_adjust(int tiny, int small, int large)
1321 /* this function doesn't revert the changes if adding new buffers
1322 * failed. It's up to the user space caller to revert the
1325 if (!the_lnet.ln_routing)
1328 return lnet_rtrpools_adjust_helper(tiny, small, large);
1332 lnet_rtrpools_enable(void)
1336 if (the_lnet.ln_routing)
1339 if (the_lnet.ln_rtrpools == NULL)
1340 /* If routing is turned off, and we have never
1341 * initialized the pools before, just call the
1342 * standard buffer pool allocation routine as
1343 * if we are just configuring this for the first
1345 rc = lnet_rtrpools_alloc(1);
1347 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1351 lnet_net_lock(LNET_LOCK_EX);
1352 the_lnet.ln_routing = 1;
1354 the_lnet.ln_ping_target->pb_info.pi_features &=
1355 ~LNET_PING_FEAT_RTE_DISABLED;
1356 lnet_net_unlock(LNET_LOCK_EX);
1362 lnet_rtrpools_disable(void)
1364 if (!the_lnet.ln_routing)
1367 lnet_net_lock(LNET_LOCK_EX);
1368 the_lnet.ln_routing = 0;
1369 the_lnet.ln_ping_target->pb_info.pi_features |=
1370 LNET_PING_FEAT_RTE_DISABLED;
1372 tiny_router_buffers = 0;
1373 small_router_buffers = 0;
1374 large_router_buffers = 0;
1375 lnet_net_unlock(LNET_LOCK_EX);
1376 lnet_rtrpools_free(1);
1380 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, int alive, time64_t when)
1382 struct lnet_peer_ni *lp = NULL;
1383 time64_t now = ktime_get_seconds();
1384 int cpt = lnet_cpt_of_nid(nid, ni);
1386 LASSERT (!in_interrupt ());
1388 CDEBUG (D_NET, "%s notifying %s: %s\n",
1389 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1390 libcfs_nid2str(nid),
1391 alive ? "up" : "down");
1394 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1395 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1396 libcfs_nid2str(nid), alive ? "birth" : "death",
1397 libcfs_nid2str(ni->ni_nid));
1401 /* can't do predictions... */
1403 CWARN("Ignoring prediction from %s of %s %s "
1404 "%lld seconds in the future\n",
1405 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1406 libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1410 if (ni != NULL && !alive && /* LND telling me she's down */
1411 !auto_down) { /* auto-down disabled */
1412 CDEBUG(D_NET, "Auto-down disabled\n");
1418 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1419 lnet_net_unlock(cpt);
1423 lp = lnet_find_peer_ni_locked(nid);
1426 lnet_net_unlock(cpt);
1427 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1432 * It is possible for this function to be called for the same peer
1433 * but with different NIs. We want to synchronize the notification
1434 * between the different calls. So we will use the lpni_cpt to
1435 * grab the net lock.
1437 if (lp->lpni_cpt != cpt) {
1438 lnet_net_unlock(cpt);
1443 /* We can't fully trust LND on reporting exact peer last_alive
1444 * if he notifies us about dead peer. For example ksocklnd can
1445 * call us with when == _time_when_the_node_was_booted_ if
1446 * no connections were successfully established */
1447 if (ni != NULL && !alive && when < lp->lpni_last_alive)
1448 when = lp->lpni_last_alive;
1450 lnet_notify_locked(lp, ni == NULL, alive, when);
1453 lnet_ni_notify_locked(ni, lp);
1455 lnet_peer_ni_decref_locked(lp);
1457 lnet_net_unlock(cpt);
1460 EXPORT_SYMBOL(lnet_notify);