Whamcloud - gitweb
LU-56 lnet: multiple cleanups for inspection
[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_feats &
352                              LNET_PING_FEAT_NI_STATUS) != 0) {
353                                 cfs_list_for_each_entry(rtr, &peer->lp_routes,
354                                                         lr_gwlist) {
355                                         /* downis on any route should be the
356                                          * number of downis on the gateway */
357                                         if (rtr->lr_downis != 0) {
358                                                 down_ni = rtr->lr_downis;
359                                                 break;
360                                         }
361                                 }
362                         }
363
364                         if (deadline == 0)
365                                 s += snprintf(s, tmpstr + tmpsiz - s,
366                                               "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
367                                               nrefs, nrtrrefs, alive_cnt,
368                                               alive ? "up" : "down", last_ping,
369                                               pingsent, "NA", down_ni,
370                                               libcfs_nid2str(nid));
371                         else
372                                 s += snprintf(s, tmpstr + tmpsiz - s,
373                                               "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
374                                               nrefs, nrtrrefs, alive_cnt,
375                                               alive ? "up" : "down", last_ping,
376                                               pingsent,
377                                               cfs_duration_sec(cfs_time_sub(deadline, now)),
378                                               down_ni, libcfs_nid2str(nid));
379                         LASSERT (tmpstr + tmpsiz - s > 0);
380                 }
381
382                 lnet_net_unlock(0);
383         }
384
385         len = s - tmpstr;     /* how many bytes was written */
386
387         if (len > *lenp) {    /* linux-supplied buffer is too small */
388                 rc = -EINVAL;
389         } else if (len > 0) { /* wrote something */
390                 if (cfs_copy_to_user(buffer, tmpstr, len))
391                         rc = -EFAULT;
392                 else {
393                         off += 1;
394                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
395                 }
396         }
397
398         LIBCFS_FREE(tmpstr, tmpsiz);
399
400         if (rc == 0)
401                 *lenp = len;
402
403         return rc;
404 }
405
406 int LL_PROC_PROTO(proc_lnet_peers)
407 {
408         const int               tmpsiz  = 256;
409         struct lnet_peer_table  *ptable;
410         char                    *tmpstr;
411         char                    *s;
412         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
413         int                     ver  = LNET_PROC_VER_GET(*ppos);
414         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
415         int                     hash = LNET_PROC_HASH_GET(*ppos);
416         int                     rc = 0;
417         int                     len;
418
419         CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
420         LASSERT(!write);
421
422         if (*lenp == 0)
423                 return 0;
424
425         if (cpt >= LNET_CPT_NUMBER)
426                 return 0;
427
428         LIBCFS_ALLOC(tmpstr, tmpsiz);
429         if (tmpstr == NULL)
430                 return -ENOMEM;
431
432         s = tmpstr; /* points to current position in tmpstr[] */
433
434         if (*ppos == 0) {
435                 s += snprintf(s, tmpstr + tmpsiz - s,
436                               "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
437                               "nid", "refs", "state", "last", "max",
438                               "rtr", "min", "tx", "min", "queue");
439                 LASSERT (tmpstr + tmpsiz - s > 0);
440
441                 hoff++;
442         } else {
443                 struct lnet_peer        *peer   = NULL;
444                 cfs_list_t              *p      = NULL;
445                 int                     skip    = hoff - 1;
446
447  again:
448                 lnet_net_lock(cpt);
449                 ptable = the_lnet.ln_peer_tables[cpt];
450                 if (hoff == 1)
451                         ver = LNET_PROC_VERSION(ptable->pt_version);
452
453                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
454                         lnet_net_unlock(cpt);
455                         LIBCFS_FREE(tmpstr, tmpsiz);
456                         return -ESTALE;
457                 }
458
459                 while (hash < LNET_PEER_HASH_SIZE) {
460                         if (p == NULL)
461                                 p = ptable->pt_hash[hash].next;
462
463                         while (p != &ptable->pt_hash[hash]) {
464                                 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
465                                                                  lp_hashlist);
466                                 if (skip == 0) {
467                                         peer = lp;
468
469                                         /* minor optimization: start from idx+1
470                                          * on next iteration if we've just
471                                          * drained lp_hashlist */
472                                         if (lp->lp_hashlist.next ==
473                                             &ptable->pt_hash[hash]) {
474                                                 hoff = 1;
475                                                 hash++;
476                                         } else {
477                                                 hoff++;
478                                         }
479
480                                         break;
481                                 }
482
483                                 skip--;
484                                 p = lp->lp_hashlist.next;
485                         }
486
487                         if (peer != NULL)
488                                 break;
489
490                         p = NULL;
491                         hoff = 1;
492                         hash++;
493                 }
494
495                 if (peer != NULL) {
496                         lnet_nid_t nid       = peer->lp_nid;
497                         int        nrefs     = peer->lp_refcount;
498                         int        lastalive = -1;
499                         char      *aliveness = "NA";
500                         int        maxcr     = peer->lp_ni->ni_peertxcredits;
501                         int        txcr      = peer->lp_txcredits;
502                         int        mintxcr   = peer->lp_mintxcredits;
503                         int        rtrcr     = peer->lp_rtrcredits;
504                         int        minrtrcr  = peer->lp_minrtrcredits;
505                         int        txqnob    = peer->lp_txqnob;
506
507                         if (lnet_isrouter(peer) ||
508                             lnet_peer_aliveness_enabled(peer))
509                                 aliveness = peer->lp_alive ? "up" : "down";
510
511                         if (lnet_peer_aliveness_enabled(peer)) {
512                                 cfs_time_t     now = cfs_time_current();
513                                 cfs_duration_t delta;
514
515                                 delta = cfs_time_sub(now, peer->lp_last_alive);
516                                 lastalive = cfs_duration_sec(delta);
517
518                                 /* No need to mess up peers contents with
519                                  * arbitrarily long integers - it suffices to
520                                  * know that lastalive is more than 10000s old
521                                  */
522                                 if (lastalive >= 10000)
523                                         lastalive = 9999;
524                         }
525
526                         lnet_net_unlock(cpt);
527
528                         s += snprintf(s, tmpstr + tmpsiz - s,
529                                       "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
530                                       libcfs_nid2str(nid), nrefs, aliveness,
531                                       lastalive, maxcr, rtrcr, minrtrcr, txcr,
532                                       mintxcr, txqnob);
533                         LASSERT (tmpstr + tmpsiz - s > 0);
534
535                 } else { /* peer is NULL */
536                         lnet_net_unlock(cpt);
537
538                         if (hash == LNET_PEER_HASH_SIZE &&
539                             cpt < LNET_CPT_NUMBER - 1) {
540                                 cpt++;
541                                 hash = 0;
542                                 hoff = 1;
543                                 goto again;
544                         }
545                 }
546         }
547
548         len = s - tmpstr;     /* how many bytes was written */
549
550         if (len > *lenp) {    /* linux-supplied buffer is too small */
551                 rc = -EINVAL;
552         } else if (len > 0) { /* wrote something */
553                 if (cfs_copy_to_user(buffer, tmpstr, len))
554                         rc = -EFAULT;
555                 else
556                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
557         }
558
559         LIBCFS_FREE(tmpstr, tmpsiz);
560
561         if (rc == 0)
562                 *lenp = len;
563
564         return rc;
565 }
566
567 static int __proc_lnet_buffers(void *data, int write,
568                                loff_t pos, void *buffer, int nob)
569 {
570         char            *s;
571         char            *tmpstr;
572         int             tmpsiz;
573         int             idx;
574         int             len;
575         int             rc;
576         int             i;
577
578         LASSERT(!write);
579
580         /* (4 %d) * 4 * LNET_CPT_NUMBER */
581         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
582         LIBCFS_ALLOC(tmpstr, tmpsiz);
583         if (tmpstr == NULL)
584                 return -ENOMEM;
585
586         s = tmpstr; /* points to current position in tmpstr[] */
587
588         s += snprintf(s, tmpstr + tmpsiz - s,
589                       "%5s %5s %7s %7s\n",
590                       "pages", "count", "credits", "min");
591         LASSERT (tmpstr + tmpsiz - s > 0);
592
593         if (the_lnet.ln_rtrpools == NULL)
594                 goto out; /* I'm not a router */
595
596         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
597                 lnet_rtrbufpool_t *rbp;
598
599                 lnet_net_lock(LNET_LOCK_EX);
600                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
601                         s += snprintf(s, tmpstr + tmpsiz - s,
602                                       "%5d %5d %7d %7d\n",
603                                       rbp[idx].rbp_npages,
604                                       rbp[idx].rbp_nbuffers,
605                                       rbp[idx].rbp_credits,
606                                       rbp[idx].rbp_mincredits);
607                         LASSERT(tmpstr + tmpsiz - s > 0);
608                 }
609                 lnet_net_unlock(LNET_LOCK_EX);
610         }
611
612  out:
613         len = s - tmpstr;
614
615         if (pos >= min_t(int, len, strlen(tmpstr)))
616                 rc = 0;
617         else
618                 rc = cfs_trace_copyout_string(buffer, nob,
619                                               tmpstr + pos, NULL);
620
621         LIBCFS_FREE(tmpstr, tmpsiz);
622         return rc;
623 }
624
625 DECLARE_PROC_HANDLER(proc_lnet_buffers);
626
627 int LL_PROC_PROTO(proc_lnet_nis)
628 {
629         int     tmpsiz = 128 * LNET_CPT_NUMBER;
630         int        rc = 0;
631         char      *tmpstr;
632         char      *s;
633         int        len;
634
635         DECLARE_LL_PROC_PPOS_DECL;
636
637         LASSERT (!write);
638
639         if (*lenp == 0)
640                 return 0;
641
642         LIBCFS_ALLOC(tmpstr, tmpsiz);
643         if (tmpstr == NULL)
644                 return -ENOMEM;
645
646         s = tmpstr; /* points to current position in tmpstr[] */
647
648         if (*ppos == 0) {
649                 s += snprintf(s, tmpstr + tmpsiz - s,
650                               "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
651                               "nid", "status", "alive", "refs", "peer",
652                               "rtr", "max", "tx", "min");
653                 LASSERT (tmpstr + tmpsiz - s > 0);
654         } else {
655                 cfs_list_t        *n;
656                 lnet_ni_t         *ni   = NULL;
657                 int                skip = *ppos - 1;
658
659                 lnet_net_lock(0);
660
661                 n = the_lnet.ln_nis.next;
662
663                 while (n != &the_lnet.ln_nis) {
664                         lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
665
666                         if (skip == 0) {
667                                 ni = a_ni;
668                                 break;
669                         }
670
671                         skip--;
672                         n = n->next;
673                 }
674
675                 if (ni != NULL) {
676                         struct lnet_tx_queue    *tq;
677                         char    *stat;
678                         long    now = cfs_time_current_sec();
679                         int     last_alive = -1;
680                         int     i;
681                         int     j;
682
683                         if (the_lnet.ln_routing)
684                                 last_alive = now - ni->ni_last_alive;
685
686                         /* @lo forever alive */
687                         if (ni->ni_lnd->lnd_type == LOLND)
688                                 last_alive = 0;
689
690                         lnet_ni_lock(ni);
691                         LASSERT(ni->ni_status != NULL);
692                         stat = (ni->ni_status->ns_status ==
693                                 LNET_NI_STATUS_UP) ? "up" : "down";
694                         lnet_ni_unlock(ni);
695
696                         /* we actually output credits information for
697                          * TX queue of each partition */
698                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
699                                 for (j = 0; ni->ni_cpts != NULL &&
700                                      j < ni->ni_ncpts; j++) {
701                                         if (i == ni->ni_cpts[j])
702                                                 break;
703                                 }
704
705                                 if (j == ni->ni_ncpts)
706                                         continue;
707
708                                 if (i != 0)
709                                         lnet_net_lock(i);
710
711                                 s += snprintf(s, tmpstr + tmpsiz - s,
712                                       "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
713                                       libcfs_nid2str(ni->ni_nid), stat,
714                                       last_alive, *ni->ni_refs[i],
715                                       ni->ni_peertxcredits,
716                                       ni->ni_peerrtrcredits,
717                                       tq->tq_credits_max,
718                                       tq->tq_credits, tq->tq_credits_min);
719                                 if (i != 0)
720                                         lnet_net_unlock(i);
721                         }
722                         LASSERT(tmpstr + tmpsiz - s > 0);
723                 }
724
725                 lnet_net_unlock(0);
726         }
727
728         len = s - tmpstr;     /* how many bytes was written */
729
730         if (len > *lenp) {    /* linux-supplied buffer is too small */
731                 rc = -EINVAL;
732         } else if (len > 0) { /* wrote something */
733                 if (cfs_copy_to_user(buffer, tmpstr, len))
734                         rc = -EFAULT;
735                 else
736                         *ppos += 1;
737         }
738
739         LIBCFS_FREE(tmpstr, tmpsiz);
740
741         if (rc == 0)
742                 *lenp = len;
743
744         return rc;
745 }
746
747 static cfs_sysctl_table_t lnet_table[] = {
748         /*
749          * NB No .strategy entries have been provided since sysctl(8) prefers
750          * to go via /proc for portability.
751          */
752         {
753                 INIT_CTL_NAME(PSDEV_LNET_STATS)
754                 .procname = "stats",
755                 .mode     = 0644,
756                 .proc_handler = &proc_lnet_stats,
757         },
758         {
759                 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
760                 .procname = "routes",
761                 .mode     = 0444,
762                 .proc_handler = &proc_lnet_routes,
763         },
764         {
765                 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
766                 .procname = "routers",
767                 .mode     = 0444,
768                 .proc_handler = &proc_lnet_routers,
769         },
770         {
771                 INIT_CTL_NAME(PSDEV_LNET_PEERS)
772                 .procname = "peers",
773                 .mode     = 0444,
774                 .proc_handler = &proc_lnet_peers,
775         },
776         {
777                 INIT_CTL_NAME(PSDEV_LNET_PEERS)
778                 .procname = "buffers",
779                 .mode     = 0444,
780                 .proc_handler = &proc_lnet_buffers,
781         },
782         {
783                 INIT_CTL_NAME(PSDEV_LNET_NIS)
784                 .procname = "nis",
785                 .mode     = 0444,
786                 .proc_handler = &proc_lnet_nis,
787         },
788         {
789                 INIT_CTL_NAME(0)
790         }
791 };
792
793 static cfs_sysctl_table_t top_table[] = {
794         {
795                 INIT_CTL_NAME(CTL_LNET)
796                 .procname = "lnet",
797                 .mode     = 0555,
798                 .data     = NULL,
799                 .maxlen   = 0,
800                 .child    = lnet_table,
801         },
802         {
803                 INIT_CTL_NAME(0)
804         }
805 };
806
807 void
808 lnet_proc_init(void)
809 {
810 #ifdef CONFIG_SYSCTL
811         if (lnet_table_header == NULL)
812                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
813 #endif
814 }
815
816 void
817 lnet_proc_fini(void)
818 {
819 #ifdef CONFIG_SYSCTL
820         if (lnet_table_header != NULL)
821                 cfs_unregister_sysctl_table(lnet_table_header);
822
823         lnet_table_header = NULL;
824 #endif
825 }
826
827 #else
828
829 void
830 lnet_proc_init(void)
831 {
832 }
833
834 void
835 lnet_proc_fini(void)
836 {
837 }
838
839 #endif