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