Whamcloud - gitweb
LU-6977 lod: do_index_try should be called first
[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 Lustre, https://wiki.hpdd.intel.com/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24 #include <lnet/lib-lnet.h>
25
26 #define LNET_NRB_TINY_MIN       512     /* min value for each CPT */
27 #define LNET_NRB_TINY           (LNET_NRB_TINY_MIN * 4)
28 #define LNET_NRB_SMALL_MIN      4096    /* min value for each CPT */
29 #define LNET_NRB_SMALL          (LNET_NRB_SMALL_MIN * 4)
30 #define LNET_NRB_SMALL_PAGES    1
31 #define LNET_NRB_LARGE_MIN      256     /* min value for each CPT */
32 #define LNET_NRB_LARGE          (LNET_NRB_LARGE_MIN * 4)
33 #define LNET_NRB_LARGE_PAGES    ((LNET_MTU + PAGE_CACHE_SIZE - 1) >> \
34                                   PAGE_CACHE_SHIFT)
35
36 static char *forwarding = "";
37 CFS_MODULE_PARM(forwarding, "s", charp, 0444,
38                 "Explicitly enable/disable forwarding between networks");
39
40 static int tiny_router_buffers;
41 CFS_MODULE_PARM(tiny_router_buffers, "i", int, 0444,
42                 "# of 0 payload messages to buffer in the router");
43 static int small_router_buffers;
44 CFS_MODULE_PARM(small_router_buffers, "i", int, 0444,
45                 "# of small (1 page) messages to buffer in the router");
46 static int large_router_buffers;
47 CFS_MODULE_PARM(large_router_buffers, "i", int, 0444,
48                 "# of large messages to buffer in the router");
49 static int peer_buffer_credits = 0;
50 CFS_MODULE_PARM(peer_buffer_credits, "i", int, 0444,
51                 "# router buffer credits per peer");
52
53 static int auto_down = 1;
54 CFS_MODULE_PARM(auto_down, "i", int, 0444,
55                 "Automatically mark peers down on comms error");
56
57 int
58 lnet_peer_buffer_credits(lnet_ni_t *ni)
59 {
60         /* NI option overrides LNet default */
61         if (ni->ni_peerrtrcredits > 0)
62                 return ni->ni_peerrtrcredits;
63         if (peer_buffer_credits > 0)
64                 return peer_buffer_credits;
65
66         /* As an approximation, allow this peer the same number of router
67          * buffers as it is allowed outstanding sends */
68         return ni->ni_peertxcredits;
69 }
70
71 /* forward ref's */
72 static int lnet_router_checker(void *);
73
74 static int check_routers_before_use = 0;
75 CFS_MODULE_PARM(check_routers_before_use, "i", int, 0444,
76                 "Assume routers are down and ping them before use");
77
78 int avoid_asym_router_failure = 1;
79 CFS_MODULE_PARM(avoid_asym_router_failure, "i", int, 0644,
80                 "Avoid asymmetrical router failures (0 to disable)");
81
82 static int dead_router_check_interval = 60;
83 CFS_MODULE_PARM(dead_router_check_interval, "i", int, 0644,
84                 "Seconds between dead router health checks (<= 0 to disable)");
85
86 static int live_router_check_interval = 60;
87 CFS_MODULE_PARM(live_router_check_interval, "i", int, 0644,
88                 "Seconds between live router health checks (<= 0 to disable)");
89
90 static int router_ping_timeout = 50;
91 CFS_MODULE_PARM(router_ping_timeout, "i", int, 0644,
92                 "Seconds to wait for the reply to a router health query");
93
94 int
95 lnet_peers_start_down(void)
96 {
97         return check_routers_before_use;
98 }
99
100 void
101 lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive, cfs_time_t when)
102 {
103         if (cfs_time_before(when, lp->lp_timestamp)) { /* out of date information */
104                 CDEBUG(D_NET, "Out of date\n");
105                 return;
106         }
107
108         lp->lp_timestamp = when;                /* update timestamp */
109         lp->lp_ping_deadline = 0;               /* disable ping timeout */
110
111         if (lp->lp_alive_count != 0 &&          /* got old news */
112             (!lp->lp_alive) == (!alive)) {      /* new date for old news */
113                 CDEBUG(D_NET, "Old news\n");
114                 return;
115         }
116
117         /* Flag that notification is outstanding */
118
119         lp->lp_alive_count++;
120         lp->lp_alive = !(!alive);               /* 1 bit! */
121         lp->lp_notify = 1;
122         lp->lp_notifylnd |= notifylnd;
123         if (lp->lp_alive)
124                 lp->lp_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
125
126         CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lp_nid), alive);
127 }
128
129 static void
130 lnet_ni_notify_locked(lnet_ni_t *ni, lnet_peer_t *lp)
131 {
132         int        alive;
133         int        notifylnd;
134
135         /* Notify only in 1 thread at any time to ensure ordered notification.
136          * NB individual events can be missed; the only guarantee is that you
137          * always get the most recent news */
138
139         if (lp->lp_notifying || ni == NULL)
140                 return;
141
142         lp->lp_notifying = 1;
143
144         while (lp->lp_notify) {
145                 alive     = lp->lp_alive;
146                 notifylnd = lp->lp_notifylnd;
147
148                 lp->lp_notifylnd = 0;
149                 lp->lp_notify    = 0;
150
151                 if (notifylnd && ni->ni_lnd->lnd_notify != NULL) {
152                         lnet_net_unlock(lp->lp_cpt);
153
154                         /* A new notification could happen now; I'll handle it
155                          * when control returns to me */
156
157                         (ni->ni_lnd->lnd_notify)(ni, lp->lp_nid, alive);
158
159                         lnet_net_lock(lp->lp_cpt);
160                 }
161         }
162
163         lp->lp_notifying = 0;
164 }
165
166
167 static void
168 lnet_rtr_addref_locked(lnet_peer_t *lp)
169 {
170         LASSERT(lp->lp_refcount > 0);
171         LASSERT(lp->lp_rtr_refcount >= 0);
172
173         /* lnet_net_lock must be exclusively locked */
174         lp->lp_rtr_refcount++;
175         if (lp->lp_rtr_refcount == 1) {
176                 struct list_head *pos;
177
178                 /* a simple insertion sort */
179                 list_for_each_prev(pos, &the_lnet.ln_routers) {
180                         lnet_peer_t *rtr = list_entry(pos, lnet_peer_t,
181                                                       lp_rtr_list);
182
183                         if (rtr->lp_nid < lp->lp_nid)
184                                 break;
185                 }
186
187                 list_add(&lp->lp_rtr_list, pos);
188                 /* addref for the_lnet.ln_routers */
189                 lnet_peer_addref_locked(lp);
190                 the_lnet.ln_routers_version++;
191         }
192 }
193
194 static void
195 lnet_rtr_decref_locked(lnet_peer_t *lp)
196 {
197         LASSERT(lp->lp_refcount > 0);
198         LASSERT(lp->lp_rtr_refcount > 0);
199
200         /* lnet_net_lock must be exclusively locked */
201         lp->lp_rtr_refcount--;
202         if (lp->lp_rtr_refcount == 0) {
203                 LASSERT(list_empty(&lp->lp_routes));
204
205                 if (lp->lp_rcd != NULL) {
206                         list_add(&lp->lp_rcd->rcd_list,
207                                  &the_lnet.ln_rcd_deathrow);
208                         lp->lp_rcd = NULL;
209                 }
210
211                 list_del(&lp->lp_rtr_list);
212                 /* decref for the_lnet.ln_routers */
213                 lnet_peer_decref_locked(lp);
214                 the_lnet.ln_routers_version++;
215         }
216 }
217
218 lnet_remotenet_t *
219 lnet_find_net_locked (__u32 net)
220 {
221         lnet_remotenet_t *rnet;
222         struct list_head *tmp;
223         struct list_head *rn_list;
224
225         LASSERT(!the_lnet.ln_shutdown);
226
227         rn_list = lnet_net2rnethash(net);
228         list_for_each(tmp, rn_list) {
229                 rnet = list_entry(tmp, lnet_remotenet_t, lrn_list);
230
231                 if (rnet->lrn_net == net)
232                         return rnet;
233         }
234         return NULL;
235 }
236
237 static void lnet_shuffle_seed(void)
238 {
239         static int seeded;
240         __u32 lnd_type;
241         __u32 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                 set_current_state(TASK_UNINTERRUPTIBLE);
820                 schedule_timeout(cfs_time_seconds(1));
821         }
822 }
823
824 void
825 lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net)
826 {
827         lnet_route_t *rte;
828
829         if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0) {
830                 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
831                         if (rte->lr_net == net) {
832                                 rte->lr_downis = 0;
833                                 break;
834                         }
835                 }
836         }
837 }
838
839 static void
840 lnet_update_ni_status_locked(void)
841 {
842         lnet_ni_t       *ni;
843         long            now;
844         int             timeout;
845
846         LASSERT(the_lnet.ln_routing);
847
848         timeout = router_ping_timeout +
849                   MAX(live_router_check_interval, dead_router_check_interval);
850
851         now = cfs_time_current_sec();
852         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
853                 if (ni->ni_lnd->lnd_type == LOLND)
854                         continue;
855
856                 if (now < ni->ni_last_alive + timeout)
857                         continue;
858
859                 lnet_ni_lock(ni);
860                 /* re-check with lock */
861                 if (now < ni->ni_last_alive + timeout) {
862                         lnet_ni_unlock(ni);
863                         continue;
864                 }
865
866                 LASSERT(ni->ni_status != NULL);
867
868                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
869                         CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
870                                libcfs_nid2str(ni->ni_nid), timeout);
871                         /* NB: so far, this is the only place to set
872                          * NI status to "down" */
873                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
874                 }
875                 lnet_ni_unlock(ni);
876         }
877 }
878
879 static void
880 lnet_destroy_rc_data(lnet_rc_data_t *rcd)
881 {
882         LASSERT(list_empty(&rcd->rcd_list));
883         /* detached from network */
884         LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
885
886         if (rcd->rcd_gateway != NULL) {
887                 int cpt = rcd->rcd_gateway->lp_cpt;
888
889                 lnet_net_lock(cpt);
890                 lnet_peer_decref_locked(rcd->rcd_gateway);
891                 lnet_net_unlock(cpt);
892         }
893
894         if (rcd->rcd_pinginfo != NULL)
895                 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
896
897         LIBCFS_FREE(rcd, sizeof(*rcd));
898 }
899
900 static lnet_rc_data_t *
901 lnet_create_rc_data_locked(lnet_peer_t *gateway)
902 {
903         lnet_rc_data_t          *rcd = NULL;
904         lnet_ping_info_t        *pi;
905         int                     rc;
906         int                     i;
907
908         lnet_net_unlock(gateway->lp_cpt);
909
910         LIBCFS_ALLOC(rcd, sizeof(*rcd));
911         if (rcd == NULL)
912                 goto out;
913
914         LNetInvalidateHandle(&rcd->rcd_mdh);
915         INIT_LIST_HEAD(&rcd->rcd_list);
916
917         LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
918         if (pi == NULL)
919                 goto out;
920
921         for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
922                 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
923                 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
924         }
925         rcd->rcd_pinginfo = pi;
926
927         LASSERT (!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
928         rc = LNetMDBind((lnet_md_t){.start     = pi,
929                                     .user_ptr  = rcd,
930                                     .length    = LNET_PINGINFO_SIZE,
931                                     .threshold = LNET_MD_THRESH_INF,
932                                     .options   = LNET_MD_TRUNCATE,
933                                     .eq_handle = the_lnet.ln_rc_eqh},
934                         LNET_UNLINK,
935                         &rcd->rcd_mdh);
936         if (rc < 0) {
937                 CERROR("Can't bind MD: %d\n", rc);
938                 goto out;
939         }
940         LASSERT(rc == 0);
941
942         lnet_net_lock(gateway->lp_cpt);
943         /* router table changed or someone has created rcd for this gateway */
944         if (!lnet_isrouter(gateway) || gateway->lp_rcd != NULL) {
945                 lnet_net_unlock(gateway->lp_cpt);
946                 goto out;
947         }
948
949         lnet_peer_addref_locked(gateway);
950         rcd->rcd_gateway = gateway;
951         gateway->lp_rcd = rcd;
952         gateway->lp_ping_notsent = 0;
953
954         return rcd;
955
956  out:
957         if (rcd != NULL) {
958                 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
959                         rc = LNetMDUnlink(rcd->rcd_mdh);
960                         LASSERT(rc == 0);
961                 }
962                 lnet_destroy_rc_data(rcd);
963         }
964
965         lnet_net_lock(gateway->lp_cpt);
966         return gateway->lp_rcd;
967 }
968
969 static int
970 lnet_router_check_interval (lnet_peer_t *rtr)
971 {
972         int secs;
973
974         secs = rtr->lp_alive ? live_router_check_interval :
975                                dead_router_check_interval;
976         if (secs < 0)
977                 secs = 0;
978
979         return secs;
980 }
981
982 static void
983 lnet_ping_router_locked (lnet_peer_t *rtr)
984 {
985         lnet_rc_data_t *rcd = NULL;
986         cfs_time_t      now = cfs_time_current();
987         int             secs;
988
989         lnet_peer_addref_locked(rtr);
990
991         if (rtr->lp_ping_deadline != 0 && /* ping timed out? */
992             cfs_time_after(now, rtr->lp_ping_deadline))
993                 lnet_notify_locked(rtr, 1, 0, now);
994
995         /* Run any outstanding notifications */
996         lnet_ni_notify_locked(rtr->lp_ni, rtr);
997
998         if (!lnet_isrouter(rtr) ||
999             the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1000                 /* router table changed or router checker is shutting down */
1001                 lnet_peer_decref_locked(rtr);
1002                 return;
1003         }
1004
1005         rcd = rtr->lp_rcd != NULL ?
1006               rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
1007
1008         if (rcd == NULL)
1009                 return;
1010
1011         secs = lnet_router_check_interval(rtr);
1012
1013         CDEBUG(D_NET,
1014                "rtr %s %d: deadline %lu ping_notsent %d alive %d "
1015                "alive_count %d lp_ping_timestamp %lu\n",
1016                libcfs_nid2str(rtr->lp_nid), secs,
1017                rtr->lp_ping_deadline, rtr->lp_ping_notsent,
1018                rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
1019
1020         if (secs != 0 && !rtr->lp_ping_notsent &&
1021             cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
1022                                              cfs_time_seconds(secs)))) {
1023                 int               rc;
1024                 lnet_process_id_t id;
1025                 lnet_handle_md_t  mdh;
1026
1027                 id.nid = rtr->lp_nid;
1028                 id.pid = LNET_PID_LUSTRE;
1029                 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
1030
1031                 rtr->lp_ping_notsent   = 1;
1032                 rtr->lp_ping_timestamp = now;
1033
1034                 mdh = rcd->rcd_mdh;
1035
1036                 if (rtr->lp_ping_deadline == 0) {
1037                         rtr->lp_ping_deadline =
1038                                 cfs_time_shift(router_ping_timeout);
1039                 }
1040
1041                 lnet_net_unlock(rtr->lp_cpt);
1042
1043                 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
1044                              LNET_PROTO_PING_MATCHBITS, 0);
1045
1046                 lnet_net_lock(rtr->lp_cpt);
1047                 if (rc != 0)
1048                         rtr->lp_ping_notsent = 0; /* no event pending */
1049         }
1050
1051         lnet_peer_decref_locked(rtr);
1052         return;
1053 }
1054
1055 int
1056 lnet_router_checker_start(void)
1057 {
1058         int                     rc;
1059         int                     eqsz = 0;
1060         struct task_struct     *task;
1061
1062         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1063
1064         if (check_routers_before_use &&
1065             dead_router_check_interval <= 0) {
1066                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
1067                                    " set if 'check_routers_before_use' is set"
1068                                    "\n");
1069                 return -EINVAL;
1070         }
1071
1072         sema_init(&the_lnet.ln_rc_signal, 0);
1073
1074         rc = LNetEQAlloc(0, lnet_router_checker_event, &the_lnet.ln_rc_eqh);
1075         if (rc != 0) {
1076                 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1077                 return -ENOMEM;
1078         }
1079
1080         the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
1081         task = kthread_run(lnet_router_checker, NULL, "router_checker");
1082         if (IS_ERR(task)) {
1083                 rc = PTR_ERR(task);
1084                 CERROR("Can't start router checker thread: %d\n", rc);
1085                 /* block until event callback signals exit */
1086                 down(&the_lnet.ln_rc_signal);
1087                 rc = LNetEQFree(the_lnet.ln_rc_eqh);
1088                 LASSERT(rc == 0);
1089                 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1090                 return -ENOMEM;
1091         }
1092
1093         if (check_routers_before_use) {
1094                 /* Note that a helpful side-effect of pinging all known routers
1095                  * at startup is that it makes them drop stale connections they
1096                  * may have to a previous instance of me. */
1097                 lnet_wait_known_routerstate();
1098         }
1099
1100         return 0;
1101 }
1102
1103 void
1104 lnet_router_checker_stop (void)
1105 {
1106         int rc;
1107
1108         if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1109                 return;
1110
1111         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1112         the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1113         /* wakeup the RC thread if it's sleeping */
1114         wake_up(&the_lnet.ln_rc_waitq);
1115
1116         /* block until event callback signals exit */
1117         down(&the_lnet.ln_rc_signal);
1118         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1119
1120         rc = LNetEQFree(the_lnet.ln_rc_eqh);
1121         LASSERT (rc == 0);
1122         return;
1123 }
1124
1125 static void
1126 lnet_prune_rc_data(int wait_unlink)
1127 {
1128         lnet_rc_data_t          *rcd;
1129         lnet_rc_data_t          *tmp;
1130         lnet_peer_t             *lp;
1131         struct list_head         head;
1132         int                      i = 2;
1133
1134         if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1135                    list_empty(&the_lnet.ln_rcd_deathrow) &&
1136                    list_empty(&the_lnet.ln_rcd_zombie)))
1137                 return;
1138
1139         INIT_LIST_HEAD(&head);
1140
1141         lnet_net_lock(LNET_LOCK_EX);
1142
1143         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1144                 /* router checker is stopping, prune all */
1145                 list_for_each_entry(lp, &the_lnet.ln_routers,
1146                                     lp_rtr_list) {
1147                         if (lp->lp_rcd == NULL)
1148                                 continue;
1149
1150                         LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1151                         list_add(&lp->lp_rcd->rcd_list,
1152                                  &the_lnet.ln_rcd_deathrow);
1153                         lp->lp_rcd = NULL;
1154                 }
1155         }
1156
1157         /* unlink all RCDs on deathrow list */
1158         list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1159
1160         if (!list_empty(&head)) {
1161                 lnet_net_unlock(LNET_LOCK_EX);
1162
1163                 list_for_each_entry(rcd, &head, rcd_list)
1164                         LNetMDUnlink(rcd->rcd_mdh);
1165
1166                 lnet_net_lock(LNET_LOCK_EX);
1167         }
1168
1169         list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1170
1171         /* release all zombie RCDs */
1172         while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1173                 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
1174                                          rcd_list) {
1175                         if (LNetHandleIsInvalid(rcd->rcd_mdh))
1176                                 list_move(&rcd->rcd_list, &head);
1177                 }
1178
1179                 wait_unlink = wait_unlink &&
1180                               !list_empty(&the_lnet.ln_rcd_zombie);
1181
1182                 lnet_net_unlock(LNET_LOCK_EX);
1183
1184                 while (!list_empty(&head)) {
1185                         rcd = list_entry(head.next,
1186                                          lnet_rc_data_t, rcd_list);
1187                         list_del_init(&rcd->rcd_list);
1188                         lnet_destroy_rc_data(rcd);
1189                 }
1190
1191                 if (!wait_unlink)
1192                         return;
1193
1194                 i++;
1195                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1196                        "Waiting for rc buffers to unlink\n");
1197                 set_current_state(TASK_UNINTERRUPTIBLE);
1198                 schedule_timeout(cfs_time_seconds(1) / 4);
1199
1200                 lnet_net_lock(LNET_LOCK_EX);
1201         }
1202
1203         lnet_net_unlock(LNET_LOCK_EX);
1204 }
1205
1206 /*
1207  * This function is called to check if the RC should block indefinitely.
1208  * It's called from lnet_router_checker() as well as being passed to
1209  * wait_event_interruptible() to avoid the lost wake_up problem.
1210  *
1211  * When it's called from wait_event_interruptible() it is necessary to
1212  * also not sleep if the rc state is not running to avoid a deadlock
1213  * when the system is shutting down
1214  */
1215 static inline bool
1216 lnet_router_checker_active(void)
1217 {
1218         if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING)
1219                 return true;
1220
1221         /* Router Checker thread needs to run when routing is enabled in
1222          * order to call lnet_update_ni_status_locked() */
1223         if (the_lnet.ln_routing)
1224                 return true;
1225
1226         return !list_empty(&the_lnet.ln_routers) &&
1227                 (live_router_check_interval > 0 ||
1228                  dead_router_check_interval > 0);
1229 }
1230
1231 static int
1232 lnet_router_checker(void *arg)
1233 {
1234         lnet_peer_t       *rtr;
1235         struct list_head  *entry;
1236
1237         cfs_block_allsigs();
1238
1239         LASSERT (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
1240
1241         while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
1242                 __u64   version;
1243                 int     cpt;
1244                 int     cpt2;
1245
1246                 cpt = lnet_net_lock_current();
1247 rescan:
1248                 version = the_lnet.ln_routers_version;
1249
1250                 list_for_each(entry, &the_lnet.ln_routers) {
1251                         rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1252
1253                         cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1254                         if (cpt != cpt2) {
1255                                 lnet_net_unlock(cpt);
1256                                 cpt = cpt2;
1257                                 lnet_net_lock(cpt);
1258                                 /* the routers list has changed */
1259                                 if (version != the_lnet.ln_routers_version)
1260                                         goto rescan;
1261                         }
1262
1263                         lnet_ping_router_locked(rtr);
1264
1265                         /* NB dropped lock */
1266                         if (version != the_lnet.ln_routers_version) {
1267                                 /* the routers list has changed */
1268                                 goto rescan;
1269                         }
1270                 }
1271
1272                 if (the_lnet.ln_routing)
1273                         lnet_update_ni_status_locked();
1274
1275                 lnet_net_unlock(cpt);
1276
1277                 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1278
1279                 /* Call schedule_timeout() here always adds 1 to load average
1280                  * because kernel counts # active tasks as nr_running
1281                  * + nr_uninterruptible. */
1282                 /* if there are any routes then wakeup every second.  If
1283                  * there are no routes then sleep indefinitely until woken
1284                  * up by a user adding a route */
1285                 if (!lnet_router_checker_active())
1286                         wait_event_interruptible(the_lnet.ln_rc_waitq,
1287                                                  lnet_router_checker_active());
1288                 else
1289                         wait_event_interruptible_timeout(the_lnet.ln_rc_waitq,
1290                                                          false,
1291                                                          cfs_time_seconds(1));
1292         }
1293
1294         LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1295
1296         lnet_prune_rc_data(1); /* wait for UNLINK */
1297
1298         the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1299         up(&the_lnet.ln_rc_signal);
1300         /* The unlink event callback will signal final completion */
1301         return 0;
1302 }
1303
1304 void
1305 lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1306 {
1307         int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1308
1309         while (--npages >= 0)
1310                 __free_page(rb->rb_kiov[npages].kiov_page);
1311
1312         LIBCFS_FREE(rb, sz);
1313 }
1314
1315 static lnet_rtrbuf_t *
1316 lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1317 {
1318         int            npages = rbp->rbp_npages;
1319         int            sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1320         struct page   *page;
1321         lnet_rtrbuf_t *rb;
1322         int            i;
1323
1324         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1325         if (rb == NULL)
1326                 return NULL;
1327
1328         rb->rb_pool = rbp;
1329
1330         for (i = 0; i < npages; i++) {
1331                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1332                                           __GFP_ZERO | GFP_IOFS);
1333                 if (page == NULL) {
1334                         while (--i >= 0)
1335                                 __free_page(rb->rb_kiov[i].kiov_page);
1336
1337                         LIBCFS_FREE(rb, sz);
1338                         return NULL;
1339                 }
1340
1341                 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1342                 rb->rb_kiov[i].kiov_offset = 0;
1343                 rb->rb_kiov[i].kiov_page = page;
1344         }
1345
1346         return rb;
1347 }
1348
1349 static void
1350 lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp, int cpt)
1351 {
1352         int              npages = rbp->rbp_npages;
1353         lnet_rtrbuf_t    *rb;
1354         struct list_head tmp;
1355
1356         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1357                 return;
1358
1359         INIT_LIST_HEAD(&tmp);
1360
1361         lnet_net_lock(cpt);
1362         lnet_drop_routed_msgs_locked(&rbp->rbp_msgs, cpt);
1363         list_splice_init(&rbp->rbp_bufs, &tmp);
1364         rbp->rbp_req_nbuffers = 0;
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             old_req_nbufs;
1385         int             npages = rbp->rbp_npages;
1386
1387         lnet_net_lock(cpt);
1388         /* If we are called for less buffers than already in the pool, we
1389          * just lower the req_nbuffers number and excess buffers will be
1390          * thrown away as they are returned to the free list.  Credits
1391          * then get adjusted as well.
1392          * If we already have enough buffers allocated to serve the
1393          * increase requested, then we can treat that the same way as we
1394          * do the decrease. */
1395         num_rb = nbufs - rbp->rbp_nbuffers;
1396         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1397                 rbp->rbp_req_nbuffers = nbufs;
1398                 lnet_net_unlock(cpt);
1399                 return 0;
1400         }
1401         /* store the older value of rbp_req_nbuffers and then set it to
1402          * the new request to prevent lnet_return_rx_credits_locked() from
1403          * freeing buffers that we need to keep around */
1404         old_req_nbufs = rbp->rbp_req_nbuffers;
1405         rbp->rbp_req_nbuffers = nbufs;
1406         lnet_net_unlock(cpt);
1407
1408         INIT_LIST_HEAD(&rb_list);
1409
1410         /* allocate the buffers on a local list first.  If all buffers are
1411          * allocated successfully then join this list to the rbp buffer
1412          * list.  If not then free all allocated buffers. */
1413         while (num_rb-- > 0) {
1414                 rb = lnet_new_rtrbuf(rbp, cpt);
1415                 if (rb == NULL) {
1416                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1417                                nbufs, npages);
1418
1419                         lnet_net_lock(cpt);
1420                         rbp->rbp_req_nbuffers = old_req_nbufs;
1421                         lnet_net_unlock(cpt);
1422
1423                         goto failed;
1424                 }
1425
1426                 list_add(&rb->rb_list, &rb_list);
1427                 num_buffers++;
1428         }
1429
1430         lnet_net_lock(cpt);
1431
1432         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1433         rbp->rbp_nbuffers += num_buffers;
1434         rbp->rbp_credits += num_buffers;
1435         rbp->rbp_mincredits = rbp->rbp_credits;
1436         /* We need to schedule blocked msg using the newly
1437          * added buffers. */
1438         while (!list_empty(&rbp->rbp_bufs) &&
1439                !list_empty(&rbp->rbp_msgs))
1440                 lnet_schedule_blocked_locked(rbp);
1441
1442         lnet_net_unlock(cpt);
1443
1444         return 0;
1445
1446 failed:
1447         while (!list_empty(&rb_list)) {
1448                 rb = list_entry(rb_list.next, lnet_rtrbuf_t, rb_list);
1449                 list_del(&rb->rb_list);
1450                 lnet_destroy_rtrbuf(rb, npages);
1451         }
1452
1453         return -ENOMEM;
1454 }
1455
1456 static void
1457 lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1458 {
1459         INIT_LIST_HEAD(&rbp->rbp_msgs);
1460         INIT_LIST_HEAD(&rbp->rbp_bufs);
1461
1462         rbp->rbp_npages = npages;
1463         rbp->rbp_credits = 0;
1464         rbp->rbp_mincredits = 0;
1465 }
1466
1467 void
1468 lnet_rtrpools_free(int keep_pools)
1469 {
1470         lnet_rtrbufpool_t *rtrp;
1471         int               i;
1472
1473         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1474                 return;
1475
1476         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1477                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1478                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1479                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1480         }
1481
1482         if (!keep_pools) {
1483                 cfs_percpt_free(the_lnet.ln_rtrpools);
1484                 the_lnet.ln_rtrpools = NULL;
1485         }
1486 }
1487
1488 static int
1489 lnet_nrb_tiny_calculate(void)
1490 {
1491         int     nrbs = LNET_NRB_TINY;
1492
1493         if (tiny_router_buffers < 0) {
1494                 LCONSOLE_ERROR_MSG(0x10c,
1495                                    "tiny_router_buffers=%d invalid when "
1496                                    "routing enabled\n", tiny_router_buffers);
1497                 return -1;
1498         }
1499
1500         if (tiny_router_buffers > 0)
1501                 nrbs = tiny_router_buffers;
1502
1503         nrbs /= LNET_CPT_NUMBER;
1504         return max(nrbs, LNET_NRB_TINY_MIN);
1505 }
1506
1507 static int
1508 lnet_nrb_small_calculate(void)
1509 {
1510         int     nrbs = LNET_NRB_SMALL;
1511
1512         if (small_router_buffers < 0) {
1513                 LCONSOLE_ERROR_MSG(0x10c,
1514                                    "small_router_buffers=%d invalid when "
1515                                    "routing enabled\n", small_router_buffers);
1516                 return -1;
1517         }
1518
1519         if (small_router_buffers > 0)
1520                 nrbs = small_router_buffers;
1521
1522         nrbs /= LNET_CPT_NUMBER;
1523         return max(nrbs, LNET_NRB_SMALL_MIN);
1524 }
1525
1526 static int
1527 lnet_nrb_large_calculate(void)
1528 {
1529         int     nrbs = LNET_NRB_LARGE;
1530
1531         if (large_router_buffers < 0) {
1532                 LCONSOLE_ERROR_MSG(0x10c,
1533                                    "large_router_buffers=%d invalid when "
1534                                    "routing enabled\n", large_router_buffers);
1535                 return -1;
1536         }
1537
1538         if (large_router_buffers > 0)
1539                 nrbs = large_router_buffers;
1540
1541         nrbs /= LNET_CPT_NUMBER;
1542         return max(nrbs, LNET_NRB_LARGE_MIN);
1543 }
1544
1545 int
1546 lnet_rtrpools_alloc(int im_a_router)
1547 {
1548         lnet_rtrbufpool_t *rtrp;
1549         int     nrb_tiny;
1550         int     nrb_small;
1551         int     nrb_large;
1552         int     rc;
1553         int     i;
1554
1555         if (!strcmp(forwarding, "")) {
1556                 /* not set either way */
1557                 if (!im_a_router)
1558                         return 0;
1559         } else if (!strcmp(forwarding, "disabled")) {
1560                 /* explicitly disabled */
1561                 return 0;
1562         } else if (!strcmp(forwarding, "enabled")) {
1563                 /* explicitly enabled */
1564         } else {
1565                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1566                                    "'enabled' or 'disabled'\n");
1567                 return -EINVAL;
1568         }
1569
1570         nrb_tiny = lnet_nrb_tiny_calculate();
1571         if (nrb_tiny < 0)
1572                 return -EINVAL;
1573
1574         nrb_small = lnet_nrb_small_calculate();
1575         if (nrb_small < 0)
1576                 return -EINVAL;
1577
1578         nrb_large = lnet_nrb_large_calculate();
1579         if (nrb_large < 0)
1580                 return -EINVAL;
1581
1582         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1583                                                 LNET_NRBPOOLS *
1584                                                 sizeof(lnet_rtrbufpool_t));
1585         if (the_lnet.ln_rtrpools == NULL) {
1586                 LCONSOLE_ERROR_MSG(0x10c,
1587                                    "Failed to initialize router buffe pool\n");
1588                 return -ENOMEM;
1589         }
1590
1591         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1592                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1593                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1594                                               nrb_tiny, i);
1595                 if (rc != 0)
1596                         goto failed;
1597
1598                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1599                                   LNET_NRB_SMALL_PAGES);
1600                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1601                                               nrb_small, i);
1602                 if (rc != 0)
1603                         goto failed;
1604
1605                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1606                                   LNET_NRB_LARGE_PAGES);
1607                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1608                                               nrb_large, i);
1609                 if (rc != 0)
1610                         goto failed;
1611         }
1612
1613         lnet_net_lock(LNET_LOCK_EX);
1614         the_lnet.ln_routing = 1;
1615         lnet_net_unlock(LNET_LOCK_EX);
1616         return 0;
1617
1618  failed:
1619         lnet_rtrpools_free(0);
1620         return rc;
1621 }
1622
1623 static int
1624 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1625 {
1626         int nrb = 0;
1627         int rc = 0;
1628         int i;
1629         lnet_rtrbufpool_t *rtrp;
1630
1631         /* If the provided values for each buffer pool are different than the
1632          * configured values, we need to take action. */
1633         if (tiny >= 0) {
1634                 tiny_router_buffers = tiny;
1635                 nrb = lnet_nrb_tiny_calculate();
1636                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1637                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1638                                                       nrb, i);
1639                         if (rc != 0)
1640                                 return rc;
1641                 }
1642         }
1643         if (small >= 0) {
1644                 small_router_buffers = small;
1645                 nrb = lnet_nrb_small_calculate();
1646                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1647                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1648                                                       nrb, i);
1649                         if (rc != 0)
1650                                 return rc;
1651                 }
1652         }
1653         if (large >= 0) {
1654                 large_router_buffers = large;
1655                 nrb = lnet_nrb_large_calculate();
1656                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1657                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1658                                                       nrb, i);
1659                         if (rc != 0)
1660                                 return rc;
1661                 }
1662         }
1663
1664         return 0;
1665 }
1666
1667 int
1668 lnet_rtrpools_adjust(int tiny, int small, int large)
1669 {
1670         /* this function doesn't revert the changes if adding new buffers
1671          * failed.  It's up to the user space caller to revert the
1672          * changes. */
1673
1674         if (!the_lnet.ln_routing)
1675                 return 0;
1676
1677         return lnet_rtrpools_adjust_helper(tiny, small, large);
1678 }
1679
1680 int
1681 lnet_rtrpools_enable(void)
1682 {
1683         int rc;
1684
1685         if (the_lnet.ln_routing)
1686                 return 0;
1687
1688         if (the_lnet.ln_rtrpools == NULL)
1689                 /* If routing is turned off, and we have never
1690                  * initialized the pools before, just call the
1691                  * standard buffer pool allocation routine as
1692                  * if we are just configuring this for the first
1693                  * time. */
1694                 return lnet_rtrpools_alloc(1);
1695
1696         rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1697         if (rc != 0)
1698                 return rc;
1699
1700         lnet_net_lock(LNET_LOCK_EX);
1701         the_lnet.ln_routing = 1;
1702
1703         the_lnet.ln_ping_info->pi_features &= ~LNET_PING_FEAT_RTE_DISABLED;
1704         lnet_net_unlock(LNET_LOCK_EX);
1705
1706         return 0;
1707 }
1708
1709 void
1710 lnet_rtrpools_disable(void)
1711 {
1712         if (!the_lnet.ln_routing)
1713                 return;
1714
1715         lnet_net_lock(LNET_LOCK_EX);
1716         the_lnet.ln_routing = 0;
1717         the_lnet.ln_ping_info->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1718
1719         tiny_router_buffers = 0;
1720         small_router_buffers = 0;
1721         large_router_buffers = 0;
1722         lnet_net_unlock(LNET_LOCK_EX);
1723         lnet_rtrpools_free(1);
1724 }
1725
1726 int
1727 lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, cfs_time_t when)
1728 {
1729         struct lnet_peer        *lp = NULL;
1730         cfs_time_t              now = cfs_time_current();
1731         int                     cpt = lnet_cpt_of_nid(nid);
1732
1733         LASSERT (!in_interrupt ());
1734
1735         CDEBUG (D_NET, "%s notifying %s: %s\n",
1736                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1737                 libcfs_nid2str(nid),
1738                 alive ? "up" : "down");
1739
1740         if (ni != NULL &&
1741             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1742                 CWARN ("Ignoring notification of %s %s by %s (different net)\n",
1743                         libcfs_nid2str(nid), alive ? "birth" : "death",
1744                         libcfs_nid2str(ni->ni_nid));
1745                 return -EINVAL;
1746         }
1747
1748         /* can't do predictions... */
1749         if (cfs_time_after(when, now)) {
1750                 CWARN ("Ignoring prediction from %s of %s %s "
1751                        "%ld seconds in the future\n",
1752                        (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1753                        libcfs_nid2str(nid), alive ? "up" : "down",
1754                        cfs_duration_sec(cfs_time_sub(when, now)));
1755                 return -EINVAL;
1756         }
1757
1758         if (ni != NULL && !alive &&             /* LND telling me she's down */
1759             !auto_down) {                       /* auto-down disabled */
1760                 CDEBUG(D_NET, "Auto-down disabled\n");
1761                 return 0;
1762         }
1763
1764         lnet_net_lock(cpt);
1765
1766         if (the_lnet.ln_shutdown) {
1767                 lnet_net_unlock(cpt);
1768                 return -ESHUTDOWN;
1769         }
1770
1771         lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
1772         if (lp == NULL) {
1773                 /* nid not found */
1774                 lnet_net_unlock(cpt);
1775                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1776                 return 0;
1777         }
1778
1779         /* We can't fully trust LND on reporting exact peer last_alive
1780          * if he notifies us about dead peer. For example ksocklnd can
1781          * call us with when == _time_when_the_node_was_booted_ if
1782          * no connections were successfully established */
1783         if (ni != NULL && !alive && when < lp->lp_last_alive)
1784                 when = lp->lp_last_alive;
1785
1786         lnet_notify_locked(lp, ni == NULL, alive, when);
1787
1788         if (ni != NULL)
1789                 lnet_ni_notify_locked(ni, lp);
1790
1791         lnet_peer_decref_locked(lp);
1792
1793         lnet_net_unlock(cpt);
1794         return 0;
1795 }
1796 EXPORT_SYMBOL(lnet_notify);