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