cls_matchall.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. return NULL;
  107. }
  108. static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
  109. [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
  110. [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
  111. };
  112. static int mall_set_parms(struct net *net, struct tcf_proto *tp,
  113. struct cls_mall_head *head,
  114. unsigned long base, struct nlattr **tb,
  115. struct nlattr *est, bool ovr,
  116. struct netlink_ext_ack *extack)
  117. {
  118. int err;
  119. err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, extack);
  120. if (err < 0)
  121. return err;
  122. if (tb[TCA_MATCHALL_CLASSID]) {
  123. head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
  124. tcf_bind_filter(tp, &head->res, base);
  125. }
  126. return 0;
  127. }
  128. static int mall_change(struct net *net, struct sk_buff *in_skb,
  129. struct tcf_proto *tp, unsigned long base,
  130. u32 handle, struct nlattr **tca,
  131. void **arg, bool ovr, struct netlink_ext_ack *extack)
  132. {
  133. struct cls_mall_head *head = rtnl_dereference(tp->root);
  134. struct nlattr *tb[TCA_MATCHALL_MAX + 1];
  135. struct cls_mall_head *new;
  136. u32 flags = 0;
  137. int err;
  138. if (!tca[TCA_OPTIONS])
  139. return -EINVAL;
  140. if (head)
  141. return -EEXIST;
  142. err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
  143. mall_policy, NULL);
  144. if (err < 0)
  145. return err;
  146. if (tb[TCA_MATCHALL_FLAGS]) {
  147. flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
  148. if (!tc_flags_valid(flags))
  149. return -EINVAL;
  150. }
  151. new = kzalloc(sizeof(*new), GFP_KERNEL);
  152. if (!new)
  153. return -ENOBUFS;
  154. err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
  155. if (err)
  156. goto err_exts_init;
  157. if (!handle)
  158. handle = 1;
  159. new->handle = handle;
  160. new->flags = flags;
  161. err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
  162. extack);
  163. if (err)
  164. goto err_set_parms;
  165. if (!tc_skip_hw(new->flags)) {
  166. err = mall_replace_hw_filter(tp, new, (unsigned long)new,
  167. extack);
  168. if (err)
  169. goto err_replace_hw_filter;
  170. }
  171. if (!tc_in_hw(new->flags))
  172. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  173. *arg = head;
  174. rcu_assign_pointer(tp->root, new);
  175. return 0;
  176. err_replace_hw_filter:
  177. err_set_parms:
  178. tcf_exts_destroy(&new->exts);
  179. err_exts_init:
  180. kfree(new);
  181. return err;
  182. }
  183. static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
  184. struct netlink_ext_ack *extack)
  185. {
  186. return -EOPNOTSUPP;
  187. }
  188. static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  189. {
  190. struct cls_mall_head *head = rtnl_dereference(tp->root);
  191. if (arg->count < arg->skip)
  192. goto skip;
  193. if (arg->fn(tp, head, arg) < 0)
  194. arg->stop = 1;
  195. skip:
  196. arg->count++;
  197. }
  198. static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
  199. void *cb_priv, struct netlink_ext_ack *extack)
  200. {
  201. struct cls_mall_head *head = rtnl_dereference(tp->root);
  202. struct tc_cls_matchall_offload cls_mall = {};
  203. struct tcf_block *block = tp->chain->block;
  204. int err;
  205. if (tc_skip_hw(head->flags))
  206. return 0;
  207. tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
  208. cls_mall.command = add ?
  209. TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
  210. cls_mall.exts = &head->exts;
  211. cls_mall.cookie = (unsigned long)head;
  212. err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv);
  213. if (err) {
  214. if (add && tc_skip_sw(head->flags))
  215. return err;
  216. return 0;
  217. }
  218. tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add);
  219. return 0;
  220. }
  221. static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
  222. struct sk_buff *skb, struct tcmsg *t)
  223. {
  224. struct cls_mall_head *head = fh;
  225. struct nlattr *nest;
  226. if (!head)
  227. return skb->len;
  228. t->tcm_handle = head->handle;
  229. nest = nla_nest_start(skb, TCA_OPTIONS);
  230. if (!nest)
  231. goto nla_put_failure;
  232. if (head->res.classid &&
  233. nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
  234. goto nla_put_failure;
  235. if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
  236. goto nla_put_failure;
  237. if (tcf_exts_dump(skb, &head->exts))
  238. goto nla_put_failure;
  239. nla_nest_end(skb, nest);
  240. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  241. goto nla_put_failure;
  242. return skb->len;
  243. nla_put_failure:
  244. nla_nest_cancel(skb, nest);
  245. return -1;
  246. }
  247. static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
  248. {
  249. struct cls_mall_head *head = fh;
  250. if (head && head->res.classid == classid)
  251. head->res.class = cl;
  252. }
  253. static struct tcf_proto_ops cls_mall_ops __read_mostly = {
  254. .kind = "matchall",
  255. .classify = mall_classify,
  256. .init = mall_init,
  257. .destroy = mall_destroy,
  258. .get = mall_get,
  259. .change = mall_change,
  260. .delete = mall_delete,
  261. .walk = mall_walk,
  262. .reoffload = mall_reoffload,
  263. .dump = mall_dump,
  264. .bind_class = mall_bind_class,
  265. .owner = THIS_MODULE,
  266. };
  267. static int __init cls_mall_init(void)
  268. {
  269. return register_tcf_proto_ops(&cls_mall_ops);
  270. }
  271. static void __exit cls_mall_exit(void)
  272. {
  273. unregister_tcf_proto_ops(&cls_mall_ops);
  274. }
  275. module_init(cls_mall_init);
  276. module_exit(cls_mall_exit);
  277. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  278. MODULE_DESCRIPTION("Match-all classifier");
  279. MODULE_LICENSE("GPL v2");