nft_hash.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/rhashtable.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/netfilter/nf_tables.h>
  20. #include <net/netfilter/nf_tables.h>
  21. /* We target a hash table size of 4, element hint is 75% of final size */
  22. #define NFT_HASH_ELEMENT_HINT 3
  23. struct nft_hash_elem {
  24. struct rhash_head node;
  25. struct nft_data key;
  26. struct nft_data data[];
  27. };
  28. static bool nft_hash_lookup(const struct nft_set *set,
  29. const struct nft_data *key,
  30. struct nft_data *data)
  31. {
  32. const struct rhashtable *priv = nft_set_priv(set);
  33. const struct nft_hash_elem *he;
  34. he = rhashtable_lookup(priv, key);
  35. if (he && set->flags & NFT_SET_MAP)
  36. nft_data_copy(data, he->data);
  37. return !!he;
  38. }
  39. static int nft_hash_insert(const struct nft_set *set,
  40. const struct nft_set_elem *elem)
  41. {
  42. struct rhashtable *priv = nft_set_priv(set);
  43. struct nft_hash_elem *he;
  44. unsigned int size;
  45. if (elem->flags != 0)
  46. return -EINVAL;
  47. size = sizeof(*he);
  48. if (set->flags & NFT_SET_MAP)
  49. size += sizeof(he->data[0]);
  50. he = kzalloc(size, GFP_KERNEL);
  51. if (he == NULL)
  52. return -ENOMEM;
  53. nft_data_copy(&he->key, &elem->key);
  54. if (set->flags & NFT_SET_MAP)
  55. nft_data_copy(he->data, &elem->data);
  56. rhashtable_insert(priv, &he->node, GFP_KERNEL);
  57. return 0;
  58. }
  59. static void nft_hash_elem_destroy(const struct nft_set *set,
  60. struct nft_hash_elem *he)
  61. {
  62. nft_data_uninit(&he->key, NFT_DATA_VALUE);
  63. if (set->flags & NFT_SET_MAP)
  64. nft_data_uninit(he->data, set->dtype);
  65. kfree(he);
  66. }
  67. static void nft_hash_remove(const struct nft_set *set,
  68. const struct nft_set_elem *elem)
  69. {
  70. struct rhashtable *priv = nft_set_priv(set);
  71. struct rhash_head *he, __rcu **pprev;
  72. pprev = elem->cookie;
  73. he = rht_dereference((*pprev), priv);
  74. rhashtable_remove_pprev(priv, he, pprev, GFP_KERNEL);
  75. synchronize_rcu();
  76. kfree(he);
  77. }
  78. static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
  79. {
  80. const struct rhashtable *priv = nft_set_priv(set);
  81. const struct bucket_table *tbl = rht_dereference_rcu(priv->tbl, priv);
  82. struct rhash_head __rcu * const *pprev;
  83. struct nft_hash_elem *he;
  84. u32 h;
  85. h = rhashtable_hashfn(priv, &elem->key, set->klen);
  86. pprev = &tbl->buckets[h];
  87. rht_for_each_entry_rcu(he, tbl->buckets[h], node) {
  88. if (nft_data_cmp(&he->key, &elem->key, set->klen)) {
  89. pprev = &he->node.next;
  90. continue;
  91. }
  92. elem->cookie = (void *)pprev;
  93. elem->flags = 0;
  94. if (set->flags & NFT_SET_MAP)
  95. nft_data_copy(&elem->data, he->data);
  96. return 0;
  97. }
  98. return -ENOENT;
  99. }
  100. static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
  101. struct nft_set_iter *iter)
  102. {
  103. const struct rhashtable *priv = nft_set_priv(set);
  104. const struct bucket_table *tbl;
  105. const struct nft_hash_elem *he;
  106. struct nft_set_elem elem;
  107. unsigned int i;
  108. tbl = rht_dereference_rcu(priv->tbl, priv);
  109. for (i = 0; i < tbl->size; i++) {
  110. rht_for_each_entry_rcu(he, tbl->buckets[i], node) {
  111. if (iter->count < iter->skip)
  112. goto cont;
  113. memcpy(&elem.key, &he->key, sizeof(elem.key));
  114. if (set->flags & NFT_SET_MAP)
  115. memcpy(&elem.data, he->data, sizeof(elem.data));
  116. elem.flags = 0;
  117. iter->err = iter->fn(ctx, set, iter, &elem);
  118. if (iter->err < 0)
  119. return;
  120. cont:
  121. iter->count++;
  122. }
  123. }
  124. }
  125. static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
  126. {
  127. return sizeof(struct rhashtable);
  128. }
  129. static int lockdep_nfnl_lock_is_held(void)
  130. {
  131. return lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES);
  132. }
  133. static int nft_hash_init(const struct nft_set *set,
  134. const struct nft_set_desc *desc,
  135. const struct nlattr * const tb[])
  136. {
  137. struct rhashtable *priv = nft_set_priv(set);
  138. struct rhashtable_params params = {
  139. .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT,
  140. .head_offset = offsetof(struct nft_hash_elem, node),
  141. .key_offset = offsetof(struct nft_hash_elem, key),
  142. .key_len = set->klen,
  143. .hashfn = jhash,
  144. .grow_decision = rht_grow_above_75,
  145. .shrink_decision = rht_shrink_below_30,
  146. .mutex_is_held = lockdep_nfnl_lock_is_held,
  147. };
  148. return rhashtable_init(priv, &params);
  149. }
  150. static void nft_hash_destroy(const struct nft_set *set)
  151. {
  152. const struct rhashtable *priv = nft_set_priv(set);
  153. const struct bucket_table *tbl = priv->tbl;
  154. struct nft_hash_elem *he, *next;
  155. unsigned int i;
  156. for (i = 0; i < tbl->size; i++) {
  157. for (he = rht_entry(tbl->buckets[i], struct nft_hash_elem, node);
  158. he != NULL; he = next) {
  159. next = rht_entry(he->node.next, struct nft_hash_elem, node);
  160. nft_hash_elem_destroy(set, he);
  161. }
  162. }
  163. rhashtable_destroy(priv);
  164. }
  165. static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
  166. struct nft_set_estimate *est)
  167. {
  168. unsigned int esize;
  169. esize = sizeof(struct nft_hash_elem);
  170. if (features & NFT_SET_MAP)
  171. esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
  172. if (desc->size) {
  173. est->size = sizeof(struct rhashtable) +
  174. roundup_pow_of_two(desc->size * 4 / 3) *
  175. sizeof(struct nft_hash_elem *) +
  176. desc->size * esize;
  177. } else {
  178. /* Resizing happens when the load drops below 30% or goes
  179. * above 75%. The average of 52.5% load (approximated by 50%)
  180. * is used for the size estimation of the hash buckets,
  181. * meaning we calculate two buckets per element.
  182. */
  183. est->size = esize + 2 * sizeof(struct nft_hash_elem *);
  184. }
  185. est->class = NFT_SET_CLASS_O_1;
  186. return true;
  187. }
  188. static struct nft_set_ops nft_hash_ops __read_mostly = {
  189. .privsize = nft_hash_privsize,
  190. .estimate = nft_hash_estimate,
  191. .init = nft_hash_init,
  192. .destroy = nft_hash_destroy,
  193. .get = nft_hash_get,
  194. .insert = nft_hash_insert,
  195. .remove = nft_hash_remove,
  196. .lookup = nft_hash_lookup,
  197. .walk = nft_hash_walk,
  198. .features = NFT_SET_MAP,
  199. .owner = THIS_MODULE,
  200. };
  201. static int __init nft_hash_module_init(void)
  202. {
  203. return nft_register_set(&nft_hash_ops);
  204. }
  205. static void __exit nft_hash_module_exit(void)
  206. {
  207. nft_unregister_set(&nft_hash_ops);
  208. }
  209. module_init(nft_hash_module_init);
  210. module_exit(nft_hash_module_exit);
  211. MODULE_LICENSE("GPL");
  212. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  213. MODULE_ALIAS_NFT_SET();