Skip to contentSkip to conversation

json: implement json() hook for "symbol_expr_ops"/"variabl_expr_ops"

PATCH In review Thomas Haller 3 participants 15 messages Nov 2, 2023 – Nov 3, 2023 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 json: implement json() hook for "symbol_expr_ops"/"variabl_expr_ops"

The ultimate goal is that all "struct expr_ops" have a "json()" hook set.

It's also faster, to just create the JSON node, instead of creating a memory stream, write there using print only to get the sting.

---
The patches 1/2 and 2/2 replaces

Subject: [PATCH nft 2/7] json: drop messages "warning: stmt ops chain have no json callback"
Date: Tue, 31 Oct 2023 19:53:28 +0100

 include/json.h   |  4 ++++
 src/expression.c |  2 ++
 src/json.c       | 10 ++++++++++
 3 files changed, 16 insertions(+)
include/json.h +4 -0
index 39be8928e8ee..134e503afe54 100644
@@ -49,7 +49,9 @@ json_t *rt_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *numgen_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *hash_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *symbol_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *variable_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *xfrm_expr_json(const struct expr *expr, struct output_ctx *octx);
@@ -156,7 +158,9 @@ EXPR_PRINT_STUB(set_elem_catchall_expr)
 EXPR_PRINT_STUB(numgen_expr)
 EXPR_PRINT_STUB(hash_expr)
 EXPR_PRINT_STUB(fib_expr)
+EXPR_PRINT_STUB(symbol_expr)
 EXPR_PRINT_STUB(constant_expr)
+EXPR_PRINT_STUB(variable_expr)
 EXPR_PRINT_STUB(socket_expr)
 EXPR_PRINT_STUB(osf_expr)
 EXPR_PRINT_STUB(xfrm_expr)
src/expression.c +2 -0
index a21dfec25722..d6a2e69db572 100644
@@ -321,6 +321,7 @@ static const struct expr_ops symbol_expr_ops = {
 	.type		= EXPR_SYMBOL,
 	.name		= "symbol",
 	.print		= symbol_expr_print,
+	.json           = symbol_expr_json,
 	.clone		= symbol_expr_clone,
 	.destroy	= symbol_expr_destroy,
 };
@@ -362,6 +363,7 @@ static const struct expr_ops variable_expr_ops = {
 	.type		= EXPR_VARIABLE,
 	.name		= "variable",
 	.print		= variable_expr_print,
+	.json		= variable_expr_json,
 	.clone		= variable_expr_clone,
 	.destroy	= variable_expr_destroy,
 };
src/json.c +10 -0
index 068c423addc7..0fd78b763af7 100644
@@ -982,11 +982,21 @@ static json_t *datatype_json(const struct expr *expr, struct output_ctx *octx)
 	    expr->dtype->name);
 }
 
+json_t *symbol_expr_json(const struct expr *expr, struct output_ctx *octx)
+{
+	return json_string(expr->identifier);
+}
+
 json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx)
 {
 	return datatype_json(expr, octx);
 }
 
+json_t *variable_expr_json(const struct expr *expr, struct output_ctx *octx)
+{
+	return json_string(expr->sym->identifier);
+}
+
 json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx)
 {
 	return json_pack("{s:{s:s}}", "socket", "key",
Signature · 1 line
2.41.0
#1 lore raw

PATCH 2/2 json: drop handling missing json() hook for "struct expr_ops"

By now, all "struct expr_ops" have a json() hook set. Thus, drop handling the possibility that they might not. From now on, it's a bug to create a ops without the hook.

It's not clear what the code tried to do. It bothered to implement a fallback via fmemopen(), but apparently that fallback is no considered a good solution as it also printed a "warning". Either the fallback is good and does not warrant a warning. Or the condition is to be avoided to begin with, which we should do by testing the expr_ops structures.

As the fallback path has an overhead to create the memory stream, the fallback path is indeed not great. That is the reason to outlaw a missing json() hook, to require that all hooks are present, and to drop the fallback path.

A missing hook is very easy to cover with unit tests. Such a test shall be added soon.

---
 src/json.c | 40 ++--------------------------------------
 1 file changed, 2 insertions(+), 38 deletions(-)
src/json.c +2 -38
index 0fd78b763af7..cbd2abbc8b52 100644
@@ -44,26 +44,7 @@
 
 static json_t *expr_print_json(const struct expr *expr, struct output_ctx *octx)
 {
-	const struct expr_ops *ops;
-	char buf[1024];
-	FILE *fp;
-
-	ops = expr_ops(expr);
-	if (ops->json)
-		return ops->json(expr, octx);
-
-	fprintf(stderr, "warning: expr ops %s have no json callback\n",
-		expr_name(expr));
-
-	fp = octx->output_fp;
-	octx->output_fp = fmemopen(buf, 1024, "w");
-
-	ops->print(expr, octx);
-
-	fclose(octx->output_fp);
-	octx->output_fp = fp;
-
-	return json_pack("s", buf);
+	return expr_ops(expr)->json(expr, octx);
 }
 
 static json_t *set_dtype_json(const struct expr *key)
@@ -89,24 +70,7 @@ static json_t *set_dtype_json(const struct expr *key)
 
 static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
 {
-	char buf[1024];
-	FILE *fp;
-
-	if (stmt->ops->json)
-		return stmt->ops->json(stmt, octx);
-
-	fprintf(stderr, "warning: stmt ops %s have no json callback\n",
-		stmt->ops->name);
-
-	fp = octx->output_fp;
-	octx->output_fp = fmemopen(buf, 1024, "w");
-
-	stmt->ops->print(stmt, octx);
-
-	fclose(octx->output_fp);
-	octx->output_fp = fp;
-
-	return json_pack("s", buf);
+	return stmt->ops->json(stmt, octx);
 }
 
 static json_t *set_stmt_list_json(const struct list_head *stmt_list,
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 10 lines · Thomas Haller

By now, all "struct expr_ops" have a json() hook set. Thus, drop handling the possibility that they might not. From now on, it's a bug to create a ops without the hook.

It's not clear what the code tried to do. It bothered to implement a fallback via fmemopen(), but apparently that fallback is no considered a good solution as it also printed a "warning". Either the fallback is good and does not warrant a warning. Or the condition is to be avoided to begin with, which we should do by testing the expr_ops structures.

Quoted 7 lines

As the fallback path has an overhead to create the memory stream, the fallback path is indeed not great. That is the reason to outlaw a missing json() hook, to require that all hooks are present, and to drop the fallback path.

A missing hook is very easy to cover with unit tests. Such a test shall be added soon.

That's fine to simplify code.

But then, in 1/2 you better set some STUB that hits BUG() because we should not ever see variable and symbol expression from json listing path ever.

#3 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 28 lines · Pablo Neira Ayuso
Quoted 23 lines · Thomas Haller

By now, all "struct expr_ops" have a json() hook set. Thus, drop handling the possibility that they might not. From now on, it's a bug
to create a ops without the hook.

It's not clear what the code tried to do. It bothered to implement a
fallback via fmemopen(), but apparently that fallback is no considered a
good solution as it also printed a "warning". Either the fallback is
good and does not warrant a warning. Or the condition is to be avoided
to begin with, which we should do by testing the expr_ops structures.

As the fallback path has an overhead to create the memory stream, the
fallback path is indeed not great. That is the reason to outlaw a missing json() hook, to require that all hooks are present, and to drop
the fallback path.

Quoted 3 lines

A missing hook is very easy to cover with unit tests. Such a test shall
be added soon.

Quoted 6 lines

That's fine to simplify code.

But then, in 1/2 you better set some STUB that hits BUG() because we should not ever see variable and symbol expression from json listing path ever.

I think BUG() would not work. This does happen, as the tests in patches

Subject: [PATCH nft 0/7] add and check dump files for JSON in tests/shell
Date: Tue, 31 Oct 2023 19:53:26 +0100

expose.

When you apply the patchset (without patch 2/7) you can get those failures easily.

Thomas

#4 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 36 lines · Thomas Haller
Quoted 28 lines · Pablo Neira Ayuso
Quoted 23 lines · Thomas Haller

By now, all "struct expr_ops" have a json() hook set. Thus, drop handling the possibility that they might not. From now on, it's a bug
to create a ops without the hook.

It's not clear what the code tried to do. It bothered to implement a
fallback via fmemopen(), but apparently that fallback is no considered a
good solution as it also printed a "warning". Either the fallback is
good and does not warrant a warning. Or the condition is to be avoided
to begin with, which we should do by testing the expr_ops structures.

As the fallback path has an overhead to create the memory stream, the
fallback path is indeed not great. That is the reason to outlaw a missing json() hook, to require that all hooks are present, and to drop
the fallback path.

Quoted 3 lines

A missing hook is very easy to cover with unit tests. Such a test shall
be added soon.

Quoted 6 lines

That's fine to simplify code.

But then, in 1/2 you better set some STUB that hits BUG() because we should not ever see variable and symbol expression from json listing path ever.

Quoted 6 lines

I think BUG() would not work. This does happen, as the tests in patches

Subject: [PATCH nft 0/7] add and check dump files for JSON in tests/shell
Date: Tue, 31 Oct 2023 19:53:26 +0100

expose.

No listing from the kernel would use the variable expression.

What example would be triggering bug?

#5 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Thomas Haller

I think BUG() would not work. This does happen, as the tests in patches

Subject: [PATCH nft 0/7] add and check dump files for JSON
in tests/shell
Date: Tue, 31 Oct 2023 19:53:26 +0100

Quoted 5 lines
Quoted 1 line

expose.

No listing from the kernel would use the variable expression.

What example would be triggering bug?

when you apply above patchset (and revert patch 2/7), and:

    $ make
    $ ./tests/shell/run-tests.sh
    ...
    $ grep '^[^ ]*W[^ ]*:' /tmp/nft-test.latest.*/test.log
    W: [CHK DUMP]     8/381 tests/shell/testcases/chains/0041chain_binding_0  
    W: [CHK DUMP]    66/381 tests/shell/testcases/cache/0010_implicit_chain_0
    W: [CHK DUMP]   226/381 tests/shell/testcases/nft-f/sample-ruleset
    $ grep -R 'Command `./tests/shell/../../src/nft -j list ruleset""` failed' /tmp/nft-test.latest.*/
    ...

Gives:

tests/shell/testcases/nft-f/sample-ruleset
tests/shell/testcases/cache/0010_implicit_chain_0
tests/shell/testcases/chains/0041chain_binding_0

For example:

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:Command ./tests/shell/../../src/nft -j list ruleset"" failed /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:>>>> /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:warning: stmt ops chain have no json callback /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:warning: stmt ops chain have no json callback /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:<<<<

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

Using Fedora kernel 6.5.6-300.fc39.x86_64.

Thomas

#6 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 21 lines · Thomas Haller
Quoted 1 line · Pablo Neira Ayuso

What example would be triggering bug?

when you apply above patchset (and revert patch 2/7), and:

    $ make
    $ ./tests/shell/run-tests.sh
    ...
    $ grep '^[^ ]*W[^ ]*:' /tmp/nft-test.latest.*/test.log
    W: [CHK DUMP]     8/381 tests/shell/testcases/chains/0041chain_binding_0  
    W: [CHK DUMP]    66/381 tests/shell/testcases/cache/0010_implicit_chain_0
    W: [CHK DUMP]   226/381 tests/shell/testcases/nft-f/sample-ruleset
    $ grep -R 'Command `./tests/shell/../../src/nft -j list ruleset""` failed' /tmp/nft-test.latest.*/
    ...

Gives:

tests/shell/testcases/nft-f/sample-ruleset
tests/shell/testcases/cache/0010_implicit_chain_0
tests/shell/testcases/chains/0041chain_binding_0

Quoted 6 lines

For example:

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:Command ./tests/shell/../../src/nft -j list ruleset"" failed /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:>>>> /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:warning: stmt ops chain have no json callback /tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:warning: stmt ops chain have no json callback

Yes, chain statement is lacking a json output, that is correct, that needs to be done.

But, as for variable and symbol expressions, I do not see how those can be found in the 'list ruleset' path. Note that symbol expressions represent a preliminary state of the expression, these type of expressions go away after evaluation. Same thing applies to variable expression. They have no use for listing path.

Do you have tests that explicitly refer to the lack of json callback for variable and symbol expressions just like in the warning above?

Quoted 5 lines

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains-0041chain_binding_0.4/rc-failed-chkdump:<<<<

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

What does 'unstable' mean in this case?

#7 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 5 lines · Pablo Neira Ayuso

On Thu, Nov 02, 2023 at 05:17:56PM +0100, Thomas Haller wrote:

Yes, chain statement is lacking a json output, that is correct, that needs to be done.

What is the correct JSON syntax for printing a chain?

For example, for test "tests/shell/testcases/nft-f/sample-ruleset" I get the following from nft -j list ruleset:

    [...]
    {
      "rule": {
        "family": "inet",
        "table": "filter",
        "chain": "home_input",
        "handle": 91,
        "expr": [
          {
            "match": {
              "op": "==",
              "left": {
                "meta": {
                  "key": "l4proto"
                }
              },
              "right": {
                "set": [
                  "tcp",
                  "udp"
                ]
              }
            }
          },
          {
            "match": {
              "op": "==",
              "left": {
                "payload": {
                  "protocol": "th",
                  "field": "dport"
                }
              },
              "right": 53
            }
          },
          "jump {\n\t\t\tip6 saddr != { fd00::/8, fe80::/64 } counter packets 0 bytes 0 reject with icmpv6 port-unreachable\n\t\t\taccept\n\t\t}"
        ]
      }
    },
    [...]

In man libnftables-json, searching for "jump" only gives:

    { "jump": { "target": * STRING *}}

Is there an example how this JSON output should look like?

(or a test, after all, I want to feed this output back into nft -j --check -f -).

Quoted 5 lines

But, as for variable and symbol expressions, I do not see how those can be found in the 'list ruleset' path. Note that symbol expressions represent a preliminary state of the expression, these type of expressions go away after evaluation. Same thing applies to variable expression. They have no use for listing path.

ACK about symbol_expr_ops + variable_expr_ops. I will send a minor patch about that (essentially with code comments and remove the elaborate fallback code).

Quoted 4 lines

Do you have tests that explicitly refer to the lack of json callback for variable and symbol expressions just like in the warning above?

Quoted 9 lines
Quoted 6 lines

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains- 0041chain_binding_0.4/rc-failed-chkdump:<<<<

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

What does 'unstable' mean in this case?

It seems, that the order of the elements of the list is unstable. I didn't investigate. At this point, I only want to add the .json-nft files for tests that pass, and worry about the remaining issues after the basic test infrastructure about .json-nft tests is up.

Thomas

#8 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Hi Thomas,

Quoted 8 lines · Thomas Haller
Quoted 5 lines · Pablo Neira Ayuso

On Thu, Nov 02, 2023 at 05:17:56PM +0100, Thomas Haller wrote:

Yes, chain statement is lacking a json output, that is correct, that needs to be done.

What is the correct JSON syntax for printing a chain?

There is currently no syntax, so this needs to be defined.

Quoted 46 lines

For example, for test "tests/shell/testcases/nft-f/sample-ruleset" I get the following from nft -j list ruleset:

    [...]
    {
      "rule": {
        "family": "inet",
        "table": "filter",
        "chain": "home_input",
        "handle": 91,
        "expr": [
          {
            "match": {
              "op": "==",
              "left": {
                "meta": {
                  "key": "l4proto"
                }
              },
              "right": {
                "set": [
                  "tcp",
                  "udp"
                ]
              }
            }
          },
          {
            "match": {
              "op": "==",
              "left": {
                "payload": {
                  "protocol": "th",
                  "field": "dport"
                }
              },
              "right": 53
            }
          },
          "jump {\n\t\t\tip6 saddr != { fd00::/8, fe80::/64 } counter packets 0 bytes 0 reject with icmpv6 port-unreachable\n\t\t\taccept\n\t\t}"
        ]
      }
    },
    [...]
Quoted 8 lines

In man libnftables-json, searching for "jump" only gives:

    { "jump": { "target": * STRING *}}

Is there an example how this JSON output should look like?

(or a test, after all, I want to feed this output back into nft -j --check -f -).

Maybe something like:

     { "jump": { "chain" : [ rules here ] }

but I would need to sketch some code to explore how complicate this is to reuse existing JSON code.

Quoted 9 lines
Quoted 5 lines

But, as for variable and symbol expressions, I do not see how those can be found in the 'list ruleset' path. Note that symbol expressions represent a preliminary state of the expression, these type of expressions go away after evaluation. Same thing applies to variable expression. They have no use for listing path.

ACK about symbol_expr_ops + variable_expr_ops. I will send a minor patch about that (essentially with code comments and remove the elaborate fallback code).

OK, so it is chain statement that is missing the json callback.

Quoted 6 lines
Quoted 6 lines

Do you have tests that explicitly refer to the lack of json callback for variable and symbol expressions just like in the warning above?

Quoted 3 lines

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains- 0041chain_binding_0.4/rc-failed-chkdump:<<<<

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

What does 'unstable' mean in this case?

It seems, that the order of the elements of the list is unstable.

Ah, I see, so it is not easy to compare. Thanks for explaining.

Quoted 4 lines

I didn't investigate. At this point, I only want to add the .json-nft files for tests that pass, and worry about the remaining issues after the basic test infrastructure about .json-nft tests is up.

maintainer
#9 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 10 lines · Pablo Neira Ayuso
Quoted 10 lines
Quoted 10 lines

Do you have tests that explicitly refer to the lack of json callback for variable and symbol expressions just like in the warning above?

Quoted 6 lines

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains- 0041chain_binding_0.4/rc-failed-chkdump:<<<<

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What does 'unstable' mean in this case?

It seems, that the order of the elements of the list is unstable.

Ah, I see, so it is not easy to compare. Thanks for explaining.

If its really just that the element ordering is random, then I think we should change libnftables to sort before printing, it should not be too much work/code.

#10 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 13 lines · Florian Westphal
Quoted 12 lines · Pablo Neira Ayuso
Quoted 12 lines
Quoted 11 lines

Do you have tests that explicitly refer to the lack of json callback for variable and symbol expressions just like in the warning above?

Quoted 6 lines

/tmp/nft-test.latest.thom/test-tests-shell-testcases-chains- 0041chain_binding_0.4/rc-failed-chkdump:<<<<

There are also other failures. e.g. tests/shell/testcases/parsing/large_rule_pipe does not give stable output. I need to drop that .json-nft file in v2.

What does 'unstable' mean in this case?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

It seems, that the order of the elements of the list is unstable.

Ah, I see, so it is not easy to compare. Thanks for explaining.

If its really just that the element ordering is random, then I think we should change libnftables to sort before printing, it should not be too much work/code.

There is a sort function that is already used when listing elements IIRC.

Maybe this is missing the json codepath?

maintainer
#11 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

In reply to Thomas Haller
Quoted 1 line · Thomas Haller

What is the correct JSON syntax for printing a chain?

Thats the problem, the chain has no user-visible name :-)

Its a shortcut syntax alias. Essentially, this:

meta l4proto { tcp, udp } th dport domain jump {
ip6 saddr != $private_ip6 counter reject
accept
}

is the same as
chain $RANDOM_NAME {
ip6 saddr != $private_ip6 counter reject
accept
}
meta l4proto { tcp, udp } th dport domain jump $RANDOM_NAME

Except that, if you remove the rule, then $RANDOM_NAME chain is deleted as well and that $RANDOM_NAME is readonly after creation (you cannot add or remove rules from it).

Quoted 46 lines

For example, for test "tests/shell/testcases/nft-f/sample-ruleset" I get the following from nft -j list ruleset:

    [...]
    {
      "rule": {
        "family": "inet",
        "table": "filter",
        "chain": "home_input",
        "handle": 91,
        "expr": [
          {
            "match": {
              "op": "==",
              "left": {
                "meta": {
                  "key": "l4proto"
                }
              },
              "right": {
                "set": [
                  "tcp",
                  "udp"
                ]
              }
            }
          },
          {
            "match": {
              "op": "==",
              "left": {
                "payload": {
                  "protocol": "th",
                  "field": "dport"
                }
              },
              "right": 53
            }
          },
          "jump {\n\t\t\tip6 saddr != { fd00::/8, fe80::/64 } counter packets 0 bytes 0 reject with icmpv6 port-unreachable\n\t\t\taccept\n\t\t}"
        ]
      }
    },
    [...]
Quoted 5 lines

In man libnftables-json, searching for "jump" only gives:

    { "jump": { "target": * STRING *}}

Is there an example how this JSON output should look like?

We need to define a new syntax for this case.
I think it would be best to recurse, i.e. something like:

# i.e., make it clear that this is a jump ("left" :) and that the target is a complete, anonymous chain with 0 or more rules embedded within.

},

 "jump" : {"chain" : { "rule" : {
    "family" : "inet",
    "table": "t", "chain": "c",
    "handle": 5,
    "expr":
       [{"match": {"op": "!=", "left": {
      "payload": {
      "protocol": "ip6",
      "field": "saddr"}},
       "right": {
          "set":
	      [{"prefix": {"addr": "fd00::", "len": 8}}, { ....
#12 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

In reply to Thomas Haller
Quoted 25 lines · Thomas Haller


 static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
 {

src/json.c +0 -7
-	char buf[1024];
-	FILE *fp;
-
-	if (stmt->ops->json)
-		return stmt->ops->json(stmt, octx);
-
-	fprintf(stderr, "warning: stmt ops %s have no json

callback\n",

src/json.c +1 -11
-		stmt->ops->name);
-
-	fp = octx->output_fp;
-	octx->output_fp = fmemopen(buf, 1024, "w");
-
-	stmt->ops->print(stmt, octx);
-
-	fclose(octx->output_fp);
-	octx->output_fp = fp;
-
-	return json_pack("s", buf);
+	return stmt->ops->json(stmt, octx);
Quoted 1 line

 }

Regardless the ongoing discussion...

This part of the patch is wrong. Must not be applied.

stmt->ops is a "struct stmt_ops", which still has NULL "json" callbacks. The patch 1/1 only addresses "json" hooks in "struct expr_ops".

Thomas

#13 lore raw

Re: [PATCH nft 2/2] json: drop handling missing json() hook for "struct expr_ops"

Quoted 7 lines · Thomas Haller

This part of the patch is wrong. Must not be applied.

stmt->ops is a "struct stmt_ops", which still has NULL "json" callbacks. The patch 1/1 only addresses "json" hooks in "struct expr_ops".

The NULL pointers in json() hooks are:

in struct expr_ops:

- symbol_expr_ops
- variabl_expr_ops

in struct stmt_ops:

- chain_stmt_ops

The two patches lack handling chain_stmt_ops.json.

Thomas

#14 lore raw
In reply to Thomas Haller
Quoted 23 lines · Thomas Haller

The ultimate goal is that all "struct expr_ops" have a "json()" hook set.

It's also faster, to just create the JSON node, instead of creating a memory stream, write there using print only to get the sting.

---
The patches 1/2 and 2/2 replaces

Subject: [PATCH nft 2/7] json: drop messages "warning: stmt ops chain have no json callback"
Date: Tue, 31 Oct 2023 19:53:28 +0100

 include/json.h   |  4 ++++
 src/expression.c |  2 ++
 src/json.c       | 10 ++++++++++
 3 files changed, 16 insertions(+)
include/json.h +0 -0
index 39be8928e8ee..134e503afe54 100644

include/json.h json_t *rt_expr_json(const struct expr *expr, struct output_ctx *octx); · line 54

Quoted 6 lines
include/json.h +2 -0
@@ -49,7 +49,9 @@ json_t *rt_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *numgen_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *hash_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *symbol_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *variable_expr_json(const struct expr *expr, struct output_ctx *octx);

Makes no sense.

Are these expression really triggering specifically errors? I don't think so.

This expressions are consumed, ie. the get transformed into a value_expr, but never use in an output path.