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