xref: /OK3568_Linux_fs/kernel/net/sched/act_simple.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * net/sched/act_simple.c	Simple example of an action
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Authors:	Jamal Hadi Salim (2005-8)
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/skbuff.h>
13*4882a593Smuzhiyun #include <linux/rtnetlink.h>
14*4882a593Smuzhiyun #include <net/netlink.h>
15*4882a593Smuzhiyun #include <net/pkt_sched.h>
16*4882a593Smuzhiyun #include <net/pkt_cls.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/tc_act/tc_defact.h>
19*4882a593Smuzhiyun #include <net/tc_act/tc_defact.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun static unsigned int simp_net_id;
22*4882a593Smuzhiyun static struct tc_action_ops act_simp_ops;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define SIMP_MAX_DATA	32
tcf_simp_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)25*4882a593Smuzhiyun static int tcf_simp_act(struct sk_buff *skb, const struct tc_action *a,
26*4882a593Smuzhiyun 			struct tcf_result *res)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	struct tcf_defact *d = to_defact(a);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	spin_lock(&d->tcf_lock);
31*4882a593Smuzhiyun 	tcf_lastuse_update(&d->tcf_tm);
32*4882a593Smuzhiyun 	bstats_update(&d->tcf_bstats, skb);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* print policy string followed by _ then packet count
35*4882a593Smuzhiyun 	 * Example if this was the 3rd packet and the string was "hello"
36*4882a593Smuzhiyun 	 * then it would look like "hello_3" (without quotes)
37*4882a593Smuzhiyun 	 */
38*4882a593Smuzhiyun 	pr_info("simple: %s_%llu\n",
39*4882a593Smuzhiyun 	       (char *)d->tcfd_defdata, d->tcf_bstats.packets);
40*4882a593Smuzhiyun 	spin_unlock(&d->tcf_lock);
41*4882a593Smuzhiyun 	return d->tcf_action;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
tcf_simp_release(struct tc_action * a)44*4882a593Smuzhiyun static void tcf_simp_release(struct tc_action *a)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct tcf_defact *d = to_defact(a);
47*4882a593Smuzhiyun 	kfree(d->tcfd_defdata);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
alloc_defdata(struct tcf_defact * d,const struct nlattr * defdata)50*4882a593Smuzhiyun static int alloc_defdata(struct tcf_defact *d, const struct nlattr *defdata)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL);
53*4882a593Smuzhiyun 	if (unlikely(!d->tcfd_defdata))
54*4882a593Smuzhiyun 		return -ENOMEM;
55*4882a593Smuzhiyun 	nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
56*4882a593Smuzhiyun 	return 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
reset_policy(struct tc_action * a,const struct nlattr * defdata,struct tc_defact * p,struct tcf_proto * tp,struct netlink_ext_ack * extack)59*4882a593Smuzhiyun static int reset_policy(struct tc_action *a, const struct nlattr *defdata,
60*4882a593Smuzhiyun 			struct tc_defact *p, struct tcf_proto *tp,
61*4882a593Smuzhiyun 			struct netlink_ext_ack *extack)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	struct tcf_chain *goto_ch = NULL;
64*4882a593Smuzhiyun 	struct tcf_defact *d;
65*4882a593Smuzhiyun 	int err;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	err = tcf_action_check_ctrlact(p->action, tp, &goto_ch, extack);
68*4882a593Smuzhiyun 	if (err < 0)
69*4882a593Smuzhiyun 		return err;
70*4882a593Smuzhiyun 	d = to_defact(a);
71*4882a593Smuzhiyun 	spin_lock_bh(&d->tcf_lock);
72*4882a593Smuzhiyun 	goto_ch = tcf_action_set_ctrlact(a, p->action, goto_ch);
73*4882a593Smuzhiyun 	memset(d->tcfd_defdata, 0, SIMP_MAX_DATA);
74*4882a593Smuzhiyun 	nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
75*4882a593Smuzhiyun 	spin_unlock_bh(&d->tcf_lock);
76*4882a593Smuzhiyun 	if (goto_ch)
77*4882a593Smuzhiyun 		tcf_chain_put_by_act(goto_ch);
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
82*4882a593Smuzhiyun 	[TCA_DEF_PARMS]	= { .len = sizeof(struct tc_defact) },
83*4882a593Smuzhiyun 	[TCA_DEF_DATA]	= { .type = NLA_STRING, .len = SIMP_MAX_DATA },
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
tcf_simp_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,int ovr,int bind,bool rtnl_held,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)86*4882a593Smuzhiyun static int tcf_simp_init(struct net *net, struct nlattr *nla,
87*4882a593Smuzhiyun 			 struct nlattr *est, struct tc_action **a,
88*4882a593Smuzhiyun 			 int ovr, int bind, bool rtnl_held,
89*4882a593Smuzhiyun 			 struct tcf_proto *tp, u32 flags,
90*4882a593Smuzhiyun 			 struct netlink_ext_ack *extack)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct tc_action_net *tn = net_generic(net, simp_net_id);
93*4882a593Smuzhiyun 	struct nlattr *tb[TCA_DEF_MAX + 1];
94*4882a593Smuzhiyun 	struct tcf_chain *goto_ch = NULL;
95*4882a593Smuzhiyun 	struct tc_defact *parm;
96*4882a593Smuzhiyun 	struct tcf_defact *d;
97*4882a593Smuzhiyun 	bool exists = false;
98*4882a593Smuzhiyun 	int ret = 0, err;
99*4882a593Smuzhiyun 	u32 index;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (nla == NULL)
102*4882a593Smuzhiyun 		return -EINVAL;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	err = nla_parse_nested_deprecated(tb, TCA_DEF_MAX, nla, simple_policy,
105*4882a593Smuzhiyun 					  NULL);
106*4882a593Smuzhiyun 	if (err < 0)
107*4882a593Smuzhiyun 		return err;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (tb[TCA_DEF_PARMS] == NULL)
110*4882a593Smuzhiyun 		return -EINVAL;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	parm = nla_data(tb[TCA_DEF_PARMS]);
113*4882a593Smuzhiyun 	index = parm->index;
114*4882a593Smuzhiyun 	err = tcf_idr_check_alloc(tn, &index, a, bind);
115*4882a593Smuzhiyun 	if (err < 0)
116*4882a593Smuzhiyun 		return err;
117*4882a593Smuzhiyun 	exists = err;
118*4882a593Smuzhiyun 	if (exists && bind)
119*4882a593Smuzhiyun 		return 0;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (tb[TCA_DEF_DATA] == NULL) {
122*4882a593Smuzhiyun 		if (exists)
123*4882a593Smuzhiyun 			tcf_idr_release(*a, bind);
124*4882a593Smuzhiyun 		else
125*4882a593Smuzhiyun 			tcf_idr_cleanup(tn, index);
126*4882a593Smuzhiyun 		return -EINVAL;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (!exists) {
130*4882a593Smuzhiyun 		ret = tcf_idr_create(tn, index, est, a,
131*4882a593Smuzhiyun 				     &act_simp_ops, bind, false, 0);
132*4882a593Smuzhiyun 		if (ret) {
133*4882a593Smuzhiyun 			tcf_idr_cleanup(tn, index);
134*4882a593Smuzhiyun 			return ret;
135*4882a593Smuzhiyun 		}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		d = to_defact(*a);
138*4882a593Smuzhiyun 		err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch,
139*4882a593Smuzhiyun 					       extack);
140*4882a593Smuzhiyun 		if (err < 0)
141*4882a593Smuzhiyun 			goto release_idr;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		err = alloc_defdata(d, tb[TCA_DEF_DATA]);
144*4882a593Smuzhiyun 		if (err < 0)
145*4882a593Smuzhiyun 			goto put_chain;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 		tcf_action_set_ctrlact(*a, parm->action, goto_ch);
148*4882a593Smuzhiyun 		ret = ACT_P_CREATED;
149*4882a593Smuzhiyun 	} else {
150*4882a593Smuzhiyun 		if (!ovr) {
151*4882a593Smuzhiyun 			err = -EEXIST;
152*4882a593Smuzhiyun 			goto release_idr;
153*4882a593Smuzhiyun 		}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		err = reset_policy(*a, tb[TCA_DEF_DATA], parm, tp, extack);
156*4882a593Smuzhiyun 		if (err)
157*4882a593Smuzhiyun 			goto release_idr;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	return ret;
161*4882a593Smuzhiyun put_chain:
162*4882a593Smuzhiyun 	if (goto_ch)
163*4882a593Smuzhiyun 		tcf_chain_put_by_act(goto_ch);
164*4882a593Smuzhiyun release_idr:
165*4882a593Smuzhiyun 	tcf_idr_release(*a, bind);
166*4882a593Smuzhiyun 	return err;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
tcf_simp_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)169*4882a593Smuzhiyun static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a,
170*4882a593Smuzhiyun 			 int bind, int ref)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	unsigned char *b = skb_tail_pointer(skb);
173*4882a593Smuzhiyun 	struct tcf_defact *d = to_defact(a);
174*4882a593Smuzhiyun 	struct tc_defact opt = {
175*4882a593Smuzhiyun 		.index   = d->tcf_index,
176*4882a593Smuzhiyun 		.refcnt  = refcount_read(&d->tcf_refcnt) - ref,
177*4882a593Smuzhiyun 		.bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
178*4882a593Smuzhiyun 	};
179*4882a593Smuzhiyun 	struct tcf_t t;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	spin_lock_bh(&d->tcf_lock);
182*4882a593Smuzhiyun 	opt.action = d->tcf_action;
183*4882a593Smuzhiyun 	if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) ||
184*4882a593Smuzhiyun 	    nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata))
185*4882a593Smuzhiyun 		goto nla_put_failure;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	tcf_tm_dump(&t, &d->tcf_tm);
188*4882a593Smuzhiyun 	if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD))
189*4882a593Smuzhiyun 		goto nla_put_failure;
190*4882a593Smuzhiyun 	spin_unlock_bh(&d->tcf_lock);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return skb->len;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun nla_put_failure:
195*4882a593Smuzhiyun 	spin_unlock_bh(&d->tcf_lock);
196*4882a593Smuzhiyun 	nlmsg_trim(skb, b);
197*4882a593Smuzhiyun 	return -1;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
tcf_simp_walker(struct net * net,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)200*4882a593Smuzhiyun static int tcf_simp_walker(struct net *net, struct sk_buff *skb,
201*4882a593Smuzhiyun 			   struct netlink_callback *cb, int type,
202*4882a593Smuzhiyun 			   const struct tc_action_ops *ops,
203*4882a593Smuzhiyun 			   struct netlink_ext_ack *extack)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	struct tc_action_net *tn = net_generic(net, simp_net_id);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
tcf_simp_search(struct net * net,struct tc_action ** a,u32 index)210*4882a593Smuzhiyun static int tcf_simp_search(struct net *net, struct tc_action **a, u32 index)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct tc_action_net *tn = net_generic(net, simp_net_id);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	return tcf_idr_search(tn, a, index);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun static struct tc_action_ops act_simp_ops = {
218*4882a593Smuzhiyun 	.kind		=	"simple",
219*4882a593Smuzhiyun 	.id		=	TCA_ID_SIMP,
220*4882a593Smuzhiyun 	.owner		=	THIS_MODULE,
221*4882a593Smuzhiyun 	.act		=	tcf_simp_act,
222*4882a593Smuzhiyun 	.dump		=	tcf_simp_dump,
223*4882a593Smuzhiyun 	.cleanup	=	tcf_simp_release,
224*4882a593Smuzhiyun 	.init		=	tcf_simp_init,
225*4882a593Smuzhiyun 	.walk		=	tcf_simp_walker,
226*4882a593Smuzhiyun 	.lookup		=	tcf_simp_search,
227*4882a593Smuzhiyun 	.size		=	sizeof(struct tcf_defact),
228*4882a593Smuzhiyun };
229*4882a593Smuzhiyun 
simp_init_net(struct net * net)230*4882a593Smuzhiyun static __net_init int simp_init_net(struct net *net)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct tc_action_net *tn = net_generic(net, simp_net_id);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return tc_action_net_init(net, tn, &act_simp_ops);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
simp_exit_net(struct list_head * net_list)237*4882a593Smuzhiyun static void __net_exit simp_exit_net(struct list_head *net_list)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	tc_action_net_exit(net_list, simp_net_id);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun static struct pernet_operations simp_net_ops = {
243*4882a593Smuzhiyun 	.init = simp_init_net,
244*4882a593Smuzhiyun 	.exit_batch = simp_exit_net,
245*4882a593Smuzhiyun 	.id   = &simp_net_id,
246*4882a593Smuzhiyun 	.size = sizeof(struct tc_action_net),
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun MODULE_AUTHOR("Jamal Hadi Salim(2005)");
250*4882a593Smuzhiyun MODULE_DESCRIPTION("Simple example action");
251*4882a593Smuzhiyun MODULE_LICENSE("GPL");
252*4882a593Smuzhiyun 
simp_init_module(void)253*4882a593Smuzhiyun static int __init simp_init_module(void)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	int ret = tcf_register_action(&act_simp_ops, &simp_net_ops);
256*4882a593Smuzhiyun 	if (!ret)
257*4882a593Smuzhiyun 		pr_info("Simple TC action Loaded\n");
258*4882a593Smuzhiyun 	return ret;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
simp_cleanup_module(void)261*4882a593Smuzhiyun static void __exit simp_cleanup_module(void)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	tcf_unregister_action(&act_simp_ops, &simp_net_ops);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun module_init(simp_init_module);
267*4882a593Smuzhiyun module_exit(simp_cleanup_module);
268