nft_hash.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/list.h>
  14. #include <linux/log2.h>
  15. #include <linux/jhash.h>
  16. #include <linux/netlink.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/rhashtable.h>
  19. #include <linux/netfilter.h>
  20. #include <linux/netfilter/nf_tables.h>
  21. #include <net/netfilter/nf_tables.h>
  22. /* We target a hash table size of 4, element hint is 75% of final size */
  23. #define NFT_HASH_ELEMENT_HINT 3
  24. struct nft_hash {
  25. struct rhashtable ht;
  26. struct delayed_work gc_work;
  27. };
  28. struct nft_hash_elem {
  29. struct rhash_head node;
  30. struct nft_set_ext ext;
  31. };
  32. struct nft_hash_cmp_arg {
  33. const struct nft_set *set;
  34. const u32 *key;
  35. u8 genmask;
  36. };
  37. static const struct rhashtable_params nft_hash_params;
  38. static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
  39. {
  40. const struct nft_hash_cmp_arg *arg = data;
  41. return jhash(arg->key, len, seed);
  42. }
  43. static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
  44. {
  45. const struct nft_hash_elem *he = data;
  46. return jhash(nft_set_ext_key(&he->ext), len, seed);
  47. }
  48. static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
  49. const void *ptr)
  50. {
  51. const struct nft_hash_cmp_arg *x = arg->key;
  52. const struct nft_hash_elem *he = ptr;
  53. if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
  54. return 1;
  55. if (nft_set_elem_expired(&he->ext))
  56. return 1;
  57. if (!nft_set_elem_active(&he->ext, x->genmask))
  58. return 1;
  59. return 0;
  60. }
  61. static bool nft_hash_lookup(const struct nft_set *set, const u32 *key,
  62. const struct nft_set_ext **ext)
  63. {
  64. struct nft_hash *priv = nft_set_priv(set);
  65. const struct nft_hash_elem *he;
  66. struct nft_hash_cmp_arg arg = {
  67. .genmask = nft_genmask_cur(read_pnet(&set->pnet)),
  68. .set = set,
  69. .key = key,
  70. };
  71. he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
  72. if (he != NULL)
  73. *ext = &he->ext;
  74. return !!he;
  75. }
  76. static bool nft_hash_update(struct nft_set *set, const u32 *key,
  77. void *(*new)(struct nft_set *,
  78. const struct nft_expr *,
  79. struct nft_regs *regs),
  80. const struct nft_expr *expr,
  81. struct nft_regs *regs,
  82. const struct nft_set_ext **ext)
  83. {
  84. struct nft_hash *priv = nft_set_priv(set);
  85. struct nft_hash_elem *he;
  86. struct nft_hash_cmp_arg arg = {
  87. .genmask = NFT_GENMASK_ANY,
  88. .set = set,
  89. .key = key,
  90. };
  91. he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
  92. if (he != NULL)
  93. goto out;
  94. he = new(set, expr, regs);
  95. if (he == NULL)
  96. goto err1;
  97. if (rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
  98. nft_hash_params))
  99. goto err2;
  100. out:
  101. *ext = &he->ext;
  102. return true;
  103. err2:
  104. nft_set_elem_destroy(set, he);
  105. err1:
  106. return false;
  107. }
  108. static int nft_hash_insert(const struct nft_set *set,
  109. const struct nft_set_elem *elem)
  110. {
  111. struct nft_hash *priv = nft_set_priv(set);
  112. struct nft_hash_elem *he = elem->priv;
  113. struct nft_hash_cmp_arg arg = {
  114. .genmask = nft_genmask_next(read_pnet(&set->pnet)),
  115. .set = set,
  116. .key = elem->key.val.data,
  117. };
  118. return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
  119. nft_hash_params);
  120. }
  121. static void nft_hash_activate(const struct nft_set *set,
  122. const struct nft_set_elem *elem)
  123. {
  124. struct nft_hash_elem *he = elem->priv;
  125. nft_set_elem_change_active(set, &he->ext);
  126. nft_set_elem_clear_busy(&he->ext);
  127. }
  128. static void *nft_hash_deactivate(const struct nft_set *set,
  129. const struct nft_set_elem *elem)
  130. {
  131. struct nft_hash *priv = nft_set_priv(set);
  132. struct nft_hash_elem *he;
  133. struct nft_hash_cmp_arg arg = {
  134. .genmask = nft_genmask_next(read_pnet(&set->pnet)),
  135. .set = set,
  136. .key = elem->key.val.data,
  137. };
  138. rcu_read_lock();
  139. he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
  140. if (he != NULL) {
  141. if (!nft_set_elem_mark_busy(&he->ext))
  142. nft_set_elem_change_active(set, &he->ext);
  143. else
  144. he = NULL;
  145. }
  146. rcu_read_unlock();
  147. return he;
  148. }
  149. static void nft_hash_remove(const struct nft_set *set,
  150. const struct nft_set_elem *elem)
  151. {
  152. struct nft_hash *priv = nft_set_priv(set);
  153. struct nft_hash_elem *he = elem->priv;
  154. rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
  155. }
  156. static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
  157. struct nft_set_iter *iter)
  158. {
  159. struct nft_hash *priv = nft_set_priv(set);
  160. struct nft_hash_elem *he;
  161. struct rhashtable_iter hti;
  162. struct nft_set_elem elem;
  163. u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
  164. int err;
  165. err = rhashtable_walk_init(&priv->ht, &hti);
  166. iter->err = err;
  167. if (err)
  168. return;
  169. err = rhashtable_walk_start(&hti);
  170. if (err && err != -EAGAIN) {
  171. iter->err = err;
  172. goto out;
  173. }
  174. while ((he = rhashtable_walk_next(&hti))) {
  175. if (IS_ERR(he)) {
  176. err = PTR_ERR(he);
  177. if (err != -EAGAIN) {
  178. iter->err = err;
  179. goto out;
  180. }
  181. continue;
  182. }
  183. if (iter->count < iter->skip)
  184. goto cont;
  185. if (nft_set_elem_expired(&he->ext))
  186. goto cont;
  187. if (!nft_set_elem_active(&he->ext, genmask))
  188. goto cont;
  189. elem.priv = he;
  190. iter->err = iter->fn(ctx, set, iter, &elem);
  191. if (iter->err < 0)
  192. goto out;
  193. cont:
  194. iter->count++;
  195. }
  196. out:
  197. rhashtable_walk_stop(&hti);
  198. rhashtable_walk_exit(&hti);
  199. }
  200. static void nft_hash_gc(struct work_struct *work)
  201. {
  202. struct nft_set *set;
  203. struct nft_hash_elem *he;
  204. struct nft_hash *priv;
  205. struct nft_set_gc_batch *gcb = NULL;
  206. struct rhashtable_iter hti;
  207. int err;
  208. priv = container_of(work, struct nft_hash, gc_work.work);
  209. set = nft_set_container_of(priv);
  210. err = rhashtable_walk_init(&priv->ht, &hti);
  211. if (err)
  212. goto schedule;
  213. err = rhashtable_walk_start(&hti);
  214. if (err && err != -EAGAIN)
  215. goto out;
  216. while ((he = rhashtable_walk_next(&hti))) {
  217. if (IS_ERR(he)) {
  218. if (PTR_ERR(he) != -EAGAIN)
  219. goto out;
  220. continue;
  221. }
  222. if (!nft_set_elem_expired(&he->ext))
  223. continue;
  224. if (nft_set_elem_mark_busy(&he->ext))
  225. continue;
  226. gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
  227. if (gcb == NULL)
  228. goto out;
  229. rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
  230. atomic_dec(&set->nelems);
  231. nft_set_gc_batch_add(gcb, he);
  232. }
  233. out:
  234. rhashtable_walk_stop(&hti);
  235. rhashtable_walk_exit(&hti);
  236. nft_set_gc_batch_complete(gcb);
  237. schedule:
  238. queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
  239. nft_set_gc_interval(set));
  240. }
  241. static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
  242. {
  243. return sizeof(struct nft_hash);
  244. }
  245. static const struct rhashtable_params nft_hash_params = {
  246. .head_offset = offsetof(struct nft_hash_elem, node),
  247. .hashfn = nft_hash_key,
  248. .obj_hashfn = nft_hash_obj,
  249. .obj_cmpfn = nft_hash_cmp,
  250. .automatic_shrinking = true,
  251. };
  252. static int nft_hash_init(const struct nft_set *set,
  253. const struct nft_set_desc *desc,
  254. const struct nlattr * const tb[])
  255. {
  256. struct nft_hash *priv = nft_set_priv(set);
  257. struct rhashtable_params params = nft_hash_params;
  258. int err;
  259. params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
  260. params.key_len = set->klen;
  261. err = rhashtable_init(&priv->ht, &params);
  262. if (err < 0)
  263. return err;
  264. INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
  265. if (set->flags & NFT_SET_TIMEOUT)
  266. queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
  267. nft_set_gc_interval(set));
  268. return 0;
  269. }
  270. static void nft_hash_elem_destroy(void *ptr, void *arg)
  271. {
  272. nft_set_elem_destroy((const struct nft_set *)arg, ptr);
  273. }
  274. static void nft_hash_destroy(const struct nft_set *set)
  275. {
  276. struct nft_hash *priv = nft_set_priv(set);
  277. cancel_delayed_work_sync(&priv->gc_work);
  278. rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
  279. (void *)set);
  280. }
  281. static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
  282. struct nft_set_estimate *est)
  283. {
  284. unsigned int esize;
  285. esize = sizeof(struct nft_hash_elem);
  286. if (desc->size) {
  287. est->size = sizeof(struct nft_hash) +
  288. roundup_pow_of_two(desc->size * 4 / 3) *
  289. sizeof(struct nft_hash_elem *) +
  290. desc->size * esize;
  291. } else {
  292. /* Resizing happens when the load drops below 30% or goes
  293. * above 75%. The average of 52.5% load (approximated by 50%)
  294. * is used for the size estimation of the hash buckets,
  295. * meaning we calculate two buckets per element.
  296. */
  297. est->size = esize + 2 * sizeof(struct nft_hash_elem *);
  298. }
  299. est->class = NFT_SET_CLASS_O_1;
  300. return true;
  301. }
  302. static struct nft_set_ops nft_hash_ops __read_mostly = {
  303. .privsize = nft_hash_privsize,
  304. .elemsize = offsetof(struct nft_hash_elem, ext),
  305. .estimate = nft_hash_estimate,
  306. .init = nft_hash_init,
  307. .destroy = nft_hash_destroy,
  308. .insert = nft_hash_insert,
  309. .activate = nft_hash_activate,
  310. .deactivate = nft_hash_deactivate,
  311. .remove = nft_hash_remove,
  312. .lookup = nft_hash_lookup,
  313. .update = nft_hash_update,
  314. .walk = nft_hash_walk,
  315. .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
  316. .owner = THIS_MODULE,
  317. };
  318. static int __init nft_hash_module_init(void)
  319. {
  320. return nft_register_set(&nft_hash_ops);
  321. }
  322. static void __exit nft_hash_module_exit(void)
  323. {
  324. nft_unregister_set(&nft_hash_ops);
  325. }
  326. module_init(nft_hash_module_init);
  327. module_exit(nft_hash_module_exit);
  328. MODULE_LICENSE("GPL");
  329. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  330. MODULE_ALIAS_NFT_SET();