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