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