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