357-v4.18-netfilter-nf_flow_table-move-init-code-to-nf_flow_ta.patch 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Date: Sun, 18 Feb 2018 18:16:31 +0100
  3. Subject: [PATCH] netfilter: nf_flow_table: move init code to
  4. nf_flow_table_core.c
  5. Reduces duplication of .gc and .params in flowtable type definitions and
  6. makes the API clearer
  7. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  8. ---
  9. --- a/include/net/netfilter/nf_flow_table.h
  10. +++ b/include/net/netfilter/nf_flow_table.h
  11. @@ -14,9 +14,8 @@ struct nf_flowtable;
  12. struct nf_flowtable_type {
  13. struct list_head list;
  14. int family;
  15. - void (*gc)(struct work_struct *work);
  16. + int (*init)(struct nf_flowtable *ft);
  17. void (*free)(struct nf_flowtable *ft);
  18. - const struct rhashtable_params *params;
  19. nf_hookfn *hook;
  20. struct module *owner;
  21. };
  22. @@ -100,9 +99,8 @@ int nf_flow_table_iterate(struct nf_flow
  23. void nf_flow_table_cleanup(struct net *net, struct net_device *dev);
  24. +int nf_flow_table_init(struct nf_flowtable *flow_table);
  25. void nf_flow_table_free(struct nf_flowtable *flow_table);
  26. -void nf_flow_offload_work_gc(struct work_struct *work);
  27. -extern const struct rhashtable_params nf_flow_offload_rhash_params;
  28. void flow_offload_dead(struct flow_offload *flow);
  29. --- a/net/ipv4/netfilter/nf_flow_table_ipv4.c
  30. +++ b/net/ipv4/netfilter/nf_flow_table_ipv4.c
  31. @@ -7,8 +7,7 @@
  32. static struct nf_flowtable_type flowtable_ipv4 = {
  33. .family = NFPROTO_IPV4,
  34. - .params = &nf_flow_offload_rhash_params,
  35. - .gc = nf_flow_offload_work_gc,
  36. + .init = nf_flow_table_init,
  37. .free = nf_flow_table_free,
  38. .hook = nf_flow_offload_ip_hook,
  39. .owner = THIS_MODULE,
  40. --- a/net/ipv6/netfilter/nf_flow_table_ipv6.c
  41. +++ b/net/ipv6/netfilter/nf_flow_table_ipv6.c
  42. @@ -8,8 +8,7 @@
  43. static struct nf_flowtable_type flowtable_ipv6 = {
  44. .family = NFPROTO_IPV6,
  45. - .params = &nf_flow_offload_rhash_params,
  46. - .gc = nf_flow_offload_work_gc,
  47. + .init = nf_flow_table_init,
  48. .free = nf_flow_table_free,
  49. .hook = nf_flow_offload_ipv6_hook,
  50. .owner = THIS_MODULE,
  51. --- a/net/netfilter/nf_flow_table_core.c
  52. +++ b/net/netfilter/nf_flow_table_core.c
  53. @@ -116,16 +116,50 @@ void flow_offload_dead(struct flow_offlo
  54. }
  55. EXPORT_SYMBOL_GPL(flow_offload_dead);
  56. +static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
  57. +{
  58. + const struct flow_offload_tuple *tuple = data;
  59. +
  60. + return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
  61. +}
  62. +
  63. +static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
  64. +{
  65. + const struct flow_offload_tuple_rhash *tuplehash = data;
  66. +
  67. + return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
  68. +}
  69. +
  70. +static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
  71. + const void *ptr)
  72. +{
  73. + const struct flow_offload_tuple *tuple = arg->key;
  74. + const struct flow_offload_tuple_rhash *x = ptr;
  75. +
  76. + if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
  77. + return 1;
  78. +
  79. + return 0;
  80. +}
  81. +
  82. +static const struct rhashtable_params nf_flow_offload_rhash_params = {
  83. + .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
  84. + .hashfn = flow_offload_hash,
  85. + .obj_hashfn = flow_offload_hash_obj,
  86. + .obj_cmpfn = flow_offload_hash_cmp,
  87. + .automatic_shrinking = true,
  88. +};
  89. +
  90. int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
  91. {
  92. flow->timeout = (u32)jiffies;
  93. rhashtable_insert_fast(&flow_table->rhashtable,
  94. &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
  95. - *flow_table->type->params);
  96. + nf_flow_offload_rhash_params);
  97. rhashtable_insert_fast(&flow_table->rhashtable,
  98. &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
  99. - *flow_table->type->params);
  100. + nf_flow_offload_rhash_params);
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(flow_offload_add);
  104. @@ -135,10 +169,10 @@ static void flow_offload_del(struct nf_f
  105. {
  106. rhashtable_remove_fast(&flow_table->rhashtable,
  107. &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
  108. - *flow_table->type->params);
  109. + nf_flow_offload_rhash_params);
  110. rhashtable_remove_fast(&flow_table->rhashtable,
  111. &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
  112. - *flow_table->type->params);
  113. + nf_flow_offload_rhash_params);
  114. flow_offload_free(flow);
  115. }
  116. @@ -148,7 +182,7 @@ flow_offload_lookup(struct nf_flowtable
  117. struct flow_offload_tuple *tuple)
  118. {
  119. return rhashtable_lookup_fast(&flow_table->rhashtable, tuple,
  120. - *flow_table->type->params);
  121. + nf_flow_offload_rhash_params);
  122. }
  123. EXPORT_SYMBOL_GPL(flow_offload_lookup);
  124. @@ -237,7 +271,7 @@ out:
  125. return 1;
  126. }
  127. -void nf_flow_offload_work_gc(struct work_struct *work)
  128. +static void nf_flow_offload_work_gc(struct work_struct *work)
  129. {
  130. struct nf_flowtable *flow_table;
  131. @@ -245,42 +279,6 @@ void nf_flow_offload_work_gc(struct work
  132. nf_flow_offload_gc_step(flow_table);
  133. queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
  134. }
  135. -EXPORT_SYMBOL_GPL(nf_flow_offload_work_gc);
  136. -
  137. -static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
  138. -{
  139. - const struct flow_offload_tuple *tuple = data;
  140. -
  141. - return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
  142. -}
  143. -
  144. -static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
  145. -{
  146. - const struct flow_offload_tuple_rhash *tuplehash = data;
  147. -
  148. - return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
  149. -}
  150. -
  151. -static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
  152. - const void *ptr)
  153. -{
  154. - const struct flow_offload_tuple *tuple = arg->key;
  155. - const struct flow_offload_tuple_rhash *x = ptr;
  156. -
  157. - if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
  158. - return 1;
  159. -
  160. - return 0;
  161. -}
  162. -
  163. -const struct rhashtable_params nf_flow_offload_rhash_params = {
  164. - .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
  165. - .hashfn = flow_offload_hash,
  166. - .obj_hashfn = flow_offload_hash_obj,
  167. - .obj_cmpfn = flow_offload_hash_cmp,
  168. - .automatic_shrinking = true,
  169. -};
  170. -EXPORT_SYMBOL_GPL(nf_flow_offload_rhash_params);
  171. static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
  172. __be16 port, __be16 new_port)
  173. @@ -398,6 +396,24 @@ int nf_flow_dnat_port(const struct flow_
  174. }
  175. EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
  176. +int nf_flow_table_init(struct nf_flowtable *flowtable)
  177. +{
  178. + int err;
  179. +
  180. + INIT_DEFERRABLE_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
  181. +
  182. + err = rhashtable_init(&flowtable->rhashtable,
  183. + &nf_flow_offload_rhash_params);
  184. + if (err < 0)
  185. + return err;
  186. +
  187. + queue_delayed_work(system_power_efficient_wq,
  188. + &flowtable->gc_work, HZ);
  189. +
  190. + return 0;
  191. +}
  192. +EXPORT_SYMBOL_GPL(nf_flow_table_init);
  193. +
  194. static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
  195. {
  196. struct net_device *dev = data;
  197. @@ -423,8 +439,10 @@ EXPORT_SYMBOL_GPL(nf_flow_table_cleanup)
  198. void nf_flow_table_free(struct nf_flowtable *flow_table)
  199. {
  200. + cancel_delayed_work_sync(&flow_table->gc_work);
  201. nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
  202. WARN_ON(!nf_flow_offload_gc_step(flow_table));
  203. + rhashtable_destroy(&flow_table->rhashtable);
  204. }
  205. EXPORT_SYMBOL_GPL(nf_flow_table_free);
  206. --- a/net/netfilter/nf_flow_table_inet.c
  207. +++ b/net/netfilter/nf_flow_table_inet.c
  208. @@ -22,8 +22,7 @@ nf_flow_offload_inet_hook(void *priv, st
  209. static struct nf_flowtable_type flowtable_inet = {
  210. .family = NFPROTO_INET,
  211. - .params = &nf_flow_offload_rhash_params,
  212. - .gc = nf_flow_offload_work_gc,
  213. + .init = nf_flow_table_init,
  214. .free = nf_flow_table_free,
  215. .hook = nf_flow_offload_inet_hook,
  216. .owner = THIS_MODULE,
  217. --- a/net/netfilter/nf_tables_api.c
  218. +++ b/net/netfilter/nf_tables_api.c
  219. @@ -5109,40 +5109,38 @@ static int nf_tables_newflowtable(struct
  220. }
  221. flowtable->data.type = type;
  222. - err = rhashtable_init(&flowtable->data.rhashtable, type->params);
  223. + err = type->init(&flowtable->data);
  224. if (err < 0)
  225. goto err3;
  226. err = nf_tables_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
  227. flowtable);
  228. if (err < 0)
  229. - goto err3;
  230. + goto err4;
  231. for (i = 0; i < flowtable->ops_len; i++) {
  232. err = nf_register_net_hook(net, &flowtable->ops[i]);
  233. if (err < 0)
  234. - goto err4;
  235. + goto err5;
  236. }
  237. err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
  238. if (err < 0)
  239. - goto err5;
  240. -
  241. - INIT_DEFERRABLE_WORK(&flowtable->data.gc_work, type->gc);
  242. - queue_delayed_work(system_power_efficient_wq,
  243. - &flowtable->data.gc_work, HZ);
  244. + goto err6;
  245. list_add_tail_rcu(&flowtable->list, &table->flowtables);
  246. table->use++;
  247. return 0;
  248. -err5:
  249. +err6:
  250. i = flowtable->ops_len;
  251. -err4:
  252. +err5:
  253. for (k = i - 1; k >= 0; k--)
  254. nf_unregister_net_hook(net, &flowtable->ops[i]);
  255. kfree(flowtable->ops);
  256. +err4:
  257. + flowtable->data.type->free(&flowtable->data);
  258. err3:
  259. module_put(type->owner);
  260. err2:
  261. @@ -5423,10 +5421,8 @@ err:
  262. static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
  263. {
  264. - cancel_delayed_work_sync(&flowtable->data.gc_work);
  265. kfree(flowtable->name);
  266. flowtable->data.type->free(&flowtable->data);
  267. - rhashtable_destroy(&flowtable->data.rhashtable);
  268. module_put(flowtable->data.type->owner);
  269. }