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