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