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