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