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