cregit-Linux how code gets into the kernel

Release 4.11 net/netfilter/nft_set_hash.c

Directory: net/netfilter
/*
 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com/)
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/log2.h>
#include <linux/jhash.h>
#include <linux/netlink.h>
#include <linux/workqueue.h>
#include <linux/rhashtable.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <net/netfilter/nf_tables.h>

/* We target a hash table size of 4, element hint is 75% of final size */

#define NFT_HASH_ELEMENT_HINT 3


struct nft_hash {
	
struct rhashtable		ht;
	
struct delayed_work		gc_work;
};


struct nft_hash_elem {
	
struct rhash_head		node;
	
struct nft_set_ext		ext;
};


struct nft_hash_cmp_arg {
	
const struct nft_set		*set;
	
const u32			*key;
	
u8				genmask;
};


static const struct rhashtable_params nft_hash_params;


static inline u32 nft_hash_key(const void *data, u32 len, u32 seed) { const struct nft_hash_cmp_arg *arg = data; return jhash(arg->key, len, seed); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy38100.00%1100.00%
Total38100.00%1100.00%


static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed) { const struct nft_hash_elem *he = data; return jhash(nft_set_ext_key(&he->ext), len, seed); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy42100.00%2100.00%
Total42100.00%2100.00%


static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg, const void *ptr) { const struct nft_hash_cmp_arg *x = arg->key; const struct nft_hash_elem *he = ptr; if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen)) return 1; if (nft_set_elem_expired(&he->ext)) return 1; if (!nft_set_elem_active(&he->ext, x->genmask)) return 1; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy95100.00%5100.00%
Total95100.00%5100.00%


static bool nft_hash_lookup(const struct net *net, const struct nft_set *set, const u32 *key, const struct nft_set_ext **ext) { struct nft_hash *priv = nft_set_priv(set); const struct nft_hash_elem *he; struct nft_hash_cmp_arg arg = { .genmask = nft_genmask_cur(net), .set = set, .key = key, }; he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); if (he != NULL) *ext = &he->ext; return !!he; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy7370.19%770.00%
Thomas Graf2120.19%110.00%
Pablo Neira Ayuso76.73%110.00%
Herbert Xu32.88%110.00%
Total104100.00%10100.00%


static bool nft_hash_update(struct nft_set *set, const u32 *key, void *(*new)(struct nft_set *, const struct nft_expr *, struct nft_regs *regs), const struct nft_expr *expr, struct nft_regs *regs, const struct nft_set_ext **ext) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he, *prev; struct nft_hash_cmp_arg arg = { .genmask = NFT_GENMASK_ANY, .set = set, .key = key, }; he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); if (he != NULL) goto out; he = new(set, expr, regs); if (he == NULL) goto err1; prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, nft_hash_params); if (IS_ERR(prev)) goto err2; /* Another cpu may race to insert the element with the same key */ if (prev) { nft_set_elem_destroy(set, he, true); he = prev; } out: *ext = &he->ext; return true; err2: nft_set_elem_destroy(set, he, true); err1: return false; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy18383.94%360.00%
Liping Zhang3516.06%240.00%
Total218100.00%5100.00%


static int nft_hash_insert(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem, struct nft_set_ext **ext) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he = elem->priv; struct nft_hash_cmp_arg arg = { .genmask = nft_genmask_next(net), .set = set, .key = elem->key.val.data, }; struct nft_hash_elem *prev; prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, nft_hash_params); if (IS_ERR(prev)) return PTR_ERR(prev); if (prev) { *ext = &prev->ext; return -EEXIST; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy7755.80%763.64%
Pablo Neira Ayuso5539.86%218.18%
Thomas Graf32.17%19.09%
Herbert Xu32.17%19.09%
Total138100.00%11100.00%


static void nft_hash_activate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash_elem *he = elem->priv; nft_set_elem_change_active(net, set, &he->ext); nft_set_elem_clear_busy(&he->ext); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy4483.02%571.43%
Pablo Neira Ayuso815.09%114.29%
Thomas Graf11.89%114.29%
Total53100.00%7100.00%


static bool nft_hash_flush(const struct net *net, const struct nft_set *set, void *priv) { struct nft_hash_elem *he = priv; if (!nft_set_elem_mark_busy(&he->ext) || !nft_is_active(net, &he->ext)) { nft_set_elem_change_active(net, set, &he->ext); return true; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
Pablo Neira Ayuso71100.00%2100.00%
Total71100.00%2100.00%


static void *nft_hash_deactivate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; struct nft_hash_cmp_arg arg = { .genmask = nft_genmask_next(net), .set = set, .key = elem->key.val.data, }; rcu_read_lock(); he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); if (he != NULL && !nft_hash_flush(net, set, he)) he = NULL; rcu_read_unlock(); return he; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy8171.05%964.29%
Herbert Xu2118.42%17.14%
Pablo Neira Ayuso1210.53%428.57%
Total114100.00%14100.00%


static void nft_hash_remove(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he = elem->priv; rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy5086.21%466.67%
Pablo Neira Ayuso610.34%116.67%
Thomas Graf23.45%116.67%
Total58100.00%6100.00%


static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, struct nft_set_iter *iter) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; struct rhashtable_iter hti; struct nft_set_elem elem; int err; err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL); iter->err = err; if (err) return; err = rhashtable_walk_start(&hti); if (err && err != -EAGAIN) { iter->err = err; goto out; } while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { err = PTR_ERR(he); if (err != -EAGAIN) { iter->err = err; goto out; } continue; } if (iter->count < iter->skip) goto cont; if (nft_set_elem_expired(&he->ext)) goto cont; if (!nft_set_elem_active(&he->ext, iter->genmask)) goto cont; elem.priv = he; iter->err = iter->fn(ctx, set, iter, &elem); if (iter->err < 0) goto out; cont: iter->count++; } out: rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy13453.39%654.55%
Herbert Xu10742.63%218.18%
Thomas Graf62.39%19.09%
Bob Copeland20.80%19.09%
Pablo Neira Ayuso20.80%19.09%
Total251100.00%11100.00%


static void nft_hash_gc(struct work_struct *work) { struct nft_set *set; struct nft_hash_elem *he; struct nft_hash *priv; struct nft_set_gc_batch *gcb = NULL; struct rhashtable_iter hti; int err; priv = container_of(work, struct nft_hash, gc_work.work); set = nft_set_container_of(priv); err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL); if (err) goto schedule; err = rhashtable_walk_start(&hti); if (err && err != -EAGAIN) goto out; while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { if (PTR_ERR(he) != -EAGAIN) goto out; continue; } if (!nft_set_elem_expired(&he->ext)) continue; if (nft_set_elem_mark_busy(&he->ext)) continue; gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC); if (gcb == NULL) goto out; rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params); atomic_dec(&set->nelems); nft_set_gc_batch_add(gcb, he); } out: rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); nft_set_gc_batch_complete(gcb); schedule: queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy24799.20%266.67%
Bob Copeland20.80%133.33%
Total249100.00%3100.00%


static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) { return sizeof(struct nft_hash); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy1986.36%375.00%
Thomas Graf313.64%125.00%
Total22100.00%4100.00%

static const struct rhashtable_params nft_hash_params = { .head_offset = offsetof(struct nft_hash_elem, node), .hashfn = nft_hash_key, .obj_hashfn = nft_hash_obj, .obj_cmpfn = nft_hash_cmp, .automatic_shrinking = true, };
static int nft_hash_init(const struct nft_set *set, const struct nft_set_desc *desc, const struct nlattr * const tb[]) { struct nft_hash *priv = nft_set_priv(set); struct rhashtable_params params = nft_hash_params; int err; params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT; params.key_len = set->klen; err = rhashtable_init(&priv->ht, &params); if (err < 0) return err; INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc); if (set->flags & NFT_SET_TIMEOUT) queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy9981.15%675.00%
Thomas Graf1512.30%112.50%
Herbert Xu86.56%112.50%
Total122100.00%8100.00%


static void nft_hash_elem_destroy(void *ptr, void *arg) { nft_set_elem_destroy((const struct nft_set *)arg, ptr, true); }

Contributors

PersonTokensPropCommitsCommitProp
Thomas Graf2586.21%133.33%
Patrick McHardy26.90%133.33%
Liping Zhang26.90%133.33%
Total29100.00%3100.00%


static void nft_hash_destroy(const struct nft_set *set) { struct nft_hash *priv = nft_set_priv(set); cancel_delayed_work_sync(&priv->gc_work); rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy, (void *)set); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy4086.96%562.50%
Thomas Graf613.04%337.50%
Total46100.00%8100.00%


static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) { unsigned int esize; esize = sizeof(struct nft_hash_elem); if (desc->size) { est->size = sizeof(struct nft_hash) + roundup_pow_of_two(desc->size * 4 / 3) * sizeof(struct nft_hash_elem *) + desc->size * esize; } else { /* Resizing happens when the load drops below 30% or goes * above 75%. The average of 52.5% load (approximated by 50%) * is used for the size estimation of the hash buckets, * meaning we calculate two buckets per element. */ est->size = esize + 2 * sizeof(struct nft_hash_elem *); } est->lookup = NFT_SET_CLASS_O_1; est->space = NFT_SET_CLASS_O_N; return true; }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy9688.89%240.00%
Pablo Neira Ayuso76.48%240.00%
Thomas Graf54.63%120.00%
Total108100.00%5100.00%

static struct nft_set_ops nft_hash_ops __read_mostly = { .privsize = nft_hash_privsize, .elemsize = offsetof(struct nft_hash_elem, ext), .estimate = nft_hash_estimate, .init = nft_hash_init, .destroy = nft_hash_destroy, .insert = nft_hash_insert, .activate = nft_hash_activate, .deactivate = nft_hash_deactivate, .flush = nft_hash_flush, .remove = nft_hash_remove, .lookup = nft_hash_lookup, .update = nft_hash_update, .walk = nft_hash_walk, .features = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT, .owner = THIS_MODULE, };
static int __init nft_hash_module_init(void) { return nft_register_set(&nft_hash_ops); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy16100.00%2100.00%
Total16100.00%2100.00%


static void __exit nft_hash_module_exit(void) { nft_unregister_set(&nft_hash_ops); }

Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy15100.00%2100.00%
Total15100.00%2100.00%

module_init(nft_hash_module_init); module_exit(nft_hash_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); MODULE_ALIAS_NFT_SET();

Overall Contributors

PersonTokensPropCommitsCommitProp
Patrick McHardy153975.55%1741.46%
Pablo Neira Ayuso1758.59%1126.83%
Herbert Xu1738.49%37.32%
Thomas Graf1075.25%614.63%
Liping Zhang371.82%24.88%
Bob Copeland40.20%12.44%
David S. Miller20.10%12.44%
Total2037100.00%41100.00%
Directory: net/netfilter
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.