Whamcloud - gitweb
LU-1095 llite: improve max_readahead console messages
[fs/lustre-release.git] / lnet / lnet / router.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2014, Intel Corporation.
5  *
6  *   This file is part of Portals
7  *   http://sourceforge.net/projects/sandiaportals/
8  *
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.
12  *
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.
17  *
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.
21  *
22  */
23
24 #define DEBUG_SUBSYSTEM S_LNET
25 #include <lnet/lib-lnet.h>
26
27 #if defined(__KERNEL__) && defined(LNET_ROUTER)
28
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_SMALL_PAGES    1
34 #define LNET_NRB_LARGE_MIN      256     /* min value for each CPT */
35 #define LNET_NRB_LARGE          (LNET_NRB_LARGE_MIN * 4)
36 #define LNET_NRB_LARGE_PAGES    ((LNET_MTU + PAGE_CACHE_SIZE - 1) >> \
37                                   PAGE_CACHE_SHIFT)
38
39 static char *forwarding = "";
40 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
41                 "Explicitly enable/disable forwarding between networks");
42
43 static int tiny_router_buffers;
44 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
45                 "# of 0 payload messages to buffer in the router");
46 static int small_router_buffers;
47 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
48                 "# of small (1 page) messages to buffer in the router");
49 static int large_router_buffers;
50 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
51                 "# of large messages to buffer in the router");
52 static int peer_buffer_credits = 0;
53 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
54                 "# router buffer credits per peer");
55
56 static int auto_down = 1;
57 CFS_MODULE_PARM(auto_down, "i", int, 0444,
58                 "Automatically mark peers down on comms error");
59
60 int
61 lnet_peer_buffer_credits(lnet_ni_t *ni)
62 {
63         /* NI option overrides LNet default */
64         if (ni->ni_peerrtrcredits > 0)
65                 return ni->ni_peerrtrcredits;
66         if (peer_buffer_credits > 0)
67                 return peer_buffer_credits;
68
69         /* As an approximation, allow this peer the same number of router
70          * buffers as it is allowed outstanding sends */
71         return ni->ni_peertxcredits;
72 }
73
74 /* forward ref's */
75 static int lnet_router_checker(void *);
76 #else
77
78 int
79 lnet_peer_buffer_credits(lnet_ni_t *ni)
80 {
81         return 0;
82 }
83
84 #endif
85
86 static int check_routers_before_use = 0;
87 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
88                 "Assume routers are down and ping them before use");
89
90 int avoid_asym_router_failure = 1;
91 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0644,
92                 "Avoid asymmetrical router failures (0 to disable)");
93
94 static int dead_router_check_interval = 60;
95 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0644,
96                 "Seconds between dead router health checks (<= 0 to disable)");
97
98 static int live_router_check_interval = 60;
99 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0644,
100                 "Seconds between live router health checks (<= 0 to disable)");
101
102 static int router_ping_timeout = 50;
103 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0644,
104                 "Seconds to wait for the reply to a router health query");
105
106 int
107 lnet_peers_start_down(void)
108 {
109         return check_routers_before_use;
110 }
111
112 void
113 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
114 {
115         if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
116                 CDEBUG(D_NET, "Out of date\n");
117                 return;
118         }
119
120         lp->lp_timestamp = when;                /* update timestamp */
121         lp->lp_ping_deadline = 0;               /* disable ping timeout */
122
123         if (lp->lp_alive_count != 0 &&          /* got old news */
124             (!lp->lp_alive) == (!alive)) {      /* new date for old news */
125                 CDEBUG(D_NET, "Old news\n");
126                 return;
127         }
128
129         /* Flag that notification is outstanding */
130
131         lp->lp_alive_count++;
132         lp->lp_alive = !(!alive);               /* 1 bit! */
133         lp->lp_notify = 1;
134         lp->lp_notifylnd |= notifylnd;
135         if (lp->lp_alive)
136                 lp->lp_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
137
138         CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
139 }
140
141 static void
142 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
143 {
144         int        alive;
145         int        notifylnd;
146
147         /* Notify only in 1 thread at any time to ensure ordered notification.
148          * NB individual events can be missed; the only guarantee is that you
149          * always get the most recent news */
150
151         if (lp->lp_notifying)
152                 return;
153
154         lp->lp_notifying = 1;
155
156         while (lp->lp_notify) {
157                 alive     = lp->lp_alive;
158                 notifylnd = lp->lp_notifylnd;
159
160                 lp->lp_notifylnd = 0;
161                 lp->lp_notify    = 0;
162
163                 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
164                         lnet_net_unlock(lp->lp_cpt);
165
166                         /* A new notification could happen now; I'll handle it
167                          * when control returns to me */
168
169                         (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
170
171                         lnet_net_lock(lp->lp_cpt);
172                 }
173         }
174
175         lp->lp_notifying = 0;
176 }
177
178
179 static void
180 lnet_rtr_addref_locked(lnet_peer_t *lp)
181 {
182         LASSERT(lp->lp_refcount > 0);
183         LASSERT(lp->lp_rtr_refcount >= 0);
184
185         /* lnet_net_lock must be exclusively locked */
186         lp->lp_rtr_refcount++;
187         if (lp->lp_rtr_refcount == 1) {
188                 struct list_head *pos;
189
190                 /* a simple insertion sort */
191                 list_for_each_prev(pos, &the_lnet.ln_routers) {
192                         lnet_peer_t *rtr = list_entry(pos, lnet_peer_t,
193                                                       lp_rtr_list);
194
195                         if (rtr->lp_nid < lp->lp_nid)
196                                 break;
197                 }
198
199                 list_add(&lp->lp_rtr_list, pos);
200                 /* addref for the_lnet.ln_routers */
201                 lnet_peer_addref_locked(lp);
202                 the_lnet.ln_routers_version++;
203         }
204 }
205
206 static void
207 lnet_rtr_decref_locked(lnet_peer_t *lp)
208 {
209         LASSERT(lp->lp_refcount > 0);
210         LASSERT(lp->lp_rtr_refcount > 0);
211
212         /* lnet_net_lock must be exclusively locked */
213         lp->lp_rtr_refcount--;
214         if (lp->lp_rtr_refcount == 0) {
215                 LASSERT(list_empty(&lp->lp_routes));
216
217                 if (lp->lp_rcd != NULL) {
218                         list_add(&lp->lp_rcd->rcd_list,
219                                  &the_lnet.ln_rcd_deathrow);
220                         lp->lp_rcd = NULL;
221                 }
222
223                 list_del(&lp->lp_rtr_list);
224                 /* decref for the_lnet.ln_routers */
225                 lnet_peer_decref_locked(lp);
226                 the_lnet.ln_routers_version++;
227         }
228 }
229
230 lnet_remotenet_t *
231 lnet_find_net_locked (__u32 net)
232 {
233         lnet_remotenet_t *rnet;
234         struct list_head *tmp;
235         struct list_head *rn_list;
236
237         LASSERT(!the_lnet.ln_shutdown);
238
239         rn_list = lnet_net2rnethash(net);
240         list_for_each(tmp, rn_list) {
241                 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
242
243                 if (rnet->lrn_net == net)
244                         return rnet;
245         }
246         return NULL;
247 }
248
249 static void lnet_shuffle_seed(void)
250 {
251         static int seeded = 0;
252         int lnd_type, seed[2];
253         struct timeval tv;
254         lnet_ni_t *ni;
255         struct list_head *tmp;
256
257         if (seeded)
258                 return;
259
260         cfs_get_random_bytes(seed, sizeof(seed));
261
262         /* Nodes with small feet have little entropy
263          * the NID for this node gives the most entropy in the low bits */
264         list_for_each(tmp, &the_lnet.ln_nis) {
265                 ni = list_entry(tmp, lnet_ni_t, ni_list);
266                 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
267
268                 if (lnd_type != LOLND)
269                         seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
270         }
271
272         do_gettimeofday(&tv);
273         cfs_srand(tv.tv_sec ^ seed[0], tv.tv_usec ^ seed[1]);
274         seeded = 1;
275         return;
276 }
277
278 /* NB expects LNET_LOCK held */
279 static void
280 lnet_add_route_to_rnet(lnet_remotenet_t *rnet, lnet_route_t *route)
281 {
282         unsigned int      len = 0;
283         unsigned int      offset = 0;
284         struct list_head *e;
285
286         lnet_shuffle_seed();
287
288         list_for_each(e, &rnet->lrn_routes) {
289                 len++;
290         }
291
292         /* len+1 positions to add a new entry, also prevents division by 0 */
293         offset = cfs_rand() % (len + 1);
294         list_for_each(e, &rnet->lrn_routes) {
295                 if (offset == 0)
296                         break;
297                 offset--;
298         }
299         list_add(&route->lr_list, e);
300         list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
301
302         the_lnet.ln_remote_nets_version++;
303         lnet_rtr_addref_locked(route->lr_gateway);
304 }
305
306 int
307 lnet_add_route(__u32 net, unsigned int hops, lnet_nid_t gateway,
308                unsigned int priority)
309 {
310         struct list_head        *e;
311         lnet_remotenet_t        *rnet;
312         lnet_remotenet_t        *rnet2;
313         lnet_route_t            *route;
314         lnet_ni_t               *ni;
315         int                     add_route;
316         int                     rc;
317
318         CDEBUG(D_NET, "Add route: net %s hops %u priority %u gw %s\n",
319                libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
320
321         if (gateway == LNET_NID_ANY ||
322             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
323             net == LNET_NIDNET(LNET_NID_ANY) ||
324             LNET_NETTYP(net) == LOLND ||
325             LNET_NIDNET(gateway) == net ||
326             hops < 1 || hops > 255)
327                 return -EINVAL;
328
329         if (lnet_islocalnet(net))       /* it's a local network */
330                 return -EEXIST;
331
332         /* Assume net, route, all new */
333         LIBCFS_ALLOC(route, sizeof(*route));
334         LIBCFS_ALLOC(rnet, sizeof(*rnet));
335         if (route == NULL || rnet == NULL) {
336                 CERROR("Out of memory creating route %s %d %s\n",
337                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
338                 if (route != NULL)
339                         LIBCFS_FREE(route, sizeof(*route));
340                 if (rnet != NULL)
341                         LIBCFS_FREE(rnet, sizeof(*rnet));
342                 return -ENOMEM;
343         }
344
345         INIT_LIST_HEAD(&rnet->lrn_routes);
346         rnet->lrn_net = net;
347         route->lr_hops = hops;
348         route->lr_net = net;
349         route->lr_priority = priority;
350
351         lnet_net_lock(LNET_LOCK_EX);
352
353         rc = lnet_nid2peer_locked(&route->lr_gateway, gateway, LNET_LOCK_EX);
354         if (rc != 0) {
355                 lnet_net_unlock(LNET_LOCK_EX);
356
357                 LIBCFS_FREE(route, sizeof(*route));
358                 LIBCFS_FREE(rnet, sizeof(*rnet));
359
360                 if (rc == -EHOSTUNREACH) /* gateway is not on a local net. */
361                         return rc;       /* ignore the route entry */
362                 CERROR("Error %d creating route %s %d %s\n", rc,
363                         libcfs_net2str(net), hops,
364                         libcfs_nid2str(gateway));
365                 return rc;
366         }
367
368         LASSERT(!the_lnet.ln_shutdown);
369
370         rnet2 = lnet_find_net_locked(net);
371         if (rnet2 == NULL) {
372                 /* new network */
373                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
374                 rnet2 = rnet;
375         }
376
377         /* Search for a duplicate route (it's a NOOP if it is) */
378         add_route = 1;
379         list_for_each(e, &rnet2->lrn_routes) {
380                 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
381
382                 if (route2->lr_gateway == route->lr_gateway) {
383                         add_route = 0;
384                         break;
385                 }
386
387                 /* our lookups must be true */
388                 LASSERT(route2->lr_gateway->lp_nid != gateway);
389         }
390
391         if (add_route) {
392                 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
393                 lnet_add_route_to_rnet(rnet2, route);
394
395                 ni = route->lr_gateway->lp_ni;
396                 lnet_net_unlock(LNET_LOCK_EX);
397
398                 /* XXX Assume alive */
399                 if (ni->ni_lnd->lnd_notify != NULL)
400                         (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
401
402                 lnet_net_lock(LNET_LOCK_EX);
403         }
404
405         /* -1 for notify or !add_route */
406         lnet_peer_decref_locked(route->lr_gateway);
407         lnet_net_unlock(LNET_LOCK_EX);
408
409         rc = 0;
410
411         if (!add_route) {
412                 rc = -EEXIST;
413                 LIBCFS_FREE(route, sizeof(*route));
414         }
415
416         if (rnet != rnet2)
417                 LIBCFS_FREE(rnet, sizeof(*rnet));
418
419         return rc;
420 }
421
422 int
423 lnet_check_routes(void)
424 {
425         lnet_remotenet_t *rnet;
426         lnet_route_t     *route;
427         lnet_route_t     *route2;
428         struct list_head *e1;
429         struct list_head *e2;
430         int               cpt;
431         struct list_head *rn_list;
432         int               i;
433
434         cpt = lnet_net_lock_current();
435
436         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
437                 rn_list = &the_lnet.ln_remote_nets_hash[i];
438                 list_for_each(e1, rn_list) {
439                         rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
440
441                         route2 = NULL;
442                         list_for_each(e2, &rnet->lrn_routes) {
443                                 lnet_nid_t      nid1;
444                                 lnet_nid_t      nid2;
445                                 int             net;
446
447                                 route = list_entry(e2, lnet_route_t,
448                                                    lr_list);
449
450                                 if (route2 == NULL) {
451                                         route2 = route;
452                                         continue;
453                                 }
454
455                                 if (route->lr_gateway->lp_ni ==
456                                     route2->lr_gateway->lp_ni)
457                                         continue;
458
459                                 nid1 = route->lr_gateway->lp_nid;
460                                 nid2 = route2->lr_gateway->lp_nid;
461                                 net = rnet->lrn_net;
462
463                                 lnet_net_unlock(cpt);
464
465                                 CERROR("Routes to %s via %s and %s not "
466                                        "supported\n",
467                                        libcfs_net2str(net),
468                                        libcfs_nid2str(nid1),
469                                        libcfs_nid2str(nid2));
470                                 return -EINVAL;
471                         }
472                 }
473         }
474
475         lnet_net_unlock(cpt);
476         return 0;
477 }
478
479 int
480 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
481 {
482         struct lnet_peer        *gateway;
483         lnet_remotenet_t        *rnet;
484         lnet_route_t            *route;
485         struct list_head        *e1;
486         struct list_head        *e2;
487         int                     rc = -ENOENT;
488         struct list_head        *rn_list;
489         int                     idx = 0;
490
491         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
492                libcfs_net2str(net), libcfs_nid2str(gw_nid));
493
494         /* NB Caller may specify either all routes via the given gateway
495          * or a specific route entry actual NIDs) */
496
497         lnet_net_lock(LNET_LOCK_EX);
498         if (net == LNET_NIDNET(LNET_NID_ANY))
499                 rn_list = &the_lnet.ln_remote_nets_hash[0];
500         else
501                 rn_list = lnet_net2rnethash(net);
502
503 again:
504         list_for_each(e1, rn_list) {
505                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
506
507                 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
508                         net == rnet->lrn_net))
509                         continue;
510
511                 list_for_each(e2, &rnet->lrn_routes) {
512                         route = list_entry(e2, lnet_route_t, lr_list);
513
514                         gateway = route->lr_gateway;
515                         if (!(gw_nid == LNET_NID_ANY ||
516                               gw_nid == gateway->lp_nid))
517                                 continue;
518
519                         list_del(&route->lr_list);
520                         list_del(&route->lr_gwlist);
521                         the_lnet.ln_remote_nets_version++;
522
523                         if (list_empty(&rnet->lrn_routes))
524                                 list_del(&rnet->lrn_list);
525                         else
526                                 rnet = NULL;
527
528                         lnet_rtr_decref_locked(gateway);
529                         lnet_peer_decref_locked(gateway);
530
531                         lnet_net_unlock(LNET_LOCK_EX);
532
533                         LIBCFS_FREE(route, sizeof(*route));
534
535                         if (rnet != NULL)
536                                 LIBCFS_FREE(rnet, sizeof(*rnet));
537
538                         rc = 0;
539                         lnet_net_lock(LNET_LOCK_EX);
540                         goto again;
541                 }
542         }
543
544         if (net == LNET_NIDNET(LNET_NID_ANY) &&
545             ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
546                 rn_list = &the_lnet.ln_remote_nets_hash[idx];
547                 goto again;
548         }
549         lnet_net_unlock(LNET_LOCK_EX);
550
551         return rc;
552 }
553
554 void
555 lnet_destroy_routes (void)
556 {
557         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
558 }
559
560 int lnet_get_rtr_pool_cfg(int idx, struct lnet_ioctl_pool_cfg *pool_cfg)
561 {
562         int i, rc = -ENOENT, lidx, j;
563
564         if (the_lnet.ln_rtrpools == NULL)
565                 return rc;
566
567         for (i = 0; i < LNET_NRBPOOLS; i++) {
568                 lnet_rtrbufpool_t *rbp;
569
570                 lnet_net_lock(LNET_LOCK_EX);
571                 lidx = idx;
572                 cfs_percpt_for_each(rbp, j, the_lnet.ln_rtrpools) {
573                         if (lidx-- == 0) {
574                                 rc = 0;
575                                 pool_cfg->pl_pools[i].pl_npages =
576                                         rbp[i].rbp_npages;
577                                 pool_cfg->pl_pools[i].pl_nbuffers =
578                                         rbp[i].rbp_nbuffers;
579                                 pool_cfg->pl_pools[i].pl_credits =
580                                         rbp[i].rbp_credits;
581                                 pool_cfg->pl_pools[i].pl_mincredits =
582                                         rbp[i].rbp_mincredits;
583                                 break;
584                         }
585                 }
586                 lnet_net_unlock(LNET_LOCK_EX);
587         }
588
589         lnet_net_lock(LNET_LOCK_EX);
590         pool_cfg->pl_routing = the_lnet.ln_routing;
591         lnet_net_unlock(LNET_LOCK_EX);
592
593         return rc;
594 }
595
596 int
597 lnet_get_route(int idx, __u32 *net, __u32 *hops,
598                lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
599 {
600         struct list_head *e1;
601         struct list_head *e2;
602         lnet_remotenet_t *rnet;
603         lnet_route_t     *route;
604         int               cpt;
605         int               i;
606         struct list_head *rn_list;
607
608         cpt = lnet_net_lock_current();
609
610         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
611                 rn_list = &the_lnet.ln_remote_nets_hash[i];
612                 list_for_each(e1, rn_list) {
613                         rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
614
615                         list_for_each(e2, &rnet->lrn_routes) {
616                                 route = list_entry(e2, lnet_route_t,
617                                                    lr_list);
618
619                                 if (idx-- == 0) {
620                                         *net      = rnet->lrn_net;
621                                         *hops     = route->lr_hops;
622                                         *priority = route->lr_priority;
623                                         *gateway  = route->lr_gateway->lp_nid;
624                                         *alive    =
625                                                 route->lr_gateway->lp_alive &&
626                                                         !route->lr_downis;
627                                         lnet_net_unlock(cpt);
628                                         return 0;
629                                 }
630                         }
631                 }
632         }
633
634         lnet_net_unlock(cpt);
635         return -ENOENT;
636 }
637
638 void
639 lnet_swap_pinginfo(lnet_ping_info_t *info)
640 {
641         int               i;
642         lnet_ni_status_t *stat;
643
644         __swab32s(&info->pi_magic);
645         __swab32s(&info->pi_features);
646         __swab32s(&info->pi_pid);
647         __swab32s(&info->pi_nnis);
648         for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
649                 stat = &info->pi_ni[i];
650                 __swab64s(&stat->ns_nid);
651                 __swab32s(&stat->ns_status);
652         }
653         return;
654 }
655
656 /**
657  * parse router-checker pinginfo, record number of down NIs for remote
658  * networks on that router.
659  */
660 static void
661 lnet_parse_rc_info(lnet_rc_data_t *rcd)
662 {
663         lnet_ping_info_t        *info = rcd->rcd_pinginfo;
664         struct lnet_peer        *gw   = rcd->rcd_gateway;
665         lnet_route_t            *rte;
666
667         if (!gw->lp_alive)
668                 return;
669
670         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
671                 lnet_swap_pinginfo(info);
672
673         /* NB always racing with network! */
674         if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
675                 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
676                        libcfs_nid2str(gw->lp_nid), info->pi_magic);
677                 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
678                 return;
679         }
680
681         gw->lp_ping_feats = info->pi_features;
682         if ((gw->lp_ping_feats & LNET_PING_FEAT_MASK) == 0) {
683                 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
684                        libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
685                 return; /* nothing I can understand */
686         }
687
688         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) == 0)
689                 return; /* can't carry NI status info */
690
691         list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
692                 int     down = 0;
693                 int     up = 0;
694                 int     i;
695
696                 if ((gw->lp_ping_feats & LNET_PING_FEAT_RTE_DISABLED) != 0) {
697                         rte->lr_downis = 1;
698                         continue;
699                 }
700
701                 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
702                         lnet_ni_status_t *stat = &info->pi_ni[i];
703                         lnet_nid_t       nid = stat->ns_nid;
704
705                         if (nid == LNET_NID_ANY) {
706                                 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
707                                        libcfs_nid2str(gw->lp_nid));
708                                 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
709                                 return;
710                         }
711
712                         if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
713                                 continue;
714
715                         if (stat->ns_status == LNET_NI_STATUS_DOWN) {
716                                 down++;
717                                 continue;
718                         }
719
720                         if (stat->ns_status == LNET_NI_STATUS_UP) {
721                                 if (LNET_NIDNET(nid) == rte->lr_net) {
722                                         up = 1;
723                                         break;
724                                 }
725                                 continue;
726                         }
727
728                         CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
729                                libcfs_nid2str(gw->lp_nid), stat->ns_status);
730                         gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
731                         return;
732                 }
733
734                 if (up) { /* ignore downed NIs if NI for dest network is up */
735                         rte->lr_downis = 0;
736                         continue;
737                 }
738                 /* if @down is zero and this route is single-hop, it means
739                  * we can't find NI for target network */
740                 if (down == 0 && rte->lr_hops == 1)
741                         down = 1;
742
743                 rte->lr_downis = down;
744         }
745 }
746
747 static void
748 lnet_router_checker_event(lnet_event_t *event)
749 {
750         lnet_rc_data_t          *rcd = event->md.user_ptr;
751         struct lnet_peer        *lp;
752
753         LASSERT(rcd != NULL);
754
755         if (event->unlinked) {
756                 LNetInvalidateHandle(&rcd->rcd_mdh);
757                 return;
758         }
759
760         LASSERT(event->type == LNET_EVENT_SEND ||
761                 event->type == LNET_EVENT_REPLY);
762
763         lp = rcd->rcd_gateway;
764         LASSERT(lp != NULL);
765
766          /* NB: it's called with holding lnet_res_lock, we have a few
767           * places need to hold both locks at the same time, please take
768           * care of lock ordering */
769         lnet_net_lock(lp->lp_cpt);
770         if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
771                 /* ignore if no longer a router or rcd is replaced */
772                 goto out;
773         }
774
775         if (event->type == LNET_EVENT_SEND) {
776                 lp->lp_ping_notsent = 0;
777                 if (event->status == 0)
778                         goto out;
779         }
780
781         /* LNET_EVENT_REPLY */
782         /* A successful REPLY means the router is up.  If _any_ comms
783          * to the router fail I assume it's down (this will happen if
784          * we ping alive routers to try to detect router death before
785          * apps get burned). */
786
787         lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
788         /* The router checker will wake up very shortly and do the
789          * actual notification.
790          * XXX If 'lp' stops being a router before then, it will still
791          * have the notification pending!!! */
792
793         if (avoid_asym_router_failure && event->status == 0)
794                 lnet_parse_rc_info(rcd);
795
796  out:
797         lnet_net_unlock(lp->lp_cpt);
798 }
799
800 static void
801 lnet_wait_known_routerstate(void)
802 {
803         lnet_peer_t      *rtr;
804         struct list_head *entry;
805         int               all_known;
806
807         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
808
809         for (;;) {
810                 int cpt = lnet_net_lock_current();
811
812                 all_known = 1;
813                 list_for_each(entry, &the_lnet.ln_routers) {
814                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
815
816                         if (rtr->lp_alive_count == 0) {
817                                 all_known = 0;
818                                 break;
819                         }
820                 }
821
822                 lnet_net_unlock(cpt);
823
824                 if (all_known)
825                         return;
826
827 #ifndef __KERNEL__
828                 lnet_router_checker();
829 #endif
830                 cfs_pause(cfs_time_seconds(1));
831         }
832 }
833
834 void
835 lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net)
836 {
837         lnet_route_t *rte;
838
839         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0) {
840                 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
841                         if (rte->lr_net == net) {
842                                 rte->lr_downis = 0;
843                                 break;
844                         }
845                 }
846         }
847 }
848
849 static void
850 lnet_update_ni_status_locked(void)
851 {
852         lnet_ni_t       *ni;
853         long            now;
854         int             timeout;
855
856         LASSERT(the_lnet.ln_routing);
857
858         timeout = router_ping_timeout +
859                   MAX(live_router_check_interval, dead_router_check_interval);
860
861         now = cfs_time_current_sec();
862         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
863                 if (ni->ni_lnd->lnd_type == LOLND)
864                         continue;
865
866                 if (now < ni->ni_last_alive + timeout)
867                         continue;
868
869                 lnet_ni_lock(ni);
870                 /* re-check with lock */
871                 if (now < ni->ni_last_alive + timeout) {
872                         lnet_ni_unlock(ni);
873                         continue;
874                 }
875
876                 LASSERT(ni->ni_status != NULL);
877
878                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
879                         CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
880                                libcfs_nid2str(ni->ni_nid), timeout);
881                         /* NB: so far, this is the only place to set
882                          * NI status to "down" */
883                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
884                 }
885                 lnet_ni_unlock(ni);
886         }
887 }
888
889 static void
890 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
891 {
892         LASSERT(list_empty(&rcd->rcd_list));
893         /* detached from network */
894         LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
895
896         if (rcd->rcd_gateway != NULL) {
897                 int cpt = rcd->rcd_gateway->lp_cpt;
898
899                 lnet_net_lock(cpt);
900                 lnet_peer_decref_locked(rcd->rcd_gateway);
901                 lnet_net_unlock(cpt);
902         }
903
904         if (rcd->rcd_pinginfo != NULL)
905                 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
906
907         LIBCFS_FREE(rcd, sizeof(*rcd));
908 }
909
910 static lnet_rc_data_t *
911 lnet_create_rc_data_locked(lnet_peer_t *gateway)
912 {
913         lnet_rc_data_t          *rcd = NULL;
914         lnet_ping_info_t        *pi;
915         int                     rc;
916         int                     i;
917
918         lnet_net_unlock(gateway->lp_cpt);
919
920         LIBCFS_ALLOC(rcd, sizeof(*rcd));
921         if (rcd == NULL)
922                 goto out;
923
924         LNetInvalidateHandle(&rcd->rcd_mdh);
925         INIT_LIST_HEAD(&rcd->rcd_list);
926
927         LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
928         if (pi == NULL)
929                 goto out;
930
931         for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
932                 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
933                 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
934         }
935         rcd->rcd_pinginfo = pi;
936
937         LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
938         rc = LNetMDBind((lnet_md_t){.start     = pi,
939                                     .user_ptr  = rcd,
940                                     .length    = LNET_PINGINFO_SIZE,
941                                     .threshold = LNET_MD_THRESH_INF,
942                                     .options   = LNET_MD_TRUNCATE,
943                                     .eq_handle = the_lnet.ln_rc_eqh},
944                         LNET_UNLINK,
945                         &rcd->rcd_mdh);
946         if (rc < 0) {
947                 CERROR("Can't bind MD: %d\n", rc);
948                 goto out;
949         }
950         LASSERT(rc == 0);
951
952         lnet_net_lock(gateway->lp_cpt);
953         /* router table changed or someone has created rcd for this gateway */
954         if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
955                 lnet_net_unlock(gateway->lp_cpt);
956                 goto out;
957         }
958
959         lnet_peer_addref_locked(gateway);
960         rcd->rcd_gateway = gateway;
961         gateway->lp_rcd = rcd;
962         gateway->lp_ping_notsent = 0;
963
964         return rcd;
965
966  out:
967         if (rcd != NULL) {
968                 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
969                         rc = LNetMDUnlink(rcd->rcd_mdh);
970                         LASSERT(rc == 0);
971                 }
972                 lnet_destroy_rc_data(rcd);
973         }
974
975         lnet_net_lock(gateway->lp_cpt);
976         return gateway->lp_rcd;
977 }
978
979 static int
980 lnet_router_check_interval (lnet_peer_t *rtr)
981 {
982         int secs;
983
984         secs = rtr->lp_alive ? live_router_check_interval :
985                                dead_router_check_interval;
986         if (secs < 0)
987                 secs = 0;
988
989         return secs;
990 }
991
992 static void
993 lnet_ping_router_locked (lnet_peer_t *rtr)
994 {
995         lnet_rc_data_t *rcd = NULL;
996         cfs_time_t      now = cfs_time_current();
997         int             secs;
998
999         lnet_peer_addref_locked(rtr);
1000
1001         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
1002             cfs_time_after(now, rtr->lp_ping_deadline))
1003                 lnet_notify_locked(rtr, 1, 0, now);
1004
1005         /* Run any outstanding notifications */
1006         lnet_ni_notify_locked(rtr->lp_ni, rtr);
1007
1008         if (!lnet_isrouter(rtr) ||
1009             the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1010                 /* router table changed or router checker is shutting down */
1011                 lnet_peer_decref_locked(rtr);
1012                 return;
1013         }
1014
1015         rcd = rtr->lp_rcd != NULL ?
1016               rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
1017
1018         if (rcd == NULL)
1019                 return;
1020
1021         secs = lnet_router_check_interval(rtr);
1022
1023         CDEBUG(D_NET,
1024                "rtr %s %d: deadline %lu ping_notsent %d alive %d "
1025                "alive_count %d lp_ping_timestamp %lu\n",
1026                libcfs_nid2str(rtr->lp_nid), secs,
1027                rtr->lp_ping_deadline, rtr->lp_ping_notsent,
1028                rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
1029
1030         if (secs != 0 && !rtr->lp_ping_notsent &&
1031             cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
1032                                              cfs_time_seconds(secs)))) {
1033                 int               rc;
1034                 lnet_process_id_t id;
1035                 lnet_handle_md_t  mdh;
1036
1037                 id.nid = rtr->lp_nid;
1038                 id.pid = LNET_PID_LUSTRE;
1039                 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
1040
1041                 rtr->lp_ping_notsent   = 1;
1042                 rtr->lp_ping_timestamp = now;
1043
1044                 mdh = rcd->rcd_mdh;
1045
1046                 if (rtr->lp_ping_deadline == 0) {
1047                         rtr->lp_ping_deadline =
1048                                 cfs_time_shift(router_ping_timeout);
1049                 }
1050
1051                 lnet_net_unlock(rtr->lp_cpt);
1052
1053                 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
1054                              LNET_PROTO_PING_MATCHBITS, 0);
1055
1056                 lnet_net_lock(rtr->lp_cpt);
1057                 if (rc != 0)
1058                         rtr->lp_ping_notsent = 0; /* no event pending */
1059         }
1060
1061         lnet_peer_decref_locked(rtr);
1062         return;
1063 }
1064
1065 int
1066 lnet_router_checker_start(void)
1067 {
1068         int                     rc;
1069         int                     eqsz;
1070 #ifdef __KERNEL__
1071         struct task_struct     *task;
1072 #else /* __KERNEL__ */
1073         lnet_peer_t            *rtr;
1074         __u64                   version;
1075         int                     nrtr = 0;
1076         int                     router_checker_max_eqsize = 10240;
1077
1078         LASSERT (check_routers_before_use);
1079         LASSERT (dead_router_check_interval > 0);
1080
1081         lnet_net_lock(0);
1082
1083         /* As an approximation, allow each router the same number of
1084          * outstanding events as it is allowed outstanding sends */
1085         eqsz = 0;
1086         version = the_lnet.ln_routers_version;
1087         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1088                 lnet_ni_t         *ni = rtr->lp_ni;
1089                 lnet_process_id_t  id;
1090
1091                 nrtr++;
1092                 eqsz += ni->ni_peertxcredits;
1093
1094                 /* one async ping reply per router */
1095                 id.nid = rtr->lp_nid;
1096                 id.pid = LNET_PID_LUSTRE;
1097
1098                 lnet_net_unlock(0);
1099
1100                 rc = LNetSetAsync(id, 1);
1101                 if (rc != 0) {
1102                         CWARN("LNetSetAsync %s failed: %d\n",
1103                               libcfs_id2str(id), rc);
1104                         return rc;
1105                 }
1106
1107                 lnet_net_lock(0);
1108                 /* NB router list doesn't change in userspace */
1109                 LASSERT(version == the_lnet.ln_routers_version);
1110         }
1111
1112         lnet_net_unlock(0);
1113
1114         if (nrtr == 0) {
1115                 CDEBUG(D_NET,
1116                        "No router found, not starting router checker\n");
1117                 return 0;
1118         }
1119
1120         /* at least allow a SENT and a REPLY per router */
1121         if (router_checker_max_eqsize < 2 * nrtr)
1122                 router_checker_max_eqsize = 2 * nrtr;
1123
1124         LASSERT (eqsz > 0);
1125         if (eqsz > router_checker_max_eqsize)
1126                 eqsz = router_checker_max_eqsize;
1127 #endif
1128
1129         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1130
1131         if (check_routers_before_use &&
1132             dead_router_check_interval <= 0) {
1133                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1134                                    " set if 'check_routers_before_use' is set"
1135                                    "\n");
1136                 return -EINVAL;
1137         }
1138
1139         if (!the_lnet.ln_routing &&
1140             live_router_check_interval <= 0 &&
1141             dead_router_check_interval <= 0)
1142                 return 0;
1143
1144 #ifdef __KERNEL__
1145         sema_init(&the_lnet.ln_rc_signal, 0);
1146         /* EQ size doesn't matter; the callback is guaranteed to get every
1147          * event */
1148         eqsz = 0;
1149         rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1150                          &the_lnet.ln_rc_eqh);
1151 #else
1152         rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1153                          &the_lnet.ln_rc_eqh);
1154 #endif
1155         if (rc != 0) {
1156                 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1157                 return -ENOMEM;
1158         }
1159
1160         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1161 #ifdef __KERNEL__
1162         task = kthread_run(lnet_router_checker, NULL, "router_checker");
1163         if (IS_ERR(task)) {
1164                 rc = PTR_ERR(task);
1165                 CERROR("Can't start router checker thread: %d\n", rc);
1166                 /* block until event callback signals exit */
1167                 down(&the_lnet.ln_rc_signal);
1168                 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1169                 LASSERT(rc == 0);
1170                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1171                 return -ENOMEM;
1172         }
1173 #endif
1174
1175         if (check_routers_before_use) {
1176                 /* Note that a helpful side-effect of pinging all known routers
1177                  * at startup is that it makes them drop stale connections they
1178                  * may have to a previous instance of me. */
1179                 lnet_wait_known_routerstate();
1180         }
1181
1182         return 0;
1183 }
1184
1185 void
1186 lnet_router_checker_stop (void)
1187 {
1188         int rc;
1189
1190         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1191                 return;
1192
1193         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1194         the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1195
1196 #ifdef __KERNEL__
1197         /* block until event callback signals exit */
1198         down(&the_lnet.ln_rc_signal);
1199 #else
1200         lnet_router_checker();
1201 #endif
1202         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1203
1204         rc = LNetEQFree(the_lnet.ln_rc_eqh);
1205         LASSERT (rc == 0);
1206         return;
1207 }
1208
1209 static void
1210 lnet_prune_rc_data(int wait_unlink)
1211 {
1212         lnet_rc_data_t          *rcd;
1213         lnet_rc_data_t          *tmp;
1214         lnet_peer_t             *lp;
1215         struct list_head         head;
1216         int                      i = 2;
1217
1218         if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1219                    list_empty(&the_lnet.ln_rcd_deathrow) &&
1220                    list_empty(&the_lnet.ln_rcd_zombie)))
1221                 return;
1222
1223         INIT_LIST_HEAD(&head);
1224
1225         lnet_net_lock(LNET_LOCK_EX);
1226
1227         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1228                 /* router checker is stopping, prune all */
1229                 list_for_each_entry(lp, &the_lnet.ln_routers,
1230                                     lp_rtr_list) {
1231                         if (lp->lp_rcd == NULL)
1232                                 continue;
1233
1234                         LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1235                         list_add(&lp->lp_rcd->rcd_list,
1236                                  &the_lnet.ln_rcd_deathrow);
1237                         lp->lp_rcd = NULL;
1238                 }
1239         }
1240
1241         /* unlink all RCDs on deathrow list */
1242         list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1243
1244         if (!list_empty(&head)) {
1245                 lnet_net_unlock(LNET_LOCK_EX);
1246
1247                 list_for_each_entry(rcd, &head, rcd_list)
1248                         LNetMDUnlink(rcd->rcd_mdh);
1249
1250                 lnet_net_lock(LNET_LOCK_EX);
1251         }
1252
1253         list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1254
1255         /* release all zombie RCDs */
1256         while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1257                 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1258                                          rcd_list) {
1259                         if (LNetHandleIsInvalid(rcd->rcd_mdh))
1260                                 list_move(&rcd->rcd_list, &head);
1261                 }
1262
1263                 wait_unlink = wait_unlink &&
1264                               !list_empty(&the_lnet.ln_rcd_zombie);
1265
1266                 lnet_net_unlock(LNET_LOCK_EX);
1267
1268                 while (!list_empty(&head)) {
1269                         rcd = list_entry(head.next,
1270                                          lnet_rc_data_t, rcd_list);
1271                         list_del_init(&rcd->rcd_list);
1272                         lnet_destroy_rc_data(rcd);
1273                 }
1274
1275                 if (!wait_unlink)
1276                         return;
1277
1278                 i++;
1279                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1280                        "Waiting for rc buffers to unlink\n");
1281                 cfs_pause(cfs_time_seconds(1) / 4);
1282
1283                 lnet_net_lock(LNET_LOCK_EX);
1284         }
1285
1286         lnet_net_unlock(LNET_LOCK_EX);
1287 }
1288
1289
1290 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1291
1292 static int
1293 lnet_router_checker(void *arg)
1294 {
1295         lnet_peer_t       *rtr;
1296         struct list_head  *entry;
1297
1298         cfs_block_allsigs();
1299
1300         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1301
1302         while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1303                 __u64   version;
1304                 int     cpt;
1305                 int     cpt2;
1306
1307                 cpt = lnet_net_lock_current();
1308 rescan:
1309                 version = the_lnet.ln_routers_version;
1310
1311                 list_for_each(entry, &the_lnet.ln_routers) {
1312                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1313
1314                         cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1315                         if (cpt != cpt2) {
1316                                 lnet_net_unlock(cpt);
1317                                 cpt = cpt2;
1318                                 lnet_net_lock(cpt);
1319                                 /* the routers list has changed */
1320                                 if (version != the_lnet.ln_routers_version)
1321                                         goto rescan;
1322                         }
1323
1324                         lnet_ping_router_locked(rtr);
1325
1326                         /* NB dropped lock */
1327                         if (version != the_lnet.ln_routers_version) {
1328                                 /* the routers list has changed */
1329                                 goto rescan;
1330                         }
1331                 }
1332
1333                 if (the_lnet.ln_routing)
1334                         lnet_update_ni_status_locked();
1335
1336                 lnet_net_unlock(cpt);
1337
1338                 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1339
1340                 /* Call cfs_pause() here always adds 1 to load average
1341                  * because kernel counts # active tasks as nr_running
1342                  * + nr_uninterruptible. */
1343                 schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,
1344                                                    cfs_time_seconds(1));
1345         }
1346
1347         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1348
1349         lnet_prune_rc_data(1); /* wait for UNLINK */
1350
1351         the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1352         up(&the_lnet.ln_rc_signal);
1353         /* The unlink event callback will signal final completion */
1354         return 0;
1355 }
1356
1357 void
1358 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1359 {
1360         int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1361
1362         while (--npages >= 0)
1363                 __free_page(rb->rb_kiov[npages].kiov_page);
1364
1365         LIBCFS_FREE(rb, sz);
1366 }
1367
1368 static lnet_rtrbuf_t *
1369 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1370 {
1371         int            npages = rbp->rbp_npages;
1372         int            sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1373         struct page   *page;
1374         lnet_rtrbuf_t *rb;
1375         int            i;
1376
1377         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1378         if (rb == NULL)
1379                 return NULL;
1380
1381         rb->rb_pool = rbp;
1382
1383         for (i = 0; i < npages; i++) {
1384                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1385                                           __GFP_ZERO | GFP_IOFS);
1386                 if (page == NULL) {
1387                         while (--i >= 0)
1388                                 __free_page(rb->rb_kiov[i].kiov_page);
1389
1390                         LIBCFS_FREE(rb, sz);
1391                         return NULL;
1392                 }
1393
1394                 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1395                 rb->rb_kiov[i].kiov_offset = 0;
1396                 rb->rb_kiov[i].kiov_page = page;
1397         }
1398
1399         return rb;
1400 }
1401
1402 static void
1403 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp, int cpt)
1404 {
1405         int              npages = rbp->rbp_npages;
1406         lnet_rtrbuf_t    *rb;
1407         struct list_head tmp;
1408
1409         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1410                 return;
1411
1412         INIT_LIST_HEAD(&tmp);
1413
1414         lnet_net_lock(cpt);
1415         lnet_drop_routed_msgs_locked(&rbp->rbp_msgs, cpt);
1416         list_splice_init(&rbp->rbp_bufs, &tmp);
1417         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1418         rbp->rbp_mincredits = 0;
1419         lnet_net_unlock(cpt);
1420
1421         /* Free buffers on the free list. */
1422         while (!list_empty(&tmp)) {
1423                 rb = list_entry(tmp.next, lnet_rtrbuf_t, rb_list);
1424                 list_del(&rb->rb_list);
1425                 lnet_destroy_rtrbuf(rb, npages);
1426         }
1427 }
1428
1429 static int
1430 lnet_rtrpool_adjust_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
1431 {
1432         struct list_head rb_list;
1433         lnet_rtrbuf_t   *rb;
1434         int             num_rb;
1435         int             num_buffers = 0;
1436         int             npages = rbp->rbp_npages;
1437
1438         /* If we are called for less buffers than already in the pool, we
1439          * just lower the nbuffers number and excess buffers will be
1440          * thrown away as they are returned to the free list.  Credits
1441          * then get adjusted as well. */
1442         if (nbufs <= rbp->rbp_nbuffers) {
1443                 lnet_net_lock(cpt);
1444                 rbp->rbp_nbuffers = nbufs;
1445                 lnet_net_unlock(cpt);
1446                 return 0;
1447         }
1448
1449         INIT_LIST_HEAD(&rb_list);
1450
1451         /* allocate the buffers on a local list first.  If all buffers are
1452          * allocated successfully then join this list to the rbp buffer
1453          * list.  If not then free all allocated buffers. */
1454         num_rb = rbp->rbp_nbuffers;
1455
1456         while (num_rb < nbufs) {
1457                 rb = lnet_new_rtrbuf(rbp, cpt);
1458                 if (rb == NULL) {
1459                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1460                                nbufs, npages);
1461                         goto failed;
1462                 }
1463
1464                 list_add(&rb->rb_list, &rb_list);
1465                 num_buffers++;
1466                 num_rb++;
1467         }
1468
1469         lnet_net_lock(cpt);
1470
1471         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1472         rbp->rbp_nbuffers += num_buffers;
1473         rbp->rbp_credits += num_buffers;
1474         rbp->rbp_mincredits = rbp->rbp_credits;
1475         /* We need to schedule blocked msg using the newly
1476          * added buffers. */
1477         while (!list_empty(&rbp->rbp_bufs) &&
1478                !list_empty(&rbp->rbp_msgs))
1479                 lnet_schedule_blocked_locked(rbp);
1480
1481         lnet_net_unlock(cpt);
1482
1483         return 0;
1484
1485 failed:
1486         while (!list_empty(&rb_list)) {
1487                 rb = list_entry(rb_list.next, lnet_rtrbuf_t, rb_list);
1488                 list_del(&rb->rb_list);
1489                 lnet_destroy_rtrbuf(rb, npages);
1490         }
1491
1492         return -ENOMEM;
1493 }
1494
1495 static void
1496 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1497 {
1498         INIT_LIST_HEAD(&rbp->rbp_msgs);
1499         INIT_LIST_HEAD(&rbp->rbp_bufs);
1500
1501         rbp->rbp_npages = npages;
1502         rbp->rbp_credits = 0;
1503         rbp->rbp_mincredits = 0;
1504 }
1505
1506 void
1507 lnet_rtrpools_free(int keep_pools)
1508 {
1509         lnet_rtrbufpool_t *rtrp;
1510         int               i;
1511
1512         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1513                 return;
1514
1515         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1516                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1517                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1518                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1519         }
1520
1521         if (!keep_pools) {
1522                 cfs_percpt_free(the_lnet.ln_rtrpools);
1523                 the_lnet.ln_rtrpools = NULL;
1524         }
1525 }
1526
1527 static int
1528 lnet_nrb_tiny_calculate(void)
1529 {
1530         int     nrbs = LNET_NRB_TINY;
1531
1532         if (tiny_router_buffers < 0) {
1533                 LCONSOLE_ERROR_MSG(0x10c,
1534                                    "tiny_router_buffers=%d invalid when "
1535                                    "routing enabled\n", tiny_router_buffers);
1536                 return -1;
1537         }
1538
1539         if (tiny_router_buffers > 0)
1540                 nrbs = tiny_router_buffers;
1541
1542         nrbs /= LNET_CPT_NUMBER;
1543         return max(nrbs, LNET_NRB_TINY_MIN);
1544 }
1545
1546 static int
1547 lnet_nrb_small_calculate(void)
1548 {
1549         int     nrbs = LNET_NRB_SMALL;
1550
1551         if (small_router_buffers < 0) {
1552                 LCONSOLE_ERROR_MSG(0x10c,
1553                                    "small_router_buffers=%d invalid when "
1554                                    "routing enabled\n", small_router_buffers);
1555                 return -1;
1556         }
1557
1558         if (small_router_buffers > 0)
1559                 nrbs = small_router_buffers;
1560
1561         nrbs /= LNET_CPT_NUMBER;
1562         return max(nrbs, LNET_NRB_SMALL_MIN);
1563 }
1564
1565 static int
1566 lnet_nrb_large_calculate(void)
1567 {
1568         int     nrbs = LNET_NRB_LARGE;
1569
1570         if (large_router_buffers < 0) {
1571                 LCONSOLE_ERROR_MSG(0x10c,
1572                                    "large_router_buffers=%d invalid when "
1573                                    "routing enabled\n", large_router_buffers);
1574                 return -1;
1575         }
1576
1577         if (large_router_buffers > 0)
1578                 nrbs = large_router_buffers;
1579
1580         nrbs /= LNET_CPT_NUMBER;
1581         return max(nrbs, LNET_NRB_LARGE_MIN);
1582 }
1583
1584 int
1585 lnet_rtrpools_alloc(int im_a_router)
1586 {
1587         lnet_rtrbufpool_t *rtrp;
1588         int     nrb_tiny;
1589         int     nrb_small;
1590         int     nrb_large;
1591         int     rc;
1592         int     i;
1593
1594         if (!strcmp(forwarding, "")) {
1595                 /* not set either way */
1596                 if (!im_a_router)
1597                         return 0;
1598         } else if (!strcmp(forwarding, "disabled")) {
1599                 /* explicitly disabled */
1600                 return 0;
1601         } else if (!strcmp(forwarding, "enabled")) {
1602                 /* explicitly enabled */
1603         } else {
1604                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1605                                    "'enabled' or 'disabled'\n");
1606                 return -EINVAL;
1607         }
1608
1609         nrb_tiny = lnet_nrb_tiny_calculate();
1610         if (nrb_tiny < 0)
1611                 return -EINVAL;
1612
1613         nrb_small = lnet_nrb_small_calculate();
1614         if (nrb_small < 0)
1615                 return -EINVAL;
1616
1617         nrb_large = lnet_nrb_large_calculate();
1618         if (nrb_large < 0)
1619                 return -EINVAL;
1620
1621         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1622                                                 LNET_NRBPOOLS *
1623                                                 sizeof(lnet_rtrbufpool_t));
1624         if (the_lnet.ln_rtrpools == NULL) {
1625                 LCONSOLE_ERROR_MSG(0x10c,
1626                                    "Failed to initialize router buffe pool\n");
1627                 return -ENOMEM;
1628         }
1629
1630         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1631                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1632                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1633                                               nrb_tiny, i);
1634                 if (rc != 0)
1635                         goto failed;
1636
1637                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1638                                   LNET_NRB_SMALL_PAGES);
1639                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1640                                               nrb_small, i);
1641                 if (rc != 0)
1642                         goto failed;
1643
1644                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1645                                   LNET_NRB_LARGE_PAGES);
1646                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1647                                               nrb_large, i);
1648                 if (rc != 0)
1649                         goto failed;
1650         }
1651
1652         lnet_net_lock(LNET_LOCK_EX);
1653         the_lnet.ln_routing = 1;
1654         lnet_net_unlock(LNET_LOCK_EX);
1655         return 0;
1656
1657  failed:
1658         lnet_rtrpools_free(0);
1659         return rc;
1660 }
1661
1662 static int
1663 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1664 {
1665         int nrb = 0;
1666         int rc = 0;
1667         int i;
1668         lnet_rtrbufpool_t *rtrp;
1669
1670         /* If the provided values for each buffer pool are different than the
1671          * configured values, we need to take action. */
1672         if (tiny >= 0) {
1673                 tiny_router_buffers = tiny;
1674                 nrb = lnet_nrb_tiny_calculate();
1675                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1676                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1677                                                       nrb, i);
1678                         if (rc != 0)
1679                                 return rc;
1680                 }
1681         }
1682         if (small >= 0) {
1683                 small_router_buffers = small;
1684                 nrb = lnet_nrb_small_calculate();
1685                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1686                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1687                                                       nrb, i);
1688                         if (rc != 0)
1689                                 return rc;
1690                 }
1691         }
1692         if (large >= 0) {
1693                 large_router_buffers = large;
1694                 nrb = lnet_nrb_large_calculate();
1695                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1696                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1697                                                       nrb, i);
1698                         if (rc != 0)
1699                                 return rc;
1700                 }
1701         }
1702
1703         return 0;
1704 }
1705
1706 int
1707 lnet_rtrpools_adjust(int tiny, int small, int large)
1708 {
1709         /* this function doesn't revert the changes if adding new buffers
1710          * failed.  It's up to the user space caller to revert the
1711          * changes. */
1712
1713         if (!the_lnet.ln_routing)
1714                 return 0;
1715
1716         return lnet_rtrpools_adjust_helper(tiny, small, large);
1717 }
1718
1719 int
1720 lnet_rtrpools_enable(void)
1721 {
1722         int rc;
1723
1724         if (the_lnet.ln_routing)
1725                 return 0;
1726
1727         if (the_lnet.ln_rtrpools == NULL)
1728                 /* If routing is turned off, and we have never
1729                  * initialized the pools before, just call the
1730                  * standard buffer pool allocation routine as
1731                  * if we are just configuring this for the first
1732                  * time. */
1733                 return lnet_rtrpools_alloc(1);
1734
1735         rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1736         if (rc != 0)
1737                 return rc;
1738
1739         lnet_net_lock(LNET_LOCK_EX);
1740         the_lnet.ln_routing = 1;
1741
1742         the_lnet.ln_ping_info->pi_features &= ~LNET_PING_FEAT_RTE_DISABLED;
1743         lnet_net_unlock(LNET_LOCK_EX);
1744
1745         return 0;
1746 }
1747
1748 void
1749 lnet_rtrpools_disable(void)
1750 {
1751         if (!the_lnet.ln_routing)
1752                 return;
1753
1754         lnet_net_lock(LNET_LOCK_EX);
1755         the_lnet.ln_routing = 0;
1756         the_lnet.ln_ping_info->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1757
1758         tiny_router_buffers = 0;
1759         small_router_buffers = 0;
1760         large_router_buffers = 0;
1761         lnet_net_unlock(LNET_LOCK_EX);
1762         lnet_rtrpools_free(1);
1763 }
1764
1765 int
1766 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1767 {
1768         struct lnet_peer        *lp = NULL;
1769         cfs_time_t              now = cfs_time_current();
1770         int                     cpt = lnet_cpt_of_nid(nid);
1771
1772         LASSERT (!in_interrupt ());
1773
1774         CDEBUG (D_NET, "%s notifying %s: %s\n",
1775                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1776                 libcfs_nid2str(nid),
1777                 alive ? "up" : "down");
1778
1779         if (ni != NULL &&
1780             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1781                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1782                         libcfs_nid2str(nid), alive ? "birth" : "death",
1783                         libcfs_nid2str(ni->ni_nid));
1784                 return -EINVAL;
1785         }
1786
1787         /* can't do predictions... */
1788         if (cfs_time_after(when, now)) {
1789                 CWARN ("Ignoring prediction from %s of %s %s "
1790                        "%ld seconds in the future\n",
1791                        (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1792                        libcfs_nid2str(nid), alive ? "up" : "down",
1793                        cfs_duration_sec(cfs_time_sub(when, now)));
1794                 return -EINVAL;
1795         }
1796
1797         if (ni != NULL && !alive &&             /* LND telling me she's down */
1798             !auto_down) {                       /* auto-down disabled */
1799                 CDEBUG(D_NET, "Auto-down disabled\n");
1800                 return 0;
1801         }
1802
1803         lnet_net_lock(cpt);
1804
1805         if (the_lnet.ln_shutdown) {
1806                 lnet_net_unlock(cpt);
1807                 return -ESHUTDOWN;
1808         }
1809
1810         lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1811         if (lp == NULL) {
1812                 /* nid not found */
1813                 lnet_net_unlock(cpt);
1814                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1815                 return 0;
1816         }
1817
1818         /* We can't fully trust LND on reporting exact peer last_alive
1819          * if he notifies us about dead peer. For example ksocklnd can
1820          * call us with when == _time_when_the_node_was_booted_ if
1821          * no connections were successfully established */
1822         if (ni != NULL && !alive && when < lp->lp_last_alive)
1823                 when = lp->lp_last_alive;
1824
1825         lnet_notify_locked(lp, ni == NULL, alive, when);
1826
1827         if (ni != NULL)
1828                 lnet_ni_notify_locked(ni, lp);
1829
1830         lnet_peer_decref_locked(lp);
1831
1832         lnet_net_unlock(cpt);
1833         return 0;
1834 }
1835 EXPORT_SYMBOL(lnet_notify);
1836
1837 void
1838 lnet_get_tunables (void)
1839 {
1840         return;
1841 }
1842
1843 #else
1844
1845 int
1846 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1847 {
1848         return -EOPNOTSUPP;
1849 }
1850
1851 void
1852 lnet_router_checker (void)
1853 {
1854         static time_t last = 0;
1855         static int    running = 0;
1856
1857         time_t            now = cfs_time_current_sec();
1858         int               interval = now - last;
1859         int               rc;
1860         __u64             version;
1861         lnet_peer_t      *rtr;
1862
1863         /* It's no use to call me again within a sec - all intervals and
1864          * timeouts are measured in seconds */
1865         if (last != 0 && interval < 2)
1866                 return;
1867
1868         if (last != 0 &&
1869             interval > MAX(live_router_check_interval,
1870                            dead_router_check_interval))
1871                 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1872                         live_router_check_interval, dead_router_check_interval,
1873                         interval);
1874
1875         LASSERT(LNET_CPT_NUMBER == 1);
1876
1877         lnet_net_lock(0);
1878         LASSERT(!running); /* recursion check */
1879         running = 1;
1880         lnet_net_unlock(0);
1881
1882         last = now;
1883
1884         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1885                 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1886
1887         /* consume all pending events */
1888         while (1) {
1889                 int          i;
1890                 lnet_event_t ev;
1891
1892                 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1893                  * recursion breaker in LNetEQPoll would fail */
1894                 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1895                 if (rc == 0)   /* no event pending */
1896                         break;
1897
1898                 /* NB a lost SENT prevents me from pinging a router again */
1899                 if (rc == -EOVERFLOW) {
1900                         CERROR("Dropped an event!!!\n");
1901                         abort();
1902                 }
1903
1904                 LASSERT (rc == 1);
1905
1906                 lnet_router_checker_event(&ev);
1907         }
1908
1909         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1910                 lnet_prune_rc_data(1); /* release rcd */
1911                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1912                 running = 0;
1913                 return;
1914         }
1915
1916         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1917
1918         lnet_net_lock(0);
1919
1920         version = the_lnet.ln_routers_version;
1921         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1922                 lnet_ping_router_locked(rtr);
1923                 LASSERT(version == the_lnet.ln_routers_version);
1924         }
1925
1926         lnet_net_unlock(0);
1927
1928         running = 0; /* lock only needed for the recursion check */
1929         return;
1930 }
1931
1932 /* NB lnet_peers_start_down depends on me,
1933  * so must be called before any peer creation */
1934 void
1935 lnet_get_tunables (void)
1936 {
1937         char *s;
1938
1939         s = getenv("LNET_ROUTER_PING_TIMEOUT");
1940         if (s != NULL) router_ping_timeout = atoi(s);
1941
1942         s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1943         if (s != NULL) live_router_check_interval = atoi(s);
1944
1945         s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1946         if (s != NULL) dead_router_check_interval = atoi(s);
1947
1948         /* This replaces old lnd_notify mechanism */
1949         check_routers_before_use = 1;
1950         if (dead_router_check_interval <= 0)
1951                 dead_router_check_interval = 30;
1952 }
1953
1954 void
1955 lnet_rtrpools_free(int keep_pools)
1956 {
1957 }
1958
1959 int
1960 lnet_rtrpools_alloc(int im_a_arouter)
1961 {
1962         return 0;
1963 }
1964
1965 #endif