1 /* net/sched/sch_ingress.c - Ingress and clsact qdisc 2 * 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 11 #include <linux/module.h> 12 #include <linux/types.h> 13 #include <linux/list.h> 14 #include <linux/skbuff.h> 15 #include <linux/rtnetlink.h> 16 17 #include <net/netlink.h> 18 #include <net/pkt_sched.h> 19 #include <net/pkt_cls.h> 20 21 struct ingress_sched_data { 22 struct tcf_block *block; 23 struct tcf_block_ext_info block_info; 24 }; 25 26 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg) 27 { 28 return NULL; 29 } 30 31 static unsigned long ingress_find(struct Qdisc *sch, u32 classid) 32 { 33 return TC_H_MIN(classid) + 1; 34 } 35 36 static unsigned long ingress_bind_filter(struct Qdisc *sch, 37 unsigned long parent, u32 classid) 38 { 39 return ingress_find(sch, classid); 40 } 41 42 static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl) 43 { 44 } 45 46 static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker) 47 { 48 } 49 50 static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl) 51 { 52 struct ingress_sched_data *q = qdisc_priv(sch); 53 54 return q->block; 55 } 56 57 static int ingress_init(struct Qdisc *sch, struct nlattr *opt) 58 { 59 struct ingress_sched_data *q = qdisc_priv(sch); 60 struct net_device *dev = qdisc_dev(sch); 61 int err; 62 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); 67 if (err) 68 return err; 69 70 net_inc_ingress_queue(); 71 sch->flags |= TCQ_F_CPUSTATS; 72 73 return 0; 74 } 75 76 static void ingress_destroy(struct Qdisc *sch) 77 { 78 struct ingress_sched_data *q = qdisc_priv(sch); 79 struct net_device *dev = qdisc_dev(sch); 80 81 tcf_block_put_ext(q->block, &dev->ingress_cl_list, 82 sch, &q->block_info); 83 net_dec_ingress_queue(); 84 } 85 86 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) 87 { 88 struct nlattr *nest; 89 90 nest = nla_nest_start(skb, TCA_OPTIONS); 91 if (nest == NULL) 92 goto nla_put_failure; 93 94 return nla_nest_end(skb, nest); 95 96 nla_put_failure: 97 nla_nest_cancel(skb, nest); 98 return -1; 99 } 100 101 static const struct Qdisc_class_ops ingress_class_ops = { 102 .leaf = ingress_leaf, 103 .find = ingress_find, 104 .walk = ingress_walk, 105 .tcf_block = ingress_tcf_block, 106 .bind_tcf = ingress_bind_filter, 107 .unbind_tcf = ingress_unbind_filter, 108 }; 109 110 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { 111 .cl_ops = &ingress_class_ops, 112 .id = "ingress", 113 .priv_size = sizeof(struct ingress_sched_data), 114 .init = ingress_init, 115 .destroy = ingress_destroy, 116 .dump = ingress_dump, 117 .owner = THIS_MODULE, 118 }; 119 120 struct clsact_sched_data { 121 struct tcf_block *ingress_block; 122 struct tcf_block *egress_block; 123 struct tcf_block_ext_info ingress_block_info; 124 struct tcf_block_ext_info egress_block_info; 125 }; 126 127 static unsigned long clsact_find(struct Qdisc *sch, u32 classid) 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 { 141 return clsact_find(sch, classid); 142 } 143 144 static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl) 145 { 146 struct clsact_sched_data *q = qdisc_priv(sch); 147 148 switch (cl) { 149 case TC_H_MIN(TC_H_MIN_INGRESS): 150 return q->ingress_block; 151 case TC_H_MIN(TC_H_MIN_EGRESS): 152 return q->egress_block; 153 default: 154 return NULL; 155 } 156 } 157 158 static int clsact_init(struct Qdisc *sch, struct nlattr *opt) 159 { 160 struct clsact_sched_data *q = qdisc_priv(sch); 161 struct net_device *dev = qdisc_dev(sch); 162 int err; 163 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); 168 if (err) 169 return err; 170 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); 175 if (err) 176 goto err_egress_block_get; 177 178 net_inc_ingress_queue(); 179 net_inc_egress_queue(); 180 181 sch->flags |= TCQ_F_CPUSTATS; 182 183 return 0; 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; 189 } 190 191 static void clsact_destroy(struct Qdisc *sch) 192 { 193 struct clsact_sched_data *q = qdisc_priv(sch); 194 struct net_device *dev = qdisc_dev(sch); 195 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); 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, 207 .find = clsact_find, 208 .walk = ingress_walk, 209 .tcf_block = clsact_tcf_block, 210 .bind_tcf = clsact_bind_filter, 211 .unbind_tcf = ingress_unbind_filter, 212 }; 213 214 static struct Qdisc_ops clsact_qdisc_ops __read_mostly = { 215 .cl_ops = &clsact_class_ops, 216 .id = "clsact", 217 .priv_size = sizeof(struct clsact_sched_data), 218 .init = clsact_init, 219 .destroy = clsact_destroy, 220 .dump = ingress_dump, 221 .owner = THIS_MODULE, 222 }; 223 224 static int __init ingress_module_init(void) 225 { 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; 236 } 237 238 static void __exit ingress_module_exit(void) 239 { 240 unregister_qdisc(&ingress_qdisc_ops); 241 unregister_qdisc(&clsact_qdisc_ops); 242 } 243 244 module_init(ingress_module_init); 245 module_exit(ingress_module_exit); 246 247 MODULE_ALIAS("sch_clsact"); 248 MODULE_LICENSE("GPL"); 249