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