]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: add expression handler hashtable
authorPablo Neira Ayuso <pablo@netfilter.org>
Thu, 20 Aug 2020 16:21:37 +0000 (18:21 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 26 Aug 2020 16:52:28 +0000 (18:52 +0200)
netlink_parsers is actually small, but update this code to use a
hashtable instead since more expressions may come in the future.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/cache.h
include/netlink.h
include/rule.h
src/libnftables.c
src/netlink_delinearize.c

index 213a6eaf9104b1b9e9195fc97572a883342150b8..b9db1a8f7650bc4b20d3cc206a18550c2dbe9baa 100644 (file)
@@ -35,4 +35,14 @@ enum cache_level_flags {
        NFT_CACHE_FLUSHED       = (1 << 31),
 };
 
+static inline uint32_t djb_hash(const char *key)
+{
+       uint32_t i, hash = 5381;
+
+       for (i = 0; i < strlen(key); i++)
+               hash = ((hash << 5) + hash) + key[i];
+
+       return hash;
+}
+
 #endif /* _NFT_CACHE_H_ */
index 1077096ea0b1a07eb42c78e5dad1b3dfb3685fd7..ad2247e9dd575fabbc60792a6da15f441f051458 100644 (file)
@@ -213,4 +213,7 @@ int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
 
 enum nft_data_types dtype_map_to_kernel(const struct datatype *dtype);
 
+void expr_handler_init(void);
+void expr_handler_exit(void);
+
 #endif /* NFTABLES_NETLINK_H */
index caca63d0f6485a1a989d3b380294c3610c0ca711..f2f82cc0ca4b119fc41832b7fbe801484747c5d7 100644 (file)
@@ -7,6 +7,7 @@
 #include <netinet/in.h>
 #include <libnftnl/object.h>   /* For NFTNL_CTTIMEOUT_ARRAY_MAX. */
 #include <linux/netfilter/nf_tables.h>
+#include <string.h>
 
 /**
  * struct handle_spec - handle ID
index 668e3fc430318f5a684d8908cffabc979fcc58b6..fce52ad4003b0d9f0c280ad5af0c1b81a10bb891 100644 (file)
@@ -88,10 +88,12 @@ static void nft_init(struct nft_ctx *ctx)
        realm_table_rt_init(ctx);
        devgroup_table_init(ctx);
        ct_label_table_init(ctx);
+       expr_handler_init();
 }
 
 static void nft_exit(struct nft_ctx *ctx)
 {
+       expr_handler_exit();
        ct_label_table_exit(ctx);
        realm_table_rt_exit(ctx);
        devgroup_table_exit(ctx);
index 9e3ed53d09f1cb663faf2228fc06cad665819b83..43d7ff821504d08c8e8753d99ef99011922f58bb 100644 (file)
@@ -27,6 +27,7 @@
 #include <erec.h>
 #include <sys/socket.h>
 #include <libnftnl/udata.h>
+#include <cache.h>
 #include <xt.h>
 
 static int netlink_parse_expr(const struct nftnl_expr *nle,
@@ -1627,12 +1628,14 @@ static void netlink_parse_objref(struct netlink_parse_ctx *ctx,
        ctx->stmt = stmt;
 }
 
-static const struct {
+struct expr_handler {
        const char      *name;
        void            (*parse)(struct netlink_parse_ctx *ctx,
                                 const struct location *loc,
                                 const struct nftnl_expr *nle);
-} netlink_parsers[] = {
+};
+
+static const struct expr_handler netlink_parsers[] = {
        { .name = "immediate",  .parse = netlink_parse_immediate },
        { .name = "cmp",        .parse = netlink_parse_cmp },
        { .name = "lookup",     .parse = netlink_parse_lookup },
@@ -1673,25 +1676,48 @@ static const struct {
        { .name = "synproxy",   .parse = netlink_parse_synproxy },
 };
 
+static const struct expr_handler **expr_handle_ht;
+
+#define NFT_EXPR_HSIZE 4096
+
+void expr_handler_init(void)
+{
+       unsigned int i;
+       uint32_t hash;
+
+       expr_handle_ht = calloc(NFT_EXPR_HSIZE, sizeof(expr_handle_ht));
+       if (!expr_handle_ht)
+               memory_allocation_error();
+
+       for (i = 0; i < array_size(netlink_parsers); i++) {
+               hash = djb_hash(netlink_parsers[i].name) % NFT_EXPR_HSIZE;
+               assert(expr_handle_ht[hash] == NULL);
+               expr_handle_ht[hash] = &netlink_parsers[i];
+       }
+}
+
+void expr_handler_exit(void)
+{
+       xfree(expr_handle_ht);
+}
+
 static int netlink_parse_expr(const struct nftnl_expr *nle,
                              struct netlink_parse_ctx *ctx)
 {
        const char *type = nftnl_expr_get_str(nle, NFTNL_EXPR_NAME);
        struct location loc;
-       unsigned int i;
+       uint32_t hash;
 
        memset(&loc, 0, sizeof(loc));
        loc.indesc = &indesc_netlink;
        loc.nle = nle;
 
-       for (i = 0; i < array_size(netlink_parsers); i++) {
-               if (strcmp(type, netlink_parsers[i].name))
-                       continue;
-               netlink_parsers[i].parse(ctx, &loc, nle);
-               return 0;
-       }
+       hash = djb_hash(type) % NFT_EXPR_HSIZE;
+       if (expr_handle_ht[hash])
+               expr_handle_ht[hash]->parse(ctx, &loc, nle);
+       else
+               netlink_error(ctx, &loc, "unknown expression type '%s'", type);
 
-       netlink_error(ctx, &loc, "unknown expression type '%s'", type);
        return 0;
 }