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