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