1 /* 2 * net/sched/cls_matchll.c Match-all classifier 3 * 4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/init.h> 14 #include <linux/module.h> 15 16 #include <net/sch_generic.h> 17 #include <net/pkt_cls.h> 18 19 struct cls_mall_head { 20 struct tcf_exts exts; 21 struct tcf_result res; 22 u32 handle; 23 u32 flags; 24 struct rcu_head rcu; 25 }; 26 27 static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp, 28 struct tcf_result *res) 29 { 30 struct cls_mall_head *head = rcu_dereference_bh(tp->root); 31 32 if (tc_skip_sw(head->flags)) 33 return -1; 34 35 *res = head->res; 36 return tcf_exts_exec(skb, &head->exts, res); 37 } 38 39 static int mall_init(struct tcf_proto *tp) 40 { 41 return 0; 42 } 43 44 static void mall_destroy_rcu(struct rcu_head *rcu) 45 { 46 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head, 47 rcu); 48 49 tcf_exts_destroy(&head->exts); 50 kfree(head); 51 } 52 53 static void mall_destroy_hw_filter(struct tcf_proto *tp, 54 struct cls_mall_head *head, 55 unsigned long cookie) 56 { 57 struct tc_cls_matchall_offload cls_mall = {}; 58 struct tcf_block *block = tp->chain->block; 59 60 tc_cls_common_offload_init(&cls_mall.common, tp); 61 cls_mall.command = TC_CLSMATCHALL_DESTROY; 62 cls_mall.cookie = cookie; 63 64 tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL, &cls_mall, false); 65 } 66 67 static int mall_replace_hw_filter(struct tcf_proto *tp, 68 struct cls_mall_head *head, 69 unsigned long cookie) 70 { 71 struct tc_cls_matchall_offload cls_mall = {}; 72 struct tcf_block *block = tp->chain->block; 73 bool skip_sw = tc_skip_sw(head->flags); 74 int err; 75 76 tc_cls_common_offload_init(&cls_mall.common, tp); 77 cls_mall.command = TC_CLSMATCHALL_REPLACE; 78 cls_mall.exts = &head->exts; 79 cls_mall.cookie = cookie; 80 81 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL, 82 &cls_mall, skip_sw); 83 if (err < 0) { 84 mall_destroy_hw_filter(tp, head, cookie); 85 return err; 86 } else if (err > 0) { 87 head->flags |= TCA_CLS_FLAGS_IN_HW; 88 } 89 90 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) 91 return -EINVAL; 92 93 return 0; 94 } 95 96 static void mall_destroy(struct tcf_proto *tp) 97 { 98 struct cls_mall_head *head = rtnl_dereference(tp->root); 99 100 if (!head) 101 return; 102 103 if (!tc_skip_hw(head->flags)) 104 mall_destroy_hw_filter(tp, head, (unsigned long) head); 105 106 call_rcu(&head->rcu, mall_destroy_rcu); 107 } 108 109 static void *mall_get(struct tcf_proto *tp, u32 handle) 110 { 111 return NULL; 112 } 113 114 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = { 115 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC }, 116 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 }, 117 }; 118 119 static int mall_set_parms(struct net *net, struct tcf_proto *tp, 120 struct cls_mall_head *head, 121 unsigned long base, struct nlattr **tb, 122 struct nlattr *est, bool ovr) 123 { 124 int err; 125 126 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr); 127 if (err < 0) 128 return err; 129 130 if (tb[TCA_MATCHALL_CLASSID]) { 131 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); 132 tcf_bind_filter(tp, &head->res, base); 133 } 134 return 0; 135 } 136 137 static int mall_change(struct net *net, struct sk_buff *in_skb, 138 struct tcf_proto *tp, unsigned long base, 139 u32 handle, struct nlattr **tca, 140 void **arg, bool ovr) 141 { 142 struct cls_mall_head *head = rtnl_dereference(tp->root); 143 struct nlattr *tb[TCA_MATCHALL_MAX + 1]; 144 struct cls_mall_head *new; 145 u32 flags = 0; 146 int err; 147 148 if (!tca[TCA_OPTIONS]) 149 return -EINVAL; 150 151 if (head) 152 return -EEXIST; 153 154 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS], 155 mall_policy, NULL); 156 if (err < 0) 157 return err; 158 159 if (tb[TCA_MATCHALL_FLAGS]) { 160 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); 161 if (!tc_flags_valid(flags)) 162 return -EINVAL; 163 } 164 165 new = kzalloc(sizeof(*new), GFP_KERNEL); 166 if (!new) 167 return -ENOBUFS; 168 169 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0); 170 if (err) 171 goto err_exts_init; 172 173 if (!handle) 174 handle = 1; 175 new->handle = handle; 176 new->flags = flags; 177 178 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr); 179 if (err) 180 goto err_set_parms; 181 182 if (!tc_skip_hw(new->flags)) { 183 err = mall_replace_hw_filter(tp, new, (unsigned long) new); 184 if (err) 185 goto err_replace_hw_filter; 186 } 187 188 if (!tc_in_hw(new->flags)) 189 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 190 191 *arg = head; 192 rcu_assign_pointer(tp->root, new); 193 return 0; 194 195 err_replace_hw_filter: 196 err_set_parms: 197 tcf_exts_destroy(&new->exts); 198 err_exts_init: 199 kfree(new); 200 return err; 201 } 202 203 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last) 204 { 205 return -EOPNOTSUPP; 206 } 207 208 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg) 209 { 210 struct cls_mall_head *head = rtnl_dereference(tp->root); 211 212 if (arg->count < arg->skip) 213 goto skip; 214 if (arg->fn(tp, head, arg) < 0) 215 arg->stop = 1; 216 skip: 217 arg->count++; 218 } 219 220 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh, 221 struct sk_buff *skb, struct tcmsg *t) 222 { 223 struct cls_mall_head *head = fh; 224 struct nlattr *nest; 225 226 if (!head) 227 return skb->len; 228 229 t->tcm_handle = head->handle; 230 231 nest = nla_nest_start(skb, TCA_OPTIONS); 232 if (!nest) 233 goto nla_put_failure; 234 235 if (head->res.classid && 236 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) 237 goto nla_put_failure; 238 239 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) 240 goto nla_put_failure; 241 242 if (tcf_exts_dump(skb, &head->exts)) 243 goto nla_put_failure; 244 245 nla_nest_end(skb, nest); 246 247 if (tcf_exts_dump_stats(skb, &head->exts) < 0) 248 goto nla_put_failure; 249 250 return skb->len; 251 252 nla_put_failure: 253 nla_nest_cancel(skb, nest); 254 return -1; 255 } 256 257 static void mall_bind_class(void *fh, u32 classid, unsigned long cl) 258 { 259 struct cls_mall_head *head = fh; 260 261 if (head && head->res.classid == classid) 262 head->res.class = cl; 263 } 264 265 static struct tcf_proto_ops cls_mall_ops __read_mostly = { 266 .kind = "matchall", 267 .classify = mall_classify, 268 .init = mall_init, 269 .destroy = mall_destroy, 270 .get = mall_get, 271 .change = mall_change, 272 .delete = mall_delete, 273 .walk = mall_walk, 274 .dump = mall_dump, 275 .bind_class = mall_bind_class, 276 .owner = THIS_MODULE, 277 }; 278 279 static int __init cls_mall_init(void) 280 { 281 return register_tcf_proto_ops(&cls_mall_ops); 282 } 283 284 static void __exit cls_mall_exit(void) 285 { 286 unregister_tcf_proto_ops(&cls_mall_ops); 287 } 288 289 module_init(cls_mall_init); 290 module_exit(cls_mall_exit); 291 292 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); 293 MODULE_DESCRIPTION("Match-all classifier"); 294 MODULE_LICENSE("GPL v2"); 295