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