blob: 3bab5f66c39291fb4f20888dbdb779dc7edc2776 [file] [log] [blame]
Daniel Borkmann1f211a12016-01-07 22:29:47 +01001/* net/sched/sch_ingress.c - Ingress and clsact qdisc
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070013#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/rtnetlink.h>
Daniel Borkmannd2788d32015-05-09 22:51:32 +020016
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070017#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010019#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
22{
23 return NULL;
24}
25
Patrick McHardy58f4df42008-01-21 00:11:01 -080026static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return TC_H_MIN(classid) + 1;
29}
30
Daniel Borkmann92c075d2016-06-06 22:50:39 +020031static bool ingress_cl_offload(u32 classid)
32{
33 return true;
34}
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080037 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 return ingress_get(sch, classid);
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void ingress_put(struct Qdisc *sch, unsigned long cl)
43{
44}
45
Patrick McHardy58f4df42008-01-21 00:11:01 -080046static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
John Fastabend25d8c0d2014-09-12 20:05:27 -070050static struct tcf_proto __rcu **ingress_find_tcf(struct Qdisc *sch,
51 unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Daniel Borkmannd2788d32015-05-09 22:51:32 +020053 struct net_device *dev = qdisc_dev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Daniel Borkmannd2788d32015-05-09 22:51:32 +020055 return &dev->ingress_cl_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Daniel Borkmann45771392015-04-10 23:07:54 +020058static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
59{
60 net_inc_ingress_queue();
Alexei Starovoitov087c1a62015-04-30 20:14:07 -070061 sch->flags |= TCQ_F_CPUSTATS;
Daniel Borkmann45771392015-04-10 23:07:54 +020062
63 return 0;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ingress_destroy(struct Qdisc *sch)
67{
Daniel Borkmannd2788d32015-05-09 22:51:32 +020068 struct net_device *dev = qdisc_dev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Daniel Borkmannd2788d32015-05-09 22:51:32 +020070 tcf_destroy_chain(&dev->ingress_cl_list);
Daniel Borkmann45771392015-04-10 23:07:54 +020071 net_dec_ingress_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
75{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080076 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080078 nest = nla_nest_start(skb, TCA_OPTIONS);
79 if (nest == NULL)
80 goto nla_put_failure;
Daniel Borkmannd2788d32015-05-09 22:51:32 +020081
Yang Yingliangd59b7d82014-03-12 10:20:32 +080082 return nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Patrick McHardy1e904742008-01-22 22:11:17 -080084nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080085 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -1;
87}
88
Eric Dumazet20fea082007-11-14 01:44:41 -080089static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .leaf = ingress_leaf,
91 .get = ingress_get,
92 .put = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .walk = ingress_walk,
94 .tcf_chain = ingress_find_tcf,
Daniel Borkmann92c075d2016-06-06 22:50:39 +020095 .tcf_cl_offload = ingress_cl_offload,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .bind_tcf = ingress_bind_filter,
97 .unbind_tcf = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric Dumazet20fea082007-11-14 01:44:41 -0800100static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .cl_ops = &ingress_class_ops,
102 .id = "ingress",
Daniel Borkmann45771392015-04-10 23:07:54 +0200103 .init = ingress_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .dump = ingress_dump,
106 .owner = THIS_MODULE,
107};
108
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100109static unsigned long clsact_get(struct Qdisc *sch, u32 classid)
110{
111 switch (TC_H_MIN(classid)) {
112 case TC_H_MIN(TC_H_MIN_INGRESS):
113 case TC_H_MIN(TC_H_MIN_EGRESS):
114 return TC_H_MIN(classid);
115 default:
116 return 0;
117 }
118}
119
Daniel Borkmann92c075d2016-06-06 22:50:39 +0200120static bool clsact_cl_offload(u32 classid)
121{
122 return TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS);
123}
124
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100125static unsigned long clsact_bind_filter(struct Qdisc *sch,
126 unsigned long parent, u32 classid)
127{
128 return clsact_get(sch, classid);
129}
130
131static struct tcf_proto __rcu **clsact_find_tcf(struct Qdisc *sch,
132 unsigned long cl)
133{
134 struct net_device *dev = qdisc_dev(sch);
135
136 switch (cl) {
137 case TC_H_MIN(TC_H_MIN_INGRESS):
138 return &dev->ingress_cl_list;
139 case TC_H_MIN(TC_H_MIN_EGRESS):
140 return &dev->egress_cl_list;
141 default:
142 return NULL;
143 }
144}
145
146static int clsact_init(struct Qdisc *sch, struct nlattr *opt)
147{
148 net_inc_ingress_queue();
149 net_inc_egress_queue();
150
151 sch->flags |= TCQ_F_CPUSTATS;
152
153 return 0;
154}
155
156static void clsact_destroy(struct Qdisc *sch)
157{
158 struct net_device *dev = qdisc_dev(sch);
159
160 tcf_destroy_chain(&dev->ingress_cl_list);
161 tcf_destroy_chain(&dev->egress_cl_list);
162
163 net_dec_ingress_queue();
164 net_dec_egress_queue();
165}
166
167static const struct Qdisc_class_ops clsact_class_ops = {
168 .leaf = ingress_leaf,
169 .get = clsact_get,
170 .put = ingress_put,
171 .walk = ingress_walk,
172 .tcf_chain = clsact_find_tcf,
Daniel Borkmann92c075d2016-06-06 22:50:39 +0200173 .tcf_cl_offload = clsact_cl_offload,
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100174 .bind_tcf = clsact_bind_filter,
175 .unbind_tcf = ingress_put,
176};
177
178static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
179 .cl_ops = &clsact_class_ops,
180 .id = "clsact",
181 .init = clsact_init,
182 .destroy = clsact_destroy,
183 .dump = ingress_dump,
184 .owner = THIS_MODULE,
185};
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int __init ingress_module_init(void)
188{
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100189 int ret;
190
191 ret = register_qdisc(&ingress_qdisc_ops);
192 if (!ret) {
193 ret = register_qdisc(&clsact_qdisc_ops);
194 if (ret)
195 unregister_qdisc(&ingress_qdisc_ops);
196 }
197
198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800200
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900201static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 unregister_qdisc(&ingress_qdisc_ops);
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100204 unregister_qdisc(&clsact_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800206
Daniel Borkmannd2788d32015-05-09 22:51:32 +0200207module_init(ingress_module_init);
208module_exit(ingress_module_exit);
209
Daniel Borkmann1f211a12016-01-07 22:29:47 +0100210MODULE_ALIAS("sch_clsact");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211MODULE_LICENSE("GPL");