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