cls_matchall.c 8.2 KB

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