Whamcloud - gitweb
b=12302
[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 (C) 2002 Cluster File Systems, 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
43 static int auto_down = 1;
44 CFS_MODULE_PARM(auto_down, "i", int, 0444,
45                 "Automatically mark peers down on comms error");
46
47 static int check_routers_before_use = 0;
48 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
49                 "Assume routers are down and ping them before use");
50
51 static int dead_router_check_interval = 0;
52 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0444,
53                 "Seconds between dead router health checks (<= 0 to disable)");
54
55 static int live_router_check_interval = 0;
56 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0444,
57                 "Seconds between live router health checks (<= 0 to disable)");
58
59 static int router_ping_timeout = 50;
60 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0444,
61                 "Seconds to wait for the reply to a router health query");
62
63 int
64 lnet_peers_start_down(void)
65 {
66         return check_routers_before_use;
67 }
68
69 void
70 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, time_t when)
71 {
72         if (when < lp->lp_timestamp) {          /* out of date information */
73                 CDEBUG(D_NET, "Out of date\n");
74                 return;
75         }
76
77         lp->lp_timestamp = when;                /* update timestamp */
78         lp->lp_ping_deadline = 0;               /* disable ping timeout */
79
80         if (lp->lp_alive_count != 0 &&          /* got old news */
81             (!lp->lp_alive) == (!alive)) {      /* new date for old news */
82                 CDEBUG(D_NET, "Old news\n");
83                 return;
84         }
85
86         /* Flag that notification is outstanding */
87
88         lp->lp_alive_count++;
89         lp->lp_alive = !(!alive);               /* 1 bit! */
90         lp->lp_notify = 1;
91         lp->lp_notifylnd |= notifylnd;
92
93         CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
94 }
95
96 void
97 lnet_do_notify (lnet_peer_t *lp) 
98 {
99         lnet_ni_t *ni = lp->lp_ni;
100         int        alive;
101         int        notifylnd;
102         
103         LNET_LOCK();
104                 
105         /* Notify only in 1 thread at any time to ensure ordered notification.
106          * NB individual events can be missed; the only guarantee is that you
107          * always get the most recent news */
108
109         if (lp->lp_notifying) {
110                 LNET_UNLOCK();
111                 return;
112         }
113
114         lp->lp_notifying = 1;
115         
116         while (lp->lp_notify) {
117                 alive     = lp->lp_alive;
118                 notifylnd = lp->lp_notifylnd;
119
120                 lp->lp_notifylnd = 0;
121                 lp->lp_notify    = 0;
122
123                 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
124                         LNET_UNLOCK();
125
126                         /* A new notification could happen now; I'll handle it
127                          * when control returns to me */
128
129                         (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
130
131                         LNET_LOCK();
132                 }
133         }
134
135         lp->lp_notifying = 0;
136
137         LNET_UNLOCK();
138 }
139
140 int
141 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, time_t when)
142 {
143         lnet_peer_t         *lp = NULL;
144         time_t               now = cfs_time_current_sec();
145
146         LASSERT (!in_interrupt ());
147
148         CDEBUG (D_NET, "%s notifying %s: %s\n",
149                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
150                 libcfs_nid2str(nid),
151                 alive ? "up" : "down");
152
153         if (ni != NULL &&
154             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
155                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
156                         libcfs_nid2str(nid), alive ? "birth" : "death",
157                         libcfs_nid2str(ni->ni_nid));
158                 return -EINVAL;
159         }
160
161         /* can't do predictions... */
162         if (when > now) {
163                 CWARN ("Ignoring prediction from %s of %s %s "
164                        "%ld seconds in the future\n",
165                        (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
166                        libcfs_nid2str(nid), alive ? "up" : "down",
167                        when - now);
168                 return -EINVAL;
169         }
170
171         if (ni != NULL && !alive &&             /* LND telling me she's down */
172             !auto_down) {                       /* auto-down disabled */
173                 CDEBUG(D_NET, "Auto-down disabled\n");
174                 return 0;
175         }
176         
177         LNET_LOCK();
178
179         lp = lnet_find_peer_locked(nid);
180         if (lp == NULL) {
181                 /* nid not found */
182                 LNET_UNLOCK();
183                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
184                 return 0;
185         }
186
187         lnet_notify_locked(lp, ni == NULL, alive, when);
188
189         LNET_UNLOCK();
190         
191         lnet_do_notify(lp);
192         
193         LNET_LOCK();
194
195         lnet_peer_decref_locked(lp);
196
197         LNET_UNLOCK();
198         return 0;
199 }
200 EXPORT_SYMBOL(lnet_notify);
201
202 #else
203
204 int
205 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, time_t when)
206 {
207         return -EOPNOTSUPP;
208 }
209
210 #endif
211
212 static void
213 lnet_rtr_addref_locked(lnet_peer_t *lp)
214 {
215         LASSERT (lp->lp_refcount > 0);
216         LASSERT (lp->lp_rtr_refcount >= 0);
217
218         lp->lp_rtr_refcount++;
219         if (lp->lp_rtr_refcount == 1) {
220                 struct list_head *pos;
221
222                 /* a simple insertion sort */
223                 list_for_each_prev(pos, &the_lnet.ln_routers) {
224                         lnet_peer_t *rtr = list_entry(pos, lnet_peer_t, 
225                                                       lp_rtr_list);
226
227                         if (rtr->lp_nid < lp->lp_nid)
228                                 break;
229                 }
230
231                 list_add(&lp->lp_rtr_list, pos);
232                 /* addref for the_lnet.ln_routers */
233                 lnet_peer_addref_locked(lp);
234                 the_lnet.ln_routers_version++;
235         }
236 }
237
238 static void
239 lnet_rtr_decref_locked(lnet_peer_t *lp)
240 {
241         LASSERT (lp->lp_refcount > 0);
242         LASSERT (lp->lp_rtr_refcount > 0);
243
244         lp->lp_rtr_refcount--;
245         if (lp->lp_rtr_refcount == 0) {
246                 list_del(&lp->lp_rtr_list);
247                 /* decref for the_lnet.ln_routers */
248                 lnet_peer_decref_locked(lp);
249                 the_lnet.ln_routers_version++;
250         }
251 }
252
253 lnet_remotenet_t *
254 lnet_find_net_locked (__u32 net)
255 {
256         lnet_remotenet_t *rnet;
257         struct list_head *tmp;
258
259         LASSERT (!the_lnet.ln_shutdown);
260
261         list_for_each (tmp, &the_lnet.ln_remote_nets) {
262                 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
263
264                 if (rnet->lrn_net == net)
265                         return rnet;
266         }
267         return NULL;
268 }
269
270 int
271 lnet_add_route (__u32 net, unsigned int hops, lnet_nid_t gateway)
272 {
273         struct list_head     zombies;
274         struct list_head    *e;
275         lnet_remotenet_t    *rnet;
276         lnet_remotenet_t    *rnet2;
277         lnet_route_t        *route;
278         lnet_route_t        *route2;
279         lnet_ni_t           *ni;
280         int                  add_route;
281         int                  rc;
282
283         CDEBUG(D_NET, "Add route: net %s hops %u gw %s\n",
284                libcfs_net2str(net), hops, libcfs_nid2str(gateway));
285
286         if (gateway == LNET_NID_ANY ||
287             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
288             net == LNET_NIDNET(LNET_NID_ANY) ||
289             LNET_NETTYP(net) == LOLND ||
290             LNET_NIDNET(gateway) == net ||
291             hops < 1 || hops > 255)
292                 return (-EINVAL);
293
294         if (lnet_islocalnet(net))               /* it's a local network */
295                 return 0;                       /* ignore the route entry */
296
297         /* Assume net, route, all new */
298         LIBCFS_ALLOC(route, sizeof(*route));
299         LIBCFS_ALLOC(rnet, sizeof(*rnet));
300         if (route == NULL || rnet == NULL) {
301                 CERROR("Out of memory creating route %s %d %s\n",
302                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
303                 if (route != NULL)
304                         LIBCFS_FREE(route, sizeof(*route));
305                 if (rnet != NULL)
306                         LIBCFS_FREE(rnet, sizeof(*rnet));
307                 return -ENOMEM;
308         }
309
310         INIT_LIST_HEAD(&rnet->lrn_routes);
311         rnet->lrn_net = net;
312         rnet->lrn_hops = hops;
313
314         LNET_LOCK();
315
316         rc = lnet_nid2peer_locked(&route->lr_gateway, gateway);
317         if (rc != 0) {
318                 LNET_UNLOCK();
319
320                 LIBCFS_FREE(route, sizeof(*route));
321                 LIBCFS_FREE(rnet, sizeof(*rnet));
322
323                 if (rc == -EHOSTUNREACH)        /* gateway is not on a local net */
324                         return 0;               /* ignore the route entry */
325
326                 CERROR("Error %d creating route %s %d %s\n", rc,
327                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
328                 return rc;
329         }
330
331         LASSERT (!the_lnet.ln_shutdown);
332         CFS_INIT_LIST_HEAD(&zombies);
333
334         rnet2 = lnet_find_net_locked(net);
335         if (rnet2 == NULL) {
336                 /* new network */
337                 list_add_tail(&rnet->lrn_list, &the_lnet.ln_remote_nets);
338                 rnet2 = rnet;
339         }
340
341         if (hops > rnet2->lrn_hops) {
342                 /* New route is longer; ignore it */
343                 add_route = 0;
344         } else if (hops < rnet2->lrn_hops) {
345                 /* new route supercedes all currently known routes to this
346                  * net */
347                 list_add(&zombies, &rnet2->lrn_routes);
348                 list_del_init(&rnet2->lrn_routes);
349                 add_route = 1;
350         } else {
351                 add_route = 1;
352                 /* New route has the same hopcount as existing routes; search
353                  * for a duplicate route (it's a NOOP if it is) */
354                 list_for_each (e, &rnet2->lrn_routes) {
355                         route2 = list_entry(e, lnet_route_t, lr_list);
356
357                         if (route2->lr_gateway == route->lr_gateway) {
358                                 add_route = 0;
359                                 break;
360                         }
361
362                         /* our loopups must be true */
363                         LASSERT (route2->lr_gateway->lp_nid != gateway);
364                 }
365         }
366         
367         if (add_route) {
368                 ni = route->lr_gateway->lp_ni;
369                 lnet_ni_addref_locked(ni);
370                 
371                 LASSERT (rc == 0);
372                 list_add_tail(&route->lr_list, &rnet2->lrn_routes);
373                 the_lnet.ln_remote_nets_version++;
374
375                 lnet_rtr_addref_locked(route->lr_gateway);
376
377                 LNET_UNLOCK();
378
379                 /* XXX Assume alive */
380                 if (ni->ni_lnd->lnd_notify != NULL)
381                         (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
382
383                 lnet_ni_decref(ni);
384         } else {
385                 lnet_peer_decref_locked(route->lr_gateway);
386                 LNET_UNLOCK();
387                 LIBCFS_FREE(route, sizeof(*route));
388         }
389
390         if (rnet != rnet2)
391                 LIBCFS_FREE(rnet, sizeof(*rnet));
392
393         while (!list_empty(&zombies)) {
394                 route = list_entry(zombies.next, lnet_route_t, lr_list);
395                 list_del(&route->lr_list);
396                 
397                 LNET_LOCK();
398                 lnet_rtr_decref_locked(route->lr_gateway);
399                 lnet_peer_decref_locked(route->lr_gateway);
400                 LNET_UNLOCK();
401                 LIBCFS_FREE(route, sizeof(*route));
402         }
403
404         return rc;
405 }
406
407 int
408 lnet_check_routes (void)
409 {
410         lnet_remotenet_t    *rnet;
411         lnet_route_t        *route;
412         lnet_route_t        *route2;
413         struct list_head    *e1;
414         struct list_head    *e2;
415
416         LNET_LOCK();
417
418         list_for_each (e1, &the_lnet.ln_remote_nets) {
419                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
420
421                 route2 = NULL;
422                 list_for_each (e2, &rnet->lrn_routes) {
423                         route = list_entry(e2, lnet_route_t, lr_list);
424
425                         if (route2 == NULL)
426                                 route2 = route;
427                         else if (route->lr_gateway->lp_ni !=
428                                  route2->lr_gateway->lp_ni) {
429                                 LNET_UNLOCK();
430                                 
431                                 CERROR("Routes to %s via %s and %s not supported\n",
432                                        libcfs_net2str(rnet->lrn_net),
433                                        libcfs_nid2str(route->lr_gateway->lp_nid),
434                                        libcfs_nid2str(route2->lr_gateway->lp_nid));
435                                 return -EINVAL;
436                         }
437                 }
438         }
439         
440         LNET_UNLOCK();
441         return 0;
442 }
443
444 int
445 lnet_del_route (__u32 net, lnet_nid_t gw_nid)
446 {
447         lnet_remotenet_t    *rnet;
448         lnet_route_t        *route;
449         struct list_head    *e1;
450         struct list_head    *e2;
451         int                  rc = -ENOENT;
452
453         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
454                libcfs_net2str(net), libcfs_nid2str(gw_nid));
455
456         /* NB Caller may specify either all routes via the given gateway
457          * or a specific route entry actual NIDs) */
458
459  again:
460         LNET_LOCK();
461
462         list_for_each (e1, &the_lnet.ln_remote_nets) {
463                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
464
465                 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
466                       net == rnet->lrn_net))
467                         continue;
468
469                 list_for_each (e2, &rnet->lrn_routes) {
470                         route = list_entry(e2, lnet_route_t, lr_list);
471
472                         if (!(gw_nid == LNET_NID_ANY ||
473                               gw_nid == route->lr_gateway->lp_nid))
474                                 continue;
475
476                         list_del(&route->lr_list);
477                         the_lnet.ln_remote_nets_version++;
478
479                         if (list_empty(&rnet->lrn_routes))
480                                 list_del(&rnet->lrn_list);
481                         else
482                                 rnet = NULL;
483
484                         lnet_rtr_decref_locked(route->lr_gateway);
485                         lnet_peer_decref_locked(route->lr_gateway);
486                         LNET_UNLOCK();
487
488                         LIBCFS_FREE(route, sizeof (*route));
489
490                         if (rnet != NULL)
491                                 LIBCFS_FREE(rnet, sizeof(*rnet));
492
493                         rc = 0;
494                         goto again;
495                 }
496         }
497
498         LNET_UNLOCK();
499         return rc;
500 }
501
502 void
503 lnet_destroy_routes (void)
504 {
505         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
506 }
507
508 int
509 lnet_get_route (int idx, __u32 *net, __u32 *hops,
510                lnet_nid_t *gateway, __u32 *alive)
511 {
512         struct list_head    *e1;
513         struct list_head    *e2;
514         lnet_remotenet_t    *rnet;
515         lnet_route_t        *route;
516
517         LNET_LOCK();
518
519         list_for_each (e1, &the_lnet.ln_remote_nets) {
520                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
521
522                 list_for_each (e2, &rnet->lrn_routes) {
523                         route = list_entry(e2, lnet_route_t, lr_list);
524
525                         if (idx-- == 0) {
526                                 *net     = rnet->lrn_net;
527                                 *hops    = rnet->lrn_hops;
528                                 *gateway = route->lr_gateway->lp_nid;
529                                 *alive   = route->lr_gateway->lp_alive;
530                                 LNET_UNLOCK();
531                                 return 0;
532                         }
533                 }
534         }
535
536         LNET_UNLOCK();
537         return -ENOENT;
538 }
539
540 #if defined(__KERNEL__) && defined(LNET_ROUTER)
541 static void
542 lnet_router_checker_event (lnet_event_t *event)
543 {
544         /* CAVEAT EMPTOR: I'm called with LNET_LOCKed and I'm not allowed to
545          * drop it (that's how come I see _every_ event, even ones that would
546          * overflow my EQ) */
547         lnet_peer_t   *lp;
548         lnet_nid_t     nid;
549
550         if (event->unlinked) {
551                 /* The router checker thread has unlinked the rc_md
552                  * and exited. */
553                 LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKING);
554                 the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKED; 
555                 mutex_up(&the_lnet.ln_rc_signal); 
556                 return;
557         }
558
559         LASSERT (event->type == LNET_EVENT_SEND || 
560                  event->type == LNET_EVENT_REPLY);
561         
562         nid = (event->type == LNET_EVENT_SEND) ?
563               event->target.nid : event->initiator.nid;
564
565         lp = lnet_find_peer_locked(nid);
566         if (lp == NULL) {
567                 /* router may have been removed */
568                 CDEBUG(D_NET, "Router %s not found\n", libcfs_nid2str(nid));
569                 return;
570         }
571
572         if (event->type == LNET_EVENT_SEND)     /* re-enable another ping */
573                 lp->lp_ping_notsent = 0;
574
575         if (lnet_isrouter(lp) &&                /* ignore if no longer a router */
576             (event->status != 0 ||
577              event->type == LNET_EVENT_REPLY)) {
578                 
579                 /* A successful REPLY means the router is up.  If _any_ comms
580                  * to the router fail I assume it's down (this will happen if
581                  * we ping alive routers to try to detect router death before
582                  * apps get burned). */
583
584                 lnet_notify_locked(lp, 1, (event->status == 0),
585                                    cfs_time_current_sec());
586
587                 /* The router checker will wake up very shortly and do the
588                  * actual notification.  
589                  * XXX If 'lp' stops being a router before then, it will still
590                  * have the notification pending!!! */
591         }
592
593         /* This decref will NOT drop LNET_LOCK (it had to have 1 ref when it
594          * was in the peer table and I've not dropped the lock, so no-one else
595          * can have reduced the refcount) */
596         LASSERT(lp->lp_refcount > 1);
597
598         lnet_peer_decref_locked(lp);
599 }
600
601 static int
602 lnet_router_checker(void *arg)
603 {
604         static lnet_ping_info_t   pinginfo;
605
606         int                  rc;
607         lnet_handle_md_t     mdh;
608         lnet_peer_t         *rtr;
609         struct list_head    *entry;
610         time_t               now;
611         lnet_process_id_t    rtr_id;
612         int                  secs;
613
614         cfs_daemonize("router_checker");
615         cfs_block_allsigs();
616
617         rtr_id.pid = LUSTRE_SRV_LNET_PID;
618
619         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
620
621         rc = LNetMDBind((lnet_md_t){.start     = &pinginfo,
622                                     .length    = sizeof(pinginfo),
623                                     .threshold = LNET_MD_THRESH_INF,
624                                     .options   = LNET_MD_TRUNCATE,
625                                     .eq_handle = the_lnet.ln_rc_eqh},
626                         LNET_UNLINK,
627                         &mdh);
628
629         if (rc < 0) {
630                 CERROR("Can't bind MD: %d\n", rc);
631                 the_lnet.ln_rc_state = rc;
632                 mutex_up(&the_lnet.ln_rc_signal);
633                 return rc;
634         }
635
636         LASSERT (rc == 0);
637
638         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
639         mutex_up(&the_lnet.ln_rc_signal);       /* let my parent go */
640
641         while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
642                 __u64 version;
643
644                 LNET_LOCK();
645 rescan:
646                 version = the_lnet.ln_routers_version;
647
648                 list_for_each (entry, &the_lnet.ln_routers) {
649                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
650
651                         lnet_peer_addref_locked(rtr);
652
653                         now = cfs_time_current_sec();
654
655                         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
656                             now > rtr->lp_ping_deadline)
657                                 lnet_notify_locked(rtr, 1, 0, now);
658
659                         LNET_UNLOCK();
660
661                         /* Run any outstanding notificiations */
662                         lnet_do_notify(rtr);
663
664                         if (rtr->lp_alive) {
665                                 secs = live_router_check_interval;
666                         } else {
667                                 secs = dead_router_check_interval;
668                         }
669                         if (secs <= 0)
670                                 secs = 0;
671                         
672                         if (secs != 0 &&
673                             !rtr->lp_ping_notsent &&
674                             now > rtr->lp_ping_timestamp + secs) {
675                                 CDEBUG(D_NET, "Check: %s\n",
676                                        libcfs_nid2str(rtr->lp_nid));
677
678                                 LNET_LOCK();
679                                 rtr_id.nid = rtr->lp_nid;
680                                 rtr->lp_ping_notsent = 1;
681                                 rtr->lp_ping_timestamp = now;
682
683                                 if (rtr->lp_ping_deadline == 0)
684                                         rtr->lp_ping_deadline = 
685                                                 now + router_ping_timeout;
686
687                                 LNET_UNLOCK();
688
689                                 LNetGet(LNET_NID_ANY, mdh, rtr_id,
690                                         LNET_RESERVED_PORTAL,
691                                         LNET_PROTO_PING_MATCHBITS, 0);
692                         }
693                         
694                         LNET_LOCK();
695                         lnet_peer_decref_locked(rtr);
696
697                         if (version != the_lnet.ln_routers_version) {
698                                 /* the routers list has changed */
699                                 goto rescan;
700                         }
701                 }
702
703                 LNET_UNLOCK();
704
705                 /* Call cfs_pause() here always adds 1 to load average 
706                  * because kernel counts # active tasks as nr_running 
707                  * + nr_uninterruptible. */
708                 set_current_state(CFS_TASK_INTERRUPTIBLE);
709                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
710                                      cfs_time_seconds(1));
711         }
712
713         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_STOPTHREAD);
714         the_lnet.ln_rc_state = LNET_RC_STATE_UNLINKING;
715         
716         rc = LNetMDUnlink(mdh);
717         LASSERT (rc == 0);
718
719         /* The unlink event callback will signal final completion */
720
721         return 0;
722 }
723
724
725 void
726 lnet_wait_known_routerstate(void)
727 {
728         lnet_peer_t         *rtr;
729         struct list_head    *entry;
730         int                  all_known;
731
732         for (;;) {
733                 LNET_LOCK();
734                 
735                 all_known = 1;
736                 list_for_each (entry, &the_lnet.ln_routers) {
737                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
738                 
739                         if (rtr->lp_alive_count == 0) {
740                                 all_known = 0;
741                                 break;
742                         }
743                 }
744
745                 LNET_UNLOCK();
746
747                 if (all_known)
748                         return;
749
750                 cfs_pause(cfs_time_seconds(1));
751         }
752 }
753
754 void
755 lnet_router_checker_stop(void)
756 {
757         int       rc;
758
759         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING ||
760                  the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
761
762         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
763                 return;
764
765         the_lnet.ln_rc_state = LNET_RC_STATE_STOPTHREAD;
766         /* block until event callback signals exit */
767         mutex_down(&the_lnet.ln_rc_signal);
768
769         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_UNLINKED);
770
771         rc = LNetEQFree(the_lnet.ln_rc_eqh);
772         LASSERT (rc == 0);
773         
774         the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
775 }
776
777 int
778 lnet_router_checker_start(void)
779 {
780         int  rc;
781
782         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
783
784         if (check_routers_before_use &&
785             dead_router_check_interval <= 0) {
786                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
787                                    " set if 'check_routers_before_use' is set"
788                                    "\n");
789                 return -EINVAL;
790         }
791         
792         if (live_router_check_interval <= 0 &&
793             dead_router_check_interval <= 0)
794                 return 0;
795
796         init_mutex_locked(&the_lnet.ln_rc_signal);
797
798         /* EQ size doesn't matter; the callback is guaranteed to get every
799          * event */
800         rc = LNetEQAlloc(1, lnet_router_checker_event,
801                          &the_lnet.ln_rc_eqh);
802         if (rc != 0) {
803                 CERROR("Can't allocate EQ: %d\n", rc);
804                 return -ENOMEM;
805         }
806
807         rc = (int)cfs_kernel_thread(lnet_router_checker, NULL, 0);
808         if (rc < 0) {
809                 CERROR("Can't start router checker thread: %d\n", rc);
810                 goto failed;
811         }
812
813         mutex_down(&the_lnet.ln_rc_signal);     /* wait for checker to startup */
814
815         rc = the_lnet.ln_rc_state;
816         if (rc < 0) {
817                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
818                 goto failed;
819         }
820         
821         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
822
823         if (check_routers_before_use) {
824                 /* Note that a helpful side-effect of pinging all known routers
825                  * at startup is that it makes them drop stale connections they
826                  * may have to a previous instance of me. */
827                 lnet_wait_known_routerstate();
828         }
829         
830         return 0;
831         
832  failed:
833         rc = LNetEQFree(the_lnet.ln_rc_eqh);
834         LASSERT (rc == 0);
835         return rc;
836 }
837
838 void
839 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
840 {
841         int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
842
843         while (--npages >= 0)
844                 cfs_free_page(rb->rb_kiov[npages].kiov_page);
845
846         LIBCFS_FREE(rb, sz);
847 }
848
849 lnet_rtrbuf_t *
850 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp)
851 {
852         int            npages = rbp->rbp_npages;
853         int            sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
854         struct page   *page;
855         lnet_rtrbuf_t *rb;
856         int            i;
857
858         LIBCFS_ALLOC(rb, sz);
859         if (rb == NULL)
860                 return NULL;
861
862         rb->rb_pool = rbp;
863
864         for (i = 0; i < npages; i++) {
865                 page = cfs_alloc_page(CFS_ALLOC_ZERO | CFS_ALLOC_STD);
866                 if (page == NULL) {
867                         while (--i >= 0)
868                                 cfs_free_page(rb->rb_kiov[i].kiov_page);
869
870                         LIBCFS_FREE(rb, sz);
871                         return NULL;
872                 }
873
874                 rb->rb_kiov[i].kiov_len = CFS_PAGE_SIZE;
875                 rb->rb_kiov[i].kiov_offset = 0;
876                 rb->rb_kiov[i].kiov_page = page;
877         }
878
879         return rb;
880 }
881
882 void
883 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
884 {
885         int            npages = rbp->rbp_npages;
886         int            nbuffers = 0;
887         lnet_rtrbuf_t *rb;
888
889         LASSERT (list_empty(&rbp->rbp_msgs));
890         LASSERT (rbp->rbp_credits == rbp->rbp_nbuffers);
891
892         while (!list_empty(&rbp->rbp_bufs)) {
893                 LASSERT (rbp->rbp_credits > 0);
894
895                 rb = list_entry(rbp->rbp_bufs.next,
896                                 lnet_rtrbuf_t, rb_list);
897                 list_del(&rb->rb_list);
898                 lnet_destroy_rtrbuf(rb, npages);
899                 nbuffers++;
900         }
901
902         LASSERT (rbp->rbp_nbuffers == nbuffers);
903         LASSERT (rbp->rbp_credits == nbuffers);
904
905         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
906 }
907
908 int
909 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs)
910 {
911         lnet_rtrbuf_t *rb;
912         int            i;
913
914         if (rbp->rbp_nbuffers != 0) {
915                 LASSERT (rbp->rbp_nbuffers == nbufs);
916                 return 0;
917         }
918         
919         for (i = 0; i < nbufs; i++) {
920                 rb = lnet_new_rtrbuf(rbp);
921
922                 if (rb == NULL) {
923                         CERROR("Failed to allocate %d router bufs of %d pages\n",
924                                nbufs, rbp->rbp_npages);
925                         return -ENOMEM;
926                 }
927
928                 rbp->rbp_nbuffers++;
929                 rbp->rbp_credits++;
930                 rbp->rbp_mincredits++;
931                 list_add(&rb->rb_list, &rbp->rbp_bufs);
932
933                 /* No allocation "under fire" */
934                 /* Otherwise we'd need code to schedule blocked msgs etc */
935                 LASSERT (!the_lnet.ln_routing);
936         }
937
938         LASSERT (rbp->rbp_credits == nbufs);
939         return 0;
940 }
941
942 void
943 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
944 {
945         CFS_INIT_LIST_HEAD(&rbp->rbp_msgs);
946         CFS_INIT_LIST_HEAD(&rbp->rbp_bufs);
947
948         rbp->rbp_npages = npages;
949         rbp->rbp_credits = 0;
950         rbp->rbp_mincredits = 0;
951 }
952
953 void
954 lnet_free_rtrpools(void)
955 {
956         lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[0]);
957         lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[1]);
958         lnet_rtrpool_free_bufs(&the_lnet.ln_rtrpools[2]);
959 }
960
961 void
962 lnet_init_rtrpools(void)
963 {
964         int small_pages = 1;
965         int large_pages = (LNET_MTU + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
966
967         lnet_rtrpool_init(&the_lnet.ln_rtrpools[0], 0);
968         lnet_rtrpool_init(&the_lnet.ln_rtrpools[1], small_pages);
969         lnet_rtrpool_init(&the_lnet.ln_rtrpools[2], large_pages);
970 }
971
972
973 int
974 lnet_alloc_rtrpools(int im_a_router)
975 {
976         int       rc;
977         
978         if (!strcmp(forwarding, "")) {
979                 /* not set either way */
980                 if (!im_a_router)
981                         return 0;
982         } else if (!strcmp(forwarding, "disabled")) {
983                 /* explicitly disabled */
984                 return 0;
985         } else if (!strcmp(forwarding, "enabled")) {
986                 /* explicitly enabled */
987         } else {
988                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
989                                    "'enabled' or 'disabled'\n");
990                 return -EINVAL;
991         }
992         
993         if (tiny_router_buffers <= 0) {
994                 LCONSOLE_ERROR_MSG(0x10c, "tiny_router_buffers=%d invalid when "
995                                    "routing enabled\n", tiny_router_buffers);
996                 rc = -EINVAL;
997                 goto failed;
998         }
999
1000         rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[0],
1001                                      tiny_router_buffers);
1002         if (rc != 0)
1003                 goto failed;
1004
1005         if (small_router_buffers <= 0) {
1006                 LCONSOLE_ERROR_MSG(0x10d, "small_router_buffers=%d invalid when"
1007                                    " routing enabled\n", small_router_buffers);
1008                 rc = -EINVAL;
1009                 goto failed;
1010         }
1011
1012         rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[1],
1013                                      small_router_buffers);
1014         if (rc != 0)
1015                 goto failed;
1016
1017         if (large_router_buffers <= 0) {
1018                 LCONSOLE_ERROR_MSG(0x10e, "large_router_buffers=%d invalid when"
1019                                    " routing enabled\n", large_router_buffers);
1020                 rc = -EINVAL;
1021                 goto failed;
1022         }
1023
1024         rc = lnet_rtrpool_alloc_bufs(&the_lnet.ln_rtrpools[2],
1025                                      large_router_buffers);
1026         if (rc != 0)
1027                 goto failed;
1028
1029         LNET_LOCK();
1030         the_lnet.ln_routing = 1;
1031         LNET_UNLOCK();
1032         
1033         return 0;
1034
1035  failed:
1036         lnet_free_rtrpools();
1037         return rc;
1038 }
1039
1040 #else
1041
1042 int
1043 lnet_peers_start_down(void)
1044 {
1045         return 0;
1046 }
1047
1048 void
1049 lnet_router_checker_stop(void)
1050 {
1051         return;
1052 }
1053
1054 int
1055 lnet_router_checker_start(void)
1056 {
1057         return 0;
1058 }
1059
1060 void
1061 lnet_free_rtrpools (void)
1062 {
1063 }
1064
1065 void
1066 lnet_init_rtrpools (void)
1067 {
1068 }
1069
1070 int
1071 lnet_alloc_rtrpools (int im_a_arouter)
1072 {
1073         return 0;
1074 }
1075
1076 #endif