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