Whamcloud - gitweb
LU-5396 lnet: make some functions static
[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                 rte->lr_downis = down;
739         }
740 }
741
742 static void
743 lnet_router_checker_event(lnet_event_t *event)
744 {
745         lnet_rc_data_t          *rcd = event->md.user_ptr;
746         struct lnet_peer        *lp;
747
748         LASSERT(rcd != NULL);
749
750         if (event->unlinked) {
751                 LNetInvalidateHandle(&rcd->rcd_mdh);
752                 return;
753         }
754
755         LASSERT(event->type == LNET_EVENT_SEND ||
756                 event->type == LNET_EVENT_REPLY);
757
758         lp = rcd->rcd_gateway;
759         LASSERT(lp != NULL);
760
761          /* NB: it's called with holding lnet_res_lock, we have a few
762           * places need to hold both locks at the same time, please take
763           * care of lock ordering */
764         lnet_net_lock(lp->lp_cpt);
765         if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
766                 /* ignore if no longer a router or rcd is replaced */
767                 goto out;
768         }
769
770         if (event->type == LNET_EVENT_SEND) {
771                 lp->lp_ping_notsent = 0;
772                 if (event->status == 0)
773                         goto out;
774         }
775
776         /* LNET_EVENT_REPLY */
777         /* A successful REPLY means the router is up.  If _any_ comms
778          * to the router fail I assume it's down (this will happen if
779          * we ping alive routers to try to detect router death before
780          * apps get burned). */
781
782         lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
783         /* The router checker will wake up very shortly and do the
784          * actual notification.
785          * XXX If 'lp' stops being a router before then, it will still
786          * have the notification pending!!! */
787
788         if (avoid_asym_router_failure && event->status == 0)
789                 lnet_parse_rc_info(rcd);
790
791  out:
792         lnet_net_unlock(lp->lp_cpt);
793 }
794
795 static void
796 lnet_wait_known_routerstate(void)
797 {
798         lnet_peer_t      *rtr;
799         struct list_head *entry;
800         int               all_known;
801
802         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
803
804         for (;;) {
805                 int cpt = lnet_net_lock_current();
806
807                 all_known = 1;
808                 list_for_each(entry, &the_lnet.ln_routers) {
809                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
810
811                         if (rtr->lp_alive_count == 0) {
812                                 all_known = 0;
813                                 break;
814                         }
815                 }
816
817                 lnet_net_unlock(cpt);
818
819                 if (all_known)
820                         return;
821
822 #ifndef __KERNEL__
823                 lnet_router_checker();
824 #endif
825                 cfs_pause(cfs_time_seconds(1));
826         }
827 }
828
829 void
830 lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net)
831 {
832         lnet_route_t *rte;
833
834         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0) {
835                 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
836                         if (rte->lr_net == net) {
837                                 rte->lr_downis = 0;
838                                 break;
839                         }
840                 }
841         }
842 }
843
844 static void
845 lnet_update_ni_status_locked(void)
846 {
847         lnet_ni_t       *ni;
848         long            now;
849         int             timeout;
850
851         LASSERT(the_lnet.ln_routing);
852
853         timeout = router_ping_timeout +
854                   MAX(live_router_check_interval, dead_router_check_interval);
855
856         now = cfs_time_current_sec();
857         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
858                 if (ni->ni_lnd->lnd_type == LOLND)
859                         continue;
860
861                 if (now < ni->ni_last_alive + timeout)
862                         continue;
863
864                 lnet_ni_lock(ni);
865                 /* re-check with lock */
866                 if (now < ni->ni_last_alive + timeout) {
867                         lnet_ni_unlock(ni);
868                         continue;
869                 }
870
871                 LASSERT(ni->ni_status != NULL);
872
873                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
874                         CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
875                                libcfs_nid2str(ni->ni_nid), timeout);
876                         /* NB: so far, this is the only place to set
877                          * NI status to "down" */
878                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
879                 }
880                 lnet_ni_unlock(ni);
881         }
882 }
883
884 static void
885 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
886 {
887         LASSERT(list_empty(&rcd->rcd_list));
888         /* detached from network */
889         LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
890
891         if (rcd->rcd_gateway != NULL) {
892                 int cpt = rcd->rcd_gateway->lp_cpt;
893
894                 lnet_net_lock(cpt);
895                 lnet_peer_decref_locked(rcd->rcd_gateway);
896                 lnet_net_unlock(cpt);
897         }
898
899         if (rcd->rcd_pinginfo != NULL)
900                 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
901
902         LIBCFS_FREE(rcd, sizeof(*rcd));
903 }
904
905 static lnet_rc_data_t *
906 lnet_create_rc_data_locked(lnet_peer_t *gateway)
907 {
908         lnet_rc_data_t          *rcd = NULL;
909         lnet_ping_info_t        *pi;
910         int                     rc;
911         int                     i;
912
913         lnet_net_unlock(gateway->lp_cpt);
914
915         LIBCFS_ALLOC(rcd, sizeof(*rcd));
916         if (rcd == NULL)
917                 goto out;
918
919         LNetInvalidateHandle(&rcd->rcd_mdh);
920         INIT_LIST_HEAD(&rcd->rcd_list);
921
922         LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
923         if (pi == NULL)
924                 goto out;
925
926         for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
927                 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
928                 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
929         }
930         rcd->rcd_pinginfo = pi;
931
932         LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
933         rc = LNetMDBind((lnet_md_t){.start     = pi,
934                                     .user_ptr  = rcd,
935                                     .length    = LNET_PINGINFO_SIZE,
936                                     .threshold = LNET_MD_THRESH_INF,
937                                     .options   = LNET_MD_TRUNCATE,
938                                     .eq_handle = the_lnet.ln_rc_eqh},
939                         LNET_UNLINK,
940                         &rcd->rcd_mdh);
941         if (rc < 0) {
942                 CERROR("Can't bind MD: %d\n", rc);
943                 goto out;
944         }
945         LASSERT(rc == 0);
946
947         lnet_net_lock(gateway->lp_cpt);
948         /* router table changed or someone has created rcd for this gateway */
949         if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
950                 lnet_net_unlock(gateway->lp_cpt);
951                 goto out;
952         }
953
954         lnet_peer_addref_locked(gateway);
955         rcd->rcd_gateway = gateway;
956         gateway->lp_rcd = rcd;
957         gateway->lp_ping_notsent = 0;
958
959         return rcd;
960
961  out:
962         if (rcd != NULL) {
963                 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
964                         rc = LNetMDUnlink(rcd->rcd_mdh);
965                         LASSERT(rc == 0);
966                 }
967                 lnet_destroy_rc_data(rcd);
968         }
969
970         lnet_net_lock(gateway->lp_cpt);
971         return gateway->lp_rcd;
972 }
973
974 static int
975 lnet_router_check_interval (lnet_peer_t *rtr)
976 {
977         int secs;
978
979         secs = rtr->lp_alive ? live_router_check_interval :
980                                dead_router_check_interval;
981         if (secs < 0)
982                 secs = 0;
983
984         return secs;
985 }
986
987 static void
988 lnet_ping_router_locked (lnet_peer_t *rtr)
989 {
990         lnet_rc_data_t *rcd = NULL;
991         cfs_time_t      now = cfs_time_current();
992         int             secs;
993
994         lnet_peer_addref_locked(rtr);
995
996         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
997             cfs_time_after(now, rtr->lp_ping_deadline))
998                 lnet_notify_locked(rtr, 1, 0, now);
999
1000         /* Run any outstanding notifications */
1001         lnet_ni_notify_locked(rtr->lp_ni, rtr);
1002
1003         if (!lnet_isrouter(rtr) ||
1004             the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1005                 /* router table changed or router checker is shutting down */
1006                 lnet_peer_decref_locked(rtr);
1007                 return;
1008         }
1009
1010         rcd = rtr->lp_rcd != NULL ?
1011               rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
1012
1013         if (rcd == NULL)
1014                 return;
1015
1016         secs = lnet_router_check_interval(rtr);
1017
1018         CDEBUG(D_NET,
1019                "rtr %s %d: deadline %lu ping_notsent %d alive %d "
1020                "alive_count %d lp_ping_timestamp %lu\n",
1021                libcfs_nid2str(rtr->lp_nid), secs,
1022                rtr->lp_ping_deadline, rtr->lp_ping_notsent,
1023                rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
1024
1025         if (secs != 0 && !rtr->lp_ping_notsent &&
1026             cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
1027                                              cfs_time_seconds(secs)))) {
1028                 int               rc;
1029                 lnet_process_id_t id;
1030                 lnet_handle_md_t  mdh;
1031
1032                 id.nid = rtr->lp_nid;
1033                 id.pid = LNET_PID_LUSTRE;
1034                 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
1035
1036                 rtr->lp_ping_notsent   = 1;
1037                 rtr->lp_ping_timestamp = now;
1038
1039                 mdh = rcd->rcd_mdh;
1040
1041                 if (rtr->lp_ping_deadline == 0) {
1042                         rtr->lp_ping_deadline =
1043                                 cfs_time_shift(router_ping_timeout);
1044                 }
1045
1046                 lnet_net_unlock(rtr->lp_cpt);
1047
1048                 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
1049                              LNET_PROTO_PING_MATCHBITS, 0);
1050
1051                 lnet_net_lock(rtr->lp_cpt);
1052                 if (rc != 0)
1053                         rtr->lp_ping_notsent = 0; /* no event pending */
1054         }
1055
1056         lnet_peer_decref_locked(rtr);
1057         return;
1058 }
1059
1060 int
1061 lnet_router_checker_start(void)
1062 {
1063         int                     rc;
1064         int                     eqsz;
1065 #ifdef __KERNEL__
1066         struct task_struct     *task;
1067 #else /* __KERNEL__ */
1068         lnet_peer_t            *rtr;
1069         __u64                   version;
1070         int                     nrtr = 0;
1071         int                     router_checker_max_eqsize = 10240;
1072
1073         LASSERT (check_routers_before_use);
1074         LASSERT (dead_router_check_interval > 0);
1075
1076         lnet_net_lock(0);
1077
1078         /* As an approximation, allow each router the same number of
1079          * outstanding events as it is allowed outstanding sends */
1080         eqsz = 0;
1081         version = the_lnet.ln_routers_version;
1082         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1083                 lnet_ni_t         *ni = rtr->lp_ni;
1084                 lnet_process_id_t  id;
1085
1086                 nrtr++;
1087                 eqsz += ni->ni_peertxcredits;
1088
1089                 /* one async ping reply per router */
1090                 id.nid = rtr->lp_nid;
1091                 id.pid = LNET_PID_LUSTRE;
1092
1093                 lnet_net_unlock(0);
1094
1095                 rc = LNetSetAsync(id, 1);
1096                 if (rc != 0) {
1097                         CWARN("LNetSetAsync %s failed: %d\n",
1098                               libcfs_id2str(id), rc);
1099                         return rc;
1100                 }
1101
1102                 lnet_net_lock(0);
1103                 /* NB router list doesn't change in userspace */
1104                 LASSERT(version == the_lnet.ln_routers_version);
1105         }
1106
1107         lnet_net_unlock(0);
1108
1109         if (nrtr == 0) {
1110                 CDEBUG(D_NET,
1111                        "No router found, not starting router checker\n");
1112                 return 0;
1113         }
1114
1115         /* at least allow a SENT and a REPLY per router */
1116         if (router_checker_max_eqsize < 2 * nrtr)
1117                 router_checker_max_eqsize = 2 * nrtr;
1118
1119         LASSERT (eqsz > 0);
1120         if (eqsz > router_checker_max_eqsize)
1121                 eqsz = router_checker_max_eqsize;
1122 #endif
1123
1124         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1125
1126         if (check_routers_before_use &&
1127             dead_router_check_interval <= 0) {
1128                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1129                                    " set if 'check_routers_before_use' is set"
1130                                    "\n");
1131                 return -EINVAL;
1132         }
1133
1134         if (!the_lnet.ln_routing &&
1135             live_router_check_interval <= 0 &&
1136             dead_router_check_interval <= 0)
1137                 return 0;
1138
1139 #ifdef __KERNEL__
1140         sema_init(&the_lnet.ln_rc_signal, 0);
1141         /* EQ size doesn't matter; the callback is guaranteed to get every
1142          * event */
1143         eqsz = 0;
1144         rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1145                          &the_lnet.ln_rc_eqh);
1146 #else
1147         rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1148                          &the_lnet.ln_rc_eqh);
1149 #endif
1150         if (rc != 0) {
1151                 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1152                 return -ENOMEM;
1153         }
1154
1155         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1156 #ifdef __KERNEL__
1157         task = kthread_run(lnet_router_checker, NULL, "router_checker");
1158         if (IS_ERR(task)) {
1159                 rc = PTR_ERR(task);
1160                 CERROR("Can't start router checker thread: %d\n", rc);
1161                 /* block until event callback signals exit */
1162                 down(&the_lnet.ln_rc_signal);
1163                 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1164                 LASSERT(rc == 0);
1165                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1166                 return -ENOMEM;
1167         }
1168 #endif
1169
1170         if (check_routers_before_use) {
1171                 /* Note that a helpful side-effect of pinging all known routers
1172                  * at startup is that it makes them drop stale connections they
1173                  * may have to a previous instance of me. */
1174                 lnet_wait_known_routerstate();
1175         }
1176
1177         return 0;
1178 }
1179
1180 void
1181 lnet_router_checker_stop (void)
1182 {
1183         int rc;
1184
1185         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1186                 return;
1187
1188         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1189         the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1190
1191 #ifdef __KERNEL__
1192         /* block until event callback signals exit */
1193         down(&the_lnet.ln_rc_signal);
1194 #else
1195         lnet_router_checker();
1196 #endif
1197         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1198
1199         rc = LNetEQFree(the_lnet.ln_rc_eqh);
1200         LASSERT (rc == 0);
1201         return;
1202 }
1203
1204 static void
1205 lnet_prune_rc_data(int wait_unlink)
1206 {
1207         lnet_rc_data_t          *rcd;
1208         lnet_rc_data_t          *tmp;
1209         lnet_peer_t             *lp;
1210         struct list_head         head;
1211         int                      i = 2;
1212
1213         if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1214                    list_empty(&the_lnet.ln_rcd_deathrow) &&
1215                    list_empty(&the_lnet.ln_rcd_zombie)))
1216                 return;
1217
1218         INIT_LIST_HEAD(&head);
1219
1220         lnet_net_lock(LNET_LOCK_EX);
1221
1222         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1223                 /* router checker is stopping, prune all */
1224                 list_for_each_entry(lp, &the_lnet.ln_routers,
1225                                     lp_rtr_list) {
1226                         if (lp->lp_rcd == NULL)
1227                                 continue;
1228
1229                         LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1230                         list_add(&lp->lp_rcd->rcd_list,
1231                                  &the_lnet.ln_rcd_deathrow);
1232                         lp->lp_rcd = NULL;
1233                 }
1234         }
1235
1236         /* unlink all RCDs on deathrow list */
1237         list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1238
1239         if (!list_empty(&head)) {
1240                 lnet_net_unlock(LNET_LOCK_EX);
1241
1242                 list_for_each_entry(rcd, &head, rcd_list)
1243                         LNetMDUnlink(rcd->rcd_mdh);
1244
1245                 lnet_net_lock(LNET_LOCK_EX);
1246         }
1247
1248         list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1249
1250         /* release all zombie RCDs */
1251         while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1252                 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1253                                          rcd_list) {
1254                         if (LNetHandleIsInvalid(rcd->rcd_mdh))
1255                                 list_move(&rcd->rcd_list, &head);
1256                 }
1257
1258                 wait_unlink = wait_unlink &&
1259                               !list_empty(&the_lnet.ln_rcd_zombie);
1260
1261                 lnet_net_unlock(LNET_LOCK_EX);
1262
1263                 while (!list_empty(&head)) {
1264                         rcd = list_entry(head.next,
1265                                          lnet_rc_data_t, rcd_list);
1266                         list_del_init(&rcd->rcd_list);
1267                         lnet_destroy_rc_data(rcd);
1268                 }
1269
1270                 if (!wait_unlink)
1271                         return;
1272
1273                 i++;
1274                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1275                        "Waiting for rc buffers to unlink\n");
1276                 cfs_pause(cfs_time_seconds(1) / 4);
1277
1278                 lnet_net_lock(LNET_LOCK_EX);
1279         }
1280
1281         lnet_net_unlock(LNET_LOCK_EX);
1282 }
1283
1284
1285 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1286
1287 static int
1288 lnet_router_checker(void *arg)
1289 {
1290         lnet_peer_t       *rtr;
1291         struct list_head  *entry;
1292
1293         cfs_block_allsigs();
1294
1295         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1296
1297         while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1298                 __u64   version;
1299                 int     cpt;
1300                 int     cpt2;
1301
1302                 cpt = lnet_net_lock_current();
1303 rescan:
1304                 version = the_lnet.ln_routers_version;
1305
1306                 list_for_each(entry, &the_lnet.ln_routers) {
1307                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1308
1309                         cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1310                         if (cpt != cpt2) {
1311                                 lnet_net_unlock(cpt);
1312                                 cpt = cpt2;
1313                                 lnet_net_lock(cpt);
1314                                 /* the routers list has changed */
1315                                 if (version != the_lnet.ln_routers_version)
1316                                         goto rescan;
1317                         }
1318
1319                         lnet_ping_router_locked(rtr);
1320
1321                         /* NB dropped lock */
1322                         if (version != the_lnet.ln_routers_version) {
1323                                 /* the routers list has changed */
1324                                 goto rescan;
1325                         }
1326                 }
1327
1328                 if (the_lnet.ln_routing)
1329                         lnet_update_ni_status_locked();
1330
1331                 lnet_net_unlock(cpt);
1332
1333                 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1334
1335                 /* Call cfs_pause() here always adds 1 to load average
1336                  * because kernel counts # active tasks as nr_running
1337                  * + nr_uninterruptible. */
1338                 schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,
1339                                                    cfs_time_seconds(1));
1340         }
1341
1342         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1343
1344         lnet_prune_rc_data(1); /* wait for UNLINK */
1345
1346         the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1347         up(&the_lnet.ln_rc_signal);
1348         /* The unlink event callback will signal final completion */
1349         return 0;
1350 }
1351
1352 void
1353 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1354 {
1355         int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1356
1357         while (--npages >= 0)
1358                 __free_page(rb->rb_kiov[npages].kiov_page);
1359
1360         LIBCFS_FREE(rb, sz);
1361 }
1362
1363 static lnet_rtrbuf_t *
1364 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1365 {
1366         int            npages = rbp->rbp_npages;
1367         int            sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1368         struct page   *page;
1369         lnet_rtrbuf_t *rb;
1370         int            i;
1371
1372         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1373         if (rb == NULL)
1374                 return NULL;
1375
1376         rb->rb_pool = rbp;
1377
1378         for (i = 0; i < npages; i++) {
1379                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1380                                           __GFP_ZERO | GFP_IOFS);
1381                 if (page == NULL) {
1382                         while (--i >= 0)
1383                                 __free_page(rb->rb_kiov[i].kiov_page);
1384
1385                         LIBCFS_FREE(rb, sz);
1386                         return NULL;
1387                 }
1388
1389                 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1390                 rb->rb_kiov[i].kiov_offset = 0;
1391                 rb->rb_kiov[i].kiov_page = page;
1392         }
1393
1394         return rb;
1395 }
1396
1397 static void
1398 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp, int cpt)
1399 {
1400         int              npages = rbp->rbp_npages;
1401         lnet_rtrbuf_t    *rb;
1402         struct list_head tmp;
1403
1404         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1405                 return;
1406
1407         INIT_LIST_HEAD(&tmp);
1408
1409         lnet_net_lock(cpt);
1410         lnet_drop_routed_msgs_locked(&rbp->rbp_msgs, cpt);
1411         list_splice_init(&rbp->rbp_bufs, &tmp);
1412         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1413         rbp->rbp_mincredits = 0;
1414         lnet_net_unlock(cpt);
1415
1416         /* Free buffers on the free list. */
1417         while (!list_empty(&tmp)) {
1418                 rb = list_entry(tmp.next, lnet_rtrbuf_t, rb_list);
1419                 list_del(&rb->rb_list);
1420                 lnet_destroy_rtrbuf(rb, npages);
1421         }
1422 }
1423
1424 static int
1425 lnet_rtrpool_adjust_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
1426 {
1427         struct list_head rb_list;
1428         lnet_rtrbuf_t   *rb;
1429         int             num_rb;
1430         int             num_buffers = 0;
1431         int             npages = rbp->rbp_npages;
1432
1433         /* If we are called for less buffers than already in the pool, we
1434          * just lower the nbuffers number and excess buffers will be
1435          * thrown away as they are returned to the free list.  Credits
1436          * then get adjusted as well. */
1437         if (nbufs <= rbp->rbp_nbuffers) {
1438                 lnet_net_lock(cpt);
1439                 rbp->rbp_nbuffers = nbufs;
1440                 lnet_net_unlock(cpt);
1441                 return 0;
1442         }
1443
1444         INIT_LIST_HEAD(&rb_list);
1445
1446         /* allocate the buffers on a local list first.  If all buffers are
1447          * allocated successfully then join this list to the rbp buffer
1448          * list.  If not then free all allocated buffers. */
1449         num_rb = rbp->rbp_nbuffers;
1450
1451         while (num_rb < nbufs) {
1452                 rb = lnet_new_rtrbuf(rbp, cpt);
1453                 if (rb == NULL) {
1454                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1455                                nbufs, npages);
1456                         goto failed;
1457                 }
1458
1459                 list_add(&rb->rb_list, &rb_list);
1460                 num_buffers++;
1461                 num_rb++;
1462         }
1463
1464         lnet_net_lock(cpt);
1465
1466         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1467         rbp->rbp_nbuffers += num_buffers;
1468         rbp->rbp_credits += num_buffers;
1469         rbp->rbp_mincredits = rbp->rbp_credits;
1470         /* We need to schedule blocked msg using the newly
1471          * added buffers. */
1472         while (!list_empty(&rbp->rbp_bufs) &&
1473                !list_empty(&rbp->rbp_msgs))
1474                 lnet_schedule_blocked_locked(rbp);
1475
1476         lnet_net_unlock(cpt);
1477
1478         return 0;
1479
1480 failed:
1481         while (!list_empty(&rb_list)) {
1482                 rb = list_entry(rb_list.next, lnet_rtrbuf_t, rb_list);
1483                 list_del(&rb->rb_list);
1484                 lnet_destroy_rtrbuf(rb, npages);
1485         }
1486
1487         return -ENOMEM;
1488 }
1489
1490 static void
1491 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1492 {
1493         INIT_LIST_HEAD(&rbp->rbp_msgs);
1494         INIT_LIST_HEAD(&rbp->rbp_bufs);
1495
1496         rbp->rbp_npages = npages;
1497         rbp->rbp_credits = 0;
1498         rbp->rbp_mincredits = 0;
1499 }
1500
1501 void
1502 lnet_rtrpools_free(int keep_pools)
1503 {
1504         lnet_rtrbufpool_t *rtrp;
1505         int               i;
1506
1507         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1508                 return;
1509
1510         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1511                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1512                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1513                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1514         }
1515
1516         if (!keep_pools) {
1517                 cfs_percpt_free(the_lnet.ln_rtrpools);
1518                 the_lnet.ln_rtrpools = NULL;
1519         }
1520 }
1521
1522 static int
1523 lnet_nrb_tiny_calculate(void)
1524 {
1525         int     nrbs = LNET_NRB_TINY;
1526
1527         if (tiny_router_buffers < 0) {
1528                 LCONSOLE_ERROR_MSG(0x10c,
1529                                    "tiny_router_buffers=%d invalid when "
1530                                    "routing enabled\n", tiny_router_buffers);
1531                 return -1;
1532         }
1533
1534         if (tiny_router_buffers > 0)
1535                 nrbs = tiny_router_buffers;
1536
1537         nrbs /= LNET_CPT_NUMBER;
1538         return max(nrbs, LNET_NRB_TINY_MIN);
1539 }
1540
1541 static int
1542 lnet_nrb_small_calculate(void)
1543 {
1544         int     nrbs = LNET_NRB_SMALL;
1545
1546         if (small_router_buffers < 0) {
1547                 LCONSOLE_ERROR_MSG(0x10c,
1548                                    "small_router_buffers=%d invalid when "
1549                                    "routing enabled\n", small_router_buffers);
1550                 return -1;
1551         }
1552
1553         if (small_router_buffers > 0)
1554                 nrbs = small_router_buffers;
1555
1556         nrbs /= LNET_CPT_NUMBER;
1557         return max(nrbs, LNET_NRB_SMALL_MIN);
1558 }
1559
1560 static int
1561 lnet_nrb_large_calculate(void)
1562 {
1563         int     nrbs = LNET_NRB_LARGE;
1564
1565         if (large_router_buffers < 0) {
1566                 LCONSOLE_ERROR_MSG(0x10c,
1567                                    "large_router_buffers=%d invalid when "
1568                                    "routing enabled\n", large_router_buffers);
1569                 return -1;
1570         }
1571
1572         if (large_router_buffers > 0)
1573                 nrbs = large_router_buffers;
1574
1575         nrbs /= LNET_CPT_NUMBER;
1576         return max(nrbs, LNET_NRB_LARGE_MIN);
1577 }
1578
1579 int
1580 lnet_rtrpools_alloc(int im_a_router)
1581 {
1582         lnet_rtrbufpool_t *rtrp;
1583         int     nrb_tiny;
1584         int     nrb_small;
1585         int     nrb_large;
1586         int     rc;
1587         int     i;
1588
1589         if (!strcmp(forwarding, "")) {
1590                 /* not set either way */
1591                 if (!im_a_router)
1592                         return 0;
1593         } else if (!strcmp(forwarding, "disabled")) {
1594                 /* explicitly disabled */
1595                 return 0;
1596         } else if (!strcmp(forwarding, "enabled")) {
1597                 /* explicitly enabled */
1598         } else {
1599                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1600                                    "'enabled' or 'disabled'\n");
1601                 return -EINVAL;
1602         }
1603
1604         nrb_tiny = lnet_nrb_tiny_calculate();
1605         if (nrb_tiny < 0)
1606                 return -EINVAL;
1607
1608         nrb_small = lnet_nrb_small_calculate();
1609         if (nrb_small < 0)
1610                 return -EINVAL;
1611
1612         nrb_large = lnet_nrb_large_calculate();
1613         if (nrb_large < 0)
1614                 return -EINVAL;
1615
1616         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1617                                                 LNET_NRBPOOLS *
1618                                                 sizeof(lnet_rtrbufpool_t));
1619         if (the_lnet.ln_rtrpools == NULL) {
1620                 LCONSOLE_ERROR_MSG(0x10c,
1621                                    "Failed to initialize router buffe pool\n");
1622                 return -ENOMEM;
1623         }
1624
1625         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1626                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1627                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1628                                               nrb_tiny, i);
1629                 if (rc != 0)
1630                         goto failed;
1631
1632                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1633                                   LNET_NRB_SMALL_PAGES);
1634                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1635                                               nrb_small, i);
1636                 if (rc != 0)
1637                         goto failed;
1638
1639                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1640                                   LNET_NRB_LARGE_PAGES);
1641                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1642                                               nrb_large, i);
1643                 if (rc != 0)
1644                         goto failed;
1645         }
1646
1647         lnet_net_lock(LNET_LOCK_EX);
1648         the_lnet.ln_routing = 1;
1649         lnet_net_unlock(LNET_LOCK_EX);
1650         return 0;
1651
1652  failed:
1653         lnet_rtrpools_free(0);
1654         return rc;
1655 }
1656
1657 static int
1658 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1659 {
1660         int nrb = 0;
1661         int rc = 0;
1662         int i;
1663         lnet_rtrbufpool_t *rtrp;
1664
1665         /* If the provided values for each buffer pool are different than the
1666          * configured values, we need to take action. */
1667         if (tiny >= 0) {
1668                 tiny_router_buffers = tiny;
1669                 nrb = lnet_nrb_tiny_calculate();
1670                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1671                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1672                                                       nrb, i);
1673                         if (rc != 0)
1674                                 return rc;
1675                 }
1676         }
1677         if (small >= 0) {
1678                 small_router_buffers = small;
1679                 nrb = lnet_nrb_small_calculate();
1680                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1681                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1682                                                       nrb, i);
1683                         if (rc != 0)
1684                                 return rc;
1685                 }
1686         }
1687         if (large >= 0) {
1688                 large_router_buffers = large;
1689                 nrb = lnet_nrb_large_calculate();
1690                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1691                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1692                                                       nrb, i);
1693                         if (rc != 0)
1694                                 return rc;
1695                 }
1696         }
1697
1698         return 0;
1699 }
1700
1701 int
1702 lnet_rtrpools_adjust(int tiny, int small, int large)
1703 {
1704         /* this function doesn't revert the changes if adding new buffers
1705          * failed.  It's up to the user space caller to revert the
1706          * changes. */
1707
1708         if (!the_lnet.ln_routing)
1709                 return 0;
1710
1711         return lnet_rtrpools_adjust_helper(tiny, small, large);
1712 }
1713
1714 int
1715 lnet_rtrpools_enable(void)
1716 {
1717         int rc;
1718
1719         if (the_lnet.ln_routing)
1720                 return 0;
1721
1722         if (the_lnet.ln_rtrpools == NULL)
1723                 /* If routing is turned off, and we have never
1724                  * initialized the pools before, just call the
1725                  * standard buffer pool allocation routine as
1726                  * if we are just configuring this for the first
1727                  * time. */
1728                 return lnet_rtrpools_alloc(1);
1729
1730         rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1731         if (rc != 0)
1732                 return rc;
1733
1734         lnet_net_lock(LNET_LOCK_EX);
1735         the_lnet.ln_routing = 1;
1736
1737         the_lnet.ln_ping_info->pi_features &= ~LNET_PING_FEAT_RTE_DISABLED;
1738         lnet_net_unlock(LNET_LOCK_EX);
1739
1740         return 0;
1741 }
1742
1743 void
1744 lnet_rtrpools_disable(void)
1745 {
1746         if (!the_lnet.ln_routing)
1747                 return;
1748
1749         lnet_net_lock(LNET_LOCK_EX);
1750         the_lnet.ln_routing = 0;
1751         the_lnet.ln_ping_info->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1752
1753         tiny_router_buffers = 0;
1754         small_router_buffers = 0;
1755         large_router_buffers = 0;
1756         lnet_net_unlock(LNET_LOCK_EX);
1757         lnet_rtrpools_free(1);
1758 }
1759
1760 int
1761 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1762 {
1763         struct lnet_peer        *lp = NULL;
1764         cfs_time_t              now = cfs_time_current();
1765         int                     cpt = lnet_cpt_of_nid(nid);
1766
1767         LASSERT (!in_interrupt ());
1768
1769         CDEBUG (D_NET, "%s notifying %s: %s\n",
1770                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1771                 libcfs_nid2str(nid),
1772                 alive ? "up" : "down");
1773
1774         if (ni != NULL &&
1775             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1776                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1777                         libcfs_nid2str(nid), alive ? "birth" : "death",
1778                         libcfs_nid2str(ni->ni_nid));
1779                 return -EINVAL;
1780         }
1781
1782         /* can't do predictions... */
1783         if (cfs_time_after(when, now)) {
1784                 CWARN ("Ignoring prediction from %s of %s %s "
1785                        "%ld seconds in the future\n",
1786                        (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1787                        libcfs_nid2str(nid), alive ? "up" : "down",
1788                        cfs_duration_sec(cfs_time_sub(when, now)));
1789                 return -EINVAL;
1790         }
1791
1792         if (ni != NULL && !alive &&             /* LND telling me she's down */
1793             !auto_down) {                       /* auto-down disabled */
1794                 CDEBUG(D_NET, "Auto-down disabled\n");
1795                 return 0;
1796         }
1797
1798         lnet_net_lock(cpt);
1799
1800         if (the_lnet.ln_shutdown) {
1801                 lnet_net_unlock(cpt);
1802                 return -ESHUTDOWN;
1803         }
1804
1805         lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1806         if (lp == NULL) {
1807                 /* nid not found */
1808                 lnet_net_unlock(cpt);
1809                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1810                 return 0;
1811         }
1812
1813         /* We can't fully trust LND on reporting exact peer last_alive
1814          * if he notifies us about dead peer. For example ksocklnd can
1815          * call us with when == _time_when_the_node_was_booted_ if
1816          * no connections were successfully established */
1817         if (ni != NULL && !alive && when < lp->lp_last_alive)
1818                 when = lp->lp_last_alive;
1819
1820         lnet_notify_locked(lp, ni == NULL, alive, when);
1821
1822         if (ni != NULL)
1823                 lnet_ni_notify_locked(ni, lp);
1824
1825         lnet_peer_decref_locked(lp);
1826
1827         lnet_net_unlock(cpt);
1828         return 0;
1829 }
1830 EXPORT_SYMBOL(lnet_notify);
1831
1832 void
1833 lnet_get_tunables (void)
1834 {
1835         return;
1836 }
1837
1838 #else
1839
1840 int
1841 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1842 {
1843         return -EOPNOTSUPP;
1844 }
1845
1846 void
1847 lnet_router_checker (void)
1848 {
1849         static time_t last = 0;
1850         static int    running = 0;
1851
1852         time_t            now = cfs_time_current_sec();
1853         int               interval = now - last;
1854         int               rc;
1855         __u64             version;
1856         lnet_peer_t      *rtr;
1857
1858         /* It's no use to call me again within a sec - all intervals and
1859          * timeouts are measured in seconds */
1860         if (last != 0 && interval < 2)
1861                 return;
1862
1863         if (last != 0 &&
1864             interval > MAX(live_router_check_interval,
1865                            dead_router_check_interval))
1866                 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1867                         live_router_check_interval, dead_router_check_interval,
1868                         interval);
1869
1870         LASSERT(LNET_CPT_NUMBER == 1);
1871
1872         lnet_net_lock(0);
1873         LASSERT(!running); /* recursion check */
1874         running = 1;
1875         lnet_net_unlock(0);
1876
1877         last = now;
1878
1879         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1880                 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1881
1882         /* consume all pending events */
1883         while (1) {
1884                 int          i;
1885                 lnet_event_t ev;
1886
1887                 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1888                  * recursion breaker in LNetEQPoll would fail */
1889                 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1890                 if (rc == 0)   /* no event pending */
1891                         break;
1892
1893                 /* NB a lost SENT prevents me from pinging a router again */
1894                 if (rc == -EOVERFLOW) {
1895                         CERROR("Dropped an event!!!\n");
1896                         abort();
1897                 }
1898
1899                 LASSERT (rc == 1);
1900
1901                 lnet_router_checker_event(&ev);
1902         }
1903
1904         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1905                 lnet_prune_rc_data(1); /* release rcd */
1906                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1907                 running = 0;
1908                 return;
1909         }
1910
1911         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1912
1913         lnet_net_lock(0);
1914
1915         version = the_lnet.ln_routers_version;
1916         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1917                 lnet_ping_router_locked(rtr);
1918                 LASSERT(version == the_lnet.ln_routers_version);
1919         }
1920
1921         lnet_net_unlock(0);
1922
1923         running = 0; /* lock only needed for the recursion check */
1924         return;
1925 }
1926
1927 /* NB lnet_peers_start_down depends on me,
1928  * so must be called before any peer creation */
1929 void
1930 lnet_get_tunables (void)
1931 {
1932         char *s;
1933
1934         s = getenv("LNET_ROUTER_PING_TIMEOUT");
1935         if (s != NULL) router_ping_timeout = atoi(s);
1936
1937         s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1938         if (s != NULL) live_router_check_interval = atoi(s);
1939
1940         s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1941         if (s != NULL) dead_router_check_interval = atoi(s);
1942
1943         /* This replaces old lnd_notify mechanism */
1944         check_routers_before_use = 1;
1945         if (dead_router_check_interval <= 0)
1946                 dead_router_check_interval = 30;
1947 }
1948
1949 void
1950 lnet_rtrpools_free(int keep_pools)
1951 {
1952 }
1953
1954 int
1955 lnet_rtrpools_alloc(int im_a_arouter)
1956 {
1957         return 0;
1958 }
1959
1960 #endif