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