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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg) |
| 22 | { |
| 23 | return NULL; |
| 24 | } |
| 25 | |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 26 | static unsigned long ingress_get(struct Qdisc *sch, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | return TC_H_MIN(classid) + 1; |
| 29 | } |
| 30 | |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 31 | static bool ingress_cl_offload(u32 classid) |
| 32 | { |
| 33 | return true; |
| 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 | { |
| 39 | return ingress_get(sch, classid); |
| 40 | } |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static void ingress_put(struct Qdisc *sch, unsigned long cl) |
| 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 | |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 50 | static struct tcf_proto __rcu **ingress_find_tcf(struct Qdisc *sch, |
| 51 | unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 53 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 55 | return &dev->ingress_cl_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 58 | static int ingress_init(struct Qdisc *sch, struct nlattr *opt) |
| 59 | { |
| 60 | net_inc_ingress_queue(); |
Alexei Starovoitov | 087c1a6 | 2015-04-30 20:14:07 -0700 | [diff] [blame] | 61 | sch->flags |= TCQ_F_CPUSTATS; |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static void ingress_destroy(struct Qdisc *sch) |
| 67 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 68 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 70 | tcf_destroy_chain(&dev->ingress_cl_list); |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 71 | net_dec_ingress_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 75 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 76 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 78 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 79 | if (nest == NULL) |
| 80 | goto nla_put_failure; |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 81 | |
Yang Yingliang | d59b7d8 | 2014-03-12 10:20:32 +0800 | [diff] [blame] | 82 | return nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 84 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 85 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 89 | static const struct Qdisc_class_ops ingress_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | .leaf = ingress_leaf, |
| 91 | .get = ingress_get, |
| 92 | .put = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .walk = ingress_walk, |
| 94 | .tcf_chain = ingress_find_tcf, |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 95 | .tcf_cl_offload = ingress_cl_offload, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | .bind_tcf = ingress_bind_filter, |
| 97 | .unbind_tcf = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 100 | static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .cl_ops = &ingress_class_ops, |
| 102 | .id = "ingress", |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 103 | .init = ingress_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | .destroy = ingress_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .dump = ingress_dump, |
| 106 | .owner = THIS_MODULE, |
| 107 | }; |
| 108 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 109 | static 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 Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 120 | static bool clsact_cl_offload(u32 classid) |
| 121 | { |
| 122 | return TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS); |
| 123 | } |
| 124 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 125 | static unsigned long clsact_bind_filter(struct Qdisc *sch, |
| 126 | unsigned long parent, u32 classid) |
| 127 | { |
| 128 | return clsact_get(sch, classid); |
| 129 | } |
| 130 | |
| 131 | static 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 | |
| 146 | static 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 | |
| 156 | static 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 | |
| 167 | static 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 Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 173 | .tcf_cl_offload = clsact_cl_offload, |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 174 | .bind_tcf = clsact_bind_filter, |
| 175 | .unbind_tcf = ingress_put, |
| 176 | }; |
| 177 | |
| 178 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static int __init ingress_module_init(void) |
| 188 | { |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 189 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 200 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 201 | static void __exit ingress_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | unregister_qdisc(&ingress_qdisc_ops); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 204 | unregister_qdisc(&clsact_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 206 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 207 | module_init(ingress_module_init); |
| 208 | module_exit(ingress_module_exit); |
| 209 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 210 | MODULE_ALIAS("sch_clsact"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | MODULE_LICENSE("GPL"); |