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