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