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