]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
json: add flow statement json export + parser
authorFlorian Westphal <fw@strlen.de>
Mon, 7 Feb 2022 12:03:20 +0000 (13:03 +0100)
committerFlorian Westphal <fw@strlen.de>
Mon, 7 Feb 2022 13:56:26 +0000 (14:56 +0100)
flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

Signed-off-by: Florian Westphal <fw@strlen.de>
include/json.h
src/ct.c
src/json.c
src/parser_json.c

index 3db9f2782d11ac6767e967c3fcf7cc4506bd53a7..a753f359aa52791d1c37cd8ac2193f37a91c86f5 100644 (file)
@@ -69,6 +69,7 @@ json_t *uid_type_json(const struct expr *expr, struct output_ctx *octx);
 json_t *gid_type_json(const struct expr *expr, struct output_ctx *octx);
 
 json_t *expr_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
+json_t *flow_offload_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *payload_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *exthdr_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *quota_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
@@ -169,6 +170,7 @@ EXPR_PRINT_STUB(uid_type)
 EXPR_PRINT_STUB(gid_type)
 
 STMT_PRINT_STUB(expr)
+STMT_PRINT_STUB(flow_offload)
 STMT_PRINT_STUB(payload)
 STMT_PRINT_STUB(exthdr)
 STMT_PRINT_STUB(quota)
index 6049157198ad9b437648b53cf35e51bebf51a1e4..e246d30392407c917c89a9324466c049a679d992 100644 (file)
--- a/src/ct.c
+++ b/src/ct.c
@@ -578,6 +578,7 @@ static const struct stmt_ops flow_offload_stmt_ops = {
        .name           = "flow_offload",
        .print          = flow_offload_stmt_print,
        .destroy        = flow_offload_stmt_destroy,
+       .json           = flow_offload_stmt_json,
 };
 
 struct stmt *flow_offload_stmt_alloc(const struct location *loc,
index 63b325afc8d1c173f876d255822d68a9075f6fae..4f800c908c6638556e3bf565e8ab9e7da73988b3 100644 (file)
@@ -1122,6 +1122,13 @@ json_t *expr_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
        return expr_print_json(stmt->expr, octx);
 }
 
+json_t *flow_offload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
+{
+       return json_pack("{s:{s:s, s:s+}}", "flow",
+                        "op", "add", "flowtable",
+                        "@", stmt->flow.table_name);
+}
+
 json_t *payload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
 {
        return json_pack("{s: {s:o, s:o}}", "mangle",
index 2fad308f7783b3def8bca7f8162b0b2ba08255f4..f07b798adecd6df109c130cf3db2c941b5c4195e 100644 (file)
@@ -1903,6 +1903,28 @@ out_err:
        return NULL;
 }
 
+static struct stmt *json_parse_flow_offload_stmt(struct json_ctx *ctx,
+                                                const char *key, json_t *value)
+{
+       const char *opstr, *flowtable;
+
+       if (json_unpack_err(ctx, value, "{s:s, s:s}",
+                           "op", &opstr, "flowtable", &flowtable))
+               return NULL;
+
+       if (strcmp(opstr, "add")) {
+               json_error(ctx, "Unknown flow offload statement op '%s'.", opstr);
+               return NULL;
+       }
+
+       if (flowtable[0] != '@') {
+               json_error(ctx, "Illegal flowtable reference in flow offload statement.");
+               return NULL;
+       }
+
+       return flow_offload_stmt_alloc(int_loc, xstrdup(flowtable + 1));
+}
+
 static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx,
                                        const char *key, json_t *value)
 {
@@ -2647,6 +2669,7 @@ static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root)
                { "mangle", json_parse_mangle_stmt },
                { "quota", json_parse_quota_stmt },
                { "limit", json_parse_limit_stmt },
+               { "flow", json_parse_flow_offload_stmt },
                { "fwd", json_parse_fwd_stmt },
                { "notrack", json_parse_notrack_stmt },
                { "dup", json_parse_dup_stmt },