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