Whamcloud - gitweb
f4827ad5f810277b7220b6e0194a423b22000ea6
[fs/lustre-release.git] / lnet / lnet / router.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2013, 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 <lnet/lib-lnet.h>
26
27 #if defined(__KERNEL__) && 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                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 "
461                                        "supported\n",
462                                        libcfs_net2str(net),
463                                        libcfs_nid2str(nid1),
464                                        libcfs_nid2str(nid2));
465                                 return -EINVAL;
466                         }
467                 }
468         }
469
470         lnet_net_unlock(cpt);
471         return 0;
472 }
473
474 int
475 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
476 {
477         struct lnet_peer        *gateway;
478         lnet_remotenet_t        *rnet;
479         lnet_route_t            *route;
480         struct list_head        *e1;
481         struct list_head        *e2;
482         int                     rc = -ENOENT;
483         struct list_head        *rn_list;
484         int                     idx = 0;
485
486         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
487                libcfs_net2str(net), libcfs_nid2str(gw_nid));
488
489         /* NB Caller may specify either all routes via the given gateway
490          * or a specific route entry actual NIDs) */
491
492         lnet_net_lock(LNET_LOCK_EX);
493         if (net == LNET_NIDNET(LNET_NID_ANY))
494                 rn_list = &the_lnet.ln_remote_nets_hash[0];
495         else
496                 rn_list = lnet_net2rnethash(net);
497
498 again:
499         list_for_each(e1, rn_list) {
500                 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
501
502                 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
503                         net == rnet->lrn_net))
504                         continue;
505
506                 list_for_each(e2, &rnet->lrn_routes) {
507                         route = list_entry(e2, lnet_route_t, lr_list);
508
509                         gateway = route->lr_gateway;
510                         if (!(gw_nid == LNET_NID_ANY ||
511                               gw_nid == gateway->lp_nid))
512                                 continue;
513
514                         list_del(&route->lr_list);
515                         list_del(&route->lr_gwlist);
516                         the_lnet.ln_remote_nets_version++;
517
518                         if (list_empty(&rnet->lrn_routes))
519                                 list_del(&rnet->lrn_list);
520                         else
521                                 rnet = NULL;
522
523                         lnet_rtr_decref_locked(gateway);
524                         lnet_peer_decref_locked(gateway);
525
526                         lnet_net_unlock(LNET_LOCK_EX);
527
528                         LIBCFS_FREE(route, sizeof(*route));
529
530                         if (rnet != NULL)
531                                 LIBCFS_FREE(rnet, sizeof(*rnet));
532
533                         rc = 0;
534                         lnet_net_lock(LNET_LOCK_EX);
535                         goto again;
536                 }
537         }
538
539         if (net == LNET_NIDNET(LNET_NID_ANY) &&
540             ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
541                 rn_list = &the_lnet.ln_remote_nets_hash[idx];
542                 goto again;
543         }
544         lnet_net_unlock(LNET_LOCK_EX);
545
546         return rc;
547 }
548
549 void
550 lnet_destroy_routes (void)
551 {
552         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
553 }
554
555 int
556 lnet_get_route(int idx, __u32 *net, __u32 *hops,
557                lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
558 {
559         struct list_head *e1;
560         struct list_head *e2;
561         lnet_remotenet_t *rnet;
562         lnet_route_t     *route;
563         int               cpt;
564         int               i;
565         struct list_head *rn_list;
566
567         cpt = lnet_net_lock_current();
568
569         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
570                 rn_list = &the_lnet.ln_remote_nets_hash[i];
571                 list_for_each(e1, rn_list) {
572                         rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
573
574                         list_for_each(e2, &rnet->lrn_routes) {
575                                 route = list_entry(e2, lnet_route_t,
576                                                    lr_list);
577
578                                 if (idx-- == 0) {
579                                         *net      = rnet->lrn_net;
580                                         *hops     = route->lr_hops;
581                                         *priority = route->lr_priority;
582                                         *gateway  = route->lr_gateway->lp_nid;
583                                         *alive    = route->lr_gateway->lp_alive;
584                                         lnet_net_unlock(cpt);
585                                         return 0;
586                                 }
587                         }
588                 }
589         }
590
591         lnet_net_unlock(cpt);
592         return -ENOENT;
593 }
594
595 void
596 lnet_swap_pinginfo(lnet_ping_info_t *info)
597 {
598         int               i;
599         lnet_ni_status_t *stat;
600
601         __swab32s(&info->pi_magic);
602         __swab32s(&info->pi_features);
603         __swab32s(&info->pi_pid);
604         __swab32s(&info->pi_nnis);
605         for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
606                 stat = &info->pi_ni[i];
607                 __swab64s(&stat->ns_nid);
608                 __swab32s(&stat->ns_status);
609         }
610         return;
611 }
612
613 /**
614  * parse router-checker pinginfo, record number of down NIs for remote
615  * networks on that router.
616  */
617 static void
618 lnet_parse_rc_info(lnet_rc_data_t *rcd)
619 {
620         lnet_ping_info_t        *info = rcd->rcd_pinginfo;
621         struct lnet_peer        *gw   = rcd->rcd_gateway;
622         lnet_route_t            *rtr;
623
624         if (!gw->lp_alive)
625                 return;
626
627         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
628                 lnet_swap_pinginfo(info);
629
630         /* NB always racing with network! */
631         if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
632                 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
633                        libcfs_nid2str(gw->lp_nid), info->pi_magic);
634                 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
635                 return;
636         }
637
638         gw->lp_ping_feats = info->pi_features;
639         if ((gw->lp_ping_feats & LNET_PING_FEAT_MASK) == 0) {
640                 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
641                        libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
642                 return; /* nothing I can understand */
643         }
644
645         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) == 0)
646                 return; /* can't carry NI status info */
647
648         list_for_each_entry(rtr, &gw->lp_routes, lr_gwlist) {
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                                 down++;
669                                 continue;
670                         }
671
672                         if (stat->ns_status == LNET_NI_STATUS_UP) {
673                                 if (LNET_NIDNET(nid) == rtr->lr_net) {
674                                         up = 1;
675                                         break;
676                                 }
677                                 continue;
678                         }
679
680                         CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
681                                libcfs_nid2str(gw->lp_nid), stat->ns_status);
682                         gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
683                         return;
684                 }
685
686                 if (up) { /* ignore downed NIs if NI for dest network is up */
687                         rtr->lr_downis = 0;
688                         continue;
689                 }
690                 rtr->lr_downis = down;
691         }
692 }
693
694 static void
695 lnet_router_checker_event(lnet_event_t *event)
696 {
697         lnet_rc_data_t          *rcd = event->md.user_ptr;
698         struct lnet_peer        *lp;
699
700         LASSERT(rcd != NULL);
701
702         if (event->unlinked) {
703                 LNetInvalidateHandle(&rcd->rcd_mdh);
704                 return;
705         }
706
707         LASSERT(event->type == LNET_EVENT_SEND ||
708                 event->type == LNET_EVENT_REPLY);
709
710         lp = rcd->rcd_gateway;
711         LASSERT(lp != NULL);
712
713          /* NB: it's called with holding lnet_res_lock, we have a few
714           * places need to hold both locks at the same time, please take
715           * care of lock ordering */
716         lnet_net_lock(lp->lp_cpt);
717         if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
718                 /* ignore if no longer a router or rcd is replaced */
719                 goto out;
720         }
721
722         if (event->type == LNET_EVENT_SEND) {
723                 lp->lp_ping_notsent = 0;
724                 if (event->status == 0)
725                         goto out;
726         }
727
728         /* LNET_EVENT_REPLY */
729         /* A successful REPLY means the router is up.  If _any_ comms
730          * to the router fail I assume it's down (this will happen if
731          * we ping alive routers to try to detect router death before
732          * apps get burned). */
733
734         lnet_notify_locked(lp, 1, (event->status == 0), cfs_time_current());
735         /* The router checker will wake up very shortly and do the
736          * actual notification.
737          * XXX If 'lp' stops being a router before then, it will still
738          * have the notification pending!!! */
739
740         if (avoid_asym_router_failure && event->status == 0)
741                 lnet_parse_rc_info(rcd);
742
743  out:
744         lnet_net_unlock(lp->lp_cpt);
745 }
746
747 void
748 lnet_wait_known_routerstate(void)
749 {
750         lnet_peer_t      *rtr;
751         struct list_head *entry;
752         int               all_known;
753
754         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
755
756         for (;;) {
757                 int cpt = lnet_net_lock_current();
758
759                 all_known = 1;
760                 list_for_each(entry, &the_lnet.ln_routers) {
761                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
762
763                         if (rtr->lp_alive_count == 0) {
764                                 all_known = 0;
765                                 break;
766                         }
767                 }
768
769                 lnet_net_unlock(cpt);
770
771                 if (all_known)
772                         return;
773
774 #ifndef __KERNEL__
775                 lnet_router_checker();
776 #endif
777                 cfs_pause(cfs_time_seconds(1));
778         }
779 }
780
781 void
782 lnet_update_ni_status_locked(void)
783 {
784         lnet_ni_t       *ni;
785         long            now;
786         int             timeout;
787
788         LASSERT(the_lnet.ln_routing);
789
790         timeout = router_ping_timeout +
791                   MAX(live_router_check_interval, dead_router_check_interval);
792
793         now = cfs_time_current_sec();
794         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
795                 if (ni->ni_lnd->lnd_type == LOLND)
796                         continue;
797
798                 if (now < ni->ni_last_alive + timeout)
799                         continue;
800
801                 lnet_ni_lock(ni);
802                 /* re-check with lock */
803                 if (now < ni->ni_last_alive + timeout) {
804                         lnet_ni_unlock(ni);
805                         continue;
806                 }
807
808                 LASSERT(ni->ni_status != NULL);
809
810                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
811                         CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
812                                libcfs_nid2str(ni->ni_nid), timeout);
813                         /* NB: so far, this is the only place to set
814                          * NI status to "down" */
815                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
816                 }
817                 lnet_ni_unlock(ni);
818         }
819 }
820
821 void
822 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
823 {
824         LASSERT(list_empty(&rcd->rcd_list));
825         /* detached from network */
826         LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
827
828         if (rcd->rcd_gateway != NULL) {
829                 int cpt = rcd->rcd_gateway->lp_cpt;
830
831                 lnet_net_lock(cpt);
832                 lnet_peer_decref_locked(rcd->rcd_gateway);
833                 lnet_net_unlock(cpt);
834         }
835
836         if (rcd->rcd_pinginfo != NULL)
837                 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
838
839         LIBCFS_FREE(rcd, sizeof(*rcd));
840 }
841
842 lnet_rc_data_t *
843 lnet_create_rc_data_locked(lnet_peer_t *gateway)
844 {
845         lnet_rc_data_t          *rcd = NULL;
846         lnet_ping_info_t        *pi;
847         int                     rc;
848         int                     i;
849
850         lnet_net_unlock(gateway->lp_cpt);
851
852         LIBCFS_ALLOC(rcd, sizeof(*rcd));
853         if (rcd == NULL)
854                 goto out;
855
856         LNetInvalidateHandle(&rcd->rcd_mdh);
857         INIT_LIST_HEAD(&rcd->rcd_list);
858
859         LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
860         if (pi == NULL)
861                 goto out;
862
863         memset(pi, 0, LNET_PINGINFO_SIZE);
864         for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
865                 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
866                 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
867         }
868         rcd->rcd_pinginfo = pi;
869
870         LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
871         rc = LNetMDBind((lnet_md_t){.start     = pi,
872                                     .user_ptr  = rcd,
873                                     .length    = LNET_PINGINFO_SIZE,
874                                     .threshold = LNET_MD_THRESH_INF,
875                                     .options   = LNET_MD_TRUNCATE,
876                                     .eq_handle = the_lnet.ln_rc_eqh},
877                         LNET_UNLINK,
878                         &rcd->rcd_mdh);
879         if (rc < 0) {
880                 CERROR("Can't bind MD: %d\n", rc);
881                 goto out;
882         }
883         LASSERT(rc == 0);
884
885         lnet_net_lock(gateway->lp_cpt);
886         /* router table changed or someone has created rcd for this gateway */
887         if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
888                 lnet_net_unlock(gateway->lp_cpt);
889                 goto out;
890         }
891
892         lnet_peer_addref_locked(gateway);
893         rcd->rcd_gateway = gateway;
894         gateway->lp_rcd = rcd;
895         gateway->lp_ping_notsent = 0;
896
897         return rcd;
898
899  out:
900         if (rcd != NULL) {
901                 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
902                         rc = LNetMDUnlink(rcd->rcd_mdh);
903                         LASSERT(rc == 0);
904                 }
905                 lnet_destroy_rc_data(rcd);
906         }
907
908         lnet_net_lock(gateway->lp_cpt);
909         return gateway->lp_rcd;
910 }
911
912 static int
913 lnet_router_check_interval (lnet_peer_t *rtr)
914 {
915         int secs;
916
917         secs = rtr->lp_alive ? live_router_check_interval :
918                                dead_router_check_interval;
919         if (secs < 0)
920                 secs = 0;
921
922         return secs;
923 }
924
925 static void
926 lnet_ping_router_locked (lnet_peer_t *rtr)
927 {
928         lnet_rc_data_t *rcd = NULL;
929         cfs_time_t      now = cfs_time_current();
930         int             secs;
931
932         lnet_peer_addref_locked(rtr);
933
934         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
935             cfs_time_after(now, rtr->lp_ping_deadline))
936                 lnet_notify_locked(rtr, 1, 0, now);
937
938         /* Run any outstanding notifications */
939         lnet_ni_notify_locked(rtr->lp_ni, rtr);
940
941         if (!lnet_isrouter(rtr) ||
942             the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
943                 /* router table changed or router checker is shutting down */
944                 lnet_peer_decref_locked(rtr);
945                 return;
946         }
947
948         rcd = rtr->lp_rcd != NULL ?
949               rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
950
951         if (rcd == NULL)
952                 return;
953
954         secs = lnet_router_check_interval(rtr);
955
956         CDEBUG(D_NET,
957                "rtr %s %d: deadline %lu ping_notsent %d alive %d "
958                "alive_count %d lp_ping_timestamp %lu\n",
959                libcfs_nid2str(rtr->lp_nid), secs,
960                rtr->lp_ping_deadline, rtr->lp_ping_notsent,
961                rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
962
963         if (secs != 0 && !rtr->lp_ping_notsent &&
964             cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
965                                              cfs_time_seconds(secs)))) {
966                 int               rc;
967                 lnet_process_id_t id;
968                 lnet_handle_md_t  mdh;
969
970                 id.nid = rtr->lp_nid;
971                 id.pid = LUSTRE_SRV_LNET_PID;
972                 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
973
974                 rtr->lp_ping_notsent   = 1;
975                 rtr->lp_ping_timestamp = now;
976
977                 mdh = rcd->rcd_mdh;
978
979                 if (rtr->lp_ping_deadline == 0) {
980                         rtr->lp_ping_deadline =
981                                 cfs_time_shift(router_ping_timeout);
982                 }
983
984                 lnet_net_unlock(rtr->lp_cpt);
985
986                 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
987                              LNET_PROTO_PING_MATCHBITS, 0);
988
989                 lnet_net_lock(rtr->lp_cpt);
990                 if (rc != 0)
991                         rtr->lp_ping_notsent = 0; /* no event pending */
992         }
993
994         lnet_peer_decref_locked(rtr);
995         return;
996 }
997
998 int
999 lnet_router_checker_start(void)
1000 {
1001         int                     rc;
1002         int                     eqsz;
1003 #ifdef __KERNEL__
1004         struct task_struct     *task;
1005 #else /* __KERNEL__ */
1006         lnet_peer_t            *rtr;
1007         __u64                   version;
1008         int                     nrtr = 0;
1009         int                     router_checker_max_eqsize = 10240;
1010
1011         LASSERT (check_routers_before_use);
1012         LASSERT (dead_router_check_interval > 0);
1013
1014         lnet_net_lock(0);
1015
1016         /* As an approximation, allow each router the same number of
1017          * outstanding events as it is allowed outstanding sends */
1018         eqsz = 0;
1019         version = the_lnet.ln_routers_version;
1020         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1021                 lnet_ni_t         *ni = rtr->lp_ni;
1022                 lnet_process_id_t  id;
1023
1024                 nrtr++;
1025                 eqsz += ni->ni_peertxcredits;
1026
1027                 /* one async ping reply per router */
1028                 id.nid = rtr->lp_nid;
1029                 id.pid = LUSTRE_SRV_LNET_PID;
1030
1031                 lnet_net_unlock(0);
1032
1033                 rc = LNetSetAsync(id, 1);
1034                 if (rc != 0) {
1035                         CWARN("LNetSetAsync %s failed: %d\n",
1036                               libcfs_id2str(id), rc);
1037                         return rc;
1038                 }
1039
1040                 lnet_net_lock(0);
1041                 /* NB router list doesn't change in userspace */
1042                 LASSERT(version == the_lnet.ln_routers_version);
1043         }
1044
1045         lnet_net_unlock(0);
1046
1047         if (nrtr == 0) {
1048                 CDEBUG(D_NET,
1049                        "No router found, not starting router checker\n");
1050                 return 0;
1051         }
1052
1053         /* at least allow a SENT and a REPLY per router */
1054         if (router_checker_max_eqsize < 2 * nrtr)
1055                 router_checker_max_eqsize = 2 * nrtr;
1056
1057         LASSERT (eqsz > 0);
1058         if (eqsz > router_checker_max_eqsize)
1059                 eqsz = router_checker_max_eqsize;
1060 #endif
1061
1062         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1063
1064         if (check_routers_before_use &&
1065             dead_router_check_interval <= 0) {
1066                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1067                                    " set if 'check_routers_before_use' is set"
1068                                    "\n");
1069                 return -EINVAL;
1070         }
1071
1072         if (!the_lnet.ln_routing &&
1073             live_router_check_interval <= 0 &&
1074             dead_router_check_interval <= 0)
1075                 return 0;
1076
1077 #ifdef __KERNEL__
1078         sema_init(&the_lnet.ln_rc_signal, 0);
1079         /* EQ size doesn't matter; the callback is guaranteed to get every
1080          * event */
1081         eqsz = 0;
1082         rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1083                          &the_lnet.ln_rc_eqh);
1084 #else
1085         rc = LNetEQAlloc(eqsz, LNET_EQ_HANDLER_NONE,
1086                          &the_lnet.ln_rc_eqh);
1087 #endif
1088         if (rc != 0) {
1089                 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1090                 return -ENOMEM;
1091         }
1092
1093         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1094 #ifdef __KERNEL__
1095         task = kthread_run(lnet_router_checker, NULL, "router_checker");
1096         if (IS_ERR(task)) {
1097                 rc = PTR_ERR(task);
1098                 CERROR("Can't start router checker thread: %d\n", rc);
1099                 /* block until event callback signals exit */
1100                 down(&the_lnet.ln_rc_signal);
1101                 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1102                 LASSERT(rc == 0);
1103                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1104                 return -ENOMEM;
1105         }
1106 #endif
1107
1108         if (check_routers_before_use) {
1109                 /* Note that a helpful side-effect of pinging all known routers
1110                  * at startup is that it makes them drop stale connections they
1111                  * may have to a previous instance of me. */
1112                 lnet_wait_known_routerstate();
1113         }
1114
1115         return 0;
1116 }
1117
1118 void
1119 lnet_router_checker_stop (void)
1120 {
1121         int rc;
1122
1123         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1124                 return;
1125
1126         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1127         the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1128
1129 #ifdef __KERNEL__
1130         /* block until event callback signals exit */
1131         down(&the_lnet.ln_rc_signal);
1132 #else
1133         lnet_router_checker();
1134 #endif
1135         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1136
1137         rc = LNetEQFree(the_lnet.ln_rc_eqh);
1138         LASSERT (rc == 0);
1139         return;
1140 }
1141
1142 static void
1143 lnet_prune_rc_data(int wait_unlink)
1144 {
1145         lnet_rc_data_t          *rcd;
1146         lnet_rc_data_t          *tmp;
1147         lnet_peer_t             *lp;
1148         struct list_head         head;
1149         int                      i = 2;
1150
1151         if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1152                    list_empty(&the_lnet.ln_rcd_deathrow) &&
1153                    list_empty(&the_lnet.ln_rcd_zombie)))
1154                 return;
1155
1156         INIT_LIST_HEAD(&head);
1157
1158         lnet_net_lock(LNET_LOCK_EX);
1159
1160         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1161                 /* router checker is stopping, prune all */
1162                 list_for_each_entry(lp, &the_lnet.ln_routers,
1163                                     lp_rtr_list) {
1164                         if (lp->lp_rcd == NULL)
1165                                 continue;
1166
1167                         LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1168                         list_add(&lp->lp_rcd->rcd_list,
1169                                  &the_lnet.ln_rcd_deathrow);
1170                         lp->lp_rcd = NULL;
1171                 }
1172         }
1173
1174         /* unlink all RCDs on deathrow list */
1175         list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1176
1177         if (!list_empty(&head)) {
1178                 lnet_net_unlock(LNET_LOCK_EX);
1179
1180                 list_for_each_entry(rcd, &head, rcd_list)
1181                         LNetMDUnlink(rcd->rcd_mdh);
1182
1183                 lnet_net_lock(LNET_LOCK_EX);
1184         }
1185
1186         list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1187
1188         /* release all zombie RCDs */
1189         while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1190                 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1191                                          rcd_list) {
1192                         if (LNetHandleIsInvalid(rcd->rcd_mdh))
1193                                 list_move(&rcd->rcd_list, &head);
1194                 }
1195
1196                 wait_unlink = wait_unlink &&
1197                               !list_empty(&the_lnet.ln_rcd_zombie);
1198
1199                 lnet_net_unlock(LNET_LOCK_EX);
1200
1201                 while (!list_empty(&head)) {
1202                         rcd = list_entry(head.next,
1203                                          lnet_rc_data_t, rcd_list);
1204                         list_del_init(&rcd->rcd_list);
1205                         lnet_destroy_rc_data(rcd);
1206                 }
1207
1208                 if (!wait_unlink)
1209                         return;
1210
1211                 i++;
1212                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1213                        "Waiting for rc buffers to unlink\n");
1214                 cfs_pause(cfs_time_seconds(1) / 4);
1215
1216                 lnet_net_lock(LNET_LOCK_EX);
1217         }
1218
1219         lnet_net_unlock(LNET_LOCK_EX);
1220 }
1221
1222
1223 #if defined(__KERNEL__) && defined(LNET_ROUTER)
1224
1225 static int
1226 lnet_router_checker(void *arg)
1227 {
1228         lnet_peer_t       *rtr;
1229         struct list_head  *entry;
1230
1231         cfs_block_allsigs();
1232
1233         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1234
1235         while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1236                 __u64   version;
1237                 int     cpt;
1238                 int     cpt2;
1239
1240                 cpt = lnet_net_lock_current();
1241 rescan:
1242                 version = the_lnet.ln_routers_version;
1243
1244                 list_for_each(entry, &the_lnet.ln_routers) {
1245                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1246
1247                         cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1248                         if (cpt != cpt2) {
1249                                 lnet_net_unlock(cpt);
1250                                 cpt = cpt2;
1251                                 lnet_net_lock(cpt);
1252                                 /* the routers list has changed */
1253                                 if (version != the_lnet.ln_routers_version)
1254                                         goto rescan;
1255                         }
1256
1257                         lnet_ping_router_locked(rtr);
1258
1259                         /* NB dropped lock */
1260                         if (version != the_lnet.ln_routers_version) {
1261                                 /* the routers list has changed */
1262                                 goto rescan;
1263                         }
1264                 }
1265
1266                 if (the_lnet.ln_routing)
1267                         lnet_update_ni_status_locked();
1268
1269                 lnet_net_unlock(cpt);
1270
1271                 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1272
1273                 /* Call cfs_pause() here always adds 1 to load average
1274                  * because kernel counts # active tasks as nr_running
1275                  * + nr_uninterruptible. */
1276                 schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,
1277                                                    cfs_time_seconds(1));
1278         }
1279
1280         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1281
1282         lnet_prune_rc_data(1); /* wait for UNLINK */
1283
1284         the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1285         up(&the_lnet.ln_rc_signal);
1286         /* The unlink event callback will signal final completion */
1287         return 0;
1288 }
1289
1290 void
1291 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1292 {
1293         int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1294
1295         while (--npages >= 0)
1296                 __free_page(rb->rb_kiov[npages].kiov_page);
1297
1298         LIBCFS_FREE(rb, sz);
1299 }
1300
1301 lnet_rtrbuf_t *
1302 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1303 {
1304         int            npages = rbp->rbp_npages;
1305         int            sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1306         struct page   *page;
1307         lnet_rtrbuf_t *rb;
1308         int            i;
1309
1310         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1311         if (rb == NULL)
1312                 return NULL;
1313
1314         rb->rb_pool = rbp;
1315
1316         for (i = 0; i < npages; i++) {
1317                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1318                                           __GFP_ZERO | GFP_IOFS);
1319                 if (page == NULL) {
1320                         while (--i >= 0)
1321                                 __free_page(rb->rb_kiov[i].kiov_page);
1322
1323                         LIBCFS_FREE(rb, sz);
1324                         return NULL;
1325                 }
1326
1327                 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1328                 rb->rb_kiov[i].kiov_offset = 0;
1329                 rb->rb_kiov[i].kiov_page = page;
1330         }
1331
1332         return rb;
1333 }
1334
1335 void
1336 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp)
1337 {
1338         int             npages = rbp->rbp_npages;
1339         int             nbuffers = 0;
1340         lnet_rtrbuf_t   *rb;
1341
1342         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1343                 return;
1344
1345         LASSERT(list_empty(&rbp->rbp_msgs));
1346         LASSERT(rbp->rbp_credits == rbp->rbp_nbuffers);
1347
1348         while (!list_empty(&rbp->rbp_bufs)) {
1349                 LASSERT(rbp->rbp_credits > 0);
1350
1351                 rb = list_entry(rbp->rbp_bufs.next,
1352                                 lnet_rtrbuf_t, rb_list);
1353                 list_del(&rb->rb_list);
1354                 lnet_destroy_rtrbuf(rb, npages);
1355                 nbuffers++;
1356         }
1357
1358         LASSERT(rbp->rbp_nbuffers == nbuffers);
1359         LASSERT(rbp->rbp_credits == nbuffers);
1360
1361         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1362 }
1363
1364 int
1365 lnet_rtrpool_alloc_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
1366 {
1367         lnet_rtrbuf_t *rb;
1368         int            i;
1369
1370         if (rbp->rbp_nbuffers != 0) {
1371                 LASSERT (rbp->rbp_nbuffers == nbufs);
1372                 return 0;
1373         }
1374
1375         for (i = 0; i < nbufs; i++) {
1376                 rb = lnet_new_rtrbuf(rbp, cpt);
1377
1378                 if (rb == NULL) {
1379                         CERROR("Failed to allocate %d router bufs of %d pages\n",
1380                                nbufs, rbp->rbp_npages);
1381                         return -ENOMEM;
1382                 }
1383
1384                 rbp->rbp_nbuffers++;
1385                 rbp->rbp_credits++;
1386                 rbp->rbp_mincredits++;
1387                 list_add(&rb->rb_list, &rbp->rbp_bufs);
1388
1389                 /* No allocation "under fire" */
1390                 /* Otherwise we'd need code to schedule blocked msgs etc */
1391                 LASSERT(!the_lnet.ln_routing);
1392         }
1393
1394         LASSERT(rbp->rbp_credits == nbufs);
1395         return 0;
1396 }
1397
1398 void
1399 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1400 {
1401         INIT_LIST_HEAD(&rbp->rbp_msgs);
1402         INIT_LIST_HEAD(&rbp->rbp_bufs);
1403
1404         rbp->rbp_npages = npages;
1405         rbp->rbp_credits = 0;
1406         rbp->rbp_mincredits = 0;
1407 }
1408
1409 void
1410 lnet_rtrpools_free(void)
1411 {
1412         lnet_rtrbufpool_t *rtrp;
1413         int               i;
1414
1415         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1416                 return;
1417
1418         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1419                 lnet_rtrpool_free_bufs(&rtrp[0]);
1420                 lnet_rtrpool_free_bufs(&rtrp[1]);
1421                 lnet_rtrpool_free_bufs(&rtrp[2]);
1422         }
1423
1424         cfs_percpt_free(the_lnet.ln_rtrpools);
1425         the_lnet.ln_rtrpools = NULL;
1426 }
1427
1428 static int
1429 lnet_nrb_tiny_calculate(int npages)
1430 {
1431         int     nrbs = LNET_NRB_TINY;
1432
1433         if (tiny_router_buffers < 0) {
1434                 LCONSOLE_ERROR_MSG(0x10c,
1435                                    "tiny_router_buffers=%d invalid when "
1436                                    "routing enabled\n", tiny_router_buffers);
1437                 return -1;
1438         }
1439
1440         if (tiny_router_buffers > 0)
1441                 nrbs = tiny_router_buffers;
1442
1443         nrbs /= LNET_CPT_NUMBER;
1444         return max(nrbs, LNET_NRB_TINY_MIN);
1445 }
1446
1447 static int
1448 lnet_nrb_small_calculate(int npages)
1449 {
1450         int     nrbs = LNET_NRB_SMALL;
1451
1452         if (small_router_buffers < 0) {
1453                 LCONSOLE_ERROR_MSG(0x10c,
1454                                    "small_router_buffers=%d invalid when "
1455                                    "routing enabled\n", small_router_buffers);
1456                 return -1;
1457         }
1458
1459         if (small_router_buffers > 0)
1460                 nrbs = small_router_buffers;
1461
1462         nrbs /= LNET_CPT_NUMBER;
1463         return max(nrbs, LNET_NRB_SMALL_MIN);
1464 }
1465
1466 static int
1467 lnet_nrb_large_calculate(int npages)
1468 {
1469         int     nrbs = LNET_NRB_LARGE;
1470
1471         if (large_router_buffers < 0) {
1472                 LCONSOLE_ERROR_MSG(0x10c,
1473                                    "large_router_buffers=%d invalid when "
1474                                    "routing enabled\n", large_router_buffers);
1475                 return -1;
1476         }
1477
1478         if (large_router_buffers > 0)
1479                 nrbs = large_router_buffers;
1480
1481         nrbs /= LNET_CPT_NUMBER;
1482         return max(nrbs, LNET_NRB_LARGE_MIN);
1483 }
1484
1485 int
1486 lnet_rtrpools_alloc(int im_a_router)
1487 {
1488         lnet_rtrbufpool_t *rtrp;
1489         int     large_pages = (LNET_MTU + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1490         int     small_pages = 1;
1491         int     nrb_tiny;
1492         int     nrb_small;
1493         int     nrb_large;
1494         int     rc;
1495         int     i;
1496
1497         if (!strcmp(forwarding, "")) {
1498                 /* not set either way */
1499                 if (!im_a_router)
1500                         return 0;
1501         } else if (!strcmp(forwarding, "disabled")) {
1502                 /* explicitly disabled */
1503                 return 0;
1504         } else if (!strcmp(forwarding, "enabled")) {
1505                 /* explicitly enabled */
1506         } else {
1507                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1508                                    "'enabled' or 'disabled'\n");
1509                 return -EINVAL;
1510         }
1511
1512         nrb_tiny = lnet_nrb_tiny_calculate(0);
1513         if (nrb_tiny < 0)
1514                 return -EINVAL;
1515
1516         nrb_small = lnet_nrb_small_calculate(small_pages);
1517         if (nrb_small < 0)
1518                 return -EINVAL;
1519
1520         nrb_large = lnet_nrb_large_calculate(large_pages);
1521         if (nrb_large < 0)
1522                 return -EINVAL;
1523
1524         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1525                                                 LNET_NRBPOOLS *
1526                                                 sizeof(lnet_rtrbufpool_t));
1527         if (the_lnet.ln_rtrpools == NULL) {
1528                 LCONSOLE_ERROR_MSG(0x10c,
1529                                    "Failed to initialize router buffe pool\n");
1530                 return -ENOMEM;
1531         }
1532
1533         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1534                 lnet_rtrpool_init(&rtrp[0], 0);
1535                 rc = lnet_rtrpool_alloc_bufs(&rtrp[0], nrb_tiny, i);
1536                 if (rc != 0)
1537                         goto failed;
1538
1539                 lnet_rtrpool_init(&rtrp[1], small_pages);
1540                 rc = lnet_rtrpool_alloc_bufs(&rtrp[1], nrb_small, i);
1541                 if (rc != 0)
1542                         goto failed;
1543
1544                 lnet_rtrpool_init(&rtrp[2], large_pages);
1545                 rc = lnet_rtrpool_alloc_bufs(&rtrp[2], nrb_large, i);
1546                 if (rc != 0)
1547                         goto failed;
1548         }
1549
1550         lnet_net_lock(LNET_LOCK_EX);
1551         the_lnet.ln_routing = 1;
1552         lnet_net_unlock(LNET_LOCK_EX);
1553
1554         return 0;
1555
1556  failed:
1557         lnet_rtrpools_free();
1558         return rc;
1559 }
1560
1561 int
1562 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1563 {
1564         struct lnet_peer        *lp = NULL;
1565         cfs_time_t              now = cfs_time_current();
1566         int                     cpt = lnet_cpt_of_nid(nid);
1567
1568         LASSERT (!in_interrupt ());
1569
1570         CDEBUG (D_NET, "%s notifying %s: %s\n",
1571                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1572                 libcfs_nid2str(nid),
1573                 alive ? "up" : "down");
1574
1575         if (ni != NULL &&
1576             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1577                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1578                         libcfs_nid2str(nid), alive ? "birth" : "death",
1579                         libcfs_nid2str(ni->ni_nid));
1580                 return -EINVAL;
1581         }
1582
1583         /* can't do predictions... */
1584         if (cfs_time_after(when, now)) {
1585                 CWARN ("Ignoring prediction from %s of %s %s "
1586                        "%ld seconds in the future\n",
1587                        (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1588                        libcfs_nid2str(nid), alive ? "up" : "down",
1589                        cfs_duration_sec(cfs_time_sub(when, now)));
1590                 return -EINVAL;
1591         }
1592
1593         if (ni != NULL && !alive &&             /* LND telling me she's down */
1594             !auto_down) {                       /* auto-down disabled */
1595                 CDEBUG(D_NET, "Auto-down disabled\n");
1596                 return 0;
1597         }
1598
1599         lnet_net_lock(cpt);
1600
1601         if (the_lnet.ln_shutdown) {
1602                 lnet_net_unlock(cpt);
1603                 return -ESHUTDOWN;
1604         }
1605
1606         lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1607         if (lp == NULL) {
1608                 /* nid not found */
1609                 lnet_net_unlock(cpt);
1610                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1611                 return 0;
1612         }
1613
1614         /* We can't fully trust LND on reporting exact peer last_alive
1615          * if he notifies us about dead peer. For example ksocklnd can
1616          * call us with when == _time_when_the_node_was_booted_ if
1617          * no connections were successfully established */
1618         if (ni != NULL && !alive && when < lp->lp_last_alive)
1619                 when = lp->lp_last_alive;
1620
1621         lnet_notify_locked(lp, ni == NULL, alive, when);
1622
1623         if (ni != NULL)
1624                 lnet_ni_notify_locked(ni, lp);
1625
1626         lnet_peer_decref_locked(lp);
1627
1628         lnet_net_unlock(cpt);
1629         return 0;
1630 }
1631 EXPORT_SYMBOL(lnet_notify);
1632
1633 void
1634 lnet_get_tunables (void)
1635 {
1636         return;
1637 }
1638
1639 #else
1640
1641 int
1642 lnet_notify (lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1643 {
1644         return -EOPNOTSUPP;
1645 }
1646
1647 void
1648 lnet_router_checker (void)
1649 {
1650         static time_t last = 0;
1651         static int    running = 0;
1652
1653         time_t            now = cfs_time_current_sec();
1654         int               interval = now - last;
1655         int               rc;
1656         __u64             version;
1657         lnet_peer_t      *rtr;
1658
1659         /* It's no use to call me again within a sec - all intervals and
1660          * timeouts are measured in seconds */
1661         if (last != 0 && interval < 2)
1662                 return;
1663
1664         if (last != 0 &&
1665             interval > MAX(live_router_check_interval,
1666                            dead_router_check_interval))
1667                 CNETERR("Checker(%d/%d) not called for %d seconds\n",
1668                         live_router_check_interval, dead_router_check_interval,
1669                         interval);
1670
1671         LASSERT(LNET_CPT_NUMBER == 1);
1672
1673         lnet_net_lock(0);
1674         LASSERT(!running); /* recursion check */
1675         running = 1;
1676         lnet_net_unlock(0);
1677
1678         last = now;
1679
1680         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING)
1681                 lnet_prune_rc_data(0); /* unlink all rcd and nowait */
1682
1683         /* consume all pending events */
1684         while (1) {
1685                 int          i;
1686                 lnet_event_t ev;
1687
1688                 /* NB ln_rc_eqh must be the 1st in 'eventqs' otherwise the
1689                  * recursion breaker in LNetEQPoll would fail */
1690                 rc = LNetEQPoll(&the_lnet.ln_rc_eqh, 1, 0, &ev, &i);
1691                 if (rc == 0)   /* no event pending */
1692                         break;
1693
1694                 /* NB a lost SENT prevents me from pinging a router again */
1695                 if (rc == -EOVERFLOW) {
1696                         CERROR("Dropped an event!!!\n");
1697                         abort();
1698                 }
1699
1700                 LASSERT (rc == 1);
1701
1702                 lnet_router_checker_event(&ev);
1703         }
1704
1705         if (the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING) {
1706                 lnet_prune_rc_data(1); /* release rcd */
1707                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1708                 running = 0;
1709                 return;
1710         }
1711
1712         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1713
1714         lnet_net_lock(0);
1715
1716         version = the_lnet.ln_routers_version;
1717         list_for_each_entry(rtr, &the_lnet.ln_routers, lp_rtr_list) {
1718                 lnet_ping_router_locked(rtr);
1719                 LASSERT(version == the_lnet.ln_routers_version);
1720         }
1721
1722         lnet_net_unlock(0);
1723
1724         running = 0; /* lock only needed for the recursion check */
1725         return;
1726 }
1727
1728 /* NB lnet_peers_start_down depends on me,
1729  * so must be called before any peer creation */
1730 void
1731 lnet_get_tunables (void)
1732 {
1733         char *s;
1734
1735         s = getenv("LNET_ROUTER_PING_TIMEOUT");
1736         if (s != NULL) router_ping_timeout = atoi(s);
1737
1738         s = getenv("LNET_LIVE_ROUTER_CHECK_INTERVAL");
1739         if (s != NULL) live_router_check_interval = atoi(s);
1740
1741         s = getenv("LNET_DEAD_ROUTER_CHECK_INTERVAL");
1742         if (s != NULL) dead_router_check_interval = atoi(s);
1743
1744         /* This replaces old lnd_notify mechanism */
1745         check_routers_before_use = 1;
1746         if (dead_router_check_interval <= 0)
1747                 dead_router_check_interval = 30;
1748 }
1749
1750 void
1751 lnet_rtrpools_free(void)
1752 {
1753 }
1754
1755 int
1756 lnet_rtrpools_alloc(int im_a_arouter)
1757 {
1758         return 0;
1759 }
1760
1761 #endif