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