Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 1 | /* net/sched/sch_ingress.c - Ingress and clsact qdisc |
| 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
Patrick McHardy | 0ba4805 | 2007-07-02 22:49:07 -0700 | [diff] [blame] | 13 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/rtnetlink.h> |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 17 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/pkt_sched.h> |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 19 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 21 | struct ingress_sched_data { |
| 22 | struct tcf_block *block; |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 23 | struct tcf_block_ext_info block_info; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg) |
| 27 | { |
| 28 | return NULL; |
| 29 | } |
| 30 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 31 | static unsigned long ingress_find(struct Qdisc *sch, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return TC_H_MIN(classid) + 1; |
| 34 | } |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static unsigned long ingress_bind_filter(struct Qdisc *sch, |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 37 | unsigned long parent, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 39 | return ingress_find(sch, classid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 42 | static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 46 | static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 50 | static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 52 | struct ingress_sched_data *q = qdisc_priv(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 54 | return q->block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 57 | static int ingress_init(struct Qdisc *sch, struct nlattr *opt) |
| 58 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 59 | struct ingress_sched_data *q = qdisc_priv(sch); |
| 60 | struct net_device *dev = qdisc_dev(sch); |
| 61 | int err; |
| 62 | |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 63 | q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS; |
| 64 | |
| 65 | err = tcf_block_get_ext(&q->block, &dev->ingress_cl_list, |
| 66 | sch, &q->block_info); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 67 | if (err) |
| 68 | return err; |
| 69 | |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 70 | net_inc_ingress_queue(); |
Alexei Starovoitov | 087c1a6 | 2015-04-30 20:14:07 -0700 | [diff] [blame] | 71 | sch->flags |= TCQ_F_CPUSTATS; |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | static void ingress_destroy(struct Qdisc *sch) |
| 77 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 78 | struct ingress_sched_data *q = qdisc_priv(sch); |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 79 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 81 | tcf_block_put_ext(q->block, &dev->ingress_cl_list, |
| 82 | sch, &q->block_info); |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 83 | net_dec_ingress_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 87 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 88 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 90 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 91 | if (nest == NULL) |
| 92 | goto nla_put_failure; |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 93 | |
Yang Yingliang | d59b7d8 | 2014-03-12 10:20:32 +0800 | [diff] [blame] | 94 | return nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 96 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 97 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return -1; |
| 99 | } |
| 100 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 101 | static const struct Qdisc_class_ops ingress_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | .leaf = ingress_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 103 | .find = ingress_find, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | .walk = ingress_walk, |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 105 | .tcf_block = ingress_tcf_block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .bind_tcf = ingress_bind_filter, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 107 | .unbind_tcf = ingress_unbind_filter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 110 | static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .cl_ops = &ingress_class_ops, |
| 112 | .id = "ingress", |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 113 | .priv_size = sizeof(struct ingress_sched_data), |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 114 | .init = ingress_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .destroy = ingress_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .dump = ingress_dump, |
| 117 | .owner = THIS_MODULE, |
| 118 | }; |
| 119 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 120 | struct clsact_sched_data { |
| 121 | struct tcf_block *ingress_block; |
| 122 | struct tcf_block *egress_block; |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 123 | struct tcf_block_ext_info ingress_block_info; |
| 124 | struct tcf_block_ext_info egress_block_info; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 127 | static unsigned long clsact_find(struct Qdisc *sch, u32 classid) |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 128 | { |
| 129 | switch (TC_H_MIN(classid)) { |
| 130 | case TC_H_MIN(TC_H_MIN_INGRESS): |
| 131 | case TC_H_MIN(TC_H_MIN_EGRESS): |
| 132 | return TC_H_MIN(classid); |
| 133 | default: |
| 134 | return 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static unsigned long clsact_bind_filter(struct Qdisc *sch, |
| 139 | unsigned long parent, u32 classid) |
| 140 | { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 141 | return clsact_find(sch, classid); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 144 | static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl) |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 145 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 146 | struct clsact_sched_data *q = qdisc_priv(sch); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 147 | |
| 148 | switch (cl) { |
| 149 | case TC_H_MIN(TC_H_MIN_INGRESS): |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 150 | return q->ingress_block; |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 151 | case TC_H_MIN(TC_H_MIN_EGRESS): |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 152 | return q->egress_block; |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 153 | default: |
| 154 | return NULL; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static int clsact_init(struct Qdisc *sch, struct nlattr *opt) |
| 159 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 160 | struct clsact_sched_data *q = qdisc_priv(sch); |
| 161 | struct net_device *dev = qdisc_dev(sch); |
| 162 | int err; |
| 163 | |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 164 | q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS; |
| 165 | |
| 166 | err = tcf_block_get_ext(&q->ingress_block, &dev->ingress_cl_list, |
| 167 | sch, &q->ingress_block_info); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 168 | if (err) |
| 169 | return err; |
| 170 | |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 171 | q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS; |
| 172 | |
| 173 | err = tcf_block_get_ext(&q->egress_block, &dev->egress_cl_list, |
| 174 | sch, &q->egress_block_info); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 175 | if (err) |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 176 | goto err_egress_block_get; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 177 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 178 | net_inc_ingress_queue(); |
| 179 | net_inc_egress_queue(); |
| 180 | |
| 181 | sch->flags |= TCQ_F_CPUSTATS; |
| 182 | |
| 183 | return 0; |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 184 | |
| 185 | err_egress_block_get: |
| 186 | tcf_block_put_ext(q->ingress_block, &dev->ingress_cl_list, |
| 187 | sch, &q->ingress_block_info); |
| 188 | return err; |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void clsact_destroy(struct Qdisc *sch) |
| 192 | { |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 193 | struct clsact_sched_data *q = qdisc_priv(sch); |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 194 | struct net_device *dev = qdisc_dev(sch); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 195 | |
Jiri Pirko | 6e40cf2 | 2017-10-19 15:50:30 +0200 | [diff] [blame^] | 196 | tcf_block_put_ext(q->egress_block, &dev->egress_cl_list, |
| 197 | sch, &q->egress_block_info); |
| 198 | tcf_block_put_ext(q->ingress_block, &dev->ingress_cl_list, |
| 199 | sch, &q->ingress_block_info); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 200 | |
| 201 | net_dec_ingress_queue(); |
| 202 | net_dec_egress_queue(); |
| 203 | } |
| 204 | |
| 205 | static const struct Qdisc_class_ops clsact_class_ops = { |
| 206 | .leaf = ingress_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 207 | .find = clsact_find, |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 208 | .walk = ingress_walk, |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 209 | .tcf_block = clsact_tcf_block, |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 210 | .bind_tcf = clsact_bind_filter, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 211 | .unbind_tcf = ingress_unbind_filter, |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static struct Qdisc_ops clsact_qdisc_ops __read_mostly = { |
| 215 | .cl_ops = &clsact_class_ops, |
| 216 | .id = "clsact", |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 217 | .priv_size = sizeof(struct clsact_sched_data), |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 218 | .init = clsact_init, |
| 219 | .destroy = clsact_destroy, |
| 220 | .dump = ingress_dump, |
| 221 | .owner = THIS_MODULE, |
| 222 | }; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static int __init ingress_module_init(void) |
| 225 | { |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 226 | int ret; |
| 227 | |
| 228 | ret = register_qdisc(&ingress_qdisc_ops); |
| 229 | if (!ret) { |
| 230 | ret = register_qdisc(&clsact_qdisc_ops); |
| 231 | if (ret) |
| 232 | unregister_qdisc(&ingress_qdisc_ops); |
| 233 | } |
| 234 | |
| 235 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 237 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 238 | static void __exit ingress_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | unregister_qdisc(&ingress_qdisc_ops); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 241 | unregister_qdisc(&clsact_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 243 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 244 | module_init(ingress_module_init); |
| 245 | module_exit(ingress_module_exit); |
| 246 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 247 | MODULE_ALIAS("sch_clsact"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | MODULE_LICENSE("GPL"); |