staging: lustre: return appropriate errno when adding route
[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 */
be8240ac 320 return -EEXIST;
d7e09d03
PT
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 */
be8240ac 351 return rc; /* 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);
be8240ac 398 rc = 0;
d7e09d03 399
be8240ac
AS
400 if (!add_route) {
401 rc = -EEXIST;
d7e09d03 402 LIBCFS_FREE(route, sizeof(*route));
be8240ac 403 }
d7e09d03
PT
404
405 if (rnet != rnet2)
406 LIBCFS_FREE(rnet, sizeof(*rnet));
407
be8240ac 408 return rc;
d7e09d03
PT
409}
410
411int
412lnet_check_routes(void)
413{
7e7ab095
MS
414 lnet_remotenet_t *rnet;
415 lnet_route_t *route;
416 lnet_route_t *route2;
417 struct list_head *e1;
418 struct list_head *e2;
419 int cpt;
420 struct list_head *rn_list;
421 int i;
d7e09d03
PT
422
423 cpt = lnet_net_lock_current();
424
425 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
426 rn_list = &the_lnet.ln_remote_nets_hash[i];
427 list_for_each(e1, rn_list) {
428 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
429
430 route2 = NULL;
431 list_for_each(e2, &rnet->lrn_routes) {
7e7ab095
MS
432 lnet_nid_t nid1;
433 lnet_nid_t nid2;
434 int net;
d7e09d03 435
c314c319 436 route = list_entry(e2, lnet_route_t, lr_list);
d7e09d03 437
06ace26e 438 if (!route2) {
d7e09d03
PT
439 route2 = route;
440 continue;
441 }
442
443 if (route->lr_gateway->lp_ni ==
444 route2->lr_gateway->lp_ni)
445 continue;
446
447 nid1 = route->lr_gateway->lp_nid;
448 nid2 = route2->lr_gateway->lp_nid;
449 net = rnet->lrn_net;
450
451 lnet_net_unlock(cpt);
452
2d00bd17 453 CERROR("Routes to %s via %s and %s not supported\n",
d7e09d03
PT
454 libcfs_net2str(net),
455 libcfs_nid2str(nid1),
456 libcfs_nid2str(nid2));
457 return -EINVAL;
458 }
459 }
460 }
461
462 lnet_net_unlock(cpt);
463 return 0;
464}
465
466int
467lnet_del_route(__u32 net, lnet_nid_t gw_nid)
468{
7e7ab095
MS
469 struct lnet_peer *gateway;
470 lnet_remotenet_t *rnet;
471 lnet_route_t *route;
472 struct list_head *e1;
473 struct list_head *e2;
474 int rc = -ENOENT;
475 struct list_head *rn_list;
476 int idx = 0;
d7e09d03
PT
477
478 CDEBUG(D_NET, "Del route: net %s : gw %s\n",
479 libcfs_net2str(net), libcfs_nid2str(gw_nid));
480
4420cfd3
JS
481 /*
482 * NB Caller may specify either all routes via the given gateway
483 * or a specific route entry actual NIDs)
484 */
d7e09d03
PT
485 lnet_net_lock(LNET_LOCK_EX);
486 if (net == LNET_NIDNET(LNET_NID_ANY))
487 rn_list = &the_lnet.ln_remote_nets_hash[0];
488 else
489 rn_list = lnet_net2rnethash(net);
490
491 again:
492 list_for_each(e1, rn_list) {
493 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
494
495 if (!(net == LNET_NIDNET(LNET_NID_ANY) ||
c314c319 496 net == rnet->lrn_net))
d7e09d03
PT
497 continue;
498
499 list_for_each(e2, &rnet->lrn_routes) {
500 route = list_entry(e2, lnet_route_t, lr_list);
501
502 gateway = route->lr_gateway;
503 if (!(gw_nid == LNET_NID_ANY ||
504 gw_nid == gateway->lp_nid))
505 continue;
506
507 list_del(&route->lr_list);
508 list_del(&route->lr_gwlist);
509 the_lnet.ln_remote_nets_version++;
510
511 if (list_empty(&rnet->lrn_routes))
512 list_del(&rnet->lrn_list);
513 else
514 rnet = NULL;
515
516 lnet_rtr_decref_locked(gateway);
517 lnet_peer_decref_locked(gateway);
518
519 lnet_net_unlock(LNET_LOCK_EX);
520
521 LIBCFS_FREE(route, sizeof(*route));
522
06ace26e 523 if (rnet)
d7e09d03
PT
524 LIBCFS_FREE(rnet, sizeof(*rnet));
525
526 rc = 0;
527 lnet_net_lock(LNET_LOCK_EX);
528 goto again;
529 }
530 }
531
532 if (net == LNET_NIDNET(LNET_NID_ANY) &&
533 ++idx < LNET_REMOTE_NETS_HASH_SIZE) {
534 rn_list = &the_lnet.ln_remote_nets_hash[idx];
535 goto again;
536 }
537 lnet_net_unlock(LNET_LOCK_EX);
538
539 return rc;
540}
541
542void
939af333 543lnet_destroy_routes(void)
d7e09d03
PT
544{
545 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
546}
547
edeb5d8c
AS
548int lnet_get_rtr_pool_cfg(int idx, struct lnet_ioctl_pool_cfg *pool_cfg)
549{
550 int i, rc = -ENOENT, j;
551
552 if (!the_lnet.ln_rtrpools)
553 return rc;
554
555 for (i = 0; i < LNET_NRBPOOLS; i++) {
556 lnet_rtrbufpool_t *rbp;
557
558 lnet_net_lock(LNET_LOCK_EX);
559 cfs_percpt_for_each(rbp, j, the_lnet.ln_rtrpools) {
560 if (i++ != idx)
561 continue;
562
563 pool_cfg->pl_pools[i].pl_npages = rbp[i].rbp_npages;
564 pool_cfg->pl_pools[i].pl_nbuffers = rbp[i].rbp_nbuffers;
565 pool_cfg->pl_pools[i].pl_credits = rbp[i].rbp_credits;
566 pool_cfg->pl_pools[i].pl_mincredits = rbp[i].rbp_mincredits;
567 rc = 0;
568 break;
569 }
570 lnet_net_unlock(LNET_LOCK_EX);
571 }
572
573 lnet_net_lock(LNET_LOCK_EX);
574 pool_cfg->pl_routing = the_lnet.ln_routing;
575 lnet_net_unlock(LNET_LOCK_EX);
576
577 return rc;
578}
579
d7e09d03
PT
580int
581lnet_get_route(int idx, __u32 *net, __u32 *hops,
e75fb87f 582 lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
d7e09d03 583{
7e7ab095
MS
584 struct list_head *e1;
585 struct list_head *e2;
586 lnet_remotenet_t *rnet;
587 lnet_route_t *route;
588 int cpt;
589 int i;
590 struct list_head *rn_list;
d7e09d03
PT
591
592 cpt = lnet_net_lock_current();
593
594 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
595 rn_list = &the_lnet.ln_remote_nets_hash[i];
596 list_for_each(e1, rn_list) {
597 rnet = list_entry(e1, lnet_remotenet_t, lrn_list);
598
599 list_for_each(e2, &rnet->lrn_routes) {
c314c319 600 route = list_entry(e2, lnet_route_t, lr_list);
d7e09d03 601
5fd88337 602 if (!idx--) {
7e7ab095
MS
603 *net = rnet->lrn_net;
604 *hops = route->lr_hops;
e75fb87f
DO
605 *priority = route->lr_priority;
606 *gateway = route->lr_gateway->lp_nid;
86ef6250
AS
607 *alive = route->lr_gateway->lp_alive &&
608 !route->lr_downis;
d7e09d03
PT
609 lnet_net_unlock(cpt);
610 return 0;
611 }
612 }
613 }
614 }
615
616 lnet_net_unlock(cpt);
617 return -ENOENT;
618}
619
620void
621lnet_swap_pinginfo(lnet_ping_info_t *info)
622{
7e7ab095 623 int i;
d7e09d03
PT
624 lnet_ni_status_t *stat;
625
626 __swab32s(&info->pi_magic);
627 __swab32s(&info->pi_features);
628 __swab32s(&info->pi_pid);
629 __swab32s(&info->pi_nnis);
630 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
631 stat = &info->pi_ni[i];
632 __swab64s(&stat->ns_nid);
633 __swab32s(&stat->ns_status);
634 }
d7e09d03
PT
635}
636
637/**
638 * parse router-checker pinginfo, record number of down NIs for remote
639 * networks on that router.
640 */
641static void
642lnet_parse_rc_info(lnet_rc_data_t *rcd)
643{
7e7ab095
MS
644 lnet_ping_info_t *info = rcd->rcd_pinginfo;
645 struct lnet_peer *gw = rcd->rcd_gateway;
86ef6250 646 lnet_route_t *rte;
d7e09d03
PT
647
648 if (!gw->lp_alive)
649 return;
650
651 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
652 lnet_swap_pinginfo(info);
653
654 /* NB always racing with network! */
655 if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
656 CDEBUG(D_NET, "%s: Unexpected magic %08x\n",
657 libcfs_nid2str(gw->lp_nid), info->pi_magic);
658 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
659 return;
660 }
661
662 gw->lp_ping_feats = info->pi_features;
5fd88337 663 if (!(gw->lp_ping_feats & LNET_PING_FEAT_MASK)) {
d7e09d03
PT
664 CDEBUG(D_NET, "%s: Unexpected features 0x%x\n",
665 libcfs_nid2str(gw->lp_nid), gw->lp_ping_feats);
666 return; /* nothing I can understand */
667 }
668
5fd88337 669 if (!(gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS))
d7e09d03
PT
670 return; /* can't carry NI status info */
671
86ef6250 672 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
7e7ab095
MS
673 int down = 0;
674 int up = 0;
675 int i;
d7e09d03 676
86ef6250
AS
677 if (gw->lp_ping_feats & LNET_PING_FEAT_RTE_DISABLED) {
678 rte->lr_downis = 1;
679 continue;
680 }
681
d7e09d03
PT
682 for (i = 0; i < info->pi_nnis && i < LNET_MAX_RTR_NIS; i++) {
683 lnet_ni_status_t *stat = &info->pi_ni[i];
7e7ab095 684 lnet_nid_t nid = stat->ns_nid;
d7e09d03
PT
685
686 if (nid == LNET_NID_ANY) {
687 CDEBUG(D_NET, "%s: unexpected LNET_NID_ANY\n",
688 libcfs_nid2str(gw->lp_nid));
689 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
690 return;
691 }
692
693 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
694 continue;
695
696 if (stat->ns_status == LNET_NI_STATUS_DOWN) {
764d2e9a 697 down++;
d7e09d03
PT
698 continue;
699 }
700
701 if (stat->ns_status == LNET_NI_STATUS_UP) {
86ef6250 702 if (LNET_NIDNET(nid) == rte->lr_net) {
d7e09d03
PT
703 up = 1;
704 break;
705 }
d7e09d03
PT
706 continue;
707 }
708
709 CDEBUG(D_NET, "%s: Unexpected status 0x%x\n",
710 libcfs_nid2str(gw->lp_nid), stat->ns_status);
711 gw->lp_ping_feats = LNET_PING_FEAT_INVAL;
712 return;
713 }
714
715 if (up) { /* ignore downed NIs if NI for dest network is up */
86ef6250 716 rte->lr_downis = 0;
d7e09d03
PT
717 continue;
718 }
86ef6250 719 rte->lr_downis = down;
d7e09d03
PT
720 }
721}
722
723static void
724lnet_router_checker_event(lnet_event_t *event)
725{
7e7ab095
MS
726 lnet_rc_data_t *rcd = event->md.user_ptr;
727 struct lnet_peer *lp;
d7e09d03 728
06ace26e 729 LASSERT(rcd);
d7e09d03
PT
730
731 if (event->unlinked) {
732 LNetInvalidateHandle(&rcd->rcd_mdh);
733 return;
734 }
735
736 LASSERT(event->type == LNET_EVENT_SEND ||
737 event->type == LNET_EVENT_REPLY);
738
739 lp = rcd->rcd_gateway;
06ace26e 740 LASSERT(lp);
d7e09d03 741
4420cfd3
JS
742 /*
743 * NB: it's called with holding lnet_res_lock, we have a few
744 * places need to hold both locks at the same time, please take
745 * care of lock ordering
746 */
d7e09d03
PT
747 lnet_net_lock(lp->lp_cpt);
748 if (!lnet_isrouter(lp) || lp->lp_rcd != rcd) {
749 /* ignore if no longer a router or rcd is replaced */
750 goto out;
751 }
752
753 if (event->type == LNET_EVENT_SEND) {
754 lp->lp_ping_notsent = 0;
5fd88337 755 if (!event->status)
d7e09d03
PT
756 goto out;
757 }
758
759 /* LNET_EVENT_REPLY */
4420cfd3
JS
760 /*
761 * A successful REPLY means the router is up. If _any_ comms
d7e09d03
PT
762 * to the router fail I assume it's down (this will happen if
763 * we ping alive routers to try to detect router death before
4420cfd3
JS
764 * apps get burned).
765 */
5fd88337 766 lnet_notify_locked(lp, 1, !event->status, cfs_time_current());
4420cfd3
JS
767
768 /*
769 * The router checker will wake up very shortly and do the
d7e09d03
PT
770 * actual notification.
771 * XXX If 'lp' stops being a router before then, it will still
4420cfd3
JS
772 * have the notification pending!!!
773 */
5fd88337 774 if (avoid_asym_router_failure && !event->status)
d7e09d03
PT
775 lnet_parse_rc_info(rcd);
776
777 out:
778 lnet_net_unlock(lp->lp_cpt);
779}
780
2595fa36 781static void
d7e09d03
PT
782lnet_wait_known_routerstate(void)
783{
7e7ab095
MS
784 lnet_peer_t *rtr;
785 struct list_head *entry;
786 int all_known;
d7e09d03 787
939af333 788 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
d7e09d03
PT
789
790 for (;;) {
7e7ab095 791 int cpt = lnet_net_lock_current();
d7e09d03
PT
792
793 all_known = 1;
939af333 794 list_for_each(entry, &the_lnet.ln_routers) {
d7e09d03
PT
795 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
796
5fd88337 797 if (!rtr->lp_alive_count) {
d7e09d03
PT
798 all_known = 0;
799 break;
800 }
801 }
802
803 lnet_net_unlock(cpt);
804
805 if (all_known)
806 return;
807
d3caf4d5
PT
808 set_current_state(TASK_UNINTERRUPTIBLE);
809 schedule_timeout(cfs_time_seconds(1));
d7e09d03
PT
810 }
811}
812
af3fa7c7
LZ
813void
814lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net)
815{
816 lnet_route_t *rte;
817
5fd88337 818 if ((gw->lp_ping_feats & LNET_PING_FEAT_NI_STATUS)) {
af3fa7c7
LZ
819 list_for_each_entry(rte, &gw->lp_routes, lr_gwlist) {
820 if (rte->lr_net == net) {
821 rte->lr_downis = 0;
822 break;
823 }
824 }
825 }
826}
827
2595fa36 828static void
d7e09d03
PT
829lnet_update_ni_status_locked(void)
830{
7e7ab095 831 lnet_ni_t *ni;
ec0067d1 832 time64_t now;
7e7ab095 833 int timeout;
d7e09d03
PT
834
835 LASSERT(the_lnet.ln_routing);
836
837 timeout = router_ping_timeout +
0c575417 838 max(live_router_check_interval, dead_router_check_interval);
d7e09d03 839
ec0067d1 840 now = ktime_get_real_seconds();
d7e09d03
PT
841 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
842 if (ni->ni_lnd->lnd_type == LOLND)
843 continue;
844
845 if (now < ni->ni_last_alive + timeout)
846 continue;
847
848 lnet_ni_lock(ni);
849 /* re-check with lock */
850 if (now < ni->ni_last_alive + timeout) {
851 lnet_ni_unlock(ni);
852 continue;
853 }
854
06ace26e 855 LASSERT(ni->ni_status);
d7e09d03
PT
856
857 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
858 CDEBUG(D_NET, "NI(%s:%d) status changed to down\n",
859 libcfs_nid2str(ni->ni_nid), timeout);
4420cfd3
JS
860 /*
861 * NB: so far, this is the only place to set
862 * NI status to "down"
863 */
d7e09d03
PT
864 ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
865 }
866 lnet_ni_unlock(ni);
867 }
868}
869
2595fa36 870static void
d7e09d03
PT
871lnet_destroy_rc_data(lnet_rc_data_t *rcd)
872{
873 LASSERT(list_empty(&rcd->rcd_list));
874 /* detached from network */
875 LASSERT(LNetHandleIsInvalid(rcd->rcd_mdh));
876
06ace26e 877 if (rcd->rcd_gateway) {
d7e09d03
PT
878 int cpt = rcd->rcd_gateway->lp_cpt;
879
880 lnet_net_lock(cpt);
881 lnet_peer_decref_locked(rcd->rcd_gateway);
882 lnet_net_unlock(cpt);
883 }
884
06ace26e 885 if (rcd->rcd_pinginfo)
d7e09d03
PT
886 LIBCFS_FREE(rcd->rcd_pinginfo, LNET_PINGINFO_SIZE);
887
888 LIBCFS_FREE(rcd, sizeof(*rcd));
889}
890
2595fa36 891static lnet_rc_data_t *
d7e09d03
PT
892lnet_create_rc_data_locked(lnet_peer_t *gateway)
893{
7e7ab095
MS
894 lnet_rc_data_t *rcd = NULL;
895 lnet_ping_info_t *pi;
896 int rc;
897 int i;
d7e09d03
PT
898
899 lnet_net_unlock(gateway->lp_cpt);
900
901 LIBCFS_ALLOC(rcd, sizeof(*rcd));
06ace26e 902 if (!rcd)
d7e09d03
PT
903 goto out;
904
905 LNetInvalidateHandle(&rcd->rcd_mdh);
906 INIT_LIST_HEAD(&rcd->rcd_list);
907
908 LIBCFS_ALLOC(pi, LNET_PINGINFO_SIZE);
06ace26e 909 if (!pi)
d7e09d03
PT
910 goto out;
911
d7e09d03
PT
912 for (i = 0; i < LNET_MAX_RTR_NIS; i++) {
913 pi->pi_ni[i].ns_nid = LNET_NID_ANY;
914 pi->pi_ni[i].ns_status = LNET_NI_STATUS_INVALID;
915 }
916 rcd->rcd_pinginfo = pi;
917
939af333 918 LASSERT(!LNetHandleIsInvalid(the_lnet.ln_rc_eqh));
d7e09d03
PT
919 rc = LNetMDBind((lnet_md_t){.start = pi,
920 .user_ptr = rcd,
921 .length = LNET_PINGINFO_SIZE,
922 .threshold = LNET_MD_THRESH_INF,
923 .options = LNET_MD_TRUNCATE,
924 .eq_handle = the_lnet.ln_rc_eqh},
925 LNET_UNLINK,
926 &rcd->rcd_mdh);
927 if (rc < 0) {
928 CERROR("Can't bind MD: %d\n", rc);
929 goto out;
930 }
5fd88337 931 LASSERT(!rc);
d7e09d03
PT
932
933 lnet_net_lock(gateway->lp_cpt);
934 /* router table changed or someone has created rcd for this gateway */
06ace26e 935 if (!lnet_isrouter(gateway) || gateway->lp_rcd) {
d7e09d03
PT
936 lnet_net_unlock(gateway->lp_cpt);
937 goto out;
938 }
939
940 lnet_peer_addref_locked(gateway);
941 rcd->rcd_gateway = gateway;
942 gateway->lp_rcd = rcd;
943 gateway->lp_ping_notsent = 0;
944
945 return rcd;
946
947 out:
06ace26e 948 if (rcd) {
d7e09d03
PT
949 if (!LNetHandleIsInvalid(rcd->rcd_mdh)) {
950 rc = LNetMDUnlink(rcd->rcd_mdh);
5fd88337 951 LASSERT(!rc);
d7e09d03
PT
952 }
953 lnet_destroy_rc_data(rcd);
954 }
955
956 lnet_net_lock(gateway->lp_cpt);
957 return gateway->lp_rcd;
958}
959
960static int
939af333 961lnet_router_check_interval(lnet_peer_t *rtr)
d7e09d03
PT
962{
963 int secs;
964
965 secs = rtr->lp_alive ? live_router_check_interval :
966 dead_router_check_interval;
967 if (secs < 0)
968 secs = 0;
969
970 return secs;
971}
972
973static void
939af333 974lnet_ping_router_locked(lnet_peer_t *rtr)
d7e09d03
PT
975{
976 lnet_rc_data_t *rcd = NULL;
7e7ab095
MS
977 unsigned long now = cfs_time_current();
978 int secs;
d7e09d03
PT
979
980 lnet_peer_addref_locked(rtr);
981
5fd88337 982 if (rtr->lp_ping_deadline && /* ping timed out? */
d7e09d03
PT
983 cfs_time_after(now, rtr->lp_ping_deadline))
984 lnet_notify_locked(rtr, 1, 0, now);
985
986 /* Run any outstanding notifications */
987 lnet_ni_notify_locked(rtr->lp_ni, rtr);
988
989 if (!lnet_isrouter(rtr) ||
990 the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
991 /* router table changed or router checker is shutting down */
992 lnet_peer_decref_locked(rtr);
993 return;
994 }
995
06ace26e 996 rcd = rtr->lp_rcd ?
d7e09d03
PT
997 rtr->lp_rcd : lnet_create_rc_data_locked(rtr);
998
06ace26e 999 if (!rcd)
d7e09d03
PT
1000 return;
1001
1002 secs = lnet_router_check_interval(rtr);
1003
1004 CDEBUG(D_NET,
2d00bd17 1005 "rtr %s %d: deadline %lu ping_notsent %d alive %d alive_count %d lp_ping_timestamp %lu\n",
d7e09d03
PT
1006 libcfs_nid2str(rtr->lp_nid), secs,
1007 rtr->lp_ping_deadline, rtr->lp_ping_notsent,
1008 rtr->lp_alive, rtr->lp_alive_count, rtr->lp_ping_timestamp);
1009
5fd88337 1010 if (secs && !rtr->lp_ping_notsent &&
d7e09d03
PT
1011 cfs_time_after(now, cfs_time_add(rtr->lp_ping_timestamp,
1012 cfs_time_seconds(secs)))) {
7e7ab095 1013 int rc;
d7e09d03 1014 lnet_process_id_t id;
7e7ab095 1015 lnet_handle_md_t mdh;
d7e09d03
PT
1016
1017 id.nid = rtr->lp_nid;
fe7cb65d 1018 id.pid = LNET_PID_LUSTRE;
d7e09d03
PT
1019 CDEBUG(D_NET, "Check: %s\n", libcfs_id2str(id));
1020
1021 rtr->lp_ping_notsent = 1;
1022 rtr->lp_ping_timestamp = now;
1023
1024 mdh = rcd->rcd_mdh;
1025
5fd88337 1026 if (!rtr->lp_ping_deadline) {
d7e09d03
PT
1027 rtr->lp_ping_deadline =
1028 cfs_time_shift(router_ping_timeout);
1029 }
1030
1031 lnet_net_unlock(rtr->lp_cpt);
1032
1033 rc = LNetGet(LNET_NID_ANY, mdh, id, LNET_RESERVED_PORTAL,
1034 LNET_PROTO_PING_MATCHBITS, 0);
1035
1036 lnet_net_lock(rtr->lp_cpt);
5fd88337 1037 if (rc)
d7e09d03
PT
1038 rtr->lp_ping_notsent = 0; /* no event pending */
1039 }
1040
1041 lnet_peer_decref_locked(rtr);
d7e09d03
PT
1042}
1043
1044int
1045lnet_router_checker_start(void)
1046{
060c2820 1047 struct task_struct *task;
7e7ab095
MS
1048 int rc;
1049 int eqsz;
d7e09d03 1050
939af333 1051 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
d7e09d03
PT
1052
1053 if (check_routers_before_use &&
1054 dead_router_check_interval <= 0) {
2d00bd17 1055 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be set if 'check_routers_before_use' is set\n");
d7e09d03
PT
1056 return -EINVAL;
1057 }
1058
1059 if (!the_lnet.ln_routing &&
1060 live_router_check_interval <= 0 &&
1061 dead_router_check_interval <= 0)
1062 return 0;
1063
1064 sema_init(&the_lnet.ln_rc_signal, 0);
4420cfd3
JS
1065 /*
1066 * EQ size doesn't matter; the callback is guaranteed to get every
1067 * event
1068 */
d7e09d03
PT
1069 eqsz = 0;
1070 rc = LNetEQAlloc(eqsz, lnet_router_checker_event,
1071 &the_lnet.ln_rc_eqh);
5fd88337 1072 if (rc) {
d7e09d03
PT
1073 CERROR("Can't allocate EQ(%d): %d\n", eqsz, rc);
1074 return -ENOMEM;
1075 }
1076
1077 the_lnet.ln_rc_state = LNET_RC_STATE_RUNNING;
060c2820
JH
1078 task = kthread_run(lnet_router_checker, NULL, "router_checker");
1079 if (IS_ERR(task)) {
1080 rc = PTR_ERR(task);
d7e09d03
PT
1081 CERROR("Can't start router checker thread: %d\n", rc);
1082 /* block until event callback signals exit */
1083 down(&the_lnet.ln_rc_signal);
1084 rc = LNetEQFree(the_lnet.ln_rc_eqh);
5fd88337 1085 LASSERT(!rc);
d7e09d03
PT
1086 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1087 return -ENOMEM;
1088 }
1089
1090 if (check_routers_before_use) {
4420cfd3
JS
1091 /*
1092 * Note that a helpful side-effect of pinging all known routers
d7e09d03 1093 * at startup is that it makes them drop stale connections they
4420cfd3
JS
1094 * may have to a previous instance of me.
1095 */
d7e09d03
PT
1096 lnet_wait_known_routerstate();
1097 }
1098
1099 return 0;
1100}
1101
1102void
939af333 1103lnet_router_checker_stop(void)
d7e09d03
PT
1104{
1105 int rc;
1106
1107 if (the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN)
1108 return;
1109
939af333 1110 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
d7e09d03
PT
1111 the_lnet.ln_rc_state = LNET_RC_STATE_STOPPING;
1112
1113 /* block until event callback signals exit */
1114 down(&the_lnet.ln_rc_signal);
1115 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_SHUTDOWN);
1116
1117 rc = LNetEQFree(the_lnet.ln_rc_eqh);
5fd88337 1118 LASSERT(!rc);
d7e09d03
PT
1119}
1120
1121static void
1122lnet_prune_rc_data(int wait_unlink)
1123{
7e7ab095
MS
1124 lnet_rc_data_t *rcd;
1125 lnet_rc_data_t *tmp;
1126 lnet_peer_t *lp;
1127 struct list_head head;
1128 int i = 2;
d7e09d03
PT
1129
1130 if (likely(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
1131 list_empty(&the_lnet.ln_rcd_deathrow) &&
1132 list_empty(&the_lnet.ln_rcd_zombie)))
1133 return;
1134
1135 INIT_LIST_HEAD(&head);
1136
1137 lnet_net_lock(LNET_LOCK_EX);
1138
1139 if (the_lnet.ln_rc_state != LNET_RC_STATE_RUNNING) {
1140 /* router checker is stopping, prune all */
1141 list_for_each_entry(lp, &the_lnet.ln_routers,
c314c319 1142 lp_rtr_list) {
06ace26e 1143 if (!lp->lp_rcd)
d7e09d03
PT
1144 continue;
1145
1146 LASSERT(list_empty(&lp->lp_rcd->rcd_list));
1147 list_add(&lp->lp_rcd->rcd_list,
c314c319 1148 &the_lnet.ln_rcd_deathrow);
d7e09d03
PT
1149 lp->lp_rcd = NULL;
1150 }
1151 }
1152
1153 /* unlink all RCDs on deathrow list */
1154 list_splice_init(&the_lnet.ln_rcd_deathrow, &head);
1155
1156 if (!list_empty(&head)) {
1157 lnet_net_unlock(LNET_LOCK_EX);
1158
1159 list_for_each_entry(rcd, &head, rcd_list)
1160 LNetMDUnlink(rcd->rcd_mdh);
1161
1162 lnet_net_lock(LNET_LOCK_EX);
1163 }
1164
1165 list_splice_init(&head, &the_lnet.ln_rcd_zombie);
1166
1167 /* release all zombie RCDs */
1168 while (!list_empty(&the_lnet.ln_rcd_zombie)) {
1169 list_for_each_entry_safe(rcd, tmp, &the_lnet.ln_rcd_zombie,
c314c319 1170 rcd_list) {
d7e09d03
PT
1171 if (LNetHandleIsInvalid(rcd->rcd_mdh))
1172 list_move(&rcd->rcd_list, &head);
1173 }
1174
1175 wait_unlink = wait_unlink &&
1176 !list_empty(&the_lnet.ln_rcd_zombie);
1177
1178 lnet_net_unlock(LNET_LOCK_EX);
1179
1180 while (!list_empty(&head)) {
1181 rcd = list_entry(head.next,
c314c319 1182 lnet_rc_data_t, rcd_list);
d7e09d03
PT
1183 list_del_init(&rcd->rcd_list);
1184 lnet_destroy_rc_data(rcd);
1185 }
1186
1187 if (!wait_unlink)
1188 return;
1189
1190 i++;
1191 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
1192 "Waiting for rc buffers to unlink\n");
d3caf4d5
PT
1193 set_current_state(TASK_UNINTERRUPTIBLE);
1194 schedule_timeout(cfs_time_seconds(1) / 4);
d7e09d03
PT
1195
1196 lnet_net_lock(LNET_LOCK_EX);
1197 }
1198
1199 lnet_net_unlock(LNET_LOCK_EX);
1200}
1201
d7e09d03
PT
1202static int
1203lnet_router_checker(void *arg)
1204{
7e7ab095
MS
1205 lnet_peer_t *rtr;
1206 struct list_head *entry;
d7e09d03
PT
1207
1208 cfs_block_allsigs();
1209
939af333 1210 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING);
d7e09d03
PT
1211
1212 while (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING) {
7e7ab095
MS
1213 __u64 version;
1214 int cpt;
1215 int cpt2;
d7e09d03
PT
1216
1217 cpt = lnet_net_lock_current();
1218rescan:
1219 version = the_lnet.ln_routers_version;
1220
1221 list_for_each(entry, &the_lnet.ln_routers) {
1222 rtr = list_entry(entry, lnet_peer_t, lp_rtr_list);
1223
1224 cpt2 = lnet_cpt_of_nid_locked(rtr->lp_nid);
1225 if (cpt != cpt2) {
1226 lnet_net_unlock(cpt);
1227 cpt = cpt2;
1228 lnet_net_lock(cpt);
1229 /* the routers list has changed */
1230 if (version != the_lnet.ln_routers_version)
1231 goto rescan;
1232 }
1233
1234 lnet_ping_router_locked(rtr);
1235
1236 /* NB dropped lock */
1237 if (version != the_lnet.ln_routers_version) {
1238 /* the routers list has changed */
1239 goto rescan;
1240 }
1241 }
1242
1243 if (the_lnet.ln_routing)
1244 lnet_update_ni_status_locked();
1245
1246 lnet_net_unlock(cpt);
1247
1248 lnet_prune_rc_data(0); /* don't wait for UNLINK */
1249
4420cfd3
JS
1250 /*
1251 * Call schedule_timeout() here always adds 1 to load average
d7e09d03 1252 * because kernel counts # active tasks as nr_running
4420cfd3
JS
1253 * + nr_uninterruptible.
1254 */
18fd5baa
PT
1255 set_current_state(TASK_INTERRUPTIBLE);
1256 schedule_timeout(cfs_time_seconds(1));
d7e09d03
PT
1257 }
1258
1259 LASSERT(the_lnet.ln_rc_state == LNET_RC_STATE_STOPPING);
1260
1261 lnet_prune_rc_data(1); /* wait for UNLINK */
1262
1263 the_lnet.ln_rc_state = LNET_RC_STATE_SHUTDOWN;
1264 up(&the_lnet.ln_rc_signal);
1265 /* The unlink event callback will signal final completion */
1266 return 0;
1267}
1268
86ef6250 1269void
d7e09d03
PT
1270lnet_destroy_rtrbuf(lnet_rtrbuf_t *rb, int npages)
1271{
1272 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1273
1274 while (--npages >= 0)
1275 __free_page(rb->rb_kiov[npages].kiov_page);
1276
1277 LIBCFS_FREE(rb, sz);
1278}
1279
2595fa36 1280static lnet_rtrbuf_t *
d7e09d03
PT
1281lnet_new_rtrbuf(lnet_rtrbufpool_t *rbp, int cpt)
1282{
7e7ab095
MS
1283 int npages = rbp->rbp_npages;
1284 int sz = offsetof(lnet_rtrbuf_t, rb_kiov[npages]);
1285 struct page *page;
d7e09d03 1286 lnet_rtrbuf_t *rb;
7e7ab095 1287 int i;
d7e09d03
PT
1288
1289 LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
06ace26e 1290 if (!rb)
d7e09d03
PT
1291 return NULL;
1292
1293 rb->rb_pool = rbp;
1294
1295 for (i = 0; i < npages; i++) {
49c02a75
PT
1296 page = alloc_pages_node(
1297 cfs_cpt_spread_node(lnet_cpt_table(), cpt),
40113370 1298 GFP_KERNEL | __GFP_ZERO, 0);
06ace26e 1299 if (!page) {
d7e09d03
PT
1300 while (--i >= 0)
1301 __free_page(rb->rb_kiov[i].kiov_page);
1302
1303 LIBCFS_FREE(rb, sz);
1304 return NULL;
1305 }
1306
1307 rb->rb_kiov[i].kiov_len = PAGE_CACHE_SIZE;
1308 rb->rb_kiov[i].kiov_offset = 0;
1309 rb->rb_kiov[i].kiov_page = page;
1310 }
1311
1312 return rb;
1313}
1314
2595fa36 1315static void
86ef6250 1316lnet_rtrpool_free_bufs(lnet_rtrbufpool_t *rbp, int cpt)
d7e09d03 1317{
7e7ab095 1318 int npages = rbp->rbp_npages;
86ef6250 1319 struct list_head tmp;
7e7ab095 1320 lnet_rtrbuf_t *rb;
d7e09d03 1321
5fd88337 1322 if (!rbp->rbp_nbuffers) /* not initialized or already freed */
d7e09d03
PT
1323 return;
1324
86ef6250 1325 INIT_LIST_HEAD(&tmp);
d7e09d03 1326
86ef6250
AS
1327 lnet_net_lock(cpt);
1328 lnet_drop_routed_msgs_locked(&rbp->rbp_msgs, cpt);
1329 list_splice_init(&rbp->rbp_bufs, &tmp);
1330 rbp->rbp_nbuffers = 0;
1331 rbp->rbp_credits = 0;
1332 rbp->rbp_mincredits = 0;
1333 lnet_net_unlock(cpt);
d7e09d03 1334
86ef6250
AS
1335 /* Free buffers on the free list. */
1336 while (!list_empty(&tmp)) {
1337 rb = list_entry(tmp.next, lnet_rtrbuf_t, rb_list);
d7e09d03
PT
1338 list_del(&rb->rb_list);
1339 lnet_destroy_rtrbuf(rb, npages);
d7e09d03 1340 }
d7e09d03
PT
1341}
1342
2595fa36 1343static int
86ef6250 1344lnet_rtrpool_adjust_bufs(lnet_rtrbufpool_t *rbp, int nbufs, int cpt)
d7e09d03 1345{
86ef6250 1346 struct list_head rb_list;
d7e09d03 1347 lnet_rtrbuf_t *rb;
86ef6250
AS
1348 int num_rb;
1349 int num_buffers = 0;
1350 int npages = rbp->rbp_npages;
d7e09d03 1351
86ef6250
AS
1352 /*
1353 * If we are called for less buffers than already in the pool, we
1354 * just lower the nbuffers number and excess buffers will be
1355 * thrown away as they are returned to the free list. Credits
1356 * then get adjusted as well.
1357 */
1358 if (nbufs <= rbp->rbp_nbuffers) {
1359 lnet_net_lock(cpt);
1360 rbp->rbp_nbuffers = nbufs;
1361 lnet_net_unlock(cpt);
d7e09d03
PT
1362 return 0;
1363 }
1364
86ef6250 1365 INIT_LIST_HEAD(&rb_list);
d7e09d03 1366
86ef6250
AS
1367 /*
1368 * allocate the buffers on a local list first. If all buffers are
1369 * allocated successfully then join this list to the rbp buffer
1370 * list. If not then free all allocated buffers.
1371 */
1372 num_rb = rbp->rbp_nbuffers;
1373
1374 while (num_rb < nbufs) {
1375 rb = lnet_new_rtrbuf(rbp, cpt);
06ace26e 1376 if (!rb) {
86ef6250
AS
1377 CERROR("Failed to allocate %d route bufs of %d pages\n",
1378 nbufs, npages);
1379 goto failed;
d7e09d03
PT
1380 }
1381
86ef6250
AS
1382 list_add(&rb->rb_list, &rb_list);
1383 num_buffers++;
1384 num_rb++;
d7e09d03
PT
1385 }
1386
86ef6250
AS
1387 lnet_net_lock(cpt);
1388
1389 list_splice_tail(&rb_list, &rbp->rbp_bufs);
1390 rbp->rbp_nbuffers += num_buffers;
1391 rbp->rbp_credits += num_buffers;
1392 rbp->rbp_mincredits = rbp->rbp_credits;
1393 /*
1394 * We need to schedule blocked msg using the newly
1395 * added buffers.
1396 */
1397 while (!list_empty(&rbp->rbp_bufs) &&
1398 !list_empty(&rbp->rbp_msgs))
1399 lnet_schedule_blocked_locked(rbp);
1400
1401 lnet_net_unlock(cpt);
1402
d7e09d03 1403 return 0;
86ef6250
AS
1404
1405failed:
1406 while (!list_empty(&rb_list)) {
1407 rb = list_entry(rb_list.next, lnet_rtrbuf_t, rb_list);
1408 list_del(&rb->rb_list);
1409 lnet_destroy_rtrbuf(rb, npages);
1410 }
1411
1412 return -ENOMEM;
d7e09d03
PT
1413}
1414
2595fa36 1415static void
d7e09d03
PT
1416lnet_rtrpool_init(lnet_rtrbufpool_t *rbp, int npages)
1417{
1418 INIT_LIST_HEAD(&rbp->rbp_msgs);
1419 INIT_LIST_HEAD(&rbp->rbp_bufs);
1420
1421 rbp->rbp_npages = npages;
1422 rbp->rbp_credits = 0;
1423 rbp->rbp_mincredits = 0;
1424}
1425
1426void
86ef6250 1427lnet_rtrpools_free(int keep_pools)
d7e09d03
PT
1428{
1429 lnet_rtrbufpool_t *rtrp;
7e7ab095 1430 int i;
d7e09d03 1431
06ace26e 1432 if (!the_lnet.ln_rtrpools) /* uninitialized or freed */
d7e09d03
PT
1433 return;
1434
1435 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
86ef6250
AS
1436 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1437 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1438 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
d7e09d03
PT
1439 }
1440
86ef6250
AS
1441 if (!keep_pools) {
1442 cfs_percpt_free(the_lnet.ln_rtrpools);
1443 the_lnet.ln_rtrpools = NULL;
1444 }
d7e09d03
PT
1445}
1446
1447static int
86ef6250 1448lnet_nrb_tiny_calculate(void)
d7e09d03 1449{
7e7ab095 1450 int nrbs = LNET_NRB_TINY;
d7e09d03
PT
1451
1452 if (tiny_router_buffers < 0) {
1453 LCONSOLE_ERROR_MSG(0x10c,
2d00bd17
JP
1454 "tiny_router_buffers=%d invalid when routing enabled\n",
1455 tiny_router_buffers);
d7e09d03
PT
1456 return -1;
1457 }
1458
1459 if (tiny_router_buffers > 0)
1460 nrbs = tiny_router_buffers;
1461
1462 nrbs /= LNET_CPT_NUMBER;
1463 return max(nrbs, LNET_NRB_TINY_MIN);
1464}
1465
1466static int
86ef6250 1467lnet_nrb_small_calculate(void)
d7e09d03 1468{
7e7ab095 1469 int nrbs = LNET_NRB_SMALL;
d7e09d03
PT
1470
1471 if (small_router_buffers < 0) {
1472 LCONSOLE_ERROR_MSG(0x10c,
2d00bd17
JP
1473 "small_router_buffers=%d invalid when routing enabled\n",
1474 small_router_buffers);
d7e09d03
PT
1475 return -1;
1476 }
1477
1478 if (small_router_buffers > 0)
1479 nrbs = small_router_buffers;
1480
1481 nrbs /= LNET_CPT_NUMBER;
1482 return max(nrbs, LNET_NRB_SMALL_MIN);
1483}
1484
1485static int
86ef6250 1486lnet_nrb_large_calculate(void)
d7e09d03 1487{
7e7ab095 1488 int nrbs = LNET_NRB_LARGE;
d7e09d03
PT
1489
1490 if (large_router_buffers < 0) {
1491 LCONSOLE_ERROR_MSG(0x10c,
2d00bd17
JP
1492 "large_router_buffers=%d invalid when routing enabled\n",
1493 large_router_buffers);
d7e09d03
PT
1494 return -1;
1495 }
1496
1497 if (large_router_buffers > 0)
1498 nrbs = large_router_buffers;
1499
1500 nrbs /= LNET_CPT_NUMBER;
1501 return max(nrbs, LNET_NRB_LARGE_MIN);
1502}
1503
1504int
1505lnet_rtrpools_alloc(int im_a_router)
1506{
1507 lnet_rtrbufpool_t *rtrp;
7e7ab095
MS
1508 int nrb_tiny;
1509 int nrb_small;
1510 int nrb_large;
1511 int rc;
1512 int i;
d7e09d03
PT
1513
1514 if (!strcmp(forwarding, "")) {
1515 /* not set either way */
1516 if (!im_a_router)
1517 return 0;
1518 } else if (!strcmp(forwarding, "disabled")) {
1519 /* explicitly disabled */
1520 return 0;
1521 } else if (!strcmp(forwarding, "enabled")) {
1522 /* explicitly enabled */
1523 } else {
2d00bd17 1524 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either 'enabled' or 'disabled'\n");
d7e09d03
PT
1525 return -EINVAL;
1526 }
1527
86ef6250 1528 nrb_tiny = lnet_nrb_tiny_calculate();
d7e09d03
PT
1529 if (nrb_tiny < 0)
1530 return -EINVAL;
1531
86ef6250 1532 nrb_small = lnet_nrb_small_calculate();
d7e09d03
PT
1533 if (nrb_small < 0)
1534 return -EINVAL;
1535
86ef6250 1536 nrb_large = lnet_nrb_large_calculate();
d7e09d03
PT
1537 if (nrb_large < 0)
1538 return -EINVAL;
1539
1540 the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1541 LNET_NRBPOOLS *
1542 sizeof(lnet_rtrbufpool_t));
06ace26e 1543 if (!the_lnet.ln_rtrpools) {
d7e09d03
PT
1544 LCONSOLE_ERROR_MSG(0x10c,
1545 "Failed to initialize router buffe pool\n");
1546 return -ENOMEM;
1547 }
1548
1549 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
86ef6250
AS
1550 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1551 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1552 nrb_tiny, i);
5fd88337 1553 if (rc)
d7e09d03
PT
1554 goto failed;
1555
86ef6250
AS
1556 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1557 LNET_NRB_SMALL_PAGES);
1558 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1559 nrb_small, i);
5fd88337 1560 if (rc)
d7e09d03
PT
1561 goto failed;
1562
86ef6250
AS
1563 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1564 LNET_NRB_LARGE_PAGES);
1565 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1566 nrb_large, i);
5fd88337 1567 if (rc)
d7e09d03
PT
1568 goto failed;
1569 }
1570
1571 lnet_net_lock(LNET_LOCK_EX);
1572 the_lnet.ln_routing = 1;
1573 lnet_net_unlock(LNET_LOCK_EX);
1574
1575 return 0;
1576
1577 failed:
86ef6250 1578 lnet_rtrpools_free(0);
d7e09d03
PT
1579 return rc;
1580}
1581
edeb5d8c
AS
1582static int
1583lnet_rtrpools_adjust_helper(int tiny, int small, int large)
86ef6250
AS
1584{
1585 int nrb = 0;
1586 int rc = 0;
1587 int i;
1588 lnet_rtrbufpool_t *rtrp;
1589
86ef6250
AS
1590 /*
1591 * If the provided values for each buffer pool are different than the
1592 * configured values, we need to take action.
1593 */
edeb5d8c 1594 if (tiny >= 0) {
86ef6250
AS
1595 tiny_router_buffers = tiny;
1596 nrb = lnet_nrb_tiny_calculate();
1597 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1598 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1599 nrb, i);
1600 if (rc)
1601 return rc;
1602 }
1603 }
edeb5d8c 1604 if (small >= 0) {
86ef6250
AS
1605 small_router_buffers = small;
1606 nrb = lnet_nrb_small_calculate();
1607 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1608 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1609 nrb, i);
1610 if (rc)
1611 return rc;
1612 }
1613 }
edeb5d8c 1614 if (large >= 0) {
86ef6250
AS
1615 large_router_buffers = large;
1616 nrb = lnet_nrb_large_calculate();
1617 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1618 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1619 nrb, i);
1620 if (rc)
1621 return rc;
1622 }
1623 }
1624
1625 return 0;
1626}
1627
edeb5d8c
AS
1628int
1629lnet_rtrpools_adjust(int tiny, int small, int large)
1630{
1631 /*
1632 * this function doesn't revert the changes if adding new buffers
1633 * failed. It's up to the user space caller to revert the
1634 * changes.
1635 */
1636 if (!the_lnet.ln_routing)
1637 return 0;
1638
1639 return lnet_rtrpools_adjust_helper(tiny, small, large);
1640}
1641
86ef6250
AS
1642int
1643lnet_rtrpools_enable(void)
1644{
1645 int rc;
1646
1647 if (the_lnet.ln_routing)
1648 return 0;
1649
1650 if (!the_lnet.ln_rtrpools)
1651 /*
1652 * If routing is turned off, and we have never
1653 * initialized the pools before, just call the
1654 * standard buffer pool allocation routine as
1655 * if we are just configuring this for the first
1656 * time.
1657 */
1658 return lnet_rtrpools_alloc(1);
1659
edeb5d8c 1660 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
86ef6250
AS
1661 if (rc)
1662 return rc;
1663
1664 lnet_net_lock(LNET_LOCK_EX);
1665 the_lnet.ln_routing = 1;
1666
1667 the_lnet.ln_ping_info->pi_features &= ~LNET_PING_FEAT_RTE_DISABLED;
1668 lnet_net_unlock(LNET_LOCK_EX);
1669
1670 return 0;
1671}
1672
1673void
1674lnet_rtrpools_disable(void)
1675{
1676 if (!the_lnet.ln_routing)
1677 return;
1678
1679 lnet_net_lock(LNET_LOCK_EX);
1680 the_lnet.ln_routing = 0;
1681 the_lnet.ln_ping_info->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1682
1683 tiny_router_buffers = 0;
1684 small_router_buffers = 0;
1685 large_router_buffers = 0;
1686 lnet_net_unlock(LNET_LOCK_EX);
1687 lnet_rtrpools_free(1);
1688}
1689
d7e09d03 1690int
a649ad1d 1691lnet_notify(lnet_ni_t *ni, lnet_nid_t nid, int alive, unsigned long when)
d7e09d03 1692{
7e7ab095
MS
1693 struct lnet_peer *lp = NULL;
1694 unsigned long now = cfs_time_current();
1695 int cpt = lnet_cpt_of_nid(nid);
d7e09d03 1696
59cfb96f 1697 LASSERT(!in_interrupt());
d7e09d03 1698
939af333 1699 CDEBUG(D_NET, "%s notifying %s: %s\n",
06ace26e 1700 !ni ? "userspace" : libcfs_nid2str(ni->ni_nid),
c314c319
JS
1701 libcfs_nid2str(nid),
1702 alive ? "up" : "down");
d7e09d03 1703
06ace26e 1704 if (ni &&
d7e09d03 1705 LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
939af333 1706 CWARN("Ignoring notification of %s %s by %s (different net)\n",
c314c319
JS
1707 libcfs_nid2str(nid), alive ? "birth" : "death",
1708 libcfs_nid2str(ni->ni_nid));
d7e09d03
PT
1709 return -EINVAL;
1710 }
1711
1712 /* can't do predictions... */
1713 if (cfs_time_after(when, now)) {
2d00bd17 1714 CWARN("Ignoring prediction from %s of %s %s %ld seconds in the future\n",
06ace26e 1715 !ni ? "userspace" : libcfs_nid2str(ni->ni_nid),
2d00bd17
JP
1716 libcfs_nid2str(nid), alive ? "up" : "down",
1717 cfs_duration_sec(cfs_time_sub(when, now)));
d7e09d03
PT
1718 return -EINVAL;
1719 }
1720
06ace26e 1721 if (ni && !alive && /* LND telling me she's down */
d7e09d03
PT
1722 !auto_down) { /* auto-down disabled */
1723 CDEBUG(D_NET, "Auto-down disabled\n");
1724 return 0;
1725 }
1726
1727 lnet_net_lock(cpt);
1728
1729 if (the_lnet.ln_shutdown) {
1730 lnet_net_unlock(cpt);
1731 return -ESHUTDOWN;
1732 }
1733
1734 lp = lnet_find_peer_locked(the_lnet.ln_peer_tables[cpt], nid);
06ace26e 1735 if (!lp) {
d7e09d03
PT
1736 /* nid not found */
1737 lnet_net_unlock(cpt);
1738 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1739 return 0;
1740 }
1741
4420cfd3
JS
1742 /*
1743 * We can't fully trust LND on reporting exact peer last_alive
d7e09d03
PT
1744 * if he notifies us about dead peer. For example ksocklnd can
1745 * call us with when == _time_when_the_node_was_booted_ if
4420cfd3
JS
1746 * no connections were successfully established
1747 */
06ace26e 1748 if (ni && !alive && when < lp->lp_last_alive)
d7e09d03
PT
1749 when = lp->lp_last_alive;
1750
06ace26e 1751 lnet_notify_locked(lp, !ni, alive, when);
d7e09d03
PT
1752
1753 lnet_ni_notify_locked(ni, lp);
1754
1755 lnet_peer_decref_locked(lp);
1756
1757 lnet_net_unlock(cpt);
1758 return 0;
1759}
1760EXPORT_SYMBOL(lnet_notify);
This page took 0.712129 seconds and 5 git commands to generate.