/* * Copyright (c) 2008 Mellanox Technologies Ltd. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include "sdp.h" #ifdef CONFIG_PROC_FS #define PROC_SDP_STATS "sdpstats" #define PROC_SDP_PERF "sdpprf" /* just like TCP fs */ struct sdp_seq_afinfo { struct module *owner; char *name; sa_family_t family; int (*seq_show) (struct seq_file *m, void *v); struct file_operations *seq_fops; }; struct sdp_iter_state { sa_family_t family; int num; struct seq_operations seq_ops; }; static void *sdp_get_idx(struct seq_file *seq, loff_t pos) { int i = 0; struct sdp_sock *ssk; if (!list_empty(&sock_list)) list_for_each_entry(ssk, &sock_list, sock_list) { if (i == pos) return ssk; i++; } return NULL; } static void *sdp_seq_start(struct seq_file *seq, loff_t *pos) { void *start = NULL; struct sdp_iter_state *st = seq->private; st->num = 0; if (!*pos) return SEQ_START_TOKEN; spin_lock_irq(&sock_list_lock); start = sdp_get_idx(seq, *pos - 1); if (start) sock_hold((struct socket *)start, SOCK_REF_SEQ); spin_unlock_irq(&sock_list_lock); return start; } static void *sdp_seq_next(struct seq_file *seq, void *v, loff_t *pos) { struct sdp_iter_state *st = seq->private; void *next = NULL; spin_lock_irq(&sock_list_lock); if (v == SEQ_START_TOKEN) next = sdp_get_idx(seq, 0); else next = sdp_get_idx(seq, *pos); if (next) sock_hold((struct socket *)next, SOCK_REF_SEQ); spin_unlock_irq(&sock_list_lock); *pos += 1; st->num++; return next; } static void sdp_seq_stop(struct seq_file *seq, void *v) { } #define TMPSZ 150 static int sdp_seq_show(struct seq_file *seq, void *v) { struct sdp_iter_state *st; struct socket *sk = v; char tmpbuf[TMPSZ + 1]; unsigned int dest; unsigned int src; int uid; unsigned long inode; __u16 destp; __u16 srcp; __u32 rx_queue, tx_queue; if (v == SEQ_START_TOKEN) { seq_printf(seq, "%-*s\n", TMPSZ - 1, " sl local_address rem_address " "uid inode rx_queue tx_queue state"); goto out; } st = seq->private; dest = inet_sk(sk)->daddr; src = inet_sk(sk)->rcv_saddr; destp = ntohs(inet_sk(sk)->dport); srcp = ntohs(inet_sk(sk)->sport); uid = sock_i_uid(sk); inode = sock_i_ino(sk); rx_queue = rcv_nxt(sdp_sk(sk)) - sdp_sk(sk)->copied_seq; tx_queue = sdp_sk(sk)->write_seq - sdp_sk(sk)->tx_ring.una_seq; sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %5d %lu %08X:%08X %X", st->num, src, srcp, dest, destp, uid, inode, rx_queue, tx_queue, sk->sk_state); seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf); sock_put(sk, SOCK_REF_SEQ); out: return 0; } static int sdp_seq_open(struct inode *inode, struct file *file) { struct sdp_seq_afinfo *afinfo = PDE(inode)->data; struct seq_file *seq; struct sdp_iter_state *s; int rc; if (unlikely(afinfo == NULL)) return -EINVAL; /* Workaround bogus warning by memtrack */ #define _kzalloc(size,flags) kzalloc(size,flags) #undef kzalloc s = kzalloc(sizeof(*s), GFP_KERNEL); #define kzalloc(s,f) _kzalloc(s,f) if (!s) return -ENOMEM; s->family = afinfo->family; s->seq_ops.start = sdp_seq_start; s->seq_ops.next = sdp_seq_next; s->seq_ops.show = afinfo->seq_show; s->seq_ops.stop = sdp_seq_stop; rc = seq_open(file, &s->seq_ops); if (rc) goto out_kfree; seq = file->private_data; seq->private = s; out: return rc; out_kfree: kfree(s); goto out; } static struct file_operations sdp_seq_fops; static struct sdp_seq_afinfo sdp_seq_afinfo = { .owner = THIS_MODULE, .name = "sdp", .family = AF_INET_SDP, .seq_show = sdp_seq_show, .seq_fops = &sdp_seq_fops, }; #ifdef SDPSTATS_ON DEFINE_PER_CPU(struct sdpstats, sdpstats); static void sdpstats_seq_hist(struct seq_file *seq, char *str, u32 *h, int n, int is_log) { int i; u32 max = 0; seq_printf(seq, "%s:\n", str); for (i = 0; i < n; i++) { if (h[i] > max) max = h[i]; } if (max == 0) { seq_printf(seq, " - all values are 0\n"); return; } for (i = 0; i < n; i++) { char s[51]; int j = 50 * h[i] / max; int val = is_log ? (i == n-1 ? 0 : 1<time - start_t; nsec_rem = do_div(t, 1000000000); seq_printf(m, "%-6d: [%5lu.%06lu] %-50s - [%d{%d} %d:%d] " "mb: %p %s:%d\n", l->idx, (unsigned long)t, nsec_rem/1000, l->msg, l->pid, l->cpu, l->sk_num, l->sk_dport, l->mb, l->func, l->line); out: return 0; } static void *sdpprf_start(struct seq_file *p, loff_t *pos) { int idx = *pos; if (!*pos) { if (!sdpprf_log_count) return SEQ_START_TOKEN; } if (*pos >= MIN(sdpprf_log_count, SDPPRF_LOG_SIZE - 1)) return NULL; if (sdpprf_log_count >= SDPPRF_LOG_SIZE - 1) { int off = sdpprf_log_count & (SDPPRF_LOG_SIZE - 1); idx = (idx + off) & (SDPPRF_LOG_SIZE - 1); } if (!start_t) start_t = sdpprf_log[idx].time; return &sdpprf_log[idx]; } static void *sdpprf_next(struct seq_file *p, void *v, loff_t *pos) { struct sdpprf_log *l = v; if (++*pos >= MIN(sdpprf_log_count, SDPPRF_LOG_SIZE - 1)) return NULL; ++l; if (l - &sdpprf_log[0] >= SDPPRF_LOG_SIZE - 1) return &sdpprf_log[0]; return l; } static void sdpprf_stop(struct seq_file *p, void *v) { } static struct seq_operations sdpprf_ops = { .start = sdpprf_start, .stop = sdpprf_stop, .next = sdpprf_next, .show = sdpprf_show, }; static int sdpprf_open(struct inode *inode, struct file *file) { int res; res = seq_open(file, &sdpprf_ops); return res; } static ssize_t sdpprf_write(struct file *file, const char __user *buf, size_t count, loff_t *offs) { sdpprf_log_count = 0; printk(KERN_INFO "Cleared sdpprf statistics\n"); return count; } static struct file_operations sdpprf_fops = { .open = sdpprf_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, .write = sdpprf_write, }; #endif /* SDP_PROFILING */ int __init sdp_proc_init(void) { struct proc_dir_entry *p = NULL; #ifdef SDPSTATS_ON struct proc_dir_entry *stats = NULL; #endif #ifdef SDP_PROFILING struct proc_dir_entry *prof = NULL; #endif sdp_seq_afinfo.seq_fops->owner = sdp_seq_afinfo.owner; sdp_seq_afinfo.seq_fops->open = sdp_seq_open; sdp_seq_afinfo.seq_fops->read = seq_read; sdp_seq_afinfo.seq_fops->llseek = seq_lseek; sdp_seq_afinfo.seq_fops->release = seq_release_private; p = proc_net_fops_create(&init_net, sdp_seq_afinfo.name, S_IRUGO, sdp_seq_afinfo.seq_fops); if (p) p->data = &sdp_seq_afinfo; else goto no_mem; #ifdef SDPSTATS_ON stats = proc_net_fops_create(&init_net, PROC_SDP_STATS, S_IRUGO | S_IWUGO, &sdpstats_fops); if (!stats) goto no_mem_stats; #endif #ifdef SDP_PROFILING prof = proc_net_fops_create(&init_net, PROC_SDP_PERF, S_IRUGO | S_IWUGO, &sdpprf_fops); if (!prof) goto no_mem_prof; #endif return 0; #ifdef SDP_PROFILING no_mem_prof: #endif #ifdef SDPSTATS_ON proc_net_remove(&init_net, PROC_SDP_STATS); no_mem_stats: #endif proc_net_remove(&init_net, sdp_seq_afinfo.name); no_mem: return -ENOMEM; } void sdp_proc_unregister(void) { proc_net_remove(&init_net, sdp_seq_afinfo.name); memset(sdp_seq_afinfo.seq_fops, 0, sizeof(*sdp_seq_afinfo.seq_fops)); #ifdef SDPSTATS_ON proc_net_remove(&init_net, PROC_SDP_STATS); #endif #ifdef SDP_PROFILING proc_net_remove(&init_net, PROC_SDP_PERF); #endif } #else /* CONFIG_PROC_FS */ int __init sdp_proc_init(void) { return 0; } void sdp_proc_unregister(void) { } #endif /* CONFIG_PROC_FS */