]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
datatype: Describe rt symbol tables
authorPhil Sutter <phil@nwl.cc>
Fri, 22 Dec 2023 16:00:44 +0000 (17:00 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 2 Jan 2024 17:29:51 +0000 (18:29 +0100)
Implement a symbol_table_print() wrapper for the run-time populated
rt_symbol_tables which formats output similar to expr_describe() and
includes the data source.

Since these tables reside in struct output_ctx there is no implicit
connection between data type and therefore providing callbacks for
relevant datat types which feed the data into said wrapper is a simpler
solution than extending expr_describe() itself.

Signed-off-by: Phil Sutter <phil@nwl.cc>
include/datatype.h
src/ct.c
src/datatype.c
src/meta.c
src/rt.c

index 09a7894567e4d34312bc980a1ac91926498fb58f..c4d6282d6f591b097be0de8172d77e837ddb7fe3 100644 (file)
@@ -252,6 +252,9 @@ extern void symbol_table_print(const struct symbol_table *tbl,
 
 extern struct symbol_table *rt_symbol_table_init(const char *filename);
 extern void rt_symbol_table_free(const struct symbol_table *tbl);
+extern void rt_symbol_table_describe(struct output_ctx *octx, const char *name,
+                                    const struct symbol_table *tbl,
+                                    const struct datatype *type);
 
 extern const struct datatype invalid_type;
 extern const struct datatype verdict_type;
index ebfd90a1ab0d3f56b0562fa47e15ef3cf6e8396f..6793464859cade676ec457ca79a792a49c8129ad 100644 (file)
--- a/src/ct.c
+++ b/src/ct.c
@@ -216,10 +216,17 @@ static struct error_record *ct_label_type_parse(struct parse_ctx *ctx,
        return NULL;
 }
 
+static void ct_label_type_describe(struct output_ctx *octx)
+{
+       rt_symbol_table_describe(octx, CONNLABEL_CONF,
+                                octx->tbl.ct_label, &ct_label_type);
+}
+
 const struct datatype ct_label_type = {
        .type           = TYPE_CT_LABEL,
        .name           = "ct_label",
        .desc           = "conntrack label",
+       .describe       = ct_label_type_describe,
        .byteorder      = BYTEORDER_HOST_ENDIAN,
        .size           = CT_LABEL_BIT_SIZE,
        .basetype       = &bitmask_type,
index 4d867798222be1b74834b0a2774c5d052ea7d226..3b19ae8ef52d8d27546f65042fc2cec17c2dda35 100644 (file)
@@ -946,6 +946,33 @@ void rt_symbol_table_free(const struct symbol_table *tbl)
        free_const(tbl);
 }
 
+void rt_symbol_table_describe(struct output_ctx *octx, const char *name,
+                             const struct symbol_table *tbl,
+                             const struct datatype *type)
+{
+       char *path = NULL;
+       FILE *f;
+
+       if (!tbl || !tbl->symbols[0].identifier)
+               return;
+
+       f = open_iproute2_db(name, &path);
+       if (f)
+               fclose(f);
+       if (!path && asprintf(&path, "%s%s",
+                             name[0] == '/' ? "" : "unknown location of ",
+                             name) < 0)
+               return;
+
+       nft_print(octx, "\npre-defined symbolic constants from %s ", path);
+       if (tbl->base == BASE_DECIMAL)
+               nft_print(octx, "(in decimal):\n");
+       else
+               nft_print(octx, "(in hexadecimal):\n");
+       symbol_table_print(tbl, type, type->byteorder, octx);
+       free(path);
+}
+
 void mark_table_init(struct nft_ctx *ctx)
 {
        ctx->output.tbl.mark = rt_symbol_table_init("rt_marks");
@@ -968,10 +995,17 @@ static struct error_record *mark_type_parse(struct parse_ctx *ctx,
        return symbolic_constant_parse(ctx, sym, ctx->tbl->mark, res);
 }
 
+static void mark_type_describe(struct output_ctx *octx)
+{
+       rt_symbol_table_describe(octx, "rt_marks",
+                                octx->tbl.mark, &mark_type);
+}
+
 const struct datatype mark_type = {
        .type           = TYPE_MARK,
        .name           = "mark",
        .desc           = "packet mark",
+       .describe       = mark_type_describe,
        .size           = 4 * BITS_PER_BYTE,
        .byteorder      = BYTEORDER_HOST_ENDIAN,
        .basetype       = &integer_type,
index 6f76f0033a630279cc20b3fe4f5dbd6c310f87e1..eca8dac72098a9939e89da56edf188a583b665a5 100644 (file)
@@ -346,10 +346,17 @@ static struct error_record *devgroup_type_parse(struct parse_ctx *ctx,
        return symbolic_constant_parse(ctx, sym, ctx->tbl->devgroup, res);
 }
 
+static void devgroup_type_describe(struct output_ctx *octx)
+{
+       rt_symbol_table_describe(octx, "group",
+                                octx->tbl.devgroup, &devgroup_type);
+}
+
 const struct datatype devgroup_type = {
        .type           = TYPE_DEVGROUP,
        .name           = "devgroup",
        .desc           = "devgroup name",
+       .describe       = devgroup_type_describe,
        .byteorder      = BYTEORDER_HOST_ENDIAN,
        .size           = 4 * BITS_PER_BYTE,
        .basetype       = &integer_type,
index 3ee710ddc05b59e33a965184582e5f0137d5abec..d8f3352f4b4a72c86097c1420e11546a8b3acc5f 100644 (file)
--- a/src/rt.c
+++ b/src/rt.c
@@ -45,10 +45,17 @@ static struct error_record *realm_type_parse(struct parse_ctx *ctx,
        return symbolic_constant_parse(ctx, sym, ctx->tbl->realm, res);
 }
 
+static void realm_type_describe(struct output_ctx *octx)
+{
+       rt_symbol_table_describe(octx, "rt_realms",
+                                octx->tbl.realm, &realm_type);
+}
+
 const struct datatype realm_type = {
        .type           = TYPE_REALM,
        .name           = "realm",
        .desc           = "routing realm",
+       .describe       = realm_type_describe,
        .byteorder      = BYTEORDER_HOST_ENDIAN,
        .size           = 4 * BITS_PER_BYTE,
        .basetype       = &integer_type,