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