cregit-Linux how code gets into the kernel

Release 4.16 net/sched/cls_basic.c

Directory: net/sched
/*
 * net/sched/cls_basic.c        Basic Packet Classifier.
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *
 * Authors:     Thomas Graf <tgraf@suug.ch>
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/rtnetlink.h>
#include <linux/skbuff.h>
#include <linux/idr.h>
#include <net/netlink.h>
#include <net/act_api.h>
#include <net/pkt_cls.h>


struct basic_head {
	
struct list_head	flist;
	
struct idr		handle_idr;
	
struct rcu_head		rcu;
};


struct basic_filter {
	
u32			handle;
	
struct tcf_exts		exts;
	
struct tcf_ematch_tree	ematches;
	
struct tcf_result	res;
	
struct tcf_proto	*tp;
	
struct list_head	link;
	
union {
		
struct work_struct	work;
		
struct rcu_head		rcu;
	};
};


static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res) { int r; struct basic_head *head = rcu_dereference_bh(tp->root); struct basic_filter *f; list_for_each_entry_rcu(f, &head->flist, link) { if (!tcf_em_tree_match(skb, &f->ematches, NULL)) continue; *res = f->res; r = tcf_exts_exec(skb, &f->exts, res); if (r < 0) continue; return r; } return -1; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller9895.15%133.33%
John Fastabend43.88%133.33%
Eric Dumazet10.97%133.33%
Total103100.00%3100.00%


static void *basic_get(struct tcf_proto *tp, u32 handle) { struct basic_head *head = rtnl_dereference(tp->root); struct basic_filter *f; list_for_each_entry(f, &head->flist, link) { if (f->handle == handle) { return f; } } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller4679.31%125.00%
Américo Wang58.62%125.00%
Daniel Borkmann46.90%125.00%
John Fastabend35.17%125.00%
Total58100.00%4100.00%


static int basic_init(struct tcf_proto *tp) { struct basic_head *head; head = kzalloc(sizeof(*head), GFP_KERNEL); if (head == NULL) return -ENOBUFS; INIT_LIST_HEAD(&head->flist); idr_init(&head->handle_idr); rcu_assign_pointer(tp->root, head); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy4161.19%125.00%
David S. Miller1420.90%125.00%
Américo Wang811.94%125.00%
John Fastabend45.97%125.00%
Total67100.00%4100.00%


static void __basic_delete_filter(struct basic_filter *f) { tcf_exts_destroy(&f->exts); tcf_em_tree_destroy(&f->ematches); tcf_exts_put_net(&f->exts); kfree(f); }

Contributors

PersonTokensPropCommitsCommitProp
Américo Wang40100.00%1100.00%
Total40100.00%1100.00%


static void basic_delete_filter_work(struct work_struct *work) { struct basic_filter *f = container_of(work, struct basic_filter, work); rtnl_lock(); __basic_delete_filter(f); rtnl_unlock(); }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller1437.84%125.00%
Américo Wang1232.43%250.00%
John Fastabend1129.73%125.00%
Total37100.00%4100.00%


static void basic_delete_filter(struct rcu_head *head) { struct basic_filter *f = container_of(head, struct basic_filter, rcu); INIT_WORK(&f->work, basic_delete_filter_work); tcf_queue_work(&f->work); }

Contributors

PersonTokensPropCommitsCommitProp
Américo Wang44100.00%1100.00%
Total44100.00%1100.00%


static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack) { struct basic_head *head = rtnl_dereference(tp->root); struct basic_filter *f, *n; list_for_each_entry_safe(f, n, &head->flist, link) { list_del_rcu(&f->link); tcf_unbind_filter(tp, &f->res); idr_remove(&head->handle_idr, f->handle); if (tcf_exts_get_net(&f->exts)) call_rcu(&f->rcu, basic_delete_filter); else __basic_delete_filter(f); } idr_destroy(&head->handle_idr); kfree_rcu(head, rcu); }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller4638.66%111.11%
Américo Wang3630.25%333.33%
John Fastabend2722.69%222.22%
Jakub Kiciński54.20%111.11%
Patrick McHardy43.36%111.11%
Matthew Wilcox10.84%111.11%
Total119100.00%9100.00%


static int basic_delete(struct tcf_proto *tp, void *arg, bool *last, struct netlink_ext_ack *extack) { struct basic_head *head = rtnl_dereference(tp->root); struct basic_filter *f = arg; list_del_rcu(&f->link); tcf_unbind_filter(tp, &f->res); idr_remove(&head->handle_idr, f->handle); tcf_exts_get_net(&f->exts); call_rcu(&f->rcu, basic_delete_filter); *last = list_empty(&head->flist); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Américo Wang4845.71%440.00%
David S. Miller3230.48%110.00%
John Fastabend1615.24%220.00%
Alexander Aring54.76%110.00%
Jiri Pirko32.86%110.00%
Matthew Wilcox10.95%110.00%
Total105100.00%10100.00%

static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = { [TCA_BASIC_CLASSID] = { .type = NLA_U32 }, [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED }, };
static int basic_set_parms(struct net *net, struct tcf_proto *tp, struct basic_filter *f, unsigned long base, struct nlattr **tb, struct nlattr *est, bool ovr, struct netlink_ext_ack *extack) { int err; err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack); if (err < 0) return err; err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches); if (err < 0) return err; if (tb[TCA_BASIC_CLASSID]) { f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]); tcf_bind_filter(tp, &f->res, base); } f->tp = tp; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller10872.97%111.11%
Jiri Pirko128.11%222.22%
Benjamin LaHaise74.73%111.11%
Alexander Aring74.73%111.11%
John Fastabend64.05%111.11%
Cong Wang53.38%111.11%
Patrick McHardy32.03%222.22%
Total148100.00%9100.00%


static int basic_change(struct net *net, struct sk_buff *in_skb, struct tcf_proto *tp, unsigned long base, u32 handle, struct nlattr **tca, void **arg, bool ovr, struct netlink_ext_ack *extack) { int err; struct basic_head *head = rtnl_dereference(tp->root); struct nlattr *tb[TCA_BASIC_MAX + 1]; struct basic_filter *fold = (struct basic_filter *) *arg; struct basic_filter *fnew; if (tca[TCA_OPTIONS] == NULL) return -EINVAL; err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS], basic_policy, NULL); if (err < 0) return err; if (fold != NULL) { if (handle && fold->handle != handle) return -EINVAL; } fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); if (!fnew) return -ENOBUFS; err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE); if (err < 0) goto errout; if (!handle) { handle = 1; err = idr_alloc_u32(&head->handle_idr, fnew, &handle, INT_MAX, GFP_KERNEL); } else if (!fold) { err = idr_alloc_u32(&head->handle_idr, fnew, &handle, handle, GFP_KERNEL); } if (err) goto errout; fnew->handle = handle; err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr, extack); if (err < 0) { if (!fold) idr_remove(&head->handle_idr, fnew->handle); goto errout; } *arg = fnew; if (fold) { idr_replace(&head->handle_idr, fnew, fnew->handle); list_replace_rcu(&fold->link, &fnew->link); tcf_unbind_filter(tp, &fold->res); tcf_exts_get_net(&fold->exts); call_rcu(&fold->rcu, basic_delete_filter); } else { list_add_rcu(&fnew->link, &head->flist); } return 0; errout: tcf_exts_destroy(&fnew->exts); kfree(fnew); return err; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller19445.97%14.35%
Américo Wang9823.22%626.09%
John Fastabend6916.35%28.70%
Matthew Wilcox153.55%313.04%
Patrick McHardy143.32%313.04%
Benjamin LaHaise71.66%14.35%
Alexander Aring71.66%28.70%
Eric W. Biedermann51.18%14.35%
Cong Wang51.18%14.35%
Jiri Pirko51.18%14.35%
Johannes Berg20.47%14.35%
Panagiotis Issaris10.24%14.35%
Total422100.00%23100.00%


static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg) { struct basic_head *head = rtnl_dereference(tp->root); struct basic_filter *f; list_for_each_entry(f, &head->flist, link) { if (arg->count < arg->skip) goto skip; if (arg->fn(tp, f, arg) < 0) { arg->stop = 1; break; } skip: arg->count++; } }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller8496.55%150.00%
John Fastabend33.45%150.00%
Total87100.00%2100.00%


static void basic_bind_class(void *fh, u32 classid, unsigned long cl) { struct basic_filter *f = fh; if (f && f->res.classid == classid) f->res.class = cl; }

Contributors

PersonTokensPropCommitsCommitProp
Américo Wang44100.00%1100.00%
Total44100.00%1100.00%


static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh, struct sk_buff *skb, struct tcmsg *t) { struct basic_filter *f = fh; struct nlattr *nest; if (f == NULL) return skb->len; t->tcm_handle = f->handle; nest = nla_nest_start(skb, TCA_OPTIONS); if (nest == NULL) goto nla_put_failure; if (f->res.classid && nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid)) goto nla_put_failure; if (tcf_exts_dump(skb, &f->exts) < 0 || tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0) goto nla_put_failure; nla_nest_end(skb, nest); if (tcf_exts_dump_stats(skb, &f->exts) < 0) goto nla_put_failure; return skb->len; nla_put_failure: nla_nest_cancel(skb, nest); return -1; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller11263.64%222.22%
Patrick McHardy2011.36%222.22%
Thomas Graf1810.23%111.11%
Stephen Hemminger179.66%111.11%
Américo Wang73.98%222.22%
Arnaldo Carvalho de Melo21.14%111.11%
Total176100.00%9100.00%

static struct tcf_proto_ops cls_basic_ops __read_mostly = { .kind = "basic", .classify = basic_classify, .init = basic_init, .destroy = basic_destroy, .get = basic_get, .change = basic_change, .delete = basic_delete, .walk = basic_walk, .dump = basic_dump, .bind_class = basic_bind_class, .owner = THIS_MODULE, };
static int __init init_basic(void) { return register_tcf_proto_ops(&cls_basic_ops); }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller16100.00%1100.00%
Total16100.00%1100.00%


static void __exit exit_basic(void) { unregister_tcf_proto_ops(&cls_basic_ops); }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller15100.00%1100.00%
Total15100.00%1100.00%

module_init(init_basic) module_exit(exit_basic) MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
David S. Miller91053.94%24.44%
Américo Wang36221.46%1022.22%
John Fastabend1569.25%24.44%
Patrick McHardy1186.99%817.78%
Jiri Pirko201.19%48.89%
Alexander Aring191.13%36.67%
Thomas Graf181.07%12.22%
Stephen Hemminger171.01%12.22%
Matthew Wilcox171.01%36.67%
Benjamin LaHaise140.83%12.22%
Cong Wang100.59%12.22%
Jakub Kiciński50.30%12.22%
Arnaldo Carvalho de Melo50.30%24.44%
Eric W. Biedermann50.30%12.22%
Daniel Borkmann40.24%12.22%
Tejun Heo30.18%12.22%
Johannes Berg20.12%12.22%
Eric Dumazet10.06%12.22%
Panagiotis Issaris10.06%12.22%
Total1687100.00%45100.00%
Directory: net/sched
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.