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