]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
set: dump set backend name (hash, rbtree...) and elem count, if available
authorFlorian Westphal <fw@strlen.de>
Tue, 8 Apr 2025 13:58:42 +0000 (15:58 +0200)
committerFlorian Westphal <fw@strlen.de>
Sun, 22 Jun 2025 17:38:17 +0000 (19:38 +0200)
In case kernel provided the information do include it in debug dump:

nft --debug=netlink list ruleset
family 2 s t 0 backend nft_set_rhash_type
family 2 __set0 t 3 size 3 backend nft_set_hash_fast_type count 3
family 2 __set1 t 3 size 2 backend nft_set_bitmap_type count 2
[..]

Signed-off-by: Florian Westphal <fw@strlen.de>
include/libnftnl/set.h
include/linux/netfilter/nf_tables.h
include/set.h
src/set.c

index e2e5795aa9b40b368fd80718893a55117eb83d0b..cad5e8e81c7abdbe4efacf0d7c4988ca4f655682 100644 (file)
@@ -32,6 +32,7 @@ enum nftnl_set_attr {
        NFTNL_SET_DESC_CONCAT,
        NFTNL_SET_EXPR,
        NFTNL_SET_EXPRESSIONS,
+       NFTNL_SET_COUNT,
        __NFTNL_SET_MAX
 };
 #define NFTNL_SET_MAX (__NFTNL_SET_MAX - 1)
index 49c944e78463f3039c59848af4b1599125956485..7d6bc19a0153f3f0c522405ddd8482b82a3fc5c0 100644 (file)
@@ -394,6 +394,8 @@ enum nft_set_field_attributes {
  * @NFTA_SET_HANDLE: set handle (NLA_U64)
  * @NFTA_SET_EXPR: set expression (NLA_NESTED: nft_expr_attributes)
  * @NFTA_SET_EXPRESSIONS: list of expressions (NLA_NESTED: nft_list_attributes)
+ * @NFTA_SET_TYPE: set backend type (NLA_STRING)
+ * @NFTA_SET_COUNT: number of set elements (NLA_U32)
  */
 enum nft_set_attributes {
        NFTA_SET_UNSPEC,
@@ -415,6 +417,8 @@ enum nft_set_attributes {
        NFTA_SET_HANDLE,
        NFTA_SET_EXPR,
        NFTA_SET_EXPRESSIONS,
+       NFTA_SET_TYPE,
+       NFTA_SET_COUNT,
        __NFTA_SET_MAX
 };
 #define NFTA_SET_MAX           (__NFTA_SET_MAX - 1)
index 55018b6b9ba9538ae1207d1408508ebd1f003d92..bda80685590dcff20a6f4398654394ec9031864b 100644 (file)
@@ -11,6 +11,7 @@ struct nftnl_set {
        uint32_t                set_flags;
        const char              *table;
        const char              *name;
+       const char              *type;
        uint64_t                handle;
        uint32_t                key_type;
        uint32_t                key_len;
@@ -31,6 +32,7 @@ struct nftnl_set {
        struct list_head        element_list;
 
        uint32_t                flags;
+       uint32_t                elemcount;
        uint32_t                gc_interval;
        uint64_t                timeout;
        struct list_head        expr_list;
index a0208441f31ecda9811e172d58616f18a76cb8fa..412bdac627d3123aeac34396bf54257a3d5ce64a 100644 (file)
--- a/src/set.c
+++ b/src/set.c
@@ -59,6 +59,8 @@ void nftnl_set_free(const struct nftnl_set *s)
                list_del(&elem->head);
                nftnl_set_elem_free(elem);
        }
+
+       xfree(s->type);
        xfree(s);
 }
 
@@ -97,6 +99,7 @@ void nftnl_set_unset(struct nftnl_set *s, uint16_t attr)
        case NFTNL_SET_DESC_CONCAT:
        case NFTNL_SET_TIMEOUT:
        case NFTNL_SET_GC_INTERVAL:
+       case NFTNL_SET_COUNT:
                break;
        case NFTNL_SET_USERDATA:
                xfree(s->user.data);
@@ -129,6 +132,7 @@ static uint32_t nftnl_set_validate[NFTNL_SET_MAX + 1] = {
        [NFTNL_SET_DESC_SIZE]   = sizeof(uint32_t),
        [NFTNL_SET_TIMEOUT]             = sizeof(uint64_t),
        [NFTNL_SET_GC_INTERVAL] = sizeof(uint32_t),
+       [NFTNL_SET_COUNT]       = sizeof(uint32_t),
 };
 
 EXPORT_SYMBOL(nftnl_set_set_data);
@@ -198,6 +202,9 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
        case NFTNL_SET_GC_INTERVAL:
                memcpy(&s->gc_interval, data, sizeof(s->gc_interval));
                break;
+       case NFTNL_SET_COUNT:
+               memcpy(&s->elemcount, data, sizeof(s->elemcount));
+               break;
        case NFTNL_SET_USERDATA:
                if (s->flags & (1 << NFTNL_SET_USERDATA))
                        xfree(s->user.data);
@@ -304,6 +311,9 @@ const void *nftnl_set_get_data(const struct nftnl_set *s, uint16_t attr,
        case NFTNL_SET_GC_INTERVAL:
                *data_len = sizeof(uint32_t);
                return &s->gc_interval;
+       case NFTNL_SET_COUNT:
+               *data_len = sizeof(uint32_t);
+               return &s->elemcount;
        case NFTNL_SET_USERDATA:
                *data_len = s->user.len;
                return s->user.data;
@@ -381,6 +391,8 @@ struct nftnl_set *nftnl_set_clone(const struct nftnl_set *set)
                list_add_tail(&newelem->head, &newset->element_list);
        }
 
+       newset->type = NULL;
+
        return newset;
 err:
        nftnl_set_free(newset);
@@ -523,6 +535,7 @@ static int nftnl_set_parse_attr_cb(const struct nlattr *attr, void *data)
        switch(type) {
        case NFTA_SET_TABLE:
        case NFTA_SET_NAME:
+       case NFTA_SET_TYPE:
                if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
                        abi_breakage();
                break;
@@ -538,6 +551,7 @@ static int nftnl_set_parse_attr_cb(const struct nlattr *attr, void *data)
        case NFTA_SET_ID:
        case NFTA_SET_POLICY:
        case NFTA_SET_GC_INTERVAL:
+       case NFTA_SET_COUNT:
                if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
                        abi_breakage();
                break;
@@ -738,6 +752,16 @@ int nftnl_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s)
                s->flags |= (1 << NFTNL_SET_EXPRESSIONS);
        }
 
+       if (tb[NFTA_SET_TYPE]) {
+               xfree(s->type);
+               s->type = strdup(mnl_attr_get_str(tb[NFTA_SET_TYPE]));
+       }
+
+       if (tb[NFTA_SET_COUNT]) {
+               s->elemcount = ntohl(mnl_attr_get_u32(tb[NFTA_SET_COUNT]));
+               s->flags |= (1 << NFTNL_SET_COUNT);
+       }
+
        s->family = nfg->nfgen_family;
        s->flags |= (1 << NFTNL_SET_FAMILY);
 
@@ -802,6 +826,16 @@ static int nftnl_set_snprintf_default(char *buf, size_t remain,
                SNPRINTF_BUFFER_SIZE(ret, remain, offset);
        }
 
+       if (s->type) {
+               ret = snprintf(buf + offset, remain, " backend %s", s->type);
+               SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+       }
+
+       if (s->elemcount) {
+               ret = snprintf(buf + offset, remain, " count %u", s->elemcount);
+               SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+       }
+
        /* Empty set? Skip printinf of elements */
        if (list_empty(&s->element_list))
                return offset;