cregit-Linux how code gets into the kernel

Release 4.15 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 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_ext(&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. Miller4640.35%114.29%
Américo Wang3732.46%342.86%
John Fastabend2723.68%228.57%
Patrick McHardy43.51%114.29%
Total114100.00%7100.00%


static int basic_delete(struct tcf_proto *tp, void *arg, bool *last) { 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_ext(&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 Wang4949.00%450.00%
David S. Miller3232.00%112.50%
John Fastabend1616.00%225.00%
Jiri Pirko33.00%112.50%
Total100100.00%8100.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) { int err; err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr); 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. Miller10876.60%112.50%
Jiri Pirko128.51%225.00%
Benjamin LaHaise74.96%112.50%
John Fastabend64.26%112.50%
Cong Wang53.55%112.50%
Patrick McHardy32.13%225.00%
Total141100.00%8100.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) { 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; unsigned long idr_index; 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) { fnew->handle = handle; if (!fold) { err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, handle, handle + 1, GFP_KERNEL); if (err) goto errout; } } else { err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, 1, 0x7FFFFFFF, GFP_KERNEL); if (err) goto errout; fnew->handle = idr_index; } err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr); if (err < 0) { if (!fold) idr_remove_ext(&head->handle_idr, fnew->handle); goto errout; } *arg = fnew; if (fold) { idr_replace_ext(&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. Miller19845.52%15.56%
Américo Wang12328.28%633.33%
John Fastabend7517.24%211.11%
Patrick McHardy143.22%316.67%
Benjamin LaHaise71.61%15.56%
Eric W. Biedermann51.15%15.56%
Jiri Pirko51.15%15.56%
Cong Wang51.15%15.56%
Johannes Berg20.46%15.56%
Panagiotis Issaris10.23%15.56%
Total435100.00%18100.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. Miller91454.31%25.26%
Américo Wang38923.11%1026.32%
John Fastabend1629.63%25.26%
Patrick McHardy1187.01%821.05%
Jiri Pirko201.19%410.53%
Thomas Graf181.07%12.63%
Stephen Hemminger171.01%12.63%
Benjamin LaHaise140.83%12.63%
Cong Wang100.59%12.63%
Eric W. Biedermann50.30%12.63%
Arnaldo Carvalho de Melo50.30%25.26%
Daniel Borkmann40.24%12.63%
Tejun Heo30.18%12.63%
Johannes Berg20.12%12.63%
Panagiotis Issaris10.06%12.63%
Eric Dumazet10.06%12.63%
Total1683100.00%38100.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.