OSDN Git Service

staging: lustre: Coalesce string fragments
[android-x86/kernel.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 "../../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 module_param(forwarding, charp, 0444);
38 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
39
40 static int tiny_router_buffers;
41 module_param(tiny_router_buffers, int, 0444);
42 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
43 static int small_router_buffers;
44 module_param(small_router_buffers, int, 0444);
45 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
46 static int large_router_buffers;
47 module_param(large_router_buffers, int, 0444);
48 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
49 static int peer_buffer_credits = 0;
50 module_param(peer_buffer_credits, int, 0444);
51 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
52
53 static int auto_down = 1;
54 module_param(auto_down, int, 0444);
55 MODULE_PARM_DESC(auto_down, "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 module_param(check_routers_before_use, int, 0444);
85 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
86
87 static int avoid_asym_router_failure = 1;
88 module_param(avoid_asym_router_failure, int, 0644);
89 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
90
91 static int dead_router_check_interval = 60;
92 module_param(dead_router_check_interval, int, 0644);
93 MODULE_PARM_DESC(dead_router_check_interval, "Seconds between dead router health checks (<= 0 to disable)");
94
95 static int live_router_check_interval = 60;
96 module_param(live_router_check_interval, int, 0644);
97 MODULE_PARM_DESC(live_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
98
99 static int router_ping_timeout = 50;
100 module_param(router_ping_timeout, int, 0644);
101 MODULE_PARM_DESC(router_ping_timeout, "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, unsigned long when)
111 {
112         if (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 static 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 || ni == NULL)
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 static 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                unsigned int priority)
306 {
307         struct list_head          *e;
308         lnet_remotenet_t    *rnet;
309         lnet_remotenet_t    *rnet2;
310         lnet_route_t    *route;
311         lnet_ni_t          *ni;
312         int               add_route;
313         int               rc;
314
315         CDEBUG(D_NET, "Add route: net %s hops %u priority %u gw %s\n",
316                libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
317
318         if (gateway == LNET_NID_ANY ||
319             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
320             net == LNET_NIDNET(LNET_NID_ANY) ||
321             LNET_NETTYP(net) == LOLND ||
322             LNET_NIDNET(gateway) == net ||
323             hops < 1 || hops > 255)
324                 return -EINVAL;
325
326         if (lnet_islocalnet(net))              /* it's a local network */
327                 return 0;                      /* ignore the route entry */
328
329         /* Assume net, route, all new */
330         LIBCFS_ALLOC(route, sizeof(*route));
331         LIBCFS_ALLOC(rnet, sizeof(*rnet));
332         if (route == NULL || rnet == NULL) {
333                 CERROR("Out of memory creating route %s %d %s\n",
334                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
335                 if (route != NULL)
336                         LIBCFS_FREE(route, sizeof(*route));
337                 if (rnet != NULL)
338                         LIBCFS_FREE(rnet, sizeof(*rnet));
339                 return -ENOMEM;
340         }
341
342         INIT_LIST_HEAD(&rnet->lrn_routes);
343         rnet->lrn_net = net;
344         route->lr_hops = hops;
345         route->lr_net = net;
346         route->lr_priority = priority;
347
348         lnet_net_lock(LNET_LOCK_EX);
349
350         rc = lnet_nid2peer_locked(&route->lr_gateway, gateway, LNET_LOCK_EX);
351         if (rc != 0) {
352                 lnet_net_unlock(LNET_LOCK_EX);
353
354                 LIBCFS_FREE(route, sizeof(*route));
355                 LIBCFS_FREE(rnet, sizeof(*rnet));
356
357                 if (rc == -EHOSTUNREACH) { /* gateway is not on a local net */
358                         return 0;       /* ignore the route entry */
359                 } else {
360                         CERROR("Error %d creating route %s %d %s\n", rc,
361                                libcfs_net2str(net), hops,
362                                libcfs_nid2str(gateway));
363                 }
364                 return rc;
365         }
366
367         LASSERT (!the_lnet.ln_shutdown);
368
369         rnet2 = lnet_find_net_locked(net);
370         if (rnet2 == NULL) {
371                 /* new network */
372                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
373                 rnet2 = rnet;
374         }
375
376         /* Search for a duplicate route (it's a NOOP if it is) */
377         add_route = 1;
378         list_for_each (e, &rnet2->lrn_routes) {
379                 lnet_route_t *route2 = list_entry(e, lnet_route_t, lr_list);
380
381                 if (route2->lr_gateway == route->lr_gateway) {
382                         add_route = 0;
383                         break;
384                 }
385
386                 /* our lookups must be true */
387                 LASSERT (route2->lr_gateway->lp_nid != gateway);
388         }
389
390         if (add_route) {
391                 lnet_peer_addref_locked(route->lr_gateway); /* +1 for notify */
392                 lnet_add_route_to_rnet(rnet2, route);
393
394                 ni = route->lr_gateway->lp_ni;
395                 lnet_net_unlock(LNET_LOCK_EX);
396
397                 /* XXX Assume alive */
398                 if (ni->ni_lnd->lnd_notify != NULL)
399                         (ni->ni_lnd->lnd_notify)(ni, gateway, 1);
400
401                 lnet_net_lock(LNET_LOCK_EX);
402         }
403
404         /* -1 for notify or !add_route */
405         lnet_peer_decref_locked(route->lr_gateway);
406         lnet_net_unlock(LNET_LOCK_EX);
407
408         if (!add_route)
409                 LIBCFS_FREE(route, sizeof(*route));
410
411         if (rnet != rnet2)
412                 LIBCFS_FREE(rnet, sizeof(*rnet));
413
414         return 0;
415 }
416
417 int
418 lnet_check_routes(void)
419 {
420         lnet_remotenet_t        *rnet;
421         lnet_route_t            *route;
422         lnet_route_t            *route2;
423         struct list_head                *e1;
424         struct list_head                *e2;
425         int                     cpt;
426         struct list_head                *rn_list;
427         int                     i;
428
429         cpt = lnet_net_lock_current();
430
431         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
432                 rn_list = &the_lnet.ln_remote_nets_hash[i];
433                 list_for_each(e1, rn_list) {
434                         rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
435
436                         route2 = NULL;
437                         list_for_each(e2, &rnet->lrn_routes) {
438                                 lnet_nid_t      nid1;
439                                 lnet_nid_t      nid2;
440                                 int             net;
441
442                                 route = list_entry(e2, lnet_route_t,
443                                                        lr_list);
444
445                                 if (route2 == NULL) {
446                                         route2 = route;
447                                         continue;
448                                 }
449
450                                 if (route->lr_gateway->lp_ni ==
451                                     route2->lr_gateway->lp_ni)
452                                         continue;
453
454                                 nid1 = route->lr_gateway->lp_nid;
455                                 nid2 = route2->lr_gateway->lp_nid;
456                                 net = rnet->lrn_net;
457
458                                 lnet_net_unlock(cpt);
459
460                                 CERROR("Routes to %s via %s and %s not supported\n",
461                                        libcfs_net2str(net),
462                                        libcfs_nid2str(nid1),
463                                        libcfs_nid2str(nid2));
464                                 return -EINVAL;
465                         }
466                 }
467         }
468
469         lnet_net_unlock(cpt);
470         return 0;
471 }
472
473 int
474 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
475 {
476         struct lnet_peer        *gateway;
477         lnet_remotenet_t        *rnet;
478         lnet_route_t            *route;
479         struct list_head                *e1;
480         struct list_head                *e2;
481         int                     rc = -ENOENT;
482         struct list_head                *rn_list;
483         int                     idx = 0;
484
485         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
486                libcfs_net2str(net), libcfs_nid2str(gw_nid));
487
488         /* NB Caller may specify either all routes via the given gateway
489          * or a specific route entry actual NIDs) */
490
491         lnet_net_lock(LNET_LOCK_EX);
492         if (net == LNET_NIDNET(LNET_NID_ANY))
493                 rn_list = &the_lnet.ln_remote_nets_hash[0];
494         else
495                 rn_list = lnet_net2rnethash(net);
496
497  again:
498         list_for_each(e1, rn_list) {
499                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
500
501                 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
502                         net == rnet->lrn_net))
503                         continue;
504
505                 list_for_each(e2, &rnet->lrn_routes) {
506                         route = list_entry(e2, lnet_route_t, lr_list);
507
508                         gateway = route->lr_gateway;
509                         if (!(gw_nid == LNET_NID_ANY ||
510                               gw_nid == gateway->lp_nid))
511                                 continue;
512
513                         list_del(&route->lr_list);
514                         list_del(&route->lr_gwlist);
515                         the_lnet.ln_remote_nets_version++;
516
517                         if (list_empty(&rnet->lrn_routes))
518                                 list_del(&rnet->lrn_list);
519                         else
520                                 rnet = NULL;
521
522                         lnet_rtr_decref_locked(gateway);
523                         lnet_peer_decref_locked(gateway);
524
525                         lnet_net_unlock(LNET_LOCK_EX);
526
527                         LIBCFS_FREE(route, sizeof(*route));
528
529                         if (rnet != NULL)
530                                 LIBCFS_FREE(rnet, sizeof(*rnet));
531
532                         rc = 0;
533                         lnet_net_lock(LNET_LOCK_EX);
534                         goto again;
535                 }
536         }
537
538         if (net == LNET_NIDNET(LNET_NID_ANY) &&
539             ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
540                 rn_list = &the_lnet.ln_remote_nets_hash[idx];
541                 goto again;
542         }
543         lnet_net_unlock(LNET_LOCK_EX);
544
545         return rc;
546 }
547
548 void
549 lnet_destroy_routes (void)
550 {
551         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
552 }
553
554 int
555 lnet_get_route(int idx, __u32 *net, __u32 *hops,
556                lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
557 {
558         struct list_head                *e1;
559         struct list_head                *e2;
560         lnet_remotenet_t        *rnet;
561         lnet_route_t            *route;
562         int                     cpt;
563         int                     i;
564         struct list_head                *rn_list;
565
566         cpt = lnet_net_lock_current();
567
568         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
569                 rn_list = &the_lnet.ln_remote_nets_hash[i];
570                 list_for_each(e1, rn_list) {
571                         rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
572
573                         list_for_each(e2, &rnet->lrn_routes) {
574                                 route = list_entry(e2, lnet_route_t,
575                                                        lr_list);
576
577                                 if (idx-- == 0) {
578                                         *net      = rnet->lrn_net;
579                                         *hops     = route->lr_hops;
580                                         *priority = route->lr_priority;
581                                         *gateway  = route->lr_gateway->lp_nid;
582                                         *alive    = route->lr_gateway->lp_alive;
583                                         lnet_net_unlock(cpt);
584                                         return 0;
585                                 }
586                         }
587                 }
588         }
589
590         lnet_net_unlock(cpt);
591         return -ENOENT;
592 }
593
594 void
595 lnet_swap_pinginfo(lnet_ping_info_t *info)
596 {
597         int            i;
598         lnet_ni_status_t *stat;
599
600         __swab32s(&info->pi_magic);
601         __swab32s(&info->pi_features);
602         __swab32s(&info->pi_pid);
603         __swab32s(&info->pi_nnis);
604         for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
605                 stat = &info->pi_ni[i];
606                 __swab64s(&stat->ns_nid);
607                 __swab32s(&stat->ns_status);
608         }
609         return;
610 }
611
612 /**
613  * parse router-checker pinginfo, record number of down NIs for remote
614  * networks on that router.
615  */
616 static void
617 lnet_parse_rc_info(lnet_rc_data_t *rcd)
618 {
619         lnet_ping_info_t        *info = rcd->rcd_pinginfo;
620         struct lnet_peer        *gw   = rcd->rcd_gateway;
621         lnet_route_t            *rtr;
622
623         if (!gw->lp_alive)
624                 return;
625
626         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
627                 lnet_swap_pinginfo(info);
628
629         /* NB always racing with network! */
630         if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
631                 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
632                        libcfs_nid2str(gw->lp_nid), info->pi_magic);
633                 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
634                 return;
635         }
636
637         gw->lp_ping_feats = info->pi_features;
638         if ((gw->lp_ping_feats & LNET_PING_FEAT_MASK) == 0) {
639                 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
640                        libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
641                 return; /* nothing I can understand */
642         }
643
644         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) == 0)
645                 return; /* can't carry NI status info */
646
647         list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
648                 int     ptl_status = LNET_NI_STATUS_INVALID;
649                 int     down = 0;
650                 int     up = 0;
651                 int     i;
652
653                 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
654                         lnet_ni_status_t *stat = &info->pi_ni[i];
655                         lnet_nid_t       nid = stat->ns_nid;
656
657                         if (nid == LNET_NID_ANY) {
658                                 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
659                                        libcfs_nid2str(gw->lp_nid));
660                                 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
661                                 return;
662                         }
663
664                         if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
665                                 continue;
666
667                         if (stat->ns_status == LNET_NI_STATUS_DOWN) {
668                                 if (LNET_NETTYP(LNET_NIDNET(nid)) != PTLLND)
669                                         down++;
670                                 else if (ptl_status != LNET_NI_STATUS_UP)
671                                         ptl_status = LNET_NI_STATUS_DOWN;
672                                 continue;
673                         }
674
675                         if (stat->ns_status == LNET_NI_STATUS_UP) {
676                                 if (LNET_NIDNET(nid) == rtr->lr_net) {
677                                         up = 1;
678                                         break;
679                                 }
680                                 /* ptl NIs are considered down only when
681                                  * they're all down */
682                                 if (LNET_NETTYP(LNET_NIDNET(nid)) == PTLLND)
683                                         ptl_status = LNET_NI_STATUS_UP;
684                                 continue;
685                         }
686
687                         CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
688                                libcfs_nid2str(gw->lp_nid), stat->ns_status);
689                         gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
690                         return;
691                 }
692
693                 if (up) { /* ignore downed NIs if NI for dest network is up */
694                         rtr->lr_downis = 0;
695                         continue;
696                 }
697                 rtr->lr_downis = down + (ptl_status == LNET_NI_STATUS_DOWN);
698         }
699 }
700
701 static void
702 lnet_router_checker_event(lnet_event_t *event)
703 {
704         lnet_rc_data_t          *rcd = event->md.user_ptr;
705         struct lnet_peer        *lp;
706
707         LASSERT(rcd != NULL);
708
709         if (event->unlinked) {
710                 LNetInvalidateHandle(&rcd->rcd_mdh);
711                 return;
712         }
713
714         LASSERT(event->type == LNET_EVENT_SEND ||
715                 event->type == LNET_EVENT_REPLY);
716
717         lp = rcd->rcd_gateway;
718         LASSERT(lp != NULL);
719
720          /* NB: it's called with holding lnet_res_lock, we have a few
721           * places need to hold both locks at the same time, please take
722           * care of lock ordering */
723         lnet_net_lock(lp->lp_cpt);
724         if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
725                 /* ignore if no longer a router or rcd is replaced */
726                 goto out;
727         }
728
729         if (event->type == LNET_EVENT_SEND) {
730                 lp->lp_ping_notsent = 0;
731                 if (event->status == 0)
732                         goto out;
733         }
734
735         /* LNET_EVENT_REPLY */
736         /* A successful REPLY means the router is up.  If _any_ comms
737          * to the router fail I assume it's down (this will happen if
738          * we ping alive routers to try to detect router death before
739          * apps get burned). */
740
741         lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
742         /* The router checker will wake up very shortly and do the
743          * actual notification.
744          * XXX If 'lp' stops being a router before then, it will still
745          * have the notification pending!!! */
746
747         if (avoid_asym_router_failure && event->status == 0)
748                 lnet_parse_rc_info(rcd);
749
750  out:
751         lnet_net_unlock(lp->lp_cpt);
752 }
753
754 void
755 lnet_wait_known_routerstate(void)
756 {
757         lnet_peer_t      *rtr;
758         struct list_head          *entry;
759         int               all_known;
760
761         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
762
763         for (;;) {
764                 int     cpt = lnet_net_lock_current();
765
766                 all_known = 1;
767                 list_for_each (entry, &the_lnet.ln_routers) {
768                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
769
770                         if (rtr->lp_alive_count == 0) {
771                                 all_known = 0;
772                                 break;
773                         }
774                 }
775
776                 lnet_net_unlock(cpt);
777
778                 if (all_known)
779                         return;
780
781                 set_current_state(TASK_UNINTERRUPTIBLE);
782                 schedule_timeout(cfs_time_seconds(1));
783         }
784 }
785
786 void
787 lnet_update_ni_status_locked(void)
788 {
789         lnet_ni_t       *ni;
790         long            now;
791         int             timeout;
792
793         LASSERT(the_lnet.ln_routing);
794
795         timeout = router_ping_timeout +
796                   MAX(live_router_check_interval, dead_router_check_interval);
797
798         now = get_seconds();
799         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
800                 if (ni->ni_lnd->lnd_type == LOLND)
801                         continue;
802
803                 if (now < ni->ni_last_alive + timeout)
804                         continue;
805
806                 lnet_ni_lock(ni);
807                 /* re-check with lock */
808                 if (now < ni->ni_last_alive + timeout) {
809                         lnet_ni_unlock(ni);
810                         continue;
811                 }
812
813                 LASSERT(ni->ni_status != NULL);
814
815                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
816                         CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
817                                libcfs_nid2str(ni->ni_nid), timeout);
818                         /* NB: so far, this is the only place to set
819                          * NI status to "down" */
820                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
821                 }
822                 lnet_ni_unlock(ni);
823         }
824 }
825
826 void
827 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
828 {
829         LASSERT(list_empty(&rcd->rcd_list));
830         /* detached from network */
831         LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
832
833         if (rcd->rcd_gateway != NULL) {
834                 int cpt = rcd->rcd_gateway->lp_cpt;
835
836                 lnet_net_lock(cpt);
837                 lnet_peer_decref_locked(rcd->rcd_gateway);
838                 lnet_net_unlock(cpt);
839         }
840
841         if (rcd->rcd_pinginfo != NULL)
842                 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
843
844         LIBCFS_FREE(rcd, sizeof(*rcd));
845 }
846
847 lnet_rc_data_t *
848 lnet_create_rc_data_locked(lnet_peer_t *gateway)
849 {
850         lnet_rc_data_t          *rcd = NULL;
851         lnet_ping_info_t        *pi;
852         int                     rc;
853         int                     i;
854
855         lnet_net_unlock(gateway->lp_cpt);
856
857         LIBCFS_ALLOC(rcd, sizeof(*rcd));
858         if (rcd == NULL)
859                 goto out;
860
861         LNetInvalidateHandle(&rcd->rcd_mdh);
862         INIT_LIST_HEAD(&rcd->rcd_list);
863
864         LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
865         if (pi == NULL)
866                 goto out;
867
868         for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
869                 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
870                 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
871         }
872         rcd->rcd_pinginfo = pi;
873
874         LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
875         rc = LNetMDBind((lnet_md_t){.start     = pi,
876                                     .user_ptr  = rcd,
877                                     .length    = LNET_PINGINFO_SIZE,
878                                     .threshold = LNET_MD_THRESH_INF,
879                                     .options   = LNET_MD_TRUNCATE,
880                                     .eq_handle = the_lnet.ln_rc_eqh},
881                         LNET_UNLINK,
882                         &rcd->rcd_mdh);
883         if (rc < 0) {
884                 CERROR("Can't bind MD: %d\n", rc);
885                 goto out;
886         }
887         LASSERT(rc == 0);
888
889         lnet_net_lock(gateway->lp_cpt);
890         /* router table changed or someone has created rcd for this gateway */
891         if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
892                 lnet_net_unlock(gateway->lp_cpt);
893                 goto out;
894         }
895
896         lnet_peer_addref_locked(gateway);
897         rcd->rcd_gateway = gateway;
898         gateway->lp_rcd = rcd;
899         gateway->lp_ping_notsent = 0;
900
901         return rcd;
902
903  out:
904         if (rcd != NULL) {
905                 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
906                         rc = LNetMDUnlink(rcd->rcd_mdh);
907                         LASSERT(rc == 0);
908                 }
909                 lnet_destroy_rc_data(rcd);
910         }
911
912         lnet_net_lock(gateway->lp_cpt);
913         return gateway->lp_rcd;
914 }
915
916 static int
917 lnet_router_check_interval (lnet_peer_t *rtr)
918 {
919         int secs;
920
921         secs = rtr->lp_alive ? live_router_check_interval :
922                                dead_router_check_interval;
923         if (secs < 0)
924                 secs = 0;
925
926         return secs;
927 }
928
929 static void
930 lnet_ping_router_locked (lnet_peer_t *rtr)
931 {
932         lnet_rc_data_t *rcd = NULL;
933         unsigned long      now = cfs_time_current();
934         int          secs;
935
936         lnet_peer_addref_locked(rtr);
937
938         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
939             cfs_time_after(now, rtr->lp_ping_deadline))
940                 lnet_notify_locked(rtr, 1, 0, now);
941
942         /* Run any outstanding notifications */
943         lnet_ni_notify_locked(rtr->lp_ni, rtr);
944
945         if (!lnet_isrouter(rtr) ||
946             the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
947                 /* router table changed or router checker is shutting down */
948                 lnet_peer_decref_locked(rtr);
949                 return;
950         }
951
952         rcd = rtr->lp_rcd != NULL ?
953               rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
954
955         if (rcd == NULL)
956                 return;
957
958         secs = lnet_router_check_interval(rtr);
959
960         CDEBUG(D_NET,
961                "rtr %s %d: deadline %lu ping_notsent %d alive %d alive_count %d lp_ping_timestamp %lu\n",
962                libcfs_nid2str(rtr->lp_nid), secs,
963                rtr->lp_ping_deadline, rtr->lp_ping_notsent,
964                rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
965
966         if (secs != 0 && !rtr->lp_ping_notsent &&
967             cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
968                                              cfs_time_seconds(secs)))) {
969                 int            rc;
970                 lnet_process_id_t id;
971                 lnet_handle_md_t  mdh;
972
973                 id.nid = rtr->lp_nid;
974                 id.pid = LUSTRE_SRV_LNET_PID;
975                 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
976
977                 rtr->lp_ping_notsent   = 1;
978                 rtr->lp_ping_timestamp = now;
979
980                 mdh = rcd->rcd_mdh;
981
982                 if (rtr->lp_ping_deadline == 0) {
983                         rtr->lp_ping_deadline =
984                                 cfs_time_shift(router_ping_timeout);
985                 }
986
987                 lnet_net_unlock(rtr->lp_cpt);
988
989                 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
990                              LNET_PROTO_PING_MATCHBITS, 0);
991
992                 lnet_net_lock(rtr->lp_cpt);
993                 if (rc != 0)
994                         rtr->lp_ping_notsent = 0; /* no event pending */
995         }
996
997         lnet_peer_decref_locked(rtr);
998         return;
999 }
1000
1001 int
1002 lnet_router_checker_start(void)
1003 {
1004         int       rc;
1005         int       eqsz;
1006
1007         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1008
1009         if (check_routers_before_use &&
1010             dead_router_check_interval <= 0) {
1011                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be set if 'check_routers_before_use' is set\n");
1012                 return -EINVAL;
1013         }
1014
1015         if (!the_lnet.ln_routing &&
1016             live_router_check_interval <= 0 &&
1017             dead_router_check_interval <= 0)
1018                 return 0;
1019
1020         sema_init(&the_lnet.ln_rc_signal, 0);
1021         /* EQ size doesn't matter; the callback is guaranteed to get every
1022          * event */
1023         eqsz = 0;
1024         rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1025                          &the_lnet.ln_rc_eqh);
1026         if (rc != 0) {
1027                 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1028                 return -ENOMEM;
1029         }
1030
1031         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1032         rc = PTR_ERR(kthread_run(lnet_router_checker,
1033                                  NULL, "router_checker"));
1034         if (IS_ERR_VALUE(rc)) {
1035                 CERROR("Can't start router checker thread: %d\n", rc);
1036                 /* block until event callback signals exit */
1037                 down(&the_lnet.ln_rc_signal);
1038                 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1039                 LASSERT(rc == 0);
1040                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1041                 return -ENOMEM;
1042         }
1043
1044         if (check_routers_before_use) {
1045                 /* Note that a helpful side-effect of pinging all known routers
1046                  * at startup is that it makes them drop stale connections they
1047                  * may have to a previous instance of me. */
1048                 lnet_wait_known_routerstate();
1049         }
1050
1051         return 0;
1052 }
1053
1054 void
1055 lnet_router_checker_stop (void)
1056 {
1057         int rc;
1058
1059         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1060                 return;
1061
1062         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1063         the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1064
1065         /* block until event callback signals exit */
1066         down(&the_lnet.ln_rc_signal);
1067         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1068
1069         rc = LNetEQFree(the_lnet.ln_rc_eqh);
1070         LASSERT (rc == 0);
1071         return;
1072 }
1073
1074 static void
1075 lnet_prune_rc_data(int wait_unlink)
1076 {
1077         lnet_rc_data_t          *rcd;
1078         lnet_rc_data_t          *tmp;
1079         lnet_peer_t             *lp;
1080         struct list_head                head;
1081         int                     i = 2;
1082
1083         if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1084                    list_empty(&the_lnet.ln_rcd_deathrow) &&
1085                    list_empty(&the_lnet.ln_rcd_zombie)))
1086                 return;
1087
1088         INIT_LIST_HEAD(&head);
1089
1090         lnet_net_lock(LNET_LOCK_EX);
1091
1092         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1093                 /* router checker is stopping, prune all */
1094                 list_for_each_entry(lp, &the_lnet.ln_routers,
1095                                         lp_rtr_list) {
1096                         if (lp->lp_rcd == NULL)
1097                                 continue;
1098
1099                         LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1100                         list_add(&lp->lp_rcd->rcd_list,
1101                                      &the_lnet.ln_rcd_deathrow);
1102                         lp->lp_rcd = NULL;
1103                 }
1104         }
1105
1106         /* unlink all RCDs on deathrow list */
1107         list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1108
1109         if (!list_empty(&head)) {
1110                 lnet_net_unlock(LNET_LOCK_EX);
1111
1112                 list_for_each_entry(rcd, &head, rcd_list)
1113                         LNetMDUnlink(rcd->rcd_mdh);
1114
1115                 lnet_net_lock(LNET_LOCK_EX);
1116         }
1117
1118         list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1119
1120         /* release all zombie RCDs */
1121         while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1122                 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1123                                              rcd_list) {
1124                         if (LNetHandleIsInvalid(rcd->rcd_mdh))
1125                                 list_move(&rcd->rcd_list, &head);
1126                 }
1127
1128                 wait_unlink = wait_unlink &&
1129                               !list_empty(&the_lnet.ln_rcd_zombie);
1130
1131                 lnet_net_unlock(LNET_LOCK_EX);
1132
1133                 while (!list_empty(&head)) {
1134                         rcd = list_entry(head.next,
1135                                              lnet_rc_data_t, rcd_list);
1136                         list_del_init(&rcd->rcd_list);
1137                         lnet_destroy_rc_data(rcd);
1138                 }
1139
1140                 if (!wait_unlink)
1141                         return;
1142
1143                 i++;
1144                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1145                        "Waiting for rc buffers to unlink\n");
1146                 set_current_state(TASK_UNINTERRUPTIBLE);
1147                 schedule_timeout(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 schedule_timeout() here always adds 1 to load average
1207                  * because kernel counts # active tasks as nr_running
1208                  * + nr_uninterruptible. */
1209                 set_current_state(TASK_INTERRUPTIBLE);
1210                 schedule_timeout(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 routing enabled\n",
1370                                    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 routing enabled\n",
1389                                    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 routing enabled\n",
1408                                    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 'enabled' or 'disabled'\n");
1442                 return -EINVAL;
1443         }
1444
1445         nrb_tiny = lnet_nrb_tiny_calculate(0);
1446         if (nrb_tiny < 0)
1447                 return -EINVAL;
1448
1449         nrb_small = lnet_nrb_small_calculate(small_pages);
1450         if (nrb_small < 0)
1451                 return -EINVAL;
1452
1453         nrb_large = lnet_nrb_large_calculate(large_pages);
1454         if (nrb_large < 0)
1455                 return -EINVAL;
1456
1457         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1458                                                 LNET_NRBPOOLS *
1459                                                 sizeof(lnet_rtrbufpool_t));
1460         if (the_lnet.ln_rtrpools == NULL) {
1461                 LCONSOLE_ERROR_MSG(0x10c,
1462                                    "Failed to initialize router buffe pool\n");
1463                 return -ENOMEM;
1464         }
1465
1466         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1467                 lnet_rtrpool_init(&rtrp[0], 0);
1468                 rc = lnet_rtrpool_alloc_bufs(&rtrp[0], nrb_tiny, i);
1469                 if (rc != 0)
1470                         goto failed;
1471
1472                 lnet_rtrpool_init(&rtrp[1], small_pages);
1473                 rc = lnet_rtrpool_alloc_bufs(&rtrp[1], nrb_small, i);
1474                 if (rc != 0)
1475                         goto failed;
1476
1477                 lnet_rtrpool_init(&rtrp[2], large_pages);
1478                 rc = lnet_rtrpool_alloc_bufs(&rtrp[2], nrb_large, i);
1479                 if (rc != 0)
1480                         goto failed;
1481         }
1482
1483         lnet_net_lock(LNET_LOCK_EX);
1484         the_lnet.ln_routing = 1;
1485         lnet_net_unlock(LNET_LOCK_EX);
1486
1487         return 0;
1488
1489  failed:
1490         lnet_rtrpools_free();
1491         return rc;
1492 }
1493
1494 int
1495 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, unsigned long when)
1496 {
1497         struct lnet_peer        *lp = NULL;
1498         unsigned long           now = cfs_time_current();
1499         int                     cpt = lnet_cpt_of_nid(nid);
1500
1501         LASSERT (!in_interrupt ());
1502
1503         CDEBUG (D_NET, "%s notifying %s: %s\n",
1504                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1505                 libcfs_nid2str(nid),
1506                 alive ? "up" : "down");
1507
1508         if (ni != NULL &&
1509             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1510                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1511                         libcfs_nid2str(nid), alive ? "birth" : "death",
1512                         libcfs_nid2str(ni->ni_nid));
1513                 return -EINVAL;
1514         }
1515
1516         /* can't do predictions... */
1517         if (cfs_time_after(when, now)) {
1518                 CWARN("Ignoring prediction from %s of %s %s %ld seconds in the future\n",
1519                       (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1520                       libcfs_nid2str(nid), alive ? "up" : "down",
1521                       cfs_duration_sec(cfs_time_sub(when, now)));
1522                 return -EINVAL;
1523         }
1524
1525         if (ni != NULL && !alive &&          /* LND telling me she's down */
1526             !auto_down) {                      /* auto-down disabled */
1527                 CDEBUG(D_NET, "Auto-down disabled\n");
1528                 return 0;
1529         }
1530
1531         lnet_net_lock(cpt);
1532
1533         if (the_lnet.ln_shutdown) {
1534                 lnet_net_unlock(cpt);
1535                 return -ESHUTDOWN;
1536         }
1537
1538         lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1539         if (lp == NULL) {
1540                 /* nid not found */
1541                 lnet_net_unlock(cpt);
1542                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1543                 return 0;
1544         }
1545
1546         /* We can't fully trust LND on reporting exact peer last_alive
1547          * if he notifies us about dead peer. For example ksocklnd can
1548          * call us with when == _time_when_the_node_was_booted_ if
1549          * no connections were successfully established */
1550         if (ni != NULL && !alive && when < lp->lp_last_alive)
1551                 when = lp->lp_last_alive;
1552
1553         lnet_notify_locked(lp, ni == NULL, alive, when);
1554
1555         lnet_ni_notify_locked(ni, lp);
1556
1557         lnet_peer_decref_locked(lp);
1558
1559         lnet_net_unlock(cpt);
1560         return 0;
1561 }
1562 EXPORT_SYMBOL(lnet_notify);
1563
1564 void
1565 lnet_get_tunables (void)
1566 {
1567         return;
1568 }
1569
1570 #else
1571
1572 int
1573 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, unsigned long when)
1574 {
1575         return -EOPNOTSUPP;
1576 }
1577
1578 void
1579 lnet_router_checker (void)
1580 {
1581         static time_t last = 0;
1582         static int    running = 0;
1583
1584         time_t      now = get_seconds();
1585         int            interval = now - last;
1586         int            rc;
1587         __u64        version;
1588         lnet_peer_t      *rtr;
1589
1590         /* It's no use to call me again within a sec - all intervals and
1591          * timeouts are measured in seconds */
1592         if (last != 0 && interval < 2)
1593                 return;
1594
1595         if (last != 0 &&
1596             interval > MAX(live_router_check_interval,
1597                            dead_router_check_interval))
1598                 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1599                         live_router_check_interval, dead_router_check_interval,
1600                         interval);
1601
1602         LASSERT(LNET_CPT_NUMBER == 1);
1603
1604         lnet_net_lock(0);
1605         LASSERT(!running); /* recursion check */
1606         running = 1;
1607         lnet_net_unlock(0);
1608
1609         last = now;
1610
1611         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1612                 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1613
1614         /* consume all pending events */
1615         while (1) {
1616                 int       i;
1617                 lnet_event_t ev;
1618
1619                 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1620                  * recursion breaker in LNetEQPoll would fail */
1621                 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1622                 if (rc == 0)   /* no event pending */
1623                         break;
1624
1625                 /* NB a lost SENT prevents me from pinging a router again */
1626                 if (rc == -EOVERFLOW) {
1627                         CERROR("Dropped an event!!!\n");
1628                         abort();
1629                 }
1630
1631                 LASSERT (rc == 1);
1632
1633                 lnet_router_checker_event(&ev);
1634         }
1635
1636         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1637                 lnet_prune_rc_data(1); /* release rcd */
1638                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1639                 running = 0;
1640                 return;
1641         }
1642
1643         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1644
1645         lnet_net_lock(0);
1646
1647         version = the_lnet.ln_routers_version;
1648         list_for_each_entry (rtr, &the_lnet.ln_routers, lp_rtr_list) {
1649                 lnet_ping_router_locked(rtr);
1650                 LASSERT (version == the_lnet.ln_routers_version);
1651         }
1652
1653         lnet_net_unlock(0);
1654
1655         running = 0; /* lock only needed for the recursion check */
1656         return;
1657 }
1658
1659 /* NB lnet_peers_start_down depends on me,
1660  * so must be called before any peer creation */
1661 void
1662 lnet_get_tunables (void)
1663 {
1664         char *s;
1665
1666         s = getenv("LNET_ROUTER_PING_TIMEOUT");
1667         if (s != NULL) router_ping_timeout = atoi(s);
1668
1669         s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1670         if (s != NULL) live_router_check_interval = atoi(s);
1671
1672         s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1673         if (s != NULL) dead_router_check_interval = atoi(s);
1674
1675         /* This replaces old lnd_notify mechanism */
1676         check_routers_before_use = 1;
1677         if (dead_router_check_interval <= 0)
1678                 dead_router_check_interval = 30;
1679 }
1680
1681 void
1682 lnet_rtrpools_free(void)
1683 {
1684 }
1685
1686 int
1687 lnet_rtrpools_alloc(int im_a_arouter)
1688 {
1689         return 0;
1690 }
1691
1692 #endif