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