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