Linux Audio

Check our new training course

Loading...
v4.6
 
  1/* net/sched/sch_ingress.c - Ingress and clsact qdisc
  2 *
  3 *              This program is free software; you can redistribute it and/or
  4 *              modify it under the terms of the GNU General Public License
  5 *              as published by the Free Software Foundation; either version
  6 *              2 of the License, or (at your option) any later version.
  7 *
  8 * Authors:     Jamal Hadi Salim 1999
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/types.h>
 13#include <linux/list.h>
 14#include <linux/skbuff.h>
 15#include <linux/rtnetlink.h>
 16
 17#include <net/netlink.h>
 18#include <net/pkt_sched.h>
 
 
 
 
 
 
 
 
 19
 20static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
 21{
 22	return NULL;
 23}
 24
 25static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
 26{
 27	return TC_H_MIN(classid) + 1;
 28}
 29
 30static unsigned long ingress_bind_filter(struct Qdisc *sch,
 31					 unsigned long parent, u32 classid)
 32{
 33	return ingress_get(sch, classid);
 34}
 35
 36static void ingress_put(struct Qdisc *sch, unsigned long cl)
 37{
 38}
 39
 40static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
 41{
 42}
 43
 44static struct tcf_proto __rcu **ingress_find_tcf(struct Qdisc *sch,
 45						 unsigned long cl)
 46{
 47	struct net_device *dev = qdisc_dev(sch);
 
 
 
 
 
 
 
 
 
 
 48
 49	return &dev->ingress_cl_list;
 
 
 
 
 50}
 51
 52static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
 53{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54	net_inc_ingress_queue();
 55	sch->flags |= TCQ_F_CPUSTATS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56
 57	return 0;
 58}
 59
 60static void ingress_destroy(struct Qdisc *sch)
 61{
 
 62	struct net_device *dev = qdisc_dev(sch);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 63
 64	tcf_destroy_chain(&dev->ingress_cl_list);
 65	net_dec_ingress_queue();
 66}
 67
 68static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
 69{
 70	struct nlattr *nest;
 71
 72	nest = nla_nest_start(skb, TCA_OPTIONS);
 73	if (nest == NULL)
 74		goto nla_put_failure;
 75
 76	return nla_nest_end(skb, nest);
 77
 78nla_put_failure:
 79	nla_nest_cancel(skb, nest);
 80	return -1;
 81}
 82
 83static const struct Qdisc_class_ops ingress_class_ops = {
 
 84	.leaf		=	ingress_leaf,
 85	.get		=	ingress_get,
 86	.put		=	ingress_put,
 87	.walk		=	ingress_walk,
 88	.tcf_chain	=	ingress_find_tcf,
 89	.bind_tcf	=	ingress_bind_filter,
 90	.unbind_tcf	=	ingress_put,
 91};
 92
 93static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
 94	.cl_ops		=	&ingress_class_ops,
 95	.id		=	"ingress",
 96	.init		=	ingress_init,
 97	.destroy	=	ingress_destroy,
 98	.dump		=	ingress_dump,
 99	.owner		=	THIS_MODULE,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100};
101
102static unsigned long clsact_get(struct Qdisc *sch, u32 classid)
103{
104	switch (TC_H_MIN(classid)) {
105	case TC_H_MIN(TC_H_MIN_INGRESS):
106	case TC_H_MIN(TC_H_MIN_EGRESS):
107		return TC_H_MIN(classid);
108	default:
109		return 0;
110	}
111}
112
113static unsigned long clsact_bind_filter(struct Qdisc *sch,
114					unsigned long parent, u32 classid)
115{
116	return clsact_get(sch, classid);
117}
118
119static struct tcf_proto __rcu **clsact_find_tcf(struct Qdisc *sch,
120						unsigned long cl)
121{
122	struct net_device *dev = qdisc_dev(sch);
123
124	switch (cl) {
125	case TC_H_MIN(TC_H_MIN_INGRESS):
126		return &dev->ingress_cl_list;
127	case TC_H_MIN(TC_H_MIN_EGRESS):
128		return &dev->egress_cl_list;
129	default:
130		return NULL;
131	}
132}
133
134static int clsact_init(struct Qdisc *sch, struct nlattr *opt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136	net_inc_ingress_queue();
137	net_inc_egress_queue();
138
139	sch->flags |= TCQ_F_CPUSTATS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
141	return 0;
142}
143
144static void clsact_destroy(struct Qdisc *sch)
145{
 
146	struct net_device *dev = qdisc_dev(sch);
 
 
 
 
 
 
 
 
147
148	tcf_destroy_chain(&dev->ingress_cl_list);
149	tcf_destroy_chain(&dev->egress_cl_list);
 
 
 
 
 
 
 
 
 
 
 
 
 
150
151	net_dec_ingress_queue();
152	net_dec_egress_queue();
153}
154
155static const struct Qdisc_class_ops clsact_class_ops = {
 
156	.leaf		=	ingress_leaf,
157	.get		=	clsact_get,
158	.put		=	ingress_put,
159	.walk		=	ingress_walk,
160	.tcf_chain	=	clsact_find_tcf,
161	.bind_tcf	=	clsact_bind_filter,
162	.unbind_tcf	=	ingress_put,
163};
164
165static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
166	.cl_ops		=	&clsact_class_ops,
167	.id		=	"clsact",
168	.init		=	clsact_init,
169	.destroy	=	clsact_destroy,
170	.dump		=	ingress_dump,
171	.owner		=	THIS_MODULE,
 
 
 
 
 
 
172};
 
173
174static int __init ingress_module_init(void)
175{
176	int ret;
177
178	ret = register_qdisc(&ingress_qdisc_ops);
179	if (!ret) {
180		ret = register_qdisc(&clsact_qdisc_ops);
181		if (ret)
182			unregister_qdisc(&ingress_qdisc_ops);
183	}
184
185	return ret;
186}
187
188static void __exit ingress_module_exit(void)
189{
190	unregister_qdisc(&ingress_qdisc_ops);
191	unregister_qdisc(&clsact_qdisc_ops);
192}
193
194module_init(ingress_module_init);
195module_exit(ingress_module_exit);
196
197MODULE_ALIAS("sch_clsact");
198MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* net/sched/sch_ingress.c - Ingress and clsact qdisc
  3 *
 
 
 
 
 
  4 * Authors:     Jamal Hadi Salim 1999
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/types.h>
  9#include <linux/list.h>
 10#include <linux/skbuff.h>
 11#include <linux/rtnetlink.h>
 12
 13#include <net/netlink.h>
 14#include <net/pkt_sched.h>
 15#include <net/pkt_cls.h>
 16#include <net/tcx.h>
 17
 18struct ingress_sched_data {
 19	struct tcf_block *block;
 20	struct tcf_block_ext_info block_info;
 21	struct mini_Qdisc_pair miniqp;
 22};
 23
 24static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
 25{
 26	return NULL;
 27}
 28
 29static unsigned long ingress_find(struct Qdisc *sch, u32 classid)
 30{
 31	return TC_H_MIN(classid) + 1;
 32}
 33
 34static unsigned long ingress_bind_filter(struct Qdisc *sch,
 35					 unsigned long parent, u32 classid)
 36{
 37	return ingress_find(sch, classid);
 38}
 39
 40static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl)
 41{
 42}
 43
 44static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
 45{
 46}
 47
 48static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl,
 49					   struct netlink_ext_ack *extack)
 50{
 51	struct ingress_sched_data *q = qdisc_priv(sch);
 52
 53	return q->block;
 54}
 55
 56static void clsact_chain_head_change(struct tcf_proto *tp_head, void *priv)
 57{
 58	struct mini_Qdisc_pair *miniqp = priv;
 59
 60	mini_qdisc_pair_swap(miniqp, tp_head);
 61};
 62
 63static void ingress_ingress_block_set(struct Qdisc *sch, u32 block_index)
 64{
 65	struct ingress_sched_data *q = qdisc_priv(sch);
 66
 67	q->block_info.block_index = block_index;
 68}
 69
 70static u32 ingress_ingress_block_get(struct Qdisc *sch)
 71{
 72	struct ingress_sched_data *q = qdisc_priv(sch);
 73
 74	return q->block_info.block_index;
 75}
 76
 77static int ingress_init(struct Qdisc *sch, struct nlattr *opt,
 78			struct netlink_ext_ack *extack)
 79{
 80	struct ingress_sched_data *q = qdisc_priv(sch);
 81	struct net_device *dev = qdisc_dev(sch);
 82	struct bpf_mprog_entry *entry;
 83	bool created;
 84	int err;
 85
 86	if (sch->parent != TC_H_INGRESS)
 87		return -EOPNOTSUPP;
 88
 89	net_inc_ingress_queue();
 90
 91	entry = tcx_entry_fetch_or_create(dev, true, &created);
 92	if (!entry)
 93		return -ENOMEM;
 94	tcx_miniq_inc(entry);
 95	mini_qdisc_pair_init(&q->miniqp, sch, &tcx_entry(entry)->miniq);
 96	if (created)
 97		tcx_entry_update(dev, entry, true);
 98
 99	q->block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
100	q->block_info.chain_head_change = clsact_chain_head_change;
101	q->block_info.chain_head_change_priv = &q->miniqp;
102
103	err = tcf_block_get_ext(&q->block, sch, &q->block_info, extack);
104	if (err)
105		return err;
106
107	mini_qdisc_pair_block_init(&q->miniqp, q->block);
108
109	return 0;
110}
111
112static void ingress_destroy(struct Qdisc *sch)
113{
114	struct ingress_sched_data *q = qdisc_priv(sch);
115	struct net_device *dev = qdisc_dev(sch);
116	struct bpf_mprog_entry *entry = rtnl_dereference(dev->tcx_ingress);
117
118	if (sch->parent != TC_H_INGRESS)
119		return;
120
121	tcf_block_put_ext(q->block, sch, &q->block_info);
122
123	if (entry) {
124		tcx_miniq_dec(entry);
125		if (!tcx_entry_is_active(entry)) {
126			tcx_entry_update(dev, NULL, true);
127			tcx_entry_free(entry);
128		}
129	}
130
 
131	net_dec_ingress_queue();
132}
133
134static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
135{
136	struct nlattr *nest;
137
138	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
139	if (nest == NULL)
140		goto nla_put_failure;
141
142	return nla_nest_end(skb, nest);
143
144nla_put_failure:
145	nla_nest_cancel(skb, nest);
146	return -1;
147}
148
149static const struct Qdisc_class_ops ingress_class_ops = {
150	.flags		=	QDISC_CLASS_OPS_DOIT_UNLOCKED,
151	.leaf		=	ingress_leaf,
152	.find		=	ingress_find,
 
153	.walk		=	ingress_walk,
154	.tcf_block	=	ingress_tcf_block,
155	.bind_tcf	=	ingress_bind_filter,
156	.unbind_tcf	=	ingress_unbind_filter,
157};
158
159static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
160	.cl_ops			=	&ingress_class_ops,
161	.id			=	"ingress",
162	.priv_size		=	sizeof(struct ingress_sched_data),
163	.static_flags		=	TCQ_F_INGRESS | TCQ_F_CPUSTATS,
164	.init			=	ingress_init,
165	.destroy		=	ingress_destroy,
166	.dump			=	ingress_dump,
167	.ingress_block_set	=	ingress_ingress_block_set,
168	.ingress_block_get	=	ingress_ingress_block_get,
169	.owner			=	THIS_MODULE,
170};
171MODULE_ALIAS_NET_SCH("ingress");
172
173struct clsact_sched_data {
174	struct tcf_block *ingress_block;
175	struct tcf_block *egress_block;
176	struct tcf_block_ext_info ingress_block_info;
177	struct tcf_block_ext_info egress_block_info;
178	struct mini_Qdisc_pair miniqp_ingress;
179	struct mini_Qdisc_pair miniqp_egress;
180};
181
182static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
183{
184	switch (TC_H_MIN(classid)) {
185	case TC_H_MIN(TC_H_MIN_INGRESS):
186	case TC_H_MIN(TC_H_MIN_EGRESS):
187		return TC_H_MIN(classid);
188	default:
189		return 0;
190	}
191}
192
193static unsigned long clsact_bind_filter(struct Qdisc *sch,
194					unsigned long parent, u32 classid)
195{
196	return clsact_find(sch, classid);
197}
198
199static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl,
200					  struct netlink_ext_ack *extack)
201{
202	struct clsact_sched_data *q = qdisc_priv(sch);
203
204	switch (cl) {
205	case TC_H_MIN(TC_H_MIN_INGRESS):
206		return q->ingress_block;
207	case TC_H_MIN(TC_H_MIN_EGRESS):
208		return q->egress_block;
209	default:
210		return NULL;
211	}
212}
213
214static void clsact_ingress_block_set(struct Qdisc *sch, u32 block_index)
215{
216	struct clsact_sched_data *q = qdisc_priv(sch);
217
218	q->ingress_block_info.block_index = block_index;
219}
220
221static void clsact_egress_block_set(struct Qdisc *sch, u32 block_index)
222{
223	struct clsact_sched_data *q = qdisc_priv(sch);
224
225	q->egress_block_info.block_index = block_index;
226}
227
228static u32 clsact_ingress_block_get(struct Qdisc *sch)
229{
230	struct clsact_sched_data *q = qdisc_priv(sch);
231
232	return q->ingress_block_info.block_index;
233}
234
235static u32 clsact_egress_block_get(struct Qdisc *sch)
236{
237	struct clsact_sched_data *q = qdisc_priv(sch);
238
239	return q->egress_block_info.block_index;
240}
241
242static int clsact_init(struct Qdisc *sch, struct nlattr *opt,
243		       struct netlink_ext_ack *extack)
244{
245	struct clsact_sched_data *q = qdisc_priv(sch);
246	struct net_device *dev = qdisc_dev(sch);
247	struct bpf_mprog_entry *entry;
248	bool created;
249	int err;
250
251	if (sch->parent != TC_H_CLSACT)
252		return -EOPNOTSUPP;
253
254	net_inc_ingress_queue();
255	net_inc_egress_queue();
256
257	entry = tcx_entry_fetch_or_create(dev, true, &created);
258	if (!entry)
259		return -ENOMEM;
260	tcx_miniq_inc(entry);
261	mini_qdisc_pair_init(&q->miniqp_ingress, sch, &tcx_entry(entry)->miniq);
262	if (created)
263		tcx_entry_update(dev, entry, true);
264
265	q->ingress_block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
266	q->ingress_block_info.chain_head_change = clsact_chain_head_change;
267	q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
268
269	err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info,
270				extack);
271	if (err)
272		return err;
273
274	mini_qdisc_pair_block_init(&q->miniqp_ingress, q->ingress_block);
275
276	entry = tcx_entry_fetch_or_create(dev, false, &created);
277	if (!entry)
278		return -ENOMEM;
279	tcx_miniq_inc(entry);
280	mini_qdisc_pair_init(&q->miniqp_egress, sch, &tcx_entry(entry)->miniq);
281	if (created)
282		tcx_entry_update(dev, entry, false);
283
284	q->egress_block_info.binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
285	q->egress_block_info.chain_head_change = clsact_chain_head_change;
286	q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
287
288	return tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info, extack);
289}
290
291static void clsact_destroy(struct Qdisc *sch)
292{
293	struct clsact_sched_data *q = qdisc_priv(sch);
294	struct net_device *dev = qdisc_dev(sch);
295	struct bpf_mprog_entry *ingress_entry = rtnl_dereference(dev->tcx_ingress);
296	struct bpf_mprog_entry *egress_entry = rtnl_dereference(dev->tcx_egress);
297
298	if (sch->parent != TC_H_CLSACT)
299		return;
300
301	tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
302	tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
303
304	if (ingress_entry) {
305		tcx_miniq_dec(ingress_entry);
306		if (!tcx_entry_is_active(ingress_entry)) {
307			tcx_entry_update(dev, NULL, true);
308			tcx_entry_free(ingress_entry);
309		}
310	}
311
312	if (egress_entry) {
313		tcx_miniq_dec(egress_entry);
314		if (!tcx_entry_is_active(egress_entry)) {
315			tcx_entry_update(dev, NULL, false);
316			tcx_entry_free(egress_entry);
317		}
318	}
319
320	net_dec_ingress_queue();
321	net_dec_egress_queue();
322}
323
324static const struct Qdisc_class_ops clsact_class_ops = {
325	.flags		=	QDISC_CLASS_OPS_DOIT_UNLOCKED,
326	.leaf		=	ingress_leaf,
327	.find		=	clsact_find,
 
328	.walk		=	ingress_walk,
329	.tcf_block	=	clsact_tcf_block,
330	.bind_tcf	=	clsact_bind_filter,
331	.unbind_tcf	=	ingress_unbind_filter,
332};
333
334static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
335	.cl_ops			=	&clsact_class_ops,
336	.id			=	"clsact",
337	.priv_size		=	sizeof(struct clsact_sched_data),
338	.static_flags		=	TCQ_F_INGRESS | TCQ_F_CPUSTATS,
339	.init			=	clsact_init,
340	.destroy		=	clsact_destroy,
341	.dump			=	ingress_dump,
342	.ingress_block_set	=	clsact_ingress_block_set,
343	.egress_block_set	=	clsact_egress_block_set,
344	.ingress_block_get	=	clsact_ingress_block_get,
345	.egress_block_get	=	clsact_egress_block_get,
346	.owner			=	THIS_MODULE,
347};
348MODULE_ALIAS_NET_SCH("clsact");
349
350static int __init ingress_module_init(void)
351{
352	int ret;
353
354	ret = register_qdisc(&ingress_qdisc_ops);
355	if (!ret) {
356		ret = register_qdisc(&clsact_qdisc_ops);
357		if (ret)
358			unregister_qdisc(&ingress_qdisc_ops);
359	}
360
361	return ret;
362}
363
364static void __exit ingress_module_exit(void)
365{
366	unregister_qdisc(&ingress_qdisc_ops);
367	unregister_qdisc(&clsact_qdisc_ops);
368}
369
370module_init(ingress_module_init);
371module_exit(ingress_module_exit);
372
 
373MODULE_LICENSE("GPL");
374MODULE_DESCRIPTION("Ingress and clsact based ingress and egress qdiscs");