From patchwork Tue Jan 13 11:14:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kiran Kumar Raparthy X-Patchwork-Id: 42989 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f70.google.com (mail-ee0-f70.google.com [74.125.83.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 963EB20DE8 for ; Tue, 13 Jan 2015 11:14:43 +0000 (UTC) Received: by mail-ee0-f70.google.com with SMTP id b57sf1569108eek.1 for ; Tue, 13 Jan 2015 03:14:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :mime-version:content-type:content-transfer-encoding:sender :precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=qI6CqfAjylTxJbwUvFIaa8HLVCVykP7nNQmzih0ZP74=; b=Au5GyZY1+75i33JzatsVUMWgbQc0NNbJPNqpaABZgg9x2ez/yo22jVZXVhSAUK573Z 8v0KKQzhQrXZIn6He9cNTDRRGsT0T5AfoB7A9quf2oy3jAUeJFEkgeySYx/9y2BhDthp wfvE/NDx9XJnnohXGf52taP24S3qXEIsvyc0MDt/GVWN4YPuiXgocbHOkj4M0sOg4ffu +Y+ceV23GdtAczTQlNAoDk8rrGJRuVlAuN5UkQdrGovm0aQ1cE7D2vEbTQSUtssIGNbu km7JbLvFvYCZjbiHvZih6gMEWpOpCeQWoQXjA36A4BthCxErsP5K07c3YohSnzMDIjpY ztsg== X-Gm-Message-State: ALoCoQlRWvhqAzmPj1gfdM3XoGCxQUyfm822Mfu/q0420B6jHhHfFoq7N88MrENzBXb6nAFUsKq+ X-Received: by 10.112.171.74 with SMTP id as10mr1415129lbc.8.1421147682421; Tue, 13 Jan 2015 03:14:42 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.87.50 with SMTP id u18ls741794laz.96.gmail; Tue, 13 Jan 2015 03:14:42 -0800 (PST) X-Received: by 10.153.8.132 with SMTP id dk4mr41551297lad.56.1421147682142; Tue, 13 Jan 2015 03:14:42 -0800 (PST) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com. [209.85.217.180]) by mx.google.com with ESMTPS id b5si2637370laa.32.2015.01.13.03.14.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 13 Jan 2015 03:14:42 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) client-ip=209.85.217.180; Received: by mail-lb0-f180.google.com with SMTP id l4so1953280lbv.11 for ; Tue, 13 Jan 2015 03:14:42 -0800 (PST) X-Received: by 10.112.64.35 with SMTP id l3mr41309927lbs.82.1421147681950; Tue, 13 Jan 2015 03:14:41 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.9.200 with SMTP id c8csp1348672lbb; Tue, 13 Jan 2015 03:14:41 -0800 (PST) X-Received: by 10.66.141.42 with SMTP id rl10mr52099320pab.100.1421147680015; Tue, 13 Jan 2015 03:14:40 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ne7si26764307pbc.49.2015.01.13.03.14.38; Tue, 13 Jan 2015 03:14:40 -0800 (PST) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751812AbbAMLOh (ORCPT + 27 others); Tue, 13 Jan 2015 06:14:37 -0500 Received: from mail-pd0-f177.google.com ([209.85.192.177]:38645 "EHLO mail-pd0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751000AbbAMLOe (ORCPT ); Tue, 13 Jan 2015 06:14:34 -0500 Received: by mail-pd0-f177.google.com with SMTP id ft15so2935295pdb.8 for ; Tue, 13 Jan 2015 03:14:33 -0800 (PST) X-Received: by 10.69.31.33 with SMTP id kj1mr51000864pbd.43.1421147673779; Tue, 13 Jan 2015 03:14:33 -0800 (PST) Received: from c-krapar-linux.qualcomm.com ([202.46.23.54]) by mx.google.com with ESMTPSA id y2sm16808684pdm.31.2015.01.13.03.14.28 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 13 Jan 2015 03:14:33 -0800 (PST) From: Kiran Raparthy To: linux-kernel@vger.kernel.org Cc: Mike Chan , Arnd Bergmann , Greg Kroah-Hartman , "David S. Miller" , Alexey Kuznetsov , James Morris , Hideaki YOSHIFUJI , Patrick McHardy , netdev@vger.kernel.org, Android Kernel Team , John Stultz , Sumit Semwal , JP Abgrall , =?UTF-8?q?Arve=20Hj=F8nnev=E5g?= , Kiran Raparthy Subject: [RFC 1/2] misc: uidstat: Add uid stat driver to collect network statistics. Date: Tue, 13 Jan 2015 16:44:01 +0530 Message-Id: <1421147642-28360-1-git-send-email-kiran.kumar@linaro.org> X-Mailer: git-send-email 1.8.2.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: kiran.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Mike Chan misc: uidstat: Add uid stat driver to collect network statistics. To analyze application's network statistics, we need a mechanism to export the UID based statistics to userspace so that userspace tools can use the exported numbers and generate the report against the UID. This patch allows the user to explore the UID based network statistics exported to /proc/uid_stat. Cc: Arnd Bergmann Cc: Greg Kroah-Hartman Cc: David S. Miller Cc: Alexey Kuznetsov Cc: James Morris Cc: Hideaki YOSHIFUJI Cc: Patrick McHardy cc: Mike Chan Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: Android Kernel Team Cc: John Stultz Cc: Sumit Semwal Cc: JP Abgrall Cc: Arve Hjřnnevĺg Signed-off-by: Mike Chan [Kiran: Added context to commit message. included build fixes from JP Abgrall and Arve. Used single_release instead of seq_release. internally continued to use uid_t but used covnersion from kuid_t where ever necessary] Signed-off-by: Kiran Raparthy --- drivers/misc/Kconfig | 7 +++ drivers/misc/Makefile | 1 + drivers/misc/uid_stat.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/uid_stat.h | 33 ++++++++++ net/ipv4/tcp.c | 10 +++ 5 files changed, 208 insertions(+) create mode 100644 drivers/misc/uid_stat.c create mode 100644 include/linux/uid_stat.h diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 006242c..1c79b94 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -402,6 +402,13 @@ config TI_DAC7512 This driver can also be built as a module. If so, the module will be called ti_dac7512. +config UID_STAT + bool "UID based statistics tracking" + default n + help + This option allows to enable UID based network statistics tracking. + statistics are exported to /proc/uid_stat. + config VMWARE_BALLOON tristate "VMware Balloon Driver" depends on X86 && HYPERVISOR_GUEST diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 7d5c4cd..3754347 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_ISL29020) += isl29020.o obj-$(CONFIG_SENSORS_TSL2550) += tsl2550.o obj-$(CONFIG_DS1682) += ds1682.o obj-$(CONFIG_TI_DAC7512) += ti_dac7512.o +obj-$(CONFIG_UID_STAT) += uid_stat.o obj-$(CONFIG_C2PORT) += c2port/ obj-$(CONFIG_HMC6352) += hmc6352.o obj-y += eeprom/ diff --git a/drivers/misc/uid_stat.c b/drivers/misc/uid_stat.c new file mode 100644 index 0000000..aaaa406 --- /dev/null +++ b/drivers/misc/uid_stat.c @@ -0,0 +1,157 @@ +/* drivers/misc/uid_stat.c + * + * Copyright (C) 2008 - 2009 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static DEFINE_SPINLOCK(uid_lock); +static LIST_HEAD(uid_list); +static struct proc_dir_entry *parent; + +struct uid_stat { + struct list_head link; + uid_t uid; + atomic_t tcp_rcv; + atomic_t tcp_snd; +}; + +static struct uid_stat *find_uid_stat(uid_t uid) +{ + struct uid_stat *entry; + + list_for_each_entry(entry, &uid_list, link) { + if (entry->uid == uid) + return entry; + } + return NULL; +} + +static int uid_stat_atomic_int_show(struct seq_file *m, void *v) +{ + unsigned int bytes; + atomic_t *counter = m->private; + + bytes = (unsigned int) (atomic_read(counter) + INT_MIN); + return seq_printf(m, "%u\n", bytes); +} + +static int uid_stat_read_atomic_int_open(struct inode *inode, struct file *file) +{ + return single_open(file, uid_stat_atomic_int_show, PDE_DATA(inode)); +} + +static const struct file_operations uid_stat_read_atomic_int_fops = { + .open = uid_stat_read_atomic_int_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +/* Create a new entry for tracking the specified uid. */ +static struct uid_stat *create_stat(uid_t uid) +{ + struct uid_stat *new_uid; + /* Create the uid stat struct and append it to the list. */ + new_uid = kmalloc(sizeof(struct uid_stat), GFP_ATOMIC); + if (!new_uid) + return NULL; + + new_uid->uid = uid; + /* Counters start at INT_MIN, so we can track 4GB of network traffic. */ + atomic_set(&new_uid->tcp_rcv, INT_MIN); + atomic_set(&new_uid->tcp_snd, INT_MIN); + + list_add_tail(&new_uid->link, &uid_list); + return new_uid; +} + +static void create_stat_proc(struct uid_stat *new_uid) +{ + char uid_s[32]; + struct proc_dir_entry *entry; + + sprintf(uid_s, "%d", new_uid->uid); + entry = proc_mkdir(uid_s, parent); + + /* Keep reference to uid_stat so we know what uid to read stats from. */ + proc_create_data("tcp_snd", S_IRUGO, entry, + &uid_stat_read_atomic_int_fops, &new_uid->tcp_snd); + + proc_create_data("tcp_rcv", S_IRUGO, entry, + &uid_stat_read_atomic_int_fops, &new_uid->tcp_rcv); +} + +static struct uid_stat *find_or_create_uid_stat(kuid_t uid) +{ + struct uid_stat *entry; + unsigned long flags; + uid_t proc_uid; + + proc_uid = from_kuid(&init_user_ns, uid); + spin_lock_irqsave(&uid_lock, flags); + entry = find_uid_stat(proc_uid); + if (entry) { + spin_unlock_irqrestore(&uid_lock, flags); + return entry; + } + entry = create_stat(proc_uid); + spin_unlock_irqrestore(&uid_lock, flags); + if (entry) + create_stat_proc(entry); + return entry; +} + +int uid_stat_tcp_snd(kuid_t uid, int size) +{ + struct uid_stat *entry; + + entry = find_or_create_uid_stat(uid); + if (!entry) + return -1; + atomic_add(size, &entry->tcp_snd); + return 0; +} + +int uid_stat_tcp_rcv(kuid_t uid, int size) +{ + struct uid_stat *entry; + + entry = find_or_create_uid_stat(uid); + if (!entry) + return -1; + atomic_add(size, &entry->tcp_rcv); + return 0; +} + +static int __init uid_stat_init(void) +{ + parent = proc_mkdir("uid_stat", NULL); + if (!parent) { + pr_err("uid_stat: failed to create proc entry\n"); + return -1; + } + return 0; +} + +device_initcall(uid_stat_init); diff --git a/include/linux/uid_stat.h b/include/linux/uid_stat.h new file mode 100644 index 0000000..68823d3 --- /dev/null +++ b/include/linux/uid_stat.h @@ -0,0 +1,33 @@ +/* include/linux/uid_stat.h + * + * Copyright (C) 2008-2009 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifndef __uid_stat_h +#define __uid_stat_h + +/* Contains definitions for resource tracking per uid. */ + +#ifdef CONFIG_UID_STAT +int uid_stat_tcp_snd(kuid_t uid, int size); +int uid_stat_tcp_rcv(kuid_t uid, int size); +#else +static inline int uid_stat_tcp_snd(kuid_t uid, int size) +{ +} +static inline int uid_stat_tcp_rcv(kuid_t uid, int size) +{ +} +#endif + +#endif /* _LINUX_UID_STAT_H */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 3075723..00eb156 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -268,6 +268,7 @@ #include #include #include +#include #include #include @@ -1280,6 +1281,9 @@ out: tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); out_nopush: release_sock(sk); + + if (copied + copied_syn) + uid_stat_tcp_snd(current_uid(), copied + copied_syn); return copied + copied_syn; do_fault: @@ -1551,6 +1555,7 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc, if (copied > 0) { tcp_recv_skb(sk, seq, &offset); tcp_cleanup_rbuf(sk, copied); + uid_stat_tcp_rcv(current_uid(), copied); } return copied; } @@ -1879,6 +1884,9 @@ skip_copy: tcp_cleanup_rbuf(sk, copied); release_sock(sk); + + if (copied > 0) + uid_stat_tcp_rcv(current_uid(), copied); return copied; out: @@ -1887,6 +1895,8 @@ out: recv_urg: err = tcp_recv_urg(sk, msg, len, flags); + if (err > 0) + uid_stat_tcp_rcv(current_uid(), err); goto out; recv_sndq: