1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2012, 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  *   You should have received a copy of the GNU General Public License
19  *   along with Portals; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23 
24 #define DEBUG_SUBSYSTEM S_LNET
25 #include "../../include/linux/libcfs/libcfs.h"
26 #include "../../include/linux/lnet/lib-lnet.h"
27 
28 #if  defined(LNET_ROUTER)
29 
30 /* This is really lnet_proc.c. You might need to update sanity test 215
31  * if any file format is changed. */
32 
33 static struct ctl_table_header *lnet_table_header;
34 
35 #define CTL_LNET	 (0x100)
36 enum {
37 	PSDEV_LNET_STATS = 100,
38 	PSDEV_LNET_ROUTES,
39 	PSDEV_LNET_ROUTERS,
40 	PSDEV_LNET_PEERS,
41 	PSDEV_LNET_BUFFERS,
42 	PSDEV_LNET_NIS,
43 	PSDEV_LNET_PTL_ROTOR,
44 };
45 
46 #define LNET_LOFFT_BITS		(sizeof(loff_t) * 8)
47 /*
48  * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
49  */
50 #define LNET_PROC_CPT_BITS	(LNET_CPT_BITS + 1)
51 /* change version, 16 bits or 8 bits */
52 #define LNET_PROC_VER_BITS	max_t(size_t, min_t(size_t, LNET_LOFFT_BITS, 64) / 4, 8)
53 
54 #define LNET_PROC_HASH_BITS	LNET_PEER_HASH_BITS
55 /*
56  * bits for peer hash offset
57  * NB: we don't use the highest bit of *ppos because it's signed
58  */
59 #define LNET_PROC_HOFF_BITS	(LNET_LOFFT_BITS -       \
60 				 LNET_PROC_CPT_BITS -    \
61 				 LNET_PROC_VER_BITS -    \
62 				 LNET_PROC_HASH_BITS - 1)
63 /* bits for hash index + position */
64 #define LNET_PROC_HPOS_BITS	(LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
65 /* bits for peer hash table + hash version */
66 #define LNET_PROC_VPOS_BITS	(LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
67 
68 #define LNET_PROC_CPT_MASK	((1ULL << LNET_PROC_CPT_BITS) - 1)
69 #define LNET_PROC_VER_MASK	((1ULL << LNET_PROC_VER_BITS) - 1)
70 #define LNET_PROC_HASH_MASK	((1ULL << LNET_PROC_HASH_BITS) - 1)
71 #define LNET_PROC_HOFF_MASK	((1ULL << LNET_PROC_HOFF_BITS) - 1)
72 
73 #define LNET_PROC_CPT_GET(pos)				\
74 	(int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
75 
76 #define LNET_PROC_VER_GET(pos)				\
77 	(int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
78 
79 #define LNET_PROC_HASH_GET(pos)				\
80 	(int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
81 
82 #define LNET_PROC_HOFF_GET(pos)				\
83 	(int)((pos) & LNET_PROC_HOFF_MASK)
84 
85 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off)		\
86 	(((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) |   \
87 	((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) |   \
88 	((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
89 	((off) & LNET_PROC_HOFF_MASK))
90 
91 #define LNET_PROC_VERSION(v)	((unsigned int)((v) & LNET_PROC_VER_MASK))
92 
proc_call_handler(void * data,int write,loff_t * ppos,void __user * buffer,size_t * lenp,int (* handler)(void * data,int write,loff_t pos,void __user * buffer,int len))93 static int proc_call_handler(void *data, int write, loff_t *ppos,
94 		void __user *buffer, size_t *lenp,
95 		int (*handler)(void *data, int write,
96 		loff_t pos, void __user *buffer, int len))
97 {
98 	int rc = handler(data, write, *ppos, buffer, *lenp);
99 
100 	if (rc < 0)
101 		return rc;
102 
103 	if (write) {
104 		*ppos += *lenp;
105 	} else {
106 		*lenp = rc;
107 		*ppos += rc;
108 	}
109 	return 0;
110 }
111 
__proc_lnet_stats(void * data,int write,loff_t pos,void __user * buffer,int nob)112 static int __proc_lnet_stats(void *data, int write,
113 			     loff_t pos, void __user *buffer, int nob)
114 {
115 	int	      rc;
116 	lnet_counters_t *ctrs;
117 	int	      len;
118 	char	    *tmpstr;
119 	const int	tmpsiz = 256; /* 7 %u and 4 %llu */
120 
121 	if (write) {
122 		lnet_counters_reset();
123 		return 0;
124 	}
125 
126 	/* read */
127 
128 	LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
129 	if (ctrs == NULL)
130 		return -ENOMEM;
131 
132 	LIBCFS_ALLOC(tmpstr, tmpsiz);
133 	if (tmpstr == NULL) {
134 		LIBCFS_FREE(ctrs, sizeof(*ctrs));
135 		return -ENOMEM;
136 	}
137 
138 	lnet_counters_get(ctrs);
139 
140 	len = snprintf(tmpstr, tmpsiz,
141 		       "%u %u %u %u %u %u %u %llu %llu %llu %llu",
142 		       ctrs->msgs_alloc, ctrs->msgs_max,
143 		       ctrs->errors,
144 		       ctrs->send_count, ctrs->recv_count,
145 		       ctrs->route_count, ctrs->drop_count,
146 		       ctrs->send_length, ctrs->recv_length,
147 		       ctrs->route_length, ctrs->drop_length);
148 
149 	if (pos >= min_t(int, len, strlen(tmpstr)))
150 		rc = 0;
151 	else
152 		rc = cfs_trace_copyout_string(buffer, nob,
153 					      tmpstr + pos, "\n");
154 
155 	LIBCFS_FREE(tmpstr, tmpsiz);
156 	LIBCFS_FREE(ctrs, sizeof(*ctrs));
157 	return rc;
158 }
159 
proc_lnet_stats(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)160 static int proc_lnet_stats(struct ctl_table *table, int write,
161 			   void __user *buffer, size_t *lenp, loff_t *ppos)
162 {
163 	return proc_call_handler(table->data, write, ppos, buffer, lenp,
164 				 __proc_lnet_stats);
165 }
166 
proc_lnet_routes(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)167 static int proc_lnet_routes(struct ctl_table *table, int write,
168 			    void __user *buffer, size_t *lenp, loff_t *ppos)
169 {
170 	const int	tmpsiz = 256;
171 	char		*tmpstr;
172 	char		*s;
173 	int		rc = 0;
174 	int		len;
175 	int		ver;
176 	int		off;
177 
178 	CLASSERT(sizeof(loff_t) >= 4);
179 
180 	off = LNET_PROC_HOFF_GET(*ppos);
181 	ver = LNET_PROC_VER_GET(*ppos);
182 
183 	LASSERT(!write);
184 
185 	if (*lenp == 0)
186 		return 0;
187 
188 	LIBCFS_ALLOC(tmpstr, tmpsiz);
189 	if (tmpstr == NULL)
190 		return -ENOMEM;
191 
192 	s = tmpstr; /* points to current position in tmpstr[] */
193 
194 	if (*ppos == 0) {
195 		s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
196 			      the_lnet.ln_routing ? "enabled" : "disabled");
197 		LASSERT(tmpstr + tmpsiz - s > 0);
198 
199 		s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
200 			      "net", "hops", "priority", "state", "router");
201 		LASSERT(tmpstr + tmpsiz - s > 0);
202 
203 		lnet_net_lock(0);
204 		ver = (unsigned int)the_lnet.ln_remote_nets_version;
205 		lnet_net_unlock(0);
206 		*ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
207 	} else {
208 		struct list_head		*n;
209 		struct list_head		*r;
210 		lnet_route_t		*route = NULL;
211 		lnet_remotenet_t	*rnet  = NULL;
212 		int			skip  = off - 1;
213 		struct list_head		*rn_list;
214 		int			i;
215 
216 		lnet_net_lock(0);
217 
218 		if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
219 			lnet_net_unlock(0);
220 			LIBCFS_FREE(tmpstr, tmpsiz);
221 			return -ESTALE;
222 		}
223 
224 		for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
225 		     i++) {
226 			rn_list = &the_lnet.ln_remote_nets_hash[i];
227 
228 			n = rn_list->next;
229 
230 			while (n != rn_list && route == NULL) {
231 				rnet = list_entry(n, lnet_remotenet_t,
232 						      lrn_list);
233 
234 				r = rnet->lrn_routes.next;
235 
236 				while (r != &rnet->lrn_routes) {
237 					lnet_route_t *re =
238 						list_entry(r, lnet_route_t,
239 							       lr_list);
240 					if (skip == 0) {
241 						route = re;
242 						break;
243 					}
244 
245 					skip--;
246 					r = r->next;
247 				}
248 
249 				n = n->next;
250 			}
251 		}
252 
253 		if (route != NULL) {
254 			__u32        net	= rnet->lrn_net;
255 			unsigned int hops	= route->lr_hops;
256 			unsigned int priority	= route->lr_priority;
257 			lnet_nid_t   nid	= route->lr_gateway->lp_nid;
258 			int          alive	= route->lr_gateway->lp_alive;
259 
260 			s += snprintf(s, tmpstr + tmpsiz - s,
261 				      "%-8s %4u %8u %7s %s\n",
262 				      libcfs_net2str(net), hops,
263 				      priority,
264 				      alive ? "up" : "down",
265 				      libcfs_nid2str(nid));
266 			LASSERT(tmpstr + tmpsiz - s > 0);
267 		}
268 
269 		lnet_net_unlock(0);
270 	}
271 
272 	len = s - tmpstr;     /* how many bytes was written */
273 
274 	if (len > *lenp) {    /* linux-supplied buffer is too small */
275 		rc = -EINVAL;
276 	} else if (len > 0) { /* wrote something */
277 		if (copy_to_user(buffer, tmpstr, len))
278 			rc = -EFAULT;
279 		else {
280 			off += 1;
281 			*ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
282 		}
283 	}
284 
285 	LIBCFS_FREE(tmpstr, tmpsiz);
286 
287 	if (rc == 0)
288 		*lenp = len;
289 
290 	return rc;
291 }
292 
proc_lnet_routers(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)293 static int proc_lnet_routers(struct ctl_table *table, int write,
294 			     void __user *buffer, size_t *lenp, loff_t *ppos)
295 {
296 	int	rc = 0;
297 	char      *tmpstr;
298 	char      *s;
299 	const int  tmpsiz = 256;
300 	int	len;
301 	int	ver;
302 	int	off;
303 
304 	off = LNET_PROC_HOFF_GET(*ppos);
305 	ver = LNET_PROC_VER_GET(*ppos);
306 
307 	LASSERT(!write);
308 
309 	if (*lenp == 0)
310 		return 0;
311 
312 	LIBCFS_ALLOC(tmpstr, tmpsiz);
313 	if (tmpstr == NULL)
314 		return -ENOMEM;
315 
316 	s = tmpstr; /* points to current position in tmpstr[] */
317 
318 	if (*ppos == 0) {
319 		s += snprintf(s, tmpstr + tmpsiz - s,
320 			      "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
321 			      "ref", "rtr_ref", "alive_cnt", "state",
322 			      "last_ping", "ping_sent", "deadline",
323 			      "down_ni", "router");
324 		LASSERT(tmpstr + tmpsiz - s > 0);
325 
326 		lnet_net_lock(0);
327 		ver = (unsigned int)the_lnet.ln_routers_version;
328 		lnet_net_unlock(0);
329 		*ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
330 	} else {
331 		struct list_head		*r;
332 		struct lnet_peer	*peer = NULL;
333 		int			skip = off - 1;
334 
335 		lnet_net_lock(0);
336 
337 		if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
338 			lnet_net_unlock(0);
339 
340 			LIBCFS_FREE(tmpstr, tmpsiz);
341 			return -ESTALE;
342 		}
343 
344 		r = the_lnet.ln_routers.next;
345 
346 		while (r != &the_lnet.ln_routers) {
347 			lnet_peer_t *lp = list_entry(r, lnet_peer_t,
348 							 lp_rtr_list);
349 
350 			if (skip == 0) {
351 				peer = lp;
352 				break;
353 			}
354 
355 			skip--;
356 			r = r->next;
357 		}
358 
359 		if (peer != NULL) {
360 			lnet_nid_t nid = peer->lp_nid;
361 			unsigned long now = cfs_time_current();
362 			unsigned long deadline = peer->lp_ping_deadline;
363 			int nrefs     = peer->lp_refcount;
364 			int nrtrrefs  = peer->lp_rtr_refcount;
365 			int alive_cnt = peer->lp_alive_count;
366 			int alive     = peer->lp_alive;
367 			int pingsent  = !peer->lp_ping_notsent;
368 			int last_ping = cfs_duration_sec(cfs_time_sub(now,
369 						     peer->lp_ping_timestamp));
370 			int down_ni   = 0;
371 			lnet_route_t *rtr;
372 
373 			if ((peer->lp_ping_feats &
374 			     LNET_PING_FEAT_NI_STATUS) != 0) {
375 				list_for_each_entry(rtr, &peer->lp_routes,
376 							lr_gwlist) {
377 					/* downis on any route should be the
378 					 * number of downis on the gateway */
379 					if (rtr->lr_downis != 0) {
380 						down_ni = rtr->lr_downis;
381 						break;
382 					}
383 				}
384 			}
385 
386 			if (deadline == 0)
387 				s += snprintf(s, tmpstr + tmpsiz - s,
388 					      "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
389 					      nrefs, nrtrrefs, alive_cnt,
390 					      alive ? "up" : "down", last_ping,
391 					      pingsent, "NA", down_ni,
392 					      libcfs_nid2str(nid));
393 			else
394 				s += snprintf(s, tmpstr + tmpsiz - s,
395 					      "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
396 					      nrefs, nrtrrefs, alive_cnt,
397 					      alive ? "up" : "down", last_ping,
398 					      pingsent,
399 					      cfs_duration_sec(cfs_time_sub(deadline, now)),
400 					      down_ni, libcfs_nid2str(nid));
401 			LASSERT(tmpstr + tmpsiz - s > 0);
402 		}
403 
404 		lnet_net_unlock(0);
405 	}
406 
407 	len = s - tmpstr;     /* how many bytes was written */
408 
409 	if (len > *lenp) {    /* linux-supplied buffer is too small */
410 		rc = -EINVAL;
411 	} else if (len > 0) { /* wrote something */
412 		if (copy_to_user(buffer, tmpstr, len))
413 			rc = -EFAULT;
414 		else {
415 			off += 1;
416 			*ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
417 		}
418 	}
419 
420 	LIBCFS_FREE(tmpstr, tmpsiz);
421 
422 	if (rc == 0)
423 		*lenp = len;
424 
425 	return rc;
426 }
427 
proc_lnet_peers(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)428 static int proc_lnet_peers(struct ctl_table *table, int write,
429 			   void __user *buffer, size_t *lenp, loff_t *ppos)
430 {
431 	const int		tmpsiz  = 256;
432 	struct lnet_peer_table	*ptable;
433 	char			*tmpstr;
434 	char			*s;
435 	int			cpt  = LNET_PROC_CPT_GET(*ppos);
436 	int			ver  = LNET_PROC_VER_GET(*ppos);
437 	int			hash = LNET_PROC_HASH_GET(*ppos);
438 	int			hoff = LNET_PROC_HOFF_GET(*ppos);
439 	int			rc = 0;
440 	int			len;
441 
442 	CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
443 	LASSERT(!write);
444 
445 	if (*lenp == 0)
446 		return 0;
447 
448 	if (cpt >= LNET_CPT_NUMBER) {
449 		*lenp = 0;
450 		return 0;
451 	}
452 
453 	LIBCFS_ALLOC(tmpstr, tmpsiz);
454 	if (tmpstr == NULL)
455 		return -ENOMEM;
456 
457 	s = tmpstr; /* points to current position in tmpstr[] */
458 
459 	if (*ppos == 0) {
460 		s += snprintf(s, tmpstr + tmpsiz - s,
461 			      "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
462 			      "nid", "refs", "state", "last", "max",
463 			      "rtr", "min", "tx", "min", "queue");
464 		LASSERT(tmpstr + tmpsiz - s > 0);
465 
466 		hoff++;
467 	} else {
468 		struct lnet_peer	*peer;
469 		struct list_head		*p;
470 		int			skip;
471  again:
472 		p = NULL;
473 		peer = NULL;
474 		skip = hoff - 1;
475 
476 		lnet_net_lock(cpt);
477 		ptable = the_lnet.ln_peer_tables[cpt];
478 		if (hoff == 1)
479 			ver = LNET_PROC_VERSION(ptable->pt_version);
480 
481 		if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
482 			lnet_net_unlock(cpt);
483 			LIBCFS_FREE(tmpstr, tmpsiz);
484 			return -ESTALE;
485 		}
486 
487 		while (hash < LNET_PEER_HASH_SIZE) {
488 			if (p == NULL)
489 				p = ptable->pt_hash[hash].next;
490 
491 			while (p != &ptable->pt_hash[hash]) {
492 				lnet_peer_t *lp = list_entry(p, lnet_peer_t,
493 								 lp_hashlist);
494 				if (skip == 0) {
495 					peer = lp;
496 
497 					/* minor optimization: start from idx+1
498 					 * on next iteration if we've just
499 					 * drained lp_hashlist */
500 					if (lp->lp_hashlist.next ==
501 					    &ptable->pt_hash[hash]) {
502 						hoff = 1;
503 						hash++;
504 					} else {
505 						hoff++;
506 					}
507 
508 					break;
509 				}
510 
511 				skip--;
512 				p = lp->lp_hashlist.next;
513 			}
514 
515 			if (peer != NULL)
516 				break;
517 
518 			p = NULL;
519 			hoff = 1;
520 			hash++;
521 		}
522 
523 		if (peer != NULL) {
524 			lnet_nid_t nid       = peer->lp_nid;
525 			int	nrefs     = peer->lp_refcount;
526 			int	lastalive = -1;
527 			char      *aliveness = "NA";
528 			int	maxcr     = peer->lp_ni->ni_peertxcredits;
529 			int	txcr      = peer->lp_txcredits;
530 			int	mintxcr   = peer->lp_mintxcredits;
531 			int	rtrcr     = peer->lp_rtrcredits;
532 			int	minrtrcr  = peer->lp_minrtrcredits;
533 			int	txqnob    = peer->lp_txqnob;
534 
535 			if (lnet_isrouter(peer) ||
536 			    lnet_peer_aliveness_enabled(peer))
537 				aliveness = peer->lp_alive ? "up" : "down";
538 
539 			if (lnet_peer_aliveness_enabled(peer)) {
540 				unsigned long     now = cfs_time_current();
541 				long delta;
542 
543 				delta = cfs_time_sub(now, peer->lp_last_alive);
544 				lastalive = cfs_duration_sec(delta);
545 
546 				/* No need to mess up peers contents with
547 				 * arbitrarily long integers - it suffices to
548 				 * know that lastalive is more than 10000s old
549 				 */
550 				if (lastalive >= 10000)
551 					lastalive = 9999;
552 			}
553 
554 			lnet_net_unlock(cpt);
555 
556 			s += snprintf(s, tmpstr + tmpsiz - s,
557 				      "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
558 				      libcfs_nid2str(nid), nrefs, aliveness,
559 				      lastalive, maxcr, rtrcr, minrtrcr, txcr,
560 				      mintxcr, txqnob);
561 			LASSERT(tmpstr + tmpsiz - s > 0);
562 
563 		} else { /* peer is NULL */
564 			lnet_net_unlock(cpt);
565 		}
566 
567 		if (hash == LNET_PEER_HASH_SIZE) {
568 			cpt++;
569 			hash = 0;
570 			hoff = 1;
571 			if (peer == NULL && cpt < LNET_CPT_NUMBER)
572 				goto again;
573 		}
574 	}
575 
576 	len = s - tmpstr;     /* how many bytes was written */
577 
578 	if (len > *lenp) {    /* linux-supplied buffer is too small */
579 		rc = -EINVAL;
580 	} else if (len > 0) { /* wrote something */
581 		if (copy_to_user(buffer, tmpstr, len))
582 			rc = -EFAULT;
583 		else
584 			*ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
585 	}
586 
587 	LIBCFS_FREE(tmpstr, tmpsiz);
588 
589 	if (rc == 0)
590 		*lenp = len;
591 
592 	return rc;
593 }
594 
__proc_lnet_buffers(void * data,int write,loff_t pos,void __user * buffer,int nob)595 static int __proc_lnet_buffers(void *data, int write,
596 			       loff_t pos, void __user *buffer, int nob)
597 {
598 	char	    *s;
599 	char	    *tmpstr;
600 	int		tmpsiz;
601 	int		idx;
602 	int		len;
603 	int		rc;
604 	int		i;
605 
606 	LASSERT(!write);
607 
608 	/* (4 %d) * 4 * LNET_CPT_NUMBER */
609 	tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
610 	LIBCFS_ALLOC(tmpstr, tmpsiz);
611 	if (tmpstr == NULL)
612 		return -ENOMEM;
613 
614 	s = tmpstr; /* points to current position in tmpstr[] */
615 
616 	s += snprintf(s, tmpstr + tmpsiz - s,
617 		      "%5s %5s %7s %7s\n",
618 		      "pages", "count", "credits", "min");
619 	LASSERT(tmpstr + tmpsiz - s > 0);
620 
621 	if (the_lnet.ln_rtrpools == NULL)
622 		goto out; /* I'm not a router */
623 
624 	for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
625 		lnet_rtrbufpool_t *rbp;
626 
627 		lnet_net_lock(LNET_LOCK_EX);
628 		cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
629 			s += snprintf(s, tmpstr + tmpsiz - s,
630 				      "%5d %5d %7d %7d\n",
631 				      rbp[idx].rbp_npages,
632 				      rbp[idx].rbp_nbuffers,
633 				      rbp[idx].rbp_credits,
634 				      rbp[idx].rbp_mincredits);
635 			LASSERT(tmpstr + tmpsiz - s > 0);
636 		}
637 		lnet_net_unlock(LNET_LOCK_EX);
638 	}
639 
640  out:
641 	len = s - tmpstr;
642 
643 	if (pos >= min_t(int, len, strlen(tmpstr)))
644 		rc = 0;
645 	else
646 		rc = cfs_trace_copyout_string(buffer, nob,
647 					      tmpstr + pos, NULL);
648 
649 	LIBCFS_FREE(tmpstr, tmpsiz);
650 	return rc;
651 }
652 
proc_lnet_buffers(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)653 static int proc_lnet_buffers(struct ctl_table *table, int write,
654 			     void __user *buffer, size_t *lenp, loff_t *ppos)
655 {
656 	return proc_call_handler(table->data, write, ppos, buffer, lenp,
657 				 __proc_lnet_buffers);
658 }
659 
proc_lnet_nis(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)660 static int proc_lnet_nis(struct ctl_table *table, int write,
661 			 void __user *buffer, size_t *lenp, loff_t *ppos)
662 {
663 	int	tmpsiz = 128 * LNET_CPT_NUMBER;
664 	int	rc = 0;
665 	char      *tmpstr;
666 	char      *s;
667 	int	len;
668 
669 	LASSERT(!write);
670 
671 	if (*lenp == 0)
672 		return 0;
673 
674 	LIBCFS_ALLOC(tmpstr, tmpsiz);
675 	if (tmpstr == NULL)
676 		return -ENOMEM;
677 
678 	s = tmpstr; /* points to current position in tmpstr[] */
679 
680 	if (*ppos == 0) {
681 		s += snprintf(s, tmpstr + tmpsiz - s,
682 			      "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
683 			      "nid", "status", "alive", "refs", "peer",
684 			      "rtr", "max", "tx", "min");
685 		LASSERT(tmpstr + tmpsiz - s > 0);
686 	} else {
687 		struct list_head	*n;
688 		lnet_ni_t	 *ni   = NULL;
689 		int		skip = *ppos - 1;
690 
691 		lnet_net_lock(0);
692 
693 		n = the_lnet.ln_nis.next;
694 
695 		while (n != &the_lnet.ln_nis) {
696 			lnet_ni_t *a_ni = list_entry(n, lnet_ni_t, ni_list);
697 
698 			if (skip == 0) {
699 				ni = a_ni;
700 				break;
701 			}
702 
703 			skip--;
704 			n = n->next;
705 		}
706 
707 		if (ni != NULL) {
708 			struct lnet_tx_queue	*tq;
709 			char	*stat;
710 			long	now = get_seconds();
711 			int	last_alive = -1;
712 			int	i;
713 			int	j;
714 
715 			if (the_lnet.ln_routing)
716 				last_alive = now - ni->ni_last_alive;
717 
718 			/* @lo forever alive */
719 			if (ni->ni_lnd->lnd_type == LOLND)
720 				last_alive = 0;
721 
722 			lnet_ni_lock(ni);
723 			LASSERT(ni->ni_status != NULL);
724 			stat = (ni->ni_status->ns_status ==
725 				LNET_NI_STATUS_UP) ? "up" : "down";
726 			lnet_ni_unlock(ni);
727 
728 			/* we actually output credits information for
729 			 * TX queue of each partition */
730 			cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
731 				for (j = 0; ni->ni_cpts != NULL &&
732 				     j < ni->ni_ncpts; j++) {
733 					if (i == ni->ni_cpts[j])
734 						break;
735 				}
736 
737 				if (j == ni->ni_ncpts)
738 					continue;
739 
740 				if (i != 0)
741 					lnet_net_lock(i);
742 
743 				s += snprintf(s, tmpstr + tmpsiz - s,
744 				      "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
745 				      libcfs_nid2str(ni->ni_nid), stat,
746 				      last_alive, *ni->ni_refs[i],
747 				      ni->ni_peertxcredits,
748 				      ni->ni_peerrtrcredits,
749 				      tq->tq_credits_max,
750 				      tq->tq_credits, tq->tq_credits_min);
751 				if (i != 0)
752 					lnet_net_unlock(i);
753 			}
754 			LASSERT(tmpstr + tmpsiz - s > 0);
755 		}
756 
757 		lnet_net_unlock(0);
758 	}
759 
760 	len = s - tmpstr;     /* how many bytes was written */
761 
762 	if (len > *lenp) {    /* linux-supplied buffer is too small */
763 		rc = -EINVAL;
764 	} else if (len > 0) { /* wrote something */
765 		if (copy_to_user(buffer, tmpstr, len))
766 			rc = -EFAULT;
767 		else
768 			*ppos += 1;
769 	}
770 
771 	LIBCFS_FREE(tmpstr, tmpsiz);
772 
773 	if (rc == 0)
774 		*lenp = len;
775 
776 	return rc;
777 }
778 
779 struct lnet_portal_rotors {
780 	int	     pr_value;
781 	const char      *pr_name;
782 	const char	*pr_desc;
783 };
784 
785 static struct lnet_portal_rotors	portal_rotors[] = {
786 	{
787 		.pr_value = LNET_PTL_ROTOR_OFF,
788 		.pr_name  = "OFF",
789 		.pr_desc  = "Turn off message rotor for wildcard portals"
790 	},
791 	{
792 		.pr_value = LNET_PTL_ROTOR_ON,
793 		.pr_name  = "ON",
794 		.pr_desc  = "round-robin dispatch all PUT messages for wildcard portals"
795 	},
796 	{
797 		.pr_value = LNET_PTL_ROTOR_RR_RT,
798 		.pr_name  = "RR_RT",
799 		.pr_desc  = "round-robin dispatch routed PUT message for wildcard portals"
800 	},
801 	{
802 		.pr_value = LNET_PTL_ROTOR_HASH_RT,
803 		.pr_name  = "HASH_RT",
804 		.pr_desc  = "dispatch routed PUT message by hashing source NID for wildcard portals"
805 	},
806 	{
807 		.pr_value = -1,
808 		.pr_name  = NULL,
809 		.pr_desc  = NULL
810 	},
811 };
812 
813 extern int portal_rotor;
814 
__proc_lnet_portal_rotor(void * data,int write,loff_t pos,void __user * buffer,int nob)815 static int __proc_lnet_portal_rotor(void *data, int write,
816 				    loff_t pos, void __user *buffer, int nob)
817 {
818 	const int	buf_len	= 128;
819 	char		*buf;
820 	char		*tmp;
821 	int		rc;
822 	int		i;
823 
824 	LIBCFS_ALLOC(buf, buf_len);
825 	if (buf == NULL)
826 		return -ENOMEM;
827 
828 	if (!write) {
829 		lnet_res_lock(0);
830 
831 		for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
832 			if (portal_rotors[i].pr_value == portal_rotor)
833 				break;
834 		}
835 
836 		LASSERT(portal_rotors[i].pr_value == portal_rotor);
837 		lnet_res_unlock(0);
838 
839 		rc = snprintf(buf, buf_len,
840 			      "{\n\tportals: all\n"
841 			      "\trotor: %s\n\tdescription: %s\n}",
842 			      portal_rotors[i].pr_name,
843 			      portal_rotors[i].pr_desc);
844 
845 		if (pos >= min_t(int, rc, buf_len)) {
846 			rc = 0;
847 		} else {
848 			rc = cfs_trace_copyout_string(buffer, nob,
849 					buf + pos, "\n");
850 		}
851 		goto out;
852 	}
853 
854 	rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
855 	if (rc < 0)
856 		goto out;
857 
858 	tmp = cfs_trimwhite(buf);
859 
860 	rc = -EINVAL;
861 	lnet_res_lock(0);
862 	for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
863 		if (strncasecmp(portal_rotors[i].pr_name, tmp,
864 				strlen(portal_rotors[i].pr_name)) == 0) {
865 			portal_rotor = portal_rotors[i].pr_value;
866 			rc = 0;
867 			break;
868 		}
869 	}
870 	lnet_res_unlock(0);
871 out:
872 	LIBCFS_FREE(buf, buf_len);
873 	return rc;
874 }
875 
proc_lnet_portal_rotor(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)876 static int proc_lnet_portal_rotor(struct ctl_table *table, int write,
877 				  void __user *buffer, size_t *lenp,
878 				  loff_t *ppos)
879 {
880 	return proc_call_handler(table->data, write, ppos, buffer, lenp,
881 				 __proc_lnet_portal_rotor);
882 }
883 
884 static struct ctl_table lnet_table[] = {
885 	/*
886 	 * NB No .strategy entries have been provided since sysctl(8) prefers
887 	 * to go via /proc for portability.
888 	 */
889 	{
890 		.procname = "stats",
891 		.mode     = 0644,
892 		.proc_handler = &proc_lnet_stats,
893 	},
894 	{
895 		.procname = "routes",
896 		.mode     = 0444,
897 		.proc_handler = &proc_lnet_routes,
898 	},
899 	{
900 		.procname = "routers",
901 		.mode     = 0444,
902 		.proc_handler = &proc_lnet_routers,
903 	},
904 	{
905 		.procname = "peers",
906 		.mode     = 0444,
907 		.proc_handler = &proc_lnet_peers,
908 	},
909 	{
910 		.procname = "buffers",
911 		.mode     = 0444,
912 		.proc_handler = &proc_lnet_buffers,
913 	},
914 	{
915 		.procname = "nis",
916 		.mode     = 0444,
917 		.proc_handler = &proc_lnet_nis,
918 	},
919 	{
920 		.procname = "portal_rotor",
921 		.mode     = 0644,
922 		.proc_handler = &proc_lnet_portal_rotor,
923 	},
924 	{
925 	}
926 };
927 
928 static struct ctl_table top_table[] = {
929 	{
930 		.procname = "lnet",
931 		.mode     = 0555,
932 		.data     = NULL,
933 		.maxlen   = 0,
934 		.child    = lnet_table,
935 	},
936 	{
937 	}
938 };
939 
940 void
lnet_proc_init(void)941 lnet_proc_init(void)
942 {
943 	if (lnet_table_header == NULL)
944 		lnet_table_header = register_sysctl_table(top_table);
945 }
946 
947 void
lnet_proc_fini(void)948 lnet_proc_fini(void)
949 {
950 	if (lnet_table_header != NULL)
951 		unregister_sysctl_table(lnet_table_header);
952 
953 	lnet_table_header = NULL;
954 }
955 
956 #else
957 
958 void
lnet_proc_init(void)959 lnet_proc_init(void)
960 {
961 }
962 
963 void
lnet_proc_fini(void)964 lnet_proc_fini(void)
965 {
966 }
967 
968 #endif
969