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