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