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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg) |
| 21 | { |
| 22 | return NULL; |
| 23 | } |
| 24 | |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 25 | static unsigned long ingress_get(struct Qdisc *sch, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | return TC_H_MIN(classid) + 1; |
| 28 | } |
| 29 | |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 30 | static bool ingress_cl_offload(u32 classid) |
| 31 | { |
| 32 | return true; |
| 33 | } |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static unsigned long ingress_bind_filter(struct Qdisc *sch, |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 36 | unsigned long parent, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | return ingress_get(sch, classid); |
| 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static void ingress_put(struct Qdisc *sch, unsigned long cl) |
| 42 | { |
| 43 | } |
| 44 | |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 45 | static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 49 | static struct tcf_proto __rcu **ingress_find_tcf(struct Qdisc *sch, |
| 50 | unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 52 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 54 | return &dev->ingress_cl_list; |
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 | { |
| 59 | net_inc_ingress_queue(); |
Alexei Starovoitov | 087c1a6 | 2015-04-30 20:14:07 -0700 | [diff] [blame] | 60 | sch->flags |= TCQ_F_CPUSTATS; |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static void ingress_destroy(struct Qdisc *sch) |
| 66 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 67 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 69 | tcf_destroy_chain(&dev->ingress_cl_list); |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 70 | net_dec_ingress_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 74 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 75 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 77 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 78 | if (nest == NULL) |
| 79 | goto nla_put_failure; |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 80 | |
Yang Yingliang | d59b7d8 | 2014-03-12 10:20:32 +0800 | [diff] [blame] | 81 | return nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 83 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 84 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 88 | static const struct Qdisc_class_ops ingress_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .leaf = ingress_leaf, |
| 90 | .get = ingress_get, |
| 91 | .put = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | .walk = ingress_walk, |
| 93 | .tcf_chain = ingress_find_tcf, |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 94 | .tcf_cl_offload = ingress_cl_offload, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | .bind_tcf = ingress_bind_filter, |
| 96 | .unbind_tcf = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 99 | static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .cl_ops = &ingress_class_ops, |
| 101 | .id = "ingress", |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 102 | .init = ingress_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .destroy = ingress_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | .dump = ingress_dump, |
| 105 | .owner = THIS_MODULE, |
| 106 | }; |
| 107 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 108 | static unsigned long clsact_get(struct Qdisc *sch, u32 classid) |
| 109 | { |
| 110 | switch (TC_H_MIN(classid)) { |
| 111 | case TC_H_MIN(TC_H_MIN_INGRESS): |
| 112 | case TC_H_MIN(TC_H_MIN_EGRESS): |
| 113 | return TC_H_MIN(classid); |
| 114 | default: |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 119 | static bool clsact_cl_offload(u32 classid) |
| 120 | { |
| 121 | return TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS); |
| 122 | } |
| 123 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 124 | static unsigned long clsact_bind_filter(struct Qdisc *sch, |
| 125 | unsigned long parent, u32 classid) |
| 126 | { |
| 127 | return clsact_get(sch, classid); |
| 128 | } |
| 129 | |
| 130 | static struct tcf_proto __rcu **clsact_find_tcf(struct Qdisc *sch, |
| 131 | unsigned long cl) |
| 132 | { |
| 133 | struct net_device *dev = qdisc_dev(sch); |
| 134 | |
| 135 | switch (cl) { |
| 136 | case TC_H_MIN(TC_H_MIN_INGRESS): |
| 137 | return &dev->ingress_cl_list; |
| 138 | case TC_H_MIN(TC_H_MIN_EGRESS): |
| 139 | return &dev->egress_cl_list; |
| 140 | default: |
| 141 | return NULL; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static int clsact_init(struct Qdisc *sch, struct nlattr *opt) |
| 146 | { |
| 147 | net_inc_ingress_queue(); |
| 148 | net_inc_egress_queue(); |
| 149 | |
| 150 | sch->flags |= TCQ_F_CPUSTATS; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static void clsact_destroy(struct Qdisc *sch) |
| 156 | { |
| 157 | struct net_device *dev = qdisc_dev(sch); |
| 158 | |
| 159 | tcf_destroy_chain(&dev->ingress_cl_list); |
| 160 | tcf_destroy_chain(&dev->egress_cl_list); |
| 161 | |
| 162 | net_dec_ingress_queue(); |
| 163 | net_dec_egress_queue(); |
| 164 | } |
| 165 | |
| 166 | static const struct Qdisc_class_ops clsact_class_ops = { |
| 167 | .leaf = ingress_leaf, |
| 168 | .get = clsact_get, |
| 169 | .put = ingress_put, |
| 170 | .walk = ingress_walk, |
| 171 | .tcf_chain = clsact_find_tcf, |
Daniel Borkmann | 92c075d | 2016-06-06 22:50:39 +0200 | [diff] [blame] | 172 | .tcf_cl_offload = clsact_cl_offload, |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 173 | .bind_tcf = clsact_bind_filter, |
| 174 | .unbind_tcf = ingress_put, |
| 175 | }; |
| 176 | |
| 177 | static struct Qdisc_ops clsact_qdisc_ops __read_mostly = { |
| 178 | .cl_ops = &clsact_class_ops, |
| 179 | .id = "clsact", |
| 180 | .init = clsact_init, |
| 181 | .destroy = clsact_destroy, |
| 182 | .dump = ingress_dump, |
| 183 | .owner = THIS_MODULE, |
| 184 | }; |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | static int __init ingress_module_init(void) |
| 187 | { |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | ret = register_qdisc(&ingress_qdisc_ops); |
| 191 | if (!ret) { |
| 192 | ret = register_qdisc(&clsact_qdisc_ops); |
| 193 | if (ret) |
| 194 | unregister_qdisc(&ingress_qdisc_ops); |
| 195 | } |
| 196 | |
| 197 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 199 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 200 | static void __exit ingress_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | unregister_qdisc(&ingress_qdisc_ops); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 203 | unregister_qdisc(&clsact_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 205 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 206 | module_init(ingress_module_init); |
| 207 | module_exit(ingress_module_exit); |
| 208 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 209 | MODULE_ALIAS("sch_clsact"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | MODULE_LICENSE("GPL"); |