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