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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static unsigned long ingress_bind_filter(struct Qdisc *sch, |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 31 | unsigned long parent, u32 classid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | return ingress_get(sch, classid); |
| 34 | } |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static void ingress_put(struct Qdisc *sch, unsigned long cl) |
| 37 | { |
| 38 | } |
| 39 | |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 40 | static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 44 | static struct tcf_proto __rcu **ingress_find_tcf(struct Qdisc *sch, |
| 45 | unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 47 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 49 | return &dev->ingress_cl_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 52 | static int ingress_init(struct Qdisc *sch, struct nlattr *opt) |
| 53 | { |
| 54 | net_inc_ingress_queue(); |
Alexei Starovoitov | 087c1a6 | 2015-04-30 20:14:07 -0700 | [diff] [blame] | 55 | sch->flags |= TCQ_F_CPUSTATS; |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static void ingress_destroy(struct Qdisc *sch) |
| 61 | { |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 62 | struct net_device *dev = qdisc_dev(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 64 | tcf_destroy_chain(&dev->ingress_cl_list); |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 65 | net_dec_ingress_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 69 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 70 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 72 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 73 | if (nest == NULL) |
| 74 | goto nla_put_failure; |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 75 | |
Yang Yingliang | d59b7d8 | 2014-03-12 10:20:32 +0800 | [diff] [blame] | 76 | return nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 78 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 79 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 83 | static const struct Qdisc_class_ops ingress_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | .leaf = ingress_leaf, |
| 85 | .get = ingress_get, |
| 86 | .put = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | .walk = ingress_walk, |
| 88 | .tcf_chain = ingress_find_tcf, |
| 89 | .bind_tcf = ingress_bind_filter, |
| 90 | .unbind_tcf = ingress_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 93 | static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .cl_ops = &ingress_class_ops, |
| 95 | .id = "ingress", |
Daniel Borkmann | 4577139 | 2015-04-10 23:07:54 +0200 | [diff] [blame] | 96 | .init = ingress_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | .destroy = ingress_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | .dump = ingress_dump, |
| 99 | .owner = THIS_MODULE, |
| 100 | }; |
| 101 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 102 | static 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 | |
| 113 | static unsigned long clsact_bind_filter(struct Qdisc *sch, |
| 114 | unsigned long parent, u32 classid) |
| 115 | { |
| 116 | return clsact_get(sch, classid); |
| 117 | } |
| 118 | |
| 119 | static 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 | |
| 134 | static 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 | |
| 144 | static 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 | |
| 155 | static 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 | |
| 165 | static 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | static int __init ingress_module_init(void) |
| 175 | { |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 187 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 188 | static void __exit ingress_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | unregister_qdisc(&ingress_qdisc_ops); |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 191 | unregister_qdisc(&clsact_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Patrick McHardy | 58f4df4 | 2008-01-21 00:11:01 -0800 | [diff] [blame] | 193 | |
Daniel Borkmann | d2788d3 | 2015-05-09 22:51:32 +0200 | [diff] [blame] | 194 | module_init(ingress_module_init); |
| 195 | module_exit(ingress_module_exit); |
| 196 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 197 | MODULE_ALIAS("sch_clsact"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | MODULE_LICENSE("GPL"); |